bfd/
[binutils.git] / gold / arm.cc
blob17028b5ebf758486e3f9b65a973be38b16755c77
1 // arm.cc -- arm target support for gold.
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>.
7 // This file is part of gold.
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.
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.
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.
24 #include "gold.h"
26 #include <cstring>
27 #include <limits>
28 #include <cstdio>
29 #include <string>
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"
46 namespace
49 using namespace gold;
51 template<bool big_endian>
52 class Output_data_plt_arm;
54 // The arm target class.
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.
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 //
77 // TODOs:
78 // - Generate various branch stubs.
79 // - Support interworking.
80 // - Define section symbols __exidx_start and __exidx_stop.
81 // - Support more relocation types as needed.
82 // - Make PLTs more flexible for different architecture features like
83 // Thumb-2 and BE8.
84 // There are probably a lot more.
86 // Utilities for manipulating integers of up to 32-bits
88 namespace utils
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)
96 gold_assert(no_bits >= 0 && no_bits <= 32);
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;
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)
111 gold_assert(no_bits >= 0 && no_bits <= 32);
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;
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)
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;
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); }
145 template<bool big_endian>
146 class Target_arm : public Sized_target<32, big_endian>
148 public:
149 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
150 Reloc_section;
152 Target_arm()
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)
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);
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);
189 // Finalize the sections.
190 void
191 do_finalize_sections(Layout*);
193 // Return the value to use for a dynamic symbol which requires special
194 // treatment.
195 uint64_t
196 do_dynsym_value(const Symbol*) const;
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,
208 section_size_type view_size);
210 // Scan the relocs during a relocatable link.
211 void
212 scan_relocatable_relocs(const General_options& options,
213 Symbol_table* symtab,
214 Layout* layout,
215 Sized_relobj<32, big_endian>* object,
216 unsigned int data_shndx,
217 unsigned int sh_type,
218 const unsigned char* prelocs,
219 size_t reloc_count,
220 Output_section* output_section,
221 bool needs_special_offset_handling,
222 size_t local_symbol_count,
223 const unsigned char* plocal_symbols,
224 Relocatable_relocs*);
226 // Relocate a section during a relocatable link.
227 void
228 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
229 unsigned int sh_type,
230 const unsigned char* prelocs,
231 size_t reloc_count,
232 Output_section* output_section,
233 off_t offset_in_output_section,
234 const Relocatable_relocs*,
235 unsigned char* view,
236 elfcpp::Elf_types<32>::Elf_Addr view_address,
237 section_size_type view_size,
238 unsigned char* reloc_view,
239 section_size_type reloc_view_size);
241 // Return whether SYM is defined by the ABI.
242 bool
243 do_is_defined_by_abi(Symbol* sym) const
244 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
246 // Return the size of the GOT section.
247 section_size_type
248 got_size()
250 gold_assert(this->got_ != NULL);
251 return this->got_->data_size();
254 // Map platform-specific reloc types
255 static unsigned int
256 get_real_reloc_type (unsigned int r_type);
258 private:
259 // The class which scans relocations.
260 class Scan
262 public:
263 Scan()
264 : issued_non_pic_error_(false)
267 inline void
268 local(const General_options& options, Symbol_table* symtab,
269 Layout* layout, Target_arm* target,
270 Sized_relobj<32, big_endian>* object,
271 unsigned int data_shndx,
272 Output_section* output_section,
273 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
274 const elfcpp::Sym<32, big_endian>& lsym);
276 inline void
277 global(const General_options& options, Symbol_table* symtab,
278 Layout* layout, Target_arm* target,
279 Sized_relobj<32, big_endian>* object,
280 unsigned int data_shndx,
281 Output_section* output_section,
282 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
283 Symbol* gsym);
285 private:
286 static void
287 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
288 unsigned int r_type);
290 static void
291 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
292 unsigned int r_type, Symbol*);
294 void
295 check_non_pic(Relobj*, unsigned int r_type);
297 // Almost identical to Symbol::needs_plt_entry except that it also
298 // handles STT_ARM_TFUNC.
299 static bool
300 symbol_needs_plt_entry(const Symbol* sym)
302 // An undefined symbol from an executable does not need a PLT entry.
303 if (sym->is_undefined() && !parameters->options().shared())
304 return false;
306 return (!parameters->doing_static_link()
307 && (sym->type() == elfcpp::STT_FUNC
308 || sym->type() == elfcpp::STT_ARM_TFUNC)
309 && (sym->is_from_dynobj()
310 || sym->is_undefined()
311 || sym->is_preemptible()));
314 // Whether we have issued an error about a non-PIC compilation.
315 bool issued_non_pic_error_;
318 // The class which implements relocation.
319 class Relocate
321 public:
322 Relocate()
325 ~Relocate()
328 // Return whether the static relocation needs to be applied.
329 inline bool
330 should_apply_static_reloc(const Sized_symbol<32>* gsym,
331 int ref_flags,
332 bool is_32bit,
333 Output_section* output_section);
335 // Do a relocation. Return false if the caller should not issue
336 // any warnings about this relocation.
337 inline bool
338 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
339 Output_section*, size_t relnum,
340 const elfcpp::Rel<32, big_endian>&,
341 unsigned int r_type, const Sized_symbol<32>*,
342 const Symbol_value<32>*,
343 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
344 section_size_type);
346 // Return whether we want to pass flag NON_PIC_REF for this
347 // reloc.
348 static inline bool
349 reloc_is_non_pic (unsigned int r_type)
351 switch (r_type)
353 case elfcpp::R_ARM_REL32:
354 case elfcpp::R_ARM_THM_CALL:
355 case elfcpp::R_ARM_CALL:
356 case elfcpp::R_ARM_JUMP24:
357 case elfcpp::R_ARM_PREL31:
358 return true;
359 default:
360 return false;
365 // A class which returns the size required for a relocation type,
366 // used while scanning relocs during a relocatable link.
367 class Relocatable_size_for_reloc
369 public:
370 unsigned int
371 get_size_for_reloc(unsigned int, Relobj*);
374 // Get the GOT section, creating it if necessary.
375 Output_data_got<32, big_endian>*
376 got_section(Symbol_table*, Layout*);
378 // Get the GOT PLT section.
379 Output_data_space*
380 got_plt_section() const
382 gold_assert(this->got_plt_ != NULL);
383 return this->got_plt_;
386 // Create a PLT entry for a global symbol.
387 void
388 make_plt_entry(Symbol_table*, Layout*, Symbol*);
390 // Get the PLT section.
391 const Output_data_plt_arm<big_endian>*
392 plt_section() const
394 gold_assert(this->plt_ != NULL);
395 return this->plt_;
398 // Get the dynamic reloc section, creating it if necessary.
399 Reloc_section*
400 rel_dyn_section(Layout*);
402 // Return true if the symbol may need a COPY relocation.
403 // References from an executable object to non-function symbols
404 // defined in a dynamic object may need a COPY relocation.
405 bool
406 may_need_copy_reloc(Symbol* gsym)
408 return (gsym->type() != elfcpp::STT_ARM_TFUNC
409 && gsym->may_need_copy_reloc());
412 // Add a potential copy relocation.
413 void
414 copy_reloc(Symbol_table* symtab, Layout* layout,
415 Sized_relobj<32, big_endian>* object,
416 unsigned int shndx, Output_section* output_section,
417 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
419 this->copy_relocs_.copy_reloc(symtab, layout,
420 symtab->get_sized_symbol<32>(sym),
421 object, shndx, output_section, reloc,
422 this->rel_dyn_section(layout));
425 // Information about this specific target which we pass to the
426 // general Target structure.
427 static const Target::Target_info arm_info;
429 // The types of GOT entries needed for this platform.
430 enum Got_type
432 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
435 // The GOT section.
436 Output_data_got<32, big_endian>* got_;
437 // The PLT section.
438 Output_data_plt_arm<big_endian>* plt_;
439 // The GOT PLT section.
440 Output_data_space* got_plt_;
441 // The dynamic reloc section.
442 Reloc_section* rel_dyn_;
443 // Relocs saved to avoid a COPY reloc.
444 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
445 // Space for variables copied with a COPY reloc.
446 Output_data_space* dynbss_;
449 template<bool big_endian>
450 const Target::Target_info Target_arm<big_endian>::arm_info =
452 32, // size
453 big_endian, // is_big_endian
454 elfcpp::EM_ARM, // machine_code
455 false, // has_make_symbol
456 false, // has_resolve
457 false, // has_code_fill
458 true, // is_default_stack_executable
459 '\0', // wrap_char
460 "/usr/lib/libc.so.1", // dynamic_linker
461 0x8000, // default_text_segment_address
462 0x1000, // abi_pagesize (overridable by -z max-page-size)
463 0x1000, // common_pagesize (overridable by -z common-page-size)
464 elfcpp::SHN_UNDEF, // small_common_shndx
465 elfcpp::SHN_UNDEF, // large_common_shndx
466 0, // small_common_section_flags
467 0 // large_common_section_flags
470 // Arm relocate functions class
473 template<bool big_endian>
474 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
476 public:
477 typedef enum
479 STATUS_OKAY, // No error during relocation.
480 STATUS_OVERFLOW, // Relocation oveflow.
481 STATUS_BAD_RELOC // Relocation cannot be applied.
482 } Status;
484 private:
485 typedef Relocate_functions<32, big_endian> Base;
486 typedef Arm_relocate_functions<big_endian> This;
488 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
489 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
490 // to distinguish ARM and THUMB functions and it is treated specially.
491 static inline Symbol_value<32>::Value
492 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
493 const Symbol_value<32>* psymval,
494 Symbol_value<32>::Value addend,
495 bool has_thumb_bit)
497 typedef Symbol_value<32>::Value Valtype;
499 if (has_thumb_bit)
501 Valtype raw = psymval->value(object, 0);
502 Valtype thumb_bit = raw & 1;
503 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
505 else
506 return psymval->value(object, addend);
509 // FIXME: This probably only works for Android on ARM v5te. We should
510 // following GNU ld for the general case.
511 template<unsigned r_type>
512 static inline typename This::Status
513 arm_branch_common(unsigned char *view,
514 const Sized_relobj<32, big_endian>* object,
515 const Symbol_value<32>* psymval,
516 elfcpp::Elf_types<32>::Elf_Addr address,
517 bool has_thumb_bit)
519 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
520 Valtype* wv = reinterpret_cast<Valtype*>(view);
521 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
523 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
524 && ((val & 0x0f000000UL) == 0x0a000000UL);
525 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
526 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
527 && ((val & 0x0f000000UL) == 0x0b000000UL);
528 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
529 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
531 if (r_type == elfcpp::R_ARM_CALL)
533 if (!insn_is_uncond_bl && !insn_is_blx)
534 return This::STATUS_BAD_RELOC;
536 else if (r_type == elfcpp::R_ARM_JUMP24)
538 if (!insn_is_b && !insn_is_cond_bl)
539 return This::STATUS_BAD_RELOC;
541 else if (r_type == elfcpp::R_ARM_PLT32)
543 if (!insn_is_any_branch)
544 return This::STATUS_BAD_RELOC;
546 else
547 gold_unreachable();
549 Valtype addend = utils::sign_extend<26>(val << 2);
550 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
551 - address);
553 // If target has thumb bit set, we need to either turn the BL
554 // into a BLX (for ARMv5 or above) or generate a stub.
555 if (x & 1)
557 // Turn BL to BLX.
558 if (insn_is_uncond_bl)
559 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
560 else
561 return This::STATUS_BAD_RELOC;
563 else
564 gold_assert(!insn_is_blx);
566 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
567 elfcpp::Swap<32, big_endian>::writeval(wv, val);
568 return (utils::has_overflow<26>(x)
569 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
572 public:
574 // R_ARM_ABS8: S + A
575 static inline typename This::Status
576 abs8(unsigned char *view,
577 const Sized_relobj<32, big_endian>* object,
578 const Symbol_value<32>* psymval, bool has_thumb_bit)
580 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
581 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
582 Valtype* wv = reinterpret_cast<Valtype*>(view);
583 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
584 Reltype addend = utils::sign_extend<8>(val);
585 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
586 val = utils::bit_select(val, x, 0xffU);
587 elfcpp::Swap<8, big_endian>::writeval(wv, val);
588 return (utils::has_signed_unsigned_overflow<8>(x)
589 ? This::STATUS_OVERFLOW
590 : This::STATUS_OKAY);
593 // R_ARM_ABS32: (S + A) | T
594 static inline typename This::Status
595 abs32(unsigned char *view,
596 const Sized_relobj<32, big_endian>* object,
597 const Symbol_value<32>* psymval,
598 bool has_thumb_bit)
600 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
601 Valtype* wv = reinterpret_cast<Valtype*>(view);
602 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
603 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
604 elfcpp::Swap<32, big_endian>::writeval(wv, x);
605 return This::STATUS_OKAY;
608 // R_ARM_REL32: (S + A) | T - P
609 static inline typename This::Status
610 rel32(unsigned char *view,
611 const Sized_relobj<32, big_endian>* object,
612 const Symbol_value<32>* psymval,
613 elfcpp::Elf_types<32>::Elf_Addr address,
614 bool has_thumb_bit)
616 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
617 Valtype* wv = reinterpret_cast<Valtype*>(view);
618 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
619 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
620 - address);
621 elfcpp::Swap<32, big_endian>::writeval(wv, x);
622 return This::STATUS_OKAY;
625 // R_ARM_THM_CALL: (S + A) | T - P
626 static inline typename This::Status
627 thm_call(unsigned char *view,
628 const Sized_relobj<32, big_endian>* object,
629 const Symbol_value<32>* psymval,
630 elfcpp::Elf_types<32>::Elf_Addr address,
631 bool has_thumb_bit)
633 // A thumb call consists of two instructions.
634 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
635 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
636 Valtype* wv = reinterpret_cast<Valtype*>(view);
637 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
638 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
639 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
640 gold_assert((lo & 0xf800) == 0xf800);
641 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
642 | ((lo & 0x7ff) << 1));
643 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
644 - address);
646 // If target has no thumb bit set, we need to either turn the BL
647 // into a BLX (for ARMv5 or above) or generate a stub.
648 if ((x & 1) == 0)
650 // This only works for ARMv5 and above with interworking enabled.
651 lo &= 0xefff;
653 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
654 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
655 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
656 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
657 return (utils::has_overflow<23>(x)
658 ? This::STATUS_OVERFLOW
659 : This::STATUS_OKAY);
662 // R_ARM_BASE_PREL: B(S) + A - P
663 static inline typename This::Status
664 base_prel(unsigned char* view,
665 elfcpp::Elf_types<32>::Elf_Addr origin,
666 elfcpp::Elf_types<32>::Elf_Addr address)
668 Base::rel32(view, origin - address);
669 return STATUS_OKAY;
672 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
673 static inline typename This::Status
674 got_brel(unsigned char* view,
675 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
677 Base::rel32(view, got_offset);
678 return This::STATUS_OKAY;
681 // R_ARM_PLT32: (S + A) | T - P
682 static inline typename This::Status
683 plt32(unsigned char *view,
684 const Sized_relobj<32, big_endian>* object,
685 const Symbol_value<32>* psymval,
686 elfcpp::Elf_types<32>::Elf_Addr address,
687 bool has_thumb_bit)
689 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
690 address, has_thumb_bit);
693 // R_ARM_CALL: (S + A) | T - P
694 static inline typename This::Status
695 call(unsigned char *view,
696 const Sized_relobj<32, big_endian>* object,
697 const Symbol_value<32>* psymval,
698 elfcpp::Elf_types<32>::Elf_Addr address,
699 bool has_thumb_bit)
701 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
702 address, has_thumb_bit);
705 // R_ARM_JUMP24: (S + A) | T - P
706 static inline typename This::Status
707 jump24(unsigned char *view,
708 const Sized_relobj<32, big_endian>* object,
709 const Symbol_value<32>* psymval,
710 elfcpp::Elf_types<32>::Elf_Addr address,
711 bool has_thumb_bit)
713 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
714 address, has_thumb_bit);
717 // R_ARM_PREL: (S + A) | T - P
718 static inline typename This::Status
719 prel31(unsigned char *view,
720 const Sized_relobj<32, big_endian>* object,
721 const Symbol_value<32>* psymval,
722 elfcpp::Elf_types<32>::Elf_Addr address,
723 bool has_thumb_bit)
725 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
726 Valtype* wv = reinterpret_cast<Valtype*>(view);
727 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
728 Valtype addend = utils::sign_extend<31>(val);
729 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
730 - address);
731 val = utils::bit_select(val, x, 0x7fffffffU);
732 elfcpp::Swap<32, big_endian>::writeval(wv, val);
733 return (utils::has_overflow<31>(x) ?
734 This::STATUS_OVERFLOW : This::STATUS_OKAY);
738 // Get the GOT section, creating it if necessary.
740 template<bool big_endian>
741 Output_data_got<32, big_endian>*
742 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
744 if (this->got_ == NULL)
746 gold_assert(symtab != NULL && layout != NULL);
748 this->got_ = new Output_data_got<32, big_endian>();
750 Output_section* os;
751 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
752 (elfcpp::SHF_ALLOC
753 | elfcpp::SHF_WRITE),
754 this->got_);
755 os->set_is_relro();
757 // The old GNU linker creates a .got.plt section. We just
758 // create another set of data in the .got section. Note that we
759 // always create a PLT if we create a GOT, although the PLT
760 // might be empty.
761 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
762 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
763 (elfcpp::SHF_ALLOC
764 | elfcpp::SHF_WRITE),
765 this->got_plt_);
766 os->set_is_relro();
768 // The first three entries are reserved.
769 this->got_plt_->set_current_data_size(3 * 4);
771 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
772 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
773 this->got_plt_,
774 0, 0, elfcpp::STT_OBJECT,
775 elfcpp::STB_LOCAL,
776 elfcpp::STV_HIDDEN, 0,
777 false, false);
779 return this->got_;
782 // Get the dynamic reloc section, creating it if necessary.
784 template<bool big_endian>
785 typename Target_arm<big_endian>::Reloc_section*
786 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
788 if (this->rel_dyn_ == NULL)
790 gold_assert(layout != NULL);
791 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
792 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
793 elfcpp::SHF_ALLOC, this->rel_dyn_);
795 return this->rel_dyn_;
798 // A class to handle the PLT data.
800 template<bool big_endian>
801 class Output_data_plt_arm : public Output_section_data
803 public:
804 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
805 Reloc_section;
807 Output_data_plt_arm(Layout*, Output_data_space*);
809 // Add an entry to the PLT.
810 void
811 add_entry(Symbol* gsym);
813 // Return the .rel.plt section data.
814 const Reloc_section*
815 rel_plt() const
816 { return this->rel_; }
818 protected:
819 void
820 do_adjust_output_section(Output_section* os);
822 // Write to a map file.
823 void
824 do_print_to_mapfile(Mapfile* mapfile) const
825 { mapfile->print_output_data(this, _("** PLT")); }
827 private:
828 // Template for the first PLT entry.
829 static const uint32_t first_plt_entry[5];
831 // Template for subsequent PLT entries.
832 static const uint32_t plt_entry[3];
834 // Set the final size.
835 void
836 set_final_data_size()
838 this->set_data_size(sizeof(first_plt_entry)
839 + this->count_ * sizeof(plt_entry));
842 // Write out the PLT data.
843 void
844 do_write(Output_file*);
846 // The reloc section.
847 Reloc_section* rel_;
848 // The .got.plt section.
849 Output_data_space* got_plt_;
850 // The number of PLT entries.
851 unsigned int count_;
854 // Create the PLT section. The ordinary .got section is an argument,
855 // since we need to refer to the start. We also create our own .got
856 // section just for PLT entries.
858 template<bool big_endian>
859 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
860 Output_data_space* got_plt)
861 : Output_section_data(4), got_plt_(got_plt), count_(0)
863 this->rel_ = new Reloc_section(false);
864 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
865 elfcpp::SHF_ALLOC, this->rel_);
868 template<bool big_endian>
869 void
870 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
872 os->set_entsize(0);
875 // Add an entry to the PLT.
877 template<bool big_endian>
878 void
879 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
881 gold_assert(!gsym->has_plt_offset());
883 // Note that when setting the PLT offset we skip the initial
884 // reserved PLT entry.
885 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
886 + sizeof(first_plt_entry));
888 ++this->count_;
890 section_offset_type got_offset = this->got_plt_->current_data_size();
892 // Every PLT entry needs a GOT entry which points back to the PLT
893 // entry (this will be changed by the dynamic linker, normally
894 // lazily when the function is called).
895 this->got_plt_->set_current_data_size(got_offset + 4);
897 // Every PLT entry needs a reloc.
898 gsym->set_needs_dynsym_entry();
899 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
900 got_offset);
902 // Note that we don't need to save the symbol. The contents of the
903 // PLT are independent of which symbols are used. The symbols only
904 // appear in the relocations.
907 // ARM PLTs.
908 // FIXME: This is not very flexible. Right now this has only been tested
909 // on armv5te. If we are to support additional architecture features like
910 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
912 // The first entry in the PLT.
913 template<bool big_endian>
914 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
916 0xe52de004, // str lr, [sp, #-4]!
917 0xe59fe004, // ldr lr, [pc, #4]
918 0xe08fe00e, // add lr, pc, lr
919 0xe5bef008, // ldr pc, [lr, #8]!
920 0x00000000, // &GOT[0] - .
923 // Subsequent entries in the PLT.
925 template<bool big_endian>
926 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
928 0xe28fc600, // add ip, pc, #0xNN00000
929 0xe28cca00, // add ip, ip, #0xNN000
930 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
933 // Write out the PLT. This uses the hand-coded instructions above,
934 // and adjusts them as needed. This is all specified by the arm ELF
935 // Processor Supplement.
937 template<bool big_endian>
938 void
939 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
941 const off_t offset = this->offset();
942 const section_size_type oview_size =
943 convert_to_section_size_type(this->data_size());
944 unsigned char* const oview = of->get_output_view(offset, oview_size);
946 const off_t got_file_offset = this->got_plt_->offset();
947 const section_size_type got_size =
948 convert_to_section_size_type(this->got_plt_->data_size());
949 unsigned char* const got_view = of->get_output_view(got_file_offset,
950 got_size);
951 unsigned char* pov = oview;
953 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
954 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
956 // Write first PLT entry. All but the last word are constants.
957 const size_t num_first_plt_words = (sizeof(first_plt_entry)
958 / sizeof(plt_entry[0]));
959 for (size_t i = 0; i < num_first_plt_words - 1; i++)
960 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
961 // Last word in first PLT entry is &GOT[0] - .
962 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
963 got_address - (plt_address + 16));
964 pov += sizeof(first_plt_entry);
966 unsigned char* got_pov = got_view;
968 memset(got_pov, 0, 12);
969 got_pov += 12;
971 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
972 unsigned int plt_offset = sizeof(first_plt_entry);
973 unsigned int plt_rel_offset = 0;
974 unsigned int got_offset = 12;
975 const unsigned int count = this->count_;
976 for (unsigned int i = 0;
977 i < count;
978 ++i,
979 pov += sizeof(plt_entry),
980 got_pov += 4,
981 plt_offset += sizeof(plt_entry),
982 plt_rel_offset += rel_size,
983 got_offset += 4)
985 // Set and adjust the PLT entry itself.
986 int32_t offset = ((got_address + got_offset)
987 - (plt_address + plt_offset + 8));
989 gold_assert(offset >= 0 && offset < 0x0fffffff);
990 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
991 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
992 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
993 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
994 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
995 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
997 // Set the entry in the GOT.
998 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
1001 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1002 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1004 of->write_output_view(offset, oview_size, oview);
1005 of->write_output_view(got_file_offset, got_size, got_view);
1008 // Create a PLT entry for a global symbol.
1010 template<bool big_endian>
1011 void
1012 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1013 Symbol* gsym)
1015 if (gsym->has_plt_offset())
1016 return;
1018 if (this->plt_ == NULL)
1020 // Create the GOT sections first.
1021 this->got_section(symtab, layout);
1023 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
1024 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1025 (elfcpp::SHF_ALLOC
1026 | elfcpp::SHF_EXECINSTR),
1027 this->plt_);
1029 this->plt_->add_entry(gsym);
1032 // Report an unsupported relocation against a local symbol.
1034 template<bool big_endian>
1035 void
1036 Target_arm<big_endian>::Scan::unsupported_reloc_local(
1037 Sized_relobj<32, big_endian>* object,
1038 unsigned int r_type)
1040 gold_error(_("%s: unsupported reloc %u against local symbol"),
1041 object->name().c_str(), r_type);
1044 // We are about to emit a dynamic relocation of type R_TYPE. If the
1045 // dynamic linker does not support it, issue an error. The GNU linker
1046 // only issues a non-PIC error for an allocated read-only section.
1047 // Here we know the section is allocated, but we don't know that it is
1048 // read-only. But we check for all the relocation types which the
1049 // glibc dynamic linker supports, so it seems appropriate to issue an
1050 // error even if the section is not read-only.
1052 template<bool big_endian>
1053 void
1054 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
1055 unsigned int r_type)
1057 switch (r_type)
1059 // These are the relocation types supported by glibc for ARM.
1060 case elfcpp::R_ARM_RELATIVE:
1061 case elfcpp::R_ARM_COPY:
1062 case elfcpp::R_ARM_GLOB_DAT:
1063 case elfcpp::R_ARM_JUMP_SLOT:
1064 case elfcpp::R_ARM_ABS32:
1065 case elfcpp::R_ARM_PC24:
1066 // FIXME: The following 3 types are not supported by Android's dynamic
1067 // linker.
1068 case elfcpp::R_ARM_TLS_DTPMOD32:
1069 case elfcpp::R_ARM_TLS_DTPOFF32:
1070 case elfcpp::R_ARM_TLS_TPOFF32:
1071 return;
1073 default:
1074 // This prevents us from issuing more than one error per reloc
1075 // section. But we can still wind up issuing more than one
1076 // error per object file.
1077 if (this->issued_non_pic_error_)
1078 return;
1079 object->error(_("requires unsupported dynamic reloc; "
1080 "recompile with -fPIC"));
1081 this->issued_non_pic_error_ = true;
1082 return;
1084 case elfcpp::R_ARM_NONE:
1085 gold_unreachable();
1089 // Scan a relocation for a local symbol.
1090 // FIXME: This only handles a subset of relocation types used by Android
1091 // on ARM v5te devices.
1093 template<bool big_endian>
1094 inline void
1095 Target_arm<big_endian>::Scan::local(const General_options&,
1096 Symbol_table* symtab,
1097 Layout* layout,
1098 Target_arm* target,
1099 Sized_relobj<32, big_endian>* object,
1100 unsigned int data_shndx,
1101 Output_section* output_section,
1102 const elfcpp::Rel<32, big_endian>& reloc,
1103 unsigned int r_type,
1104 const elfcpp::Sym<32, big_endian>&)
1106 r_type = get_real_reloc_type(r_type);
1107 switch (r_type)
1109 case elfcpp::R_ARM_NONE:
1110 break;
1112 case elfcpp::R_ARM_ABS8:
1113 if (parameters->options().output_is_position_independent())
1115 // FIXME: Create a dynamic relocation for this location.
1116 gold_error(_("%s: gold bug: need dynamic ABS8 reloc"),
1117 object->name().c_str());
1119 break;
1121 case elfcpp::R_ARM_ABS32:
1122 // If building a shared library (or a position-independent
1123 // executable), we need to create a dynamic relocation for
1124 // this location. The relocation applied at link time will
1125 // apply the link-time value, so we flag the location with
1126 // an R_ARM_RELATIVE relocation so the dynamic loader can
1127 // relocate it easily.
1128 if (parameters->options().output_is_position_independent())
1130 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1131 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1132 // If we are to add more other reloc types than R_ARM_ABS32,
1133 // we need to add check_non_pic(object, r_type) here.
1134 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
1135 output_section, data_shndx,
1136 reloc.get_r_offset());
1138 break;
1140 case elfcpp::R_ARM_REL32:
1141 case elfcpp::R_ARM_THM_CALL:
1142 case elfcpp::R_ARM_CALL:
1143 case elfcpp::R_ARM_PREL31:
1144 case elfcpp::R_ARM_JUMP24:
1145 case elfcpp::R_ARM_PLT32:
1146 break;
1148 case elfcpp::R_ARM_GOTOFF32:
1149 // We need a GOT section:
1150 target->got_section(symtab, layout);
1151 break;
1153 case elfcpp::R_ARM_BASE_PREL:
1154 // FIXME: What about this?
1155 break;
1157 case elfcpp::R_ARM_GOT_BREL:
1159 // The symbol requires a GOT entry.
1160 Output_data_got<32, big_endian>* got =
1161 target->got_section(symtab, layout);
1162 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1163 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1165 // If we are generating a shared object, we need to add a
1166 // dynamic RELATIVE relocation for this symbol's GOT entry.
1167 if (parameters->options().output_is_position_independent())
1169 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1170 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1171 rel_dyn->add_local_relative(
1172 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
1173 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1177 break;
1179 case elfcpp::R_ARM_TARGET1:
1180 // This should have been mapped to another type already.
1181 // Fall through.
1182 case elfcpp::R_ARM_COPY:
1183 case elfcpp::R_ARM_GLOB_DAT:
1184 case elfcpp::R_ARM_JUMP_SLOT:
1185 case elfcpp::R_ARM_RELATIVE:
1186 // These are relocations which should only be seen by the
1187 // dynamic linker, and should never be seen here.
1188 gold_error(_("%s: unexpected reloc %u in object file"),
1189 object->name().c_str(), r_type);
1190 break;
1192 default:
1193 unsupported_reloc_local(object, r_type);
1194 break;
1198 // Report an unsupported relocation against a global symbol.
1200 template<bool big_endian>
1201 void
1202 Target_arm<big_endian>::Scan::unsupported_reloc_global(
1203 Sized_relobj<32, big_endian>* object,
1204 unsigned int r_type,
1205 Symbol* gsym)
1207 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1208 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1211 // Scan a relocation for a global symbol.
1212 // FIXME: This only handles a subset of relocation types used by Android
1213 // on ARM v5te devices.
1215 template<bool big_endian>
1216 inline void
1217 Target_arm<big_endian>::Scan::global(const General_options&,
1218 Symbol_table* symtab,
1219 Layout* layout,
1220 Target_arm* target,
1221 Sized_relobj<32, big_endian>* object,
1222 unsigned int data_shndx,
1223 Output_section* output_section,
1224 const elfcpp::Rel<32, big_endian>& reloc,
1225 unsigned int r_type,
1226 Symbol* gsym)
1228 r_type = get_real_reloc_type(r_type);
1229 switch (r_type)
1231 case elfcpp::R_ARM_NONE:
1232 break;
1234 case elfcpp::R_ARM_ABS8:
1235 // Make a dynamic relocation if necessary.
1236 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1238 // FIXME: Create a dynamic relocation for this location.
1239 gold_error(_("%s: gold bug: need dynamic ABS8 reloc for %s"),
1240 object->name().c_str(), gsym->demangled_name().c_str());
1242 break;
1244 case elfcpp::R_ARM_ABS32:
1246 // Make a dynamic relocation if necessary.
1247 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1249 if (target->may_need_copy_reloc(gsym))
1251 target->copy_reloc(symtab, layout, object,
1252 data_shndx, output_section, gsym, reloc);
1254 else if (gsym->can_use_relative_reloc(false))
1256 // If we are to add more other reloc types than R_ARM_ABS32,
1257 // we need to add check_non_pic(object, r_type) here.
1258 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1259 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
1260 output_section, object,
1261 data_shndx, reloc.get_r_offset());
1263 else
1265 // If we are to add more other reloc types than R_ARM_ABS32,
1266 // we need to add check_non_pic(object, r_type) here.
1267 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1268 rel_dyn->add_global(gsym, r_type, output_section, object,
1269 data_shndx, reloc.get_r_offset());
1273 break;
1275 case elfcpp::R_ARM_REL32:
1276 case elfcpp::R_ARM_PREL31:
1278 // Make a dynamic relocation if necessary.
1279 int flags = Symbol::NON_PIC_REF;
1280 if (gsym->needs_dynamic_reloc(flags))
1282 if (target->may_need_copy_reloc(gsym))
1284 target->copy_reloc(symtab, layout, object,
1285 data_shndx, output_section, gsym, reloc);
1287 else
1289 check_non_pic(object, r_type);
1290 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1291 rel_dyn->add_global(gsym, r_type, output_section, object,
1292 data_shndx, reloc.get_r_offset());
1296 break;
1298 case elfcpp::R_ARM_JUMP24:
1299 case elfcpp::R_ARM_THM_CALL:
1300 case elfcpp::R_ARM_CALL:
1302 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
1303 target->make_plt_entry(symtab, layout, gsym);
1304 // Make a dynamic relocation if necessary.
1305 int flags = Symbol::NON_PIC_REF;
1306 if (gsym->type() == elfcpp::STT_FUNC
1307 || gsym->type() == elfcpp::STT_ARM_TFUNC)
1308 flags |= Symbol::FUNCTION_CALL;
1309 if (gsym->needs_dynamic_reloc(flags))
1311 if (target->may_need_copy_reloc(gsym))
1313 target->copy_reloc(symtab, layout, object,
1314 data_shndx, output_section, gsym,
1315 reloc);
1317 else
1319 check_non_pic(object, r_type);
1320 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1321 rel_dyn->add_global(gsym, r_type, output_section, object,
1322 data_shndx, reloc.get_r_offset());
1326 break;
1328 case elfcpp::R_ARM_PLT32:
1329 // If the symbol is fully resolved, this is just a relative
1330 // local reloc. Otherwise we need a PLT entry.
1331 if (gsym->final_value_is_known())
1332 break;
1333 // If building a shared library, we can also skip the PLT entry
1334 // if the symbol is defined in the output file and is protected
1335 // or hidden.
1336 if (gsym->is_defined()
1337 && !gsym->is_from_dynobj()
1338 && !gsym->is_preemptible())
1339 break;
1340 target->make_plt_entry(symtab, layout, gsym);
1341 break;
1343 case elfcpp::R_ARM_GOTOFF32:
1344 // We need a GOT section.
1345 target->got_section(symtab, layout);
1346 break;
1348 case elfcpp::R_ARM_BASE_PREL:
1349 // FIXME: What about this?
1350 break;
1352 case elfcpp::R_ARM_GOT_BREL:
1354 // The symbol requires a GOT entry.
1355 Output_data_got<32, big_endian>* got =
1356 target->got_section(symtab, layout);
1357 if (gsym->final_value_is_known())
1358 got->add_global(gsym, GOT_TYPE_STANDARD);
1359 else
1361 // If this symbol is not fully resolved, we need to add a
1362 // GOT entry with a dynamic relocation.
1363 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1364 if (gsym->is_from_dynobj()
1365 || gsym->is_undefined()
1366 || gsym->is_preemptible())
1367 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1368 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
1369 else
1371 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1372 rel_dyn->add_global_relative(
1373 gsym, elfcpp::R_ARM_RELATIVE, got,
1374 gsym->got_offset(GOT_TYPE_STANDARD));
1378 break;
1380 case elfcpp::R_ARM_TARGET1:
1381 // This should have been mapped to another type already.
1382 // Fall through.
1383 case elfcpp::R_ARM_COPY:
1384 case elfcpp::R_ARM_GLOB_DAT:
1385 case elfcpp::R_ARM_JUMP_SLOT:
1386 case elfcpp::R_ARM_RELATIVE:
1387 // These are relocations which should only be seen by the
1388 // dynamic linker, and should never be seen here.
1389 gold_error(_("%s: unexpected reloc %u in object file"),
1390 object->name().c_str(), r_type);
1391 break;
1393 default:
1394 unsupported_reloc_global(object, r_type, gsym);
1395 break;
1399 // Process relocations for gc.
1401 template<bool big_endian>
1402 void
1403 Target_arm<big_endian>::gc_process_relocs(const General_options& options,
1404 Symbol_table* symtab,
1405 Layout* layout,
1406 Sized_relobj<32, big_endian>* object,
1407 unsigned int data_shndx,
1408 unsigned int,
1409 const unsigned char* prelocs,
1410 size_t reloc_count,
1411 Output_section* output_section,
1412 bool needs_special_offset_handling,
1413 size_t local_symbol_count,
1414 const unsigned char* plocal_symbols)
1416 typedef Target_arm<big_endian> Arm;
1417 typedef typename Target_arm<big_endian>::Scan Scan;
1419 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
1420 options,
1421 symtab,
1422 layout,
1423 this,
1424 object,
1425 data_shndx,
1426 prelocs,
1427 reloc_count,
1428 output_section,
1429 needs_special_offset_handling,
1430 local_symbol_count,
1431 plocal_symbols);
1434 // Scan relocations for a section.
1436 template<bool big_endian>
1437 void
1438 Target_arm<big_endian>::scan_relocs(const General_options& options,
1439 Symbol_table* symtab,
1440 Layout* layout,
1441 Sized_relobj<32, big_endian>* object,
1442 unsigned int data_shndx,
1443 unsigned int sh_type,
1444 const unsigned char* prelocs,
1445 size_t reloc_count,
1446 Output_section* output_section,
1447 bool needs_special_offset_handling,
1448 size_t local_symbol_count,
1449 const unsigned char* plocal_symbols)
1451 typedef typename Target_arm<big_endian>::Scan Scan;
1452 if (sh_type == elfcpp::SHT_RELA)
1454 gold_error(_("%s: unsupported RELA reloc section"),
1455 object->name().c_str());
1456 return;
1459 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
1460 options,
1461 symtab,
1462 layout,
1463 this,
1464 object,
1465 data_shndx,
1466 prelocs,
1467 reloc_count,
1468 output_section,
1469 needs_special_offset_handling,
1470 local_symbol_count,
1471 plocal_symbols);
1474 // Finalize the sections.
1476 template<bool big_endian>
1477 void
1478 Target_arm<big_endian>::do_finalize_sections(Layout* layout)
1480 // Fill in some more dynamic tags.
1481 Output_data_dynamic* const odyn = layout->dynamic_data();
1482 if (odyn != NULL)
1484 if (this->got_plt_ != NULL)
1485 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1487 if (this->plt_ != NULL)
1489 const Output_data* od = this->plt_->rel_plt();
1490 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1491 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1492 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1495 if (this->rel_dyn_ != NULL)
1497 const Output_data* od = this->rel_dyn_;
1498 odyn->add_section_address(elfcpp::DT_REL, od);
1499 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1500 odyn->add_constant(elfcpp::DT_RELENT,
1501 elfcpp::Elf_sizes<32>::rel_size);
1504 if (!parameters->options().shared())
1506 // The value of the DT_DEBUG tag is filled in by the dynamic
1507 // linker at run time, and used by the debugger.
1508 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1512 // Emit any relocs we saved in an attempt to avoid generating COPY
1513 // relocs.
1514 if (this->copy_relocs_.any_saved_relocs())
1515 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1517 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
1518 // the .ARM.exidx section.
1519 if (!layout->script_options()->saw_phdrs_clause()
1520 && !parameters->options().relocatable())
1522 Output_section* exidx_section =
1523 layout->find_output_section(".ARM.exidx");
1525 if (exidx_section != NULL
1526 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
1528 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
1529 == NULL);
1530 Output_segment* exidx_segment =
1531 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
1532 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
1537 // Return whether a direct absolute static relocation needs to be applied.
1538 // In cases where Scan::local() or Scan::global() has created
1539 // a dynamic relocation other than R_ARM_RELATIVE, the addend
1540 // of the relocation is carried in the data, and we must not
1541 // apply the static relocation.
1543 template<bool big_endian>
1544 inline bool
1545 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
1546 const Sized_symbol<32>* gsym,
1547 int ref_flags,
1548 bool is_32bit,
1549 Output_section* output_section)
1551 // If the output section is not allocated, then we didn't call
1552 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1553 // the reloc here.
1554 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1555 return true;
1557 // For local symbols, we will have created a non-RELATIVE dynamic
1558 // relocation only if (a) the output is position independent,
1559 // (b) the relocation is absolute (not pc- or segment-relative), and
1560 // (c) the relocation is not 32 bits wide.
1561 if (gsym == NULL)
1562 return !(parameters->options().output_is_position_independent()
1563 && (ref_flags & Symbol::ABSOLUTE_REF)
1564 && !is_32bit);
1566 // For global symbols, we use the same helper routines used in the
1567 // scan pass. If we did not create a dynamic relocation, or if we
1568 // created a RELATIVE dynamic relocation, we should apply the static
1569 // relocation.
1570 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1571 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1572 && gsym->can_use_relative_reloc(ref_flags
1573 & Symbol::FUNCTION_CALL);
1574 return !has_dyn || is_rel;
1577 // Perform a relocation.
1579 template<bool big_endian>
1580 inline bool
1581 Target_arm<big_endian>::Relocate::relocate(
1582 const Relocate_info<32, big_endian>* relinfo,
1583 Target_arm* target,
1584 Output_section *output_section,
1585 size_t relnum,
1586 const elfcpp::Rel<32, big_endian>& rel,
1587 unsigned int r_type,
1588 const Sized_symbol<32>* gsym,
1589 const Symbol_value<32>* psymval,
1590 unsigned char* view,
1591 elfcpp::Elf_types<32>::Elf_Addr address,
1592 section_size_type /* view_size */ )
1594 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
1596 r_type = get_real_reloc_type(r_type);
1598 // If this the symbol may be a Thumb function, set thumb bit to 1.
1599 bool has_thumb_bit = ((gsym != NULL)
1600 && (gsym->type() == elfcpp::STT_FUNC
1601 || gsym->type() == elfcpp::STT_ARM_TFUNC));
1603 // Pick the value to use for symbols defined in shared objects.
1604 Symbol_value<32> symval;
1605 if (gsym != NULL
1606 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
1608 symval.set_output_value(target->plt_section()->address()
1609 + gsym->plt_offset());
1610 psymval = &symval;
1611 has_thumb_bit = 0;
1614 const Sized_relobj<32, big_endian>* object = relinfo->object;
1616 // Get the GOT offset if needed.
1617 // The GOT pointer points to the end of the GOT section.
1618 // We need to subtract the size of the GOT section to get
1619 // the actual offset to use in the relocation.
1620 bool have_got_offset = false;
1621 unsigned int got_offset = 0;
1622 switch (r_type)
1624 case elfcpp::R_ARM_GOT_BREL:
1625 if (gsym != NULL)
1627 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1628 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1629 - target->got_size());
1631 else
1633 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1634 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1635 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1636 - target->got_size());
1638 have_got_offset = true;
1639 break;
1641 default:
1642 break;
1645 typename Arm_relocate_functions::Status reloc_status =
1646 Arm_relocate_functions::STATUS_OKAY;
1647 switch (r_type)
1649 case elfcpp::R_ARM_NONE:
1650 break;
1652 case elfcpp::R_ARM_ABS8:
1653 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1654 output_section))
1655 reloc_status = Arm_relocate_functions::abs8(view, object, psymval,
1656 has_thumb_bit);
1657 break;
1659 case elfcpp::R_ARM_ABS32:
1660 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1661 output_section))
1662 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
1663 has_thumb_bit);
1664 break;
1666 case elfcpp::R_ARM_REL32:
1667 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1668 address, has_thumb_bit);
1669 break;
1671 case elfcpp::R_ARM_THM_CALL:
1672 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
1673 address, has_thumb_bit);
1674 break;
1676 case elfcpp::R_ARM_GOTOFF32:
1678 elfcpp::Elf_types<32>::Elf_Addr got_origin;
1679 got_origin = target->got_plt_section()->address();
1680 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1681 got_origin, has_thumb_bit);
1683 break;
1685 case elfcpp::R_ARM_BASE_PREL:
1687 uint32_t origin;
1688 // Get the addressing origin of the output segment defining the
1689 // symbol gsym (AAELF 4.6.1.2 Relocation types)
1690 gold_assert(gsym != NULL);
1691 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
1692 origin = gsym->output_segment()->vaddr();
1693 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
1694 origin = gsym->output_data()->address();
1695 else
1697 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1698 _("cannot find origin of R_ARM_BASE_PREL"));
1699 return true;
1701 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
1703 break;
1705 case elfcpp::R_ARM_GOT_BREL:
1706 gold_assert(have_got_offset);
1707 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
1708 break;
1710 case elfcpp::R_ARM_PLT32:
1711 gold_assert(gsym == NULL
1712 || gsym->has_plt_offset()
1713 || gsym->final_value_is_known()
1714 || (gsym->is_defined()
1715 && !gsym->is_from_dynobj()
1716 && !gsym->is_preemptible()));
1717 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
1718 address, has_thumb_bit);
1719 break;
1721 case elfcpp::R_ARM_CALL:
1722 reloc_status = Arm_relocate_functions::call(view, object, psymval,
1723 address, has_thumb_bit);
1724 break;
1726 case elfcpp::R_ARM_JUMP24:
1727 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
1728 address, has_thumb_bit);
1729 break;
1731 case elfcpp::R_ARM_PREL31:
1732 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
1733 address, has_thumb_bit);
1734 break;
1736 case elfcpp::R_ARM_TARGET1:
1737 // This should have been mapped to another type already.
1738 // Fall through.
1739 case elfcpp::R_ARM_COPY:
1740 case elfcpp::R_ARM_GLOB_DAT:
1741 case elfcpp::R_ARM_JUMP_SLOT:
1742 case elfcpp::R_ARM_RELATIVE:
1743 // These are relocations which should only be seen by the
1744 // dynamic linker, and should never be seen here.
1745 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1746 _("unexpected reloc %u in object file"),
1747 r_type);
1748 break;
1750 default:
1751 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1752 _("unsupported reloc %u"),
1753 r_type);
1754 break;
1757 // Report any errors.
1758 switch (reloc_status)
1760 case Arm_relocate_functions::STATUS_OKAY:
1761 break;
1762 case Arm_relocate_functions::STATUS_OVERFLOW:
1763 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1764 _("relocation overflow in relocation %u"),
1765 r_type);
1766 break;
1767 case Arm_relocate_functions::STATUS_BAD_RELOC:
1768 gold_error_at_location(
1769 relinfo,
1770 relnum,
1771 rel.get_r_offset(),
1772 _("unexpected opcode while processing relocation %u"),
1773 r_type);
1774 break;
1775 default:
1776 gold_unreachable();
1779 return true;
1782 // Relocate section data.
1784 template<bool big_endian>
1785 void
1786 Target_arm<big_endian>::relocate_section(
1787 const Relocate_info<32, big_endian>* relinfo,
1788 unsigned int sh_type,
1789 const unsigned char* prelocs,
1790 size_t reloc_count,
1791 Output_section* output_section,
1792 bool needs_special_offset_handling,
1793 unsigned char* view,
1794 elfcpp::Elf_types<32>::Elf_Addr address,
1795 section_size_type view_size)
1797 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
1798 gold_assert(sh_type == elfcpp::SHT_REL);
1800 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
1801 Arm_relocate>(
1802 relinfo,
1803 this,
1804 prelocs,
1805 reloc_count,
1806 output_section,
1807 needs_special_offset_handling,
1808 view,
1809 address,
1810 view_size);
1813 // Return the size of a relocation while scanning during a relocatable
1814 // link.
1816 template<bool big_endian>
1817 unsigned int
1818 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1819 unsigned int r_type,
1820 Relobj* object)
1822 r_type = get_real_reloc_type(r_type);
1823 switch (r_type)
1825 case elfcpp::R_ARM_NONE:
1826 return 0;
1828 case elfcpp::R_ARM_ABS8:
1829 return 1;
1831 case elfcpp::R_ARM_ABS32:
1832 case elfcpp::R_ARM_REL32:
1833 case elfcpp::R_ARM_THM_CALL:
1834 case elfcpp::R_ARM_GOTOFF32:
1835 case elfcpp::R_ARM_BASE_PREL:
1836 case elfcpp::R_ARM_GOT_BREL:
1837 case elfcpp::R_ARM_PLT32:
1838 case elfcpp::R_ARM_CALL:
1839 case elfcpp::R_ARM_JUMP24:
1840 case elfcpp::R_ARM_PREL31:
1841 return 4;
1843 case elfcpp::R_ARM_TARGET1:
1844 // This should have been mapped to another type already.
1845 // Fall through.
1846 case elfcpp::R_ARM_COPY:
1847 case elfcpp::R_ARM_GLOB_DAT:
1848 case elfcpp::R_ARM_JUMP_SLOT:
1849 case elfcpp::R_ARM_RELATIVE:
1850 // These are relocations which should only be seen by the
1851 // dynamic linker, and should never be seen here.
1852 gold_error(_("%s: unexpected reloc %u in object file"),
1853 object->name().c_str(), r_type);
1854 return 0;
1856 default:
1857 object->error(_("unsupported reloc %u in object file"), r_type);
1858 return 0;
1862 // Scan the relocs during a relocatable link.
1864 template<bool big_endian>
1865 void
1866 Target_arm<big_endian>::scan_relocatable_relocs(
1867 const General_options& options,
1868 Symbol_table* symtab,
1869 Layout* layout,
1870 Sized_relobj<32, big_endian>* object,
1871 unsigned int data_shndx,
1872 unsigned int sh_type,
1873 const unsigned char* prelocs,
1874 size_t reloc_count,
1875 Output_section* output_section,
1876 bool needs_special_offset_handling,
1877 size_t local_symbol_count,
1878 const unsigned char* plocal_symbols,
1879 Relocatable_relocs* rr)
1881 gold_assert(sh_type == elfcpp::SHT_REL);
1883 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
1884 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1886 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
1887 Scan_relocatable_relocs>(
1888 options,
1889 symtab,
1890 layout,
1891 object,
1892 data_shndx,
1893 prelocs,
1894 reloc_count,
1895 output_section,
1896 needs_special_offset_handling,
1897 local_symbol_count,
1898 plocal_symbols,
1899 rr);
1902 // Relocate a section during a relocatable link.
1904 template<bool big_endian>
1905 void
1906 Target_arm<big_endian>::relocate_for_relocatable(
1907 const Relocate_info<32, big_endian>* relinfo,
1908 unsigned int sh_type,
1909 const unsigned char* prelocs,
1910 size_t reloc_count,
1911 Output_section* output_section,
1912 off_t offset_in_output_section,
1913 const Relocatable_relocs* rr,
1914 unsigned char* view,
1915 elfcpp::Elf_types<32>::Elf_Addr view_address,
1916 section_size_type view_size,
1917 unsigned char* reloc_view,
1918 section_size_type reloc_view_size)
1920 gold_assert(sh_type == elfcpp::SHT_REL);
1922 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
1923 relinfo,
1924 prelocs,
1925 reloc_count,
1926 output_section,
1927 offset_in_output_section,
1929 view,
1930 view_address,
1931 view_size,
1932 reloc_view,
1933 reloc_view_size);
1936 // Return the value to use for a dynamic symbol which requires special
1937 // treatment. This is how we support equality comparisons of function
1938 // pointers across shared library boundaries, as described in the
1939 // processor specific ABI supplement.
1941 template<bool big_endian>
1942 uint64_t
1943 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
1945 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1946 return this->plt_section()->address() + gsym->plt_offset();
1949 // Map platform-specific relocs to real relocs
1951 template<bool big_endian>
1952 unsigned int
1953 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
1955 switch (r_type)
1957 case elfcpp::R_ARM_TARGET1:
1958 // This is either R_ARM_ABS32 or R_ARM_REL32;
1959 return elfcpp::R_ARM_ABS32;
1961 case elfcpp::R_ARM_TARGET2:
1962 // This can be any reloc type but ususally is R_ARM_GOT_PREL
1963 return elfcpp::R_ARM_GOT_PREL;
1965 default:
1966 return r_type;
1970 // The selector for arm object files.
1972 template<bool big_endian>
1973 class Target_selector_arm : public Target_selector
1975 public:
1976 Target_selector_arm()
1977 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
1978 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
1981 Target*
1982 do_instantiate_target()
1983 { return new Target_arm<big_endian>(); }
1986 Target_selector_arm<false> target_selector_arm;
1987 Target_selector_arm<true> target_selector_armbe;
1989 } // End anonymous namespace.