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.
30 #include "workqueue.h"
35 class General_options
;
38 class Read_relocs_data
;
46 template<int size
, bool big_endian
>
52 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
53 class Output_data_reloc
;
55 // A class to read the relocations for an object file, and then queue
56 // up a task to see if they require any GOT/PLT/COPY relocations in
59 class Read_relocs
: public Task
62 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
63 // unblocked when the Scan_relocs task completes.
64 Read_relocs(const General_options
& options
, Symbol_table
* symtab
,
65 Layout
* layout
, Relobj
* object
, Task_token
* symtab_lock
,
67 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
68 symtab_lock_(symtab_lock
), blocker_(blocker
)
71 // The standard Task methods.
86 const General_options
& options_
;
87 Symbol_table
* symtab_
;
90 Task_token
* symtab_lock_
;
94 // Scan the relocations for an object to see if they require any
95 // GOT/PLT/COPY relocations.
97 class Scan_relocs
: public Task
100 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
101 // unblocked when the task completes.
102 Scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
103 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
104 Task_token
* symtab_lock
, Task_token
* blocker
)
105 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
106 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
109 // The standard Task methods.
124 const General_options
& options_
;
125 Symbol_table
* symtab_
;
128 Read_relocs_data
* rd_
;
129 Task_token
* symtab_lock_
;
130 Task_token
* blocker_
;
133 // A class to perform all the relocations for an object file.
135 class Relocate_task
: public Task
138 Relocate_task(const General_options
& options
, const Symbol_table
* symtab
,
139 const Layout
* layout
, Relobj
* object
, Output_file
* of
,
140 Task_token
* input_sections_blocker
,
141 Task_token
* output_sections_blocker
, Task_token
* final_blocker
)
142 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
143 of_(of
), input_sections_blocker_(input_sections_blocker
),
144 output_sections_blocker_(output_sections_blocker
),
145 final_blocker_(final_blocker
)
148 // The standard Task methods.
163 const General_options
& options_
;
164 const Symbol_table
* symtab_
;
165 const Layout
* layout_
;
168 Task_token
* input_sections_blocker_
;
169 Task_token
* output_sections_blocker_
;
170 Task_token
* final_blocker_
;
173 // Standard relocation routines which are used on many targets. Here
174 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
176 template<int size
, bool big_endian
>
177 class Relocate_functions
180 // Do a simple relocation with the addend in the section contents.
181 // VALSIZE is the size of the value.
182 template<int valsize
>
184 rel(unsigned char* view
,
185 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
187 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
188 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
189 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
190 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
193 // Do a simple relocation using a Symbol_value with the addend in
194 // the section contents. VALSIZE is the size of the value to
196 template<int valsize
>
198 rel(unsigned char* view
,
199 const Sized_relobj
<size
, big_endian
>* object
,
200 const Symbol_value
<size
>* psymval
)
202 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
203 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
204 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
205 x
= psymval
->value(object
, x
);
206 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
209 // Do a simple relocation with the addend in the relocation.
210 // VALSIZE is the size of the value.
211 template<int valsize
>
213 rela(unsigned char* view
,
214 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
215 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
217 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
218 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
219 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
);
222 // Do a simple relocation using a symbol value with the addend in
223 // the relocation. VALSIZE is the size of the value.
224 template<int valsize
>
226 rela(unsigned char* view
,
227 const Sized_relobj
<size
, big_endian
>* object
,
228 const Symbol_value
<size
>* psymval
,
229 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
231 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
232 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
233 Valtype x
= psymval
->value(object
, addend
);
234 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
237 // Do a simple PC relative relocation with the addend in the section
238 // contents. VALSIZE is the size of the value.
239 template<int valsize
>
241 pcrel(unsigned char* view
,
242 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
243 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
245 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
246 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
247 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
248 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
251 // Do a simple PC relative relocation with a Symbol_value with the
252 // addend in the section contents. VALSIZE is the size of the
254 template<int valsize
>
256 pcrel(unsigned char* view
,
257 const Sized_relobj
<size
, big_endian
>* object
,
258 const Symbol_value
<size
>* psymval
,
259 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
261 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
262 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
263 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
264 x
= psymval
->value(object
, x
);
265 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
268 // Do a simple PC relative relocation with the addend in the
269 // relocation. VALSIZE is the size of the value.
270 template<int valsize
>
272 pcrela(unsigned char* view
,
273 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
274 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
275 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
277 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
278 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
279 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
282 // Do a simple PC relative relocation with a Symbol_value with the
283 // addend in the relocation. VALSIZE is the size of the value.
284 template<int valsize
>
286 pcrela(unsigned char* view
,
287 const Sized_relobj
<size
, big_endian
>* object
,
288 const Symbol_value
<size
>* psymval
,
289 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
290 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
292 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
293 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
294 Valtype x
= psymval
->value(object
, addend
);
295 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
298 typedef Relocate_functions
<size
, big_endian
> This
;
301 // Do a simple 8-bit REL relocation with the addend in the section
304 rel8(unsigned char* view
, unsigned char value
)
305 { This::template rel
<8>(view
, value
); }
308 rel8(unsigned char* view
,
309 const Sized_relobj
<size
, big_endian
>* object
,
310 const Symbol_value
<size
>* psymval
)
311 { This::template rel
<8>(view
, object
, psymval
); }
313 // Do an 8-bit RELA relocation with the addend in the relocation.
315 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
316 { This::template rela
<8>(view
, value
, addend
); }
319 rela8(unsigned char* view
,
320 const Sized_relobj
<size
, big_endian
>* object
,
321 const Symbol_value
<size
>* psymval
,
322 unsigned char addend
)
323 { This::template rela
<8>(view
, object
, psymval
, addend
); }
325 // Do a simple 8-bit PC relative relocation with the addend in the
328 pcrel8(unsigned char* view
, unsigned char value
,
329 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
330 { This::template pcrel
<8>(view
, value
, address
); }
333 pcrel8(unsigned char* view
,
334 const Sized_relobj
<size
, big_endian
>* object
,
335 const Symbol_value
<size
>* psymval
,
336 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
337 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
339 // Do a simple 8-bit PC relative RELA relocation with the addend in
342 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
343 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
344 { This::template pcrela
<8>(view
, value
, addend
, address
); }
347 pcrela8(unsigned char* view
,
348 const Sized_relobj
<size
, big_endian
>* object
,
349 const Symbol_value
<size
>* psymval
,
350 unsigned char addend
,
351 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
352 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
354 // Do a simple 16-bit REL relocation with the addend in the section
357 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
358 { This::template rel
<16>(view
, value
); }
361 rel16(unsigned char* view
,
362 const Sized_relobj
<size
, big_endian
>* object
,
363 const Symbol_value
<size
>* psymval
)
364 { This::template rel
<16>(view
, object
, psymval
); }
366 // Do an 16-bit RELA relocation with the addend in the relocation.
368 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
369 { This::template rela
<16>(view
, value
, addend
); }
372 rela16(unsigned char* view
,
373 const Sized_relobj
<size
, big_endian
>* object
,
374 const Symbol_value
<size
>* psymval
,
375 elfcpp::Elf_Half addend
)
376 { This::template rela
<16>(view
, object
, psymval
, addend
); }
378 // Do a simple 16-bit PC relative REL relocation with the addend in
379 // the section contents.
381 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
382 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
383 { This::template pcrel
<16>(view
, value
, address
); }
386 pcrel16(unsigned char* view
,
387 const Sized_relobj
<size
, big_endian
>* object
,
388 const Symbol_value
<size
>* psymval
,
389 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
390 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
392 // Do a simple 16-bit PC relative RELA relocation with the addend in
395 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
396 elfcpp::Elf_Half addend
,
397 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
398 { This::template pcrela
<16>(view
, value
, addend
, address
); }
401 pcrela16(unsigned char* view
,
402 const Sized_relobj
<size
, big_endian
>* object
,
403 const Symbol_value
<size
>* psymval
,
404 elfcpp::Elf_Half addend
,
405 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
406 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
408 // Do a simple 32-bit REL relocation with the addend in the section
411 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
412 { This::template rel
<32>(view
, value
); }
415 rel32(unsigned char* view
,
416 const Sized_relobj
<size
, big_endian
>* object
,
417 const Symbol_value
<size
>* psymval
)
418 { This::template rel
<32>(view
, object
, psymval
); }
420 // Do an 32-bit RELA relocation with the addend in the relocation.
422 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
423 { This::template rela
<32>(view
, value
, addend
); }
426 rela32(unsigned char* view
,
427 const Sized_relobj
<size
, big_endian
>* object
,
428 const Symbol_value
<size
>* psymval
,
429 elfcpp::Elf_Word addend
)
430 { This::template rela
<32>(view
, object
, psymval
, addend
); }
432 // Do a simple 32-bit PC relative REL relocation with the addend in
433 // the section contents.
435 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
436 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
437 { This::template pcrel
<32>(view
, value
, address
); }
440 pcrel32(unsigned char* view
,
441 const Sized_relobj
<size
, big_endian
>* object
,
442 const Symbol_value
<size
>* psymval
,
443 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
444 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
446 // Do a simple 32-bit PC relative RELA relocation with the addend in
449 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
450 elfcpp::Elf_Word addend
,
451 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
452 { This::template pcrela
<32>(view
, value
, addend
, address
); }
455 pcrela32(unsigned char* view
,
456 const Sized_relobj
<size
, big_endian
>* object
,
457 const Symbol_value
<size
>* psymval
,
458 elfcpp::Elf_Word addend
,
459 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
460 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
462 // Do a simple 64-bit REL relocation with the addend in the section
465 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
466 { This::template rel
<64>(view
, value
); }
469 rel64(unsigned char* view
,
470 const Sized_relobj
<size
, big_endian
>* object
,
471 const Symbol_value
<size
>* psymval
)
472 { This::template rel
<64>(view
, object
, psymval
); }
474 // Do a 64-bit RELA relocation with the addend in the relocation.
476 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
477 elfcpp::Elf_Xword addend
)
478 { This::template rela
<64>(view
, value
, addend
); }
481 rela64(unsigned char* view
,
482 const Sized_relobj
<size
, big_endian
>* object
,
483 const Symbol_value
<size
>* psymval
,
484 elfcpp::Elf_Xword addend
)
485 { This::template rela
<64>(view
, object
, psymval
, addend
); }
487 // Do a simple 64-bit PC relative REL relocation with the addend in
488 // the section contents.
490 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
491 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
492 { This::template pcrel
<64>(view
, value
, address
); }
495 pcrel64(unsigned char* view
,
496 const Sized_relobj
<size
, big_endian
>* object
,
497 const Symbol_value
<size
>* psymval
,
498 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
499 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
501 // Do a simple 64-bit PC relative RELA relocation with the addend in
504 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
505 elfcpp::Elf_Xword addend
,
506 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
507 { This::template pcrela
<64>(view
, value
, addend
, address
); }
510 pcrela64(unsigned char* view
,
511 const Sized_relobj
<size
, big_endian
>* object
,
512 const Symbol_value
<size
>* psymval
,
513 elfcpp::Elf_Xword addend
,
514 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
515 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
518 // We try to avoid COPY relocations when possible. A COPY relocation
519 // may be required when an executable refers to a variable defined in
520 // a shared library. COPY relocations are problematic because they
521 // tie the executable to the exact size of the variable in the shared
522 // library. We can avoid them if all the references to the variable
523 // are in a writeable section. In that case we can simply use dynamic
524 // relocations. However, when scanning relocs, we don't know when we
525 // see the relocation whether we will be forced to use a COPY
526 // relocation or not. So we have to save the relocation during the
527 // reloc scanning, and then emit it as a dynamic relocation if
528 // necessary. This class implements that. It is used by the target
531 template<int size
, bool big_endian
>
539 // Return whether we need a COPY reloc for a reloc against GSYM,
540 // which is being applied to section SHNDX in OBJECT.
542 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
543 Sized_symbol
<size
>* gsym
);
545 // Save a Rel against SYM for possible emission later. SHNDX is the
546 // index of the section to which the reloc is being applied.
548 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
549 Output_section
* output_section
, const elfcpp::Rel
<size
, big_endian
>&);
551 // Save a Rela against SYM for possible emission later.
553 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
554 Output_section
* output_section
, const elfcpp::Rela
<size
, big_endian
>&);
556 // Return whether there are any relocs to emit. This also discards
557 // entries which need not be emitted.
561 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
562 // is still defined in the dynamic object).
563 template<int sh_type
>
565 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
568 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
569 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
571 // This POD class holds the entries we are saving.
572 class Copy_reloc_entry
575 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
576 Relobj
* relobj
, unsigned int shndx
,
577 Output_section
* output_section
,
578 Address address
, Addend addend
)
579 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
580 shndx_(shndx
), output_section_(output_section
),
581 address_(address
), addend_(addend
)
584 // Return whether we should emit this reloc. If we should not
585 // emit, we clear it.
592 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
595 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
599 unsigned int reloc_type_
;
602 Output_section
* output_section_
;
607 // A list of relocs to be saved.
608 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
610 // The list of relocs we are saving.
611 Copy_reloc_entries entries_
;
614 // Track relocations while reading a section. This lets you ask for
615 // the relocation at a certain offset, and see how relocs occur
616 // between points of interest.
618 template<int size
, bool big_endian
>
623 : prelocs_(NULL
), len_(0), pos_(0), reloc_size_(0)
626 // Initialize the Track_relocs object. OBJECT is the object holding
627 // the reloc section, RELOC_SHNDX is the section index of the reloc
628 // section, and RELOC_TYPE is the type of the reloc section
629 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
630 // something went wrong.
632 initialize(Object
* object
, unsigned int reloc_shndx
,
633 unsigned int reloc_type
);
635 // Return the offset in the data section to which the next reloc
636 // applies. THis returns -1 if there is no next reloc.
640 // Return the symbol index of the next reloc. This returns -1U if
641 // there is no next reloc.
645 // Advance to OFFSET within the data section, and return the number
646 // of relocs which would be skipped.
648 advance(off_t offset
);
651 // The contents of the input object's reloc section.
652 const unsigned char* prelocs_
;
653 // The length of the reloc section.
654 section_size_type len_
;
655 // Our current position in the reloc section.
656 section_size_type pos_
;
657 // The size of the relocs in the section.
661 } // End namespace gold.
663 #endif // !defined(GOLD_RELOC_H)