1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "workqueue.h"
34 class General_options
;
37 class Read_relocs_data
;
45 template<int size
, bool big_endian
>
51 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
52 class Output_data_reloc
;
54 // A class to read the relocations for an object file, and then queue
55 // up a task to see if they require any GOT/PLT/COPY relocations in
58 class Read_relocs
: public Task
61 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
62 // unblocked when the Scan_relocs task completes.
63 Read_relocs(const General_options
& options
, Symbol_table
* symtab
,
64 Layout
* layout
, Relobj
* object
, Task_token
* symtab_lock
,
66 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
67 symtab_lock_(symtab_lock
), blocker_(blocker
)
70 // The standard Task methods.
73 is_runnable(Workqueue
*);
82 const General_options
& options_
;
83 Symbol_table
* symtab_
;
86 Task_token
* symtab_lock_
;
90 // Scan the relocations for an object to see if they require any
91 // GOT/PLT/COPY relocations.
93 class Scan_relocs
: public Task
96 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
97 // unblocked when the task completes.
98 Scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
99 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
100 Task_token
* symtab_lock
, Task_token
* blocker
)
101 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
102 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
105 // The standard Task methods.
108 is_runnable(Workqueue
*);
117 class Scan_relocs_locker
;
119 const General_options
& options_
;
120 Symbol_table
* symtab_
;
123 Read_relocs_data
* rd_
;
124 Task_token
* symtab_lock_
;
125 Task_token
* blocker_
;
128 // A class to perform all the relocations for an object file.
130 class Relocate_task
: public Task
133 Relocate_task(const General_options
& options
, const Symbol_table
* symtab
,
134 const Layout
* layout
, Relobj
* object
, Output_file
* of
,
135 Task_token
* input_sections_blocker
,
136 Task_token
* output_sections_blocker
, Task_token
* final_blocker
)
137 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
138 of_(of
), input_sections_blocker_(input_sections_blocker
),
139 output_sections_blocker_(output_sections_blocker
),
140 final_blocker_(final_blocker
)
143 // The standard Task methods.
146 is_runnable(Workqueue
*);
155 class Relocate_locker
;
157 const General_options
& options_
;
158 const Symbol_table
* symtab_
;
159 const Layout
* layout_
;
162 Task_token
* input_sections_blocker_
;
163 Task_token
* output_sections_blocker_
;
164 Task_token
* final_blocker_
;
167 // Standard relocation routines which are used on many targets. Here
168 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
170 template<int size
, bool big_endian
>
171 class Relocate_functions
174 // Do a simple relocation with the addend in the section contents.
175 // VALSIZE is the size of the value.
176 template<int valsize
>
178 rel(unsigned char* view
,
179 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
181 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
182 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
183 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
184 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
187 // Do a simple relocation using a Symbol_value with the addend in
188 // the section contents. VALSIZE is the size of the value to
190 template<int valsize
>
192 rel(unsigned char* view
,
193 const Sized_relobj
<size
, big_endian
>* object
,
194 const Symbol_value
<size
>* psymval
)
196 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
197 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
198 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
199 x
= psymval
->value(object
, x
);
200 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
203 // Do a simple relocation with the addend in the relocation.
204 // VALSIZE is the size of the value.
205 template<int valsize
>
207 rela(unsigned char* view
,
208 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
209 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
211 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
212 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
213 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
);
216 // Do a simple relocation using a symbol value with the addend in
217 // the relocation. VALSIZE is the size of the value.
218 template<int valsize
>
220 rela(unsigned char* view
,
221 const Sized_relobj
<size
, big_endian
>* object
,
222 const Symbol_value
<size
>* psymval
,
223 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
225 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
226 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
227 Valtype x
= psymval
->value(object
, addend
);
228 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
231 // Do a simple PC relative relocation with the addend in the section
232 // contents. VALSIZE is the size of the value.
233 template<int valsize
>
235 pcrel(unsigned char* view
,
236 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
237 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
239 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
240 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
241 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
242 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
245 // Do a simple PC relative relocation with a Symbol_value with the
246 // addend in the section contents. VALSIZE is the size of the
248 template<int valsize
>
250 pcrel(unsigned char* view
,
251 const Sized_relobj
<size
, big_endian
>* object
,
252 const Symbol_value
<size
>* psymval
,
253 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
255 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
256 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
257 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
258 x
= psymval
->value(object
, x
);
259 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
262 // Do a simple PC relative relocation with the addend in the
263 // relocation. VALSIZE is the size of the value.
264 template<int valsize
>
266 pcrela(unsigned char* view
,
267 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
268 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
269 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
271 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
272 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
273 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
276 // Do a simple PC relative relocation with a Symbol_value with the
277 // addend in the relocation. VALSIZE is the size of the value.
278 template<int valsize
>
280 pcrela(unsigned char* view
,
281 const Sized_relobj
<size
, big_endian
>* object
,
282 const Symbol_value
<size
>* psymval
,
283 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
284 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
286 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
287 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
288 Valtype x
= psymval
->value(object
, addend
);
289 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
292 typedef Relocate_functions
<size
, big_endian
> This
;
295 // Do a simple 8-bit REL relocation with the addend in the section
298 rel8(unsigned char* view
, unsigned char value
)
299 { This::template rel
<8>(view
, value
); }
302 rel8(unsigned char* view
,
303 const Sized_relobj
<size
, big_endian
>* object
,
304 const Symbol_value
<size
>* psymval
)
305 { This::template rel
<8>(view
, object
, psymval
); }
307 // Do an 8-bit RELA relocation with the addend in the relocation.
309 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
310 { This::template rela
<8>(view
, value
, addend
); }
313 rela8(unsigned char* view
,
314 const Sized_relobj
<size
, big_endian
>* object
,
315 const Symbol_value
<size
>* psymval
,
316 unsigned char addend
)
317 { This::template rela
<8>(view
, object
, psymval
, addend
); }
319 // Do a simple 8-bit PC relative relocation with the addend in the
322 pcrel8(unsigned char* view
, unsigned char value
,
323 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
324 { This::template pcrel
<8>(view
, value
, address
); }
327 pcrel8(unsigned char* view
,
328 const Sized_relobj
<size
, big_endian
>* object
,
329 const Symbol_value
<size
>* psymval
,
330 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
331 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
333 // Do a simple 8-bit PC relative RELA relocation with the addend in
336 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
337 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
338 { This::template pcrela
<8>(view
, value
, addend
, address
); }
341 pcrela8(unsigned char* view
,
342 const Sized_relobj
<size
, big_endian
>* object
,
343 const Symbol_value
<size
>* psymval
,
344 unsigned char addend
,
345 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
346 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
348 // Do a simple 16-bit REL relocation with the addend in the section
351 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
352 { This::template rel
<16>(view
, value
); }
355 rel16(unsigned char* view
,
356 const Sized_relobj
<size
, big_endian
>* object
,
357 const Symbol_value
<size
>* psymval
)
358 { This::template rel
<16>(view
, object
, psymval
); }
360 // Do an 16-bit RELA relocation with the addend in the relocation.
362 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
363 { This::template rela
<16>(view
, value
, addend
); }
366 rela16(unsigned char* view
,
367 const Sized_relobj
<size
, big_endian
>* object
,
368 const Symbol_value
<size
>* psymval
,
369 elfcpp::Elf_Half addend
)
370 { This::template rela
<16>(view
, object
, psymval
, addend
); }
372 // Do a simple 16-bit PC relative REL relocation with the addend in
373 // the section contents.
375 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
376 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
377 { This::template pcrel
<16>(view
, value
, address
); }
380 pcrel16(unsigned char* view
,
381 const Sized_relobj
<size
, big_endian
>* object
,
382 const Symbol_value
<size
>* psymval
,
383 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
384 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
386 // Do a simple 16-bit PC relative RELA relocation with the addend in
389 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
390 elfcpp::Elf_Half addend
,
391 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
392 { This::template pcrela
<16>(view
, value
, addend
, address
); }
395 pcrela16(unsigned char* view
,
396 const Sized_relobj
<size
, big_endian
>* object
,
397 const Symbol_value
<size
>* psymval
,
398 elfcpp::Elf_Half addend
,
399 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
400 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
402 // Do a simple 32-bit REL relocation with the addend in the section
405 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
406 { This::template rel
<32>(view
, value
); }
409 rel32(unsigned char* view
,
410 const Sized_relobj
<size
, big_endian
>* object
,
411 const Symbol_value
<size
>* psymval
)
412 { This::template rel
<32>(view
, object
, psymval
); }
414 // Do an 32-bit RELA relocation with the addend in the relocation.
416 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
417 { This::template rela
<32>(view
, value
, addend
); }
420 rela32(unsigned char* view
,
421 const Sized_relobj
<size
, big_endian
>* object
,
422 const Symbol_value
<size
>* psymval
,
423 elfcpp::Elf_Word addend
)
424 { This::template rela
<32>(view
, object
, psymval
, addend
); }
426 // Do a simple 32-bit PC relative REL relocation with the addend in
427 // the section contents.
429 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
430 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
431 { This::template pcrel
<32>(view
, value
, address
); }
434 pcrel32(unsigned char* view
,
435 const Sized_relobj
<size
, big_endian
>* object
,
436 const Symbol_value
<size
>* psymval
,
437 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
438 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
440 // Do a simple 32-bit PC relative RELA relocation with the addend in
443 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
444 elfcpp::Elf_Word addend
,
445 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
446 { This::template pcrela
<32>(view
, value
, addend
, address
); }
449 pcrela32(unsigned char* view
,
450 const Sized_relobj
<size
, big_endian
>* object
,
451 const Symbol_value
<size
>* psymval
,
452 elfcpp::Elf_Word addend
,
453 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
454 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
456 // Do a simple 64-bit REL relocation with the addend in the section
459 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
460 { This::template rel
<64>(view
, value
); }
463 rel64(unsigned char* view
,
464 const Sized_relobj
<size
, big_endian
>* object
,
465 const Symbol_value
<size
>* psymval
)
466 { This::template rel
<64>(view
, object
, psymval
); }
468 // Do a 64-bit RELA relocation with the addend in the relocation.
470 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
471 elfcpp::Elf_Xword addend
)
472 { This::template rela
<64>(view
, value
, addend
); }
475 rela64(unsigned char* view
,
476 const Sized_relobj
<size
, big_endian
>* object
,
477 const Symbol_value
<size
>* psymval
,
478 elfcpp::Elf_Xword addend
)
479 { This::template rela
<64>(view
, object
, psymval
, addend
); }
481 // Do a simple 64-bit PC relative REL relocation with the addend in
482 // the section contents.
484 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
485 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
486 { This::template pcrel
<64>(view
, value
, address
); }
489 pcrel64(unsigned char* view
,
490 const Sized_relobj
<size
, big_endian
>* object
,
491 const Symbol_value
<size
>* psymval
,
492 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
493 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
495 // Do a simple 64-bit PC relative RELA relocation with the addend in
498 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
499 elfcpp::Elf_Xword addend
,
500 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
501 { This::template pcrela
<64>(view
, value
, addend
, address
); }
504 pcrela64(unsigned char* view
,
505 const Sized_relobj
<size
, big_endian
>* object
,
506 const Symbol_value
<size
>* psymval
,
507 elfcpp::Elf_Xword addend
,
508 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
509 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
512 // We try to avoid COPY relocations when possible. A COPY relocation
513 // may be required when an executable refers to a variable defined in
514 // a shared library. COPY relocations are problematic because they
515 // tie the executable to the exact size of the variable in the shared
516 // library. We can avoid them if all the references to the variable
517 // are in a writeable section. In that case we can simply use dynamic
518 // relocations. However, when scanning relocs, we don't know when we
519 // see the relocation whether we will be forced to use a COPY
520 // relocation or not. So we have to save the relocation during the
521 // reloc scanning, and then emit it as a dynamic relocation if
522 // necessary. This class implements that. It is used by the target
525 template<int size
, bool big_endian
>
533 // Return whether we need a COPY reloc for a reloc against GSYM,
534 // which is being applied to section SHNDX in OBJECT.
536 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
537 Sized_symbol
<size
>* gsym
);
539 // Save a Rel against SYM for possible emission later. SHNDX is the
540 // index of the section to which the reloc is being applied.
542 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
543 Output_section
* output_section
, const elfcpp::Rel
<size
, big_endian
>&);
545 // Save a Rela against SYM for possible emission later.
547 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
548 Output_section
* output_section
, const elfcpp::Rela
<size
, big_endian
>&);
550 // Return whether there are any relocs to emit. This also discards
551 // entries which need not be emitted.
555 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
556 // is still defined in the dynamic object).
557 template<int sh_type
>
559 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
562 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
563 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
565 // This POD class holds the entries we are saving.
566 class Copy_reloc_entry
569 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
570 Relobj
* relobj
, unsigned int shndx
,
571 Output_section
* output_section
,
572 Address address
, Addend addend
)
573 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
574 shndx_(shndx
), output_section_(output_section
),
575 address_(address
), addend_(addend
)
578 // Return whether we should emit this reloc. If we should not
579 // emit, we clear it.
586 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
589 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
593 unsigned int reloc_type_
;
596 Output_section
* output_section_
;
601 // A list of relocs to be saved.
602 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
604 // The list of relocs we are saving.
605 Copy_reloc_entries entries_
;
608 // Track relocations while reading a section. This lets you ask for
609 // the relocation at a certain offset, and see how relocs occur
610 // between points of interest.
612 template<int size
, bool big_endian
>
617 : prelocs_(NULL
), len_(0), pos_(0), reloc_size_(0)
620 // Initialize the Track_relocs object. OBJECT is the object holding
621 // the reloc section, RELOC_SHNDX is the section index of the reloc
622 // section, and RELOC_TYPE is the type of the reloc section
623 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
624 // something went wrong.
626 initialize(Object
* object
, unsigned int reloc_shndx
,
627 unsigned int reloc_type
);
629 // Return the offset in the data section to which the next reloc
630 // applies. THis returns -1 if there is no next reloc.
634 // Return the symbol index of the next reloc. This returns -1U if
635 // there is no next reloc.
639 // Advance to OFFSET within the data section, and return the number
640 // of relocs which would be skipped.
642 advance(off_t offset
);
645 // The contents of the input object's reloc section.
646 const unsigned char* prelocs_
;
647 // The length of the reloc section.
649 // Our current position in the reloc section.
651 // The size of the relocs in the section.
655 } // End namespace gold.
657 #endif // !defined(GOLD_RELOC_H)