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.
32 #include "parameters.h"
39 #include "copy-relocs.h"
41 #include "target-reloc.h"
42 #include "target-select.h"
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
80 // - Generate various branch stubs.
81 // - Support interworking.
82 // - Define section symbols __exidx_start and __exidx_stop.
83 // - Support more relocation types as needed.
84 // - Make PLTs more flexible for different architecture features like
86 // There are probably a lot more.
88 // Utilities for manipulating integers of up to 32-bits
92 // Sign extend an n-bit unsigned integer stored in an uint32_t into
93 // an int32_t. NO_BITS must be between 1 to 32.
96 sign_extend(uint32_t bits)
98 gold_assert(no_bits >= 0 && no_bits <= 32);
100 return static_cast<int32_t>(bits);
101 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
103 uint32_t top_bit = 1U << (no_bits - 1);
104 int32_t as_signed = static_cast<int32_t>(bits);
105 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
108 // Detects overflow of an NO_BITS integer stored in a uint32_t.
109 template<int no_bits>
111 has_overflow(uint32_t bits)
113 gold_assert(no_bits >= 0 && no_bits <= 32);
116 int32_t max = (1 << (no_bits - 1)) - 1;
117 int32_t min = -(1 << (no_bits - 1));
118 int32_t as_signed = static_cast<int32_t>(bits);
119 return as_signed > max || as_signed < min;
122 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
123 // fits in the given number of bits as either a signed or unsigned value.
124 // For example, has_signed_unsigned_overflow<8> would check
125 // -128 <= bits <= 255
126 template<int no_bits>
128 has_signed_unsigned_overflow(uint32_t bits)
130 gold_assert(no_bits >= 2 && no_bits <= 32);
133 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
134 int32_t min = -(1 << (no_bits - 1));
135 int32_t as_signed = static_cast<int32_t>(bits);
136 return as_signed > max || as_signed < min;
139 // Select bits from A and B using bits in MASK. For each n in [0..31],
140 // the n-th bit in the result is chosen from the n-th bits of A and B.
141 // A zero selects A and a one selects B.
142 static inline uint32_t
143 bit_select(uint32_t a, uint32_t b, uint32_t mask)
144 { return (a & ~mask) | (b & mask); }
147 template<bool big_endian>
148 class Target_arm : public Sized_target<32, big_endian>
151 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
155 : Sized_target<32, big_endian>(&arm_info),
156 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
157 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL)
160 // Process the relocations to determine unreferenced sections for
161 // garbage collection.
163 gc_process_relocs(const General_options& options,
164 Symbol_table* symtab,
166 Sized_relobj<32, big_endian>* object,
167 unsigned int data_shndx,
168 unsigned int sh_type,
169 const unsigned char* prelocs,
171 Output_section* output_section,
172 bool needs_special_offset_handling,
173 size_t local_symbol_count,
174 const unsigned char* plocal_symbols);
176 // Scan the relocations to look for symbol adjustments.
178 scan_relocs(const General_options& options,
179 Symbol_table* symtab,
181 Sized_relobj<32, big_endian>* object,
182 unsigned int data_shndx,
183 unsigned int sh_type,
184 const unsigned char* prelocs,
186 Output_section* output_section,
187 bool needs_special_offset_handling,
188 size_t local_symbol_count,
189 const unsigned char* plocal_symbols);
191 // Finalize the sections.
193 do_finalize_sections(Layout*);
195 // Return the value to use for a dynamic symbol which requires special
198 do_dynsym_value(const Symbol*) const;
200 // Relocate a section.
202 relocate_section(const Relocate_info<32, big_endian>*,
203 unsigned int sh_type,
204 const unsigned char* prelocs,
206 Output_section* output_section,
207 bool needs_special_offset_handling,
209 elfcpp::Elf_types<32>::Elf_Addr view_address,
210 section_size_type view_size,
211 const Reloc_symbol_changes*);
213 // Scan the relocs during a relocatable link.
215 scan_relocatable_relocs(const General_options& options,
216 Symbol_table* symtab,
218 Sized_relobj<32, big_endian>* object,
219 unsigned int data_shndx,
220 unsigned int sh_type,
221 const unsigned char* prelocs,
223 Output_section* output_section,
224 bool needs_special_offset_handling,
225 size_t local_symbol_count,
226 const unsigned char* plocal_symbols,
227 Relocatable_relocs*);
229 // Relocate a section during a relocatable link.
231 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
232 unsigned int sh_type,
233 const unsigned char* prelocs,
235 Output_section* output_section,
236 off_t offset_in_output_section,
237 const Relocatable_relocs*,
239 elfcpp::Elf_types<32>::Elf_Addr view_address,
240 section_size_type view_size,
241 unsigned char* reloc_view,
242 section_size_type reloc_view_size);
244 // Return whether SYM is defined by the ABI.
246 do_is_defined_by_abi(Symbol* sym) const
247 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
249 // Return the size of the GOT section.
253 gold_assert(this->got_ != NULL);
254 return this->got_->data_size();
257 // Map platform-specific reloc types
259 get_real_reloc_type (unsigned int r_type);
262 // The class which scans relocations.
267 : issued_non_pic_error_(false)
271 local(const General_options& options, Symbol_table* symtab,
272 Layout* layout, Target_arm* target,
273 Sized_relobj<32, big_endian>* object,
274 unsigned int data_shndx,
275 Output_section* output_section,
276 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
277 const elfcpp::Sym<32, big_endian>& lsym);
280 global(const General_options& options, Symbol_table* symtab,
281 Layout* layout, Target_arm* target,
282 Sized_relobj<32, big_endian>* object,
283 unsigned int data_shndx,
284 Output_section* output_section,
285 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
290 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
291 unsigned int r_type);
294 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
295 unsigned int r_type, Symbol*);
298 check_non_pic(Relobj*, unsigned int r_type);
300 // Almost identical to Symbol::needs_plt_entry except that it also
301 // handles STT_ARM_TFUNC.
303 symbol_needs_plt_entry(const Symbol* sym)
305 // An undefined symbol from an executable does not need a PLT entry.
306 if (sym->is_undefined() && !parameters->options().shared())
309 return (!parameters->doing_static_link()
310 && (sym->type() == elfcpp::STT_FUNC
311 || sym->type() == elfcpp::STT_ARM_TFUNC)
312 && (sym->is_from_dynobj()
313 || sym->is_undefined()
314 || sym->is_preemptible()));
317 // Whether we have issued an error about a non-PIC compilation.
318 bool issued_non_pic_error_;
321 // The class which implements relocation.
331 // Return whether the static relocation needs to be applied.
333 should_apply_static_reloc(const Sized_symbol<32>* gsym,
336 Output_section* output_section);
338 // Do a relocation. Return false if the caller should not issue
339 // any warnings about this relocation.
341 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
342 Output_section*, size_t relnum,
343 const elfcpp::Rel<32, big_endian>&,
344 unsigned int r_type, const Sized_symbol<32>*,
345 const Symbol_value<32>*,
346 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
349 // Return whether we want to pass flag NON_PIC_REF for this
352 reloc_is_non_pic (unsigned int r_type)
356 case elfcpp::R_ARM_REL32:
357 case elfcpp::R_ARM_THM_CALL:
358 case elfcpp::R_ARM_CALL:
359 case elfcpp::R_ARM_JUMP24:
360 case elfcpp::R_ARM_PREL31:
368 // A class which returns the size required for a relocation type,
369 // used while scanning relocs during a relocatable link.
370 class Relocatable_size_for_reloc
374 get_size_for_reloc(unsigned int, Relobj*);
377 // Get the GOT section, creating it if necessary.
378 Output_data_got<32, big_endian>*
379 got_section(Symbol_table*, Layout*);
381 // Get the GOT PLT section.
383 got_plt_section() const
385 gold_assert(this->got_plt_ != NULL);
386 return this->got_plt_;
389 // Create a PLT entry for a global symbol.
391 make_plt_entry(Symbol_table*, Layout*, Symbol*);
393 // Get the PLT section.
394 const Output_data_plt_arm<big_endian>*
397 gold_assert(this->plt_ != NULL);
401 // Get the dynamic reloc section, creating it if necessary.
403 rel_dyn_section(Layout*);
405 // Return true if the symbol may need a COPY relocation.
406 // References from an executable object to non-function symbols
407 // defined in a dynamic object may need a COPY relocation.
409 may_need_copy_reloc(Symbol* gsym)
411 return (gsym->type() != elfcpp::STT_ARM_TFUNC
412 && gsym->may_need_copy_reloc());
415 // Add a potential copy relocation.
417 copy_reloc(Symbol_table* symtab, Layout* layout,
418 Sized_relobj<32, big_endian>* object,
419 unsigned int shndx, Output_section* output_section,
420 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
422 this->copy_relocs_.copy_reloc(symtab, layout,
423 symtab->get_sized_symbol<32>(sym),
424 object, shndx, output_section, reloc,
425 this->rel_dyn_section(layout));
428 // Information about this specific target which we pass to the
429 // general Target structure.
430 static const Target::Target_info arm_info;
432 // The types of GOT entries needed for this platform.
435 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
439 Output_data_got<32, big_endian>* got_;
441 Output_data_plt_arm<big_endian>* plt_;
442 // The GOT PLT section.
443 Output_data_space* got_plt_;
444 // The dynamic reloc section.
445 Reloc_section* rel_dyn_;
446 // Relocs saved to avoid a COPY reloc.
447 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
448 // Space for variables copied with a COPY reloc.
449 Output_data_space* dynbss_;
452 template<bool big_endian>
453 const Target::Target_info Target_arm<big_endian>::arm_info =
456 big_endian, // is_big_endian
457 elfcpp::EM_ARM, // machine_code
458 false, // has_make_symbol
459 false, // has_resolve
460 false, // has_code_fill
461 true, // is_default_stack_executable
463 "/usr/lib/libc.so.1", // dynamic_linker
464 0x8000, // default_text_segment_address
465 0x1000, // abi_pagesize (overridable by -z max-page-size)
466 0x1000, // common_pagesize (overridable by -z common-page-size)
467 elfcpp::SHN_UNDEF, // small_common_shndx
468 elfcpp::SHN_UNDEF, // large_common_shndx
469 0, // small_common_section_flags
470 0 // large_common_section_flags
473 // Arm relocate functions class
476 template<bool big_endian>
477 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
482 STATUS_OKAY, // No error during relocation.
483 STATUS_OVERFLOW, // Relocation oveflow.
484 STATUS_BAD_RELOC // Relocation cannot be applied.
488 typedef Relocate_functions<32, big_endian> Base;
489 typedef Arm_relocate_functions<big_endian> This;
491 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
492 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
493 // to distinguish ARM and THUMB functions and it is treated specially.
494 static inline Symbol_value<32>::Value
495 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
496 const Symbol_value<32>* psymval,
497 Symbol_value<32>::Value addend,
500 typedef Symbol_value<32>::Value Valtype;
504 Valtype raw = psymval->value(object, 0);
505 Valtype thumb_bit = raw & 1;
506 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
509 return psymval->value(object, addend);
512 // FIXME: This probably only works for Android on ARM v5te. We should
513 // following GNU ld for the general case.
514 template<unsigned r_type>
515 static inline typename This::Status
516 arm_branch_common(unsigned char *view,
517 const Sized_relobj<32, big_endian>* object,
518 const Symbol_value<32>* psymval,
519 elfcpp::Elf_types<32>::Elf_Addr address,
522 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
523 Valtype* wv = reinterpret_cast<Valtype*>(view);
524 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
526 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
527 && ((val & 0x0f000000UL) == 0x0a000000UL);
528 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
529 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
530 && ((val & 0x0f000000UL) == 0x0b000000UL);
531 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
532 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
534 if (r_type == elfcpp::R_ARM_CALL)
536 if (!insn_is_uncond_bl && !insn_is_blx)
537 return This::STATUS_BAD_RELOC;
539 else if (r_type == elfcpp::R_ARM_JUMP24)
541 if (!insn_is_b && !insn_is_cond_bl)
542 return This::STATUS_BAD_RELOC;
544 else if (r_type == elfcpp::R_ARM_PLT32)
546 if (!insn_is_any_branch)
547 return This::STATUS_BAD_RELOC;
552 Valtype addend = utils::sign_extend<26>(val << 2);
553 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
556 // If target has thumb bit set, we need to either turn the BL
557 // into a BLX (for ARMv5 or above) or generate a stub.
561 if (insn_is_uncond_bl)
562 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
564 return This::STATUS_BAD_RELOC;
567 gold_assert(!insn_is_blx);
569 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
570 elfcpp::Swap<32, big_endian>::writeval(wv, val);
571 return (utils::has_overflow<26>(x)
572 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
578 static inline typename This::Status
579 abs8(unsigned char *view,
580 const Sized_relobj<32, big_endian>* object,
581 const Symbol_value<32>* psymval, bool has_thumb_bit)
583 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
584 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
585 Valtype* wv = reinterpret_cast<Valtype*>(view);
586 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
587 Reltype addend = utils::sign_extend<8>(val);
588 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
589 val = utils::bit_select(val, x, 0xffU);
590 elfcpp::Swap<8, big_endian>::writeval(wv, val);
591 return (utils::has_signed_unsigned_overflow<8>(x)
592 ? This::STATUS_OVERFLOW
593 : This::STATUS_OKAY);
596 // R_ARM_ABS32: (S + A) | T
597 static inline typename This::Status
598 abs32(unsigned char *view,
599 const Sized_relobj<32, big_endian>* object,
600 const Symbol_value<32>* psymval,
603 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
604 Valtype* wv = reinterpret_cast<Valtype*>(view);
605 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
606 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
607 elfcpp::Swap<32, big_endian>::writeval(wv, x);
608 return This::STATUS_OKAY;
611 // R_ARM_REL32: (S + A) | T - P
612 static inline typename This::Status
613 rel32(unsigned char *view,
614 const Sized_relobj<32, big_endian>* object,
615 const Symbol_value<32>* psymval,
616 elfcpp::Elf_types<32>::Elf_Addr address,
619 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
620 Valtype* wv = reinterpret_cast<Valtype*>(view);
621 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
622 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
624 elfcpp::Swap<32, big_endian>::writeval(wv, x);
625 return This::STATUS_OKAY;
628 // R_ARM_THM_CALL: (S + A) | T - P
629 static inline typename This::Status
630 thm_call(unsigned char *view,
631 const Sized_relobj<32, big_endian>* object,
632 const Symbol_value<32>* psymval,
633 elfcpp::Elf_types<32>::Elf_Addr address,
636 // A thumb call consists of two instructions.
637 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
638 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
639 Valtype* wv = reinterpret_cast<Valtype*>(view);
640 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
641 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
642 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
643 gold_assert((lo & 0xf800) == 0xf800);
644 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
645 | ((lo & 0x7ff) << 1));
646 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
649 // If target has no thumb bit set, we need to either turn the BL
650 // into a BLX (for ARMv5 or above) or generate a stub.
653 // This only works for ARMv5 and above with interworking enabled.
656 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
657 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
658 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
659 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
660 return (utils::has_overflow<23>(x)
661 ? This::STATUS_OVERFLOW
662 : This::STATUS_OKAY);
665 // R_ARM_BASE_PREL: B(S) + A - P
666 static inline typename This::Status
667 base_prel(unsigned char* view,
668 elfcpp::Elf_types<32>::Elf_Addr origin,
669 elfcpp::Elf_types<32>::Elf_Addr address)
671 Base::rel32(view, origin - address);
675 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
676 static inline typename This::Status
677 got_brel(unsigned char* view,
678 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
680 Base::rel32(view, got_offset);
681 return This::STATUS_OKAY;
684 // R_ARM_GOT_PREL: GOT(S) + A – P
685 static inline typename This::Status
686 got_prel(unsigned char* view,
687 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
688 elfcpp::Elf_types<32>::Elf_Addr address)
690 Base::rel32(view, got_offset - address);
691 return This::STATUS_OKAY;
694 // R_ARM_PLT32: (S + A) | T - P
695 static inline typename This::Status
696 plt32(unsigned char *view,
697 const Sized_relobj<32, big_endian>* object,
698 const Symbol_value<32>* psymval,
699 elfcpp::Elf_types<32>::Elf_Addr address,
702 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
703 address, has_thumb_bit);
706 // R_ARM_CALL: (S + A) | T - P
707 static inline typename This::Status
708 call(unsigned char *view,
709 const Sized_relobj<32, big_endian>* object,
710 const Symbol_value<32>* psymval,
711 elfcpp::Elf_types<32>::Elf_Addr address,
714 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
715 address, has_thumb_bit);
718 // R_ARM_JUMP24: (S + A) | T - P
719 static inline typename This::Status
720 jump24(unsigned char *view,
721 const Sized_relobj<32, big_endian>* object,
722 const Symbol_value<32>* psymval,
723 elfcpp::Elf_types<32>::Elf_Addr address,
726 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
727 address, has_thumb_bit);
730 // R_ARM_PREL: (S + A) | T - P
731 static inline typename This::Status
732 prel31(unsigned char *view,
733 const Sized_relobj<32, big_endian>* object,
734 const Symbol_value<32>* psymval,
735 elfcpp::Elf_types<32>::Elf_Addr address,
738 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
739 Valtype* wv = reinterpret_cast<Valtype*>(view);
740 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
741 Valtype addend = utils::sign_extend<31>(val);
742 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
744 val = utils::bit_select(val, x, 0x7fffffffU);
745 elfcpp::Swap<32, big_endian>::writeval(wv, val);
746 return (utils::has_overflow<31>(x) ?
747 This::STATUS_OVERFLOW : This::STATUS_OKAY);
751 // Get the GOT section, creating it if necessary.
753 template<bool big_endian>
754 Output_data_got<32, big_endian>*
755 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
757 if (this->got_ == NULL)
759 gold_assert(symtab != NULL && layout != NULL);
761 this->got_ = new Output_data_got<32, big_endian>();
764 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
766 | elfcpp::SHF_WRITE),
770 // The old GNU linker creates a .got.plt section. We just
771 // create another set of data in the .got section. Note that we
772 // always create a PLT if we create a GOT, although the PLT
774 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
775 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
777 | elfcpp::SHF_WRITE),
781 // The first three entries are reserved.
782 this->got_plt_->set_current_data_size(3 * 4);
784 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
785 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
787 0, 0, elfcpp::STT_OBJECT,
789 elfcpp::STV_HIDDEN, 0,
795 // Get the dynamic reloc section, creating it if necessary.
797 template<bool big_endian>
798 typename Target_arm<big_endian>::Reloc_section*
799 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
801 if (this->rel_dyn_ == NULL)
803 gold_assert(layout != NULL);
804 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
805 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
806 elfcpp::SHF_ALLOC, this->rel_dyn_);
808 return this->rel_dyn_;
811 // A class to handle the PLT data.
813 template<bool big_endian>
814 class Output_data_plt_arm : public Output_section_data
817 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
820 Output_data_plt_arm(Layout*, Output_data_space*);
822 // Add an entry to the PLT.
824 add_entry(Symbol* gsym);
826 // Return the .rel.plt section data.
829 { return this->rel_; }
833 do_adjust_output_section(Output_section* os);
835 // Write to a map file.
837 do_print_to_mapfile(Mapfile* mapfile) const
838 { mapfile->print_output_data(this, _("** PLT")); }
841 // Template for the first PLT entry.
842 static const uint32_t first_plt_entry[5];
844 // Template for subsequent PLT entries.
845 static const uint32_t plt_entry[3];
847 // Set the final size.
849 set_final_data_size()
851 this->set_data_size(sizeof(first_plt_entry)
852 + this->count_ * sizeof(plt_entry));
855 // Write out the PLT data.
857 do_write(Output_file*);
859 // The reloc section.
861 // The .got.plt section.
862 Output_data_space* got_plt_;
863 // The number of PLT entries.
867 // Create the PLT section. The ordinary .got section is an argument,
868 // since we need to refer to the start. We also create our own .got
869 // section just for PLT entries.
871 template<bool big_endian>
872 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
873 Output_data_space* got_plt)
874 : Output_section_data(4), got_plt_(got_plt), count_(0)
876 this->rel_ = new Reloc_section(false);
877 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
878 elfcpp::SHF_ALLOC, this->rel_);
881 template<bool big_endian>
883 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
888 // Add an entry to the PLT.
890 template<bool big_endian>
892 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
894 gold_assert(!gsym->has_plt_offset());
896 // Note that when setting the PLT offset we skip the initial
897 // reserved PLT entry.
898 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
899 + sizeof(first_plt_entry));
903 section_offset_type got_offset = this->got_plt_->current_data_size();
905 // Every PLT entry needs a GOT entry which points back to the PLT
906 // entry (this will be changed by the dynamic linker, normally
907 // lazily when the function is called).
908 this->got_plt_->set_current_data_size(got_offset + 4);
910 // Every PLT entry needs a reloc.
911 gsym->set_needs_dynsym_entry();
912 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
915 // Note that we don't need to save the symbol. The contents of the
916 // PLT are independent of which symbols are used. The symbols only
917 // appear in the relocations.
921 // FIXME: This is not very flexible. Right now this has only been tested
922 // on armv5te. If we are to support additional architecture features like
923 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
925 // The first entry in the PLT.
926 template<bool big_endian>
927 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
929 0xe52de004, // str lr, [sp, #-4]!
930 0xe59fe004, // ldr lr, [pc, #4]
931 0xe08fe00e, // add lr, pc, lr
932 0xe5bef008, // ldr pc, [lr, #8]!
933 0x00000000, // &GOT[0] - .
936 // Subsequent entries in the PLT.
938 template<bool big_endian>
939 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
941 0xe28fc600, // add ip, pc, #0xNN00000
942 0xe28cca00, // add ip, ip, #0xNN000
943 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
946 // Write out the PLT. This uses the hand-coded instructions above,
947 // and adjusts them as needed. This is all specified by the arm ELF
948 // Processor Supplement.
950 template<bool big_endian>
952 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
954 const off_t offset = this->offset();
955 const section_size_type oview_size =
956 convert_to_section_size_type(this->data_size());
957 unsigned char* const oview = of->get_output_view(offset, oview_size);
959 const off_t got_file_offset = this->got_plt_->offset();
960 const section_size_type got_size =
961 convert_to_section_size_type(this->got_plt_->data_size());
962 unsigned char* const got_view = of->get_output_view(got_file_offset,
964 unsigned char* pov = oview;
966 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
967 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
969 // Write first PLT entry. All but the last word are constants.
970 const size_t num_first_plt_words = (sizeof(first_plt_entry)
971 / sizeof(plt_entry[0]));
972 for (size_t i = 0; i < num_first_plt_words - 1; i++)
973 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
974 // Last word in first PLT entry is &GOT[0] - .
975 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
976 got_address - (plt_address + 16));
977 pov += sizeof(first_plt_entry);
979 unsigned char* got_pov = got_view;
981 memset(got_pov, 0, 12);
984 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
985 unsigned int plt_offset = sizeof(first_plt_entry);
986 unsigned int plt_rel_offset = 0;
987 unsigned int got_offset = 12;
988 const unsigned int count = this->count_;
989 for (unsigned int i = 0;
992 pov += sizeof(plt_entry),
994 plt_offset += sizeof(plt_entry),
995 plt_rel_offset += rel_size,
998 // Set and adjust the PLT entry itself.
999 int32_t offset = ((got_address + got_offset)
1000 - (plt_address + plt_offset + 8));
1002 gold_assert(offset >= 0 && offset < 0x0fffffff);
1003 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
1004 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
1005 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
1006 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
1007 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
1008 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
1010 // Set the entry in the GOT.
1011 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
1014 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1015 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1017 of->write_output_view(offset, oview_size, oview);
1018 of->write_output_view(got_file_offset, got_size, got_view);
1021 // Create a PLT entry for a global symbol.
1023 template<bool big_endian>
1025 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1028 if (gsym->has_plt_offset())
1031 if (this->plt_ == NULL)
1033 // Create the GOT sections first.
1034 this->got_section(symtab, layout);
1036 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
1037 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1039 | elfcpp::SHF_EXECINSTR),
1042 this->plt_->add_entry(gsym);
1045 // Report an unsupported relocation against a local symbol.
1047 template<bool big_endian>
1049 Target_arm<big_endian>::Scan::unsupported_reloc_local(
1050 Sized_relobj<32, big_endian>* object,
1051 unsigned int r_type)
1053 gold_error(_("%s: unsupported reloc %u against local symbol"),
1054 object->name().c_str(), r_type);
1057 // We are about to emit a dynamic relocation of type R_TYPE. If the
1058 // dynamic linker does not support it, issue an error. The GNU linker
1059 // only issues a non-PIC error for an allocated read-only section.
1060 // Here we know the section is allocated, but we don't know that it is
1061 // read-only. But we check for all the relocation types which the
1062 // glibc dynamic linker supports, so it seems appropriate to issue an
1063 // error even if the section is not read-only.
1065 template<bool big_endian>
1067 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
1068 unsigned int r_type)
1072 // These are the relocation types supported by glibc for ARM.
1073 case elfcpp::R_ARM_RELATIVE:
1074 case elfcpp::R_ARM_COPY:
1075 case elfcpp::R_ARM_GLOB_DAT:
1076 case elfcpp::R_ARM_JUMP_SLOT:
1077 case elfcpp::R_ARM_ABS32:
1078 case elfcpp::R_ARM_PC24:
1079 // FIXME: The following 3 types are not supported by Android's dynamic
1081 case elfcpp::R_ARM_TLS_DTPMOD32:
1082 case elfcpp::R_ARM_TLS_DTPOFF32:
1083 case elfcpp::R_ARM_TLS_TPOFF32:
1087 // This prevents us from issuing more than one error per reloc
1088 // section. But we can still wind up issuing more than one
1089 // error per object file.
1090 if (this->issued_non_pic_error_)
1092 object->error(_("requires unsupported dynamic reloc; "
1093 "recompile with -fPIC"));
1094 this->issued_non_pic_error_ = true;
1097 case elfcpp::R_ARM_NONE:
1102 // Scan a relocation for a local symbol.
1103 // FIXME: This only handles a subset of relocation types used by Android
1104 // on ARM v5te devices.
1106 template<bool big_endian>
1108 Target_arm<big_endian>::Scan::local(const General_options&,
1109 Symbol_table* symtab,
1112 Sized_relobj<32, big_endian>* object,
1113 unsigned int data_shndx,
1114 Output_section* output_section,
1115 const elfcpp::Rel<32, big_endian>& reloc,
1116 unsigned int r_type,
1117 const elfcpp::Sym<32, big_endian>&)
1119 r_type = get_real_reloc_type(r_type);
1122 case elfcpp::R_ARM_NONE:
1125 case elfcpp::R_ARM_ABS8:
1126 if (parameters->options().output_is_position_independent())
1128 // FIXME: Create a dynamic relocation for this location.
1129 gold_error(_("%s: gold bug: need dynamic ABS8 reloc"),
1130 object->name().c_str());
1134 case elfcpp::R_ARM_ABS32:
1135 // If building a shared library (or a position-independent
1136 // executable), we need to create a dynamic relocation for
1137 // this location. The relocation applied at link time will
1138 // apply the link-time value, so we flag the location with
1139 // an R_ARM_RELATIVE relocation so the dynamic loader can
1140 // relocate it easily.
1141 if (parameters->options().output_is_position_independent())
1143 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1144 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1145 // If we are to add more other reloc types than R_ARM_ABS32,
1146 // we need to add check_non_pic(object, r_type) here.
1147 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
1148 output_section, data_shndx,
1149 reloc.get_r_offset());
1153 case elfcpp::R_ARM_REL32:
1154 case elfcpp::R_ARM_THM_CALL:
1155 case elfcpp::R_ARM_CALL:
1156 case elfcpp::R_ARM_PREL31:
1157 case elfcpp::R_ARM_JUMP24:
1158 case elfcpp::R_ARM_PLT32:
1161 case elfcpp::R_ARM_GOTOFF32:
1162 // We need a GOT section:
1163 target->got_section(symtab, layout);
1166 case elfcpp::R_ARM_BASE_PREL:
1167 // FIXME: What about this?
1170 case elfcpp::R_ARM_GOT_BREL:
1171 case elfcpp::R_ARM_GOT_PREL:
1173 // The symbol requires a GOT entry.
1174 Output_data_got<32, big_endian>* got =
1175 target->got_section(symtab, layout);
1176 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1177 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1179 // If we are generating a shared object, we need to add a
1180 // dynamic RELATIVE relocation for this symbol's GOT entry.
1181 if (parameters->options().output_is_position_independent())
1183 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1184 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1185 rel_dyn->add_local_relative(
1186 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
1187 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1193 case elfcpp::R_ARM_TARGET1:
1194 // This should have been mapped to another type already.
1196 case elfcpp::R_ARM_COPY:
1197 case elfcpp::R_ARM_GLOB_DAT:
1198 case elfcpp::R_ARM_JUMP_SLOT:
1199 case elfcpp::R_ARM_RELATIVE:
1200 // These are relocations which should only be seen by the
1201 // dynamic linker, and should never be seen here.
1202 gold_error(_("%s: unexpected reloc %u in object file"),
1203 object->name().c_str(), r_type);
1207 unsupported_reloc_local(object, r_type);
1212 // Report an unsupported relocation against a global symbol.
1214 template<bool big_endian>
1216 Target_arm<big_endian>::Scan::unsupported_reloc_global(
1217 Sized_relobj<32, big_endian>* object,
1218 unsigned int r_type,
1221 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1222 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1225 // Scan a relocation for a global symbol.
1226 // FIXME: This only handles a subset of relocation types used by Android
1227 // on ARM v5te devices.
1229 template<bool big_endian>
1231 Target_arm<big_endian>::Scan::global(const General_options&,
1232 Symbol_table* symtab,
1235 Sized_relobj<32, big_endian>* object,
1236 unsigned int data_shndx,
1237 Output_section* output_section,
1238 const elfcpp::Rel<32, big_endian>& reloc,
1239 unsigned int r_type,
1242 r_type = get_real_reloc_type(r_type);
1245 case elfcpp::R_ARM_NONE:
1248 case elfcpp::R_ARM_ABS8:
1249 // Make a dynamic relocation if necessary.
1250 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1252 // FIXME: Create a dynamic relocation for this location.
1253 gold_error(_("%s: gold bug: need dynamic ABS8 reloc for %s"),
1254 object->name().c_str(), gsym->demangled_name().c_str());
1258 case elfcpp::R_ARM_ABS32:
1260 // Make a dynamic relocation if necessary.
1261 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1263 if (target->may_need_copy_reloc(gsym))
1265 target->copy_reloc(symtab, layout, object,
1266 data_shndx, output_section, gsym, reloc);
1268 else if (gsym->can_use_relative_reloc(false))
1270 // If we are to add more other reloc types than R_ARM_ABS32,
1271 // we need to add check_non_pic(object, r_type) here.
1272 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1273 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
1274 output_section, object,
1275 data_shndx, reloc.get_r_offset());
1279 // If we are to add more other reloc types than R_ARM_ABS32,
1280 // we need to add check_non_pic(object, r_type) here.
1281 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1282 rel_dyn->add_global(gsym, r_type, output_section, object,
1283 data_shndx, reloc.get_r_offset());
1289 case elfcpp::R_ARM_REL32:
1290 case elfcpp::R_ARM_PREL31:
1292 // Make a dynamic relocation if necessary.
1293 int flags = Symbol::NON_PIC_REF;
1294 if (gsym->needs_dynamic_reloc(flags))
1296 if (target->may_need_copy_reloc(gsym))
1298 target->copy_reloc(symtab, layout, object,
1299 data_shndx, output_section, gsym, reloc);
1303 check_non_pic(object, r_type);
1304 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1305 rel_dyn->add_global(gsym, r_type, output_section, object,
1306 data_shndx, reloc.get_r_offset());
1312 case elfcpp::R_ARM_JUMP24:
1313 case elfcpp::R_ARM_THM_CALL:
1314 case elfcpp::R_ARM_CALL:
1316 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
1317 target->make_plt_entry(symtab, layout, gsym);
1318 // Make a dynamic relocation if necessary.
1319 int flags = Symbol::NON_PIC_REF;
1320 if (gsym->type() == elfcpp::STT_FUNC
1321 || gsym->type() == elfcpp::STT_ARM_TFUNC)
1322 flags |= Symbol::FUNCTION_CALL;
1323 if (gsym->needs_dynamic_reloc(flags))
1325 if (target->may_need_copy_reloc(gsym))
1327 target->copy_reloc(symtab, layout, object,
1328 data_shndx, output_section, gsym,
1333 check_non_pic(object, r_type);
1334 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1335 rel_dyn->add_global(gsym, r_type, output_section, object,
1336 data_shndx, reloc.get_r_offset());
1342 case elfcpp::R_ARM_PLT32:
1343 // If the symbol is fully resolved, this is just a relative
1344 // local reloc. Otherwise we need a PLT entry.
1345 if (gsym->final_value_is_known())
1347 // If building a shared library, we can also skip the PLT entry
1348 // if the symbol is defined in the output file and is protected
1350 if (gsym->is_defined()
1351 && !gsym->is_from_dynobj()
1352 && !gsym->is_preemptible())
1354 target->make_plt_entry(symtab, layout, gsym);
1357 case elfcpp::R_ARM_GOTOFF32:
1358 // We need a GOT section.
1359 target->got_section(symtab, layout);
1362 case elfcpp::R_ARM_BASE_PREL:
1363 // FIXME: What about this?
1366 case elfcpp::R_ARM_GOT_BREL:
1367 case elfcpp::R_ARM_GOT_PREL:
1369 // The symbol requires a GOT entry.
1370 Output_data_got<32, big_endian>* got =
1371 target->got_section(symtab, layout);
1372 if (gsym->final_value_is_known())
1373 got->add_global(gsym, GOT_TYPE_STANDARD);
1376 // If this symbol is not fully resolved, we need to add a
1377 // GOT entry with a dynamic relocation.
1378 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1379 if (gsym->is_from_dynobj()
1380 || gsym->is_undefined()
1381 || gsym->is_preemptible())
1382 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1383 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
1386 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1387 rel_dyn->add_global_relative(
1388 gsym, elfcpp::R_ARM_RELATIVE, got,
1389 gsym->got_offset(GOT_TYPE_STANDARD));
1395 case elfcpp::R_ARM_TARGET1:
1396 // This should have been mapped to another type already.
1398 case elfcpp::R_ARM_COPY:
1399 case elfcpp::R_ARM_GLOB_DAT:
1400 case elfcpp::R_ARM_JUMP_SLOT:
1401 case elfcpp::R_ARM_RELATIVE:
1402 // These are relocations which should only be seen by the
1403 // dynamic linker, and should never be seen here.
1404 gold_error(_("%s: unexpected reloc %u in object file"),
1405 object->name().c_str(), r_type);
1409 unsupported_reloc_global(object, r_type, gsym);
1414 // Process relocations for gc.
1416 template<bool big_endian>
1418 Target_arm<big_endian>::gc_process_relocs(const General_options& options,
1419 Symbol_table* symtab,
1421 Sized_relobj<32, big_endian>* object,
1422 unsigned int data_shndx,
1424 const unsigned char* prelocs,
1426 Output_section* output_section,
1427 bool needs_special_offset_handling,
1428 size_t local_symbol_count,
1429 const unsigned char* plocal_symbols)
1431 typedef Target_arm<big_endian> Arm;
1432 typedef typename Target_arm<big_endian>::Scan Scan;
1434 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
1444 needs_special_offset_handling,
1449 // Scan relocations for a section.
1451 template<bool big_endian>
1453 Target_arm<big_endian>::scan_relocs(const General_options& options,
1454 Symbol_table* symtab,
1456 Sized_relobj<32, big_endian>* object,
1457 unsigned int data_shndx,
1458 unsigned int sh_type,
1459 const unsigned char* prelocs,
1461 Output_section* output_section,
1462 bool needs_special_offset_handling,
1463 size_t local_symbol_count,
1464 const unsigned char* plocal_symbols)
1466 typedef typename Target_arm<big_endian>::Scan Scan;
1467 if (sh_type == elfcpp::SHT_RELA)
1469 gold_error(_("%s: unsupported RELA reloc section"),
1470 object->name().c_str());
1474 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
1484 needs_special_offset_handling,
1489 // Finalize the sections.
1491 template<bool big_endian>
1493 Target_arm<big_endian>::do_finalize_sections(Layout* layout)
1495 // Fill in some more dynamic tags.
1496 Output_data_dynamic* const odyn = layout->dynamic_data();
1499 if (this->got_plt_ != NULL)
1500 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1502 if (this->plt_ != NULL)
1504 const Output_data* od = this->plt_->rel_plt();
1505 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1506 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1507 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1510 if (this->rel_dyn_ != NULL)
1512 const Output_data* od = this->rel_dyn_;
1513 odyn->add_section_address(elfcpp::DT_REL, od);
1514 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1515 odyn->add_constant(elfcpp::DT_RELENT,
1516 elfcpp::Elf_sizes<32>::rel_size);
1519 if (!parameters->options().shared())
1521 // The value of the DT_DEBUG tag is filled in by the dynamic
1522 // linker at run time, and used by the debugger.
1523 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1527 // Emit any relocs we saved in an attempt to avoid generating COPY
1529 if (this->copy_relocs_.any_saved_relocs())
1530 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1532 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
1533 // the .ARM.exidx section.
1534 if (!layout->script_options()->saw_phdrs_clause()
1535 && !parameters->options().relocatable())
1537 Output_section* exidx_section =
1538 layout->find_output_section(".ARM.exidx");
1540 if (exidx_section != NULL
1541 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
1543 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
1545 Output_segment* exidx_segment =
1546 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
1547 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
1552 // Return whether a direct absolute static relocation needs to be applied.
1553 // In cases where Scan::local() or Scan::global() has created
1554 // a dynamic relocation other than R_ARM_RELATIVE, the addend
1555 // of the relocation is carried in the data, and we must not
1556 // apply the static relocation.
1558 template<bool big_endian>
1560 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
1561 const Sized_symbol<32>* gsym,
1564 Output_section* output_section)
1566 // If the output section is not allocated, then we didn't call
1567 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1569 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1572 // For local symbols, we will have created a non-RELATIVE dynamic
1573 // relocation only if (a) the output is position independent,
1574 // (b) the relocation is absolute (not pc- or segment-relative), and
1575 // (c) the relocation is not 32 bits wide.
1577 return !(parameters->options().output_is_position_independent()
1578 && (ref_flags & Symbol::ABSOLUTE_REF)
1581 // For global symbols, we use the same helper routines used in the
1582 // scan pass. If we did not create a dynamic relocation, or if we
1583 // created a RELATIVE dynamic relocation, we should apply the static
1585 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1586 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1587 && gsym->can_use_relative_reloc(ref_flags
1588 & Symbol::FUNCTION_CALL);
1589 return !has_dyn || is_rel;
1592 // Perform a relocation.
1594 template<bool big_endian>
1596 Target_arm<big_endian>::Relocate::relocate(
1597 const Relocate_info<32, big_endian>* relinfo,
1599 Output_section *output_section,
1601 const elfcpp::Rel<32, big_endian>& rel,
1602 unsigned int r_type,
1603 const Sized_symbol<32>* gsym,
1604 const Symbol_value<32>* psymval,
1605 unsigned char* view,
1606 elfcpp::Elf_types<32>::Elf_Addr address,
1607 section_size_type /* view_size */ )
1609 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
1611 r_type = get_real_reloc_type(r_type);
1613 // If this the symbol may be a Thumb function, set thumb bit to 1.
1614 bool has_thumb_bit = ((gsym != NULL)
1615 && (gsym->type() == elfcpp::STT_FUNC
1616 || gsym->type() == elfcpp::STT_ARM_TFUNC));
1618 // Pick the value to use for symbols defined in shared objects.
1619 Symbol_value<32> symval;
1621 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
1623 symval.set_output_value(target->plt_section()->address()
1624 + gsym->plt_offset());
1629 const Sized_relobj<32, big_endian>* object = relinfo->object;
1631 // Get the GOT offset if needed.
1632 // The GOT pointer points to the end of the GOT section.
1633 // We need to subtract the size of the GOT section to get
1634 // the actual offset to use in the relocation.
1635 bool have_got_offset = false;
1636 unsigned int got_offset = 0;
1639 case elfcpp::R_ARM_GOT_BREL:
1640 case elfcpp::R_ARM_GOT_PREL:
1643 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1644 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1645 - target->got_size());
1649 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1650 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1651 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1652 - target->got_size());
1654 have_got_offset = true;
1661 typename Arm_relocate_functions::Status reloc_status =
1662 Arm_relocate_functions::STATUS_OKAY;
1665 case elfcpp::R_ARM_NONE:
1668 case elfcpp::R_ARM_ABS8:
1669 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1671 reloc_status = Arm_relocate_functions::abs8(view, object, psymval,
1675 case elfcpp::R_ARM_ABS32:
1676 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1678 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
1682 case elfcpp::R_ARM_REL32:
1683 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1684 address, has_thumb_bit);
1687 case elfcpp::R_ARM_THM_CALL:
1688 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
1689 address, has_thumb_bit);
1692 case elfcpp::R_ARM_GOTOFF32:
1694 elfcpp::Elf_types<32>::Elf_Addr got_origin;
1695 got_origin = target->got_plt_section()->address();
1696 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1697 got_origin, has_thumb_bit);
1701 case elfcpp::R_ARM_BASE_PREL:
1704 // Get the addressing origin of the output segment defining the
1705 // symbol gsym (AAELF 4.6.1.2 Relocation types)
1706 gold_assert(gsym != NULL);
1707 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
1708 origin = gsym->output_segment()->vaddr();
1709 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
1710 origin = gsym->output_data()->address();
1713 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1714 _("cannot find origin of R_ARM_BASE_PREL"));
1717 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
1721 case elfcpp::R_ARM_GOT_BREL:
1722 gold_assert(have_got_offset);
1723 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
1726 case elfcpp::R_ARM_GOT_PREL:
1727 gold_assert(have_got_offset);
1728 // Get the address origin for GOT PLT, which is allocated right
1729 // after the GOT section, to calculate an absolute address of
1730 // the symbol GOT entry (got_origin + got_offset).
1731 elfcpp::Elf_types<32>::Elf_Addr got_origin;
1732 got_origin = target->got_plt_section()->address();
1733 reloc_status = Arm_relocate_functions::got_prel(view,
1734 got_origin + got_offset,
1738 case elfcpp::R_ARM_PLT32:
1739 gold_assert(gsym == NULL
1740 || gsym->has_plt_offset()
1741 || gsym->final_value_is_known()
1742 || (gsym->is_defined()
1743 && !gsym->is_from_dynobj()
1744 && !gsym->is_preemptible()));
1745 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
1746 address, has_thumb_bit);
1749 case elfcpp::R_ARM_CALL:
1750 reloc_status = Arm_relocate_functions::call(view, object, psymval,
1751 address, has_thumb_bit);
1754 case elfcpp::R_ARM_JUMP24:
1755 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
1756 address, has_thumb_bit);
1759 case elfcpp::R_ARM_PREL31:
1760 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
1761 address, has_thumb_bit);
1764 case elfcpp::R_ARM_TARGET1:
1765 // This should have been mapped to another type already.
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_at_location(relinfo, relnum, rel.get_r_offset(),
1774 _("unexpected reloc %u in object file"),
1779 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1780 _("unsupported reloc %u"),
1785 // Report any errors.
1786 switch (reloc_status)
1788 case Arm_relocate_functions::STATUS_OKAY:
1790 case Arm_relocate_functions::STATUS_OVERFLOW:
1791 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1792 _("relocation overflow in relocation %u"),
1795 case Arm_relocate_functions::STATUS_BAD_RELOC:
1796 gold_error_at_location(
1800 _("unexpected opcode while processing relocation %u"),
1810 // Relocate section data.
1812 template<bool big_endian>
1814 Target_arm<big_endian>::relocate_section(
1815 const Relocate_info<32, big_endian>* relinfo,
1816 unsigned int sh_type,
1817 const unsigned char* prelocs,
1819 Output_section* output_section,
1820 bool needs_special_offset_handling,
1821 unsigned char* view,
1822 elfcpp::Elf_types<32>::Elf_Addr address,
1823 section_size_type view_size,
1824 const Reloc_symbol_changes* reloc_symbol_changes)
1826 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
1827 gold_assert(sh_type == elfcpp::SHT_REL);
1829 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
1836 needs_special_offset_handling,
1840 reloc_symbol_changes);
1843 // Return the size of a relocation while scanning during a relocatable
1846 template<bool big_endian>
1848 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1849 unsigned int r_type,
1852 r_type = get_real_reloc_type(r_type);
1855 case elfcpp::R_ARM_NONE:
1858 case elfcpp::R_ARM_ABS8:
1861 case elfcpp::R_ARM_ABS32:
1862 case elfcpp::R_ARM_REL32:
1863 case elfcpp::R_ARM_THM_CALL:
1864 case elfcpp::R_ARM_GOTOFF32:
1865 case elfcpp::R_ARM_BASE_PREL:
1866 case elfcpp::R_ARM_GOT_BREL:
1867 case elfcpp::R_ARM_GOT_PREL:
1868 case elfcpp::R_ARM_PLT32:
1869 case elfcpp::R_ARM_CALL:
1870 case elfcpp::R_ARM_JUMP24:
1871 case elfcpp::R_ARM_PREL31:
1874 case elfcpp::R_ARM_TARGET1:
1875 // This should have been mapped to another type already.
1877 case elfcpp::R_ARM_COPY:
1878 case elfcpp::R_ARM_GLOB_DAT:
1879 case elfcpp::R_ARM_JUMP_SLOT:
1880 case elfcpp::R_ARM_RELATIVE:
1881 // These are relocations which should only be seen by the
1882 // dynamic linker, and should never be seen here.
1883 gold_error(_("%s: unexpected reloc %u in object file"),
1884 object->name().c_str(), r_type);
1888 object->error(_("unsupported reloc %u in object file"), r_type);
1893 // Scan the relocs during a relocatable link.
1895 template<bool big_endian>
1897 Target_arm<big_endian>::scan_relocatable_relocs(
1898 const General_options& options,
1899 Symbol_table* symtab,
1901 Sized_relobj<32, big_endian>* object,
1902 unsigned int data_shndx,
1903 unsigned int sh_type,
1904 const unsigned char* prelocs,
1906 Output_section* output_section,
1907 bool needs_special_offset_handling,
1908 size_t local_symbol_count,
1909 const unsigned char* plocal_symbols,
1910 Relocatable_relocs* rr)
1912 gold_assert(sh_type == elfcpp::SHT_REL);
1914 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
1915 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1917 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
1918 Scan_relocatable_relocs>(
1927 needs_special_offset_handling,
1933 // Relocate a section during a relocatable link.
1935 template<bool big_endian>
1937 Target_arm<big_endian>::relocate_for_relocatable(
1938 const Relocate_info<32, big_endian>* relinfo,
1939 unsigned int sh_type,
1940 const unsigned char* prelocs,
1942 Output_section* output_section,
1943 off_t offset_in_output_section,
1944 const Relocatable_relocs* rr,
1945 unsigned char* view,
1946 elfcpp::Elf_types<32>::Elf_Addr view_address,
1947 section_size_type view_size,
1948 unsigned char* reloc_view,
1949 section_size_type reloc_view_size)
1951 gold_assert(sh_type == elfcpp::SHT_REL);
1953 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
1958 offset_in_output_section,
1967 // Return the value to use for a dynamic symbol which requires special
1968 // treatment. This is how we support equality comparisons of function
1969 // pointers across shared library boundaries, as described in the
1970 // processor specific ABI supplement.
1972 template<bool big_endian>
1974 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
1976 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1977 return this->plt_section()->address() + gsym->plt_offset();
1980 // Map platform-specific relocs to real relocs
1982 template<bool big_endian>
1984 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
1988 case elfcpp::R_ARM_TARGET1:
1989 // This is either R_ARM_ABS32 or R_ARM_REL32;
1990 return elfcpp::R_ARM_ABS32;
1992 case elfcpp::R_ARM_TARGET2:
1993 // This can be any reloc type but ususally is R_ARM_GOT_PREL
1994 return elfcpp::R_ARM_GOT_PREL;
2001 // The selector for arm object files.
2003 template<bool big_endian>
2004 class Target_selector_arm : public Target_selector
2007 Target_selector_arm()
2008 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
2009 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
2013 do_instantiate_target()
2014 { return new Target_arm<big_endian>(); }
2017 Target_selector_arm<false> target_selector_arm;
2018 Target_selector_arm<true> target_selector_armbe;
2020 } // End anonymous namespace.