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(const General_options
& options
, Symbol_table
* symtab
,
68 Layout
* layout
, Relobj
* object
, Task_token
* symtab_lock
,
70 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
71 symtab_lock_(symtab_lock
), blocker_(blocker
)
74 // The standard Task methods.
89 const General_options
& options_
;
90 Symbol_table
* symtab_
;
93 Task_token
* symtab_lock_
;
97 // Process the relocs to figure out which sections are garbage.
98 // Very similar to scan relocs.
100 class Gc_process_relocs
: public Task
103 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
104 // unblocked when the task completes.
105 Gc_process_relocs(const General_options
& options
, Symbol_table
* symtab
,
106 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
107 Task_token
* symtab_lock
, Task_token
* blocker
)
108 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
109 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
112 // The standard Task methods.
127 const General_options
& options_
;
128 Symbol_table
* symtab_
;
131 Read_relocs_data
* rd_
;
132 Task_token
* symtab_lock_
;
133 Task_token
* blocker_
;
136 // Scan the relocations for an object to see if they require any
137 // GOT/PLT/COPY relocations.
139 class Scan_relocs
: public Task
142 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
143 // unblocked when the task completes.
144 Scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
145 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
146 Task_token
* symtab_lock
, Task_token
* blocker
)
147 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
148 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
151 // The standard Task methods.
166 const General_options
& options_
;
167 Symbol_table
* symtab_
;
170 Read_relocs_data
* rd_
;
171 Task_token
* symtab_lock_
;
172 Task_token
* blocker_
;
175 // A class to perform all the relocations for an object file.
177 class Relocate_task
: public Task
180 Relocate_task(const General_options
& options
, const Symbol_table
* symtab
,
181 const Layout
* layout
, Relobj
* object
, Output_file
* of
,
182 Task_token
* input_sections_blocker
,
183 Task_token
* output_sections_blocker
, Task_token
* final_blocker
)
184 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
185 of_(of
), input_sections_blocker_(input_sections_blocker
),
186 output_sections_blocker_(output_sections_blocker
),
187 final_blocker_(final_blocker
)
190 // The standard Task methods.
205 const General_options
& options_
;
206 const Symbol_table
* symtab_
;
207 const Layout
* layout_
;
210 Task_token
* input_sections_blocker_
;
211 Task_token
* output_sections_blocker_
;
212 Task_token
* final_blocker_
;
215 // During a relocatable link, this class records how relocations
216 // should be handled for a single input reloc section. An instance of
217 // this class is created while scanning relocs, and it is used while
218 // processing relocs.
220 class Relocatable_relocs
223 // We use a vector of unsigned char to indicate how the input relocs
224 // should be handled. Each element is one of the following values.
225 // We create this vector when we initially scan the relocations.
228 // Copy the input reloc. Don't modify it other than updating the
229 // r_offset field and the r_sym part of the r_info field.
231 // Copy the input reloc which is against an STT_SECTION symbol.
232 // Update the r_offset and r_sym part of the r_info field. Adjust
233 // the addend by subtracting the value of the old local symbol and
234 // adding the value of the new local symbol. The addend is in the
235 // SHT_RELA reloc and the contents of the data section do not need
237 RELOC_ADJUST_FOR_SECTION_RELA
,
238 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
240 RELOC_ADJUST_FOR_SECTION_0
,
241 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
242 // section need to be changed. The number indicates the number of
243 // bytes in the addend in the section contents.
244 RELOC_ADJUST_FOR_SECTION_1
,
245 RELOC_ADJUST_FOR_SECTION_2
,
246 RELOC_ADJUST_FOR_SECTION_4
,
247 RELOC_ADJUST_FOR_SECTION_8
,
248 // Discard the input reloc--process it completely when relocating
249 // the data section contents.
251 // An input reloc which is not discarded, but which requires
252 // target specific processing in order to update it.
257 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL
)
260 // Record the number of relocs.
262 set_reloc_count(size_t reloc_count
)
263 { this->reloc_strategies_
.reserve(reloc_count
); }
265 // Record what to do for the next reloc.
267 set_next_reloc_strategy(Reloc_strategy strategy
)
269 this->reloc_strategies_
.push_back(static_cast<unsigned char>(strategy
));
270 if (strategy
!= RELOC_DISCARD
)
271 ++this->output_reloc_count_
;
274 // Record the Output_data associated with this reloc section.
276 set_output_data(Output_data
* posd
)
278 gold_assert(this->posd_
== NULL
);
282 // Return the Output_data associated with this reloc section.
285 { return this->posd_
; }
287 // Return what to do for reloc I.
289 strategy(unsigned int i
) const
291 gold_assert(i
< this->reloc_strategies_
.size());
292 return static_cast<Reloc_strategy
>(this->reloc_strategies_
[i
]);
295 // Return the number of relocations to create in the output file.
297 output_reloc_count() const
298 { return this->output_reloc_count_
; }
301 typedef std::vector
<unsigned char> Reloc_strategies
;
303 // The strategies for the input reloc. There is one entry in this
304 // vector for each relocation in the input section.
305 Reloc_strategies reloc_strategies_
;
306 // The number of relocations to be created in the output file.
307 size_t output_reloc_count_
;
308 // The output data structure associated with this relocation.
312 // Standard relocation routines which are used on many targets. Here
313 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
315 template<int size
, bool big_endian
>
316 class Relocate_functions
319 // Do a simple relocation with the addend in the section contents.
320 // VALSIZE is the size of the value.
321 template<int valsize
>
323 rel(unsigned char* view
,
324 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
326 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
327 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
328 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
329 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
332 // Do a simple relocation using a Symbol_value with the addend in
333 // the section contents. VALSIZE is the size of the value to
335 template<int valsize
>
337 rel(unsigned char* view
,
338 const Sized_relobj
<size
, big_endian
>* object
,
339 const Symbol_value
<size
>* psymval
)
341 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
342 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
343 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
344 x
= psymval
->value(object
, x
);
345 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
348 // Do a simple relocation with the addend in the relocation.
349 // VALSIZE is the size of the value.
350 template<int valsize
>
352 rela(unsigned char* view
,
353 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
354 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
356 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
357 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
358 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
);
361 // Do a simple relocation using a symbol value with the addend in
362 // the relocation. VALSIZE is the size of the value.
363 template<int valsize
>
365 rela(unsigned char* view
,
366 const Sized_relobj
<size
, big_endian
>* object
,
367 const Symbol_value
<size
>* psymval
,
368 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
370 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
371 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
372 Valtype x
= psymval
->value(object
, addend
);
373 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
376 // Do a simple PC relative relocation with the addend in the section
377 // contents. VALSIZE is the size of the value.
378 template<int valsize
>
380 pcrel(unsigned char* view
,
381 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
382 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
384 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
385 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
386 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
387 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
390 // Do a simple PC relative relocation with a Symbol_value with the
391 // addend in the section contents. VALSIZE is the size of the
393 template<int valsize
>
395 pcrel(unsigned char* view
,
396 const Sized_relobj
<size
, big_endian
>* object
,
397 const Symbol_value
<size
>* psymval
,
398 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
400 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
401 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
402 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
403 x
= psymval
->value(object
, x
);
404 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
407 // Do a simple PC relative relocation with the addend in the
408 // relocation. VALSIZE is the size of the value.
409 template<int valsize
>
411 pcrela(unsigned char* view
,
412 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
413 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
414 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
416 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
417 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
418 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
421 // Do a simple PC relative relocation with a Symbol_value with the
422 // addend in the relocation. VALSIZE is the size of the value.
423 template<int valsize
>
425 pcrela(unsigned char* view
,
426 const Sized_relobj
<size
, big_endian
>* object
,
427 const Symbol_value
<size
>* psymval
,
428 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
429 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
431 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
432 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
433 Valtype x
= psymval
->value(object
, addend
);
434 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
437 typedef Relocate_functions
<size
, big_endian
> This
;
440 // Do a simple 8-bit REL relocation with the addend in the section
443 rel8(unsigned char* view
, unsigned char value
)
444 { This::template rel
<8>(view
, value
); }
447 rel8(unsigned char* view
,
448 const Sized_relobj
<size
, big_endian
>* object
,
449 const Symbol_value
<size
>* psymval
)
450 { This::template rel
<8>(view
, object
, psymval
); }
452 // Do an 8-bit RELA relocation with the addend in the relocation.
454 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
455 { This::template rela
<8>(view
, value
, addend
); }
458 rela8(unsigned char* view
,
459 const Sized_relobj
<size
, big_endian
>* object
,
460 const Symbol_value
<size
>* psymval
,
461 unsigned char addend
)
462 { This::template rela
<8>(view
, object
, psymval
, addend
); }
464 // Do a simple 8-bit PC relative relocation with the addend in the
467 pcrel8(unsigned char* view
, unsigned char value
,
468 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
469 { This::template pcrel
<8>(view
, value
, address
); }
472 pcrel8(unsigned char* view
,
473 const Sized_relobj
<size
, big_endian
>* object
,
474 const Symbol_value
<size
>* psymval
,
475 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
476 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
478 // Do a simple 8-bit PC relative RELA relocation with the addend in
481 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
482 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
483 { This::template pcrela
<8>(view
, value
, addend
, address
); }
486 pcrela8(unsigned char* view
,
487 const Sized_relobj
<size
, big_endian
>* object
,
488 const Symbol_value
<size
>* psymval
,
489 unsigned char addend
,
490 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
491 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
493 // Do a simple 16-bit REL relocation with the addend in the section
496 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
497 { This::template rel
<16>(view
, value
); }
500 rel16(unsigned char* view
,
501 const Sized_relobj
<size
, big_endian
>* object
,
502 const Symbol_value
<size
>* psymval
)
503 { This::template rel
<16>(view
, object
, psymval
); }
505 // Do an 16-bit RELA relocation with the addend in the relocation.
507 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
508 { This::template rela
<16>(view
, value
, addend
); }
511 rela16(unsigned char* view
,
512 const Sized_relobj
<size
, big_endian
>* object
,
513 const Symbol_value
<size
>* psymval
,
514 elfcpp::Elf_Half addend
)
515 { This::template rela
<16>(view
, object
, psymval
, addend
); }
517 // Do a simple 16-bit PC relative REL relocation with the addend in
518 // the section contents.
520 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
521 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
522 { This::template pcrel
<16>(view
, value
, address
); }
525 pcrel16(unsigned char* view
,
526 const Sized_relobj
<size
, big_endian
>* object
,
527 const Symbol_value
<size
>* psymval
,
528 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
529 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
531 // Do a simple 16-bit PC relative RELA relocation with the addend in
534 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
535 elfcpp::Elf_Half addend
,
536 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
537 { This::template pcrela
<16>(view
, value
, addend
, address
); }
540 pcrela16(unsigned char* view
,
541 const Sized_relobj
<size
, big_endian
>* object
,
542 const Symbol_value
<size
>* psymval
,
543 elfcpp::Elf_Half addend
,
544 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
545 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
547 // Do a simple 32-bit REL relocation with the addend in the section
550 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
551 { This::template rel
<32>(view
, value
); }
554 rel32(unsigned char* view
,
555 const Sized_relobj
<size
, big_endian
>* object
,
556 const Symbol_value
<size
>* psymval
)
557 { This::template rel
<32>(view
, object
, psymval
); }
559 // Do an 32-bit RELA relocation with the addend in the relocation.
561 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
562 { This::template rela
<32>(view
, value
, addend
); }
565 rela32(unsigned char* view
,
566 const Sized_relobj
<size
, big_endian
>* object
,
567 const Symbol_value
<size
>* psymval
,
568 elfcpp::Elf_Word addend
)
569 { This::template rela
<32>(view
, object
, psymval
, addend
); }
571 // Do a simple 32-bit PC relative REL relocation with the addend in
572 // the section contents.
574 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
575 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
576 { This::template pcrel
<32>(view
, value
, address
); }
579 pcrel32(unsigned char* view
,
580 const Sized_relobj
<size
, big_endian
>* object
,
581 const Symbol_value
<size
>* psymval
,
582 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
583 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
585 // Do a simple 32-bit PC relative RELA relocation with the addend in
588 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
589 elfcpp::Elf_Word addend
,
590 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
591 { This::template pcrela
<32>(view
, value
, addend
, address
); }
594 pcrela32(unsigned char* view
,
595 const Sized_relobj
<size
, big_endian
>* object
,
596 const Symbol_value
<size
>* psymval
,
597 elfcpp::Elf_Word addend
,
598 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
599 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
601 // Do a simple 64-bit REL relocation with the addend in the section
604 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
605 { This::template rel
<64>(view
, value
); }
608 rel64(unsigned char* view
,
609 const Sized_relobj
<size
, big_endian
>* object
,
610 const Symbol_value
<size
>* psymval
)
611 { This::template rel
<64>(view
, object
, psymval
); }
613 // Do a 64-bit RELA relocation with the addend in the relocation.
615 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
616 elfcpp::Elf_Xword addend
)
617 { This::template rela
<64>(view
, value
, addend
); }
620 rela64(unsigned char* view
,
621 const Sized_relobj
<size
, big_endian
>* object
,
622 const Symbol_value
<size
>* psymval
,
623 elfcpp::Elf_Xword addend
)
624 { This::template rela
<64>(view
, object
, psymval
, addend
); }
626 // Do a simple 64-bit PC relative REL relocation with the addend in
627 // the section contents.
629 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
630 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
631 { This::template pcrel
<64>(view
, value
, address
); }
634 pcrel64(unsigned char* view
,
635 const Sized_relobj
<size
, big_endian
>* object
,
636 const Symbol_value
<size
>* psymval
,
637 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
638 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
640 // Do a simple 64-bit PC relative RELA relocation with the addend in
643 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
644 elfcpp::Elf_Xword addend
,
645 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
646 { This::template pcrela
<64>(view
, value
, addend
, address
); }
649 pcrela64(unsigned char* view
,
650 const Sized_relobj
<size
, big_endian
>* object
,
651 const Symbol_value
<size
>* psymval
,
652 elfcpp::Elf_Xword addend
,
653 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
654 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
657 // Track relocations while reading a section. This lets you ask for
658 // the relocation at a certain offset, and see how relocs occur
659 // between points of interest.
661 template<int size
, bool big_endian
>
666 : prelocs_(NULL
), len_(0), pos_(0), reloc_size_(0)
669 // Initialize the Track_relocs object. OBJECT is the object holding
670 // the reloc section, RELOC_SHNDX is the section index of the reloc
671 // section, and RELOC_TYPE is the type of the reloc section
672 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
673 // something went wrong.
675 initialize(Object
* object
, unsigned int reloc_shndx
,
676 unsigned int reloc_type
);
678 // Return the offset in the data section to which the next reloc
679 // applies. THis returns -1 if there is no next reloc.
683 // Return the symbol index of the next reloc. This returns -1U if
684 // there is no next reloc.
688 // Advance to OFFSET within the data section, and return the number
689 // of relocs which would be skipped.
691 advance(off_t offset
);
694 // The contents of the input object's reloc section.
695 const unsigned char* prelocs_
;
696 // The length of the reloc section.
697 section_size_type len_
;
698 // Our current position in the reloc section.
699 section_size_type pos_
;
700 // The size of the relocs in the section.
704 } // End namespace gold.
706 #endif // !defined(GOLD_RELOC_H)