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.
35 #include "parameters.h"
42 #include "copy-relocs.h"
44 #include "target-reloc.h"
45 #include "target-select.h"
55 template<bool big_endian>
56 class Output_data_plt_arm;
58 template<bool big_endian>
61 template<bool big_endian>
62 class Arm_input_section;
64 template<bool big_endian>
65 class Arm_output_section;
67 template<bool big_endian>
70 template<bool big_endian>
74 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
76 // Maximum branch offsets for ARM, THUMB and THUMB2.
77 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
78 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
79 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
80 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
81 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
82 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
84 // The arm target class.
86 // This is a very simple port of gold for ARM-EABI. It is intended for
87 // supporting Android only for the time being. Only these relocation types
116 // R_ARM_THM_MOVW_ABS_NC
117 // R_ARM_THM_MOVT_ABS
118 // R_ARM_MOVW_PREL_NC
120 // R_ARM_THM_MOVW_PREL_NC
121 // R_ARM_THM_MOVT_PREL
124 // - Generate various branch stubs.
125 // - Support interworking.
126 // - Define section symbols __exidx_start and __exidx_stop.
127 // - Support more relocation types as needed.
128 // - Make PLTs more flexible for different architecture features like
130 // There are probably a lot more.
132 // Instruction template class. This class is similar to the insn_sequence
133 // struct in bfd/elf32-arm.c.
138 // Types of instruction templates.
147 // Factory methods to create instrunction templates in different formats.
149 static const Insn_template
150 thumb16_insn(uint32_t data)
151 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
153 // A bit of a hack. A Thumb conditional branch, in which the proper
154 // condition is inserted when we build the stub.
155 static const Insn_template
156 thumb16_bcond_insn(uint32_t data)
157 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 1); }
159 static const Insn_template
160 thumb32_insn(uint32_t data)
161 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
163 static const Insn_template
164 thumb32_b_insn(uint32_t data, int reloc_addend)
166 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
170 static const Insn_template
171 arm_insn(uint32_t data)
172 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
174 static const Insn_template
175 arm_rel_insn(unsigned data, int reloc_addend)
176 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
178 static const Insn_template
179 data_word(unsigned data, unsigned int r_type, int reloc_addend)
180 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
182 // Accessors. This class is used for read-only objects so no modifiers
187 { return this->data_; }
189 // Return the instruction sequence type of this.
192 { return this->type_; }
194 // Return the ARM relocation type of this.
197 { return this->r_type_; }
201 { return this->reloc_addend_; }
203 // Return size of instrunction template in bytes.
207 // Return byte-alignment of instrunction template.
212 // We make the constructor private to ensure that only the factory
215 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
216 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
219 // Instruction specific data. This is used to store information like
220 // some of the instruction bits.
222 // Instruction template type.
224 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
225 unsigned int r_type_;
226 // Relocation addend.
227 int32_t reloc_addend_;
230 // Macro for generating code to stub types. One entry per long/short
234 DEF_STUB(long_branch_any_any) \
235 DEF_STUB(long_branch_v4t_arm_thumb) \
236 DEF_STUB(long_branch_thumb_only) \
237 DEF_STUB(long_branch_v4t_thumb_thumb) \
238 DEF_STUB(long_branch_v4t_thumb_arm) \
239 DEF_STUB(short_branch_v4t_thumb_arm) \
240 DEF_STUB(long_branch_any_arm_pic) \
241 DEF_STUB(long_branch_any_thumb_pic) \
242 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
243 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
244 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
245 DEF_STUB(long_branch_thumb_only_pic) \
246 DEF_STUB(a8_veneer_b_cond) \
247 DEF_STUB(a8_veneer_b) \
248 DEF_STUB(a8_veneer_bl) \
249 DEF_STUB(a8_veneer_blx)
253 #define DEF_STUB(x) arm_stub_##x,
259 // First reloc stub type.
260 arm_stub_reloc_first = arm_stub_long_branch_any_any,
261 // Last reloc stub type.
262 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
264 // First Cortex-A8 stub type.
265 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
266 // Last Cortex-A8 stub type.
267 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
270 arm_stub_type_last = arm_stub_a8_veneer_blx
274 // Stub template class. Templates are meant to be read-only objects.
275 // A stub template for a stub type contains all read-only attributes
276 // common to all stubs of the same type.
281 Stub_template(Stub_type, const Insn_template*, size_t);
289 { return this->type_; }
291 // Return an array of instruction templates.
294 { return this->insns_; }
296 // Return size of template in number of instructions.
299 { return this->insn_count_; }
301 // Return size of template in bytes.
304 { return this->size_; }
306 // Return alignment of the stub template.
309 { return this->alignment_; }
311 // Return whether entry point is in thumb mode.
313 entry_in_thumb_mode() const
314 { return this->entry_in_thumb_mode_; }
316 // Return number of relocations in this template.
319 { return this->relocs_.size(); }
321 // Return index of the I-th instruction with relocation.
323 reloc_insn_index(size_t i) const
325 gold_assert(i < this->relocs_.size());
326 return this->relocs_[i].first;
329 // Return the offset of the I-th instruction with relocation from the
330 // beginning of the stub.
332 reloc_offset(size_t i) const
334 gold_assert(i < this->relocs_.size());
335 return this->relocs_[i].second;
339 // This contains information about an instruction template with a relocation
340 // and its offset from start of stub.
341 typedef std::pair<size_t, section_size_type> Reloc;
343 // A Stub_template may not be copied. We want to share templates as much
345 Stub_template(const Stub_template&);
346 Stub_template& operator=(const Stub_template&);
350 // Points to an array of Insn_templates.
351 const Insn_template* insns_;
352 // Number of Insn_templates in insns_[].
354 // Size of templated instructions in bytes.
356 // Alignment of templated instructions.
358 // Flag to indicate if entry is in thumb mode.
359 bool entry_in_thumb_mode_;
360 // A table of reloc instruction indices and offsets. We can find these by
361 // looking at the instruction templates but we pre-compute and then stash
362 // them here for speed.
363 std::vector<Reloc> relocs_;
367 // A class for code stubs. This is a base class for different type of
368 // stubs used in the ARM target.
374 static const section_offset_type invalid_offset =
375 static_cast<section_offset_type>(-1);
378 Stub(const Stub_template* stub_template)
379 : stub_template_(stub_template), offset_(invalid_offset)
386 // Return the stub template.
388 stub_template() const
389 { return this->stub_template_; }
391 // Return offset of code stub from beginning of its containing stub table.
395 gold_assert(this->offset_ != invalid_offset);
396 return this->offset_;
399 // Set offset of code stub from beginning of its containing stub table.
401 set_offset(section_offset_type offset)
402 { this->offset_ = offset; }
404 // Return the relocation target address of the i-th relocation in the
405 // stub. This must be defined in a child class.
407 reloc_target(size_t i)
408 { return this->do_reloc_target(i); }
410 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
412 write(unsigned char* view, section_size_type view_size, bool big_endian)
413 { this->do_write(view, view_size, big_endian); }
416 // This must be defined in the child class.
418 do_reloc_target(size_t) = 0;
420 // This must be defined in the child class.
422 do_write(unsigned char*, section_size_type, bool) = 0;
426 const Stub_template* stub_template_;
427 // Offset within the section of containing this stub.
428 section_offset_type offset_;
431 // Reloc stub class. These are stubs we use to fix up relocation because
432 // of limited branch ranges.
434 class Reloc_stub : public Stub
437 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
438 // We assume we never jump to this address.
439 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
441 // Return destination address.
443 destination_address() const
445 gold_assert(this->destination_address_ != this->invalid_address);
446 return this->destination_address_;
449 // Set destination address.
451 set_destination_address(Arm_address address)
453 gold_assert(address != this->invalid_address);
454 this->destination_address_ = address;
457 // Reset destination address.
459 reset_destination_address()
460 { this->destination_address_ = this->invalid_address; }
462 // Determine stub type for a branch of a relocation of R_TYPE going
463 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
464 // the branch target is a thumb instruction. TARGET is used for look
465 // up ARM-specific linker settings.
467 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
468 Arm_address branch_target, bool target_is_thumb);
470 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
471 // and an addend. Since we treat global and local symbol differently, we
472 // use a Symbol object for a global symbol and a object-index pair for
477 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
478 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
479 // and R_SYM must not be invalid_index.
480 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
481 unsigned int r_sym, int32_t addend)
482 : stub_type_(stub_type), addend_(addend)
486 this->r_sym_ = Reloc_stub::invalid_index;
487 this->u_.symbol = symbol;
491 gold_assert(relobj != NULL && r_sym != invalid_index);
492 this->r_sym_ = r_sym;
493 this->u_.relobj = relobj;
500 // Accessors: Keys are meant to be read-only object so no modifiers are
506 { return this->stub_type_; }
508 // Return the local symbol index or invalid_index.
511 { return this->r_sym_; }
513 // Return the symbol if there is one.
516 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
518 // Return the relobj if there is one.
521 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
523 // Whether this equals to another key k.
525 eq(const Key& k) const
527 return ((this->stub_type_ == k.stub_type_)
528 && (this->r_sym_ == k.r_sym_)
529 && ((this->r_sym_ != Reloc_stub::invalid_index)
530 ? (this->u_.relobj == k.u_.relobj)
531 : (this->u_.symbol == k.u_.symbol))
532 && (this->addend_ == k.addend_));
535 // Return a hash value.
539 return (this->stub_type_
541 ^ gold::string_hash<char>(
542 (this->r_sym_ != Reloc_stub::invalid_index)
543 ? this->u_.relobj->name().c_str()
544 : this->u_.symbol->name())
548 // Functors for STL associative containers.
552 operator()(const Key& k) const
553 { return k.hash_value(); }
559 operator()(const Key& k1, const Key& k2) const
560 { return k1.eq(k2); }
563 // Name of key. This is mainly for debugging.
569 Stub_type stub_type_;
570 // If this is a local symbol, this is the index in the defining object.
571 // Otherwise, it is invalid_index for a global symbol.
573 // If r_sym_ is invalid index. This points to a global symbol.
574 // Otherwise, this points a relobj. We used the unsized and target
575 // independent Symbol and Relobj classes instead of Arm_symbol and
576 // Arm_relobj. This is done to avoid making the stub class a template
577 // as most of the stub machinery is endianity-neutral. However, it
578 // may require a bit of casting done by users of this class.
581 const Symbol* symbol;
582 const Relobj* relobj;
584 // Addend associated with a reloc.
589 // Reloc_stubs are created via a stub factory. So these are protected.
590 Reloc_stub(const Stub_template* stub_template)
591 : Stub(stub_template), destination_address_(invalid_address)
597 friend class Stub_factory;
600 // Return the relocation target address of the i-th relocation in the
603 do_reloc_target(size_t i)
605 // All reloc stub have only one relocation.
607 return this->destination_address_;
610 // A template to implement do_write below.
611 template<bool big_endian>
613 do_fixed_endian_write(unsigned char*, section_size_type);
617 do_write(unsigned char* view, section_size_type view_size, bool big_endian);
619 // Address of destination.
620 Arm_address destination_address_;
623 // Stub factory class.
628 // Return the unique instance of this class.
629 static const Stub_factory&
632 static Stub_factory singleton;
636 // Make a relocation stub.
638 make_reloc_stub(Stub_type stub_type) const
640 gold_assert(stub_type >= arm_stub_reloc_first
641 && stub_type <= arm_stub_reloc_last);
642 return new Reloc_stub(this->stub_templates_[stub_type]);
646 // Constructor and destructor are protected since we only return a single
647 // instance created in Stub_factory::get_instance().
651 // A Stub_factory may not be copied since it is a singleton.
652 Stub_factory(const Stub_factory&);
653 Stub_factory& operator=(Stub_factory&);
655 // Stub templates. These are initialized in the constructor.
656 const Stub_template* stub_templates_[arm_stub_type_last+1];
659 // A class to hold stubs for the ARM target.
661 template<bool big_endian>
662 class Stub_table : public Output_data
665 Stub_table(Arm_input_section<big_endian>* owner)
666 : Output_data(), addralign_(1), owner_(owner), has_been_changed_(false),
673 // Owner of this stub table.
674 Arm_input_section<big_endian>*
676 { return this->owner_; }
678 // Whether this stub table is empty.
681 { return this->reloc_stubs_.empty(); }
683 // Whether this has been changed.
685 has_been_changed() const
686 { return this->has_been_changed_; }
688 // Set the has-been-changed flag.
690 set_has_been_changed(bool value)
691 { this->has_been_changed_ = value; }
693 // Return the current data size.
695 current_data_size() const
696 { return this->current_data_size_for_child(); }
698 // Add a STUB with using KEY. Caller is reponsible for avoid adding
699 // if already a STUB with the same key has been added.
701 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key);
703 // Look up a relocation stub using KEY. Return NULL if there is none.
705 find_reloc_stub(const Reloc_stub::Key& key) const
707 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
708 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
711 // Relocate stubs in this stub table.
713 relocate_stubs(const Relocate_info<32, big_endian>*,
714 Target_arm<big_endian>*, Output_section*,
715 unsigned char*, Arm_address, section_size_type);
718 // Write out section contents.
720 do_write(Output_file*);
722 // Return the required alignment.
725 { return this->addralign_; }
727 // Finalize data size.
729 set_final_data_size()
730 { this->set_data_size(this->current_data_size_for_child()); }
732 // Reset address and file offset.
734 do_reset_address_and_file_offset();
737 // Unordered map of stubs.
739 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
740 Reloc_stub::Key::equal_to>
745 // Owner of this stub table.
746 Arm_input_section<big_endian>* owner_;
747 // This is set to true during relaxiong if the size of the stub table
749 bool has_been_changed_;
750 // The relocation stubs.
751 Reloc_stub_map reloc_stubs_;
754 // A class to wrap an ordinary input section containing executable code.
756 template<bool big_endian>
757 class Arm_input_section : public Output_relaxed_input_section
760 Arm_input_section(Relobj* relobj, unsigned int shndx)
761 : Output_relaxed_input_section(relobj, shndx, 1),
762 original_addralign_(1), original_size_(0), stub_table_(NULL)
772 // Whether this is a stub table owner.
774 is_stub_table_owner() const
775 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
777 // Return the stub table.
778 Stub_table<big_endian>*
780 { return this->stub_table_; }
782 // Set the stub_table.
784 set_stub_table(Stub_table<big_endian>* stub_table)
785 { this->stub_table_ = stub_table; }
787 // Downcast a base pointer to an Arm_input_section pointer. This is
788 // not type-safe but we only use Arm_input_section not the base class.
789 static Arm_input_section<big_endian>*
790 as_arm_input_section(Output_relaxed_input_section* poris)
791 { return static_cast<Arm_input_section<big_endian>*>(poris); }
794 // Write data to output file.
796 do_write(Output_file*);
798 // Return required alignment of this.
802 if (this->is_stub_table_owner())
803 return std::max(this->stub_table_->addralign(),
804 this->original_addralign_);
806 return this->original_addralign_;
809 // Finalize data size.
811 set_final_data_size();
813 // Reset address and file offset.
815 do_reset_address_and_file_offset();
819 do_output_offset(const Relobj* object, unsigned int shndx,
820 section_offset_type offset,
821 section_offset_type* poutput) const
823 if ((object == this->relobj())
824 && (shndx == this->shndx())
826 && (convert_types<uint64_t, section_offset_type>(offset)
827 <= this->original_size_))
837 // Copying is not allowed.
838 Arm_input_section(const Arm_input_section&);
839 Arm_input_section& operator=(const Arm_input_section&);
841 // Address alignment of the original input section.
842 uint64_t original_addralign_;
843 // Section size of the original input section.
844 uint64_t original_size_;
846 Stub_table<big_endian>* stub_table_;
849 // Arm output section class. This is defined mainly to add a number of
850 // stub generation methods.
852 template<bool big_endian>
853 class Arm_output_section : public Output_section
856 Arm_output_section(const char* name, elfcpp::Elf_Word type,
857 elfcpp::Elf_Xword flags)
858 : Output_section(name, type, flags)
861 ~Arm_output_section()
864 // Group input sections for stub generation.
866 group_sections(section_size_type, bool, Target_arm<big_endian>*);
868 // Downcast a base pointer to an Arm_output_section pointer. This is
869 // not type-safe but we only use Arm_output_section not the base class.
870 static Arm_output_section<big_endian>*
871 as_arm_output_section(Output_section* os)
872 { return static_cast<Arm_output_section<big_endian>*>(os); }
876 typedef Output_section::Input_section Input_section;
877 typedef Output_section::Input_section_list Input_section_list;
879 // Create a stub group.
880 void create_stub_group(Input_section_list::const_iterator,
881 Input_section_list::const_iterator,
882 Input_section_list::const_iterator,
883 Target_arm<big_endian>*,
884 std::vector<Output_relaxed_input_section*>*);
887 // Utilities for manipulating integers of up to 32-bits
891 // Sign extend an n-bit unsigned integer stored in an uint32_t into
892 // an int32_t. NO_BITS must be between 1 to 32.
893 template<int no_bits>
894 static inline int32_t
895 sign_extend(uint32_t bits)
897 gold_assert(no_bits >= 0 && no_bits <= 32);
899 return static_cast<int32_t>(bits);
900 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
902 uint32_t top_bit = 1U << (no_bits - 1);
903 int32_t as_signed = static_cast<int32_t>(bits);
904 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
907 // Detects overflow of an NO_BITS integer stored in a uint32_t.
908 template<int no_bits>
910 has_overflow(uint32_t bits)
912 gold_assert(no_bits >= 0 && no_bits <= 32);
915 int32_t max = (1 << (no_bits - 1)) - 1;
916 int32_t min = -(1 << (no_bits - 1));
917 int32_t as_signed = static_cast<int32_t>(bits);
918 return as_signed > max || as_signed < min;
921 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
922 // fits in the given number of bits as either a signed or unsigned value.
923 // For example, has_signed_unsigned_overflow<8> would check
924 // -128 <= bits <= 255
925 template<int no_bits>
927 has_signed_unsigned_overflow(uint32_t bits)
929 gold_assert(no_bits >= 2 && no_bits <= 32);
932 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
933 int32_t min = -(1 << (no_bits - 1));
934 int32_t as_signed = static_cast<int32_t>(bits);
935 return as_signed > max || as_signed < min;
938 // Select bits from A and B using bits in MASK. For each n in [0..31],
939 // the n-th bit in the result is chosen from the n-th bits of A and B.
940 // A zero selects A and a one selects B.
941 static inline uint32_t
942 bit_select(uint32_t a, uint32_t b, uint32_t mask)
943 { return (a & ~mask) | (b & mask); }
946 template<bool big_endian>
947 class Target_arm : public Sized_target<32, big_endian>
950 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
954 : Sized_target<32, big_endian>(&arm_info),
955 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
956 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
957 may_use_blx_(true), should_force_pic_veneer_(false)
960 // Whether we can use BLX.
963 { return this->may_use_blx_; }
967 set_may_use_blx(bool value)
968 { this->may_use_blx_ = value; }
970 // Whether we force PCI branch veneers.
972 should_force_pic_veneer() const
973 { return this->should_force_pic_veneer_; }
975 // Set PIC veneer flag.
977 set_should_force_pic_veneer(bool value)
978 { this->should_force_pic_veneer_ = value; }
980 // Whether we use THUMB-2 instructions.
984 // FIXME: This should not hard-coded.
988 // Whether we use THUMB/THUMB-2 instructions only.
990 using_thumb_only() const
992 // FIXME: This should not hard-coded.
996 // Process the relocations to determine unreferenced sections for
997 // garbage collection.
999 gc_process_relocs(const General_options& options,
1000 Symbol_table* symtab,
1002 Sized_relobj<32, big_endian>* object,
1003 unsigned int data_shndx,
1004 unsigned int sh_type,
1005 const unsigned char* prelocs,
1007 Output_section* output_section,
1008 bool needs_special_offset_handling,
1009 size_t local_symbol_count,
1010 const unsigned char* plocal_symbols);
1012 // Scan the relocations to look for symbol adjustments.
1014 scan_relocs(const General_options& options,
1015 Symbol_table* symtab,
1017 Sized_relobj<32, big_endian>* object,
1018 unsigned int data_shndx,
1019 unsigned int sh_type,
1020 const unsigned char* prelocs,
1022 Output_section* output_section,
1023 bool needs_special_offset_handling,
1024 size_t local_symbol_count,
1025 const unsigned char* plocal_symbols);
1027 // Finalize the sections.
1029 do_finalize_sections(Layout*);
1031 // Return the value to use for a dynamic symbol which requires special
1034 do_dynsym_value(const Symbol*) const;
1036 // Relocate a section.
1038 relocate_section(const Relocate_info<32, big_endian>*,
1039 unsigned int sh_type,
1040 const unsigned char* prelocs,
1042 Output_section* output_section,
1043 bool needs_special_offset_handling,
1044 unsigned char* view,
1045 elfcpp::Elf_types<32>::Elf_Addr view_address,
1046 section_size_type view_size,
1047 const Reloc_symbol_changes*);
1049 // Scan the relocs during a relocatable link.
1051 scan_relocatable_relocs(const General_options& options,
1052 Symbol_table* symtab,
1054 Sized_relobj<32, big_endian>* object,
1055 unsigned int data_shndx,
1056 unsigned int sh_type,
1057 const unsigned char* prelocs,
1059 Output_section* output_section,
1060 bool needs_special_offset_handling,
1061 size_t local_symbol_count,
1062 const unsigned char* plocal_symbols,
1063 Relocatable_relocs*);
1065 // Relocate a section during a relocatable link.
1067 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1068 unsigned int sh_type,
1069 const unsigned char* prelocs,
1071 Output_section* output_section,
1072 off_t offset_in_output_section,
1073 const Relocatable_relocs*,
1074 unsigned char* view,
1075 elfcpp::Elf_types<32>::Elf_Addr view_address,
1076 section_size_type view_size,
1077 unsigned char* reloc_view,
1078 section_size_type reloc_view_size);
1080 // Return whether SYM is defined by the ABI.
1082 do_is_defined_by_abi(Symbol* sym) const
1083 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1085 // Return the size of the GOT section.
1089 gold_assert(this->got_ != NULL);
1090 return this->got_->data_size();
1093 // Map platform-specific reloc types
1095 get_real_reloc_type (unsigned int r_type);
1097 // Get the default ARM target.
1098 static const Target_arm<big_endian>&
1101 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1102 && parameters->target().is_big_endian() == big_endian);
1103 return static_cast<const Target_arm<big_endian>&>(parameters->target());
1107 // The class which scans relocations.
1112 : issued_non_pic_error_(false)
1116 local(const General_options& options, Symbol_table* symtab,
1117 Layout* layout, Target_arm* target,
1118 Sized_relobj<32, big_endian>* object,
1119 unsigned int data_shndx,
1120 Output_section* output_section,
1121 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1122 const elfcpp::Sym<32, big_endian>& lsym);
1125 global(const General_options& options, Symbol_table* symtab,
1126 Layout* layout, Target_arm* target,
1127 Sized_relobj<32, big_endian>* object,
1128 unsigned int data_shndx,
1129 Output_section* output_section,
1130 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1135 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1136 unsigned int r_type);
1139 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1140 unsigned int r_type, Symbol*);
1143 check_non_pic(Relobj*, unsigned int r_type);
1145 // Almost identical to Symbol::needs_plt_entry except that it also
1146 // handles STT_ARM_TFUNC.
1148 symbol_needs_plt_entry(const Symbol* sym)
1150 // An undefined symbol from an executable does not need a PLT entry.
1151 if (sym->is_undefined() && !parameters->options().shared())
1154 return (!parameters->doing_static_link()
1155 && (sym->type() == elfcpp::STT_FUNC
1156 || sym->type() == elfcpp::STT_ARM_TFUNC)
1157 && (sym->is_from_dynobj()
1158 || sym->is_undefined()
1159 || sym->is_preemptible()));
1162 // Whether we have issued an error about a non-PIC compilation.
1163 bool issued_non_pic_error_;
1166 // The class which implements relocation.
1176 // Return whether the static relocation needs to be applied.
1178 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1181 Output_section* output_section);
1183 // Do a relocation. Return false if the caller should not issue
1184 // any warnings about this relocation.
1186 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1187 Output_section*, size_t relnum,
1188 const elfcpp::Rel<32, big_endian>&,
1189 unsigned int r_type, const Sized_symbol<32>*,
1190 const Symbol_value<32>*,
1191 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
1194 // Return whether we want to pass flag NON_PIC_REF for this
1197 reloc_is_non_pic (unsigned int r_type)
1201 case elfcpp::R_ARM_REL32:
1202 case elfcpp::R_ARM_THM_CALL:
1203 case elfcpp::R_ARM_CALL:
1204 case elfcpp::R_ARM_JUMP24:
1205 case elfcpp::R_ARM_PREL31:
1206 case elfcpp::R_ARM_THM_ABS5:
1207 case elfcpp::R_ARM_ABS8:
1208 case elfcpp::R_ARM_ABS12:
1209 case elfcpp::R_ARM_ABS16:
1210 case elfcpp::R_ARM_BASE_ABS:
1218 // A class which returns the size required for a relocation type,
1219 // used while scanning relocs during a relocatable link.
1220 class Relocatable_size_for_reloc
1224 get_size_for_reloc(unsigned int, Relobj*);
1227 // Get the GOT section, creating it if necessary.
1228 Output_data_got<32, big_endian>*
1229 got_section(Symbol_table*, Layout*);
1231 // Get the GOT PLT section.
1233 got_plt_section() const
1235 gold_assert(this->got_plt_ != NULL);
1236 return this->got_plt_;
1239 // Create a PLT entry for a global symbol.
1241 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1243 // Get the PLT section.
1244 const Output_data_plt_arm<big_endian>*
1247 gold_assert(this->plt_ != NULL);
1251 // Get the dynamic reloc section, creating it if necessary.
1253 rel_dyn_section(Layout*);
1255 // Return true if the symbol may need a COPY relocation.
1256 // References from an executable object to non-function symbols
1257 // defined in a dynamic object may need a COPY relocation.
1259 may_need_copy_reloc(Symbol* gsym)
1261 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1262 && gsym->may_need_copy_reloc());
1265 // Add a potential copy relocation.
1267 copy_reloc(Symbol_table* symtab, Layout* layout,
1268 Sized_relobj<32, big_endian>* object,
1269 unsigned int shndx, Output_section* output_section,
1270 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1272 this->copy_relocs_.copy_reloc(symtab, layout,
1273 symtab->get_sized_symbol<32>(sym),
1274 object, shndx, output_section, reloc,
1275 this->rel_dyn_section(layout));
1278 // Information about this specific target which we pass to the
1279 // general Target structure.
1280 static const Target::Target_info arm_info;
1282 // The types of GOT entries needed for this platform.
1285 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1289 Output_data_got<32, big_endian>* got_;
1291 Output_data_plt_arm<big_endian>* plt_;
1292 // The GOT PLT section.
1293 Output_data_space* got_plt_;
1294 // The dynamic reloc section.
1295 Reloc_section* rel_dyn_;
1296 // Relocs saved to avoid a COPY reloc.
1297 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1298 // Space for variables copied with a COPY reloc.
1299 Output_data_space* dynbss_;
1300 // Whether we can use BLX.
1302 // Whether we force PIC branch veneers.
1303 bool should_force_pic_veneer_;
1306 template<bool big_endian>
1307 const Target::Target_info Target_arm<big_endian>::arm_info =
1310 big_endian, // is_big_endian
1311 elfcpp::EM_ARM, // machine_code
1312 false, // has_make_symbol
1313 false, // has_resolve
1314 false, // has_code_fill
1315 true, // is_default_stack_executable
1317 "/usr/lib/libc.so.1", // dynamic_linker
1318 0x8000, // default_text_segment_address
1319 0x1000, // abi_pagesize (overridable by -z max-page-size)
1320 0x1000, // common_pagesize (overridable by -z common-page-size)
1321 elfcpp::SHN_UNDEF, // small_common_shndx
1322 elfcpp::SHN_UNDEF, // large_common_shndx
1323 0, // small_common_section_flags
1324 0 // large_common_section_flags
1327 // Arm relocate functions class
1330 template<bool big_endian>
1331 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1336 STATUS_OKAY, // No error during relocation.
1337 STATUS_OVERFLOW, // Relocation oveflow.
1338 STATUS_BAD_RELOC // Relocation cannot be applied.
1342 typedef Relocate_functions<32, big_endian> Base;
1343 typedef Arm_relocate_functions<big_endian> This;
1345 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
1346 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
1347 // to distinguish ARM and THUMB functions and it is treated specially.
1348 static inline Symbol_value<32>::Value
1349 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
1350 const Symbol_value<32>* psymval,
1351 Symbol_value<32>::Value addend,
1354 typedef Symbol_value<32>::Value Valtype;
1358 Valtype raw = psymval->value(object, 0);
1359 Valtype thumb_bit = raw & 1;
1360 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
1363 return psymval->value(object, addend);
1366 // Encoding of imm16 argument for movt and movw ARM instructions
1369 // imm16 := imm4 | imm12
1371 // 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
1372 // +-------+---------------+-------+-------+-----------------------+
1373 // | | |imm4 | |imm12 |
1374 // +-------+---------------+-------+-------+-----------------------+
1376 // Extract the relocation addend from VAL based on the ARM
1377 // instruction encoding described above.
1378 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1379 extract_arm_movw_movt_addend(
1380 typename elfcpp::Swap<32, big_endian>::Valtype val)
1382 // According to the Elf ABI for ARM Architecture the immediate
1383 // field is sign-extended to form the addend.
1384 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1387 // Insert X into VAL based on the ARM instruction encoding described
1389 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1390 insert_val_arm_movw_movt(
1391 typename elfcpp::Swap<32, big_endian>::Valtype val,
1392 typename elfcpp::Swap<32, big_endian>::Valtype x)
1396 val |= (x & 0xf000) << 4;
1400 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1403 // imm16 := imm4 | i | imm3 | imm8
1405 // 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
1406 // +---------+-+-----------+-------++-+-----+-------+---------------+
1407 // | |i| |imm4 || |imm3 | |imm8 |
1408 // +---------+-+-----------+-------++-+-----+-------+---------------+
1410 // Extract the relocation addend from VAL based on the Thumb2
1411 // instruction encoding described above.
1412 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1413 extract_thumb_movw_movt_addend(
1414 typename elfcpp::Swap<32, big_endian>::Valtype val)
1416 // According to the Elf ABI for ARM Architecture the immediate
1417 // field is sign-extended to form the addend.
1418 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1419 | ((val >> 15) & 0x0800)
1420 | ((val >> 4) & 0x0700)
1424 // Insert X into VAL based on the Thumb2 instruction encoding
1426 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1427 insert_val_thumb_movw_movt(
1428 typename elfcpp::Swap<32, big_endian>::Valtype val,
1429 typename elfcpp::Swap<32, big_endian>::Valtype x)
1432 val |= (x & 0xf000) << 4;
1433 val |= (x & 0x0800) << 15;
1434 val |= (x & 0x0700) << 4;
1435 val |= (x & 0x00ff);
1439 // FIXME: This probably only works for Android on ARM v5te. We should
1440 // following GNU ld for the general case.
1441 template<unsigned r_type>
1442 static inline typename This::Status
1443 arm_branch_common(unsigned char *view,
1444 const Sized_relobj<32, big_endian>* object,
1445 const Symbol_value<32>* psymval,
1446 elfcpp::Elf_types<32>::Elf_Addr address,
1449 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1450 Valtype* wv = reinterpret_cast<Valtype*>(view);
1451 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1453 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
1454 && ((val & 0x0f000000UL) == 0x0a000000UL);
1455 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
1456 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
1457 && ((val & 0x0f000000UL) == 0x0b000000UL);
1458 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
1459 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
1461 if (r_type == elfcpp::R_ARM_CALL)
1463 if (!insn_is_uncond_bl && !insn_is_blx)
1464 return This::STATUS_BAD_RELOC;
1466 else if (r_type == elfcpp::R_ARM_JUMP24)
1468 if (!insn_is_b && !insn_is_cond_bl)
1469 return This::STATUS_BAD_RELOC;
1471 else if (r_type == elfcpp::R_ARM_PLT32)
1473 if (!insn_is_any_branch)
1474 return This::STATUS_BAD_RELOC;
1479 Valtype addend = utils::sign_extend<26>(val << 2);
1480 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1483 // If target has thumb bit set, we need to either turn the BL
1484 // into a BLX (for ARMv5 or above) or generate a stub.
1488 if (insn_is_uncond_bl)
1489 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
1491 return This::STATUS_BAD_RELOC;
1494 gold_assert(!insn_is_blx);
1496 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
1497 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1498 return (utils::has_overflow<26>(x)
1499 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
1504 // R_ARM_ABS8: S + A
1505 static inline typename This::Status
1506 abs8(unsigned char *view,
1507 const Sized_relobj<32, big_endian>* object,
1508 const Symbol_value<32>* psymval)
1510 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
1511 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1512 Valtype* wv = reinterpret_cast<Valtype*>(view);
1513 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
1514 Reltype addend = utils::sign_extend<8>(val);
1515 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1516 val = utils::bit_select(val, x, 0xffU);
1517 elfcpp::Swap<8, big_endian>::writeval(wv, val);
1518 return (utils::has_signed_unsigned_overflow<8>(x)
1519 ? This::STATUS_OVERFLOW
1520 : This::STATUS_OKAY);
1523 // R_ARM_THM_ABS5: S + A
1524 static inline typename This::Status
1525 thm_abs5(unsigned char *view,
1526 const Sized_relobj<32, big_endian>* object,
1527 const Symbol_value<32>* psymval)
1529 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1530 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1531 Valtype* wv = reinterpret_cast<Valtype*>(view);
1532 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1533 Reltype addend = (val & 0x7e0U) >> 6;
1534 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1535 val = utils::bit_select(val, x << 6, 0x7e0U);
1536 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1537 return (utils::has_overflow<5>(x)
1538 ? This::STATUS_OVERFLOW
1539 : This::STATUS_OKAY);
1542 // R_ARM_ABS12: S + A
1543 static inline typename This::Status
1544 abs12(unsigned char *view,
1545 const Sized_relobj<32, big_endian>* object,
1546 const Symbol_value<32>* psymval)
1548 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1549 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1550 Valtype* wv = reinterpret_cast<Valtype*>(view);
1551 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1552 Reltype addend = val & 0x0fffU;
1553 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1554 val = utils::bit_select(val, x, 0x0fffU);
1555 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1556 return (utils::has_overflow<12>(x)
1557 ? This::STATUS_OVERFLOW
1558 : This::STATUS_OKAY);
1561 // R_ARM_ABS16: S + A
1562 static inline typename This::Status
1563 abs16(unsigned char *view,
1564 const Sized_relobj<32, big_endian>* object,
1565 const Symbol_value<32>* psymval)
1567 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1568 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1569 Valtype* wv = reinterpret_cast<Valtype*>(view);
1570 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1571 Reltype addend = utils::sign_extend<16>(val);
1572 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1573 val = utils::bit_select(val, x, 0xffffU);
1574 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1575 return (utils::has_signed_unsigned_overflow<16>(x)
1576 ? This::STATUS_OVERFLOW
1577 : This::STATUS_OKAY);
1580 // R_ARM_ABS32: (S + A) | T
1581 static inline typename This::Status
1582 abs32(unsigned char *view,
1583 const Sized_relobj<32, big_endian>* object,
1584 const Symbol_value<32>* psymval,
1587 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1588 Valtype* wv = reinterpret_cast<Valtype*>(view);
1589 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1590 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1591 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1592 return This::STATUS_OKAY;
1595 // R_ARM_REL32: (S + A) | T - P
1596 static inline typename This::Status
1597 rel32(unsigned char *view,
1598 const Sized_relobj<32, big_endian>* object,
1599 const Symbol_value<32>* psymval,
1600 elfcpp::Elf_types<32>::Elf_Addr address,
1603 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1604 Valtype* wv = reinterpret_cast<Valtype*>(view);
1605 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1606 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1608 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1609 return This::STATUS_OKAY;
1612 // R_ARM_THM_CALL: (S + A) | T - P
1613 static inline typename This::Status
1614 thm_call(unsigned char *view,
1615 const Sized_relobj<32, big_endian>* object,
1616 const Symbol_value<32>* psymval,
1617 elfcpp::Elf_types<32>::Elf_Addr address,
1620 // A thumb call consists of two instructions.
1621 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1622 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1623 Valtype* wv = reinterpret_cast<Valtype*>(view);
1624 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
1625 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
1626 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1627 gold_assert((lo & 0xf800) == 0xf800);
1628 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
1629 | ((lo & 0x7ff) << 1));
1630 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1633 // If target has no thumb bit set, we need to either turn the BL
1634 // into a BLX (for ARMv5 or above) or generate a stub.
1637 // This only works for ARMv5 and above with interworking enabled.
1640 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
1641 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
1642 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
1643 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
1644 return (utils::has_overflow<23>(x)
1645 ? This::STATUS_OVERFLOW
1646 : This::STATUS_OKAY);
1649 // R_ARM_BASE_PREL: B(S) + A - P
1650 static inline typename This::Status
1651 base_prel(unsigned char* view,
1652 elfcpp::Elf_types<32>::Elf_Addr origin,
1653 elfcpp::Elf_types<32>::Elf_Addr address)
1655 Base::rel32(view, origin - address);
1659 // R_ARM_BASE_ABS: B(S) + A
1660 static inline typename This::Status
1661 base_abs(unsigned char* view,
1662 elfcpp::Elf_types<32>::Elf_Addr origin)
1664 Base::rel32(view, origin);
1668 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1669 static inline typename This::Status
1670 got_brel(unsigned char* view,
1671 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1673 Base::rel32(view, got_offset);
1674 return This::STATUS_OKAY;
1677 // R_ARM_GOT_PREL: GOT(S) + A – P
1678 static inline typename This::Status
1679 got_prel(unsigned char* view,
1680 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
1681 elfcpp::Elf_types<32>::Elf_Addr address)
1683 Base::rel32(view, got_offset - address);
1684 return This::STATUS_OKAY;
1687 // R_ARM_PLT32: (S + A) | T - P
1688 static inline typename This::Status
1689 plt32(unsigned char *view,
1690 const Sized_relobj<32, big_endian>* object,
1691 const Symbol_value<32>* psymval,
1692 elfcpp::Elf_types<32>::Elf_Addr address,
1695 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
1696 address, has_thumb_bit);
1699 // R_ARM_CALL: (S + A) | T - P
1700 static inline typename This::Status
1701 call(unsigned char *view,
1702 const Sized_relobj<32, big_endian>* object,
1703 const Symbol_value<32>* psymval,
1704 elfcpp::Elf_types<32>::Elf_Addr address,
1707 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
1708 address, has_thumb_bit);
1711 // R_ARM_JUMP24: (S + A) | T - P
1712 static inline typename This::Status
1713 jump24(unsigned char *view,
1714 const Sized_relobj<32, big_endian>* object,
1715 const Symbol_value<32>* psymval,
1716 elfcpp::Elf_types<32>::Elf_Addr address,
1719 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
1720 address, has_thumb_bit);
1723 // R_ARM_PREL: (S + A) | T - P
1724 static inline typename This::Status
1725 prel31(unsigned char *view,
1726 const Sized_relobj<32, big_endian>* object,
1727 const Symbol_value<32>* psymval,
1728 elfcpp::Elf_types<32>::Elf_Addr address,
1731 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1732 Valtype* wv = reinterpret_cast<Valtype*>(view);
1733 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1734 Valtype addend = utils::sign_extend<31>(val);
1735 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1737 val = utils::bit_select(val, x, 0x7fffffffU);
1738 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1739 return (utils::has_overflow<31>(x) ?
1740 This::STATUS_OVERFLOW : This::STATUS_OKAY);
1743 // R_ARM_MOVW_ABS_NC: (S + A) | T
1744 static inline typename This::Status
1745 movw_abs_nc(unsigned char *view,
1746 const Sized_relobj<32, big_endian>* object,
1747 const Symbol_value<32>* psymval,
1750 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1751 Valtype* wv = reinterpret_cast<Valtype*>(view);
1752 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1753 Valtype addend = This::extract_arm_movw_movt_addend(val);
1754 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1755 val = This::insert_val_arm_movw_movt(val, x);
1756 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1757 return This::STATUS_OKAY;
1760 // R_ARM_MOVT_ABS: S + A
1761 static inline typename This::Status
1762 movt_abs(unsigned char *view,
1763 const Sized_relobj<32, big_endian>* object,
1764 const Symbol_value<32>* psymval)
1766 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1767 Valtype* wv = reinterpret_cast<Valtype*>(view);
1768 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1769 Valtype addend = This::extract_arm_movw_movt_addend(val);
1770 Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1771 val = This::insert_val_arm_movw_movt(val, x);
1772 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1773 return This::STATUS_OKAY;
1776 // R_ARM_THM_MOVW_ABS_NC: S + A | T
1777 static inline typename This::Status
1778 thm_movw_abs_nc(unsigned char *view,
1779 const Sized_relobj<32, big_endian>* object,
1780 const Symbol_value<32>* psymval,
1783 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1784 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1785 Valtype* wv = reinterpret_cast<Valtype*>(view);
1786 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1787 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1788 Reltype addend = extract_thumb_movw_movt_addend(val);
1789 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1790 val = This::insert_val_thumb_movw_movt(val, x);
1791 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1792 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1793 return This::STATUS_OKAY;
1796 // R_ARM_THM_MOVT_ABS: S + A
1797 static inline typename This::Status
1798 thm_movt_abs(unsigned char *view,
1799 const Sized_relobj<32, big_endian>* object,
1800 const Symbol_value<32>* psymval)
1802 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1803 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1804 Valtype* wv = reinterpret_cast<Valtype*>(view);
1805 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1806 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1807 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1808 Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1809 val = This::insert_val_thumb_movw_movt(val, x);
1810 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1811 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1812 return This::STATUS_OKAY;
1815 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
1816 static inline typename This::Status
1817 movw_prel_nc(unsigned char *view,
1818 const Sized_relobj<32, big_endian>* object,
1819 const Symbol_value<32>* psymval,
1820 elfcpp::Elf_types<32>::Elf_Addr address,
1823 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1824 Valtype* wv = reinterpret_cast<Valtype*>(view);
1825 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1826 Valtype addend = This::extract_arm_movw_movt_addend(val);
1827 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1829 val = This::insert_val_arm_movw_movt(val, x);
1830 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1831 return This::STATUS_OKAY;
1834 // R_ARM_MOVT_PREL: S + A - P
1835 static inline typename This::Status
1836 movt_prel(unsigned char *view,
1837 const Sized_relobj<32, big_endian>* object,
1838 const Symbol_value<32>* psymval,
1839 elfcpp::Elf_types<32>::Elf_Addr address)
1841 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1842 Valtype* wv = reinterpret_cast<Valtype*>(view);
1843 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1844 Valtype addend = This::extract_arm_movw_movt_addend(val);
1845 Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
1847 val = This::insert_val_arm_movw_movt(val, x);
1848 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1849 return This::STATUS_OKAY;
1852 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
1853 static inline typename This::Status
1854 thm_movw_prel_nc(unsigned char *view,
1855 const Sized_relobj<32, big_endian>* object,
1856 const Symbol_value<32>* psymval,
1857 elfcpp::Elf_types<32>::Elf_Addr address,
1860 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1861 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1862 Valtype* wv = reinterpret_cast<Valtype*>(view);
1863 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1864 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1865 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1866 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1868 val = This::insert_val_thumb_movw_movt(val, x);
1869 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1870 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1871 return This::STATUS_OKAY;
1874 // R_ARM_THM_MOVT_PREL: S + A - P
1875 static inline typename This::Status
1876 thm_movt_prel(unsigned char *view,
1877 const Sized_relobj<32, big_endian>* object,
1878 const Symbol_value<32>* psymval,
1879 elfcpp::Elf_types<32>::Elf_Addr address)
1881 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1882 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1883 Valtype* wv = reinterpret_cast<Valtype*>(view);
1884 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1885 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1886 Reltype addend = This::extract_thumb_movw_movt_addend(val);
1887 Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
1889 val = This::insert_val_thumb_movw_movt(val, x);
1890 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1891 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1892 return This::STATUS_OKAY;
1896 // Get the GOT section, creating it if necessary.
1898 template<bool big_endian>
1899 Output_data_got<32, big_endian>*
1900 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
1902 if (this->got_ == NULL)
1904 gold_assert(symtab != NULL && layout != NULL);
1906 this->got_ = new Output_data_got<32, big_endian>();
1909 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1911 | elfcpp::SHF_WRITE),
1915 // The old GNU linker creates a .got.plt section. We just
1916 // create another set of data in the .got section. Note that we
1917 // always create a PLT if we create a GOT, although the PLT
1919 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
1920 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1922 | elfcpp::SHF_WRITE),
1926 // The first three entries are reserved.
1927 this->got_plt_->set_current_data_size(3 * 4);
1929 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1930 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1932 0, 0, elfcpp::STT_OBJECT,
1934 elfcpp::STV_HIDDEN, 0,
1940 // Get the dynamic reloc section, creating it if necessary.
1942 template<bool big_endian>
1943 typename Target_arm<big_endian>::Reloc_section*
1944 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
1946 if (this->rel_dyn_ == NULL)
1948 gold_assert(layout != NULL);
1949 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
1950 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1951 elfcpp::SHF_ALLOC, this->rel_dyn_);
1953 return this->rel_dyn_;
1956 // Insn_template methods.
1958 // Return byte size of an instruction template.
1961 Insn_template::size() const
1963 switch (this->type())
1976 // Return alignment of an instruction template.
1979 Insn_template::alignment() const
1981 switch (this->type())
1994 // Stub_template methods.
1996 Stub_template::Stub_template(
1997 Stub_type type, const Insn_template* insns,
1999 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2000 entry_in_thumb_mode_(false), relocs_()
2004 // Compute byte size and alignment of stub template.
2005 for (size_t i = 0; i < insn_count; i++)
2007 unsigned insn_alignment = insns[i].alignment();
2008 size_t insn_size = insns[i].size();
2009 gold_assert((offset & (insn_alignment - 1)) == 0);
2010 this->alignment_ = std::max(this->alignment_, insn_alignment);
2011 switch (insns[i].type())
2013 case Insn_template::THUMB16_TYPE:
2015 this->entry_in_thumb_mode_ = true;
2018 case Insn_template::THUMB32_TYPE:
2019 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2020 this->relocs_.push_back(Reloc(i, offset));
2022 this->entry_in_thumb_mode_ = true;
2025 case Insn_template::ARM_TYPE:
2026 // Handle cases where the target is encoded within the
2028 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2029 this->relocs_.push_back(Reloc(i, offset));
2032 case Insn_template::DATA_TYPE:
2033 // Entry point cannot be data.
2034 gold_assert(i != 0);
2035 this->relocs_.push_back(Reloc(i, offset));
2041 offset += insn_size;
2043 this->size_ = offset;
2046 // Reloc_stub::Key methods.
2048 // Dump a Key as a string for debugging.
2051 Reloc_stub::Key::name() const
2053 if (this->r_sym_ == invalid_index)
2055 // Global symbol key name
2056 // <stub-type>:<symbol name>:<addend>.
2057 const std::string sym_name = this->u_.symbol->name();
2058 // We need to print two hex number and two colons. So just add 100 bytes
2059 // to the symbol name size.
2060 size_t len = sym_name.size() + 100;
2061 char* buffer = new char[len];
2062 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2063 sym_name.c_str(), this->addend_);
2064 gold_assert(c > 0 && c < static_cast<int>(len));
2066 return std::string(buffer);
2070 // local symbol key name
2071 // <stub-type>:<object>:<r_sym>:<addend>.
2072 const size_t len = 200;
2074 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2075 this->u_.relobj, this->r_sym_, this->addend_);
2076 gold_assert(c > 0 && c < static_cast<int>(len));
2077 return std::string(buffer);
2081 // Reloc_stub methods.
2083 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2084 // LOCATION to DESTINATION.
2085 // This code is based on the arm_type_of_stub function in
2086 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2090 Reloc_stub::stub_type_for_reloc(
2091 unsigned int r_type,
2092 Arm_address location,
2093 Arm_address destination,
2094 bool target_is_thumb)
2096 Stub_type stub_type = arm_stub_none;
2098 // This is a bit ugly but we want to avoid using a templated class for
2099 // big and little endianities.
2101 bool should_force_pic_veneer;
2104 if (parameters->target().is_big_endian())
2106 const Target_arm<true>& big_endian_target =
2107 Target_arm<true>::default_target();
2108 may_use_blx = big_endian_target.may_use_blx();
2109 should_force_pic_veneer = big_endian_target.should_force_pic_veneer();
2110 thumb2 = big_endian_target.using_thumb2();
2111 thumb_only = big_endian_target.using_thumb_only();
2115 const Target_arm<false>& little_endian_target =
2116 Target_arm<false>::default_target();
2117 may_use_blx = little_endian_target.may_use_blx();
2118 should_force_pic_veneer = little_endian_target.should_force_pic_veneer();
2119 thumb2 = little_endian_target.using_thumb2();
2120 thumb_only = little_endian_target.using_thumb_only();
2123 int64_t branch_offset = (int64_t)destination - location;
2125 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2127 // Handle cases where:
2128 // - this call goes too far (different Thumb/Thumb2 max
2130 // - it's a Thumb->Arm call and blx is not available, or it's a
2131 // Thumb->Arm branch (not bl). A stub is needed in this case.
2133 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2134 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2136 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2137 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2138 || ((!target_is_thumb)
2139 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2140 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
2142 if (target_is_thumb)
2147 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2150 && (r_type == elfcpp::R_ARM_THM_CALL))
2151 // V5T and above. Stub starts with ARM code, so
2152 // we must be able to switch mode before
2153 // reaching it, which is only possible for 'bl'
2154 // (ie R_ARM_THM_CALL relocation).
2155 ? arm_stub_long_branch_any_thumb_pic
2156 // On V4T, use Thumb code only.
2157 : arm_stub_long_branch_v4t_thumb_thumb_pic)
2161 && (r_type == elfcpp::R_ARM_THM_CALL))
2162 ? arm_stub_long_branch_any_any // V5T and above.
2163 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
2167 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2168 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
2169 : arm_stub_long_branch_thumb_only; // non-PIC stub.
2176 // FIXME: We should check that the input section is from an
2177 // object that has interwork enabled.
2179 stub_type = (parameters->options().shared()
2180 || should_force_pic_veneer)
2183 && (r_type == elfcpp::R_ARM_THM_CALL))
2184 ? arm_stub_long_branch_any_arm_pic // V5T and above.
2185 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
2189 && (r_type == elfcpp::R_ARM_THM_CALL))
2190 ? arm_stub_long_branch_any_any // V5T and above.
2191 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
2193 // Handle v4t short branches.
2194 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
2195 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
2196 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
2197 stub_type = arm_stub_short_branch_v4t_thumb_arm;
2201 else if (r_type == elfcpp::R_ARM_CALL
2202 || r_type == elfcpp::R_ARM_JUMP24
2203 || r_type == elfcpp::R_ARM_PLT32)
2205 if (target_is_thumb)
2209 // FIXME: We should check that the input section is from an
2210 // object that has interwork enabled.
2212 // We have an extra 2-bytes reach because of
2213 // the mode change (bit 24 (H) of BLX encoding).
2214 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
2215 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2216 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
2217 || (r_type == elfcpp::R_ARM_JUMP24)
2218 || (r_type == elfcpp::R_ARM_PLT32))
2220 stub_type = (parameters->options().shared()
2221 || should_force_pic_veneer)
2224 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
2225 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
2229 ? arm_stub_long_branch_any_any // V5T and above.
2230 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
2236 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
2237 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
2239 stub_type = (parameters->options().shared()
2240 || should_force_pic_veneer)
2241 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
2242 : arm_stub_long_branch_any_any; /// non-PIC.
2250 // Template to implement do_write for a specific target endianity.
2252 template<bool big_endian>
2254 Reloc_stub::do_fixed_endian_write(unsigned char* view,
2255 section_size_type view_size)
2257 const Stub_template* stub_template = this->stub_template();
2258 const Insn_template* insns = stub_template->insns();
2260 // FIXME: We do not handle BE8 encoding yet.
2261 unsigned char* pov = view;
2262 for (size_t i = 0; i < stub_template->insn_count(); i++)
2264 switch (insns[i].type())
2266 case Insn_template::THUMB16_TYPE:
2267 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2268 gold_assert(insns[i].reloc_addend() == 0);
2269 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2271 case Insn_template::THUMB32_TYPE:
2273 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2274 uint32_t lo = insns[i].data() & 0xffff;
2275 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2276 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2279 case Insn_template::ARM_TYPE:
2280 case Insn_template::DATA_TYPE:
2281 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2286 pov += insns[i].size();
2288 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2291 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2294 Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2298 this->do_fixed_endian_write<true>(view, view_size);
2300 this->do_fixed_endian_write<false>(view, view_size);
2303 // Stub_factory methods.
2305 Stub_factory::Stub_factory()
2307 // The instruction template sequences are declared as static
2308 // objects and initialized first time the constructor runs.
2310 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2311 // to reach the stub if necessary.
2312 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2314 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2315 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2316 // dcd R_ARM_ABS32(X)
2319 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2321 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2323 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2324 Insn_template::arm_insn(0xe12fff1c), // bx ip
2325 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2326 // dcd R_ARM_ABS32(X)
2329 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2330 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2332 Insn_template::thumb16_insn(0xb401), // push {r0}
2333 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2334 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2335 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2336 Insn_template::thumb16_insn(0x4760), // bx ip
2337 Insn_template::thumb16_insn(0xbf00), // nop
2338 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2339 // dcd R_ARM_ABS32(X)
2342 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2344 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2346 Insn_template::thumb16_insn(0x4778), // bx pc
2347 Insn_template::thumb16_insn(0x46c0), // nop
2348 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2349 Insn_template::arm_insn(0xe12fff1c), // bx ip
2350 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2351 // dcd R_ARM_ABS32(X)
2354 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2356 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2358 Insn_template::thumb16_insn(0x4778), // bx pc
2359 Insn_template::thumb16_insn(0x46c0), // nop
2360 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2361 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2362 // dcd R_ARM_ABS32(X)
2365 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2366 // one, when the destination is close enough.
2367 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2369 Insn_template::thumb16_insn(0x4778), // bx pc
2370 Insn_template::thumb16_insn(0x46c0), // nop
2371 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2374 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2375 // blx to reach the stub if necessary.
2376 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2378 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2379 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2380 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2381 // dcd R_ARM_REL32(X-4)
2384 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2385 // blx to reach the stub if necessary. We can not add into pc;
2386 // it is not guaranteed to mode switch (different in ARMv6 and
2388 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2390 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2391 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2392 Insn_template::arm_insn(0xe12fff1c), // bx ip
2393 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2394 // dcd R_ARM_REL32(X)
2397 // V4T ARM -> ARM long branch stub, PIC.
2398 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2400 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2401 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2402 Insn_template::arm_insn(0xe12fff1c), // bx ip
2403 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2404 // dcd R_ARM_REL32(X)
2407 // V4T Thumb -> ARM long branch stub, PIC.
2408 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2410 Insn_template::thumb16_insn(0x4778), // bx pc
2411 Insn_template::thumb16_insn(0x46c0), // nop
2412 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2413 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2414 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2415 // dcd R_ARM_REL32(X)
2418 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2420 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
2422 Insn_template::thumb16_insn(0xb401), // push {r0}
2423 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2424 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2425 Insn_template::thumb16_insn(0x4484), // add ip, r0
2426 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2427 Insn_template::thumb16_insn(0x4760), // bx ip
2428 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
2429 // dcd R_ARM_REL32(X)
2432 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2434 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2436 Insn_template::thumb16_insn(0x4778), // bx pc
2437 Insn_template::thumb16_insn(0x46c0), // nop
2438 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2439 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2440 Insn_template::arm_insn(0xe12fff1c), // bx ip
2441 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2442 // dcd R_ARM_REL32(X)
2445 // Cortex-A8 erratum-workaround stubs.
2447 // Stub used for conditional branches (which may be beyond +/-1MB away,
2448 // so we can't use a conditional branch to reach this stub).
2455 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
2457 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2458 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2459 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2463 // Stub used for b.w and bl.w instructions.
2465 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
2467 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2470 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
2472 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2475 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2476 // instruction (which switches to ARM mode) to point to this stub. Jump to
2477 // the real destination using an ARM-mode branch.
2478 const Insn_template elf32_arm_stub_a8_veneer_blx[] =
2480 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2483 // Fill in the stub template look-up table. Stub templates are constructed
2484 // per instance of Stub_factory for fast look-up without locking
2485 // in a thread-enabled environment.
2487 this->stub_templates_[arm_stub_none] =
2488 new Stub_template(arm_stub_none, NULL, 0);
2490 #define DEF_STUB(x) \
2494 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2495 Stub_type type = arm_stub_##x; \
2496 this->stub_templates_[type] = \
2497 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2505 // Stub_table methods.
2507 // Add a STUB with using KEY. Caller is reponsible for avoid adding
2508 // if already a STUB with the same key has been added.
2510 template<bool big_endian>
2512 Stub_table<big_endian>::add_reloc_stub(
2514 const Reloc_stub::Key& key)
2516 const Stub_template* stub_template = stub->stub_template();
2517 gold_assert(stub_template->type() == key.stub_type());
2518 this->reloc_stubs_[key] = stub;
2519 if (this->addralign_ < stub_template->alignment())
2520 this->addralign_ = stub_template->alignment();
2521 this->has_been_changed_ = true;
2524 template<bool big_endian>
2526 Stub_table<big_endian>::relocate_stubs(
2527 const Relocate_info<32, big_endian>* relinfo,
2528 Target_arm<big_endian>* arm_target,
2529 Output_section* output_section,
2530 unsigned char* view,
2531 Arm_address address,
2532 section_size_type view_size)
2534 // If we are passed a view bigger than the stub table's. we need to
2536 gold_assert(address == this->address()
2538 == static_cast<section_size_type>(this->data_size())));
2540 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2541 p != this->reloc_stubs_.end();
2544 Reloc_stub* stub = p->second;
2545 const Stub_template* stub_template = stub->stub_template();
2546 if (stub_template->reloc_count() != 0)
2548 // Adjust view to cover the stub only.
2549 section_size_type offset = stub->offset();
2550 section_size_type stub_size = stub_template->size();
2551 gold_assert(offset + stub_size <= view_size);
2553 arm_target->relocate_stub(stub, relinfo, output_section,
2554 view + offset, address + offset,
2560 // Reset address and file offset.
2562 template<bool big_endian>
2564 Stub_table<big_endian>::do_reset_address_and_file_offset()
2567 uint64_t max_addralign = 1;
2568 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2569 p != this->reloc_stubs_.end();
2572 Reloc_stub* stub = p->second;
2573 const Stub_template* stub_template = stub->stub_template();
2574 uint64_t stub_addralign = stub_template->alignment();
2575 max_addralign = std::max(max_addralign, stub_addralign);
2576 off = align_address(off, stub_addralign);
2577 stub->set_offset(off);
2578 stub->reset_destination_address();
2579 off += stub_template->size();
2582 this->addralign_ = max_addralign;
2583 this->set_current_data_size_for_child(off);
2586 // Write out the stubs to file.
2588 template<bool big_endian>
2590 Stub_table<big_endian>::do_write(Output_file* of)
2592 off_t offset = this->offset();
2593 const section_size_type oview_size =
2594 convert_to_section_size_type(this->data_size());
2595 unsigned char* const oview = of->get_output_view(offset, oview_size);
2597 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2598 p != this->reloc_stubs_.end();
2601 Reloc_stub* stub = p->second;
2602 Arm_address address = this->address() + stub->offset();
2604 == align_address(address,
2605 stub->stub_template()->alignment()));
2606 stub->write(oview + stub->offset(), stub->stub_template()->size(),
2609 of->write_output_view(this->offset(), oview_size, oview);
2612 // Arm_input_section methods.
2614 // Initialize an Arm_input_section.
2616 template<bool big_endian>
2618 Arm_input_section<big_endian>::init()
2620 Relobj* relobj = this->relobj();
2621 unsigned int shndx = this->shndx();
2623 // Cache these to speed up size and alignment queries. It is too slow
2624 // to call section_addraglin and section_size every time.
2625 this->original_addralign_ = relobj->section_addralign(shndx);
2626 this->original_size_ = relobj->section_size(shndx);
2628 // We want to make this look like the original input section after
2629 // output sections are finalized.
2630 Output_section* os = relobj->output_section(shndx);
2631 off_t offset = relobj->output_section_offset(shndx);
2632 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2633 this->set_address(os->address() + offset);
2634 this->set_file_offset(os->offset() + offset);
2636 this->set_current_data_size(this->original_size_);
2637 this->finalize_data_size();
2640 template<bool big_endian>
2642 Arm_input_section<big_endian>::do_write(Output_file* of)
2644 // We have to write out the original section content.
2645 section_size_type section_size;
2646 const unsigned char* section_contents =
2647 this->relobj()->section_contents(this->shndx(), §ion_size, false);
2648 of->write(this->offset(), section_contents, section_size);
2650 // If this owns a stub table and it is not empty, write it.
2651 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2652 this->stub_table_->write(of);
2655 // Finalize data size.
2657 template<bool big_endian>
2659 Arm_input_section<big_endian>::set_final_data_size()
2661 // If this owns a stub table, finalize its data size as well.
2662 if (this->is_stub_table_owner())
2664 uint64_t address = this->address();
2666 // The stub table comes after the original section contents.
2667 address += this->original_size_;
2668 address = align_address(address, this->stub_table_->addralign());
2669 off_t offset = this->offset() + (address - this->address());
2670 this->stub_table_->set_address_and_file_offset(address, offset);
2671 address += this->stub_table_->data_size();
2672 gold_assert(address == this->address() + this->current_data_size());
2675 this->set_data_size(this->current_data_size());
2678 // Reset address and file offset.
2680 template<bool big_endian>
2682 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
2684 // Size of the original input section contents.
2685 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2687 // If this is a stub table owner, account for the stub table size.
2688 if (this->is_stub_table_owner())
2690 Stub_table<big_endian>* stub_table = this->stub_table_;
2692 // Reset the stub table's address and file offset. The
2693 // current data size for child will be updated after that.
2694 stub_table_->reset_address_and_file_offset();
2695 off = align_address(off, stub_table_->addralign());
2696 off += stub_table->current_data_size();
2699 this->set_current_data_size(off);
2702 // Arm_output_section methods.
2704 // Create a stub group for input sections from BEGIN to END. OWNER
2705 // points to the input section to be the owner a new stub table.
2707 template<bool big_endian>
2709 Arm_output_section<big_endian>::create_stub_group(
2710 Input_section_list::const_iterator begin,
2711 Input_section_list::const_iterator end,
2712 Input_section_list::const_iterator owner,
2713 Target_arm<big_endian>* target,
2714 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
2716 // Currently we convert ordinary input sections into relaxed sections only
2717 // at this point but we may want to support creating relaxed input section
2718 // very early. So we check here to see if owner is already a relaxed
2721 Arm_input_section<big_endian>* arm_input_section;
2722 if (owner->is_relaxed_input_section())
2725 Arm_input_section<big_endian>::as_arm_input_section(
2726 owner->relaxed_input_section());
2730 gold_assert(owner->is_input_section());
2731 // Create a new relaxed input section.
2733 target->new_arm_input_section(owner->relobj(), owner->shndx());
2734 new_relaxed_sections->push_back(arm_input_section);
2737 // Create a stub table.
2738 Stub_table<big_endian>* stub_table =
2739 target->new_stub_table(arm_input_section);
2741 arm_input_section->set_stub_table(stub_table);
2743 Input_section_list::const_iterator p = begin;
2744 Input_section_list::const_iterator prev_p;
2746 // Look for input sections or relaxed input sections in [begin ... end].
2749 if (p->is_input_section() || p->is_relaxed_input_section())
2751 // The stub table information for input sections live
2752 // in their objects.
2753 Arm_relobj<big_endian>* arm_relobj =
2754 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
2755 arm_relobj->set_stub_table(p->shndx(), stub_table);
2759 while (prev_p != end);
2762 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
2763 // of stub groups. We grow a stub group by adding input section until the
2764 // size is just below GROUP_SIZE. The last input section will be converted
2765 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
2766 // input section after the stub table, effectively double the group size.
2768 // This is similar to the group_sections() function in elf32-arm.c but is
2769 // implemented differently.
2771 template<bool big_endian>
2773 Arm_output_section<big_endian>::group_sections(
2774 section_size_type group_size,
2775 bool stubs_always_after_branch,
2776 Target_arm<big_endian>* target)
2778 // We only care about sections containing code.
2779 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
2782 // States for grouping.
2785 // No group is being built.
2787 // A group is being built but the stub table is not found yet.
2788 // We keep group a stub group until the size is just under GROUP_SIZE.
2789 // The last input section in the group will be used as the stub table.
2790 FINDING_STUB_SECTION,
2791 // A group is being built and we have already found a stub table.
2792 // We enter this state to grow a stub group by adding input section
2793 // after the stub table. This effectively doubles the group size.
2797 // Any newly created relaxed sections are stored here.
2798 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
2800 State state = NO_GROUP;
2801 section_size_type off = 0;
2802 section_size_type group_begin_offset = 0;
2803 section_size_type group_end_offset = 0;
2804 section_size_type stub_table_end_offset = 0;
2805 Input_section_list::const_iterator group_begin =
2806 this->input_sections().end();
2807 Input_section_list::const_iterator stub_table =
2808 this->input_sections().end();
2809 Input_section_list::const_iterator group_end = this->input_sections().end();
2810 for (Input_section_list::const_iterator p = this->input_sections().begin();
2811 p != this->input_sections().end();
2814 section_size_type section_begin_offset =
2815 align_address(off, p->addralign());
2816 section_size_type section_end_offset =
2817 section_begin_offset + p->data_size();
2819 // Check to see if we should group the previously seens sections.
2825 case FINDING_STUB_SECTION:
2826 // Adding this section makes the group larger than GROUP_SIZE.
2827 if (section_end_offset - group_begin_offset >= group_size)
2829 if (stubs_always_after_branch)
2831 gold_assert(group_end != this->input_sections().end());
2832 this->create_stub_group(group_begin, group_end, group_end,
2833 target, &new_relaxed_sections);
2838 // But wait, there's more! Input sections up to
2839 // stub_group_size bytes after the stub table can be
2840 // handled by it too.
2841 state = HAS_STUB_SECTION;
2842 stub_table = group_end;
2843 stub_table_end_offset = group_end_offset;
2848 case HAS_STUB_SECTION:
2849 // Adding this section makes the post stub-section group larger
2851 if (section_end_offset - stub_table_end_offset >= group_size)
2853 gold_assert(group_end != this->input_sections().end());
2854 this->create_stub_group(group_begin, group_end, stub_table,
2855 target, &new_relaxed_sections);
2864 // If we see an input section and currently there is no group, start
2865 // a new one. Skip any empty sections.
2866 if ((p->is_input_section() || p->is_relaxed_input_section())
2867 && (p->relobj()->section_size(p->shndx()) != 0))
2869 if (state == NO_GROUP)
2871 state = FINDING_STUB_SECTION;
2873 group_begin_offset = section_begin_offset;
2876 // Keep track of the last input section seen.
2878 group_end_offset = section_end_offset;
2881 off = section_end_offset;
2884 // Create a stub group for any ungrouped sections.
2885 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
2887 gold_assert(group_end != this->input_sections().end());
2888 this->create_stub_group(group_begin, group_end,
2889 (state == FINDING_STUB_SECTION
2892 target, &new_relaxed_sections);
2895 // Convert input section into relaxed input section in a batch.
2896 if (!new_relaxed_sections.empty())
2897 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
2899 // Update the section offsets
2900 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
2902 Arm_relobj<big_endian>* arm_relobj =
2903 Arm_relobj<big_endian>::as_arm_relobj(
2904 new_relaxed_sections[i]->relobj());
2905 unsigned int shndx = new_relaxed_sections[i]->shndx();
2906 // Tell Arm_relobj that this input section is converted.
2907 arm_relobj->convert_input_section_to_relaxed_section(shndx);
2911 // A class to handle the PLT data.
2913 template<bool big_endian>
2914 class Output_data_plt_arm : public Output_section_data
2917 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
2920 Output_data_plt_arm(Layout*, Output_data_space*);
2922 // Add an entry to the PLT.
2924 add_entry(Symbol* gsym);
2926 // Return the .rel.plt section data.
2927 const Reloc_section*
2929 { return this->rel_; }
2933 do_adjust_output_section(Output_section* os);
2935 // Write to a map file.
2937 do_print_to_mapfile(Mapfile* mapfile) const
2938 { mapfile->print_output_data(this, _("** PLT")); }
2941 // Template for the first PLT entry.
2942 static const uint32_t first_plt_entry[5];
2944 // Template for subsequent PLT entries.
2945 static const uint32_t plt_entry[3];
2947 // Set the final size.
2949 set_final_data_size()
2951 this->set_data_size(sizeof(first_plt_entry)
2952 + this->count_ * sizeof(plt_entry));
2955 // Write out the PLT data.
2957 do_write(Output_file*);
2959 // The reloc section.
2960 Reloc_section* rel_;
2961 // The .got.plt section.
2962 Output_data_space* got_plt_;
2963 // The number of PLT entries.
2964 unsigned int count_;
2967 // Create the PLT section. The ordinary .got section is an argument,
2968 // since we need to refer to the start. We also create our own .got
2969 // section just for PLT entries.
2971 template<bool big_endian>
2972 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
2973 Output_data_space* got_plt)
2974 : Output_section_data(4), got_plt_(got_plt), count_(0)
2976 this->rel_ = new Reloc_section(false);
2977 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2978 elfcpp::SHF_ALLOC, this->rel_);
2981 template<bool big_endian>
2983 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
2988 // Add an entry to the PLT.
2990 template<bool big_endian>
2992 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
2994 gold_assert(!gsym->has_plt_offset());
2996 // Note that when setting the PLT offset we skip the initial
2997 // reserved PLT entry.
2998 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
2999 + sizeof(first_plt_entry));
3003 section_offset_type got_offset = this->got_plt_->current_data_size();
3005 // Every PLT entry needs a GOT entry which points back to the PLT
3006 // entry (this will be changed by the dynamic linker, normally
3007 // lazily when the function is called).
3008 this->got_plt_->set_current_data_size(got_offset + 4);
3010 // Every PLT entry needs a reloc.
3011 gsym->set_needs_dynsym_entry();
3012 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
3015 // Note that we don't need to save the symbol. The contents of the
3016 // PLT are independent of which symbols are used. The symbols only
3017 // appear in the relocations.
3021 // FIXME: This is not very flexible. Right now this has only been tested
3022 // on armv5te. If we are to support additional architecture features like
3023 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3025 // The first entry in the PLT.
3026 template<bool big_endian>
3027 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
3029 0xe52de004, // str lr, [sp, #-4]!
3030 0xe59fe004, // ldr lr, [pc, #4]
3031 0xe08fe00e, // add lr, pc, lr
3032 0xe5bef008, // ldr pc, [lr, #8]!
3033 0x00000000, // &GOT[0] - .
3036 // Subsequent entries in the PLT.
3038 template<bool big_endian>
3039 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
3041 0xe28fc600, // add ip, pc, #0xNN00000
3042 0xe28cca00, // add ip, ip, #0xNN000
3043 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
3046 // Write out the PLT. This uses the hand-coded instructions above,
3047 // and adjusts them as needed. This is all specified by the arm ELF
3048 // Processor Supplement.
3050 template<bool big_endian>
3052 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
3054 const off_t offset = this->offset();
3055 const section_size_type oview_size =
3056 convert_to_section_size_type(this->data_size());
3057 unsigned char* const oview = of->get_output_view(offset, oview_size);
3059 const off_t got_file_offset = this->got_plt_->offset();
3060 const section_size_type got_size =
3061 convert_to_section_size_type(this->got_plt_->data_size());
3062 unsigned char* const got_view = of->get_output_view(got_file_offset,
3064 unsigned char* pov = oview;
3066 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
3067 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
3069 // Write first PLT entry. All but the last word are constants.
3070 const size_t num_first_plt_words = (sizeof(first_plt_entry)
3071 / sizeof(plt_entry[0]));
3072 for (size_t i = 0; i < num_first_plt_words - 1; i++)
3073 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
3074 // Last word in first PLT entry is &GOT[0] - .
3075 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
3076 got_address - (plt_address + 16));
3077 pov += sizeof(first_plt_entry);
3079 unsigned char* got_pov = got_view;
3081 memset(got_pov, 0, 12);
3084 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
3085 unsigned int plt_offset = sizeof(first_plt_entry);
3086 unsigned int plt_rel_offset = 0;
3087 unsigned int got_offset = 12;
3088 const unsigned int count = this->count_;
3089 for (unsigned int i = 0;
3092 pov += sizeof(plt_entry),
3094 plt_offset += sizeof(plt_entry),
3095 plt_rel_offset += rel_size,
3098 // Set and adjust the PLT entry itself.
3099 int32_t offset = ((got_address + got_offset)
3100 - (plt_address + plt_offset + 8));
3102 gold_assert(offset >= 0 && offset < 0x0fffffff);
3103 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
3104 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
3105 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
3106 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
3107 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
3108 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
3110 // Set the entry in the GOT.
3111 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
3114 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
3115 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
3117 of->write_output_view(offset, oview_size, oview);
3118 of->write_output_view(got_file_offset, got_size, got_view);
3121 // Create a PLT entry for a global symbol.
3123 template<bool big_endian>
3125 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
3128 if (gsym->has_plt_offset())
3131 if (this->plt_ == NULL)
3133 // Create the GOT sections first.
3134 this->got_section(symtab, layout);
3136 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
3137 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
3139 | elfcpp::SHF_EXECINSTR),
3142 this->plt_->add_entry(gsym);
3145 // Report an unsupported relocation against a local symbol.
3147 template<bool big_endian>
3149 Target_arm<big_endian>::Scan::unsupported_reloc_local(
3150 Sized_relobj<32, big_endian>* object,
3151 unsigned int r_type)
3153 gold_error(_("%s: unsupported reloc %u against local symbol"),
3154 object->name().c_str(), r_type);
3157 // We are about to emit a dynamic relocation of type R_TYPE. If the
3158 // dynamic linker does not support it, issue an error. The GNU linker
3159 // only issues a non-PIC error for an allocated read-only section.
3160 // Here we know the section is allocated, but we don't know that it is
3161 // read-only. But we check for all the relocation types which the
3162 // glibc dynamic linker supports, so it seems appropriate to issue an
3163 // error even if the section is not read-only.
3165 template<bool big_endian>
3167 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
3168 unsigned int r_type)
3172 // These are the relocation types supported by glibc for ARM.
3173 case elfcpp::R_ARM_RELATIVE:
3174 case elfcpp::R_ARM_COPY:
3175 case elfcpp::R_ARM_GLOB_DAT:
3176 case elfcpp::R_ARM_JUMP_SLOT:
3177 case elfcpp::R_ARM_ABS32:
3178 case elfcpp::R_ARM_ABS32_NOI:
3179 case elfcpp::R_ARM_PC24:
3180 // FIXME: The following 3 types are not supported by Android's dynamic
3182 case elfcpp::R_ARM_TLS_DTPMOD32:
3183 case elfcpp::R_ARM_TLS_DTPOFF32:
3184 case elfcpp::R_ARM_TLS_TPOFF32:
3188 // This prevents us from issuing more than one error per reloc
3189 // section. But we can still wind up issuing more than one
3190 // error per object file.
3191 if (this->issued_non_pic_error_)
3193 object->error(_("requires unsupported dynamic reloc; "
3194 "recompile with -fPIC"));
3195 this->issued_non_pic_error_ = true;
3198 case elfcpp::R_ARM_NONE:
3203 // Scan a relocation for a local symbol.
3204 // FIXME: This only handles a subset of relocation types used by Android
3205 // on ARM v5te devices.
3207 template<bool big_endian>
3209 Target_arm<big_endian>::Scan::local(const General_options&,
3210 Symbol_table* symtab,
3213 Sized_relobj<32, big_endian>* object,
3214 unsigned int data_shndx,
3215 Output_section* output_section,
3216 const elfcpp::Rel<32, big_endian>& reloc,
3217 unsigned int r_type,
3218 const elfcpp::Sym<32, big_endian>&)
3220 r_type = get_real_reloc_type(r_type);
3223 case elfcpp::R_ARM_NONE:
3226 case elfcpp::R_ARM_ABS32:
3227 case elfcpp::R_ARM_ABS32_NOI:
3228 // If building a shared library (or a position-independent
3229 // executable), we need to create a dynamic relocation for
3230 // this location. The relocation applied at link time will
3231 // apply the link-time value, so we flag the location with
3232 // an R_ARM_RELATIVE relocation so the dynamic loader can
3233 // relocate it easily.
3234 if (parameters->options().output_is_position_independent())
3236 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3237 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3238 // If we are to add more other reloc types than R_ARM_ABS32,
3239 // we need to add check_non_pic(object, r_type) here.
3240 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
3241 output_section, data_shndx,
3242 reloc.get_r_offset());
3246 case elfcpp::R_ARM_REL32:
3247 case elfcpp::R_ARM_THM_CALL:
3248 case elfcpp::R_ARM_CALL:
3249 case elfcpp::R_ARM_PREL31:
3250 case elfcpp::R_ARM_JUMP24:
3251 case elfcpp::R_ARM_PLT32:
3252 case elfcpp::R_ARM_THM_ABS5:
3253 case elfcpp::R_ARM_ABS8:
3254 case elfcpp::R_ARM_ABS12:
3255 case elfcpp::R_ARM_ABS16:
3256 case elfcpp::R_ARM_BASE_ABS:
3257 case elfcpp::R_ARM_MOVW_ABS_NC:
3258 case elfcpp::R_ARM_MOVT_ABS:
3259 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3260 case elfcpp::R_ARM_THM_MOVT_ABS:
3261 case elfcpp::R_ARM_MOVW_PREL_NC:
3262 case elfcpp::R_ARM_MOVT_PREL:
3263 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3264 case elfcpp::R_ARM_THM_MOVT_PREL:
3267 case elfcpp::R_ARM_GOTOFF32:
3268 // We need a GOT section:
3269 target->got_section(symtab, layout);
3272 case elfcpp::R_ARM_BASE_PREL:
3273 // FIXME: What about this?
3276 case elfcpp::R_ARM_GOT_BREL:
3277 case elfcpp::R_ARM_GOT_PREL:
3279 // The symbol requires a GOT entry.
3280 Output_data_got<32, big_endian>* got =
3281 target->got_section(symtab, layout);
3282 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3283 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
3285 // If we are generating a shared object, we need to add a
3286 // dynamic RELATIVE relocation for this symbol's GOT entry.
3287 if (parameters->options().output_is_position_independent())
3289 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3290 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3291 rel_dyn->add_local_relative(
3292 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
3293 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
3299 case elfcpp::R_ARM_TARGET1:
3300 // This should have been mapped to another type already.
3302 case elfcpp::R_ARM_COPY:
3303 case elfcpp::R_ARM_GLOB_DAT:
3304 case elfcpp::R_ARM_JUMP_SLOT:
3305 case elfcpp::R_ARM_RELATIVE:
3306 // These are relocations which should only be seen by the
3307 // dynamic linker, and should never be seen here.
3308 gold_error(_("%s: unexpected reloc %u in object file"),
3309 object->name().c_str(), r_type);
3313 unsupported_reloc_local(object, r_type);
3318 // Report an unsupported relocation against a global symbol.
3320 template<bool big_endian>
3322 Target_arm<big_endian>::Scan::unsupported_reloc_global(
3323 Sized_relobj<32, big_endian>* object,
3324 unsigned int r_type,
3327 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3328 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3331 // Scan a relocation for a global symbol.
3332 // FIXME: This only handles a subset of relocation types used by Android
3333 // on ARM v5te devices.
3335 template<bool big_endian>
3337 Target_arm<big_endian>::Scan::global(const General_options&,
3338 Symbol_table* symtab,
3341 Sized_relobj<32, big_endian>* object,
3342 unsigned int data_shndx,
3343 Output_section* output_section,
3344 const elfcpp::Rel<32, big_endian>& reloc,
3345 unsigned int r_type,
3348 r_type = get_real_reloc_type(r_type);
3351 case elfcpp::R_ARM_NONE:
3354 case elfcpp::R_ARM_ABS32:
3355 case elfcpp::R_ARM_ABS32_NOI:
3357 // Make a dynamic relocation if necessary.
3358 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
3360 if (target->may_need_copy_reloc(gsym))
3362 target->copy_reloc(symtab, layout, object,
3363 data_shndx, output_section, gsym, reloc);
3365 else if (gsym->can_use_relative_reloc(false))
3367 // If we are to add more other reloc types than R_ARM_ABS32,
3368 // we need to add check_non_pic(object, r_type) here.
3369 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3370 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
3371 output_section, object,
3372 data_shndx, reloc.get_r_offset());
3376 // If we are to add more other reloc types than R_ARM_ABS32,
3377 // we need to add check_non_pic(object, r_type) here.
3378 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3379 rel_dyn->add_global(gsym, r_type, output_section, object,
3380 data_shndx, reloc.get_r_offset());
3386 case elfcpp::R_ARM_MOVW_ABS_NC:
3387 case elfcpp::R_ARM_MOVT_ABS:
3388 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3389 case elfcpp::R_ARM_THM_MOVT_ABS:
3390 case elfcpp::R_ARM_MOVW_PREL_NC:
3391 case elfcpp::R_ARM_MOVT_PREL:
3392 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3393 case elfcpp::R_ARM_THM_MOVT_PREL:
3396 case elfcpp::R_ARM_THM_ABS5:
3397 case elfcpp::R_ARM_ABS8:
3398 case elfcpp::R_ARM_ABS12:
3399 case elfcpp::R_ARM_ABS16:
3400 case elfcpp::R_ARM_BASE_ABS:
3402 // No dynamic relocs of this kinds.
3403 // Report the error in case of PIC.
3404 int flags = Symbol::NON_PIC_REF;
3405 if (gsym->type() == elfcpp::STT_FUNC
3406 || gsym->type() == elfcpp::STT_ARM_TFUNC)
3407 flags |= Symbol::FUNCTION_CALL;
3408 if (gsym->needs_dynamic_reloc(flags))
3409 check_non_pic(object, r_type);
3413 case elfcpp::R_ARM_REL32:
3414 case elfcpp::R_ARM_PREL31:
3416 // Make a dynamic relocation if necessary.
3417 int flags = Symbol::NON_PIC_REF;
3418 if (gsym->needs_dynamic_reloc(flags))
3420 if (target->may_need_copy_reloc(gsym))
3422 target->copy_reloc(symtab, layout, object,
3423 data_shndx, output_section, gsym, reloc);
3427 check_non_pic(object, r_type);
3428 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3429 rel_dyn->add_global(gsym, r_type, output_section, object,
3430 data_shndx, reloc.get_r_offset());
3436 case elfcpp::R_ARM_JUMP24:
3437 case elfcpp::R_ARM_THM_CALL:
3438 case elfcpp::R_ARM_CALL:
3440 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
3441 target->make_plt_entry(symtab, layout, gsym);
3442 // Make a dynamic relocation if necessary.
3443 int flags = Symbol::NON_PIC_REF;
3444 if (gsym->type() == elfcpp::STT_FUNC
3445 || gsym->type() == elfcpp::STT_ARM_TFUNC)
3446 flags |= Symbol::FUNCTION_CALL;
3447 if (gsym->needs_dynamic_reloc(flags))
3449 if (target->may_need_copy_reloc(gsym))
3451 target->copy_reloc(symtab, layout, object,
3452 data_shndx, output_section, gsym,
3457 check_non_pic(object, r_type);
3458 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3459 rel_dyn->add_global(gsym, r_type, output_section, object,
3460 data_shndx, reloc.get_r_offset());
3466 case elfcpp::R_ARM_PLT32:
3467 // If the symbol is fully resolved, this is just a relative
3468 // local reloc. Otherwise we need a PLT entry.
3469 if (gsym->final_value_is_known())
3471 // If building a shared library, we can also skip the PLT entry
3472 // if the symbol is defined in the output file and is protected
3474 if (gsym->is_defined()
3475 && !gsym->is_from_dynobj()
3476 && !gsym->is_preemptible())
3478 target->make_plt_entry(symtab, layout, gsym);
3481 case elfcpp::R_ARM_GOTOFF32:
3482 // We need a GOT section.
3483 target->got_section(symtab, layout);
3486 case elfcpp::R_ARM_BASE_PREL:
3487 // FIXME: What about this?
3490 case elfcpp::R_ARM_GOT_BREL:
3491 case elfcpp::R_ARM_GOT_PREL:
3493 // The symbol requires a GOT entry.
3494 Output_data_got<32, big_endian>* got =
3495 target->got_section(symtab, layout);
3496 if (gsym->final_value_is_known())
3497 got->add_global(gsym, GOT_TYPE_STANDARD);
3500 // If this symbol is not fully resolved, we need to add a
3501 // GOT entry with a dynamic relocation.
3502 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3503 if (gsym->is_from_dynobj()
3504 || gsym->is_undefined()
3505 || gsym->is_preemptible())
3506 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
3507 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
3510 if (got->add_global(gsym, GOT_TYPE_STANDARD))
3511 rel_dyn->add_global_relative(
3512 gsym, elfcpp::R_ARM_RELATIVE, got,
3513 gsym->got_offset(GOT_TYPE_STANDARD));
3519 case elfcpp::R_ARM_TARGET1:
3520 // This should have been mapped to another type already.
3522 case elfcpp::R_ARM_COPY:
3523 case elfcpp::R_ARM_GLOB_DAT:
3524 case elfcpp::R_ARM_JUMP_SLOT:
3525 case elfcpp::R_ARM_RELATIVE:
3526 // These are relocations which should only be seen by the
3527 // dynamic linker, and should never be seen here.
3528 gold_error(_("%s: unexpected reloc %u in object file"),
3529 object->name().c_str(), r_type);
3533 unsupported_reloc_global(object, r_type, gsym);
3538 // Process relocations for gc.
3540 template<bool big_endian>
3542 Target_arm<big_endian>::gc_process_relocs(const General_options& options,
3543 Symbol_table* symtab,
3545 Sized_relobj<32, big_endian>* object,
3546 unsigned int data_shndx,
3548 const unsigned char* prelocs,
3550 Output_section* output_section,
3551 bool needs_special_offset_handling,
3552 size_t local_symbol_count,
3553 const unsigned char* plocal_symbols)
3555 typedef Target_arm<big_endian> Arm;
3556 typedef typename Target_arm<big_endian>::Scan Scan;
3558 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
3568 needs_special_offset_handling,
3573 // Scan relocations for a section.
3575 template<bool big_endian>
3577 Target_arm<big_endian>::scan_relocs(const General_options& options,
3578 Symbol_table* symtab,
3580 Sized_relobj<32, big_endian>* object,
3581 unsigned int data_shndx,
3582 unsigned int sh_type,
3583 const unsigned char* prelocs,
3585 Output_section* output_section,
3586 bool needs_special_offset_handling,
3587 size_t local_symbol_count,
3588 const unsigned char* plocal_symbols)
3590 typedef typename Target_arm<big_endian>::Scan Scan;
3591 if (sh_type == elfcpp::SHT_RELA)
3593 gold_error(_("%s: unsupported RELA reloc section"),
3594 object->name().c_str());
3598 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
3608 needs_special_offset_handling,
3613 // Finalize the sections.
3615 template<bool big_endian>
3617 Target_arm<big_endian>::do_finalize_sections(Layout* layout)
3619 // Fill in some more dynamic tags.
3620 Output_data_dynamic* const odyn = layout->dynamic_data();
3623 if (this->got_plt_ != NULL)
3624 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
3626 if (this->plt_ != NULL)
3628 const Output_data* od = this->plt_->rel_plt();
3629 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
3630 odyn->add_section_address(elfcpp::DT_JMPREL, od);
3631 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
3634 if (this->rel_dyn_ != NULL)
3636 const Output_data* od = this->rel_dyn_;
3637 odyn->add_section_address(elfcpp::DT_REL, od);
3638 odyn->add_section_size(elfcpp::DT_RELSZ, od);
3639 odyn->add_constant(elfcpp::DT_RELENT,
3640 elfcpp::Elf_sizes<32>::rel_size);
3643 if (!parameters->options().shared())
3645 // The value of the DT_DEBUG tag is filled in by the dynamic
3646 // linker at run time, and used by the debugger.
3647 odyn->add_constant(elfcpp::DT_DEBUG, 0);
3651 // Emit any relocs we saved in an attempt to avoid generating COPY
3653 if (this->copy_relocs_.any_saved_relocs())
3654 this->copy_relocs_.emit(this->rel_dyn_section(layout));
3656 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
3657 // the .ARM.exidx section.
3658 if (!layout->script_options()->saw_phdrs_clause()
3659 && !parameters->options().relocatable())
3661 Output_section* exidx_section =
3662 layout->find_output_section(".ARM.exidx");
3664 if (exidx_section != NULL
3665 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
3667 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
3669 Output_segment* exidx_segment =
3670 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
3671 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
3676 // Return whether a direct absolute static relocation needs to be applied.
3677 // In cases where Scan::local() or Scan::global() has created
3678 // a dynamic relocation other than R_ARM_RELATIVE, the addend
3679 // of the relocation is carried in the data, and we must not
3680 // apply the static relocation.
3682 template<bool big_endian>
3684 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
3685 const Sized_symbol<32>* gsym,
3688 Output_section* output_section)
3690 // If the output section is not allocated, then we didn't call
3691 // scan_relocs, we didn't create a dynamic reloc, and we must apply
3693 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
3696 // For local symbols, we will have created a non-RELATIVE dynamic
3697 // relocation only if (a) the output is position independent,
3698 // (b) the relocation is absolute (not pc- or segment-relative), and
3699 // (c) the relocation is not 32 bits wide.
3701 return !(parameters->options().output_is_position_independent()
3702 && (ref_flags & Symbol::ABSOLUTE_REF)
3705 // For global symbols, we use the same helper routines used in the
3706 // scan pass. If we did not create a dynamic relocation, or if we
3707 // created a RELATIVE dynamic relocation, we should apply the static
3709 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
3710 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
3711 && gsym->can_use_relative_reloc(ref_flags
3712 & Symbol::FUNCTION_CALL);
3713 return !has_dyn || is_rel;
3716 // Perform a relocation.
3718 template<bool big_endian>
3720 Target_arm<big_endian>::Relocate::relocate(
3721 const Relocate_info<32, big_endian>* relinfo,
3723 Output_section *output_section,
3725 const elfcpp::Rel<32, big_endian>& rel,
3726 unsigned int r_type,
3727 const Sized_symbol<32>* gsym,
3728 const Symbol_value<32>* psymval,
3729 unsigned char* view,
3730 elfcpp::Elf_types<32>::Elf_Addr address,
3731 section_size_type /* view_size */ )
3733 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
3735 r_type = get_real_reloc_type(r_type);
3737 // If this the symbol may be a Thumb function, set thumb bit to 1.
3738 bool has_thumb_bit = ((gsym != NULL)
3739 && (gsym->type() == elfcpp::STT_FUNC
3740 || gsym->type() == elfcpp::STT_ARM_TFUNC));
3742 // Pick the value to use for symbols defined in shared objects.
3743 Symbol_value<32> symval;
3745 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
3747 symval.set_output_value(target->plt_section()->address()
3748 + gsym->plt_offset());
3753 const Sized_relobj<32, big_endian>* object = relinfo->object;
3755 // Get the GOT offset if needed.
3756 // The GOT pointer points to the end of the GOT section.
3757 // We need to subtract the size of the GOT section to get
3758 // the actual offset to use in the relocation.
3759 bool have_got_offset = false;
3760 unsigned int got_offset = 0;
3763 case elfcpp::R_ARM_GOT_BREL:
3764 case elfcpp::R_ARM_GOT_PREL:
3767 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3768 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
3769 - target->got_size());
3773 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3774 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3775 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
3776 - target->got_size());
3778 have_got_offset = true;
3785 typename Arm_relocate_functions::Status reloc_status =
3786 Arm_relocate_functions::STATUS_OKAY;
3789 case elfcpp::R_ARM_NONE:
3792 case elfcpp::R_ARM_ABS8:
3793 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3795 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
3798 case elfcpp::R_ARM_ABS12:
3799 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3801 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
3804 case elfcpp::R_ARM_ABS16:
3805 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3807 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
3810 case elfcpp::R_ARM_ABS32:
3811 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3813 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
3817 case elfcpp::R_ARM_ABS32_NOI:
3818 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3820 // No thumb bit for this relocation: (S + A)
3821 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
3825 case elfcpp::R_ARM_MOVW_ABS_NC:
3826 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3828 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
3832 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
3833 "a shared object; recompile with -fPIC"));
3836 case elfcpp::R_ARM_MOVT_ABS:
3837 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3839 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
3841 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
3842 "a shared object; recompile with -fPIC"));
3845 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3846 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3848 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
3852 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
3853 "making a shared object; recompile with -fPIC"));
3856 case elfcpp::R_ARM_THM_MOVT_ABS:
3857 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3859 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
3862 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
3863 "making a shared object; recompile with -fPIC"));
3866 case elfcpp::R_ARM_MOVW_PREL_NC:
3867 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
3872 case elfcpp::R_ARM_MOVT_PREL:
3873 reloc_status = Arm_relocate_functions::movt_prel(view, object,
3877 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3878 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
3883 case elfcpp::R_ARM_THM_MOVT_PREL:
3884 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
3888 case elfcpp::R_ARM_REL32:
3889 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
3890 address, has_thumb_bit);
3893 case elfcpp::R_ARM_THM_ABS5:
3894 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
3896 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
3899 case elfcpp::R_ARM_THM_CALL:
3900 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
3901 address, has_thumb_bit);
3904 case elfcpp::R_ARM_GOTOFF32:
3906 elfcpp::Elf_types<32>::Elf_Addr got_origin;
3907 got_origin = target->got_plt_section()->address();
3908 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
3909 got_origin, has_thumb_bit);
3913 case elfcpp::R_ARM_BASE_PREL:
3916 // Get the addressing origin of the output segment defining the
3917 // symbol gsym (AAELF 4.6.1.2 Relocation types)
3918 gold_assert(gsym != NULL);
3919 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
3920 origin = gsym->output_segment()->vaddr();
3921 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
3922 origin = gsym->output_data()->address();
3925 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3926 _("cannot find origin of R_ARM_BASE_PREL"));
3929 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
3933 case elfcpp::R_ARM_BASE_ABS:
3935 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
3940 // Get the addressing origin of the output segment defining
3941 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
3943 // R_ARM_BASE_ABS with the NULL symbol will give the
3944 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
3945 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
3946 origin = target->got_plt_section()->address();
3947 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
3948 origin = gsym->output_segment()->vaddr();
3949 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
3950 origin = gsym->output_data()->address();
3953 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3954 _("cannot find origin of R_ARM_BASE_ABS"));
3958 reloc_status = Arm_relocate_functions::base_abs(view, origin);
3962 case elfcpp::R_ARM_GOT_BREL:
3963 gold_assert(have_got_offset);
3964 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
3967 case elfcpp::R_ARM_GOT_PREL:
3968 gold_assert(have_got_offset);
3969 // Get the address origin for GOT PLT, which is allocated right
3970 // after the GOT section, to calculate an absolute address of
3971 // the symbol GOT entry (got_origin + got_offset).
3972 elfcpp::Elf_types<32>::Elf_Addr got_origin;
3973 got_origin = target->got_plt_section()->address();
3974 reloc_status = Arm_relocate_functions::got_prel(view,
3975 got_origin + got_offset,
3979 case elfcpp::R_ARM_PLT32:
3980 gold_assert(gsym == NULL
3981 || gsym->has_plt_offset()
3982 || gsym->final_value_is_known()
3983 || (gsym->is_defined()
3984 && !gsym->is_from_dynobj()
3985 && !gsym->is_preemptible()));
3986 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
3987 address, has_thumb_bit);
3990 case elfcpp::R_ARM_CALL:
3991 reloc_status = Arm_relocate_functions::call(view, object, psymval,
3992 address, has_thumb_bit);
3995 case elfcpp::R_ARM_JUMP24:
3996 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
3997 address, has_thumb_bit);
4000 case elfcpp::R_ARM_PREL31:
4001 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
4002 address, has_thumb_bit);
4005 case elfcpp::R_ARM_TARGET1:
4006 // This should have been mapped to another type already.
4008 case elfcpp::R_ARM_COPY:
4009 case elfcpp::R_ARM_GLOB_DAT:
4010 case elfcpp::R_ARM_JUMP_SLOT:
4011 case elfcpp::R_ARM_RELATIVE:
4012 // These are relocations which should only be seen by the
4013 // dynamic linker, and should never be seen here.
4014 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4015 _("unexpected reloc %u in object file"),
4020 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4021 _("unsupported reloc %u"),
4026 // Report any errors.
4027 switch (reloc_status)
4029 case Arm_relocate_functions::STATUS_OKAY:
4031 case Arm_relocate_functions::STATUS_OVERFLOW:
4032 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4033 _("relocation overflow in relocation %u"),
4036 case Arm_relocate_functions::STATUS_BAD_RELOC:
4037 gold_error_at_location(
4041 _("unexpected opcode while processing relocation %u"),
4051 // Relocate section data.
4053 template<bool big_endian>
4055 Target_arm<big_endian>::relocate_section(
4056 const Relocate_info<32, big_endian>* relinfo,
4057 unsigned int sh_type,
4058 const unsigned char* prelocs,
4060 Output_section* output_section,
4061 bool needs_special_offset_handling,
4062 unsigned char* view,
4063 elfcpp::Elf_types<32>::Elf_Addr address,
4064 section_size_type view_size,
4065 const Reloc_symbol_changes* reloc_symbol_changes)
4067 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
4068 gold_assert(sh_type == elfcpp::SHT_REL);
4070 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
4077 needs_special_offset_handling,
4081 reloc_symbol_changes);
4084 // Return the size of a relocation while scanning during a relocatable
4087 template<bool big_endian>
4089 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
4090 unsigned int r_type,
4093 r_type = get_real_reloc_type(r_type);
4096 case elfcpp::R_ARM_NONE:
4099 case elfcpp::R_ARM_ABS8:
4102 case elfcpp::R_ARM_ABS16:
4103 case elfcpp::R_ARM_THM_ABS5:
4106 case elfcpp::R_ARM_ABS32:
4107 case elfcpp::R_ARM_ABS32_NOI:
4108 case elfcpp::R_ARM_ABS12:
4109 case elfcpp::R_ARM_BASE_ABS:
4110 case elfcpp::R_ARM_REL32:
4111 case elfcpp::R_ARM_THM_CALL:
4112 case elfcpp::R_ARM_GOTOFF32:
4113 case elfcpp::R_ARM_BASE_PREL:
4114 case elfcpp::R_ARM_GOT_BREL:
4115 case elfcpp::R_ARM_GOT_PREL:
4116 case elfcpp::R_ARM_PLT32:
4117 case elfcpp::R_ARM_CALL:
4118 case elfcpp::R_ARM_JUMP24:
4119 case elfcpp::R_ARM_PREL31:
4120 case elfcpp::R_ARM_MOVW_ABS_NC:
4121 case elfcpp::R_ARM_MOVT_ABS:
4122 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4123 case elfcpp::R_ARM_THM_MOVT_ABS:
4124 case elfcpp::R_ARM_MOVW_PREL_NC:
4125 case elfcpp::R_ARM_MOVT_PREL:
4126 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4127 case elfcpp::R_ARM_THM_MOVT_PREL:
4130 case elfcpp::R_ARM_TARGET1:
4131 // This should have been mapped to another type already.
4133 case elfcpp::R_ARM_COPY:
4134 case elfcpp::R_ARM_GLOB_DAT:
4135 case elfcpp::R_ARM_JUMP_SLOT:
4136 case elfcpp::R_ARM_RELATIVE:
4137 // These are relocations which should only be seen by the
4138 // dynamic linker, and should never be seen here.
4139 gold_error(_("%s: unexpected reloc %u in object file"),
4140 object->name().c_str(), r_type);
4144 object->error(_("unsupported reloc %u in object file"), r_type);
4149 // Scan the relocs during a relocatable link.
4151 template<bool big_endian>
4153 Target_arm<big_endian>::scan_relocatable_relocs(
4154 const General_options& options,
4155 Symbol_table* symtab,
4157 Sized_relobj<32, big_endian>* object,
4158 unsigned int data_shndx,
4159 unsigned int sh_type,
4160 const unsigned char* prelocs,
4162 Output_section* output_section,
4163 bool needs_special_offset_handling,
4164 size_t local_symbol_count,
4165 const unsigned char* plocal_symbols,
4166 Relocatable_relocs* rr)
4168 gold_assert(sh_type == elfcpp::SHT_REL);
4170 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
4171 Relocatable_size_for_reloc> Scan_relocatable_relocs;
4173 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
4174 Scan_relocatable_relocs>(
4183 needs_special_offset_handling,
4189 // Relocate a section during a relocatable link.
4191 template<bool big_endian>
4193 Target_arm<big_endian>::relocate_for_relocatable(
4194 const Relocate_info<32, big_endian>* relinfo,
4195 unsigned int sh_type,
4196 const unsigned char* prelocs,
4198 Output_section* output_section,
4199 off_t offset_in_output_section,
4200 const Relocatable_relocs* rr,
4201 unsigned char* view,
4202 elfcpp::Elf_types<32>::Elf_Addr view_address,
4203 section_size_type view_size,
4204 unsigned char* reloc_view,
4205 section_size_type reloc_view_size)
4207 gold_assert(sh_type == elfcpp::SHT_REL);
4209 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
4214 offset_in_output_section,
4223 // Return the value to use for a dynamic symbol which requires special
4224 // treatment. This is how we support equality comparisons of function
4225 // pointers across shared library boundaries, as described in the
4226 // processor specific ABI supplement.
4228 template<bool big_endian>
4230 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4232 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4233 return this->plt_section()->address() + gsym->plt_offset();
4236 // Map platform-specific relocs to real relocs
4238 template<bool big_endian>
4240 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4244 case elfcpp::R_ARM_TARGET1:
4245 // This is either R_ARM_ABS32 or R_ARM_REL32;
4246 return elfcpp::R_ARM_ABS32;
4248 case elfcpp::R_ARM_TARGET2:
4249 // This can be any reloc type but ususally is R_ARM_GOT_PREL
4250 return elfcpp::R_ARM_GOT_PREL;
4257 // The selector for arm object files.
4259 template<bool big_endian>
4260 class Target_selector_arm : public Target_selector
4263 Target_selector_arm()
4264 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
4265 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
4269 do_instantiate_target()
4270 { return new Target_arm<big_endian>(); }
4273 Target_selector_arm<false> target_selector_arm;
4274 Target_selector_arm<true> target_selector_armbe;
4276 } // End anonymous namespace.