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
*);
85 const General_options
& options_
;
86 Symbol_table
* symtab_
;
89 Task_token
* symtab_lock_
;
93 // Scan the relocations for an object to see if they require any
94 // GOT/PLT/COPY relocations.
96 class Scan_relocs
: public Task
99 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
100 // unblocked when the task completes.
101 Scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
102 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
103 Task_token
* symtab_lock
, Task_token
* blocker
)
104 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
105 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
108 // The standard Task methods.
111 is_runnable(Workqueue
*);
123 class Scan_relocs_locker
;
125 const General_options
& options_
;
126 Symbol_table
* symtab_
;
129 Read_relocs_data
* rd_
;
130 Task_token
* symtab_lock_
;
131 Task_token
* blocker_
;
134 // A class to perform all the relocations for an object file.
136 class Relocate_task
: public Task
139 Relocate_task(const General_options
& options
, const Symbol_table
* symtab
,
140 const Layout
* layout
, Relobj
* object
, Output_file
* of
,
141 Task_token
* input_sections_blocker
,
142 Task_token
* output_sections_blocker
, Task_token
* final_blocker
)
143 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
144 of_(of
), input_sections_blocker_(input_sections_blocker
),
145 output_sections_blocker_(output_sections_blocker
),
146 final_blocker_(final_blocker
)
149 // The standard Task methods.
152 is_runnable(Workqueue
*);
164 class Relocate_locker
;
166 const General_options
& options_
;
167 const Symbol_table
* symtab_
;
168 const Layout
* layout_
;
171 Task_token
* input_sections_blocker_
;
172 Task_token
* output_sections_blocker_
;
173 Task_token
* final_blocker_
;
176 // Standard relocation routines which are used on many targets. Here
177 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
179 template<int size
, bool big_endian
>
180 class Relocate_functions
183 // Do a simple relocation with the addend in the section contents.
184 // VALSIZE is the size of the value.
185 template<int valsize
>
187 rel(unsigned char* view
,
188 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
190 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
191 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
192 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
193 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
196 // Do a simple relocation using a Symbol_value with the addend in
197 // the section contents. VALSIZE is the size of the value to
199 template<int valsize
>
201 rel(unsigned char* view
,
202 const Sized_relobj
<size
, big_endian
>* object
,
203 const Symbol_value
<size
>* psymval
)
205 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
206 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
207 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
208 x
= psymval
->value(object
, x
);
209 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
212 // Do a simple relocation with the addend in the relocation.
213 // VALSIZE is the size of the value.
214 template<int valsize
>
216 rela(unsigned char* view
,
217 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
218 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
220 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
221 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
222 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
);
225 // Do a simple relocation using a symbol value with the addend in
226 // the relocation. VALSIZE is the size of the value.
227 template<int valsize
>
229 rela(unsigned char* view
,
230 const Sized_relobj
<size
, big_endian
>* object
,
231 const Symbol_value
<size
>* psymval
,
232 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
234 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
235 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
236 Valtype x
= psymval
->value(object
, addend
);
237 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
240 // Do a simple PC relative relocation with the addend in the section
241 // contents. VALSIZE is the size of the value.
242 template<int valsize
>
244 pcrel(unsigned char* view
,
245 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
246 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
248 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
249 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
250 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
251 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
254 // Do a simple PC relative relocation with a Symbol_value with the
255 // addend in the section contents. VALSIZE is the size of the
257 template<int valsize
>
259 pcrel(unsigned char* view
,
260 const Sized_relobj
<size
, big_endian
>* object
,
261 const Symbol_value
<size
>* psymval
,
262 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
264 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
265 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
266 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
267 x
= psymval
->value(object
, x
);
268 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
271 // Do a simple PC relative relocation with the addend in the
272 // relocation. VALSIZE is the size of the value.
273 template<int valsize
>
275 pcrela(unsigned char* view
,
276 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
277 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
278 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
280 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
281 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
282 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
285 // Do a simple PC relative relocation with a Symbol_value with the
286 // addend in the relocation. VALSIZE is the size of the value.
287 template<int valsize
>
289 pcrela(unsigned char* view
,
290 const Sized_relobj
<size
, big_endian
>* object
,
291 const Symbol_value
<size
>* psymval
,
292 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
293 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
295 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
296 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
297 Valtype x
= psymval
->value(object
, addend
);
298 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
301 typedef Relocate_functions
<size
, big_endian
> This
;
304 // Do a simple 8-bit REL relocation with the addend in the section
307 rel8(unsigned char* view
, unsigned char value
)
308 { This::template rel
<8>(view
, value
); }
311 rel8(unsigned char* view
,
312 const Sized_relobj
<size
, big_endian
>* object
,
313 const Symbol_value
<size
>* psymval
)
314 { This::template rel
<8>(view
, object
, psymval
); }
316 // Do an 8-bit RELA relocation with the addend in the relocation.
318 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
319 { This::template rela
<8>(view
, value
, addend
); }
322 rela8(unsigned char* view
,
323 const Sized_relobj
<size
, big_endian
>* object
,
324 const Symbol_value
<size
>* psymval
,
325 unsigned char addend
)
326 { This::template rela
<8>(view
, object
, psymval
, addend
); }
328 // Do a simple 8-bit PC relative relocation with the addend in the
331 pcrel8(unsigned char* view
, unsigned char value
,
332 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
333 { This::template pcrel
<8>(view
, value
, address
); }
336 pcrel8(unsigned char* view
,
337 const Sized_relobj
<size
, big_endian
>* object
,
338 const Symbol_value
<size
>* psymval
,
339 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
340 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
342 // Do a simple 8-bit PC relative RELA relocation with the addend in
345 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
346 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
347 { This::template pcrela
<8>(view
, value
, addend
, address
); }
350 pcrela8(unsigned char* view
,
351 const Sized_relobj
<size
, big_endian
>* object
,
352 const Symbol_value
<size
>* psymval
,
353 unsigned char addend
,
354 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
355 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
357 // Do a simple 16-bit REL relocation with the addend in the section
360 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
361 { This::template rel
<16>(view
, value
); }
364 rel16(unsigned char* view
,
365 const Sized_relobj
<size
, big_endian
>* object
,
366 const Symbol_value
<size
>* psymval
)
367 { This::template rel
<16>(view
, object
, psymval
); }
369 // Do an 16-bit RELA relocation with the addend in the relocation.
371 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
372 { This::template rela
<16>(view
, value
, addend
); }
375 rela16(unsigned char* view
,
376 const Sized_relobj
<size
, big_endian
>* object
,
377 const Symbol_value
<size
>* psymval
,
378 elfcpp::Elf_Half addend
)
379 { This::template rela
<16>(view
, object
, psymval
, addend
); }
381 // Do a simple 16-bit PC relative REL relocation with the addend in
382 // the section contents.
384 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
385 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
386 { This::template pcrel
<16>(view
, value
, address
); }
389 pcrel16(unsigned char* view
,
390 const Sized_relobj
<size
, big_endian
>* object
,
391 const Symbol_value
<size
>* psymval
,
392 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
393 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
395 // Do a simple 16-bit PC relative RELA relocation with the addend in
398 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
399 elfcpp::Elf_Half addend
,
400 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
401 { This::template pcrela
<16>(view
, value
, addend
, address
); }
404 pcrela16(unsigned char* view
,
405 const Sized_relobj
<size
, big_endian
>* object
,
406 const Symbol_value
<size
>* psymval
,
407 elfcpp::Elf_Half addend
,
408 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
409 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
411 // Do a simple 32-bit REL relocation with the addend in the section
414 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
415 { This::template rel
<32>(view
, value
); }
418 rel32(unsigned char* view
,
419 const Sized_relobj
<size
, big_endian
>* object
,
420 const Symbol_value
<size
>* psymval
)
421 { This::template rel
<32>(view
, object
, psymval
); }
423 // Do an 32-bit RELA relocation with the addend in the relocation.
425 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
426 { This::template rela
<32>(view
, value
, addend
); }
429 rela32(unsigned char* view
,
430 const Sized_relobj
<size
, big_endian
>* object
,
431 const Symbol_value
<size
>* psymval
,
432 elfcpp::Elf_Word addend
)
433 { This::template rela
<32>(view
, object
, psymval
, addend
); }
435 // Do a simple 32-bit PC relative REL relocation with the addend in
436 // the section contents.
438 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
439 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
440 { This::template pcrel
<32>(view
, value
, address
); }
443 pcrel32(unsigned char* view
,
444 const Sized_relobj
<size
, big_endian
>* object
,
445 const Symbol_value
<size
>* psymval
,
446 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
447 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
449 // Do a simple 32-bit PC relative RELA relocation with the addend in
452 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
453 elfcpp::Elf_Word addend
,
454 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
455 { This::template pcrela
<32>(view
, value
, addend
, address
); }
458 pcrela32(unsigned char* view
,
459 const Sized_relobj
<size
, big_endian
>* object
,
460 const Symbol_value
<size
>* psymval
,
461 elfcpp::Elf_Word addend
,
462 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
463 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
465 // Do a simple 64-bit REL relocation with the addend in the section
468 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
469 { This::template rel
<64>(view
, value
); }
472 rel64(unsigned char* view
,
473 const Sized_relobj
<size
, big_endian
>* object
,
474 const Symbol_value
<size
>* psymval
)
475 { This::template rel
<64>(view
, object
, psymval
); }
477 // Do a 64-bit RELA relocation with the addend in the relocation.
479 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
480 elfcpp::Elf_Xword addend
)
481 { This::template rela
<64>(view
, value
, addend
); }
484 rela64(unsigned char* view
,
485 const Sized_relobj
<size
, big_endian
>* object
,
486 const Symbol_value
<size
>* psymval
,
487 elfcpp::Elf_Xword addend
)
488 { This::template rela
<64>(view
, object
, psymval
, addend
); }
490 // Do a simple 64-bit PC relative REL relocation with the addend in
491 // the section contents.
493 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
494 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
495 { This::template pcrel
<64>(view
, value
, address
); }
498 pcrel64(unsigned char* view
,
499 const Sized_relobj
<size
, big_endian
>* object
,
500 const Symbol_value
<size
>* psymval
,
501 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
502 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
504 // Do a simple 64-bit PC relative RELA relocation with the addend in
507 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
508 elfcpp::Elf_Xword addend
,
509 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
510 { This::template pcrela
<64>(view
, value
, addend
, address
); }
513 pcrela64(unsigned char* view
,
514 const Sized_relobj
<size
, big_endian
>* object
,
515 const Symbol_value
<size
>* psymval
,
516 elfcpp::Elf_Xword addend
,
517 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
518 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
521 // We try to avoid COPY relocations when possible. A COPY relocation
522 // may be required when an executable refers to a variable defined in
523 // a shared library. COPY relocations are problematic because they
524 // tie the executable to the exact size of the variable in the shared
525 // library. We can avoid them if all the references to the variable
526 // are in a writeable section. In that case we can simply use dynamic
527 // relocations. However, when scanning relocs, we don't know when we
528 // see the relocation whether we will be forced to use a COPY
529 // relocation or not. So we have to save the relocation during the
530 // reloc scanning, and then emit it as a dynamic relocation if
531 // necessary. This class implements that. It is used by the target
534 template<int size
, bool big_endian
>
542 // Return whether we need a COPY reloc for a reloc against GSYM,
543 // which is being applied to section SHNDX in OBJECT.
545 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
546 Sized_symbol
<size
>* gsym
);
548 // Save a Rel against SYM for possible emission later. SHNDX is the
549 // index of the section to which the reloc is being applied.
551 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
552 Output_section
* output_section
, const elfcpp::Rel
<size
, big_endian
>&);
554 // Save a Rela against SYM for possible emission later.
556 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
557 Output_section
* output_section
, const elfcpp::Rela
<size
, big_endian
>&);
559 // Return whether there are any relocs to emit. This also discards
560 // entries which need not be emitted.
564 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
565 // is still defined in the dynamic object).
566 template<int sh_type
>
568 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
571 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
572 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
574 // This POD class holds the entries we are saving.
575 class Copy_reloc_entry
578 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
579 Relobj
* relobj
, unsigned int shndx
,
580 Output_section
* output_section
,
581 Address address
, Addend addend
)
582 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
583 shndx_(shndx
), output_section_(output_section
),
584 address_(address
), addend_(addend
)
587 // Return whether we should emit this reloc. If we should not
588 // emit, we clear it.
595 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
598 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
602 unsigned int reloc_type_
;
605 Output_section
* output_section_
;
610 // A list of relocs to be saved.
611 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
613 // The list of relocs we are saving.
614 Copy_reloc_entries entries_
;
617 // Track relocations while reading a section. This lets you ask for
618 // the relocation at a certain offset, and see how relocs occur
619 // between points of interest.
621 template<int size
, bool big_endian
>
626 : prelocs_(NULL
), len_(0), pos_(0), reloc_size_(0)
629 // Initialize the Track_relocs object. OBJECT is the object holding
630 // the reloc section, RELOC_SHNDX is the section index of the reloc
631 // section, and RELOC_TYPE is the type of the reloc section
632 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
633 // something went wrong.
635 initialize(Object
* object
, unsigned int reloc_shndx
,
636 unsigned int reloc_type
);
638 // Return the offset in the data section to which the next reloc
639 // applies. THis returns -1 if there is no next reloc.
643 // Return the symbol index of the next reloc. This returns -1U if
644 // there is no next reloc.
648 // Advance to OFFSET within the data section, and return the number
649 // of relocs which would be skipped.
651 advance(off_t offset
);
654 // The contents of the input object's reloc section.
655 const unsigned char* prelocs_
;
656 // The length of the reloc section.
658 // Our current position in the reloc section.
660 // The size of the relocs in the section.
664 } // End namespace gold.
666 #endif // !defined(GOLD_RELOC_H)