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