1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 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.
27 #ifdef HAVE_BYTESWAP_H
32 #include "workqueue.h"
37 class General_options
;
40 class Read_relocs_data
;
49 template<int size
, bool big_endian
>
55 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
56 class Output_data_reloc
;
58 // A class to read the relocations for an object file, and then queue
59 // up a task to see if they require any GOT/PLT/COPY relocations in
62 class Read_relocs
: public Task
65 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
66 // unblocked when the Scan_relocs task completes.
67 Read_relocs(Symbol_table
* symtab
, Layout
* layout
, Relobj
* object
,
68 Task_token
* symtab_lock
, Task_token
* blocker
)
69 : symtab_(symtab
), layout_(layout
), object_(object
),
70 symtab_lock_(symtab_lock
), blocker_(blocker
)
73 // The standard Task methods.
88 Symbol_table
* symtab_
;
91 Task_token
* symtab_lock_
;
95 // Process the relocs to figure out which sections are garbage.
96 // Very similar to scan relocs.
98 class Gc_process_relocs
: public Task
101 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
102 // unblocked when the task completes.
103 Gc_process_relocs(Symbol_table
* symtab
, Layout
* layout
, Relobj
* object
,
104 Read_relocs_data
* rd
, Task_token
* symtab_lock
,
106 : symtab_(symtab
), layout_(layout
), object_(object
), rd_(rd
),
107 symtab_lock_(symtab_lock
), blocker_(blocker
)
110 // The standard Task methods.
125 Symbol_table
* symtab_
;
128 Read_relocs_data
* rd_
;
129 Task_token
* symtab_lock_
;
130 Task_token
* blocker_
;
133 // Scan the relocations for an object to see if they require any
134 // GOT/PLT/COPY relocations.
136 class Scan_relocs
: public Task
139 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
140 // unblocked when the task completes.
141 Scan_relocs(Symbol_table
* symtab
, Layout
* layout
, Relobj
* object
,
142 Read_relocs_data
* rd
, Task_token
* symtab_lock
,
144 : symtab_(symtab
), layout_(layout
), object_(object
), rd_(rd
),
145 symtab_lock_(symtab_lock
), blocker_(blocker
)
148 // The standard Task methods.
163 Symbol_table
* symtab_
;
166 Read_relocs_data
* rd_
;
167 Task_token
* symtab_lock_
;
168 Task_token
* blocker_
;
171 // A class to perform all the relocations for an object file.
173 class Relocate_task
: public Task
176 Relocate_task(const Symbol_table
* symtab
, const Layout
* layout
,
177 Relobj
* object
, Output_file
* of
,
178 Task_token
* input_sections_blocker
,
179 Task_token
* output_sections_blocker
, Task_token
* final_blocker
)
180 : symtab_(symtab
), layout_(layout
), object_(object
), of_(of
),
181 input_sections_blocker_(input_sections_blocker
),
182 output_sections_blocker_(output_sections_blocker
),
183 final_blocker_(final_blocker
)
186 // The standard Task methods.
201 const Symbol_table
* symtab_
;
202 const Layout
* layout_
;
205 Task_token
* input_sections_blocker_
;
206 Task_token
* output_sections_blocker_
;
207 Task_token
* final_blocker_
;
210 // During a relocatable link, this class records how relocations
211 // should be handled for a single input reloc section. An instance of
212 // this class is created while scanning relocs, and it is used while
213 // processing relocs.
215 class Relocatable_relocs
218 // We use a vector of unsigned char to indicate how the input relocs
219 // should be handled. Each element is one of the following values.
220 // We create this vector when we initially scan the relocations.
223 // Copy the input reloc. Don't modify it other than updating the
224 // r_offset field and the r_sym part of the r_info field.
226 // Copy the input reloc which is against an STT_SECTION symbol.
227 // Update the r_offset and r_sym part of the r_info field. Adjust
228 // the addend by subtracting the value of the old local symbol and
229 // adding the value of the new local symbol. The addend is in the
230 // SHT_RELA reloc and the contents of the data section do not need
232 RELOC_ADJUST_FOR_SECTION_RELA
,
233 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
235 RELOC_ADJUST_FOR_SECTION_0
,
236 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
237 // section need to be changed. The number indicates the number of
238 // bytes in the addend in the section contents.
239 RELOC_ADJUST_FOR_SECTION_1
,
240 RELOC_ADJUST_FOR_SECTION_2
,
241 RELOC_ADJUST_FOR_SECTION_4
,
242 RELOC_ADJUST_FOR_SECTION_8
,
243 // Discard the input reloc--process it completely when relocating
244 // the data section contents.
246 // An input reloc which is not discarded, but which requires
247 // target specific processing in order to update it.
252 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL
)
255 // Record the number of relocs.
257 set_reloc_count(size_t reloc_count
)
258 { this->reloc_strategies_
.reserve(reloc_count
); }
260 // Record what to do for the next reloc.
262 set_next_reloc_strategy(Reloc_strategy strategy
)
264 this->reloc_strategies_
.push_back(static_cast<unsigned char>(strategy
));
265 if (strategy
!= RELOC_DISCARD
)
266 ++this->output_reloc_count_
;
269 // Record the Output_data associated with this reloc section.
271 set_output_data(Output_data
* posd
)
273 gold_assert(this->posd_
== NULL
);
277 // Return the Output_data associated with this reloc section.
280 { return this->posd_
; }
282 // Return what to do for reloc I.
284 strategy(unsigned int i
) const
286 gold_assert(i
< this->reloc_strategies_
.size());
287 return static_cast<Reloc_strategy
>(this->reloc_strategies_
[i
]);
290 // Return the number of relocations to create in the output file.
292 output_reloc_count() const
293 { return this->output_reloc_count_
; }
296 typedef std::vector
<unsigned char> Reloc_strategies
;
298 // The strategies for the input reloc. There is one entry in this
299 // vector for each relocation in the input section.
300 Reloc_strategies reloc_strategies_
;
301 // The number of relocations to be created in the output file.
302 size_t output_reloc_count_
;
303 // The output data structure associated with this relocation.
307 // Standard relocation routines which are used on many targets. Here
308 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
310 template<int size
, bool big_endian
>
311 class Relocate_functions
314 // Do a simple relocation with the addend in the section contents.
315 // VALSIZE is the size of the value.
316 template<int valsize
>
318 rel(unsigned char* view
,
319 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
321 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
322 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
323 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
324 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
327 // Do a simple relocation using a Symbol_value with the addend in
328 // the section contents. VALSIZE is the size of the value to
330 template<int valsize
>
332 rel(unsigned char* view
,
333 const Sized_relobj
<size
, big_endian
>* object
,
334 const Symbol_value
<size
>* psymval
)
336 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
337 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
338 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
339 x
= psymval
->value(object
, x
);
340 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
343 // Do a simple relocation with the addend in the relocation.
344 // VALSIZE is the size of the value.
345 template<int valsize
>
347 rela(unsigned char* view
,
348 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
349 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
351 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
352 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
353 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
);
356 // Do a simple relocation using a symbol value with the addend in
357 // the relocation. VALSIZE is the size of the value.
358 template<int valsize
>
360 rela(unsigned char* view
,
361 const Sized_relobj
<size
, big_endian
>* object
,
362 const Symbol_value
<size
>* psymval
,
363 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
365 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
366 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
367 Valtype x
= psymval
->value(object
, addend
);
368 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
371 // Do a simple PC relative relocation with the addend in the section
372 // contents. VALSIZE is the size of the value.
373 template<int valsize
>
375 pcrel(unsigned char* view
,
376 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
377 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
379 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
380 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
381 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
382 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
385 // Do a simple PC relative relocation with a Symbol_value with the
386 // addend in the section contents. VALSIZE is the size of the
388 template<int valsize
>
390 pcrel(unsigned char* view
,
391 const Sized_relobj
<size
, big_endian
>* object
,
392 const Symbol_value
<size
>* psymval
,
393 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
395 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
396 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
397 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
398 x
= psymval
->value(object
, x
);
399 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
402 // Do a simple PC relative relocation with the addend in the
403 // relocation. VALSIZE is the size of the value.
404 template<int valsize
>
406 pcrela(unsigned char* view
,
407 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
408 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
409 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
411 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
412 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
413 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
416 // Do a simple PC relative relocation with a Symbol_value with the
417 // addend in the relocation. VALSIZE is the size of the value.
418 template<int valsize
>
420 pcrela(unsigned char* view
,
421 const Sized_relobj
<size
, big_endian
>* object
,
422 const Symbol_value
<size
>* psymval
,
423 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
424 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
426 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
427 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
428 Valtype x
= psymval
->value(object
, addend
);
429 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
432 typedef Relocate_functions
<size
, big_endian
> This
;
435 // Do a simple 8-bit REL relocation with the addend in the section
438 rel8(unsigned char* view
, unsigned char value
)
439 { This::template rel
<8>(view
, value
); }
442 rel8(unsigned char* view
,
443 const Sized_relobj
<size
, big_endian
>* object
,
444 const Symbol_value
<size
>* psymval
)
445 { This::template rel
<8>(view
, object
, psymval
); }
447 // Do an 8-bit RELA relocation with the addend in the relocation.
449 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
450 { This::template rela
<8>(view
, value
, addend
); }
453 rela8(unsigned char* view
,
454 const Sized_relobj
<size
, big_endian
>* object
,
455 const Symbol_value
<size
>* psymval
,
456 unsigned char addend
)
457 { This::template rela
<8>(view
, object
, psymval
, addend
); }
459 // Do a simple 8-bit PC relative relocation with the addend in the
462 pcrel8(unsigned char* view
, unsigned char value
,
463 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
464 { This::template pcrel
<8>(view
, value
, address
); }
467 pcrel8(unsigned char* view
,
468 const Sized_relobj
<size
, big_endian
>* object
,
469 const Symbol_value
<size
>* psymval
,
470 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
471 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
473 // Do a simple 8-bit PC relative RELA relocation with the addend in
476 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
477 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
478 { This::template pcrela
<8>(view
, value
, addend
, address
); }
481 pcrela8(unsigned char* view
,
482 const Sized_relobj
<size
, big_endian
>* object
,
483 const Symbol_value
<size
>* psymval
,
484 unsigned char addend
,
485 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
486 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
488 // Do a simple 16-bit REL relocation with the addend in the section
491 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
492 { This::template rel
<16>(view
, value
); }
495 rel16(unsigned char* view
,
496 const Sized_relobj
<size
, big_endian
>* object
,
497 const Symbol_value
<size
>* psymval
)
498 { This::template rel
<16>(view
, object
, psymval
); }
500 // Do an 16-bit RELA relocation with the addend in the relocation.
502 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
503 { This::template rela
<16>(view
, value
, addend
); }
506 rela16(unsigned char* view
,
507 const Sized_relobj
<size
, big_endian
>* object
,
508 const Symbol_value
<size
>* psymval
,
509 elfcpp::Elf_Half addend
)
510 { This::template rela
<16>(view
, object
, psymval
, addend
); }
512 // Do a simple 16-bit PC relative REL relocation with the addend in
513 // the section contents.
515 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
516 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
517 { This::template pcrel
<16>(view
, value
, address
); }
520 pcrel16(unsigned char* view
,
521 const Sized_relobj
<size
, big_endian
>* object
,
522 const Symbol_value
<size
>* psymval
,
523 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
524 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
526 // Do a simple 16-bit PC relative RELA relocation with the addend in
529 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
530 elfcpp::Elf_Half addend
,
531 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
532 { This::template pcrela
<16>(view
, value
, addend
, address
); }
535 pcrela16(unsigned char* view
,
536 const Sized_relobj
<size
, big_endian
>* object
,
537 const Symbol_value
<size
>* psymval
,
538 elfcpp::Elf_Half addend
,
539 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
540 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
542 // Do a simple 32-bit REL relocation with the addend in the section
545 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
546 { This::template rel
<32>(view
, value
); }
549 rel32(unsigned char* view
,
550 const Sized_relobj
<size
, big_endian
>* object
,
551 const Symbol_value
<size
>* psymval
)
552 { This::template rel
<32>(view
, object
, psymval
); }
554 // Do an 32-bit RELA relocation with the addend in the relocation.
556 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
557 { This::template rela
<32>(view
, value
, addend
); }
560 rela32(unsigned char* view
,
561 const Sized_relobj
<size
, big_endian
>* object
,
562 const Symbol_value
<size
>* psymval
,
563 elfcpp::Elf_Word addend
)
564 { This::template rela
<32>(view
, object
, psymval
, addend
); }
566 // Do a simple 32-bit PC relative REL relocation with the addend in
567 // the section contents.
569 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
570 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
571 { This::template pcrel
<32>(view
, value
, address
); }
574 pcrel32(unsigned char* view
,
575 const Sized_relobj
<size
, big_endian
>* object
,
576 const Symbol_value
<size
>* psymval
,
577 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
578 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
580 // Do a simple 32-bit PC relative RELA relocation with the addend in
583 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
584 elfcpp::Elf_Word addend
,
585 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
586 { This::template pcrela
<32>(view
, value
, addend
, address
); }
589 pcrela32(unsigned char* view
,
590 const Sized_relobj
<size
, big_endian
>* object
,
591 const Symbol_value
<size
>* psymval
,
592 elfcpp::Elf_Word addend
,
593 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
594 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
596 // Do a simple 64-bit REL relocation with the addend in the section
599 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
600 { This::template rel
<64>(view
, value
); }
603 rel64(unsigned char* view
,
604 const Sized_relobj
<size
, big_endian
>* object
,
605 const Symbol_value
<size
>* psymval
)
606 { This::template rel
<64>(view
, object
, psymval
); }
608 // Do a 64-bit RELA relocation with the addend in the relocation.
610 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
611 elfcpp::Elf_Xword addend
)
612 { This::template rela
<64>(view
, value
, addend
); }
615 rela64(unsigned char* view
,
616 const Sized_relobj
<size
, big_endian
>* object
,
617 const Symbol_value
<size
>* psymval
,
618 elfcpp::Elf_Xword addend
)
619 { This::template rela
<64>(view
, object
, psymval
, addend
); }
621 // Do a simple 64-bit PC relative REL relocation with the addend in
622 // the section contents.
624 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
625 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
626 { This::template pcrel
<64>(view
, value
, address
); }
629 pcrel64(unsigned char* view
,
630 const Sized_relobj
<size
, big_endian
>* object
,
631 const Symbol_value
<size
>* psymval
,
632 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
633 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
635 // Do a simple 64-bit PC relative RELA relocation with the addend in
638 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
639 elfcpp::Elf_Xword addend
,
640 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
641 { This::template pcrela
<64>(view
, value
, addend
, address
); }
644 pcrela64(unsigned char* view
,
645 const Sized_relobj
<size
, big_endian
>* object
,
646 const Symbol_value
<size
>* psymval
,
647 elfcpp::Elf_Xword addend
,
648 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
649 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
652 // Track relocations while reading a section. This lets you ask for
653 // the relocation at a certain offset, and see how relocs occur
654 // between points of interest.
656 template<int size
, bool big_endian
>
661 : prelocs_(NULL
), len_(0), pos_(0), reloc_size_(0)
664 // Initialize the Track_relocs object. OBJECT is the object holding
665 // the reloc section, RELOC_SHNDX is the section index of the reloc
666 // section, and RELOC_TYPE is the type of the reloc section
667 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
668 // something went wrong.
670 initialize(Object
* object
, unsigned int reloc_shndx
,
671 unsigned int reloc_type
);
673 // Return the offset in the data section to which the next reloc
674 // applies. THis returns -1 if there is no next reloc.
678 // Return the symbol index of the next reloc. This returns -1U if
679 // there is no next reloc.
683 // Advance to OFFSET within the data section, and return the number
684 // of relocs which would be skipped.
686 advance(off_t offset
);
689 // The contents of the input object's reloc section.
690 const unsigned char* prelocs_
;
691 // The length of the reloc section.
692 section_size_type len_
;
693 // Our current position in the reloc section.
694 section_size_type pos_
;
695 // The size of the relocs in the section.
699 } // End namespace gold.
701 #endif // !defined(GOLD_RELOC_H)