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