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<size, big_endian>::Valtype Sizetype;
232 Valtype* wv = reinterpret_cast<Valtype*>(view);
233 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
234 // Fancy formula to sign-extend x to size.
235 const Sizetype mask = 1U << (sizeof(valsize) * 8 - 1);
236 Sizetype sign_extended_x = x;
237 sign_extended_x = (sign_extended_x ^ mask) - mask;
238 x = psymval->value(object, sign_extended_x);
239 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
242 // Do a simple PC relative relocation with the addend in the section
243 // contents. VALSIZE is the size of the value.
244 template<int valsize>
246 pcrel(unsigned char* view,
247 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
248 typename elfcpp::Elf_types<size>::Elf_Addr address)
250 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
251 Valtype* wv = reinterpret_cast<Valtype*>(view);
252 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
253 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
256 // Do a simple PC relative relocation with a Symbol_value with the
257 // addend in the section contents. VALSIZE is the size of the
259 template<int valsize>
261 pcrel(unsigned char* view,
262 const Sized_relobj<size, big_endian>* object,
263 const Symbol_value<size>* psymval,
264 typename elfcpp::Elf_types<size>::Elf_Addr address)
266 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
267 Valtype* wv = reinterpret_cast<Valtype*>(view);
268 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
269 x = psymval->value(object, x);
270 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
273 // Do a simple PC relative relocation with the addend in the
274 // relocation. VALSIZE is the size of the value.
275 template<int valsize>
277 pcrela(unsigned char* view,
278 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
279 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
280 typename elfcpp::Elf_types<size>::Elf_Addr address)
282 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
283 Valtype* wv = reinterpret_cast<Valtype*>(view);
284 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
287 // Do a simple PC relative relocation with a Symbol_value with the
288 // addend in the relocation. VALSIZE is the size of the value.
289 template<int valsize>
291 pcrela(unsigned char* view,
292 const Sized_relobj<size, big_endian>* object,
293 const Symbol_value<size>* psymval,
294 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
295 typename elfcpp::Elf_types<size>::Elf_Addr address)
297 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
298 Valtype* wv = reinterpret_cast<Valtype*>(view);
299 Valtype x = psymval->value(object, addend);
300 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
303 typedef Relocate_functions<size, big_endian> This;
306 // Do a simple 8-bit REL relocation with the addend in the section
309 rel8(unsigned char* view, unsigned char value)
310 { This::template rel<8>(view, value); }
313 rel8(unsigned char* view,
314 const Sized_relobj<size, big_endian>* object,
315 const Symbol_value<size>* psymval)
316 { This::template rel<8>(view, object, psymval); }
318 // Do an 8-bit RELA relocation with the addend in the relocation.
320 rel8a(unsigned char* view, unsigned char value, unsigned char addend)
321 { This::template rela<8>(view, value, addend); }
324 rel8a(unsigned char* view,
325 const Sized_relobj<size, big_endian>* object,
326 const Symbol_value<size>* psymval,
327 unsigned char addend)
328 { This::template rela<8>(view, object, psymval, addend); }
330 // Do an 8-bit REL relocation, sign extending the addend to SIZE.
332 rel8s(unsigned char* view,
333 const Sized_relobj<size, big_endian>* object,
334 const Symbol_value<size>* psymval)
335 { This::template signedrel<8>(view, object, psymval); }
337 // Do a simple 8-bit PC relative relocation with the addend in the
340 pcrel8(unsigned char* view, unsigned char value,
341 typename elfcpp::Elf_types<size>::Elf_Addr address)
342 { This::template pcrel<8>(view, value, address); }
345 pcrel8(unsigned char* view,
346 const Sized_relobj<size, big_endian>* object,
347 const Symbol_value<size>* psymval,
348 typename elfcpp::Elf_types<size>::Elf_Addr address)
349 { This::template pcrel<8>(view, object, psymval, address); }
351 // Do a simple 8-bit PC relative RELA relocation with the addend in
354 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
355 typename elfcpp::Elf_types<size>::Elf_Addr address)
356 { This::template pcrela<8>(view, value, addend, address); }
359 pcrela8(unsigned char* view,
360 const Sized_relobj<size, big_endian>* object,
361 const Symbol_value<size>* psymval,
362 unsigned char addend,
363 typename elfcpp::Elf_types<size>::Elf_Addr address)
364 { This::template pcrela<8>(view, object, psymval, addend, address); }
366 // Do a simple 16-bit REL relocation with the addend in the section
369 rel16(unsigned char* view, elfcpp::Elf_Half value)
370 { This::template rel<16>(view, value); }
373 rel16(unsigned char* view,
374 const Sized_relobj<size, big_endian>* object,
375 const Symbol_value<size>* psymval)
376 { This::template rel<16>(view, object, psymval); }
378 // Do an 16-bit RELA relocation with the addend in the relocation.
380 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
381 { This::template rela<16>(view, value, addend); }
384 rela16(unsigned char* view,
385 const Sized_relobj<size, big_endian>* object,
386 const Symbol_value<size>* psymval,
387 elfcpp::Elf_Half addend)
388 { This::template rela<16>(view, object, psymval, addend); }
390 // Do a 16-bit REL relocation, sign extending the addend to SIZE.
392 rel16s(unsigned char* view,
393 const Sized_relobj<size, big_endian>* object,
394 const Symbol_value<size>* psymval)
395 { This::template signedrel<16>(view, object, psymval); }
397 // Do a simple 16-bit PC relative REL relocation with the addend in
398 // the section contents.
400 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
401 typename elfcpp::Elf_types<size>::Elf_Addr address)
402 { This::template pcrel<16>(view, value, address); }
405 pcrel16(unsigned char* view,
406 const Sized_relobj<size, big_endian>* object,
407 const Symbol_value<size>* psymval,
408 typename elfcpp::Elf_types<size>::Elf_Addr address)
409 { This::template pcrel<16>(view, object, psymval, address); }
411 // Do a simple 16-bit PC relative RELA relocation with the addend in
414 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
415 elfcpp::Elf_Half addend,
416 typename elfcpp::Elf_types<size>::Elf_Addr address)
417 { This::template pcrela<16>(view, value, addend, address); }
420 pcrela16(unsigned char* view,
421 const Sized_relobj<size, big_endian>* object,
422 const Symbol_value<size>* psymval,
423 elfcpp::Elf_Half addend,
424 typename elfcpp::Elf_types<size>::Elf_Addr address)
425 { This::template pcrela<16>(view, object, psymval, addend, address); }
427 // Do a simple 32-bit REL relocation with the addend in the section
430 rel32(unsigned char* view, elfcpp::Elf_Word value)
431 { This::template rel<32>(view, value); }
434 rel32(unsigned char* view,
435 const Sized_relobj<size, big_endian>* object,
436 const Symbol_value<size>* psymval)
437 { This::template rel<32>(view, object, psymval); }
439 // Do an 32-bit RELA relocation with the addend in the relocation.
441 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
442 { This::template rela<32>(view, value, addend); }
445 rela32(unsigned char* view,
446 const Sized_relobj<size, big_endian>* object,
447 const Symbol_value<size>* psymval,
448 elfcpp::Elf_Word addend)
449 { This::template rela<32>(view, object, psymval, addend); }
451 // Do a 32-bit REL relocation, sign extending the addend to SIZE.
453 rel32s(unsigned char* view,
454 const Sized_relobj<size, big_endian>* object,
455 const Symbol_value<size>* psymval)
456 { This::template signedrel<32>(view, object, psymval); }
458 // Do a simple 32-bit PC relative REL relocation with the addend in
459 // the section contents.
461 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
462 typename elfcpp::Elf_types<size>::Elf_Addr address)
463 { This::template pcrel<32>(view, value, address); }
466 pcrel32(unsigned char* view,
467 const Sized_relobj<size, big_endian>* object,
468 const Symbol_value<size>* psymval,
469 typename elfcpp::Elf_types<size>::Elf_Addr address)
470 { This::template pcrel<32>(view, object, psymval, address); }
472 // Do a simple 32-bit PC relative RELA relocation with the addend in
475 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
476 elfcpp::Elf_Word addend,
477 typename elfcpp::Elf_types<size>::Elf_Addr address)
478 { This::template pcrela<32>(view, value, addend, address); }
481 pcrela32(unsigned char* view,
482 const Sized_relobj<size, big_endian>* object,
483 const Symbol_value<size>* psymval,
484 elfcpp::Elf_Word addend,
485 typename elfcpp::Elf_types<size>::Elf_Addr address)
486 { This::template pcrela<32>(view, object, psymval, addend, address); }
488 // Do a simple 64-bit REL relocation with the addend in the section
491 rel64(unsigned char* view, elfcpp::Elf_Xword value)
492 { This::template rel<64>(view, value); }
495 rel64(unsigned char* view,
496 const Sized_relobj<size, big_endian>* object,
497 const Symbol_value<size>* psymval)
498 { This::template rel<64>(view, object, psymval); }
500 // Do a 64-bit RELA relocation with the addend in the relocation.
502 rela64(unsigned char* view, elfcpp::Elf_Xword value,
503 elfcpp::Elf_Xword addend)
504 { This::template rela<64>(view, value, addend); }
507 rela64(unsigned char* view,
508 const Sized_relobj<size, big_endian>* object,
509 const Symbol_value<size>* psymval,
510 elfcpp::Elf_Xword addend)
511 { This::template rela<64>(view, object, psymval, addend); }
513 // Do a simple 64-bit PC relative REL relocation with the addend in
514 // the section contents.
516 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
517 typename elfcpp::Elf_types<size>::Elf_Addr address)
518 { This::template pcrel<64>(view, value, address); }
521 pcrel64(unsigned char* view,
522 const Sized_relobj<size, big_endian>* object,
523 const Symbol_value<size>* psymval,
524 typename elfcpp::Elf_types<size>::Elf_Addr address)
525 { This::template pcrel<64>(view, object, psymval, address); }
527 // Do a simple 64-bit PC relative RELA relocation with the addend in
530 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
531 elfcpp::Elf_Xword addend,
532 typename elfcpp::Elf_types<size>::Elf_Addr address)
533 { This::template pcrela<64>(view, value, addend, address); }
536 pcrela64(unsigned char* view,
537 const Sized_relobj<size, big_endian>* object,
538 const Symbol_value<size>* psymval,
539 elfcpp::Elf_Xword addend,
540 typename elfcpp::Elf_types<size>::Elf_Addr address)
541 { This::template pcrela<64>(view, object, psymval, addend, address); }
544 // We try to avoid COPY relocations when possible. A COPY relocation
545 // may be required when an executable refers to a variable defined in
546 // a shared library. COPY relocations are problematic because they
547 // tie the executable to the exact size of the variable in the shared
548 // library. We can avoid them if all the references to the variable
549 // are in a writeable section. In that case we can simply use dynamic
550 // relocations. However, when scanning relocs, we don't know when we
551 // see the relocation whether we will be forced to use a COPY
552 // relocation or not. So we have to save the relocation during the
553 // reloc scanning, and then emit it as a dynamic relocation if
554 // necessary. This class implements that. It is used by the target
557 template<int size, bool big_endian>
565 // Return whether we need a COPY reloc for a reloc against GSYM,
566 // which is being applied to section SHNDX in OBJECT.
568 need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
569 Sized_symbol<size>* gsym);
571 // Save a Rel against SYM for possible emission later. SHNDX is the
572 // index of the section to which the reloc is being applied.
574 save(Symbol* sym, Relobj*, unsigned int shndx,
575 const elfcpp::Rel<size, big_endian>&);
577 // Save a Rela against SYM for possible emission later.
579 save(Symbol* sym, Relobj*, unsigned int shndx,
580 const elfcpp::Rela<size, big_endian>&);
582 // Return whether there are any relocs to emit. This also discards
583 // entries which need not be emitted.
587 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
588 // is still defined in the dynamic object).
589 template<int sh_type>
591 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
594 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
595 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
597 // This POD class holds the entries we are saving.
598 class Copy_reloc_entry
601 Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
602 Relobj* relobj, unsigned int shndx,
603 Address address, Addend addend)
604 : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
605 shndx_(shndx), address_(address), addend_(addend)
608 // Return whether we should emit this reloc. If we should not
609 // emit, we clear it.
616 emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
619 emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
623 unsigned int reloc_type_;
630 // A list of relocs to be saved.
631 typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
633 // The list of relocs we are saving.
634 Copy_reloc_entries entries_;
637 } // End namespace gold.
639 #endif // !defined(GOLD_RELOC_H)