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