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
;
44 template<int size
, bool big_endian
>
50 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
51 class Output_data_reloc
;
53 // A class to read the relocations for an object file, and then queue
54 // up a task to see if they require any GOT/PLT/COPY relocations in
57 class Read_relocs
: public Task
60 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
61 // unblocked when the Scan_relocs task completes.
62 Read_relocs(const General_options
& options
, Symbol_table
* symtab
,
63 Layout
* layout
, Relobj
* object
, Task_token
* symtab_lock
,
65 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
66 symtab_lock_(symtab_lock
), blocker_(blocker
)
69 // The standard Task methods.
72 is_runnable(Workqueue
*);
81 const General_options
& options_
;
82 Symbol_table
* symtab_
;
85 Task_token
* symtab_lock_
;
89 // Scan the relocations for an object to see if they require any
90 // GOT/PLT/COPY relocations.
92 class Scan_relocs
: public Task
95 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
96 // unblocked when the task completes.
97 Scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
98 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
99 Task_token
* symtab_lock
, Task_token
* blocker
)
100 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
101 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
104 // The standard Task methods.
107 is_runnable(Workqueue
*);
116 class Scan_relocs_locker
;
118 const General_options
& options_
;
119 Symbol_table
* symtab_
;
122 Read_relocs_data
* rd_
;
123 Task_token
* symtab_lock_
;
124 Task_token
* blocker_
;
127 // A class to perform all the relocations for an object file.
129 class Relocate_task
: public Task
132 Relocate_task(const General_options
& options
, const Symbol_table
* symtab
,
133 const Layout
* layout
, Relobj
* object
, Output_file
* of
,
134 Task_token
* input_sections_blocker
,
135 Task_token
* output_sections_blocker
, Task_token
* final_blocker
)
136 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
137 of_(of
), input_sections_blocker_(input_sections_blocker
),
138 output_sections_blocker_(output_sections_blocker
),
139 final_blocker_(final_blocker
)
142 // The standard Task methods.
145 is_runnable(Workqueue
*);
154 class Relocate_locker
;
156 const General_options
& options_
;
157 const Symbol_table
* symtab_
;
158 const Layout
* layout_
;
161 Task_token
* input_sections_blocker_
;
162 Task_token
* output_sections_blocker_
;
163 Task_token
* final_blocker_
;
166 // Standard relocation routines which are used on many targets. Here
167 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
169 template<int size
, bool big_endian
>
170 class Relocate_functions
173 // Do a simple relocation with the addend in the section contents.
174 // VALSIZE is the size of the value.
175 template<int valsize
>
177 rel(unsigned char* view
,
178 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
180 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
181 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
182 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
183 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
186 // Do a simple relocation using a Symbol_value with the addend in
187 // the section contents. VALSIZE is the size of the value to
189 template<int valsize
>
191 rel(unsigned char* view
,
192 const Sized_relobj
<size
, big_endian
>* object
,
193 const Symbol_value
<size
>* psymval
)
195 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
196 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
197 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
198 x
= psymval
->value(object
, x
);
199 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
202 // Do a simple relocation with the addend in the relocation.
203 // VALSIZE is the size of the value.
204 template<int valsize
>
206 rela(unsigned char* view
,
207 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
208 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
210 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
211 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
212 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
);
215 // Do a simple relocation using a symbol value with the addend in
216 // the relocation. VALSIZE is the size of the value.
217 template<int valsize
>
219 rela(unsigned char* view
,
220 const Sized_relobj
<size
, big_endian
>* object
,
221 const Symbol_value
<size
>* psymval
,
222 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
224 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
225 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
226 Valtype x
= psymval
->value(object
, addend
);
227 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
230 // Do a simple PC relative relocation with the addend in the section
231 // contents. VALSIZE is the size of the value.
232 template<int valsize
>
234 pcrel(unsigned char* view
,
235 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
236 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
238 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
239 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
240 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
241 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
244 // Do a simple PC relative relocation with a Symbol_value with the
245 // addend in the section contents. VALSIZE is the size of the
247 template<int valsize
>
249 pcrel(unsigned char* view
,
250 const Sized_relobj
<size
, big_endian
>* object
,
251 const Symbol_value
<size
>* psymval
,
252 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
254 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
255 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
256 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
257 x
= psymval
->value(object
, x
);
258 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
261 // Do a simple PC relative relocation with the addend in the
262 // relocation. VALSIZE is the size of the value.
263 template<int valsize
>
265 pcrela(unsigned char* view
,
266 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
267 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
268 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
270 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
271 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
272 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
275 // Do a simple PC relative relocation with a Symbol_value with the
276 // addend in the relocation. VALSIZE is the size of the value.
277 template<int valsize
>
279 pcrela(unsigned char* view
,
280 const Sized_relobj
<size
, big_endian
>* object
,
281 const Symbol_value
<size
>* psymval
,
282 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
283 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
285 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
286 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
287 Valtype x
= psymval
->value(object
, addend
);
288 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
291 typedef Relocate_functions
<size
, big_endian
> This
;
294 // Do a simple 8-bit REL relocation with the addend in the section
297 rel8(unsigned char* view
, unsigned char value
)
298 { This::template rel
<8>(view
, value
); }
301 rel8(unsigned char* view
,
302 const Sized_relobj
<size
, big_endian
>* object
,
303 const Symbol_value
<size
>* psymval
)
304 { This::template rel
<8>(view
, object
, psymval
); }
306 // Do an 8-bit RELA relocation with the addend in the relocation.
308 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
309 { This::template rela
<8>(view
, value
, addend
); }
312 rela8(unsigned char* view
,
313 const Sized_relobj
<size
, big_endian
>* object
,
314 const Symbol_value
<size
>* psymval
,
315 unsigned char addend
)
316 { This::template rela
<8>(view
, object
, psymval
, addend
); }
318 // Do a simple 8-bit PC relative relocation with the addend in the
321 pcrel8(unsigned char* view
, unsigned char value
,
322 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
323 { This::template pcrel
<8>(view
, value
, address
); }
326 pcrel8(unsigned char* view
,
327 const Sized_relobj
<size
, big_endian
>* object
,
328 const Symbol_value
<size
>* psymval
,
329 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
330 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
332 // Do a simple 8-bit PC relative RELA relocation with the addend in
335 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
336 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
337 { This::template pcrela
<8>(view
, value
, addend
, address
); }
340 pcrela8(unsigned char* view
,
341 const Sized_relobj
<size
, big_endian
>* object
,
342 const Symbol_value
<size
>* psymval
,
343 unsigned char addend
,
344 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
345 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
347 // Do a simple 16-bit REL relocation with the addend in the section
350 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
351 { This::template rel
<16>(view
, value
); }
354 rel16(unsigned char* view
,
355 const Sized_relobj
<size
, big_endian
>* object
,
356 const Symbol_value
<size
>* psymval
)
357 { This::template rel
<16>(view
, object
, psymval
); }
359 // Do an 16-bit RELA relocation with the addend in the relocation.
361 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
362 { This::template rela
<16>(view
, value
, addend
); }
365 rela16(unsigned char* view
,
366 const Sized_relobj
<size
, big_endian
>* object
,
367 const Symbol_value
<size
>* psymval
,
368 elfcpp::Elf_Half addend
)
369 { This::template rela
<16>(view
, object
, psymval
, addend
); }
371 // Do a simple 16-bit PC relative REL relocation with the addend in
372 // the section contents.
374 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
375 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
376 { This::template pcrel
<16>(view
, value
, address
); }
379 pcrel16(unsigned char* view
,
380 const Sized_relobj
<size
, big_endian
>* object
,
381 const Symbol_value
<size
>* psymval
,
382 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
383 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
385 // Do a simple 16-bit PC relative RELA relocation with the addend in
388 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
389 elfcpp::Elf_Half addend
,
390 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
391 { This::template pcrela
<16>(view
, value
, addend
, address
); }
394 pcrela16(unsigned char* view
,
395 const Sized_relobj
<size
, big_endian
>* object
,
396 const Symbol_value
<size
>* psymval
,
397 elfcpp::Elf_Half addend
,
398 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
399 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
401 // Do a simple 32-bit REL relocation with the addend in the section
404 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
405 { This::template rel
<32>(view
, value
); }
408 rel32(unsigned char* view
,
409 const Sized_relobj
<size
, big_endian
>* object
,
410 const Symbol_value
<size
>* psymval
)
411 { This::template rel
<32>(view
, object
, psymval
); }
413 // Do an 32-bit RELA relocation with the addend in the relocation.
415 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
416 { This::template rela
<32>(view
, value
, addend
); }
419 rela32(unsigned char* view
,
420 const Sized_relobj
<size
, big_endian
>* object
,
421 const Symbol_value
<size
>* psymval
,
422 elfcpp::Elf_Word addend
)
423 { This::template rela
<32>(view
, object
, psymval
, addend
); }
425 // Do a simple 32-bit PC relative REL relocation with the addend in
426 // the section contents.
428 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
429 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
430 { This::template pcrel
<32>(view
, value
, address
); }
433 pcrel32(unsigned char* view
,
434 const Sized_relobj
<size
, big_endian
>* object
,
435 const Symbol_value
<size
>* psymval
,
436 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
437 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
439 // Do a simple 32-bit PC relative RELA relocation with the addend in
442 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
443 elfcpp::Elf_Word addend
,
444 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
445 { This::template pcrela
<32>(view
, value
, addend
, address
); }
448 pcrela32(unsigned char* view
,
449 const Sized_relobj
<size
, big_endian
>* object
,
450 const Symbol_value
<size
>* psymval
,
451 elfcpp::Elf_Word addend
,
452 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
453 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
455 // Do a simple 64-bit REL relocation with the addend in the section
458 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
459 { This::template rel
<64>(view
, value
); }
462 rel64(unsigned char* view
,
463 const Sized_relobj
<size
, big_endian
>* object
,
464 const Symbol_value
<size
>* psymval
)
465 { This::template rel
<64>(view
, object
, psymval
); }
467 // Do a 64-bit RELA relocation with the addend in the relocation.
469 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
470 elfcpp::Elf_Xword addend
)
471 { This::template rela
<64>(view
, value
, addend
); }
474 rela64(unsigned char* view
,
475 const Sized_relobj
<size
, big_endian
>* object
,
476 const Symbol_value
<size
>* psymval
,
477 elfcpp::Elf_Xword addend
)
478 { This::template rela
<64>(view
, object
, psymval
, addend
); }
480 // Do a simple 64-bit PC relative REL relocation with the addend in
481 // the section contents.
483 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
484 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
485 { This::template pcrel
<64>(view
, value
, address
); }
488 pcrel64(unsigned char* view
,
489 const Sized_relobj
<size
, big_endian
>* object
,
490 const Symbol_value
<size
>* psymval
,
491 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
492 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
494 // Do a simple 64-bit PC relative RELA relocation with the addend in
497 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
498 elfcpp::Elf_Xword addend
,
499 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
500 { This::template pcrela
<64>(view
, value
, addend
, address
); }
503 pcrela64(unsigned char* view
,
504 const Sized_relobj
<size
, big_endian
>* object
,
505 const Symbol_value
<size
>* psymval
,
506 elfcpp::Elf_Xword addend
,
507 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
508 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
511 // We try to avoid COPY relocations when possible. A COPY relocation
512 // may be required when an executable refers to a variable defined in
513 // a shared library. COPY relocations are problematic because they
514 // tie the executable to the exact size of the variable in the shared
515 // library. We can avoid them if all the references to the variable
516 // are in a writeable section. In that case we can simply use dynamic
517 // relocations. However, when scanning relocs, we don't know when we
518 // see the relocation whether we will be forced to use a COPY
519 // relocation or not. So we have to save the relocation during the
520 // reloc scanning, and then emit it as a dynamic relocation if
521 // necessary. This class implements that. It is used by the target
524 template<int size
, bool big_endian
>
532 // Return whether we need a COPY reloc for a reloc against GSYM,
533 // which is being applied to section SHNDX in OBJECT.
535 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
536 Sized_symbol
<size
>* gsym
);
538 // Save a Rel against SYM for possible emission later. SHNDX is the
539 // index of the section to which the reloc is being applied.
541 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
542 const elfcpp::Rel
<size
, big_endian
>&);
544 // Save a Rela against SYM for possible emission later.
546 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
547 const elfcpp::Rela
<size
, big_endian
>&);
549 // Return whether there are any relocs to emit. This also discards
550 // entries which need not be emitted.
554 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
555 // is still defined in the dynamic object).
556 template<int sh_type
>
558 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
561 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
562 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
564 // This POD class holds the entries we are saving.
565 class Copy_reloc_entry
568 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
569 Relobj
* relobj
, unsigned int shndx
,
570 Address address
, Addend addend
)
571 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
572 shndx_(shndx
), address_(address
), addend_(addend
)
575 // Return whether we should emit this reloc. If we should not
576 // emit, we clear it.
583 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
586 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
590 unsigned int reloc_type_
;
597 // A list of relocs to be saved.
598 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
600 // The list of relocs we are saving.
601 Copy_reloc_entries entries_
;
604 // Track relocations while reading a section. This lets you ask for
605 // the relocation at a certain offset, and see how relocs occur
606 // between points of interest.
608 template<int size
, bool big_endian
>
613 : prelocs_(NULL
), len_(0), pos_(0), reloc_size_(0)
616 // Initialize the Track_relocs object. OBJECT is the object holding
617 // the reloc section, RELOC_SHNDX is the section index of the reloc
618 // section, and RELOC_TYPE is the type of the reloc section
619 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
620 // something went wrong.
622 initialize(Object
* object
, unsigned int reloc_shndx
,
623 unsigned int reloc_type
);
625 // Return the offset in the data section to which the next reloc
626 // applies. THis returns -1 if there is no next reloc.
630 // Return the symbol index of the next reloc. This returns -1U if
631 // there is no next reloc.
635 // Advance to OFFSET within the data section, and return the number
636 // of relocs which would be skipped.
638 advance(off_t offset
);
641 // The contents of the input object's 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)