]>
Commit | Line | Data |
---|---|---|
4a657b0d DK |
1 | // arm.cc -- arm target support for gold. |
2 | ||
3 | // Copyright 2009 Free Software Foundation, Inc. | |
4 | // Written by Doug Kwan <dougkwan@google.com> based on the i386 code | |
5 | // by Ian Lance Taylor <iant@google.com>. | |
b569affa DK |
6 | // This file also contains borrowed and adapted code from |
7 | // bfd/elf32-arm.c. | |
4a657b0d DK |
8 | |
9 | // This file is part of gold. | |
10 | ||
11 | // This program is free software; you can redistribute it and/or modify | |
12 | // it under the terms of the GNU General Public License as published by | |
13 | // the Free Software Foundation; either version 3 of the License, or | |
14 | // (at your option) any later version. | |
15 | ||
16 | // This program is distributed in the hope that it will be useful, | |
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | // GNU General Public License for more details. | |
20 | ||
21 | // You should have received a copy of the GNU General Public License | |
22 | // along with this program; if not, write to the Free Software | |
23 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
24 | // MA 02110-1301, USA. | |
25 | ||
26 | #include "gold.h" | |
27 | ||
28 | #include <cstring> | |
29 | #include <limits> | |
30 | #include <cstdio> | |
31 | #include <string> | |
56ee5e00 | 32 | #include <algorithm> |
4a657b0d DK |
33 | |
34 | #include "elfcpp.h" | |
35 | #include "parameters.h" | |
36 | #include "reloc.h" | |
37 | #include "arm.h" | |
38 | #include "object.h" | |
39 | #include "symtab.h" | |
40 | #include "layout.h" | |
41 | #include "output.h" | |
42 | #include "copy-relocs.h" | |
43 | #include "target.h" | |
44 | #include "target-reloc.h" | |
45 | #include "target-select.h" | |
46 | #include "tls.h" | |
47 | #include "defstd.h" | |
f345227a | 48 | #include "gc.h" |
4a657b0d DK |
49 | |
50 | namespace | |
51 | { | |
52 | ||
53 | using namespace gold; | |
54 | ||
94cdfcff DK |
55 | template<bool big_endian> |
56 | class Output_data_plt_arm; | |
57 | ||
56ee5e00 DK |
58 | template<bool big_endian> |
59 | class Stub_table; | |
60 | ||
61 | template<bool big_endian> | |
62 | class Arm_input_section; | |
63 | ||
07f508a2 DK |
64 | template<bool big_endian> |
65 | class Arm_output_section; | |
66 | ||
67 | template<bool big_endian> | |
68 | class Arm_relobj; | |
69 | ||
b569affa DK |
70 | template<bool big_endian> |
71 | class Target_arm; | |
72 | ||
73 | // For convenience. | |
74 | typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address; | |
75 | ||
76 | // Maximum branch offsets for ARM, THUMB and THUMB2. | |
77 | const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8); | |
78 | const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8); | |
79 | const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4); | |
80 | const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4); | |
81 | const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4); | |
82 | const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4); | |
83 | ||
4a657b0d DK |
84 | // The arm target class. |
85 | // | |
86 | // This is a very simple port of gold for ARM-EABI. It is intended for | |
87 | // supporting Android only for the time being. Only these relocation types | |
88 | // are supported. | |
89 | // | |
90 | // R_ARM_NONE | |
91 | // R_ARM_ABS32 | |
be8fcb75 ILT |
92 | // R_ARM_ABS32_NOI |
93 | // R_ARM_ABS16 | |
94 | // R_ARM_ABS12 | |
95 | // R_ARM_ABS8 | |
96 | // R_ARM_THM_ABS5 | |
97 | // R_ARM_BASE_ABS | |
4a657b0d DK |
98 | // R_ARM_REL32 |
99 | // R_ARM_THM_CALL | |
100 | // R_ARM_COPY | |
101 | // R_ARM_GLOB_DAT | |
102 | // R_ARM_BASE_PREL | |
103 | // R_ARM_JUMP_SLOT | |
104 | // R_ARM_RELATIVE | |
105 | // R_ARM_GOTOFF32 | |
106 | // R_ARM_GOT_BREL | |
7f5309a5 | 107 | // R_ARM_GOT_PREL |
4a657b0d DK |
108 | // R_ARM_PLT32 |
109 | // R_ARM_CALL | |
110 | // R_ARM_JUMP24 | |
111 | // R_ARM_TARGET1 | |
112 | // R_ARM_PREL31 | |
7f5309a5 | 113 | // R_ARM_ABS8 |
fd3c5f0b ILT |
114 | // R_ARM_MOVW_ABS_NC |
115 | // R_ARM_MOVT_ABS | |
116 | // R_ARM_THM_MOVW_ABS_NC | |
c2a122b6 ILT |
117 | // R_ARM_THM_MOVT_ABS |
118 | // R_ARM_MOVW_PREL_NC | |
119 | // R_ARM_MOVT_PREL | |
120 | // R_ARM_THM_MOVW_PREL_NC | |
121 | // R_ARM_THM_MOVT_PREL | |
4a657b0d | 122 | // |
4a657b0d | 123 | // TODOs: |
4a657b0d | 124 | // - Support more relocation types as needed. |
94cdfcff DK |
125 | // - Make PLTs more flexible for different architecture features like |
126 | // Thumb-2 and BE8. | |
11af873f | 127 | // There are probably a lot more. |
4a657b0d | 128 | |
b569affa DK |
129 | // Instruction template class. This class is similar to the insn_sequence |
130 | // struct in bfd/elf32-arm.c. | |
131 | ||
132 | class Insn_template | |
133 | { | |
134 | public: | |
135 | // Types of instruction templates. | |
136 | enum Type | |
137 | { | |
138 | THUMB16_TYPE = 1, | |
139 | THUMB32_TYPE, | |
140 | ARM_TYPE, | |
141 | DATA_TYPE | |
142 | }; | |
143 | ||
144 | // Factory methods to create instrunction templates in different formats. | |
145 | ||
146 | static const Insn_template | |
147 | thumb16_insn(uint32_t data) | |
148 | { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); } | |
149 | ||
150 | // A bit of a hack. A Thumb conditional branch, in which the proper | |
151 | // condition is inserted when we build the stub. | |
152 | static const Insn_template | |
153 | thumb16_bcond_insn(uint32_t data) | |
154 | { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 1); } | |
155 | ||
156 | static const Insn_template | |
157 | thumb32_insn(uint32_t data) | |
158 | { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); } | |
159 | ||
160 | static const Insn_template | |
161 | thumb32_b_insn(uint32_t data, int reloc_addend) | |
162 | { | |
163 | return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24, | |
164 | reloc_addend); | |
165 | } | |
166 | ||
167 | static const Insn_template | |
168 | arm_insn(uint32_t data) | |
169 | { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); } | |
170 | ||
171 | static const Insn_template | |
172 | arm_rel_insn(unsigned data, int reloc_addend) | |
173 | { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); } | |
174 | ||
175 | static const Insn_template | |
176 | data_word(unsigned data, unsigned int r_type, int reloc_addend) | |
177 | { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); } | |
178 | ||
179 | // Accessors. This class is used for read-only objects so no modifiers | |
180 | // are provided. | |
181 | ||
182 | uint32_t | |
183 | data() const | |
184 | { return this->data_; } | |
185 | ||
186 | // Return the instruction sequence type of this. | |
187 | Type | |
188 | type() const | |
189 | { return this->type_; } | |
190 | ||
191 | // Return the ARM relocation type of this. | |
192 | unsigned int | |
193 | r_type() const | |
194 | { return this->r_type_; } | |
195 | ||
196 | int32_t | |
197 | reloc_addend() const | |
198 | { return this->reloc_addend_; } | |
199 | ||
200 | // Return size of instrunction template in bytes. | |
201 | size_t | |
202 | size() const; | |
203 | ||
204 | // Return byte-alignment of instrunction template. | |
205 | unsigned | |
206 | alignment() const; | |
207 | ||
208 | private: | |
209 | // We make the constructor private to ensure that only the factory | |
210 | // methods are used. | |
211 | inline | |
212 | Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend) | |
213 | : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend) | |
214 | { } | |
215 | ||
216 | // Instruction specific data. This is used to store information like | |
217 | // some of the instruction bits. | |
218 | uint32_t data_; | |
219 | // Instruction template type. | |
220 | Type type_; | |
221 | // Relocation type if there is a relocation or R_ARM_NONE otherwise. | |
222 | unsigned int r_type_; | |
223 | // Relocation addend. | |
224 | int32_t reloc_addend_; | |
225 | }; | |
226 | ||
227 | // Macro for generating code to stub types. One entry per long/short | |
228 | // branch stub | |
229 | ||
230 | #define DEF_STUBS \ | |
231 | DEF_STUB(long_branch_any_any) \ | |
232 | DEF_STUB(long_branch_v4t_arm_thumb) \ | |
233 | DEF_STUB(long_branch_thumb_only) \ | |
234 | DEF_STUB(long_branch_v4t_thumb_thumb) \ | |
235 | DEF_STUB(long_branch_v4t_thumb_arm) \ | |
236 | DEF_STUB(short_branch_v4t_thumb_arm) \ | |
237 | DEF_STUB(long_branch_any_arm_pic) \ | |
238 | DEF_STUB(long_branch_any_thumb_pic) \ | |
239 | DEF_STUB(long_branch_v4t_thumb_thumb_pic) \ | |
240 | DEF_STUB(long_branch_v4t_arm_thumb_pic) \ | |
241 | DEF_STUB(long_branch_v4t_thumb_arm_pic) \ | |
242 | DEF_STUB(long_branch_thumb_only_pic) \ | |
243 | DEF_STUB(a8_veneer_b_cond) \ | |
244 | DEF_STUB(a8_veneer_b) \ | |
245 | DEF_STUB(a8_veneer_bl) \ | |
246 | DEF_STUB(a8_veneer_blx) | |
247 | ||
248 | // Stub types. | |
249 | ||
250 | #define DEF_STUB(x) arm_stub_##x, | |
251 | typedef enum | |
252 | { | |
253 | arm_stub_none, | |
254 | DEF_STUBS | |
255 | ||
256 | // First reloc stub type. | |
257 | arm_stub_reloc_first = arm_stub_long_branch_any_any, | |
258 | // Last reloc stub type. | |
259 | arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic, | |
260 | ||
261 | // First Cortex-A8 stub type. | |
262 | arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond, | |
263 | // Last Cortex-A8 stub type. | |
264 | arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx, | |
265 | ||
266 | // Last stub type. | |
267 | arm_stub_type_last = arm_stub_a8_veneer_blx | |
268 | } Stub_type; | |
269 | #undef DEF_STUB | |
270 | ||
271 | // Stub template class. Templates are meant to be read-only objects. | |
272 | // A stub template for a stub type contains all read-only attributes | |
273 | // common to all stubs of the same type. | |
274 | ||
275 | class Stub_template | |
276 | { | |
277 | public: | |
278 | Stub_template(Stub_type, const Insn_template*, size_t); | |
279 | ||
280 | ~Stub_template() | |
281 | { } | |
282 | ||
283 | // Return stub type. | |
284 | Stub_type | |
285 | type() const | |
286 | { return this->type_; } | |
287 | ||
288 | // Return an array of instruction templates. | |
289 | const Insn_template* | |
290 | insns() const | |
291 | { return this->insns_; } | |
292 | ||
293 | // Return size of template in number of instructions. | |
294 | size_t | |
295 | insn_count() const | |
296 | { return this->insn_count_; } | |
297 | ||
298 | // Return size of template in bytes. | |
299 | size_t | |
300 | size() const | |
301 | { return this->size_; } | |
302 | ||
303 | // Return alignment of the stub template. | |
304 | unsigned | |
305 | alignment() const | |
306 | { return this->alignment_; } | |
307 | ||
308 | // Return whether entry point is in thumb mode. | |
309 | bool | |
310 | entry_in_thumb_mode() const | |
311 | { return this->entry_in_thumb_mode_; } | |
312 | ||
313 | // Return number of relocations in this template. | |
314 | size_t | |
315 | reloc_count() const | |
316 | { return this->relocs_.size(); } | |
317 | ||
318 | // Return index of the I-th instruction with relocation. | |
319 | size_t | |
320 | reloc_insn_index(size_t i) const | |
321 | { | |
322 | gold_assert(i < this->relocs_.size()); | |
323 | return this->relocs_[i].first; | |
324 | } | |
325 | ||
326 | // Return the offset of the I-th instruction with relocation from the | |
327 | // beginning of the stub. | |
328 | section_size_type | |
329 | reloc_offset(size_t i) const | |
330 | { | |
331 | gold_assert(i < this->relocs_.size()); | |
332 | return this->relocs_[i].second; | |
333 | } | |
334 | ||
335 | private: | |
336 | // This contains information about an instruction template with a relocation | |
337 | // and its offset from start of stub. | |
338 | typedef std::pair<size_t, section_size_type> Reloc; | |
339 | ||
340 | // A Stub_template may not be copied. We want to share templates as much | |
341 | // as possible. | |
342 | Stub_template(const Stub_template&); | |
343 | Stub_template& operator=(const Stub_template&); | |
344 | ||
345 | // Stub type. | |
346 | Stub_type type_; | |
347 | // Points to an array of Insn_templates. | |
348 | const Insn_template* insns_; | |
349 | // Number of Insn_templates in insns_[]. | |
350 | size_t insn_count_; | |
351 | // Size of templated instructions in bytes. | |
352 | size_t size_; | |
353 | // Alignment of templated instructions. | |
354 | unsigned alignment_; | |
355 | // Flag to indicate if entry is in thumb mode. | |
356 | bool entry_in_thumb_mode_; | |
357 | // A table of reloc instruction indices and offsets. We can find these by | |
358 | // looking at the instruction templates but we pre-compute and then stash | |
359 | // them here for speed. | |
360 | std::vector<Reloc> relocs_; | |
361 | }; | |
362 | ||
363 | // | |
364 | // A class for code stubs. This is a base class for different type of | |
365 | // stubs used in the ARM target. | |
366 | // | |
367 | ||
368 | class Stub | |
369 | { | |
370 | private: | |
371 | static const section_offset_type invalid_offset = | |
372 | static_cast<section_offset_type>(-1); | |
373 | ||
374 | public: | |
375 | Stub(const Stub_template* stub_template) | |
376 | : stub_template_(stub_template), offset_(invalid_offset) | |
377 | { } | |
378 | ||
379 | virtual | |
380 | ~Stub() | |
381 | { } | |
382 | ||
383 | // Return the stub template. | |
384 | const Stub_template* | |
385 | stub_template() const | |
386 | { return this->stub_template_; } | |
387 | ||
388 | // Return offset of code stub from beginning of its containing stub table. | |
389 | section_offset_type | |
390 | offset() const | |
391 | { | |
392 | gold_assert(this->offset_ != invalid_offset); | |
393 | return this->offset_; | |
394 | } | |
395 | ||
396 | // Set offset of code stub from beginning of its containing stub table. | |
397 | void | |
398 | set_offset(section_offset_type offset) | |
399 | { this->offset_ = offset; } | |
400 | ||
401 | // Return the relocation target address of the i-th relocation in the | |
402 | // stub. This must be defined in a child class. | |
403 | Arm_address | |
404 | reloc_target(size_t i) | |
405 | { return this->do_reloc_target(i); } | |
406 | ||
407 | // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written. | |
408 | void | |
409 | write(unsigned char* view, section_size_type view_size, bool big_endian) | |
410 | { this->do_write(view, view_size, big_endian); } | |
411 | ||
412 | protected: | |
413 | // This must be defined in the child class. | |
414 | virtual Arm_address | |
415 | do_reloc_target(size_t) = 0; | |
416 | ||
417 | // This must be defined in the child class. | |
418 | virtual void | |
419 | do_write(unsigned char*, section_size_type, bool) = 0; | |
420 | ||
421 | private: | |
422 | // Its template. | |
423 | const Stub_template* stub_template_; | |
424 | // Offset within the section of containing this stub. | |
425 | section_offset_type offset_; | |
426 | }; | |
427 | ||
428 | // Reloc stub class. These are stubs we use to fix up relocation because | |
429 | // of limited branch ranges. | |
430 | ||
431 | class Reloc_stub : public Stub | |
432 | { | |
433 | public: | |
434 | static const unsigned int invalid_index = static_cast<unsigned int>(-1); | |
435 | // We assume we never jump to this address. | |
436 | static const Arm_address invalid_address = static_cast<Arm_address>(-1); | |
437 | ||
438 | // Return destination address. | |
439 | Arm_address | |
440 | destination_address() const | |
441 | { | |
442 | gold_assert(this->destination_address_ != this->invalid_address); | |
443 | return this->destination_address_; | |
444 | } | |
445 | ||
446 | // Set destination address. | |
447 | void | |
448 | set_destination_address(Arm_address address) | |
449 | { | |
450 | gold_assert(address != this->invalid_address); | |
451 | this->destination_address_ = address; | |
452 | } | |
453 | ||
454 | // Reset destination address. | |
455 | void | |
456 | reset_destination_address() | |
457 | { this->destination_address_ = this->invalid_address; } | |
458 | ||
459 | // Determine stub type for a branch of a relocation of R_TYPE going | |
460 | // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set, | |
461 | // the branch target is a thumb instruction. TARGET is used for look | |
462 | // up ARM-specific linker settings. | |
463 | static Stub_type | |
464 | stub_type_for_reloc(unsigned int r_type, Arm_address branch_address, | |
465 | Arm_address branch_target, bool target_is_thumb); | |
466 | ||
467 | // Reloc_stub key. A key is logically a triplet of a stub type, a symbol | |
468 | // and an addend. Since we treat global and local symbol differently, we | |
469 | // use a Symbol object for a global symbol and a object-index pair for | |
470 | // a local symbol. | |
471 | class Key | |
472 | { | |
473 | public: | |
474 | // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and | |
475 | // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL | |
476 | // and R_SYM must not be invalid_index. | |
477 | Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj, | |
478 | unsigned int r_sym, int32_t addend) | |
479 | : stub_type_(stub_type), addend_(addend) | |
480 | { | |
481 | if (symbol != NULL) | |
482 | { | |
483 | this->r_sym_ = Reloc_stub::invalid_index; | |
484 | this->u_.symbol = symbol; | |
485 | } | |
486 | else | |
487 | { | |
488 | gold_assert(relobj != NULL && r_sym != invalid_index); | |
489 | this->r_sym_ = r_sym; | |
490 | this->u_.relobj = relobj; | |
491 | } | |
492 | } | |
493 | ||
494 | ~Key() | |
495 | { } | |
496 | ||
497 | // Accessors: Keys are meant to be read-only object so no modifiers are | |
498 | // provided. | |
499 | ||
500 | // Return stub type. | |
501 | Stub_type | |
502 | stub_type() const | |
503 | { return this->stub_type_; } | |
504 | ||
505 | // Return the local symbol index or invalid_index. | |
506 | unsigned int | |
507 | r_sym() const | |
508 | { return this->r_sym_; } | |
509 | ||
510 | // Return the symbol if there is one. | |
511 | const Symbol* | |
512 | symbol() const | |
513 | { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; } | |
514 | ||
515 | // Return the relobj if there is one. | |
516 | const Relobj* | |
517 | relobj() const | |
518 | { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; } | |
519 | ||
520 | // Whether this equals to another key k. | |
521 | bool | |
522 | eq(const Key& k) const | |
523 | { | |
524 | return ((this->stub_type_ == k.stub_type_) | |
525 | && (this->r_sym_ == k.r_sym_) | |
526 | && ((this->r_sym_ != Reloc_stub::invalid_index) | |
527 | ? (this->u_.relobj == k.u_.relobj) | |
528 | : (this->u_.symbol == k.u_.symbol)) | |
529 | && (this->addend_ == k.addend_)); | |
530 | } | |
531 | ||
532 | // Return a hash value. | |
533 | size_t | |
534 | hash_value() const | |
535 | { | |
536 | return (this->stub_type_ | |
537 | ^ this->r_sym_ | |
538 | ^ gold::string_hash<char>( | |
539 | (this->r_sym_ != Reloc_stub::invalid_index) | |
540 | ? this->u_.relobj->name().c_str() | |
541 | : this->u_.symbol->name()) | |
542 | ^ this->addend_); | |
543 | } | |
544 | ||
545 | // Functors for STL associative containers. | |
546 | struct hash | |
547 | { | |
548 | size_t | |
549 | operator()(const Key& k) const | |
550 | { return k.hash_value(); } | |
551 | }; | |
552 | ||
553 | struct equal_to | |
554 | { | |
555 | bool | |
556 | operator()(const Key& k1, const Key& k2) const | |
557 | { return k1.eq(k2); } | |
558 | }; | |
559 | ||
560 | // Name of key. This is mainly for debugging. | |
561 | std::string | |
562 | name() const; | |
563 | ||
564 | private: | |
565 | // Stub type. | |
566 | Stub_type stub_type_; | |
567 | // If this is a local symbol, this is the index in the defining object. | |
568 | // Otherwise, it is invalid_index for a global symbol. | |
569 | unsigned int r_sym_; | |
570 | // If r_sym_ is invalid index. This points to a global symbol. | |
571 | // Otherwise, this points a relobj. We used the unsized and target | |
eb44217c | 572 | // independent Symbol and Relobj classes instead of Sized_symbol<32> and |
b569affa DK |
573 | // Arm_relobj. This is done to avoid making the stub class a template |
574 | // as most of the stub machinery is endianity-neutral. However, it | |
575 | // may require a bit of casting done by users of this class. | |
576 | union | |
577 | { | |
578 | const Symbol* symbol; | |
579 | const Relobj* relobj; | |
580 | } u_; | |
581 | // Addend associated with a reloc. | |
582 | int32_t addend_; | |
583 | }; | |
584 | ||
585 | protected: | |
586 | // Reloc_stubs are created via a stub factory. So these are protected. | |
587 | Reloc_stub(const Stub_template* stub_template) | |
588 | : Stub(stub_template), destination_address_(invalid_address) | |
589 | { } | |
590 | ||
591 | ~Reloc_stub() | |
592 | { } | |
593 | ||
594 | friend class Stub_factory; | |
595 | ||
596 | private: | |
597 | // Return the relocation target address of the i-th relocation in the | |
598 | // stub. | |
599 | Arm_address | |
600 | do_reloc_target(size_t i) | |
601 | { | |
602 | // All reloc stub have only one relocation. | |
603 | gold_assert(i == 0); | |
604 | return this->destination_address_; | |
605 | } | |
606 | ||
607 | // A template to implement do_write below. | |
608 | template<bool big_endian> | |
609 | void inline | |
610 | do_fixed_endian_write(unsigned char*, section_size_type); | |
611 | ||
612 | // Write a stub. | |
613 | void | |
614 | do_write(unsigned char* view, section_size_type view_size, bool big_endian); | |
615 | ||
616 | // Address of destination. | |
617 | Arm_address destination_address_; | |
618 | }; | |
619 | ||
620 | // Stub factory class. | |
621 | ||
622 | class Stub_factory | |
623 | { | |
624 | public: | |
625 | // Return the unique instance of this class. | |
626 | static const Stub_factory& | |
627 | get_instance() | |
628 | { | |
629 | static Stub_factory singleton; | |
630 | return singleton; | |
631 | } | |
632 | ||
633 | // Make a relocation stub. | |
634 | Reloc_stub* | |
635 | make_reloc_stub(Stub_type stub_type) const | |
636 | { | |
637 | gold_assert(stub_type >= arm_stub_reloc_first | |
638 | && stub_type <= arm_stub_reloc_last); | |
639 | return new Reloc_stub(this->stub_templates_[stub_type]); | |
640 | } | |
641 | ||
642 | private: | |
643 | // Constructor and destructor are protected since we only return a single | |
644 | // instance created in Stub_factory::get_instance(). | |
645 | ||
646 | Stub_factory(); | |
647 | ||
648 | // A Stub_factory may not be copied since it is a singleton. | |
649 | Stub_factory(const Stub_factory&); | |
650 | Stub_factory& operator=(Stub_factory&); | |
651 | ||
652 | // Stub templates. These are initialized in the constructor. | |
653 | const Stub_template* stub_templates_[arm_stub_type_last+1]; | |
654 | }; | |
655 | ||
56ee5e00 DK |
656 | // A class to hold stubs for the ARM target. |
657 | ||
658 | template<bool big_endian> | |
659 | class Stub_table : public Output_data | |
660 | { | |
661 | public: | |
662 | Stub_table(Arm_input_section<big_endian>* owner) | |
663 | : Output_data(), addralign_(1), owner_(owner), has_been_changed_(false), | |
664 | reloc_stubs_() | |
665 | { } | |
666 | ||
667 | ~Stub_table() | |
668 | { } | |
669 | ||
670 | // Owner of this stub table. | |
671 | Arm_input_section<big_endian>* | |
672 | owner() const | |
673 | { return this->owner_; } | |
674 | ||
675 | // Whether this stub table is empty. | |
676 | bool | |
677 | empty() const | |
678 | { return this->reloc_stubs_.empty(); } | |
679 | ||
680 | // Whether this has been changed. | |
681 | bool | |
682 | has_been_changed() const | |
683 | { return this->has_been_changed_; } | |
684 | ||
685 | // Set the has-been-changed flag. | |
686 | void | |
687 | set_has_been_changed(bool value) | |
688 | { this->has_been_changed_ = value; } | |
689 | ||
690 | // Return the current data size. | |
691 | off_t | |
692 | current_data_size() const | |
693 | { return this->current_data_size_for_child(); } | |
694 | ||
695 | // Add a STUB with using KEY. Caller is reponsible for avoid adding | |
696 | // if already a STUB with the same key has been added. | |
697 | void | |
698 | add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key); | |
699 | ||
700 | // Look up a relocation stub using KEY. Return NULL if there is none. | |
701 | Reloc_stub* | |
702 | find_reloc_stub(const Reloc_stub::Key& key) const | |
703 | { | |
704 | typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key); | |
705 | return (p != this->reloc_stubs_.end()) ? p->second : NULL; | |
706 | } | |
707 | ||
708 | // Relocate stubs in this stub table. | |
709 | void | |
710 | relocate_stubs(const Relocate_info<32, big_endian>*, | |
711 | Target_arm<big_endian>*, Output_section*, | |
712 | unsigned char*, Arm_address, section_size_type); | |
713 | ||
714 | protected: | |
715 | // Write out section contents. | |
716 | void | |
717 | do_write(Output_file*); | |
718 | ||
719 | // Return the required alignment. | |
720 | uint64_t | |
721 | do_addralign() const | |
722 | { return this->addralign_; } | |
723 | ||
724 | // Finalize data size. | |
725 | void | |
726 | set_final_data_size() | |
727 | { this->set_data_size(this->current_data_size_for_child()); } | |
728 | ||
729 | // Reset address and file offset. | |
730 | void | |
731 | do_reset_address_and_file_offset(); | |
732 | ||
733 | private: | |
734 | // Unordered map of stubs. | |
735 | typedef | |
736 | Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash, | |
737 | Reloc_stub::Key::equal_to> | |
738 | Reloc_stub_map; | |
739 | ||
740 | // Address alignment | |
741 | uint64_t addralign_; | |
742 | // Owner of this stub table. | |
743 | Arm_input_section<big_endian>* owner_; | |
744 | // This is set to true during relaxiong if the size of the stub table | |
745 | // has been changed. | |
746 | bool has_been_changed_; | |
747 | // The relocation stubs. | |
748 | Reloc_stub_map reloc_stubs_; | |
749 | }; | |
750 | ||
10ad9fe5 DK |
751 | // A class to wrap an ordinary input section containing executable code. |
752 | ||
753 | template<bool big_endian> | |
754 | class Arm_input_section : public Output_relaxed_input_section | |
755 | { | |
756 | public: | |
757 | Arm_input_section(Relobj* relobj, unsigned int shndx) | |
758 | : Output_relaxed_input_section(relobj, shndx, 1), | |
759 | original_addralign_(1), original_size_(0), stub_table_(NULL) | |
760 | { } | |
761 | ||
762 | ~Arm_input_section() | |
763 | { } | |
764 | ||
765 | // Initialize. | |
766 | void | |
767 | init(); | |
768 | ||
769 | // Whether this is a stub table owner. | |
770 | bool | |
771 | is_stub_table_owner() const | |
772 | { return this->stub_table_ != NULL && this->stub_table_->owner() == this; } | |
773 | ||
774 | // Return the stub table. | |
775 | Stub_table<big_endian>* | |
776 | stub_table() const | |
777 | { return this->stub_table_; } | |
778 | ||
779 | // Set the stub_table. | |
780 | void | |
781 | set_stub_table(Stub_table<big_endian>* stub_table) | |
782 | { this->stub_table_ = stub_table; } | |
783 | ||
07f508a2 DK |
784 | // Downcast a base pointer to an Arm_input_section pointer. This is |
785 | // not type-safe but we only use Arm_input_section not the base class. | |
786 | static Arm_input_section<big_endian>* | |
787 | as_arm_input_section(Output_relaxed_input_section* poris) | |
788 | { return static_cast<Arm_input_section<big_endian>*>(poris); } | |
789 | ||
10ad9fe5 DK |
790 | protected: |
791 | // Write data to output file. | |
792 | void | |
793 | do_write(Output_file*); | |
794 | ||
795 | // Return required alignment of this. | |
796 | uint64_t | |
797 | do_addralign() const | |
798 | { | |
799 | if (this->is_stub_table_owner()) | |
800 | return std::max(this->stub_table_->addralign(), | |
801 | this->original_addralign_); | |
802 | else | |
803 | return this->original_addralign_; | |
804 | } | |
805 | ||
806 | // Finalize data size. | |
807 | void | |
808 | set_final_data_size(); | |
809 | ||
810 | // Reset address and file offset. | |
811 | void | |
812 | do_reset_address_and_file_offset(); | |
813 | ||
814 | // Output offset. | |
815 | bool | |
816 | do_output_offset(const Relobj* object, unsigned int shndx, | |
817 | section_offset_type offset, | |
818 | section_offset_type* poutput) const | |
819 | { | |
820 | if ((object == this->relobj()) | |
821 | && (shndx == this->shndx()) | |
822 | && (offset >= 0) | |
823 | && (convert_types<uint64_t, section_offset_type>(offset) | |
824 | <= this->original_size_)) | |
825 | { | |
826 | *poutput = offset; | |
827 | return true; | |
828 | } | |
829 | else | |
830 | return false; | |
831 | } | |
832 | ||
833 | private: | |
834 | // Copying is not allowed. | |
835 | Arm_input_section(const Arm_input_section&); | |
836 | Arm_input_section& operator=(const Arm_input_section&); | |
837 | ||
838 | // Address alignment of the original input section. | |
839 | uint64_t original_addralign_; | |
840 | // Section size of the original input section. | |
841 | uint64_t original_size_; | |
842 | // Stub table. | |
843 | Stub_table<big_endian>* stub_table_; | |
844 | }; | |
845 | ||
07f508a2 DK |
846 | // Arm output section class. This is defined mainly to add a number of |
847 | // stub generation methods. | |
848 | ||
849 | template<bool big_endian> | |
850 | class Arm_output_section : public Output_section | |
851 | { | |
852 | public: | |
853 | Arm_output_section(const char* name, elfcpp::Elf_Word type, | |
854 | elfcpp::Elf_Xword flags) | |
855 | : Output_section(name, type, flags) | |
856 | { } | |
857 | ||
858 | ~Arm_output_section() | |
859 | { } | |
860 | ||
861 | // Group input sections for stub generation. | |
862 | void | |
863 | group_sections(section_size_type, bool, Target_arm<big_endian>*); | |
864 | ||
865 | // Downcast a base pointer to an Arm_output_section pointer. This is | |
866 | // not type-safe but we only use Arm_output_section not the base class. | |
867 | static Arm_output_section<big_endian>* | |
868 | as_arm_output_section(Output_section* os) | |
869 | { return static_cast<Arm_output_section<big_endian>*>(os); } | |
870 | ||
871 | private: | |
872 | // For convenience. | |
873 | typedef Output_section::Input_section Input_section; | |
874 | typedef Output_section::Input_section_list Input_section_list; | |
875 | ||
876 | // Create a stub group. | |
877 | void create_stub_group(Input_section_list::const_iterator, | |
878 | Input_section_list::const_iterator, | |
879 | Input_section_list::const_iterator, | |
880 | Target_arm<big_endian>*, | |
881 | std::vector<Output_relaxed_input_section*>*); | |
882 | }; | |
883 | ||
8ffa3667 DK |
884 | // Arm_relobj class. |
885 | ||
886 | template<bool big_endian> | |
887 | class Arm_relobj : public Sized_relobj<32, big_endian> | |
888 | { | |
889 | public: | |
890 | static const Arm_address invalid_address = static_cast<Arm_address>(-1); | |
891 | ||
892 | Arm_relobj(const std::string& name, Input_file* input_file, off_t offset, | |
893 | const typename elfcpp::Ehdr<32, big_endian>& ehdr) | |
894 | : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr), | |
895 | stub_tables_(), local_symbol_is_thumb_function_() | |
896 | { } | |
897 | ||
898 | ~Arm_relobj() | |
899 | { } | |
900 | ||
901 | // Return the stub table of the SHNDX-th section if there is one. | |
902 | Stub_table<big_endian>* | |
903 | stub_table(unsigned int shndx) const | |
904 | { | |
905 | gold_assert(shndx < this->stub_tables_.size()); | |
906 | return this->stub_tables_[shndx]; | |
907 | } | |
908 | ||
909 | // Set STUB_TABLE to be the stub_table of the SHNDX-th section. | |
910 | void | |
911 | set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table) | |
912 | { | |
913 | gold_assert(shndx < this->stub_tables_.size()); | |
914 | this->stub_tables_[shndx] = stub_table; | |
915 | } | |
916 | ||
917 | // Whether a local symbol is a THUMB function. R_SYM is the symbol table | |
918 | // index. This is only valid after do_count_local_symbol is called. | |
919 | bool | |
920 | local_symbol_is_thumb_function(unsigned int r_sym) const | |
921 | { | |
922 | gold_assert(r_sym < this->local_symbol_is_thumb_function_.size()); | |
923 | return this->local_symbol_is_thumb_function_[r_sym]; | |
924 | } | |
925 | ||
926 | // Scan all relocation sections for stub generation. | |
927 | void | |
928 | scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*, | |
929 | const Layout*); | |
930 | ||
931 | // Convert regular input section with index SHNDX to a relaxed section. | |
932 | void | |
933 | convert_input_section_to_relaxed_section(unsigned shndx) | |
934 | { | |
935 | // The stubs have relocations and we need to process them after writing | |
936 | // out the stubs. So relocation now must follow section write. | |
937 | this->invalidate_section_offset(shndx); | |
938 | this->set_relocs_must_follow_section_writes(); | |
939 | } | |
940 | ||
941 | // Downcast a base pointer to an Arm_relobj pointer. This is | |
942 | // not type-safe but we only use Arm_relobj not the base class. | |
943 | static Arm_relobj<big_endian>* | |
944 | as_arm_relobj(Relobj* relobj) | |
945 | { return static_cast<Arm_relobj<big_endian>*>(relobj); } | |
946 | ||
d5b40221 DK |
947 | // Processor-specific flags in ELF file header. This is valid only after |
948 | // reading symbols. | |
949 | elfcpp::Elf_Word | |
950 | processor_specific_flags() const | |
951 | { return this->processor_specific_flags_; } | |
952 | ||
8ffa3667 DK |
953 | protected: |
954 | // Post constructor setup. | |
955 | void | |
956 | do_setup() | |
957 | { | |
958 | // Call parent's setup method. | |
959 | Sized_relobj<32, big_endian>::do_setup(); | |
960 | ||
961 | // Initialize look-up tables. | |
962 | Stub_table_list empty_stub_table_list(this->shnum(), NULL); | |
963 | this->stub_tables_.swap(empty_stub_table_list); | |
964 | } | |
965 | ||
966 | // Count the local symbols. | |
967 | void | |
968 | do_count_local_symbols(Stringpool_template<char>*, | |
969 | Stringpool_template<char>*); | |
970 | ||
971 | void | |
43d12afe | 972 | do_relocate_sections(const Symbol_table* symtab, const Layout* layout, |
8ffa3667 DK |
973 | const unsigned char* pshdrs, |
974 | typename Sized_relobj<32, big_endian>::Views* pivews); | |
975 | ||
d5b40221 DK |
976 | // Read the symbol information. |
977 | void | |
978 | do_read_symbols(Read_symbols_data* sd); | |
979 | ||
8ffa3667 DK |
980 | private: |
981 | // List of stub tables. | |
982 | typedef std::vector<Stub_table<big_endian>*> Stub_table_list; | |
983 | Stub_table_list stub_tables_; | |
984 | // Bit vector to tell if a local symbol is a thumb function or not. | |
985 | // This is only valid after do_count_local_symbol is called. | |
986 | std::vector<bool> local_symbol_is_thumb_function_; | |
d5b40221 DK |
987 | // processor-specific flags in ELF file header. |
988 | elfcpp::Elf_Word processor_specific_flags_; | |
989 | }; | |
990 | ||
991 | // Arm_dynobj class. | |
992 | ||
993 | template<bool big_endian> | |
994 | class Arm_dynobj : public Sized_dynobj<32, big_endian> | |
995 | { | |
996 | public: | |
997 | Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset, | |
998 | const elfcpp::Ehdr<32, big_endian>& ehdr) | |
999 | : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr), | |
1000 | processor_specific_flags_(0) | |
1001 | { } | |
1002 | ||
1003 | ~Arm_dynobj() | |
1004 | { } | |
1005 | ||
1006 | // Downcast a base pointer to an Arm_relobj pointer. This is | |
1007 | // not type-safe but we only use Arm_relobj not the base class. | |
1008 | static Arm_dynobj<big_endian>* | |
1009 | as_arm_dynobj(Dynobj* dynobj) | |
1010 | { return static_cast<Arm_dynobj<big_endian>*>(dynobj); } | |
1011 | ||
1012 | // Processor-specific flags in ELF file header. This is valid only after | |
1013 | // reading symbols. | |
1014 | elfcpp::Elf_Word | |
1015 | processor_specific_flags() const | |
1016 | { return this->processor_specific_flags_; } | |
1017 | ||
1018 | protected: | |
1019 | // Read the symbol information. | |
1020 | void | |
1021 | do_read_symbols(Read_symbols_data* sd); | |
1022 | ||
1023 | private: | |
1024 | // processor-specific flags in ELF file header. | |
1025 | elfcpp::Elf_Word processor_specific_flags_; | |
8ffa3667 DK |
1026 | }; |
1027 | ||
e9bbb538 DK |
1028 | // Functor to read reloc addends during stub generation. |
1029 | ||
1030 | template<int sh_type, bool big_endian> | |
1031 | struct Stub_addend_reader | |
1032 | { | |
1033 | // Return the addend for a relocation of a particular type. Depending | |
1034 | // on whether this is a REL or RELA relocation, read the addend from a | |
1035 | // view or from a Reloc object. | |
1036 | elfcpp::Elf_types<32>::Elf_Swxword | |
1037 | operator()( | |
1038 | unsigned int /* r_type */, | |
1039 | const unsigned char* /* view */, | |
1040 | const typename Reloc_types<sh_type, | |
ebd95253 | 1041 | 32, big_endian>::Reloc& /* reloc */) const; |
e9bbb538 DK |
1042 | }; |
1043 | ||
1044 | // Specialized Stub_addend_reader for SHT_REL type relocation sections. | |
1045 | ||
1046 | template<bool big_endian> | |
1047 | struct Stub_addend_reader<elfcpp::SHT_REL, big_endian> | |
1048 | { | |
1049 | elfcpp::Elf_types<32>::Elf_Swxword | |
1050 | operator()( | |
1051 | unsigned int, | |
1052 | const unsigned char*, | |
1053 | const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const; | |
1054 | }; | |
1055 | ||
1056 | // Specialized Stub_addend_reader for RELA type relocation sections. | |
1057 | // We currently do not handle RELA type relocation sections but it is trivial | |
1058 | // to implement the addend reader. This is provided for completeness and to | |
1059 | // make it easier to add support for RELA relocation sections in the future. | |
1060 | ||
1061 | template<bool big_endian> | |
1062 | struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian> | |
1063 | { | |
1064 | elfcpp::Elf_types<32>::Elf_Swxword | |
1065 | operator()( | |
1066 | unsigned int, | |
1067 | const unsigned char*, | |
1068 | const typename Reloc_types<elfcpp::SHT_RELA, 32, | |
ebd95253 DK |
1069 | big_endian>::Reloc& reloc) const |
1070 | { return reloc.get_r_addend(); } | |
e9bbb538 DK |
1071 | }; |
1072 | ||
c121c671 DK |
1073 | // Utilities for manipulating integers of up to 32-bits |
1074 | ||
1075 | namespace utils | |
1076 | { | |
1077 | // Sign extend an n-bit unsigned integer stored in an uint32_t into | |
1078 | // an int32_t. NO_BITS must be between 1 to 32. | |
1079 | template<int no_bits> | |
1080 | static inline int32_t | |
1081 | sign_extend(uint32_t bits) | |
1082 | { | |
96d49306 | 1083 | gold_assert(no_bits >= 0 && no_bits <= 32); |
c121c671 DK |
1084 | if (no_bits == 32) |
1085 | return static_cast<int32_t>(bits); | |
1086 | uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits); | |
1087 | bits &= mask; | |
1088 | uint32_t top_bit = 1U << (no_bits - 1); | |
1089 | int32_t as_signed = static_cast<int32_t>(bits); | |
1090 | return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed; | |
1091 | } | |
1092 | ||
1093 | // Detects overflow of an NO_BITS integer stored in a uint32_t. | |
1094 | template<int no_bits> | |
1095 | static inline bool | |
1096 | has_overflow(uint32_t bits) | |
1097 | { | |
96d49306 | 1098 | gold_assert(no_bits >= 0 && no_bits <= 32); |
c121c671 DK |
1099 | if (no_bits == 32) |
1100 | return false; | |
1101 | int32_t max = (1 << (no_bits - 1)) - 1; | |
1102 | int32_t min = -(1 << (no_bits - 1)); | |
1103 | int32_t as_signed = static_cast<int32_t>(bits); | |
1104 | return as_signed > max || as_signed < min; | |
1105 | } | |
1106 | ||
5e445df6 ILT |
1107 | // Detects overflow of an NO_BITS integer stored in a uint32_t when it |
1108 | // fits in the given number of bits as either a signed or unsigned value. | |
1109 | // For example, has_signed_unsigned_overflow<8> would check | |
1110 | // -128 <= bits <= 255 | |
1111 | template<int no_bits> | |
1112 | static inline bool | |
1113 | has_signed_unsigned_overflow(uint32_t bits) | |
1114 | { | |
1115 | gold_assert(no_bits >= 2 && no_bits <= 32); | |
1116 | if (no_bits == 32) | |
1117 | return false; | |
1118 | int32_t max = static_cast<int32_t>((1U << no_bits) - 1); | |
1119 | int32_t min = -(1 << (no_bits - 1)); | |
1120 | int32_t as_signed = static_cast<int32_t>(bits); | |
1121 | return as_signed > max || as_signed < min; | |
1122 | } | |
1123 | ||
c121c671 DK |
1124 | // Select bits from A and B using bits in MASK. For each n in [0..31], |
1125 | // the n-th bit in the result is chosen from the n-th bits of A and B. | |
1126 | // A zero selects A and a one selects B. | |
1127 | static inline uint32_t | |
1128 | bit_select(uint32_t a, uint32_t b, uint32_t mask) | |
1129 | { return (a & ~mask) | (b & mask); } | |
1130 | }; | |
1131 | ||
4a657b0d DK |
1132 | template<bool big_endian> |
1133 | class Target_arm : public Sized_target<32, big_endian> | |
1134 | { | |
1135 | public: | |
1136 | typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian> | |
1137 | Reloc_section; | |
1138 | ||
2daedcd6 DK |
1139 | // When were are relocating a stub, we pass this as the relocation number. |
1140 | static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1); | |
1141 | ||
a6d1ef57 DK |
1142 | Target_arm() |
1143 | : Sized_target<32, big_endian>(&arm_info), | |
1144 | got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL), | |
1145 | copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(), | |
1146 | stub_factory_(Stub_factory::get_instance()), | |
1147 | may_use_blx_(true), should_force_pic_veneer_(false), | |
1148 | arm_input_section_map_() | |
1149 | { } | |
4a657b0d | 1150 | |
b569affa DK |
1151 | // Whether we can use BLX. |
1152 | bool | |
1153 | may_use_blx() const | |
1154 | { return this->may_use_blx_; } | |
1155 | ||
1156 | // Set use-BLX flag. | |
1157 | void | |
1158 | set_may_use_blx(bool value) | |
1159 | { this->may_use_blx_ = value; } | |
1160 | ||
1161 | // Whether we force PCI branch veneers. | |
1162 | bool | |
1163 | should_force_pic_veneer() const | |
1164 | { return this->should_force_pic_veneer_; } | |
1165 | ||
1166 | // Set PIC veneer flag. | |
1167 | void | |
1168 | set_should_force_pic_veneer(bool value) | |
1169 | { this->should_force_pic_veneer_ = value; } | |
1170 | ||
1171 | // Whether we use THUMB-2 instructions. | |
1172 | bool | |
1173 | using_thumb2() const | |
1174 | { | |
1175 | // FIXME: This should not hard-coded. | |
1176 | return false; | |
1177 | } | |
1178 | ||
1179 | // Whether we use THUMB/THUMB-2 instructions only. | |
1180 | bool | |
1181 | using_thumb_only() const | |
1182 | { | |
1183 | // FIXME: This should not hard-coded. | |
1184 | return false; | |
1185 | } | |
1186 | ||
d204b6e9 DK |
1187 | // Whether we have an NOP instruction. If not, use mov r0, r0 instead. |
1188 | bool | |
1189 | may_use_arm_nop() const | |
1190 | { | |
1191 | // FIXME: This should not hard-coded. | |
1192 | return false; | |
1193 | } | |
1194 | ||
51938283 DK |
1195 | // Whether we have THUMB-2 NOP.W instruction. |
1196 | bool | |
1197 | may_use_thumb2_nop() const | |
1198 | { | |
1199 | // FIXME: This should not hard-coded. | |
1200 | return false; | |
1201 | } | |
1202 | ||
4a657b0d DK |
1203 | // Process the relocations to determine unreferenced sections for |
1204 | // garbage collection. | |
1205 | void | |
ad0f2072 | 1206 | gc_process_relocs(Symbol_table* symtab, |
4a657b0d DK |
1207 | Layout* layout, |
1208 | Sized_relobj<32, big_endian>* object, | |
1209 | unsigned int data_shndx, | |
1210 | unsigned int sh_type, | |
1211 | const unsigned char* prelocs, | |
1212 | size_t reloc_count, | |
1213 | Output_section* output_section, | |
1214 | bool needs_special_offset_handling, | |
1215 | size_t local_symbol_count, | |
1216 | const unsigned char* plocal_symbols); | |
1217 | ||
1218 | // Scan the relocations to look for symbol adjustments. | |
1219 | void | |
ad0f2072 | 1220 | scan_relocs(Symbol_table* symtab, |
4a657b0d DK |
1221 | Layout* layout, |
1222 | Sized_relobj<32, big_endian>* object, | |
1223 | unsigned int data_shndx, | |
1224 | unsigned int sh_type, | |
1225 | const unsigned char* prelocs, | |
1226 | size_t reloc_count, | |
1227 | Output_section* output_section, | |
1228 | bool needs_special_offset_handling, | |
1229 | size_t local_symbol_count, | |
1230 | const unsigned char* plocal_symbols); | |
1231 | ||
1232 | // Finalize the sections. | |
1233 | void | |
f59f41f3 | 1234 | do_finalize_sections(Layout*, const Input_objects*, Symbol_table*); |
4a657b0d | 1235 | |
94cdfcff | 1236 | // Return the value to use for a dynamic symbol which requires special |
4a657b0d DK |
1237 | // treatment. |
1238 | uint64_t | |
1239 | do_dynsym_value(const Symbol*) const; | |
1240 | ||
1241 | // Relocate a section. | |
1242 | void | |
1243 | relocate_section(const Relocate_info<32, big_endian>*, | |
1244 | unsigned int sh_type, | |
1245 | const unsigned char* prelocs, | |
1246 | size_t reloc_count, | |
1247 | Output_section* output_section, | |
1248 | bool needs_special_offset_handling, | |
1249 | unsigned char* view, | |
ebabffbd | 1250 | Arm_address view_address, |
364c7fa5 ILT |
1251 | section_size_type view_size, |
1252 | const Reloc_symbol_changes*); | |
4a657b0d DK |
1253 | |
1254 | // Scan the relocs during a relocatable link. | |
1255 | void | |
ad0f2072 | 1256 | scan_relocatable_relocs(Symbol_table* symtab, |
4a657b0d DK |
1257 | Layout* layout, |
1258 | Sized_relobj<32, big_endian>* object, | |
1259 | unsigned int data_shndx, | |
1260 | unsigned int sh_type, | |
1261 | const unsigned char* prelocs, | |
1262 | size_t reloc_count, | |
1263 | Output_section* output_section, | |
1264 | bool needs_special_offset_handling, | |
1265 | size_t local_symbol_count, | |
1266 | const unsigned char* plocal_symbols, | |
1267 | Relocatable_relocs*); | |
1268 | ||
1269 | // Relocate a section during a relocatable link. | |
1270 | void | |
1271 | relocate_for_relocatable(const Relocate_info<32, big_endian>*, | |
1272 | unsigned int sh_type, | |
1273 | const unsigned char* prelocs, | |
1274 | size_t reloc_count, | |
1275 | Output_section* output_section, | |
1276 | off_t offset_in_output_section, | |
1277 | const Relocatable_relocs*, | |
1278 | unsigned char* view, | |
ebabffbd | 1279 | Arm_address view_address, |
4a657b0d DK |
1280 | section_size_type view_size, |
1281 | unsigned char* reloc_view, | |
1282 | section_size_type reloc_view_size); | |
1283 | ||
1284 | // Return whether SYM is defined by the ABI. | |
1285 | bool | |
1286 | do_is_defined_by_abi(Symbol* sym) const | |
1287 | { return strcmp(sym->name(), "__tls_get_addr") == 0; } | |
1288 | ||
94cdfcff DK |
1289 | // Return the size of the GOT section. |
1290 | section_size_type | |
1291 | got_size() | |
1292 | { | |
1293 | gold_assert(this->got_ != NULL); | |
1294 | return this->got_->data_size(); | |
1295 | } | |
1296 | ||
4a657b0d | 1297 | // Map platform-specific reloc types |
a6d1ef57 DK |
1298 | static unsigned int |
1299 | get_real_reloc_type (unsigned int r_type); | |
4a657b0d | 1300 | |
55da9579 DK |
1301 | // |
1302 | // Methods to support stub-generations. | |
1303 | // | |
1304 | ||
1305 | // Return the stub factory | |
1306 | const Stub_factory& | |
1307 | stub_factory() const | |
1308 | { return this->stub_factory_; } | |
1309 | ||
1310 | // Make a new Arm_input_section object. | |
1311 | Arm_input_section<big_endian>* | |
1312 | new_arm_input_section(Relobj*, unsigned int); | |
1313 | ||
1314 | // Find the Arm_input_section object corresponding to the SHNDX-th input | |
1315 | // section of RELOBJ. | |
1316 | Arm_input_section<big_endian>* | |
1317 | find_arm_input_section(Relobj* relobj, unsigned int shndx) const; | |
1318 | ||
1319 | // Make a new Stub_table | |
1320 | Stub_table<big_endian>* | |
1321 | new_stub_table(Arm_input_section<big_endian>*); | |
1322 | ||
eb44217c DK |
1323 | // Scan a section for stub generation. |
1324 | void | |
1325 | scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int, | |
1326 | const unsigned char*, size_t, Output_section*, | |
1327 | bool, const unsigned char*, Arm_address, | |
1328 | section_size_type); | |
1329 | ||
43d12afe DK |
1330 | // Relocate a stub. |
1331 | void | |
1332 | relocate_stub(Reloc_stub*, const Relocate_info<32, big_endian>*, | |
1333 | Output_section*, unsigned char*, Arm_address, | |
1334 | section_size_type); | |
1335 | ||
b569affa | 1336 | // Get the default ARM target. |
43d12afe | 1337 | static Target_arm<big_endian>* |
b569affa DK |
1338 | default_target() |
1339 | { | |
1340 | gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM | |
1341 | && parameters->target().is_big_endian() == big_endian); | |
43d12afe DK |
1342 | return static_cast<Target_arm<big_endian>*>( |
1343 | parameters->sized_target<32, big_endian>()); | |
b569affa DK |
1344 | } |
1345 | ||
55da9579 DK |
1346 | // Whether relocation type uses LSB to distinguish THUMB addresses. |
1347 | static bool | |
1348 | reloc_uses_thumb_bit(unsigned int r_type); | |
1349 | ||
d5b40221 | 1350 | protected: |
eb44217c DK |
1351 | // Make an ELF object. |
1352 | Object* | |
1353 | do_make_elf_object(const std::string&, Input_file*, off_t, | |
1354 | const elfcpp::Ehdr<32, big_endian>& ehdr); | |
1355 | ||
1356 | Object* | |
1357 | do_make_elf_object(const std::string&, Input_file*, off_t, | |
1358 | const elfcpp::Ehdr<32, !big_endian>&) | |
1359 | { gold_unreachable(); } | |
1360 | ||
1361 | Object* | |
1362 | do_make_elf_object(const std::string&, Input_file*, off_t, | |
1363 | const elfcpp::Ehdr<64, false>&) | |
1364 | { gold_unreachable(); } | |
1365 | ||
1366 | Object* | |
1367 | do_make_elf_object(const std::string&, Input_file*, off_t, | |
1368 | const elfcpp::Ehdr<64, true>&) | |
1369 | { gold_unreachable(); } | |
1370 | ||
1371 | // Make an output section. | |
1372 | Output_section* | |
1373 | do_make_output_section(const char* name, elfcpp::Elf_Word type, | |
1374 | elfcpp::Elf_Xword flags) | |
1375 | { return new Arm_output_section<big_endian>(name, type, flags); } | |
1376 | ||
d5b40221 DK |
1377 | void |
1378 | do_adjust_elf_header(unsigned char* view, int len) const; | |
1379 | ||
eb44217c DK |
1380 | // We only need to generate stubs, and hence perform relaxation if we are |
1381 | // not doing relocatable linking. | |
1382 | bool | |
1383 | do_may_relax() const | |
1384 | { return !parameters->options().relocatable(); } | |
1385 | ||
1386 | bool | |
1387 | do_relax(int, const Input_objects*, Symbol_table*, Layout*); | |
1388 | ||
4a657b0d DK |
1389 | private: |
1390 | // The class which scans relocations. | |
1391 | class Scan | |
1392 | { | |
1393 | public: | |
1394 | Scan() | |
bec53400 | 1395 | : issued_non_pic_error_(false) |
4a657b0d DK |
1396 | { } |
1397 | ||
1398 | inline void | |
ad0f2072 | 1399 | local(Symbol_table* symtab, Layout* layout, Target_arm* target, |
4a657b0d DK |
1400 | Sized_relobj<32, big_endian>* object, |
1401 | unsigned int data_shndx, | |
1402 | Output_section* output_section, | |
1403 | const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type, | |
1404 | const elfcpp::Sym<32, big_endian>& lsym); | |
1405 | ||
1406 | inline void | |
ad0f2072 | 1407 | global(Symbol_table* symtab, Layout* layout, Target_arm* target, |
4a657b0d DK |
1408 | Sized_relobj<32, big_endian>* object, |
1409 | unsigned int data_shndx, | |
1410 | Output_section* output_section, | |
1411 | const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type, | |
1412 | Symbol* gsym); | |
1413 | ||
1414 | private: | |
1415 | static void | |
1416 | unsupported_reloc_local(Sized_relobj<32, big_endian>*, | |
1417 | unsigned int r_type); | |
1418 | ||
1419 | static void | |
1420 | unsupported_reloc_global(Sized_relobj<32, big_endian>*, | |
1421 | unsigned int r_type, Symbol*); | |
bec53400 DK |
1422 | |
1423 | void | |
1424 | check_non_pic(Relobj*, unsigned int r_type); | |
1425 | ||
1426 | // Almost identical to Symbol::needs_plt_entry except that it also | |
1427 | // handles STT_ARM_TFUNC. | |
1428 | static bool | |
1429 | symbol_needs_plt_entry(const Symbol* sym) | |
1430 | { | |
1431 | // An undefined symbol from an executable does not need a PLT entry. | |
1432 | if (sym->is_undefined() && !parameters->options().shared()) | |
1433 | return false; | |
1434 | ||
1435 | return (!parameters->doing_static_link() | |
1436 | && (sym->type() == elfcpp::STT_FUNC | |
1437 | || sym->type() == elfcpp::STT_ARM_TFUNC) | |
1438 | && (sym->is_from_dynobj() | |
1439 | || sym->is_undefined() | |
1440 | || sym->is_preemptible())); | |
1441 | } | |
1442 | ||
1443 | // Whether we have issued an error about a non-PIC compilation. | |
1444 | bool issued_non_pic_error_; | |
4a657b0d DK |
1445 | }; |
1446 | ||
1447 | // The class which implements relocation. | |
1448 | class Relocate | |
1449 | { | |
1450 | public: | |
1451 | Relocate() | |
1452 | { } | |
1453 | ||
1454 | ~Relocate() | |
1455 | { } | |
1456 | ||
bec53400 DK |
1457 | // Return whether the static relocation needs to be applied. |
1458 | inline bool | |
1459 | should_apply_static_reloc(const Sized_symbol<32>* gsym, | |
1460 | int ref_flags, | |
1461 | bool is_32bit, | |
1462 | Output_section* output_section); | |
1463 | ||
4a657b0d DK |
1464 | // Do a relocation. Return false if the caller should not issue |
1465 | // any warnings about this relocation. | |
1466 | inline bool | |
1467 | relocate(const Relocate_info<32, big_endian>*, Target_arm*, | |
1468 | Output_section*, size_t relnum, | |
1469 | const elfcpp::Rel<32, big_endian>&, | |
1470 | unsigned int r_type, const Sized_symbol<32>*, | |
1471 | const Symbol_value<32>*, | |
ebabffbd | 1472 | unsigned char*, Arm_address, |
4a657b0d | 1473 | section_size_type); |
c121c671 DK |
1474 | |
1475 | // Return whether we want to pass flag NON_PIC_REF for this | |
f4e5969c DK |
1476 | // reloc. This means the relocation type accesses a symbol not via |
1477 | // GOT or PLT. | |
c121c671 DK |
1478 | static inline bool |
1479 | reloc_is_non_pic (unsigned int r_type) | |
1480 | { | |
1481 | switch (r_type) | |
1482 | { | |
f4e5969c DK |
1483 | // These relocation types reference GOT or PLT entries explicitly. |
1484 | case elfcpp::R_ARM_GOT_BREL: | |
1485 | case elfcpp::R_ARM_GOT_ABS: | |
1486 | case elfcpp::R_ARM_GOT_PREL: | |
1487 | case elfcpp::R_ARM_GOT_BREL12: | |
1488 | case elfcpp::R_ARM_PLT32_ABS: | |
1489 | case elfcpp::R_ARM_TLS_GD32: | |
1490 | case elfcpp::R_ARM_TLS_LDM32: | |
1491 | case elfcpp::R_ARM_TLS_IE32: | |
1492 | case elfcpp::R_ARM_TLS_IE12GP: | |
1493 | ||
1494 | // These relocate types may use PLT entries. | |
c121c671 | 1495 | case elfcpp::R_ARM_CALL: |
f4e5969c | 1496 | case elfcpp::R_ARM_THM_CALL: |
c121c671 | 1497 | case elfcpp::R_ARM_JUMP24: |
f4e5969c DK |
1498 | case elfcpp::R_ARM_THM_JUMP24: |
1499 | case elfcpp::R_ARM_THM_JUMP19: | |
1500 | case elfcpp::R_ARM_PLT32: | |
1501 | case elfcpp::R_ARM_THM_XPC22: | |
c121c671 | 1502 | return false; |
f4e5969c DK |
1503 | |
1504 | default: | |
1505 | return true; | |
c121c671 DK |
1506 | } |
1507 | } | |
4a657b0d DK |
1508 | }; |
1509 | ||
1510 | // A class which returns the size required for a relocation type, | |
1511 | // used while scanning relocs during a relocatable link. | |
1512 | class Relocatable_size_for_reloc | |
1513 | { | |
1514 | public: | |
1515 | unsigned int | |
1516 | get_size_for_reloc(unsigned int, Relobj*); | |
1517 | }; | |
1518 | ||
94cdfcff DK |
1519 | // Get the GOT section, creating it if necessary. |
1520 | Output_data_got<32, big_endian>* | |
1521 | got_section(Symbol_table*, Layout*); | |
1522 | ||
1523 | // Get the GOT PLT section. | |
1524 | Output_data_space* | |
1525 | got_plt_section() const | |
1526 | { | |
1527 | gold_assert(this->got_plt_ != NULL); | |
1528 | return this->got_plt_; | |
1529 | } | |
1530 | ||
1531 | // Create a PLT entry for a global symbol. | |
1532 | void | |
1533 | make_plt_entry(Symbol_table*, Layout*, Symbol*); | |
1534 | ||
1535 | // Get the PLT section. | |
1536 | const Output_data_plt_arm<big_endian>* | |
1537 | plt_section() const | |
1538 | { | |
1539 | gold_assert(this->plt_ != NULL); | |
1540 | return this->plt_; | |
1541 | } | |
1542 | ||
1543 | // Get the dynamic reloc section, creating it if necessary. | |
1544 | Reloc_section* | |
1545 | rel_dyn_section(Layout*); | |
1546 | ||
1547 | // Return true if the symbol may need a COPY relocation. | |
1548 | // References from an executable object to non-function symbols | |
1549 | // defined in a dynamic object may need a COPY relocation. | |
1550 | bool | |
1551 | may_need_copy_reloc(Symbol* gsym) | |
1552 | { | |
966d4097 DK |
1553 | return (gsym->type() != elfcpp::STT_ARM_TFUNC |
1554 | && gsym->may_need_copy_reloc()); | |
94cdfcff DK |
1555 | } |
1556 | ||
1557 | // Add a potential copy relocation. | |
1558 | void | |
1559 | copy_reloc(Symbol_table* symtab, Layout* layout, | |
1560 | Sized_relobj<32, big_endian>* object, | |
1561 | unsigned int shndx, Output_section* output_section, | |
1562 | Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc) | |
1563 | { | |
1564 | this->copy_relocs_.copy_reloc(symtab, layout, | |
1565 | symtab->get_sized_symbol<32>(sym), | |
1566 | object, shndx, output_section, reloc, | |
1567 | this->rel_dyn_section(layout)); | |
1568 | } | |
1569 | ||
d5b40221 DK |
1570 | // Whether two EABI versions are compatible. |
1571 | static bool | |
1572 | are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2); | |
1573 | ||
1574 | // Merge processor-specific flags from input object and those in the ELF | |
1575 | // header of the output. | |
1576 | void | |
1577 | merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word); | |
1578 | ||
eb44217c DK |
1579 | // |
1580 | // Methods to support stub-generations. | |
1581 | // | |
d5b40221 | 1582 | |
eb44217c DK |
1583 | // Group input sections for stub generation. |
1584 | void | |
1585 | group_sections(Layout*, section_size_type, bool); | |
d5b40221 | 1586 | |
eb44217c DK |
1587 | // Scan a relocation for stub generation. |
1588 | void | |
1589 | scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int, | |
1590 | const Sized_symbol<32>*, unsigned int, | |
1591 | const Symbol_value<32>*, | |
1592 | elfcpp::Elf_types<32>::Elf_Swxword, Arm_address); | |
d5b40221 | 1593 | |
eb44217c DK |
1594 | // Scan a relocation section for stub. |
1595 | template<int sh_type> | |
1596 | void | |
1597 | scan_reloc_section_for_stubs( | |
1598 | const Relocate_info<32, big_endian>* relinfo, | |
1599 | const unsigned char* prelocs, | |
1600 | size_t reloc_count, | |
1601 | Output_section* output_section, | |
1602 | bool needs_special_offset_handling, | |
1603 | const unsigned char* view, | |
1604 | elfcpp::Elf_types<32>::Elf_Addr view_address, | |
1605 | section_size_type); | |
d5b40221 | 1606 | |
4a657b0d DK |
1607 | // Information about this specific target which we pass to the |
1608 | // general Target structure. | |
1609 | static const Target::Target_info arm_info; | |
94cdfcff DK |
1610 | |
1611 | // The types of GOT entries needed for this platform. | |
1612 | enum Got_type | |
1613 | { | |
1614 | GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol | |
1615 | }; | |
1616 | ||
55da9579 DK |
1617 | typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list; |
1618 | ||
1619 | // Map input section to Arm_input_section. | |
1620 | typedef Unordered_map<Input_section_specifier, | |
1621 | Arm_input_section<big_endian>*, | |
1622 | Input_section_specifier::hash, | |
1623 | Input_section_specifier::equal_to> | |
1624 | Arm_input_section_map; | |
1625 | ||
94cdfcff DK |
1626 | // The GOT section. |
1627 | Output_data_got<32, big_endian>* got_; | |
1628 | // The PLT section. | |
1629 | Output_data_plt_arm<big_endian>* plt_; | |
1630 | // The GOT PLT section. | |
1631 | Output_data_space* got_plt_; | |
1632 | // The dynamic reloc section. | |
1633 | Reloc_section* rel_dyn_; | |
1634 | // Relocs saved to avoid a COPY reloc. | |
1635 | Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_; | |
1636 | // Space for variables copied with a COPY reloc. | |
1637 | Output_data_space* dynbss_; | |
55da9579 DK |
1638 | // Vector of Stub_tables created. |
1639 | Stub_table_list stub_tables_; | |
1640 | // Stub factory. | |
1641 | const Stub_factory &stub_factory_; | |
b569affa DK |
1642 | // Whether we can use BLX. |
1643 | bool may_use_blx_; | |
1644 | // Whether we force PIC branch veneers. | |
1645 | bool should_force_pic_veneer_; | |
eb44217c DK |
1646 | // Map for locating Arm_input_sections. |
1647 | Arm_input_section_map arm_input_section_map_; | |
4a657b0d DK |
1648 | }; |
1649 | ||
1650 | template<bool big_endian> | |
1651 | const Target::Target_info Target_arm<big_endian>::arm_info = | |
1652 | { | |
1653 | 32, // size | |
1654 | big_endian, // is_big_endian | |
1655 | elfcpp::EM_ARM, // machine_code | |
1656 | false, // has_make_symbol | |
1657 | false, // has_resolve | |
1658 | false, // has_code_fill | |
1659 | true, // is_default_stack_executable | |
1660 | '\0', // wrap_char | |
1661 | "/usr/lib/libc.so.1", // dynamic_linker | |
1662 | 0x8000, // default_text_segment_address | |
1663 | 0x1000, // abi_pagesize (overridable by -z max-page-size) | |
8a5e3e08 ILT |
1664 | 0x1000, // common_pagesize (overridable by -z common-page-size) |
1665 | elfcpp::SHN_UNDEF, // small_common_shndx | |
1666 | elfcpp::SHN_UNDEF, // large_common_shndx | |
1667 | 0, // small_common_section_flags | |
1668 | 0 // large_common_section_flags | |
4a657b0d DK |
1669 | }; |
1670 | ||
c121c671 DK |
1671 | // Arm relocate functions class |
1672 | // | |
1673 | ||
1674 | template<bool big_endian> | |
1675 | class Arm_relocate_functions : public Relocate_functions<32, big_endian> | |
1676 | { | |
1677 | public: | |
1678 | typedef enum | |
1679 | { | |
1680 | STATUS_OKAY, // No error during relocation. | |
1681 | STATUS_OVERFLOW, // Relocation oveflow. | |
1682 | STATUS_BAD_RELOC // Relocation cannot be applied. | |
1683 | } Status; | |
1684 | ||
1685 | private: | |
1686 | typedef Relocate_functions<32, big_endian> Base; | |
1687 | typedef Arm_relocate_functions<big_endian> This; | |
1688 | ||
fd3c5f0b ILT |
1689 | // Encoding of imm16 argument for movt and movw ARM instructions |
1690 | // from ARM ARM: | |
1691 | // | |
1692 | // imm16 := imm4 | imm12 | |
1693 | // | |
1694 | // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0 | |
1695 | // +-------+---------------+-------+-------+-----------------------+ | |
1696 | // | | |imm4 | |imm12 | | |
1697 | // +-------+---------------+-------+-------+-----------------------+ | |
1698 | ||
1699 | // Extract the relocation addend from VAL based on the ARM | |
1700 | // instruction encoding described above. | |
1701 | static inline typename elfcpp::Swap<32, big_endian>::Valtype | |
1702 | extract_arm_movw_movt_addend( | |
1703 | typename elfcpp::Swap<32, big_endian>::Valtype val) | |
1704 | { | |
1705 | // According to the Elf ABI for ARM Architecture the immediate | |
1706 | // field is sign-extended to form the addend. | |
1707 | return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff)); | |
1708 | } | |
1709 | ||
1710 | // Insert X into VAL based on the ARM instruction encoding described | |
1711 | // above. | |
1712 | static inline typename elfcpp::Swap<32, big_endian>::Valtype | |
1713 | insert_val_arm_movw_movt( | |
1714 | typename elfcpp::Swap<32, big_endian>::Valtype val, | |
1715 | typename elfcpp::Swap<32, big_endian>::Valtype x) | |
1716 | { | |
1717 | val &= 0xfff0f000; | |
1718 | val |= x & 0x0fff; | |
1719 | val |= (x & 0xf000) << 4; | |
1720 | return val; | |
1721 | } | |
1722 | ||
1723 | // Encoding of imm16 argument for movt and movw Thumb2 instructions | |
1724 | // from ARM ARM: | |
1725 | // | |
1726 | // imm16 := imm4 | i | imm3 | imm8 | |
1727 | // | |
1728 | // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0 | |
1729 | // +---------+-+-----------+-------++-+-----+-------+---------------+ | |
1730 | // | |i| |imm4 || |imm3 | |imm8 | | |
1731 | // +---------+-+-----------+-------++-+-----+-------+---------------+ | |
1732 | ||
1733 | // Extract the relocation addend from VAL based on the Thumb2 | |
1734 | // instruction encoding described above. | |
1735 | static inline typename elfcpp::Swap<32, big_endian>::Valtype | |
1736 | extract_thumb_movw_movt_addend( | |
1737 | typename elfcpp::Swap<32, big_endian>::Valtype val) | |
1738 | { | |
1739 | // According to the Elf ABI for ARM Architecture the immediate | |
1740 | // field is sign-extended to form the addend. | |
1741 | return utils::sign_extend<16>(((val >> 4) & 0xf000) | |
1742 | | ((val >> 15) & 0x0800) | |
1743 | | ((val >> 4) & 0x0700) | |
1744 | | (val & 0x00ff)); | |
1745 | } | |
1746 | ||
1747 | // Insert X into VAL based on the Thumb2 instruction encoding | |
1748 | // described above. | |
1749 | static inline typename elfcpp::Swap<32, big_endian>::Valtype | |
1750 | insert_val_thumb_movw_movt( | |
1751 | typename elfcpp::Swap<32, big_endian>::Valtype val, | |
1752 | typename elfcpp::Swap<32, big_endian>::Valtype x) | |
1753 | { | |
1754 | val &= 0xfbf08f00; | |
1755 | val |= (x & 0xf000) << 4; | |
1756 | val |= (x & 0x0800) << 15; | |
1757 | val |= (x & 0x0700) << 4; | |
1758 | val |= (x & 0x00ff); | |
1759 | return val; | |
1760 | } | |
1761 | ||
d204b6e9 DK |
1762 | // Handle ARM long branches. |
1763 | static typename This::Status | |
1764 | arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*, | |
1765 | unsigned char *, const Sized_symbol<32>*, | |
1766 | const Arm_relobj<big_endian>*, unsigned int, | |
1767 | const Symbol_value<32>*, Arm_address, Arm_address, bool); | |
c121c671 | 1768 | |
51938283 DK |
1769 | // Handle THUMB long branches. |
1770 | static typename This::Status | |
1771 | thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*, | |
1772 | unsigned char *, const Sized_symbol<32>*, | |
1773 | const Arm_relobj<big_endian>*, unsigned int, | |
1774 | const Symbol_value<32>*, Arm_address, Arm_address, bool); | |
1775 | ||
c121c671 | 1776 | public: |
5e445df6 ILT |
1777 | |
1778 | // R_ARM_ABS8: S + A | |
1779 | static inline typename This::Status | |
1780 | abs8(unsigned char *view, | |
1781 | const Sized_relobj<32, big_endian>* object, | |
be8fcb75 | 1782 | const Symbol_value<32>* psymval) |
5e445df6 ILT |
1783 | { |
1784 | typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype; | |
1785 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; | |
1786 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
1787 | Valtype val = elfcpp::Swap<8, big_endian>::readval(wv); | |
1788 | Reltype addend = utils::sign_extend<8>(val); | |
2daedcd6 | 1789 | Reltype x = psymval->value(object, addend); |
5e445df6 ILT |
1790 | val = utils::bit_select(val, x, 0xffU); |
1791 | elfcpp::Swap<8, big_endian>::writeval(wv, val); | |
1792 | return (utils::has_signed_unsigned_overflow<8>(x) | |
1793 | ? This::STATUS_OVERFLOW | |
1794 | : This::STATUS_OKAY); | |
1795 | } | |
1796 | ||
be8fcb75 ILT |
1797 | // R_ARM_THM_ABS5: S + A |
1798 | static inline typename This::Status | |
1799 | thm_abs5(unsigned char *view, | |
1800 | const Sized_relobj<32, big_endian>* object, | |
1801 | const Symbol_value<32>* psymval) | |
1802 | { | |
1803 | typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; | |
1804 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; | |
1805 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
1806 | Valtype val = elfcpp::Swap<16, big_endian>::readval(wv); | |
1807 | Reltype addend = (val & 0x7e0U) >> 6; | |
2daedcd6 | 1808 | Reltype x = psymval->value(object, addend); |
be8fcb75 ILT |
1809 | val = utils::bit_select(val, x << 6, 0x7e0U); |
1810 | elfcpp::Swap<16, big_endian>::writeval(wv, val); | |
1811 | return (utils::has_overflow<5>(x) | |
1812 | ? This::STATUS_OVERFLOW | |
1813 | : This::STATUS_OKAY); | |
1814 | } | |
1815 | ||
1816 | // R_ARM_ABS12: S + A | |
1817 | static inline typename This::Status | |
1818 | abs12(unsigned char *view, | |
51938283 DK |
1819 | const Sized_relobj<32, big_endian>* object, |
1820 | const Symbol_value<32>* psymval) | |
be8fcb75 ILT |
1821 | { |
1822 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
1823 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; | |
1824 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
1825 | Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); | |
1826 | Reltype addend = val & 0x0fffU; | |
2daedcd6 | 1827 | Reltype x = psymval->value(object, addend); |
be8fcb75 ILT |
1828 | val = utils::bit_select(val, x, 0x0fffU); |
1829 | elfcpp::Swap<32, big_endian>::writeval(wv, val); | |
1830 | return (utils::has_overflow<12>(x) | |
1831 | ? This::STATUS_OVERFLOW | |
1832 | : This::STATUS_OKAY); | |
1833 | } | |
1834 | ||
1835 | // R_ARM_ABS16: S + A | |
1836 | static inline typename This::Status | |
1837 | abs16(unsigned char *view, | |
51938283 DK |
1838 | const Sized_relobj<32, big_endian>* object, |
1839 | const Symbol_value<32>* psymval) | |
be8fcb75 ILT |
1840 | { |
1841 | typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; | |
1842 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; | |
1843 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
1844 | Valtype val = elfcpp::Swap<16, big_endian>::readval(wv); | |
1845 | Reltype addend = utils::sign_extend<16>(val); | |
2daedcd6 | 1846 | Reltype x = psymval->value(object, addend); |
be8fcb75 ILT |
1847 | val = utils::bit_select(val, x, 0xffffU); |
1848 | elfcpp::Swap<16, big_endian>::writeval(wv, val); | |
1849 | return (utils::has_signed_unsigned_overflow<16>(x) | |
1850 | ? This::STATUS_OVERFLOW | |
1851 | : This::STATUS_OKAY); | |
1852 | } | |
1853 | ||
c121c671 DK |
1854 | // R_ARM_ABS32: (S + A) | T |
1855 | static inline typename This::Status | |
1856 | abs32(unsigned char *view, | |
1857 | const Sized_relobj<32, big_endian>* object, | |
1858 | const Symbol_value<32>* psymval, | |
2daedcd6 | 1859 | Arm_address thumb_bit) |
c121c671 DK |
1860 | { |
1861 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
1862 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
1863 | Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv); | |
2daedcd6 | 1864 | Valtype x = psymval->value(object, addend) | thumb_bit; |
c121c671 DK |
1865 | elfcpp::Swap<32, big_endian>::writeval(wv, x); |
1866 | return This::STATUS_OKAY; | |
1867 | } | |
1868 | ||
1869 | // R_ARM_REL32: (S + A) | T - P | |
1870 | static inline typename This::Status | |
1871 | rel32(unsigned char *view, | |
1872 | const Sized_relobj<32, big_endian>* object, | |
1873 | const Symbol_value<32>* psymval, | |
ebabffbd | 1874 | Arm_address address, |
2daedcd6 | 1875 | Arm_address thumb_bit) |
c121c671 DK |
1876 | { |
1877 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
1878 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
1879 | Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv); | |
2daedcd6 | 1880 | Valtype x = (psymval->value(object, addend) | thumb_bit) - address; |
c121c671 DK |
1881 | elfcpp::Swap<32, big_endian>::writeval(wv, x); |
1882 | return This::STATUS_OKAY; | |
1883 | } | |
1884 | ||
1885 | // R_ARM_THM_CALL: (S + A) | T - P | |
1886 | static inline typename This::Status | |
51938283 DK |
1887 | thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view, |
1888 | const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object, | |
1889 | unsigned int r_sym, const Symbol_value<32>* psymval, | |
1890 | Arm_address address, Arm_address thumb_bit, | |
1891 | bool is_weakly_undefined_without_plt) | |
c121c671 | 1892 | { |
51938283 DK |
1893 | return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym, |
1894 | object, r_sym, psymval, address, thumb_bit, | |
1895 | is_weakly_undefined_without_plt); | |
1896 | } | |
c121c671 | 1897 | |
51938283 DK |
1898 | // R_ARM_THM_JUMP24: (S + A) | T - P |
1899 | static inline typename This::Status | |
1900 | thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view, | |
1901 | const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object, | |
1902 | unsigned int r_sym, const Symbol_value<32>* psymval, | |
1903 | Arm_address address, Arm_address thumb_bit, | |
1904 | bool is_weakly_undefined_without_plt) | |
1905 | { | |
1906 | return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym, | |
1907 | object, r_sym, psymval, address, thumb_bit, | |
1908 | is_weakly_undefined_without_plt); | |
1909 | } | |
1910 | ||
1911 | // R_ARM_THM_XPC22: (S + A) | T - P | |
1912 | static inline typename This::Status | |
1913 | thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view, | |
1914 | const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object, | |
1915 | unsigned int r_sym, const Symbol_value<32>* psymval, | |
1916 | Arm_address address, Arm_address thumb_bit, | |
1917 | bool is_weakly_undefined_without_plt) | |
1918 | { | |
1919 | return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym, | |
1920 | object, r_sym, psymval, address, thumb_bit, | |
1921 | is_weakly_undefined_without_plt); | |
c121c671 DK |
1922 | } |
1923 | ||
1924 | // R_ARM_BASE_PREL: B(S) + A - P | |
1925 | static inline typename This::Status | |
1926 | base_prel(unsigned char* view, | |
ebabffbd DK |
1927 | Arm_address origin, |
1928 | Arm_address address) | |
c121c671 DK |
1929 | { |
1930 | Base::rel32(view, origin - address); | |
1931 | return STATUS_OKAY; | |
1932 | } | |
1933 | ||
be8fcb75 ILT |
1934 | // R_ARM_BASE_ABS: B(S) + A |
1935 | static inline typename This::Status | |
1936 | base_abs(unsigned char* view, | |
f4e5969c | 1937 | Arm_address origin) |
be8fcb75 ILT |
1938 | { |
1939 | Base::rel32(view, origin); | |
1940 | return STATUS_OKAY; | |
1941 | } | |
1942 | ||
c121c671 DK |
1943 | // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG |
1944 | static inline typename This::Status | |
1945 | got_brel(unsigned char* view, | |
1946 | typename elfcpp::Swap<32, big_endian>::Valtype got_offset) | |
1947 | { | |
1948 | Base::rel32(view, got_offset); | |
1949 | return This::STATUS_OKAY; | |
1950 | } | |
1951 | ||
f4e5969c | 1952 | // R_ARM_GOT_PREL: GOT(S) + A - P |
7f5309a5 | 1953 | static inline typename This::Status |
f4e5969c DK |
1954 | got_prel(unsigned char *view, |
1955 | Arm_address got_entry, | |
ebabffbd | 1956 | Arm_address address) |
7f5309a5 | 1957 | { |
f4e5969c | 1958 | Base::rel32(view, got_entry - address); |
7f5309a5 ILT |
1959 | return This::STATUS_OKAY; |
1960 | } | |
1961 | ||
c121c671 DK |
1962 | // R_ARM_PLT32: (S + A) | T - P |
1963 | static inline typename This::Status | |
d204b6e9 DK |
1964 | plt32(const Relocate_info<32, big_endian>* relinfo, |
1965 | unsigned char *view, | |
1966 | const Sized_symbol<32>* gsym, | |
1967 | const Arm_relobj<big_endian>* object, | |
1968 | unsigned int r_sym, | |
c121c671 | 1969 | const Symbol_value<32>* psymval, |
ebabffbd | 1970 | Arm_address address, |
d204b6e9 DK |
1971 | Arm_address thumb_bit, |
1972 | bool is_weakly_undefined_without_plt) | |
1973 | { | |
1974 | return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym, | |
1975 | object, r_sym, psymval, address, thumb_bit, | |
1976 | is_weakly_undefined_without_plt); | |
1977 | } | |
1978 | ||
1979 | // R_ARM_XPC25: (S + A) | T - P | |
1980 | static inline typename This::Status | |
1981 | xpc25(const Relocate_info<32, big_endian>* relinfo, | |
1982 | unsigned char *view, | |
1983 | const Sized_symbol<32>* gsym, | |
1984 | const Arm_relobj<big_endian>* object, | |
1985 | unsigned int r_sym, | |
1986 | const Symbol_value<32>* psymval, | |
1987 | Arm_address address, | |
1988 | Arm_address thumb_bit, | |
1989 | bool is_weakly_undefined_without_plt) | |
c121c671 | 1990 | { |
d204b6e9 DK |
1991 | return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym, |
1992 | object, r_sym, psymval, address, thumb_bit, | |
1993 | is_weakly_undefined_without_plt); | |
c121c671 DK |
1994 | } |
1995 | ||
1996 | // R_ARM_CALL: (S + A) | T - P | |
1997 | static inline typename This::Status | |
d204b6e9 DK |
1998 | call(const Relocate_info<32, big_endian>* relinfo, |
1999 | unsigned char *view, | |
2000 | const Sized_symbol<32>* gsym, | |
2001 | const Arm_relobj<big_endian>* object, | |
2002 | unsigned int r_sym, | |
c121c671 | 2003 | const Symbol_value<32>* psymval, |
ebabffbd | 2004 | Arm_address address, |
d204b6e9 DK |
2005 | Arm_address thumb_bit, |
2006 | bool is_weakly_undefined_without_plt) | |
c121c671 | 2007 | { |
d204b6e9 DK |
2008 | return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym, |
2009 | object, r_sym, psymval, address, thumb_bit, | |
2010 | is_weakly_undefined_without_plt); | |
c121c671 DK |
2011 | } |
2012 | ||
2013 | // R_ARM_JUMP24: (S + A) | T - P | |
2014 | static inline typename This::Status | |
d204b6e9 DK |
2015 | jump24(const Relocate_info<32, big_endian>* relinfo, |
2016 | unsigned char *view, | |
2017 | const Sized_symbol<32>* gsym, | |
2018 | const Arm_relobj<big_endian>* object, | |
2019 | unsigned int r_sym, | |
c121c671 | 2020 | const Symbol_value<32>* psymval, |
ebabffbd | 2021 | Arm_address address, |
d204b6e9 DK |
2022 | Arm_address thumb_bit, |
2023 | bool is_weakly_undefined_without_plt) | |
c121c671 | 2024 | { |
d204b6e9 DK |
2025 | return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym, |
2026 | object, r_sym, psymval, address, thumb_bit, | |
2027 | is_weakly_undefined_without_plt); | |
c121c671 DK |
2028 | } |
2029 | ||
2030 | // R_ARM_PREL: (S + A) | T - P | |
2031 | static inline typename This::Status | |
2032 | prel31(unsigned char *view, | |
2033 | const Sized_relobj<32, big_endian>* object, | |
2034 | const Symbol_value<32>* psymval, | |
ebabffbd | 2035 | Arm_address address, |
2daedcd6 | 2036 | Arm_address thumb_bit) |
c121c671 DK |
2037 | { |
2038 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
2039 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2040 | Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); | |
2041 | Valtype addend = utils::sign_extend<31>(val); | |
2daedcd6 | 2042 | Valtype x = (psymval->value(object, addend) | thumb_bit) - address; |
c121c671 DK |
2043 | val = utils::bit_select(val, x, 0x7fffffffU); |
2044 | elfcpp::Swap<32, big_endian>::writeval(wv, val); | |
2045 | return (utils::has_overflow<31>(x) ? | |
2046 | This::STATUS_OVERFLOW : This::STATUS_OKAY); | |
2047 | } | |
fd3c5f0b ILT |
2048 | |
2049 | // R_ARM_MOVW_ABS_NC: (S + A) | T | |
2050 | static inline typename This::Status | |
2051 | movw_abs_nc(unsigned char *view, | |
2052 | const Sized_relobj<32, big_endian>* object, | |
2053 | const Symbol_value<32>* psymval, | |
2daedcd6 | 2054 | Arm_address thumb_bit) |
fd3c5f0b ILT |
2055 | { |
2056 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
2057 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2058 | Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); | |
2059 | Valtype addend = This::extract_arm_movw_movt_addend(val); | |
2daedcd6 | 2060 | Valtype x = psymval->value(object, addend) | thumb_bit; |
fd3c5f0b ILT |
2061 | val = This::insert_val_arm_movw_movt(val, x); |
2062 | elfcpp::Swap<32, big_endian>::writeval(wv, val); | |
2063 | return This::STATUS_OKAY; | |
2064 | } | |
2065 | ||
2066 | // R_ARM_MOVT_ABS: S + A | |
2067 | static inline typename This::Status | |
2068 | movt_abs(unsigned char *view, | |
2069 | const Sized_relobj<32, big_endian>* object, | |
2070 | const Symbol_value<32>* psymval) | |
2071 | { | |
2072 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
2073 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2074 | Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); | |
2075 | Valtype addend = This::extract_arm_movw_movt_addend(val); | |
2daedcd6 | 2076 | Valtype x = psymval->value(object, addend) >> 16; |
fd3c5f0b ILT |
2077 | val = This::insert_val_arm_movw_movt(val, x); |
2078 | elfcpp::Swap<32, big_endian>::writeval(wv, val); | |
2079 | return This::STATUS_OKAY; | |
2080 | } | |
2081 | ||
2082 | // R_ARM_THM_MOVW_ABS_NC: S + A | T | |
2083 | static inline typename This::Status | |
2084 | thm_movw_abs_nc(unsigned char *view, | |
2085 | const Sized_relobj<32, big_endian>* object, | |
2086 | const Symbol_value<32>* psymval, | |
2daedcd6 | 2087 | Arm_address thumb_bit) |
fd3c5f0b ILT |
2088 | { |
2089 | typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; | |
2090 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; | |
2091 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2092 | Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16) | |
2093 | | elfcpp::Swap<16, big_endian>::readval(wv + 1)); | |
2094 | Reltype addend = extract_thumb_movw_movt_addend(val); | |
2daedcd6 | 2095 | Reltype x = psymval->value(object, addend) | thumb_bit; |
fd3c5f0b ILT |
2096 | val = This::insert_val_thumb_movw_movt(val, x); |
2097 | elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16); | |
2098 | elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff); | |
2099 | return This::STATUS_OKAY; | |
2100 | } | |
2101 | ||
2102 | // R_ARM_THM_MOVT_ABS: S + A | |
2103 | static inline typename This::Status | |
2104 | thm_movt_abs(unsigned char *view, | |
2105 | const Sized_relobj<32, big_endian>* object, | |
2106 | const Symbol_value<32>* psymval) | |
2107 | { | |
2108 | typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; | |
2109 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; | |
2110 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2111 | Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16) | |
2112 | | elfcpp::Swap<16, big_endian>::readval(wv + 1)); | |
2113 | Reltype addend = This::extract_thumb_movw_movt_addend(val); | |
2daedcd6 | 2114 | Reltype x = psymval->value(object, addend) >> 16; |
fd3c5f0b ILT |
2115 | val = This::insert_val_thumb_movw_movt(val, x); |
2116 | elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16); | |
2117 | elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff); | |
2118 | return This::STATUS_OKAY; | |
2119 | } | |
2120 | ||
c2a122b6 ILT |
2121 | // R_ARM_MOVW_PREL_NC: (S + A) | T - P |
2122 | static inline typename This::Status | |
2123 | movw_prel_nc(unsigned char *view, | |
2124 | const Sized_relobj<32, big_endian>* object, | |
2125 | const Symbol_value<32>* psymval, | |
ebabffbd | 2126 | Arm_address address, |
2daedcd6 | 2127 | Arm_address thumb_bit) |
c2a122b6 ILT |
2128 | { |
2129 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
2130 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2131 | Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); | |
2132 | Valtype addend = This::extract_arm_movw_movt_addend(val); | |
2daedcd6 | 2133 | Valtype x = (psymval->value(object, addend) | thumb_bit) - address; |
c2a122b6 ILT |
2134 | val = This::insert_val_arm_movw_movt(val, x); |
2135 | elfcpp::Swap<32, big_endian>::writeval(wv, val); | |
2136 | return This::STATUS_OKAY; | |
2137 | } | |
2138 | ||
2139 | // R_ARM_MOVT_PREL: S + A - P | |
2140 | static inline typename This::Status | |
2141 | movt_prel(unsigned char *view, | |
2142 | const Sized_relobj<32, big_endian>* object, | |
2143 | const Symbol_value<32>* psymval, | |
ebabffbd | 2144 | Arm_address address) |
c2a122b6 ILT |
2145 | { |
2146 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
2147 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2148 | Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); | |
2149 | Valtype addend = This::extract_arm_movw_movt_addend(val); | |
2daedcd6 | 2150 | Valtype x = (psymval->value(object, addend) - address) >> 16; |
c2a122b6 ILT |
2151 | val = This::insert_val_arm_movw_movt(val, x); |
2152 | elfcpp::Swap<32, big_endian>::writeval(wv, val); | |
2153 | return This::STATUS_OKAY; | |
2154 | } | |
2155 | ||
2156 | // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P | |
2157 | static inline typename This::Status | |
2158 | thm_movw_prel_nc(unsigned char *view, | |
2159 | const Sized_relobj<32, big_endian>* object, | |
2160 | const Symbol_value<32>* psymval, | |
ebabffbd | 2161 | Arm_address address, |
2daedcd6 | 2162 | Arm_address thumb_bit) |
c2a122b6 ILT |
2163 | { |
2164 | typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; | |
2165 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; | |
2166 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2167 | Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16) | |
2168 | | elfcpp::Swap<16, big_endian>::readval(wv + 1); | |
2169 | Reltype addend = This::extract_thumb_movw_movt_addend(val); | |
2daedcd6 | 2170 | Reltype x = (psymval->value(object, addend) | thumb_bit) - address; |
c2a122b6 ILT |
2171 | val = This::insert_val_thumb_movw_movt(val, x); |
2172 | elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16); | |
2173 | elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff); | |
2174 | return This::STATUS_OKAY; | |
2175 | } | |
2176 | ||
2177 | // R_ARM_THM_MOVT_PREL: S + A - P | |
2178 | static inline typename This::Status | |
2179 | thm_movt_prel(unsigned char *view, | |
2180 | const Sized_relobj<32, big_endian>* object, | |
2181 | const Symbol_value<32>* psymval, | |
ebabffbd | 2182 | Arm_address address) |
c2a122b6 ILT |
2183 | { |
2184 | typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; | |
2185 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; | |
2186 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2187 | Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16) | |
2188 | | elfcpp::Swap<16, big_endian>::readval(wv + 1); | |
2189 | Reltype addend = This::extract_thumb_movw_movt_addend(val); | |
2daedcd6 | 2190 | Reltype x = (psymval->value(object, addend) - address) >> 16; |
c2a122b6 ILT |
2191 | val = This::insert_val_thumb_movw_movt(val, x); |
2192 | elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16); | |
2193 | elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff); | |
2194 | return This::STATUS_OKAY; | |
2195 | } | |
c121c671 DK |
2196 | }; |
2197 | ||
d204b6e9 DK |
2198 | // Relocate ARM long branches. This handles relocation types |
2199 | // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25. | |
2200 | // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly | |
2201 | // undefined and we do not use PLT in this relocation. In such a case, | |
2202 | // the branch is converted into an NOP. | |
2203 | ||
2204 | template<bool big_endian> | |
2205 | typename Arm_relocate_functions<big_endian>::Status | |
2206 | Arm_relocate_functions<big_endian>::arm_branch_common( | |
2207 | unsigned int r_type, | |
2208 | const Relocate_info<32, big_endian>* relinfo, | |
2209 | unsigned char *view, | |
2210 | const Sized_symbol<32>* gsym, | |
2211 | const Arm_relobj<big_endian>* object, | |
2212 | unsigned int r_sym, | |
2213 | const Symbol_value<32>* psymval, | |
2214 | Arm_address address, | |
2215 | Arm_address thumb_bit, | |
2216 | bool is_weakly_undefined_without_plt) | |
2217 | { | |
2218 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
2219 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2220 | Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); | |
2221 | ||
2222 | bool insn_is_b = (((val >> 28) & 0xf) <= 0xe) | |
2223 | && ((val & 0x0f000000UL) == 0x0a000000UL); | |
2224 | bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL; | |
2225 | bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe) | |
2226 | && ((val & 0x0f000000UL) == 0x0b000000UL); | |
2227 | bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL; | |
2228 | bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL; | |
2229 | ||
2230 | // Check that the instruction is valid. | |
2231 | if (r_type == elfcpp::R_ARM_CALL) | |
2232 | { | |
2233 | if (!insn_is_uncond_bl && !insn_is_blx) | |
2234 | return This::STATUS_BAD_RELOC; | |
2235 | } | |
2236 | else if (r_type == elfcpp::R_ARM_JUMP24) | |
2237 | { | |
2238 | if (!insn_is_b && !insn_is_cond_bl) | |
2239 | return This::STATUS_BAD_RELOC; | |
2240 | } | |
2241 | else if (r_type == elfcpp::R_ARM_PLT32) | |
2242 | { | |
2243 | if (!insn_is_any_branch) | |
2244 | return This::STATUS_BAD_RELOC; | |
2245 | } | |
2246 | else if (r_type == elfcpp::R_ARM_XPC25) | |
2247 | { | |
2248 | // FIXME: AAELF document IH0044C does not say much about it other | |
2249 | // than it being obsolete. | |
2250 | if (!insn_is_any_branch) | |
2251 | return This::STATUS_BAD_RELOC; | |
2252 | } | |
2253 | else | |
2254 | gold_unreachable(); | |
2255 | ||
2256 | // A branch to an undefined weak symbol is turned into a jump to | |
2257 | // the next instruction unless a PLT entry will be created. | |
2258 | // Do the same for local undefined symbols. | |
2259 | // The jump to the next instruction is optimized as a NOP depending | |
2260 | // on the architecture. | |
2261 | const Target_arm<big_endian>* arm_target = | |
2262 | Target_arm<big_endian>::default_target(); | |
2263 | if (is_weakly_undefined_without_plt) | |
2264 | { | |
2265 | Valtype cond = val & 0xf0000000U; | |
2266 | if (arm_target->may_use_arm_nop()) | |
2267 | val = cond | 0x0320f000; | |
2268 | else | |
2269 | val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0. | |
2270 | elfcpp::Swap<32, big_endian>::writeval(wv, val); | |
2271 | return This::STATUS_OKAY; | |
2272 | } | |
2273 | ||
2274 | Valtype addend = utils::sign_extend<26>(val << 2); | |
2275 | Valtype branch_target = psymval->value(object, addend); | |
2276 | int32_t branch_offset = branch_target - address; | |
2277 | ||
2278 | // We need a stub if the branch offset is too large or if we need | |
2279 | // to switch mode. | |
2280 | bool may_use_blx = arm_target->may_use_blx(); | |
2281 | Reloc_stub* stub = NULL; | |
2282 | if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET) | |
2283 | || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET) | |
2284 | || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL))) | |
2285 | { | |
2286 | Stub_type stub_type = | |
2287 | Reloc_stub::stub_type_for_reloc(r_type, address, branch_target, | |
2288 | (thumb_bit != 0)); | |
2289 | if (stub_type != arm_stub_none) | |
2290 | { | |
2291 | Stub_table<big_endian>* stub_table = | |
2292 | object->stub_table(relinfo->data_shndx); | |
2293 | gold_assert(stub_table != NULL); | |
2294 | ||
2295 | Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend); | |
2296 | stub = stub_table->find_reloc_stub(stub_key); | |
2297 | gold_assert(stub != NULL); | |
2298 | thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0; | |
2299 | branch_target = stub_table->address() + stub->offset() + addend; | |
2300 | branch_offset = branch_target - address; | |
2301 | gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET) | |
2302 | && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET)); | |
2303 | } | |
2304 | } | |
2305 | ||
2306 | // At this point, if we still need to switch mode, the instruction | |
2307 | // must either be a BLX or a BL that can be converted to a BLX. | |
2308 | if (thumb_bit != 0) | |
2309 | { | |
2310 | // Turn BL to BLX. | |
2311 | gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL); | |
2312 | val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23); | |
2313 | } | |
2314 | ||
2315 | val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL); | |
2316 | elfcpp::Swap<32, big_endian>::writeval(wv, val); | |
2317 | return (utils::has_overflow<26>(branch_offset) | |
2318 | ? This::STATUS_OVERFLOW : This::STATUS_OKAY); | |
2319 | } | |
2320 | ||
51938283 DK |
2321 | // Relocate THUMB long branches. This handles relocation types |
2322 | // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22. | |
2323 | // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly | |
2324 | // undefined and we do not use PLT in this relocation. In such a case, | |
2325 | // the branch is converted into an NOP. | |
2326 | ||
2327 | template<bool big_endian> | |
2328 | typename Arm_relocate_functions<big_endian>::Status | |
2329 | Arm_relocate_functions<big_endian>::thumb_branch_common( | |
2330 | unsigned int r_type, | |
2331 | const Relocate_info<32, big_endian>* relinfo, | |
2332 | unsigned char *view, | |
2333 | const Sized_symbol<32>* gsym, | |
2334 | const Arm_relobj<big_endian>* object, | |
2335 | unsigned int r_sym, | |
2336 | const Symbol_value<32>* psymval, | |
2337 | Arm_address address, | |
2338 | Arm_address thumb_bit, | |
2339 | bool is_weakly_undefined_without_plt) | |
2340 | { | |
2341 | typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; | |
2342 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
2343 | uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv); | |
2344 | uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1); | |
2345 | ||
2346 | // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference | |
2347 | // into account. | |
2348 | bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U; | |
2349 | bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U; | |
2350 | ||
2351 | // Check that the instruction is valid. | |
2352 | if (r_type == elfcpp::R_ARM_THM_CALL) | |
2353 | { | |
2354 | if (!is_bl_insn && !is_blx_insn) | |
2355 | return This::STATUS_BAD_RELOC; | |
2356 | } | |
2357 | else if (r_type == elfcpp::R_ARM_THM_JUMP24) | |
2358 | { | |
2359 | // This cannot be a BLX. | |
2360 | if (!is_bl_insn) | |
2361 | return This::STATUS_BAD_RELOC; | |
2362 | } | |
2363 | else if (r_type == elfcpp::R_ARM_THM_XPC22) | |
2364 | { | |
2365 | // Check for Thumb to Thumb call. | |
2366 | if (!is_blx_insn) | |
2367 | return This::STATUS_BAD_RELOC; | |
2368 | if (thumb_bit != 0) | |
2369 | { | |
2370 | gold_warning(_("%s: Thumb BLX instruction targets " | |
2371 | "thumb function '%s'."), | |
2372 | object->name().c_str(), | |
2373 | (gsym ? gsym->name() : "(local)")); | |
2374 | // Convert BLX to BL. | |
2375 | lower_insn |= 0x1000U; | |
2376 | } | |
2377 | } | |
2378 | else | |
2379 | gold_unreachable(); | |
2380 | ||
2381 | // A branch to an undefined weak symbol is turned into a jump to | |
2382 | // the next instruction unless a PLT entry will be created. | |
2383 | // The jump to the next instruction is optimized as a NOP.W for | |
2384 | // Thumb-2 enabled architectures. | |
2385 | const Target_arm<big_endian>* arm_target = | |
2386 | Target_arm<big_endian>::default_target(); | |
2387 | if (is_weakly_undefined_without_plt) | |
2388 | { | |
2389 | if (arm_target->may_use_thumb2_nop()) | |
2390 | { | |
2391 | elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af); | |
2392 | elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000); | |
2393 | } | |
2394 | else | |
2395 | { | |
2396 | elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000); | |
2397 | elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00); | |
2398 | } | |
2399 | return This::STATUS_OKAY; | |
2400 | } | |
2401 | ||
2402 | // Fetch the addend. We use the Thumb-2 encoding (backwards compatible | |
2403 | // with Thumb-1) involving the J1 and J2 bits. | |
2404 | uint32_t s = (upper_insn & (1 << 10)) >> 10; | |
2405 | uint32_t upper = upper_insn & 0x3ff; | |
2406 | uint32_t lower = lower_insn & 0x7ff; | |
2407 | uint32_t j1 = (lower_insn & (1 << 13)) >> 13; | |
2408 | uint32_t j2 = (lower_insn & (1 << 11)) >> 11; | |
2409 | uint32_t i1 = j1 ^ s ? 0 : 1; | |
2410 | uint32_t i2 = j2 ^ s ? 0 : 1; | |
2411 | ||
2412 | int32_t addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1); | |
2413 | // Sign extend. | |
2414 | addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24); | |
2415 | ||
2416 | Arm_address branch_target = psymval->value(object, addend); | |
2417 | int32_t branch_offset = branch_target - address; | |
2418 | ||
2419 | // We need a stub if the branch offset is too large or if we need | |
2420 | // to switch mode. | |
2421 | bool may_use_blx = arm_target->may_use_blx(); | |
2422 | bool thumb2 = arm_target->using_thumb2(); | |
2423 | if ((!thumb2 | |
2424 | && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET | |
2425 | || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET))) | |
2426 | || (thumb2 | |
2427 | && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET | |
2428 | || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET))) | |
2429 | || ((thumb_bit == 0) | |
2430 | && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx) | |
2431 | || r_type == elfcpp::R_ARM_THM_JUMP24))) | |
2432 | { | |
2433 | Stub_type stub_type = | |
2434 | Reloc_stub::stub_type_for_reloc(r_type, address, branch_target, | |
2435 | (thumb_bit != 0)); | |
2436 | if (stub_type != arm_stub_none) | |
2437 | { | |
2438 | Stub_table<big_endian>* stub_table = | |
2439 | object->stub_table(relinfo->data_shndx); | |
2440 | gold_assert(stub_table != NULL); | |
2441 | ||
2442 | Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend); | |
2443 | Reloc_stub* stub = stub_table->find_reloc_stub(stub_key); | |
2444 | gold_assert(stub != NULL); | |
2445 | thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0; | |
2446 | branch_target = stub_table->address() + stub->offset() + addend; | |
2447 | branch_offset = branch_target - address; | |
2448 | } | |
2449 | } | |
2450 | ||
2451 | // At this point, if we still need to switch mode, the instruction | |
2452 | // must either be a BLX or a BL that can be converted to a BLX. | |
2453 | if (thumb_bit == 0) | |
2454 | { | |
2455 | gold_assert(may_use_blx | |
2456 | && (r_type == elfcpp::R_ARM_THM_CALL | |
2457 | || r_type == elfcpp::R_ARM_THM_XPC22)); | |
2458 | // Make sure this is a BLX. | |
2459 | lower_insn &= ~0x1000U; | |
2460 | } | |
2461 | else | |
2462 | { | |
2463 | // Make sure this is a BL. | |
2464 | lower_insn |= 0x1000U; | |
2465 | } | |
2466 | ||
2467 | uint32_t reloc_sign = (branch_offset < 0) ? 1 : 0; | |
2468 | uint32_t relocation = static_cast<uint32_t>(branch_offset); | |
2469 | ||
2470 | if ((lower_insn & 0x5000U) == 0x4000U) | |
2471 | // For a BLX instruction, make sure that the relocation is rounded up | |
2472 | // to a word boundary. This follows the semantics of the instruction | |
2473 | // which specifies that bit 1 of the target address will come from bit | |
2474 | // 1 of the base address. | |
2475 | relocation = (relocation + 2U) & ~3U; | |
2476 | ||
2477 | // Put BRANCH_OFFSET back into the insn. Assumes two's complement. | |
2478 | // We use the Thumb-2 encoding, which is safe even if dealing with | |
2479 | // a Thumb-1 instruction by virtue of our overflow check above. */ | |
2480 | upper_insn = (upper_insn & ~0x7ffU) | |
2481 | | ((relocation >> 12) & 0x3ffU) | |
2482 | | (reloc_sign << 10); | |
2483 | lower_insn = (lower_insn & ~0x2fffU) | |
2484 | | (((!((relocation >> 23) & 1U)) ^ reloc_sign) << 13) | |
2485 | | (((!((relocation >> 22) & 1U)) ^ reloc_sign) << 11) | |
2486 | | ((relocation >> 1) & 0x7ffU); | |
2487 | ||
2488 | elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn); | |
2489 | elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn); | |
2490 | ||
2491 | return ((thumb2 | |
2492 | ? utils::has_overflow<25>(relocation) | |
2493 | : utils::has_overflow<23>(relocation)) | |
2494 | ? This::STATUS_OVERFLOW | |
2495 | : This::STATUS_OKAY); | |
2496 | } | |
2497 | ||
94cdfcff DK |
2498 | // Get the GOT section, creating it if necessary. |
2499 | ||
2500 | template<bool big_endian> | |
2501 | Output_data_got<32, big_endian>* | |
2502 | Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout) | |
2503 | { | |
2504 | if (this->got_ == NULL) | |
2505 | { | |
2506 | gold_assert(symtab != NULL && layout != NULL); | |
2507 | ||
2508 | this->got_ = new Output_data_got<32, big_endian>(); | |
2509 | ||
2510 | Output_section* os; | |
2511 | os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, | |
2512 | (elfcpp::SHF_ALLOC | |
2513 | | elfcpp::SHF_WRITE), | |
f5c870d2 | 2514 | this->got_, false); |
94cdfcff DK |
2515 | os->set_is_relro(); |
2516 | ||
2517 | // The old GNU linker creates a .got.plt section. We just | |
2518 | // create another set of data in the .got section. Note that we | |
2519 | // always create a PLT if we create a GOT, although the PLT | |
2520 | // might be empty. | |
2521 | this->got_plt_ = new Output_data_space(4, "** GOT PLT"); | |
2522 | os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, | |
2523 | (elfcpp::SHF_ALLOC | |
2524 | | elfcpp::SHF_WRITE), | |
f5c870d2 | 2525 | this->got_plt_, false); |
94cdfcff DK |
2526 | os->set_is_relro(); |
2527 | ||
2528 | // The first three entries are reserved. | |
2529 | this->got_plt_->set_current_data_size(3 * 4); | |
2530 | ||
2531 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. | |
2532 | symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, | |
2533 | this->got_plt_, | |
2534 | 0, 0, elfcpp::STT_OBJECT, | |
2535 | elfcpp::STB_LOCAL, | |
2536 | elfcpp::STV_HIDDEN, 0, | |
2537 | false, false); | |
2538 | } | |
2539 | return this->got_; | |
2540 | } | |
2541 | ||
2542 | // Get the dynamic reloc section, creating it if necessary. | |
2543 | ||
2544 | template<bool big_endian> | |
2545 | typename Target_arm<big_endian>::Reloc_section* | |
2546 | Target_arm<big_endian>::rel_dyn_section(Layout* layout) | |
2547 | { | |
2548 | if (this->rel_dyn_ == NULL) | |
2549 | { | |
2550 | gold_assert(layout != NULL); | |
2551 | this->rel_dyn_ = new Reloc_section(parameters->options().combreloc()); | |
2552 | layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL, | |
f5c870d2 | 2553 | elfcpp::SHF_ALLOC, this->rel_dyn_, true); |
94cdfcff DK |
2554 | } |
2555 | return this->rel_dyn_; | |
2556 | } | |
2557 | ||
b569affa DK |
2558 | // Insn_template methods. |
2559 | ||
2560 | // Return byte size of an instruction template. | |
2561 | ||
2562 | size_t | |
2563 | Insn_template::size() const | |
2564 | { | |
2565 | switch (this->type()) | |
2566 | { | |
2567 | case THUMB16_TYPE: | |
2568 | return 2; | |
2569 | case ARM_TYPE: | |
2570 | case THUMB32_TYPE: | |
2571 | case DATA_TYPE: | |
2572 | return 4; | |
2573 | default: | |
2574 | gold_unreachable(); | |
2575 | } | |
2576 | } | |
2577 | ||
2578 | // Return alignment of an instruction template. | |
2579 | ||
2580 | unsigned | |
2581 | Insn_template::alignment() const | |
2582 | { | |
2583 | switch (this->type()) | |
2584 | { | |
2585 | case THUMB16_TYPE: | |
2586 | case THUMB32_TYPE: | |
2587 | return 2; | |
2588 | case ARM_TYPE: | |
2589 | case DATA_TYPE: | |
2590 | return 4; | |
2591 | default: | |
2592 | gold_unreachable(); | |
2593 | } | |
2594 | } | |
2595 | ||
2596 | // Stub_template methods. | |
2597 | ||
2598 | Stub_template::Stub_template( | |
2599 | Stub_type type, const Insn_template* insns, | |
2600 | size_t insn_count) | |
2601 | : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1), | |
2602 | entry_in_thumb_mode_(false), relocs_() | |
2603 | { | |
2604 | off_t offset = 0; | |
2605 | ||
2606 | // Compute byte size and alignment of stub template. | |
2607 | for (size_t i = 0; i < insn_count; i++) | |
2608 | { | |
2609 | unsigned insn_alignment = insns[i].alignment(); | |
2610 | size_t insn_size = insns[i].size(); | |
2611 | gold_assert((offset & (insn_alignment - 1)) == 0); | |
2612 | this->alignment_ = std::max(this->alignment_, insn_alignment); | |
2613 | switch (insns[i].type()) | |
2614 | { | |
2615 | case Insn_template::THUMB16_TYPE: | |
2616 | if (i == 0) | |
2617 | this->entry_in_thumb_mode_ = true; | |
2618 | break; | |
2619 | ||
2620 | case Insn_template::THUMB32_TYPE: | |
2621 | if (insns[i].r_type() != elfcpp::R_ARM_NONE) | |
2622 | this->relocs_.push_back(Reloc(i, offset)); | |
2623 | if (i == 0) | |
2624 | this->entry_in_thumb_mode_ = true; | |
2625 | break; | |
2626 | ||
2627 | case Insn_template::ARM_TYPE: | |
2628 | // Handle cases where the target is encoded within the | |
2629 | // instruction. | |
2630 | if (insns[i].r_type() == elfcpp::R_ARM_JUMP24) | |
2631 | this->relocs_.push_back(Reloc(i, offset)); | |
2632 | break; | |
2633 | ||
2634 | case Insn_template::DATA_TYPE: | |
2635 | // Entry point cannot be data. | |
2636 | gold_assert(i != 0); | |
2637 | this->relocs_.push_back(Reloc(i, offset)); | |
2638 | break; | |
2639 | ||
2640 | default: | |
2641 | gold_unreachable(); | |
2642 | } | |
2643 | offset += insn_size; | |
2644 | } | |
2645 | this->size_ = offset; | |
2646 | } | |
2647 | ||
2648 | // Reloc_stub::Key methods. | |
2649 | ||
2650 | // Dump a Key as a string for debugging. | |
2651 | ||
2652 | std::string | |
2653 | Reloc_stub::Key::name() const | |
2654 | { | |
2655 | if (this->r_sym_ == invalid_index) | |
2656 | { | |
2657 | // Global symbol key name | |
2658 | // <stub-type>:<symbol name>:<addend>. | |
2659 | const std::string sym_name = this->u_.symbol->name(); | |
2660 | // We need to print two hex number and two colons. So just add 100 bytes | |
2661 | // to the symbol name size. | |
2662 | size_t len = sym_name.size() + 100; | |
2663 | char* buffer = new char[len]; | |
2664 | int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_, | |
2665 | sym_name.c_str(), this->addend_); | |
2666 | gold_assert(c > 0 && c < static_cast<int>(len)); | |
2667 | delete[] buffer; | |
2668 | return std::string(buffer); | |
2669 | } | |
2670 | else | |
2671 | { | |
2672 | // local symbol key name | |
2673 | // <stub-type>:<object>:<r_sym>:<addend>. | |
2674 | const size_t len = 200; | |
2675 | char buffer[len]; | |
2676 | int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_, | |
2677 | this->u_.relobj, this->r_sym_, this->addend_); | |
2678 | gold_assert(c > 0 && c < static_cast<int>(len)); | |
2679 | return std::string(buffer); | |
2680 | } | |
2681 | } | |
2682 | ||
2683 | // Reloc_stub methods. | |
2684 | ||
2685 | // Determine the type of stub needed, if any, for a relocation of R_TYPE at | |
2686 | // LOCATION to DESTINATION. | |
2687 | // This code is based on the arm_type_of_stub function in | |
2688 | // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub | |
2689 | // class simple. | |
2690 | ||
2691 | Stub_type | |
2692 | Reloc_stub::stub_type_for_reloc( | |
2693 | unsigned int r_type, | |
2694 | Arm_address location, | |
2695 | Arm_address destination, | |
2696 | bool target_is_thumb) | |
2697 | { | |
2698 | Stub_type stub_type = arm_stub_none; | |
2699 | ||
2700 | // This is a bit ugly but we want to avoid using a templated class for | |
2701 | // big and little endianities. | |
2702 | bool may_use_blx; | |
2703 | bool should_force_pic_veneer; | |
2704 | bool thumb2; | |
2705 | bool thumb_only; | |
2706 | if (parameters->target().is_big_endian()) | |
2707 | { | |
43d12afe | 2708 | const Target_arm<true>* big_endian_target = |
b569affa | 2709 | Target_arm<true>::default_target(); |
43d12afe DK |
2710 | may_use_blx = big_endian_target->may_use_blx(); |
2711 | should_force_pic_veneer = big_endian_target->should_force_pic_veneer(); | |
2712 | thumb2 = big_endian_target->using_thumb2(); | |
2713 | thumb_only = big_endian_target->using_thumb_only(); | |
b569affa DK |
2714 | } |
2715 | else | |
2716 | { | |
43d12afe | 2717 | const Target_arm<false>* little_endian_target = |
b569affa | 2718 | Target_arm<false>::default_target(); |
43d12afe DK |
2719 | may_use_blx = little_endian_target->may_use_blx(); |
2720 | should_force_pic_veneer = little_endian_target->should_force_pic_veneer(); | |
2721 | thumb2 = little_endian_target->using_thumb2(); | |
2722 | thumb_only = little_endian_target->using_thumb_only(); | |
b569affa DK |
2723 | } |
2724 | ||
2725 | int64_t branch_offset = (int64_t)destination - location; | |
2726 | ||
2727 | if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24) | |
2728 | { | |
2729 | // Handle cases where: | |
2730 | // - this call goes too far (different Thumb/Thumb2 max | |
2731 | // distance) | |
2732 | // - it's a Thumb->Arm call and blx is not available, or it's a | |
2733 | // Thumb->Arm branch (not bl). A stub is needed in this case. | |
2734 | if ((!thumb2 | |
2735 | && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET | |
2736 | || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET))) | |
2737 | || (thumb2 | |
2738 | && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET | |
2739 | || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET))) | |
2740 | || ((!target_is_thumb) | |
2741 | && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx) | |
2742 | || (r_type == elfcpp::R_ARM_THM_JUMP24)))) | |
2743 | { | |
2744 | if (target_is_thumb) | |
2745 | { | |
2746 | // Thumb to thumb. | |
2747 | if (!thumb_only) | |
2748 | { | |
51938283 DK |
2749 | stub_type = (parameters->options().shared() |
2750 | || should_force_pic_veneer) | |
b569affa DK |
2751 | // PIC stubs. |
2752 | ? ((may_use_blx | |
2753 | && (r_type == elfcpp::R_ARM_THM_CALL)) | |
2754 | // V5T and above. Stub starts with ARM code, so | |
2755 | // we must be able to switch mode before | |
2756 | // reaching it, which is only possible for 'bl' | |
2757 | // (ie R_ARM_THM_CALL relocation). | |
2758 | ? arm_stub_long_branch_any_thumb_pic | |
2759 | // On V4T, use Thumb code only. | |
2760 | : arm_stub_long_branch_v4t_thumb_thumb_pic) | |
2761 | ||
2762 | // non-PIC stubs. | |
2763 | : ((may_use_blx | |
2764 | && (r_type == elfcpp::R_ARM_THM_CALL)) | |
2765 | ? arm_stub_long_branch_any_any // V5T and above. | |
2766 | : arm_stub_long_branch_v4t_thumb_thumb); // V4T. | |
2767 | } | |
2768 | else | |
2769 | { | |
51938283 DK |
2770 | stub_type = (parameters->options().shared() |
2771 | || should_force_pic_veneer) | |
b569affa DK |
2772 | ? arm_stub_long_branch_thumb_only_pic // PIC stub. |
2773 | : arm_stub_long_branch_thumb_only; // non-PIC stub. | |
2774 | } | |
2775 | } | |
2776 | else | |
2777 | { | |
2778 | // Thumb to arm. | |
2779 | ||
2780 | // FIXME: We should check that the input section is from an | |
2781 | // object that has interwork enabled. | |
2782 | ||
2783 | stub_type = (parameters->options().shared() | |
2784 | || should_force_pic_veneer) | |
2785 | // PIC stubs. | |
2786 | ? ((may_use_blx | |
2787 | && (r_type == elfcpp::R_ARM_THM_CALL)) | |
2788 | ? arm_stub_long_branch_any_arm_pic // V5T and above. | |
2789 | : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T. | |
2790 | ||
2791 | // non-PIC stubs. | |
2792 | : ((may_use_blx | |
2793 | && (r_type == elfcpp::R_ARM_THM_CALL)) | |
2794 | ? arm_stub_long_branch_any_any // V5T and above. | |
2795 | : arm_stub_long_branch_v4t_thumb_arm); // V4T. | |
2796 | ||
2797 | // Handle v4t short branches. | |
2798 | if ((stub_type == arm_stub_long_branch_v4t_thumb_arm) | |
2799 | && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET) | |
2800 | && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET)) | |
2801 | stub_type = arm_stub_short_branch_v4t_thumb_arm; | |
2802 | } | |
2803 | } | |
2804 | } | |
2805 | else if (r_type == elfcpp::R_ARM_CALL | |
2806 | || r_type == elfcpp::R_ARM_JUMP24 | |
2807 | || r_type == elfcpp::R_ARM_PLT32) | |
2808 | { | |
2809 | if (target_is_thumb) | |
2810 | { | |
2811 | // Arm to thumb. | |
2812 | ||
2813 | // FIXME: We should check that the input section is from an | |
2814 | // object that has interwork enabled. | |
2815 | ||
2816 | // We have an extra 2-bytes reach because of | |
2817 | // the mode change (bit 24 (H) of BLX encoding). | |
2818 | if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2) | |
2819 | || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET) | |
2820 | || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx) | |
2821 | || (r_type == elfcpp::R_ARM_JUMP24) | |
2822 | || (r_type == elfcpp::R_ARM_PLT32)) | |
2823 | { | |
2824 | stub_type = (parameters->options().shared() | |
2825 | || should_force_pic_veneer) | |
2826 | // PIC stubs. | |
2827 | ? (may_use_blx | |
2828 | ? arm_stub_long_branch_any_thumb_pic// V5T and above. | |
2829 | : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub. | |
2830 | ||
2831 | // non-PIC stubs. | |
2832 | : (may_use_blx | |
2833 | ? arm_stub_long_branch_any_any // V5T and above. | |
2834 | : arm_stub_long_branch_v4t_arm_thumb); // V4T. | |
2835 | } | |
2836 | } | |
2837 | else | |
2838 | { | |
2839 | // Arm to arm. | |
2840 | if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET | |
2841 | || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)) | |
2842 | { | |
2843 | stub_type = (parameters->options().shared() | |
2844 | || should_force_pic_veneer) | |
2845 | ? arm_stub_long_branch_any_arm_pic // PIC stubs. | |
2846 | : arm_stub_long_branch_any_any; /// non-PIC. | |
2847 | } | |
2848 | } | |
2849 | } | |
2850 | ||
2851 | return stub_type; | |
2852 | } | |
2853 | ||
2854 | // Template to implement do_write for a specific target endianity. | |
2855 | ||
2856 | template<bool big_endian> | |
2857 | void inline | |
2858 | Reloc_stub::do_fixed_endian_write(unsigned char* view, | |
2859 | section_size_type view_size) | |
2860 | { | |
2861 | const Stub_template* stub_template = this->stub_template(); | |
2862 | const Insn_template* insns = stub_template->insns(); | |
2863 | ||
2864 | // FIXME: We do not handle BE8 encoding yet. | |
2865 | unsigned char* pov = view; | |
2866 | for (size_t i = 0; i < stub_template->insn_count(); i++) | |
2867 | { | |
2868 | switch (insns[i].type()) | |
2869 | { | |
2870 | case Insn_template::THUMB16_TYPE: | |
2871 | // Non-zero reloc addends are only used in Cortex-A8 stubs. | |
2872 | gold_assert(insns[i].reloc_addend() == 0); | |
2873 | elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff); | |
2874 | break; | |
2875 | case Insn_template::THUMB32_TYPE: | |
2876 | { | |
2877 | uint32_t hi = (insns[i].data() >> 16) & 0xffff; | |
2878 | uint32_t lo = insns[i].data() & 0xffff; | |
2879 | elfcpp::Swap<16, big_endian>::writeval(pov, hi); | |
2880 | elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo); | |
2881 | } | |
2882 | break; | |
2883 | case Insn_template::ARM_TYPE: | |
2884 | case Insn_template::DATA_TYPE: | |
2885 | elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data()); | |
2886 | break; | |
2887 | default: | |
2888 | gold_unreachable(); | |
2889 | } | |
2890 | pov += insns[i].size(); | |
2891 | } | |
2892 | gold_assert(static_cast<section_size_type>(pov - view) == view_size); | |
2893 | } | |
2894 | ||
2895 | // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN. | |
2896 | ||
2897 | void | |
2898 | Reloc_stub::do_write(unsigned char* view, section_size_type view_size, | |
2899 | bool big_endian) | |
2900 | { | |
2901 | if (big_endian) | |
2902 | this->do_fixed_endian_write<true>(view, view_size); | |
2903 | else | |
2904 | this->do_fixed_endian_write<false>(view, view_size); | |
2905 | } | |
2906 | ||
2907 | // Stub_factory methods. | |
2908 | ||
2909 | Stub_factory::Stub_factory() | |
2910 | { | |
2911 | // The instruction template sequences are declared as static | |
2912 | // objects and initialized first time the constructor runs. | |
2913 | ||
2914 | // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx | |
2915 | // to reach the stub if necessary. | |
2916 | static const Insn_template elf32_arm_stub_long_branch_any_any[] = | |
2917 | { | |
2918 | Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4] | |
2919 | Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0), | |
2920 | // dcd R_ARM_ABS32(X) | |
2921 | }; | |
2922 | ||
2923 | // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not | |
2924 | // available. | |
2925 | static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] = | |
2926 | { | |
2927 | Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0] | |
2928 | Insn_template::arm_insn(0xe12fff1c), // bx ip | |
2929 | Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0), | |
2930 | // dcd R_ARM_ABS32(X) | |
2931 | }; | |
2932 | ||
2933 | // Thumb -> Thumb long branch stub. Used on M-profile architectures. | |
2934 | static const Insn_template elf32_arm_stub_long_branch_thumb_only[] = | |
2935 | { | |
2936 | Insn_template::thumb16_insn(0xb401), // push {r0} | |
2937 | Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8] | |
2938 | Insn_template::thumb16_insn(0x4684), // mov ip, r0 | |
2939 | Insn_template::thumb16_insn(0xbc01), // pop {r0} | |
2940 | Insn_template::thumb16_insn(0x4760), // bx ip | |
2941 | Insn_template::thumb16_insn(0xbf00), // nop | |
2942 | Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0), | |
2943 | // dcd R_ARM_ABS32(X) | |
2944 | }; | |
2945 | ||
2946 | // V4T Thumb -> Thumb long branch stub. Using the stack is not | |
2947 | // allowed. | |
2948 | static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] = | |
2949 | { | |
2950 | Insn_template::thumb16_insn(0x4778), // bx pc | |
2951 | Insn_template::thumb16_insn(0x46c0), // nop | |
2952 | Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0] | |
2953 | Insn_template::arm_insn(0xe12fff1c), // bx ip | |
2954 | Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0), | |
2955 | // dcd R_ARM_ABS32(X) | |
2956 | }; | |
2957 | ||
2958 | // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not | |
2959 | // available. | |
2960 | static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] = | |
2961 | { | |
2962 | Insn_template::thumb16_insn(0x4778), // bx pc | |
2963 | Insn_template::thumb16_insn(0x46c0), // nop | |
2964 | Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4] | |
2965 | Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0), | |
2966 | // dcd R_ARM_ABS32(X) | |
2967 | }; | |
2968 | ||
2969 | // V4T Thumb -> ARM short branch stub. Shorter variant of the above | |
2970 | // one, when the destination is close enough. | |
2971 | static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] = | |
2972 | { | |
2973 | Insn_template::thumb16_insn(0x4778), // bx pc | |
2974 | Insn_template::thumb16_insn(0x46c0), // nop | |
2975 | Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8) | |
2976 | }; | |
2977 | ||
2978 | // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use | |
2979 | // blx to reach the stub if necessary. | |
2980 | static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] = | |
2981 | { | |
2982 | Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc] | |
2983 | Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip | |
2984 | Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4), | |
2985 | // dcd R_ARM_REL32(X-4) | |
2986 | }; | |
2987 | ||
2988 | // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use | |
2989 | // blx to reach the stub if necessary. We can not add into pc; | |
2990 | // it is not guaranteed to mode switch (different in ARMv6 and | |
2991 | // ARMv7). | |
2992 | static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] = | |
2993 | { | |
2994 | Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4] | |
2995 | Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip | |
2996 | Insn_template::arm_insn(0xe12fff1c), // bx ip | |
2997 | Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0), | |
2998 | // dcd R_ARM_REL32(X) | |
2999 | }; | |
3000 | ||
3001 | // V4T ARM -> ARM long branch stub, PIC. | |
3002 | static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] = | |
3003 | { | |
3004 | Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4] | |
3005 | Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip | |
3006 | Insn_template::arm_insn(0xe12fff1c), // bx ip | |
3007 | Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0), | |
3008 | // dcd R_ARM_REL32(X) | |
3009 | }; | |
3010 | ||
3011 | // V4T Thumb -> ARM long branch stub, PIC. | |
3012 | static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] = | |
3013 | { | |
3014 | Insn_template::thumb16_insn(0x4778), // bx pc | |
3015 | Insn_template::thumb16_insn(0x46c0), // nop | |
3016 | Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0] | |
3017 | Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc | |
3018 | Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4), | |
3019 | // dcd R_ARM_REL32(X) | |
3020 | }; | |
3021 | ||
3022 | // Thumb -> Thumb long branch stub, PIC. Used on M-profile | |
3023 | // architectures. | |
3024 | static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] = | |
3025 | { | |
3026 | Insn_template::thumb16_insn(0xb401), // push {r0} | |
3027 | Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8] | |
3028 | Insn_template::thumb16_insn(0x46fc), // mov ip, pc | |
3029 | Insn_template::thumb16_insn(0x4484), // add ip, r0 | |
3030 | Insn_template::thumb16_insn(0xbc01), // pop {r0} | |
3031 | Insn_template::thumb16_insn(0x4760), // bx ip | |
3032 | Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4), | |
3033 | // dcd R_ARM_REL32(X) | |
3034 | }; | |
3035 | ||
3036 | // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not | |
3037 | // allowed. | |
3038 | static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] = | |
3039 | { | |
3040 | Insn_template::thumb16_insn(0x4778), // bx pc | |
3041 | Insn_template::thumb16_insn(0x46c0), // nop | |
3042 | Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4] | |
3043 | Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip | |
3044 | Insn_template::arm_insn(0xe12fff1c), // bx ip | |
3045 | Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0), | |
3046 | // dcd R_ARM_REL32(X) | |
3047 | }; | |
3048 | ||
3049 | // Cortex-A8 erratum-workaround stubs. | |
3050 | ||
3051 | // Stub used for conditional branches (which may be beyond +/-1MB away, | |
3052 | // so we can't use a conditional branch to reach this stub). | |
3053 | ||
3054 | // original code: | |
3055 | // | |
3056 | // b<cond> X | |
3057 | // after: | |
3058 | // | |
3059 | static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] = | |
3060 | { | |
3061 | Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true | |
3062 | Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after | |
3063 | Insn_template::thumb32_b_insn(0xf000b800, -4) // true: | |
3064 | // b.w X | |
3065 | }; | |
3066 | ||
3067 | // Stub used for b.w and bl.w instructions. | |
3068 | ||
3069 | static const Insn_template elf32_arm_stub_a8_veneer_b[] = | |
3070 | { | |
3071 | Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest | |
3072 | }; | |
3073 | ||
3074 | static const Insn_template elf32_arm_stub_a8_veneer_bl[] = | |
3075 | { | |
3076 | Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest | |
3077 | }; | |
3078 | ||
3079 | // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w | |
3080 | // instruction (which switches to ARM mode) to point to this stub. Jump to | |
3081 | // the real destination using an ARM-mode branch. | |
3082 | const Insn_template elf32_arm_stub_a8_veneer_blx[] = | |
3083 | { | |
3084 | Insn_template::arm_rel_insn(0xea000000, -8) // b dest | |
3085 | }; | |
3086 | ||
3087 | // Fill in the stub template look-up table. Stub templates are constructed | |
3088 | // per instance of Stub_factory for fast look-up without locking | |
3089 | // in a thread-enabled environment. | |
3090 | ||
3091 | this->stub_templates_[arm_stub_none] = | |
3092 | new Stub_template(arm_stub_none, NULL, 0); | |
3093 | ||
3094 | #define DEF_STUB(x) \ | |
3095 | do \ | |
3096 | { \ | |
3097 | size_t array_size \ | |
3098 | = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \ | |
3099 | Stub_type type = arm_stub_##x; \ | |
3100 | this->stub_templates_[type] = \ | |
3101 | new Stub_template(type, elf32_arm_stub_##x, array_size); \ | |
3102 | } \ | |
3103 | while (0); | |
3104 | ||
3105 | DEF_STUBS | |
3106 | #undef DEF_STUB | |
3107 | } | |
3108 | ||
56ee5e00 DK |
3109 | // Stub_table methods. |
3110 | ||
3111 | // Add a STUB with using KEY. Caller is reponsible for avoid adding | |
3112 | // if already a STUB with the same key has been added. | |
3113 | ||
3114 | template<bool big_endian> | |
3115 | void | |
3116 | Stub_table<big_endian>::add_reloc_stub( | |
3117 | Reloc_stub* stub, | |
3118 | const Reloc_stub::Key& key) | |
3119 | { | |
3120 | const Stub_template* stub_template = stub->stub_template(); | |
3121 | gold_assert(stub_template->type() == key.stub_type()); | |
3122 | this->reloc_stubs_[key] = stub; | |
3123 | if (this->addralign_ < stub_template->alignment()) | |
3124 | this->addralign_ = stub_template->alignment(); | |
3125 | this->has_been_changed_ = true; | |
3126 | } | |
3127 | ||
3128 | template<bool big_endian> | |
3129 | void | |
3130 | Stub_table<big_endian>::relocate_stubs( | |
3131 | const Relocate_info<32, big_endian>* relinfo, | |
3132 | Target_arm<big_endian>* arm_target, | |
3133 | Output_section* output_section, | |
3134 | unsigned char* view, | |
3135 | Arm_address address, | |
3136 | section_size_type view_size) | |
3137 | { | |
3138 | // If we are passed a view bigger than the stub table's. we need to | |
3139 | // adjust the view. | |
3140 | gold_assert(address == this->address() | |
3141 | && (view_size | |
3142 | == static_cast<section_size_type>(this->data_size()))); | |
3143 | ||
3144 | for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin(); | |
3145 | p != this->reloc_stubs_.end(); | |
3146 | ++p) | |
3147 | { | |
3148 | Reloc_stub* stub = p->second; | |
3149 | const Stub_template* stub_template = stub->stub_template(); | |
3150 | if (stub_template->reloc_count() != 0) | |
3151 | { | |
3152 | // Adjust view to cover the stub only. | |
3153 | section_size_type offset = stub->offset(); | |
3154 | section_size_type stub_size = stub_template->size(); | |
3155 | gold_assert(offset + stub_size <= view_size); | |
3156 | ||
3157 | arm_target->relocate_stub(stub, relinfo, output_section, | |
3158 | view + offset, address + offset, | |
3159 | stub_size); | |
3160 | } | |
3161 | } | |
3162 | } | |
3163 | ||
3164 | // Reset address and file offset. | |
3165 | ||
3166 | template<bool big_endian> | |
3167 | void | |
3168 | Stub_table<big_endian>::do_reset_address_and_file_offset() | |
3169 | { | |
3170 | off_t off = 0; | |
3171 | uint64_t max_addralign = 1; | |
3172 | for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin(); | |
3173 | p != this->reloc_stubs_.end(); | |
3174 | ++p) | |
3175 | { | |
3176 | Reloc_stub* stub = p->second; | |
3177 | const Stub_template* stub_template = stub->stub_template(); | |
3178 | uint64_t stub_addralign = stub_template->alignment(); | |
3179 | max_addralign = std::max(max_addralign, stub_addralign); | |
3180 | off = align_address(off, stub_addralign); | |
3181 | stub->set_offset(off); | |
3182 | stub->reset_destination_address(); | |
3183 | off += stub_template->size(); | |
3184 | } | |
3185 | ||
3186 | this->addralign_ = max_addralign; | |
3187 | this->set_current_data_size_for_child(off); | |
3188 | } | |
3189 | ||
3190 | // Write out the stubs to file. | |
3191 | ||
3192 | template<bool big_endian> | |
3193 | void | |
3194 | Stub_table<big_endian>::do_write(Output_file* of) | |
3195 | { | |
3196 | off_t offset = this->offset(); | |
3197 | const section_size_type oview_size = | |
3198 | convert_to_section_size_type(this->data_size()); | |
3199 | unsigned char* const oview = of->get_output_view(offset, oview_size); | |
3200 | ||
3201 | for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin(); | |
3202 | p != this->reloc_stubs_.end(); | |
3203 | ++p) | |
3204 | { | |
3205 | Reloc_stub* stub = p->second; | |
3206 | Arm_address address = this->address() + stub->offset(); | |
3207 | gold_assert(address | |
3208 | == align_address(address, | |
3209 | stub->stub_template()->alignment())); | |
3210 | stub->write(oview + stub->offset(), stub->stub_template()->size(), | |
3211 | big_endian); | |
3212 | } | |
3213 | of->write_output_view(this->offset(), oview_size, oview); | |
3214 | } | |
3215 | ||
10ad9fe5 DK |
3216 | // Arm_input_section methods. |
3217 | ||
3218 | // Initialize an Arm_input_section. | |
3219 | ||
3220 | template<bool big_endian> | |
3221 | void | |
3222 | Arm_input_section<big_endian>::init() | |
3223 | { | |
3224 | Relobj* relobj = this->relobj(); | |
3225 | unsigned int shndx = this->shndx(); | |
3226 | ||
3227 | // Cache these to speed up size and alignment queries. It is too slow | |
3228 | // to call section_addraglin and section_size every time. | |
3229 | this->original_addralign_ = relobj->section_addralign(shndx); | |
3230 | this->original_size_ = relobj->section_size(shndx); | |
3231 | ||
3232 | // We want to make this look like the original input section after | |
3233 | // output sections are finalized. | |
3234 | Output_section* os = relobj->output_section(shndx); | |
3235 | off_t offset = relobj->output_section_offset(shndx); | |
3236 | gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx)); | |
3237 | this->set_address(os->address() + offset); | |
3238 | this->set_file_offset(os->offset() + offset); | |
3239 | ||
3240 | this->set_current_data_size(this->original_size_); | |
3241 | this->finalize_data_size(); | |
3242 | } | |
3243 | ||
3244 | template<bool big_endian> | |
3245 | void | |
3246 | Arm_input_section<big_endian>::do_write(Output_file* of) | |
3247 | { | |
3248 | // We have to write out the original section content. | |
3249 | section_size_type section_size; | |
3250 | const unsigned char* section_contents = | |
3251 | this->relobj()->section_contents(this->shndx(), §ion_size, false); | |
3252 | of->write(this->offset(), section_contents, section_size); | |
3253 | ||
3254 | // If this owns a stub table and it is not empty, write it. | |
3255 | if (this->is_stub_table_owner() && !this->stub_table_->empty()) | |
3256 | this->stub_table_->write(of); | |
3257 | } | |
3258 | ||
3259 | // Finalize data size. | |
3260 | ||
3261 | template<bool big_endian> | |
3262 | void | |
3263 | Arm_input_section<big_endian>::set_final_data_size() | |
3264 | { | |
3265 | // If this owns a stub table, finalize its data size as well. | |
3266 | if (this->is_stub_table_owner()) | |
3267 | { | |
3268 | uint64_t address = this->address(); | |
3269 | ||
3270 | // The stub table comes after the original section contents. | |
3271 | address += this->original_size_; | |
3272 | address = align_address(address, this->stub_table_->addralign()); | |
3273 | off_t offset = this->offset() + (address - this->address()); | |
3274 | this->stub_table_->set_address_and_file_offset(address, offset); | |
3275 | address += this->stub_table_->data_size(); | |
3276 | gold_assert(address == this->address() + this->current_data_size()); | |
3277 | } | |
3278 | ||
3279 | this->set_data_size(this->current_data_size()); | |
3280 | } | |
3281 | ||
3282 | // Reset address and file offset. | |
3283 | ||
3284 | template<bool big_endian> | |
3285 | void | |
3286 | Arm_input_section<big_endian>::do_reset_address_and_file_offset() | |
3287 | { | |
3288 | // Size of the original input section contents. | |
3289 | off_t off = convert_types<off_t, uint64_t>(this->original_size_); | |
3290 | ||
3291 | // If this is a stub table owner, account for the stub table size. | |
3292 | if (this->is_stub_table_owner()) | |
3293 | { | |
3294 | Stub_table<big_endian>* stub_table = this->stub_table_; | |
3295 | ||
3296 | // Reset the stub table's address and file offset. The | |
3297 | // current data size for child will be updated after that. | |
3298 | stub_table_->reset_address_and_file_offset(); | |
3299 | off = align_address(off, stub_table_->addralign()); | |
3300 | off += stub_table->current_data_size(); | |
3301 | } | |
3302 | ||
3303 | this->set_current_data_size(off); | |
3304 | } | |
3305 | ||
07f508a2 DK |
3306 | // Arm_output_section methods. |
3307 | ||
3308 | // Create a stub group for input sections from BEGIN to END. OWNER | |
3309 | // points to the input section to be the owner a new stub table. | |
3310 | ||
3311 | template<bool big_endian> | |
3312 | void | |
3313 | Arm_output_section<big_endian>::create_stub_group( | |
3314 | Input_section_list::const_iterator begin, | |
3315 | Input_section_list::const_iterator end, | |
3316 | Input_section_list::const_iterator owner, | |
3317 | Target_arm<big_endian>* target, | |
3318 | std::vector<Output_relaxed_input_section*>* new_relaxed_sections) | |
3319 | { | |
3320 | // Currently we convert ordinary input sections into relaxed sections only | |
3321 | // at this point but we may want to support creating relaxed input section | |
3322 | // very early. So we check here to see if owner is already a relaxed | |
3323 | // section. | |
3324 | ||
3325 | Arm_input_section<big_endian>* arm_input_section; | |
3326 | if (owner->is_relaxed_input_section()) | |
3327 | { | |
3328 | arm_input_section = | |
3329 | Arm_input_section<big_endian>::as_arm_input_section( | |
3330 | owner->relaxed_input_section()); | |
3331 | } | |
3332 | else | |
3333 | { | |
3334 | gold_assert(owner->is_input_section()); | |
3335 | // Create a new relaxed input section. | |
3336 | arm_input_section = | |
3337 | target->new_arm_input_section(owner->relobj(), owner->shndx()); | |
3338 | new_relaxed_sections->push_back(arm_input_section); | |
3339 | } | |
3340 | ||
3341 | // Create a stub table. | |
3342 | Stub_table<big_endian>* stub_table = | |
3343 | target->new_stub_table(arm_input_section); | |
3344 | ||
3345 | arm_input_section->set_stub_table(stub_table); | |
3346 | ||
3347 | Input_section_list::const_iterator p = begin; | |
3348 | Input_section_list::const_iterator prev_p; | |
3349 | ||
3350 | // Look for input sections or relaxed input sections in [begin ... end]. | |
3351 | do | |
3352 | { | |
3353 | if (p->is_input_section() || p->is_relaxed_input_section()) | |
3354 | { | |
3355 | // The stub table information for input sections live | |
3356 | // in their objects. | |
3357 | Arm_relobj<big_endian>* arm_relobj = | |
3358 | Arm_relobj<big_endian>::as_arm_relobj(p->relobj()); | |
3359 | arm_relobj->set_stub_table(p->shndx(), stub_table); | |
3360 | } | |
3361 | prev_p = p++; | |
3362 | } | |
3363 | while (prev_p != end); | |
3364 | } | |
3365 | ||
3366 | // Group input sections for stub generation. GROUP_SIZE is roughly the limit | |
3367 | // of stub groups. We grow a stub group by adding input section until the | |
3368 | // size is just below GROUP_SIZE. The last input section will be converted | |
3369 | // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add | |
3370 | // input section after the stub table, effectively double the group size. | |
3371 | // | |
3372 | // This is similar to the group_sections() function in elf32-arm.c but is | |
3373 | // implemented differently. | |
3374 | ||
3375 | template<bool big_endian> | |
3376 | void | |
3377 | Arm_output_section<big_endian>::group_sections( | |
3378 | section_size_type group_size, | |
3379 | bool stubs_always_after_branch, | |
3380 | Target_arm<big_endian>* target) | |
3381 | { | |
3382 | // We only care about sections containing code. | |
3383 | if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0) | |
3384 | return; | |
3385 | ||
3386 | // States for grouping. | |
3387 | typedef enum | |
3388 | { | |
3389 | // No group is being built. | |
3390 | NO_GROUP, | |
3391 | // A group is being built but the stub table is not found yet. | |
3392 | // We keep group a stub group until the size is just under GROUP_SIZE. | |
3393 | // The last input section in the group will be used as the stub table. | |
3394 | FINDING_STUB_SECTION, | |
3395 | // A group is being built and we have already found a stub table. | |
3396 | // We enter this state to grow a stub group by adding input section | |
3397 | // after the stub table. This effectively doubles the group size. | |
3398 | HAS_STUB_SECTION | |
3399 | } State; | |
3400 | ||
3401 | // Any newly created relaxed sections are stored here. | |
3402 | std::vector<Output_relaxed_input_section*> new_relaxed_sections; | |
3403 | ||
3404 | State state = NO_GROUP; | |
3405 | section_size_type off = 0; | |
3406 | section_size_type group_begin_offset = 0; | |
3407 | section_size_type group_end_offset = 0; | |
3408 | section_size_type stub_table_end_offset = 0; | |
3409 | Input_section_list::const_iterator group_begin = | |
3410 | this->input_sections().end(); | |
3411 | Input_section_list::const_iterator stub_table = | |
3412 | this->input_sections().end(); | |
3413 | Input_section_list::const_iterator group_end = this->input_sections().end(); | |
3414 | for (Input_section_list::const_iterator p = this->input_sections().begin(); | |
3415 | p != this->input_sections().end(); | |
3416 | ++p) | |
3417 | { | |
3418 | section_size_type section_begin_offset = | |
3419 | align_address(off, p->addralign()); | |
3420 | section_size_type section_end_offset = | |
3421 | section_begin_offset + p->data_size(); | |
3422 | ||
3423 | // Check to see if we should group the previously seens sections. | |
e9bbb538 | 3424 | switch (state) |
07f508a2 DK |
3425 | { |
3426 | case NO_GROUP: | |
3427 | break; | |
3428 | ||
3429 | case FINDING_STUB_SECTION: | |
3430 | // Adding this section makes the group larger than GROUP_SIZE. | |
3431 | if (section_end_offset - group_begin_offset >= group_size) | |
3432 | { | |
3433 | if (stubs_always_after_branch) | |
3434 | { | |
3435 | gold_assert(group_end != this->input_sections().end()); | |
3436 | this->create_stub_group(group_begin, group_end, group_end, | |
3437 | target, &new_relaxed_sections); | |
3438 | state = NO_GROUP; | |
3439 | } | |
3440 | else | |
3441 | { | |
3442 | // But wait, there's more! Input sections up to | |
3443 | // stub_group_size bytes after the stub table can be | |
3444 | // handled by it too. | |
3445 | state = HAS_STUB_SECTION; | |
3446 | stub_table = group_end; | |
3447 | stub_table_end_offset = group_end_offset; | |
3448 | } | |
3449 | } | |
3450 | break; | |
3451 | ||
3452 | case HAS_STUB_SECTION: | |
3453 | // Adding this section makes the post stub-section group larger | |
3454 | // than GROUP_SIZE. | |
3455 | if (section_end_offset - stub_table_end_offset >= group_size) | |
3456 | { | |
3457 | gold_assert(group_end != this->input_sections().end()); | |
3458 | this->create_stub_group(group_begin, group_end, stub_table, | |
3459 | target, &new_relaxed_sections); | |
3460 | state = NO_GROUP; | |
3461 | } | |
3462 | break; | |
3463 | ||
3464 | default: | |
3465 | gold_unreachable(); | |
3466 | } | |
3467 | ||
3468 | // If we see an input section and currently there is no group, start | |
3469 | // a new one. Skip any empty sections. | |
3470 | if ((p->is_input_section() || p->is_relaxed_input_section()) | |
3471 | && (p->relobj()->section_size(p->shndx()) != 0)) | |
3472 | { | |
3473 | if (state == NO_GROUP) | |
3474 | { | |
3475 | state = FINDING_STUB_SECTION; | |
3476 | group_begin = p; | |
3477 | group_begin_offset = section_begin_offset; | |
3478 | } | |
3479 | ||
3480 | // Keep track of the last input section seen. | |
3481 | group_end = p; | |
3482 | group_end_offset = section_end_offset; | |
3483 | } | |
3484 | ||
3485 | off = section_end_offset; | |
3486 | } | |
3487 | ||
3488 | // Create a stub group for any ungrouped sections. | |
3489 | if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION) | |
3490 | { | |
3491 | gold_assert(group_end != this->input_sections().end()); | |
3492 | this->create_stub_group(group_begin, group_end, | |
3493 | (state == FINDING_STUB_SECTION | |
3494 | ? group_end | |
3495 | : stub_table), | |
3496 | target, &new_relaxed_sections); | |
3497 | } | |
3498 | ||
3499 | // Convert input section into relaxed input section in a batch. | |
3500 | if (!new_relaxed_sections.empty()) | |
3501 | this->convert_input_sections_to_relaxed_sections(new_relaxed_sections); | |
3502 | ||
3503 | // Update the section offsets | |
3504 | for (size_t i = 0; i < new_relaxed_sections.size(); ++i) | |
3505 | { | |
3506 | Arm_relobj<big_endian>* arm_relobj = | |
3507 | Arm_relobj<big_endian>::as_arm_relobj( | |
3508 | new_relaxed_sections[i]->relobj()); | |
3509 | unsigned int shndx = new_relaxed_sections[i]->shndx(); | |
3510 | // Tell Arm_relobj that this input section is converted. | |
3511 | arm_relobj->convert_input_section_to_relaxed_section(shndx); | |
3512 | } | |
3513 | } | |
3514 | ||
8ffa3667 DK |
3515 | // Arm_relobj methods. |
3516 | ||
3517 | // Scan relocations for stub generation. | |
3518 | ||
3519 | template<bool big_endian> | |
3520 | void | |
3521 | Arm_relobj<big_endian>::scan_sections_for_stubs( | |
3522 | Target_arm<big_endian>* arm_target, | |
3523 | const Symbol_table* symtab, | |
3524 | const Layout* layout) | |
3525 | { | |
3526 | unsigned int shnum = this->shnum(); | |
3527 | const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size; | |
3528 | ||
3529 | // Read the section headers. | |
3530 | const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(), | |
3531 | shnum * shdr_size, | |
3532 | true, true); | |
3533 | ||
3534 | // To speed up processing, we set up hash tables for fast lookup of | |
3535 | // input offsets to output addresses. | |
3536 | this->initialize_input_to_output_maps(); | |
3537 | ||
3538 | const Relobj::Output_sections& out_sections(this->output_sections()); | |
3539 | ||
3540 | Relocate_info<32, big_endian> relinfo; | |
8ffa3667 DK |
3541 | relinfo.symtab = symtab; |
3542 | relinfo.layout = layout; | |
3543 | relinfo.object = this; | |
3544 | ||
3545 | const unsigned char* p = pshdrs + shdr_size; | |
3546 | for (unsigned int i = 1; i < shnum; ++i, p += shdr_size) | |
3547 | { | |
3548 | typename elfcpp::Shdr<32, big_endian> shdr(p); | |
3549 | ||
3550 | unsigned int sh_type = shdr.get_sh_type(); | |
3551 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) | |
3552 | continue; | |
3553 | ||
3554 | off_t sh_size = shdr.get_sh_size(); | |
3555 | if (sh_size == 0) | |
3556 | continue; | |
3557 | ||
3558 | unsigned int index = this->adjust_shndx(shdr.get_sh_info()); | |
3559 | if (index >= this->shnum()) | |
3560 | { | |
3561 | // Ignore reloc section with bad info. This error will be | |
3562 | // reported in the final link. | |
3563 | continue; | |
3564 | } | |
3565 | ||
3566 | Output_section* os = out_sections[index]; | |
3567 | if (os == NULL) | |
3568 | { | |
3569 | // This relocation section is against a section which we | |
3570 | // discarded. | |
3571 | continue; | |
3572 | } | |
3573 | Arm_address output_offset = this->get_output_section_offset(index); | |
3574 | ||
3575 | if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx()) | |
3576 | { | |
3577 | // Ignore reloc section with unexpected symbol table. The | |
3578 | // error will be reported in the final link. | |
3579 | continue; | |
3580 | } | |
3581 | ||
3582 | const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(), | |
3583 | sh_size, true, false); | |
3584 | ||
3585 | unsigned int reloc_size; | |
3586 | if (sh_type == elfcpp::SHT_REL) | |
3587 | reloc_size = elfcpp::Elf_sizes<32>::rel_size; | |
3588 | else | |
3589 | reloc_size = elfcpp::Elf_sizes<32>::rela_size; | |
3590 | ||
3591 | if (reloc_size != shdr.get_sh_entsize()) | |
3592 | { | |
3593 | // Ignore reloc section with unexpected entsize. The error | |
3594 | // will be reported in the final link. | |
3595 | continue; | |
3596 | } | |
3597 | ||
3598 | size_t reloc_count = sh_size / reloc_size; | |
3599 | if (static_cast<off_t>(reloc_count * reloc_size) != sh_size) | |
3600 | { | |
3601 | // Ignore reloc section with uneven size. The error will be | |
3602 | // reported in the final link. | |
3603 | continue; | |
3604 | } | |
3605 | ||
3606 | gold_assert(output_offset != invalid_address | |
3607 | || this->relocs_must_follow_section_writes()); | |
3608 | ||
3609 | // Get the section contents. This does work for the case in which | |
3610 | // we modify the contents of an input section. We need to pass the | |
3611 | // output view under such circumstances. | |
3612 | section_size_type input_view_size = 0; | |
3613 | const unsigned char* input_view = | |
3614 | this->section_contents(index, &input_view_size, false); | |
3615 | ||
3616 | relinfo.reloc_shndx = i; | |
3617 | relinfo.data_shndx = index; | |
3618 | arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs, | |
3619 | reloc_count, os, | |
3620 | output_offset == invalid_address, | |
3621 | input_view, | |
3622 | os->address(), | |
3623 | input_view_size); | |
3624 | } | |
3625 | ||
3626 | // After we've done the relocations, we release the hash tables, | |
3627 | // since we no longer need them. | |
3628 | this->free_input_to_output_maps(); | |
3629 | } | |
3630 | ||
3631 | // Count the local symbols. The ARM backend needs to know if a symbol | |
3632 | // is a THUMB function or not. For global symbols, it is easy because | |
3633 | // the Symbol object keeps the ELF symbol type. For local symbol it is | |
3634 | // harder because we cannot access this information. So we override the | |
3635 | // do_count_local_symbol in parent and scan local symbols to mark | |
3636 | // THUMB functions. This is not the most efficient way but I do not want to | |
3637 | // slow down other ports by calling a per symbol targer hook inside | |
3638 | // Sized_relobj<size, big_endian>::do_count_local_symbols. | |
3639 | ||
3640 | template<bool big_endian> | |
3641 | void | |
3642 | Arm_relobj<big_endian>::do_count_local_symbols( | |
3643 | Stringpool_template<char>* pool, | |
3644 | Stringpool_template<char>* dynpool) | |
3645 | { | |
3646 | // We need to fix-up the values of any local symbols whose type are | |
3647 | // STT_ARM_TFUNC. | |
3648 | ||
3649 | // Ask parent to count the local symbols. | |
3650 | Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool); | |
3651 | const unsigned int loccount = this->local_symbol_count(); | |
3652 | if (loccount == 0) | |
3653 | return; | |
3654 | ||
3655 | // Intialize the thumb function bit-vector. | |
3656 | std::vector<bool> empty_vector(loccount, false); | |
3657 | this->local_symbol_is_thumb_function_.swap(empty_vector); | |
3658 | ||
3659 | // Read the symbol table section header. | |
3660 | const unsigned int symtab_shndx = this->symtab_shndx(); | |
3661 | elfcpp::Shdr<32, big_endian> | |
3662 | symtabshdr(this, this->elf_file()->section_header(symtab_shndx)); | |
3663 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); | |
3664 | ||
3665 | // Read the local symbols. | |
3666 | const int sym_size =elfcpp::Elf_sizes<32>::sym_size; | |
3667 | gold_assert(loccount == symtabshdr.get_sh_info()); | |
3668 | off_t locsize = loccount * sym_size; | |
3669 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), | |
3670 | locsize, true, true); | |
3671 | ||
3672 | // Loop over the local symbols and mark any local symbols pointing | |
3673 | // to THUMB functions. | |
3674 | ||
3675 | // Skip the first dummy symbol. | |
3676 | psyms += sym_size; | |
3677 | typename Sized_relobj<32, big_endian>::Local_values* plocal_values = | |
3678 | this->local_values(); | |
3679 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) | |
3680 | { | |
3681 | elfcpp::Sym<32, big_endian> sym(psyms); | |
3682 | elfcpp::STT st_type = sym.get_st_type(); | |
3683 | Symbol_value<32>& lv((*plocal_values)[i]); | |
3684 | Arm_address input_value = lv.input_value(); | |
3685 | ||
3686 | if (st_type == elfcpp::STT_ARM_TFUNC | |
3687 | || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0))) | |
3688 | { | |
3689 | // This is a THUMB function. Mark this and canonicalize the | |
3690 | // symbol value by setting LSB. | |
3691 | this->local_symbol_is_thumb_function_[i] = true; | |
3692 | if ((input_value & 1) == 0) | |
3693 | lv.set_input_value(input_value | 1); | |
3694 | } | |
3695 | } | |
3696 | } | |
3697 | ||
3698 | // Relocate sections. | |
3699 | template<bool big_endian> | |
3700 | void | |
3701 | Arm_relobj<big_endian>::do_relocate_sections( | |
8ffa3667 DK |
3702 | const Symbol_table* symtab, |
3703 | const Layout* layout, | |
3704 | const unsigned char* pshdrs, | |
3705 | typename Sized_relobj<32, big_endian>::Views* pviews) | |
3706 | { | |
3707 | // Call parent to relocate sections. | |
43d12afe DK |
3708 | Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs, |
3709 | pviews); | |
8ffa3667 DK |
3710 | |
3711 | // We do not generate stubs if doing a relocatable link. | |
3712 | if (parameters->options().relocatable()) | |
3713 | return; | |
3714 | ||
3715 | // Relocate stub tables. | |
3716 | unsigned int shnum = this->shnum(); | |
3717 | ||
3718 | Target_arm<big_endian>* arm_target = | |
3719 | Target_arm<big_endian>::default_target(); | |
3720 | ||
3721 | Relocate_info<32, big_endian> relinfo; | |
8ffa3667 DK |
3722 | relinfo.symtab = symtab; |
3723 | relinfo.layout = layout; | |
3724 | relinfo.object = this; | |
3725 | ||
3726 | for (unsigned int i = 1; i < shnum; ++i) | |
3727 | { | |
3728 | Arm_input_section<big_endian>* arm_input_section = | |
3729 | arm_target->find_arm_input_section(this, i); | |
3730 | ||
3731 | if (arm_input_section == NULL | |
3732 | || !arm_input_section->is_stub_table_owner() | |
3733 | || arm_input_section->stub_table()->empty()) | |
3734 | continue; | |
3735 | ||
3736 | // We cannot discard a section if it owns a stub table. | |
3737 | Output_section* os = this->output_section(i); | |
3738 | gold_assert(os != NULL); | |
3739 | ||
3740 | relinfo.reloc_shndx = elfcpp::SHN_UNDEF; | |
3741 | relinfo.reloc_shdr = NULL; | |
3742 | relinfo.data_shndx = i; | |
3743 | relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size; | |
3744 | ||
3745 | gold_assert((*pviews)[i].view != NULL); | |
3746 | ||
3747 | // We are passed the output section view. Adjust it to cover the | |
3748 | // stub table only. | |
3749 | Stub_table<big_endian>* stub_table = arm_input_section->stub_table(); | |
3750 | gold_assert((stub_table->address() >= (*pviews)[i].address) | |
3751 | && ((stub_table->address() + stub_table->data_size()) | |
3752 | <= (*pviews)[i].address + (*pviews)[i].view_size)); | |
3753 | ||
3754 | off_t offset = stub_table->address() - (*pviews)[i].address; | |
3755 | unsigned char* view = (*pviews)[i].view + offset; | |
3756 | Arm_address address = stub_table->address(); | |
3757 | section_size_type view_size = stub_table->data_size(); | |
3758 | ||
3759 | stub_table->relocate_stubs(&relinfo, arm_target, os, view, address, | |
3760 | view_size); | |
3761 | } | |
3762 | } | |
3763 | ||
d5b40221 DK |
3764 | // Read the symbol information. |
3765 | ||
3766 | template<bool big_endian> | |
3767 | void | |
3768 | Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd) | |
3769 | { | |
3770 | // Call parent class to read symbol information. | |
3771 | Sized_relobj<32, big_endian>::do_read_symbols(sd); | |
3772 | ||
3773 | // Read processor-specific flags in ELF file header. | |
3774 | const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset, | |
3775 | elfcpp::Elf_sizes<32>::ehdr_size, | |
3776 | true, false); | |
3777 | elfcpp::Ehdr<32, big_endian> ehdr(pehdr); | |
3778 | this->processor_specific_flags_ = ehdr.get_e_flags(); | |
3779 | } | |
3780 | ||
3781 | // Arm_dynobj methods. | |
3782 | ||
3783 | // Read the symbol information. | |
3784 | ||
3785 | template<bool big_endian> | |
3786 | void | |
3787 | Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd) | |
3788 | { | |
3789 | // Call parent class to read symbol information. | |
3790 | Sized_dynobj<32, big_endian>::do_read_symbols(sd); | |
3791 | ||
3792 | // Read processor-specific flags in ELF file header. | |
3793 | const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset, | |
3794 | elfcpp::Elf_sizes<32>::ehdr_size, | |
3795 | true, false); | |
3796 | elfcpp::Ehdr<32, big_endian> ehdr(pehdr); | |
3797 | this->processor_specific_flags_ = ehdr.get_e_flags(); | |
3798 | } | |
3799 | ||
e9bbb538 DK |
3800 | // Stub_addend_reader methods. |
3801 | ||
3802 | // Read the addend of a REL relocation of type R_TYPE at VIEW. | |
3803 | ||
3804 | template<bool big_endian> | |
3805 | elfcpp::Elf_types<32>::Elf_Swxword | |
3806 | Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()( | |
3807 | unsigned int r_type, | |
3808 | const unsigned char* view, | |
3809 | const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const | |
3810 | { | |
3811 | switch (r_type) | |
3812 | { | |
3813 | case elfcpp::R_ARM_CALL: | |
3814 | case elfcpp::R_ARM_JUMP24: | |
3815 | case elfcpp::R_ARM_PLT32: | |
3816 | { | |
3817 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
3818 | const Valtype* wv = reinterpret_cast<const Valtype*>(view); | |
3819 | Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); | |
3820 | return utils::sign_extend<26>(val << 2); | |
3821 | } | |
3822 | ||
3823 | case elfcpp::R_ARM_THM_CALL: | |
3824 | case elfcpp::R_ARM_THM_JUMP24: | |
3825 | case elfcpp::R_ARM_THM_XPC22: | |
3826 | { | |
3827 | // Fetch the addend. We use the Thumb-2 encoding (backwards | |
3828 | // compatible with Thumb-1) involving the J1 and J2 bits. | |
3829 | typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; | |
3830 | const Valtype* wv = reinterpret_cast<const Valtype*>(view); | |
3831 | Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv); | |
3832 | Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1); | |
3833 | ||
3834 | uint32_t s = (upper_insn & (1 << 10)) >> 10; | |
3835 | uint32_t upper = upper_insn & 0x3ff; | |
3836 | uint32_t lower = lower_insn & 0x7ff; | |
3837 | uint32_t j1 = (lower_insn & (1 << 13)) >> 13; | |
3838 | uint32_t j2 = (lower_insn & (1 << 11)) >> 11; | |
3839 | uint32_t i1 = j1 ^ s ? 0 : 1; | |
3840 | uint32_t i2 = j2 ^ s ? 0 : 1; | |
3841 | ||
3842 | return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22) | |
3843 | | (upper << 12) | (lower << 1)); | |
3844 | } | |
3845 | ||
3846 | case elfcpp::R_ARM_THM_JUMP19: | |
3847 | { | |
3848 | typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; | |
3849 | const Valtype* wv = reinterpret_cast<const Valtype*>(view); | |
3850 | Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv); | |
3851 | Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1); | |
3852 | ||
3853 | // Reconstruct the top three bits and squish the two 11 bit pieces | |
3854 | // together. | |
3855 | uint32_t S = (upper_insn & 0x0400) >> 10; | |
3856 | uint32_t J1 = (lower_insn & 0x2000) >> 13; | |
3857 | uint32_t J2 = (lower_insn & 0x0800) >> 11; | |
3858 | uint32_t upper = | |
3859 | (S << 8) | (J2 << 7) | (J1 << 6) | (upper_insn & 0x003f); | |
3860 | uint32_t lower = (lower_insn & 0x07ff); | |
3861 | return utils::sign_extend<23>((upper << 12) | (lower << 1)); | |
3862 | } | |
3863 | ||
3864 | default: | |
3865 | gold_unreachable(); | |
3866 | } | |
3867 | } | |
3868 | ||
94cdfcff DK |
3869 | // A class to handle the PLT data. |
3870 | ||
3871 | template<bool big_endian> | |
3872 | class Output_data_plt_arm : public Output_section_data | |
3873 | { | |
3874 | public: | |
3875 | typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian> | |
3876 | Reloc_section; | |
3877 | ||
3878 | Output_data_plt_arm(Layout*, Output_data_space*); | |
3879 | ||
3880 | // Add an entry to the PLT. | |
3881 | void | |
3882 | add_entry(Symbol* gsym); | |
3883 | ||
3884 | // Return the .rel.plt section data. | |
3885 | const Reloc_section* | |
3886 | rel_plt() const | |
3887 | { return this->rel_; } | |
3888 | ||
3889 | protected: | |
3890 | void | |
3891 | do_adjust_output_section(Output_section* os); | |
3892 | ||
3893 | // Write to a map file. | |
3894 | void | |
3895 | do_print_to_mapfile(Mapfile* mapfile) const | |
3896 | { mapfile->print_output_data(this, _("** PLT")); } | |
3897 | ||
3898 | private: | |
3899 | // Template for the first PLT entry. | |
3900 | static const uint32_t first_plt_entry[5]; | |
3901 | ||
3902 | // Template for subsequent PLT entries. | |
3903 | static const uint32_t plt_entry[3]; | |
3904 | ||
3905 | // Set the final size. | |
3906 | void | |
3907 | set_final_data_size() | |
3908 | { | |
3909 | this->set_data_size(sizeof(first_plt_entry) | |
3910 | + this->count_ * sizeof(plt_entry)); | |
3911 | } | |
3912 | ||
3913 | // Write out the PLT data. | |
3914 | void | |
3915 | do_write(Output_file*); | |
3916 | ||
3917 | // The reloc section. | |
3918 | Reloc_section* rel_; | |
3919 | // The .got.plt section. | |
3920 | Output_data_space* got_plt_; | |
3921 | // The number of PLT entries. | |
3922 | unsigned int count_; | |
3923 | }; | |
3924 | ||
3925 | // Create the PLT section. The ordinary .got section is an argument, | |
3926 | // since we need to refer to the start. We also create our own .got | |
3927 | // section just for PLT entries. | |
3928 | ||
3929 | template<bool big_endian> | |
3930 | Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout, | |
3931 | Output_data_space* got_plt) | |
3932 | : Output_section_data(4), got_plt_(got_plt), count_(0) | |
3933 | { | |
3934 | this->rel_ = new Reloc_section(false); | |
3935 | layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL, | |
f5c870d2 | 3936 | elfcpp::SHF_ALLOC, this->rel_, true); |
94cdfcff DK |
3937 | } |
3938 | ||
3939 | template<bool big_endian> | |
3940 | void | |
3941 | Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os) | |
3942 | { | |
3943 | os->set_entsize(0); | |
3944 | } | |
3945 | ||
3946 | // Add an entry to the PLT. | |
3947 | ||
3948 | template<bool big_endian> | |
3949 | void | |
3950 | Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym) | |
3951 | { | |
3952 | gold_assert(!gsym->has_plt_offset()); | |
3953 | ||
3954 | // Note that when setting the PLT offset we skip the initial | |
3955 | // reserved PLT entry. | |
3956 | gsym->set_plt_offset((this->count_) * sizeof(plt_entry) | |
3957 | + sizeof(first_plt_entry)); | |
3958 | ||
3959 | ++this->count_; | |
3960 | ||
3961 | section_offset_type got_offset = this->got_plt_->current_data_size(); | |
3962 | ||
3963 | // Every PLT entry needs a GOT entry which points back to the PLT | |
3964 | // entry (this will be changed by the dynamic linker, normally | |
3965 | // lazily when the function is called). | |
3966 | this->got_plt_->set_current_data_size(got_offset + 4); | |
3967 | ||
3968 | // Every PLT entry needs a reloc. | |
3969 | gsym->set_needs_dynsym_entry(); | |
3970 | this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_, | |
3971 | got_offset); | |
3972 | ||
3973 | // Note that we don't need to save the symbol. The contents of the | |
3974 | // PLT are independent of which symbols are used. The symbols only | |
3975 | // appear in the relocations. | |
3976 | } | |
3977 | ||
3978 | // ARM PLTs. | |
3979 | // FIXME: This is not very flexible. Right now this has only been tested | |
3980 | // on armv5te. If we are to support additional architecture features like | |
3981 | // Thumb-2 or BE8, we need to make this more flexible like GNU ld. | |
3982 | ||
3983 | // The first entry in the PLT. | |
3984 | template<bool big_endian> | |
3985 | const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] = | |
3986 | { | |
3987 | 0xe52de004, // str lr, [sp, #-4]! | |
3988 | 0xe59fe004, // ldr lr, [pc, #4] | |
3989 | 0xe08fe00e, // add lr, pc, lr | |
3990 | 0xe5bef008, // ldr pc, [lr, #8]! | |
3991 | 0x00000000, // &GOT[0] - . | |
3992 | }; | |
3993 | ||
3994 | // Subsequent entries in the PLT. | |
3995 | ||
3996 | template<bool big_endian> | |
3997 | const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] = | |
3998 | { | |
3999 | 0xe28fc600, // add ip, pc, #0xNN00000 | |
4000 | 0xe28cca00, // add ip, ip, #0xNN000 | |
4001 | 0xe5bcf000, // ldr pc, [ip, #0xNNN]! | |
4002 | }; | |
4003 | ||
4004 | // Write out the PLT. This uses the hand-coded instructions above, | |
4005 | // and adjusts them as needed. This is all specified by the arm ELF | |
4006 | // Processor Supplement. | |
4007 | ||
4008 | template<bool big_endian> | |
4009 | void | |
4010 | Output_data_plt_arm<big_endian>::do_write(Output_file* of) | |
4011 | { | |
4012 | const off_t offset = this->offset(); | |
4013 | const section_size_type oview_size = | |
4014 | convert_to_section_size_type(this->data_size()); | |
4015 | unsigned char* const oview = of->get_output_view(offset, oview_size); | |
4016 | ||
4017 | const off_t got_file_offset = this->got_plt_->offset(); | |
4018 | const section_size_type got_size = | |
4019 | convert_to_section_size_type(this->got_plt_->data_size()); | |
4020 | unsigned char* const got_view = of->get_output_view(got_file_offset, | |
4021 | got_size); | |
4022 | unsigned char* pov = oview; | |
4023 | ||
ebabffbd DK |
4024 | Arm_address plt_address = this->address(); |
4025 | Arm_address got_address = this->got_plt_->address(); | |
94cdfcff DK |
4026 | |
4027 | // Write first PLT entry. All but the last word are constants. | |
4028 | const size_t num_first_plt_words = (sizeof(first_plt_entry) | |
4029 | / sizeof(plt_entry[0])); | |
4030 | for (size_t i = 0; i < num_first_plt_words - 1; i++) | |
4031 | elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]); | |
4032 | // Last word in first PLT entry is &GOT[0] - . | |
4033 | elfcpp::Swap<32, big_endian>::writeval(pov + 16, | |
4034 | got_address - (plt_address + 16)); | |
4035 | pov += sizeof(first_plt_entry); | |
4036 | ||
4037 | unsigned char* got_pov = got_view; | |
4038 | ||
4039 | memset(got_pov, 0, 12); | |
4040 | got_pov += 12; | |
4041 | ||
4042 | const int rel_size = elfcpp::Elf_sizes<32>::rel_size; | |
4043 | unsigned int plt_offset = sizeof(first_plt_entry); | |
4044 | unsigned int plt_rel_offset = 0; | |
4045 | unsigned int got_offset = 12; | |
4046 | const unsigned int count = this->count_; | |
4047 | for (unsigned int i = 0; | |
4048 | i < count; | |
4049 | ++i, | |
4050 | pov += sizeof(plt_entry), | |
4051 | got_pov += 4, | |
4052 | plt_offset += sizeof(plt_entry), | |
4053 | plt_rel_offset += rel_size, | |
4054 | got_offset += 4) | |
4055 | { | |
4056 | // Set and adjust the PLT entry itself. | |
4057 | int32_t offset = ((got_address + got_offset) | |
4058 | - (plt_address + plt_offset + 8)); | |
4059 | ||
4060 | gold_assert(offset >= 0 && offset < 0x0fffffff); | |
4061 | uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff); | |
4062 | elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0); | |
4063 | uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff); | |
4064 | elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1); | |
4065 | uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff); | |
4066 | elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2); | |
4067 | ||
4068 | // Set the entry in the GOT. | |
4069 | elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address); | |
4070 | } | |
4071 | ||
4072 | gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); | |
4073 | gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); | |
4074 | ||
4075 | of->write_output_view(offset, oview_size, oview); | |
4076 | of->write_output_view(got_file_offset, got_size, got_view); | |
4077 | } | |
4078 | ||
4079 | // Create a PLT entry for a global symbol. | |
4080 | ||
4081 | template<bool big_endian> | |
4082 | void | |
4083 | Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout, | |
4084 | Symbol* gsym) | |
4085 | { | |
4086 | if (gsym->has_plt_offset()) | |
4087 | return; | |
4088 | ||
4089 | if (this->plt_ == NULL) | |
4090 | { | |
4091 | // Create the GOT sections first. | |
4092 | this->got_section(symtab, layout); | |
4093 | ||
4094 | this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_); | |
4095 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, | |
4096 | (elfcpp::SHF_ALLOC | |
4097 | | elfcpp::SHF_EXECINSTR), | |
f5c870d2 | 4098 | this->plt_, false); |
94cdfcff DK |
4099 | } |
4100 | this->plt_->add_entry(gsym); | |
4101 | } | |
4102 | ||
4a657b0d DK |
4103 | // Report an unsupported relocation against a local symbol. |
4104 | ||
4105 | template<bool big_endian> | |
4106 | void | |
4107 | Target_arm<big_endian>::Scan::unsupported_reloc_local( | |
4108 | Sized_relobj<32, big_endian>* object, | |
4109 | unsigned int r_type) | |
4110 | { | |
4111 | gold_error(_("%s: unsupported reloc %u against local symbol"), | |
4112 | object->name().c_str(), r_type); | |
4113 | } | |
4114 | ||
bec53400 DK |
4115 | // We are about to emit a dynamic relocation of type R_TYPE. If the |
4116 | // dynamic linker does not support it, issue an error. The GNU linker | |
4117 | // only issues a non-PIC error for an allocated read-only section. | |
4118 | // Here we know the section is allocated, but we don't know that it is | |
4119 | // read-only. But we check for all the relocation types which the | |
4120 | // glibc dynamic linker supports, so it seems appropriate to issue an | |
4121 | // error even if the section is not read-only. | |
4122 | ||
4123 | template<bool big_endian> | |
4124 | void | |
4125 | Target_arm<big_endian>::Scan::check_non_pic(Relobj* object, | |
4126 | unsigned int r_type) | |
4127 | { | |
4128 | switch (r_type) | |
4129 | { | |
4130 | // These are the relocation types supported by glibc for ARM. | |
4131 | case elfcpp::R_ARM_RELATIVE: | |
4132 | case elfcpp::R_ARM_COPY: | |
4133 | case elfcpp::R_ARM_GLOB_DAT: | |
4134 | case elfcpp::R_ARM_JUMP_SLOT: | |
4135 | case elfcpp::R_ARM_ABS32: | |
be8fcb75 | 4136 | case elfcpp::R_ARM_ABS32_NOI: |
bec53400 DK |
4137 | case elfcpp::R_ARM_PC24: |
4138 | // FIXME: The following 3 types are not supported by Android's dynamic | |
4139 | // linker. | |
4140 | case elfcpp::R_ARM_TLS_DTPMOD32: | |
4141 | case elfcpp::R_ARM_TLS_DTPOFF32: | |
4142 | case elfcpp::R_ARM_TLS_TPOFF32: | |
4143 | return; | |
4144 | ||
4145 | default: | |
4146 | // This prevents us from issuing more than one error per reloc | |
4147 | // section. But we can still wind up issuing more than one | |
4148 | // error per object file. | |
4149 | if (this->issued_non_pic_error_) | |
4150 | return; | |
4151 | object->error(_("requires unsupported dynamic reloc; " | |
4152 | "recompile with -fPIC")); | |
4153 | this->issued_non_pic_error_ = true; | |
4154 | return; | |
4155 | ||
4156 | case elfcpp::R_ARM_NONE: | |
4157 | gold_unreachable(); | |
4158 | } | |
4159 | } | |
4160 | ||
4a657b0d | 4161 | // Scan a relocation for a local symbol. |
bec53400 DK |
4162 | // FIXME: This only handles a subset of relocation types used by Android |
4163 | // on ARM v5te devices. | |
4a657b0d DK |
4164 | |
4165 | template<bool big_endian> | |
4166 | inline void | |
ad0f2072 | 4167 | Target_arm<big_endian>::Scan::local(Symbol_table* symtab, |
bec53400 DK |
4168 | Layout* layout, |
4169 | Target_arm* target, | |
4a657b0d | 4170 | Sized_relobj<32, big_endian>* object, |
bec53400 DK |
4171 | unsigned int data_shndx, |
4172 | Output_section* output_section, | |
4173 | const elfcpp::Rel<32, big_endian>& reloc, | |
4a657b0d DK |
4174 | unsigned int r_type, |
4175 | const elfcpp::Sym<32, big_endian>&) | |
4176 | { | |
a6d1ef57 | 4177 | r_type = get_real_reloc_type(r_type); |
4a657b0d DK |
4178 | switch (r_type) |
4179 | { | |
4180 | case elfcpp::R_ARM_NONE: | |
4181 | break; | |
4182 | ||
bec53400 | 4183 | case elfcpp::R_ARM_ABS32: |
be8fcb75 | 4184 | case elfcpp::R_ARM_ABS32_NOI: |
bec53400 DK |
4185 | // If building a shared library (or a position-independent |
4186 | // executable), we need to create a dynamic relocation for | |
4187 | // this location. The relocation applied at link time will | |
4188 | // apply the link-time value, so we flag the location with | |
4189 | // an R_ARM_RELATIVE relocation so the dynamic loader can | |
4190 | // relocate it easily. | |
4191 | if (parameters->options().output_is_position_independent()) | |
4192 | { | |
4193 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
4194 | unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); | |
4195 | // If we are to add more other reloc types than R_ARM_ABS32, | |
4196 | // we need to add check_non_pic(object, r_type) here. | |
4197 | rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE, | |
4198 | output_section, data_shndx, | |
4199 | reloc.get_r_offset()); | |
4200 | } | |
4201 | break; | |
4202 | ||
4203 | case elfcpp::R_ARM_REL32: | |
4204 | case elfcpp::R_ARM_THM_CALL: | |
4205 | case elfcpp::R_ARM_CALL: | |
4206 | case elfcpp::R_ARM_PREL31: | |
4207 | case elfcpp::R_ARM_JUMP24: | |
4208 | case elfcpp::R_ARM_PLT32: | |
be8fcb75 ILT |
4209 | case elfcpp::R_ARM_THM_ABS5: |
4210 | case elfcpp::R_ARM_ABS8: | |
4211 | case elfcpp::R_ARM_ABS12: | |
4212 | case elfcpp::R_ARM_ABS16: | |
4213 | case elfcpp::R_ARM_BASE_ABS: | |
fd3c5f0b ILT |
4214 | case elfcpp::R_ARM_MOVW_ABS_NC: |
4215 | case elfcpp::R_ARM_MOVT_ABS: | |
4216 | case elfcpp::R_ARM_THM_MOVW_ABS_NC: | |
4217 | case elfcpp::R_ARM_THM_MOVT_ABS: | |
c2a122b6 ILT |
4218 | case elfcpp::R_ARM_MOVW_PREL_NC: |
4219 | case elfcpp::R_ARM_MOVT_PREL: | |
4220 | case elfcpp::R_ARM_THM_MOVW_PREL_NC: | |
4221 | case elfcpp::R_ARM_THM_MOVT_PREL: | |
bec53400 DK |
4222 | break; |
4223 | ||
4224 | case elfcpp::R_ARM_GOTOFF32: | |
4225 | // We need a GOT section: | |
4226 | target->got_section(symtab, layout); | |
4227 | break; | |
4228 | ||
4229 | case elfcpp::R_ARM_BASE_PREL: | |
4230 | // FIXME: What about this? | |
4231 | break; | |
4232 | ||
4233 | case elfcpp::R_ARM_GOT_BREL: | |
7f5309a5 | 4234 | case elfcpp::R_ARM_GOT_PREL: |
bec53400 DK |
4235 | { |
4236 | // The symbol requires a GOT entry. | |
4237 | Output_data_got<32, big_endian>* got = | |
4238 | target->got_section(symtab, layout); | |
4239 | unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); | |
4240 | if (got->add_local(object, r_sym, GOT_TYPE_STANDARD)) | |
4241 | { | |
4242 | // If we are generating a shared object, we need to add a | |
4243 | // dynamic RELATIVE relocation for this symbol's GOT entry. | |
4244 | if (parameters->options().output_is_position_independent()) | |
4245 | { | |
4246 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
4247 | unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); | |
4248 | rel_dyn->add_local_relative( | |
4249 | object, r_sym, elfcpp::R_ARM_RELATIVE, got, | |
4250 | object->local_got_offset(r_sym, GOT_TYPE_STANDARD)); | |
4251 | } | |
4252 | } | |
4253 | } | |
4254 | break; | |
4255 | ||
4256 | case elfcpp::R_ARM_TARGET1: | |
4257 | // This should have been mapped to another type already. | |
4258 | // Fall through. | |
4259 | case elfcpp::R_ARM_COPY: | |
4260 | case elfcpp::R_ARM_GLOB_DAT: | |
4261 | case elfcpp::R_ARM_JUMP_SLOT: | |
4262 | case elfcpp::R_ARM_RELATIVE: | |
4263 | // These are relocations which should only be seen by the | |
4264 | // dynamic linker, and should never be seen here. | |
4265 | gold_error(_("%s: unexpected reloc %u in object file"), | |
4266 | object->name().c_str(), r_type); | |
4267 | break; | |
4268 | ||
4a657b0d DK |
4269 | default: |
4270 | unsupported_reloc_local(object, r_type); | |
4271 | break; | |
4272 | } | |
4273 | } | |
4274 | ||
4275 | // Report an unsupported relocation against a global symbol. | |
4276 | ||
4277 | template<bool big_endian> | |
4278 | void | |
4279 | Target_arm<big_endian>::Scan::unsupported_reloc_global( | |
4280 | Sized_relobj<32, big_endian>* object, | |
4281 | unsigned int r_type, | |
4282 | Symbol* gsym) | |
4283 | { | |
4284 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), | |
4285 | object->name().c_str(), r_type, gsym->demangled_name().c_str()); | |
4286 | } | |
4287 | ||
4288 | // Scan a relocation for a global symbol. | |
bec53400 DK |
4289 | // FIXME: This only handles a subset of relocation types used by Android |
4290 | // on ARM v5te devices. | |
4a657b0d DK |
4291 | |
4292 | template<bool big_endian> | |
4293 | inline void | |
ad0f2072 | 4294 | Target_arm<big_endian>::Scan::global(Symbol_table* symtab, |
bec53400 DK |
4295 | Layout* layout, |
4296 | Target_arm* target, | |
4a657b0d | 4297 | Sized_relobj<32, big_endian>* object, |
bec53400 DK |
4298 | unsigned int data_shndx, |
4299 | Output_section* output_section, | |
4300 | const elfcpp::Rel<32, big_endian>& reloc, | |
4a657b0d DK |
4301 | unsigned int r_type, |
4302 | Symbol* gsym) | |
4303 | { | |
a6d1ef57 | 4304 | r_type = get_real_reloc_type(r_type); |
4a657b0d DK |
4305 | switch (r_type) |
4306 | { | |
4307 | case elfcpp::R_ARM_NONE: | |
4308 | break; | |
4309 | ||
bec53400 | 4310 | case elfcpp::R_ARM_ABS32: |
be8fcb75 | 4311 | case elfcpp::R_ARM_ABS32_NOI: |
bec53400 DK |
4312 | { |
4313 | // Make a dynamic relocation if necessary. | |
4314 | if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF)) | |
4315 | { | |
4316 | if (target->may_need_copy_reloc(gsym)) | |
4317 | { | |
4318 | target->copy_reloc(symtab, layout, object, | |
4319 | data_shndx, output_section, gsym, reloc); | |
4320 | } | |
4321 | else if (gsym->can_use_relative_reloc(false)) | |
4322 | { | |
4323 | // If we are to add more other reloc types than R_ARM_ABS32, | |
4324 | // we need to add check_non_pic(object, r_type) here. | |
4325 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
4326 | rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE, | |
4327 | output_section, object, | |
4328 | data_shndx, reloc.get_r_offset()); | |
4329 | } | |
4330 | else | |
4331 | { | |
4332 | // If we are to add more other reloc types than R_ARM_ABS32, | |
4333 | // we need to add check_non_pic(object, r_type) here. | |
4334 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
4335 | rel_dyn->add_global(gsym, r_type, output_section, object, | |
4336 | data_shndx, reloc.get_r_offset()); | |
4337 | } | |
4338 | } | |
4339 | } | |
4340 | break; | |
4341 | ||
fd3c5f0b ILT |
4342 | case elfcpp::R_ARM_MOVW_ABS_NC: |
4343 | case elfcpp::R_ARM_MOVT_ABS: | |
4344 | case elfcpp::R_ARM_THM_MOVW_ABS_NC: | |
4345 | case elfcpp::R_ARM_THM_MOVT_ABS: | |
c2a122b6 ILT |
4346 | case elfcpp::R_ARM_MOVW_PREL_NC: |
4347 | case elfcpp::R_ARM_MOVT_PREL: | |
4348 | case elfcpp::R_ARM_THM_MOVW_PREL_NC: | |
4349 | case elfcpp::R_ARM_THM_MOVT_PREL: | |
fd3c5f0b ILT |
4350 | break; |
4351 | ||
be8fcb75 ILT |
4352 | case elfcpp::R_ARM_THM_ABS5: |
4353 | case elfcpp::R_ARM_ABS8: | |
4354 | case elfcpp::R_ARM_ABS12: | |
4355 | case elfcpp::R_ARM_ABS16: | |
4356 | case elfcpp::R_ARM_BASE_ABS: | |
4357 | { | |
4358 | // No dynamic relocs of this kinds. | |
4359 | // Report the error in case of PIC. | |
4360 | int flags = Symbol::NON_PIC_REF; | |
4361 | if (gsym->type() == elfcpp::STT_FUNC | |
4362 | || gsym->type() == elfcpp::STT_ARM_TFUNC) | |
4363 | flags |= Symbol::FUNCTION_CALL; | |
4364 | if (gsym->needs_dynamic_reloc(flags)) | |
4365 | check_non_pic(object, r_type); | |
4366 | } | |
4367 | break; | |
4368 | ||
bec53400 DK |
4369 | case elfcpp::R_ARM_REL32: |
4370 | case elfcpp::R_ARM_PREL31: | |
4371 | { | |
4372 | // Make a dynamic relocation if necessary. | |
4373 | int flags = Symbol::NON_PIC_REF; | |
4374 | if (gsym->needs_dynamic_reloc(flags)) | |
4375 | { | |
4376 | if (target->may_need_copy_reloc(gsym)) | |
4377 | { | |
4378 | target->copy_reloc(symtab, layout, object, | |
4379 | data_shndx, output_section, gsym, reloc); | |
4380 | } | |
4381 | else | |
4382 | { | |
4383 | check_non_pic(object, r_type); | |
4384 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
4385 | rel_dyn->add_global(gsym, r_type, output_section, object, | |
4386 | data_shndx, reloc.get_r_offset()); | |
4387 | } | |
4388 | } | |
4389 | } | |
4390 | break; | |
4391 | ||
4392 | case elfcpp::R_ARM_JUMP24: | |
f4e5969c | 4393 | case elfcpp::R_ARM_THM_JUMP24: |
bec53400 | 4394 | case elfcpp::R_ARM_CALL: |
f4e5969c DK |
4395 | case elfcpp::R_ARM_THM_CALL: |
4396 | ||
4397 | if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym)) | |
4398 | target->make_plt_entry(symtab, layout, gsym); | |
4399 | else | |
4400 | { | |
4401 | // Check to see if this is a function that would need a PLT | |
4402 | // but does not get one because the function symbol is untyped. | |
4403 | // This happens in assembly code missing a proper .type directive. | |
4404 | if ((!gsym->is_undefined() || parameters->options().shared()) | |
4405 | && !parameters->doing_static_link() | |
4406 | && gsym->type() == elfcpp::STT_NOTYPE | |
4407 | && (gsym->is_from_dynobj() | |
4408 | || gsym->is_undefined() | |
4409 | || gsym->is_preemptible())) | |
4410 | gold_error(_("%s is not a function."), | |
4411 | gsym->demangled_name().c_str()); | |
4412 | } | |
bec53400 DK |
4413 | break; |
4414 | ||
4415 | case elfcpp::R_ARM_PLT32: | |
4416 | // If the symbol is fully resolved, this is just a relative | |
4417 | // local reloc. Otherwise we need a PLT entry. | |
4418 | if (gsym->final_value_is_known()) | |
4419 | break; | |
4420 | // If building a shared library, we can also skip the PLT entry | |
4421 | // if the symbol is defined in the output file and is protected | |
4422 | // or hidden. | |
4423 | if (gsym->is_defined() | |
4424 | && !gsym->is_from_dynobj() | |
4425 | && !gsym->is_preemptible()) | |
4426 | break; | |
4427 | target->make_plt_entry(symtab, layout, gsym); | |
4428 | break; | |
4429 | ||
4430 | case elfcpp::R_ARM_GOTOFF32: | |
4431 | // We need a GOT section. | |
4432 | target->got_section(symtab, layout); | |
4433 | break; | |
4434 | ||
4435 | case elfcpp::R_ARM_BASE_PREL: | |
4436 | // FIXME: What about this? | |
4437 | break; | |
4438 | ||
4439 | case elfcpp::R_ARM_GOT_BREL: | |
7f5309a5 | 4440 | case elfcpp::R_ARM_GOT_PREL: |
bec53400 DK |
4441 | { |
4442 | // The symbol requires a GOT entry. | |
4443 | Output_data_got<32, big_endian>* got = | |
4444 | target->got_section(symtab, layout); | |
4445 | if (gsym->final_value_is_known()) | |
4446 | got->add_global(gsym, GOT_TYPE_STANDARD); | |
4447 | else | |
4448 | { | |
4449 | // If this symbol is not fully resolved, we need to add a | |
4450 | // GOT entry with a dynamic relocation. | |
4451 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
4452 | if (gsym->is_from_dynobj() | |
4453 | || gsym->is_undefined() | |
4454 | || gsym->is_preemptible()) | |
4455 | got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, | |
4456 | rel_dyn, elfcpp::R_ARM_GLOB_DAT); | |
4457 | else | |
4458 | { | |
4459 | if (got->add_global(gsym, GOT_TYPE_STANDARD)) | |
4460 | rel_dyn->add_global_relative( | |
4461 | gsym, elfcpp::R_ARM_RELATIVE, got, | |
4462 | gsym->got_offset(GOT_TYPE_STANDARD)); | |
4463 | } | |
4464 | } | |
4465 | } | |
4466 | break; | |
4467 | ||
4468 | case elfcpp::R_ARM_TARGET1: | |
4469 | // This should have been mapped to another type already. | |
4470 | // Fall through. | |
4471 | case elfcpp::R_ARM_COPY: | |
4472 | case elfcpp::R_ARM_GLOB_DAT: | |
4473 | case elfcpp::R_ARM_JUMP_SLOT: | |
4474 | case elfcpp::R_ARM_RELATIVE: | |
4475 | // These are relocations which should only be seen by the | |
4476 | // dynamic linker, and should never be seen here. | |
4477 | gold_error(_("%s: unexpected reloc %u in object file"), | |
4478 | object->name().c_str(), r_type); | |
4479 | break; | |
4480 | ||
4a657b0d DK |
4481 | default: |
4482 | unsupported_reloc_global(object, r_type, gsym); | |
4483 | break; | |
4484 | } | |
4485 | } | |
4486 | ||
4487 | // Process relocations for gc. | |
4488 | ||
4489 | template<bool big_endian> | |
4490 | void | |
ad0f2072 | 4491 | Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab, |
4a657b0d DK |
4492 | Layout* layout, |
4493 | Sized_relobj<32, big_endian>* object, | |
4494 | unsigned int data_shndx, | |
4495 | unsigned int, | |
4496 | const unsigned char* prelocs, | |
4497 | size_t reloc_count, | |
4498 | Output_section* output_section, | |
4499 | bool needs_special_offset_handling, | |
4500 | size_t local_symbol_count, | |
4501 | const unsigned char* plocal_symbols) | |
4502 | { | |
4503 | typedef Target_arm<big_endian> Arm; | |
4504 | typedef typename Target_arm<big_endian>::Scan Scan; | |
4505 | ||
4506 | gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>( | |
4a657b0d DK |
4507 | symtab, |
4508 | layout, | |
4509 | this, | |
4510 | object, | |
4511 | data_shndx, | |
4512 | prelocs, | |
4513 | reloc_count, | |
4514 | output_section, | |
4515 | needs_special_offset_handling, | |
4516 | local_symbol_count, | |
4517 | plocal_symbols); | |
4518 | } | |
4519 | ||
4520 | // Scan relocations for a section. | |
4521 | ||
4522 | template<bool big_endian> | |
4523 | void | |
ad0f2072 | 4524 | Target_arm<big_endian>::scan_relocs(Symbol_table* symtab, |
4a657b0d DK |
4525 | Layout* layout, |
4526 | Sized_relobj<32, big_endian>* object, | |
4527 | unsigned int data_shndx, | |
4528 | unsigned int sh_type, | |
4529 | const unsigned char* prelocs, | |
4530 | size_t reloc_count, | |
4531 | Output_section* output_section, | |
4532 | bool needs_special_offset_handling, | |
4533 | size_t local_symbol_count, | |
4534 | const unsigned char* plocal_symbols) | |
4535 | { | |
4536 | typedef typename Target_arm<big_endian>::Scan Scan; | |
4537 | if (sh_type == elfcpp::SHT_RELA) | |
4538 | { | |
4539 | gold_error(_("%s: unsupported RELA reloc section"), | |
4540 | object->name().c_str()); | |
4541 | return; | |
4542 | } | |
4543 | ||
4544 | gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>( | |
4a657b0d DK |
4545 | symtab, |
4546 | layout, | |
4547 | this, | |
4548 | object, | |
4549 | data_shndx, | |
4550 | prelocs, | |
4551 | reloc_count, | |
4552 | output_section, | |
4553 | needs_special_offset_handling, | |
4554 | local_symbol_count, | |
4555 | plocal_symbols); | |
4556 | } | |
4557 | ||
4558 | // Finalize the sections. | |
4559 | ||
4560 | template<bool big_endian> | |
4561 | void | |
d5b40221 DK |
4562 | Target_arm<big_endian>::do_finalize_sections( |
4563 | Layout* layout, | |
f59f41f3 DK |
4564 | const Input_objects* input_objects, |
4565 | Symbol_table* symtab) | |
4a657b0d | 4566 | { |
d5b40221 DK |
4567 | // Merge processor-specific flags. |
4568 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
4569 | p != input_objects->relobj_end(); | |
4570 | ++p) | |
4571 | { | |
4572 | Arm_relobj<big_endian>* arm_relobj = | |
4573 | Arm_relobj<big_endian>::as_arm_relobj(*p); | |
4574 | this->merge_processor_specific_flags( | |
4575 | arm_relobj->name(), | |
4576 | arm_relobj->processor_specific_flags()); | |
4577 | } | |
4578 | ||
4579 | for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin(); | |
4580 | p != input_objects->dynobj_end(); | |
4581 | ++p) | |
4582 | { | |
4583 | Arm_dynobj<big_endian>* arm_dynobj = | |
4584 | Arm_dynobj<big_endian>::as_arm_dynobj(*p); | |
4585 | this->merge_processor_specific_flags( | |
4586 | arm_dynobj->name(), | |
4587 | arm_dynobj->processor_specific_flags()); | |
4588 | } | |
4589 | ||
94cdfcff DK |
4590 | // Fill in some more dynamic tags. |
4591 | Output_data_dynamic* const odyn = layout->dynamic_data(); | |
4592 | if (odyn != NULL) | |
4593 | { | |
22b127cc ILT |
4594 | if (this->got_plt_ != NULL |
4595 | && this->got_plt_->output_section() != NULL) | |
94cdfcff DK |
4596 | odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_); |
4597 | ||
22b127cc ILT |
4598 | if (this->plt_ != NULL |
4599 | && this->plt_->output_section() != NULL) | |
94cdfcff DK |
4600 | { |
4601 | const Output_data* od = this->plt_->rel_plt(); | |
4602 | odyn->add_section_size(elfcpp::DT_PLTRELSZ, od); | |
4603 | odyn->add_section_address(elfcpp::DT_JMPREL, od); | |
4604 | odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL); | |
4605 | } | |
4606 | ||
22b127cc ILT |
4607 | if (this->rel_dyn_ != NULL |
4608 | && this->rel_dyn_->output_section() != NULL) | |
94cdfcff DK |
4609 | { |
4610 | const Output_data* od = this->rel_dyn_; | |
4611 | odyn->add_section_address(elfcpp::DT_REL, od); | |
4612 | odyn->add_section_size(elfcpp::DT_RELSZ, od); | |
4613 | odyn->add_constant(elfcpp::DT_RELENT, | |
4614 | elfcpp::Elf_sizes<32>::rel_size); | |
4615 | } | |
4616 | ||
4617 | if (!parameters->options().shared()) | |
4618 | { | |
4619 | // The value of the DT_DEBUG tag is filled in by the dynamic | |
4620 | // linker at run time, and used by the debugger. | |
4621 | odyn->add_constant(elfcpp::DT_DEBUG, 0); | |
4622 | } | |
4623 | } | |
4624 | ||
4625 | // Emit any relocs we saved in an attempt to avoid generating COPY | |
4626 | // relocs. | |
4627 | if (this->copy_relocs_.any_saved_relocs()) | |
4628 | this->copy_relocs_.emit(this->rel_dyn_section(layout)); | |
11af873f | 4629 | |
f59f41f3 DK |
4630 | // Handle the .ARM.exidx section. |
4631 | Output_section* exidx_section = layout->find_output_section(".ARM.exidx"); | |
4632 | if (exidx_section != NULL | |
4633 | && exidx_section->type() == elfcpp::SHT_ARM_EXIDX | |
11af873f DK |
4634 | && !parameters->options().relocatable()) |
4635 | { | |
f59f41f3 DK |
4636 | // Create __exidx_start and __exdix_end symbols. |
4637 | symtab->define_in_output_data("__exidx_start", NULL, exidx_section, | |
4638 | 0, 0, elfcpp::STT_OBJECT, | |
4639 | elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0, | |
4640 | false, false); | |
4641 | symtab->define_in_output_data("__exidx_end", NULL, exidx_section, | |
4642 | 0, 0, elfcpp::STT_OBJECT, | |
4643 | elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0, | |
4644 | true, false); | |
11af873f | 4645 | |
f59f41f3 DK |
4646 | // For the ARM target, we need to add a PT_ARM_EXIDX segment for |
4647 | // the .ARM.exidx section. | |
4648 | if (!layout->script_options()->saw_phdrs_clause()) | |
11af873f DK |
4649 | { |
4650 | gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0) | |
4651 | == NULL); | |
4652 | Output_segment* exidx_segment = | |
4653 | layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R); | |
f5c870d2 ILT |
4654 | exidx_segment->add_output_section(exidx_section, elfcpp::PF_R, |
4655 | false); | |
11af873f DK |
4656 | } |
4657 | } | |
4a657b0d DK |
4658 | } |
4659 | ||
bec53400 DK |
4660 | // Return whether a direct absolute static relocation needs to be applied. |
4661 | // In cases where Scan::local() or Scan::global() has created | |
4662 | // a dynamic relocation other than R_ARM_RELATIVE, the addend | |
4663 | // of the relocation is carried in the data, and we must not | |
4664 | // apply the static relocation. | |
4665 | ||
4666 | template<bool big_endian> | |
4667 | inline bool | |
4668 | Target_arm<big_endian>::Relocate::should_apply_static_reloc( | |
4669 | const Sized_symbol<32>* gsym, | |
4670 | int ref_flags, | |
4671 | bool is_32bit, | |
4672 | Output_section* output_section) | |
4673 | { | |
4674 | // If the output section is not allocated, then we didn't call | |
4675 | // scan_relocs, we didn't create a dynamic reloc, and we must apply | |
4676 | // the reloc here. | |
4677 | if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0) | |
4678 | return true; | |
4679 | ||
4680 | // For local symbols, we will have created a non-RELATIVE dynamic | |
4681 | // relocation only if (a) the output is position independent, | |
4682 | // (b) the relocation is absolute (not pc- or segment-relative), and | |
4683 | // (c) the relocation is not 32 bits wide. | |
4684 | if (gsym == NULL) | |
4685 | return !(parameters->options().output_is_position_independent() | |
4686 | && (ref_flags & Symbol::ABSOLUTE_REF) | |
4687 | && !is_32bit); | |
4688 | ||
4689 | // For global symbols, we use the same helper routines used in the | |
4690 | // scan pass. If we did not create a dynamic relocation, or if we | |
4691 | // created a RELATIVE dynamic relocation, we should apply the static | |
4692 | // relocation. | |
4693 | bool has_dyn = gsym->needs_dynamic_reloc(ref_flags); | |
4694 | bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF) | |
4695 | && gsym->can_use_relative_reloc(ref_flags | |
4696 | & Symbol::FUNCTION_CALL); | |
4697 | return !has_dyn || is_rel; | |
4698 | } | |
4699 | ||
4a657b0d DK |
4700 | // Perform a relocation. |
4701 | ||
4702 | template<bool big_endian> | |
4703 | inline bool | |
4704 | Target_arm<big_endian>::Relocate::relocate( | |
c121c671 DK |
4705 | const Relocate_info<32, big_endian>* relinfo, |
4706 | Target_arm* target, | |
4707 | Output_section *output_section, | |
4708 | size_t relnum, | |
4709 | const elfcpp::Rel<32, big_endian>& rel, | |
4a657b0d | 4710 | unsigned int r_type, |
c121c671 DK |
4711 | const Sized_symbol<32>* gsym, |
4712 | const Symbol_value<32>* psymval, | |
4713 | unsigned char* view, | |
ebabffbd | 4714 | Arm_address address, |
4a657b0d DK |
4715 | section_size_type /* view_size */ ) |
4716 | { | |
c121c671 DK |
4717 | typedef Arm_relocate_functions<big_endian> Arm_relocate_functions; |
4718 | ||
a6d1ef57 | 4719 | r_type = get_real_reloc_type(r_type); |
c121c671 | 4720 | |
2daedcd6 DK |
4721 | const Arm_relobj<big_endian>* object = |
4722 | Arm_relobj<big_endian>::as_arm_relobj(relinfo->object); | |
c121c671 | 4723 | |
2daedcd6 DK |
4724 | // If the final branch target of a relocation is THUMB instruction, this |
4725 | // is 1. Otherwise it is 0. | |
4726 | Arm_address thumb_bit = 0; | |
c121c671 | 4727 | Symbol_value<32> symval; |
d204b6e9 | 4728 | bool is_weakly_undefined_without_plt = false; |
2daedcd6 | 4729 | if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs) |
c121c671 | 4730 | { |
2daedcd6 DK |
4731 | if (gsym != NULL) |
4732 | { | |
4733 | // This is a global symbol. Determine if we use PLT and if the | |
4734 | // final target is THUMB. | |
4735 | if (gsym->use_plt_offset(reloc_is_non_pic(r_type))) | |
4736 | { | |
4737 | // This uses a PLT, change the symbol value. | |
4738 | symval.set_output_value(target->plt_section()->address() | |
4739 | + gsym->plt_offset()); | |
4740 | psymval = &symval; | |
4741 | } | |
d204b6e9 DK |
4742 | else if (gsym->is_weak_undefined()) |
4743 | { | |
4744 | // This is a weakly undefined symbol and we do not use PLT | |
4745 | // for this relocation. A branch targeting this symbol will | |
4746 | // be converted into an NOP. | |
4747 | is_weakly_undefined_without_plt = true; | |
4748 | } | |
2daedcd6 DK |
4749 | else |
4750 | { | |
4751 | // Set thumb bit if symbol: | |
4752 | // -Has type STT_ARM_TFUNC or | |
4753 | // -Has type STT_FUNC, is defined and with LSB in value set. | |
4754 | thumb_bit = | |
4755 | (((gsym->type() == elfcpp::STT_ARM_TFUNC) | |
4756 | || (gsym->type() == elfcpp::STT_FUNC | |
4757 | && !gsym->is_undefined() | |
4758 | && ((psymval->value(object, 0) & 1) != 0))) | |
4759 | ? 1 | |
4760 | : 0); | |
4761 | } | |
4762 | } | |
4763 | else | |
4764 | { | |
4765 | // This is a local symbol. Determine if the final target is THUMB. | |
4766 | // We saved this information when all the local symbols were read. | |
4767 | elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info(); | |
4768 | unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info); | |
4769 | thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0; | |
4770 | } | |
4771 | } | |
4772 | else | |
4773 | { | |
4774 | // This is a fake relocation synthesized for a stub. It does not have | |
4775 | // a real symbol. We just look at the LSB of the symbol value to | |
4776 | // determine if the target is THUMB or not. | |
4777 | thumb_bit = ((psymval->value(object, 0) & 1) != 0); | |
c121c671 DK |
4778 | } |
4779 | ||
2daedcd6 DK |
4780 | // Strip LSB if this points to a THUMB target. |
4781 | if (thumb_bit != 0 | |
4782 | && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type) | |
4783 | && ((psymval->value(object, 0) & 1) != 0)) | |
4784 | { | |
4785 | Arm_address stripped_value = | |
4786 | psymval->value(object, 0) & ~static_cast<Arm_address>(1); | |
4787 | symval.set_output_value(stripped_value); | |
4788 | psymval = &symval; | |
4789 | } | |
4790 | ||
c121c671 DK |
4791 | // Get the GOT offset if needed. |
4792 | // The GOT pointer points to the end of the GOT section. | |
4793 | // We need to subtract the size of the GOT section to get | |
4794 | // the actual offset to use in the relocation. | |
4795 | bool have_got_offset = false; | |
4796 | unsigned int got_offset = 0; | |
4797 | switch (r_type) | |
4798 | { | |
4799 | case elfcpp::R_ARM_GOT_BREL: | |
7f5309a5 | 4800 | case elfcpp::R_ARM_GOT_PREL: |
c121c671 DK |
4801 | if (gsym != NULL) |
4802 | { | |
4803 | gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); | |
4804 | got_offset = (gsym->got_offset(GOT_TYPE_STANDARD) | |
4805 | - target->got_size()); | |
4806 | } | |
4807 | else | |
4808 | { | |
4809 | unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info()); | |
4810 | gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); | |
4811 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD) | |
4812 | - target->got_size()); | |
4813 | } | |
4814 | have_got_offset = true; | |
4815 | break; | |
4816 | ||
4817 | default: | |
4818 | break; | |
4819 | } | |
4820 | ||
d204b6e9 DK |
4821 | // To look up relocation stubs, we need to pass the symbol table index of |
4822 | // a local symbol. | |
4823 | unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info()); | |
4824 | ||
c121c671 DK |
4825 | typename Arm_relocate_functions::Status reloc_status = |
4826 | Arm_relocate_functions::STATUS_OKAY; | |
4a657b0d DK |
4827 | switch (r_type) |
4828 | { | |
4829 | case elfcpp::R_ARM_NONE: | |
4830 | break; | |
4831 | ||
5e445df6 ILT |
4832 | case elfcpp::R_ARM_ABS8: |
4833 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false, | |
4834 | output_section)) | |
be8fcb75 ILT |
4835 | reloc_status = Arm_relocate_functions::abs8(view, object, psymval); |
4836 | break; | |
4837 | ||
4838 | case elfcpp::R_ARM_ABS12: | |
4839 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false, | |
4840 | output_section)) | |
4841 | reloc_status = Arm_relocate_functions::abs12(view, object, psymval); | |
4842 | break; | |
4843 | ||
4844 | case elfcpp::R_ARM_ABS16: | |
4845 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false, | |
4846 | output_section)) | |
4847 | reloc_status = Arm_relocate_functions::abs16(view, object, psymval); | |
5e445df6 ILT |
4848 | break; |
4849 | ||
c121c671 DK |
4850 | case elfcpp::R_ARM_ABS32: |
4851 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true, | |
4852 | output_section)) | |
4853 | reloc_status = Arm_relocate_functions::abs32(view, object, psymval, | |
2daedcd6 | 4854 | thumb_bit); |
c121c671 DK |
4855 | break; |
4856 | ||
be8fcb75 ILT |
4857 | case elfcpp::R_ARM_ABS32_NOI: |
4858 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true, | |
4859 | output_section)) | |
4860 | // No thumb bit for this relocation: (S + A) | |
4861 | reloc_status = Arm_relocate_functions::abs32(view, object, psymval, | |
f4e5969c | 4862 | 0); |
be8fcb75 ILT |
4863 | break; |
4864 | ||
fd3c5f0b ILT |
4865 | case elfcpp::R_ARM_MOVW_ABS_NC: |
4866 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true, | |
4867 | output_section)) | |
4868 | reloc_status = Arm_relocate_functions::movw_abs_nc(view, object, | |
4869 | psymval, | |
2daedcd6 | 4870 | thumb_bit); |
fd3c5f0b ILT |
4871 | else |
4872 | gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making" | |
4873 | "a shared object; recompile with -fPIC")); | |
4874 | break; | |
4875 | ||
4876 | case elfcpp::R_ARM_MOVT_ABS: | |
4877 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true, | |
4878 | output_section)) | |
4879 | reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval); | |
4880 | else | |
4881 | gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making" | |
4882 | "a shared object; recompile with -fPIC")); | |
4883 | break; | |
4884 | ||
4885 | case elfcpp::R_ARM_THM_MOVW_ABS_NC: | |
4886 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true, | |
4887 | output_section)) | |
4888 | reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object, | |
4889 | psymval, | |
2daedcd6 | 4890 | thumb_bit); |
fd3c5f0b ILT |
4891 | else |
4892 | gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when" | |
4893 | "making a shared object; recompile with -fPIC")); | |
4894 | break; | |
4895 | ||
4896 | case elfcpp::R_ARM_THM_MOVT_ABS: | |
4897 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true, | |
4898 | output_section)) | |
4899 | reloc_status = Arm_relocate_functions::thm_movt_abs(view, object, | |
4900 | psymval); | |
4901 | else | |
4902 | gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when" | |
4903 | "making a shared object; recompile with -fPIC")); | |
4904 | break; | |
4905 | ||
c2a122b6 ILT |
4906 | case elfcpp::R_ARM_MOVW_PREL_NC: |
4907 | reloc_status = Arm_relocate_functions::movw_prel_nc(view, object, | |
4908 | psymval, address, | |
2daedcd6 | 4909 | thumb_bit); |
c2a122b6 ILT |
4910 | break; |
4911 | ||
4912 | case elfcpp::R_ARM_MOVT_PREL: | |
4913 | reloc_status = Arm_relocate_functions::movt_prel(view, object, | |
4914 | psymval, address); | |
4915 | break; | |
4916 | ||
4917 | case elfcpp::R_ARM_THM_MOVW_PREL_NC: | |
4918 | reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object, | |
4919 | psymval, address, | |
2daedcd6 | 4920 | thumb_bit); |
c2a122b6 ILT |
4921 | break; |
4922 | ||
4923 | case elfcpp::R_ARM_THM_MOVT_PREL: | |
4924 | reloc_status = Arm_relocate_functions::thm_movt_prel(view, object, | |
4925 | psymval, address); | |
4926 | break; | |
4927 | ||
c121c671 DK |
4928 | case elfcpp::R_ARM_REL32: |
4929 | reloc_status = Arm_relocate_functions::rel32(view, object, psymval, | |
2daedcd6 | 4930 | address, thumb_bit); |
c121c671 DK |
4931 | break; |
4932 | ||
be8fcb75 ILT |
4933 | case elfcpp::R_ARM_THM_ABS5: |
4934 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false, | |
4935 | output_section)) | |
4936 | reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval); | |
4937 | break; | |
4938 | ||
c121c671 | 4939 | case elfcpp::R_ARM_THM_CALL: |
51938283 DK |
4940 | reloc_status = |
4941 | Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym, | |
4942 | psymval, address, thumb_bit, | |
4943 | is_weakly_undefined_without_plt); | |
c121c671 DK |
4944 | break; |
4945 | ||
d204b6e9 DK |
4946 | case elfcpp::R_ARM_XPC25: |
4947 | reloc_status = | |
4948 | Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym, | |
4949 | psymval, address, thumb_bit, | |
4950 | is_weakly_undefined_without_plt); | |
4951 | break; | |
4952 | ||
51938283 DK |
4953 | case elfcpp::R_ARM_THM_XPC22: |
4954 | reloc_status = | |
4955 | Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym, | |
4956 | psymval, address, thumb_bit, | |
4957 | is_weakly_undefined_without_plt); | |
4958 | break; | |
4959 | ||
c121c671 DK |
4960 | case elfcpp::R_ARM_GOTOFF32: |
4961 | { | |
ebabffbd | 4962 | Arm_address got_origin; |
c121c671 DK |
4963 | got_origin = target->got_plt_section()->address(); |
4964 | reloc_status = Arm_relocate_functions::rel32(view, object, psymval, | |
2daedcd6 | 4965 | got_origin, thumb_bit); |
c121c671 DK |
4966 | } |
4967 | break; | |
4968 | ||
4969 | case elfcpp::R_ARM_BASE_PREL: | |
4970 | { | |
4971 | uint32_t origin; | |
4972 | // Get the addressing origin of the output segment defining the | |
4973 | // symbol gsym (AAELF 4.6.1.2 Relocation types) | |
4974 | gold_assert(gsym != NULL); | |
4975 | if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT) | |
4976 | origin = gsym->output_segment()->vaddr(); | |
4977 | else if (gsym->source () == Symbol::IN_OUTPUT_DATA) | |
4978 | origin = gsym->output_data()->address(); | |
4979 | else | |
4980 | { | |
4981 | gold_error_at_location(relinfo, relnum, rel.get_r_offset(), | |
4982 | _("cannot find origin of R_ARM_BASE_PREL")); | |
4983 | return true; | |
4984 | } | |
4985 | reloc_status = Arm_relocate_functions::base_prel(view, origin, address); | |
4986 | } | |
4987 | break; | |
4988 | ||
be8fcb75 ILT |
4989 | case elfcpp::R_ARM_BASE_ABS: |
4990 | { | |
4991 | if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true, | |
4992 | output_section)) | |
4993 | break; | |
4994 | ||
4995 | uint32_t origin; | |
4996 | // Get the addressing origin of the output segment defining | |
4997 | // the symbol gsym (AAELF 4.6.1.2 Relocation types). | |
4998 | if (gsym == NULL) | |
4999 | // R_ARM_BASE_ABS with the NULL symbol will give the | |
5000 | // absolute address of the GOT origin (GOT_ORG) (see ARM IHI | |
5001 | // 0044C (AAELF): 4.6.1.8 Proxy generating relocations). | |
5002 | origin = target->got_plt_section()->address(); | |
5003 | else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT) | |
5004 | origin = gsym->output_segment()->vaddr(); | |
5005 | else if (gsym->source () == Symbol::IN_OUTPUT_DATA) | |
5006 | origin = gsym->output_data()->address(); | |
5007 | else | |
5008 | { | |
5009 | gold_error_at_location(relinfo, relnum, rel.get_r_offset(), | |
5010 | _("cannot find origin of R_ARM_BASE_ABS")); | |
5011 | return true; | |
5012 | } | |
5013 | ||
5014 | reloc_status = Arm_relocate_functions::base_abs(view, origin); | |
5015 | } | |
5016 | break; | |
5017 | ||
c121c671 DK |
5018 | case elfcpp::R_ARM_GOT_BREL: |
5019 | gold_assert(have_got_offset); | |
5020 | reloc_status = Arm_relocate_functions::got_brel(view, got_offset); | |
5021 | break; | |
5022 | ||
7f5309a5 ILT |
5023 | case elfcpp::R_ARM_GOT_PREL: |
5024 | gold_assert(have_got_offset); | |
5025 | // Get the address origin for GOT PLT, which is allocated right | |
5026 | // after the GOT section, to calculate an absolute address of | |
5027 | // the symbol GOT entry (got_origin + got_offset). | |
ebabffbd | 5028 | Arm_address got_origin; |
7f5309a5 ILT |
5029 | got_origin = target->got_plt_section()->address(); |
5030 | reloc_status = Arm_relocate_functions::got_prel(view, | |
5031 | got_origin + got_offset, | |
5032 | address); | |
5033 | break; | |
5034 | ||
c121c671 DK |
5035 | case elfcpp::R_ARM_PLT32: |
5036 | gold_assert(gsym == NULL | |
5037 | || gsym->has_plt_offset() | |
5038 | || gsym->final_value_is_known() | |
5039 | || (gsym->is_defined() | |
5040 | && !gsym->is_from_dynobj() | |
5041 | && !gsym->is_preemptible())); | |
d204b6e9 DK |
5042 | reloc_status = |
5043 | Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym, | |
5044 | psymval, address, thumb_bit, | |
5045 | is_weakly_undefined_without_plt); | |
c121c671 DK |
5046 | break; |
5047 | ||
5048 | case elfcpp::R_ARM_CALL: | |
d204b6e9 DK |
5049 | reloc_status = |
5050 | Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym, | |
5051 | psymval, address, thumb_bit, | |
5052 | is_weakly_undefined_without_plt); | |
c121c671 DK |
5053 | break; |
5054 | ||
5055 | case elfcpp::R_ARM_JUMP24: | |
d204b6e9 DK |
5056 | reloc_status = |
5057 | Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym, | |
5058 | psymval, address, thumb_bit, | |
5059 | is_weakly_undefined_without_plt); | |
c121c671 DK |
5060 | break; |
5061 | ||
51938283 DK |
5062 | case elfcpp::R_ARM_THM_JUMP24: |
5063 | reloc_status = | |
5064 | Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym, | |
5065 | psymval, address, thumb_bit, | |
5066 | is_weakly_undefined_without_plt); | |
5067 | break; | |
5068 | ||
c121c671 DK |
5069 | case elfcpp::R_ARM_PREL31: |
5070 | reloc_status = Arm_relocate_functions::prel31(view, object, psymval, | |
2daedcd6 | 5071 | address, thumb_bit); |
c121c671 DK |
5072 | break; |
5073 | ||
5074 | case elfcpp::R_ARM_TARGET1: | |
5075 | // This should have been mapped to another type already. | |
5076 | // Fall through. | |
5077 | case elfcpp::R_ARM_COPY: | |
5078 | case elfcpp::R_ARM_GLOB_DAT: | |
5079 | case elfcpp::R_ARM_JUMP_SLOT: | |
5080 | case elfcpp::R_ARM_RELATIVE: | |
5081 | // These are relocations which should only be seen by the | |
5082 | // dynamic linker, and should never be seen here. | |
5083 | gold_error_at_location(relinfo, relnum, rel.get_r_offset(), | |
5084 | _("unexpected reloc %u in object file"), | |
5085 | r_type); | |
5086 | break; | |
5087 | ||
5088 | default: | |
5089 | gold_error_at_location(relinfo, relnum, rel.get_r_offset(), | |
5090 | _("unsupported reloc %u"), | |
5091 | r_type); | |
5092 | break; | |
5093 | } | |
5094 | ||
5095 | // Report any errors. | |
5096 | switch (reloc_status) | |
5097 | { | |
5098 | case Arm_relocate_functions::STATUS_OKAY: | |
5099 | break; | |
5100 | case Arm_relocate_functions::STATUS_OVERFLOW: | |
5101 | gold_error_at_location(relinfo, relnum, rel.get_r_offset(), | |
5102 | _("relocation overflow in relocation %u"), | |
5103 | r_type); | |
5104 | break; | |
5105 | case Arm_relocate_functions::STATUS_BAD_RELOC: | |
5106 | gold_error_at_location( | |
5107 | relinfo, | |
5108 | relnum, | |
5109 | rel.get_r_offset(), | |
5110 | _("unexpected opcode while processing relocation %u"), | |
5111 | r_type); | |
5112 | break; | |
4a657b0d DK |
5113 | default: |
5114 | gold_unreachable(); | |
5115 | } | |
5116 | ||
5117 | return true; | |
5118 | } | |
5119 | ||
5120 | // Relocate section data. | |
5121 | ||
5122 | template<bool big_endian> | |
5123 | void | |
5124 | Target_arm<big_endian>::relocate_section( | |
5125 | const Relocate_info<32, big_endian>* relinfo, | |
5126 | unsigned int sh_type, | |
5127 | const unsigned char* prelocs, | |
5128 | size_t reloc_count, | |
5129 | Output_section* output_section, | |
5130 | bool needs_special_offset_handling, | |
5131 | unsigned char* view, | |
ebabffbd | 5132 | Arm_address address, |
364c7fa5 ILT |
5133 | section_size_type view_size, |
5134 | const Reloc_symbol_changes* reloc_symbol_changes) | |
4a657b0d DK |
5135 | { |
5136 | typedef typename Target_arm<big_endian>::Relocate Arm_relocate; | |
5137 | gold_assert(sh_type == elfcpp::SHT_REL); | |
5138 | ||
43d12afe DK |
5139 | Arm_input_section<big_endian>* arm_input_section = |
5140 | this->find_arm_input_section(relinfo->object, relinfo->data_shndx); | |
5141 | ||
5142 | // This is an ARM input section and the view covers the whole output | |
5143 | // section. | |
5144 | if (arm_input_section != NULL) | |
5145 | { | |
5146 | gold_assert(needs_special_offset_handling); | |
5147 | Arm_address section_address = arm_input_section->address(); | |
5148 | section_size_type section_size = arm_input_section->data_size(); | |
5149 | ||
5150 | gold_assert((arm_input_section->address() >= address) | |
5151 | && ((arm_input_section->address() | |
5152 | + arm_input_section->data_size()) | |
5153 | <= (address + view_size))); | |
5154 | ||
5155 | off_t offset = section_address - address; | |
5156 | view += offset; | |
5157 | address += offset; | |
5158 | view_size = section_size; | |
5159 | } | |
5160 | ||
4a657b0d DK |
5161 | gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL, |
5162 | Arm_relocate>( | |
5163 | relinfo, | |
5164 | this, | |
5165 | prelocs, | |
5166 | reloc_count, | |
5167 | output_section, | |
5168 | needs_special_offset_handling, | |
5169 | view, | |
5170 | address, | |
364c7fa5 ILT |
5171 | view_size, |
5172 | reloc_symbol_changes); | |
4a657b0d DK |
5173 | } |
5174 | ||
5175 | // Return the size of a relocation while scanning during a relocatable | |
5176 | // link. | |
5177 | ||
5178 | template<bool big_endian> | |
5179 | unsigned int | |
5180 | Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc( | |
5181 | unsigned int r_type, | |
5182 | Relobj* object) | |
5183 | { | |
a6d1ef57 | 5184 | r_type = get_real_reloc_type(r_type); |
4a657b0d DK |
5185 | switch (r_type) |
5186 | { | |
5187 | case elfcpp::R_ARM_NONE: | |
5188 | return 0; | |
5189 | ||
5e445df6 ILT |
5190 | case elfcpp::R_ARM_ABS8: |
5191 | return 1; | |
5192 | ||
be8fcb75 ILT |
5193 | case elfcpp::R_ARM_ABS16: |
5194 | case elfcpp::R_ARM_THM_ABS5: | |
5195 | return 2; | |
5196 | ||
4a657b0d | 5197 | case elfcpp::R_ARM_ABS32: |
be8fcb75 ILT |
5198 | case elfcpp::R_ARM_ABS32_NOI: |
5199 | case elfcpp::R_ARM_ABS12: | |
5200 | case elfcpp::R_ARM_BASE_ABS: | |
4a657b0d DK |
5201 | case elfcpp::R_ARM_REL32: |
5202 | case elfcpp::R_ARM_THM_CALL: | |
5203 | case elfcpp::R_ARM_GOTOFF32: | |
5204 | case elfcpp::R_ARM_BASE_PREL: | |
5205 | case elfcpp::R_ARM_GOT_BREL: | |
7f5309a5 | 5206 | case elfcpp::R_ARM_GOT_PREL: |
4a657b0d DK |
5207 | case elfcpp::R_ARM_PLT32: |
5208 | case elfcpp::R_ARM_CALL: | |
5209 | case elfcpp::R_ARM_JUMP24: | |
5210 | case elfcpp::R_ARM_PREL31: | |
fd3c5f0b ILT |
5211 | case elfcpp::R_ARM_MOVW_ABS_NC: |
5212 | case elfcpp::R_ARM_MOVT_ABS: | |
5213 | case elfcpp::R_ARM_THM_MOVW_ABS_NC: | |
5214 | case elfcpp::R_ARM_THM_MOVT_ABS: | |
c2a122b6 ILT |
5215 | case elfcpp::R_ARM_MOVW_PREL_NC: |
5216 | case elfcpp::R_ARM_MOVT_PREL: | |
5217 | case elfcpp::R_ARM_THM_MOVW_PREL_NC: | |
5218 | case elfcpp::R_ARM_THM_MOVT_PREL: | |
4a657b0d DK |
5219 | return 4; |
5220 | ||
5221 | case elfcpp::R_ARM_TARGET1: | |
5222 | // This should have been mapped to another type already. | |
5223 | // Fall through. | |
5224 | case elfcpp::R_ARM_COPY: | |
5225 | case elfcpp::R_ARM_GLOB_DAT: | |
5226 | case elfcpp::R_ARM_JUMP_SLOT: | |
5227 | case elfcpp::R_ARM_RELATIVE: | |
5228 | // These are relocations which should only be seen by the | |
5229 | // dynamic linker, and should never be seen here. | |
5230 | gold_error(_("%s: unexpected reloc %u in object file"), | |
5231 | object->name().c_str(), r_type); | |
5232 | return 0; | |
5233 | ||
5234 | default: | |
5235 | object->error(_("unsupported reloc %u in object file"), r_type); | |
5236 | return 0; | |
5237 | } | |
5238 | } | |
5239 | ||
5240 | // Scan the relocs during a relocatable link. | |
5241 | ||
5242 | template<bool big_endian> | |
5243 | void | |
5244 | Target_arm<big_endian>::scan_relocatable_relocs( | |
4a657b0d DK |
5245 | Symbol_table* symtab, |
5246 | Layout* layout, | |
5247 | Sized_relobj<32, big_endian>* object, | |
5248 | unsigned int data_shndx, | |
5249 | unsigned int sh_type, | |
5250 | const unsigned char* prelocs, | |
5251 | size_t reloc_count, | |
5252 | Output_section* output_section, | |
5253 | bool needs_special_offset_handling, | |
5254 | size_t local_symbol_count, | |
5255 | const unsigned char* plocal_symbols, | |
5256 | Relocatable_relocs* rr) | |
5257 | { | |
5258 | gold_assert(sh_type == elfcpp::SHT_REL); | |
5259 | ||
5260 | typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL, | |
5261 | Relocatable_size_for_reloc> Scan_relocatable_relocs; | |
5262 | ||
5263 | gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL, | |
5264 | Scan_relocatable_relocs>( | |
4a657b0d DK |
5265 | symtab, |
5266 | layout, | |
5267 | object, | |
5268 | data_shndx, | |
5269 | prelocs, | |
5270 | reloc_count, | |
5271 | output_section, | |
5272 | needs_special_offset_handling, | |
5273 | local_symbol_count, | |
5274 | plocal_symbols, | |
5275 | rr); | |
5276 | } | |
5277 | ||
5278 | // Relocate a section during a relocatable link. | |
5279 | ||
5280 | template<bool big_endian> | |
5281 | void | |
5282 | Target_arm<big_endian>::relocate_for_relocatable( | |
5283 | const Relocate_info<32, big_endian>* relinfo, | |
5284 | unsigned int sh_type, | |
5285 | const unsigned char* prelocs, | |
5286 | size_t reloc_count, | |
5287 | Output_section* output_section, | |
5288 | off_t offset_in_output_section, | |
5289 | const Relocatable_relocs* rr, | |
5290 | unsigned char* view, | |
ebabffbd | 5291 | Arm_address view_address, |
4a657b0d DK |
5292 | section_size_type view_size, |
5293 | unsigned char* reloc_view, | |
5294 | section_size_type reloc_view_size) | |
5295 | { | |
5296 | gold_assert(sh_type == elfcpp::SHT_REL); | |
5297 | ||
5298 | gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>( | |
5299 | relinfo, | |
5300 | prelocs, | |
5301 | reloc_count, | |
5302 | output_section, | |
5303 | offset_in_output_section, | |
5304 | rr, | |
5305 | view, | |
5306 | view_address, | |
5307 | view_size, | |
5308 | reloc_view, | |
5309 | reloc_view_size); | |
5310 | } | |
5311 | ||
94cdfcff DK |
5312 | // Return the value to use for a dynamic symbol which requires special |
5313 | // treatment. This is how we support equality comparisons of function | |
5314 | // pointers across shared library boundaries, as described in the | |
5315 | // processor specific ABI supplement. | |
5316 | ||
4a657b0d DK |
5317 | template<bool big_endian> |
5318 | uint64_t | |
94cdfcff | 5319 | Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const |
4a657b0d | 5320 | { |
94cdfcff DK |
5321 | gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); |
5322 | return this->plt_section()->address() + gsym->plt_offset(); | |
4a657b0d DK |
5323 | } |
5324 | ||
5325 | // Map platform-specific relocs to real relocs | |
5326 | // | |
5327 | template<bool big_endian> | |
5328 | unsigned int | |
a6d1ef57 | 5329 | Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type) |
4a657b0d DK |
5330 | { |
5331 | switch (r_type) | |
5332 | { | |
5333 | case elfcpp::R_ARM_TARGET1: | |
a6d1ef57 DK |
5334 | // This is either R_ARM_ABS32 or R_ARM_REL32; |
5335 | return elfcpp::R_ARM_ABS32; | |
4a657b0d DK |
5336 | |
5337 | case elfcpp::R_ARM_TARGET2: | |
a6d1ef57 DK |
5338 | // This can be any reloc type but ususally is R_ARM_GOT_PREL |
5339 | return elfcpp::R_ARM_GOT_PREL; | |
4a657b0d DK |
5340 | |
5341 | default: | |
5342 | return r_type; | |
5343 | } | |
5344 | } | |
5345 | ||
d5b40221 DK |
5346 | // Whether if two EABI versions V1 and V2 are compatible. |
5347 | ||
5348 | template<bool big_endian> | |
5349 | bool | |
5350 | Target_arm<big_endian>::are_eabi_versions_compatible( | |
5351 | elfcpp::Elf_Word v1, | |
5352 | elfcpp::Elf_Word v2) | |
5353 | { | |
5354 | // v4 and v5 are the same spec before and after it was released, | |
5355 | // so allow mixing them. | |
5356 | if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5) | |
5357 | || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4)) | |
5358 | return true; | |
5359 | ||
5360 | return v1 == v2; | |
5361 | } | |
5362 | ||
5363 | // Combine FLAGS from an input object called NAME and the processor-specific | |
5364 | // flags in the ELF header of the output. Much of this is adapted from the | |
5365 | // processor-specific flags merging code in elf32_arm_merge_private_bfd_data | |
5366 | // in bfd/elf32-arm.c. | |
5367 | ||
5368 | template<bool big_endian> | |
5369 | void | |
5370 | Target_arm<big_endian>::merge_processor_specific_flags( | |
5371 | const std::string& name, | |
5372 | elfcpp::Elf_Word flags) | |
5373 | { | |
5374 | if (this->are_processor_specific_flags_set()) | |
5375 | { | |
5376 | elfcpp::Elf_Word out_flags = this->processor_specific_flags(); | |
5377 | ||
5378 | // Nothing to merge if flags equal to those in output. | |
5379 | if (flags == out_flags) | |
5380 | return; | |
5381 | ||
5382 | // Complain about various flag mismatches. | |
5383 | elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags); | |
5384 | elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags); | |
5385 | if (!this->are_eabi_versions_compatible(version1, version2)) | |
5386 | gold_error(_("Source object %s has EABI version %d but output has " | |
5387 | "EABI version %d."), | |
5388 | name.c_str(), | |
5389 | (flags & elfcpp::EF_ARM_EABIMASK) >> 24, | |
5390 | (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24); | |
5391 | } | |
5392 | else | |
5393 | { | |
5394 | // If the input is the default architecture and had the default | |
5395 | // flags then do not bother setting the flags for the output | |
5396 | // architecture, instead allow future merges to do this. If no | |
5397 | // future merges ever set these flags then they will retain their | |
5398 | // uninitialised values, which surprise surprise, correspond | |
5399 | // to the default values. | |
5400 | if (flags == 0) | |
5401 | return; | |
5402 | ||
5403 | // This is the first time, just copy the flags. | |
5404 | // We only copy the EABI version for now. | |
5405 | this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK); | |
5406 | } | |
5407 | } | |
5408 | ||
5409 | // Adjust ELF file header. | |
5410 | template<bool big_endian> | |
5411 | void | |
5412 | Target_arm<big_endian>::do_adjust_elf_header( | |
5413 | unsigned char* view, | |
5414 | int len) const | |
5415 | { | |
5416 | gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size); | |
5417 | ||
5418 | elfcpp::Ehdr<32, big_endian> ehdr(view); | |
5419 | unsigned char e_ident[elfcpp::EI_NIDENT]; | |
5420 | memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT); | |
5421 | ||
5422 | if (elfcpp::arm_eabi_version(this->processor_specific_flags()) | |
5423 | == elfcpp::EF_ARM_EABI_UNKNOWN) | |
5424 | e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM; | |
5425 | else | |
5426 | e_ident[elfcpp::EI_OSABI] = 0; | |
5427 | e_ident[elfcpp::EI_ABIVERSION] = 0; | |
5428 | ||
5429 | // FIXME: Do EF_ARM_BE8 adjustment. | |
5430 | ||
5431 | elfcpp::Ehdr_write<32, big_endian> oehdr(view); | |
5432 | oehdr.put_e_ident(e_ident); | |
5433 | } | |
5434 | ||
5435 | // do_make_elf_object to override the same function in the base class. | |
5436 | // We need to use a target-specific sub-class of Sized_relobj<32, big_endian> | |
5437 | // to store ARM specific information. Hence we need to have our own | |
5438 | // ELF object creation. | |
5439 | ||
5440 | template<bool big_endian> | |
5441 | Object* | |
5442 | Target_arm<big_endian>::do_make_elf_object( | |
5443 | const std::string& name, | |
5444 | Input_file* input_file, | |
5445 | off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr) | |
5446 | { | |
5447 | int et = ehdr.get_e_type(); | |
5448 | if (et == elfcpp::ET_REL) | |
5449 | { | |
5450 | Arm_relobj<big_endian>* obj = | |
5451 | new Arm_relobj<big_endian>(name, input_file, offset, ehdr); | |
5452 | obj->setup(); | |
5453 | return obj; | |
5454 | } | |
5455 | else if (et == elfcpp::ET_DYN) | |
5456 | { | |
5457 | Sized_dynobj<32, big_endian>* obj = | |
5458 | new Arm_dynobj<big_endian>(name, input_file, offset, ehdr); | |
5459 | obj->setup(); | |
5460 | return obj; | |
5461 | } | |
5462 | else | |
5463 | { | |
5464 | gold_error(_("%s: unsupported ELF file type %d"), | |
5465 | name.c_str(), et); | |
5466 | return NULL; | |
5467 | } | |
5468 | } | |
5469 | ||
55da9579 DK |
5470 | // Return whether a relocation type used the LSB to distinguish THUMB |
5471 | // addresses. | |
5472 | template<bool big_endian> | |
5473 | bool | |
5474 | Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type) | |
5475 | { | |
5476 | switch (r_type) | |
5477 | { | |
5478 | case elfcpp::R_ARM_PC24: | |
5479 | case elfcpp::R_ARM_ABS32: | |
5480 | case elfcpp::R_ARM_REL32: | |
5481 | case elfcpp::R_ARM_SBREL32: | |
5482 | case elfcpp::R_ARM_THM_CALL: | |
5483 | case elfcpp::R_ARM_GLOB_DAT: | |
5484 | case elfcpp::R_ARM_JUMP_SLOT: | |
5485 | case elfcpp::R_ARM_GOTOFF32: | |
5486 | case elfcpp::R_ARM_PLT32: | |
5487 | case elfcpp::R_ARM_CALL: | |
5488 | case elfcpp::R_ARM_JUMP24: | |
5489 | case elfcpp::R_ARM_THM_JUMP24: | |
5490 | case elfcpp::R_ARM_SBREL31: | |
5491 | case elfcpp::R_ARM_PREL31: | |
5492 | case elfcpp::R_ARM_MOVW_ABS_NC: | |
5493 | case elfcpp::R_ARM_MOVW_PREL_NC: | |
5494 | case elfcpp::R_ARM_THM_MOVW_ABS_NC: | |
5495 | case elfcpp::R_ARM_THM_MOVW_PREL_NC: | |
5496 | case elfcpp::R_ARM_THM_JUMP19: | |
5497 | case elfcpp::R_ARM_THM_ALU_PREL_11_0: | |
5498 | case elfcpp::R_ARM_ALU_PC_G0_NC: | |
5499 | case elfcpp::R_ARM_ALU_PC_G0: | |
5500 | case elfcpp::R_ARM_ALU_PC_G1_NC: | |
5501 | case elfcpp::R_ARM_ALU_PC_G1: | |
5502 | case elfcpp::R_ARM_ALU_PC_G2: | |
5503 | case elfcpp::R_ARM_ALU_SB_G0_NC: | |
5504 | case elfcpp::R_ARM_ALU_SB_G0: | |
5505 | case elfcpp::R_ARM_ALU_SB_G1_NC: | |
5506 | case elfcpp::R_ARM_ALU_SB_G1: | |
5507 | case elfcpp::R_ARM_ALU_SB_G2: | |
5508 | case elfcpp::R_ARM_MOVW_BREL_NC: | |
5509 | case elfcpp::R_ARM_MOVW_BREL: | |
5510 | case elfcpp::R_ARM_THM_MOVW_BREL_NC: | |
5511 | case elfcpp::R_ARM_THM_MOVW_BREL: | |
5512 | return true; | |
5513 | default: | |
5514 | return false; | |
5515 | } | |
5516 | } | |
5517 | ||
5518 | // Stub-generation methods for Target_arm. | |
5519 | ||
5520 | // Make a new Arm_input_section object. | |
5521 | ||
5522 | template<bool big_endian> | |
5523 | Arm_input_section<big_endian>* | |
5524 | Target_arm<big_endian>::new_arm_input_section( | |
5525 | Relobj* relobj, | |
5526 | unsigned int shndx) | |
5527 | { | |
5528 | Input_section_specifier iss(relobj, shndx); | |
5529 | ||
5530 | Arm_input_section<big_endian>* arm_input_section = | |
5531 | new Arm_input_section<big_endian>(relobj, shndx); | |
5532 | arm_input_section->init(); | |
5533 | ||
5534 | // Register new Arm_input_section in map for look-up. | |
5535 | std::pair<typename Arm_input_section_map::iterator, bool> ins = | |
5536 | this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section)); | |
5537 | ||
5538 | // Make sure that it we have not created another Arm_input_section | |
5539 | // for this input section already. | |
5540 | gold_assert(ins.second); | |
5541 | ||
5542 | return arm_input_section; | |
5543 | } | |
5544 | ||
5545 | // Find the Arm_input_section object corresponding to the SHNDX-th input | |
5546 | // section of RELOBJ. | |
5547 | ||
5548 | template<bool big_endian> | |
5549 | Arm_input_section<big_endian>* | |
5550 | Target_arm<big_endian>::find_arm_input_section( | |
5551 | Relobj* relobj, | |
5552 | unsigned int shndx) const | |
5553 | { | |
5554 | Input_section_specifier iss(relobj, shndx); | |
5555 | typename Arm_input_section_map::const_iterator p = | |
5556 | this->arm_input_section_map_.find(iss); | |
5557 | return (p != this->arm_input_section_map_.end()) ? p->second : NULL; | |
5558 | } | |
5559 | ||
5560 | // Make a new stub table. | |
5561 | ||
5562 | template<bool big_endian> | |
5563 | Stub_table<big_endian>* | |
5564 | Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner) | |
5565 | { | |
5566 | Stub_table<big_endian>* stub_table = | |
5567 | new Stub_table<big_endian>(owner); | |
5568 | this->stub_tables_.push_back(stub_table); | |
5569 | ||
5570 | stub_table->set_address(owner->address() + owner->data_size()); | |
5571 | stub_table->set_file_offset(owner->offset() + owner->data_size()); | |
5572 | stub_table->finalize_data_size(); | |
5573 | ||
5574 | return stub_table; | |
5575 | } | |
5576 | ||
eb44217c DK |
5577 | // Scan a relocation for stub generation. |
5578 | ||
5579 | template<bool big_endian> | |
5580 | void | |
5581 | Target_arm<big_endian>::scan_reloc_for_stub( | |
5582 | const Relocate_info<32, big_endian>* relinfo, | |
5583 | unsigned int r_type, | |
5584 | const Sized_symbol<32>* gsym, | |
5585 | unsigned int r_sym, | |
5586 | const Symbol_value<32>* psymval, | |
5587 | elfcpp::Elf_types<32>::Elf_Swxword addend, | |
5588 | Arm_address address) | |
5589 | { | |
5590 | typedef typename Target_arm<big_endian>::Relocate Relocate; | |
5591 | ||
5592 | const Arm_relobj<big_endian>* arm_relobj = | |
5593 | Arm_relobj<big_endian>::as_arm_relobj(relinfo->object); | |
5594 | ||
5595 | bool target_is_thumb; | |
5596 | Symbol_value<32> symval; | |
5597 | if (gsym != NULL) | |
5598 | { | |
5599 | // This is a global symbol. Determine if we use PLT and if the | |
5600 | // final target is THUMB. | |
5601 | if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type))) | |
5602 | { | |
5603 | // This uses a PLT, change the symbol value. | |
5604 | symval.set_output_value(this->plt_section()->address() | |
5605 | + gsym->plt_offset()); | |
5606 | psymval = &symval; | |
5607 | target_is_thumb = false; | |
5608 | } | |
5609 | else if (gsym->is_undefined()) | |
5610 | // There is no need to generate a stub symbol is undefined. | |
5611 | return; | |
5612 | else | |
5613 | { | |
5614 | target_is_thumb = | |
5615 | ((gsym->type() == elfcpp::STT_ARM_TFUNC) | |
5616 | || (gsym->type() == elfcpp::STT_FUNC | |
5617 | && !gsym->is_undefined() | |
5618 | && ((psymval->value(arm_relobj, 0) & 1) != 0))); | |
5619 | } | |
5620 | } | |
5621 | else | |
5622 | { | |
5623 | // This is a local symbol. Determine if the final target is THUMB. | |
5624 | target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym); | |
5625 | } | |
5626 | ||
5627 | // Strip LSB if this points to a THUMB target. | |
5628 | if (target_is_thumb | |
5629 | && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type) | |
5630 | && ((psymval->value(arm_relobj, 0) & 1) != 0)) | |
5631 | { | |
5632 | Arm_address stripped_value = | |
5633 | psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1); | |
5634 | symval.set_output_value(stripped_value); | |
5635 | psymval = &symval; | |
5636 | } | |
5637 | ||
5638 | // Get the symbol value. | |
5639 | Symbol_value<32>::Value value = psymval->value(arm_relobj, 0); | |
5640 | ||
5641 | // Owing to pipelining, the PC relative branches below actually skip | |
5642 | // two instructions when the branch offset is 0. | |
5643 | Arm_address destination; | |
5644 | switch (r_type) | |
5645 | { | |
5646 | case elfcpp::R_ARM_CALL: | |
5647 | case elfcpp::R_ARM_JUMP24: | |
5648 | case elfcpp::R_ARM_PLT32: | |
5649 | // ARM branches. | |
5650 | destination = value + addend + 8; | |
5651 | break; | |
5652 | case elfcpp::R_ARM_THM_CALL: | |
5653 | case elfcpp::R_ARM_THM_XPC22: | |
5654 | case elfcpp::R_ARM_THM_JUMP24: | |
5655 | case elfcpp::R_ARM_THM_JUMP19: | |
5656 | // THUMB branches. | |
5657 | destination = value + addend + 4; | |
5658 | break; | |
5659 | default: | |
5660 | gold_unreachable(); | |
5661 | } | |
5662 | ||
5663 | Stub_type stub_type = | |
5664 | Reloc_stub::stub_type_for_reloc(r_type, address, destination, | |
5665 | target_is_thumb); | |
5666 | ||
5667 | // This reloc does not need a stub. | |
5668 | if (stub_type == arm_stub_none) | |
5669 | return; | |
5670 | ||
5671 | // Try looking up an existing stub from a stub table. | |
5672 | Stub_table<big_endian>* stub_table = | |
5673 | arm_relobj->stub_table(relinfo->data_shndx); | |
5674 | gold_assert(stub_table != NULL); | |
5675 | ||
5676 | // Locate stub by destination. | |
5677 | Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend); | |
5678 | ||
5679 | // Create a stub if there is not one already | |
5680 | Reloc_stub* stub = stub_table->find_reloc_stub(stub_key); | |
5681 | if (stub == NULL) | |
5682 | { | |
5683 | // create a new stub and add it to stub table. | |
5684 | stub = this->stub_factory().make_reloc_stub(stub_type); | |
5685 | stub_table->add_reloc_stub(stub, stub_key); | |
5686 | } | |
5687 | ||
5688 | // Record the destination address. | |
5689 | stub->set_destination_address(destination | |
5690 | | (target_is_thumb ? 1 : 0)); | |
5691 | } | |
5692 | ||
5693 | // This function scans a relocation sections for stub generation. | |
5694 | // The template parameter Relocate must be a class type which provides | |
5695 | // a single function, relocate(), which implements the machine | |
5696 | // specific part of a relocation. | |
5697 | ||
5698 | // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type: | |
5699 | // SHT_REL or SHT_RELA. | |
5700 | ||
5701 | // PRELOCS points to the relocation data. RELOC_COUNT is the number | |
5702 | // of relocs. OUTPUT_SECTION is the output section. | |
5703 | // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be | |
5704 | // mapped to output offsets. | |
5705 | ||
5706 | // VIEW is the section data, VIEW_ADDRESS is its memory address, and | |
5707 | // VIEW_SIZE is the size. These refer to the input section, unless | |
5708 | // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to | |
5709 | // the output section. | |
5710 | ||
5711 | template<bool big_endian> | |
5712 | template<int sh_type> | |
5713 | void inline | |
5714 | Target_arm<big_endian>::scan_reloc_section_for_stubs( | |
5715 | const Relocate_info<32, big_endian>* relinfo, | |
5716 | const unsigned char* prelocs, | |
5717 | size_t reloc_count, | |
5718 | Output_section* output_section, | |
5719 | bool needs_special_offset_handling, | |
5720 | const unsigned char* view, | |
5721 | elfcpp::Elf_types<32>::Elf_Addr view_address, | |
5722 | section_size_type) | |
5723 | { | |
5724 | typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype; | |
5725 | const int reloc_size = | |
5726 | Reloc_types<sh_type, 32, big_endian>::reloc_size; | |
5727 | ||
5728 | Arm_relobj<big_endian>* arm_object = | |
5729 | Arm_relobj<big_endian>::as_arm_relobj(relinfo->object); | |
5730 | unsigned int local_count = arm_object->local_symbol_count(); | |
5731 | ||
5732 | Comdat_behavior comdat_behavior = CB_UNDETERMINED; | |
5733 | ||
5734 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
5735 | { | |
5736 | Reltype reloc(prelocs); | |
5737 | ||
5738 | typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info(); | |
5739 | unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info); | |
5740 | unsigned int r_type = elfcpp::elf_r_type<32>(r_info); | |
5741 | ||
5742 | r_type = this->get_real_reloc_type(r_type); | |
5743 | ||
5744 | // Only a few relocation types need stubs. | |
5745 | if ((r_type != elfcpp::R_ARM_CALL) | |
5746 | && (r_type != elfcpp::R_ARM_JUMP24) | |
5747 | && (r_type != elfcpp::R_ARM_PLT32) | |
5748 | && (r_type != elfcpp::R_ARM_THM_CALL) | |
5749 | && (r_type != elfcpp::R_ARM_THM_XPC22) | |
5750 | && (r_type != elfcpp::R_ARM_THM_JUMP24) | |
5751 | && (r_type != elfcpp::R_ARM_THM_JUMP19)) | |
5752 | continue; | |
5753 | ||
5754 | section_offset_type offset = | |
5755 | convert_to_section_size_type(reloc.get_r_offset()); | |
5756 | ||
5757 | if (needs_special_offset_handling) | |
5758 | { | |
5759 | offset = output_section->output_offset(relinfo->object, | |
5760 | relinfo->data_shndx, | |
5761 | offset); | |
5762 | if (offset == -1) | |
5763 | continue; | |
5764 | } | |
5765 | ||
5766 | // Get the addend. | |
5767 | Stub_addend_reader<sh_type, big_endian> stub_addend_reader; | |
5768 | elfcpp::Elf_types<32>::Elf_Swxword addend = | |
5769 | stub_addend_reader(r_type, view + offset, reloc); | |
5770 | ||
5771 | const Sized_symbol<32>* sym; | |
5772 | ||
5773 | Symbol_value<32> symval; | |
5774 | const Symbol_value<32> *psymval; | |
5775 | if (r_sym < local_count) | |
5776 | { | |
5777 | sym = NULL; | |
5778 | psymval = arm_object->local_symbol(r_sym); | |
5779 | ||
5780 | // If the local symbol belongs to a section we are discarding, | |
5781 | // and that section is a debug section, try to find the | |
5782 | // corresponding kept section and map this symbol to its | |
5783 | // counterpart in the kept section. The symbol must not | |
5784 | // correspond to a section we are folding. | |
5785 | bool is_ordinary; | |
5786 | unsigned int shndx = psymval->input_shndx(&is_ordinary); | |
5787 | if (is_ordinary | |
5788 | && shndx != elfcpp::SHN_UNDEF | |
5789 | && !arm_object->is_section_included(shndx) | |
5790 | && !(relinfo->symtab->is_section_folded(arm_object, shndx))) | |
5791 | { | |
5792 | if (comdat_behavior == CB_UNDETERMINED) | |
5793 | { | |
5794 | std::string name = | |
5795 | arm_object->section_name(relinfo->data_shndx); | |
5796 | comdat_behavior = get_comdat_behavior(name.c_str()); | |
5797 | } | |
5798 | if (comdat_behavior == CB_PRETEND) | |
5799 | { | |
5800 | bool found; | |
5801 | typename elfcpp::Elf_types<32>::Elf_Addr value = | |
5802 | arm_object->map_to_kept_section(shndx, &found); | |
5803 | if (found) | |
5804 | symval.set_output_value(value + psymval->input_value()); | |
5805 | else | |
5806 | symval.set_output_value(0); | |
5807 | } | |
5808 | else | |
5809 | { | |
5810 | symval.set_output_value(0); | |
5811 | } | |
5812 | symval.set_no_output_symtab_entry(); | |
5813 | psymval = &symval; | |
5814 | } | |
5815 | } | |
5816 | else | |
5817 | { | |
5818 | const Symbol* gsym = arm_object->global_symbol(r_sym); | |
5819 | gold_assert(gsym != NULL); | |
5820 | if (gsym->is_forwarder()) | |
5821 | gsym = relinfo->symtab->resolve_forwards(gsym); | |
5822 | ||
5823 | sym = static_cast<const Sized_symbol<32>*>(gsym); | |
5824 | if (sym->has_symtab_index()) | |
5825 | symval.set_output_symtab_index(sym->symtab_index()); | |
5826 | else | |
5827 | symval.set_no_output_symtab_entry(); | |
5828 | ||
5829 | // We need to compute the would-be final value of this global | |
5830 | // symbol. | |
5831 | const Symbol_table* symtab = relinfo->symtab; | |
5832 | const Sized_symbol<32>* sized_symbol = | |
5833 | symtab->get_sized_symbol<32>(gsym); | |
5834 | Symbol_table::Compute_final_value_status status; | |
5835 | Arm_address value = | |
5836 | symtab->compute_final_value<32>(sized_symbol, &status); | |
5837 | ||
5838 | // Skip this if the symbol has not output section. | |
5839 | if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION) | |
5840 | continue; | |
5841 | ||
5842 | symval.set_output_value(value); | |
5843 | psymval = &symval; | |
5844 | } | |
5845 | ||
5846 | // If symbol is a section symbol, we don't know the actual type of | |
5847 | // destination. Give up. | |
5848 | if (psymval->is_section_symbol()) | |
5849 | continue; | |
5850 | ||
5851 | this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval, | |
5852 | addend, view_address + offset); | |
5853 | } | |
5854 | } | |
5855 | ||
5856 | // Scan an input section for stub generation. | |
5857 | ||
5858 | template<bool big_endian> | |
5859 | void | |
5860 | Target_arm<big_endian>::scan_section_for_stubs( | |
5861 | const Relocate_info<32, big_endian>* relinfo, | |
5862 | unsigned int sh_type, | |
5863 | const unsigned char* prelocs, | |
5864 | size_t reloc_count, | |
5865 | Output_section* output_section, | |
5866 | bool needs_special_offset_handling, | |
5867 | const unsigned char* view, | |
5868 | Arm_address view_address, | |
5869 | section_size_type view_size) | |
5870 | { | |
5871 | if (sh_type == elfcpp::SHT_REL) | |
5872 | this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>( | |
5873 | relinfo, | |
5874 | prelocs, | |
5875 | reloc_count, | |
5876 | output_section, | |
5877 | needs_special_offset_handling, | |
5878 | view, | |
5879 | view_address, | |
5880 | view_size); | |
5881 | else if (sh_type == elfcpp::SHT_RELA) | |
5882 | // We do not support RELA type relocations yet. This is provided for | |
5883 | // completeness. | |
5884 | this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>( | |
5885 | relinfo, | |
5886 | prelocs, | |
5887 | reloc_count, | |
5888 | output_section, | |
5889 | needs_special_offset_handling, | |
5890 | view, | |
5891 | view_address, | |
5892 | view_size); | |
5893 | else | |
5894 | gold_unreachable(); | |
5895 | } | |
5896 | ||
5897 | // Group input sections for stub generation. | |
5898 | // | |
5899 | // We goup input sections in an output sections so that the total size, | |
5900 | // including any padding space due to alignment is smaller than GROUP_SIZE | |
5901 | // unless the only input section in group is bigger than GROUP_SIZE already. | |
5902 | // Then an ARM stub table is created to follow the last input section | |
5903 | // in group. For each group an ARM stub table is created an is placed | |
5904 | // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further | |
5905 | // extend the group after the stub table. | |
5906 | ||
5907 | template<bool big_endian> | |
5908 | void | |
5909 | Target_arm<big_endian>::group_sections( | |
5910 | Layout* layout, | |
5911 | section_size_type group_size, | |
5912 | bool stubs_always_after_branch) | |
5913 | { | |
5914 | // Group input sections and insert stub table | |
5915 | Layout::Section_list section_list; | |
5916 | layout->get_allocated_sections(§ion_list); | |
5917 | for (Layout::Section_list::const_iterator p = section_list.begin(); | |
5918 | p != section_list.end(); | |
5919 | ++p) | |
5920 | { | |
5921 | Arm_output_section<big_endian>* output_section = | |
5922 | Arm_output_section<big_endian>::as_arm_output_section(*p); | |
5923 | output_section->group_sections(group_size, stubs_always_after_branch, | |
5924 | this); | |
5925 | } | |
5926 | } | |
5927 | ||
5928 | // Relaxation hook. This is where we do stub generation. | |
5929 | ||
5930 | template<bool big_endian> | |
5931 | bool | |
5932 | Target_arm<big_endian>::do_relax( | |
5933 | int pass, | |
5934 | const Input_objects* input_objects, | |
5935 | Symbol_table* symtab, | |
5936 | Layout* layout) | |
5937 | { | |
5938 | // No need to generate stubs if this is a relocatable link. | |
5939 | gold_assert(!parameters->options().relocatable()); | |
5940 | ||
5941 | // If this is the first pass, we need to group input sections into | |
5942 | // stub groups. | |
5943 | if (pass == 1) | |
5944 | { | |
5945 | // Determine the stub group size. The group size is the absolute | |
5946 | // value of the parameter --stub-group-size. If --stub-group-size | |
5947 | // is passed a negative value, we restict stubs to be always after | |
5948 | // the stubbed branches. | |
5949 | int32_t stub_group_size_param = | |
5950 | parameters->options().stub_group_size(); | |
5951 | bool stubs_always_after_branch = stub_group_size_param < 0; | |
5952 | section_size_type stub_group_size = abs(stub_group_size_param); | |
5953 | ||
5954 | if (stub_group_size == 1) | |
5955 | { | |
5956 | // Default value. | |
5957 | // Thumb branch range is +-4MB has to be used as the default | |
5958 | // maximum size (a given section can contain both ARM and Thumb | |
5959 | // code, so the worst case has to be taken into account). | |
5960 | // | |
5961 | // This value is 24K less than that, which allows for 2025 | |
5962 | // 12-byte stubs. If we exceed that, then we will fail to link. | |
5963 | // The user will have to relink with an explicit group size | |
5964 | // option. | |
5965 | stub_group_size = 4170000; | |
5966 | } | |
5967 | ||
5968 | group_sections(layout, stub_group_size, stubs_always_after_branch); | |
5969 | } | |
5970 | ||
5971 | // clear changed flags for all stub_tables | |
5972 | typedef typename Stub_table_list::iterator Stub_table_iterator; | |
5973 | for (Stub_table_iterator sp = this->stub_tables_.begin(); | |
5974 | sp != this->stub_tables_.end(); | |
5975 | ++sp) | |
5976 | (*sp)->set_has_been_changed(false); | |
5977 | ||
5978 | // scan relocs for stubs | |
5979 | for (Input_objects::Relobj_iterator op = input_objects->relobj_begin(); | |
5980 | op != input_objects->relobj_end(); | |
5981 | ++op) | |
5982 | { | |
5983 | Arm_relobj<big_endian>* arm_relobj = | |
5984 | Arm_relobj<big_endian>::as_arm_relobj(*op); | |
5985 | arm_relobj->scan_sections_for_stubs(this, symtab, layout); | |
5986 | } | |
5987 | ||
5988 | bool any_stub_table_changed = false; | |
5989 | for (Stub_table_iterator sp = this->stub_tables_.begin(); | |
5990 | (sp != this->stub_tables_.end()) && !any_stub_table_changed; | |
5991 | ++sp) | |
5992 | { | |
5993 | if ((*sp)->has_been_changed()) | |
5994 | any_stub_table_changed = true; | |
5995 | } | |
5996 | ||
5997 | return any_stub_table_changed; | |
5998 | } | |
5999 | ||
43d12afe DK |
6000 | // Relocate a stub. |
6001 | ||
6002 | template<bool big_endian> | |
6003 | void | |
6004 | Target_arm<big_endian>::relocate_stub( | |
6005 | Reloc_stub* stub, | |
6006 | const Relocate_info<32, big_endian>* relinfo, | |
6007 | Output_section* output_section, | |
6008 | unsigned char* view, | |
6009 | Arm_address address, | |
6010 | section_size_type view_size) | |
6011 | { | |
6012 | Relocate relocate; | |
6013 | const Stub_template* stub_template = stub->stub_template(); | |
6014 | for (size_t i = 0; i < stub_template->reloc_count(); i++) | |
6015 | { | |
6016 | size_t reloc_insn_index = stub_template->reloc_insn_index(i); | |
6017 | const Insn_template* insn = &stub_template->insns()[reloc_insn_index]; | |
6018 | ||
6019 | unsigned int r_type = insn->r_type(); | |
6020 | section_size_type reloc_offset = stub_template->reloc_offset(i); | |
6021 | section_size_type reloc_size = insn->size(); | |
6022 | gold_assert(reloc_offset + reloc_size <= view_size); | |
6023 | ||
6024 | // This is the address of the stub destination. | |
6025 | Arm_address target = stub->reloc_target(i); | |
6026 | Symbol_value<32> symval; | |
6027 | symval.set_output_value(target); | |
6028 | ||
6029 | // Synthesize a fake reloc just in case. We don't have a symbol so | |
6030 | // we use 0. | |
6031 | unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size]; | |
6032 | memset(reloc_buffer, 0, sizeof(reloc_buffer)); | |
6033 | elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer); | |
6034 | reloc_write.put_r_offset(reloc_offset); | |
6035 | reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type)); | |
6036 | elfcpp::Rel<32, big_endian> rel(reloc_buffer); | |
6037 | ||
6038 | relocate.relocate(relinfo, this, output_section, | |
6039 | this->fake_relnum_for_stubs, rel, r_type, | |
6040 | NULL, &symval, view + reloc_offset, | |
6041 | address + reloc_offset, reloc_size); | |
6042 | } | |
6043 | } | |
6044 | ||
4a657b0d DK |
6045 | // The selector for arm object files. |
6046 | ||
6047 | template<bool big_endian> | |
6048 | class Target_selector_arm : public Target_selector | |
6049 | { | |
6050 | public: | |
6051 | Target_selector_arm() | |
6052 | : Target_selector(elfcpp::EM_ARM, 32, big_endian, | |
6053 | (big_endian ? "elf32-bigarm" : "elf32-littlearm")) | |
6054 | { } | |
6055 | ||
6056 | Target* | |
6057 | do_instantiate_target() | |
6058 | { return new Target_arm<big_endian>(); } | |
6059 | }; | |
6060 | ||
6061 | Target_selector_arm<false> target_selector_arm; | |
6062 | Target_selector_arm<true> target_selector_armbe; | |
6063 | ||
6064 | } // End anonymous namespace. |