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