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
* final_blocker
)
133 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
134 of_(of
), final_blocker_(final_blocker
)
137 // The standard Task methods.
140 is_runnable(Workqueue
*);
149 class Relocate_locker
;
151 const General_options
& options_
;
152 const Symbol_table
* symtab_
;
153 const Layout
* layout_
;
156 Task_token
* final_blocker_
;
159 // Standard relocation routines which are used on many targets. Here
160 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
162 template<int size
, bool big_endian
>
163 class Relocate_functions
166 // Do a simple relocation with the addend in the section contents.
167 // VALSIZE is the size of the value.
168 template<int valsize
>
170 rel(unsigned char* view
,
171 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
173 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
174 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
175 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
176 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
179 // Do a simple relocation using a Symbol_value with the addend in
180 // the section contents. VALSIZE is the size of the value to
182 template<int valsize
>
184 rel(unsigned char* view
,
185 const Sized_relobj
<size
, big_endian
>* object
,
186 const Symbol_value
<size
>* psymval
)
188 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
189 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
190 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
191 x
= psymval
->value(object
, x
);
192 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
195 // Do a simple relocation with the addend in the relocation.
196 // VALSIZE is the size of the value.
197 template<int valsize
>
199 rela(unsigned char* view
,
200 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
201 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
203 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
204 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
205 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
);
208 // Do a simple relocation using a symbol value with the addend in
209 // the relocation. VALSIZE is the size of the value.
210 template<int valsize
>
212 rela(unsigned char* view
,
213 const Sized_relobj
<size
, big_endian
>* object
,
214 const Symbol_value
<size
>* psymval
,
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 Valtype x
= psymval
->value(object
, addend
);
220 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
223 // Like rel(), but sign-extends the value to SIZE.
224 template<int valsize
>
226 signedrel(unsigned char* view
,
227 const Sized_relobj
<size
, big_endian
>* object
,
228 const Symbol_value
<size
>* psymval
)
230 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
231 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Signed_valtype
233 typedef typename
elfcpp::Swap
<size
, big_endian
>::Valtype Sizetype
;
234 typedef typename
elfcpp::Swap
<size
, big_endian
>::Signed_valtype
236 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
237 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
239 // Sign extend the value.
240 Signed_valtype signed_x
= static_cast<Signed_valtype
>(x
);
241 Signed_sizetype signed_extended_x
= static_cast<Signed_sizetype
>(signed_x
);
242 Sizetype unsigned_extended_x
= static_cast<Sizetype
>(signed_extended_x
);
244 x
= psymval
->value(object
, unsigned_extended_x
);
245 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
248 // Do a simple PC relative relocation with the addend in the section
249 // contents. VALSIZE is the size of the value.
250 template<int valsize
>
252 pcrel(unsigned char* view
,
253 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
254 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
256 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
257 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
258 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
259 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
262 // Do a simple PC relative relocation with a Symbol_value with the
263 // addend in the section contents. VALSIZE is the size of the
265 template<int valsize
>
267 pcrel(unsigned char* view
,
268 const Sized_relobj
<size
, big_endian
>* object
,
269 const Symbol_value
<size
>* psymval
,
270 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
272 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
273 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
274 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
275 x
= psymval
->value(object
, x
);
276 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
279 // Do a simple PC relative relocation with the addend in the
280 // relocation. VALSIZE is the size of the value.
281 template<int valsize
>
283 pcrela(unsigned char* view
,
284 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
285 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
286 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
288 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
289 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
290 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
293 // Do a simple PC relative relocation with a Symbol_value with the
294 // addend in the relocation. VALSIZE is the size of the value.
295 template<int valsize
>
297 pcrela(unsigned char* view
,
298 const Sized_relobj
<size
, big_endian
>* object
,
299 const Symbol_value
<size
>* psymval
,
300 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
301 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
303 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
304 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
305 Valtype x
= psymval
->value(object
, addend
);
306 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
309 typedef Relocate_functions
<size
, big_endian
> This
;
312 // Do a simple 8-bit REL relocation with the addend in the section
315 rel8(unsigned char* view
, unsigned char value
)
316 { This::template rel
<8>(view
, value
); }
319 rel8(unsigned char* view
,
320 const Sized_relobj
<size
, big_endian
>* object
,
321 const Symbol_value
<size
>* psymval
)
322 { This::template rel
<8>(view
, object
, psymval
); }
324 // Do an 8-bit RELA relocation with the addend in the relocation.
326 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
327 { This::template rela
<8>(view
, value
, addend
); }
330 rela8(unsigned char* view
,
331 const Sized_relobj
<size
, big_endian
>* object
,
332 const Symbol_value
<size
>* psymval
,
333 unsigned char addend
)
334 { This::template rela
<8>(view
, object
, psymval
, addend
); }
336 // Do an 8-bit REL relocation, sign extending the addend to SIZE.
338 rel8s(unsigned char* view
,
339 const Sized_relobj
<size
, big_endian
>* object
,
340 const Symbol_value
<size
>* psymval
)
341 { This::template signedrel
<8>(view
, object
, psymval
); }
343 // Do a simple 8-bit PC relative relocation with the addend in the
346 pcrel8(unsigned char* view
, unsigned char value
,
347 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
348 { This::template pcrel
<8>(view
, value
, address
); }
351 pcrel8(unsigned char* view
,
352 const Sized_relobj
<size
, big_endian
>* object
,
353 const Symbol_value
<size
>* psymval
,
354 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
355 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
357 // Do a simple 8-bit PC relative RELA relocation with the addend in
360 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
361 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
362 { This::template pcrela
<8>(view
, value
, addend
, address
); }
365 pcrela8(unsigned char* view
,
366 const Sized_relobj
<size
, big_endian
>* object
,
367 const Symbol_value
<size
>* psymval
,
368 unsigned char addend
,
369 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
370 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
372 // Do a simple 16-bit REL relocation with the addend in the section
375 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
376 { This::template rel
<16>(view
, value
); }
379 rel16(unsigned char* view
,
380 const Sized_relobj
<size
, big_endian
>* object
,
381 const Symbol_value
<size
>* psymval
)
382 { This::template rel
<16>(view
, object
, psymval
); }
384 // Do an 16-bit RELA relocation with the addend in the relocation.
386 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
387 { This::template rela
<16>(view
, value
, addend
); }
390 rela16(unsigned char* view
,
391 const Sized_relobj
<size
, big_endian
>* object
,
392 const Symbol_value
<size
>* psymval
,
393 elfcpp::Elf_Half addend
)
394 { This::template rela
<16>(view
, object
, psymval
, addend
); }
396 // Do a 16-bit REL relocation, sign extending the addend to SIZE.
398 rel16s(unsigned char* view
,
399 const Sized_relobj
<size
, big_endian
>* object
,
400 const Symbol_value
<size
>* psymval
)
401 { This::template signedrel
<16>(view
, object
, psymval
); }
403 // Do a simple 16-bit PC relative REL relocation with the addend in
404 // the section contents.
406 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
407 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
408 { This::template pcrel
<16>(view
, value
, address
); }
411 pcrel16(unsigned char* view
,
412 const Sized_relobj
<size
, big_endian
>* object
,
413 const Symbol_value
<size
>* psymval
,
414 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
415 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
417 // Do a simple 16-bit PC relative RELA relocation with the addend in
420 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
421 elfcpp::Elf_Half addend
,
422 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
423 { This::template pcrela
<16>(view
, value
, addend
, address
); }
426 pcrela16(unsigned char* view
,
427 const Sized_relobj
<size
, big_endian
>* object
,
428 const Symbol_value
<size
>* psymval
,
429 elfcpp::Elf_Half addend
,
430 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
431 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
433 // Do a simple 32-bit REL relocation with the addend in the section
436 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
437 { This::template rel
<32>(view
, value
); }
440 rel32(unsigned char* view
,
441 const Sized_relobj
<size
, big_endian
>* object
,
442 const Symbol_value
<size
>* psymval
)
443 { This::template rel
<32>(view
, object
, psymval
); }
445 // Do an 32-bit RELA relocation with the addend in the relocation.
447 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
448 { This::template rela
<32>(view
, value
, addend
); }
451 rela32(unsigned char* view
,
452 const Sized_relobj
<size
, big_endian
>* object
,
453 const Symbol_value
<size
>* psymval
,
454 elfcpp::Elf_Word addend
)
455 { This::template rela
<32>(view
, object
, psymval
, addend
); }
457 // Do a 32-bit REL relocation, sign extending the addend to SIZE.
459 rel32s(unsigned char* view
,
460 const Sized_relobj
<size
, big_endian
>* object
,
461 const Symbol_value
<size
>* psymval
)
462 { This::template signedrel
<32>(view
, object
, psymval
); }
464 // Do a simple 32-bit PC relative REL relocation with the addend in
465 // the section contents.
467 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
468 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
469 { This::template pcrel
<32>(view
, value
, address
); }
472 pcrel32(unsigned char* view
,
473 const Sized_relobj
<size
, big_endian
>* object
,
474 const Symbol_value
<size
>* psymval
,
475 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
476 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
478 // Do a simple 32-bit PC relative RELA relocation with the addend in
481 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
482 elfcpp::Elf_Word addend
,
483 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
484 { This::template pcrela
<32>(view
, value
, addend
, address
); }
487 pcrela32(unsigned char* view
,
488 const Sized_relobj
<size
, big_endian
>* object
,
489 const Symbol_value
<size
>* psymval
,
490 elfcpp::Elf_Word addend
,
491 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
492 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
494 // Do a simple 64-bit REL relocation with the addend in the section
497 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
498 { This::template rel
<64>(view
, value
); }
501 rel64(unsigned char* view
,
502 const Sized_relobj
<size
, big_endian
>* object
,
503 const Symbol_value
<size
>* psymval
)
504 { This::template rel
<64>(view
, object
, psymval
); }
506 // Do a 64-bit RELA relocation with the addend in the relocation.
508 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
509 elfcpp::Elf_Xword addend
)
510 { This::template rela
<64>(view
, value
, addend
); }
513 rela64(unsigned char* view
,
514 const Sized_relobj
<size
, big_endian
>* object
,
515 const Symbol_value
<size
>* psymval
,
516 elfcpp::Elf_Xword addend
)
517 { This::template rela
<64>(view
, object
, psymval
, addend
); }
519 // Do a simple 64-bit PC relative REL relocation with the addend in
520 // the section contents.
522 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
523 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
524 { This::template pcrel
<64>(view
, value
, address
); }
527 pcrel64(unsigned char* view
,
528 const Sized_relobj
<size
, big_endian
>* object
,
529 const Symbol_value
<size
>* psymval
,
530 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
531 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
533 // Do a simple 64-bit PC relative RELA relocation with the addend in
536 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
537 elfcpp::Elf_Xword addend
,
538 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
539 { This::template pcrela
<64>(view
, value
, addend
, address
); }
542 pcrela64(unsigned char* view
,
543 const Sized_relobj
<size
, big_endian
>* object
,
544 const Symbol_value
<size
>* psymval
,
545 elfcpp::Elf_Xword addend
,
546 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
547 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
550 // We try to avoid COPY relocations when possible. A COPY relocation
551 // may be required when an executable refers to a variable defined in
552 // a shared library. COPY relocations are problematic because they
553 // tie the executable to the exact size of the variable in the shared
554 // library. We can avoid them if all the references to the variable
555 // are in a writeable section. In that case we can simply use dynamic
556 // relocations. However, when scanning relocs, we don't know when we
557 // see the relocation whether we will be forced to use a COPY
558 // relocation or not. So we have to save the relocation during the
559 // reloc scanning, and then emit it as a dynamic relocation if
560 // necessary. This class implements that. It is used by the target
563 template<int size
, bool big_endian
>
571 // Return whether we need a COPY reloc for a reloc against GSYM,
572 // which is being applied to section SHNDX in OBJECT.
574 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
575 Sized_symbol
<size
>* gsym
);
577 // Save a Rel against SYM for possible emission later. SHNDX is the
578 // index of the section to which the reloc is being applied.
580 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
581 const elfcpp::Rel
<size
, big_endian
>&);
583 // Save a Rela against SYM for possible emission later.
585 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
586 const elfcpp::Rela
<size
, big_endian
>&);
588 // Return whether there are any relocs to emit. This also discards
589 // entries which need not be emitted.
593 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
594 // is still defined in the dynamic object).
595 template<int sh_type
>
597 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
600 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
601 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
603 // This POD class holds the entries we are saving.
604 class Copy_reloc_entry
607 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
608 Relobj
* relobj
, unsigned int shndx
,
609 Address address
, Addend addend
)
610 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
611 shndx_(shndx
), address_(address
), addend_(addend
)
614 // Return whether we should emit this reloc. If we should not
615 // emit, we clear it.
622 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
625 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
629 unsigned int reloc_type_
;
636 // A list of relocs to be saved.
637 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
639 // The list of relocs we are saving.
640 Copy_reloc_entries entries_
;
643 } // End namespace gold.
645 #endif // !defined(GOLD_RELOC_H)