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>.
6 // This file also contains borrowed and adapted code from
9 // This file is part of gold.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
34 #include "parameters.h"
41 #include "copy-relocs.h"
43 #include "target-reloc.h"
44 #include "target-select.h"
54 template<bool big_endian>
55 class Output_data_plt_arm;
57 template<bool big_endian>
61 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
63 // Maximum branch offsets for ARM, THUMB and THUMB2.
64 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
65 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
66 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
67 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
68 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
69 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
71 // The arm target class.
73 // This is a very simple port of gold for ARM-EABI. It is intended for
74 // supporting Android only for the time being. Only these relocation types
103 // R_ARM_THM_MOVW_ABS_NC
104 // R_ARM_THM_MOVT_ABS
105 // R_ARM_MOVW_PREL_NC
107 // R_ARM_THM_MOVW_PREL_NC
108 // R_ARM_THM_MOVT_PREL
111 // - Generate various branch stubs.
112 // - Support interworking.
113 // - Define section symbols __exidx_start and __exidx_stop.
114 // - Support more relocation types as needed.
115 // - Make PLTs more flexible for different architecture features like
117 // There are probably a lot more.
119 // Instruction template class. This class is similar to the insn_sequence
120 // struct in bfd/elf32-arm.c.
125 // Types of instruction templates.
134 // Factory methods to create instrunction templates in different formats.
136 static const Insn_template
137 thumb16_insn(uint32_t data)
138 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
140 // A bit of a hack. A Thumb conditional branch, in which the proper
141 // condition is inserted when we build the stub.
142 static const Insn_template
143 thumb16_bcond_insn(uint32_t data)
144 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 1); }
146 static const Insn_template
147 thumb32_insn(uint32_t data)
148 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
150 static const Insn_template
151 thumb32_b_insn(uint32_t data, int reloc_addend)
153 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
157 static const Insn_template
158 arm_insn(uint32_t data)
159 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
161 static const Insn_template
162 arm_rel_insn(unsigned data, int reloc_addend)
163 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
165 static const Insn_template
166 data_word(unsigned data, unsigned int r_type, int reloc_addend)
167 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
169 // Accessors. This class is used for read-only objects so no modifiers
174 { return this->data_; }
176 // Return the instruction sequence type of this.
179 { return this->type_; }
181 // Return the ARM relocation type of this.
184 { return this->r_type_; }
188 { return this->reloc_addend_; }
190 // Return size of instrunction template in bytes.
194 // Return byte-alignment of instrunction template.
199 // We make the constructor private to ensure that only the factory
202 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
203 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
206 // Instruction specific data. This is used to store information like
207 // some of the instruction bits.
209 // Instruction template type.
211 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
212 unsigned int r_type_;
213 // Relocation addend.
214 int32_t reloc_addend_;
217 // Macro for generating code to stub types. One entry per long/short
221 DEF_STUB(long_branch_any_any) \
222 DEF_STUB(long_branch_v4t_arm_thumb) \
223 DEF_STUB(long_branch_thumb_only) \
224 DEF_STUB(long_branch_v4t_thumb_thumb) \
225 DEF_STUB(long_branch_v4t_thumb_arm) \
226 DEF_STUB(short_branch_v4t_thumb_arm) \
227 DEF_STUB(long_branch_any_arm_pic) \
228 DEF_STUB(long_branch_any_thumb_pic) \
229 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
230 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
231 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
232 DEF_STUB(long_branch_thumb_only_pic) \
233 DEF_STUB(a8_veneer_b_cond) \
234 DEF_STUB(a8_veneer_b) \
235 DEF_STUB(a8_veneer_bl) \
236 DEF_STUB(a8_veneer_blx)
240 #define DEF_STUB(x) arm_stub_##x,
246 // First reloc stub type.
247 arm_stub_reloc_first = arm_stub_long_branch_any_any,
248 // Last reloc stub type.
249 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
251 // First Cortex-A8 stub type.
252 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
253 // Last Cortex-A8 stub type.
254 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
257 arm_stub_type_last = arm_stub_a8_veneer_blx
261 // Stub template class. Templates are meant to be read-only objects.
262 // A stub template for a stub type contains all read-only attributes
263 // common to all stubs of the same type.
268 Stub_template(Stub_type, const Insn_template*, size_t);
276 { return this->type_; }
278 // Return an array of instruction templates.
281 { return this->insns_; }
283 // Return size of template in number of instructions.
286 { return this->insn_count_; }
288 // Return size of template in bytes.
291 { return this->size_; }
293 // Return alignment of the stub template.
296 { return this->alignment_; }
298 // Return whether entry point is in thumb mode.
300 entry_in_thumb_mode() const
301 { return this->entry_in_thumb_mode_; }
303 // Return number of relocations in this template.
306 { return this->relocs_.size(); }
308 // Return index of the I-th instruction with relocation.
310 reloc_insn_index(size_t i) const
312 gold_assert(i < this->relocs_.size());
313 return this->relocs_[i].first;
316 // Return the offset of the I-th instruction with relocation from the
317 // beginning of the stub.
319 reloc_offset(size_t i) const
321 gold_assert(i < this->relocs_.size());
322 return this->relocs_[i].second;
326 // This contains information about an instruction template with a relocation
327 // and its offset from start of stub.
328 typedef std::pair<size_t, section_size_type> Reloc;
330 // A Stub_template may not be copied. We want to share templates as much
332 Stub_template(const Stub_template&);
333 Stub_template& operator=(const Stub_template&);
337 // Points to an array of Insn_templates.
338 const Insn_template* insns_;
339 // Number of Insn_templates in insns_[].
341 // Size of templated instructions in bytes.
343 // Alignment of templated instructions.
345 // Flag to indicate if entry is in thumb mode.
346 bool entry_in_thumb_mode_;
347 // A table of reloc instruction indices and offsets. We can find these by
348 // looking at the instruction templates but we pre-compute and then stash
349 // them here for speed.
350 std::vector<Reloc> relocs_;
354 // A class for code stubs. This is a base class for different type of
355 // stubs used in the ARM target.
361 static const section_offset_type invalid_offset =
362 static_cast<section_offset_type>(-1);
365 Stub(const Stub_template* stub_template)
366 : stub_template_(stub_template), offset_(invalid_offset)
373 // Return the stub template.
375 stub_template() const
376 { return this->stub_template_; }
378 // Return offset of code stub from beginning of its containing stub table.
382 gold_assert(this->offset_ != invalid_offset);
383 return this->offset_;
386 // Set offset of code stub from beginning of its containing stub table.
388 set_offset(section_offset_type offset)
389 { this->offset_ = offset; }
391 // Return the relocation target address of the i-th relocation in the
392 // stub. This must be defined in a child class.
394 reloc_target(size_t i)
395 { return this->do_reloc_target(i); }
397 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
399 write(unsigned char* view, section_size_type view_size, bool big_endian)
400 { this->do_write(view, view_size, big_endian); }
403 // This must be defined in the child class.
405 do_reloc_target(size_t) = 0;
407 // This must be defined in the child class.
409 do_write(unsigned char*, section_size_type, bool) = 0;
413 const Stub_template* stub_template_;
414 // Offset within the section of containing this stub.
415 section_offset_type offset_;
418 // Reloc stub class. These are stubs we use to fix up relocation because
419 // of limited branch ranges.
421 class Reloc_stub : public Stub
424 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
425 // We assume we never jump to this address.
426 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
428 // Return destination address.
430 destination_address() const
432 gold_assert(this->destination_address_ != this->invalid_address);
433 return this->destination_address_;
436 // Set destination address.
438 set_destination_address(Arm_address address)
440 gold_assert(address != this->invalid_address);
441 this->destination_address_ = address;
444 // Reset destination address.
446 reset_destination_address()
447 { this->destination_address_ = this->invalid_address; }
449 // Determine stub type for a branch of a relocation of R_TYPE going
450 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
451 // the branch target is a thumb instruction. TARGET is used for look
452 // up ARM-specific linker settings.
454 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
455 Arm_address branch_target, bool target_is_thumb);
457 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
458 // and an addend. Since we treat global and local symbol differently, we
459 // use a Symbol object for a global symbol and a object-index pair for
464 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
465 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
466 // and R_SYM must not be invalid_index.
467 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
468 unsigned int r_sym, int32_t addend)
469 : stub_type_(stub_type), addend_(addend)
473 this->r_sym_ = Reloc_stub::invalid_index;
474 this->u_.symbol = symbol;
478 gold_assert(relobj != NULL && r_sym != invalid_index);
479 this->r_sym_ = r_sym;
480 this->u_.relobj = relobj;
487 // Accessors: Keys are meant to be read-only object so no modifiers are
493 { return this->stub_type_; }
495 // Return the local symbol index or invalid_index.
498 { return this->r_sym_; }
500 // Return the symbol if there is one.
503 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
505 // Return the relobj if there is one.
508 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
510 // Whether this equals to another key k.
512 eq(const Key& k) const
514 return ((this->stub_type_ == k.stub_type_)
515 && (this->r_sym_ == k.r_sym_)
516 && ((this->r_sym_ != Reloc_stub::invalid_index)
517 ? (this->u_.relobj == k.u_.relobj)
518 : (this->u_.symbol == k.u_.symbol))
519 && (this->addend_ == k.addend_));
522 // Return a hash value.
526 return (this->stub_type_
528 ^ gold::string_hash<char>(
529 (this->r_sym_ != Reloc_stub::invalid_index)
530 ? this->u_.relobj->name().c_str()
531 : this->u_.symbol->name())
535 // Functors for STL associative containers.
539 operator()(const Key& k) const
540 { return k.hash_value(); }
546 operator()(const Key& k1, const Key& k2) const
547 { return k1.eq(k2); }
550 // Name of key. This is mainly for debugging.
556 Stub_type stub_type_;
557 // If this is a local symbol, this is the index in the defining object.
558 // Otherwise, it is invalid_index for a global symbol.
560 // If r_sym_ is invalid index. This points to a global symbol.
561 // Otherwise, this points a relobj. We used the unsized and target
562 // independent Symbol and Relobj classes instead of Arm_symbol and
563 // Arm_relobj. This is done to avoid making the stub class a template
564 // as most of the stub machinery is endianity-neutral. However, it
565 // may require a bit of casting done by users of this class.
568 const Symbol* symbol;
569 const Relobj* relobj;
571 // Addend associated with a reloc.
576 // Reloc_stubs are created via a stub factory. So these are protected.
577 Reloc_stub(const Stub_template* stub_template)
578 : Stub(stub_template), destination_address_(invalid_address)
584 friend class Stub_factory;
587 // Return the relocation target address of the i-th relocation in the
590 do_reloc_target(size_t i)
592 // All reloc stub have only one relocation.
594 return this->destination_address_;
597 // A template to implement do_write below.
598 template<bool big_endian>
600 do_fixed_endian_write(unsigned char*, section_size_type);
604 do_write(unsigned char* view, section_size_type view_size, bool big_endian);
606 // Address of destination.
607 Arm_address destination_address_;
610 // Stub factory class.
615 // Return the unique instance of this class.
616 static const Stub_factory&
619 static Stub_factory singleton;
623 // Make a relocation stub.
625 make_reloc_stub(Stub_type stub_type) const
627 gold_assert(stub_type >= arm_stub_reloc_first
628 && stub_type <= arm_stub_reloc_last);
629 return new Reloc_stub(this->stub_templates_[stub_type]);
633 // Constructor and destructor are protected since we only return a single
634 // instance created in Stub_factory::get_instance().
638 // A Stub_factory may not be copied since it is a singleton.
639 Stub_factory(const Stub_factory&);
640 Stub_factory& operator=(Stub_factory&);
642 // Stub templates. These are initialized in the constructor.
643 const Stub_template* stub_templates_[arm_stub_type_last+1];
646 // Utilities for manipulating integers of up to 32-bits
650 // Sign extend an n-bit unsigned integer stored in an uint32_t into
651 // an int32_t. NO_BITS must be between 1 to 32.
652 template<int no_bits>
653 static inline int32_t
654 sign_extend(uint32_t bits)
656 gold_assert(no_bits >= 0 && no_bits <= 32);
658 return static_cast<int32_t>(bits);
659 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
661 uint32_t top_bit = 1U << (no_bits - 1);
662 int32_t as_signed = static_cast<int32_t>(bits);
663 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
666 // Detects overflow of an NO_BITS integer stored in a uint32_t.
667 template<int no_bits>
669 has_overflow(uint32_t bits)
671 gold_assert(no_bits >= 0 && no_bits <= 32);
674 int32_t max = (1 << (no_bits - 1)) - 1;
675 int32_t min = -(1 << (no_bits - 1));
676 int32_t as_signed = static_cast<int32_t>(bits);
677 return as_signed > max || as_signed < min;
680 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
681 // fits in the given number of bits as either a signed or unsigned value.
682 // For example, has_signed_unsigned_overflow<8> would check
683 // -128 <= bits <= 255
684 template<int no_bits>
686 has_signed_unsigned_overflow(uint32_t bits)
688 gold_assert(no_bits >= 2 && no_bits <= 32);
691 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
692 int32_t min = -(1 << (no_bits - 1));
693 int32_t as_signed = static_cast<int32_t>(bits);
694 return as_signed > max || as_signed < min;
697 // Select bits from A and B using bits in MASK. For each n in [0..31],
698 // the n-th bit in the result is chosen from the n-th bits of A and B.
699 // A zero selects A and a one selects B.
700 static inline uint32_t
701 bit_select(uint32_t a, uint32_t b, uint32_t mask)
702 { return (a & ~mask) | (b & mask); }
705 template<bool big_endian>
706 class Target_arm : public Sized_target<32, big_endian>
709 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
713 : Sized_target<32, big_endian>(&arm_info),
714 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
715 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
716 may_use_blx_(true), should_force_pic_veneer_(false)
719 // Whether we can use BLX.
722 { return this->may_use_blx_; }
726 set_may_use_blx(bool value)
727 { this->may_use_blx_ = value; }
729 // Whether we force PCI branch veneers.
731 should_force_pic_veneer() const
732 { return this->should_force_pic_veneer_; }
734 // Set PIC veneer flag.
736 set_should_force_pic_veneer(bool value)
737 { this->should_force_pic_veneer_ = value; }
739 // Whether we use THUMB-2 instructions.
743 // FIXME: This should not hard-coded.
747 // Whether we use THUMB/THUMB-2 instructions only.
749 using_thumb_only() const
751 // FIXME: This should not hard-coded.
755 // Process the relocations to determine unreferenced sections for
756 // garbage collection.
758 gc_process_relocs(const General_options& options,
759 Symbol_table* symtab,
761 Sized_relobj<32, big_endian>* object,
762 unsigned int data_shndx,
763 unsigned int sh_type,
764 const unsigned char* prelocs,
766 Output_section* output_section,
767 bool needs_special_offset_handling,
768 size_t local_symbol_count,
769 const unsigned char* plocal_symbols);
771 // Scan the relocations to look for symbol adjustments.
773 scan_relocs(const General_options& options,
774 Symbol_table* symtab,
776 Sized_relobj<32, big_endian>* object,
777 unsigned int data_shndx,
778 unsigned int sh_type,
779 const unsigned char* prelocs,
781 Output_section* output_section,
782 bool needs_special_offset_handling,
783 size_t local_symbol_count,
784 const unsigned char* plocal_symbols);
786 // Finalize the sections.
788 do_finalize_sections(Layout*);
790 // Return the value to use for a dynamic symbol which requires special
793 do_dynsym_value(const Symbol*) const;
795 // Relocate a section.
797 relocate_section(const Relocate_info<32, big_endian>*,
798 unsigned int sh_type,
799 const unsigned char* prelocs,
801 Output_section* output_section,
802 bool needs_special_offset_handling,
804 elfcpp::Elf_types<32>::Elf_Addr view_address,
805 section_size_type view_size,
806 const Reloc_symbol_changes*);
808 // Scan the relocs during a relocatable link.
810 scan_relocatable_relocs(const General_options& options,
811 Symbol_table* symtab,
813 Sized_relobj<32, big_endian>* object,
814 unsigned int data_shndx,
815 unsigned int sh_type,
816 const unsigned char* prelocs,
818 Output_section* output_section,
819 bool needs_special_offset_handling,
820 size_t local_symbol_count,
821 const unsigned char* plocal_symbols,
822 Relocatable_relocs*);
824 // Relocate a section during a relocatable link.
826 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
827 unsigned int sh_type,
828 const unsigned char* prelocs,
830 Output_section* output_section,
831 off_t offset_in_output_section,
832 const Relocatable_relocs*,
834 elfcpp::Elf_types<32>::Elf_Addr view_address,
835 section_size_type view_size,
836 unsigned char* reloc_view,
837 section_size_type reloc_view_size);
839 // Return whether SYM is defined by the ABI.
841 do_is_defined_by_abi(Symbol* sym) const
842 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
844 // Return the size of the GOT section.
848 gold_assert(this->got_ != NULL);
849 return this->got_->data_size();
852 // Map platform-specific reloc types
854 get_real_reloc_type (unsigned int r_type);
856 // Get the default ARM target.
857 static const Target_arm<big_endian>&
860 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
861 && parameters->target().is_big_endian() == big_endian);
862 return static_cast<const Target_arm<big_endian>&>(parameters->target());
866 // The class which scans relocations.
871 : issued_non_pic_error_(false)
875 local(const General_options& options, Symbol_table* symtab,
876 Layout* layout, Target_arm* target,
877 Sized_relobj<32, big_endian>* object,
878 unsigned int data_shndx,
879 Output_section* output_section,
880 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
881 const elfcpp::Sym<32, big_endian>& lsym);
884 global(const General_options& options, Symbol_table* symtab,
885 Layout* layout, Target_arm* target,
886 Sized_relobj<32, big_endian>* object,
887 unsigned int data_shndx,
888 Output_section* output_section,
889 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
894 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
895 unsigned int r_type);
898 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
899 unsigned int r_type, Symbol*);
902 check_non_pic(Relobj*, unsigned int r_type);
904 // Almost identical to Symbol::needs_plt_entry except that it also
905 // handles STT_ARM_TFUNC.
907 symbol_needs_plt_entry(const Symbol* sym)
909 // An undefined symbol from an executable does not need a PLT entry.
910 if (sym->is_undefined() && !parameters->options().shared())
913 return (!parameters->doing_static_link()
914 && (sym->type() == elfcpp::STT_FUNC
915 || sym->type() == elfcpp::STT_ARM_TFUNC)
916 && (sym->is_from_dynobj()
917 || sym->is_undefined()
918 || sym->is_preemptible()));
921 // Whether we have issued an error about a non-PIC compilation.
922 bool issued_non_pic_error_;
925 // The class which implements relocation.
935 // Return whether the static relocation needs to be applied.
937 should_apply_static_reloc(const Sized_symbol<32>* gsym,
940 Output_section* output_section);
942 // Do a relocation. Return false if the caller should not issue
943 // any warnings about this relocation.
945 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
946 Output_section*, size_t relnum,
947 const elfcpp::Rel<32, big_endian>&,
948 unsigned int r_type, const Sized_symbol<32>*,
949 const Symbol_value<32>*,
950 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
953 // Return whether we want to pass flag NON_PIC_REF for this
956 reloc_is_non_pic (unsigned int r_type)
960 case elfcpp::R_ARM_REL32:
961 case elfcpp::R_ARM_THM_CALL:
962 case elfcpp::R_ARM_CALL:
963 case elfcpp::R_ARM_JUMP24:
964 case elfcpp::R_ARM_PREL31:
965 case elfcpp::R_ARM_THM_ABS5:
966 case elfcpp::R_ARM_ABS8:
967 case elfcpp::R_ARM_ABS12:
968 case elfcpp::R_ARM_ABS16:
969 case elfcpp::R_ARM_BASE_ABS:
977 // A class which returns the size required for a relocation type,
978 // used while scanning relocs during a relocatable link.
979 class Relocatable_size_for_reloc
983 get_size_for_reloc(unsigned int, Relobj*);
986 // Get the GOT section, creating it if necessary.
987 Output_data_got<32, big_endian>*
988 got_section(Symbol_table*, Layout*);
990 // Get the GOT PLT section.
992 got_plt_section() const
994 gold_assert(this->got_plt_ != NULL);
995 return this->got_plt_;
998 // Create a PLT entry for a global symbol.
1000 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1002 // Get the PLT section.
1003 const Output_data_plt_arm<big_endian>*
1006 gold_assert(this->plt_ != NULL);
1010 // Get the dynamic reloc section, creating it if necessary.
1012 rel_dyn_section(Layout*);
1014 // Return true if the symbol may need a COPY relocation.
1015 // References from an executable object to non-function symbols
1016 // defined in a dynamic object may need a COPY relocation.
1018 may_need_copy_reloc(Symbol* gsym)
1020 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1021 && gsym->may_need_copy_reloc());
1024 // Add a potential copy relocation.
1026 copy_reloc(Symbol_table* symtab, Layout* layout,
1027 Sized_relobj<32, big_endian>* object,
1028 unsigned int shndx, Output_section* output_section,
1029 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1031 this->copy_relocs_.copy_reloc(symtab, layout,
1032 symtab->get_sized_symbol<32>(sym),
1033 object, shndx, output_section, reloc,
1034 this->rel_dyn_section(layout));
1037 // Information about this specific target which we pass to the
1038 // general Target structure.
1039 static const Target::Target_info arm_info;
1041 // The types of GOT entries needed for this platform.
1044 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1048 Output_data_got<32, big_endian>* got_;
1050 Output_data_plt_arm<big_endian>* plt_;
1051 // The GOT PLT section.
1052 Output_data_space* got_plt_;
1053 // The dynamic reloc section.
1054 Reloc_section* rel_dyn_;
1055 // Relocs saved to avoid a COPY reloc.
1056 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1057 // Space for variables copied with a COPY reloc.
1058 Output_data_space* dynbss_;
1059 // Whether we can use BLX.
1061 // Whether we force PIC branch veneers.
1062 bool should_force_pic_veneer_;
1065 template<bool big_endian>
1066 const Target::Target_info Target_arm<big_endian>::arm_info =
1069 big_endian, // is_big_endian
1070 elfcpp::EM_ARM, // machine_code
1071 false, // has_make_symbol
1072 false, // has_resolve
1073 false, // has_code_fill
1074 true, // is_default_stack_executable
1076 "/usr/lib/libc.so.1", // dynamic_linker
1077 0x8000, // default_text_segment_address
1078 0x1000, // abi_pagesize (overridable by -z max-page-size)
1079 0x1000, // common_pagesize (overridable by -z common-page-size)
1080 elfcpp::SHN_UNDEF, // small_common_shndx
1081 elfcpp::SHN_UNDEF, // large_common_shndx
1082 0, // small_common_section_flags
1083 0 // large_common_section_flags
1086 // Arm relocate functions class
1089 template<bool big_endian>
1090 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1095 STATUS_OKAY, // No error during relocation.
1096 STATUS_OVERFLOW, // Relocation oveflow.
1097 STATUS_BAD_RELOC // Relocation cannot be applied.
1101 typedef Relocate_functions<32, big_endian> Base;
1102 typedef Arm_relocate_functions<big_endian> This;
1104 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
1105 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
1106 // to distinguish ARM and THUMB functions and it is treated specially.
1107 static inline Symbol_value<32>::Value
1108 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
1109 const Symbol_value<32>* psymval,
1110 Symbol_value<32>::Value addend,
1113 typedef Symbol_value<32>::Value Valtype;
1117 Valtype raw = psymval->value(object, 0);
1118 Valtype thumb_bit = raw & 1;
1119 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
1122 return psymval->value(object, addend);
1125 // Encoding of imm16 argument for movt and movw ARM instructions
1128 // imm16 := imm4 | imm12
1130 // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0
1131 // +-------+---------------+-------+-------+-----------------------+
1132 // | | |imm4 | |imm12 |
1133 // +-------+---------------+-------+-------+-----------------------+
1135 // Extract the relocation addend from VAL based on the ARM
1136 // instruction encoding described above.
1137 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1138 extract_arm_movw_movt_addend(
1139 typename elfcpp::Swap<32, big_endian>::Valtype val)
1141 // According to the Elf ABI for ARM Architecture the immediate
1142 // field is sign-extended to form the addend.
1143 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1146 // Insert X into VAL based on the ARM instruction encoding described
1148 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1149 insert_val_arm_movw_movt(
1150 typename elfcpp::Swap<32, big_endian>::Valtype val,
1151 typename elfcpp::Swap<32, big_endian>::Valtype x)
1155 val |= (x & 0xf000) << 4;
1159 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1162 // imm16 := imm4 | i | imm3 | imm8
1164 // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0
1165 // +---------+-+-----------+-------++-+-----+-------+---------------+
1166 // | |i| |imm4 || |imm3 | |imm8 |
1167 // +---------+-+-----------+-------++-+-----+-------+---------------+
1169 // Extract the relocation addend from VAL based on the Thumb2
1170 // instruction encoding described above.
1171 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1172 extract_thumb_movw_movt_addend(
1173 typename elfcpp::Swap<32, big_endian>::Valtype val)
1175 // According to the Elf ABI for ARM Architecture the immediate
1176 // field is sign-extended to form the addend.
1177 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1178 | ((val >> 15) & 0x0800)
1179 | ((val >> 4) & 0x0700)
1183 // Insert X into VAL based on the Thumb2 instruction encoding
1185 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1186 insert_val_thumb_movw_movt(
1187 typename elfcpp::Swap<32, big_endian>::Valtype val,
1188 typename elfcpp::Swap<32, big_endian>::Valtype x)
1191 val |= (x & 0xf000) << 4;
1192 val |= (x & 0x0800) << 15;
1193 val |= (x & 0x0700) << 4;
1194 val |= (x & 0x00ff);
1198 // FIXME: This probably only works for Android on ARM v5te. We should
1199 // following GNU ld for the general case.
1200 template<unsigned r_type>
1201 static inline typename This::Status
1202 arm_branch_common(unsigned char *view,
1203 const Sized_relobj<32, big_endian>* object,
1204 const Symbol_value<32>* psymval,
1205 elfcpp::Elf_types<32>::Elf_Addr address,
1208 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1209 Valtype* wv = reinterpret_cast<Valtype*>(view);
1210 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1212 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
1213 && ((val & 0x0f000000UL) == 0x0a000000UL);
1214 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
1215 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
1216 && ((val & 0x0f000000UL) == 0x0b000000UL);
1217 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
1218 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
1220 if (r_type == elfcpp::R_ARM_CALL)
1222 if (!insn_is_uncond_bl && !insn_is_blx)
1223 return This::STATUS_BAD_RELOC;
1225 else if (r_type == elfcpp::R_ARM_JUMP24)
1227 if (!insn_is_b && !insn_is_cond_bl)
1228 return This::STATUS_BAD_RELOC;
1230 else if (r_type == elfcpp::R_ARM_PLT32)
1232 if (!insn_is_any_branch)
1233 return This::STATUS_BAD_RELOC;
1238 Valtype addend = utils::sign_extend<26>(val << 2);
1239 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1242 // If target has thumb bit set, we need to either turn the BL
1243 // into a BLX (for ARMv5 or above) or generate a stub.
1247 if (insn_is_uncond_bl)
1248 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
1250 return This::STATUS_BAD_RELOC;
1253 gold_assert(!insn_is_blx);
1255 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
1256 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1257 return (utils::has_overflow<26>(x)
1258 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
1263 // R_ARM_ABS8: S + A
1264 static inline typename This::Status
1265 abs8(unsigned char *view,
1266 const Sized_relobj<32, big_endian>* object,
1267 const Symbol_value<32>* psymval)
1269 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
1270 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1271 Valtype* wv = reinterpret_cast<Valtype*>(view);
1272 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
1273 Reltype addend = utils::sign_extend<8>(val);
1274 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1275 val = utils::bit_select(val, x, 0xffU);
1276 elfcpp::Swap<8, big_endian>::writeval(wv, val);
1277 return (utils::has_signed_unsigned_overflow<8>(x)
1278 ? This::STATUS_OVERFLOW
1279 : This::STATUS_OKAY);
1282 // R_ARM_THM_ABS5: S + A
1283 static inline typename This::Status
1284 thm_abs5(unsigned char *view,
1285 const Sized_relobj<32, big_endian>* object,
1286 const Symbol_value<32>* psymval)
1288 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1289 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1290 Valtype* wv = reinterpret_cast<Valtype*>(view);
1291 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1292 Reltype addend = (val & 0x7e0U) >> 6;
1293 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1294 val = utils::bit_select(val, x << 6, 0x7e0U);
1295 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1296 return (utils::has_overflow<5>(x)
1297 ? This::STATUS_OVERFLOW
1298 : This::STATUS_OKAY);
1301 // R_ARM_ABS12: S + A
1302 static inline typename This::Status
1303 abs12(unsigned char *view,
1304 const Sized_relobj<32, big_endian>* object,
1305 const Symbol_value<32>* psymval)
1307 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1308 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1309 Valtype* wv = reinterpret_cast<Valtype*>(view);
1310 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1311 Reltype addend = val & 0x0fffU;
1312 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1313 val = utils::bit_select(val, x, 0x0fffU);
1314 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1315 return (utils::has_overflow<12>(x)
1316 ? This::STATUS_OVERFLOW
1317 : This::STATUS_OKAY);
1320 // R_ARM_ABS16: S + A
1321 static inline typename This::Status
1322 abs16(unsigned char *view,
1323 const Sized_relobj<32, big_endian>* object,
1324 const Symbol_value<32>* psymval)
1326 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1327 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1328 Valtype* wv = reinterpret_cast<Valtype*>(view);
1329 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1330 Reltype addend = utils::sign_extend<16>(val);
1331 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1332 val = utils::bit_select(val, x, 0xffffU);
1333 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1334 return (utils::has_signed_unsigned_overflow<16>(x)
1335 ? This::STATUS_OVERFLOW
1336 : This::STATUS_OKAY);
1339 // R_ARM_ABS32: (S + A) | T
1340 static inline typename This::Status
1341 abs32(unsigned char *view,
1342 const Sized_relobj<32, big_endian>* object,
1343 const Symbol_value<32>* psymval,
1346 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1347 Valtype* wv = reinterpret_cast<Valtype*>(view);
1348 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1349 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1350 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1351 return This::STATUS_OKAY;
1354 // R_ARM_REL32: (S + A) | T - P
1355 static inline typename This::Status
1356 rel32(unsigned char *view,
1357 const Sized_relobj<32, big_endian>* object,
1358 const Symbol_value<32>* psymval,
1359 elfcpp::Elf_types<32>::Elf_Addr address,
1362 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1363 Valtype* wv = reinterpret_cast<Valtype*>(view);
1364 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1365 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1367 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1368 return This::STATUS_OKAY;
1371 // R_ARM_THM_CALL: (S + A) | T - P
1372 static inline typename This::Status
1373 thm_call(unsigned char *view,
1374 const Sized_relobj<32, big_endian>* object,
1375 const Symbol_value<32>* psymval,
1376 elfcpp::Elf_types<32>::Elf_Addr address,
1379 // A thumb call consists of two instructions.
1380 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1381 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1382 Valtype* wv = reinterpret_cast<Valtype*>(view);
1383 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
1384 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
1385 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1386 gold_assert((lo & 0xf800) == 0xf800);
1387 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
1388 | ((lo & 0x7ff) << 1));
1389 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1392 // If target has no thumb bit set, we need to either turn the BL
1393 // into a BLX (for ARMv5 or above) or generate a stub.
1396 // This only works for ARMv5 and above with interworking enabled.
1399 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
1400 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
1401 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
1402 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
1403 return (utils::has_overflow<23>(x)
1404 ? This::STATUS_OVERFLOW
1405 : This::STATUS_OKAY);
1408 // R_ARM_BASE_PREL: B(S) + A - P
1409 static inline typename This::Status
1410 base_prel(unsigned char* view,
1411 elfcpp::Elf_types<32>::Elf_Addr origin,
1412 elfcpp::Elf_types<32>::Elf_Addr address)
1414 Base::rel32(view, origin - address);
1418 // R_ARM_BASE_ABS: B(S) + A
1419 static inline typename This::Status
1420 base_abs(unsigned char* view,
1421 elfcpp::Elf_types<32>::Elf_Addr origin)
1423 Base::rel32(view, origin);
1427 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1428 static inline typename This::Status
1429 got_brel(unsigned char* view,
1430 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1432 Base::rel32(view, got_offset);
1433 return This::STATUS_OKAY;
1436 // R_ARM_GOT_PREL: GOT(S) + A – P
1437 static inline typename This::Status
1438 got_prel(unsigned char* view,
1439 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
1440 elfcpp::Elf_types<32>::Elf_Addr address)
1442 Base::rel32(view, got_offset - address);
1443 return This::STATUS_OKAY;
1446 // R_ARM_PLT32: (S + A) | T - P
1447 static inline typename This::Status
1448 plt32(unsigned char *view,
1449 const Sized_relobj<32, big_endian>* object,
1450 const Symbol_value<32>* psymval,
1451 elfcpp::Elf_types<32>::Elf_Addr address,
1454 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
1455 address, has_thumb_bit);
1458 // R_ARM_CALL: (S + A) | T - P
1459 static inline typename This::Status
1460 call(unsigned char *view,
1461 const Sized_relobj<32, big_endian>* object,
1462 const Symbol_value<32>* psymval,
1463 elfcpp::Elf_types<32>::Elf_Addr address,
1466 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
1467 address, has_thumb_bit);
1470 // R_ARM_JUMP24: (S + A) | T - P
1471 static inline typename This::Status
1472 jump24(unsigned char *view,
1473 const Sized_relobj<32, big_endian>* object,
1474 const Symbol_value<32>* psymval,
1475 elfcpp::Elf_types<32>::Elf_Addr address,
1478 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
1479 address, has_thumb_bit);
1482 // R_ARM_PREL: (S + A) | T - P
1483 static inline typename This::Status
1484 prel31(unsigned char *view,
1485 const Sized_relobj<32, big_endian>* object,
1486 const Symbol_value<32>* psymval,
1487 elfcpp::Elf_types<32>::Elf_Addr address,
1490 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1491 Valtype* wv = reinterpret_cast<Valtype*>(view);
1492 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1493 Valtype addend = utils::sign_extend<31>(val);
1494 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1496 val = utils::bit_select(val, x, 0x7fffffffU);
1497 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1498 return (utils::has_overflow<31>(x) ?
1499 This::STATUS_OVERFLOW : This::STATUS_OKAY);
1502 // R_ARM_MOVW_ABS_NC: (S + A) | T
1503 static inline typename This::Status
1504 movw_abs_nc(unsigned char *view,
1505 const Sized_relobj<32, big_endian>* object,
1506 const Symbol_value<32>* psymval,
1509 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1510 Valtype* wv = reinterpret_cast<Valtype*>(view);
1511 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1512 Valtype addend = This::extract_arm_movw_movt_addend(val);
1513 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1514 val = This::insert_val_arm_movw_movt(val, x);
1515 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1516 return This::STATUS_OKAY;
1519 // R_ARM_MOVT_ABS: S + A
1520 static inline typename This::Status
1521 movt_abs(unsigned char *view,
1522 const Sized_relobj<32, big_endian>* object,
1523 const Symbol_value<32>* psymval)
1525 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1526 Valtype* wv = reinterpret_cast<Valtype*>(view);
1527 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1528 Valtype addend = This::extract_arm_movw_movt_addend(val);
1529 Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1530 val = This::insert_val_arm_movw_movt(val, x);
1531 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1532 return This::STATUS_OKAY;
1535 // R_ARM_THM_MOVW_ABS_NC: S + A | T
1536 static inline typename This::Status
1537 thm_movw_abs_nc(unsigned char *view,
1538 const Sized_relobj<32, big_endian>* object,
1539 const Symbol_value<32>* psymval,
1542 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1543 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1544 Valtype* wv = reinterpret_cast<Valtype*>(view);
1545 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1546 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1547 Reltype addend = extract_thumb_movw_movt_addend(val);
1548 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1549 val = This::insert_val_thumb_movw_movt(val, x);
1550 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1551 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1552 return This::STATUS_OKAY;
1555 // R_ARM_THM_MOVT_ABS: S + A
1556 static inline typename This::Status
1557 thm_movt_abs(unsigned char *view,
1558 const Sized_relobj<32, big_endian>* object,
1559 const Symbol_value<32>* psymval)
1561 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1562 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1563 Valtype* wv = reinterpret_cast<Valtype*>(view);
1564 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1565 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1566 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1567 Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1568 val = This::insert_val_thumb_movw_movt(val, x);
1569 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1570 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1571 return This::STATUS_OKAY;
1574 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
1575 static inline typename This::Status
1576 movw_prel_nc(unsigned char *view,
1577 const Sized_relobj<32, big_endian>* object,
1578 const Symbol_value<32>* psymval,
1579 elfcpp::Elf_types<32>::Elf_Addr address,
1582 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1583 Valtype* wv = reinterpret_cast<Valtype*>(view);
1584 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1585 Valtype addend = This::extract_arm_movw_movt_addend(val);
1586 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1588 val = This::insert_val_arm_movw_movt(val, x);
1589 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1590 return This::STATUS_OKAY;
1593 // R_ARM_MOVT_PREL: S + A - P
1594 static inline typename This::Status
1595 movt_prel(unsigned char *view,
1596 const Sized_relobj<32, big_endian>* object,
1597 const Symbol_value<32>* psymval,
1598 elfcpp::Elf_types<32>::Elf_Addr address)
1600 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1601 Valtype* wv = reinterpret_cast<Valtype*>(view);
1602 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1603 Valtype addend = This::extract_arm_movw_movt_addend(val);
1604 Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
1606 val = This::insert_val_arm_movw_movt(val, x);
1607 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1608 return This::STATUS_OKAY;
1611 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
1612 static inline typename This::Status
1613 thm_movw_prel_nc(unsigned char *view,
1614 const Sized_relobj<32, big_endian>* object,
1615 const Symbol_value<32>* psymval,
1616 elfcpp::Elf_types<32>::Elf_Addr address,
1619 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1620 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1621 Valtype* wv = reinterpret_cast<Valtype*>(view);
1622 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1623 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1624 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1625 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1627 val = This::insert_val_thumb_movw_movt(val, x);
1628 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1629 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1630 return This::STATUS_OKAY;
1633 // R_ARM_THM_MOVT_PREL: S + A - P
1634 static inline typename This::Status
1635 thm_movt_prel(unsigned char *view,
1636 const Sized_relobj<32, big_endian>* object,
1637 const Symbol_value<32>* psymval,
1638 elfcpp::Elf_types<32>::Elf_Addr address)
1640 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1641 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1642 Valtype* wv = reinterpret_cast<Valtype*>(view);
1643 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1644 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1645 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1646 Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
1648 val = This::insert_val_thumb_movw_movt(val, x);
1649 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1650 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1651 return This::STATUS_OKAY;
1655 // Get the GOT section, creating it if necessary.
1657 template<bool big_endian>
1658 Output_data_got<32, big_endian>*
1659 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
1661 if (this->got_ == NULL)
1663 gold_assert(symtab != NULL && layout != NULL);
1665 this->got_ = new Output_data_got<32, big_endian>();
1668 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1670 | elfcpp::SHF_WRITE),
1674 // The old GNU linker creates a .got.plt section. We just
1675 // create another set of data in the .got section. Note that we
1676 // always create a PLT if we create a GOT, although the PLT
1678 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
1679 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1681 | elfcpp::SHF_WRITE),
1685 // The first three entries are reserved.
1686 this->got_plt_->set_current_data_size(3 * 4);
1688 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1689 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1691 0, 0, elfcpp::STT_OBJECT,
1693 elfcpp::STV_HIDDEN, 0,
1699 // Get the dynamic reloc section, creating it if necessary.
1701 template<bool big_endian>
1702 typename Target_arm<big_endian>::Reloc_section*
1703 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
1705 if (this->rel_dyn_ == NULL)
1707 gold_assert(layout != NULL);
1708 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
1709 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1710 elfcpp::SHF_ALLOC, this->rel_dyn_);
1712 return this->rel_dyn_;
1715 // Insn_template methods.
1717 // Return byte size of an instruction template.
1720 Insn_template::size() const
1722 switch (this->type())
1735 // Return alignment of an instruction template.
1738 Insn_template::alignment() const
1740 switch (this->type())
1753 // Stub_template methods.
1755 Stub_template::Stub_template(
1756 Stub_type type, const Insn_template* insns,
1758 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
1759 entry_in_thumb_mode_(false), relocs_()
1763 // Compute byte size and alignment of stub template.
1764 for (size_t i = 0; i < insn_count; i++)
1766 unsigned insn_alignment = insns[i].alignment();
1767 size_t insn_size = insns[i].size();
1768 gold_assert((offset & (insn_alignment - 1)) == 0);
1769 this->alignment_ = std::max(this->alignment_, insn_alignment);
1770 switch (insns[i].type())
1772 case Insn_template::THUMB16_TYPE:
1774 this->entry_in_thumb_mode_ = true;
1777 case Insn_template::THUMB32_TYPE:
1778 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
1779 this->relocs_.push_back(Reloc(i, offset));
1781 this->entry_in_thumb_mode_ = true;
1784 case Insn_template::ARM_TYPE:
1785 // Handle cases where the target is encoded within the
1787 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
1788 this->relocs_.push_back(Reloc(i, offset));
1791 case Insn_template::DATA_TYPE:
1792 // Entry point cannot be data.
1793 gold_assert(i != 0);
1794 this->relocs_.push_back(Reloc(i, offset));
1800 offset += insn_size;
1802 this->size_ = offset;
1805 // Reloc_stub::Key methods.
1807 // Dump a Key as a string for debugging.
1810 Reloc_stub::Key::name() const
1812 if (this->r_sym_ == invalid_index)
1814 // Global symbol key name
1815 // <stub-type>:<symbol name>:<addend>.
1816 const std::string sym_name = this->u_.symbol->name();
1817 // We need to print two hex number and two colons. So just add 100 bytes
1818 // to the symbol name size.
1819 size_t len = sym_name.size() + 100;
1820 char* buffer = new char[len];
1821 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
1822 sym_name.c_str(), this->addend_);
1823 gold_assert(c > 0 && c < static_cast<int>(len));
1825 return std::string(buffer);
1829 // local symbol key name
1830 // <stub-type>:<object>:<r_sym>:<addend>.
1831 const size_t len = 200;
1833 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
1834 this->u_.relobj, this->r_sym_, this->addend_);
1835 gold_assert(c > 0 && c < static_cast<int>(len));
1836 return std::string(buffer);
1840 // Reloc_stub methods.
1842 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
1843 // LOCATION to DESTINATION.
1844 // This code is based on the arm_type_of_stub function in
1845 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
1849 Reloc_stub::stub_type_for_reloc(
1850 unsigned int r_type,
1851 Arm_address location,
1852 Arm_address destination,
1853 bool target_is_thumb)
1855 Stub_type stub_type = arm_stub_none;
1857 // This is a bit ugly but we want to avoid using a templated class for
1858 // big and little endianities.
1860 bool should_force_pic_veneer;
1863 if (parameters->target().is_big_endian())
1865 const Target_arm<true>& big_endian_target =
1866 Target_arm<true>::default_target();
1867 may_use_blx = big_endian_target.may_use_blx();
1868 should_force_pic_veneer = big_endian_target.should_force_pic_veneer();
1869 thumb2 = big_endian_target.using_thumb2();
1870 thumb_only = big_endian_target.using_thumb_only();
1874 const Target_arm<false>& little_endian_target =
1875 Target_arm<false>::default_target();
1876 may_use_blx = little_endian_target.may_use_blx();
1877 should_force_pic_veneer = little_endian_target.should_force_pic_veneer();
1878 thumb2 = little_endian_target.using_thumb2();
1879 thumb_only = little_endian_target.using_thumb_only();
1882 int64_t branch_offset = (int64_t)destination - location;
1884 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
1886 // Handle cases where:
1887 // - this call goes too far (different Thumb/Thumb2 max
1889 // - it's a Thumb->Arm call and blx is not available, or it's a
1890 // Thumb->Arm branch (not bl). A stub is needed in this case.
1892 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
1893 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
1895 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
1896 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
1897 || ((!target_is_thumb)
1898 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
1899 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
1901 if (target_is_thumb)
1906 stub_type = (parameters->options().shared() | should_force_pic_veneer)
1909 && (r_type == elfcpp::R_ARM_THM_CALL))
1910 // V5T and above. Stub starts with ARM code, so
1911 // we must be able to switch mode before
1912 // reaching it, which is only possible for 'bl'
1913 // (ie R_ARM_THM_CALL relocation).
1914 ? arm_stub_long_branch_any_thumb_pic
1915 // On V4T, use Thumb code only.
1916 : arm_stub_long_branch_v4t_thumb_thumb_pic)
1920 && (r_type == elfcpp::R_ARM_THM_CALL))
1921 ? arm_stub_long_branch_any_any // V5T and above.
1922 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
1926 stub_type = (parameters->options().shared() | should_force_pic_veneer)
1927 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
1928 : arm_stub_long_branch_thumb_only; // non-PIC stub.
1935 // FIXME: We should check that the input section is from an
1936 // object that has interwork enabled.
1938 stub_type = (parameters->options().shared()
1939 || should_force_pic_veneer)
1942 && (r_type == elfcpp::R_ARM_THM_CALL))
1943 ? arm_stub_long_branch_any_arm_pic // V5T and above.
1944 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
1948 && (r_type == elfcpp::R_ARM_THM_CALL))
1949 ? arm_stub_long_branch_any_any // V5T and above.
1950 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
1952 // Handle v4t short branches.
1953 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
1954 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
1955 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
1956 stub_type = arm_stub_short_branch_v4t_thumb_arm;
1960 else if (r_type == elfcpp::R_ARM_CALL
1961 || r_type == elfcpp::R_ARM_JUMP24
1962 || r_type == elfcpp::R_ARM_PLT32)
1964 if (target_is_thumb)
1968 // FIXME: We should check that the input section is from an
1969 // object that has interwork enabled.
1971 // We have an extra 2-bytes reach because of
1972 // the mode change (bit 24 (H) of BLX encoding).
1973 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
1974 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
1975 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
1976 || (r_type == elfcpp::R_ARM_JUMP24)
1977 || (r_type == elfcpp::R_ARM_PLT32))
1979 stub_type = (parameters->options().shared()
1980 || should_force_pic_veneer)
1983 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
1984 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
1988 ? arm_stub_long_branch_any_any // V5T and above.
1989 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
1995 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
1996 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
1998 stub_type = (parameters->options().shared()
1999 || should_force_pic_veneer)
2000 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
2001 : arm_stub_long_branch_any_any; /// non-PIC.
2009 // Template to implement do_write for a specific target endianity.
2011 template<bool big_endian>
2013 Reloc_stub::do_fixed_endian_write(unsigned char* view,
2014 section_size_type view_size)
2016 const Stub_template* stub_template = this->stub_template();
2017 const Insn_template* insns = stub_template->insns();
2019 // FIXME: We do not handle BE8 encoding yet.
2020 unsigned char* pov = view;
2021 for (size_t i = 0; i < stub_template->insn_count(); i++)
2023 switch (insns[i].type())
2025 case Insn_template::THUMB16_TYPE:
2026 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2027 gold_assert(insns[i].reloc_addend() == 0);
2028 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2030 case Insn_template::THUMB32_TYPE:
2032 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2033 uint32_t lo = insns[i].data() & 0xffff;
2034 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2035 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2038 case Insn_template::ARM_TYPE:
2039 case Insn_template::DATA_TYPE:
2040 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2045 pov += insns[i].size();
2047 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2050 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2053 Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2057 this->do_fixed_endian_write<true>(view, view_size);
2059 this->do_fixed_endian_write<false>(view, view_size);
2062 // Stub_factory methods.
2064 Stub_factory::Stub_factory()
2066 // The instruction template sequences are declared as static
2067 // objects and initialized first time the constructor runs.
2069 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2070 // to reach the stub if necessary.
2071 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2073 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2074 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2075 // dcd R_ARM_ABS32(X)
2078 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2080 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2082 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2083 Insn_template::arm_insn(0xe12fff1c), // bx ip
2084 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2085 // dcd R_ARM_ABS32(X)
2088 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2089 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2091 Insn_template::thumb16_insn(0xb401), // push {r0}
2092 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2093 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2094 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2095 Insn_template::thumb16_insn(0x4760), // bx ip
2096 Insn_template::thumb16_insn(0xbf00), // nop
2097 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2098 // dcd R_ARM_ABS32(X)
2101 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2103 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2105 Insn_template::thumb16_insn(0x4778), // bx pc
2106 Insn_template::thumb16_insn(0x46c0), // nop
2107 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2108 Insn_template::arm_insn(0xe12fff1c), // bx ip
2109 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2110 // dcd R_ARM_ABS32(X)
2113 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2115 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2117 Insn_template::thumb16_insn(0x4778), // bx pc
2118 Insn_template::thumb16_insn(0x46c0), // nop
2119 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2120 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2121 // dcd R_ARM_ABS32(X)
2124 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2125 // one, when the destination is close enough.
2126 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2128 Insn_template::thumb16_insn(0x4778), // bx pc
2129 Insn_template::thumb16_insn(0x46c0), // nop
2130 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2133 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2134 // blx to reach the stub if necessary.
2135 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2137 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2138 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2139 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2140 // dcd R_ARM_REL32(X-4)
2143 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2144 // blx to reach the stub if necessary. We can not add into pc;
2145 // it is not guaranteed to mode switch (different in ARMv6 and
2147 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2149 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2150 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2151 Insn_template::arm_insn(0xe12fff1c), // bx ip
2152 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2153 // dcd R_ARM_REL32(X)
2156 // V4T ARM -> ARM long branch stub, PIC.
2157 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2159 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2160 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2161 Insn_template::arm_insn(0xe12fff1c), // bx ip
2162 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2163 // dcd R_ARM_REL32(X)
2166 // V4T Thumb -> ARM long branch stub, PIC.
2167 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2169 Insn_template::thumb16_insn(0x4778), // bx pc
2170 Insn_template::thumb16_insn(0x46c0), // nop
2171 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2172 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2173 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2174 // dcd R_ARM_REL32(X)
2177 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2179 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
2181 Insn_template::thumb16_insn(0xb401), // push {r0}
2182 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2183 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2184 Insn_template::thumb16_insn(0x4484), // add ip, r0
2185 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2186 Insn_template::thumb16_insn(0x4760), // bx ip
2187 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
2188 // dcd R_ARM_REL32(X)
2191 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2193 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2195 Insn_template::thumb16_insn(0x4778), // bx pc
2196 Insn_template::thumb16_insn(0x46c0), // nop
2197 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2198 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2199 Insn_template::arm_insn(0xe12fff1c), // bx ip
2200 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2201 // dcd R_ARM_REL32(X)
2204 // Cortex-A8 erratum-workaround stubs.
2206 // Stub used for conditional branches (which may be beyond +/-1MB away,
2207 // so we can't use a conditional branch to reach this stub).
2214 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
2216 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2217 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2218 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2222 // Stub used for b.w and bl.w instructions.
2224 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
2226 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2229 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
2231 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2234 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2235 // instruction (which switches to ARM mode) to point to this stub. Jump to
2236 // the real destination using an ARM-mode branch.
2237 const Insn_template elf32_arm_stub_a8_veneer_blx[] =
2239 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2242 // Fill in the stub template look-up table. Stub templates are constructed
2243 // per instance of Stub_factory for fast look-up without locking
2244 // in a thread-enabled environment.
2246 this->stub_templates_[arm_stub_none] =
2247 new Stub_template(arm_stub_none, NULL, 0);
2249 #define DEF_STUB(x) \
2253 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2254 Stub_type type = arm_stub_##x; \
2255 this->stub_templates_[type] = \
2256 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2264 // A class to handle the PLT data.
2266 template<bool big_endian>
2267 class Output_data_plt_arm : public Output_section_data
2270 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
2273 Output_data_plt_arm(Layout*, Output_data_space*);
2275 // Add an entry to the PLT.
2277 add_entry(Symbol* gsym);
2279 // Return the .rel.plt section data.
2280 const Reloc_section*
2282 { return this->rel_; }
2286 do_adjust_output_section(Output_section* os);
2288 // Write to a map file.
2290 do_print_to_mapfile(Mapfile* mapfile) const
2291 { mapfile->print_output_data(this, _("** PLT")); }
2294 // Template for the first PLT entry.
2295 static const uint32_t first_plt_entry[5];
2297 // Template for subsequent PLT entries.
2298 static const uint32_t plt_entry[3];
2300 // Set the final size.
2302 set_final_data_size()
2304 this->set_data_size(sizeof(first_plt_entry)
2305 + this->count_ * sizeof(plt_entry));
2308 // Write out the PLT data.
2310 do_write(Output_file*);
2312 // The reloc section.
2313 Reloc_section* rel_;
2314 // The .got.plt section.
2315 Output_data_space* got_plt_;
2316 // The number of PLT entries.
2317 unsigned int count_;
2320 // Create the PLT section. The ordinary .got section is an argument,
2321 // since we need to refer to the start. We also create our own .got
2322 // section just for PLT entries.
2324 template<bool big_endian>
2325 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
2326 Output_data_space* got_plt)
2327 : Output_section_data(4), got_plt_(got_plt), count_(0)
2329 this->rel_ = new Reloc_section(false);
2330 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2331 elfcpp::SHF_ALLOC, this->rel_);
2334 template<bool big_endian>
2336 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
2341 // Add an entry to the PLT.
2343 template<bool big_endian>
2345 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
2347 gold_assert(!gsym->has_plt_offset());
2349 // Note that when setting the PLT offset we skip the initial
2350 // reserved PLT entry.
2351 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
2352 + sizeof(first_plt_entry));
2356 section_offset_type got_offset = this->got_plt_->current_data_size();
2358 // Every PLT entry needs a GOT entry which points back to the PLT
2359 // entry (this will be changed by the dynamic linker, normally
2360 // lazily when the function is called).
2361 this->got_plt_->set_current_data_size(got_offset + 4);
2363 // Every PLT entry needs a reloc.
2364 gsym->set_needs_dynsym_entry();
2365 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
2368 // Note that we don't need to save the symbol. The contents of the
2369 // PLT are independent of which symbols are used. The symbols only
2370 // appear in the relocations.
2374 // FIXME: This is not very flexible. Right now this has only been tested
2375 // on armv5te. If we are to support additional architecture features like
2376 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
2378 // The first entry in the PLT.
2379 template<bool big_endian>
2380 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
2382 0xe52de004, // str lr, [sp, #-4]!
2383 0xe59fe004, // ldr lr, [pc, #4]
2384 0xe08fe00e, // add lr, pc, lr
2385 0xe5bef008, // ldr pc, [lr, #8]!
2386 0x00000000, // &GOT[0] - .
2389 // Subsequent entries in the PLT.
2391 template<bool big_endian>
2392 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
2394 0xe28fc600, // add ip, pc, #0xNN00000
2395 0xe28cca00, // add ip, ip, #0xNN000
2396 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
2399 // Write out the PLT. This uses the hand-coded instructions above,
2400 // and adjusts them as needed. This is all specified by the arm ELF
2401 // Processor Supplement.
2403 template<bool big_endian>
2405 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
2407 const off_t offset = this->offset();
2408 const section_size_type oview_size =
2409 convert_to_section_size_type(this->data_size());
2410 unsigned char* const oview = of->get_output_view(offset, oview_size);
2412 const off_t got_file_offset = this->got_plt_->offset();
2413 const section_size_type got_size =
2414 convert_to_section_size_type(this->got_plt_->data_size());
2415 unsigned char* const got_view = of->get_output_view(got_file_offset,
2417 unsigned char* pov = oview;
2419 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
2420 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
2422 // Write first PLT entry. All but the last word are constants.
2423 const size_t num_first_plt_words = (sizeof(first_plt_entry)
2424 / sizeof(plt_entry[0]));
2425 for (size_t i = 0; i < num_first_plt_words - 1; i++)
2426 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
2427 // Last word in first PLT entry is &GOT[0] - .
2428 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
2429 got_address - (plt_address + 16));
2430 pov += sizeof(first_plt_entry);
2432 unsigned char* got_pov = got_view;
2434 memset(got_pov, 0, 12);
2437 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
2438 unsigned int plt_offset = sizeof(first_plt_entry);
2439 unsigned int plt_rel_offset = 0;
2440 unsigned int got_offset = 12;
2441 const unsigned int count = this->count_;
2442 for (unsigned int i = 0;
2445 pov += sizeof(plt_entry),
2447 plt_offset += sizeof(plt_entry),
2448 plt_rel_offset += rel_size,
2451 // Set and adjust the PLT entry itself.
2452 int32_t offset = ((got_address + got_offset)
2453 - (plt_address + plt_offset + 8));
2455 gold_assert(offset >= 0 && offset < 0x0fffffff);
2456 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
2457 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
2458 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
2459 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
2460 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
2461 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
2463 // Set the entry in the GOT.
2464 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
2467 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2468 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2470 of->write_output_view(offset, oview_size, oview);
2471 of->write_output_view(got_file_offset, got_size, got_view);
2474 // Create a PLT entry for a global symbol.
2476 template<bool big_endian>
2478 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
2481 if (gsym->has_plt_offset())
2484 if (this->plt_ == NULL)
2486 // Create the GOT sections first.
2487 this->got_section(symtab, layout);
2489 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
2490 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2492 | elfcpp::SHF_EXECINSTR),
2495 this->plt_->add_entry(gsym);
2498 // Report an unsupported relocation against a local symbol.
2500 template<bool big_endian>
2502 Target_arm<big_endian>::Scan::unsupported_reloc_local(
2503 Sized_relobj<32, big_endian>* object,
2504 unsigned int r_type)
2506 gold_error(_("%s: unsupported reloc %u against local symbol"),
2507 object->name().c_str(), r_type);
2510 // We are about to emit a dynamic relocation of type R_TYPE. If the
2511 // dynamic linker does not support it, issue an error. The GNU linker
2512 // only issues a non-PIC error for an allocated read-only section.
2513 // Here we know the section is allocated, but we don't know that it is
2514 // read-only. But we check for all the relocation types which the
2515 // glibc dynamic linker supports, so it seems appropriate to issue an
2516 // error even if the section is not read-only.
2518 template<bool big_endian>
2520 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
2521 unsigned int r_type)
2525 // These are the relocation types supported by glibc for ARM.
2526 case elfcpp::R_ARM_RELATIVE:
2527 case elfcpp::R_ARM_COPY:
2528 case elfcpp::R_ARM_GLOB_DAT:
2529 case elfcpp::R_ARM_JUMP_SLOT:
2530 case elfcpp::R_ARM_ABS32:
2531 case elfcpp::R_ARM_ABS32_NOI:
2532 case elfcpp::R_ARM_PC24:
2533 // FIXME: The following 3 types are not supported by Android's dynamic
2535 case elfcpp::R_ARM_TLS_DTPMOD32:
2536 case elfcpp::R_ARM_TLS_DTPOFF32:
2537 case elfcpp::R_ARM_TLS_TPOFF32:
2541 // This prevents us from issuing more than one error per reloc
2542 // section. But we can still wind up issuing more than one
2543 // error per object file.
2544 if (this->issued_non_pic_error_)
2546 object->error(_("requires unsupported dynamic reloc; "
2547 "recompile with -fPIC"));
2548 this->issued_non_pic_error_ = true;
2551 case elfcpp::R_ARM_NONE:
2556 // Scan a relocation for a local symbol.
2557 // FIXME: This only handles a subset of relocation types used by Android
2558 // on ARM v5te devices.
2560 template<bool big_endian>
2562 Target_arm<big_endian>::Scan::local(const General_options&,
2563 Symbol_table* symtab,
2566 Sized_relobj<32, big_endian>* object,
2567 unsigned int data_shndx,
2568 Output_section* output_section,
2569 const elfcpp::Rel<32, big_endian>& reloc,
2570 unsigned int r_type,
2571 const elfcpp::Sym<32, big_endian>&)
2573 r_type = get_real_reloc_type(r_type);
2576 case elfcpp::R_ARM_NONE:
2579 case elfcpp::R_ARM_ABS32:
2580 case elfcpp::R_ARM_ABS32_NOI:
2581 // If building a shared library (or a position-independent
2582 // executable), we need to create a dynamic relocation for
2583 // this location. The relocation applied at link time will
2584 // apply the link-time value, so we flag the location with
2585 // an R_ARM_RELATIVE relocation so the dynamic loader can
2586 // relocate it easily.
2587 if (parameters->options().output_is_position_independent())
2589 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2590 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2591 // If we are to add more other reloc types than R_ARM_ABS32,
2592 // we need to add check_non_pic(object, r_type) here.
2593 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
2594 output_section, data_shndx,
2595 reloc.get_r_offset());
2599 case elfcpp::R_ARM_REL32:
2600 case elfcpp::R_ARM_THM_CALL:
2601 case elfcpp::R_ARM_CALL:
2602 case elfcpp::R_ARM_PREL31:
2603 case elfcpp::R_ARM_JUMP24:
2604 case elfcpp::R_ARM_PLT32:
2605 case elfcpp::R_ARM_THM_ABS5:
2606 case elfcpp::R_ARM_ABS8:
2607 case elfcpp::R_ARM_ABS12:
2608 case elfcpp::R_ARM_ABS16:
2609 case elfcpp::R_ARM_BASE_ABS:
2610 case elfcpp::R_ARM_MOVW_ABS_NC:
2611 case elfcpp::R_ARM_MOVT_ABS:
2612 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
2613 case elfcpp::R_ARM_THM_MOVT_ABS:
2614 case elfcpp::R_ARM_MOVW_PREL_NC:
2615 case elfcpp::R_ARM_MOVT_PREL:
2616 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
2617 case elfcpp::R_ARM_THM_MOVT_PREL:
2620 case elfcpp::R_ARM_GOTOFF32:
2621 // We need a GOT section:
2622 target->got_section(symtab, layout);
2625 case elfcpp::R_ARM_BASE_PREL:
2626 // FIXME: What about this?
2629 case elfcpp::R_ARM_GOT_BREL:
2630 case elfcpp::R_ARM_GOT_PREL:
2632 // The symbol requires a GOT entry.
2633 Output_data_got<32, big_endian>* got =
2634 target->got_section(symtab, layout);
2635 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2636 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
2638 // If we are generating a shared object, we need to add a
2639 // dynamic RELATIVE relocation for this symbol's GOT entry.
2640 if (parameters->options().output_is_position_independent())
2642 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2643 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2644 rel_dyn->add_local_relative(
2645 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
2646 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
2652 case elfcpp::R_ARM_TARGET1:
2653 // This should have been mapped to another type already.
2655 case elfcpp::R_ARM_COPY:
2656 case elfcpp::R_ARM_GLOB_DAT:
2657 case elfcpp::R_ARM_JUMP_SLOT:
2658 case elfcpp::R_ARM_RELATIVE:
2659 // These are relocations which should only be seen by the
2660 // dynamic linker, and should never be seen here.
2661 gold_error(_("%s: unexpected reloc %u in object file"),
2662 object->name().c_str(), r_type);
2666 unsupported_reloc_local(object, r_type);
2671 // Report an unsupported relocation against a global symbol.
2673 template<bool big_endian>
2675 Target_arm<big_endian>::Scan::unsupported_reloc_global(
2676 Sized_relobj<32, big_endian>* object,
2677 unsigned int r_type,
2680 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2681 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2684 // Scan a relocation for a global symbol.
2685 // FIXME: This only handles a subset of relocation types used by Android
2686 // on ARM v5te devices.
2688 template<bool big_endian>
2690 Target_arm<big_endian>::Scan::global(const General_options&,
2691 Symbol_table* symtab,
2694 Sized_relobj<32, big_endian>* object,
2695 unsigned int data_shndx,
2696 Output_section* output_section,
2697 const elfcpp::Rel<32, big_endian>& reloc,
2698 unsigned int r_type,
2701 r_type = get_real_reloc_type(r_type);
2704 case elfcpp::R_ARM_NONE:
2707 case elfcpp::R_ARM_ABS32:
2708 case elfcpp::R_ARM_ABS32_NOI:
2710 // Make a dynamic relocation if necessary.
2711 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
2713 if (target->may_need_copy_reloc(gsym))
2715 target->copy_reloc(symtab, layout, object,
2716 data_shndx, output_section, gsym, reloc);
2718 else if (gsym->can_use_relative_reloc(false))
2720 // If we are to add more other reloc types than R_ARM_ABS32,
2721 // we need to add check_non_pic(object, r_type) here.
2722 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2723 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
2724 output_section, object,
2725 data_shndx, reloc.get_r_offset());
2729 // If we are to add more other reloc types than R_ARM_ABS32,
2730 // we need to add check_non_pic(object, r_type) here.
2731 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2732 rel_dyn->add_global(gsym, r_type, output_section, object,
2733 data_shndx, reloc.get_r_offset());
2739 case elfcpp::R_ARM_MOVW_ABS_NC:
2740 case elfcpp::R_ARM_MOVT_ABS:
2741 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
2742 case elfcpp::R_ARM_THM_MOVT_ABS:
2743 case elfcpp::R_ARM_MOVW_PREL_NC:
2744 case elfcpp::R_ARM_MOVT_PREL:
2745 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
2746 case elfcpp::R_ARM_THM_MOVT_PREL:
2749 case elfcpp::R_ARM_THM_ABS5:
2750 case elfcpp::R_ARM_ABS8:
2751 case elfcpp::R_ARM_ABS12:
2752 case elfcpp::R_ARM_ABS16:
2753 case elfcpp::R_ARM_BASE_ABS:
2755 // No dynamic relocs of this kinds.
2756 // Report the error in case of PIC.
2757 int flags = Symbol::NON_PIC_REF;
2758 if (gsym->type() == elfcpp::STT_FUNC
2759 || gsym->type() == elfcpp::STT_ARM_TFUNC)
2760 flags |= Symbol::FUNCTION_CALL;
2761 if (gsym->needs_dynamic_reloc(flags))
2762 check_non_pic(object, r_type);
2766 case elfcpp::R_ARM_REL32:
2767 case elfcpp::R_ARM_PREL31:
2769 // Make a dynamic relocation if necessary.
2770 int flags = Symbol::NON_PIC_REF;
2771 if (gsym->needs_dynamic_reloc(flags))
2773 if (target->may_need_copy_reloc(gsym))
2775 target->copy_reloc(symtab, layout, object,
2776 data_shndx, output_section, gsym, reloc);
2780 check_non_pic(object, r_type);
2781 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2782 rel_dyn->add_global(gsym, r_type, output_section, object,
2783 data_shndx, reloc.get_r_offset());
2789 case elfcpp::R_ARM_JUMP24:
2790 case elfcpp::R_ARM_THM_CALL:
2791 case elfcpp::R_ARM_CALL:
2793 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
2794 target->make_plt_entry(symtab, layout, gsym);
2795 // Make a dynamic relocation if necessary.
2796 int flags = Symbol::NON_PIC_REF;
2797 if (gsym->type() == elfcpp::STT_FUNC
2798 || gsym->type() == elfcpp::STT_ARM_TFUNC)
2799 flags |= Symbol::FUNCTION_CALL;
2800 if (gsym->needs_dynamic_reloc(flags))
2802 if (target->may_need_copy_reloc(gsym))
2804 target->copy_reloc(symtab, layout, object,
2805 data_shndx, output_section, gsym,
2810 check_non_pic(object, r_type);
2811 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2812 rel_dyn->add_global(gsym, r_type, output_section, object,
2813 data_shndx, reloc.get_r_offset());
2819 case elfcpp::R_ARM_PLT32:
2820 // If the symbol is fully resolved, this is just a relative
2821 // local reloc. Otherwise we need a PLT entry.
2822 if (gsym->final_value_is_known())
2824 // If building a shared library, we can also skip the PLT entry
2825 // if the symbol is defined in the output file and is protected
2827 if (gsym->is_defined()
2828 && !gsym->is_from_dynobj()
2829 && !gsym->is_preemptible())
2831 target->make_plt_entry(symtab, layout, gsym);
2834 case elfcpp::R_ARM_GOTOFF32:
2835 // We need a GOT section.
2836 target->got_section(symtab, layout);
2839 case elfcpp::R_ARM_BASE_PREL:
2840 // FIXME: What about this?
2843 case elfcpp::R_ARM_GOT_BREL:
2844 case elfcpp::R_ARM_GOT_PREL:
2846 // The symbol requires a GOT entry.
2847 Output_data_got<32, big_endian>* got =
2848 target->got_section(symtab, layout);
2849 if (gsym->final_value_is_known())
2850 got->add_global(gsym, GOT_TYPE_STANDARD);
2853 // If this symbol is not fully resolved, we need to add a
2854 // GOT entry with a dynamic relocation.
2855 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2856 if (gsym->is_from_dynobj()
2857 || gsym->is_undefined()
2858 || gsym->is_preemptible())
2859 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
2860 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
2863 if (got->add_global(gsym, GOT_TYPE_STANDARD))
2864 rel_dyn->add_global_relative(
2865 gsym, elfcpp::R_ARM_RELATIVE, got,
2866 gsym->got_offset(GOT_TYPE_STANDARD));
2872 case elfcpp::R_ARM_TARGET1:
2873 // This should have been mapped to another type already.
2875 case elfcpp::R_ARM_COPY:
2876 case elfcpp::R_ARM_GLOB_DAT:
2877 case elfcpp::R_ARM_JUMP_SLOT:
2878 case elfcpp::R_ARM_RELATIVE:
2879 // These are relocations which should only be seen by the
2880 // dynamic linker, and should never be seen here.
2881 gold_error(_("%s: unexpected reloc %u in object file"),
2882 object->name().c_str(), r_type);
2886 unsupported_reloc_global(object, r_type, gsym);
2891 // Process relocations for gc.
2893 template<bool big_endian>
2895 Target_arm<big_endian>::gc_process_relocs(const General_options& options,
2896 Symbol_table* symtab,
2898 Sized_relobj<32, big_endian>* object,
2899 unsigned int data_shndx,
2901 const unsigned char* prelocs,
2903 Output_section* output_section,
2904 bool needs_special_offset_handling,
2905 size_t local_symbol_count,
2906 const unsigned char* plocal_symbols)
2908 typedef Target_arm<big_endian> Arm;
2909 typedef typename Target_arm<big_endian>::Scan Scan;
2911 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
2921 needs_special_offset_handling,
2926 // Scan relocations for a section.
2928 template<bool big_endian>
2930 Target_arm<big_endian>::scan_relocs(const General_options& options,
2931 Symbol_table* symtab,
2933 Sized_relobj<32, big_endian>* object,
2934 unsigned int data_shndx,
2935 unsigned int sh_type,
2936 const unsigned char* prelocs,
2938 Output_section* output_section,
2939 bool needs_special_offset_handling,
2940 size_t local_symbol_count,
2941 const unsigned char* plocal_symbols)
2943 typedef typename Target_arm<big_endian>::Scan Scan;
2944 if (sh_type == elfcpp::SHT_RELA)
2946 gold_error(_("%s: unsupported RELA reloc section"),
2947 object->name().c_str());
2951 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
2961 needs_special_offset_handling,
2966 // Finalize the sections.
2968 template<bool big_endian>
2970 Target_arm<big_endian>::do_finalize_sections(Layout* layout)
2972 // Fill in some more dynamic tags.
2973 Output_data_dynamic* const odyn = layout->dynamic_data();
2976 if (this->got_plt_ != NULL)
2977 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
2979 if (this->plt_ != NULL)
2981 const Output_data* od = this->plt_->rel_plt();
2982 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
2983 odyn->add_section_address(elfcpp::DT_JMPREL, od);
2984 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
2987 if (this->rel_dyn_ != NULL)
2989 const Output_data* od = this->rel_dyn_;
2990 odyn->add_section_address(elfcpp::DT_REL, od);
2991 odyn->add_section_size(elfcpp::DT_RELSZ, od);
2992 odyn->add_constant(elfcpp::DT_RELENT,
2993 elfcpp::Elf_sizes<32>::rel_size);
2996 if (!parameters->options().shared())
2998 // The value of the DT_DEBUG tag is filled in by the dynamic
2999 // linker at run time, and used by the debugger.
3000 odyn->add_constant(elfcpp::DT_DEBUG, 0);
3004 // Emit any relocs we saved in an attempt to avoid generating COPY
3006 if (this->copy_relocs_.any_saved_relocs())
3007 this->copy_relocs_.emit(this->rel_dyn_section(layout));
3009 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
3010 // the .ARM.exidx section.
3011 if (!layout->script_options()->saw_phdrs_clause()
3012 && !parameters->options().relocatable())
3014 Output_section* exidx_section =
3015 layout->find_output_section(".ARM.exidx");
3017 if (exidx_section != NULL
3018 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
3020 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
3022 Output_segment* exidx_segment =
3023 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
3024 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
3029 // Return whether a direct absolute static relocation needs to be applied.
3030 // In cases where Scan::local() or Scan::global() has created
3031 // a dynamic relocation other than R_ARM_RELATIVE, the addend
3032 // of the relocation is carried in the data, and we must not
3033 // apply the static relocation.
3035 template<bool big_endian>
3037 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
3038 const Sized_symbol<32>* gsym,
3041 Output_section* output_section)
3043 // If the output section is not allocated, then we didn't call
3044 // scan_relocs, we didn't create a dynamic reloc, and we must apply
3046 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
3049 // For local symbols, we will have created a non-RELATIVE dynamic
3050 // relocation only if (a) the output is position independent,
3051 // (b) the relocation is absolute (not pc- or segment-relative), and
3052 // (c) the relocation is not 32 bits wide.
3054 return !(parameters->options().output_is_position_independent()
3055 && (ref_flags & Symbol::ABSOLUTE_REF)
3058 // For global symbols, we use the same helper routines used in the
3059 // scan pass. If we did not create a dynamic relocation, or if we
3060 // created a RELATIVE dynamic relocation, we should apply the static
3062 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
3063 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
3064 && gsym->can_use_relative_reloc(ref_flags
3065 & Symbol::FUNCTION_CALL);
3066 return !has_dyn || is_rel;
3069 // Perform a relocation.
3071 template<bool big_endian>
3073 Target_arm<big_endian>::Relocate::relocate(
3074 const Relocate_info<32, big_endian>* relinfo,
3076 Output_section *output_section,
3078 const elfcpp::Rel<32, big_endian>& rel,
3079 unsigned int r_type,
3080 const Sized_symbol<32>* gsym,
3081 const Symbol_value<32>* psymval,
3082 unsigned char* view,
3083 elfcpp::Elf_types<32>::Elf_Addr address,
3084 section_size_type /* view_size */ )
3086 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
3088 r_type = get_real_reloc_type(r_type);
3090 // If this the symbol may be a Thumb function, set thumb bit to 1.
3091 bool has_thumb_bit = ((gsym != NULL)
3092 && (gsym->type() == elfcpp::STT_FUNC
3093 || gsym->type() == elfcpp::STT_ARM_TFUNC));
3095 // Pick the value to use for symbols defined in shared objects.
3096 Symbol_value<32> symval;
3098 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
3100 symval.set_output_value(target->plt_section()->address()
3101 + gsym->plt_offset());
3106 const Sized_relobj<32, big_endian>* object = relinfo->object;
3108 // Get the GOT offset if needed.
3109 // The GOT pointer points to the end of the GOT section.
3110 // We need to subtract the size of the GOT section to get
3111 // the actual offset to use in the relocation.
3112 bool have_got_offset = false;
3113 unsigned int got_offset = 0;
3116 case elfcpp::R_ARM_GOT_BREL:
3117 case elfcpp::R_ARM_GOT_PREL:
3120 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3121 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
3122 - target->got_size());
3126 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3127 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3128 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3129 - target->got_size());
3131 have_got_offset = true;
3138 typename Arm_relocate_functions::Status reloc_status =
3139 Arm_relocate_functions::STATUS_OKAY;
3142 case elfcpp::R_ARM_NONE:
3145 case elfcpp::R_ARM_ABS8:
3146 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3148 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
3151 case elfcpp::R_ARM_ABS12:
3152 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3154 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
3157 case elfcpp::R_ARM_ABS16:
3158 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3160 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
3163 case elfcpp::R_ARM_ABS32:
3164 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3166 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
3170 case elfcpp::R_ARM_ABS32_NOI:
3171 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3173 // No thumb bit for this relocation: (S + A)
3174 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
3178 case elfcpp::R_ARM_MOVW_ABS_NC:
3179 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3181 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
3185 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
3186 "a shared object; recompile with -fPIC"));
3189 case elfcpp::R_ARM_MOVT_ABS:
3190 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3192 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
3194 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
3195 "a shared object; recompile with -fPIC"));
3198 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3199 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3201 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
3205 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
3206 "making a shared object; recompile with -fPIC"));
3209 case elfcpp::R_ARM_THM_MOVT_ABS:
3210 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3212 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
3215 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
3216 "making a shared object; recompile with -fPIC"));
3219 case elfcpp::R_ARM_MOVW_PREL_NC:
3220 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
3225 case elfcpp::R_ARM_MOVT_PREL:
3226 reloc_status = Arm_relocate_functions::movt_prel(view, object,
3230 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3231 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
3236 case elfcpp::R_ARM_THM_MOVT_PREL:
3237 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
3241 case elfcpp::R_ARM_REL32:
3242 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
3243 address, has_thumb_bit);
3246 case elfcpp::R_ARM_THM_ABS5:
3247 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3249 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
3252 case elfcpp::R_ARM_THM_CALL:
3253 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
3254 address, has_thumb_bit);
3257 case elfcpp::R_ARM_GOTOFF32:
3259 elfcpp::Elf_types<32>::Elf_Addr got_origin;
3260 got_origin = target->got_plt_section()->address();
3261 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
3262 got_origin, has_thumb_bit);
3266 case elfcpp::R_ARM_BASE_PREL:
3269 // Get the addressing origin of the output segment defining the
3270 // symbol gsym (AAELF 4.6.1.2 Relocation types)
3271 gold_assert(gsym != NULL);
3272 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
3273 origin = gsym->output_segment()->vaddr();
3274 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
3275 origin = gsym->output_data()->address();
3278 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3279 _("cannot find origin of R_ARM_BASE_PREL"));
3282 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
3286 case elfcpp::R_ARM_BASE_ABS:
3288 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3293 // Get the addressing origin of the output segment defining
3294 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
3296 // R_ARM_BASE_ABS with the NULL symbol will give the
3297 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
3298 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
3299 origin = target->got_plt_section()->address();
3300 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
3301 origin = gsym->output_segment()->vaddr();
3302 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
3303 origin = gsym->output_data()->address();
3306 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3307 _("cannot find origin of R_ARM_BASE_ABS"));
3311 reloc_status = Arm_relocate_functions::base_abs(view, origin);
3315 case elfcpp::R_ARM_GOT_BREL:
3316 gold_assert(have_got_offset);
3317 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
3320 case elfcpp::R_ARM_GOT_PREL:
3321 gold_assert(have_got_offset);
3322 // Get the address origin for GOT PLT, which is allocated right
3323 // after the GOT section, to calculate an absolute address of
3324 // the symbol GOT entry (got_origin + got_offset).
3325 elfcpp::Elf_types<32>::Elf_Addr got_origin;
3326 got_origin = target->got_plt_section()->address();
3327 reloc_status = Arm_relocate_functions::got_prel(view,
3328 got_origin + got_offset,
3332 case elfcpp::R_ARM_PLT32:
3333 gold_assert(gsym == NULL
3334 || gsym->has_plt_offset()
3335 || gsym->final_value_is_known()
3336 || (gsym->is_defined()
3337 && !gsym->is_from_dynobj()
3338 && !gsym->is_preemptible()));
3339 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
3340 address, has_thumb_bit);
3343 case elfcpp::R_ARM_CALL:
3344 reloc_status = Arm_relocate_functions::call(view, object, psymval,
3345 address, has_thumb_bit);
3348 case elfcpp::R_ARM_JUMP24:
3349 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
3350 address, has_thumb_bit);
3353 case elfcpp::R_ARM_PREL31:
3354 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
3355 address, has_thumb_bit);
3358 case elfcpp::R_ARM_TARGET1:
3359 // This should have been mapped to another type already.
3361 case elfcpp::R_ARM_COPY:
3362 case elfcpp::R_ARM_GLOB_DAT:
3363 case elfcpp::R_ARM_JUMP_SLOT:
3364 case elfcpp::R_ARM_RELATIVE:
3365 // These are relocations which should only be seen by the
3366 // dynamic linker, and should never be seen here.
3367 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3368 _("unexpected reloc %u in object file"),
3373 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3374 _("unsupported reloc %u"),
3379 // Report any errors.
3380 switch (reloc_status)
3382 case Arm_relocate_functions::STATUS_OKAY:
3384 case Arm_relocate_functions::STATUS_OVERFLOW:
3385 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3386 _("relocation overflow in relocation %u"),
3389 case Arm_relocate_functions::STATUS_BAD_RELOC:
3390 gold_error_at_location(
3394 _("unexpected opcode while processing relocation %u"),
3404 // Relocate section data.
3406 template<bool big_endian>
3408 Target_arm<big_endian>::relocate_section(
3409 const Relocate_info<32, big_endian>* relinfo,
3410 unsigned int sh_type,
3411 const unsigned char* prelocs,
3413 Output_section* output_section,
3414 bool needs_special_offset_handling,
3415 unsigned char* view,
3416 elfcpp::Elf_types<32>::Elf_Addr address,
3417 section_size_type view_size,
3418 const Reloc_symbol_changes* reloc_symbol_changes)
3420 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
3421 gold_assert(sh_type == elfcpp::SHT_REL);
3423 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
3430 needs_special_offset_handling,
3434 reloc_symbol_changes);
3437 // Return the size of a relocation while scanning during a relocatable
3440 template<bool big_endian>
3442 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
3443 unsigned int r_type,
3446 r_type = get_real_reloc_type(r_type);
3449 case elfcpp::R_ARM_NONE:
3452 case elfcpp::R_ARM_ABS8:
3455 case elfcpp::R_ARM_ABS16:
3456 case elfcpp::R_ARM_THM_ABS5:
3459 case elfcpp::R_ARM_ABS32:
3460 case elfcpp::R_ARM_ABS32_NOI:
3461 case elfcpp::R_ARM_ABS12:
3462 case elfcpp::R_ARM_BASE_ABS:
3463 case elfcpp::R_ARM_REL32:
3464 case elfcpp::R_ARM_THM_CALL:
3465 case elfcpp::R_ARM_GOTOFF32:
3466 case elfcpp::R_ARM_BASE_PREL:
3467 case elfcpp::R_ARM_GOT_BREL:
3468 case elfcpp::R_ARM_GOT_PREL:
3469 case elfcpp::R_ARM_PLT32:
3470 case elfcpp::R_ARM_CALL:
3471 case elfcpp::R_ARM_JUMP24:
3472 case elfcpp::R_ARM_PREL31:
3473 case elfcpp::R_ARM_MOVW_ABS_NC:
3474 case elfcpp::R_ARM_MOVT_ABS:
3475 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3476 case elfcpp::R_ARM_THM_MOVT_ABS:
3477 case elfcpp::R_ARM_MOVW_PREL_NC:
3478 case elfcpp::R_ARM_MOVT_PREL:
3479 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3480 case elfcpp::R_ARM_THM_MOVT_PREL:
3483 case elfcpp::R_ARM_TARGET1:
3484 // This should have been mapped to another type already.
3486 case elfcpp::R_ARM_COPY:
3487 case elfcpp::R_ARM_GLOB_DAT:
3488 case elfcpp::R_ARM_JUMP_SLOT:
3489 case elfcpp::R_ARM_RELATIVE:
3490 // These are relocations which should only be seen by the
3491 // dynamic linker, and should never be seen here.
3492 gold_error(_("%s: unexpected reloc %u in object file"),
3493 object->name().c_str(), r_type);
3497 object->error(_("unsupported reloc %u in object file"), r_type);
3502 // Scan the relocs during a relocatable link.
3504 template<bool big_endian>
3506 Target_arm<big_endian>::scan_relocatable_relocs(
3507 const General_options& options,
3508 Symbol_table* symtab,
3510 Sized_relobj<32, big_endian>* object,
3511 unsigned int data_shndx,
3512 unsigned int sh_type,
3513 const unsigned char* prelocs,
3515 Output_section* output_section,
3516 bool needs_special_offset_handling,
3517 size_t local_symbol_count,
3518 const unsigned char* plocal_symbols,
3519 Relocatable_relocs* rr)
3521 gold_assert(sh_type == elfcpp::SHT_REL);
3523 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3524 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3526 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
3527 Scan_relocatable_relocs>(
3536 needs_special_offset_handling,
3542 // Relocate a section during a relocatable link.
3544 template<bool big_endian>
3546 Target_arm<big_endian>::relocate_for_relocatable(
3547 const Relocate_info<32, big_endian>* relinfo,
3548 unsigned int sh_type,
3549 const unsigned char* prelocs,
3551 Output_section* output_section,
3552 off_t offset_in_output_section,
3553 const Relocatable_relocs* rr,
3554 unsigned char* view,
3555 elfcpp::Elf_types<32>::Elf_Addr view_address,
3556 section_size_type view_size,
3557 unsigned char* reloc_view,
3558 section_size_type reloc_view_size)
3560 gold_assert(sh_type == elfcpp::SHT_REL);
3562 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
3567 offset_in_output_section,
3576 // Return the value to use for a dynamic symbol which requires special
3577 // treatment. This is how we support equality comparisons of function
3578 // pointers across shared library boundaries, as described in the
3579 // processor specific ABI supplement.
3581 template<bool big_endian>
3583 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
3585 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3586 return this->plt_section()->address() + gsym->plt_offset();
3589 // Map platform-specific relocs to real relocs
3591 template<bool big_endian>
3593 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
3597 case elfcpp::R_ARM_TARGET1:
3598 // This is either R_ARM_ABS32 or R_ARM_REL32;
3599 return elfcpp::R_ARM_ABS32;
3601 case elfcpp::R_ARM_TARGET2:
3602 // This can be any reloc type but ususally is R_ARM_GOT_PREL
3603 return elfcpp::R_ARM_GOT_PREL;
3610 // The selector for arm object files.
3612 template<bool big_endian>
3613 class Target_selector_arm : public Target_selector
3616 Target_selector_arm()
3617 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
3618 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
3622 do_instantiate_target()
3623 { return new Target_arm<big_endian>(); }
3626 Target_selector_arm<false> target_selector_arm;
3627 Target_selector_arm<true> target_selector_armbe;
3629 } // End anonymous namespace.