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.
28 #include "workqueue.h"
33 class General_options
;
35 class Read_relocs_data
;
42 template<int size
, bool big_endian
>
48 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
49 class Output_data_reloc
;
51 // A class to read the relocations for an object file, and then queue
52 // up a task to see if they require any GOT/PLT/COPY relocations in
55 class Read_relocs
: public Task
58 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
59 // unblocked when the Scan_relocs task completes.
60 Read_relocs(const General_options
& options
, Symbol_table
* symtab
,
61 Layout
* layout
, Relobj
* object
, Task_token
* symtab_lock
,
63 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
64 symtab_lock_(symtab_lock
), blocker_(blocker
)
67 // The standard Task methods.
70 is_runnable(Workqueue
*);
79 const General_options
& options_
;
80 Symbol_table
* symtab_
;
83 Task_token
* symtab_lock_
;
87 // Scan the relocations for an object to see if they require any
88 // GOT/PLT/COPY relocations.
90 class Scan_relocs
: public Task
93 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
94 // unblocked when the task completes.
95 Scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
96 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
97 Task_token
* symtab_lock
, Task_token
* blocker
)
98 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
99 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
102 // The standard Task methods.
105 is_runnable(Workqueue
*);
114 class Scan_relocs_locker
;
116 const General_options
& options_
;
117 Symbol_table
* symtab_
;
120 Read_relocs_data
* rd_
;
121 Task_token
* symtab_lock_
;
122 Task_token
* blocker_
;
125 // A class to perform all the relocations for an object file.
127 class Relocate_task
: public Task
130 Relocate_task(const General_options
& options
, const Symbol_table
* symtab
,
131 const Layout
* layout
, Relobj
* object
, Output_file
* of
,
132 Task_token
* input_sections_blocker
,
133 Task_token
* output_sections_blocker
, Task_token
* final_blocker
)
134 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
135 of_(of
), input_sections_blocker_(input_sections_blocker
),
136 output_sections_blocker_(output_sections_blocker
),
137 final_blocker_(final_blocker
)
140 // The standard Task methods.
143 is_runnable(Workqueue
*);
152 class Relocate_locker
;
154 const General_options
& options_
;
155 const Symbol_table
* symtab_
;
156 const Layout
* layout_
;
159 Task_token
* input_sections_blocker_
;
160 Task_token
* output_sections_blocker_
;
161 Task_token
* final_blocker_
;
164 // Standard relocation routines which are used on many targets. Here
165 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
167 template<int size
, bool big_endian
>
168 class Relocate_functions
171 // Do a simple relocation with the addend in the section contents.
172 // VALSIZE is the size of the value.
173 template<int valsize
>
175 rel(unsigned char* view
,
176 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
178 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
179 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
180 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
181 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
184 // Do a simple relocation using a Symbol_value with the addend in
185 // the section contents. VALSIZE is the size of the value to
187 template<int valsize
>
189 rel(unsigned char* view
,
190 const Sized_relobj
<size
, big_endian
>* object
,
191 const Symbol_value
<size
>* psymval
)
193 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
194 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
195 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
196 x
= psymval
->value(object
, x
);
197 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
200 // Do a simple relocation with the addend in the relocation.
201 // VALSIZE is the size of the value.
202 template<int valsize
>
204 rela(unsigned char* view
,
205 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
206 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
208 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
209 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
210 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
);
213 // Do a simple relocation using a symbol value with the addend in
214 // the relocation. VALSIZE is the size of the value.
215 template<int valsize
>
217 rela(unsigned char* view
,
218 const Sized_relobj
<size
, big_endian
>* object
,
219 const Symbol_value
<size
>* psymval
,
220 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
222 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
223 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
224 Valtype x
= psymval
->value(object
, addend
);
225 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
228 // Do a simple PC relative relocation with the addend in the section
229 // contents. VALSIZE is the size of the value.
230 template<int valsize
>
232 pcrel(unsigned char* view
,
233 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
234 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
236 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
237 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
238 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
239 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
242 // Do a simple PC relative relocation with a Symbol_value with the
243 // addend in the section contents. VALSIZE is the size of the
245 template<int valsize
>
247 pcrel(unsigned char* view
,
248 const Sized_relobj
<size
, big_endian
>* object
,
249 const Symbol_value
<size
>* psymval
,
250 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
252 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
253 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
254 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
255 x
= psymval
->value(object
, x
);
256 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
259 // Do a simple PC relative relocation with the addend in the
260 // relocation. VALSIZE is the size of the value.
261 template<int valsize
>
263 pcrela(unsigned char* view
,
264 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
265 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
266 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
268 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
269 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
270 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
273 // Do a simple PC relative relocation with a Symbol_value with the
274 // addend in the relocation. VALSIZE is the size of the value.
275 template<int valsize
>
277 pcrela(unsigned char* view
,
278 const Sized_relobj
<size
, big_endian
>* object
,
279 const Symbol_value
<size
>* psymval
,
280 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
281 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
283 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
284 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
285 Valtype x
= psymval
->value(object
, addend
);
286 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
289 typedef Relocate_functions
<size
, big_endian
> This
;
292 // Do a simple 8-bit REL relocation with the addend in the section
295 rel8(unsigned char* view
, unsigned char value
)
296 { This::template rel
<8>(view
, value
); }
299 rel8(unsigned char* view
,
300 const Sized_relobj
<size
, big_endian
>* object
,
301 const Symbol_value
<size
>* psymval
)
302 { This::template rel
<8>(view
, object
, psymval
); }
304 // Do an 8-bit RELA relocation with the addend in the relocation.
306 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
307 { This::template rela
<8>(view
, value
, addend
); }
310 rela8(unsigned char* view
,
311 const Sized_relobj
<size
, big_endian
>* object
,
312 const Symbol_value
<size
>* psymval
,
313 unsigned char addend
)
314 { This::template rela
<8>(view
, object
, psymval
, addend
); }
316 // Do a simple 8-bit PC relative relocation with the addend in the
319 pcrel8(unsigned char* view
, unsigned char value
,
320 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
321 { This::template pcrel
<8>(view
, value
, address
); }
324 pcrel8(unsigned char* view
,
325 const Sized_relobj
<size
, big_endian
>* object
,
326 const Symbol_value
<size
>* psymval
,
327 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
328 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
330 // Do a simple 8-bit PC relative RELA relocation with the addend in
333 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
334 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
335 { This::template pcrela
<8>(view
, value
, addend
, address
); }
338 pcrela8(unsigned char* view
,
339 const Sized_relobj
<size
, big_endian
>* object
,
340 const Symbol_value
<size
>* psymval
,
341 unsigned char addend
,
342 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
343 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
345 // Do a simple 16-bit REL relocation with the addend in the section
348 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
349 { This::template rel
<16>(view
, value
); }
352 rel16(unsigned char* view
,
353 const Sized_relobj
<size
, big_endian
>* object
,
354 const Symbol_value
<size
>* psymval
)
355 { This::template rel
<16>(view
, object
, psymval
); }
357 // Do an 16-bit RELA relocation with the addend in the relocation.
359 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
360 { This::template rela
<16>(view
, value
, addend
); }
363 rela16(unsigned char* view
,
364 const Sized_relobj
<size
, big_endian
>* object
,
365 const Symbol_value
<size
>* psymval
,
366 elfcpp::Elf_Half addend
)
367 { This::template rela
<16>(view
, object
, psymval
, addend
); }
369 // Do a simple 16-bit PC relative REL relocation with the addend in
370 // the section contents.
372 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
373 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
374 { This::template pcrel
<16>(view
, value
, address
); }
377 pcrel16(unsigned char* view
,
378 const Sized_relobj
<size
, big_endian
>* object
,
379 const Symbol_value
<size
>* psymval
,
380 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
381 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
383 // Do a simple 16-bit PC relative RELA relocation with the addend in
386 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
387 elfcpp::Elf_Half addend
,
388 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
389 { This::template pcrela
<16>(view
, value
, addend
, address
); }
392 pcrela16(unsigned char* view
,
393 const Sized_relobj
<size
, big_endian
>* object
,
394 const Symbol_value
<size
>* psymval
,
395 elfcpp::Elf_Half addend
,
396 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
397 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
399 // Do a simple 32-bit REL relocation with the addend in the section
402 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
403 { This::template rel
<32>(view
, value
); }
406 rel32(unsigned char* view
,
407 const Sized_relobj
<size
, big_endian
>* object
,
408 const Symbol_value
<size
>* psymval
)
409 { This::template rel
<32>(view
, object
, psymval
); }
411 // Do an 32-bit RELA relocation with the addend in the relocation.
413 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
414 { This::template rela
<32>(view
, value
, addend
); }
417 rela32(unsigned char* view
,
418 const Sized_relobj
<size
, big_endian
>* object
,
419 const Symbol_value
<size
>* psymval
,
420 elfcpp::Elf_Word addend
)
421 { This::template rela
<32>(view
, object
, psymval
, addend
); }
423 // Do a simple 32-bit PC relative REL relocation with the addend in
424 // the section contents.
426 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
427 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
428 { This::template pcrel
<32>(view
, value
, address
); }
431 pcrel32(unsigned char* view
,
432 const Sized_relobj
<size
, big_endian
>* object
,
433 const Symbol_value
<size
>* psymval
,
434 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
435 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
437 // Do a simple 32-bit PC relative RELA relocation with the addend in
440 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
441 elfcpp::Elf_Word addend
,
442 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
443 { This::template pcrela
<32>(view
, value
, addend
, address
); }
446 pcrela32(unsigned char* view
,
447 const Sized_relobj
<size
, big_endian
>* object
,
448 const Symbol_value
<size
>* psymval
,
449 elfcpp::Elf_Word addend
,
450 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
451 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
453 // Do a simple 64-bit REL relocation with the addend in the section
456 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
457 { This::template rel
<64>(view
, value
); }
460 rel64(unsigned char* view
,
461 const Sized_relobj
<size
, big_endian
>* object
,
462 const Symbol_value
<size
>* psymval
)
463 { This::template rel
<64>(view
, object
, psymval
); }
465 // Do a 64-bit RELA relocation with the addend in the relocation.
467 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
468 elfcpp::Elf_Xword addend
)
469 { This::template rela
<64>(view
, value
, addend
); }
472 rela64(unsigned char* view
,
473 const Sized_relobj
<size
, big_endian
>* object
,
474 const Symbol_value
<size
>* psymval
,
475 elfcpp::Elf_Xword addend
)
476 { This::template rela
<64>(view
, object
, psymval
, addend
); }
478 // Do a simple 64-bit PC relative REL relocation with the addend in
479 // the section contents.
481 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
482 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
483 { This::template pcrel
<64>(view
, value
, address
); }
486 pcrel64(unsigned char* view
,
487 const Sized_relobj
<size
, big_endian
>* object
,
488 const Symbol_value
<size
>* psymval
,
489 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
490 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
492 // Do a simple 64-bit PC relative RELA relocation with the addend in
495 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
496 elfcpp::Elf_Xword addend
,
497 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
498 { This::template pcrela
<64>(view
, value
, addend
, address
); }
501 pcrela64(unsigned char* view
,
502 const Sized_relobj
<size
, big_endian
>* object
,
503 const Symbol_value
<size
>* psymval
,
504 elfcpp::Elf_Xword addend
,
505 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
506 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
509 // We try to avoid COPY relocations when possible. A COPY relocation
510 // may be required when an executable refers to a variable defined in
511 // a shared library. COPY relocations are problematic because they
512 // tie the executable to the exact size of the variable in the shared
513 // library. We can avoid them if all the references to the variable
514 // are in a writeable section. In that case we can simply use dynamic
515 // relocations. However, when scanning relocs, we don't know when we
516 // see the relocation whether we will be forced to use a COPY
517 // relocation or not. So we have to save the relocation during the
518 // reloc scanning, and then emit it as a dynamic relocation if
519 // necessary. This class implements that. It is used by the target
522 template<int size
, bool big_endian
>
530 // Return whether we need a COPY reloc for a reloc against GSYM,
531 // which is being applied to section SHNDX in OBJECT.
533 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
534 Sized_symbol
<size
>* gsym
);
536 // Save a Rel against SYM for possible emission later. SHNDX is the
537 // index of the section to which the reloc is being applied.
539 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
540 const elfcpp::Rel
<size
, big_endian
>&);
542 // Save a Rela against SYM for possible emission later.
544 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
545 const elfcpp::Rela
<size
, big_endian
>&);
547 // Return whether there are any relocs to emit. This also discards
548 // entries which need not be emitted.
552 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
553 // is still defined in the dynamic object).
554 template<int sh_type
>
556 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
559 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
560 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
562 // This POD class holds the entries we are saving.
563 class Copy_reloc_entry
566 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
567 Relobj
* relobj
, unsigned int shndx
,
568 Address address
, Addend addend
)
569 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
570 shndx_(shndx
), address_(address
), addend_(addend
)
573 // Return whether we should emit this reloc. If we should not
574 // emit, we clear it.
581 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
584 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
588 unsigned int reloc_type_
;
595 // A list of relocs to be saved.
596 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
598 // The list of relocs we are saving.
599 Copy_reloc_entries entries_
;
602 // Track relocations while reading a section. This lets you ask for
603 // the relocation at a certain offset, and see how relocs occur
604 // between points of interest.
606 template<int size
, bool big_endian
>
611 : object_(NULL
), prelocs_(NULL
), len_(0), pos_(0), reloc_size_(0)
614 // Initialize the Track_relocs object. OBJECT is the object holding
615 // the reloc section, RELOC_SHNDX is the section index of the reloc
616 // section, and RELOC_TYPE is the type of the reloc section
617 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
618 // something went wrong.
620 initialize(Sized_relobj
<size
, big_endian
>* object
, unsigned int reloc_shndx
,
621 unsigned int reloc_type
);
623 // Return the offset in the data section to which the next reloc
624 // applies. THis returns -1 if there is no next reloc.
628 // Return the symbol index of the next reloc. This returns -1U if
629 // there is no next reloc.
633 // Advance to OFFSET within the data section, and return the number
634 // of relocs which would be skipped.
636 advance(off_t offset
);
640 Sized_relobj
<size
, big_endian
>* object_
;
641 // The contents of the reloc section.
642 const unsigned char* prelocs_
;
643 // The length of the reloc section.
645 // Our current position in the reloc section.
647 // The size of the relocs in the section.
651 } // End namespace gold.
653 #endif // !defined(GOLD_RELOC_H)