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"
49 #include "attributes.h"
56 template<bool big_endian>
57 class Output_data_plt_arm;
59 template<bool big_endian>
62 template<bool big_endian>
63 class Arm_input_section;
65 template<bool big_endian>
66 class Arm_output_section;
68 template<bool big_endian>
71 template<bool big_endian>
75 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
77 // Maximum branch offsets for ARM, THUMB and THUMB2.
78 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
79 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
80 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
81 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
82 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
83 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
85 // The arm target class.
87 // This is a very simple port of gold for ARM-EABI. It is intended for
88 // supporting Android only for the time being. Only these relocation types
117 // R_ARM_THM_MOVW_ABS_NC
118 // R_ARM_THM_MOVT_ABS
119 // R_ARM_MOVW_PREL_NC
121 // R_ARM_THM_MOVW_PREL_NC
122 // R_ARM_THM_MOVT_PREL
125 // - Support more relocation types as needed.
126 // - Make PLTs more flexible for different architecture features like
128 // There are probably a lot more.
130 // Instruction template class. This class is similar to the insn_sequence
131 // struct in bfd/elf32-arm.c.
136 // Types of instruction templates.
140 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
141 // templates with class-specific semantics. Currently this is used
142 // only by the Cortex_a8_stub class for handling condition codes in
143 // conditional branches.
144 THUMB16_SPECIAL_TYPE,
150 // Factory methods to create instruction templates in different formats.
152 static const Insn_template
153 thumb16_insn(uint32_t data)
154 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
156 // A Thumb conditional branch, in which the proper condition is inserted
157 // when we build the stub.
158 static const Insn_template
159 thumb16_bcond_insn(uint32_t data)
160 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
162 static const Insn_template
163 thumb32_insn(uint32_t data)
164 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
166 static const Insn_template
167 thumb32_b_insn(uint32_t data, int reloc_addend)
169 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
173 static const Insn_template
174 arm_insn(uint32_t data)
175 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
177 static const Insn_template
178 arm_rel_insn(unsigned data, int reloc_addend)
179 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
181 static const Insn_template
182 data_word(unsigned data, unsigned int r_type, int reloc_addend)
183 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
185 // Accessors. This class is used for read-only objects so no modifiers
190 { return this->data_; }
192 // Return the instruction sequence type of this.
195 { return this->type_; }
197 // Return the ARM relocation type of this.
200 { return this->r_type_; }
204 { return this->reloc_addend_; }
206 // Return size of instruction template in bytes.
210 // Return byte-alignment of instruction template.
215 // We make the constructor private to ensure that only the factory
218 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
219 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
222 // Instruction specific data. This is used to store information like
223 // some of the instruction bits.
225 // Instruction template type.
227 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
228 unsigned int r_type_;
229 // Relocation addend.
230 int32_t reloc_addend_;
233 // Macro for generating code to stub types. One entry per long/short
237 DEF_STUB(long_branch_any_any) \
238 DEF_STUB(long_branch_v4t_arm_thumb) \
239 DEF_STUB(long_branch_thumb_only) \
240 DEF_STUB(long_branch_v4t_thumb_thumb) \
241 DEF_STUB(long_branch_v4t_thumb_arm) \
242 DEF_STUB(short_branch_v4t_thumb_arm) \
243 DEF_STUB(long_branch_any_arm_pic) \
244 DEF_STUB(long_branch_any_thumb_pic) \
245 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
246 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
247 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
248 DEF_STUB(long_branch_thumb_only_pic) \
249 DEF_STUB(a8_veneer_b_cond) \
250 DEF_STUB(a8_veneer_b) \
251 DEF_STUB(a8_veneer_bl) \
252 DEF_STUB(a8_veneer_blx)
256 #define DEF_STUB(x) arm_stub_##x,
262 // First reloc stub type.
263 arm_stub_reloc_first = arm_stub_long_branch_any_any,
264 // Last reloc stub type.
265 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
267 // First Cortex-A8 stub type.
268 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
269 // Last Cortex-A8 stub type.
270 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
273 arm_stub_type_last = arm_stub_a8_veneer_blx
277 // Stub template class. Templates are meant to be read-only objects.
278 // A stub template for a stub type contains all read-only attributes
279 // common to all stubs of the same type.
284 Stub_template(Stub_type, const Insn_template*, size_t);
292 { return this->type_; }
294 // Return an array of instruction templates.
297 { return this->insns_; }
299 // Return size of template in number of instructions.
302 { return this->insn_count_; }
304 // Return size of template in bytes.
307 { return this->size_; }
309 // Return alignment of the stub template.
312 { return this->alignment_; }
314 // Return whether entry point is in thumb mode.
316 entry_in_thumb_mode() const
317 { return this->entry_in_thumb_mode_; }
319 // Return number of relocations in this template.
322 { return this->relocs_.size(); }
324 // Return index of the I-th instruction with relocation.
326 reloc_insn_index(size_t i) const
328 gold_assert(i < this->relocs_.size());
329 return this->relocs_[i].first;
332 // Return the offset of the I-th instruction with relocation from the
333 // beginning of the stub.
335 reloc_offset(size_t i) const
337 gold_assert(i < this->relocs_.size());
338 return this->relocs_[i].second;
342 // This contains information about an instruction template with a relocation
343 // and its offset from start of stub.
344 typedef std::pair<size_t, section_size_type> Reloc;
346 // A Stub_template may not be copied. We want to share templates as much
348 Stub_template(const Stub_template&);
349 Stub_template& operator=(const Stub_template&);
353 // Points to an array of Insn_templates.
354 const Insn_template* insns_;
355 // Number of Insn_templates in insns_[].
357 // Size of templated instructions in bytes.
359 // Alignment of templated instructions.
361 // Flag to indicate if entry is in thumb mode.
362 bool entry_in_thumb_mode_;
363 // A table of reloc instruction indices and offsets. We can find these by
364 // looking at the instruction templates but we pre-compute and then stash
365 // them here for speed.
366 std::vector<Reloc> relocs_;
370 // A class for code stubs. This is a base class for different type of
371 // stubs used in the ARM target.
377 static const section_offset_type invalid_offset =
378 static_cast<section_offset_type>(-1);
381 Stub(const Stub_template* stub_template)
382 : stub_template_(stub_template), offset_(invalid_offset)
389 // Return the stub template.
391 stub_template() const
392 { return this->stub_template_; }
394 // Return offset of code stub from beginning of its containing stub table.
398 gold_assert(this->offset_ != invalid_offset);
399 return this->offset_;
402 // Set offset of code stub from beginning of its containing stub table.
404 set_offset(section_offset_type offset)
405 { this->offset_ = offset; }
407 // Return the relocation target address of the i-th relocation in the
408 // stub. This must be defined in a child class.
410 reloc_target(size_t i)
411 { return this->do_reloc_target(i); }
413 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
415 write(unsigned char* view, section_size_type view_size, bool big_endian)
416 { this->do_write(view, view_size, big_endian); }
418 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
419 // for the i-th instruction.
421 thumb16_special(size_t i)
422 { return this->do_thumb16_special(i); }
425 // This must be defined in the child class.
427 do_reloc_target(size_t) = 0;
429 // This may be overridden in the child class.
431 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
434 this->do_fixed_endian_write<true>(view, view_size);
436 this->do_fixed_endian_write<false>(view, view_size);
439 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
440 // instruction template.
442 do_thumb16_special(size_t)
443 { gold_unreachable(); }
446 // A template to implement do_write.
447 template<bool big_endian>
449 do_fixed_endian_write(unsigned char*, section_size_type);
452 const Stub_template* stub_template_;
453 // Offset within the section of containing this stub.
454 section_offset_type offset_;
457 // Reloc stub class. These are stubs we use to fix up relocation because
458 // of limited branch ranges.
460 class Reloc_stub : public Stub
463 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
464 // We assume we never jump to this address.
465 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
467 // Return destination address.
469 destination_address() const
471 gold_assert(this->destination_address_ != this->invalid_address);
472 return this->destination_address_;
475 // Set destination address.
477 set_destination_address(Arm_address address)
479 gold_assert(address != this->invalid_address);
480 this->destination_address_ = address;
483 // Reset destination address.
485 reset_destination_address()
486 { this->destination_address_ = this->invalid_address; }
488 // Determine stub type for a branch of a relocation of R_TYPE going
489 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
490 // the branch target is a thumb instruction. TARGET is used for look
491 // up ARM-specific linker settings.
493 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
494 Arm_address branch_target, bool target_is_thumb);
496 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
497 // and an addend. Since we treat global and local symbol differently, we
498 // use a Symbol object for a global symbol and a object-index pair for
503 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
504 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
505 // and R_SYM must not be invalid_index.
506 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
507 unsigned int r_sym, int32_t addend)
508 : stub_type_(stub_type), addend_(addend)
512 this->r_sym_ = Reloc_stub::invalid_index;
513 this->u_.symbol = symbol;
517 gold_assert(relobj != NULL && r_sym != invalid_index);
518 this->r_sym_ = r_sym;
519 this->u_.relobj = relobj;
526 // Accessors: Keys are meant to be read-only object so no modifiers are
532 { return this->stub_type_; }
534 // Return the local symbol index or invalid_index.
537 { return this->r_sym_; }
539 // Return the symbol if there is one.
542 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
544 // Return the relobj if there is one.
547 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
549 // Whether this equals to another key k.
551 eq(const Key& k) const
553 return ((this->stub_type_ == k.stub_type_)
554 && (this->r_sym_ == k.r_sym_)
555 && ((this->r_sym_ != Reloc_stub::invalid_index)
556 ? (this->u_.relobj == k.u_.relobj)
557 : (this->u_.symbol == k.u_.symbol))
558 && (this->addend_ == k.addend_));
561 // Return a hash value.
565 return (this->stub_type_
567 ^ gold::string_hash<char>(
568 (this->r_sym_ != Reloc_stub::invalid_index)
569 ? this->u_.relobj->name().c_str()
570 : this->u_.symbol->name())
574 // Functors for STL associative containers.
578 operator()(const Key& k) const
579 { return k.hash_value(); }
585 operator()(const Key& k1, const Key& k2) const
586 { return k1.eq(k2); }
589 // Name of key. This is mainly for debugging.
595 Stub_type stub_type_;
596 // If this is a local symbol, this is the index in the defining object.
597 // Otherwise, it is invalid_index for a global symbol.
599 // If r_sym_ is invalid index. This points to a global symbol.
600 // Otherwise, this points a relobj. We used the unsized and target
601 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
602 // Arm_relobj. This is done to avoid making the stub class a template
603 // as most of the stub machinery is endianity-neutral. However, it
604 // may require a bit of casting done by users of this class.
607 const Symbol* symbol;
608 const Relobj* relobj;
610 // Addend associated with a reloc.
615 // Reloc_stubs are created via a stub factory. So these are protected.
616 Reloc_stub(const Stub_template* stub_template)
617 : Stub(stub_template), destination_address_(invalid_address)
623 friend class Stub_factory;
625 // Return the relocation target address of the i-th relocation in the
628 do_reloc_target(size_t i)
630 // All reloc stub have only one relocation.
632 return this->destination_address_;
636 // Address of destination.
637 Arm_address destination_address_;
640 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
641 // THUMB branch that meets the following conditions:
643 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
644 // branch address is 0xffe.
645 // 2. The branch target address is in the same page as the first word of the
647 // 3. The branch follows a 32-bit instruction which is not a branch.
649 // To do the fix up, we need to store the address of the branch instruction
650 // and its target at least. We also need to store the original branch
651 // instruction bits for the condition code in a conditional branch. The
652 // condition code is used in a special instruction template. We also want
653 // to identify input sections needing Cortex-A8 workaround quickly. We store
654 // extra information about object and section index of the code section
655 // containing a branch being fixed up. The information is used to mark
656 // the code section when we finalize the Cortex-A8 stubs.
659 class Cortex_a8_stub : public Stub
665 // Return the object of the code section containing the branch being fixed
669 { return this->relobj_; }
671 // Return the section index of the code section containing the branch being
675 { return this->shndx_; }
677 // Return the source address of stub. This is the address of the original
678 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
681 source_address() const
682 { return this->source_address_; }
684 // Return the destination address of the stub. This is the branch taken
685 // address of the original branch instruction. LSB is 1 if it is a THUMB
686 // instruction address.
688 destination_address() const
689 { return this->destination_address_; }
691 // Return the instruction being fixed up.
693 original_insn() const
694 { return this->original_insn_; }
697 // Cortex_a8_stubs are created via a stub factory. So these are protected.
698 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
699 unsigned int shndx, Arm_address source_address,
700 Arm_address destination_address, uint32_t original_insn)
701 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
702 source_address_(source_address | 1U),
703 destination_address_(destination_address),
704 original_insn_(original_insn)
707 friend class Stub_factory;
709 // Return the relocation target address of the i-th relocation in the
712 do_reloc_target(size_t i)
714 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
716 // The conditional branch veneer has two relocations.
718 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
722 // All other Cortex-A8 stubs have only one relocation.
724 return this->destination_address_;
728 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
730 do_thumb16_special(size_t);
733 // Object of the code section containing the branch being fixed up.
735 // Section index of the code section containing the branch begin fixed up.
737 // Source address of original branch.
738 Arm_address source_address_;
739 // Destination address of the original branch.
740 Arm_address destination_address_;
741 // Original branch instruction. This is needed for copying the condition
742 // code from a condition branch to its stub.
743 uint32_t original_insn_;
746 // Stub factory class.
751 // Return the unique instance of this class.
752 static const Stub_factory&
755 static Stub_factory singleton;
759 // Make a relocation stub.
761 make_reloc_stub(Stub_type stub_type) const
763 gold_assert(stub_type >= arm_stub_reloc_first
764 && stub_type <= arm_stub_reloc_last);
765 return new Reloc_stub(this->stub_templates_[stub_type]);
768 // Make a Cortex-A8 stub.
770 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
771 Arm_address source, Arm_address destination,
772 uint32_t original_insn) const
774 gold_assert(stub_type >= arm_stub_cortex_a8_first
775 && stub_type <= arm_stub_cortex_a8_last);
776 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
777 source, destination, original_insn);
781 // Constructor and destructor are protected since we only return a single
782 // instance created in Stub_factory::get_instance().
786 // A Stub_factory may not be copied since it is a singleton.
787 Stub_factory(const Stub_factory&);
788 Stub_factory& operator=(Stub_factory&);
790 // Stub templates. These are initialized in the constructor.
791 const Stub_template* stub_templates_[arm_stub_type_last+1];
794 // A class to hold stubs for the ARM target.
796 template<bool big_endian>
797 class Stub_table : public Output_data
800 Stub_table(Arm_input_section<big_endian>* owner)
801 : Output_data(), addralign_(1), owner_(owner), has_been_changed_(false),
808 // Owner of this stub table.
809 Arm_input_section<big_endian>*
811 { return this->owner_; }
813 // Whether this stub table is empty.
816 { return this->reloc_stubs_.empty(); }
818 // Whether this has been changed.
820 has_been_changed() const
821 { return this->has_been_changed_; }
823 // Set the has-been-changed flag.
825 set_has_been_changed(bool value)
826 { this->has_been_changed_ = value; }
828 // Return the current data size.
830 current_data_size() const
831 { return this->current_data_size_for_child(); }
833 // Add a STUB with using KEY. Caller is reponsible for avoid adding
834 // if already a STUB with the same key has been added.
836 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key);
838 // Look up a relocation stub using KEY. Return NULL if there is none.
840 find_reloc_stub(const Reloc_stub::Key& key) const
842 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
843 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
846 // Relocate stubs in this stub table.
848 relocate_stubs(const Relocate_info<32, big_endian>*,
849 Target_arm<big_endian>*, Output_section*,
850 unsigned char*, Arm_address, section_size_type);
853 // Write out section contents.
855 do_write(Output_file*);
857 // Return the required alignment.
860 { return this->addralign_; }
862 // Finalize data size.
864 set_final_data_size()
865 { this->set_data_size(this->current_data_size_for_child()); }
867 // Reset address and file offset.
869 do_reset_address_and_file_offset();
872 // Unordered map of stubs.
874 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
875 Reloc_stub::Key::equal_to>
880 // Owner of this stub table.
881 Arm_input_section<big_endian>* owner_;
882 // This is set to true during relaxiong if the size of the stub table
884 bool has_been_changed_;
885 // The relocation stubs.
886 Reloc_stub_map reloc_stubs_;
889 // A class to wrap an ordinary input section containing executable code.
891 template<bool big_endian>
892 class Arm_input_section : public Output_relaxed_input_section
895 Arm_input_section(Relobj* relobj, unsigned int shndx)
896 : Output_relaxed_input_section(relobj, shndx, 1),
897 original_addralign_(1), original_size_(0), stub_table_(NULL)
907 // Whether this is a stub table owner.
909 is_stub_table_owner() const
910 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
912 // Return the stub table.
913 Stub_table<big_endian>*
915 { return this->stub_table_; }
917 // Set the stub_table.
919 set_stub_table(Stub_table<big_endian>* stub_table)
920 { this->stub_table_ = stub_table; }
922 // Downcast a base pointer to an Arm_input_section pointer. This is
923 // not type-safe but we only use Arm_input_section not the base class.
924 static Arm_input_section<big_endian>*
925 as_arm_input_section(Output_relaxed_input_section* poris)
926 { return static_cast<Arm_input_section<big_endian>*>(poris); }
929 // Write data to output file.
931 do_write(Output_file*);
933 // Return required alignment of this.
937 if (this->is_stub_table_owner())
938 return std::max(this->stub_table_->addralign(),
939 this->original_addralign_);
941 return this->original_addralign_;
944 // Finalize data size.
946 set_final_data_size();
948 // Reset address and file offset.
950 do_reset_address_and_file_offset();
954 do_output_offset(const Relobj* object, unsigned int shndx,
955 section_offset_type offset,
956 section_offset_type* poutput) const
958 if ((object == this->relobj())
959 && (shndx == this->shndx())
961 && (convert_types<uint64_t, section_offset_type>(offset)
962 <= this->original_size_))
972 // Copying is not allowed.
973 Arm_input_section(const Arm_input_section&);
974 Arm_input_section& operator=(const Arm_input_section&);
976 // Address alignment of the original input section.
977 uint64_t original_addralign_;
978 // Section size of the original input section.
979 uint64_t original_size_;
981 Stub_table<big_endian>* stub_table_;
984 // Arm output section class. This is defined mainly to add a number of
985 // stub generation methods.
987 template<bool big_endian>
988 class Arm_output_section : public Output_section
991 Arm_output_section(const char* name, elfcpp::Elf_Word type,
992 elfcpp::Elf_Xword flags)
993 : Output_section(name, type, flags)
996 ~Arm_output_section()
999 // Group input sections for stub generation.
1001 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1003 // Downcast a base pointer to an Arm_output_section pointer. This is
1004 // not type-safe but we only use Arm_output_section not the base class.
1005 static Arm_output_section<big_endian>*
1006 as_arm_output_section(Output_section* os)
1007 { return static_cast<Arm_output_section<big_endian>*>(os); }
1011 typedef Output_section::Input_section Input_section;
1012 typedef Output_section::Input_section_list Input_section_list;
1014 // Create a stub group.
1015 void create_stub_group(Input_section_list::const_iterator,
1016 Input_section_list::const_iterator,
1017 Input_section_list::const_iterator,
1018 Target_arm<big_endian>*,
1019 std::vector<Output_relaxed_input_section*>*);
1022 // Arm_relobj class.
1024 template<bool big_endian>
1025 class Arm_relobj : public Sized_relobj<32, big_endian>
1028 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1030 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1031 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1032 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1033 stub_tables_(), local_symbol_is_thumb_function_(),
1034 attributes_section_data_(NULL)
1038 { delete this->attributes_section_data_; }
1040 // Return the stub table of the SHNDX-th section if there is one.
1041 Stub_table<big_endian>*
1042 stub_table(unsigned int shndx) const
1044 gold_assert(shndx < this->stub_tables_.size());
1045 return this->stub_tables_[shndx];
1048 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1050 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1052 gold_assert(shndx < this->stub_tables_.size());
1053 this->stub_tables_[shndx] = stub_table;
1056 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1057 // index. This is only valid after do_count_local_symbol is called.
1059 local_symbol_is_thumb_function(unsigned int r_sym) const
1061 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1062 return this->local_symbol_is_thumb_function_[r_sym];
1065 // Scan all relocation sections for stub generation.
1067 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1070 // Convert regular input section with index SHNDX to a relaxed section.
1072 convert_input_section_to_relaxed_section(unsigned shndx)
1074 // The stubs have relocations and we need to process them after writing
1075 // out the stubs. So relocation now must follow section write.
1076 this->invalidate_section_offset(shndx);
1077 this->set_relocs_must_follow_section_writes();
1080 // Downcast a base pointer to an Arm_relobj pointer. This is
1081 // not type-safe but we only use Arm_relobj not the base class.
1082 static Arm_relobj<big_endian>*
1083 as_arm_relobj(Relobj* relobj)
1084 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1086 // Processor-specific flags in ELF file header. This is valid only after
1089 processor_specific_flags() const
1090 { return this->processor_specific_flags_; }
1092 // Attribute section data This is the contents of the .ARM.attribute section
1094 const Attributes_section_data*
1095 attributes_section_data() const
1096 { return this->attributes_section_data_; }
1099 // Post constructor setup.
1103 // Call parent's setup method.
1104 Sized_relobj<32, big_endian>::do_setup();
1106 // Initialize look-up tables.
1107 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1108 this->stub_tables_.swap(empty_stub_table_list);
1111 // Count the local symbols.
1113 do_count_local_symbols(Stringpool_template<char>*,
1114 Stringpool_template<char>*);
1117 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1118 const unsigned char* pshdrs,
1119 typename Sized_relobj<32, big_endian>::Views* pivews);
1121 // Read the symbol information.
1123 do_read_symbols(Read_symbols_data* sd);
1126 // List of stub tables.
1127 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1128 Stub_table_list stub_tables_;
1129 // Bit vector to tell if a local symbol is a thumb function or not.
1130 // This is only valid after do_count_local_symbol is called.
1131 std::vector<bool> local_symbol_is_thumb_function_;
1132 // processor-specific flags in ELF file header.
1133 elfcpp::Elf_Word processor_specific_flags_;
1134 // Object attributes if there is an .ARM.attributes section or NULL.
1135 Attributes_section_data* attributes_section_data_;
1138 // Arm_dynobj class.
1140 template<bool big_endian>
1141 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1144 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1145 const elfcpp::Ehdr<32, big_endian>& ehdr)
1146 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1147 processor_specific_flags_(0), attributes_section_data_(NULL)
1151 { delete this->attributes_section_data_; }
1153 // Downcast a base pointer to an Arm_relobj pointer. This is
1154 // not type-safe but we only use Arm_relobj not the base class.
1155 static Arm_dynobj<big_endian>*
1156 as_arm_dynobj(Dynobj* dynobj)
1157 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1159 // Processor-specific flags in ELF file header. This is valid only after
1162 processor_specific_flags() const
1163 { return this->processor_specific_flags_; }
1165 // Attributes section data.
1166 const Attributes_section_data*
1167 attributes_section_data() const
1168 { return this->attributes_section_data_; }
1171 // Read the symbol information.
1173 do_read_symbols(Read_symbols_data* sd);
1176 // processor-specific flags in ELF file header.
1177 elfcpp::Elf_Word processor_specific_flags_;
1178 // Object attributes if there is an .ARM.attributes section or NULL.
1179 Attributes_section_data* attributes_section_data_;
1182 // Functor to read reloc addends during stub generation.
1184 template<int sh_type, bool big_endian>
1185 struct Stub_addend_reader
1187 // Return the addend for a relocation of a particular type. Depending
1188 // on whether this is a REL or RELA relocation, read the addend from a
1189 // view or from a Reloc object.
1190 elfcpp::Elf_types<32>::Elf_Swxword
1192 unsigned int /* r_type */,
1193 const unsigned char* /* view */,
1194 const typename Reloc_types<sh_type,
1195 32, big_endian>::Reloc& /* reloc */) const;
1198 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1200 template<bool big_endian>
1201 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1203 elfcpp::Elf_types<32>::Elf_Swxword
1206 const unsigned char*,
1207 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1210 // Specialized Stub_addend_reader for RELA type relocation sections.
1211 // We currently do not handle RELA type relocation sections but it is trivial
1212 // to implement the addend reader. This is provided for completeness and to
1213 // make it easier to add support for RELA relocation sections in the future.
1215 template<bool big_endian>
1216 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1218 elfcpp::Elf_types<32>::Elf_Swxword
1221 const unsigned char*,
1222 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1223 big_endian>::Reloc& reloc) const
1224 { return reloc.get_r_addend(); }
1227 // Utilities for manipulating integers of up to 32-bits
1231 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1232 // an int32_t. NO_BITS must be between 1 to 32.
1233 template<int no_bits>
1234 static inline int32_t
1235 sign_extend(uint32_t bits)
1237 gold_assert(no_bits >= 0 && no_bits <= 32);
1239 return static_cast<int32_t>(bits);
1240 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1242 uint32_t top_bit = 1U << (no_bits - 1);
1243 int32_t as_signed = static_cast<int32_t>(bits);
1244 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1247 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1248 template<int no_bits>
1250 has_overflow(uint32_t bits)
1252 gold_assert(no_bits >= 0 && no_bits <= 32);
1255 int32_t max = (1 << (no_bits - 1)) - 1;
1256 int32_t min = -(1 << (no_bits - 1));
1257 int32_t as_signed = static_cast<int32_t>(bits);
1258 return as_signed > max || as_signed < min;
1261 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1262 // fits in the given number of bits as either a signed or unsigned value.
1263 // For example, has_signed_unsigned_overflow<8> would check
1264 // -128 <= bits <= 255
1265 template<int no_bits>
1267 has_signed_unsigned_overflow(uint32_t bits)
1269 gold_assert(no_bits >= 2 && no_bits <= 32);
1272 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1273 int32_t min = -(1 << (no_bits - 1));
1274 int32_t as_signed = static_cast<int32_t>(bits);
1275 return as_signed > max || as_signed < min;
1278 // Select bits from A and B using bits in MASK. For each n in [0..31],
1279 // the n-th bit in the result is chosen from the n-th bits of A and B.
1280 // A zero selects A and a one selects B.
1281 static inline uint32_t
1282 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1283 { return (a & ~mask) | (b & mask); }
1286 template<bool big_endian>
1287 class Target_arm : public Sized_target<32, big_endian>
1290 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1293 // When were are relocating a stub, we pass this as the relocation number.
1294 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1297 : Sized_target<32, big_endian>(&arm_info),
1298 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1299 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1300 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1301 should_force_pic_veneer_(false), arm_input_section_map_(),
1302 attributes_section_data_(NULL)
1305 // Whether we can use BLX.
1308 { return this->may_use_blx_; }
1310 // Set use-BLX flag.
1312 set_may_use_blx(bool value)
1313 { this->may_use_blx_ = value; }
1315 // Whether we force PCI branch veneers.
1317 should_force_pic_veneer() const
1318 { return this->should_force_pic_veneer_; }
1320 // Set PIC veneer flag.
1322 set_should_force_pic_veneer(bool value)
1323 { this->should_force_pic_veneer_ = value; }
1325 // Whether we use THUMB-2 instructions.
1327 using_thumb2() const
1329 Object_attribute* attr =
1330 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1331 int arch = attr->int_value();
1332 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
1335 // Whether we use THUMB/THUMB-2 instructions only.
1337 using_thumb_only() const
1339 Object_attribute* attr =
1340 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1341 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1342 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1344 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1345 return attr->int_value() == 'M';
1348 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1350 may_use_arm_nop() const
1352 Object_attribute* attr =
1353 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1354 int arch = attr->int_value();
1355 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1356 || arch == elfcpp::TAG_CPU_ARCH_V6K
1357 || arch == elfcpp::TAG_CPU_ARCH_V7
1358 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1361 // Whether we have THUMB-2 NOP.W instruction.
1363 may_use_thumb2_nop() const
1365 Object_attribute* attr =
1366 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1367 int arch = attr->int_value();
1368 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1369 || arch == elfcpp::TAG_CPU_ARCH_V7
1370 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1373 // Process the relocations to determine unreferenced sections for
1374 // garbage collection.
1376 gc_process_relocs(Symbol_table* symtab,
1378 Sized_relobj<32, big_endian>* object,
1379 unsigned int data_shndx,
1380 unsigned int sh_type,
1381 const unsigned char* prelocs,
1383 Output_section* output_section,
1384 bool needs_special_offset_handling,
1385 size_t local_symbol_count,
1386 const unsigned char* plocal_symbols);
1388 // Scan the relocations to look for symbol adjustments.
1390 scan_relocs(Symbol_table* symtab,
1392 Sized_relobj<32, big_endian>* object,
1393 unsigned int data_shndx,
1394 unsigned int sh_type,
1395 const unsigned char* prelocs,
1397 Output_section* output_section,
1398 bool needs_special_offset_handling,
1399 size_t local_symbol_count,
1400 const unsigned char* plocal_symbols);
1402 // Finalize the sections.
1404 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1406 // Return the value to use for a dynamic symbol which requires special
1409 do_dynsym_value(const Symbol*) const;
1411 // Relocate a section.
1413 relocate_section(const Relocate_info<32, big_endian>*,
1414 unsigned int sh_type,
1415 const unsigned char* prelocs,
1417 Output_section* output_section,
1418 bool needs_special_offset_handling,
1419 unsigned char* view,
1420 Arm_address view_address,
1421 section_size_type view_size,
1422 const Reloc_symbol_changes*);
1424 // Scan the relocs during a relocatable link.
1426 scan_relocatable_relocs(Symbol_table* symtab,
1428 Sized_relobj<32, big_endian>* object,
1429 unsigned int data_shndx,
1430 unsigned int sh_type,
1431 const unsigned char* prelocs,
1433 Output_section* output_section,
1434 bool needs_special_offset_handling,
1435 size_t local_symbol_count,
1436 const unsigned char* plocal_symbols,
1437 Relocatable_relocs*);
1439 // Relocate a section during a relocatable link.
1441 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1442 unsigned int sh_type,
1443 const unsigned char* prelocs,
1445 Output_section* output_section,
1446 off_t offset_in_output_section,
1447 const Relocatable_relocs*,
1448 unsigned char* view,
1449 Arm_address view_address,
1450 section_size_type view_size,
1451 unsigned char* reloc_view,
1452 section_size_type reloc_view_size);
1454 // Return whether SYM is defined by the ABI.
1456 do_is_defined_by_abi(Symbol* sym) const
1457 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1459 // Return the size of the GOT section.
1463 gold_assert(this->got_ != NULL);
1464 return this->got_->data_size();
1467 // Map platform-specific reloc types
1469 get_real_reloc_type (unsigned int r_type);
1472 // Methods to support stub-generations.
1475 // Return the stub factory
1477 stub_factory() const
1478 { return this->stub_factory_; }
1480 // Make a new Arm_input_section object.
1481 Arm_input_section<big_endian>*
1482 new_arm_input_section(Relobj*, unsigned int);
1484 // Find the Arm_input_section object corresponding to the SHNDX-th input
1485 // section of RELOBJ.
1486 Arm_input_section<big_endian>*
1487 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
1489 // Make a new Stub_table
1490 Stub_table<big_endian>*
1491 new_stub_table(Arm_input_section<big_endian>*);
1493 // Scan a section for stub generation.
1495 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
1496 const unsigned char*, size_t, Output_section*,
1497 bool, const unsigned char*, Arm_address,
1502 relocate_stub(Reloc_stub*, const Relocate_info<32, big_endian>*,
1503 Output_section*, unsigned char*, Arm_address,
1506 // Get the default ARM target.
1507 static Target_arm<big_endian>*
1510 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1511 && parameters->target().is_big_endian() == big_endian);
1512 return static_cast<Target_arm<big_endian>*>(
1513 parameters->sized_target<32, big_endian>());
1516 // Whether relocation type uses LSB to distinguish THUMB addresses.
1518 reloc_uses_thumb_bit(unsigned int r_type);
1521 // Make an ELF object.
1523 do_make_elf_object(const std::string&, Input_file*, off_t,
1524 const elfcpp::Ehdr<32, big_endian>& ehdr);
1527 do_make_elf_object(const std::string&, Input_file*, off_t,
1528 const elfcpp::Ehdr<32, !big_endian>&)
1529 { gold_unreachable(); }
1532 do_make_elf_object(const std::string&, Input_file*, off_t,
1533 const elfcpp::Ehdr<64, false>&)
1534 { gold_unreachable(); }
1537 do_make_elf_object(const std::string&, Input_file*, off_t,
1538 const elfcpp::Ehdr<64, true>&)
1539 { gold_unreachable(); }
1541 // Make an output section.
1543 do_make_output_section(const char* name, elfcpp::Elf_Word type,
1544 elfcpp::Elf_Xword flags)
1545 { return new Arm_output_section<big_endian>(name, type, flags); }
1548 do_adjust_elf_header(unsigned char* view, int len) const;
1550 // We only need to generate stubs, and hence perform relaxation if we are
1551 // not doing relocatable linking.
1553 do_may_relax() const
1554 { return !parameters->options().relocatable(); }
1557 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
1559 // Determine whether an object attribute tag takes an integer, a
1562 do_attribute_arg_type(int tag) const;
1564 // Reorder tags during output.
1566 do_attributes_order(int num) const;
1569 // The class which scans relocations.
1574 : issued_non_pic_error_(false)
1578 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1579 Sized_relobj<32, big_endian>* object,
1580 unsigned int data_shndx,
1581 Output_section* output_section,
1582 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1583 const elfcpp::Sym<32, big_endian>& lsym);
1586 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1587 Sized_relobj<32, big_endian>* object,
1588 unsigned int data_shndx,
1589 Output_section* output_section,
1590 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1595 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1596 unsigned int r_type);
1599 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1600 unsigned int r_type, Symbol*);
1603 check_non_pic(Relobj*, unsigned int r_type);
1605 // Almost identical to Symbol::needs_plt_entry except that it also
1606 // handles STT_ARM_TFUNC.
1608 symbol_needs_plt_entry(const Symbol* sym)
1610 // An undefined symbol from an executable does not need a PLT entry.
1611 if (sym->is_undefined() && !parameters->options().shared())
1614 return (!parameters->doing_static_link()
1615 && (sym->type() == elfcpp::STT_FUNC
1616 || sym->type() == elfcpp::STT_ARM_TFUNC)
1617 && (sym->is_from_dynobj()
1618 || sym->is_undefined()
1619 || sym->is_preemptible()));
1622 // Whether we have issued an error about a non-PIC compilation.
1623 bool issued_non_pic_error_;
1626 // The class which implements relocation.
1636 // Return whether the static relocation needs to be applied.
1638 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1641 Output_section* output_section);
1643 // Do a relocation. Return false if the caller should not issue
1644 // any warnings about this relocation.
1646 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1647 Output_section*, size_t relnum,
1648 const elfcpp::Rel<32, big_endian>&,
1649 unsigned int r_type, const Sized_symbol<32>*,
1650 const Symbol_value<32>*,
1651 unsigned char*, Arm_address,
1654 // Return whether we want to pass flag NON_PIC_REF for this
1655 // reloc. This means the relocation type accesses a symbol not via
1658 reloc_is_non_pic (unsigned int r_type)
1662 // These relocation types reference GOT or PLT entries explicitly.
1663 case elfcpp::R_ARM_GOT_BREL:
1664 case elfcpp::R_ARM_GOT_ABS:
1665 case elfcpp::R_ARM_GOT_PREL:
1666 case elfcpp::R_ARM_GOT_BREL12:
1667 case elfcpp::R_ARM_PLT32_ABS:
1668 case elfcpp::R_ARM_TLS_GD32:
1669 case elfcpp::R_ARM_TLS_LDM32:
1670 case elfcpp::R_ARM_TLS_IE32:
1671 case elfcpp::R_ARM_TLS_IE12GP:
1673 // These relocate types may use PLT entries.
1674 case elfcpp::R_ARM_CALL:
1675 case elfcpp::R_ARM_THM_CALL:
1676 case elfcpp::R_ARM_JUMP24:
1677 case elfcpp::R_ARM_THM_JUMP24:
1678 case elfcpp::R_ARM_THM_JUMP19:
1679 case elfcpp::R_ARM_PLT32:
1680 case elfcpp::R_ARM_THM_XPC22:
1689 // A class which returns the size required for a relocation type,
1690 // used while scanning relocs during a relocatable link.
1691 class Relocatable_size_for_reloc
1695 get_size_for_reloc(unsigned int, Relobj*);
1698 // Get the GOT section, creating it if necessary.
1699 Output_data_got<32, big_endian>*
1700 got_section(Symbol_table*, Layout*);
1702 // Get the GOT PLT section.
1704 got_plt_section() const
1706 gold_assert(this->got_plt_ != NULL);
1707 return this->got_plt_;
1710 // Create a PLT entry for a global symbol.
1712 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1714 // Get the PLT section.
1715 const Output_data_plt_arm<big_endian>*
1718 gold_assert(this->plt_ != NULL);
1722 // Get the dynamic reloc section, creating it if necessary.
1724 rel_dyn_section(Layout*);
1726 // Return true if the symbol may need a COPY relocation.
1727 // References from an executable object to non-function symbols
1728 // defined in a dynamic object may need a COPY relocation.
1730 may_need_copy_reloc(Symbol* gsym)
1732 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1733 && gsym->may_need_copy_reloc());
1736 // Add a potential copy relocation.
1738 copy_reloc(Symbol_table* symtab, Layout* layout,
1739 Sized_relobj<32, big_endian>* object,
1740 unsigned int shndx, Output_section* output_section,
1741 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1743 this->copy_relocs_.copy_reloc(symtab, layout,
1744 symtab->get_sized_symbol<32>(sym),
1745 object, shndx, output_section, reloc,
1746 this->rel_dyn_section(layout));
1749 // Whether two EABI versions are compatible.
1751 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1753 // Merge processor-specific flags from input object and those in the ELF
1754 // header of the output.
1756 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1758 // Get the secondary compatible architecture.
1760 get_secondary_compatible_arch(const Attributes_section_data*);
1762 // Set the secondary compatible architecture.
1764 set_secondary_compatible_arch(Attributes_section_data*, int);
1767 tag_cpu_arch_combine(const char*, int, int*, int, int);
1769 // Helper to print AEABI enum tag value.
1771 aeabi_enum_name(unsigned int);
1773 // Return string value for TAG_CPU_name.
1775 tag_cpu_name_value(unsigned int);
1777 // Merge object attributes from input object and those in the output.
1779 merge_object_attributes(const char*, const Attributes_section_data*);
1781 // Helper to get an AEABI object attribute
1783 get_aeabi_object_attribute(int tag) const
1785 Attributes_section_data* pasd = this->attributes_section_data_;
1786 gold_assert(pasd != NULL);
1787 Object_attribute* attr =
1788 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
1789 gold_assert(attr != NULL);
1794 // Methods to support stub-generations.
1797 // Group input sections for stub generation.
1799 group_sections(Layout*, section_size_type, bool);
1801 // Scan a relocation for stub generation.
1803 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
1804 const Sized_symbol<32>*, unsigned int,
1805 const Symbol_value<32>*,
1806 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
1808 // Scan a relocation section for stub.
1809 template<int sh_type>
1811 scan_reloc_section_for_stubs(
1812 const Relocate_info<32, big_endian>* relinfo,
1813 const unsigned char* prelocs,
1815 Output_section* output_section,
1816 bool needs_special_offset_handling,
1817 const unsigned char* view,
1818 elfcpp::Elf_types<32>::Elf_Addr view_address,
1821 // Information about this specific target which we pass to the
1822 // general Target structure.
1823 static const Target::Target_info arm_info;
1825 // The types of GOT entries needed for this platform.
1828 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1831 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
1833 // Map input section to Arm_input_section.
1834 typedef Unordered_map<Input_section_specifier,
1835 Arm_input_section<big_endian>*,
1836 Input_section_specifier::hash,
1837 Input_section_specifier::equal_to>
1838 Arm_input_section_map;
1841 Output_data_got<32, big_endian>* got_;
1843 Output_data_plt_arm<big_endian>* plt_;
1844 // The GOT PLT section.
1845 Output_data_space* got_plt_;
1846 // The dynamic reloc section.
1847 Reloc_section* rel_dyn_;
1848 // Relocs saved to avoid a COPY reloc.
1849 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1850 // Space for variables copied with a COPY reloc.
1851 Output_data_space* dynbss_;
1852 // Vector of Stub_tables created.
1853 Stub_table_list stub_tables_;
1855 const Stub_factory &stub_factory_;
1856 // Whether we can use BLX.
1858 // Whether we force PIC branch veneers.
1859 bool should_force_pic_veneer_;
1860 // Map for locating Arm_input_sections.
1861 Arm_input_section_map arm_input_section_map_;
1862 // Attributes section data in output.
1863 Attributes_section_data* attributes_section_data_;
1866 template<bool big_endian>
1867 const Target::Target_info Target_arm<big_endian>::arm_info =
1870 big_endian, // is_big_endian
1871 elfcpp::EM_ARM, // machine_code
1872 false, // has_make_symbol
1873 false, // has_resolve
1874 false, // has_code_fill
1875 true, // is_default_stack_executable
1877 "/usr/lib/libc.so.1", // dynamic_linker
1878 0x8000, // default_text_segment_address
1879 0x1000, // abi_pagesize (overridable by -z max-page-size)
1880 0x1000, // common_pagesize (overridable by -z common-page-size)
1881 elfcpp::SHN_UNDEF, // small_common_shndx
1882 elfcpp::SHN_UNDEF, // large_common_shndx
1883 0, // small_common_section_flags
1884 0, // large_common_section_flags
1885 ".ARM.attributes", // attributes_section
1886 "aeabi" // attributes_vendor
1889 // Arm relocate functions class
1892 template<bool big_endian>
1893 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1898 STATUS_OKAY, // No error during relocation.
1899 STATUS_OVERFLOW, // Relocation oveflow.
1900 STATUS_BAD_RELOC // Relocation cannot be applied.
1904 typedef Relocate_functions<32, big_endian> Base;
1905 typedef Arm_relocate_functions<big_endian> This;
1907 // Encoding of imm16 argument for movt and movw ARM instructions
1910 // imm16 := imm4 | imm12
1912 // 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
1913 // +-------+---------------+-------+-------+-----------------------+
1914 // | | |imm4 | |imm12 |
1915 // +-------+---------------+-------+-------+-----------------------+
1917 // Extract the relocation addend from VAL based on the ARM
1918 // instruction encoding described above.
1919 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1920 extract_arm_movw_movt_addend(
1921 typename elfcpp::Swap<32, big_endian>::Valtype val)
1923 // According to the Elf ABI for ARM Architecture the immediate
1924 // field is sign-extended to form the addend.
1925 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1928 // Insert X into VAL based on the ARM instruction encoding described
1930 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1931 insert_val_arm_movw_movt(
1932 typename elfcpp::Swap<32, big_endian>::Valtype val,
1933 typename elfcpp::Swap<32, big_endian>::Valtype x)
1937 val |= (x & 0xf000) << 4;
1941 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1944 // imm16 := imm4 | i | imm3 | imm8
1946 // 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
1947 // +---------+-+-----------+-------++-+-----+-------+---------------+
1948 // | |i| |imm4 || |imm3 | |imm8 |
1949 // +---------+-+-----------+-------++-+-----+-------+---------------+
1951 // Extract the relocation addend from VAL based on the Thumb2
1952 // instruction encoding described above.
1953 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1954 extract_thumb_movw_movt_addend(
1955 typename elfcpp::Swap<32, big_endian>::Valtype val)
1957 // According to the Elf ABI for ARM Architecture the immediate
1958 // field is sign-extended to form the addend.
1959 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1960 | ((val >> 15) & 0x0800)
1961 | ((val >> 4) & 0x0700)
1965 // Insert X into VAL based on the Thumb2 instruction encoding
1967 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1968 insert_val_thumb_movw_movt(
1969 typename elfcpp::Swap<32, big_endian>::Valtype val,
1970 typename elfcpp::Swap<32, big_endian>::Valtype x)
1973 val |= (x & 0xf000) << 4;
1974 val |= (x & 0x0800) << 15;
1975 val |= (x & 0x0700) << 4;
1976 val |= (x & 0x00ff);
1980 // Handle ARM long branches.
1981 static typename This::Status
1982 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
1983 unsigned char *, const Sized_symbol<32>*,
1984 const Arm_relobj<big_endian>*, unsigned int,
1985 const Symbol_value<32>*, Arm_address, Arm_address, bool);
1987 // Handle THUMB long branches.
1988 static typename This::Status
1989 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
1990 unsigned char *, const Sized_symbol<32>*,
1991 const Arm_relobj<big_endian>*, unsigned int,
1992 const Symbol_value<32>*, Arm_address, Arm_address, bool);
1996 // R_ARM_ABS8: S + A
1997 static inline typename This::Status
1998 abs8(unsigned char *view,
1999 const Sized_relobj<32, big_endian>* object,
2000 const Symbol_value<32>* psymval)
2002 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2003 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2004 Valtype* wv = reinterpret_cast<Valtype*>(view);
2005 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2006 Reltype addend = utils::sign_extend<8>(val);
2007 Reltype x = psymval->value(object, addend);
2008 val = utils::bit_select(val, x, 0xffU);
2009 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2010 return (utils::has_signed_unsigned_overflow<8>(x)
2011 ? This::STATUS_OVERFLOW
2012 : This::STATUS_OKAY);
2015 // R_ARM_THM_ABS5: S + A
2016 static inline typename This::Status
2017 thm_abs5(unsigned char *view,
2018 const Sized_relobj<32, big_endian>* object,
2019 const Symbol_value<32>* psymval)
2021 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2022 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2023 Valtype* wv = reinterpret_cast<Valtype*>(view);
2024 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2025 Reltype addend = (val & 0x7e0U) >> 6;
2026 Reltype x = psymval->value(object, addend);
2027 val = utils::bit_select(val, x << 6, 0x7e0U);
2028 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2029 return (utils::has_overflow<5>(x)
2030 ? This::STATUS_OVERFLOW
2031 : This::STATUS_OKAY);
2034 // R_ARM_ABS12: S + A
2035 static inline typename This::Status
2036 abs12(unsigned char *view,
2037 const Sized_relobj<32, big_endian>* object,
2038 const Symbol_value<32>* psymval)
2040 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2041 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2042 Valtype* wv = reinterpret_cast<Valtype*>(view);
2043 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2044 Reltype addend = val & 0x0fffU;
2045 Reltype x = psymval->value(object, addend);
2046 val = utils::bit_select(val, x, 0x0fffU);
2047 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2048 return (utils::has_overflow<12>(x)
2049 ? This::STATUS_OVERFLOW
2050 : This::STATUS_OKAY);
2053 // R_ARM_ABS16: S + A
2054 static inline typename This::Status
2055 abs16(unsigned char *view,
2056 const Sized_relobj<32, big_endian>* object,
2057 const Symbol_value<32>* psymval)
2059 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2060 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2061 Valtype* wv = reinterpret_cast<Valtype*>(view);
2062 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2063 Reltype addend = utils::sign_extend<16>(val);
2064 Reltype x = psymval->value(object, addend);
2065 val = utils::bit_select(val, x, 0xffffU);
2066 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2067 return (utils::has_signed_unsigned_overflow<16>(x)
2068 ? This::STATUS_OVERFLOW
2069 : This::STATUS_OKAY);
2072 // R_ARM_ABS32: (S + A) | T
2073 static inline typename This::Status
2074 abs32(unsigned char *view,
2075 const Sized_relobj<32, big_endian>* object,
2076 const Symbol_value<32>* psymval,
2077 Arm_address thumb_bit)
2079 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2080 Valtype* wv = reinterpret_cast<Valtype*>(view);
2081 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2082 Valtype x = psymval->value(object, addend) | thumb_bit;
2083 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2084 return This::STATUS_OKAY;
2087 // R_ARM_REL32: (S + A) | T - P
2088 static inline typename This::Status
2089 rel32(unsigned char *view,
2090 const Sized_relobj<32, big_endian>* object,
2091 const Symbol_value<32>* psymval,
2092 Arm_address address,
2093 Arm_address thumb_bit)
2095 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2096 Valtype* wv = reinterpret_cast<Valtype*>(view);
2097 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2098 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2099 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2100 return This::STATUS_OKAY;
2103 // R_ARM_THM_CALL: (S + A) | T - P
2104 static inline typename This::Status
2105 thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2106 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2107 unsigned int r_sym, const Symbol_value<32>* psymval,
2108 Arm_address address, Arm_address thumb_bit,
2109 bool is_weakly_undefined_without_plt)
2111 return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
2112 object, r_sym, psymval, address, thumb_bit,
2113 is_weakly_undefined_without_plt);
2116 // R_ARM_THM_JUMP24: (S + A) | T - P
2117 static inline typename This::Status
2118 thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2119 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2120 unsigned int r_sym, const Symbol_value<32>* psymval,
2121 Arm_address address, Arm_address thumb_bit,
2122 bool is_weakly_undefined_without_plt)
2124 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
2125 object, r_sym, psymval, address, thumb_bit,
2126 is_weakly_undefined_without_plt);
2129 // R_ARM_THM_XPC22: (S + A) | T - P
2130 static inline typename This::Status
2131 thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2132 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2133 unsigned int r_sym, const Symbol_value<32>* psymval,
2134 Arm_address address, Arm_address thumb_bit,
2135 bool is_weakly_undefined_without_plt)
2137 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
2138 object, r_sym, psymval, address, thumb_bit,
2139 is_weakly_undefined_without_plt);
2142 // R_ARM_BASE_PREL: B(S) + A - P
2143 static inline typename This::Status
2144 base_prel(unsigned char* view,
2146 Arm_address address)
2148 Base::rel32(view, origin - address);
2152 // R_ARM_BASE_ABS: B(S) + A
2153 static inline typename This::Status
2154 base_abs(unsigned char* view,
2157 Base::rel32(view, origin);
2161 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2162 static inline typename This::Status
2163 got_brel(unsigned char* view,
2164 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
2166 Base::rel32(view, got_offset);
2167 return This::STATUS_OKAY;
2170 // R_ARM_GOT_PREL: GOT(S) + A - P
2171 static inline typename This::Status
2172 got_prel(unsigned char *view,
2173 Arm_address got_entry,
2174 Arm_address address)
2176 Base::rel32(view, got_entry - address);
2177 return This::STATUS_OKAY;
2180 // R_ARM_PLT32: (S + A) | T - P
2181 static inline typename This::Status
2182 plt32(const Relocate_info<32, big_endian>* relinfo,
2183 unsigned char *view,
2184 const Sized_symbol<32>* gsym,
2185 const Arm_relobj<big_endian>* object,
2187 const Symbol_value<32>* psymval,
2188 Arm_address address,
2189 Arm_address thumb_bit,
2190 bool is_weakly_undefined_without_plt)
2192 return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
2193 object, r_sym, psymval, address, thumb_bit,
2194 is_weakly_undefined_without_plt);
2197 // R_ARM_XPC25: (S + A) | T - P
2198 static inline typename This::Status
2199 xpc25(const Relocate_info<32, big_endian>* relinfo,
2200 unsigned char *view,
2201 const Sized_symbol<32>* gsym,
2202 const Arm_relobj<big_endian>* object,
2204 const Symbol_value<32>* psymval,
2205 Arm_address address,
2206 Arm_address thumb_bit,
2207 bool is_weakly_undefined_without_plt)
2209 return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
2210 object, r_sym, psymval, address, thumb_bit,
2211 is_weakly_undefined_without_plt);
2214 // R_ARM_CALL: (S + A) | T - P
2215 static inline typename This::Status
2216 call(const Relocate_info<32, big_endian>* relinfo,
2217 unsigned char *view,
2218 const Sized_symbol<32>* gsym,
2219 const Arm_relobj<big_endian>* object,
2221 const Symbol_value<32>* psymval,
2222 Arm_address address,
2223 Arm_address thumb_bit,
2224 bool is_weakly_undefined_without_plt)
2226 return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2227 object, r_sym, psymval, address, thumb_bit,
2228 is_weakly_undefined_without_plt);
2231 // R_ARM_JUMP24: (S + A) | T - P
2232 static inline typename This::Status
2233 jump24(const Relocate_info<32, big_endian>* relinfo,
2234 unsigned char *view,
2235 const Sized_symbol<32>* gsym,
2236 const Arm_relobj<big_endian>* object,
2238 const Symbol_value<32>* psymval,
2239 Arm_address address,
2240 Arm_address thumb_bit,
2241 bool is_weakly_undefined_without_plt)
2243 return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2244 object, r_sym, psymval, address, thumb_bit,
2245 is_weakly_undefined_without_plt);
2248 // R_ARM_PREL: (S + A) | T - P
2249 static inline typename This::Status
2250 prel31(unsigned char *view,
2251 const Sized_relobj<32, big_endian>* object,
2252 const Symbol_value<32>* psymval,
2253 Arm_address address,
2254 Arm_address thumb_bit)
2256 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2257 Valtype* wv = reinterpret_cast<Valtype*>(view);
2258 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2259 Valtype addend = utils::sign_extend<31>(val);
2260 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2261 val = utils::bit_select(val, x, 0x7fffffffU);
2262 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2263 return (utils::has_overflow<31>(x) ?
2264 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2267 // R_ARM_MOVW_ABS_NC: (S + A) | T
2268 static inline typename This::Status
2269 movw_abs_nc(unsigned char *view,
2270 const Sized_relobj<32, big_endian>* object,
2271 const Symbol_value<32>* psymval,
2272 Arm_address thumb_bit)
2274 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2275 Valtype* wv = reinterpret_cast<Valtype*>(view);
2276 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2277 Valtype addend = This::extract_arm_movw_movt_addend(val);
2278 Valtype x = psymval->value(object, addend) | thumb_bit;
2279 val = This::insert_val_arm_movw_movt(val, x);
2280 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2281 return This::STATUS_OKAY;
2284 // R_ARM_MOVT_ABS: S + A
2285 static inline typename This::Status
2286 movt_abs(unsigned char *view,
2287 const Sized_relobj<32, big_endian>* object,
2288 const Symbol_value<32>* psymval)
2290 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2291 Valtype* wv = reinterpret_cast<Valtype*>(view);
2292 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2293 Valtype addend = This::extract_arm_movw_movt_addend(val);
2294 Valtype x = psymval->value(object, addend) >> 16;
2295 val = This::insert_val_arm_movw_movt(val, x);
2296 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2297 return This::STATUS_OKAY;
2300 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2301 static inline typename This::Status
2302 thm_movw_abs_nc(unsigned char *view,
2303 const Sized_relobj<32, big_endian>* object,
2304 const Symbol_value<32>* psymval,
2305 Arm_address thumb_bit)
2307 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2308 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2309 Valtype* wv = reinterpret_cast<Valtype*>(view);
2310 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2311 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2312 Reltype addend = extract_thumb_movw_movt_addend(val);
2313 Reltype x = psymval->value(object, addend) | thumb_bit;
2314 val = This::insert_val_thumb_movw_movt(val, x);
2315 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2316 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2317 return This::STATUS_OKAY;
2320 // R_ARM_THM_MOVT_ABS: S + A
2321 static inline typename This::Status
2322 thm_movt_abs(unsigned char *view,
2323 const Sized_relobj<32, big_endian>* object,
2324 const Symbol_value<32>* psymval)
2326 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2327 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2328 Valtype* wv = reinterpret_cast<Valtype*>(view);
2329 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2330 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2331 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2332 Reltype x = psymval->value(object, addend) >> 16;
2333 val = This::insert_val_thumb_movw_movt(val, x);
2334 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2335 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2336 return This::STATUS_OKAY;
2339 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2340 static inline typename This::Status
2341 movw_prel_nc(unsigned char *view,
2342 const Sized_relobj<32, big_endian>* object,
2343 const Symbol_value<32>* psymval,
2344 Arm_address address,
2345 Arm_address thumb_bit)
2347 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2348 Valtype* wv = reinterpret_cast<Valtype*>(view);
2349 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2350 Valtype addend = This::extract_arm_movw_movt_addend(val);
2351 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2352 val = This::insert_val_arm_movw_movt(val, x);
2353 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2354 return This::STATUS_OKAY;
2357 // R_ARM_MOVT_PREL: S + A - P
2358 static inline typename This::Status
2359 movt_prel(unsigned char *view,
2360 const Sized_relobj<32, big_endian>* object,
2361 const Symbol_value<32>* psymval,
2362 Arm_address address)
2364 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2365 Valtype* wv = reinterpret_cast<Valtype*>(view);
2366 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2367 Valtype addend = This::extract_arm_movw_movt_addend(val);
2368 Valtype x = (psymval->value(object, addend) - address) >> 16;
2369 val = This::insert_val_arm_movw_movt(val, x);
2370 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2371 return This::STATUS_OKAY;
2374 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2375 static inline typename This::Status
2376 thm_movw_prel_nc(unsigned char *view,
2377 const Sized_relobj<32, big_endian>* object,
2378 const Symbol_value<32>* psymval,
2379 Arm_address address,
2380 Arm_address thumb_bit)
2382 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2383 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2384 Valtype* wv = reinterpret_cast<Valtype*>(view);
2385 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2386 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2387 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2388 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
2389 val = This::insert_val_thumb_movw_movt(val, x);
2390 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2391 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2392 return This::STATUS_OKAY;
2395 // R_ARM_THM_MOVT_PREL: S + A - P
2396 static inline typename This::Status
2397 thm_movt_prel(unsigned char *view,
2398 const Sized_relobj<32, big_endian>* object,
2399 const Symbol_value<32>* psymval,
2400 Arm_address address)
2402 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2403 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2404 Valtype* wv = reinterpret_cast<Valtype*>(view);
2405 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2406 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2407 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2408 Reltype x = (psymval->value(object, addend) - address) >> 16;
2409 val = This::insert_val_thumb_movw_movt(val, x);
2410 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2411 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2412 return This::STATUS_OKAY;
2416 // Relocate ARM long branches. This handles relocation types
2417 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2418 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2419 // undefined and we do not use PLT in this relocation. In such a case,
2420 // the branch is converted into an NOP.
2422 template<bool big_endian>
2423 typename Arm_relocate_functions<big_endian>::Status
2424 Arm_relocate_functions<big_endian>::arm_branch_common(
2425 unsigned int r_type,
2426 const Relocate_info<32, big_endian>* relinfo,
2427 unsigned char *view,
2428 const Sized_symbol<32>* gsym,
2429 const Arm_relobj<big_endian>* object,
2431 const Symbol_value<32>* psymval,
2432 Arm_address address,
2433 Arm_address thumb_bit,
2434 bool is_weakly_undefined_without_plt)
2436 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2437 Valtype* wv = reinterpret_cast<Valtype*>(view);
2438 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2440 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
2441 && ((val & 0x0f000000UL) == 0x0a000000UL);
2442 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
2443 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
2444 && ((val & 0x0f000000UL) == 0x0b000000UL);
2445 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
2446 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
2448 // Check that the instruction is valid.
2449 if (r_type == elfcpp::R_ARM_CALL)
2451 if (!insn_is_uncond_bl && !insn_is_blx)
2452 return This::STATUS_BAD_RELOC;
2454 else if (r_type == elfcpp::R_ARM_JUMP24)
2456 if (!insn_is_b && !insn_is_cond_bl)
2457 return This::STATUS_BAD_RELOC;
2459 else if (r_type == elfcpp::R_ARM_PLT32)
2461 if (!insn_is_any_branch)
2462 return This::STATUS_BAD_RELOC;
2464 else if (r_type == elfcpp::R_ARM_XPC25)
2466 // FIXME: AAELF document IH0044C does not say much about it other
2467 // than it being obsolete.
2468 if (!insn_is_any_branch)
2469 return This::STATUS_BAD_RELOC;
2474 // A branch to an undefined weak symbol is turned into a jump to
2475 // the next instruction unless a PLT entry will be created.
2476 // Do the same for local undefined symbols.
2477 // The jump to the next instruction is optimized as a NOP depending
2478 // on the architecture.
2479 const Target_arm<big_endian>* arm_target =
2480 Target_arm<big_endian>::default_target();
2481 if (is_weakly_undefined_without_plt)
2483 Valtype cond = val & 0xf0000000U;
2484 if (arm_target->may_use_arm_nop())
2485 val = cond | 0x0320f000;
2487 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2488 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2489 return This::STATUS_OKAY;
2492 Valtype addend = utils::sign_extend<26>(val << 2);
2493 Valtype branch_target = psymval->value(object, addend);
2494 int32_t branch_offset = branch_target - address;
2496 // We need a stub if the branch offset is too large or if we need
2498 bool may_use_blx = arm_target->may_use_blx();
2499 Reloc_stub* stub = NULL;
2500 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
2501 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2502 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
2504 Stub_type stub_type =
2505 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2507 if (stub_type != arm_stub_none)
2509 Stub_table<big_endian>* stub_table =
2510 object->stub_table(relinfo->data_shndx);
2511 gold_assert(stub_table != NULL);
2513 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2514 stub = stub_table->find_reloc_stub(stub_key);
2515 gold_assert(stub != NULL);
2516 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2517 branch_target = stub_table->address() + stub->offset() + addend;
2518 branch_offset = branch_target - address;
2519 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
2520 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
2524 // At this point, if we still need to switch mode, the instruction
2525 // must either be a BLX or a BL that can be converted to a BLX.
2529 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
2530 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
2533 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
2534 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2535 return (utils::has_overflow<26>(branch_offset)
2536 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
2539 // Relocate THUMB long branches. This handles relocation types
2540 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2541 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2542 // undefined and we do not use PLT in this relocation. In such a case,
2543 // the branch is converted into an NOP.
2545 template<bool big_endian>
2546 typename Arm_relocate_functions<big_endian>::Status
2547 Arm_relocate_functions<big_endian>::thumb_branch_common(
2548 unsigned int r_type,
2549 const Relocate_info<32, big_endian>* relinfo,
2550 unsigned char *view,
2551 const Sized_symbol<32>* gsym,
2552 const Arm_relobj<big_endian>* object,
2554 const Symbol_value<32>* psymval,
2555 Arm_address address,
2556 Arm_address thumb_bit,
2557 bool is_weakly_undefined_without_plt)
2559 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2560 Valtype* wv = reinterpret_cast<Valtype*>(view);
2561 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2562 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2564 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2566 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
2567 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
2569 // Check that the instruction is valid.
2570 if (r_type == elfcpp::R_ARM_THM_CALL)
2572 if (!is_bl_insn && !is_blx_insn)
2573 return This::STATUS_BAD_RELOC;
2575 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
2577 // This cannot be a BLX.
2579 return This::STATUS_BAD_RELOC;
2581 else if (r_type == elfcpp::R_ARM_THM_XPC22)
2583 // Check for Thumb to Thumb call.
2585 return This::STATUS_BAD_RELOC;
2588 gold_warning(_("%s: Thumb BLX instruction targets "
2589 "thumb function '%s'."),
2590 object->name().c_str(),
2591 (gsym ? gsym->name() : "(local)"));
2592 // Convert BLX to BL.
2593 lower_insn |= 0x1000U;
2599 // A branch to an undefined weak symbol is turned into a jump to
2600 // the next instruction unless a PLT entry will be created.
2601 // The jump to the next instruction is optimized as a NOP.W for
2602 // Thumb-2 enabled architectures.
2603 const Target_arm<big_endian>* arm_target =
2604 Target_arm<big_endian>::default_target();
2605 if (is_weakly_undefined_without_plt)
2607 if (arm_target->may_use_thumb2_nop())
2609 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
2610 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
2614 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
2615 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
2617 return This::STATUS_OKAY;
2620 // Fetch the addend. We use the Thumb-2 encoding (backwards compatible
2621 // with Thumb-1) involving the J1 and J2 bits.
2622 uint32_t s = (upper_insn & (1 << 10)) >> 10;
2623 uint32_t upper = upper_insn & 0x3ff;
2624 uint32_t lower = lower_insn & 0x7ff;
2625 uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
2626 uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
2627 uint32_t i1 = j1 ^ s ? 0 : 1;
2628 uint32_t i2 = j2 ^ s ? 0 : 1;
2630 int32_t addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
2632 addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
2634 Arm_address branch_target = psymval->value(object, addend);
2635 int32_t branch_offset = branch_target - address;
2637 // We need a stub if the branch offset is too large or if we need
2639 bool may_use_blx = arm_target->may_use_blx();
2640 bool thumb2 = arm_target->using_thumb2();
2642 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2643 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2645 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2646 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2647 || ((thumb_bit == 0)
2648 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2649 || r_type == elfcpp::R_ARM_THM_JUMP24)))
2651 Stub_type stub_type =
2652 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2654 if (stub_type != arm_stub_none)
2656 Stub_table<big_endian>* stub_table =
2657 object->stub_table(relinfo->data_shndx);
2658 gold_assert(stub_table != NULL);
2660 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2661 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
2662 gold_assert(stub != NULL);
2663 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2664 branch_target = stub_table->address() + stub->offset() + addend;
2665 branch_offset = branch_target - address;
2669 // At this point, if we still need to switch mode, the instruction
2670 // must either be a BLX or a BL that can be converted to a BLX.
2673 gold_assert(may_use_blx
2674 && (r_type == elfcpp::R_ARM_THM_CALL
2675 || r_type == elfcpp::R_ARM_THM_XPC22));
2676 // Make sure this is a BLX.
2677 lower_insn &= ~0x1000U;
2681 // Make sure this is a BL.
2682 lower_insn |= 0x1000U;
2685 uint32_t reloc_sign = (branch_offset < 0) ? 1 : 0;
2686 uint32_t relocation = static_cast<uint32_t>(branch_offset);
2688 if ((lower_insn & 0x5000U) == 0x4000U)
2689 // For a BLX instruction, make sure that the relocation is rounded up
2690 // to a word boundary. This follows the semantics of the instruction
2691 // which specifies that bit 1 of the target address will come from bit
2692 // 1 of the base address.
2693 relocation = (relocation + 2U) & ~3U;
2695 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2696 // We use the Thumb-2 encoding, which is safe even if dealing with
2697 // a Thumb-1 instruction by virtue of our overflow check above. */
2698 upper_insn = (upper_insn & ~0x7ffU)
2699 | ((relocation >> 12) & 0x3ffU)
2700 | (reloc_sign << 10);
2701 lower_insn = (lower_insn & ~0x2fffU)
2702 | (((!((relocation >> 23) & 1U)) ^ reloc_sign) << 13)
2703 | (((!((relocation >> 22) & 1U)) ^ reloc_sign) << 11)
2704 | ((relocation >> 1) & 0x7ffU);
2706 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
2707 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
2710 ? utils::has_overflow<25>(relocation)
2711 : utils::has_overflow<23>(relocation))
2712 ? This::STATUS_OVERFLOW
2713 : This::STATUS_OKAY);
2716 // Get the GOT section, creating it if necessary.
2718 template<bool big_endian>
2719 Output_data_got<32, big_endian>*
2720 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2722 if (this->got_ == NULL)
2724 gold_assert(symtab != NULL && layout != NULL);
2726 this->got_ = new Output_data_got<32, big_endian>();
2729 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2731 | elfcpp::SHF_WRITE),
2732 this->got_, false, true, true,
2735 // The old GNU linker creates a .got.plt section. We just
2736 // create another set of data in the .got section. Note that we
2737 // always create a PLT if we create a GOT, although the PLT
2739 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2740 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2742 | elfcpp::SHF_WRITE),
2743 this->got_plt_, false, false,
2746 // The first three entries are reserved.
2747 this->got_plt_->set_current_data_size(3 * 4);
2749 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2750 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2751 Symbol_table::PREDEFINED,
2753 0, 0, elfcpp::STT_OBJECT,
2755 elfcpp::STV_HIDDEN, 0,
2761 // Get the dynamic reloc section, creating it if necessary.
2763 template<bool big_endian>
2764 typename Target_arm<big_endian>::Reloc_section*
2765 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2767 if (this->rel_dyn_ == NULL)
2769 gold_assert(layout != NULL);
2770 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2771 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
2772 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
2773 false, false, false);
2775 return this->rel_dyn_;
2778 // Insn_template methods.
2780 // Return byte size of an instruction template.
2783 Insn_template::size() const
2785 switch (this->type())
2798 // Return alignment of an instruction template.
2801 Insn_template::alignment() const
2803 switch (this->type())
2816 // Stub_template methods.
2818 Stub_template::Stub_template(
2819 Stub_type type, const Insn_template* insns,
2821 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2822 entry_in_thumb_mode_(false), relocs_()
2826 // Compute byte size and alignment of stub template.
2827 for (size_t i = 0; i < insn_count; i++)
2829 unsigned insn_alignment = insns[i].alignment();
2830 size_t insn_size = insns[i].size();
2831 gold_assert((offset & (insn_alignment - 1)) == 0);
2832 this->alignment_ = std::max(this->alignment_, insn_alignment);
2833 switch (insns[i].type())
2835 case Insn_template::THUMB16_TYPE:
2837 this->entry_in_thumb_mode_ = true;
2840 case Insn_template::THUMB32_TYPE:
2841 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2842 this->relocs_.push_back(Reloc(i, offset));
2844 this->entry_in_thumb_mode_ = true;
2847 case Insn_template::ARM_TYPE:
2848 // Handle cases where the target is encoded within the
2850 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2851 this->relocs_.push_back(Reloc(i, offset));
2854 case Insn_template::DATA_TYPE:
2855 // Entry point cannot be data.
2856 gold_assert(i != 0);
2857 this->relocs_.push_back(Reloc(i, offset));
2863 offset += insn_size;
2865 this->size_ = offset;
2870 // Template to implement do_write for a specific target endianity.
2872 template<bool big_endian>
2874 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
2876 const Stub_template* stub_template = this->stub_template();
2877 const Insn_template* insns = stub_template->insns();
2879 // FIXME: We do not handle BE8 encoding yet.
2880 unsigned char* pov = view;
2881 for (size_t i = 0; i < stub_template->insn_count(); i++)
2883 switch (insns[i].type())
2885 case Insn_template::THUMB16_TYPE:
2886 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2888 case Insn_template::THUMB16_SPECIAL_TYPE:
2889 elfcpp::Swap<16, big_endian>::writeval(
2891 this->thumb16_special(i));
2893 case Insn_template::THUMB32_TYPE:
2895 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2896 uint32_t lo = insns[i].data() & 0xffff;
2897 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2898 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2901 case Insn_template::ARM_TYPE:
2902 case Insn_template::DATA_TYPE:
2903 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2908 pov += insns[i].size();
2910 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2913 // Reloc_stub::Key methods.
2915 // Dump a Key as a string for debugging.
2918 Reloc_stub::Key::name() const
2920 if (this->r_sym_ == invalid_index)
2922 // Global symbol key name
2923 // <stub-type>:<symbol name>:<addend>.
2924 const std::string sym_name = this->u_.symbol->name();
2925 // We need to print two hex number and two colons. So just add 100 bytes
2926 // to the symbol name size.
2927 size_t len = sym_name.size() + 100;
2928 char* buffer = new char[len];
2929 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2930 sym_name.c_str(), this->addend_);
2931 gold_assert(c > 0 && c < static_cast<int>(len));
2933 return std::string(buffer);
2937 // local symbol key name
2938 // <stub-type>:<object>:<r_sym>:<addend>.
2939 const size_t len = 200;
2941 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2942 this->u_.relobj, this->r_sym_, this->addend_);
2943 gold_assert(c > 0 && c < static_cast<int>(len));
2944 return std::string(buffer);
2948 // Reloc_stub methods.
2950 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2951 // LOCATION to DESTINATION.
2952 // This code is based on the arm_type_of_stub function in
2953 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2957 Reloc_stub::stub_type_for_reloc(
2958 unsigned int r_type,
2959 Arm_address location,
2960 Arm_address destination,
2961 bool target_is_thumb)
2963 Stub_type stub_type = arm_stub_none;
2965 // This is a bit ugly but we want to avoid using a templated class for
2966 // big and little endianities.
2968 bool should_force_pic_veneer;
2971 if (parameters->target().is_big_endian())
2973 const Target_arm<true>* big_endian_target =
2974 Target_arm<true>::default_target();
2975 may_use_blx = big_endian_target->may_use_blx();
2976 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
2977 thumb2 = big_endian_target->using_thumb2();
2978 thumb_only = big_endian_target->using_thumb_only();
2982 const Target_arm<false>* little_endian_target =
2983 Target_arm<false>::default_target();
2984 may_use_blx = little_endian_target->may_use_blx();
2985 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
2986 thumb2 = little_endian_target->using_thumb2();
2987 thumb_only = little_endian_target->using_thumb_only();
2990 int64_t branch_offset = (int64_t)destination - location;
2992 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2994 // Handle cases where:
2995 // - this call goes too far (different Thumb/Thumb2 max
2997 // - it's a Thumb->Arm call and blx is not available, or it's a
2998 // Thumb->Arm branch (not bl). A stub is needed in this case.
3000 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3001 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3003 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3004 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3005 || ((!target_is_thumb)
3006 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3007 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
3009 if (target_is_thumb)
3014 stub_type = (parameters->options().shared()
3015 || should_force_pic_veneer)
3018 && (r_type == elfcpp::R_ARM_THM_CALL))
3019 // V5T and above. Stub starts with ARM code, so
3020 // we must be able to switch mode before
3021 // reaching it, which is only possible for 'bl'
3022 // (ie R_ARM_THM_CALL relocation).
3023 ? arm_stub_long_branch_any_thumb_pic
3024 // On V4T, use Thumb code only.
3025 : arm_stub_long_branch_v4t_thumb_thumb_pic)
3029 && (r_type == elfcpp::R_ARM_THM_CALL))
3030 ? arm_stub_long_branch_any_any // V5T and above.
3031 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
3035 stub_type = (parameters->options().shared()
3036 || should_force_pic_veneer)
3037 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
3038 : arm_stub_long_branch_thumb_only; // non-PIC stub.
3045 // FIXME: We should check that the input section is from an
3046 // object that has interwork enabled.
3048 stub_type = (parameters->options().shared()
3049 || should_force_pic_veneer)
3052 && (r_type == elfcpp::R_ARM_THM_CALL))
3053 ? arm_stub_long_branch_any_arm_pic // V5T and above.
3054 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
3058 && (r_type == elfcpp::R_ARM_THM_CALL))
3059 ? arm_stub_long_branch_any_any // V5T and above.
3060 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
3062 // Handle v4t short branches.
3063 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3064 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3065 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3066 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3070 else if (r_type == elfcpp::R_ARM_CALL
3071 || r_type == elfcpp::R_ARM_JUMP24
3072 || r_type == elfcpp::R_ARM_PLT32)
3074 if (target_is_thumb)
3078 // FIXME: We should check that the input section is from an
3079 // object that has interwork enabled.
3081 // We have an extra 2-bytes reach because of
3082 // the mode change (bit 24 (H) of BLX encoding).
3083 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3084 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3085 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
3086 || (r_type == elfcpp::R_ARM_JUMP24)
3087 || (r_type == elfcpp::R_ARM_PLT32))
3089 stub_type = (parameters->options().shared()
3090 || should_force_pic_veneer)
3093 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
3094 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
3098 ? arm_stub_long_branch_any_any // V5T and above.
3099 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
3105 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3106 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3108 stub_type = (parameters->options().shared()
3109 || should_force_pic_veneer)
3110 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
3111 : arm_stub_long_branch_any_any; /// non-PIC.
3119 // Cortex_a8_stub methods.
3121 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3122 // I is the position of the instruction template in the stub template.
3125 Cortex_a8_stub::do_thumb16_special(size_t i)
3127 // The only use of this is to copy condition code from a conditional
3128 // branch being worked around to the corresponding conditional branch in
3130 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3132 uint16_t data = this->stub_template()->insns()[i].data();
3133 gold_assert((data & 0xff00U) == 0xd000U);
3134 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
3138 // Stub_factory methods.
3140 Stub_factory::Stub_factory()
3142 // The instruction template sequences are declared as static
3143 // objects and initialized first time the constructor runs.
3145 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3146 // to reach the stub if necessary.
3147 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
3149 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3150 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3151 // dcd R_ARM_ABS32(X)
3154 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3156 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
3158 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3159 Insn_template::arm_insn(0xe12fff1c), // bx ip
3160 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3161 // dcd R_ARM_ABS32(X)
3164 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3165 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
3167 Insn_template::thumb16_insn(0xb401), // push {r0}
3168 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3169 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3170 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3171 Insn_template::thumb16_insn(0x4760), // bx ip
3172 Insn_template::thumb16_insn(0xbf00), // nop
3173 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3174 // dcd R_ARM_ABS32(X)
3177 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3179 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
3181 Insn_template::thumb16_insn(0x4778), // bx pc
3182 Insn_template::thumb16_insn(0x46c0), // nop
3183 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3184 Insn_template::arm_insn(0xe12fff1c), // bx ip
3185 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3186 // dcd R_ARM_ABS32(X)
3189 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3191 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
3193 Insn_template::thumb16_insn(0x4778), // bx pc
3194 Insn_template::thumb16_insn(0x46c0), // nop
3195 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3196 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3197 // dcd R_ARM_ABS32(X)
3200 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3201 // one, when the destination is close enough.
3202 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
3204 Insn_template::thumb16_insn(0x4778), // bx pc
3205 Insn_template::thumb16_insn(0x46c0), // nop
3206 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3209 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3210 // blx to reach the stub if necessary.
3211 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
3213 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3214 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3215 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3216 // dcd R_ARM_REL32(X-4)
3219 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3220 // blx to reach the stub if necessary. We can not add into pc;
3221 // it is not guaranteed to mode switch (different in ARMv6 and
3223 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
3225 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3226 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3227 Insn_template::arm_insn(0xe12fff1c), // bx ip
3228 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3229 // dcd R_ARM_REL32(X)
3232 // V4T ARM -> ARM long branch stub, PIC.
3233 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
3235 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3236 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3237 Insn_template::arm_insn(0xe12fff1c), // bx ip
3238 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3239 // dcd R_ARM_REL32(X)
3242 // V4T Thumb -> ARM long branch stub, PIC.
3243 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
3245 Insn_template::thumb16_insn(0x4778), // bx pc
3246 Insn_template::thumb16_insn(0x46c0), // nop
3247 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3248 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3249 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3250 // dcd R_ARM_REL32(X)
3253 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3255 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
3257 Insn_template::thumb16_insn(0xb401), // push {r0}
3258 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3259 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3260 Insn_template::thumb16_insn(0x4484), // add ip, r0
3261 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3262 Insn_template::thumb16_insn(0x4760), // bx ip
3263 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
3264 // dcd R_ARM_REL32(X)
3267 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3269 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
3271 Insn_template::thumb16_insn(0x4778), // bx pc
3272 Insn_template::thumb16_insn(0x46c0), // nop
3273 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3274 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3275 Insn_template::arm_insn(0xe12fff1c), // bx ip
3276 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3277 // dcd R_ARM_REL32(X)
3280 // Cortex-A8 erratum-workaround stubs.
3282 // Stub used for conditional branches (which may be beyond +/-1MB away,
3283 // so we can't use a conditional branch to reach this stub).
3290 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
3292 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3293 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3294 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3298 // Stub used for b.w and bl.w instructions.
3300 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
3302 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3305 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
3307 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3310 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3311 // instruction (which switches to ARM mode) to point to this stub. Jump to
3312 // the real destination using an ARM-mode branch.
3313 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
3315 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3318 // Fill in the stub template look-up table. Stub templates are constructed
3319 // per instance of Stub_factory for fast look-up without locking
3320 // in a thread-enabled environment.
3322 this->stub_templates_[arm_stub_none] =
3323 new Stub_template(arm_stub_none, NULL, 0);
3325 #define DEF_STUB(x) \
3329 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3330 Stub_type type = arm_stub_##x; \
3331 this->stub_templates_[type] = \
3332 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3340 // Stub_table methods.
3342 // Add a STUB with using KEY. Caller is reponsible for avoid adding
3343 // if already a STUB with the same key has been added.
3345 template<bool big_endian>
3347 Stub_table<big_endian>::add_reloc_stub(
3349 const Reloc_stub::Key& key)
3351 const Stub_template* stub_template = stub->stub_template();
3352 gold_assert(stub_template->type() == key.stub_type());
3353 this->reloc_stubs_[key] = stub;
3354 if (this->addralign_ < stub_template->alignment())
3355 this->addralign_ = stub_template->alignment();
3356 this->has_been_changed_ = true;
3359 template<bool big_endian>
3361 Stub_table<big_endian>::relocate_stubs(
3362 const Relocate_info<32, big_endian>* relinfo,
3363 Target_arm<big_endian>* arm_target,
3364 Output_section* output_section,
3365 unsigned char* view,
3366 Arm_address address,
3367 section_size_type view_size)
3369 // If we are passed a view bigger than the stub table's. we need to
3371 gold_assert(address == this->address()
3373 == static_cast<section_size_type>(this->data_size())));
3375 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3376 p != this->reloc_stubs_.end();
3379 Reloc_stub* stub = p->second;
3380 const Stub_template* stub_template = stub->stub_template();
3381 if (stub_template->reloc_count() != 0)
3383 // Adjust view to cover the stub only.
3384 section_size_type offset = stub->offset();
3385 section_size_type stub_size = stub_template->size();
3386 gold_assert(offset + stub_size <= view_size);
3388 arm_target->relocate_stub(stub, relinfo, output_section,
3389 view + offset, address + offset,
3395 // Reset address and file offset.
3397 template<bool big_endian>
3399 Stub_table<big_endian>::do_reset_address_and_file_offset()
3402 uint64_t max_addralign = 1;
3403 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3404 p != this->reloc_stubs_.end();
3407 Reloc_stub* stub = p->second;
3408 const Stub_template* stub_template = stub->stub_template();
3409 uint64_t stub_addralign = stub_template->alignment();
3410 max_addralign = std::max(max_addralign, stub_addralign);
3411 off = align_address(off, stub_addralign);
3412 stub->set_offset(off);
3413 stub->reset_destination_address();
3414 off += stub_template->size();
3417 this->addralign_ = max_addralign;
3418 this->set_current_data_size_for_child(off);
3421 // Write out the stubs to file.
3423 template<bool big_endian>
3425 Stub_table<big_endian>::do_write(Output_file* of)
3427 off_t offset = this->offset();
3428 const section_size_type oview_size =
3429 convert_to_section_size_type(this->data_size());
3430 unsigned char* const oview = of->get_output_view(offset, oview_size);
3432 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3433 p != this->reloc_stubs_.end();
3436 Reloc_stub* stub = p->second;
3437 Arm_address address = this->address() + stub->offset();
3439 == align_address(address,
3440 stub->stub_template()->alignment()));
3441 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3444 of->write_output_view(this->offset(), oview_size, oview);
3447 // Arm_input_section methods.
3449 // Initialize an Arm_input_section.
3451 template<bool big_endian>
3453 Arm_input_section<big_endian>::init()
3455 Relobj* relobj = this->relobj();
3456 unsigned int shndx = this->shndx();
3458 // Cache these to speed up size and alignment queries. It is too slow
3459 // to call section_addraglin and section_size every time.
3460 this->original_addralign_ = relobj->section_addralign(shndx);
3461 this->original_size_ = relobj->section_size(shndx);
3463 // We want to make this look like the original input section after
3464 // output sections are finalized.
3465 Output_section* os = relobj->output_section(shndx);
3466 off_t offset = relobj->output_section_offset(shndx);
3467 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
3468 this->set_address(os->address() + offset);
3469 this->set_file_offset(os->offset() + offset);
3471 this->set_current_data_size(this->original_size_);
3472 this->finalize_data_size();
3475 template<bool big_endian>
3477 Arm_input_section<big_endian>::do_write(Output_file* of)
3479 // We have to write out the original section content.
3480 section_size_type section_size;
3481 const unsigned char* section_contents =
3482 this->relobj()->section_contents(this->shndx(), §ion_size, false);
3483 of->write(this->offset(), section_contents, section_size);
3485 // If this owns a stub table and it is not empty, write it.
3486 if (this->is_stub_table_owner() && !this->stub_table_->empty())
3487 this->stub_table_->write(of);
3490 // Finalize data size.
3492 template<bool big_endian>
3494 Arm_input_section<big_endian>::set_final_data_size()
3496 // If this owns a stub table, finalize its data size as well.
3497 if (this->is_stub_table_owner())
3499 uint64_t address = this->address();
3501 // The stub table comes after the original section contents.
3502 address += this->original_size_;
3503 address = align_address(address, this->stub_table_->addralign());
3504 off_t offset = this->offset() + (address - this->address());
3505 this->stub_table_->set_address_and_file_offset(address, offset);
3506 address += this->stub_table_->data_size();
3507 gold_assert(address == this->address() + this->current_data_size());
3510 this->set_data_size(this->current_data_size());
3513 // Reset address and file offset.
3515 template<bool big_endian>
3517 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
3519 // Size of the original input section contents.
3520 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
3522 // If this is a stub table owner, account for the stub table size.
3523 if (this->is_stub_table_owner())
3525 Stub_table<big_endian>* stub_table = this->stub_table_;
3527 // Reset the stub table's address and file offset. The
3528 // current data size for child will be updated after that.
3529 stub_table_->reset_address_and_file_offset();
3530 off = align_address(off, stub_table_->addralign());
3531 off += stub_table->current_data_size();
3534 this->set_current_data_size(off);
3537 // Arm_output_section methods.
3539 // Create a stub group for input sections from BEGIN to END. OWNER
3540 // points to the input section to be the owner a new stub table.
3542 template<bool big_endian>
3544 Arm_output_section<big_endian>::create_stub_group(
3545 Input_section_list::const_iterator begin,
3546 Input_section_list::const_iterator end,
3547 Input_section_list::const_iterator owner,
3548 Target_arm<big_endian>* target,
3549 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
3551 // Currently we convert ordinary input sections into relaxed sections only
3552 // at this point but we may want to support creating relaxed input section
3553 // very early. So we check here to see if owner is already a relaxed
3556 Arm_input_section<big_endian>* arm_input_section;
3557 if (owner->is_relaxed_input_section())
3560 Arm_input_section<big_endian>::as_arm_input_section(
3561 owner->relaxed_input_section());
3565 gold_assert(owner->is_input_section());
3566 // Create a new relaxed input section.
3568 target->new_arm_input_section(owner->relobj(), owner->shndx());
3569 new_relaxed_sections->push_back(arm_input_section);
3572 // Create a stub table.
3573 Stub_table<big_endian>* stub_table =
3574 target->new_stub_table(arm_input_section);
3576 arm_input_section->set_stub_table(stub_table);
3578 Input_section_list::const_iterator p = begin;
3579 Input_section_list::const_iterator prev_p;
3581 // Look for input sections or relaxed input sections in [begin ... end].
3584 if (p->is_input_section() || p->is_relaxed_input_section())
3586 // The stub table information for input sections live
3587 // in their objects.
3588 Arm_relobj<big_endian>* arm_relobj =
3589 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
3590 arm_relobj->set_stub_table(p->shndx(), stub_table);
3594 while (prev_p != end);
3597 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3598 // of stub groups. We grow a stub group by adding input section until the
3599 // size is just below GROUP_SIZE. The last input section will be converted
3600 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3601 // input section after the stub table, effectively double the group size.
3603 // This is similar to the group_sections() function in elf32-arm.c but is
3604 // implemented differently.
3606 template<bool big_endian>
3608 Arm_output_section<big_endian>::group_sections(
3609 section_size_type group_size,
3610 bool stubs_always_after_branch,
3611 Target_arm<big_endian>* target)
3613 // We only care about sections containing code.
3614 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
3617 // States for grouping.
3620 // No group is being built.
3622 // A group is being built but the stub table is not found yet.
3623 // We keep group a stub group until the size is just under GROUP_SIZE.
3624 // The last input section in the group will be used as the stub table.
3625 FINDING_STUB_SECTION,
3626 // A group is being built and we have already found a stub table.
3627 // We enter this state to grow a stub group by adding input section
3628 // after the stub table. This effectively doubles the group size.
3632 // Any newly created relaxed sections are stored here.
3633 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
3635 State state = NO_GROUP;
3636 section_size_type off = 0;
3637 section_size_type group_begin_offset = 0;
3638 section_size_type group_end_offset = 0;
3639 section_size_type stub_table_end_offset = 0;
3640 Input_section_list::const_iterator group_begin =
3641 this->input_sections().end();
3642 Input_section_list::const_iterator stub_table =
3643 this->input_sections().end();
3644 Input_section_list::const_iterator group_end = this->input_sections().end();
3645 for (Input_section_list::const_iterator p = this->input_sections().begin();
3646 p != this->input_sections().end();
3649 section_size_type section_begin_offset =
3650 align_address(off, p->addralign());
3651 section_size_type section_end_offset =
3652 section_begin_offset + p->data_size();
3654 // Check to see if we should group the previously seens sections.
3660 case FINDING_STUB_SECTION:
3661 // Adding this section makes the group larger than GROUP_SIZE.
3662 if (section_end_offset - group_begin_offset >= group_size)
3664 if (stubs_always_after_branch)
3666 gold_assert(group_end != this->input_sections().end());
3667 this->create_stub_group(group_begin, group_end, group_end,
3668 target, &new_relaxed_sections);
3673 // But wait, there's more! Input sections up to
3674 // stub_group_size bytes after the stub table can be
3675 // handled by it too.
3676 state = HAS_STUB_SECTION;
3677 stub_table = group_end;
3678 stub_table_end_offset = group_end_offset;
3683 case HAS_STUB_SECTION:
3684 // Adding this section makes the post stub-section group larger
3686 if (section_end_offset - stub_table_end_offset >= group_size)
3688 gold_assert(group_end != this->input_sections().end());
3689 this->create_stub_group(group_begin, group_end, stub_table,
3690 target, &new_relaxed_sections);
3699 // If we see an input section and currently there is no group, start
3700 // a new one. Skip any empty sections.
3701 if ((p->is_input_section() || p->is_relaxed_input_section())
3702 && (p->relobj()->section_size(p->shndx()) != 0))
3704 if (state == NO_GROUP)
3706 state = FINDING_STUB_SECTION;
3708 group_begin_offset = section_begin_offset;
3711 // Keep track of the last input section seen.
3713 group_end_offset = section_end_offset;
3716 off = section_end_offset;
3719 // Create a stub group for any ungrouped sections.
3720 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
3722 gold_assert(group_end != this->input_sections().end());
3723 this->create_stub_group(group_begin, group_end,
3724 (state == FINDING_STUB_SECTION
3727 target, &new_relaxed_sections);
3730 // Convert input section into relaxed input section in a batch.
3731 if (!new_relaxed_sections.empty())
3732 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
3734 // Update the section offsets
3735 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
3737 Arm_relobj<big_endian>* arm_relobj =
3738 Arm_relobj<big_endian>::as_arm_relobj(
3739 new_relaxed_sections[i]->relobj());
3740 unsigned int shndx = new_relaxed_sections[i]->shndx();
3741 // Tell Arm_relobj that this input section is converted.
3742 arm_relobj->convert_input_section_to_relaxed_section(shndx);
3746 // Arm_relobj methods.
3748 // Scan relocations for stub generation.
3750 template<bool big_endian>
3752 Arm_relobj<big_endian>::scan_sections_for_stubs(
3753 Target_arm<big_endian>* arm_target,
3754 const Symbol_table* symtab,
3755 const Layout* layout)
3757 unsigned int shnum = this->shnum();
3758 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
3760 // Read the section headers.
3761 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
3765 // To speed up processing, we set up hash tables for fast lookup of
3766 // input offsets to output addresses.
3767 this->initialize_input_to_output_maps();
3769 const Relobj::Output_sections& out_sections(this->output_sections());
3771 Relocate_info<32, big_endian> relinfo;
3772 relinfo.symtab = symtab;
3773 relinfo.layout = layout;
3774 relinfo.object = this;
3776 const unsigned char* p = pshdrs + shdr_size;
3777 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
3779 typename elfcpp::Shdr<32, big_endian> shdr(p);
3781 unsigned int sh_type = shdr.get_sh_type();
3782 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
3785 off_t sh_size = shdr.get_sh_size();
3789 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
3790 if (index >= this->shnum())
3792 // Ignore reloc section with bad info. This error will be
3793 // reported in the final link.
3797 Output_section* os = out_sections[index];
3799 || symtab->is_section_folded(this, index))
3801 // This relocation section is against a section which we
3802 // discarded or if the section is folded into another
3803 // section due to ICF.
3806 Arm_address output_offset = this->get_output_section_offset(index);
3808 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
3810 // Ignore reloc section with unexpected symbol table. The
3811 // error will be reported in the final link.
3815 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
3816 sh_size, true, false);
3818 unsigned int reloc_size;
3819 if (sh_type == elfcpp::SHT_REL)
3820 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
3822 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
3824 if (reloc_size != shdr.get_sh_entsize())
3826 // Ignore reloc section with unexpected entsize. The error
3827 // will be reported in the final link.
3831 size_t reloc_count = sh_size / reloc_size;
3832 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
3834 // Ignore reloc section with uneven size. The error will be
3835 // reported in the final link.
3839 gold_assert(output_offset != invalid_address
3840 || this->relocs_must_follow_section_writes());
3842 // Get the section contents. This does work for the case in which
3843 // we modify the contents of an input section. We need to pass the
3844 // output view under such circumstances.
3845 section_size_type input_view_size = 0;
3846 const unsigned char* input_view =
3847 this->section_contents(index, &input_view_size, false);
3849 relinfo.reloc_shndx = i;
3850 relinfo.data_shndx = index;
3851 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
3853 output_offset == invalid_address,
3859 // After we've done the relocations, we release the hash tables,
3860 // since we no longer need them.
3861 this->free_input_to_output_maps();
3864 // Count the local symbols. The ARM backend needs to know if a symbol
3865 // is a THUMB function or not. For global symbols, it is easy because
3866 // the Symbol object keeps the ELF symbol type. For local symbol it is
3867 // harder because we cannot access this information. So we override the
3868 // do_count_local_symbol in parent and scan local symbols to mark
3869 // THUMB functions. This is not the most efficient way but I do not want to
3870 // slow down other ports by calling a per symbol targer hook inside
3871 // Sized_relobj<size, big_endian>::do_count_local_symbols.
3873 template<bool big_endian>
3875 Arm_relobj<big_endian>::do_count_local_symbols(
3876 Stringpool_template<char>* pool,
3877 Stringpool_template<char>* dynpool)
3879 // We need to fix-up the values of any local symbols whose type are
3882 // Ask parent to count the local symbols.
3883 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
3884 const unsigned int loccount = this->local_symbol_count();
3888 // Intialize the thumb function bit-vector.
3889 std::vector<bool> empty_vector(loccount, false);
3890 this->local_symbol_is_thumb_function_.swap(empty_vector);
3892 // Read the symbol table section header.
3893 const unsigned int symtab_shndx = this->symtab_shndx();
3894 elfcpp::Shdr<32, big_endian>
3895 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
3896 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
3898 // Read the local symbols.
3899 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
3900 gold_assert(loccount == symtabshdr.get_sh_info());
3901 off_t locsize = loccount * sym_size;
3902 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
3903 locsize, true, true);
3905 // Loop over the local symbols and mark any local symbols pointing
3906 // to THUMB functions.
3908 // Skip the first dummy symbol.
3910 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
3911 this->local_values();
3912 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
3914 elfcpp::Sym<32, big_endian> sym(psyms);
3915 elfcpp::STT st_type = sym.get_st_type();
3916 Symbol_value<32>& lv((*plocal_values)[i]);
3917 Arm_address input_value = lv.input_value();
3919 if (st_type == elfcpp::STT_ARM_TFUNC
3920 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
3922 // This is a THUMB function. Mark this and canonicalize the
3923 // symbol value by setting LSB.
3924 this->local_symbol_is_thumb_function_[i] = true;
3925 if ((input_value & 1) == 0)
3926 lv.set_input_value(input_value | 1);
3931 // Relocate sections.
3932 template<bool big_endian>
3934 Arm_relobj<big_endian>::do_relocate_sections(
3935 const Symbol_table* symtab,
3936 const Layout* layout,
3937 const unsigned char* pshdrs,
3938 typename Sized_relobj<32, big_endian>::Views* pviews)
3940 // Call parent to relocate sections.
3941 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
3944 // We do not generate stubs if doing a relocatable link.
3945 if (parameters->options().relocatable())
3948 // Relocate stub tables.
3949 unsigned int shnum = this->shnum();
3951 Target_arm<big_endian>* arm_target =
3952 Target_arm<big_endian>::default_target();
3954 Relocate_info<32, big_endian> relinfo;
3955 relinfo.symtab = symtab;
3956 relinfo.layout = layout;
3957 relinfo.object = this;
3959 for (unsigned int i = 1; i < shnum; ++i)
3961 Arm_input_section<big_endian>* arm_input_section =
3962 arm_target->find_arm_input_section(this, i);
3964 if (arm_input_section == NULL
3965 || !arm_input_section->is_stub_table_owner()
3966 || arm_input_section->stub_table()->empty())
3969 // We cannot discard a section if it owns a stub table.
3970 Output_section* os = this->output_section(i);
3971 gold_assert(os != NULL);
3973 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
3974 relinfo.reloc_shdr = NULL;
3975 relinfo.data_shndx = i;
3976 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
3978 gold_assert((*pviews)[i].view != NULL);
3980 // We are passed the output section view. Adjust it to cover the
3982 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
3983 gold_assert((stub_table->address() >= (*pviews)[i].address)
3984 && ((stub_table->address() + stub_table->data_size())
3985 <= (*pviews)[i].address + (*pviews)[i].view_size));
3987 off_t offset = stub_table->address() - (*pviews)[i].address;
3988 unsigned char* view = (*pviews)[i].view + offset;
3989 Arm_address address = stub_table->address();
3990 section_size_type view_size = stub_table->data_size();
3992 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
3997 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4000 template<bool big_endian>
4001 Attributes_section_data*
4002 read_arm_attributes_section(
4004 Read_symbols_data *sd)
4006 // Read the attributes section if there is one.
4007 // We read from the end because gas seems to put it near the end of
4008 // the section headers.
4009 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4010 const unsigned char *ps =
4011 sd->section_headers->data() + shdr_size * (object->shnum() - 1);
4012 for (unsigned int i = object->shnum(); i > 0; --i, ps -= shdr_size)
4014 elfcpp::Shdr<32, big_endian> shdr(ps);
4015 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
4017 section_offset_type section_offset = shdr.get_sh_offset();
4018 section_size_type section_size =
4019 convert_to_section_size_type(shdr.get_sh_size());
4020 File_view* view = object->get_lasting_view(section_offset,
4021 section_size, true, false);
4022 return new Attributes_section_data(view->data(), section_size);
4028 // Read the symbol information.
4030 template<bool big_endian>
4032 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4034 // Call parent class to read symbol information.
4035 Sized_relobj<32, big_endian>::do_read_symbols(sd);
4037 // Read processor-specific flags in ELF file header.
4038 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4039 elfcpp::Elf_sizes<32>::ehdr_size,
4041 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4042 this->processor_specific_flags_ = ehdr.get_e_flags();
4043 this->attributes_section_data_ =
4044 read_arm_attributes_section<big_endian>(this, sd);
4047 // Arm_dynobj methods.
4049 // Read the symbol information.
4051 template<bool big_endian>
4053 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4055 // Call parent class to read symbol information.
4056 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
4058 // Read processor-specific flags in ELF file header.
4059 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4060 elfcpp::Elf_sizes<32>::ehdr_size,
4062 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4063 this->processor_specific_flags_ = ehdr.get_e_flags();
4064 this->attributes_section_data_ =
4065 read_arm_attributes_section<big_endian>(this, sd);
4068 // Stub_addend_reader methods.
4070 // Read the addend of a REL relocation of type R_TYPE at VIEW.
4072 template<bool big_endian>
4073 elfcpp::Elf_types<32>::Elf_Swxword
4074 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
4075 unsigned int r_type,
4076 const unsigned char* view,
4077 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
4081 case elfcpp::R_ARM_CALL:
4082 case elfcpp::R_ARM_JUMP24:
4083 case elfcpp::R_ARM_PLT32:
4085 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4086 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4087 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4088 return utils::sign_extend<26>(val << 2);
4091 case elfcpp::R_ARM_THM_CALL:
4092 case elfcpp::R_ARM_THM_JUMP24:
4093 case elfcpp::R_ARM_THM_XPC22:
4095 // Fetch the addend. We use the Thumb-2 encoding (backwards
4096 // compatible with Thumb-1) involving the J1 and J2 bits.
4097 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4098 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4099 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4100 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4102 uint32_t s = (upper_insn & (1 << 10)) >> 10;
4103 uint32_t upper = upper_insn & 0x3ff;
4104 uint32_t lower = lower_insn & 0x7ff;
4105 uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
4106 uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
4107 uint32_t i1 = j1 ^ s ? 0 : 1;
4108 uint32_t i2 = j2 ^ s ? 0 : 1;
4110 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
4111 | (upper << 12) | (lower << 1));
4114 case elfcpp::R_ARM_THM_JUMP19:
4116 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4117 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4118 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4119 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4121 // Reconstruct the top three bits and squish the two 11 bit pieces
4123 uint32_t S = (upper_insn & 0x0400) >> 10;
4124 uint32_t J1 = (lower_insn & 0x2000) >> 13;
4125 uint32_t J2 = (lower_insn & 0x0800) >> 11;
4127 (S << 8) | (J2 << 7) | (J1 << 6) | (upper_insn & 0x003f);
4128 uint32_t lower = (lower_insn & 0x07ff);
4129 return utils::sign_extend<23>((upper << 12) | (lower << 1));
4137 // A class to handle the PLT data.
4139 template<bool big_endian>
4140 class Output_data_plt_arm : public Output_section_data
4143 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
4146 Output_data_plt_arm(Layout*, Output_data_space*);
4148 // Add an entry to the PLT.
4150 add_entry(Symbol* gsym);
4152 // Return the .rel.plt section data.
4153 const Reloc_section*
4155 { return this->rel_; }
4159 do_adjust_output_section(Output_section* os);
4161 // Write to a map file.
4163 do_print_to_mapfile(Mapfile* mapfile) const
4164 { mapfile->print_output_data(this, _("** PLT")); }
4167 // Template for the first PLT entry.
4168 static const uint32_t first_plt_entry[5];
4170 // Template for subsequent PLT entries.
4171 static const uint32_t plt_entry[3];
4173 // Set the final size.
4175 set_final_data_size()
4177 this->set_data_size(sizeof(first_plt_entry)
4178 + this->count_ * sizeof(plt_entry));
4181 // Write out the PLT data.
4183 do_write(Output_file*);
4185 // The reloc section.
4186 Reloc_section* rel_;
4187 // The .got.plt section.
4188 Output_data_space* got_plt_;
4189 // The number of PLT entries.
4190 unsigned int count_;
4193 // Create the PLT section. The ordinary .got section is an argument,
4194 // since we need to refer to the start. We also create our own .got
4195 // section just for PLT entries.
4197 template<bool big_endian>
4198 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
4199 Output_data_space* got_plt)
4200 : Output_section_data(4), got_plt_(got_plt), count_(0)
4202 this->rel_ = new Reloc_section(false);
4203 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
4204 elfcpp::SHF_ALLOC, this->rel_, true, false,
4208 template<bool big_endian>
4210 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
4215 // Add an entry to the PLT.
4217 template<bool big_endian>
4219 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
4221 gold_assert(!gsym->has_plt_offset());
4223 // Note that when setting the PLT offset we skip the initial
4224 // reserved PLT entry.
4225 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
4226 + sizeof(first_plt_entry));
4230 section_offset_type got_offset = this->got_plt_->current_data_size();
4232 // Every PLT entry needs a GOT entry which points back to the PLT
4233 // entry (this will be changed by the dynamic linker, normally
4234 // lazily when the function is called).
4235 this->got_plt_->set_current_data_size(got_offset + 4);
4237 // Every PLT entry needs a reloc.
4238 gsym->set_needs_dynsym_entry();
4239 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
4242 // Note that we don't need to save the symbol. The contents of the
4243 // PLT are independent of which symbols are used. The symbols only
4244 // appear in the relocations.
4248 // FIXME: This is not very flexible. Right now this has only been tested
4249 // on armv5te. If we are to support additional architecture features like
4250 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4252 // The first entry in the PLT.
4253 template<bool big_endian>
4254 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
4256 0xe52de004, // str lr, [sp, #-4]!
4257 0xe59fe004, // ldr lr, [pc, #4]
4258 0xe08fe00e, // add lr, pc, lr
4259 0xe5bef008, // ldr pc, [lr, #8]!
4260 0x00000000, // &GOT[0] - .
4263 // Subsequent entries in the PLT.
4265 template<bool big_endian>
4266 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
4268 0xe28fc600, // add ip, pc, #0xNN00000
4269 0xe28cca00, // add ip, ip, #0xNN000
4270 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4273 // Write out the PLT. This uses the hand-coded instructions above,
4274 // and adjusts them as needed. This is all specified by the arm ELF
4275 // Processor Supplement.
4277 template<bool big_endian>
4279 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
4281 const off_t offset = this->offset();
4282 const section_size_type oview_size =
4283 convert_to_section_size_type(this->data_size());
4284 unsigned char* const oview = of->get_output_view(offset, oview_size);
4286 const off_t got_file_offset = this->got_plt_->offset();
4287 const section_size_type got_size =
4288 convert_to_section_size_type(this->got_plt_->data_size());
4289 unsigned char* const got_view = of->get_output_view(got_file_offset,
4291 unsigned char* pov = oview;
4293 Arm_address plt_address = this->address();
4294 Arm_address got_address = this->got_plt_->address();
4296 // Write first PLT entry. All but the last word are constants.
4297 const size_t num_first_plt_words = (sizeof(first_plt_entry)
4298 / sizeof(plt_entry[0]));
4299 for (size_t i = 0; i < num_first_plt_words - 1; i++)
4300 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
4301 // Last word in first PLT entry is &GOT[0] - .
4302 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
4303 got_address - (plt_address + 16));
4304 pov += sizeof(first_plt_entry);
4306 unsigned char* got_pov = got_view;
4308 memset(got_pov, 0, 12);
4311 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
4312 unsigned int plt_offset = sizeof(first_plt_entry);
4313 unsigned int plt_rel_offset = 0;
4314 unsigned int got_offset = 12;
4315 const unsigned int count = this->count_;
4316 for (unsigned int i = 0;
4319 pov += sizeof(plt_entry),
4321 plt_offset += sizeof(plt_entry),
4322 plt_rel_offset += rel_size,
4325 // Set and adjust the PLT entry itself.
4326 int32_t offset = ((got_address + got_offset)
4327 - (plt_address + plt_offset + 8));
4329 gold_assert(offset >= 0 && offset < 0x0fffffff);
4330 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
4331 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
4332 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
4333 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
4334 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
4335 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
4337 // Set the entry in the GOT.
4338 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
4341 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4342 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4344 of->write_output_view(offset, oview_size, oview);
4345 of->write_output_view(got_file_offset, got_size, got_view);
4348 // Create a PLT entry for a global symbol.
4350 template<bool big_endian>
4352 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
4355 if (gsym->has_plt_offset())
4358 if (this->plt_ == NULL)
4360 // Create the GOT sections first.
4361 this->got_section(symtab, layout);
4363 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
4364 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
4366 | elfcpp::SHF_EXECINSTR),
4367 this->plt_, false, false, false, false);
4369 this->plt_->add_entry(gsym);
4372 // Report an unsupported relocation against a local symbol.
4374 template<bool big_endian>
4376 Target_arm<big_endian>::Scan::unsupported_reloc_local(
4377 Sized_relobj<32, big_endian>* object,
4378 unsigned int r_type)
4380 gold_error(_("%s: unsupported reloc %u against local symbol"),
4381 object->name().c_str(), r_type);
4384 // We are about to emit a dynamic relocation of type R_TYPE. If the
4385 // dynamic linker does not support it, issue an error. The GNU linker
4386 // only issues a non-PIC error for an allocated read-only section.
4387 // Here we know the section is allocated, but we don't know that it is
4388 // read-only. But we check for all the relocation types which the
4389 // glibc dynamic linker supports, so it seems appropriate to issue an
4390 // error even if the section is not read-only.
4392 template<bool big_endian>
4394 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
4395 unsigned int r_type)
4399 // These are the relocation types supported by glibc for ARM.
4400 case elfcpp::R_ARM_RELATIVE:
4401 case elfcpp::R_ARM_COPY:
4402 case elfcpp::R_ARM_GLOB_DAT:
4403 case elfcpp::R_ARM_JUMP_SLOT:
4404 case elfcpp::R_ARM_ABS32:
4405 case elfcpp::R_ARM_ABS32_NOI:
4406 case elfcpp::R_ARM_PC24:
4407 // FIXME: The following 3 types are not supported by Android's dynamic
4409 case elfcpp::R_ARM_TLS_DTPMOD32:
4410 case elfcpp::R_ARM_TLS_DTPOFF32:
4411 case elfcpp::R_ARM_TLS_TPOFF32:
4415 // This prevents us from issuing more than one error per reloc
4416 // section. But we can still wind up issuing more than one
4417 // error per object file.
4418 if (this->issued_non_pic_error_)
4420 object->error(_("requires unsupported dynamic reloc; "
4421 "recompile with -fPIC"));
4422 this->issued_non_pic_error_ = true;
4425 case elfcpp::R_ARM_NONE:
4430 // Scan a relocation for a local symbol.
4431 // FIXME: This only handles a subset of relocation types used by Android
4432 // on ARM v5te devices.
4434 template<bool big_endian>
4436 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
4439 Sized_relobj<32, big_endian>* object,
4440 unsigned int data_shndx,
4441 Output_section* output_section,
4442 const elfcpp::Rel<32, big_endian>& reloc,
4443 unsigned int r_type,
4444 const elfcpp::Sym<32, big_endian>&)
4446 r_type = get_real_reloc_type(r_type);
4449 case elfcpp::R_ARM_NONE:
4452 case elfcpp::R_ARM_ABS32:
4453 case elfcpp::R_ARM_ABS32_NOI:
4454 // If building a shared library (or a position-independent
4455 // executable), we need to create a dynamic relocation for
4456 // this location. The relocation applied at link time will
4457 // apply the link-time value, so we flag the location with
4458 // an R_ARM_RELATIVE relocation so the dynamic loader can
4459 // relocate it easily.
4460 if (parameters->options().output_is_position_independent())
4462 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4463 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4464 // If we are to add more other reloc types than R_ARM_ABS32,
4465 // we need to add check_non_pic(object, r_type) here.
4466 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
4467 output_section, data_shndx,
4468 reloc.get_r_offset());
4472 case elfcpp::R_ARM_REL32:
4473 case elfcpp::R_ARM_THM_CALL:
4474 case elfcpp::R_ARM_CALL:
4475 case elfcpp::R_ARM_PREL31:
4476 case elfcpp::R_ARM_JUMP24:
4477 case elfcpp::R_ARM_PLT32:
4478 case elfcpp::R_ARM_THM_ABS5:
4479 case elfcpp::R_ARM_ABS8:
4480 case elfcpp::R_ARM_ABS12:
4481 case elfcpp::R_ARM_ABS16:
4482 case elfcpp::R_ARM_BASE_ABS:
4483 case elfcpp::R_ARM_MOVW_ABS_NC:
4484 case elfcpp::R_ARM_MOVT_ABS:
4485 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4486 case elfcpp::R_ARM_THM_MOVT_ABS:
4487 case elfcpp::R_ARM_MOVW_PREL_NC:
4488 case elfcpp::R_ARM_MOVT_PREL:
4489 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4490 case elfcpp::R_ARM_THM_MOVT_PREL:
4493 case elfcpp::R_ARM_GOTOFF32:
4494 // We need a GOT section:
4495 target->got_section(symtab, layout);
4498 case elfcpp::R_ARM_BASE_PREL:
4499 // FIXME: What about this?
4502 case elfcpp::R_ARM_GOT_BREL:
4503 case elfcpp::R_ARM_GOT_PREL:
4505 // The symbol requires a GOT entry.
4506 Output_data_got<32, big_endian>* got =
4507 target->got_section(symtab, layout);
4508 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4509 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
4511 // If we are generating a shared object, we need to add a
4512 // dynamic RELATIVE relocation for this symbol's GOT entry.
4513 if (parameters->options().output_is_position_independent())
4515 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4516 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4517 rel_dyn->add_local_relative(
4518 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
4519 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
4525 case elfcpp::R_ARM_TARGET1:
4526 // This should have been mapped to another type already.
4528 case elfcpp::R_ARM_COPY:
4529 case elfcpp::R_ARM_GLOB_DAT:
4530 case elfcpp::R_ARM_JUMP_SLOT:
4531 case elfcpp::R_ARM_RELATIVE:
4532 // These are relocations which should only be seen by the
4533 // dynamic linker, and should never be seen here.
4534 gold_error(_("%s: unexpected reloc %u in object file"),
4535 object->name().c_str(), r_type);
4539 unsupported_reloc_local(object, r_type);
4544 // Report an unsupported relocation against a global symbol.
4546 template<bool big_endian>
4548 Target_arm<big_endian>::Scan::unsupported_reloc_global(
4549 Sized_relobj<32, big_endian>* object,
4550 unsigned int r_type,
4553 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4554 object->name().c_str(), r_type, gsym->demangled_name().c_str());
4557 // Scan a relocation for a global symbol.
4558 // FIXME: This only handles a subset of relocation types used by Android
4559 // on ARM v5te devices.
4561 template<bool big_endian>
4563 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
4566 Sized_relobj<32, big_endian>* object,
4567 unsigned int data_shndx,
4568 Output_section* output_section,
4569 const elfcpp::Rel<32, big_endian>& reloc,
4570 unsigned int r_type,
4573 r_type = get_real_reloc_type(r_type);
4576 case elfcpp::R_ARM_NONE:
4579 case elfcpp::R_ARM_ABS32:
4580 case elfcpp::R_ARM_ABS32_NOI:
4582 // Make a dynamic relocation if necessary.
4583 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
4585 if (target->may_need_copy_reloc(gsym))
4587 target->copy_reloc(symtab, layout, object,
4588 data_shndx, output_section, gsym, reloc);
4590 else if (gsym->can_use_relative_reloc(false))
4592 // If we are to add more other reloc types than R_ARM_ABS32,
4593 // we need to add check_non_pic(object, r_type) here.
4594 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4595 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
4596 output_section, object,
4597 data_shndx, reloc.get_r_offset());
4601 // If we are to add more other reloc types than R_ARM_ABS32,
4602 // we need to add check_non_pic(object, r_type) here.
4603 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4604 rel_dyn->add_global(gsym, r_type, output_section, object,
4605 data_shndx, reloc.get_r_offset());
4611 case elfcpp::R_ARM_MOVW_ABS_NC:
4612 case elfcpp::R_ARM_MOVT_ABS:
4613 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4614 case elfcpp::R_ARM_THM_MOVT_ABS:
4615 case elfcpp::R_ARM_MOVW_PREL_NC:
4616 case elfcpp::R_ARM_MOVT_PREL:
4617 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4618 case elfcpp::R_ARM_THM_MOVT_PREL:
4621 case elfcpp::R_ARM_THM_ABS5:
4622 case elfcpp::R_ARM_ABS8:
4623 case elfcpp::R_ARM_ABS12:
4624 case elfcpp::R_ARM_ABS16:
4625 case elfcpp::R_ARM_BASE_ABS:
4627 // No dynamic relocs of this kinds.
4628 // Report the error in case of PIC.
4629 int flags = Symbol::NON_PIC_REF;
4630 if (gsym->type() == elfcpp::STT_FUNC
4631 || gsym->type() == elfcpp::STT_ARM_TFUNC)
4632 flags |= Symbol::FUNCTION_CALL;
4633 if (gsym->needs_dynamic_reloc(flags))
4634 check_non_pic(object, r_type);
4638 case elfcpp::R_ARM_REL32:
4639 case elfcpp::R_ARM_PREL31:
4641 // Make a dynamic relocation if necessary.
4642 int flags = Symbol::NON_PIC_REF;
4643 if (gsym->needs_dynamic_reloc(flags))
4645 if (target->may_need_copy_reloc(gsym))
4647 target->copy_reloc(symtab, layout, object,
4648 data_shndx, output_section, gsym, reloc);
4652 check_non_pic(object, r_type);
4653 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4654 rel_dyn->add_global(gsym, r_type, output_section, object,
4655 data_shndx, reloc.get_r_offset());
4661 case elfcpp::R_ARM_JUMP24:
4662 case elfcpp::R_ARM_THM_JUMP24:
4663 case elfcpp::R_ARM_CALL:
4664 case elfcpp::R_ARM_THM_CALL:
4666 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
4667 target->make_plt_entry(symtab, layout, gsym);
4670 // Check to see if this is a function that would need a PLT
4671 // but does not get one because the function symbol is untyped.
4672 // This happens in assembly code missing a proper .type directive.
4673 if ((!gsym->is_undefined() || parameters->options().shared())
4674 && !parameters->doing_static_link()
4675 && gsym->type() == elfcpp::STT_NOTYPE
4676 && (gsym->is_from_dynobj()
4677 || gsym->is_undefined()
4678 || gsym->is_preemptible()))
4679 gold_error(_("%s is not a function."),
4680 gsym->demangled_name().c_str());
4684 case elfcpp::R_ARM_PLT32:
4685 // If the symbol is fully resolved, this is just a relative
4686 // local reloc. Otherwise we need a PLT entry.
4687 if (gsym->final_value_is_known())
4689 // If building a shared library, we can also skip the PLT entry
4690 // if the symbol is defined in the output file and is protected
4692 if (gsym->is_defined()
4693 && !gsym->is_from_dynobj()
4694 && !gsym->is_preemptible())
4696 target->make_plt_entry(symtab, layout, gsym);
4699 case elfcpp::R_ARM_GOTOFF32:
4700 // We need a GOT section.
4701 target->got_section(symtab, layout);
4704 case elfcpp::R_ARM_BASE_PREL:
4705 // FIXME: What about this?
4708 case elfcpp::R_ARM_GOT_BREL:
4709 case elfcpp::R_ARM_GOT_PREL:
4711 // The symbol requires a GOT entry.
4712 Output_data_got<32, big_endian>* got =
4713 target->got_section(symtab, layout);
4714 if (gsym->final_value_is_known())
4715 got->add_global(gsym, GOT_TYPE_STANDARD);
4718 // If this symbol is not fully resolved, we need to add a
4719 // GOT entry with a dynamic relocation.
4720 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4721 if (gsym->is_from_dynobj()
4722 || gsym->is_undefined()
4723 || gsym->is_preemptible())
4724 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
4725 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
4728 if (got->add_global(gsym, GOT_TYPE_STANDARD))
4729 rel_dyn->add_global_relative(
4730 gsym, elfcpp::R_ARM_RELATIVE, got,
4731 gsym->got_offset(GOT_TYPE_STANDARD));
4737 case elfcpp::R_ARM_TARGET1:
4738 // This should have been mapped to another type already.
4740 case elfcpp::R_ARM_COPY:
4741 case elfcpp::R_ARM_GLOB_DAT:
4742 case elfcpp::R_ARM_JUMP_SLOT:
4743 case elfcpp::R_ARM_RELATIVE:
4744 // These are relocations which should only be seen by the
4745 // dynamic linker, and should never be seen here.
4746 gold_error(_("%s: unexpected reloc %u in object file"),
4747 object->name().c_str(), r_type);
4751 unsupported_reloc_global(object, r_type, gsym);
4756 // Process relocations for gc.
4758 template<bool big_endian>
4760 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
4762 Sized_relobj<32, big_endian>* object,
4763 unsigned int data_shndx,
4765 const unsigned char* prelocs,
4767 Output_section* output_section,
4768 bool needs_special_offset_handling,
4769 size_t local_symbol_count,
4770 const unsigned char* plocal_symbols)
4772 typedef Target_arm<big_endian> Arm;
4773 typedef typename Target_arm<big_endian>::Scan Scan;
4775 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4784 needs_special_offset_handling,
4789 // Scan relocations for a section.
4791 template<bool big_endian>
4793 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
4795 Sized_relobj<32, big_endian>* object,
4796 unsigned int data_shndx,
4797 unsigned int sh_type,
4798 const unsigned char* prelocs,
4800 Output_section* output_section,
4801 bool needs_special_offset_handling,
4802 size_t local_symbol_count,
4803 const unsigned char* plocal_symbols)
4805 typedef typename Target_arm<big_endian>::Scan Scan;
4806 if (sh_type == elfcpp::SHT_RELA)
4808 gold_error(_("%s: unsupported RELA reloc section"),
4809 object->name().c_str());
4813 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4822 needs_special_offset_handling,
4827 // Finalize the sections.
4829 template<bool big_endian>
4831 Target_arm<big_endian>::do_finalize_sections(
4833 const Input_objects* input_objects,
4834 Symbol_table* symtab)
4836 // Merge processor-specific flags.
4837 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4838 p != input_objects->relobj_end();
4841 Arm_relobj<big_endian>* arm_relobj =
4842 Arm_relobj<big_endian>::as_arm_relobj(*p);
4843 this->merge_processor_specific_flags(
4845 arm_relobj->processor_specific_flags());
4846 this->merge_object_attributes(arm_relobj->name().c_str(),
4847 arm_relobj->attributes_section_data());
4851 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
4852 p != input_objects->dynobj_end();
4855 Arm_dynobj<big_endian>* arm_dynobj =
4856 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
4857 this->merge_processor_specific_flags(
4859 arm_dynobj->processor_specific_flags());
4860 this->merge_object_attributes(arm_dynobj->name().c_str(),
4861 arm_dynobj->attributes_section_data());
4865 Object_attribute* attr =
4866 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
4867 if (attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
4868 this->set_may_use_blx(true);
4870 // Fill in some more dynamic tags.
4871 Output_data_dynamic* const odyn = layout->dynamic_data();
4874 if (this->got_plt_ != NULL
4875 && this->got_plt_->output_section() != NULL)
4876 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
4878 if (this->plt_ != NULL
4879 && this->plt_->output_section() != NULL)
4881 const Output_data* od = this->plt_->rel_plt();
4882 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
4883 odyn->add_section_address(elfcpp::DT_JMPREL, od);
4884 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
4887 if (this->rel_dyn_ != NULL
4888 && this->rel_dyn_->output_section() != NULL)
4890 const Output_data* od = this->rel_dyn_;
4891 odyn->add_section_address(elfcpp::DT_REL, od);
4892 odyn->add_section_size(elfcpp::DT_RELSZ, od);
4893 odyn->add_constant(elfcpp::DT_RELENT,
4894 elfcpp::Elf_sizes<32>::rel_size);
4897 if (!parameters->options().shared())
4899 // The value of the DT_DEBUG tag is filled in by the dynamic
4900 // linker at run time, and used by the debugger.
4901 odyn->add_constant(elfcpp::DT_DEBUG, 0);
4905 // Emit any relocs we saved in an attempt to avoid generating COPY
4907 if (this->copy_relocs_.any_saved_relocs())
4908 this->copy_relocs_.emit(this->rel_dyn_section(layout));
4910 // Handle the .ARM.exidx section.
4911 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
4912 if (exidx_section != NULL
4913 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
4914 && !parameters->options().relocatable())
4916 // Create __exidx_start and __exdix_end symbols.
4917 symtab->define_in_output_data("__exidx_start", NULL,
4918 Symbol_table::PREDEFINED,
4919 exidx_section, 0, 0, elfcpp::STT_OBJECT,
4920 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
4922 symtab->define_in_output_data("__exidx_end", NULL,
4923 Symbol_table::PREDEFINED,
4924 exidx_section, 0, 0, elfcpp::STT_OBJECT,
4925 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
4928 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4929 // the .ARM.exidx section.
4930 if (!layout->script_options()->saw_phdrs_clause())
4932 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
4934 Output_segment* exidx_segment =
4935 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
4936 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
4941 // Create an .ARM.attributes section if there is not one already.
4942 Output_attributes_section_data* attributes_section =
4943 new Output_attributes_section_data(*this->attributes_section_data_);
4944 layout->add_output_section_data(".ARM.attributes",
4945 elfcpp::SHT_ARM_ATTRIBUTES, 0,
4946 attributes_section, false, false, false,
4950 // Return whether a direct absolute static relocation needs to be applied.
4951 // In cases where Scan::local() or Scan::global() has created
4952 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4953 // of the relocation is carried in the data, and we must not
4954 // apply the static relocation.
4956 template<bool big_endian>
4958 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
4959 const Sized_symbol<32>* gsym,
4962 Output_section* output_section)
4964 // If the output section is not allocated, then we didn't call
4965 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4967 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
4970 // For local symbols, we will have created a non-RELATIVE dynamic
4971 // relocation only if (a) the output is position independent,
4972 // (b) the relocation is absolute (not pc- or segment-relative), and
4973 // (c) the relocation is not 32 bits wide.
4975 return !(parameters->options().output_is_position_independent()
4976 && (ref_flags & Symbol::ABSOLUTE_REF)
4979 // For global symbols, we use the same helper routines used in the
4980 // scan pass. If we did not create a dynamic relocation, or if we
4981 // created a RELATIVE dynamic relocation, we should apply the static
4983 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
4984 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
4985 && gsym->can_use_relative_reloc(ref_flags
4986 & Symbol::FUNCTION_CALL);
4987 return !has_dyn || is_rel;
4990 // Perform a relocation.
4992 template<bool big_endian>
4994 Target_arm<big_endian>::Relocate::relocate(
4995 const Relocate_info<32, big_endian>* relinfo,
4997 Output_section *output_section,
4999 const elfcpp::Rel<32, big_endian>& rel,
5000 unsigned int r_type,
5001 const Sized_symbol<32>* gsym,
5002 const Symbol_value<32>* psymval,
5003 unsigned char* view,
5004 Arm_address address,
5005 section_size_type /* view_size */ )
5007 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
5009 r_type = get_real_reloc_type(r_type);
5011 const Arm_relobj<big_endian>* object =
5012 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
5014 // If the final branch target of a relocation is THUMB instruction, this
5015 // is 1. Otherwise it is 0.
5016 Arm_address thumb_bit = 0;
5017 Symbol_value<32> symval;
5018 bool is_weakly_undefined_without_plt = false;
5019 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
5023 // This is a global symbol. Determine if we use PLT and if the
5024 // final target is THUMB.
5025 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
5027 // This uses a PLT, change the symbol value.
5028 symval.set_output_value(target->plt_section()->address()
5029 + gsym->plt_offset());
5032 else if (gsym->is_weak_undefined())
5034 // This is a weakly undefined symbol and we do not use PLT
5035 // for this relocation. A branch targeting this symbol will
5036 // be converted into an NOP.
5037 is_weakly_undefined_without_plt = true;
5041 // Set thumb bit if symbol:
5042 // -Has type STT_ARM_TFUNC or
5043 // -Has type STT_FUNC, is defined and with LSB in value set.
5045 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
5046 || (gsym->type() == elfcpp::STT_FUNC
5047 && !gsym->is_undefined()
5048 && ((psymval->value(object, 0) & 1) != 0)))
5055 // This is a local symbol. Determine if the final target is THUMB.
5056 // We saved this information when all the local symbols were read.
5057 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
5058 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
5059 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
5064 // This is a fake relocation synthesized for a stub. It does not have
5065 // a real symbol. We just look at the LSB of the symbol value to
5066 // determine if the target is THUMB or not.
5067 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
5070 // Strip LSB if this points to a THUMB target.
5072 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
5073 && ((psymval->value(object, 0) & 1) != 0))
5075 Arm_address stripped_value =
5076 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
5077 symval.set_output_value(stripped_value);
5081 // Get the GOT offset if needed.
5082 // The GOT pointer points to the end of the GOT section.
5083 // We need to subtract the size of the GOT section to get
5084 // the actual offset to use in the relocation.
5085 bool have_got_offset = false;
5086 unsigned int got_offset = 0;
5089 case elfcpp::R_ARM_GOT_BREL:
5090 case elfcpp::R_ARM_GOT_PREL:
5093 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5094 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
5095 - target->got_size());
5099 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5100 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5101 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
5102 - target->got_size());
5104 have_got_offset = true;
5111 // To look up relocation stubs, we need to pass the symbol table index of
5113 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5115 typename Arm_relocate_functions::Status reloc_status =
5116 Arm_relocate_functions::STATUS_OKAY;
5119 case elfcpp::R_ARM_NONE:
5122 case elfcpp::R_ARM_ABS8:
5123 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5125 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
5128 case elfcpp::R_ARM_ABS12:
5129 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5131 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
5134 case elfcpp::R_ARM_ABS16:
5135 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5137 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5140 case elfcpp::R_ARM_ABS32:
5141 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5143 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5147 case elfcpp::R_ARM_ABS32_NOI:
5148 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5150 // No thumb bit for this relocation: (S + A)
5151 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5155 case elfcpp::R_ARM_MOVW_ABS_NC:
5156 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5158 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
5162 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5163 "a shared object; recompile with -fPIC"));
5166 case elfcpp::R_ARM_MOVT_ABS:
5167 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5169 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
5171 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5172 "a shared object; recompile with -fPIC"));
5175 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5176 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5178 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
5182 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5183 "making a shared object; recompile with -fPIC"));
5186 case elfcpp::R_ARM_THM_MOVT_ABS:
5187 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5189 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
5192 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5193 "making a shared object; recompile with -fPIC"));
5196 case elfcpp::R_ARM_MOVW_PREL_NC:
5197 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
5202 case elfcpp::R_ARM_MOVT_PREL:
5203 reloc_status = Arm_relocate_functions::movt_prel(view, object,
5207 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5208 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
5213 case elfcpp::R_ARM_THM_MOVT_PREL:
5214 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
5218 case elfcpp::R_ARM_REL32:
5219 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5220 address, thumb_bit);
5223 case elfcpp::R_ARM_THM_ABS5:
5224 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5226 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
5229 case elfcpp::R_ARM_THM_CALL:
5231 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
5232 psymval, address, thumb_bit,
5233 is_weakly_undefined_without_plt);
5236 case elfcpp::R_ARM_XPC25:
5238 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
5239 psymval, address, thumb_bit,
5240 is_weakly_undefined_without_plt);
5243 case elfcpp::R_ARM_THM_XPC22:
5245 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
5246 psymval, address, thumb_bit,
5247 is_weakly_undefined_without_plt);
5250 case elfcpp::R_ARM_GOTOFF32:
5252 Arm_address got_origin;
5253 got_origin = target->got_plt_section()->address();
5254 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5255 got_origin, thumb_bit);
5259 case elfcpp::R_ARM_BASE_PREL:
5262 // Get the addressing origin of the output segment defining the
5263 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5264 gold_assert(gsym != NULL);
5265 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5266 origin = gsym->output_segment()->vaddr();
5267 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5268 origin = gsym->output_data()->address();
5271 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5272 _("cannot find origin of R_ARM_BASE_PREL"));
5275 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
5279 case elfcpp::R_ARM_BASE_ABS:
5281 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5286 // Get the addressing origin of the output segment defining
5287 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5289 // R_ARM_BASE_ABS with the NULL symbol will give the
5290 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5291 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5292 origin = target->got_plt_section()->address();
5293 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5294 origin = gsym->output_segment()->vaddr();
5295 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5296 origin = gsym->output_data()->address();
5299 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5300 _("cannot find origin of R_ARM_BASE_ABS"));
5304 reloc_status = Arm_relocate_functions::base_abs(view, origin);
5308 case elfcpp::R_ARM_GOT_BREL:
5309 gold_assert(have_got_offset);
5310 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
5313 case elfcpp::R_ARM_GOT_PREL:
5314 gold_assert(have_got_offset);
5315 // Get the address origin for GOT PLT, which is allocated right
5316 // after the GOT section, to calculate an absolute address of
5317 // the symbol GOT entry (got_origin + got_offset).
5318 Arm_address got_origin;
5319 got_origin = target->got_plt_section()->address();
5320 reloc_status = Arm_relocate_functions::got_prel(view,
5321 got_origin + got_offset,
5325 case elfcpp::R_ARM_PLT32:
5326 gold_assert(gsym == NULL
5327 || gsym->has_plt_offset()
5328 || gsym->final_value_is_known()
5329 || (gsym->is_defined()
5330 && !gsym->is_from_dynobj()
5331 && !gsym->is_preemptible()));
5333 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
5334 psymval, address, thumb_bit,
5335 is_weakly_undefined_without_plt);
5338 case elfcpp::R_ARM_CALL:
5340 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
5341 psymval, address, thumb_bit,
5342 is_weakly_undefined_without_plt);
5345 case elfcpp::R_ARM_JUMP24:
5347 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
5348 psymval, address, thumb_bit,
5349 is_weakly_undefined_without_plt);
5352 case elfcpp::R_ARM_THM_JUMP24:
5354 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
5355 psymval, address, thumb_bit,
5356 is_weakly_undefined_without_plt);
5359 case elfcpp::R_ARM_PREL31:
5360 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
5361 address, thumb_bit);
5364 case elfcpp::R_ARM_TARGET1:
5365 // This should have been mapped to another type already.
5367 case elfcpp::R_ARM_COPY:
5368 case elfcpp::R_ARM_GLOB_DAT:
5369 case elfcpp::R_ARM_JUMP_SLOT:
5370 case elfcpp::R_ARM_RELATIVE:
5371 // These are relocations which should only be seen by the
5372 // dynamic linker, and should never be seen here.
5373 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5374 _("unexpected reloc %u in object file"),
5379 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5380 _("unsupported reloc %u"),
5385 // Report any errors.
5386 switch (reloc_status)
5388 case Arm_relocate_functions::STATUS_OKAY:
5390 case Arm_relocate_functions::STATUS_OVERFLOW:
5391 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5392 _("relocation overflow in relocation %u"),
5395 case Arm_relocate_functions::STATUS_BAD_RELOC:
5396 gold_error_at_location(
5400 _("unexpected opcode while processing relocation %u"),
5410 // Relocate section data.
5412 template<bool big_endian>
5414 Target_arm<big_endian>::relocate_section(
5415 const Relocate_info<32, big_endian>* relinfo,
5416 unsigned int sh_type,
5417 const unsigned char* prelocs,
5419 Output_section* output_section,
5420 bool needs_special_offset_handling,
5421 unsigned char* view,
5422 Arm_address address,
5423 section_size_type view_size,
5424 const Reloc_symbol_changes* reloc_symbol_changes)
5426 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
5427 gold_assert(sh_type == elfcpp::SHT_REL);
5429 Arm_input_section<big_endian>* arm_input_section =
5430 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
5432 // This is an ARM input section and the view covers the whole output
5434 if (arm_input_section != NULL)
5436 gold_assert(needs_special_offset_handling);
5437 Arm_address section_address = arm_input_section->address();
5438 section_size_type section_size = arm_input_section->data_size();
5440 gold_assert((arm_input_section->address() >= address)
5441 && ((arm_input_section->address()
5442 + arm_input_section->data_size())
5443 <= (address + view_size)));
5445 off_t offset = section_address - address;
5448 view_size = section_size;
5451 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
5458 needs_special_offset_handling,
5462 reloc_symbol_changes);
5465 // Return the size of a relocation while scanning during a relocatable
5468 template<bool big_endian>
5470 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
5471 unsigned int r_type,
5474 r_type = get_real_reloc_type(r_type);
5477 case elfcpp::R_ARM_NONE:
5480 case elfcpp::R_ARM_ABS8:
5483 case elfcpp::R_ARM_ABS16:
5484 case elfcpp::R_ARM_THM_ABS5:
5487 case elfcpp::R_ARM_ABS32:
5488 case elfcpp::R_ARM_ABS32_NOI:
5489 case elfcpp::R_ARM_ABS12:
5490 case elfcpp::R_ARM_BASE_ABS:
5491 case elfcpp::R_ARM_REL32:
5492 case elfcpp::R_ARM_THM_CALL:
5493 case elfcpp::R_ARM_GOTOFF32:
5494 case elfcpp::R_ARM_BASE_PREL:
5495 case elfcpp::R_ARM_GOT_BREL:
5496 case elfcpp::R_ARM_GOT_PREL:
5497 case elfcpp::R_ARM_PLT32:
5498 case elfcpp::R_ARM_CALL:
5499 case elfcpp::R_ARM_JUMP24:
5500 case elfcpp::R_ARM_PREL31:
5501 case elfcpp::R_ARM_MOVW_ABS_NC:
5502 case elfcpp::R_ARM_MOVT_ABS:
5503 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5504 case elfcpp::R_ARM_THM_MOVT_ABS:
5505 case elfcpp::R_ARM_MOVW_PREL_NC:
5506 case elfcpp::R_ARM_MOVT_PREL:
5507 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5508 case elfcpp::R_ARM_THM_MOVT_PREL:
5511 case elfcpp::R_ARM_TARGET1:
5512 // This should have been mapped to another type already.
5514 case elfcpp::R_ARM_COPY:
5515 case elfcpp::R_ARM_GLOB_DAT:
5516 case elfcpp::R_ARM_JUMP_SLOT:
5517 case elfcpp::R_ARM_RELATIVE:
5518 // These are relocations which should only be seen by the
5519 // dynamic linker, and should never be seen here.
5520 gold_error(_("%s: unexpected reloc %u in object file"),
5521 object->name().c_str(), r_type);
5525 object->error(_("unsupported reloc %u in object file"), r_type);
5530 // Scan the relocs during a relocatable link.
5532 template<bool big_endian>
5534 Target_arm<big_endian>::scan_relocatable_relocs(
5535 Symbol_table* symtab,
5537 Sized_relobj<32, big_endian>* object,
5538 unsigned int data_shndx,
5539 unsigned int sh_type,
5540 const unsigned char* prelocs,
5542 Output_section* output_section,
5543 bool needs_special_offset_handling,
5544 size_t local_symbol_count,
5545 const unsigned char* plocal_symbols,
5546 Relocatable_relocs* rr)
5548 gold_assert(sh_type == elfcpp::SHT_REL);
5550 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
5551 Relocatable_size_for_reloc> Scan_relocatable_relocs;
5553 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
5554 Scan_relocatable_relocs>(
5562 needs_special_offset_handling,
5568 // Relocate a section during a relocatable link.
5570 template<bool big_endian>
5572 Target_arm<big_endian>::relocate_for_relocatable(
5573 const Relocate_info<32, big_endian>* relinfo,
5574 unsigned int sh_type,
5575 const unsigned char* prelocs,
5577 Output_section* output_section,
5578 off_t offset_in_output_section,
5579 const Relocatable_relocs* rr,
5580 unsigned char* view,
5581 Arm_address view_address,
5582 section_size_type view_size,
5583 unsigned char* reloc_view,
5584 section_size_type reloc_view_size)
5586 gold_assert(sh_type == elfcpp::SHT_REL);
5588 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
5593 offset_in_output_section,
5602 // Return the value to use for a dynamic symbol which requires special
5603 // treatment. This is how we support equality comparisons of function
5604 // pointers across shared library boundaries, as described in the
5605 // processor specific ABI supplement.
5607 template<bool big_endian>
5609 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
5611 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5612 return this->plt_section()->address() + gsym->plt_offset();
5615 // Map platform-specific relocs to real relocs
5617 template<bool big_endian>
5619 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
5623 case elfcpp::R_ARM_TARGET1:
5624 // This is either R_ARM_ABS32 or R_ARM_REL32;
5625 return elfcpp::R_ARM_ABS32;
5627 case elfcpp::R_ARM_TARGET2:
5628 // This can be any reloc type but ususally is R_ARM_GOT_PREL
5629 return elfcpp::R_ARM_GOT_PREL;
5636 // Whether if two EABI versions V1 and V2 are compatible.
5638 template<bool big_endian>
5640 Target_arm<big_endian>::are_eabi_versions_compatible(
5641 elfcpp::Elf_Word v1,
5642 elfcpp::Elf_Word v2)
5644 // v4 and v5 are the same spec before and after it was released,
5645 // so allow mixing them.
5646 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
5647 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
5653 // Combine FLAGS from an input object called NAME and the processor-specific
5654 // flags in the ELF header of the output. Much of this is adapted from the
5655 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5656 // in bfd/elf32-arm.c.
5658 template<bool big_endian>
5660 Target_arm<big_endian>::merge_processor_specific_flags(
5661 const std::string& name,
5662 elfcpp::Elf_Word flags)
5664 if (this->are_processor_specific_flags_set())
5666 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
5668 // Nothing to merge if flags equal to those in output.
5669 if (flags == out_flags)
5672 // Complain about various flag mismatches.
5673 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
5674 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
5675 if (!this->are_eabi_versions_compatible(version1, version2))
5676 gold_error(_("Source object %s has EABI version %d but output has "
5677 "EABI version %d."),
5679 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
5680 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
5684 // If the input is the default architecture and had the default
5685 // flags then do not bother setting the flags for the output
5686 // architecture, instead allow future merges to do this. If no
5687 // future merges ever set these flags then they will retain their
5688 // uninitialised values, which surprise surprise, correspond
5689 // to the default values.
5693 // This is the first time, just copy the flags.
5694 // We only copy the EABI version for now.
5695 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
5699 // Adjust ELF file header.
5700 template<bool big_endian>
5702 Target_arm<big_endian>::do_adjust_elf_header(
5703 unsigned char* view,
5706 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
5708 elfcpp::Ehdr<32, big_endian> ehdr(view);
5709 unsigned char e_ident[elfcpp::EI_NIDENT];
5710 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
5712 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5713 == elfcpp::EF_ARM_EABI_UNKNOWN)
5714 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
5716 e_ident[elfcpp::EI_OSABI] = 0;
5717 e_ident[elfcpp::EI_ABIVERSION] = 0;
5719 // FIXME: Do EF_ARM_BE8 adjustment.
5721 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
5722 oehdr.put_e_ident(e_ident);
5725 // do_make_elf_object to override the same function in the base class.
5726 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5727 // to store ARM specific information. Hence we need to have our own
5728 // ELF object creation.
5730 template<bool big_endian>
5732 Target_arm<big_endian>::do_make_elf_object(
5733 const std::string& name,
5734 Input_file* input_file,
5735 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
5737 int et = ehdr.get_e_type();
5738 if (et == elfcpp::ET_REL)
5740 Arm_relobj<big_endian>* obj =
5741 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
5745 else if (et == elfcpp::ET_DYN)
5747 Sized_dynobj<32, big_endian>* obj =
5748 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
5754 gold_error(_("%s: unsupported ELF file type %d"),
5760 // Read the architecture from the Tag_also_compatible_with attribute, if any.
5761 // Returns -1 if no architecture could be read.
5762 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
5764 template<bool big_endian>
5766 Target_arm<big_endian>::get_secondary_compatible_arch(
5767 const Attributes_section_data* pasd)
5769 const Object_attribute *known_attributes =
5770 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
5772 // Note: the tag and its argument below are uleb128 values, though
5773 // currently-defined values fit in one byte for each.
5774 const std::string& sv =
5775 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
5777 && sv.data()[0] == elfcpp::Tag_CPU_arch
5778 && (sv.data()[1] & 128) != 128)
5779 return sv.data()[1];
5781 // This tag is "safely ignorable", so don't complain if it looks funny.
5785 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
5786 // The tag is removed if ARCH is -1.
5787 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
5789 template<bool big_endian>
5791 Target_arm<big_endian>::set_secondary_compatible_arch(
5792 Attributes_section_data* pasd,
5795 Object_attribute *known_attributes =
5796 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
5800 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
5804 // Note: the tag and its argument below are uleb128 values, though
5805 // currently-defined values fit in one byte for each.
5807 sv[0] = elfcpp::Tag_CPU_arch;
5808 gold_assert(arch != 0);
5812 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
5815 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
5817 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
5819 template<bool big_endian>
5821 Target_arm<big_endian>::tag_cpu_arch_combine(
5824 int* secondary_compat_out,
5826 int secondary_compat)
5828 #define T(X) elfcpp::TAG_CPU_ARCH_##X
5829 static const int v6t2[] =
5841 static const int v6k[] =
5854 static const int v7[] =
5868 static const int v6_m[] =
5883 static const int v6s_m[] =
5899 static const int v7e_m[] =
5916 static const int v4t_plus_v6_m[] =
5932 T(V4T_PLUS_V6_M) // V4T plus V6_M.
5934 static const int *comb[] =
5942 // Pseudo-architecture.
5946 // Check we've not got a higher architecture than we know about.
5948 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
5950 gold_error(_("%s: unknown CPU architecture"), name);
5954 // Override old tag if we have a Tag_also_compatible_with on the output.
5956 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
5957 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
5958 oldtag = T(V4T_PLUS_V6_M);
5960 // And override the new tag if we have a Tag_also_compatible_with on the
5963 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
5964 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
5965 newtag = T(V4T_PLUS_V6_M);
5967 // Architectures before V6KZ add features monotonically.
5968 int tagh = std::max(oldtag, newtag);
5969 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
5972 int tagl = std::min(oldtag, newtag);
5973 int result = comb[tagh - T(V6T2)][tagl];
5975 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
5976 // as the canonical version.
5977 if (result == T(V4T_PLUS_V6_M))
5980 *secondary_compat_out = T(V6_M);
5983 *secondary_compat_out = -1;
5987 gold_error(_("%s: conflicting CPU architectures %d/%d"),
5988 name, oldtag, newtag);
5996 // Helper to print AEABI enum tag value.
5998 template<bool big_endian>
6000 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
6002 static const char *aeabi_enum_names[] =
6003 { "", "variable-size", "32-bit", "" };
6004 const size_t aeabi_enum_names_size =
6005 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
6007 if (value < aeabi_enum_names_size)
6008 return std::string(aeabi_enum_names[value]);
6012 sprintf(buffer, "<unknown value %u>", value);
6013 return std::string(buffer);
6017 // Return the string value to store in TAG_CPU_name.
6019 template<bool big_endian>
6021 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
6023 static const char *name_table[] = {
6024 // These aren't real CPU names, but we can't guess
6025 // that from the architecture version alone.
6041 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
6043 if (value < name_table_size)
6044 return std::string(name_table[value]);
6048 sprintf(buffer, "<unknown CPU value %u>", value);
6049 return std::string(buffer);
6053 // Merge object attributes from input file called NAME with those of the
6054 // output. The input object attributes are in the object pointed by PASD.
6056 template<bool big_endian>
6058 Target_arm<big_endian>::merge_object_attributes(
6060 const Attributes_section_data* pasd)
6062 // Return if there is no attributes section data.
6066 // If output has no object attributes, just copy.
6067 if (this->attributes_section_data_ == NULL)
6069 this->attributes_section_data_ = new Attributes_section_data(*pasd);
6073 const int vendor = Object_attribute::OBJ_ATTR_PROC;
6074 const Object_attribute* in_attr = pasd->known_attributes(vendor);
6075 Object_attribute* out_attr =
6076 this->attributes_section_data_->known_attributes(vendor);
6078 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6079 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
6080 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
6082 // Ignore mismatches if the object doesn't use floating point. */
6083 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
6084 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
6085 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
6086 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
6087 gold_error(_("%s uses VFP register arguments, output does not"),
6091 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
6093 // Merge this attribute with existing attributes.
6096 case elfcpp::Tag_CPU_raw_name:
6097 case elfcpp::Tag_CPU_name:
6098 // These are merged after Tag_CPU_arch.
6101 case elfcpp::Tag_ABI_optimization_goals:
6102 case elfcpp::Tag_ABI_FP_optimization_goals:
6103 // Use the first value seen.
6106 case elfcpp::Tag_CPU_arch:
6108 unsigned int saved_out_attr = out_attr->int_value();
6109 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6110 int secondary_compat =
6111 this->get_secondary_compatible_arch(pasd);
6112 int secondary_compat_out =
6113 this->get_secondary_compatible_arch(
6114 this->attributes_section_data_);
6115 out_attr[i].set_int_value(
6116 tag_cpu_arch_combine(name, out_attr[i].int_value(),
6117 &secondary_compat_out,
6118 in_attr[i].int_value(),
6120 this->set_secondary_compatible_arch(this->attributes_section_data_,
6121 secondary_compat_out);
6123 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6124 if (out_attr[i].int_value() == saved_out_attr)
6125 ; // Leave the names alone.
6126 else if (out_attr[i].int_value() == in_attr[i].int_value())
6128 // The output architecture has been changed to match the
6129 // input architecture. Use the input names.
6130 out_attr[elfcpp::Tag_CPU_name].set_string_value(
6131 in_attr[elfcpp::Tag_CPU_name].string_value());
6132 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
6133 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
6137 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
6138 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
6141 // If we still don't have a value for Tag_CPU_name,
6142 // make one up now. Tag_CPU_raw_name remains blank.
6143 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
6145 const std::string cpu_name =
6146 this->tag_cpu_name_value(out_attr[i].int_value());
6147 // FIXME: If we see an unknown CPU, this will be set
6148 // to "<unknown CPU n>", where n is the attribute value.
6149 // This is different from BFD, which leaves the name alone.
6150 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
6155 case elfcpp::Tag_ARM_ISA_use:
6156 case elfcpp::Tag_THUMB_ISA_use:
6157 case elfcpp::Tag_WMMX_arch:
6158 case elfcpp::Tag_Advanced_SIMD_arch:
6159 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6160 case elfcpp::Tag_ABI_FP_rounding:
6161 case elfcpp::Tag_ABI_FP_exceptions:
6162 case elfcpp::Tag_ABI_FP_user_exceptions:
6163 case elfcpp::Tag_ABI_FP_number_model:
6164 case elfcpp::Tag_VFP_HP_extension:
6165 case elfcpp::Tag_CPU_unaligned_access:
6166 case elfcpp::Tag_T2EE_use:
6167 case elfcpp::Tag_Virtualization_use:
6168 case elfcpp::Tag_MPextension_use:
6169 // Use the largest value specified.
6170 if (in_attr[i].int_value() > out_attr[i].int_value())
6171 out_attr[i].set_int_value(in_attr[i].int_value());
6174 case elfcpp::Tag_ABI_align8_preserved:
6175 case elfcpp::Tag_ABI_PCS_RO_data:
6176 // Use the smallest value specified.
6177 if (in_attr[i].int_value() < out_attr[i].int_value())
6178 out_attr[i].set_int_value(in_attr[i].int_value());
6181 case elfcpp::Tag_ABI_align8_needed:
6182 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
6183 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
6184 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
6187 // This error message should be enabled once all non-conformant
6188 // binaries in the toolchain have had the attributes set
6190 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6194 case elfcpp::Tag_ABI_FP_denormal:
6195 case elfcpp::Tag_ABI_PCS_GOT_use:
6197 // These tags have 0 = don't care, 1 = strong requirement,
6198 // 2 = weak requirement.
6199 static const int order_021[3] = {0, 2, 1};
6201 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6202 // value if greater than 2 (for future-proofing).
6203 if ((in_attr[i].int_value() > 2
6204 && in_attr[i].int_value() > out_attr[i].int_value())
6205 || (in_attr[i].int_value() <= 2
6206 && out_attr[i].int_value() <= 2
6207 && (order_021[in_attr[i].int_value()]
6208 > order_021[out_attr[i].int_value()])))
6209 out_attr[i].set_int_value(in_attr[i].int_value());
6213 case elfcpp::Tag_CPU_arch_profile:
6214 if (out_attr[i].int_value() != in_attr[i].int_value())
6216 // 0 will merge with anything.
6217 // 'A' and 'S' merge to 'A'.
6218 // 'R' and 'S' merge to 'R'.
6219 // 'M' and 'A|R|S' is an error.
6220 if (out_attr[i].int_value() == 0
6221 || (out_attr[i].int_value() == 'S'
6222 && (in_attr[i].int_value() == 'A'
6223 || in_attr[i].int_value() == 'R')))
6224 out_attr[i].set_int_value(in_attr[i].int_value());
6225 else if (in_attr[i].int_value() == 0
6226 || (in_attr[i].int_value() == 'S'
6227 && (out_attr[i].int_value() == 'A'
6228 || out_attr[i].int_value() == 'R')))
6233 (_("conflicting architecture profiles %c/%c"),
6234 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
6235 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
6239 case elfcpp::Tag_VFP_arch:
6256 // Values greater than 6 aren't defined, so just pick the
6258 if (in_attr[i].int_value() > 6
6259 && in_attr[i].int_value() > out_attr[i].int_value())
6261 *out_attr = *in_attr;
6264 // The output uses the superset of input features
6265 // (ISA version) and registers.
6266 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
6267 vfp_versions[out_attr[i].int_value()].ver);
6268 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
6269 vfp_versions[out_attr[i].int_value()].regs);
6270 // This assumes all possible supersets are also a valid
6273 for (newval = 6; newval > 0; newval--)
6275 if (regs == vfp_versions[newval].regs
6276 && ver == vfp_versions[newval].ver)
6279 out_attr[i].set_int_value(newval);
6282 case elfcpp::Tag_PCS_config:
6283 if (out_attr[i].int_value() == 0)
6284 out_attr[i].set_int_value(in_attr[i].int_value());
6285 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6287 // It's sometimes ok to mix different configs, so this is only
6289 gold_warning(_("%s: conflicting platform configuration"), name);
6292 case elfcpp::Tag_ABI_PCS_R9_use:
6293 if (in_attr[i].int_value() != out_attr[i].int_value()
6294 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
6295 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
6297 gold_error(_("%s: conflicting use of R9"), name);
6299 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
6300 out_attr[i].set_int_value(in_attr[i].int_value());
6302 case elfcpp::Tag_ABI_PCS_RW_data:
6303 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6304 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6305 != elfcpp::AEABI_R9_SB)
6306 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6307 != elfcpp::AEABI_R9_unused))
6309 gold_error(_("%s: SB relative addressing conflicts with use "
6313 // Use the smallest value specified.
6314 if (in_attr[i].int_value() < out_attr[i].int_value())
6315 out_attr[i].set_int_value(in_attr[i].int_value());
6317 case elfcpp::Tag_ABI_PCS_wchar_t:
6318 // FIXME: Make it possible to turn off this warning.
6319 if (out_attr[i].int_value()
6320 && in_attr[i].int_value()
6321 && out_attr[i].int_value() != in_attr[i].int_value())
6323 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6324 "use %u-byte wchar_t; use of wchar_t values "
6325 "across objects may fail"),
6326 name, in_attr[i].int_value(),
6327 out_attr[i].int_value());
6329 else if (in_attr[i].int_value() && !out_attr[i].int_value())
6330 out_attr[i].set_int_value(in_attr[i].int_value());
6332 case elfcpp::Tag_ABI_enum_size:
6333 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
6335 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
6336 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
6338 // The existing object is compatible with anything.
6339 // Use whatever requirements the new object has.
6340 out_attr[i].set_int_value(in_attr[i].int_value());
6342 // FIXME: Make it possible to turn off this warning.
6343 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
6344 && out_attr[i].int_value() != in_attr[i].int_value())
6346 unsigned int in_value = in_attr[i].int_value();
6347 unsigned int out_value = out_attr[i].int_value();
6348 gold_warning(_("%s uses %s enums yet the output is to use "
6349 "%s enums; use of enum values across objects "
6352 this->aeabi_enum_name(in_value).c_str(),
6353 this->aeabi_enum_name(out_value).c_str());
6357 case elfcpp::Tag_ABI_VFP_args:
6360 case elfcpp::Tag_ABI_WMMX_args:
6361 if (in_attr[i].int_value() != out_attr[i].int_value())
6363 gold_error(_("%s uses iWMMXt register arguments, output does "
6368 case Object_attribute::Tag_compatibility:
6369 // Merged in target-independent code.
6371 case elfcpp::Tag_ABI_HardFP_use:
6372 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6373 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
6374 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
6375 out_attr[i].set_int_value(3);
6376 else if (in_attr[i].int_value() > out_attr[i].int_value())
6377 out_attr[i].set_int_value(in_attr[i].int_value());
6379 case elfcpp::Tag_ABI_FP_16bit_format:
6380 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6382 if (in_attr[i].int_value() != out_attr[i].int_value())
6383 gold_error(_("fp16 format mismatch between %s and output"),
6386 if (in_attr[i].int_value() != 0)
6387 out_attr[i].set_int_value(in_attr[i].int_value());
6390 case elfcpp::Tag_nodefaults:
6391 // This tag is set if it exists, but the value is unused (and is
6392 // typically zero). We don't actually need to do anything here -
6393 // the merge happens automatically when the type flags are merged
6396 case elfcpp::Tag_also_compatible_with:
6397 // Already done in Tag_CPU_arch.
6399 case elfcpp::Tag_conformance:
6400 // Keep the attribute if it matches. Throw it away otherwise.
6401 // No attribute means no claim to conform.
6402 if (in_attr[i].string_value() != out_attr[i].string_value())
6403 out_attr[i].set_string_value("");
6408 const char* err_object = NULL;
6410 // The "known_obj_attributes" table does contain some undefined
6411 // attributes. Ensure that there are unused.
6412 if (out_attr[i].int_value() != 0
6413 || out_attr[i].string_value() != "")
6414 err_object = "output";
6415 else if (in_attr[i].int_value() != 0
6416 || in_attr[i].string_value() != "")
6419 if (err_object != NULL)
6421 // Attribute numbers >=64 (mod 128) can be safely ignored.
6423 gold_error(_("%s: unknown mandatory EABI object attribute "
6427 gold_warning(_("%s: unknown EABI object attribute %d"),
6431 // Only pass on attributes that match in both inputs.
6432 if (!in_attr[i].matches(out_attr[i]))
6434 out_attr[i].set_int_value(0);
6435 out_attr[i].set_string_value("");
6440 // If out_attr was copied from in_attr then it won't have a type yet.
6441 if (in_attr[i].type() && !out_attr[i].type())
6442 out_attr[i].set_type(in_attr[i].type());
6445 // Merge Tag_compatibility attributes and any common GNU ones.
6446 this->attributes_section_data_->merge(name, pasd);
6448 // Check for any attributes not known on ARM.
6449 typedef Vendor_object_attributes::Other_attributes Other_attributes;
6450 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
6451 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
6452 Other_attributes* out_other_attributes =
6453 this->attributes_section_data_->other_attributes(vendor);
6454 Other_attributes::iterator out_iter = out_other_attributes->begin();
6456 while (in_iter != in_other_attributes->end()
6457 || out_iter != out_other_attributes->end())
6459 const char* err_object = NULL;
6462 // The tags for each list are in numerical order.
6463 // If the tags are equal, then merge.
6464 if (out_iter != out_other_attributes->end()
6465 && (in_iter == in_other_attributes->end()
6466 || in_iter->first > out_iter->first))
6468 // This attribute only exists in output. We can't merge, and we
6469 // don't know what the tag means, so delete it.
6470 err_object = "output";
6471 err_tag = out_iter->first;
6472 int saved_tag = out_iter->first;
6473 delete out_iter->second;
6474 out_other_attributes->erase(out_iter);
6475 out_iter = out_other_attributes->upper_bound(saved_tag);
6477 else if (in_iter != in_other_attributes->end()
6478 && (out_iter != out_other_attributes->end()
6479 || in_iter->first < out_iter->first))
6481 // This attribute only exists in input. We can't merge, and we
6482 // don't know what the tag means, so ignore it.
6484 err_tag = in_iter->first;
6487 else // The tags are equal.
6489 // As present, all attributes in the list are unknown, and
6490 // therefore can't be merged meaningfully.
6491 err_object = "output";
6492 err_tag = out_iter->first;
6494 // Only pass on attributes that match in both inputs.
6495 if (!in_iter->second->matches(*(out_iter->second)))
6497 // No match. Delete the attribute.
6498 int saved_tag = out_iter->first;
6499 delete out_iter->second;
6500 out_other_attributes->erase(out_iter);
6501 out_iter = out_other_attributes->upper_bound(saved_tag);
6505 // Matched. Keep the attribute and move to the next.
6513 // Attribute numbers >=64 (mod 128) can be safely ignored. */
6514 if ((err_tag & 127) < 64)
6516 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
6517 err_object, err_tag);
6521 gold_warning(_("%s: unknown EABI object attribute %d"),
6522 err_object, err_tag);
6528 // Return whether a relocation type used the LSB to distinguish THUMB
6530 template<bool big_endian>
6532 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
6536 case elfcpp::R_ARM_PC24:
6537 case elfcpp::R_ARM_ABS32:
6538 case elfcpp::R_ARM_REL32:
6539 case elfcpp::R_ARM_SBREL32:
6540 case elfcpp::R_ARM_THM_CALL:
6541 case elfcpp::R_ARM_GLOB_DAT:
6542 case elfcpp::R_ARM_JUMP_SLOT:
6543 case elfcpp::R_ARM_GOTOFF32:
6544 case elfcpp::R_ARM_PLT32:
6545 case elfcpp::R_ARM_CALL:
6546 case elfcpp::R_ARM_JUMP24:
6547 case elfcpp::R_ARM_THM_JUMP24:
6548 case elfcpp::R_ARM_SBREL31:
6549 case elfcpp::R_ARM_PREL31:
6550 case elfcpp::R_ARM_MOVW_ABS_NC:
6551 case elfcpp::R_ARM_MOVW_PREL_NC:
6552 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6553 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6554 case elfcpp::R_ARM_THM_JUMP19:
6555 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
6556 case elfcpp::R_ARM_ALU_PC_G0_NC:
6557 case elfcpp::R_ARM_ALU_PC_G0:
6558 case elfcpp::R_ARM_ALU_PC_G1_NC:
6559 case elfcpp::R_ARM_ALU_PC_G1:
6560 case elfcpp::R_ARM_ALU_PC_G2:
6561 case elfcpp::R_ARM_ALU_SB_G0_NC:
6562 case elfcpp::R_ARM_ALU_SB_G0:
6563 case elfcpp::R_ARM_ALU_SB_G1_NC:
6564 case elfcpp::R_ARM_ALU_SB_G1:
6565 case elfcpp::R_ARM_ALU_SB_G2:
6566 case elfcpp::R_ARM_MOVW_BREL_NC:
6567 case elfcpp::R_ARM_MOVW_BREL:
6568 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
6569 case elfcpp::R_ARM_THM_MOVW_BREL:
6576 // Stub-generation methods for Target_arm.
6578 // Make a new Arm_input_section object.
6580 template<bool big_endian>
6581 Arm_input_section<big_endian>*
6582 Target_arm<big_endian>::new_arm_input_section(
6586 Input_section_specifier iss(relobj, shndx);
6588 Arm_input_section<big_endian>* arm_input_section =
6589 new Arm_input_section<big_endian>(relobj, shndx);
6590 arm_input_section->init();
6592 // Register new Arm_input_section in map for look-up.
6593 std::pair<typename Arm_input_section_map::iterator, bool> ins =
6594 this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
6596 // Make sure that it we have not created another Arm_input_section
6597 // for this input section already.
6598 gold_assert(ins.second);
6600 return arm_input_section;
6603 // Find the Arm_input_section object corresponding to the SHNDX-th input
6604 // section of RELOBJ.
6606 template<bool big_endian>
6607 Arm_input_section<big_endian>*
6608 Target_arm<big_endian>::find_arm_input_section(
6610 unsigned int shndx) const
6612 Input_section_specifier iss(relobj, shndx);
6613 typename Arm_input_section_map::const_iterator p =
6614 this->arm_input_section_map_.find(iss);
6615 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
6618 // Make a new stub table.
6620 template<bool big_endian>
6621 Stub_table<big_endian>*
6622 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
6624 Stub_table<big_endian>* stub_table =
6625 new Stub_table<big_endian>(owner);
6626 this->stub_tables_.push_back(stub_table);
6628 stub_table->set_address(owner->address() + owner->data_size());
6629 stub_table->set_file_offset(owner->offset() + owner->data_size());
6630 stub_table->finalize_data_size();
6635 // Scan a relocation for stub generation.
6637 template<bool big_endian>
6639 Target_arm<big_endian>::scan_reloc_for_stub(
6640 const Relocate_info<32, big_endian>* relinfo,
6641 unsigned int r_type,
6642 const Sized_symbol<32>* gsym,
6644 const Symbol_value<32>* psymval,
6645 elfcpp::Elf_types<32>::Elf_Swxword addend,
6646 Arm_address address)
6648 typedef typename Target_arm<big_endian>::Relocate Relocate;
6650 const Arm_relobj<big_endian>* arm_relobj =
6651 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
6653 bool target_is_thumb;
6654 Symbol_value<32> symval;
6657 // This is a global symbol. Determine if we use PLT and if the
6658 // final target is THUMB.
6659 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
6661 // This uses a PLT, change the symbol value.
6662 symval.set_output_value(this->plt_section()->address()
6663 + gsym->plt_offset());
6665 target_is_thumb = false;
6667 else if (gsym->is_undefined())
6668 // There is no need to generate a stub symbol is undefined.
6673 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
6674 || (gsym->type() == elfcpp::STT_FUNC
6675 && !gsym->is_undefined()
6676 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
6681 // This is a local symbol. Determine if the final target is THUMB.
6682 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
6685 // Strip LSB if this points to a THUMB target.
6687 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
6688 && ((psymval->value(arm_relobj, 0) & 1) != 0))
6690 Arm_address stripped_value =
6691 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
6692 symval.set_output_value(stripped_value);
6696 // Get the symbol value.
6697 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
6699 // Owing to pipelining, the PC relative branches below actually skip
6700 // two instructions when the branch offset is 0.
6701 Arm_address destination;
6704 case elfcpp::R_ARM_CALL:
6705 case elfcpp::R_ARM_JUMP24:
6706 case elfcpp::R_ARM_PLT32:
6708 destination = value + addend + 8;
6710 case elfcpp::R_ARM_THM_CALL:
6711 case elfcpp::R_ARM_THM_XPC22:
6712 case elfcpp::R_ARM_THM_JUMP24:
6713 case elfcpp::R_ARM_THM_JUMP19:
6715 destination = value + addend + 4;
6721 Stub_type stub_type =
6722 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
6725 // This reloc does not need a stub.
6726 if (stub_type == arm_stub_none)
6729 // Try looking up an existing stub from a stub table.
6730 Stub_table<big_endian>* stub_table =
6731 arm_relobj->stub_table(relinfo->data_shndx);
6732 gold_assert(stub_table != NULL);
6734 // Locate stub by destination.
6735 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
6737 // Create a stub if there is not one already
6738 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
6741 // create a new stub and add it to stub table.
6742 stub = this->stub_factory().make_reloc_stub(stub_type);
6743 stub_table->add_reloc_stub(stub, stub_key);
6746 // Record the destination address.
6747 stub->set_destination_address(destination
6748 | (target_is_thumb ? 1 : 0));
6751 // This function scans a relocation sections for stub generation.
6752 // The template parameter Relocate must be a class type which provides
6753 // a single function, relocate(), which implements the machine
6754 // specific part of a relocation.
6756 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
6757 // SHT_REL or SHT_RELA.
6759 // PRELOCS points to the relocation data. RELOC_COUNT is the number
6760 // of relocs. OUTPUT_SECTION is the output section.
6761 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
6762 // mapped to output offsets.
6764 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
6765 // VIEW_SIZE is the size. These refer to the input section, unless
6766 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
6767 // the output section.
6769 template<bool big_endian>
6770 template<int sh_type>
6772 Target_arm<big_endian>::scan_reloc_section_for_stubs(
6773 const Relocate_info<32, big_endian>* relinfo,
6774 const unsigned char* prelocs,
6776 Output_section* output_section,
6777 bool needs_special_offset_handling,
6778 const unsigned char* view,
6779 elfcpp::Elf_types<32>::Elf_Addr view_address,
6782 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
6783 const int reloc_size =
6784 Reloc_types<sh_type, 32, big_endian>::reloc_size;
6786 Arm_relobj<big_endian>* arm_object =
6787 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
6788 unsigned int local_count = arm_object->local_symbol_count();
6790 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
6792 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6794 Reltype reloc(prelocs);
6796 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
6797 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6798 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6800 r_type = this->get_real_reloc_type(r_type);
6802 // Only a few relocation types need stubs.
6803 if ((r_type != elfcpp::R_ARM_CALL)
6804 && (r_type != elfcpp::R_ARM_JUMP24)
6805 && (r_type != elfcpp::R_ARM_PLT32)
6806 && (r_type != elfcpp::R_ARM_THM_CALL)
6807 && (r_type != elfcpp::R_ARM_THM_XPC22)
6808 && (r_type != elfcpp::R_ARM_THM_JUMP24)
6809 && (r_type != elfcpp::R_ARM_THM_JUMP19))
6812 section_offset_type offset =
6813 convert_to_section_size_type(reloc.get_r_offset());
6815 if (needs_special_offset_handling)
6817 offset = output_section->output_offset(relinfo->object,
6818 relinfo->data_shndx,
6825 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
6826 elfcpp::Elf_types<32>::Elf_Swxword addend =
6827 stub_addend_reader(r_type, view + offset, reloc);
6829 const Sized_symbol<32>* sym;
6831 Symbol_value<32> symval;
6832 const Symbol_value<32> *psymval;
6833 if (r_sym < local_count)
6836 psymval = arm_object->local_symbol(r_sym);
6838 // If the local symbol belongs to a section we are discarding,
6839 // and that section is a debug section, try to find the
6840 // corresponding kept section and map this symbol to its
6841 // counterpart in the kept section. The symbol must not
6842 // correspond to a section we are folding.
6844 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6846 && shndx != elfcpp::SHN_UNDEF
6847 && !arm_object->is_section_included(shndx)
6848 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
6850 if (comdat_behavior == CB_UNDETERMINED)
6853 arm_object->section_name(relinfo->data_shndx);
6854 comdat_behavior = get_comdat_behavior(name.c_str());
6856 if (comdat_behavior == CB_PRETEND)
6859 typename elfcpp::Elf_types<32>::Elf_Addr value =
6860 arm_object->map_to_kept_section(shndx, &found);
6862 symval.set_output_value(value + psymval->input_value());
6864 symval.set_output_value(0);
6868 symval.set_output_value(0);
6870 symval.set_no_output_symtab_entry();
6876 const Symbol* gsym = arm_object->global_symbol(r_sym);
6877 gold_assert(gsym != NULL);
6878 if (gsym->is_forwarder())
6879 gsym = relinfo->symtab->resolve_forwards(gsym);
6881 sym = static_cast<const Sized_symbol<32>*>(gsym);
6882 if (sym->has_symtab_index())
6883 symval.set_output_symtab_index(sym->symtab_index());
6885 symval.set_no_output_symtab_entry();
6887 // We need to compute the would-be final value of this global
6889 const Symbol_table* symtab = relinfo->symtab;
6890 const Sized_symbol<32>* sized_symbol =
6891 symtab->get_sized_symbol<32>(gsym);
6892 Symbol_table::Compute_final_value_status status;
6894 symtab->compute_final_value<32>(sized_symbol, &status);
6896 // Skip this if the symbol has not output section.
6897 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
6900 symval.set_output_value(value);
6904 // If symbol is a section symbol, we don't know the actual type of
6905 // destination. Give up.
6906 if (psymval->is_section_symbol())
6909 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
6910 addend, view_address + offset);
6914 // Scan an input section for stub generation.
6916 template<bool big_endian>
6918 Target_arm<big_endian>::scan_section_for_stubs(
6919 const Relocate_info<32, big_endian>* relinfo,
6920 unsigned int sh_type,
6921 const unsigned char* prelocs,
6923 Output_section* output_section,
6924 bool needs_special_offset_handling,
6925 const unsigned char* view,
6926 Arm_address view_address,
6927 section_size_type view_size)
6929 if (sh_type == elfcpp::SHT_REL)
6930 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
6935 needs_special_offset_handling,
6939 else if (sh_type == elfcpp::SHT_RELA)
6940 // We do not support RELA type relocations yet. This is provided for
6942 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
6947 needs_special_offset_handling,
6955 // Group input sections for stub generation.
6957 // We goup input sections in an output sections so that the total size,
6958 // including any padding space due to alignment is smaller than GROUP_SIZE
6959 // unless the only input section in group is bigger than GROUP_SIZE already.
6960 // Then an ARM stub table is created to follow the last input section
6961 // in group. For each group an ARM stub table is created an is placed
6962 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
6963 // extend the group after the stub table.
6965 template<bool big_endian>
6967 Target_arm<big_endian>::group_sections(
6969 section_size_type group_size,
6970 bool stubs_always_after_branch)
6972 // Group input sections and insert stub table
6973 Layout::Section_list section_list;
6974 layout->get_allocated_sections(§ion_list);
6975 for (Layout::Section_list::const_iterator p = section_list.begin();
6976 p != section_list.end();
6979 Arm_output_section<big_endian>* output_section =
6980 Arm_output_section<big_endian>::as_arm_output_section(*p);
6981 output_section->group_sections(group_size, stubs_always_after_branch,
6986 // Relaxation hook. This is where we do stub generation.
6988 template<bool big_endian>
6990 Target_arm<big_endian>::do_relax(
6992 const Input_objects* input_objects,
6993 Symbol_table* symtab,
6996 // No need to generate stubs if this is a relocatable link.
6997 gold_assert(!parameters->options().relocatable());
6999 // If this is the first pass, we need to group input sections into
7003 // Determine the stub group size. The group size is the absolute
7004 // value of the parameter --stub-group-size. If --stub-group-size
7005 // is passed a negative value, we restict stubs to be always after
7006 // the stubbed branches.
7007 int32_t stub_group_size_param =
7008 parameters->options().stub_group_size();
7009 bool stubs_always_after_branch = stub_group_size_param < 0;
7010 section_size_type stub_group_size = abs(stub_group_size_param);
7012 if (stub_group_size == 1)
7015 // Thumb branch range is +-4MB has to be used as the default
7016 // maximum size (a given section can contain both ARM and Thumb
7017 // code, so the worst case has to be taken into account).
7019 // This value is 24K less than that, which allows for 2025
7020 // 12-byte stubs. If we exceed that, then we will fail to link.
7021 // The user will have to relink with an explicit group size
7023 stub_group_size = 4170000;
7026 group_sections(layout, stub_group_size, stubs_always_after_branch);
7029 // clear changed flags for all stub_tables
7030 typedef typename Stub_table_list::iterator Stub_table_iterator;
7031 for (Stub_table_iterator sp = this->stub_tables_.begin();
7032 sp != this->stub_tables_.end();
7034 (*sp)->set_has_been_changed(false);
7036 // scan relocs for stubs
7037 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
7038 op != input_objects->relobj_end();
7041 Arm_relobj<big_endian>* arm_relobj =
7042 Arm_relobj<big_endian>::as_arm_relobj(*op);
7043 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
7046 bool any_stub_table_changed = false;
7047 for (Stub_table_iterator sp = this->stub_tables_.begin();
7048 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7051 if ((*sp)->has_been_changed())
7052 any_stub_table_changed = true;
7055 return any_stub_table_changed;
7060 template<bool big_endian>
7062 Target_arm<big_endian>::relocate_stub(
7064 const Relocate_info<32, big_endian>* relinfo,
7065 Output_section* output_section,
7066 unsigned char* view,
7067 Arm_address address,
7068 section_size_type view_size)
7071 const Stub_template* stub_template = stub->stub_template();
7072 for (size_t i = 0; i < stub_template->reloc_count(); i++)
7074 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
7075 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
7077 unsigned int r_type = insn->r_type();
7078 section_size_type reloc_offset = stub_template->reloc_offset(i);
7079 section_size_type reloc_size = insn->size();
7080 gold_assert(reloc_offset + reloc_size <= view_size);
7082 // This is the address of the stub destination.
7083 Arm_address target = stub->reloc_target(i);
7084 Symbol_value<32> symval;
7085 symval.set_output_value(target);
7087 // Synthesize a fake reloc just in case. We don't have a symbol so
7089 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
7090 memset(reloc_buffer, 0, sizeof(reloc_buffer));
7091 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
7092 reloc_write.put_r_offset(reloc_offset);
7093 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
7094 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
7096 relocate.relocate(relinfo, this, output_section,
7097 this->fake_relnum_for_stubs, rel, r_type,
7098 NULL, &symval, view + reloc_offset,
7099 address + reloc_offset, reloc_size);
7103 // Determine whether an object attribute tag takes an integer, a
7106 template<bool big_endian>
7108 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
7110 if (tag == Object_attribute::Tag_compatibility)
7111 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7112 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
7113 else if (tag == elfcpp::Tag_nodefaults)
7114 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7115 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
7116 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
7117 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
7119 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
7121 return ((tag & 1) != 0
7122 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7123 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
7126 // Reorder attributes.
7128 // The ABI defines that Tag_conformance should be emitted first, and that
7129 // Tag_nodefaults should be second (if either is defined). This sets those
7130 // two positions, and bumps up the position of all the remaining tags to
7133 template<bool big_endian>
7135 Target_arm<big_endian>::do_attributes_order(int num) const
7137 // Reorder the known object attributes in output. We want to move
7138 // Tag_conformance to position 4 and Tag_conformance to position 5
7139 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7141 return elfcpp::Tag_conformance;
7143 return elfcpp::Tag_nodefaults;
7144 if ((num - 2) < elfcpp::Tag_nodefaults)
7146 if ((num - 1) < elfcpp::Tag_conformance)
7151 template<bool big_endian>
7152 class Target_selector_arm : public Target_selector
7155 Target_selector_arm()
7156 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
7157 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
7161 do_instantiate_target()
7162 { return new Target_arm<big_endian>(); }
7165 Target_selector_arm<false> target_selector_arm;
7166 Target_selector_arm<true> target_selector_armbe;
7168 } // End anonymous namespace.