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>. | |
6 | ||
7 | // This file is part of gold. | |
8 | ||
9 | // This program is free software; you can redistribute it and/or modify | |
10 | // it under the terms of the GNU General Public License as published by | |
11 | // the Free Software Foundation; either version 3 of the License, or | |
12 | // (at your option) any later version. | |
13 | ||
14 | // This program is distributed in the hope that it will be useful, | |
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | // GNU General Public License for more details. | |
18 | ||
19 | // You should have received a copy of the GNU General Public License | |
20 | // along with this program; if not, write to the Free Software | |
21 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
22 | // MA 02110-1301, USA. | |
23 | ||
24 | #include "gold.h" | |
25 | ||
26 | #include <cstring> | |
27 | #include <limits> | |
28 | #include <cstdio> | |
29 | #include <string> | |
30 | ||
31 | #include "elfcpp.h" | |
32 | #include "parameters.h" | |
33 | #include "reloc.h" | |
34 | #include "arm.h" | |
35 | #include "object.h" | |
36 | #include "symtab.h" | |
37 | #include "layout.h" | |
38 | #include "output.h" | |
39 | #include "copy-relocs.h" | |
40 | #include "target.h" | |
41 | #include "target-reloc.h" | |
42 | #include "target-select.h" | |
43 | #include "tls.h" | |
44 | #include "defstd.h" | |
45 | ||
46 | namespace | |
47 | { | |
48 | ||
49 | using namespace gold; | |
50 | ||
94cdfcff DK |
51 | template<bool big_endian> |
52 | class Output_data_plt_arm; | |
53 | ||
4a657b0d DK |
54 | // The arm target class. |
55 | // | |
56 | // This is a very simple port of gold for ARM-EABI. It is intended for | |
57 | // supporting Android only for the time being. Only these relocation types | |
58 | // are supported. | |
59 | // | |
60 | // R_ARM_NONE | |
61 | // R_ARM_ABS32 | |
62 | // R_ARM_REL32 | |
63 | // R_ARM_THM_CALL | |
64 | // R_ARM_COPY | |
65 | // R_ARM_GLOB_DAT | |
66 | // R_ARM_BASE_PREL | |
67 | // R_ARM_JUMP_SLOT | |
68 | // R_ARM_RELATIVE | |
69 | // R_ARM_GOTOFF32 | |
70 | // R_ARM_GOT_BREL | |
71 | // R_ARM_PLT32 | |
72 | // R_ARM_CALL | |
73 | // R_ARM_JUMP24 | |
74 | // R_ARM_TARGET1 | |
75 | // R_ARM_PREL31 | |
76 | // | |
4a657b0d | 77 | // TODOs: |
11af873f DK |
78 | // - Generate various branch stubs. |
79 | // - Support interworking. | |
80 | // - Define section symbols __exidx_start and __exidx_stop. | |
4a657b0d | 81 | // - Support more relocation types as needed. |
94cdfcff DK |
82 | // - Make PLTs more flexible for different architecture features like |
83 | // Thumb-2 and BE8. | |
11af873f | 84 | // There are probably a lot more. |
4a657b0d | 85 | |
c121c671 DK |
86 | // Utilities for manipulating integers of up to 32-bits |
87 | ||
88 | namespace utils | |
89 | { | |
90 | // Sign extend an n-bit unsigned integer stored in an uint32_t into | |
91 | // an int32_t. NO_BITS must be between 1 to 32. | |
92 | template<int no_bits> | |
93 | static inline int32_t | |
94 | sign_extend(uint32_t bits) | |
95 | { | |
96d49306 | 96 | gold_assert(no_bits >= 0 && no_bits <= 32); |
c121c671 DK |
97 | if (no_bits == 32) |
98 | return static_cast<int32_t>(bits); | |
99 | uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits); | |
100 | bits &= mask; | |
101 | uint32_t top_bit = 1U << (no_bits - 1); | |
102 | int32_t as_signed = static_cast<int32_t>(bits); | |
103 | return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed; | |
104 | } | |
105 | ||
106 | // Detects overflow of an NO_BITS integer stored in a uint32_t. | |
107 | template<int no_bits> | |
108 | static inline bool | |
109 | has_overflow(uint32_t bits) | |
110 | { | |
96d49306 | 111 | gold_assert(no_bits >= 0 && no_bits <= 32); |
c121c671 DK |
112 | if (no_bits == 32) |
113 | return false; | |
114 | int32_t max = (1 << (no_bits - 1)) - 1; | |
115 | int32_t min = -(1 << (no_bits - 1)); | |
116 | int32_t as_signed = static_cast<int32_t>(bits); | |
117 | return as_signed > max || as_signed < min; | |
118 | } | |
119 | ||
5e445df6 ILT |
120 | // Detects overflow of an NO_BITS integer stored in a uint32_t when it |
121 | // fits in the given number of bits as either a signed or unsigned value. | |
122 | // For example, has_signed_unsigned_overflow<8> would check | |
123 | // -128 <= bits <= 255 | |
124 | template<int no_bits> | |
125 | static inline bool | |
126 | has_signed_unsigned_overflow(uint32_t bits) | |
127 | { | |
128 | gold_assert(no_bits >= 2 && no_bits <= 32); | |
129 | if (no_bits == 32) | |
130 | return false; | |
131 | int32_t max = static_cast<int32_t>((1U << no_bits) - 1); | |
132 | int32_t min = -(1 << (no_bits - 1)); | |
133 | int32_t as_signed = static_cast<int32_t>(bits); | |
134 | return as_signed > max || as_signed < min; | |
135 | } | |
136 | ||
c121c671 DK |
137 | // Select bits from A and B using bits in MASK. For each n in [0..31], |
138 | // the n-th bit in the result is chosen from the n-th bits of A and B. | |
139 | // A zero selects A and a one selects B. | |
140 | static inline uint32_t | |
141 | bit_select(uint32_t a, uint32_t b, uint32_t mask) | |
142 | { return (a & ~mask) | (b & mask); } | |
143 | }; | |
144 | ||
4a657b0d DK |
145 | template<bool big_endian> |
146 | class Target_arm : public Sized_target<32, big_endian> | |
147 | { | |
148 | public: | |
149 | typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian> | |
150 | Reloc_section; | |
151 | ||
152 | Target_arm() | |
94cdfcff DK |
153 | : Sized_target<32, big_endian>(&arm_info), |
154 | got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL), | |
155 | copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL) | |
4a657b0d DK |
156 | { } |
157 | ||
158 | // Process the relocations to determine unreferenced sections for | |
159 | // garbage collection. | |
160 | void | |
161 | gc_process_relocs(const General_options& options, | |
162 | Symbol_table* symtab, | |
163 | Layout* layout, | |
164 | Sized_relobj<32, big_endian>* object, | |
165 | unsigned int data_shndx, | |
166 | unsigned int sh_type, | |
167 | const unsigned char* prelocs, | |
168 | size_t reloc_count, | |
169 | Output_section* output_section, | |
170 | bool needs_special_offset_handling, | |
171 | size_t local_symbol_count, | |
172 | const unsigned char* plocal_symbols); | |
173 | ||
174 | // Scan the relocations to look for symbol adjustments. | |
175 | void | |
176 | scan_relocs(const General_options& options, | |
177 | Symbol_table* symtab, | |
178 | Layout* layout, | |
179 | Sized_relobj<32, big_endian>* object, | |
180 | unsigned int data_shndx, | |
181 | unsigned int sh_type, | |
182 | const unsigned char* prelocs, | |
183 | size_t reloc_count, | |
184 | Output_section* output_section, | |
185 | bool needs_special_offset_handling, | |
186 | size_t local_symbol_count, | |
187 | const unsigned char* plocal_symbols); | |
188 | ||
189 | // Finalize the sections. | |
190 | void | |
191 | do_finalize_sections(Layout*); | |
192 | ||
94cdfcff | 193 | // Return the value to use for a dynamic symbol which requires special |
4a657b0d DK |
194 | // treatment. |
195 | uint64_t | |
196 | do_dynsym_value(const Symbol*) const; | |
197 | ||
198 | // Relocate a section. | |
199 | void | |
200 | relocate_section(const Relocate_info<32, big_endian>*, | |
201 | unsigned int sh_type, | |
202 | const unsigned char* prelocs, | |
203 | size_t reloc_count, | |
204 | Output_section* output_section, | |
205 | bool needs_special_offset_handling, | |
206 | unsigned char* view, | |
207 | elfcpp::Elf_types<32>::Elf_Addr view_address, | |
364c7fa5 ILT |
208 | section_size_type view_size, |
209 | const Reloc_symbol_changes*); | |
4a657b0d DK |
210 | |
211 | // Scan the relocs during a relocatable link. | |
212 | void | |
213 | scan_relocatable_relocs(const General_options& options, | |
214 | Symbol_table* symtab, | |
215 | Layout* layout, | |
216 | Sized_relobj<32, big_endian>* object, | |
217 | unsigned int data_shndx, | |
218 | unsigned int sh_type, | |
219 | const unsigned char* prelocs, | |
220 | size_t reloc_count, | |
221 | Output_section* output_section, | |
222 | bool needs_special_offset_handling, | |
223 | size_t local_symbol_count, | |
224 | const unsigned char* plocal_symbols, | |
225 | Relocatable_relocs*); | |
226 | ||
227 | // Relocate a section during a relocatable link. | |
228 | void | |
229 | relocate_for_relocatable(const Relocate_info<32, big_endian>*, | |
230 | unsigned int sh_type, | |
231 | const unsigned char* prelocs, | |
232 | size_t reloc_count, | |
233 | Output_section* output_section, | |
234 | off_t offset_in_output_section, | |
235 | const Relocatable_relocs*, | |
236 | unsigned char* view, | |
237 | elfcpp::Elf_types<32>::Elf_Addr view_address, | |
238 | section_size_type view_size, | |
239 | unsigned char* reloc_view, | |
240 | section_size_type reloc_view_size); | |
241 | ||
242 | // Return whether SYM is defined by the ABI. | |
243 | bool | |
244 | do_is_defined_by_abi(Symbol* sym) const | |
245 | { return strcmp(sym->name(), "__tls_get_addr") == 0; } | |
246 | ||
94cdfcff DK |
247 | // Return the size of the GOT section. |
248 | section_size_type | |
249 | got_size() | |
250 | { | |
251 | gold_assert(this->got_ != NULL); | |
252 | return this->got_->data_size(); | |
253 | } | |
254 | ||
4a657b0d DK |
255 | // Map platform-specific reloc types |
256 | static unsigned int | |
257 | get_real_reloc_type (unsigned int r_type); | |
258 | ||
259 | private: | |
260 | // The class which scans relocations. | |
261 | class Scan | |
262 | { | |
263 | public: | |
264 | Scan() | |
bec53400 | 265 | : issued_non_pic_error_(false) |
4a657b0d DK |
266 | { } |
267 | ||
268 | inline void | |
269 | local(const General_options& options, Symbol_table* symtab, | |
270 | Layout* layout, Target_arm* target, | |
271 | Sized_relobj<32, big_endian>* object, | |
272 | unsigned int data_shndx, | |
273 | Output_section* output_section, | |
274 | const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type, | |
275 | const elfcpp::Sym<32, big_endian>& lsym); | |
276 | ||
277 | inline void | |
278 | global(const General_options& options, Symbol_table* symtab, | |
279 | Layout* layout, Target_arm* target, | |
280 | Sized_relobj<32, big_endian>* object, | |
281 | unsigned int data_shndx, | |
282 | Output_section* output_section, | |
283 | const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type, | |
284 | Symbol* gsym); | |
285 | ||
286 | private: | |
287 | static void | |
288 | unsupported_reloc_local(Sized_relobj<32, big_endian>*, | |
289 | unsigned int r_type); | |
290 | ||
291 | static void | |
292 | unsupported_reloc_global(Sized_relobj<32, big_endian>*, | |
293 | unsigned int r_type, Symbol*); | |
bec53400 DK |
294 | |
295 | void | |
296 | check_non_pic(Relobj*, unsigned int r_type); | |
297 | ||
298 | // Almost identical to Symbol::needs_plt_entry except that it also | |
299 | // handles STT_ARM_TFUNC. | |
300 | static bool | |
301 | symbol_needs_plt_entry(const Symbol* sym) | |
302 | { | |
303 | // An undefined symbol from an executable does not need a PLT entry. | |
304 | if (sym->is_undefined() && !parameters->options().shared()) | |
305 | return false; | |
306 | ||
307 | return (!parameters->doing_static_link() | |
308 | && (sym->type() == elfcpp::STT_FUNC | |
309 | || sym->type() == elfcpp::STT_ARM_TFUNC) | |
310 | && (sym->is_from_dynobj() | |
311 | || sym->is_undefined() | |
312 | || sym->is_preemptible())); | |
313 | } | |
314 | ||
315 | // Whether we have issued an error about a non-PIC compilation. | |
316 | bool issued_non_pic_error_; | |
4a657b0d DK |
317 | }; |
318 | ||
319 | // The class which implements relocation. | |
320 | class Relocate | |
321 | { | |
322 | public: | |
323 | Relocate() | |
324 | { } | |
325 | ||
326 | ~Relocate() | |
327 | { } | |
328 | ||
bec53400 DK |
329 | // Return whether the static relocation needs to be applied. |
330 | inline bool | |
331 | should_apply_static_reloc(const Sized_symbol<32>* gsym, | |
332 | int ref_flags, | |
333 | bool is_32bit, | |
334 | Output_section* output_section); | |
335 | ||
4a657b0d DK |
336 | // Do a relocation. Return false if the caller should not issue |
337 | // any warnings about this relocation. | |
338 | inline bool | |
339 | relocate(const Relocate_info<32, big_endian>*, Target_arm*, | |
340 | Output_section*, size_t relnum, | |
341 | const elfcpp::Rel<32, big_endian>&, | |
342 | unsigned int r_type, const Sized_symbol<32>*, | |
343 | const Symbol_value<32>*, | |
344 | unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, | |
345 | section_size_type); | |
c121c671 DK |
346 | |
347 | // Return whether we want to pass flag NON_PIC_REF for this | |
348 | // reloc. | |
349 | static inline bool | |
350 | reloc_is_non_pic (unsigned int r_type) | |
351 | { | |
352 | switch (r_type) | |
353 | { | |
354 | case elfcpp::R_ARM_REL32: | |
355 | case elfcpp::R_ARM_THM_CALL: | |
356 | case elfcpp::R_ARM_CALL: | |
357 | case elfcpp::R_ARM_JUMP24: | |
358 | case elfcpp::R_ARM_PREL31: | |
359 | return true; | |
360 | default: | |
361 | return false; | |
362 | } | |
363 | } | |
4a657b0d DK |
364 | }; |
365 | ||
366 | // A class which returns the size required for a relocation type, | |
367 | // used while scanning relocs during a relocatable link. | |
368 | class Relocatable_size_for_reloc | |
369 | { | |
370 | public: | |
371 | unsigned int | |
372 | get_size_for_reloc(unsigned int, Relobj*); | |
373 | }; | |
374 | ||
94cdfcff DK |
375 | // Get the GOT section, creating it if necessary. |
376 | Output_data_got<32, big_endian>* | |
377 | got_section(Symbol_table*, Layout*); | |
378 | ||
379 | // Get the GOT PLT section. | |
380 | Output_data_space* | |
381 | got_plt_section() const | |
382 | { | |
383 | gold_assert(this->got_plt_ != NULL); | |
384 | return this->got_plt_; | |
385 | } | |
386 | ||
387 | // Create a PLT entry for a global symbol. | |
388 | void | |
389 | make_plt_entry(Symbol_table*, Layout*, Symbol*); | |
390 | ||
391 | // Get the PLT section. | |
392 | const Output_data_plt_arm<big_endian>* | |
393 | plt_section() const | |
394 | { | |
395 | gold_assert(this->plt_ != NULL); | |
396 | return this->plt_; | |
397 | } | |
398 | ||
399 | // Get the dynamic reloc section, creating it if necessary. | |
400 | Reloc_section* | |
401 | rel_dyn_section(Layout*); | |
402 | ||
403 | // Return true if the symbol may need a COPY relocation. | |
404 | // References from an executable object to non-function symbols | |
405 | // defined in a dynamic object may need a COPY relocation. | |
406 | bool | |
407 | may_need_copy_reloc(Symbol* gsym) | |
408 | { | |
966d4097 DK |
409 | return (gsym->type() != elfcpp::STT_ARM_TFUNC |
410 | && gsym->may_need_copy_reloc()); | |
94cdfcff DK |
411 | } |
412 | ||
413 | // Add a potential copy relocation. | |
414 | void | |
415 | copy_reloc(Symbol_table* symtab, Layout* layout, | |
416 | Sized_relobj<32, big_endian>* object, | |
417 | unsigned int shndx, Output_section* output_section, | |
418 | Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc) | |
419 | { | |
420 | this->copy_relocs_.copy_reloc(symtab, layout, | |
421 | symtab->get_sized_symbol<32>(sym), | |
422 | object, shndx, output_section, reloc, | |
423 | this->rel_dyn_section(layout)); | |
424 | } | |
425 | ||
4a657b0d DK |
426 | // Information about this specific target which we pass to the |
427 | // general Target structure. | |
428 | static const Target::Target_info arm_info; | |
94cdfcff DK |
429 | |
430 | // The types of GOT entries needed for this platform. | |
431 | enum Got_type | |
432 | { | |
433 | GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol | |
434 | }; | |
435 | ||
436 | // The GOT section. | |
437 | Output_data_got<32, big_endian>* got_; | |
438 | // The PLT section. | |
439 | Output_data_plt_arm<big_endian>* plt_; | |
440 | // The GOT PLT section. | |
441 | Output_data_space* got_plt_; | |
442 | // The dynamic reloc section. | |
443 | Reloc_section* rel_dyn_; | |
444 | // Relocs saved to avoid a COPY reloc. | |
445 | Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_; | |
446 | // Space for variables copied with a COPY reloc. | |
447 | Output_data_space* dynbss_; | |
4a657b0d DK |
448 | }; |
449 | ||
450 | template<bool big_endian> | |
451 | const Target::Target_info Target_arm<big_endian>::arm_info = | |
452 | { | |
453 | 32, // size | |
454 | big_endian, // is_big_endian | |
455 | elfcpp::EM_ARM, // machine_code | |
456 | false, // has_make_symbol | |
457 | false, // has_resolve | |
458 | false, // has_code_fill | |
459 | true, // is_default_stack_executable | |
460 | '\0', // wrap_char | |
461 | "/usr/lib/libc.so.1", // dynamic_linker | |
462 | 0x8000, // default_text_segment_address | |
463 | 0x1000, // abi_pagesize (overridable by -z max-page-size) | |
8a5e3e08 ILT |
464 | 0x1000, // common_pagesize (overridable by -z common-page-size) |
465 | elfcpp::SHN_UNDEF, // small_common_shndx | |
466 | elfcpp::SHN_UNDEF, // large_common_shndx | |
467 | 0, // small_common_section_flags | |
468 | 0 // large_common_section_flags | |
4a657b0d DK |
469 | }; |
470 | ||
c121c671 DK |
471 | // Arm relocate functions class |
472 | // | |
473 | ||
474 | template<bool big_endian> | |
475 | class Arm_relocate_functions : public Relocate_functions<32, big_endian> | |
476 | { | |
477 | public: | |
478 | typedef enum | |
479 | { | |
480 | STATUS_OKAY, // No error during relocation. | |
481 | STATUS_OVERFLOW, // Relocation oveflow. | |
482 | STATUS_BAD_RELOC // Relocation cannot be applied. | |
483 | } Status; | |
484 | ||
485 | private: | |
486 | typedef Relocate_functions<32, big_endian> Base; | |
487 | typedef Arm_relocate_functions<big_endian> This; | |
488 | ||
489 | // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper | |
490 | // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used | |
491 | // to distinguish ARM and THUMB functions and it is treated specially. | |
492 | static inline Symbol_value<32>::Value | |
493 | arm_symbol_value (const Sized_relobj<32, big_endian> *object, | |
494 | const Symbol_value<32>* psymval, | |
495 | Symbol_value<32>::Value addend, | |
496 | bool has_thumb_bit) | |
497 | { | |
498 | typedef Symbol_value<32>::Value Valtype; | |
499 | ||
500 | if (has_thumb_bit) | |
501 | { | |
502 | Valtype raw = psymval->value(object, 0); | |
503 | Valtype thumb_bit = raw & 1; | |
504 | return ((raw & ~((Valtype) 1)) + addend) | thumb_bit; | |
505 | } | |
506 | else | |
507 | return psymval->value(object, addend); | |
508 | } | |
509 | ||
510 | // FIXME: This probably only works for Android on ARM v5te. We should | |
511 | // following GNU ld for the general case. | |
512 | template<unsigned r_type> | |
513 | static inline typename This::Status | |
514 | arm_branch_common(unsigned char *view, | |
515 | const Sized_relobj<32, big_endian>* object, | |
516 | const Symbol_value<32>* psymval, | |
517 | elfcpp::Elf_types<32>::Elf_Addr address, | |
518 | bool has_thumb_bit) | |
519 | { | |
520 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
521 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
522 | Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); | |
523 | ||
524 | bool insn_is_b = (((val >> 28) & 0xf) <= 0xe) | |
525 | && ((val & 0x0f000000UL) == 0x0a000000UL); | |
526 | bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL; | |
527 | bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe) | |
528 | && ((val & 0x0f000000UL) == 0x0b000000UL); | |
529 | bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL; | |
530 | bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL; | |
531 | ||
532 | if (r_type == elfcpp::R_ARM_CALL) | |
533 | { | |
534 | if (!insn_is_uncond_bl && !insn_is_blx) | |
535 | return This::STATUS_BAD_RELOC; | |
536 | } | |
537 | else if (r_type == elfcpp::R_ARM_JUMP24) | |
538 | { | |
539 | if (!insn_is_b && !insn_is_cond_bl) | |
540 | return This::STATUS_BAD_RELOC; | |
541 | } | |
542 | else if (r_type == elfcpp::R_ARM_PLT32) | |
543 | { | |
544 | if (!insn_is_any_branch) | |
545 | return This::STATUS_BAD_RELOC; | |
546 | } | |
547 | else | |
548 | gold_unreachable(); | |
549 | ||
550 | Valtype addend = utils::sign_extend<26>(val << 2); | |
551 | Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit) | |
552 | - address); | |
553 | ||
554 | // If target has thumb bit set, we need to either turn the BL | |
555 | // into a BLX (for ARMv5 or above) or generate a stub. | |
556 | if (x & 1) | |
557 | { | |
558 | // Turn BL to BLX. | |
559 | if (insn_is_uncond_bl) | |
560 | val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23); | |
561 | else | |
562 | return This::STATUS_BAD_RELOC; | |
563 | } | |
564 | else | |
565 | gold_assert(!insn_is_blx); | |
566 | ||
567 | val = utils::bit_select(val, (x >> 2), 0xffffffUL); | |
568 | elfcpp::Swap<32, big_endian>::writeval(wv, val); | |
569 | return (utils::has_overflow<26>(x) | |
570 | ? This::STATUS_OVERFLOW : This::STATUS_OKAY); | |
571 | } | |
572 | ||
573 | public: | |
5e445df6 ILT |
574 | |
575 | // R_ARM_ABS8: S + A | |
576 | static inline typename This::Status | |
577 | abs8(unsigned char *view, | |
578 | const Sized_relobj<32, big_endian>* object, | |
579 | const Symbol_value<32>* psymval, bool has_thumb_bit) | |
580 | { | |
581 | typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype; | |
582 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; | |
583 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
584 | Valtype val = elfcpp::Swap<8, big_endian>::readval(wv); | |
585 | Reltype addend = utils::sign_extend<8>(val); | |
586 | Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit); | |
587 | val = utils::bit_select(val, x, 0xffU); | |
588 | elfcpp::Swap<8, big_endian>::writeval(wv, val); | |
589 | return (utils::has_signed_unsigned_overflow<8>(x) | |
590 | ? This::STATUS_OVERFLOW | |
591 | : This::STATUS_OKAY); | |
592 | } | |
593 | ||
c121c671 DK |
594 | // R_ARM_ABS32: (S + A) | T |
595 | static inline typename This::Status | |
596 | abs32(unsigned char *view, | |
597 | const Sized_relobj<32, big_endian>* object, | |
598 | const Symbol_value<32>* psymval, | |
599 | bool has_thumb_bit) | |
600 | { | |
601 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
602 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
603 | Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv); | |
604 | Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit); | |
605 | elfcpp::Swap<32, big_endian>::writeval(wv, x); | |
606 | return This::STATUS_OKAY; | |
607 | } | |
608 | ||
609 | // R_ARM_REL32: (S + A) | T - P | |
610 | static inline typename This::Status | |
611 | rel32(unsigned char *view, | |
612 | const Sized_relobj<32, big_endian>* object, | |
613 | const Symbol_value<32>* psymval, | |
614 | elfcpp::Elf_types<32>::Elf_Addr address, | |
615 | bool has_thumb_bit) | |
616 | { | |
617 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
618 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
619 | Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv); | |
620 | Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit) | |
621 | - address); | |
622 | elfcpp::Swap<32, big_endian>::writeval(wv, x); | |
623 | return This::STATUS_OKAY; | |
624 | } | |
625 | ||
626 | // R_ARM_THM_CALL: (S + A) | T - P | |
627 | static inline typename This::Status | |
628 | thm_call(unsigned char *view, | |
629 | const Sized_relobj<32, big_endian>* object, | |
630 | const Symbol_value<32>* psymval, | |
631 | elfcpp::Elf_types<32>::Elf_Addr address, | |
632 | bool has_thumb_bit) | |
633 | { | |
634 | // A thumb call consists of two instructions. | |
635 | typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; | |
636 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; | |
637 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
638 | Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv); | |
639 | Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1); | |
640 | // Must be a BL instruction. lo == 11111xxxxxxxxxxx. | |
641 | gold_assert((lo & 0xf800) == 0xf800); | |
642 | Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12) | |
643 | | ((lo & 0x7ff) << 1)); | |
644 | Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit) | |
645 | - address); | |
646 | ||
647 | // If target has no thumb bit set, we need to either turn the BL | |
648 | // into a BLX (for ARMv5 or above) or generate a stub. | |
649 | if ((x & 1) == 0) | |
650 | { | |
651 | // This only works for ARMv5 and above with interworking enabled. | |
652 | lo &= 0xefff; | |
653 | } | |
654 | hi = utils::bit_select(hi, (x >> 12), 0x7ffU); | |
655 | lo = utils::bit_select(lo, (x >> 1), 0x7ffU); | |
656 | elfcpp::Swap<16, big_endian>::writeval(wv, hi); | |
657 | elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo); | |
658 | return (utils::has_overflow<23>(x) | |
659 | ? This::STATUS_OVERFLOW | |
660 | : This::STATUS_OKAY); | |
661 | } | |
662 | ||
663 | // R_ARM_BASE_PREL: B(S) + A - P | |
664 | static inline typename This::Status | |
665 | base_prel(unsigned char* view, | |
666 | elfcpp::Elf_types<32>::Elf_Addr origin, | |
667 | elfcpp::Elf_types<32>::Elf_Addr address) | |
668 | { | |
669 | Base::rel32(view, origin - address); | |
670 | return STATUS_OKAY; | |
671 | } | |
672 | ||
673 | // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG | |
674 | static inline typename This::Status | |
675 | got_brel(unsigned char* view, | |
676 | typename elfcpp::Swap<32, big_endian>::Valtype got_offset) | |
677 | { | |
678 | Base::rel32(view, got_offset); | |
679 | return This::STATUS_OKAY; | |
680 | } | |
681 | ||
682 | // R_ARM_PLT32: (S + A) | T - P | |
683 | static inline typename This::Status | |
684 | plt32(unsigned char *view, | |
685 | const Sized_relobj<32, big_endian>* object, | |
686 | const Symbol_value<32>* psymval, | |
687 | elfcpp::Elf_types<32>::Elf_Addr address, | |
688 | bool has_thumb_bit) | |
689 | { | |
690 | return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval, | |
691 | address, has_thumb_bit); | |
692 | } | |
693 | ||
694 | // R_ARM_CALL: (S + A) | T - P | |
695 | static inline typename This::Status | |
696 | call(unsigned char *view, | |
697 | const Sized_relobj<32, big_endian>* object, | |
698 | const Symbol_value<32>* psymval, | |
699 | elfcpp::Elf_types<32>::Elf_Addr address, | |
700 | bool has_thumb_bit) | |
701 | { | |
702 | return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval, | |
703 | address, has_thumb_bit); | |
704 | } | |
705 | ||
706 | // R_ARM_JUMP24: (S + A) | T - P | |
707 | static inline typename This::Status | |
708 | jump24(unsigned char *view, | |
709 | const Sized_relobj<32, big_endian>* object, | |
710 | const Symbol_value<32>* psymval, | |
711 | elfcpp::Elf_types<32>::Elf_Addr address, | |
712 | bool has_thumb_bit) | |
713 | { | |
714 | return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval, | |
715 | address, has_thumb_bit); | |
716 | } | |
717 | ||
718 | // R_ARM_PREL: (S + A) | T - P | |
719 | static inline typename This::Status | |
720 | prel31(unsigned char *view, | |
721 | const Sized_relobj<32, big_endian>* object, | |
722 | const Symbol_value<32>* psymval, | |
723 | elfcpp::Elf_types<32>::Elf_Addr address, | |
724 | bool has_thumb_bit) | |
725 | { | |
726 | typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; | |
727 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
728 | Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); | |
729 | Valtype addend = utils::sign_extend<31>(val); | |
730 | Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit) | |
731 | - address); | |
732 | val = utils::bit_select(val, x, 0x7fffffffU); | |
733 | elfcpp::Swap<32, big_endian>::writeval(wv, val); | |
734 | return (utils::has_overflow<31>(x) ? | |
735 | This::STATUS_OVERFLOW : This::STATUS_OKAY); | |
736 | } | |
737 | }; | |
738 | ||
94cdfcff DK |
739 | // Get the GOT section, creating it if necessary. |
740 | ||
741 | template<bool big_endian> | |
742 | Output_data_got<32, big_endian>* | |
743 | Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout) | |
744 | { | |
745 | if (this->got_ == NULL) | |
746 | { | |
747 | gold_assert(symtab != NULL && layout != NULL); | |
748 | ||
749 | this->got_ = new Output_data_got<32, big_endian>(); | |
750 | ||
751 | Output_section* os; | |
752 | os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, | |
753 | (elfcpp::SHF_ALLOC | |
754 | | elfcpp::SHF_WRITE), | |
755 | this->got_); | |
756 | os->set_is_relro(); | |
757 | ||
758 | // The old GNU linker creates a .got.plt section. We just | |
759 | // create another set of data in the .got section. Note that we | |
760 | // always create a PLT if we create a GOT, although the PLT | |
761 | // might be empty. | |
762 | this->got_plt_ = new Output_data_space(4, "** GOT PLT"); | |
763 | os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, | |
764 | (elfcpp::SHF_ALLOC | |
765 | | elfcpp::SHF_WRITE), | |
766 | this->got_plt_); | |
767 | os->set_is_relro(); | |
768 | ||
769 | // The first three entries are reserved. | |
770 | this->got_plt_->set_current_data_size(3 * 4); | |
771 | ||
772 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. | |
773 | symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, | |
774 | this->got_plt_, | |
775 | 0, 0, elfcpp::STT_OBJECT, | |
776 | elfcpp::STB_LOCAL, | |
777 | elfcpp::STV_HIDDEN, 0, | |
778 | false, false); | |
779 | } | |
780 | return this->got_; | |
781 | } | |
782 | ||
783 | // Get the dynamic reloc section, creating it if necessary. | |
784 | ||
785 | template<bool big_endian> | |
786 | typename Target_arm<big_endian>::Reloc_section* | |
787 | Target_arm<big_endian>::rel_dyn_section(Layout* layout) | |
788 | { | |
789 | if (this->rel_dyn_ == NULL) | |
790 | { | |
791 | gold_assert(layout != NULL); | |
792 | this->rel_dyn_ = new Reloc_section(parameters->options().combreloc()); | |
793 | layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL, | |
794 | elfcpp::SHF_ALLOC, this->rel_dyn_); | |
795 | } | |
796 | return this->rel_dyn_; | |
797 | } | |
798 | ||
799 | // A class to handle the PLT data. | |
800 | ||
801 | template<bool big_endian> | |
802 | class Output_data_plt_arm : public Output_section_data | |
803 | { | |
804 | public: | |
805 | typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian> | |
806 | Reloc_section; | |
807 | ||
808 | Output_data_plt_arm(Layout*, Output_data_space*); | |
809 | ||
810 | // Add an entry to the PLT. | |
811 | void | |
812 | add_entry(Symbol* gsym); | |
813 | ||
814 | // Return the .rel.plt section data. | |
815 | const Reloc_section* | |
816 | rel_plt() const | |
817 | { return this->rel_; } | |
818 | ||
819 | protected: | |
820 | void | |
821 | do_adjust_output_section(Output_section* os); | |
822 | ||
823 | // Write to a map file. | |
824 | void | |
825 | do_print_to_mapfile(Mapfile* mapfile) const | |
826 | { mapfile->print_output_data(this, _("** PLT")); } | |
827 | ||
828 | private: | |
829 | // Template for the first PLT entry. | |
830 | static const uint32_t first_plt_entry[5]; | |
831 | ||
832 | // Template for subsequent PLT entries. | |
833 | static const uint32_t plt_entry[3]; | |
834 | ||
835 | // Set the final size. | |
836 | void | |
837 | set_final_data_size() | |
838 | { | |
839 | this->set_data_size(sizeof(first_plt_entry) | |
840 | + this->count_ * sizeof(plt_entry)); | |
841 | } | |
842 | ||
843 | // Write out the PLT data. | |
844 | void | |
845 | do_write(Output_file*); | |
846 | ||
847 | // The reloc section. | |
848 | Reloc_section* rel_; | |
849 | // The .got.plt section. | |
850 | Output_data_space* got_plt_; | |
851 | // The number of PLT entries. | |
852 | unsigned int count_; | |
853 | }; | |
854 | ||
855 | // Create the PLT section. The ordinary .got section is an argument, | |
856 | // since we need to refer to the start. We also create our own .got | |
857 | // section just for PLT entries. | |
858 | ||
859 | template<bool big_endian> | |
860 | Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout, | |
861 | Output_data_space* got_plt) | |
862 | : Output_section_data(4), got_plt_(got_plt), count_(0) | |
863 | { | |
864 | this->rel_ = new Reloc_section(false); | |
865 | layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL, | |
866 | elfcpp::SHF_ALLOC, this->rel_); | |
867 | } | |
868 | ||
869 | template<bool big_endian> | |
870 | void | |
871 | Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os) | |
872 | { | |
873 | os->set_entsize(0); | |
874 | } | |
875 | ||
876 | // Add an entry to the PLT. | |
877 | ||
878 | template<bool big_endian> | |
879 | void | |
880 | Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym) | |
881 | { | |
882 | gold_assert(!gsym->has_plt_offset()); | |
883 | ||
884 | // Note that when setting the PLT offset we skip the initial | |
885 | // reserved PLT entry. | |
886 | gsym->set_plt_offset((this->count_) * sizeof(plt_entry) | |
887 | + sizeof(first_plt_entry)); | |
888 | ||
889 | ++this->count_; | |
890 | ||
891 | section_offset_type got_offset = this->got_plt_->current_data_size(); | |
892 | ||
893 | // Every PLT entry needs a GOT entry which points back to the PLT | |
894 | // entry (this will be changed by the dynamic linker, normally | |
895 | // lazily when the function is called). | |
896 | this->got_plt_->set_current_data_size(got_offset + 4); | |
897 | ||
898 | // Every PLT entry needs a reloc. | |
899 | gsym->set_needs_dynsym_entry(); | |
900 | this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_, | |
901 | got_offset); | |
902 | ||
903 | // Note that we don't need to save the symbol. The contents of the | |
904 | // PLT are independent of which symbols are used. The symbols only | |
905 | // appear in the relocations. | |
906 | } | |
907 | ||
908 | // ARM PLTs. | |
909 | // FIXME: This is not very flexible. Right now this has only been tested | |
910 | // on armv5te. If we are to support additional architecture features like | |
911 | // Thumb-2 or BE8, we need to make this more flexible like GNU ld. | |
912 | ||
913 | // The first entry in the PLT. | |
914 | template<bool big_endian> | |
915 | const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] = | |
916 | { | |
917 | 0xe52de004, // str lr, [sp, #-4]! | |
918 | 0xe59fe004, // ldr lr, [pc, #4] | |
919 | 0xe08fe00e, // add lr, pc, lr | |
920 | 0xe5bef008, // ldr pc, [lr, #8]! | |
921 | 0x00000000, // &GOT[0] - . | |
922 | }; | |
923 | ||
924 | // Subsequent entries in the PLT. | |
925 | ||
926 | template<bool big_endian> | |
927 | const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] = | |
928 | { | |
929 | 0xe28fc600, // add ip, pc, #0xNN00000 | |
930 | 0xe28cca00, // add ip, ip, #0xNN000 | |
931 | 0xe5bcf000, // ldr pc, [ip, #0xNNN]! | |
932 | }; | |
933 | ||
934 | // Write out the PLT. This uses the hand-coded instructions above, | |
935 | // and adjusts them as needed. This is all specified by the arm ELF | |
936 | // Processor Supplement. | |
937 | ||
938 | template<bool big_endian> | |
939 | void | |
940 | Output_data_plt_arm<big_endian>::do_write(Output_file* of) | |
941 | { | |
942 | const off_t offset = this->offset(); | |
943 | const section_size_type oview_size = | |
944 | convert_to_section_size_type(this->data_size()); | |
945 | unsigned char* const oview = of->get_output_view(offset, oview_size); | |
946 | ||
947 | const off_t got_file_offset = this->got_plt_->offset(); | |
948 | const section_size_type got_size = | |
949 | convert_to_section_size_type(this->got_plt_->data_size()); | |
950 | unsigned char* const got_view = of->get_output_view(got_file_offset, | |
951 | got_size); | |
952 | unsigned char* pov = oview; | |
953 | ||
954 | elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address(); | |
955 | elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address(); | |
956 | ||
957 | // Write first PLT entry. All but the last word are constants. | |
958 | const size_t num_first_plt_words = (sizeof(first_plt_entry) | |
959 | / sizeof(plt_entry[0])); | |
960 | for (size_t i = 0; i < num_first_plt_words - 1; i++) | |
961 | elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]); | |
962 | // Last word in first PLT entry is &GOT[0] - . | |
963 | elfcpp::Swap<32, big_endian>::writeval(pov + 16, | |
964 | got_address - (plt_address + 16)); | |
965 | pov += sizeof(first_plt_entry); | |
966 | ||
967 | unsigned char* got_pov = got_view; | |
968 | ||
969 | memset(got_pov, 0, 12); | |
970 | got_pov += 12; | |
971 | ||
972 | const int rel_size = elfcpp::Elf_sizes<32>::rel_size; | |
973 | unsigned int plt_offset = sizeof(first_plt_entry); | |
974 | unsigned int plt_rel_offset = 0; | |
975 | unsigned int got_offset = 12; | |
976 | const unsigned int count = this->count_; | |
977 | for (unsigned int i = 0; | |
978 | i < count; | |
979 | ++i, | |
980 | pov += sizeof(plt_entry), | |
981 | got_pov += 4, | |
982 | plt_offset += sizeof(plt_entry), | |
983 | plt_rel_offset += rel_size, | |
984 | got_offset += 4) | |
985 | { | |
986 | // Set and adjust the PLT entry itself. | |
987 | int32_t offset = ((got_address + got_offset) | |
988 | - (plt_address + plt_offset + 8)); | |
989 | ||
990 | gold_assert(offset >= 0 && offset < 0x0fffffff); | |
991 | uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff); | |
992 | elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0); | |
993 | uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff); | |
994 | elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1); | |
995 | uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff); | |
996 | elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2); | |
997 | ||
998 | // Set the entry in the GOT. | |
999 | elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address); | |
1000 | } | |
1001 | ||
1002 | gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); | |
1003 | gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); | |
1004 | ||
1005 | of->write_output_view(offset, oview_size, oview); | |
1006 | of->write_output_view(got_file_offset, got_size, got_view); | |
1007 | } | |
1008 | ||
1009 | // Create a PLT entry for a global symbol. | |
1010 | ||
1011 | template<bool big_endian> | |
1012 | void | |
1013 | Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout, | |
1014 | Symbol* gsym) | |
1015 | { | |
1016 | if (gsym->has_plt_offset()) | |
1017 | return; | |
1018 | ||
1019 | if (this->plt_ == NULL) | |
1020 | { | |
1021 | // Create the GOT sections first. | |
1022 | this->got_section(symtab, layout); | |
1023 | ||
1024 | this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_); | |
1025 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, | |
1026 | (elfcpp::SHF_ALLOC | |
1027 | | elfcpp::SHF_EXECINSTR), | |
1028 | this->plt_); | |
1029 | } | |
1030 | this->plt_->add_entry(gsym); | |
1031 | } | |
1032 | ||
4a657b0d DK |
1033 | // Report an unsupported relocation against a local symbol. |
1034 | ||
1035 | template<bool big_endian> | |
1036 | void | |
1037 | Target_arm<big_endian>::Scan::unsupported_reloc_local( | |
1038 | Sized_relobj<32, big_endian>* object, | |
1039 | unsigned int r_type) | |
1040 | { | |
1041 | gold_error(_("%s: unsupported reloc %u against local symbol"), | |
1042 | object->name().c_str(), r_type); | |
1043 | } | |
1044 | ||
bec53400 DK |
1045 | // We are about to emit a dynamic relocation of type R_TYPE. If the |
1046 | // dynamic linker does not support it, issue an error. The GNU linker | |
1047 | // only issues a non-PIC error for an allocated read-only section. | |
1048 | // Here we know the section is allocated, but we don't know that it is | |
1049 | // read-only. But we check for all the relocation types which the | |
1050 | // glibc dynamic linker supports, so it seems appropriate to issue an | |
1051 | // error even if the section is not read-only. | |
1052 | ||
1053 | template<bool big_endian> | |
1054 | void | |
1055 | Target_arm<big_endian>::Scan::check_non_pic(Relobj* object, | |
1056 | unsigned int r_type) | |
1057 | { | |
1058 | switch (r_type) | |
1059 | { | |
1060 | // These are the relocation types supported by glibc for ARM. | |
1061 | case elfcpp::R_ARM_RELATIVE: | |
1062 | case elfcpp::R_ARM_COPY: | |
1063 | case elfcpp::R_ARM_GLOB_DAT: | |
1064 | case elfcpp::R_ARM_JUMP_SLOT: | |
1065 | case elfcpp::R_ARM_ABS32: | |
1066 | case elfcpp::R_ARM_PC24: | |
1067 | // FIXME: The following 3 types are not supported by Android's dynamic | |
1068 | // linker. | |
1069 | case elfcpp::R_ARM_TLS_DTPMOD32: | |
1070 | case elfcpp::R_ARM_TLS_DTPOFF32: | |
1071 | case elfcpp::R_ARM_TLS_TPOFF32: | |
1072 | return; | |
1073 | ||
1074 | default: | |
1075 | // This prevents us from issuing more than one error per reloc | |
1076 | // section. But we can still wind up issuing more than one | |
1077 | // error per object file. | |
1078 | if (this->issued_non_pic_error_) | |
1079 | return; | |
1080 | object->error(_("requires unsupported dynamic reloc; " | |
1081 | "recompile with -fPIC")); | |
1082 | this->issued_non_pic_error_ = true; | |
1083 | return; | |
1084 | ||
1085 | case elfcpp::R_ARM_NONE: | |
1086 | gold_unreachable(); | |
1087 | } | |
1088 | } | |
1089 | ||
4a657b0d | 1090 | // Scan a relocation for a local symbol. |
bec53400 DK |
1091 | // FIXME: This only handles a subset of relocation types used by Android |
1092 | // on ARM v5te devices. | |
4a657b0d DK |
1093 | |
1094 | template<bool big_endian> | |
1095 | inline void | |
1096 | Target_arm<big_endian>::Scan::local(const General_options&, | |
bec53400 DK |
1097 | Symbol_table* symtab, |
1098 | Layout* layout, | |
1099 | Target_arm* target, | |
4a657b0d | 1100 | Sized_relobj<32, big_endian>* object, |
bec53400 DK |
1101 | unsigned int data_shndx, |
1102 | Output_section* output_section, | |
1103 | const elfcpp::Rel<32, big_endian>& reloc, | |
4a657b0d DK |
1104 | unsigned int r_type, |
1105 | const elfcpp::Sym<32, big_endian>&) | |
1106 | { | |
1107 | r_type = get_real_reloc_type(r_type); | |
1108 | switch (r_type) | |
1109 | { | |
1110 | case elfcpp::R_ARM_NONE: | |
1111 | break; | |
1112 | ||
5e445df6 ILT |
1113 | case elfcpp::R_ARM_ABS8: |
1114 | if (parameters->options().output_is_position_independent()) | |
1115 | { | |
1116 | // FIXME: Create a dynamic relocation for this location. | |
1117 | gold_error(_("%s: gold bug: need dynamic ABS8 reloc"), | |
1118 | object->name().c_str()); | |
1119 | } | |
1120 | break; | |
1121 | ||
bec53400 DK |
1122 | case elfcpp::R_ARM_ABS32: |
1123 | // If building a shared library (or a position-independent | |
1124 | // executable), we need to create a dynamic relocation for | |
1125 | // this location. The relocation applied at link time will | |
1126 | // apply the link-time value, so we flag the location with | |
1127 | // an R_ARM_RELATIVE relocation so the dynamic loader can | |
1128 | // relocate it easily. | |
1129 | if (parameters->options().output_is_position_independent()) | |
1130 | { | |
1131 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
1132 | unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); | |
1133 | // If we are to add more other reloc types than R_ARM_ABS32, | |
1134 | // we need to add check_non_pic(object, r_type) here. | |
1135 | rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE, | |
1136 | output_section, data_shndx, | |
1137 | reloc.get_r_offset()); | |
1138 | } | |
1139 | break; | |
1140 | ||
1141 | case elfcpp::R_ARM_REL32: | |
1142 | case elfcpp::R_ARM_THM_CALL: | |
1143 | case elfcpp::R_ARM_CALL: | |
1144 | case elfcpp::R_ARM_PREL31: | |
1145 | case elfcpp::R_ARM_JUMP24: | |
1146 | case elfcpp::R_ARM_PLT32: | |
1147 | break; | |
1148 | ||
1149 | case elfcpp::R_ARM_GOTOFF32: | |
1150 | // We need a GOT section: | |
1151 | target->got_section(symtab, layout); | |
1152 | break; | |
1153 | ||
1154 | case elfcpp::R_ARM_BASE_PREL: | |
1155 | // FIXME: What about this? | |
1156 | break; | |
1157 | ||
1158 | case elfcpp::R_ARM_GOT_BREL: | |
1159 | { | |
1160 | // The symbol requires a GOT entry. | |
1161 | Output_data_got<32, big_endian>* got = | |
1162 | target->got_section(symtab, layout); | |
1163 | unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); | |
1164 | if (got->add_local(object, r_sym, GOT_TYPE_STANDARD)) | |
1165 | { | |
1166 | // If we are generating a shared object, we need to add a | |
1167 | // dynamic RELATIVE relocation for this symbol's GOT entry. | |
1168 | if (parameters->options().output_is_position_independent()) | |
1169 | { | |
1170 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
1171 | unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); | |
1172 | rel_dyn->add_local_relative( | |
1173 | object, r_sym, elfcpp::R_ARM_RELATIVE, got, | |
1174 | object->local_got_offset(r_sym, GOT_TYPE_STANDARD)); | |
1175 | } | |
1176 | } | |
1177 | } | |
1178 | break; | |
1179 | ||
1180 | case elfcpp::R_ARM_TARGET1: | |
1181 | // This should have been mapped to another type already. | |
1182 | // Fall through. | |
1183 | case elfcpp::R_ARM_COPY: | |
1184 | case elfcpp::R_ARM_GLOB_DAT: | |
1185 | case elfcpp::R_ARM_JUMP_SLOT: | |
1186 | case elfcpp::R_ARM_RELATIVE: | |
1187 | // These are relocations which should only be seen by the | |
1188 | // dynamic linker, and should never be seen here. | |
1189 | gold_error(_("%s: unexpected reloc %u in object file"), | |
1190 | object->name().c_str(), r_type); | |
1191 | break; | |
1192 | ||
4a657b0d DK |
1193 | default: |
1194 | unsupported_reloc_local(object, r_type); | |
1195 | break; | |
1196 | } | |
1197 | } | |
1198 | ||
1199 | // Report an unsupported relocation against a global symbol. | |
1200 | ||
1201 | template<bool big_endian> | |
1202 | void | |
1203 | Target_arm<big_endian>::Scan::unsupported_reloc_global( | |
1204 | Sized_relobj<32, big_endian>* object, | |
1205 | unsigned int r_type, | |
1206 | Symbol* gsym) | |
1207 | { | |
1208 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), | |
1209 | object->name().c_str(), r_type, gsym->demangled_name().c_str()); | |
1210 | } | |
1211 | ||
1212 | // Scan a relocation for a global symbol. | |
bec53400 DK |
1213 | // FIXME: This only handles a subset of relocation types used by Android |
1214 | // on ARM v5te devices. | |
4a657b0d DK |
1215 | |
1216 | template<bool big_endian> | |
1217 | inline void | |
1218 | Target_arm<big_endian>::Scan::global(const General_options&, | |
bec53400 DK |
1219 | Symbol_table* symtab, |
1220 | Layout* layout, | |
1221 | Target_arm* target, | |
4a657b0d | 1222 | Sized_relobj<32, big_endian>* object, |
bec53400 DK |
1223 | unsigned int data_shndx, |
1224 | Output_section* output_section, | |
1225 | const elfcpp::Rel<32, big_endian>& reloc, | |
4a657b0d DK |
1226 | unsigned int r_type, |
1227 | Symbol* gsym) | |
1228 | { | |
1229 | r_type = get_real_reloc_type(r_type); | |
1230 | switch (r_type) | |
1231 | { | |
1232 | case elfcpp::R_ARM_NONE: | |
1233 | break; | |
1234 | ||
5e445df6 ILT |
1235 | case elfcpp::R_ARM_ABS8: |
1236 | // Make a dynamic relocation if necessary. | |
1237 | if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF)) | |
1238 | { | |
1239 | // FIXME: Create a dynamic relocation for this location. | |
1240 | gold_error(_("%s: gold bug: need dynamic ABS8 reloc for %s"), | |
1241 | object->name().c_str(), gsym->demangled_name().c_str()); | |
1242 | } | |
1243 | break; | |
1244 | ||
bec53400 DK |
1245 | case elfcpp::R_ARM_ABS32: |
1246 | { | |
1247 | // Make a dynamic relocation if necessary. | |
1248 | if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF)) | |
1249 | { | |
1250 | if (target->may_need_copy_reloc(gsym)) | |
1251 | { | |
1252 | target->copy_reloc(symtab, layout, object, | |
1253 | data_shndx, output_section, gsym, reloc); | |
1254 | } | |
1255 | else if (gsym->can_use_relative_reloc(false)) | |
1256 | { | |
1257 | // If we are to add more other reloc types than R_ARM_ABS32, | |
1258 | // we need to add check_non_pic(object, r_type) here. | |
1259 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
1260 | rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE, | |
1261 | output_section, object, | |
1262 | data_shndx, reloc.get_r_offset()); | |
1263 | } | |
1264 | else | |
1265 | { | |
1266 | // If we are to add more other reloc types than R_ARM_ABS32, | |
1267 | // we need to add check_non_pic(object, r_type) here. | |
1268 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
1269 | rel_dyn->add_global(gsym, r_type, output_section, object, | |
1270 | data_shndx, reloc.get_r_offset()); | |
1271 | } | |
1272 | } | |
1273 | } | |
1274 | break; | |
1275 | ||
1276 | case elfcpp::R_ARM_REL32: | |
1277 | case elfcpp::R_ARM_PREL31: | |
1278 | { | |
1279 | // Make a dynamic relocation if necessary. | |
1280 | int flags = Symbol::NON_PIC_REF; | |
1281 | if (gsym->needs_dynamic_reloc(flags)) | |
1282 | { | |
1283 | if (target->may_need_copy_reloc(gsym)) | |
1284 | { | |
1285 | target->copy_reloc(symtab, layout, object, | |
1286 | data_shndx, output_section, gsym, reloc); | |
1287 | } | |
1288 | else | |
1289 | { | |
1290 | check_non_pic(object, r_type); | |
1291 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
1292 | rel_dyn->add_global(gsym, r_type, output_section, object, | |
1293 | data_shndx, reloc.get_r_offset()); | |
1294 | } | |
1295 | } | |
1296 | } | |
1297 | break; | |
1298 | ||
1299 | case elfcpp::R_ARM_JUMP24: | |
1300 | case elfcpp::R_ARM_THM_CALL: | |
1301 | case elfcpp::R_ARM_CALL: | |
1302 | { | |
1303 | if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym)) | |
1304 | target->make_plt_entry(symtab, layout, gsym); | |
1305 | // Make a dynamic relocation if necessary. | |
1306 | int flags = Symbol::NON_PIC_REF; | |
1307 | if (gsym->type() == elfcpp::STT_FUNC | |
07800fab | 1308 | || gsym->type() == elfcpp::STT_ARM_TFUNC) |
bec53400 DK |
1309 | flags |= Symbol::FUNCTION_CALL; |
1310 | if (gsym->needs_dynamic_reloc(flags)) | |
1311 | { | |
1312 | if (target->may_need_copy_reloc(gsym)) | |
1313 | { | |
1314 | target->copy_reloc(symtab, layout, object, | |
1315 | data_shndx, output_section, gsym, | |
1316 | reloc); | |
1317 | } | |
1318 | else | |
1319 | { | |
1320 | check_non_pic(object, r_type); | |
1321 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
1322 | rel_dyn->add_global(gsym, r_type, output_section, object, | |
1323 | data_shndx, reloc.get_r_offset()); | |
1324 | } | |
1325 | } | |
1326 | } | |
1327 | break; | |
1328 | ||
1329 | case elfcpp::R_ARM_PLT32: | |
1330 | // If the symbol is fully resolved, this is just a relative | |
1331 | // local reloc. Otherwise we need a PLT entry. | |
1332 | if (gsym->final_value_is_known()) | |
1333 | break; | |
1334 | // If building a shared library, we can also skip the PLT entry | |
1335 | // if the symbol is defined in the output file and is protected | |
1336 | // or hidden. | |
1337 | if (gsym->is_defined() | |
1338 | && !gsym->is_from_dynobj() | |
1339 | && !gsym->is_preemptible()) | |
1340 | break; | |
1341 | target->make_plt_entry(symtab, layout, gsym); | |
1342 | break; | |
1343 | ||
1344 | case elfcpp::R_ARM_GOTOFF32: | |
1345 | // We need a GOT section. | |
1346 | target->got_section(symtab, layout); | |
1347 | break; | |
1348 | ||
1349 | case elfcpp::R_ARM_BASE_PREL: | |
1350 | // FIXME: What about this? | |
1351 | break; | |
1352 | ||
1353 | case elfcpp::R_ARM_GOT_BREL: | |
1354 | { | |
1355 | // The symbol requires a GOT entry. | |
1356 | Output_data_got<32, big_endian>* got = | |
1357 | target->got_section(symtab, layout); | |
1358 | if (gsym->final_value_is_known()) | |
1359 | got->add_global(gsym, GOT_TYPE_STANDARD); | |
1360 | else | |
1361 | { | |
1362 | // If this symbol is not fully resolved, we need to add a | |
1363 | // GOT entry with a dynamic relocation. | |
1364 | Reloc_section* rel_dyn = target->rel_dyn_section(layout); | |
1365 | if (gsym->is_from_dynobj() | |
1366 | || gsym->is_undefined() | |
1367 | || gsym->is_preemptible()) | |
1368 | got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, | |
1369 | rel_dyn, elfcpp::R_ARM_GLOB_DAT); | |
1370 | else | |
1371 | { | |
1372 | if (got->add_global(gsym, GOT_TYPE_STANDARD)) | |
1373 | rel_dyn->add_global_relative( | |
1374 | gsym, elfcpp::R_ARM_RELATIVE, got, | |
1375 | gsym->got_offset(GOT_TYPE_STANDARD)); | |
1376 | } | |
1377 | } | |
1378 | } | |
1379 | break; | |
1380 | ||
1381 | case elfcpp::R_ARM_TARGET1: | |
1382 | // This should have been mapped to another type already. | |
1383 | // Fall through. | |
1384 | case elfcpp::R_ARM_COPY: | |
1385 | case elfcpp::R_ARM_GLOB_DAT: | |
1386 | case elfcpp::R_ARM_JUMP_SLOT: | |
1387 | case elfcpp::R_ARM_RELATIVE: | |
1388 | // These are relocations which should only be seen by the | |
1389 | // dynamic linker, and should never be seen here. | |
1390 | gold_error(_("%s: unexpected reloc %u in object file"), | |
1391 | object->name().c_str(), r_type); | |
1392 | break; | |
1393 | ||
4a657b0d DK |
1394 | default: |
1395 | unsupported_reloc_global(object, r_type, gsym); | |
1396 | break; | |
1397 | } | |
1398 | } | |
1399 | ||
1400 | // Process relocations for gc. | |
1401 | ||
1402 | template<bool big_endian> | |
1403 | void | |
1404 | Target_arm<big_endian>::gc_process_relocs(const General_options& options, | |
1405 | Symbol_table* symtab, | |
1406 | Layout* layout, | |
1407 | Sized_relobj<32, big_endian>* object, | |
1408 | unsigned int data_shndx, | |
1409 | unsigned int, | |
1410 | const unsigned char* prelocs, | |
1411 | size_t reloc_count, | |
1412 | Output_section* output_section, | |
1413 | bool needs_special_offset_handling, | |
1414 | size_t local_symbol_count, | |
1415 | const unsigned char* plocal_symbols) | |
1416 | { | |
1417 | typedef Target_arm<big_endian> Arm; | |
1418 | typedef typename Target_arm<big_endian>::Scan Scan; | |
1419 | ||
1420 | gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>( | |
1421 | options, | |
1422 | symtab, | |
1423 | layout, | |
1424 | this, | |
1425 | object, | |
1426 | data_shndx, | |
1427 | prelocs, | |
1428 | reloc_count, | |
1429 | output_section, | |
1430 | needs_special_offset_handling, | |
1431 | local_symbol_count, | |
1432 | plocal_symbols); | |
1433 | } | |
1434 | ||
1435 | // Scan relocations for a section. | |
1436 | ||
1437 | template<bool big_endian> | |
1438 | void | |
1439 | Target_arm<big_endian>::scan_relocs(const General_options& options, | |
1440 | Symbol_table* symtab, | |
1441 | Layout* layout, | |
1442 | Sized_relobj<32, big_endian>* object, | |
1443 | unsigned int data_shndx, | |
1444 | unsigned int sh_type, | |
1445 | const unsigned char* prelocs, | |
1446 | size_t reloc_count, | |
1447 | Output_section* output_section, | |
1448 | bool needs_special_offset_handling, | |
1449 | size_t local_symbol_count, | |
1450 | const unsigned char* plocal_symbols) | |
1451 | { | |
1452 | typedef typename Target_arm<big_endian>::Scan Scan; | |
1453 | if (sh_type == elfcpp::SHT_RELA) | |
1454 | { | |
1455 | gold_error(_("%s: unsupported RELA reloc section"), | |
1456 | object->name().c_str()); | |
1457 | return; | |
1458 | } | |
1459 | ||
1460 | gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>( | |
1461 | options, | |
1462 | symtab, | |
1463 | layout, | |
1464 | this, | |
1465 | object, | |
1466 | data_shndx, | |
1467 | prelocs, | |
1468 | reloc_count, | |
1469 | output_section, | |
1470 | needs_special_offset_handling, | |
1471 | local_symbol_count, | |
1472 | plocal_symbols); | |
1473 | } | |
1474 | ||
1475 | // Finalize the sections. | |
1476 | ||
1477 | template<bool big_endian> | |
1478 | void | |
94cdfcff | 1479 | Target_arm<big_endian>::do_finalize_sections(Layout* layout) |
4a657b0d | 1480 | { |
94cdfcff DK |
1481 | // Fill in some more dynamic tags. |
1482 | Output_data_dynamic* const odyn = layout->dynamic_data(); | |
1483 | if (odyn != NULL) | |
1484 | { | |
1485 | if (this->got_plt_ != NULL) | |
1486 | odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_); | |
1487 | ||
1488 | if (this->plt_ != NULL) | |
1489 | { | |
1490 | const Output_data* od = this->plt_->rel_plt(); | |
1491 | odyn->add_section_size(elfcpp::DT_PLTRELSZ, od); | |
1492 | odyn->add_section_address(elfcpp::DT_JMPREL, od); | |
1493 | odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL); | |
1494 | } | |
1495 | ||
1496 | if (this->rel_dyn_ != NULL) | |
1497 | { | |
1498 | const Output_data* od = this->rel_dyn_; | |
1499 | odyn->add_section_address(elfcpp::DT_REL, od); | |
1500 | odyn->add_section_size(elfcpp::DT_RELSZ, od); | |
1501 | odyn->add_constant(elfcpp::DT_RELENT, | |
1502 | elfcpp::Elf_sizes<32>::rel_size); | |
1503 | } | |
1504 | ||
1505 | if (!parameters->options().shared()) | |
1506 | { | |
1507 | // The value of the DT_DEBUG tag is filled in by the dynamic | |
1508 | // linker at run time, and used by the debugger. | |
1509 | odyn->add_constant(elfcpp::DT_DEBUG, 0); | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | // Emit any relocs we saved in an attempt to avoid generating COPY | |
1514 | // relocs. | |
1515 | if (this->copy_relocs_.any_saved_relocs()) | |
1516 | this->copy_relocs_.emit(this->rel_dyn_section(layout)); | |
11af873f DK |
1517 | |
1518 | // For the ARM target, we need to add a PT_ARM_EXIDX segment for | |
1519 | // the .ARM.exidx section. | |
1520 | if (!layout->script_options()->saw_phdrs_clause() | |
1521 | && !parameters->options().relocatable()) | |
1522 | { | |
1523 | Output_section* exidx_section = | |
1524 | layout->find_output_section(".ARM.exidx"); | |
1525 | ||
1526 | if (exidx_section != NULL | |
1527 | && exidx_section->type() == elfcpp::SHT_ARM_EXIDX) | |
1528 | { | |
1529 | gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0) | |
1530 | == NULL); | |
1531 | Output_segment* exidx_segment = | |
1532 | layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R); | |
1533 | exidx_segment->add_output_section(exidx_section, elfcpp::PF_R); | |
1534 | } | |
1535 | } | |
4a657b0d DK |
1536 | } |
1537 | ||
bec53400 DK |
1538 | // Return whether a direct absolute static relocation needs to be applied. |
1539 | // In cases where Scan::local() or Scan::global() has created | |
1540 | // a dynamic relocation other than R_ARM_RELATIVE, the addend | |
1541 | // of the relocation is carried in the data, and we must not | |
1542 | // apply the static relocation. | |
1543 | ||
1544 | template<bool big_endian> | |
1545 | inline bool | |
1546 | Target_arm<big_endian>::Relocate::should_apply_static_reloc( | |
1547 | const Sized_symbol<32>* gsym, | |
1548 | int ref_flags, | |
1549 | bool is_32bit, | |
1550 | Output_section* output_section) | |
1551 | { | |
1552 | // If the output section is not allocated, then we didn't call | |
1553 | // scan_relocs, we didn't create a dynamic reloc, and we must apply | |
1554 | // the reloc here. | |
1555 | if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0) | |
1556 | return true; | |
1557 | ||
1558 | // For local symbols, we will have created a non-RELATIVE dynamic | |
1559 | // relocation only if (a) the output is position independent, | |
1560 | // (b) the relocation is absolute (not pc- or segment-relative), and | |
1561 | // (c) the relocation is not 32 bits wide. | |
1562 | if (gsym == NULL) | |
1563 | return !(parameters->options().output_is_position_independent() | |
1564 | && (ref_flags & Symbol::ABSOLUTE_REF) | |
1565 | && !is_32bit); | |
1566 | ||
1567 | // For global symbols, we use the same helper routines used in the | |
1568 | // scan pass. If we did not create a dynamic relocation, or if we | |
1569 | // created a RELATIVE dynamic relocation, we should apply the static | |
1570 | // relocation. | |
1571 | bool has_dyn = gsym->needs_dynamic_reloc(ref_flags); | |
1572 | bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF) | |
1573 | && gsym->can_use_relative_reloc(ref_flags | |
1574 | & Symbol::FUNCTION_CALL); | |
1575 | return !has_dyn || is_rel; | |
1576 | } | |
1577 | ||
4a657b0d DK |
1578 | // Perform a relocation. |
1579 | ||
1580 | template<bool big_endian> | |
1581 | inline bool | |
1582 | Target_arm<big_endian>::Relocate::relocate( | |
c121c671 DK |
1583 | const Relocate_info<32, big_endian>* relinfo, |
1584 | Target_arm* target, | |
1585 | Output_section *output_section, | |
1586 | size_t relnum, | |
1587 | const elfcpp::Rel<32, big_endian>& rel, | |
4a657b0d | 1588 | unsigned int r_type, |
c121c671 DK |
1589 | const Sized_symbol<32>* gsym, |
1590 | const Symbol_value<32>* psymval, | |
1591 | unsigned char* view, | |
1592 | elfcpp::Elf_types<32>::Elf_Addr address, | |
4a657b0d DK |
1593 | section_size_type /* view_size */ ) |
1594 | { | |
c121c671 DK |
1595 | typedef Arm_relocate_functions<big_endian> Arm_relocate_functions; |
1596 | ||
1597 | r_type = get_real_reloc_type(r_type); | |
1598 | ||
1599 | // If this the symbol may be a Thumb function, set thumb bit to 1. | |
1600 | bool has_thumb_bit = ((gsym != NULL) | |
1601 | && (gsym->type() == elfcpp::STT_FUNC | |
1602 | || gsym->type() == elfcpp::STT_ARM_TFUNC)); | |
1603 | ||
1604 | // Pick the value to use for symbols defined in shared objects. | |
1605 | Symbol_value<32> symval; | |
1606 | if (gsym != NULL | |
1607 | && gsym->use_plt_offset(reloc_is_non_pic(r_type))) | |
1608 | { | |
1609 | symval.set_output_value(target->plt_section()->address() | |
1610 | + gsym->plt_offset()); | |
1611 | psymval = &symval; | |
1612 | has_thumb_bit = 0; | |
1613 | } | |
1614 | ||
1615 | const Sized_relobj<32, big_endian>* object = relinfo->object; | |
1616 | ||
1617 | // Get the GOT offset if needed. | |
1618 | // The GOT pointer points to the end of the GOT section. | |
1619 | // We need to subtract the size of the GOT section to get | |
1620 | // the actual offset to use in the relocation. | |
1621 | bool have_got_offset = false; | |
1622 | unsigned int got_offset = 0; | |
1623 | switch (r_type) | |
1624 | { | |
1625 | case elfcpp::R_ARM_GOT_BREL: | |
1626 | if (gsym != NULL) | |
1627 | { | |
1628 | gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); | |
1629 | got_offset = (gsym->got_offset(GOT_TYPE_STANDARD) | |
1630 | - target->got_size()); | |
1631 | } | |
1632 | else | |
1633 | { | |
1634 | unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info()); | |
1635 | gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); | |
1636 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD) | |
1637 | - target->got_size()); | |
1638 | } | |
1639 | have_got_offset = true; | |
1640 | break; | |
1641 | ||
1642 | default: | |
1643 | break; | |
1644 | } | |
1645 | ||
1646 | typename Arm_relocate_functions::Status reloc_status = | |
1647 | Arm_relocate_functions::STATUS_OKAY; | |
4a657b0d DK |
1648 | switch (r_type) |
1649 | { | |
1650 | case elfcpp::R_ARM_NONE: | |
1651 | break; | |
1652 | ||
5e445df6 ILT |
1653 | case elfcpp::R_ARM_ABS8: |
1654 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false, | |
1655 | output_section)) | |
1656 | reloc_status = Arm_relocate_functions::abs8(view, object, psymval, | |
1657 | has_thumb_bit); | |
1658 | break; | |
1659 | ||
c121c671 DK |
1660 | case elfcpp::R_ARM_ABS32: |
1661 | if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true, | |
1662 | output_section)) | |
1663 | reloc_status = Arm_relocate_functions::abs32(view, object, psymval, | |
1664 | has_thumb_bit); | |
1665 | break; | |
1666 | ||
1667 | case elfcpp::R_ARM_REL32: | |
1668 | reloc_status = Arm_relocate_functions::rel32(view, object, psymval, | |
1669 | address, has_thumb_bit); | |
1670 | break; | |
1671 | ||
1672 | case elfcpp::R_ARM_THM_CALL: | |
1673 | reloc_status = Arm_relocate_functions::thm_call(view, object, psymval, | |
1674 | address, has_thumb_bit); | |
1675 | break; | |
1676 | ||
1677 | case elfcpp::R_ARM_GOTOFF32: | |
1678 | { | |
1679 | elfcpp::Elf_types<32>::Elf_Addr got_origin; | |
1680 | got_origin = target->got_plt_section()->address(); | |
1681 | reloc_status = Arm_relocate_functions::rel32(view, object, psymval, | |
1682 | got_origin, has_thumb_bit); | |
1683 | } | |
1684 | break; | |
1685 | ||
1686 | case elfcpp::R_ARM_BASE_PREL: | |
1687 | { | |
1688 | uint32_t origin; | |
1689 | // Get the addressing origin of the output segment defining the | |
1690 | // symbol gsym (AAELF 4.6.1.2 Relocation types) | |
1691 | gold_assert(gsym != NULL); | |
1692 | if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT) | |
1693 | origin = gsym->output_segment()->vaddr(); | |
1694 | else if (gsym->source () == Symbol::IN_OUTPUT_DATA) | |
1695 | origin = gsym->output_data()->address(); | |
1696 | else | |
1697 | { | |
1698 | gold_error_at_location(relinfo, relnum, rel.get_r_offset(), | |
1699 | _("cannot find origin of R_ARM_BASE_PREL")); | |
1700 | return true; | |
1701 | } | |
1702 | reloc_status = Arm_relocate_functions::base_prel(view, origin, address); | |
1703 | } | |
1704 | break; | |
1705 | ||
1706 | case elfcpp::R_ARM_GOT_BREL: | |
1707 | gold_assert(have_got_offset); | |
1708 | reloc_status = Arm_relocate_functions::got_brel(view, got_offset); | |
1709 | break; | |
1710 | ||
1711 | case elfcpp::R_ARM_PLT32: | |
1712 | gold_assert(gsym == NULL | |
1713 | || gsym->has_plt_offset() | |
1714 | || gsym->final_value_is_known() | |
1715 | || (gsym->is_defined() | |
1716 | && !gsym->is_from_dynobj() | |
1717 | && !gsym->is_preemptible())); | |
1718 | reloc_status = Arm_relocate_functions::plt32(view, object, psymval, | |
1719 | address, has_thumb_bit); | |
1720 | break; | |
1721 | ||
1722 | case elfcpp::R_ARM_CALL: | |
1723 | reloc_status = Arm_relocate_functions::call(view, object, psymval, | |
1724 | address, has_thumb_bit); | |
1725 | break; | |
1726 | ||
1727 | case elfcpp::R_ARM_JUMP24: | |
1728 | reloc_status = Arm_relocate_functions::jump24(view, object, psymval, | |
1729 | address, has_thumb_bit); | |
1730 | break; | |
1731 | ||
1732 | case elfcpp::R_ARM_PREL31: | |
1733 | reloc_status = Arm_relocate_functions::prel31(view, object, psymval, | |
1734 | address, has_thumb_bit); | |
1735 | break; | |
1736 | ||
1737 | case elfcpp::R_ARM_TARGET1: | |
1738 | // This should have been mapped to another type already. | |
1739 | // Fall through. | |
1740 | case elfcpp::R_ARM_COPY: | |
1741 | case elfcpp::R_ARM_GLOB_DAT: | |
1742 | case elfcpp::R_ARM_JUMP_SLOT: | |
1743 | case elfcpp::R_ARM_RELATIVE: | |
1744 | // These are relocations which should only be seen by the | |
1745 | // dynamic linker, and should never be seen here. | |
1746 | gold_error_at_location(relinfo, relnum, rel.get_r_offset(), | |
1747 | _("unexpected reloc %u in object file"), | |
1748 | r_type); | |
1749 | break; | |
1750 | ||
1751 | default: | |
1752 | gold_error_at_location(relinfo, relnum, rel.get_r_offset(), | |
1753 | _("unsupported reloc %u"), | |
1754 | r_type); | |
1755 | break; | |
1756 | } | |
1757 | ||
1758 | // Report any errors. | |
1759 | switch (reloc_status) | |
1760 | { | |
1761 | case Arm_relocate_functions::STATUS_OKAY: | |
1762 | break; | |
1763 | case Arm_relocate_functions::STATUS_OVERFLOW: | |
1764 | gold_error_at_location(relinfo, relnum, rel.get_r_offset(), | |
1765 | _("relocation overflow in relocation %u"), | |
1766 | r_type); | |
1767 | break; | |
1768 | case Arm_relocate_functions::STATUS_BAD_RELOC: | |
1769 | gold_error_at_location( | |
1770 | relinfo, | |
1771 | relnum, | |
1772 | rel.get_r_offset(), | |
1773 | _("unexpected opcode while processing relocation %u"), | |
1774 | r_type); | |
1775 | break; | |
4a657b0d DK |
1776 | default: |
1777 | gold_unreachable(); | |
1778 | } | |
1779 | ||
1780 | return true; | |
1781 | } | |
1782 | ||
1783 | // Relocate section data. | |
1784 | ||
1785 | template<bool big_endian> | |
1786 | void | |
1787 | Target_arm<big_endian>::relocate_section( | |
1788 | const Relocate_info<32, big_endian>* relinfo, | |
1789 | unsigned int sh_type, | |
1790 | const unsigned char* prelocs, | |
1791 | size_t reloc_count, | |
1792 | Output_section* output_section, | |
1793 | bool needs_special_offset_handling, | |
1794 | unsigned char* view, | |
1795 | elfcpp::Elf_types<32>::Elf_Addr address, | |
364c7fa5 ILT |
1796 | section_size_type view_size, |
1797 | const Reloc_symbol_changes* reloc_symbol_changes) | |
4a657b0d DK |
1798 | { |
1799 | typedef typename Target_arm<big_endian>::Relocate Arm_relocate; | |
1800 | gold_assert(sh_type == elfcpp::SHT_REL); | |
1801 | ||
1802 | gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL, | |
1803 | Arm_relocate>( | |
1804 | relinfo, | |
1805 | this, | |
1806 | prelocs, | |
1807 | reloc_count, | |
1808 | output_section, | |
1809 | needs_special_offset_handling, | |
1810 | view, | |
1811 | address, | |
364c7fa5 ILT |
1812 | view_size, |
1813 | reloc_symbol_changes); | |
4a657b0d DK |
1814 | } |
1815 | ||
1816 | // Return the size of a relocation while scanning during a relocatable | |
1817 | // link. | |
1818 | ||
1819 | template<bool big_endian> | |
1820 | unsigned int | |
1821 | Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc( | |
1822 | unsigned int r_type, | |
1823 | Relobj* object) | |
1824 | { | |
1825 | r_type = get_real_reloc_type(r_type); | |
1826 | switch (r_type) | |
1827 | { | |
1828 | case elfcpp::R_ARM_NONE: | |
1829 | return 0; | |
1830 | ||
5e445df6 ILT |
1831 | case elfcpp::R_ARM_ABS8: |
1832 | return 1; | |
1833 | ||
4a657b0d DK |
1834 | case elfcpp::R_ARM_ABS32: |
1835 | case elfcpp::R_ARM_REL32: | |
1836 | case elfcpp::R_ARM_THM_CALL: | |
1837 | case elfcpp::R_ARM_GOTOFF32: | |
1838 | case elfcpp::R_ARM_BASE_PREL: | |
1839 | case elfcpp::R_ARM_GOT_BREL: | |
1840 | case elfcpp::R_ARM_PLT32: | |
1841 | case elfcpp::R_ARM_CALL: | |
1842 | case elfcpp::R_ARM_JUMP24: | |
1843 | case elfcpp::R_ARM_PREL31: | |
1844 | return 4; | |
1845 | ||
1846 | case elfcpp::R_ARM_TARGET1: | |
1847 | // This should have been mapped to another type already. | |
1848 | // Fall through. | |
1849 | case elfcpp::R_ARM_COPY: | |
1850 | case elfcpp::R_ARM_GLOB_DAT: | |
1851 | case elfcpp::R_ARM_JUMP_SLOT: | |
1852 | case elfcpp::R_ARM_RELATIVE: | |
1853 | // These are relocations which should only be seen by the | |
1854 | // dynamic linker, and should never be seen here. | |
1855 | gold_error(_("%s: unexpected reloc %u in object file"), | |
1856 | object->name().c_str(), r_type); | |
1857 | return 0; | |
1858 | ||
1859 | default: | |
1860 | object->error(_("unsupported reloc %u in object file"), r_type); | |
1861 | return 0; | |
1862 | } | |
1863 | } | |
1864 | ||
1865 | // Scan the relocs during a relocatable link. | |
1866 | ||
1867 | template<bool big_endian> | |
1868 | void | |
1869 | Target_arm<big_endian>::scan_relocatable_relocs( | |
1870 | const General_options& options, | |
1871 | Symbol_table* symtab, | |
1872 | Layout* layout, | |
1873 | Sized_relobj<32, big_endian>* object, | |
1874 | unsigned int data_shndx, | |
1875 | unsigned int sh_type, | |
1876 | const unsigned char* prelocs, | |
1877 | size_t reloc_count, | |
1878 | Output_section* output_section, | |
1879 | bool needs_special_offset_handling, | |
1880 | size_t local_symbol_count, | |
1881 | const unsigned char* plocal_symbols, | |
1882 | Relocatable_relocs* rr) | |
1883 | { | |
1884 | gold_assert(sh_type == elfcpp::SHT_REL); | |
1885 | ||
1886 | typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL, | |
1887 | Relocatable_size_for_reloc> Scan_relocatable_relocs; | |
1888 | ||
1889 | gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL, | |
1890 | Scan_relocatable_relocs>( | |
1891 | options, | |
1892 | symtab, | |
1893 | layout, | |
1894 | object, | |
1895 | data_shndx, | |
1896 | prelocs, | |
1897 | reloc_count, | |
1898 | output_section, | |
1899 | needs_special_offset_handling, | |
1900 | local_symbol_count, | |
1901 | plocal_symbols, | |
1902 | rr); | |
1903 | } | |
1904 | ||
1905 | // Relocate a section during a relocatable link. | |
1906 | ||
1907 | template<bool big_endian> | |
1908 | void | |
1909 | Target_arm<big_endian>::relocate_for_relocatable( | |
1910 | const Relocate_info<32, big_endian>* relinfo, | |
1911 | unsigned int sh_type, | |
1912 | const unsigned char* prelocs, | |
1913 | size_t reloc_count, | |
1914 | Output_section* output_section, | |
1915 | off_t offset_in_output_section, | |
1916 | const Relocatable_relocs* rr, | |
1917 | unsigned char* view, | |
1918 | elfcpp::Elf_types<32>::Elf_Addr view_address, | |
1919 | section_size_type view_size, | |
1920 | unsigned char* reloc_view, | |
1921 | section_size_type reloc_view_size) | |
1922 | { | |
1923 | gold_assert(sh_type == elfcpp::SHT_REL); | |
1924 | ||
1925 | gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>( | |
1926 | relinfo, | |
1927 | prelocs, | |
1928 | reloc_count, | |
1929 | output_section, | |
1930 | offset_in_output_section, | |
1931 | rr, | |
1932 | view, | |
1933 | view_address, | |
1934 | view_size, | |
1935 | reloc_view, | |
1936 | reloc_view_size); | |
1937 | } | |
1938 | ||
94cdfcff DK |
1939 | // Return the value to use for a dynamic symbol which requires special |
1940 | // treatment. This is how we support equality comparisons of function | |
1941 | // pointers across shared library boundaries, as described in the | |
1942 | // processor specific ABI supplement. | |
1943 | ||
4a657b0d DK |
1944 | template<bool big_endian> |
1945 | uint64_t | |
94cdfcff | 1946 | Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const |
4a657b0d | 1947 | { |
94cdfcff DK |
1948 | gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); |
1949 | return this->plt_section()->address() + gsym->plt_offset(); | |
4a657b0d DK |
1950 | } |
1951 | ||
1952 | // Map platform-specific relocs to real relocs | |
1953 | // | |
1954 | template<bool big_endian> | |
1955 | unsigned int | |
1956 | Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type) | |
1957 | { | |
1958 | switch (r_type) | |
1959 | { | |
1960 | case elfcpp::R_ARM_TARGET1: | |
1961 | // This is either R_ARM_ABS32 or R_ARM_REL32; | |
1962 | return elfcpp::R_ARM_ABS32; | |
1963 | ||
1964 | case elfcpp::R_ARM_TARGET2: | |
1965 | // This can be any reloc type but ususally is R_ARM_GOT_PREL | |
1966 | return elfcpp::R_ARM_GOT_PREL; | |
1967 | ||
1968 | default: | |
1969 | return r_type; | |
1970 | } | |
1971 | } | |
1972 | ||
1973 | // The selector for arm object files. | |
1974 | ||
1975 | template<bool big_endian> | |
1976 | class Target_selector_arm : public Target_selector | |
1977 | { | |
1978 | public: | |
1979 | Target_selector_arm() | |
1980 | : Target_selector(elfcpp::EM_ARM, 32, big_endian, | |
1981 | (big_endian ? "elf32-bigarm" : "elf32-littlearm")) | |
1982 | { } | |
1983 | ||
1984 | Target* | |
1985 | do_instantiate_target() | |
1986 | { return new Target_arm<big_endian>(); } | |
1987 | }; | |
1988 | ||
1989 | Target_selector_arm<false> target_selector_arm; | |
1990 | Target_selector_arm<true> target_selector_armbe; | |
1991 | ||
1992 | } // End anonymous namespace. |