Fix formatting of pe-mips.c file.
[deliverable/binutils-gdb.git] / gold / reloc.h
CommitLineData
61ba1cf9
ILT
1// reloc.h -- relocate input files for gold -*- C++ -*-
2
6f2750fe 3// Copyright (C) 2006-2016 Free Software Foundation, Inc.
6cb15b7f
ILT
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
61ba1cf9
ILT
23#ifndef GOLD_RELOC_H
24#define GOLD_RELOC_H
25
17a1d0a9 26#include <vector>
3d857b98 27#ifdef HAVE_BYTESWAP_H
92e059d8 28#include <byteswap.h>
3d857b98 29#endif
92e059d8 30
4c50553d 31#include "elfcpp.h"
61ba1cf9
ILT
32#include "workqueue.h"
33
34namespace gold
35{
36
a3ad94ed 37class General_options;
b696e6d4 38class Object;
f6ce93d6 39class Relobj;
2c54b4f4 40struct Read_relocs_data;
a3ad94ed 41class Symbol;
ead1e424 42class Layout;
6a74a719 43class Output_data;
4f4c5f80 44class Output_section;
92e059d8 45
5a6f7e2d
ILT
46template<int size>
47class Sized_symbol;
48
b8e6aad9 49template<int size, bool big_endian>
6fa2a40b 50class Sized_relobj_file;
b8e6aad9
ILT
51
52template<int size>
53class Symbol_value;
54
5a6f7e2d
ILT
55template<int sh_type, bool dynamic, int size, bool big_endian>
56class Output_data_reloc;
57
92e059d8
ILT
58// A class to read the relocations for an object file, and then queue
59// up a task to see if they require any GOT/PLT/COPY relocations in
60// the symbol table.
61
62class Read_relocs : public Task
63{
64 public:
93ceb764
ILT
65 // THIS_BLOCKER and NEXT_BLOCKER are passed along to a Scan_relocs
66 // or Gc_process_relocs task, so that they run in a deterministic
67 // order.
ad0f2072 68 Read_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
93ceb764 69 Task_token* this_blocker, Task_token* next_blocker)
ad0f2072 70 : symtab_(symtab), layout_(layout), object_(object),
93ceb764 71 this_blocker_(this_blocker), next_blocker_(next_blocker)
92e059d8
ILT
72 { }
73
74 // The standard Task methods.
75
17a1d0a9
ILT
76 Task_token*
77 is_runnable();
92e059d8 78
17a1d0a9
ILT
79 void
80 locks(Task_locker*);
92e059d8
ILT
81
82 void
83 run(Workqueue*);
84
c7912668
ILT
85 std::string
86 get_name() const;
87
92e059d8 88 private:
92e059d8 89 Symbol_table* symtab_;
ead1e424 90 Layout* layout_;
f6ce93d6 91 Relobj* object_;
93ceb764
ILT
92 Task_token* this_blocker_;
93 Task_token* next_blocker_;
92e059d8
ILT
94};
95
6d03d481
ST
96// Process the relocs to figure out which sections are garbage.
97// Very similar to scan relocs.
98
99class Gc_process_relocs : public Task
100{
101 public:
93ceb764
ILT
102 // THIS_BLOCKER prevents this task from running until the previous
103 // one is finished. NEXT_BLOCKER prevents the next task from
104 // running.
ad0f2072 105 Gc_process_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
93ceb764
ILT
106 Read_relocs_data* rd, Task_token* this_blocker,
107 Task_token* next_blocker)
ad0f2072 108 : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
93ceb764 109 this_blocker_(this_blocker), next_blocker_(next_blocker)
6d03d481
ST
110 { }
111
93ceb764
ILT
112 ~Gc_process_relocs();
113
6d03d481
ST
114 // The standard Task methods.
115
116 Task_token*
117 is_runnable();
118
119 void
120 locks(Task_locker*);
121
122 void
123 run(Workqueue*);
124
125 std::string
126 get_name() const;
127
128 private:
6d03d481
ST
129 Symbol_table* symtab_;
130 Layout* layout_;
131 Relobj* object_;
132 Read_relocs_data* rd_;
93ceb764
ILT
133 Task_token* this_blocker_;
134 Task_token* next_blocker_;
6d03d481
ST
135};
136
92e059d8
ILT
137// Scan the relocations for an object to see if they require any
138// GOT/PLT/COPY relocations.
139
140class Scan_relocs : public Task
141{
142 public:
93ceb764
ILT
143 // THIS_BLOCKER prevents this task from running until the previous
144 // one is finished. NEXT_BLOCKER prevents the next task from
145 // running.
ad0f2072 146 Scan_relocs(Symbol_table* symtab, Layout* layout, Relobj* object,
93ceb764
ILT
147 Read_relocs_data* rd, Task_token* this_blocker,
148 Task_token* next_blocker)
ad0f2072 149 : symtab_(symtab), layout_(layout), object_(object), rd_(rd),
93ceb764 150 this_blocker_(this_blocker), next_blocker_(next_blocker)
92e059d8
ILT
151 { }
152
93ceb764
ILT
153 ~Scan_relocs();
154
92e059d8
ILT
155 // The standard Task methods.
156
17a1d0a9
ILT
157 Task_token*
158 is_runnable();
92e059d8 159
17a1d0a9
ILT
160 void
161 locks(Task_locker*);
92e059d8
ILT
162
163 void
164 run(Workqueue*);
165
c7912668
ILT
166 std::string
167 get_name() const;
168
92e059d8 169 private:
92e059d8 170 Symbol_table* symtab_;
ead1e424 171 Layout* layout_;
f6ce93d6 172 Relobj* object_;
92e059d8 173 Read_relocs_data* rd_;
93ceb764
ILT
174 Task_token* this_blocker_;
175 Task_token* next_blocker_;
92e059d8
ILT
176};
177
178// A class to perform all the relocations for an object file.
179
61ba1cf9
ILT
180class Relocate_task : public Task
181{
182 public:
ad0f2072
ILT
183 Relocate_task(const Symbol_table* symtab, const Layout* layout,
184 Relobj* object, Output_file* of,
730cdc88
ILT
185 Task_token* input_sections_blocker,
186 Task_token* output_sections_blocker, Task_token* final_blocker)
ad0f2072
ILT
187 : symtab_(symtab), layout_(layout), object_(object), of_(of),
188 input_sections_blocker_(input_sections_blocker),
730cdc88
ILT
189 output_sections_blocker_(output_sections_blocker),
190 final_blocker_(final_blocker)
61ba1cf9
ILT
191 { }
192
193 // The standard Task methods.
194
17a1d0a9
ILT
195 Task_token*
196 is_runnable();
61ba1cf9 197
17a1d0a9
ILT
198 void
199 locks(Task_locker*);
61ba1cf9
ILT
200
201 void
202 run(Workqueue*);
203
c7912668
ILT
204 std::string
205 get_name() const;
206
61ba1cf9 207 private:
61ba1cf9 208 const Symbol_table* symtab_;
92e059d8 209 const Layout* layout_;
f6ce93d6 210 Relobj* object_;
61ba1cf9 211 Output_file* of_;
730cdc88
ILT
212 Task_token* input_sections_blocker_;
213 Task_token* output_sections_blocker_;
61ba1cf9
ILT
214 Task_token* final_blocker_;
215};
216
6a74a719
ILT
217// During a relocatable link, this class records how relocations
218// should be handled for a single input reloc section. An instance of
219// this class is created while scanning relocs, and it is used while
220// processing relocs.
221
222class Relocatable_relocs
223{
224 public:
225 // We use a vector of unsigned char to indicate how the input relocs
226 // should be handled. Each element is one of the following values.
227 // We create this vector when we initially scan the relocations.
228 enum Reloc_strategy
229 {
230 // Copy the input reloc. Don't modify it other than updating the
231 // r_offset field and the r_sym part of the r_info field.
232 RELOC_COPY,
233 // Copy the input reloc which is against an STT_SECTION symbol.
234 // Update the r_offset and r_sym part of the r_info field. Adjust
235 // the addend by subtracting the value of the old local symbol and
236 // adding the value of the new local symbol. The addend is in the
237 // SHT_RELA reloc and the contents of the data section do not need
238 // to be changed.
239 RELOC_ADJUST_FOR_SECTION_RELA,
7019cd25
ILT
240 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
241 // adjusted.
242 RELOC_ADJUST_FOR_SECTION_0,
6a74a719
ILT
243 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
244 // section need to be changed. The number indicates the number of
245 // bytes in the addend in the section contents.
246 RELOC_ADJUST_FOR_SECTION_1,
247 RELOC_ADJUST_FOR_SECTION_2,
248 RELOC_ADJUST_FOR_SECTION_4,
249 RELOC_ADJUST_FOR_SECTION_8,
2c339f71
DK
250 // Like RELOC_ADJUST_FOR_SECTION_4 but for unaligned relocs.
251 RELOC_ADJUST_FOR_SECTION_4_UNALIGNED,
6a74a719
ILT
252 // Discard the input reloc--process it completely when relocating
253 // the data section contents.
254 RELOC_DISCARD,
255 // An input reloc which is not discarded, but which requires
256 // target specific processing in order to update it.
257 RELOC_SPECIAL
258 };
259
260 Relocatable_relocs()
261 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
262 { }
263
264 // Record the number of relocs.
265 void
266 set_reloc_count(size_t reloc_count)
267 { this->reloc_strategies_.reserve(reloc_count); }
268
269 // Record what to do for the next reloc.
270 void
2ea97941 271 set_next_reloc_strategy(Reloc_strategy strategy)
6a74a719 272 {
2ea97941
ILT
273 this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
274 if (strategy != RELOC_DISCARD)
6a74a719
ILT
275 ++this->output_reloc_count_;
276 }
277
278 // Record the Output_data associated with this reloc section.
279 void
280 set_output_data(Output_data* posd)
281 {
282 gold_assert(this->posd_ == NULL);
283 this->posd_ = posd;
284 }
285
286 // Return the Output_data associated with this reloc section.
287 Output_data*
288 output_data() const
289 { return this->posd_; }
290
291 // Return what to do for reloc I.
292 Reloc_strategy
293 strategy(unsigned int i) const
294 {
295 gold_assert(i < this->reloc_strategies_.size());
296 return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
297 }
298
91a65d2f
AM
299 // Set the strategy for reloc I.
300 void
301 set_strategy(unsigned int i, Reloc_strategy strategy)
302 {
303 gold_assert(i < this->reloc_strategies_.size());
304 this->reloc_strategies_[i] = strategy;
305 }
306
6a74a719
ILT
307 // Return the number of relocations to create in the output file.
308 size_t
309 output_reloc_count() const
310 { return this->output_reloc_count_; }
311
312 private:
313 typedef std::vector<unsigned char> Reloc_strategies;
314
315 // The strategies for the input reloc. There is one entry in this
316 // vector for each relocation in the input section.
317 Reloc_strategies reloc_strategies_;
318 // The number of relocations to be created in the output file.
319 size_t output_reloc_count_;
320 // The output data structure associated with this relocation.
321 Output_data* posd_;
322};
323
92e059d8
ILT
324// Standard relocation routines which are used on many targets. Here
325// SIZE and BIG_ENDIAN refer to the target, not the relocation type.
326
327template<int size, bool big_endian>
328class Relocate_functions
329{
330private:
331 // Do a simple relocation with the addend in the section contents.
332 // VALSIZE is the size of the value.
333 template<int valsize>
334 static inline void
f6ce93d6
ILT
335 rel(unsigned char* view,
336 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
92e059d8 337 {
f6ce93d6 338 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
92e059d8 339 Valtype* wv = reinterpret_cast<Valtype*>(view);
f6ce93d6
ILT
340 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
341 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
92e059d8
ILT
342 }
343
29ab395d
DK
344 // Like the above but for relocs at unaligned addresses.
345 template<int valsize>
346 static inline void
347 rel_unaligned(unsigned char* view,
348 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
349 {
350 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
351 Valtype;
352 Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
353 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x + value);
354 }
355
b8e6aad9
ILT
356 // Do a simple relocation using a Symbol_value with the addend in
357 // the section contents. VALSIZE is the size of the value to
358 // relocate.
359 template<int valsize>
360 static inline void
361 rel(unsigned char* view,
6fa2a40b 362 const Sized_relobj_file<size, big_endian>* object,
b8e6aad9
ILT
363 const Symbol_value<size>* psymval)
364 {
365 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
366 Valtype* wv = reinterpret_cast<Valtype*>(view);
367 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
368 x = psymval->value(object, x);
369 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
370 }
371
2c339f71
DK
372 // Like the above but for relocs at unaligned addresses.
373 template<int valsize>
374 static inline void
375 rel_unaligned(unsigned char* view,
376 const Sized_relobj_file<size, big_endian>* object,
377 const Symbol_value<size>* psymval)
378 {
379 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
380 Valtype;
381 Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
382 x = psymval->value(object, x);
383 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x);
384 }
385
d830e0e0
ILT
386 // Do a simple relocation with the addend in the relocation.
387 // VALSIZE is the size of the value.
388 template<int valsize>
389 static inline void
390 rela(unsigned char* view,
391 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
392 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
393 {
394 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
395 Valtype* wv = reinterpret_cast<Valtype*>(view);
396 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
397 }
398
399 // Do a simple relocation using a symbol value with the addend in
400 // the relocation. VALSIZE is the size of the value.
401 template<int valsize>
402 static inline void
403 rela(unsigned char* view,
6fa2a40b 404 const Sized_relobj_file<size, big_endian>* object,
d830e0e0
ILT
405 const Symbol_value<size>* psymval,
406 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
407 {
408 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
409 Valtype* wv = reinterpret_cast<Valtype*>(view);
410 Valtype x = psymval->value(object, addend);
411 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
412 }
413
92e059d8
ILT
414 // Do a simple PC relative relocation with the addend in the section
415 // contents. VALSIZE is the size of the value.
416 template<int valsize>
417 static inline void
f6ce93d6
ILT
418 pcrel(unsigned char* view,
419 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
92e059d8
ILT
420 typename elfcpp::Elf_types<size>::Elf_Addr address)
421 {
f6ce93d6 422 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
92e059d8 423 Valtype* wv = reinterpret_cast<Valtype*>(view);
f6ce93d6
ILT
424 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
425 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
92e059d8
ILT
426 }
427
29ab395d
DK
428 // Like the above but for relocs at unaligned addresses.
429 template<int valsize>
430 static inline void
431 pcrel_unaligned(unsigned char* view,
432 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
433 typename elfcpp::Elf_types<size>::Elf_Addr address)
434 {
435 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
436 Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view);
437 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
438 x + value - address);
439 }
440
b8e6aad9
ILT
441 // Do a simple PC relative relocation with a Symbol_value with the
442 // addend in the section contents. VALSIZE is the size of the
443 // value.
444 template<int valsize>
445 static inline void
446 pcrel(unsigned char* view,
6fa2a40b 447 const Sized_relobj_file<size, big_endian>* object,
b8e6aad9
ILT
448 const Symbol_value<size>* psymval,
449 typename elfcpp::Elf_types<size>::Elf_Addr address)
450 {
451 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
452 Valtype* wv = reinterpret_cast<Valtype*>(view);
453 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
454 x = psymval->value(object, x);
455 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
456 }
457
d830e0e0
ILT
458 // Do a simple PC relative relocation with the addend in the
459 // relocation. VALSIZE is the size of the value.
460 template<int valsize>
461 static inline void
462 pcrela(unsigned char* view,
463 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
464 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
465 typename elfcpp::Elf_types<size>::Elf_Addr address)
466 {
467 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
468 Valtype* wv = reinterpret_cast<Valtype*>(view);
469 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
470 }
471
472 // Do a simple PC relative relocation with a Symbol_value with the
473 // addend in the relocation. VALSIZE is the size of the value.
474 template<int valsize>
475 static inline void
476 pcrela(unsigned char* view,
6fa2a40b 477 const Sized_relobj_file<size, big_endian>* object,
d830e0e0
ILT
478 const Symbol_value<size>* psymval,
479 typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
480 typename elfcpp::Elf_types<size>::Elf_Addr address)
481 {
482 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
483 Valtype* wv = reinterpret_cast<Valtype*>(view);
484 Valtype x = psymval->value(object, addend);
485 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
486 }
487
92e059d8
ILT
488 typedef Relocate_functions<size, big_endian> This;
489
490public:
b8e6aad9
ILT
491 // Do a simple 8-bit REL relocation with the addend in the section
492 // contents.
92e059d8
ILT
493 static inline void
494 rel8(unsigned char* view, unsigned char value)
b8e6aad9
ILT
495 { This::template rel<8>(view, value); }
496
497 static inline void
498 rel8(unsigned char* view,
6fa2a40b 499 const Sized_relobj_file<size, big_endian>* object,
b8e6aad9
ILT
500 const Symbol_value<size>* psymval)
501 { This::template rel<8>(view, object, psymval); }
92e059d8 502
d830e0e0
ILT
503 // Do an 8-bit RELA relocation with the addend in the relocation.
504 static inline void
5b3463d9 505 rela8(unsigned char* view, unsigned char value, unsigned char addend)
d830e0e0
ILT
506 { This::template rela<8>(view, value, addend); }
507
508 static inline void
5b3463d9 509 rela8(unsigned char* view,
6fa2a40b 510 const Sized_relobj_file<size, big_endian>* object,
d830e0e0
ILT
511 const Symbol_value<size>* psymval,
512 unsigned char addend)
513 { This::template rela<8>(view, object, psymval, addend); }
514
92e059d8 515 // Do a simple 8-bit PC relative relocation with the addend in the
b8e6aad9 516 // section contents.
92e059d8
ILT
517 static inline void
518 pcrel8(unsigned char* view, unsigned char value,
519 typename elfcpp::Elf_types<size>::Elf_Addr address)
b8e6aad9
ILT
520 { This::template pcrel<8>(view, value, address); }
521
522 static inline void
523 pcrel8(unsigned char* view,
6fa2a40b 524 const Sized_relobj_file<size, big_endian>* object,
b8e6aad9
ILT
525 const Symbol_value<size>* psymval,
526 typename elfcpp::Elf_types<size>::Elf_Addr address)
527 { This::template pcrel<8>(view, object, psymval, address); }
92e059d8 528
d830e0e0
ILT
529 // Do a simple 8-bit PC relative RELA relocation with the addend in
530 // the reloc.
531 static inline void
532 pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
533 typename elfcpp::Elf_types<size>::Elf_Addr address)
534 { This::template pcrela<8>(view, value, addend, address); }
535
536 static inline void
537 pcrela8(unsigned char* view,
6fa2a40b 538 const Sized_relobj_file<size, big_endian>* object,
d830e0e0
ILT
539 const Symbol_value<size>* psymval,
540 unsigned char addend,
541 typename elfcpp::Elf_types<size>::Elf_Addr address)
542 { This::template pcrela<8>(view, object, psymval, addend, address); }
543
b8e6aad9
ILT
544 // Do a simple 16-bit REL relocation with the addend in the section
545 // contents.
92e059d8
ILT
546 static inline void
547 rel16(unsigned char* view, elfcpp::Elf_Half value)
b8e6aad9
ILT
548 { This::template rel<16>(view, value); }
549
550 static inline void
551 rel16(unsigned char* view,
6fa2a40b 552 const Sized_relobj_file<size, big_endian>* object,
b8e6aad9
ILT
553 const Symbol_value<size>* psymval)
554 { This::template rel<16>(view, object, psymval); }
92e059d8 555
d830e0e0
ILT
556 // Do an 16-bit RELA relocation with the addend in the relocation.
557 static inline void
558 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
559 { This::template rela<16>(view, value, addend); }
560
561 static inline void
562 rela16(unsigned char* view,
6fa2a40b 563 const Sized_relobj_file<size, big_endian>* object,
d830e0e0
ILT
564 const Symbol_value<size>* psymval,
565 elfcpp::Elf_Half addend)
566 { This::template rela<16>(view, object, psymval, addend); }
567
d830e0e0 568 // Do a simple 16-bit PC relative REL relocation with the addend in
b8e6aad9 569 // the section contents.
92e059d8 570 static inline void
d830e0e0 571 pcrel16(unsigned char* view, elfcpp::Elf_Half value,
92e059d8 572 typename elfcpp::Elf_types<size>::Elf_Addr address)
b8e6aad9
ILT
573 { This::template pcrel<16>(view, value, address); }
574
575 static inline void
576 pcrel16(unsigned char* view,
6fa2a40b 577 const Sized_relobj_file<size, big_endian>* object,
b8e6aad9
ILT
578 const Symbol_value<size>* psymval,
579 typename elfcpp::Elf_types<size>::Elf_Addr address)
580 { This::template pcrel<16>(view, object, psymval, address); }
92e059d8 581
d830e0e0
ILT
582 // Do a simple 16-bit PC relative RELA relocation with the addend in
583 // the reloc.
584 static inline void
585 pcrela16(unsigned char* view, elfcpp::Elf_Half value,
586 elfcpp::Elf_Half addend,
587 typename elfcpp::Elf_types<size>::Elf_Addr address)
588 { This::template pcrela<16>(view, value, addend, address); }
589
590 static inline void
591 pcrela16(unsigned char* view,
6fa2a40b 592 const Sized_relobj_file<size, big_endian>* object,
d830e0e0
ILT
593 const Symbol_value<size>* psymval,
594 elfcpp::Elf_Half addend,
595 typename elfcpp::Elf_types<size>::Elf_Addr address)
596 { This::template pcrela<16>(view, object, psymval, addend, address); }
597
92e059d8
ILT
598 // Do a simple 32-bit REL relocation with the addend in the section
599 // contents.
600 static inline void
601 rel32(unsigned char* view, elfcpp::Elf_Word value)
b8e6aad9
ILT
602 { This::template rel<32>(view, value); }
603
29ab395d
DK
604 // Like above but for relocs at unaligned addresses.
605 static inline void
606 rel32_unaligned(unsigned char* view, elfcpp::Elf_Word value)
607 { This::template rel_unaligned<32>(view, value); }
608
b8e6aad9
ILT
609 static inline void
610 rel32(unsigned char* view,
6fa2a40b 611 const Sized_relobj_file<size, big_endian>* object,
b8e6aad9
ILT
612 const Symbol_value<size>* psymval)
613 { This::template rel<32>(view, object, psymval); }
92e059d8 614
2c339f71
DK
615 // Like above but for relocs at unaligned addresses.
616 static inline void
617 rel32_unaligned(unsigned char* view,
618 const Sized_relobj_file<size, big_endian>* object,
619 const Symbol_value<size>* psymval)
620 { This::template rel_unaligned<32>(view, object, psymval); }
621
d830e0e0
ILT
622 // Do an 32-bit RELA relocation with the addend in the relocation.
623 static inline void
624 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
625 { This::template rela<32>(view, value, addend); }
626
627 static inline void
628 rela32(unsigned char* view,
6fa2a40b 629 const Sized_relobj_file<size, big_endian>* object,
d830e0e0
ILT
630 const Symbol_value<size>* psymval,
631 elfcpp::Elf_Word addend)
632 { This::template rela<32>(view, object, psymval, addend); }
633
92e059d8
ILT
634 // Do a simple 32-bit PC relative REL relocation with the addend in
635 // the section contents.
636 static inline void
637 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
638 typename elfcpp::Elf_types<size>::Elf_Addr address)
b8e6aad9
ILT
639 { This::template pcrel<32>(view, value, address); }
640
29ab395d
DK
641 // Unaligned version of the above.
642 static inline void
643 pcrel32_unaligned(unsigned char* view, elfcpp::Elf_Word value,
644 typename elfcpp::Elf_types<size>::Elf_Addr address)
645 { This::template pcrel_unaligned<32>(view, value, address); }
646
b8e6aad9
ILT
647 static inline void
648 pcrel32(unsigned char* view,
6fa2a40b 649 const Sized_relobj_file<size, big_endian>* object,
b8e6aad9
ILT
650 const Symbol_value<size>* psymval,
651 typename elfcpp::Elf_types<size>::Elf_Addr address)
652 { This::template pcrel<32>(view, object, psymval, address); }
92e059d8 653
d830e0e0
ILT
654 // Do a simple 32-bit PC relative RELA relocation with the addend in
655 // the relocation.
656 static inline void
657 pcrela32(unsigned char* view, elfcpp::Elf_Word value,
658 elfcpp::Elf_Word addend,
659 typename elfcpp::Elf_types<size>::Elf_Addr address)
660 { This::template pcrela<32>(view, value, addend, address); }
661
662 static inline void
663 pcrela32(unsigned char* view,
6fa2a40b 664 const Sized_relobj_file<size, big_endian>* object,
d830e0e0
ILT
665 const Symbol_value<size>* psymval,
666 elfcpp::Elf_Word addend,
667 typename elfcpp::Elf_types<size>::Elf_Addr address)
668 { This::template pcrela<32>(view, object, psymval, addend, address); }
669
92e059d8
ILT
670 // Do a simple 64-bit REL relocation with the addend in the section
671 // contents.
672 static inline void
f6ce93d6 673 rel64(unsigned char* view, elfcpp::Elf_Xword value)
b8e6aad9
ILT
674 { This::template rel<64>(view, value); }
675
676 static inline void
677 rel64(unsigned char* view,
6fa2a40b 678 const Sized_relobj_file<size, big_endian>* object,
b8e6aad9
ILT
679 const Symbol_value<size>* psymval)
680 { This::template rel<64>(view, object, psymval); }
92e059d8 681
d830e0e0
ILT
682 // Do a 64-bit RELA relocation with the addend in the relocation.
683 static inline void
684 rela64(unsigned char* view, elfcpp::Elf_Xword value,
685 elfcpp::Elf_Xword addend)
686 { This::template rela<64>(view, value, addend); }
687
688 static inline void
689 rela64(unsigned char* view,
6fa2a40b 690 const Sized_relobj_file<size, big_endian>* object,
d830e0e0
ILT
691 const Symbol_value<size>* psymval,
692 elfcpp::Elf_Xword addend)
693 { This::template rela<64>(view, object, psymval, addend); }
694
92e059d8
ILT
695 // Do a simple 64-bit PC relative REL relocation with the addend in
696 // the section contents.
697 static inline void
f6ce93d6 698 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
92e059d8 699 typename elfcpp::Elf_types<size>::Elf_Addr address)
b8e6aad9
ILT
700 { This::template pcrel<64>(view, value, address); }
701
702 static inline void
703 pcrel64(unsigned char* view,
6fa2a40b 704 const Sized_relobj_file<size, big_endian>* object,
b8e6aad9
ILT
705 const Symbol_value<size>* psymval,
706 typename elfcpp::Elf_types<size>::Elf_Addr address)
707 { This::template pcrel<64>(view, object, psymval, address); }
d830e0e0
ILT
708
709 // Do a simple 64-bit PC relative RELA relocation with the addend in
710 // the relocation.
711 static inline void
712 pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
713 elfcpp::Elf_Xword addend,
714 typename elfcpp::Elf_types<size>::Elf_Addr address)
715 { This::template pcrela<64>(view, value, addend, address); }
716
717 static inline void
718 pcrela64(unsigned char* view,
6fa2a40b 719 const Sized_relobj_file<size, big_endian>* object,
d830e0e0
ILT
720 const Symbol_value<size>* psymval,
721 elfcpp::Elf_Xword addend,
722 typename elfcpp::Elf_types<size>::Elf_Addr address)
723 { This::template pcrela<64>(view, object, psymval, addend, address); }
5a6f7e2d
ILT
724};
725
bef2b434
ILT
726// Integer manipulation functions used by various targets when
727// performing relocations.
728
729template<int bits>
730class Bits
731{
732 public:
733 // Sign extend an n-bit unsigned integer stored in a uint32_t into
2c175ebc 734 // an int32_t. BITS must be between 1 and 32.
bef2b434
ILT
735 static inline int32_t
736 sign_extend32(uint32_t val)
737 {
2c175ebc 738 gold_assert(bits > 0 && bits <= 32);
bef2b434
ILT
739 if (bits == 32)
740 return static_cast<int32_t>(val);
741 uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - bits);
742 val &= mask;
743 uint32_t top_bit = 1U << (bits - 1);
744 int32_t as_signed = static_cast<int32_t>(val);
745 if ((val & top_bit) != 0)
746 as_signed -= static_cast<int32_t>(top_bit * 2);
747 return as_signed;
748 }
749
750 // Return true if VAL (stored in a uint32_t) has overflowed a signed
751 // value with BITS bits.
752 static inline bool
753 has_overflow32(uint32_t val)
754 {
2c175ebc 755 gold_assert(bits > 0 && bits <= 32);
bef2b434
ILT
756 if (bits == 32)
757 return false;
758 int32_t max = (1 << (bits - 1)) - 1;
759 int32_t min = -(1 << (bits - 1));
760 int32_t as_signed = static_cast<int32_t>(val);
761 return as_signed > max || as_signed < min;
762 }
763
764 // Return true if VAL (stored in a uint32_t) has overflowed both a
765 // signed and an unsigned value. E.g.,
766 // Bits<8>::has_signed_unsigned_overflow32 would check -128 <= VAL <
767 // 255.
768 static inline bool
769 has_signed_unsigned_overflow32(uint32_t val)
770 {
2c175ebc 771 gold_assert(bits > 0 && bits <= 32);
bef2b434
ILT
772 if (bits == 32)
773 return false;
774 int32_t max = static_cast<int32_t>((1U << bits) - 1);
775 int32_t min = -(1 << (bits - 1));
776 int32_t as_signed = static_cast<int32_t>(val);
777 return as_signed > max || as_signed < min;
778 }
779
780 // Select bits from A and B using bits in MASK. For each n in
781 // [0..31], the n-th bit in the result is chosen from the n-th bits
782 // of A and B. A zero selects A and a one selects B.
783 static inline uint32_t
784 bit_select32(uint32_t a, uint32_t b, uint32_t mask)
785 { return (a & ~mask) | (b & mask); }
786
787 // Sign extend an n-bit unsigned integer stored in a uint64_t into
2c175ebc 788 // an int64_t. BITS must be between 1 and 64.
bef2b434
ILT
789 static inline int64_t
790 sign_extend(uint64_t val)
791 {
2c175ebc 792 gold_assert(bits > 0 && bits <= 64);
bef2b434
ILT
793 if (bits == 64)
794 return static_cast<int64_t>(val);
795 uint64_t mask = (~static_cast<uint64_t>(0)) >> (64 - bits);
796 val &= mask;
797 uint64_t top_bit = static_cast<uint64_t>(1) << (bits - 1);
798 int64_t as_signed = static_cast<int64_t>(val);
799 if ((val & top_bit) != 0)
800 as_signed -= static_cast<int64_t>(top_bit * 2);
801 return as_signed;
802 }
803
804 // Return true if VAL (stored in a uint64_t) has overflowed a signed
805 // value with BITS bits.
806 static inline bool
807 has_overflow(uint64_t val)
808 {
2c175ebc 809 gold_assert(bits > 0 && bits <= 64);
bef2b434
ILT
810 if (bits == 64)
811 return false;
812 int64_t max = (static_cast<int64_t>(1) << (bits - 1)) - 1;
813 int64_t min = -(static_cast<int64_t>(1) << (bits - 1));
814 int64_t as_signed = static_cast<int64_t>(val);
815 return as_signed > max || as_signed < min;
816 }
817
818 // Return true if VAL (stored in a uint64_t) has overflowed both a
819 // signed and an unsigned value. E.g.,
820 // Bits<8>::has_signed_unsigned_overflow would check -128 <= VAL <
821 // 255.
822 static inline bool
823 has_signed_unsigned_overflow64(uint64_t val)
824 {
2c175ebc 825 gold_assert(bits > 0 && bits <= 64);
bef2b434
ILT
826 if (bits == 64)
827 return false;
828 int64_t max = static_cast<int64_t>((static_cast<uint64_t>(1) << bits) - 1);
829 int64_t min = -(static_cast<int64_t>(1) << (bits - 1));
830 int64_t as_signed = static_cast<int64_t>(val);
831 return as_signed > max || as_signed < min;
832 }
833
834 // Select bits from A and B using bits in MASK. For each n in
835 // [0..31], the n-th bit in the result is chosen from the n-th bits
836 // of A and B. A zero selects A and a one selects B.
837 static inline uint64_t
838 bit_select64(uint64_t a, uint64_t b, uint64_t mask)
839 { return (a & ~mask) | (b & mask); }
840};
841
730cdc88
ILT
842// Track relocations while reading a section. This lets you ask for
843// the relocation at a certain offset, and see how relocs occur
844// between points of interest.
845
846template<int size, bool big_endian>
847class Track_relocs
848{
849 public:
850 Track_relocs()
b696e6d4 851 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
730cdc88
ILT
852 { }
853
854 // Initialize the Track_relocs object. OBJECT is the object holding
855 // the reloc section, RELOC_SHNDX is the section index of the reloc
856 // section, and RELOC_TYPE is the type of the reloc section
857 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
858 // something went wrong.
859 bool
b696e6d4 860 initialize(Object* object, unsigned int reloc_shndx,
730cdc88
ILT
861 unsigned int reloc_type);
862
863 // Return the offset in the data section to which the next reloc
4dbfafcc 864 // applies. This returns -1 if there is no next reloc.
730cdc88
ILT
865 off_t
866 next_offset() const;
867
868 // Return the symbol index of the next reloc. This returns -1U if
869 // there is no next reloc.
870 unsigned int
871 next_symndx() const;
872
4dbfafcc
ILT
873 // Return the addend of the next reloc. This returns 0 if there is
874 // no next reloc.
875 uint64_t
876 next_addend() const;
877
730cdc88
ILT
878 // Advance to OFFSET within the data section, and return the number
879 // of relocs which would be skipped.
880 int
881 advance(off_t offset);
882
c1027032
CC
883 // Checkpoint the current position in the reloc section.
884 section_size_type
885 checkpoint() const
886 { return this->pos_; }
887
888 // Reset the position to CHECKPOINT.
889 void
890 reset(section_size_type checkpoint)
891 { this->pos_ = checkpoint; }
892
730cdc88 893 private:
b696e6d4 894 // The contents of the input object's reloc section.
730cdc88
ILT
895 const unsigned char* prelocs_;
896 // The length of the reloc section.
8383303e 897 section_size_type len_;
730cdc88 898 // Our current position in the reloc section.
8383303e 899 section_size_type pos_;
730cdc88
ILT
900 // The size of the relocs in the section.
901 int reloc_size_;
902};
903
61ba1cf9
ILT
904} // End namespace gold.
905
906#endif // !defined(GOLD_RELOC_H)
This page took 0.436692 seconds and 4 git commands to generate.