Commit | Line | Data |
---|---|---|
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 | ||
34 | namespace gold | |
35 | { | |
36 | ||
a3ad94ed | 37 | class General_options; |
b696e6d4 | 38 | class Object; |
f6ce93d6 | 39 | class Relobj; |
2c54b4f4 | 40 | struct Read_relocs_data; |
a3ad94ed | 41 | class Symbol; |
ead1e424 | 42 | class Layout; |
6a74a719 | 43 | class Output_data; |
4f4c5f80 | 44 | class Output_section; |
92e059d8 | 45 | |
5a6f7e2d ILT |
46 | template<int size> |
47 | class Sized_symbol; | |
48 | ||
b8e6aad9 | 49 | template<int size, bool big_endian> |
6fa2a40b | 50 | class Sized_relobj_file; |
b8e6aad9 ILT |
51 | |
52 | template<int size> | |
53 | class Symbol_value; | |
54 | ||
5a6f7e2d ILT |
55 | template<int sh_type, bool dynamic, int size, bool big_endian> |
56 | class 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 | ||
62 | class 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 | ||
99 | class 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 | ||
140 | class 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 |
180 | class 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 | ||
222 | class 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 | ||
44803b5d CC |
324 | template<int valsize> |
325 | class Bits; | |
326 | ||
92e059d8 ILT |
327 | // Standard relocation routines which are used on many targets. Here |
328 | // SIZE and BIG_ENDIAN refer to the target, not the relocation type. | |
329 | ||
330 | template<int size, bool big_endian> | |
331 | class Relocate_functions | |
332 | { | |
44803b5d CC |
333 | public: |
334 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
335 | typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addendtype; | |
336 | ||
337 | enum Overflow_check | |
338 | { | |
339 | CHECK_NONE, | |
340 | CHECK_SIGNED, | |
341 | CHECK_UNSIGNED, | |
342 | CHECK_SIGNED_OR_UNSIGNED | |
343 | }; | |
344 | ||
345 | enum Reloc_status | |
346 | { | |
347 | RELOC_OK, | |
348 | RELOC_OVERFLOW | |
349 | }; | |
350 | ||
351 | private: | |
352 | // Check for overflow. | |
353 | template<int valsize> | |
354 | static inline Reloc_status | |
355 | check_overflow(Address value, Overflow_check check) | |
356 | { | |
357 | switch (check) | |
358 | { | |
359 | case CHECK_SIGNED: | |
360 | if (size == 32) | |
361 | return (Bits<valsize>::has_overflow32(value) | |
362 | ? RELOC_OVERFLOW | |
363 | : RELOC_OK); | |
364 | else | |
365 | return (Bits<valsize>::has_overflow(value) | |
366 | ? RELOC_OVERFLOW | |
367 | : RELOC_OK); | |
368 | case CHECK_UNSIGNED: | |
369 | if (size == 32) | |
370 | return (Bits<valsize>::has_unsigned_overflow32(value) | |
371 | ? RELOC_OVERFLOW | |
372 | : RELOC_OK); | |
373 | else | |
374 | return (Bits<valsize>::has_unsigned_overflow(value) | |
375 | ? RELOC_OVERFLOW | |
376 | : RELOC_OK); | |
377 | case CHECK_SIGNED_OR_UNSIGNED: | |
378 | if (size == 32) | |
379 | return (Bits<valsize>::has_signed_unsigned_overflow32(value) | |
380 | ? RELOC_OVERFLOW | |
381 | : RELOC_OK); | |
382 | else | |
383 | return (Bits<valsize>::has_signed_unsigned_overflow64(value) | |
384 | ? RELOC_OVERFLOW | |
385 | : RELOC_OK); | |
386 | case CHECK_NONE: | |
387 | default: | |
388 | return RELOC_OK; | |
389 | } | |
390 | } | |
391 | ||
92e059d8 ILT |
392 | // Do a simple relocation with the addend in the section contents. |
393 | // VALSIZE is the size of the value. | |
394 | template<int valsize> | |
44803b5d CC |
395 | static inline Reloc_status |
396 | rel(unsigned char* view, Address value, Overflow_check check) | |
92e059d8 | 397 | { |
f6ce93d6 | 398 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; |
92e059d8 | 399 | Valtype* wv = reinterpret_cast<Valtype*>(view); |
44803b5d CC |
400 | Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv); |
401 | value += addend; | |
402 | elfcpp::Swap<valsize, big_endian>:: | |
403 | writeval(wv, static_cast<Valtype>(value)); | |
404 | return check_overflow<valsize>(value, check); | |
92e059d8 ILT |
405 | } |
406 | ||
29ab395d DK |
407 | // Like the above but for relocs at unaligned addresses. |
408 | template<int valsize> | |
44803b5d CC |
409 | static inline Reloc_status |
410 | rel_unaligned(unsigned char* view, Address value, Overflow_check check) | |
29ab395d DK |
411 | { |
412 | typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype | |
413 | Valtype; | |
44803b5d CC |
414 | Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view); |
415 | value += addend; | |
416 | elfcpp::Swap_unaligned<valsize, big_endian>:: | |
417 | writeval(view, static_cast<Valtype>(value)); | |
418 | return check_overflow<valsize>(value, check); | |
29ab395d DK |
419 | } |
420 | ||
b8e6aad9 ILT |
421 | // Do a simple relocation using a Symbol_value with the addend in |
422 | // the section contents. VALSIZE is the size of the value to | |
423 | // relocate. | |
424 | template<int valsize> | |
44803b5d | 425 | static inline Reloc_status |
b8e6aad9 | 426 | rel(unsigned char* view, |
6fa2a40b | 427 | const Sized_relobj_file<size, big_endian>* object, |
44803b5d CC |
428 | const Symbol_value<size>* psymval, |
429 | Overflow_check check) | |
b8e6aad9 ILT |
430 | { |
431 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
432 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
44803b5d CC |
433 | Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv); |
434 | Address value = psymval->value(object, addend); | |
435 | elfcpp::Swap<valsize, big_endian>:: | |
436 | writeval(wv, static_cast<Valtype>(value)); | |
437 | return check_overflow<valsize>(value, check); | |
b8e6aad9 ILT |
438 | } |
439 | ||
2c339f71 DK |
440 | // Like the above but for relocs at unaligned addresses. |
441 | template<int valsize> | |
44803b5d | 442 | static inline Reloc_status |
2c339f71 DK |
443 | rel_unaligned(unsigned char* view, |
444 | const Sized_relobj_file<size, big_endian>* object, | |
44803b5d CC |
445 | const Symbol_value<size>* psymval, |
446 | Overflow_check check) | |
2c339f71 DK |
447 | { |
448 | typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype | |
449 | Valtype; | |
44803b5d CC |
450 | Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view); |
451 | Address value = psymval->value(object, addend); | |
452 | elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value); | |
453 | return check_overflow<valsize>(value, check); | |
2c339f71 DK |
454 | } |
455 | ||
d830e0e0 ILT |
456 | // Do a simple relocation with the addend in the relocation. |
457 | // VALSIZE is the size of the value. | |
458 | template<int valsize> | |
44803b5d CC |
459 | static inline Reloc_status |
460 | rela(unsigned char* view, Address value, Addendtype addend, | |
461 | Overflow_check check) | |
d830e0e0 ILT |
462 | { |
463 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
464 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
44803b5d CC |
465 | value += addend; |
466 | elfcpp::Swap<valsize, big_endian>::writeval(wv, value); | |
467 | return check_overflow<valsize>(value, check); | |
d830e0e0 ILT |
468 | } |
469 | ||
470 | // Do a simple relocation using a symbol value with the addend in | |
471 | // the relocation. VALSIZE is the size of the value. | |
472 | template<int valsize> | |
44803b5d | 473 | static inline Reloc_status |
d830e0e0 | 474 | rela(unsigned char* view, |
6fa2a40b | 475 | const Sized_relobj_file<size, big_endian>* object, |
d830e0e0 | 476 | const Symbol_value<size>* psymval, |
44803b5d CC |
477 | Addendtype addend, |
478 | Overflow_check check) | |
d830e0e0 ILT |
479 | { |
480 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
481 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
44803b5d CC |
482 | Address value = psymval->value(object, addend); |
483 | elfcpp::Swap<valsize, big_endian>::writeval(wv, value); | |
484 | return check_overflow<valsize>(value, check); | |
d830e0e0 ILT |
485 | } |
486 | ||
92e059d8 ILT |
487 | // Do a simple PC relative relocation with the addend in the section |
488 | // contents. VALSIZE is the size of the value. | |
489 | template<int valsize> | |
44803b5d CC |
490 | static inline Reloc_status |
491 | pcrel(unsigned char* view, Address value, Address address, | |
492 | Overflow_check check) | |
92e059d8 | 493 | { |
f6ce93d6 | 494 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; |
92e059d8 | 495 | Valtype* wv = reinterpret_cast<Valtype*>(view); |
44803b5d CC |
496 | Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv); |
497 | value = value + addend - address; | |
498 | elfcpp::Swap<valsize, big_endian>::writeval(wv, value); | |
499 | return check_overflow<valsize>(value, check); | |
92e059d8 ILT |
500 | } |
501 | ||
29ab395d DK |
502 | // Like the above but for relocs at unaligned addresses. |
503 | template<int valsize> | |
44803b5d CC |
504 | static inline Reloc_status |
505 | pcrel_unaligned(unsigned char* view, Address value, Address address, | |
506 | Overflow_check check) | |
29ab395d DK |
507 | { |
508 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
44803b5d CC |
509 | Valtype addend = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view); |
510 | value = value + addend - address; | |
511 | elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value); | |
512 | return check_overflow<valsize>(value, check); | |
29ab395d DK |
513 | } |
514 | ||
b8e6aad9 ILT |
515 | // Do a simple PC relative relocation with a Symbol_value with the |
516 | // addend in the section contents. VALSIZE is the size of the | |
517 | // value. | |
518 | template<int valsize> | |
44803b5d | 519 | static inline Reloc_status |
b8e6aad9 | 520 | pcrel(unsigned char* view, |
6fa2a40b | 521 | const Sized_relobj_file<size, big_endian>* object, |
b8e6aad9 | 522 | const Symbol_value<size>* psymval, |
44803b5d CC |
523 | Address address, |
524 | Overflow_check check) | |
b8e6aad9 ILT |
525 | { |
526 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
527 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
44803b5d CC |
528 | Valtype addend = elfcpp::Swap<valsize, big_endian>::readval(wv); |
529 | Address value = psymval->value(object, addend) - address; | |
530 | elfcpp::Swap<valsize, big_endian>::writeval(wv, value); | |
531 | return check_overflow<valsize>(value, check); | |
b8e6aad9 ILT |
532 | } |
533 | ||
d830e0e0 ILT |
534 | // Do a simple PC relative relocation with the addend in the |
535 | // relocation. VALSIZE is the size of the value. | |
536 | template<int valsize> | |
44803b5d CC |
537 | static inline Reloc_status |
538 | pcrela(unsigned char* view, Address value, Addendtype addend, Address address, | |
539 | Overflow_check check) | |
d830e0e0 ILT |
540 | { |
541 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
542 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
44803b5d CC |
543 | value = value + addend - address; |
544 | elfcpp::Swap<valsize, big_endian>::writeval(wv, value); | |
545 | return check_overflow<valsize>(value, check); | |
d830e0e0 ILT |
546 | } |
547 | ||
548 | // Do a simple PC relative relocation with a Symbol_value with the | |
549 | // addend in the relocation. VALSIZE is the size of the value. | |
550 | template<int valsize> | |
44803b5d | 551 | static inline Reloc_status |
d830e0e0 | 552 | pcrela(unsigned char* view, |
6fa2a40b | 553 | const Sized_relobj_file<size, big_endian>* object, |
d830e0e0 | 554 | const Symbol_value<size>* psymval, |
44803b5d CC |
555 | Addendtype addend, |
556 | Address address, | |
557 | Overflow_check check) | |
d830e0e0 ILT |
558 | { |
559 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
560 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
44803b5d CC |
561 | Address value = psymval->value(object, addend) - address; |
562 | elfcpp::Swap<valsize, big_endian>::writeval(wv, value); | |
563 | return check_overflow<valsize>(value, check); | |
d830e0e0 ILT |
564 | } |
565 | ||
92e059d8 ILT |
566 | typedef Relocate_functions<size, big_endian> This; |
567 | ||
44803b5d | 568 | public: |
b8e6aad9 ILT |
569 | // Do a simple 8-bit REL relocation with the addend in the section |
570 | // contents. | |
92e059d8 | 571 | static inline void |
44803b5d CC |
572 | rel8(unsigned char* view, Address value) |
573 | { This::template rel<8>(view, value, CHECK_NONE); } | |
574 | ||
575 | static inline Reloc_status | |
576 | rel8_check(unsigned char* view, Address value, Overflow_check check) | |
577 | { return This::template rel<8>(view, value, check); } | |
b8e6aad9 ILT |
578 | |
579 | static inline void | |
580 | rel8(unsigned char* view, | |
6fa2a40b | 581 | const Sized_relobj_file<size, big_endian>* object, |
b8e6aad9 | 582 | const Symbol_value<size>* psymval) |
44803b5d CC |
583 | { This::template rel<8>(view, object, psymval, CHECK_NONE); } |
584 | ||
585 | static inline Reloc_status | |
586 | rel8_check(unsigned char* view, | |
587 | const Sized_relobj_file<size, big_endian>* object, | |
588 | const Symbol_value<size>* psymval, | |
589 | Overflow_check check) | |
590 | { return This::template rel<8>(view, object, psymval, check); } | |
92e059d8 | 591 | |
d830e0e0 ILT |
592 | // Do an 8-bit RELA relocation with the addend in the relocation. |
593 | static inline void | |
44803b5d CC |
594 | rela8(unsigned char* view, Address value, Addendtype addend) |
595 | { This::template rela<8>(view, value, addend, CHECK_NONE); } | |
596 | ||
597 | static inline Reloc_status | |
598 | rela8_check(unsigned char* view, Address value, Addendtype addend, | |
599 | Overflow_check check) | |
600 | { return This::template rela<8>(view, value, addend, check); } | |
d830e0e0 ILT |
601 | |
602 | static inline void | |
5b3463d9 | 603 | rela8(unsigned char* view, |
6fa2a40b | 604 | const Sized_relobj_file<size, big_endian>* object, |
d830e0e0 | 605 | const Symbol_value<size>* psymval, |
44803b5d CC |
606 | Addendtype addend) |
607 | { This::template rela<8>(view, object, psymval, addend, CHECK_NONE); } | |
608 | ||
609 | static inline Reloc_status | |
610 | rela8_check(unsigned char* view, | |
611 | const Sized_relobj_file<size, big_endian>* object, | |
612 | const Symbol_value<size>* psymval, | |
613 | Addendtype addend, | |
614 | Overflow_check check) | |
615 | { return This::template rela<8>(view, object, psymval, addend, check); } | |
d830e0e0 | 616 | |
92e059d8 | 617 | // Do a simple 8-bit PC relative relocation with the addend in the |
b8e6aad9 | 618 | // section contents. |
92e059d8 | 619 | static inline void |
44803b5d CC |
620 | pcrel8(unsigned char* view, unsigned char value, Address address) |
621 | { This::template pcrel<8>(view, value, address, CHECK_NONE); } | |
622 | ||
623 | static inline Reloc_status | |
624 | pcrel8_check(unsigned char* view, unsigned char value, Address address, | |
625 | Overflow_check check) | |
626 | { return This::template pcrel<8>(view, value, address, check); } | |
b8e6aad9 ILT |
627 | |
628 | static inline void | |
629 | pcrel8(unsigned char* view, | |
6fa2a40b | 630 | const Sized_relobj_file<size, big_endian>* object, |
b8e6aad9 | 631 | const Symbol_value<size>* psymval, |
44803b5d CC |
632 | Address address) |
633 | { This::template pcrel<8>(view, object, psymval, address, CHECK_NONE); } | |
634 | ||
635 | static inline Reloc_status | |
636 | pcrel8_check(unsigned char* view, | |
637 | const Sized_relobj_file<size, big_endian>* object, | |
638 | const Symbol_value<size>* psymval, | |
639 | Address address, | |
640 | Overflow_check check) | |
641 | { return This::template pcrel<8>(view, object, psymval, address, check); } | |
92e059d8 | 642 | |
d830e0e0 ILT |
643 | // Do a simple 8-bit PC relative RELA relocation with the addend in |
644 | // the reloc. | |
645 | static inline void | |
44803b5d CC |
646 | pcrela8(unsigned char* view, Address value, Addendtype addend, |
647 | Address address) | |
648 | { This::template pcrela<8>(view, value, addend, address, CHECK_NONE); } | |
649 | ||
650 | static inline Reloc_status | |
651 | pcrela8_check(unsigned char* view, Address value, Addendtype addend, | |
652 | Address address, Overflow_check check) | |
653 | { return This::template pcrela<8>(view, value, addend, address, check); } | |
d830e0e0 ILT |
654 | |
655 | static inline void | |
656 | pcrela8(unsigned char* view, | |
6fa2a40b | 657 | const Sized_relobj_file<size, big_endian>* object, |
d830e0e0 | 658 | const Symbol_value<size>* psymval, |
44803b5d CC |
659 | Addendtype addend, |
660 | Address address) | |
661 | { This::template pcrela<8>(view, object, psymval, addend, address, | |
662 | CHECK_NONE); } | |
663 | ||
664 | static inline Reloc_status | |
665 | pcrela8_check(unsigned char* view, | |
666 | const Sized_relobj_file<size, big_endian>* object, | |
667 | const Symbol_value<size>* psymval, | |
668 | Addendtype addend, | |
669 | Address address, | |
670 | Overflow_check check) | |
671 | { return This::template pcrela<8>(view, object, psymval, addend, address, | |
672 | check); } | |
d830e0e0 | 673 | |
b8e6aad9 ILT |
674 | // Do a simple 16-bit REL relocation with the addend in the section |
675 | // contents. | |
92e059d8 | 676 | static inline void |
44803b5d CC |
677 | rel16(unsigned char* view, Address value) |
678 | { This::template rel<16>(view, value, CHECK_NONE); } | |
679 | ||
680 | static inline Reloc_status | |
681 | rel16_check(unsigned char* view, Address value, Overflow_check check) | |
682 | { return This::template rel<16>(view, value, check); } | |
b8e6aad9 ILT |
683 | |
684 | static inline void | |
685 | rel16(unsigned char* view, | |
6fa2a40b | 686 | const Sized_relobj_file<size, big_endian>* object, |
b8e6aad9 | 687 | const Symbol_value<size>* psymval) |
44803b5d CC |
688 | { This::template rel<16>(view, object, psymval, CHECK_NONE); } |
689 | ||
690 | static inline Reloc_status | |
691 | rel16_check(unsigned char* view, | |
692 | const Sized_relobj_file<size, big_endian>* object, | |
693 | const Symbol_value<size>* psymval, | |
694 | Overflow_check check) | |
695 | { return This::template rel<16>(view, object, psymval, check); } | |
92e059d8 | 696 | |
d830e0e0 ILT |
697 | // Do an 16-bit RELA relocation with the addend in the relocation. |
698 | static inline void | |
44803b5d CC |
699 | rela16(unsigned char* view, Address value, Addendtype addend) |
700 | { This::template rela<16>(view, value, addend, CHECK_NONE); } | |
701 | ||
702 | static inline Reloc_status | |
703 | rela16_check(unsigned char* view, Address value, Addendtype addend, | |
704 | Overflow_check check) | |
705 | { return This::template rela<16>(view, value, addend, check); } | |
d830e0e0 ILT |
706 | |
707 | static inline void | |
708 | rela16(unsigned char* view, | |
6fa2a40b | 709 | const Sized_relobj_file<size, big_endian>* object, |
d830e0e0 | 710 | const Symbol_value<size>* psymval, |
44803b5d CC |
711 | Addendtype addend) |
712 | { This::template rela<16>(view, object, psymval, addend, CHECK_NONE); } | |
713 | ||
714 | static inline Reloc_status | |
715 | rela16_check(unsigned char* view, | |
716 | const Sized_relobj_file<size, big_endian>* object, | |
717 | const Symbol_value<size>* psymval, | |
718 | Addendtype addend, | |
719 | Overflow_check check) | |
720 | { return This::template rela<16>(view, object, psymval, addend, check); } | |
d830e0e0 | 721 | |
d830e0e0 | 722 | // Do a simple 16-bit PC relative REL relocation with the addend in |
b8e6aad9 | 723 | // the section contents. |
92e059d8 | 724 | static inline void |
44803b5d CC |
725 | pcrel16(unsigned char* view, Address value, Address address) |
726 | { This::template pcrel<16>(view, value, address, CHECK_NONE); } | |
727 | ||
728 | static inline Reloc_status | |
729 | pcrel16_check(unsigned char* view, Address value, Address address, | |
730 | Overflow_check check) | |
731 | { return This::template pcrel<16>(view, value, address, check); } | |
b8e6aad9 ILT |
732 | |
733 | static inline void | |
734 | pcrel16(unsigned char* view, | |
6fa2a40b | 735 | const Sized_relobj_file<size, big_endian>* object, |
b8e6aad9 | 736 | const Symbol_value<size>* psymval, |
44803b5d CC |
737 | Address address) |
738 | { This::template pcrel<16>(view, object, psymval, address, CHECK_NONE); } | |
739 | ||
740 | static inline Reloc_status | |
741 | pcrel16_check(unsigned char* view, | |
742 | const Sized_relobj_file<size, big_endian>* object, | |
743 | const Symbol_value<size>* psymval, | |
744 | Address address, | |
745 | Overflow_check check) | |
746 | { return This::template pcrel<16>(view, object, psymval, address, check); } | |
92e059d8 | 747 | |
d830e0e0 ILT |
748 | // Do a simple 16-bit PC relative RELA relocation with the addend in |
749 | // the reloc. | |
750 | static inline void | |
44803b5d CC |
751 | pcrela16(unsigned char* view, Address value, Addendtype addend, |
752 | Address address) | |
753 | { This::template pcrela<16>(view, value, addend, address, CHECK_NONE); } | |
754 | ||
755 | static inline Reloc_status | |
756 | pcrela16_check(unsigned char* view, Address value, Addendtype addend, | |
757 | Address address, Overflow_check check) | |
758 | { return This::template pcrela<16>(view, value, addend, address, check); } | |
d830e0e0 ILT |
759 | |
760 | static inline void | |
761 | pcrela16(unsigned char* view, | |
6fa2a40b | 762 | const Sized_relobj_file<size, big_endian>* object, |
d830e0e0 | 763 | const Symbol_value<size>* psymval, |
44803b5d CC |
764 | Addendtype addend, |
765 | Address address) | |
766 | { This::template pcrela<16>(view, object, psymval, addend, address, | |
767 | CHECK_NONE); } | |
768 | ||
769 | static inline Reloc_status | |
770 | pcrela16_check(unsigned char* view, | |
771 | const Sized_relobj_file<size, big_endian>* object, | |
772 | const Symbol_value<size>* psymval, | |
773 | Addendtype addend, | |
774 | Address address, | |
775 | Overflow_check check) | |
776 | { return This::template pcrela<16>(view, object, psymval, addend, address, | |
777 | check); } | |
d830e0e0 | 778 | |
92e059d8 ILT |
779 | // Do a simple 32-bit REL relocation with the addend in the section |
780 | // contents. | |
781 | static inline void | |
44803b5d CC |
782 | rel32(unsigned char* view, Address value) |
783 | { This::template rel<32>(view, value, CHECK_NONE); } | |
784 | ||
785 | static inline Reloc_status | |
786 | rel32_check(unsigned char* view, Address value, Overflow_check check) | |
787 | { return This::template rel<32>(view, value, check); } | |
b8e6aad9 | 788 | |
29ab395d DK |
789 | // Like above but for relocs at unaligned addresses. |
790 | static inline void | |
44803b5d CC |
791 | rel32_unaligned(unsigned char* view, Address value) |
792 | { This::template rel_unaligned<32>(view, value, CHECK_NONE); } | |
793 | ||
794 | static inline Reloc_status | |
795 | rel32_unaligned_check(unsigned char* view, Address value, | |
796 | Overflow_check check) | |
797 | { return This::template rel_unaligned<32>(view, value, check); } | |
29ab395d | 798 | |
b8e6aad9 ILT |
799 | static inline void |
800 | rel32(unsigned char* view, | |
6fa2a40b | 801 | const Sized_relobj_file<size, big_endian>* object, |
b8e6aad9 | 802 | const Symbol_value<size>* psymval) |
44803b5d CC |
803 | { This::template rel<32>(view, object, psymval, CHECK_NONE); } |
804 | ||
805 | static inline Reloc_status | |
806 | rel32_check(unsigned char* view, | |
807 | const Sized_relobj_file<size, big_endian>* object, | |
808 | const Symbol_value<size>* psymval, | |
809 | Overflow_check check) | |
810 | { return This::template rel<32>(view, object, psymval, check); } | |
92e059d8 | 811 | |
2c339f71 DK |
812 | // Like above but for relocs at unaligned addresses. |
813 | static inline void | |
814 | rel32_unaligned(unsigned char* view, | |
815 | const Sized_relobj_file<size, big_endian>* object, | |
816 | const Symbol_value<size>* psymval) | |
44803b5d | 817 | { This::template rel_unaligned<32>(view, object, psymval, CHECK_NONE); } |
2c339f71 | 818 | |
44803b5d CC |
819 | static inline Reloc_status |
820 | rel32_unaligned_check(unsigned char* view, | |
821 | const Sized_relobj_file<size, big_endian>* object, | |
822 | const Symbol_value<size>* psymval, | |
823 | Overflow_check check) | |
824 | { return This::template rel_unaligned<32>(view, object, psymval, check); } | |
825 | ||
826 | // Do a 32-bit RELA relocation with the addend in the relocation. | |
d830e0e0 | 827 | static inline void |
44803b5d CC |
828 | rela32(unsigned char* view, Address value, Addendtype addend) |
829 | { This::template rela<32>(view, value, addend, CHECK_NONE); } | |
830 | ||
831 | static inline Reloc_status | |
832 | rela32(unsigned char* view, Address value, Addendtype addend, | |
833 | Overflow_check check) | |
834 | { return This::template rela<32>(view, value, addend, check); } | |
d830e0e0 ILT |
835 | |
836 | static inline void | |
837 | rela32(unsigned char* view, | |
6fa2a40b | 838 | const Sized_relobj_file<size, big_endian>* object, |
d830e0e0 | 839 | const Symbol_value<size>* psymval, |
44803b5d CC |
840 | Addendtype addend) |
841 | { This::template rela<32>(view, object, psymval, addend, CHECK_NONE); } | |
842 | ||
843 | static inline Reloc_status | |
844 | rela32_check(unsigned char* view, | |
845 | const Sized_relobj_file<size, big_endian>* object, | |
846 | const Symbol_value<size>* psymval, | |
847 | Addendtype addend, | |
848 | Overflow_check check) | |
849 | { return This::template rela<32>(view, object, psymval, addend, check); } | |
d830e0e0 | 850 | |
92e059d8 ILT |
851 | // Do a simple 32-bit PC relative REL relocation with the addend in |
852 | // the section contents. | |
853 | static inline void | |
44803b5d CC |
854 | pcrel32(unsigned char* view, Address value, Address address) |
855 | { This::template pcrel<32>(view, value, address, CHECK_NONE); } | |
856 | ||
857 | static inline Reloc_status | |
858 | pcrel32_check(unsigned char* view, Address value, Address address, | |
859 | Overflow_check check) | |
860 | { return This::template pcrel<32>(view, value, address, check); } | |
b8e6aad9 | 861 | |
29ab395d DK |
862 | // Unaligned version of the above. |
863 | static inline void | |
44803b5d CC |
864 | pcrel32_unaligned(unsigned char* view, Address value, Address address) |
865 | { This::template pcrel_unaligned<32>(view, value, address, CHECK_NONE); } | |
866 | ||
867 | static inline Reloc_status | |
868 | pcrel32_unaligned_check(unsigned char* view, Address value, Address address, | |
869 | Overflow_check check) | |
870 | { return This::template pcrel_unaligned<32>(view, value, address, check); } | |
29ab395d | 871 | |
b8e6aad9 ILT |
872 | static inline void |
873 | pcrel32(unsigned char* view, | |
6fa2a40b | 874 | const Sized_relobj_file<size, big_endian>* object, |
b8e6aad9 | 875 | const Symbol_value<size>* psymval, |
44803b5d CC |
876 | Address address) |
877 | { This::template pcrel<32>(view, object, psymval, address, CHECK_NONE); } | |
878 | ||
879 | static inline Reloc_status | |
880 | pcrel32_check(unsigned char* view, | |
881 | const Sized_relobj_file<size, big_endian>* object, | |
882 | const Symbol_value<size>* psymval, | |
883 | Address address, | |
884 | Overflow_check check) | |
885 | { return This::template pcrel<32>(view, object, psymval, address, check); } | |
92e059d8 | 886 | |
d830e0e0 ILT |
887 | // Do a simple 32-bit PC relative RELA relocation with the addend in |
888 | // the relocation. | |
889 | static inline void | |
44803b5d CC |
890 | pcrela32(unsigned char* view, Address value, Addendtype addend, |
891 | Address address) | |
892 | { This::template pcrela<32>(view, value, addend, address, CHECK_NONE); } | |
893 | ||
894 | static inline Reloc_status | |
895 | pcrela32_check(unsigned char* view, Address value, Addendtype addend, | |
896 | Address address, Overflow_check check) | |
897 | { return This::template pcrela<32>(view, value, addend, address, check); } | |
d830e0e0 ILT |
898 | |
899 | static inline void | |
900 | pcrela32(unsigned char* view, | |
6fa2a40b | 901 | const Sized_relobj_file<size, big_endian>* object, |
d830e0e0 | 902 | const Symbol_value<size>* psymval, |
44803b5d CC |
903 | Addendtype addend, |
904 | Address address) | |
905 | { This::template pcrela<32>(view, object, psymval, addend, address, | |
906 | CHECK_NONE); } | |
907 | ||
908 | static inline Reloc_status | |
909 | pcrela32_check(unsigned char* view, | |
910 | const Sized_relobj_file<size, big_endian>* object, | |
911 | const Symbol_value<size>* psymval, | |
912 | Addendtype addend, | |
913 | Address address, | |
914 | Overflow_check check) | |
915 | { return This::template pcrela<32>(view, object, psymval, addend, address, | |
916 | check); } | |
d830e0e0 | 917 | |
92e059d8 ILT |
918 | // Do a simple 64-bit REL relocation with the addend in the section |
919 | // contents. | |
920 | static inline void | |
44803b5d CC |
921 | rel64(unsigned char* view, Address value) |
922 | { This::template rel<64>(view, value, CHECK_NONE); } | |
b8e6aad9 ILT |
923 | |
924 | static inline void | |
925 | rel64(unsigned char* view, | |
6fa2a40b | 926 | const Sized_relobj_file<size, big_endian>* object, |
b8e6aad9 | 927 | const Symbol_value<size>* psymval) |
44803b5d | 928 | { This::template rel<64>(view, object, psymval, CHECK_NONE); } |
92e059d8 | 929 | |
d830e0e0 ILT |
930 | // Do a 64-bit RELA relocation with the addend in the relocation. |
931 | static inline void | |
44803b5d CC |
932 | rela64(unsigned char* view, Address value, Addendtype addend) |
933 | { This::template rela<64>(view, value, addend, CHECK_NONE); } | |
d830e0e0 ILT |
934 | |
935 | static inline void | |
936 | rela64(unsigned char* view, | |
6fa2a40b | 937 | const Sized_relobj_file<size, big_endian>* object, |
d830e0e0 | 938 | const Symbol_value<size>* psymval, |
44803b5d CC |
939 | Addendtype addend) |
940 | { This::template rela<64>(view, object, psymval, addend, CHECK_NONE); } | |
d830e0e0 | 941 | |
92e059d8 ILT |
942 | // Do a simple 64-bit PC relative REL relocation with the addend in |
943 | // the section contents. | |
944 | static inline void | |
44803b5d CC |
945 | pcrel64(unsigned char* view, Address value, Address address) |
946 | { This::template pcrel<64>(view, value, address, CHECK_NONE); } | |
b8e6aad9 ILT |
947 | |
948 | static inline void | |
949 | pcrel64(unsigned char* view, | |
6fa2a40b | 950 | const Sized_relobj_file<size, big_endian>* object, |
b8e6aad9 | 951 | const Symbol_value<size>* psymval, |
44803b5d CC |
952 | Address address) |
953 | { This::template pcrel<64>(view, object, psymval, address, CHECK_NONE); } | |
d830e0e0 ILT |
954 | |
955 | // Do a simple 64-bit PC relative RELA relocation with the addend in | |
956 | // the relocation. | |
957 | static inline void | |
44803b5d CC |
958 | pcrela64(unsigned char* view, Address value, Addendtype addend, |
959 | Address address) | |
960 | { This::template pcrela<64>(view, value, addend, address, CHECK_NONE); } | |
d830e0e0 ILT |
961 | |
962 | static inline void | |
963 | pcrela64(unsigned char* view, | |
6fa2a40b | 964 | const Sized_relobj_file<size, big_endian>* object, |
d830e0e0 | 965 | const Symbol_value<size>* psymval, |
44803b5d CC |
966 | Addendtype addend, |
967 | Address address) | |
968 | { This::template pcrela<64>(view, object, psymval, addend, address, | |
969 | CHECK_NONE); } | |
5a6f7e2d ILT |
970 | }; |
971 | ||
bef2b434 ILT |
972 | // Integer manipulation functions used by various targets when |
973 | // performing relocations. | |
974 | ||
975 | template<int bits> | |
976 | class Bits | |
977 | { | |
978 | public: | |
979 | // Sign extend an n-bit unsigned integer stored in a uint32_t into | |
2c175ebc | 980 | // an int32_t. BITS must be between 1 and 32. |
bef2b434 ILT |
981 | static inline int32_t |
982 | sign_extend32(uint32_t val) | |
983 | { | |
2c175ebc | 984 | gold_assert(bits > 0 && bits <= 32); |
bef2b434 ILT |
985 | if (bits == 32) |
986 | return static_cast<int32_t>(val); | |
987 | uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - bits); | |
988 | val &= mask; | |
989 | uint32_t top_bit = 1U << (bits - 1); | |
990 | int32_t as_signed = static_cast<int32_t>(val); | |
991 | if ((val & top_bit) != 0) | |
992 | as_signed -= static_cast<int32_t>(top_bit * 2); | |
993 | return as_signed; | |
994 | } | |
995 | ||
996 | // Return true if VAL (stored in a uint32_t) has overflowed a signed | |
997 | // value with BITS bits. | |
998 | static inline bool | |
999 | has_overflow32(uint32_t val) | |
1000 | { | |
2c175ebc | 1001 | gold_assert(bits > 0 && bits <= 32); |
bef2b434 ILT |
1002 | if (bits == 32) |
1003 | return false; | |
1004 | int32_t max = (1 << (bits - 1)) - 1; | |
1005 | int32_t min = -(1 << (bits - 1)); | |
1006 | int32_t as_signed = static_cast<int32_t>(val); | |
1007 | return as_signed > max || as_signed < min; | |
1008 | } | |
1009 | ||
44803b5d CC |
1010 | // Return true if VAL (stored in a uint32_t) has overflowed an unsigned |
1011 | // value with BITS bits. | |
1012 | static inline bool | |
1013 | has_unsigned_overflow32(uint32_t val) | |
1014 | { | |
1015 | gold_assert(bits > 0 && bits <= 32); | |
1016 | if (bits == 32) | |
1017 | return false; | |
72c55146 | 1018 | uint32_t max = static_cast<uint32_t>((1U << bits) - 1); |
44803b5d CC |
1019 | return val > max; |
1020 | } | |
1021 | ||
bef2b434 ILT |
1022 | // Return true if VAL (stored in a uint32_t) has overflowed both a |
1023 | // signed and an unsigned value. E.g., | |
1024 | // Bits<8>::has_signed_unsigned_overflow32 would check -128 <= VAL < | |
1025 | // 255. | |
1026 | static inline bool | |
1027 | has_signed_unsigned_overflow32(uint32_t val) | |
1028 | { | |
2c175ebc | 1029 | gold_assert(bits > 0 && bits <= 32); |
bef2b434 ILT |
1030 | if (bits == 32) |
1031 | return false; | |
1032 | int32_t max = static_cast<int32_t>((1U << bits) - 1); | |
1033 | int32_t min = -(1 << (bits - 1)); | |
1034 | int32_t as_signed = static_cast<int32_t>(val); | |
1035 | return as_signed > max || as_signed < min; | |
1036 | } | |
1037 | ||
1038 | // Select bits from A and B using bits in MASK. For each n in | |
1039 | // [0..31], the n-th bit in the result is chosen from the n-th bits | |
1040 | // of A and B. A zero selects A and a one selects B. | |
1041 | static inline uint32_t | |
1042 | bit_select32(uint32_t a, uint32_t b, uint32_t mask) | |
1043 | { return (a & ~mask) | (b & mask); } | |
1044 | ||
1045 | // Sign extend an n-bit unsigned integer stored in a uint64_t into | |
2c175ebc | 1046 | // an int64_t. BITS must be between 1 and 64. |
bef2b434 ILT |
1047 | static inline int64_t |
1048 | sign_extend(uint64_t val) | |
1049 | { | |
2c175ebc | 1050 | gold_assert(bits > 0 && bits <= 64); |
bef2b434 ILT |
1051 | if (bits == 64) |
1052 | return static_cast<int64_t>(val); | |
1053 | uint64_t mask = (~static_cast<uint64_t>(0)) >> (64 - bits); | |
1054 | val &= mask; | |
1055 | uint64_t top_bit = static_cast<uint64_t>(1) << (bits - 1); | |
1056 | int64_t as_signed = static_cast<int64_t>(val); | |
1057 | if ((val & top_bit) != 0) | |
1058 | as_signed -= static_cast<int64_t>(top_bit * 2); | |
1059 | return as_signed; | |
1060 | } | |
1061 | ||
1062 | // Return true if VAL (stored in a uint64_t) has overflowed a signed | |
1063 | // value with BITS bits. | |
1064 | static inline bool | |
1065 | has_overflow(uint64_t val) | |
1066 | { | |
2c175ebc | 1067 | gold_assert(bits > 0 && bits <= 64); |
bef2b434 ILT |
1068 | if (bits == 64) |
1069 | return false; | |
1070 | int64_t max = (static_cast<int64_t>(1) << (bits - 1)) - 1; | |
1071 | int64_t min = -(static_cast<int64_t>(1) << (bits - 1)); | |
1072 | int64_t as_signed = static_cast<int64_t>(val); | |
1073 | return as_signed > max || as_signed < min; | |
1074 | } | |
1075 | ||
44803b5d CC |
1076 | // Return true if VAL (stored in a uint64_t) has overflowed an unsigned |
1077 | // value with BITS bits. | |
1078 | static inline bool | |
1079 | has_unsigned_overflow(uint64_t val) | |
1080 | { | |
1081 | gold_assert(bits > 0 && bits <= 64); | |
1082 | if (bits == 64) | |
1083 | return false; | |
72c55146 | 1084 | uint64_t max = (static_cast<uint64_t>(1) << bits) - 1; |
44803b5d CC |
1085 | return val > max; |
1086 | } | |
1087 | ||
bef2b434 ILT |
1088 | // Return true if VAL (stored in a uint64_t) has overflowed both a |
1089 | // signed and an unsigned value. E.g., | |
1090 | // Bits<8>::has_signed_unsigned_overflow would check -128 <= VAL < | |
1091 | // 255. | |
1092 | static inline bool | |
1093 | has_signed_unsigned_overflow64(uint64_t val) | |
1094 | { | |
2c175ebc | 1095 | gold_assert(bits > 0 && bits <= 64); |
bef2b434 ILT |
1096 | if (bits == 64) |
1097 | return false; | |
1098 | int64_t max = static_cast<int64_t>((static_cast<uint64_t>(1) << bits) - 1); | |
1099 | int64_t min = -(static_cast<int64_t>(1) << (bits - 1)); | |
1100 | int64_t as_signed = static_cast<int64_t>(val); | |
1101 | return as_signed > max || as_signed < min; | |
1102 | } | |
1103 | ||
1104 | // Select bits from A and B using bits in MASK. For each n in | |
1105 | // [0..31], the n-th bit in the result is chosen from the n-th bits | |
1106 | // of A and B. A zero selects A and a one selects B. | |
1107 | static inline uint64_t | |
1108 | bit_select64(uint64_t a, uint64_t b, uint64_t mask) | |
1109 | { return (a & ~mask) | (b & mask); } | |
1110 | }; | |
1111 | ||
730cdc88 ILT |
1112 | // Track relocations while reading a section. This lets you ask for |
1113 | // the relocation at a certain offset, and see how relocs occur | |
1114 | // between points of interest. | |
1115 | ||
1116 | template<int size, bool big_endian> | |
1117 | class Track_relocs | |
1118 | { | |
1119 | public: | |
1120 | Track_relocs() | |
b696e6d4 | 1121 | : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0) |
730cdc88 ILT |
1122 | { } |
1123 | ||
1124 | // Initialize the Track_relocs object. OBJECT is the object holding | |
1125 | // the reloc section, RELOC_SHNDX is the section index of the reloc | |
1126 | // section, and RELOC_TYPE is the type of the reloc section | |
1127 | // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if | |
1128 | // something went wrong. | |
1129 | bool | |
b696e6d4 | 1130 | initialize(Object* object, unsigned int reloc_shndx, |
730cdc88 ILT |
1131 | unsigned int reloc_type); |
1132 | ||
1133 | // Return the offset in the data section to which the next reloc | |
4dbfafcc | 1134 | // applies. This returns -1 if there is no next reloc. |
730cdc88 ILT |
1135 | off_t |
1136 | next_offset() const; | |
1137 | ||
1138 | // Return the symbol index of the next reloc. This returns -1U if | |
1139 | // there is no next reloc. | |
1140 | unsigned int | |
1141 | next_symndx() const; | |
1142 | ||
4dbfafcc ILT |
1143 | // Return the addend of the next reloc. This returns 0 if there is |
1144 | // no next reloc. | |
1145 | uint64_t | |
1146 | next_addend() const; | |
1147 | ||
730cdc88 ILT |
1148 | // Advance to OFFSET within the data section, and return the number |
1149 | // of relocs which would be skipped. | |
1150 | int | |
1151 | advance(off_t offset); | |
1152 | ||
c1027032 CC |
1153 | // Checkpoint the current position in the reloc section. |
1154 | section_size_type | |
1155 | checkpoint() const | |
1156 | { return this->pos_; } | |
1157 | ||
1158 | // Reset the position to CHECKPOINT. | |
1159 | void | |
1160 | reset(section_size_type checkpoint) | |
1161 | { this->pos_ = checkpoint; } | |
1162 | ||
730cdc88 | 1163 | private: |
b696e6d4 | 1164 | // The contents of the input object's reloc section. |
730cdc88 ILT |
1165 | const unsigned char* prelocs_; |
1166 | // The length of the reloc section. | |
8383303e | 1167 | section_size_type len_; |
730cdc88 | 1168 | // Our current position in the reloc section. |
8383303e | 1169 | section_size_type pos_; |
730cdc88 ILT |
1170 | // The size of the relocs in the section. |
1171 | int reloc_size_; | |
1172 | }; | |
1173 | ||
61ba1cf9 ILT |
1174 | } // End namespace gold. |
1175 | ||
1176 | #endif // !defined(GOLD_RELOC_H) |