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.
30 #include "workqueue.h"
35 class General_options
;
38 class Read_relocs_data
;
47 template<int size
, bool big_endian
>
53 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
54 class Output_data_reloc
;
56 // A class to read the relocations for an object file, and then queue
57 // up a task to see if they require any GOT/PLT/COPY relocations in
60 class Read_relocs
: public Task
63 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
64 // unblocked when the Scan_relocs task completes.
65 Read_relocs(const General_options
& options
, Symbol_table
* symtab
,
66 Layout
* layout
, Relobj
* object
, Task_token
* symtab_lock
,
68 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
69 symtab_lock_(symtab_lock
), blocker_(blocker
)
72 // The standard Task methods.
87 const General_options
& options_
;
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(const General_options
& options
, Symbol_table
* symtab
,
104 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
105 Task_token
* symtab_lock
, Task_token
* blocker
)
106 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
107 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
110 // The standard Task methods.
125 const General_options
& options_
;
126 Symbol_table
* symtab_
;
129 Read_relocs_data
* rd_
;
130 Task_token
* symtab_lock_
;
131 Task_token
* blocker_
;
134 // Scan the relocations for an object to see if they require any
135 // GOT/PLT/COPY relocations.
137 class Scan_relocs
: public Task
140 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
141 // unblocked when the task completes.
142 Scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
143 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
144 Task_token
* symtab_lock
, Task_token
* blocker
)
145 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
146 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
149 // The standard Task methods.
164 const General_options
& options_
;
165 Symbol_table
* symtab_
;
168 Read_relocs_data
* rd_
;
169 Task_token
* symtab_lock_
;
170 Task_token
* blocker_
;
173 // A class to perform all the relocations for an object file.
175 class Relocate_task
: public Task
178 Relocate_task(const General_options
& options
, const Symbol_table
* symtab
,
179 const Layout
* layout
, Relobj
* object
, Output_file
* of
,
180 Task_token
* input_sections_blocker
,
181 Task_token
* output_sections_blocker
, Task_token
* final_blocker
)
182 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
183 of_(of
), input_sections_blocker_(input_sections_blocker
),
184 output_sections_blocker_(output_sections_blocker
),
185 final_blocker_(final_blocker
)
188 // The standard Task methods.
203 const General_options
& options_
;
204 const Symbol_table
* symtab_
;
205 const Layout
* layout_
;
208 Task_token
* input_sections_blocker_
;
209 Task_token
* output_sections_blocker_
;
210 Task_token
* final_blocker_
;
213 // During a relocatable link, this class records how relocations
214 // should be handled for a single input reloc section. An instance of
215 // this class is created while scanning relocs, and it is used while
216 // processing relocs.
218 class Relocatable_relocs
221 // We use a vector of unsigned char to indicate how the input relocs
222 // should be handled. Each element is one of the following values.
223 // We create this vector when we initially scan the relocations.
226 // Copy the input reloc. Don't modify it other than updating the
227 // r_offset field and the r_sym part of the r_info field.
229 // Copy the input reloc which is against an STT_SECTION symbol.
230 // Update the r_offset and r_sym part of the r_info field. Adjust
231 // the addend by subtracting the value of the old local symbol and
232 // adding the value of the new local symbol. The addend is in the
233 // SHT_RELA reloc and the contents of the data section do not need
235 RELOC_ADJUST_FOR_SECTION_RELA
,
236 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
238 RELOC_ADJUST_FOR_SECTION_0
,
239 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
240 // section need to be changed. The number indicates the number of
241 // bytes in the addend in the section contents.
242 RELOC_ADJUST_FOR_SECTION_1
,
243 RELOC_ADJUST_FOR_SECTION_2
,
244 RELOC_ADJUST_FOR_SECTION_4
,
245 RELOC_ADJUST_FOR_SECTION_8
,
246 // Discard the input reloc--process it completely when relocating
247 // the data section contents.
249 // An input reloc which is not discarded, but which requires
250 // target specific processing in order to update it.
255 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL
)
258 // Record the number of relocs.
260 set_reloc_count(size_t reloc_count
)
261 { this->reloc_strategies_
.reserve(reloc_count
); }
263 // Record what to do for the next reloc.
265 set_next_reloc_strategy(Reloc_strategy strategy
)
267 this->reloc_strategies_
.push_back(static_cast<unsigned char>(strategy
));
268 if (strategy
!= RELOC_DISCARD
)
269 ++this->output_reloc_count_
;
272 // Record the Output_data associated with this reloc section.
274 set_output_data(Output_data
* posd
)
276 gold_assert(this->posd_
== NULL
);
280 // Return the Output_data associated with this reloc section.
283 { return this->posd_
; }
285 // Return what to do for reloc I.
287 strategy(unsigned int i
) const
289 gold_assert(i
< this->reloc_strategies_
.size());
290 return static_cast<Reloc_strategy
>(this->reloc_strategies_
[i
]);
293 // Return the number of relocations to create in the output file.
295 output_reloc_count() const
296 { return this->output_reloc_count_
; }
299 typedef std::vector
<unsigned char> Reloc_strategies
;
301 // The strategies for the input reloc. There is one entry in this
302 // vector for each relocation in the input section.
303 Reloc_strategies reloc_strategies_
;
304 // The number of relocations to be created in the output file.
305 size_t output_reloc_count_
;
306 // The output data structure associated with this relocation.
310 // Standard relocation routines which are used on many targets. Here
311 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
313 template<int size
, bool big_endian
>
314 class Relocate_functions
317 // Do a simple relocation with the addend in the section contents.
318 // VALSIZE is the size of the value.
319 template<int valsize
>
321 rel(unsigned char* view
,
322 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
324 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
325 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
326 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
327 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
330 // Do a simple relocation using a Symbol_value with the addend in
331 // the section contents. VALSIZE is the size of the value to
333 template<int valsize
>
335 rel(unsigned char* view
,
336 const Sized_relobj
<size
, big_endian
>* object
,
337 const Symbol_value
<size
>* psymval
)
339 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
340 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
341 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
342 x
= psymval
->value(object
, x
);
343 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
346 // Do a simple relocation with the addend in the relocation.
347 // VALSIZE is the size of the value.
348 template<int valsize
>
350 rela(unsigned char* view
,
351 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
352 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
354 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
355 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
356 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
);
359 // Do a simple relocation using a symbol value with the addend in
360 // the relocation. VALSIZE is the size of the value.
361 template<int valsize
>
363 rela(unsigned char* view
,
364 const Sized_relobj
<size
, big_endian
>* object
,
365 const Symbol_value
<size
>* psymval
,
366 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
368 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
369 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
370 Valtype x
= psymval
->value(object
, addend
);
371 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
374 // Do a simple PC relative relocation with the addend in the section
375 // contents. VALSIZE is the size of the value.
376 template<int valsize
>
378 pcrel(unsigned char* view
,
379 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
380 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
382 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
383 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
384 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
385 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
388 // Do a simple PC relative relocation with a Symbol_value with the
389 // addend in the section contents. VALSIZE is the size of the
391 template<int valsize
>
393 pcrel(unsigned char* view
,
394 const Sized_relobj
<size
, big_endian
>* object
,
395 const Symbol_value
<size
>* psymval
,
396 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
398 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
399 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
400 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
401 x
= psymval
->value(object
, x
);
402 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
405 // Do a simple PC relative relocation with the addend in the
406 // relocation. VALSIZE is the size of the value.
407 template<int valsize
>
409 pcrela(unsigned char* view
,
410 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
411 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
412 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
414 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
415 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
416 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
419 // Do a simple PC relative relocation with a Symbol_value with the
420 // addend in the relocation. VALSIZE is the size of the value.
421 template<int valsize
>
423 pcrela(unsigned char* view
,
424 const Sized_relobj
<size
, big_endian
>* object
,
425 const Symbol_value
<size
>* psymval
,
426 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
427 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
429 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
430 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
431 Valtype x
= psymval
->value(object
, addend
);
432 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
435 typedef Relocate_functions
<size
, big_endian
> This
;
438 // Do a simple 8-bit REL relocation with the addend in the section
441 rel8(unsigned char* view
, unsigned char value
)
442 { This::template rel
<8>(view
, value
); }
445 rel8(unsigned char* view
,
446 const Sized_relobj
<size
, big_endian
>* object
,
447 const Symbol_value
<size
>* psymval
)
448 { This::template rel
<8>(view
, object
, psymval
); }
450 // Do an 8-bit RELA relocation with the addend in the relocation.
452 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
453 { This::template rela
<8>(view
, value
, addend
); }
456 rela8(unsigned char* view
,
457 const Sized_relobj
<size
, big_endian
>* object
,
458 const Symbol_value
<size
>* psymval
,
459 unsigned char addend
)
460 { This::template rela
<8>(view
, object
, psymval
, addend
); }
462 // Do a simple 8-bit PC relative relocation with the addend in the
465 pcrel8(unsigned char* view
, unsigned char value
,
466 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
467 { This::template pcrel
<8>(view
, value
, address
); }
470 pcrel8(unsigned char* view
,
471 const Sized_relobj
<size
, big_endian
>* object
,
472 const Symbol_value
<size
>* psymval
,
473 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
474 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
476 // Do a simple 8-bit PC relative RELA relocation with the addend in
479 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
480 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
481 { This::template pcrela
<8>(view
, value
, addend
, address
); }
484 pcrela8(unsigned char* view
,
485 const Sized_relobj
<size
, big_endian
>* object
,
486 const Symbol_value
<size
>* psymval
,
487 unsigned char addend
,
488 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
489 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
491 // Do a simple 16-bit REL relocation with the addend in the section
494 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
495 { This::template rel
<16>(view
, value
); }
498 rel16(unsigned char* view
,
499 const Sized_relobj
<size
, big_endian
>* object
,
500 const Symbol_value
<size
>* psymval
)
501 { This::template rel
<16>(view
, object
, psymval
); }
503 // Do an 16-bit RELA relocation with the addend in the relocation.
505 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
506 { This::template rela
<16>(view
, value
, addend
); }
509 rela16(unsigned char* view
,
510 const Sized_relobj
<size
, big_endian
>* object
,
511 const Symbol_value
<size
>* psymval
,
512 elfcpp::Elf_Half addend
)
513 { This::template rela
<16>(view
, object
, psymval
, addend
); }
515 // Do a simple 16-bit PC relative REL relocation with the addend in
516 // the section contents.
518 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
519 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
520 { This::template pcrel
<16>(view
, value
, address
); }
523 pcrel16(unsigned char* view
,
524 const Sized_relobj
<size
, big_endian
>* object
,
525 const Symbol_value
<size
>* psymval
,
526 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
527 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
529 // Do a simple 16-bit PC relative RELA relocation with the addend in
532 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
533 elfcpp::Elf_Half addend
,
534 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
535 { This::template pcrela
<16>(view
, value
, addend
, address
); }
538 pcrela16(unsigned char* view
,
539 const Sized_relobj
<size
, big_endian
>* object
,
540 const Symbol_value
<size
>* psymval
,
541 elfcpp::Elf_Half addend
,
542 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
543 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
545 // Do a simple 32-bit REL relocation with the addend in the section
548 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
549 { This::template rel
<32>(view
, value
); }
552 rel32(unsigned char* view
,
553 const Sized_relobj
<size
, big_endian
>* object
,
554 const Symbol_value
<size
>* psymval
)
555 { This::template rel
<32>(view
, object
, psymval
); }
557 // Do an 32-bit RELA relocation with the addend in the relocation.
559 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
560 { This::template rela
<32>(view
, value
, addend
); }
563 rela32(unsigned char* view
,
564 const Sized_relobj
<size
, big_endian
>* object
,
565 const Symbol_value
<size
>* psymval
,
566 elfcpp::Elf_Word addend
)
567 { This::template rela
<32>(view
, object
, psymval
, addend
); }
569 // Do a simple 32-bit PC relative REL relocation with the addend in
570 // the section contents.
572 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
573 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
574 { This::template pcrel
<32>(view
, value
, address
); }
577 pcrel32(unsigned char* view
,
578 const Sized_relobj
<size
, big_endian
>* object
,
579 const Symbol_value
<size
>* psymval
,
580 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
581 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
583 // Do a simple 32-bit PC relative RELA relocation with the addend in
586 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
587 elfcpp::Elf_Word addend
,
588 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
589 { This::template pcrela
<32>(view
, value
, addend
, address
); }
592 pcrela32(unsigned char* view
,
593 const Sized_relobj
<size
, big_endian
>* object
,
594 const Symbol_value
<size
>* psymval
,
595 elfcpp::Elf_Word addend
,
596 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
597 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
599 // Do a simple 64-bit REL relocation with the addend in the section
602 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
603 { This::template rel
<64>(view
, value
); }
606 rel64(unsigned char* view
,
607 const Sized_relobj
<size
, big_endian
>* object
,
608 const Symbol_value
<size
>* psymval
)
609 { This::template rel
<64>(view
, object
, psymval
); }
611 // Do a 64-bit RELA relocation with the addend in the relocation.
613 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
614 elfcpp::Elf_Xword addend
)
615 { This::template rela
<64>(view
, value
, addend
); }
618 rela64(unsigned char* view
,
619 const Sized_relobj
<size
, big_endian
>* object
,
620 const Symbol_value
<size
>* psymval
,
621 elfcpp::Elf_Xword addend
)
622 { This::template rela
<64>(view
, object
, psymval
, addend
); }
624 // Do a simple 64-bit PC relative REL relocation with the addend in
625 // the section contents.
627 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
628 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
629 { This::template pcrel
<64>(view
, value
, address
); }
632 pcrel64(unsigned char* view
,
633 const Sized_relobj
<size
, big_endian
>* object
,
634 const Symbol_value
<size
>* psymval
,
635 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
636 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
638 // Do a simple 64-bit PC relative RELA relocation with the addend in
641 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
642 elfcpp::Elf_Xword addend
,
643 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
644 { This::template pcrela
<64>(view
, value
, addend
, address
); }
647 pcrela64(unsigned char* view
,
648 const Sized_relobj
<size
, big_endian
>* object
,
649 const Symbol_value
<size
>* psymval
,
650 elfcpp::Elf_Xword addend
,
651 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
652 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
655 // Track relocations while reading a section. This lets you ask for
656 // the relocation at a certain offset, and see how relocs occur
657 // between points of interest.
659 template<int size
, bool big_endian
>
664 : prelocs_(NULL
), len_(0), pos_(0), reloc_size_(0)
667 // Initialize the Track_relocs object. OBJECT is the object holding
668 // the reloc section, RELOC_SHNDX is the section index of the reloc
669 // section, and RELOC_TYPE is the type of the reloc section
670 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
671 // something went wrong.
673 initialize(Object
* object
, unsigned int reloc_shndx
,
674 unsigned int reloc_type
);
676 // Return the offset in the data section to which the next reloc
677 // applies. THis returns -1 if there is no next reloc.
681 // Return the symbol index of the next reloc. This returns -1U if
682 // there is no next reloc.
686 // Advance to OFFSET within the data section, and return the number
687 // of relocs which would be skipped.
689 advance(off_t offset
);
692 // The contents of the input object's reloc section.
693 const unsigned char* prelocs_
;
694 // The length of the reloc section.
695 section_size_type len_
;
696 // Our current position in the reloc section.
697 section_size_type pos_
;
698 // The size of the relocs in the section.
702 } // End namespace gold.
704 #endif // !defined(GOLD_RELOC_H)