Initial -r support.
[deliverable/binutils-gdb.git] / gold / reloc.h
1 // reloc.h -- relocate input files for gold -*- C++ -*-
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #ifndef GOLD_RELOC_H
24 #define GOLD_RELOC_H
25
26 #include <vector>
27 #include <byteswap.h>
28
29 #include "elfcpp.h"
30 #include "workqueue.h"
31
32 namespace gold
33 {
34
35 class General_options;
36 class Object;
37 class Relobj;
38 class Read_relocs_data;
39 class Symbol;
40 class Layout;
41 class Output_data;
42 class Output_section;
43
44 template<int size>
45 class Sized_symbol;
46
47 template<int size, bool big_endian>
48 class Sized_relobj;
49
50 template<int size>
51 class Symbol_value;
52
53 template<int sh_type, bool dynamic, int size, bool big_endian>
54 class Output_data_reloc;
55
56 // A class to read the relocations for an object file, and then queue
57 // up a task to see if they require any GOT/PLT/COPY relocations in
58 // the symbol table.
59
60 class Read_relocs : public Task
61 {
62 public:
63 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
64 // unblocked when the Scan_relocs task completes.
65 Read_relocs(const General_options& options, Symbol_table* symtab,
66 Layout* layout, Relobj* object, Task_token* symtab_lock,
67 Task_token* blocker)
68 : options_(options), symtab_(symtab), layout_(layout), object_(object),
69 symtab_lock_(symtab_lock), blocker_(blocker)
70 { }
71
72 // The standard Task methods.
73
74 Task_token*
75 is_runnable();
76
77 void
78 locks(Task_locker*);
79
80 void
81 run(Workqueue*);
82
83 std::string
84 get_name() const;
85
86 private:
87 const General_options& options_;
88 Symbol_table* symtab_;
89 Layout* layout_;
90 Relobj* object_;
91 Task_token* symtab_lock_;
92 Task_token* blocker_;
93 };
94
95 // Scan the relocations for an object to see if they require any
96 // GOT/PLT/COPY relocations.
97
98 class Scan_relocs : public Task
99 {
100 public:
101 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
102 // unblocked when the task completes.
103 Scan_relocs(const General_options& options, Symbol_table* symtab,
104 Layout* layout, Relobj* object, Read_relocs_data* rd,
105 Task_token* symtab_lock, Task_token* blocker)
106 : options_(options), symtab_(symtab), layout_(layout), object_(object),
107 rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker)
108 { }
109
110 // The standard Task methods.
111
112 Task_token*
113 is_runnable();
114
115 void
116 locks(Task_locker*);
117
118 void
119 run(Workqueue*);
120
121 std::string
122 get_name() const;
123
124 private:
125 const General_options& options_;
126 Symbol_table* symtab_;
127 Layout* layout_;
128 Relobj* object_;
129 Read_relocs_data* rd_;
130 Task_token* symtab_lock_;
131 Task_token* blocker_;
132 };
133
134 // A class to perform all the relocations for an object file.
135
136 class Relocate_task : public Task
137 {
138 public:
139 Relocate_task(const General_options& options, const Symbol_table* symtab,
140 const Layout* layout, Relobj* object, Output_file* of,
141 Task_token* input_sections_blocker,
142 Task_token* output_sections_blocker, Task_token* final_blocker)
143 : options_(options), symtab_(symtab), layout_(layout), object_(object),
144 of_(of), input_sections_blocker_(input_sections_blocker),
145 output_sections_blocker_(output_sections_blocker),
146 final_blocker_(final_blocker)
147 { }
148
149 // The standard Task methods.
150
151 Task_token*
152 is_runnable();
153
154 void
155 locks(Task_locker*);
156
157 void
158 run(Workqueue*);
159
160 std::string
161 get_name() const;
162
163 private:
164 const General_options& options_;
165 const Symbol_table* symtab_;
166 const Layout* layout_;
167 Relobj* object_;
168 Output_file* of_;
169 Task_token* input_sections_blocker_;
170 Task_token* output_sections_blocker_;
171 Task_token* final_blocker_;
172 };
173
174 // During a relocatable link, this class records how relocations
175 // should be handled for a single input reloc section. An instance of
176 // this class is created while scanning relocs, and it is used while
177 // processing relocs.
178
179 class Relocatable_relocs
180 {
181 public:
182 // We use a vector of unsigned char to indicate how the input relocs
183 // should be handled. Each element is one of the following values.
184 // We create this vector when we initially scan the relocations.
185 enum Reloc_strategy
186 {
187 // Copy the input reloc. Don't modify it other than updating the
188 // r_offset field and the r_sym part of the r_info field.
189 RELOC_COPY,
190 // Copy the input reloc which is against an STT_SECTION symbol.
191 // Update the r_offset and r_sym part of the r_info field. Adjust
192 // the addend by subtracting the value of the old local symbol and
193 // adding the value of the new local symbol. The addend is in the
194 // SHT_RELA reloc and the contents of the data section do not need
195 // to be changed.
196 RELOC_ADJUST_FOR_SECTION_RELA,
197 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
198 // section need to be changed. The number indicates the number of
199 // bytes in the addend in the section contents.
200 RELOC_ADJUST_FOR_SECTION_1,
201 RELOC_ADJUST_FOR_SECTION_2,
202 RELOC_ADJUST_FOR_SECTION_4,
203 RELOC_ADJUST_FOR_SECTION_8,
204 // Discard the input reloc--process it completely when relocating
205 // the data section contents.
206 RELOC_DISCARD,
207 // An input reloc which is not discarded, but which requires
208 // target specific processing in order to update it.
209 RELOC_SPECIAL
210 };
211
212 Relocatable_relocs()
213 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
214 { }
215
216 // Record the number of relocs.
217 void
218 set_reloc_count(size_t reloc_count)
219 { this->reloc_strategies_.reserve(reloc_count); }
220
221 // Record what to do for the next reloc.
222 void
223 set_next_reloc_strategy(Reloc_strategy strategy)
224 {
225 this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
226 if (strategy != RELOC_DISCARD)
227 ++this->output_reloc_count_;
228 }
229
230 // Record the Output_data associated with this reloc section.
231 void
232 set_output_data(Output_data* posd)
233 {
234 gold_assert(this->posd_ == NULL);
235 this->posd_ = posd;
236 }
237
238 // Return the Output_data associated with this reloc section.
239 Output_data*
240 output_data() const
241 { return this->posd_; }
242
243 // Return what to do for reloc I.
244 Reloc_strategy
245 strategy(unsigned int i) const
246 {
247 gold_assert(i < this->reloc_strategies_.size());
248 return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
249 }
250
251 // Return the number of relocations to create in the output file.
252 size_t
253 output_reloc_count() const
254 { return this->output_reloc_count_; }
255
256 private:
257 typedef std::vector<unsigned char> Reloc_strategies;
258
259 // The strategies for the input reloc. There is one entry in this
260 // vector for each relocation in the input section.
261 Reloc_strategies reloc_strategies_;
262 // The number of relocations to be created in the output file.
263 size_t output_reloc_count_;
264 // The output data structure associated with this relocation.
265 Output_data* posd_;
266 };
267
268 // Standard relocation routines which are used on many targets. Here
269 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
270
271 template<int size, bool big_endian>
272 class Relocate_functions
273 {
274 private:
275 // Do a simple relocation with the addend in the section contents.
276 // VALSIZE is the size of the value.
277 template<int valsize>
278 static inline void
279 rel(unsigned char* view,
280 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
281 {
282 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
283 Valtype* wv = reinterpret_cast<Valtype*>(view);
284 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
285 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
286 }
287
288 // Do a simple relocation using a Symbol_value with the addend in
289 // the section contents. VALSIZE is the size of the value to
290 // relocate.
291 template<int valsize>
292 static inline void
293 rel(unsigned char* view,
294 const Sized_relobj<size, big_endian>* object,
295 const Symbol_value<size>* psymval)
296 {
297 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
298 Valtype* wv = reinterpret_cast<Valtype*>(view);
299 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
300 x = psymval->value(object, x);
301 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
302 }
303
304 // Do a simple relocation with the addend in the relocation.
305 // VALSIZE is the size of the value.
306 template<int valsize>
307 static inline void
308 rela(unsigned char* view,
309 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
310 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
311 {
312 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
313 Valtype* wv = reinterpret_cast<Valtype*>(view);
314 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
315 }
316
317 // Do a simple relocation using a symbol value with the addend in
318 // the relocation. VALSIZE is the size of the value.
319 template<int valsize>
320 static inline void
321 rela(unsigned char* view,
322 const Sized_relobj<size, big_endian>* object,
323 const Symbol_value<size>* psymval,
324 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
325 {
326 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
327 Valtype* wv = reinterpret_cast<Valtype*>(view);
328 Valtype x = psymval->value(object, addend);
329 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
330 }
331
332 // Do a simple PC relative relocation with the addend in the section
333 // contents. VALSIZE is the size of the value.
334 template<int valsize>
335 static inline void
336 pcrel(unsigned char* view,
337 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
338 typename elfcpp::Elf_types<size>::Elf_Addr address)
339 {
340 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
341 Valtype* wv = reinterpret_cast<Valtype*>(view);
342 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
343 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
344 }
345
346 // Do a simple PC relative relocation with a Symbol_value with the
347 // addend in the section contents. VALSIZE is the size of the
348 // value.
349 template<int valsize>
350 static inline void
351 pcrel(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 {
356 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
357 Valtype* wv = reinterpret_cast<Valtype*>(view);
358 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
359 x = psymval->value(object, x);
360 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
361 }
362
363 // Do a simple PC relative relocation with the addend in the
364 // relocation. VALSIZE is the size of the value.
365 template<int valsize>
366 static inline void
367 pcrela(unsigned char* view,
368 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
369 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
370 typename elfcpp::Elf_types<size>::Elf_Addr address)
371 {
372 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
373 Valtype* wv = reinterpret_cast<Valtype*>(view);
374 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
375 }
376
377 // Do a simple PC relative relocation with a Symbol_value with the
378 // addend in the relocation. VALSIZE is the size of the value.
379 template<int valsize>
380 static inline void
381 pcrela(unsigned char* view,
382 const Sized_relobj<size, big_endian>* object,
383 const Symbol_value<size>* psymval,
384 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
385 typename elfcpp::Elf_types<size>::Elf_Addr address)
386 {
387 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
388 Valtype* wv = reinterpret_cast<Valtype*>(view);
389 Valtype x = psymval->value(object, addend);
390 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
391 }
392
393 typedef Relocate_functions<size, big_endian> This;
394
395 public:
396 // Do a simple 8-bit REL relocation with the addend in the section
397 // contents.
398 static inline void
399 rel8(unsigned char* view, unsigned char value)
400 { This::template rel<8>(view, value); }
401
402 static inline void
403 rel8(unsigned char* view,
404 const Sized_relobj<size, big_endian>* object,
405 const Symbol_value<size>* psymval)
406 { This::template rel<8>(view, object, psymval); }
407
408 // Do an 8-bit RELA relocation with the addend in the relocation.
409 static inline void
410 rela8(unsigned char* view, unsigned char value, unsigned char addend)
411 { This::template rela<8>(view, value, addend); }
412
413 static inline void
414 rela8(unsigned char* view,
415 const Sized_relobj<size, big_endian>* object,
416 const Symbol_value<size>* psymval,
417 unsigned char addend)
418 { This::template rela<8>(view, object, psymval, addend); }
419
420 // Do a simple 8-bit PC relative relocation with the addend in the
421 // section contents.
422 static inline void
423 pcrel8(unsigned char* view, unsigned char value,
424 typename elfcpp::Elf_types<size>::Elf_Addr address)
425 { This::template pcrel<8>(view, value, address); }
426
427 static inline void
428 pcrel8(unsigned char* view,
429 const Sized_relobj<size, big_endian>* object,
430 const Symbol_value<size>* psymval,
431 typename elfcpp::Elf_types<size>::Elf_Addr address)
432 { This::template pcrel<8>(view, object, psymval, address); }
433
434 // Do a simple 8-bit PC relative RELA relocation with the addend in
435 // the reloc.
436 static inline void
437 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
438 typename elfcpp::Elf_types<size>::Elf_Addr address)
439 { This::template pcrela<8>(view, value, addend, address); }
440
441 static inline void
442 pcrela8(unsigned char* view,
443 const Sized_relobj<size, big_endian>* object,
444 const Symbol_value<size>* psymval,
445 unsigned char addend,
446 typename elfcpp::Elf_types<size>::Elf_Addr address)
447 { This::template pcrela<8>(view, object, psymval, addend, address); }
448
449 // Do a simple 16-bit REL relocation with the addend in the section
450 // contents.
451 static inline void
452 rel16(unsigned char* view, elfcpp::Elf_Half value)
453 { This::template rel<16>(view, value); }
454
455 static inline void
456 rel16(unsigned char* view,
457 const Sized_relobj<size, big_endian>* object,
458 const Symbol_value<size>* psymval)
459 { This::template rel<16>(view, object, psymval); }
460
461 // Do an 16-bit RELA relocation with the addend in the relocation.
462 static inline void
463 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
464 { This::template rela<16>(view, value, addend); }
465
466 static inline void
467 rela16(unsigned char* view,
468 const Sized_relobj<size, big_endian>* object,
469 const Symbol_value<size>* psymval,
470 elfcpp::Elf_Half addend)
471 { This::template rela<16>(view, object, psymval, addend); }
472
473 // Do a simple 16-bit PC relative REL relocation with the addend in
474 // the section contents.
475 static inline void
476 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
477 typename elfcpp::Elf_types<size>::Elf_Addr address)
478 { This::template pcrel<16>(view, value, address); }
479
480 static inline void
481 pcrel16(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<16>(view, object, psymval, address); }
486
487 // Do a simple 16-bit PC relative RELA relocation with the addend in
488 // the reloc.
489 static inline void
490 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
491 elfcpp::Elf_Half addend,
492 typename elfcpp::Elf_types<size>::Elf_Addr address)
493 { This::template pcrela<16>(view, value, addend, address); }
494
495 static inline void
496 pcrela16(unsigned char* view,
497 const Sized_relobj<size, big_endian>* object,
498 const Symbol_value<size>* psymval,
499 elfcpp::Elf_Half addend,
500 typename elfcpp::Elf_types<size>::Elf_Addr address)
501 { This::template pcrela<16>(view, object, psymval, addend, address); }
502
503 // Do a simple 32-bit REL relocation with the addend in the section
504 // contents.
505 static inline void
506 rel32(unsigned char* view, elfcpp::Elf_Word value)
507 { This::template rel<32>(view, value); }
508
509 static inline void
510 rel32(unsigned char* view,
511 const Sized_relobj<size, big_endian>* object,
512 const Symbol_value<size>* psymval)
513 { This::template rel<32>(view, object, psymval); }
514
515 // Do an 32-bit RELA relocation with the addend in the relocation.
516 static inline void
517 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
518 { This::template rela<32>(view, value, addend); }
519
520 static inline void
521 rela32(unsigned char* view,
522 const Sized_relobj<size, big_endian>* object,
523 const Symbol_value<size>* psymval,
524 elfcpp::Elf_Word addend)
525 { This::template rela<32>(view, object, psymval, addend); }
526
527 // Do a simple 32-bit PC relative REL relocation with the addend in
528 // the section contents.
529 static inline void
530 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
531 typename elfcpp::Elf_types<size>::Elf_Addr address)
532 { This::template pcrel<32>(view, value, address); }
533
534 static inline void
535 pcrel32(unsigned char* view,
536 const Sized_relobj<size, big_endian>* object,
537 const Symbol_value<size>* psymval,
538 typename elfcpp::Elf_types<size>::Elf_Addr address)
539 { This::template pcrel<32>(view, object, psymval, address); }
540
541 // Do a simple 32-bit PC relative RELA relocation with the addend in
542 // the relocation.
543 static inline void
544 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
545 elfcpp::Elf_Word addend,
546 typename elfcpp::Elf_types<size>::Elf_Addr address)
547 { This::template pcrela<32>(view, value, addend, address); }
548
549 static inline void
550 pcrela32(unsigned char* view,
551 const Sized_relobj<size, big_endian>* object,
552 const Symbol_value<size>* psymval,
553 elfcpp::Elf_Word addend,
554 typename elfcpp::Elf_types<size>::Elf_Addr address)
555 { This::template pcrela<32>(view, object, psymval, addend, address); }
556
557 // Do a simple 64-bit REL relocation with the addend in the section
558 // contents.
559 static inline void
560 rel64(unsigned char* view, elfcpp::Elf_Xword value)
561 { This::template rel<64>(view, value); }
562
563 static inline void
564 rel64(unsigned char* view,
565 const Sized_relobj<size, big_endian>* object,
566 const Symbol_value<size>* psymval)
567 { This::template rel<64>(view, object, psymval); }
568
569 // Do a 64-bit RELA relocation with the addend in the relocation.
570 static inline void
571 rela64(unsigned char* view, elfcpp::Elf_Xword value,
572 elfcpp::Elf_Xword addend)
573 { This::template rela<64>(view, value, addend); }
574
575 static inline void
576 rela64(unsigned char* view,
577 const Sized_relobj<size, big_endian>* object,
578 const Symbol_value<size>* psymval,
579 elfcpp::Elf_Xword addend)
580 { This::template rela<64>(view, object, psymval, addend); }
581
582 // Do a simple 64-bit PC relative REL relocation with the addend in
583 // the section contents.
584 static inline void
585 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
586 typename elfcpp::Elf_types<size>::Elf_Addr address)
587 { This::template pcrel<64>(view, value, address); }
588
589 static inline void
590 pcrel64(unsigned char* view,
591 const Sized_relobj<size, big_endian>* object,
592 const Symbol_value<size>* psymval,
593 typename elfcpp::Elf_types<size>::Elf_Addr address)
594 { This::template pcrel<64>(view, object, psymval, address); }
595
596 // Do a simple 64-bit PC relative RELA relocation with the addend in
597 // the relocation.
598 static inline void
599 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
600 elfcpp::Elf_Xword addend,
601 typename elfcpp::Elf_types<size>::Elf_Addr address)
602 { This::template pcrela<64>(view, value, addend, address); }
603
604 static inline void
605 pcrela64(unsigned char* view,
606 const Sized_relobj<size, big_endian>* object,
607 const Symbol_value<size>* psymval,
608 elfcpp::Elf_Xword addend,
609 typename elfcpp::Elf_types<size>::Elf_Addr address)
610 { This::template pcrela<64>(view, object, psymval, addend, address); }
611 };
612
613 // We try to avoid COPY relocations when possible. A COPY relocation
614 // may be required when an executable refers to a variable defined in
615 // a shared library. COPY relocations are problematic because they
616 // tie the executable to the exact size of the variable in the shared
617 // library. We can avoid them if all the references to the variable
618 // are in a writeable section. In that case we can simply use dynamic
619 // relocations. However, when scanning relocs, we don't know when we
620 // see the relocation whether we will be forced to use a COPY
621 // relocation or not. So we have to save the relocation during the
622 // reloc scanning, and then emit it as a dynamic relocation if
623 // necessary. This class implements that. It is used by the target
624 // specific code.
625
626 template<int size, bool big_endian>
627 class Copy_relocs
628 {
629 public:
630 Copy_relocs()
631 : entries_()
632 { }
633
634 // Return whether we need a COPY reloc for a reloc against GSYM,
635 // which is being applied to section SHNDX in OBJECT.
636 static bool
637 need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
638 Sized_symbol<size>* gsym);
639
640 // Save a Rel against SYM for possible emission later. SHNDX is the
641 // index of the section to which the reloc is being applied.
642 void
643 save(Symbol* sym, Relobj*, unsigned int shndx,
644 Output_section* output_section, const elfcpp::Rel<size, big_endian>&);
645
646 // Save a Rela against SYM for possible emission later.
647 void
648 save(Symbol* sym, Relobj*, unsigned int shndx,
649 Output_section* output_section, const elfcpp::Rela<size, big_endian>&);
650
651 // Return whether there are any relocs to emit. This also discards
652 // entries which need not be emitted.
653 bool
654 any_to_emit();
655
656 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
657 // is still defined in the dynamic object).
658 template<int sh_type>
659 void
660 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
661
662 private:
663 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
664 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
665
666 // This POD class holds the entries we are saving.
667 class Copy_reloc_entry
668 {
669 public:
670 Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
671 Relobj* relobj, unsigned int shndx,
672 Output_section* output_section,
673 Address address, Addend addend)
674 : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
675 shndx_(shndx), output_section_(output_section),
676 address_(address), addend_(addend)
677 { }
678
679 // Return whether we should emit this reloc. If we should not
680 // emit, we clear it.
681 bool
682 should_emit();
683
684 // Emit this reloc.
685
686 void
687 emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
688
689 void
690 emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
691
692 private:
693 Symbol* sym_;
694 unsigned int reloc_type_;
695 Relobj* relobj_;
696 unsigned int shndx_;
697 Output_section* output_section_;
698 Address address_;
699 Addend addend_;
700 };
701
702 // A list of relocs to be saved.
703 typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
704
705 // The list of relocs we are saving.
706 Copy_reloc_entries entries_;
707 };
708
709 // Track relocations while reading a section. This lets you ask for
710 // the relocation at a certain offset, and see how relocs occur
711 // between points of interest.
712
713 template<int size, bool big_endian>
714 class Track_relocs
715 {
716 public:
717 Track_relocs()
718 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
719 { }
720
721 // Initialize the Track_relocs object. OBJECT is the object holding
722 // the reloc section, RELOC_SHNDX is the section index of the reloc
723 // section, and RELOC_TYPE is the type of the reloc section
724 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
725 // something went wrong.
726 bool
727 initialize(Object* object, unsigned int reloc_shndx,
728 unsigned int reloc_type);
729
730 // Return the offset in the data section to which the next reloc
731 // applies. THis returns -1 if there is no next reloc.
732 off_t
733 next_offset() const;
734
735 // Return the symbol index of the next reloc. This returns -1U if
736 // there is no next reloc.
737 unsigned int
738 next_symndx() const;
739
740 // Advance to OFFSET within the data section, and return the number
741 // of relocs which would be skipped.
742 int
743 advance(off_t offset);
744
745 private:
746 // The contents of the input object's reloc section.
747 const unsigned char* prelocs_;
748 // The length of the reloc section.
749 section_size_type len_;
750 // Our current position in the reloc section.
751 section_size_type pos_;
752 // The size of the relocs in the section.
753 int reloc_size_;
754 };
755
756 } // End namespace gold.
757
758 #endif // !defined(GOLD_RELOC_H)
This page took 0.045931 seconds and 5 git commands to generate.