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 // Do a simple PC relative relocation with the addend in the section
224 // contents. VALSIZE is the size of the value.
225 template<int valsize
>
227 pcrel(unsigned char* view
,
228 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
229 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
231 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
232 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
233 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
234 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
237 // Do a simple PC relative relocation with a Symbol_value with the
238 // addend in the section contents. VALSIZE is the size of the
240 template<int valsize
>
242 pcrel(unsigned char* view
,
243 const Sized_relobj
<size
, big_endian
>* object
,
244 const Symbol_value
<size
>* psymval
,
245 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
247 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
248 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
249 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
250 x
= psymval
->value(object
, x
);
251 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
254 // Do a simple PC relative relocation with the addend in the
255 // relocation. VALSIZE is the size of the value.
256 template<int valsize
>
258 pcrela(unsigned char* view
,
259 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
260 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
261 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
263 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
264 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
265 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
+ addend
- address
);
268 // Do a simple PC relative relocation with a Symbol_value with the
269 // addend in the relocation. VALSIZE is the size of the value.
270 template<int valsize
>
272 pcrela(unsigned char* view
,
273 const Sized_relobj
<size
, big_endian
>* object
,
274 const Symbol_value
<size
>* psymval
,
275 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
,
276 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
278 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
279 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
280 Valtype x
= psymval
->value(object
, addend
);
281 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
284 typedef Relocate_functions
<size
, big_endian
> This
;
287 // Do a simple 8-bit REL relocation with the addend in the section
290 rel8(unsigned char* view
, unsigned char value
)
291 { This::template rel
<8>(view
, value
); }
294 rel8(unsigned char* view
,
295 const Sized_relobj
<size
, big_endian
>* object
,
296 const Symbol_value
<size
>* psymval
)
297 { This::template rel
<8>(view
, object
, psymval
); }
299 // Do an 8-bit RELA relocation with the addend in the relocation.
301 rela8(unsigned char* view
, unsigned char value
, unsigned char addend
)
302 { This::template rela
<8>(view
, value
, addend
); }
305 rela8(unsigned char* view
,
306 const Sized_relobj
<size
, big_endian
>* object
,
307 const Symbol_value
<size
>* psymval
,
308 unsigned char addend
)
309 { This::template rela
<8>(view
, object
, psymval
, addend
); }
311 // Do a simple 8-bit PC relative relocation with the addend in the
314 pcrel8(unsigned char* view
, unsigned char value
,
315 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
316 { This::template pcrel
<8>(view
, value
, address
); }
319 pcrel8(unsigned char* view
,
320 const Sized_relobj
<size
, big_endian
>* object
,
321 const Symbol_value
<size
>* psymval
,
322 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
323 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
325 // Do a simple 8-bit PC relative RELA relocation with the addend in
328 pcrela8(unsigned char* view
, unsigned char value
, unsigned char addend
,
329 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
330 { This::template pcrela
<8>(view
, value
, addend
, address
); }
333 pcrela8(unsigned char* view
,
334 const Sized_relobj
<size
, big_endian
>* object
,
335 const Symbol_value
<size
>* psymval
,
336 unsigned char addend
,
337 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
338 { This::template pcrela
<8>(view
, object
, psymval
, addend
, address
); }
340 // Do a simple 16-bit REL relocation with the addend in the section
343 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
344 { This::template rel
<16>(view
, value
); }
347 rel16(unsigned char* view
,
348 const Sized_relobj
<size
, big_endian
>* object
,
349 const Symbol_value
<size
>* psymval
)
350 { This::template rel
<16>(view
, object
, psymval
); }
352 // Do an 16-bit RELA relocation with the addend in the relocation.
354 rela16(unsigned char* view
, elfcpp::Elf_Half value
, elfcpp::Elf_Half addend
)
355 { This::template rela
<16>(view
, value
, addend
); }
358 rela16(unsigned char* view
,
359 const Sized_relobj
<size
, big_endian
>* object
,
360 const Symbol_value
<size
>* psymval
,
361 elfcpp::Elf_Half addend
)
362 { This::template rela
<16>(view
, object
, psymval
, addend
); }
364 // Do a simple 16-bit PC relative REL relocation with the addend in
365 // the section contents.
367 pcrel16(unsigned char* view
, elfcpp::Elf_Half value
,
368 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
369 { This::template pcrel
<16>(view
, value
, address
); }
372 pcrel16(unsigned char* view
,
373 const Sized_relobj
<size
, big_endian
>* object
,
374 const Symbol_value
<size
>* psymval
,
375 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
376 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
378 // Do a simple 16-bit PC relative RELA relocation with the addend in
381 pcrela16(unsigned char* view
, elfcpp::Elf_Half value
,
382 elfcpp::Elf_Half addend
,
383 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
384 { This::template pcrela
<16>(view
, value
, addend
, address
); }
387 pcrela16(unsigned char* view
,
388 const Sized_relobj
<size
, big_endian
>* object
,
389 const Symbol_value
<size
>* psymval
,
390 elfcpp::Elf_Half addend
,
391 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
392 { This::template pcrela
<16>(view
, object
, psymval
, addend
, address
); }
394 // Do a simple 32-bit REL relocation with the addend in the section
397 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
398 { This::template rel
<32>(view
, value
); }
401 rel32(unsigned char* view
,
402 const Sized_relobj
<size
, big_endian
>* object
,
403 const Symbol_value
<size
>* psymval
)
404 { This::template rel
<32>(view
, object
, psymval
); }
406 // Do an 32-bit RELA relocation with the addend in the relocation.
408 rela32(unsigned char* view
, elfcpp::Elf_Word value
, elfcpp::Elf_Word addend
)
409 { This::template rela
<32>(view
, value
, addend
); }
412 rela32(unsigned char* view
,
413 const Sized_relobj
<size
, big_endian
>* object
,
414 const Symbol_value
<size
>* psymval
,
415 elfcpp::Elf_Word addend
)
416 { This::template rela
<32>(view
, object
, psymval
, addend
); }
418 // Do a simple 32-bit PC relative REL relocation with the addend in
419 // the section contents.
421 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
422 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
423 { This::template pcrel
<32>(view
, value
, address
); }
426 pcrel32(unsigned char* view
,
427 const Sized_relobj
<size
, big_endian
>* object
,
428 const Symbol_value
<size
>* psymval
,
429 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
430 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
432 // Do a simple 32-bit PC relative RELA relocation with the addend in
435 pcrela32(unsigned char* view
, elfcpp::Elf_Word value
,
436 elfcpp::Elf_Word addend
,
437 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
438 { This::template pcrela
<32>(view
, value
, addend
, address
); }
441 pcrela32(unsigned char* view
,
442 const Sized_relobj
<size
, big_endian
>* object
,
443 const Symbol_value
<size
>* psymval
,
444 elfcpp::Elf_Word addend
,
445 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
446 { This::template pcrela
<32>(view
, object
, psymval
, addend
, address
); }
448 // Do a simple 64-bit REL relocation with the addend in the section
451 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
452 { This::template rel
<64>(view
, value
); }
455 rel64(unsigned char* view
,
456 const Sized_relobj
<size
, big_endian
>* object
,
457 const Symbol_value
<size
>* psymval
)
458 { This::template rel
<64>(view
, object
, psymval
); }
460 // Do a 64-bit RELA relocation with the addend in the relocation.
462 rela64(unsigned char* view
, elfcpp::Elf_Xword value
,
463 elfcpp::Elf_Xword addend
)
464 { This::template rela
<64>(view
, value
, addend
); }
467 rela64(unsigned char* view
,
468 const Sized_relobj
<size
, big_endian
>* object
,
469 const Symbol_value
<size
>* psymval
,
470 elfcpp::Elf_Xword addend
)
471 { This::template rela
<64>(view
, object
, psymval
, addend
); }
473 // Do a simple 64-bit PC relative REL relocation with the addend in
474 // the section contents.
476 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
477 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
478 { This::template pcrel
<64>(view
, value
, address
); }
481 pcrel64(unsigned char* view
,
482 const Sized_relobj
<size
, big_endian
>* object
,
483 const Symbol_value
<size
>* psymval
,
484 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
485 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
487 // Do a simple 64-bit PC relative RELA relocation with the addend in
490 pcrela64(unsigned char* view
, elfcpp::Elf_Xword value
,
491 elfcpp::Elf_Xword addend
,
492 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
493 { This::template pcrela
<64>(view
, value
, addend
, address
); }
496 pcrela64(unsigned char* view
,
497 const Sized_relobj
<size
, big_endian
>* object
,
498 const Symbol_value
<size
>* psymval
,
499 elfcpp::Elf_Xword addend
,
500 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
501 { This::template pcrela
<64>(view
, object
, psymval
, addend
, address
); }
504 // We try to avoid COPY relocations when possible. A COPY relocation
505 // may be required when an executable refers to a variable defined in
506 // a shared library. COPY relocations are problematic because they
507 // tie the executable to the exact size of the variable in the shared
508 // library. We can avoid them if all the references to the variable
509 // are in a writeable section. In that case we can simply use dynamic
510 // relocations. However, when scanning relocs, we don't know when we
511 // see the relocation whether we will be forced to use a COPY
512 // relocation or not. So we have to save the relocation during the
513 // reloc scanning, and then emit it as a dynamic relocation if
514 // necessary. This class implements that. It is used by the target
517 template<int size
, bool big_endian
>
525 // Return whether we need a COPY reloc for a reloc against GSYM,
526 // which is being applied to section SHNDX in OBJECT.
528 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
529 Sized_symbol
<size
>* gsym
);
531 // Save a Rel against SYM for possible emission later. SHNDX is the
532 // index of the section to which the reloc is being applied.
534 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
535 const elfcpp::Rel
<size
, big_endian
>&);
537 // Save a Rela against SYM for possible emission later.
539 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
540 const elfcpp::Rela
<size
, big_endian
>&);
542 // Return whether there are any relocs to emit. This also discards
543 // entries which need not be emitted.
547 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
548 // is still defined in the dynamic object).
549 template<int sh_type
>
551 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
554 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
555 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
557 // This POD class holds the entries we are saving.
558 class Copy_reloc_entry
561 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
562 Relobj
* relobj
, unsigned int shndx
,
563 Address address
, Addend addend
)
564 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
565 shndx_(shndx
), address_(address
), addend_(addend
)
568 // Return whether we should emit this reloc. If we should not
569 // emit, we clear it.
576 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
579 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
583 unsigned int reloc_type_
;
590 // A list of relocs to be saved.
591 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
593 // The list of relocs we are saving.
594 Copy_reloc_entries entries_
;
597 } // End namespace gold.
599 #endif // !defined(GOLD_RELOC_H)