* elfxx-mips.c (LOAD_INTERLOCKS_P): New define.
[binutils.git] / gold / arm.cc
blob491f254d1042a0d26cf6ebe3539e360c75e541e9
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 // Coming soon (pending patches):
78 // - Defining section symbols __exidx_start and __exidx_stop.
79 // - Support interworking.
80 // - Mergeing all .ARM.xxx.yyy sections into .ARM.xxx. Currently, they
81 // are incorrectly merged into an .ARM section.
83 // TODOs:
84 // - Create a PT_ARM_EXIDX program header for a shared object that
85 // might throw an exception.
86 // - Support more relocation types as needed.
87 // - Make PLTs more flexible for different architecture features like
88 // Thumb-2 and BE8.
90 // Utilities for manipulating integers of up to 32-bits
92 namespace utils
94 // Sign extend an n-bit unsigned integer stored in an uint32_t into
95 // an int32_t. NO_BITS must be between 1 to 32.
96 template<int no_bits>
97 static inline int32_t
98 sign_extend(uint32_t bits)
100 gold_assert(no_bits >= 0 && no_bits <= 32);
101 if (no_bits == 32)
102 return static_cast<int32_t>(bits);
103 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
104 bits &= mask;
105 uint32_t top_bit = 1U << (no_bits - 1);
106 int32_t as_signed = static_cast<int32_t>(bits);
107 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
110 // Detects overflow of an NO_BITS integer stored in a uint32_t.
111 template<int no_bits>
112 static inline bool
113 has_overflow(uint32_t bits)
115 gold_assert(no_bits >= 0 && no_bits <= 32);
116 if (no_bits == 32)
117 return false;
118 int32_t max = (1 << (no_bits - 1)) - 1;
119 int32_t min = -(1 << (no_bits - 1));
120 int32_t as_signed = static_cast<int32_t>(bits);
121 return as_signed > max || as_signed < min;
124 // Select bits from A and B using bits in MASK. For each n in [0..31],
125 // the n-th bit in the result is chosen from the n-th bits of A and B.
126 // A zero selects A and a one selects B.
127 static inline uint32_t
128 bit_select(uint32_t a, uint32_t b, uint32_t mask)
129 { return (a & ~mask) | (b & mask); }
132 template<bool big_endian>
133 class Target_arm : public Sized_target<32, big_endian>
135 public:
136 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
137 Reloc_section;
139 Target_arm()
140 : Sized_target<32, big_endian>(&arm_info),
141 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
142 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL)
145 // Process the relocations to determine unreferenced sections for
146 // garbage collection.
147 void
148 gc_process_relocs(const General_options& options,
149 Symbol_table* symtab,
150 Layout* layout,
151 Sized_relobj<32, big_endian>* object,
152 unsigned int data_shndx,
153 unsigned int sh_type,
154 const unsigned char* prelocs,
155 size_t reloc_count,
156 Output_section* output_section,
157 bool needs_special_offset_handling,
158 size_t local_symbol_count,
159 const unsigned char* plocal_symbols);
161 // Scan the relocations to look for symbol adjustments.
162 void
163 scan_relocs(const General_options& options,
164 Symbol_table* symtab,
165 Layout* layout,
166 Sized_relobj<32, big_endian>* object,
167 unsigned int data_shndx,
168 unsigned int sh_type,
169 const unsigned char* prelocs,
170 size_t reloc_count,
171 Output_section* output_section,
172 bool needs_special_offset_handling,
173 size_t local_symbol_count,
174 const unsigned char* plocal_symbols);
176 // Finalize the sections.
177 void
178 do_finalize_sections(Layout*);
180 // Return the value to use for a dynamic symbol which requires special
181 // treatment.
182 uint64_t
183 do_dynsym_value(const Symbol*) const;
185 // Relocate a section.
186 void
187 relocate_section(const Relocate_info<32, big_endian>*,
188 unsigned int sh_type,
189 const unsigned char* prelocs,
190 size_t reloc_count,
191 Output_section* output_section,
192 bool needs_special_offset_handling,
193 unsigned char* view,
194 elfcpp::Elf_types<32>::Elf_Addr view_address,
195 section_size_type view_size);
197 // Scan the relocs during a relocatable link.
198 void
199 scan_relocatable_relocs(const General_options& options,
200 Symbol_table* symtab,
201 Layout* layout,
202 Sized_relobj<32, big_endian>* object,
203 unsigned int data_shndx,
204 unsigned int sh_type,
205 const unsigned char* prelocs,
206 size_t reloc_count,
207 Output_section* output_section,
208 bool needs_special_offset_handling,
209 size_t local_symbol_count,
210 const unsigned char* plocal_symbols,
211 Relocatable_relocs*);
213 // Relocate a section during a relocatable link.
214 void
215 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
216 unsigned int sh_type,
217 const unsigned char* prelocs,
218 size_t reloc_count,
219 Output_section* output_section,
220 off_t offset_in_output_section,
221 const Relocatable_relocs*,
222 unsigned char* view,
223 elfcpp::Elf_types<32>::Elf_Addr view_address,
224 section_size_type view_size,
225 unsigned char* reloc_view,
226 section_size_type reloc_view_size);
228 // Return whether SYM is defined by the ABI.
229 bool
230 do_is_defined_by_abi(Symbol* sym) const
231 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
233 // Return the size of the GOT section.
234 section_size_type
235 got_size()
237 gold_assert(this->got_ != NULL);
238 return this->got_->data_size();
241 // Map platform-specific reloc types
242 static unsigned int
243 get_real_reloc_type (unsigned int r_type);
245 private:
246 // The class which scans relocations.
247 class Scan
249 public:
250 Scan()
251 : issued_non_pic_error_(false)
254 inline void
255 local(const General_options& options, Symbol_table* symtab,
256 Layout* layout, Target_arm* target,
257 Sized_relobj<32, big_endian>* object,
258 unsigned int data_shndx,
259 Output_section* output_section,
260 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
261 const elfcpp::Sym<32, big_endian>& lsym);
263 inline void
264 global(const General_options& options, Symbol_table* symtab,
265 Layout* layout, Target_arm* target,
266 Sized_relobj<32, big_endian>* object,
267 unsigned int data_shndx,
268 Output_section* output_section,
269 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
270 Symbol* gsym);
272 private:
273 static void
274 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
275 unsigned int r_type);
277 static void
278 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
279 unsigned int r_type, Symbol*);
281 void
282 check_non_pic(Relobj*, unsigned int r_type);
284 // Almost identical to Symbol::needs_plt_entry except that it also
285 // handles STT_ARM_TFUNC.
286 static bool
287 symbol_needs_plt_entry(const Symbol* sym)
289 // An undefined symbol from an executable does not need a PLT entry.
290 if (sym->is_undefined() && !parameters->options().shared())
291 return false;
293 return (!parameters->doing_static_link()
294 && (sym->type() == elfcpp::STT_FUNC
295 || sym->type() == elfcpp::STT_ARM_TFUNC)
296 && (sym->is_from_dynobj()
297 || sym->is_undefined()
298 || sym->is_preemptible()));
301 // Whether we have issued an error about a non-PIC compilation.
302 bool issued_non_pic_error_;
305 // The class which implements relocation.
306 class Relocate
308 public:
309 Relocate()
312 ~Relocate()
315 // Return whether the static relocation needs to be applied.
316 inline bool
317 should_apply_static_reloc(const Sized_symbol<32>* gsym,
318 int ref_flags,
319 bool is_32bit,
320 Output_section* output_section);
322 // Do a relocation. Return false if the caller should not issue
323 // any warnings about this relocation.
324 inline bool
325 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
326 Output_section*, size_t relnum,
327 const elfcpp::Rel<32, big_endian>&,
328 unsigned int r_type, const Sized_symbol<32>*,
329 const Symbol_value<32>*,
330 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
331 section_size_type);
333 // Return whether we want to pass flag NON_PIC_REF for this
334 // reloc.
335 static inline bool
336 reloc_is_non_pic (unsigned int r_type)
338 switch (r_type)
340 case elfcpp::R_ARM_REL32:
341 case elfcpp::R_ARM_THM_CALL:
342 case elfcpp::R_ARM_CALL:
343 case elfcpp::R_ARM_JUMP24:
344 case elfcpp::R_ARM_PREL31:
345 return true;
346 default:
347 return false;
352 // A class which returns the size required for a relocation type,
353 // used while scanning relocs during a relocatable link.
354 class Relocatable_size_for_reloc
356 public:
357 unsigned int
358 get_size_for_reloc(unsigned int, Relobj*);
361 // Get the GOT section, creating it if necessary.
362 Output_data_got<32, big_endian>*
363 got_section(Symbol_table*, Layout*);
365 // Get the GOT PLT section.
366 Output_data_space*
367 got_plt_section() const
369 gold_assert(this->got_plt_ != NULL);
370 return this->got_plt_;
373 // Create a PLT entry for a global symbol.
374 void
375 make_plt_entry(Symbol_table*, Layout*, Symbol*);
377 // Get the PLT section.
378 const Output_data_plt_arm<big_endian>*
379 plt_section() const
381 gold_assert(this->plt_ != NULL);
382 return this->plt_;
385 // Get the dynamic reloc section, creating it if necessary.
386 Reloc_section*
387 rel_dyn_section(Layout*);
389 // Return true if the symbol may need a COPY relocation.
390 // References from an executable object to non-function symbols
391 // defined in a dynamic object may need a COPY relocation.
392 bool
393 may_need_copy_reloc(Symbol* gsym)
395 return (!parameters->options().shared()
396 && gsym->is_from_dynobj()
397 && gsym->type() != elfcpp::STT_FUNC
398 && gsym->type() != elfcpp::STT_ARM_TFUNC);
401 // Add a potential copy relocation.
402 void
403 copy_reloc(Symbol_table* symtab, Layout* layout,
404 Sized_relobj<32, big_endian>* object,
405 unsigned int shndx, Output_section* output_section,
406 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
408 this->copy_relocs_.copy_reloc(symtab, layout,
409 symtab->get_sized_symbol<32>(sym),
410 object, shndx, output_section, reloc,
411 this->rel_dyn_section(layout));
414 // Information about this specific target which we pass to the
415 // general Target structure.
416 static const Target::Target_info arm_info;
418 // The types of GOT entries needed for this platform.
419 enum Got_type
421 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
424 // The GOT section.
425 Output_data_got<32, big_endian>* got_;
426 // The PLT section.
427 Output_data_plt_arm<big_endian>* plt_;
428 // The GOT PLT section.
429 Output_data_space* got_plt_;
430 // The dynamic reloc section.
431 Reloc_section* rel_dyn_;
432 // Relocs saved to avoid a COPY reloc.
433 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
434 // Space for variables copied with a COPY reloc.
435 Output_data_space* dynbss_;
438 template<bool big_endian>
439 const Target::Target_info Target_arm<big_endian>::arm_info =
441 32, // size
442 big_endian, // is_big_endian
443 elfcpp::EM_ARM, // machine_code
444 false, // has_make_symbol
445 false, // has_resolve
446 false, // has_code_fill
447 true, // is_default_stack_executable
448 '\0', // wrap_char
449 "/usr/lib/libc.so.1", // dynamic_linker
450 0x8000, // default_text_segment_address
451 0x1000, // abi_pagesize (overridable by -z max-page-size)
452 0x1000, // common_pagesize (overridable by -z common-page-size)
453 elfcpp::SHN_UNDEF, // small_common_shndx
454 elfcpp::SHN_UNDEF, // large_common_shndx
455 0, // small_common_section_flags
456 0 // large_common_section_flags
459 // Arm relocate functions class
462 template<bool big_endian>
463 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
465 public:
466 typedef enum
468 STATUS_OKAY, // No error during relocation.
469 STATUS_OVERFLOW, // Relocation oveflow.
470 STATUS_BAD_RELOC // Relocation cannot be applied.
471 } Status;
473 private:
474 typedef Relocate_functions<32, big_endian> Base;
475 typedef Arm_relocate_functions<big_endian> This;
477 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
478 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
479 // to distinguish ARM and THUMB functions and it is treated specially.
480 static inline Symbol_value<32>::Value
481 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
482 const Symbol_value<32>* psymval,
483 Symbol_value<32>::Value addend,
484 bool has_thumb_bit)
486 typedef Symbol_value<32>::Value Valtype;
488 if (has_thumb_bit)
490 Valtype raw = psymval->value(object, 0);
491 Valtype thumb_bit = raw & 1;
492 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
494 else
495 return psymval->value(object, addend);
498 // FIXME: This probably only works for Android on ARM v5te. We should
499 // following GNU ld for the general case.
500 template<unsigned r_type>
501 static inline typename This::Status
502 arm_branch_common(unsigned char *view,
503 const Sized_relobj<32, big_endian>* object,
504 const Symbol_value<32>* psymval,
505 elfcpp::Elf_types<32>::Elf_Addr address,
506 bool has_thumb_bit)
508 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
509 Valtype* wv = reinterpret_cast<Valtype*>(view);
510 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
512 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
513 && ((val & 0x0f000000UL) == 0x0a000000UL);
514 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
515 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
516 && ((val & 0x0f000000UL) == 0x0b000000UL);
517 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
518 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
520 if (r_type == elfcpp::R_ARM_CALL)
522 if (!insn_is_uncond_bl && !insn_is_blx)
523 return This::STATUS_BAD_RELOC;
525 else if (r_type == elfcpp::R_ARM_JUMP24)
527 if (!insn_is_b && !insn_is_cond_bl)
528 return This::STATUS_BAD_RELOC;
530 else if (r_type == elfcpp::R_ARM_PLT32)
532 if (!insn_is_any_branch)
533 return This::STATUS_BAD_RELOC;
535 else
536 gold_unreachable();
538 Valtype addend = utils::sign_extend<26>(val << 2);
539 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
540 - address);
542 // If target has thumb bit set, we need to either turn the BL
543 // into a BLX (for ARMv5 or above) or generate a stub.
544 if (x & 1)
546 // Turn BL to BLX.
547 if (insn_is_uncond_bl)
548 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
549 else
550 return This::STATUS_BAD_RELOC;
552 else
553 gold_assert(!insn_is_blx);
555 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
556 elfcpp::Swap<32, big_endian>::writeval(wv, val);
557 return (utils::has_overflow<26>(x)
558 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
561 public:
562 // R_ARM_ABS32: (S + A) | T
563 static inline typename This::Status
564 abs32(unsigned char *view,
565 const Sized_relobj<32, big_endian>* object,
566 const Symbol_value<32>* psymval,
567 bool has_thumb_bit)
569 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
570 Valtype* wv = reinterpret_cast<Valtype*>(view);
571 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
572 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
573 elfcpp::Swap<32, big_endian>::writeval(wv, x);
574 return This::STATUS_OKAY;
577 // R_ARM_REL32: (S + A) | T - P
578 static inline typename This::Status
579 rel32(unsigned char *view,
580 const Sized_relobj<32, big_endian>* object,
581 const Symbol_value<32>* psymval,
582 elfcpp::Elf_types<32>::Elf_Addr address,
583 bool has_thumb_bit)
585 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
586 Valtype* wv = reinterpret_cast<Valtype*>(view);
587 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
588 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
589 - address);
590 elfcpp::Swap<32, big_endian>::writeval(wv, x);
591 return This::STATUS_OKAY;
594 // R_ARM_THM_CALL: (S + A) | T - P
595 static inline typename This::Status
596 thm_call(unsigned char *view,
597 const Sized_relobj<32, big_endian>* object,
598 const Symbol_value<32>* psymval,
599 elfcpp::Elf_types<32>::Elf_Addr address,
600 bool has_thumb_bit)
602 // A thumb call consists of two instructions.
603 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
604 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
605 Valtype* wv = reinterpret_cast<Valtype*>(view);
606 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
607 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
608 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
609 gold_assert((lo & 0xf800) == 0xf800);
610 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
611 | ((lo & 0x7ff) << 1));
612 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
613 - address);
615 // If target has no thumb bit set, we need to either turn the BL
616 // into a BLX (for ARMv5 or above) or generate a stub.
617 if ((x & 1) == 0)
619 // This only works for ARMv5 and above with interworking enabled.
620 lo &= 0xefff;
622 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
623 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
624 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
625 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
626 return (utils::has_overflow<23>(x)
627 ? This::STATUS_OVERFLOW
628 : This::STATUS_OKAY);
631 // R_ARM_BASE_PREL: B(S) + A - P
632 static inline typename This::Status
633 base_prel(unsigned char* view,
634 elfcpp::Elf_types<32>::Elf_Addr origin,
635 elfcpp::Elf_types<32>::Elf_Addr address)
637 Base::rel32(view, origin - address);
638 return STATUS_OKAY;
641 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
642 static inline typename This::Status
643 got_brel(unsigned char* view,
644 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
646 Base::rel32(view, got_offset);
647 return This::STATUS_OKAY;
650 // R_ARM_PLT32: (S + A) | T - P
651 static inline typename This::Status
652 plt32(unsigned char *view,
653 const Sized_relobj<32, big_endian>* object,
654 const Symbol_value<32>* psymval,
655 elfcpp::Elf_types<32>::Elf_Addr address,
656 bool has_thumb_bit)
658 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
659 address, has_thumb_bit);
662 // R_ARM_CALL: (S + A) | T - P
663 static inline typename This::Status
664 call(unsigned char *view,
665 const Sized_relobj<32, big_endian>* object,
666 const Symbol_value<32>* psymval,
667 elfcpp::Elf_types<32>::Elf_Addr address,
668 bool has_thumb_bit)
670 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
671 address, has_thumb_bit);
674 // R_ARM_JUMP24: (S + A) | T - P
675 static inline typename This::Status
676 jump24(unsigned char *view,
677 const Sized_relobj<32, big_endian>* object,
678 const Symbol_value<32>* psymval,
679 elfcpp::Elf_types<32>::Elf_Addr address,
680 bool has_thumb_bit)
682 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
683 address, has_thumb_bit);
686 // R_ARM_PREL: (S + A) | T - P
687 static inline typename This::Status
688 prel31(unsigned char *view,
689 const Sized_relobj<32, big_endian>* object,
690 const Symbol_value<32>* psymval,
691 elfcpp::Elf_types<32>::Elf_Addr address,
692 bool has_thumb_bit)
694 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
695 Valtype* wv = reinterpret_cast<Valtype*>(view);
696 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
697 Valtype addend = utils::sign_extend<31>(val);
698 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
699 - address);
700 val = utils::bit_select(val, x, 0x7fffffffU);
701 elfcpp::Swap<32, big_endian>::writeval(wv, val);
702 return (utils::has_overflow<31>(x) ?
703 This::STATUS_OVERFLOW : This::STATUS_OKAY);
707 // Get the GOT section, creating it if necessary.
709 template<bool big_endian>
710 Output_data_got<32, big_endian>*
711 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
713 if (this->got_ == NULL)
715 gold_assert(symtab != NULL && layout != NULL);
717 this->got_ = new Output_data_got<32, big_endian>();
719 Output_section* os;
720 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
721 (elfcpp::SHF_ALLOC
722 | elfcpp::SHF_WRITE),
723 this->got_);
724 os->set_is_relro();
726 // The old GNU linker creates a .got.plt section. We just
727 // create another set of data in the .got section. Note that we
728 // always create a PLT if we create a GOT, although the PLT
729 // might be empty.
730 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
731 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
732 (elfcpp::SHF_ALLOC
733 | elfcpp::SHF_WRITE),
734 this->got_plt_);
735 os->set_is_relro();
737 // The first three entries are reserved.
738 this->got_plt_->set_current_data_size(3 * 4);
740 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
741 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
742 this->got_plt_,
743 0, 0, elfcpp::STT_OBJECT,
744 elfcpp::STB_LOCAL,
745 elfcpp::STV_HIDDEN, 0,
746 false, false);
748 return this->got_;
751 // Get the dynamic reloc section, creating it if necessary.
753 template<bool big_endian>
754 typename Target_arm<big_endian>::Reloc_section*
755 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
757 if (this->rel_dyn_ == NULL)
759 gold_assert(layout != NULL);
760 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
761 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
762 elfcpp::SHF_ALLOC, this->rel_dyn_);
764 return this->rel_dyn_;
767 // A class to handle the PLT data.
769 template<bool big_endian>
770 class Output_data_plt_arm : public Output_section_data
772 public:
773 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
774 Reloc_section;
776 Output_data_plt_arm(Layout*, Output_data_space*);
778 // Add an entry to the PLT.
779 void
780 add_entry(Symbol* gsym);
782 // Return the .rel.plt section data.
783 const Reloc_section*
784 rel_plt() const
785 { return this->rel_; }
787 protected:
788 void
789 do_adjust_output_section(Output_section* os);
791 // Write to a map file.
792 void
793 do_print_to_mapfile(Mapfile* mapfile) const
794 { mapfile->print_output_data(this, _("** PLT")); }
796 private:
797 // Template for the first PLT entry.
798 static const uint32_t first_plt_entry[5];
800 // Template for subsequent PLT entries.
801 static const uint32_t plt_entry[3];
803 // Set the final size.
804 void
805 set_final_data_size()
807 this->set_data_size(sizeof(first_plt_entry)
808 + this->count_ * sizeof(plt_entry));
811 // Write out the PLT data.
812 void
813 do_write(Output_file*);
815 // The reloc section.
816 Reloc_section* rel_;
817 // The .got.plt section.
818 Output_data_space* got_plt_;
819 // The number of PLT entries.
820 unsigned int count_;
823 // Create the PLT section. The ordinary .got section is an argument,
824 // since we need to refer to the start. We also create our own .got
825 // section just for PLT entries.
827 template<bool big_endian>
828 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
829 Output_data_space* got_plt)
830 : Output_section_data(4), got_plt_(got_plt), count_(0)
832 this->rel_ = new Reloc_section(false);
833 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
834 elfcpp::SHF_ALLOC, this->rel_);
837 template<bool big_endian>
838 void
839 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
841 os->set_entsize(0);
844 // Add an entry to the PLT.
846 template<bool big_endian>
847 void
848 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
850 gold_assert(!gsym->has_plt_offset());
852 // Note that when setting the PLT offset we skip the initial
853 // reserved PLT entry.
854 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
855 + sizeof(first_plt_entry));
857 ++this->count_;
859 section_offset_type got_offset = this->got_plt_->current_data_size();
861 // Every PLT entry needs a GOT entry which points back to the PLT
862 // entry (this will be changed by the dynamic linker, normally
863 // lazily when the function is called).
864 this->got_plt_->set_current_data_size(got_offset + 4);
866 // Every PLT entry needs a reloc.
867 gsym->set_needs_dynsym_entry();
868 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
869 got_offset);
871 // Note that we don't need to save the symbol. The contents of the
872 // PLT are independent of which symbols are used. The symbols only
873 // appear in the relocations.
876 // ARM PLTs.
877 // FIXME: This is not very flexible. Right now this has only been tested
878 // on armv5te. If we are to support additional architecture features like
879 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
881 // The first entry in the PLT.
882 template<bool big_endian>
883 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
885 0xe52de004, // str lr, [sp, #-4]!
886 0xe59fe004, // ldr lr, [pc, #4]
887 0xe08fe00e, // add lr, pc, lr
888 0xe5bef008, // ldr pc, [lr, #8]!
889 0x00000000, // &GOT[0] - .
892 // Subsequent entries in the PLT.
894 template<bool big_endian>
895 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
897 0xe28fc600, // add ip, pc, #0xNN00000
898 0xe28cca00, // add ip, ip, #0xNN000
899 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
902 // Write out the PLT. This uses the hand-coded instructions above,
903 // and adjusts them as needed. This is all specified by the arm ELF
904 // Processor Supplement.
906 template<bool big_endian>
907 void
908 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
910 const off_t offset = this->offset();
911 const section_size_type oview_size =
912 convert_to_section_size_type(this->data_size());
913 unsigned char* const oview = of->get_output_view(offset, oview_size);
915 const off_t got_file_offset = this->got_plt_->offset();
916 const section_size_type got_size =
917 convert_to_section_size_type(this->got_plt_->data_size());
918 unsigned char* const got_view = of->get_output_view(got_file_offset,
919 got_size);
920 unsigned char* pov = oview;
922 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
923 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
925 // Write first PLT entry. All but the last word are constants.
926 const size_t num_first_plt_words = (sizeof(first_plt_entry)
927 / sizeof(plt_entry[0]));
928 for (size_t i = 0; i < num_first_plt_words - 1; i++)
929 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
930 // Last word in first PLT entry is &GOT[0] - .
931 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
932 got_address - (plt_address + 16));
933 pov += sizeof(first_plt_entry);
935 unsigned char* got_pov = got_view;
937 memset(got_pov, 0, 12);
938 got_pov += 12;
940 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
941 unsigned int plt_offset = sizeof(first_plt_entry);
942 unsigned int plt_rel_offset = 0;
943 unsigned int got_offset = 12;
944 const unsigned int count = this->count_;
945 for (unsigned int i = 0;
946 i < count;
947 ++i,
948 pov += sizeof(plt_entry),
949 got_pov += 4,
950 plt_offset += sizeof(plt_entry),
951 plt_rel_offset += rel_size,
952 got_offset += 4)
954 // Set and adjust the PLT entry itself.
955 int32_t offset = ((got_address + got_offset)
956 - (plt_address + plt_offset + 8));
958 gold_assert(offset >= 0 && offset < 0x0fffffff);
959 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
960 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
961 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
962 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
963 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
964 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
966 // Set the entry in the GOT.
967 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
970 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
971 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
973 of->write_output_view(offset, oview_size, oview);
974 of->write_output_view(got_file_offset, got_size, got_view);
977 // Create a PLT entry for a global symbol.
979 template<bool big_endian>
980 void
981 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
982 Symbol* gsym)
984 if (gsym->has_plt_offset())
985 return;
987 if (this->plt_ == NULL)
989 // Create the GOT sections first.
990 this->got_section(symtab, layout);
992 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
993 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
994 (elfcpp::SHF_ALLOC
995 | elfcpp::SHF_EXECINSTR),
996 this->plt_);
998 this->plt_->add_entry(gsym);
1001 // Report an unsupported relocation against a local symbol.
1003 template<bool big_endian>
1004 void
1005 Target_arm<big_endian>::Scan::unsupported_reloc_local(
1006 Sized_relobj<32, big_endian>* object,
1007 unsigned int r_type)
1009 gold_error(_("%s: unsupported reloc %u against local symbol"),
1010 object->name().c_str(), r_type);
1013 // We are about to emit a dynamic relocation of type R_TYPE. If the
1014 // dynamic linker does not support it, issue an error. The GNU linker
1015 // only issues a non-PIC error for an allocated read-only section.
1016 // Here we know the section is allocated, but we don't know that it is
1017 // read-only. But we check for all the relocation types which the
1018 // glibc dynamic linker supports, so it seems appropriate to issue an
1019 // error even if the section is not read-only.
1021 template<bool big_endian>
1022 void
1023 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
1024 unsigned int r_type)
1026 switch (r_type)
1028 // These are the relocation types supported by glibc for ARM.
1029 case elfcpp::R_ARM_RELATIVE:
1030 case elfcpp::R_ARM_COPY:
1031 case elfcpp::R_ARM_GLOB_DAT:
1032 case elfcpp::R_ARM_JUMP_SLOT:
1033 case elfcpp::R_ARM_ABS32:
1034 case elfcpp::R_ARM_PC24:
1035 // FIXME: The following 3 types are not supported by Android's dynamic
1036 // linker.
1037 case elfcpp::R_ARM_TLS_DTPMOD32:
1038 case elfcpp::R_ARM_TLS_DTPOFF32:
1039 case elfcpp::R_ARM_TLS_TPOFF32:
1040 return;
1042 default:
1043 // This prevents us from issuing more than one error per reloc
1044 // section. But we can still wind up issuing more than one
1045 // error per object file.
1046 if (this->issued_non_pic_error_)
1047 return;
1048 object->error(_("requires unsupported dynamic reloc; "
1049 "recompile with -fPIC"));
1050 this->issued_non_pic_error_ = true;
1051 return;
1053 case elfcpp::R_ARM_NONE:
1054 gold_unreachable();
1058 // Scan a relocation for a local symbol.
1059 // FIXME: This only handles a subset of relocation types used by Android
1060 // on ARM v5te devices.
1062 template<bool big_endian>
1063 inline void
1064 Target_arm<big_endian>::Scan::local(const General_options&,
1065 Symbol_table* symtab,
1066 Layout* layout,
1067 Target_arm* target,
1068 Sized_relobj<32, big_endian>* object,
1069 unsigned int data_shndx,
1070 Output_section* output_section,
1071 const elfcpp::Rel<32, big_endian>& reloc,
1072 unsigned int r_type,
1073 const elfcpp::Sym<32, big_endian>&)
1075 r_type = get_real_reloc_type(r_type);
1076 switch (r_type)
1078 case elfcpp::R_ARM_NONE:
1079 break;
1081 case elfcpp::R_ARM_ABS32:
1082 // If building a shared library (or a position-independent
1083 // executable), we need to create a dynamic relocation for
1084 // this location. The relocation applied at link time will
1085 // apply the link-time value, so we flag the location with
1086 // an R_ARM_RELATIVE relocation so the dynamic loader can
1087 // relocate it easily.
1088 if (parameters->options().output_is_position_independent())
1090 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1091 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1092 // If we are to add more other reloc types than R_ARM_ABS32,
1093 // we need to add check_non_pic(object, r_type) here.
1094 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
1095 output_section, data_shndx,
1096 reloc.get_r_offset());
1098 break;
1100 case elfcpp::R_ARM_REL32:
1101 case elfcpp::R_ARM_THM_CALL:
1102 case elfcpp::R_ARM_CALL:
1103 case elfcpp::R_ARM_PREL31:
1104 case elfcpp::R_ARM_JUMP24:
1105 case elfcpp::R_ARM_PLT32:
1106 break;
1108 case elfcpp::R_ARM_GOTOFF32:
1109 // We need a GOT section:
1110 target->got_section(symtab, layout);
1111 break;
1113 case elfcpp::R_ARM_BASE_PREL:
1114 // FIXME: What about this?
1115 break;
1117 case elfcpp::R_ARM_GOT_BREL:
1119 // The symbol requires a GOT entry.
1120 Output_data_got<32, big_endian>* got =
1121 target->got_section(symtab, layout);
1122 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1123 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1125 // If we are generating a shared object, we need to add a
1126 // dynamic RELATIVE relocation for this symbol's GOT entry.
1127 if (parameters->options().output_is_position_independent())
1129 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1130 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1131 rel_dyn->add_local_relative(
1132 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
1133 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1137 break;
1139 case elfcpp::R_ARM_TARGET1:
1140 // This should have been mapped to another type already.
1141 // Fall through.
1142 case elfcpp::R_ARM_COPY:
1143 case elfcpp::R_ARM_GLOB_DAT:
1144 case elfcpp::R_ARM_JUMP_SLOT:
1145 case elfcpp::R_ARM_RELATIVE:
1146 // These are relocations which should only be seen by the
1147 // dynamic linker, and should never be seen here.
1148 gold_error(_("%s: unexpected reloc %u in object file"),
1149 object->name().c_str(), r_type);
1150 break;
1152 default:
1153 unsupported_reloc_local(object, r_type);
1154 break;
1158 // Report an unsupported relocation against a global symbol.
1160 template<bool big_endian>
1161 void
1162 Target_arm<big_endian>::Scan::unsupported_reloc_global(
1163 Sized_relobj<32, big_endian>* object,
1164 unsigned int r_type,
1165 Symbol* gsym)
1167 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1168 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1171 // Scan a relocation for a global symbol.
1172 // FIXME: This only handles a subset of relocation types used by Android
1173 // on ARM v5te devices.
1175 template<bool big_endian>
1176 inline void
1177 Target_arm<big_endian>::Scan::global(const General_options&,
1178 Symbol_table* symtab,
1179 Layout* layout,
1180 Target_arm* target,
1181 Sized_relobj<32, big_endian>* object,
1182 unsigned int data_shndx,
1183 Output_section* output_section,
1184 const elfcpp::Rel<32, big_endian>& reloc,
1185 unsigned int r_type,
1186 Symbol* gsym)
1188 r_type = get_real_reloc_type(r_type);
1189 switch (r_type)
1191 case elfcpp::R_ARM_NONE:
1192 break;
1194 case elfcpp::R_ARM_ABS32:
1196 // Make a dynamic relocation if necessary.
1197 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1199 if (target->may_need_copy_reloc(gsym))
1201 target->copy_reloc(symtab, layout, object,
1202 data_shndx, output_section, gsym, reloc);
1204 else if (gsym->can_use_relative_reloc(false))
1206 // If we are to add more other reloc types than R_ARM_ABS32,
1207 // we need to add check_non_pic(object, r_type) here.
1208 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1209 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
1210 output_section, object,
1211 data_shndx, reloc.get_r_offset());
1213 else
1215 // If we are to add more other reloc types than R_ARM_ABS32,
1216 // we need to add check_non_pic(object, r_type) here.
1217 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1218 rel_dyn->add_global(gsym, r_type, output_section, object,
1219 data_shndx, reloc.get_r_offset());
1223 break;
1225 case elfcpp::R_ARM_REL32:
1226 case elfcpp::R_ARM_PREL31:
1228 // Make a dynamic relocation if necessary.
1229 int flags = Symbol::NON_PIC_REF;
1230 if (gsym->needs_dynamic_reloc(flags))
1232 if (target->may_need_copy_reloc(gsym))
1234 target->copy_reloc(symtab, layout, object,
1235 data_shndx, output_section, gsym, reloc);
1237 else
1239 check_non_pic(object, r_type);
1240 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1241 rel_dyn->add_global(gsym, r_type, output_section, object,
1242 data_shndx, reloc.get_r_offset());
1246 break;
1248 case elfcpp::R_ARM_JUMP24:
1249 case elfcpp::R_ARM_THM_CALL:
1250 case elfcpp::R_ARM_CALL:
1252 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
1253 target->make_plt_entry(symtab, layout, gsym);
1254 // Make a dynamic relocation if necessary.
1255 int flags = Symbol::NON_PIC_REF;
1256 if (gsym->type() == elfcpp::STT_FUNC
1257 || gsym->type() == elfcpp::STT_ARM_TFUNC)
1258 flags |= Symbol::FUNCTION_CALL;
1259 if (gsym->needs_dynamic_reloc(flags))
1261 if (target->may_need_copy_reloc(gsym))
1263 target->copy_reloc(symtab, layout, object,
1264 data_shndx, output_section, gsym,
1265 reloc);
1267 else
1269 check_non_pic(object, r_type);
1270 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1271 rel_dyn->add_global(gsym, r_type, output_section, object,
1272 data_shndx, reloc.get_r_offset());
1276 break;
1278 case elfcpp::R_ARM_PLT32:
1279 // If the symbol is fully resolved, this is just a relative
1280 // local reloc. Otherwise we need a PLT entry.
1281 if (gsym->final_value_is_known())
1282 break;
1283 // If building a shared library, we can also skip the PLT entry
1284 // if the symbol is defined in the output file and is protected
1285 // or hidden.
1286 if (gsym->is_defined()
1287 && !gsym->is_from_dynobj()
1288 && !gsym->is_preemptible())
1289 break;
1290 target->make_plt_entry(symtab, layout, gsym);
1291 break;
1293 case elfcpp::R_ARM_GOTOFF32:
1294 // We need a GOT section.
1295 target->got_section(symtab, layout);
1296 break;
1298 case elfcpp::R_ARM_BASE_PREL:
1299 // FIXME: What about this?
1300 break;
1302 case elfcpp::R_ARM_GOT_BREL:
1304 // The symbol requires a GOT entry.
1305 Output_data_got<32, big_endian>* got =
1306 target->got_section(symtab, layout);
1307 if (gsym->final_value_is_known())
1308 got->add_global(gsym, GOT_TYPE_STANDARD);
1309 else
1311 // If this symbol is not fully resolved, we need to add a
1312 // GOT entry with a dynamic relocation.
1313 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1314 if (gsym->is_from_dynobj()
1315 || gsym->is_undefined()
1316 || gsym->is_preemptible())
1317 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1318 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
1319 else
1321 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1322 rel_dyn->add_global_relative(
1323 gsym, elfcpp::R_ARM_RELATIVE, got,
1324 gsym->got_offset(GOT_TYPE_STANDARD));
1328 break;
1330 case elfcpp::R_ARM_TARGET1:
1331 // This should have been mapped to another type already.
1332 // Fall through.
1333 case elfcpp::R_ARM_COPY:
1334 case elfcpp::R_ARM_GLOB_DAT:
1335 case elfcpp::R_ARM_JUMP_SLOT:
1336 case elfcpp::R_ARM_RELATIVE:
1337 // These are relocations which should only be seen by the
1338 // dynamic linker, and should never be seen here.
1339 gold_error(_("%s: unexpected reloc %u in object file"),
1340 object->name().c_str(), r_type);
1341 break;
1343 default:
1344 unsupported_reloc_global(object, r_type, gsym);
1345 break;
1349 // Process relocations for gc.
1351 template<bool big_endian>
1352 void
1353 Target_arm<big_endian>::gc_process_relocs(const General_options& options,
1354 Symbol_table* symtab,
1355 Layout* layout,
1356 Sized_relobj<32, big_endian>* object,
1357 unsigned int data_shndx,
1358 unsigned int,
1359 const unsigned char* prelocs,
1360 size_t reloc_count,
1361 Output_section* output_section,
1362 bool needs_special_offset_handling,
1363 size_t local_symbol_count,
1364 const unsigned char* plocal_symbols)
1366 typedef Target_arm<big_endian> Arm;
1367 typedef typename Target_arm<big_endian>::Scan Scan;
1369 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
1370 options,
1371 symtab,
1372 layout,
1373 this,
1374 object,
1375 data_shndx,
1376 prelocs,
1377 reloc_count,
1378 output_section,
1379 needs_special_offset_handling,
1380 local_symbol_count,
1381 plocal_symbols);
1384 // Scan relocations for a section.
1386 template<bool big_endian>
1387 void
1388 Target_arm<big_endian>::scan_relocs(const General_options& options,
1389 Symbol_table* symtab,
1390 Layout* layout,
1391 Sized_relobj<32, big_endian>* object,
1392 unsigned int data_shndx,
1393 unsigned int sh_type,
1394 const unsigned char* prelocs,
1395 size_t reloc_count,
1396 Output_section* output_section,
1397 bool needs_special_offset_handling,
1398 size_t local_symbol_count,
1399 const unsigned char* plocal_symbols)
1401 typedef typename Target_arm<big_endian>::Scan Scan;
1402 if (sh_type == elfcpp::SHT_RELA)
1404 gold_error(_("%s: unsupported RELA reloc section"),
1405 object->name().c_str());
1406 return;
1409 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
1410 options,
1411 symtab,
1412 layout,
1413 this,
1414 object,
1415 data_shndx,
1416 prelocs,
1417 reloc_count,
1418 output_section,
1419 needs_special_offset_handling,
1420 local_symbol_count,
1421 plocal_symbols);
1424 // Finalize the sections.
1426 template<bool big_endian>
1427 void
1428 Target_arm<big_endian>::do_finalize_sections(Layout* layout)
1430 // Fill in some more dynamic tags.
1431 Output_data_dynamic* const odyn = layout->dynamic_data();
1432 if (odyn != NULL)
1434 if (this->got_plt_ != NULL)
1435 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1437 if (this->plt_ != NULL)
1439 const Output_data* od = this->plt_->rel_plt();
1440 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1441 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1442 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1445 if (this->rel_dyn_ != NULL)
1447 const Output_data* od = this->rel_dyn_;
1448 odyn->add_section_address(elfcpp::DT_REL, od);
1449 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1450 odyn->add_constant(elfcpp::DT_RELENT,
1451 elfcpp::Elf_sizes<32>::rel_size);
1454 if (!parameters->options().shared())
1456 // The value of the DT_DEBUG tag is filled in by the dynamic
1457 // linker at run time, and used by the debugger.
1458 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1462 // Emit any relocs we saved in an attempt to avoid generating COPY
1463 // relocs.
1464 if (this->copy_relocs_.any_saved_relocs())
1465 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1468 // Return whether a direct absolute static relocation needs to be applied.
1469 // In cases where Scan::local() or Scan::global() has created
1470 // a dynamic relocation other than R_ARM_RELATIVE, the addend
1471 // of the relocation is carried in the data, and we must not
1472 // apply the static relocation.
1474 template<bool big_endian>
1475 inline bool
1476 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
1477 const Sized_symbol<32>* gsym,
1478 int ref_flags,
1479 bool is_32bit,
1480 Output_section* output_section)
1482 // If the output section is not allocated, then we didn't call
1483 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1484 // the reloc here.
1485 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1486 return true;
1488 // For local symbols, we will have created a non-RELATIVE dynamic
1489 // relocation only if (a) the output is position independent,
1490 // (b) the relocation is absolute (not pc- or segment-relative), and
1491 // (c) the relocation is not 32 bits wide.
1492 if (gsym == NULL)
1493 return !(parameters->options().output_is_position_independent()
1494 && (ref_flags & Symbol::ABSOLUTE_REF)
1495 && !is_32bit);
1497 // For global symbols, we use the same helper routines used in the
1498 // scan pass. If we did not create a dynamic relocation, or if we
1499 // created a RELATIVE dynamic relocation, we should apply the static
1500 // relocation.
1501 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1502 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1503 && gsym->can_use_relative_reloc(ref_flags
1504 & Symbol::FUNCTION_CALL);
1505 return !has_dyn || is_rel;
1508 // Perform a relocation.
1510 template<bool big_endian>
1511 inline bool
1512 Target_arm<big_endian>::Relocate::relocate(
1513 const Relocate_info<32, big_endian>* relinfo,
1514 Target_arm* target,
1515 Output_section *output_section,
1516 size_t relnum,
1517 const elfcpp::Rel<32, big_endian>& rel,
1518 unsigned int r_type,
1519 const Sized_symbol<32>* gsym,
1520 const Symbol_value<32>* psymval,
1521 unsigned char* view,
1522 elfcpp::Elf_types<32>::Elf_Addr address,
1523 section_size_type /* view_size */ )
1525 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
1527 r_type = get_real_reloc_type(r_type);
1529 // If this the symbol may be a Thumb function, set thumb bit to 1.
1530 bool has_thumb_bit = ((gsym != NULL)
1531 && (gsym->type() == elfcpp::STT_FUNC
1532 || gsym->type() == elfcpp::STT_ARM_TFUNC));
1534 // Pick the value to use for symbols defined in shared objects.
1535 Symbol_value<32> symval;
1536 if (gsym != NULL
1537 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
1539 symval.set_output_value(target->plt_section()->address()
1540 + gsym->plt_offset());
1541 psymval = &symval;
1542 has_thumb_bit = 0;
1545 const Sized_relobj<32, big_endian>* object = relinfo->object;
1547 // Get the GOT offset if needed.
1548 // The GOT pointer points to the end of the GOT section.
1549 // We need to subtract the size of the GOT section to get
1550 // the actual offset to use in the relocation.
1551 bool have_got_offset = false;
1552 unsigned int got_offset = 0;
1553 switch (r_type)
1555 case elfcpp::R_ARM_GOT_BREL:
1556 if (gsym != NULL)
1558 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1559 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1560 - target->got_size());
1562 else
1564 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1565 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1566 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1567 - target->got_size());
1569 have_got_offset = true;
1570 break;
1572 default:
1573 break;
1576 typename Arm_relocate_functions::Status reloc_status =
1577 Arm_relocate_functions::STATUS_OKAY;
1578 switch (r_type)
1580 case elfcpp::R_ARM_NONE:
1581 break;
1583 case elfcpp::R_ARM_ABS32:
1584 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1585 output_section))
1586 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
1587 has_thumb_bit);
1588 break;
1590 case elfcpp::R_ARM_REL32:
1591 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1592 address, has_thumb_bit);
1593 break;
1595 case elfcpp::R_ARM_THM_CALL:
1596 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
1597 address, has_thumb_bit);
1598 break;
1600 case elfcpp::R_ARM_GOTOFF32:
1602 elfcpp::Elf_types<32>::Elf_Addr got_origin;
1603 got_origin = target->got_plt_section()->address();
1604 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1605 got_origin, has_thumb_bit);
1607 break;
1609 case elfcpp::R_ARM_BASE_PREL:
1611 uint32_t origin;
1612 // Get the addressing origin of the output segment defining the
1613 // symbol gsym (AAELF 4.6.1.2 Relocation types)
1614 gold_assert(gsym != NULL);
1615 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
1616 origin = gsym->output_segment()->vaddr();
1617 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
1618 origin = gsym->output_data()->address();
1619 else
1621 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1622 _("cannot find origin of R_ARM_BASE_PREL"));
1623 return true;
1625 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
1627 break;
1629 case elfcpp::R_ARM_GOT_BREL:
1630 gold_assert(have_got_offset);
1631 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
1632 break;
1634 case elfcpp::R_ARM_PLT32:
1635 gold_assert(gsym == NULL
1636 || gsym->has_plt_offset()
1637 || gsym->final_value_is_known()
1638 || (gsym->is_defined()
1639 && !gsym->is_from_dynobj()
1640 && !gsym->is_preemptible()));
1641 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
1642 address, has_thumb_bit);
1643 break;
1645 case elfcpp::R_ARM_CALL:
1646 reloc_status = Arm_relocate_functions::call(view, object, psymval,
1647 address, has_thumb_bit);
1648 break;
1650 case elfcpp::R_ARM_JUMP24:
1651 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
1652 address, has_thumb_bit);
1653 break;
1655 case elfcpp::R_ARM_PREL31:
1656 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
1657 address, has_thumb_bit);
1658 break;
1660 case elfcpp::R_ARM_TARGET1:
1661 // This should have been mapped to another type already.
1662 // Fall through.
1663 case elfcpp::R_ARM_COPY:
1664 case elfcpp::R_ARM_GLOB_DAT:
1665 case elfcpp::R_ARM_JUMP_SLOT:
1666 case elfcpp::R_ARM_RELATIVE:
1667 // These are relocations which should only be seen by the
1668 // dynamic linker, and should never be seen here.
1669 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1670 _("unexpected reloc %u in object file"),
1671 r_type);
1672 break;
1674 default:
1675 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1676 _("unsupported reloc %u"),
1677 r_type);
1678 break;
1681 // Report any errors.
1682 switch (reloc_status)
1684 case Arm_relocate_functions::STATUS_OKAY:
1685 break;
1686 case Arm_relocate_functions::STATUS_OVERFLOW:
1687 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1688 _("relocation overflow in relocation %u"),
1689 r_type);
1690 break;
1691 case Arm_relocate_functions::STATUS_BAD_RELOC:
1692 gold_error_at_location(
1693 relinfo,
1694 relnum,
1695 rel.get_r_offset(),
1696 _("unexpected opcode while processing relocation %u"),
1697 r_type);
1698 break;
1699 default:
1700 gold_unreachable();
1703 return true;
1706 // Relocate section data.
1708 template<bool big_endian>
1709 void
1710 Target_arm<big_endian>::relocate_section(
1711 const Relocate_info<32, big_endian>* relinfo,
1712 unsigned int sh_type,
1713 const unsigned char* prelocs,
1714 size_t reloc_count,
1715 Output_section* output_section,
1716 bool needs_special_offset_handling,
1717 unsigned char* view,
1718 elfcpp::Elf_types<32>::Elf_Addr address,
1719 section_size_type view_size)
1721 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
1722 gold_assert(sh_type == elfcpp::SHT_REL);
1724 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
1725 Arm_relocate>(
1726 relinfo,
1727 this,
1728 prelocs,
1729 reloc_count,
1730 output_section,
1731 needs_special_offset_handling,
1732 view,
1733 address,
1734 view_size);
1737 // Return the size of a relocation while scanning during a relocatable
1738 // link.
1740 template<bool big_endian>
1741 unsigned int
1742 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1743 unsigned int r_type,
1744 Relobj* object)
1746 r_type = get_real_reloc_type(r_type);
1747 switch (r_type)
1749 case elfcpp::R_ARM_NONE:
1750 return 0;
1752 case elfcpp::R_ARM_ABS32:
1753 case elfcpp::R_ARM_REL32:
1754 case elfcpp::R_ARM_THM_CALL:
1755 case elfcpp::R_ARM_GOTOFF32:
1756 case elfcpp::R_ARM_BASE_PREL:
1757 case elfcpp::R_ARM_GOT_BREL:
1758 case elfcpp::R_ARM_PLT32:
1759 case elfcpp::R_ARM_CALL:
1760 case elfcpp::R_ARM_JUMP24:
1761 case elfcpp::R_ARM_PREL31:
1762 return 4;
1764 case elfcpp::R_ARM_TARGET1:
1765 // This should have been mapped to another type already.
1766 // Fall through.
1767 case elfcpp::R_ARM_COPY:
1768 case elfcpp::R_ARM_GLOB_DAT:
1769 case elfcpp::R_ARM_JUMP_SLOT:
1770 case elfcpp::R_ARM_RELATIVE:
1771 // These are relocations which should only be seen by the
1772 // dynamic linker, and should never be seen here.
1773 gold_error(_("%s: unexpected reloc %u in object file"),
1774 object->name().c_str(), r_type);
1775 return 0;
1777 default:
1778 object->error(_("unsupported reloc %u in object file"), r_type);
1779 return 0;
1783 // Scan the relocs during a relocatable link.
1785 template<bool big_endian>
1786 void
1787 Target_arm<big_endian>::scan_relocatable_relocs(
1788 const General_options& options,
1789 Symbol_table* symtab,
1790 Layout* layout,
1791 Sized_relobj<32, big_endian>* object,
1792 unsigned int data_shndx,
1793 unsigned int sh_type,
1794 const unsigned char* prelocs,
1795 size_t reloc_count,
1796 Output_section* output_section,
1797 bool needs_special_offset_handling,
1798 size_t local_symbol_count,
1799 const unsigned char* plocal_symbols,
1800 Relocatable_relocs* rr)
1802 gold_assert(sh_type == elfcpp::SHT_REL);
1804 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
1805 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1807 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
1808 Scan_relocatable_relocs>(
1809 options,
1810 symtab,
1811 layout,
1812 object,
1813 data_shndx,
1814 prelocs,
1815 reloc_count,
1816 output_section,
1817 needs_special_offset_handling,
1818 local_symbol_count,
1819 plocal_symbols,
1820 rr);
1823 // Relocate a section during a relocatable link.
1825 template<bool big_endian>
1826 void
1827 Target_arm<big_endian>::relocate_for_relocatable(
1828 const Relocate_info<32, big_endian>* relinfo,
1829 unsigned int sh_type,
1830 const unsigned char* prelocs,
1831 size_t reloc_count,
1832 Output_section* output_section,
1833 off_t offset_in_output_section,
1834 const Relocatable_relocs* rr,
1835 unsigned char* view,
1836 elfcpp::Elf_types<32>::Elf_Addr view_address,
1837 section_size_type view_size,
1838 unsigned char* reloc_view,
1839 section_size_type reloc_view_size)
1841 gold_assert(sh_type == elfcpp::SHT_REL);
1843 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
1844 relinfo,
1845 prelocs,
1846 reloc_count,
1847 output_section,
1848 offset_in_output_section,
1850 view,
1851 view_address,
1852 view_size,
1853 reloc_view,
1854 reloc_view_size);
1857 // Return the value to use for a dynamic symbol which requires special
1858 // treatment. This is how we support equality comparisons of function
1859 // pointers across shared library boundaries, as described in the
1860 // processor specific ABI supplement.
1862 template<bool big_endian>
1863 uint64_t
1864 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
1866 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1867 return this->plt_section()->address() + gsym->plt_offset();
1870 // Map platform-specific relocs to real relocs
1872 template<bool big_endian>
1873 unsigned int
1874 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
1876 switch (r_type)
1878 case elfcpp::R_ARM_TARGET1:
1879 // This is either R_ARM_ABS32 or R_ARM_REL32;
1880 return elfcpp::R_ARM_ABS32;
1882 case elfcpp::R_ARM_TARGET2:
1883 // This can be any reloc type but ususally is R_ARM_GOT_PREL
1884 return elfcpp::R_ARM_GOT_PREL;
1886 default:
1887 return r_type;
1891 // The selector for arm object files.
1893 template<bool big_endian>
1894 class Target_selector_arm : public Target_selector
1896 public:
1897 Target_selector_arm()
1898 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
1899 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
1902 Target*
1903 do_instantiate_target()
1904 { return new Target_arm<big_endian>(); }
1907 Target_selector_arm<false> target_selector_arm;
1908 Target_selector_arm<true> target_selector_armbe;
1910 } // End anonymous namespace.