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
;
36 class Read_relocs_data
;
43 template<int size
, bool big_endian
>
49 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
50 class Output_data_reloc
;
52 // A class to read the relocations for an object file, and then queue
53 // up a task to see if they require any GOT/PLT/COPY relocations in
56 class Read_relocs
: public Task
59 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
60 // unblocked when the Scan_relocs task completes.
61 Read_relocs(const General_options
& options
, Symbol_table
* symtab
,
62 Layout
* layout
, Relobj
* object
, Task_token
* symtab_lock
,
64 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
65 symtab_lock_(symtab_lock
), blocker_(blocker
)
68 // The standard Task methods.
71 is_runnable(Workqueue
*);
80 const General_options
& options_
;
81 Symbol_table
* symtab_
;
84 Task_token
* symtab_lock_
;
88 // Scan the relocations for an object to see if they require any
89 // GOT/PLT/COPY relocations.
91 class Scan_relocs
: public Task
94 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
95 // unblocked when the task completes.
96 Scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
97 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
98 Task_token
* symtab_lock
, Task_token
* blocker
)
99 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
100 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
103 // The standard Task methods.
106 is_runnable(Workqueue
*);
115 class Scan_relocs_locker
;
117 const General_options
& options_
;
118 Symbol_table
* symtab_
;
121 Read_relocs_data
* rd_
;
122 Task_token
* symtab_lock_
;
123 Task_token
* blocker_
;
126 // A class to perform all the relocations for an object file.
128 class Relocate_task
: public Task
131 Relocate_task(const General_options
& options
, const Symbol_table
* symtab
,
132 const Layout
* layout
, Relobj
* object
, Output_file
* of
,
133 Task_token
* input_sections_blocker
,
134 Task_token
* output_sections_blocker
, Task_token
* final_blocker
)
135 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
136 of_(of
), input_sections_blocker_(input_sections_blocker
),
137 output_sections_blocker_(output_sections_blocker
),
138 final_blocker_(final_blocker
)
141 // The standard Task methods.
144 is_runnable(Workqueue
*);
153 class Relocate_locker
;
155 const General_options
& options_
;
156 const Symbol_table
* symtab_
;
157 const Layout
* layout_
;
160 Task_token
* input_sections_blocker_
;
161 Task_token
* output_sections_blocker_
;
162 Task_token
* final_blocker_
;
165 // Standard relocation routines which are used on many targets. Here
166 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
168 template<int size
, bool big_endian
>
169 class Relocate_functions
172 // Do a simple relocation with the addend in the section contents.
173 // VALSIZE is the size of the value.
174 template<int valsize
>
176 rel(unsigned char* view
,
177 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
179 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
180 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
181 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
182 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
185 // Do a simple relocation using a Symbol_value with the addend in
186 // the section contents. VALSIZE is the size of the value to
188 template<int valsize
>
190 rel(unsigned char* view
,
191 const Sized_relobj
<size
, big_endian
>* object
,
192 const Symbol_value
<size
>* psymval
)
194 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
195 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
196 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
197 x
= psymval
->value(object
, x
);
198 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
201 // Do a simple relocation with the addend in the relocation.
202 // VALSIZE is the size of the value.
203 template<int valsize
>
205 rela(unsigned char* view
,
206 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
207 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
209 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
210 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
211 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
);
214 // Do a simple relocation using a symbol value with the addend in
215 // the relocation. VALSIZE is the size of the value.
216 template<int valsize
>
218 rela(unsigned char* view
,
219 const Sized_relobj
<size
, big_endian
>* object
,
220 const Symbol_value
<size
>* psymval
,
221 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
223 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
224 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
225 Valtype x
= psymval
->value(object
, addend
);
226 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
229 // Do a simple PC relative relocation with the addend in the section
230 // contents. VALSIZE is the size of the value.
231 template<int valsize
>
233 pcrel(unsigned char* view
,
234 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
235 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
237 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
238 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
239 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
240 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
243 // Do a simple PC relative relocation with a Symbol_value with the
244 // addend in the section contents. VALSIZE is the size of the
246 template<int valsize
>
248 pcrel(unsigned char* view
,
249 const Sized_relobj
<size
, big_endian
>* object
,
250 const Symbol_value
<size
>* psymval
,
251 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
253 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
254 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
255 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
256 x
= psymval
->value(object
, x
);
257 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
260 // Do a simple PC relative relocation with the addend in the
261 // relocation. VALSIZE is the size of the value.
262 template<int valsize
>
264 pcrela(unsigned char* view
,
265 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
266 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
267 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
269 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
270 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
271 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
274 // Do a simple PC relative relocation with a Symbol_value with the
275 // addend in the relocation. VALSIZE is the size of the value.
276 template<int valsize
>
278 pcrela(unsigned char* view
,
279 const Sized_relobj
<size
, big_endian
>* object
,
280 const Symbol_value
<size
>* psymval
,
281 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
282 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
284 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
285 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
286 Valtype x
= psymval
->value(object
, addend
);
287 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
290 typedef Relocate_functions
<size
, big_endian
> This
;
293 // Do a simple 8-bit REL relocation with the addend in the section
296 rel8(unsigned char* view
, unsigned char value
)
297 { This::template rel
<8>(view
, value
); }
300 rel8(unsigned char* view
,
301 const Sized_relobj
<size
, big_endian
>* object
,
302 const Symbol_value
<size
>* psymval
)
303 { This::template rel
<8>(view
, object
, psymval
); }
305 // Do an 8-bit RELA relocation with the addend in the relocation.
307 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
308 { This::template rela
<8>(view
, value
, addend
); }
311 rela8(unsigned char* view
,
312 const Sized_relobj
<size
, big_endian
>* object
,
313 const Symbol_value
<size
>* psymval
,
314 unsigned char addend
)
315 { This::template rela
<8>(view
, object
, psymval
, addend
); }
317 // Do a simple 8-bit PC relative relocation with the addend in the
320 pcrel8(unsigned char* view
, unsigned char value
,
321 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
322 { This::template pcrel
<8>(view
, value
, address
); }
325 pcrel8(unsigned char* view
,
326 const Sized_relobj
<size
, big_endian
>* object
,
327 const Symbol_value
<size
>* psymval
,
328 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
329 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
331 // Do a simple 8-bit PC relative RELA relocation with the addend in
334 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
335 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
336 { This::template pcrela
<8>(view
, value
, addend
, address
); }
339 pcrela8(unsigned char* view
,
340 const Sized_relobj
<size
, big_endian
>* object
,
341 const Symbol_value
<size
>* psymval
,
342 unsigned char addend
,
343 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
344 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
346 // Do a simple 16-bit REL relocation with the addend in the section
349 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
350 { This::template rel
<16>(view
, value
); }
353 rel16(unsigned char* view
,
354 const Sized_relobj
<size
, big_endian
>* object
,
355 const Symbol_value
<size
>* psymval
)
356 { This::template rel
<16>(view
, object
, psymval
); }
358 // Do an 16-bit RELA relocation with the addend in the relocation.
360 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
361 { This::template rela
<16>(view
, value
, addend
); }
364 rela16(unsigned char* view
,
365 const Sized_relobj
<size
, big_endian
>* object
,
366 const Symbol_value
<size
>* psymval
,
367 elfcpp::Elf_Half addend
)
368 { This::template rela
<16>(view
, object
, psymval
, addend
); }
370 // Do a simple 16-bit PC relative REL relocation with the addend in
371 // the section contents.
373 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
374 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
375 { This::template pcrel
<16>(view
, value
, address
); }
378 pcrel16(unsigned char* view
,
379 const Sized_relobj
<size
, big_endian
>* object
,
380 const Symbol_value
<size
>* psymval
,
381 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
382 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
384 // Do a simple 16-bit PC relative RELA relocation with the addend in
387 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
388 elfcpp::Elf_Half addend
,
389 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
390 { This::template pcrela
<16>(view
, value
, addend
, address
); }
393 pcrela16(unsigned char* view
,
394 const Sized_relobj
<size
, big_endian
>* object
,
395 const Symbol_value
<size
>* psymval
,
396 elfcpp::Elf_Half addend
,
397 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
398 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
400 // Do a simple 32-bit REL relocation with the addend in the section
403 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
404 { This::template rel
<32>(view
, value
); }
407 rel32(unsigned char* view
,
408 const Sized_relobj
<size
, big_endian
>* object
,
409 const Symbol_value
<size
>* psymval
)
410 { This::template rel
<32>(view
, object
, psymval
); }
412 // Do an 32-bit RELA relocation with the addend in the relocation.
414 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
415 { This::template rela
<32>(view
, value
, addend
); }
418 rela32(unsigned char* view
,
419 const Sized_relobj
<size
, big_endian
>* object
,
420 const Symbol_value
<size
>* psymval
,
421 elfcpp::Elf_Word addend
)
422 { This::template rela
<32>(view
, object
, psymval
, addend
); }
424 // Do a simple 32-bit PC relative REL relocation with the addend in
425 // the section contents.
427 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
428 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
429 { This::template pcrel
<32>(view
, value
, address
); }
432 pcrel32(unsigned char* view
,
433 const Sized_relobj
<size
, big_endian
>* object
,
434 const Symbol_value
<size
>* psymval
,
435 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
436 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
438 // Do a simple 32-bit PC relative RELA relocation with the addend in
441 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
442 elfcpp::Elf_Word addend
,
443 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
444 { This::template pcrela
<32>(view
, value
, addend
, address
); }
447 pcrela32(unsigned char* view
,
448 const Sized_relobj
<size
, big_endian
>* object
,
449 const Symbol_value
<size
>* psymval
,
450 elfcpp::Elf_Word addend
,
451 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
452 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
454 // Do a simple 64-bit REL relocation with the addend in the section
457 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
458 { This::template rel
<64>(view
, value
); }
461 rel64(unsigned char* view
,
462 const Sized_relobj
<size
, big_endian
>* object
,
463 const Symbol_value
<size
>* psymval
)
464 { This::template rel
<64>(view
, object
, psymval
); }
466 // Do a 64-bit RELA relocation with the addend in the relocation.
468 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
469 elfcpp::Elf_Xword addend
)
470 { This::template rela
<64>(view
, value
, addend
); }
473 rela64(unsigned char* view
,
474 const Sized_relobj
<size
, big_endian
>* object
,
475 const Symbol_value
<size
>* psymval
,
476 elfcpp::Elf_Xword addend
)
477 { This::template rela
<64>(view
, object
, psymval
, addend
); }
479 // Do a simple 64-bit PC relative REL relocation with the addend in
480 // the section contents.
482 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
483 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
484 { This::template pcrel
<64>(view
, value
, address
); }
487 pcrel64(unsigned char* view
,
488 const Sized_relobj
<size
, big_endian
>* object
,
489 const Symbol_value
<size
>* psymval
,
490 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
491 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
493 // Do a simple 64-bit PC relative RELA relocation with the addend in
496 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
497 elfcpp::Elf_Xword addend
,
498 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
499 { This::template pcrela
<64>(view
, value
, addend
, address
); }
502 pcrela64(unsigned char* view
,
503 const Sized_relobj
<size
, big_endian
>* object
,
504 const Symbol_value
<size
>* psymval
,
505 elfcpp::Elf_Xword addend
,
506 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
507 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
510 // We try to avoid COPY relocations when possible. A COPY relocation
511 // may be required when an executable refers to a variable defined in
512 // a shared library. COPY relocations are problematic because they
513 // tie the executable to the exact size of the variable in the shared
514 // library. We can avoid them if all the references to the variable
515 // are in a writeable section. In that case we can simply use dynamic
516 // relocations. However, when scanning relocs, we don't know when we
517 // see the relocation whether we will be forced to use a COPY
518 // relocation or not. So we have to save the relocation during the
519 // reloc scanning, and then emit it as a dynamic relocation if
520 // necessary. This class implements that. It is used by the target
523 template<int size
, bool big_endian
>
531 // Return whether we need a COPY reloc for a reloc against GSYM,
532 // which is being applied to section SHNDX in OBJECT.
534 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
535 Sized_symbol
<size
>* gsym
);
537 // Save a Rel against SYM for possible emission later. SHNDX is the
538 // index of the section to which the reloc is being applied.
540 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
541 const elfcpp::Rel
<size
, big_endian
>&);
543 // Save a Rela against SYM for possible emission later.
545 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
546 const elfcpp::Rela
<size
, big_endian
>&);
548 // Return whether there are any relocs to emit. This also discards
549 // entries which need not be emitted.
553 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
554 // is still defined in the dynamic object).
555 template<int sh_type
>
557 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
560 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
561 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
563 // This POD class holds the entries we are saving.
564 class Copy_reloc_entry
567 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
568 Relobj
* relobj
, unsigned int shndx
,
569 Address address
, Addend addend
)
570 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
571 shndx_(shndx
), address_(address
), addend_(addend
)
574 // Return whether we should emit this reloc. If we should not
575 // emit, we clear it.
582 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
585 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
589 unsigned int reloc_type_
;
596 // A list of relocs to be saved.
597 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
599 // The list of relocs we are saving.
600 Copy_reloc_entries entries_
;
603 // Track relocations while reading a section. This lets you ask for
604 // the relocation at a certain offset, and see how relocs occur
605 // between points of interest.
607 template<int size
, bool big_endian
>
612 : object_(NULL
), prelocs_(NULL
), len_(0), pos_(0), reloc_size_(0)
615 // Initialize the Track_relocs object. OBJECT is the object holding
616 // the reloc section, RELOC_SHNDX is the section index of the reloc
617 // section, and RELOC_TYPE is the type of the reloc section
618 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
619 // something went wrong.
621 initialize(Sized_relobj
<size
, big_endian
>* object
, unsigned int reloc_shndx
,
622 unsigned int reloc_type
);
624 // Return the offset in the data section to which the next reloc
625 // applies. THis returns -1 if there is no next reloc.
629 // Return the symbol index of the next reloc. This returns -1U if
630 // there is no next reloc.
634 // Advance to OFFSET within the data section, and return the number
635 // of relocs which would be skipped.
637 advance(off_t offset
);
641 Sized_relobj
<size
, big_endian
>* object_
;
642 // The contents of the reloc section.
643 const unsigned char* prelocs_
;
644 // The length of the reloc section.
646 // Our current position in the reloc section.
648 // The size of the relocs in the section.
652 } // End namespace gold.
654 #endif // !defined(GOLD_RELOC_H)