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(), owner_(owner), reloc_stubs_(), cortex_a8_stubs_(),
802 prev_data_size_(0), prev_addralign_(1)
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() && this->cortex_a8_stubs_.empty(); }
818 // Return the current data size.
820 current_data_size() const
821 { return this->current_data_size_for_child(); }
823 // Add a STUB with using KEY. Caller is reponsible for avoid adding
824 // if already a STUB with the same key has been added.
826 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
828 const Stub_template* stub_template = stub->stub_template();
829 gold_assert(stub_template->type() == key.stub_type());
830 this->reloc_stubs_[key] = stub;
833 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
834 // Caller is reponsible for avoid adding if already a STUB with the same
835 // address has been added.
837 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
839 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
840 this->cortex_a8_stubs_.insert(value);
843 // Remove all Cortex-A8 stubs.
845 remove_all_cortex_a8_stubs();
847 // Look up a relocation stub using KEY. Return NULL if there is none.
849 find_reloc_stub(const Reloc_stub::Key& key) const
851 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
852 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
855 // Relocate stubs in this stub table.
857 relocate_stubs(const Relocate_info<32, big_endian>*,
858 Target_arm<big_endian>*, Output_section*,
859 unsigned char*, Arm_address, section_size_type);
861 // Update data size and alignment at the end of a relaxation pass. Return
862 // true if either data size or alignment is different from that of the
863 // previous relaxation pass.
865 update_data_size_and_addralign();
867 // Finalize stubs. Set the offsets of all stubs and mark input sections
868 // needing the Cortex-A8 workaround.
872 // Apply Cortex-A8 workaround to an address range.
874 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
875 unsigned char*, Arm_address,
879 // Write out section contents.
881 do_write(Output_file*);
883 // Return the required alignment.
886 { return this->prev_addralign_; }
888 // Reset address and file offset.
890 do_reset_address_and_file_offset()
891 { this->set_current_data_size_for_child(this->prev_data_size_); }
893 // Set final data size.
895 set_final_data_size()
896 { this->set_data_size(this->current_data_size()); }
899 // Relocate one stub.
901 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
902 Target_arm<big_endian>*, Output_section*,
903 unsigned char*, Arm_address, section_size_type);
905 // Unordered map of relocation stubs.
907 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
908 Reloc_stub::Key::equal_to>
911 // List of Cortex-A8 stubs ordered by addresses of branches being
912 // fixed up in output.
913 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
915 // Owner of this stub table.
916 Arm_input_section<big_endian>* owner_;
917 // The relocation stubs.
918 Reloc_stub_map reloc_stubs_;
919 // The cortex_a8_stubs.
920 Cortex_a8_stub_list cortex_a8_stubs_;
921 // data size of this in the previous pass.
922 off_t prev_data_size_;
923 // address alignment of this in the previous pass.
924 uint64_t prev_addralign_;
927 // A class to wrap an ordinary input section containing executable code.
929 template<bool big_endian>
930 class Arm_input_section : public Output_relaxed_input_section
933 Arm_input_section(Relobj* relobj, unsigned int shndx)
934 : Output_relaxed_input_section(relobj, shndx, 1),
935 original_addralign_(1), original_size_(0), stub_table_(NULL)
945 // Whether this is a stub table owner.
947 is_stub_table_owner() const
948 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
950 // Return the stub table.
951 Stub_table<big_endian>*
953 { return this->stub_table_; }
955 // Set the stub_table.
957 set_stub_table(Stub_table<big_endian>* stub_table)
958 { this->stub_table_ = stub_table; }
960 // Downcast a base pointer to an Arm_input_section pointer. This is
961 // not type-safe but we only use Arm_input_section not the base class.
962 static Arm_input_section<big_endian>*
963 as_arm_input_section(Output_relaxed_input_section* poris)
964 { return static_cast<Arm_input_section<big_endian>*>(poris); }
967 // Write data to output file.
969 do_write(Output_file*);
971 // Return required alignment of this.
975 if (this->is_stub_table_owner())
976 return std::max(this->stub_table_->addralign(),
977 this->original_addralign_);
979 return this->original_addralign_;
982 // Finalize data size.
984 set_final_data_size();
986 // Reset address and file offset.
988 do_reset_address_and_file_offset();
992 do_output_offset(const Relobj* object, unsigned int shndx,
993 section_offset_type offset,
994 section_offset_type* poutput) const
996 if ((object == this->relobj())
997 && (shndx == this->shndx())
999 && (convert_types<uint64_t, section_offset_type>(offset)
1000 <= this->original_size_))
1010 // Copying is not allowed.
1011 Arm_input_section(const Arm_input_section&);
1012 Arm_input_section& operator=(const Arm_input_section&);
1014 // Address alignment of the original input section.
1015 uint64_t original_addralign_;
1016 // Section size of the original input section.
1017 uint64_t original_size_;
1019 Stub_table<big_endian>* stub_table_;
1022 // Arm output section class. This is defined mainly to add a number of
1023 // stub generation methods.
1025 template<bool big_endian>
1026 class Arm_output_section : public Output_section
1029 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1030 elfcpp::Elf_Xword flags)
1031 : Output_section(name, type, flags)
1034 ~Arm_output_section()
1037 // Group input sections for stub generation.
1039 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1041 // Downcast a base pointer to an Arm_output_section pointer. This is
1042 // not type-safe but we only use Arm_output_section not the base class.
1043 static Arm_output_section<big_endian>*
1044 as_arm_output_section(Output_section* os)
1045 { return static_cast<Arm_output_section<big_endian>*>(os); }
1049 typedef Output_section::Input_section Input_section;
1050 typedef Output_section::Input_section_list Input_section_list;
1052 // Create a stub group.
1053 void create_stub_group(Input_section_list::const_iterator,
1054 Input_section_list::const_iterator,
1055 Input_section_list::const_iterator,
1056 Target_arm<big_endian>*,
1057 std::vector<Output_relaxed_input_section*>*);
1060 // Arm_relobj class.
1062 template<bool big_endian>
1063 class Arm_relobj : public Sized_relobj<32, big_endian>
1066 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1068 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1069 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1070 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1071 stub_tables_(), local_symbol_is_thumb_function_(),
1072 attributes_section_data_(NULL), section_has_cortex_a8_workaround_(NULL)
1076 { delete this->attributes_section_data_; }
1078 // Return the stub table of the SHNDX-th section if there is one.
1079 Stub_table<big_endian>*
1080 stub_table(unsigned int shndx) const
1082 gold_assert(shndx < this->stub_tables_.size());
1083 return this->stub_tables_[shndx];
1086 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1088 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1090 gold_assert(shndx < this->stub_tables_.size());
1091 this->stub_tables_[shndx] = stub_table;
1094 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1095 // index. This is only valid after do_count_local_symbol is called.
1097 local_symbol_is_thumb_function(unsigned int r_sym) const
1099 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1100 return this->local_symbol_is_thumb_function_[r_sym];
1103 // Scan all relocation sections for stub generation.
1105 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1108 // Convert regular input section with index SHNDX to a relaxed section.
1110 convert_input_section_to_relaxed_section(unsigned shndx)
1112 // The stubs have relocations and we need to process them after writing
1113 // out the stubs. So relocation now must follow section write.
1114 this->invalidate_section_offset(shndx);
1115 this->set_relocs_must_follow_section_writes();
1118 // Downcast a base pointer to an Arm_relobj pointer. This is
1119 // not type-safe but we only use Arm_relobj not the base class.
1120 static Arm_relobj<big_endian>*
1121 as_arm_relobj(Relobj* relobj)
1122 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1124 // Processor-specific flags in ELF file header. This is valid only after
1127 processor_specific_flags() const
1128 { return this->processor_specific_flags_; }
1130 // Attribute section data This is the contents of the .ARM.attribute section
1132 const Attributes_section_data*
1133 attributes_section_data() const
1134 { return this->attributes_section_data_; }
1136 // Whether a section contains any Cortex-A8 workaround.
1138 section_has_cortex_a8_workaround(unsigned int shndx) const
1140 return (this->section_has_cortex_a8_workaround_ != NULL
1141 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1144 // Mark a section that has Cortex-A8 workaround.
1146 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1148 if (this->section_has_cortex_a8_workaround_ == NULL)
1149 this->section_has_cortex_a8_workaround_ =
1150 new std::vector<bool>(this->shnum(), false);
1151 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1155 // Post constructor setup.
1159 // Call parent's setup method.
1160 Sized_relobj<32, big_endian>::do_setup();
1162 // Initialize look-up tables.
1163 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1164 this->stub_tables_.swap(empty_stub_table_list);
1167 // Count the local symbols.
1169 do_count_local_symbols(Stringpool_template<char>*,
1170 Stringpool_template<char>*);
1173 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1174 const unsigned char* pshdrs,
1175 typename Sized_relobj<32, big_endian>::Views* pivews);
1177 // Read the symbol information.
1179 do_read_symbols(Read_symbols_data* sd);
1181 // Process relocs for garbage collection.
1183 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1186 // List of stub tables.
1187 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1188 Stub_table_list stub_tables_;
1189 // Bit vector to tell if a local symbol is a thumb function or not.
1190 // This is only valid after do_count_local_symbol is called.
1191 std::vector<bool> local_symbol_is_thumb_function_;
1192 // processor-specific flags in ELF file header.
1193 elfcpp::Elf_Word processor_specific_flags_;
1194 // Object attributes if there is an .ARM.attributes section or NULL.
1195 Attributes_section_data* attributes_section_data_;
1196 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1197 std::vector<bool>* section_has_cortex_a8_workaround_;
1200 // Arm_dynobj class.
1202 template<bool big_endian>
1203 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1206 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1207 const elfcpp::Ehdr<32, big_endian>& ehdr)
1208 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1209 processor_specific_flags_(0), attributes_section_data_(NULL)
1213 { delete this->attributes_section_data_; }
1215 // Downcast a base pointer to an Arm_relobj pointer. This is
1216 // not type-safe but we only use Arm_relobj not the base class.
1217 static Arm_dynobj<big_endian>*
1218 as_arm_dynobj(Dynobj* dynobj)
1219 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1221 // Processor-specific flags in ELF file header. This is valid only after
1224 processor_specific_flags() const
1225 { return this->processor_specific_flags_; }
1227 // Attributes section data.
1228 const Attributes_section_data*
1229 attributes_section_data() const
1230 { return this->attributes_section_data_; }
1233 // Read the symbol information.
1235 do_read_symbols(Read_symbols_data* sd);
1238 // processor-specific flags in ELF file header.
1239 elfcpp::Elf_Word processor_specific_flags_;
1240 // Object attributes if there is an .ARM.attributes section or NULL.
1241 Attributes_section_data* attributes_section_data_;
1244 // Functor to read reloc addends during stub generation.
1246 template<int sh_type, bool big_endian>
1247 struct Stub_addend_reader
1249 // Return the addend for a relocation of a particular type. Depending
1250 // on whether this is a REL or RELA relocation, read the addend from a
1251 // view or from a Reloc object.
1252 elfcpp::Elf_types<32>::Elf_Swxword
1254 unsigned int /* r_type */,
1255 const unsigned char* /* view */,
1256 const typename Reloc_types<sh_type,
1257 32, big_endian>::Reloc& /* reloc */) const;
1260 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1262 template<bool big_endian>
1263 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1265 elfcpp::Elf_types<32>::Elf_Swxword
1268 const unsigned char*,
1269 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1272 // Specialized Stub_addend_reader for RELA type relocation sections.
1273 // We currently do not handle RELA type relocation sections but it is trivial
1274 // to implement the addend reader. This is provided for completeness and to
1275 // make it easier to add support for RELA relocation sections in the future.
1277 template<bool big_endian>
1278 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1280 elfcpp::Elf_types<32>::Elf_Swxword
1283 const unsigned char*,
1284 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1285 big_endian>::Reloc& reloc) const
1286 { return reloc.get_r_addend(); }
1289 // Utilities for manipulating integers of up to 32-bits
1293 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1294 // an int32_t. NO_BITS must be between 1 to 32.
1295 template<int no_bits>
1296 static inline int32_t
1297 sign_extend(uint32_t bits)
1299 gold_assert(no_bits >= 0 && no_bits <= 32);
1301 return static_cast<int32_t>(bits);
1302 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1304 uint32_t top_bit = 1U << (no_bits - 1);
1305 int32_t as_signed = static_cast<int32_t>(bits);
1306 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1309 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1310 template<int no_bits>
1312 has_overflow(uint32_t bits)
1314 gold_assert(no_bits >= 0 && no_bits <= 32);
1317 int32_t max = (1 << (no_bits - 1)) - 1;
1318 int32_t min = -(1 << (no_bits - 1));
1319 int32_t as_signed = static_cast<int32_t>(bits);
1320 return as_signed > max || as_signed < min;
1323 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1324 // fits in the given number of bits as either a signed or unsigned value.
1325 // For example, has_signed_unsigned_overflow<8> would check
1326 // -128 <= bits <= 255
1327 template<int no_bits>
1329 has_signed_unsigned_overflow(uint32_t bits)
1331 gold_assert(no_bits >= 2 && no_bits <= 32);
1334 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1335 int32_t min = -(1 << (no_bits - 1));
1336 int32_t as_signed = static_cast<int32_t>(bits);
1337 return as_signed > max || as_signed < min;
1340 // Select bits from A and B using bits in MASK. For each n in [0..31],
1341 // the n-th bit in the result is chosen from the n-th bits of A and B.
1342 // A zero selects A and a one selects B.
1343 static inline uint32_t
1344 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1345 { return (a & ~mask) | (b & mask); }
1348 template<bool big_endian>
1349 class Target_arm : public Sized_target<32, big_endian>
1352 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1355 // When were are relocating a stub, we pass this as the relocation number.
1356 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1359 : Sized_target<32, big_endian>(&arm_info),
1360 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1361 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1362 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1363 should_force_pic_veneer_(false), arm_input_section_map_(),
1364 attributes_section_data_(NULL)
1367 // Whether we can use BLX.
1370 { return this->may_use_blx_; }
1372 // Set use-BLX flag.
1374 set_may_use_blx(bool value)
1375 { this->may_use_blx_ = value; }
1377 // Whether we force PCI branch veneers.
1379 should_force_pic_veneer() const
1380 { return this->should_force_pic_veneer_; }
1382 // Set PIC veneer flag.
1384 set_should_force_pic_veneer(bool value)
1385 { this->should_force_pic_veneer_ = value; }
1387 // Whether we use THUMB-2 instructions.
1389 using_thumb2() const
1391 Object_attribute* attr =
1392 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1393 int arch = attr->int_value();
1394 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
1397 // Whether we use THUMB/THUMB-2 instructions only.
1399 using_thumb_only() const
1401 Object_attribute* attr =
1402 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1403 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1404 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1406 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1407 return attr->int_value() == 'M';
1410 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1412 may_use_arm_nop() const
1414 Object_attribute* attr =
1415 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1416 int arch = attr->int_value();
1417 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1418 || arch == elfcpp::TAG_CPU_ARCH_V6K
1419 || arch == elfcpp::TAG_CPU_ARCH_V7
1420 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1423 // Whether we have THUMB-2 NOP.W instruction.
1425 may_use_thumb2_nop() const
1427 Object_attribute* attr =
1428 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1429 int arch = attr->int_value();
1430 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1431 || arch == elfcpp::TAG_CPU_ARCH_V7
1432 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1435 // Process the relocations to determine unreferenced sections for
1436 // garbage collection.
1438 gc_process_relocs(Symbol_table* symtab,
1440 Sized_relobj<32, big_endian>* object,
1441 unsigned int data_shndx,
1442 unsigned int sh_type,
1443 const unsigned char* prelocs,
1445 Output_section* output_section,
1446 bool needs_special_offset_handling,
1447 size_t local_symbol_count,
1448 const unsigned char* plocal_symbols);
1450 // Scan the relocations to look for symbol adjustments.
1452 scan_relocs(Symbol_table* symtab,
1454 Sized_relobj<32, big_endian>* object,
1455 unsigned int data_shndx,
1456 unsigned int sh_type,
1457 const unsigned char* prelocs,
1459 Output_section* output_section,
1460 bool needs_special_offset_handling,
1461 size_t local_symbol_count,
1462 const unsigned char* plocal_symbols);
1464 // Finalize the sections.
1466 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1468 // Return the value to use for a dynamic symbol which requires special
1471 do_dynsym_value(const Symbol*) const;
1473 // Relocate a section.
1475 relocate_section(const Relocate_info<32, big_endian>*,
1476 unsigned int sh_type,
1477 const unsigned char* prelocs,
1479 Output_section* output_section,
1480 bool needs_special_offset_handling,
1481 unsigned char* view,
1482 Arm_address view_address,
1483 section_size_type view_size,
1484 const Reloc_symbol_changes*);
1486 // Scan the relocs during a relocatable link.
1488 scan_relocatable_relocs(Symbol_table* symtab,
1490 Sized_relobj<32, big_endian>* object,
1491 unsigned int data_shndx,
1492 unsigned int sh_type,
1493 const unsigned char* prelocs,
1495 Output_section* output_section,
1496 bool needs_special_offset_handling,
1497 size_t local_symbol_count,
1498 const unsigned char* plocal_symbols,
1499 Relocatable_relocs*);
1501 // Relocate a section during a relocatable link.
1503 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1504 unsigned int sh_type,
1505 const unsigned char* prelocs,
1507 Output_section* output_section,
1508 off_t offset_in_output_section,
1509 const Relocatable_relocs*,
1510 unsigned char* view,
1511 Arm_address view_address,
1512 section_size_type view_size,
1513 unsigned char* reloc_view,
1514 section_size_type reloc_view_size);
1516 // Return whether SYM is defined by the ABI.
1518 do_is_defined_by_abi(Symbol* sym) const
1519 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1521 // Return the size of the GOT section.
1525 gold_assert(this->got_ != NULL);
1526 return this->got_->data_size();
1529 // Map platform-specific reloc types
1531 get_real_reloc_type (unsigned int r_type);
1534 // Methods to support stub-generations.
1537 // Return the stub factory
1539 stub_factory() const
1540 { return this->stub_factory_; }
1542 // Make a new Arm_input_section object.
1543 Arm_input_section<big_endian>*
1544 new_arm_input_section(Relobj*, unsigned int);
1546 // Find the Arm_input_section object corresponding to the SHNDX-th input
1547 // section of RELOBJ.
1548 Arm_input_section<big_endian>*
1549 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
1551 // Make a new Stub_table
1552 Stub_table<big_endian>*
1553 new_stub_table(Arm_input_section<big_endian>*);
1555 // Scan a section for stub generation.
1557 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
1558 const unsigned char*, size_t, Output_section*,
1559 bool, const unsigned char*, Arm_address,
1564 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1565 Output_section*, unsigned char*, Arm_address,
1568 // Get the default ARM target.
1569 static Target_arm<big_endian>*
1572 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1573 && parameters->target().is_big_endian() == big_endian);
1574 return static_cast<Target_arm<big_endian>*>(
1575 parameters->sized_target<32, big_endian>());
1578 // Whether relocation type uses LSB to distinguish THUMB addresses.
1580 reloc_uses_thumb_bit(unsigned int r_type);
1583 // Make an ELF object.
1585 do_make_elf_object(const std::string&, Input_file*, off_t,
1586 const elfcpp::Ehdr<32, big_endian>& ehdr);
1589 do_make_elf_object(const std::string&, Input_file*, off_t,
1590 const elfcpp::Ehdr<32, !big_endian>&)
1591 { gold_unreachable(); }
1594 do_make_elf_object(const std::string&, Input_file*, off_t,
1595 const elfcpp::Ehdr<64, false>&)
1596 { gold_unreachable(); }
1599 do_make_elf_object(const std::string&, Input_file*, off_t,
1600 const elfcpp::Ehdr<64, true>&)
1601 { gold_unreachable(); }
1603 // Make an output section.
1605 do_make_output_section(const char* name, elfcpp::Elf_Word type,
1606 elfcpp::Elf_Xword flags)
1607 { return new Arm_output_section<big_endian>(name, type, flags); }
1610 do_adjust_elf_header(unsigned char* view, int len) const;
1612 // We only need to generate stubs, and hence perform relaxation if we are
1613 // not doing relocatable linking.
1615 do_may_relax() const
1616 { return !parameters->options().relocatable(); }
1619 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
1621 // Determine whether an object attribute tag takes an integer, a
1624 do_attribute_arg_type(int tag) const;
1626 // Reorder tags during output.
1628 do_attributes_order(int num) const;
1631 // The class which scans relocations.
1636 : issued_non_pic_error_(false)
1640 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1641 Sized_relobj<32, big_endian>* object,
1642 unsigned int data_shndx,
1643 Output_section* output_section,
1644 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1645 const elfcpp::Sym<32, big_endian>& lsym);
1648 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1649 Sized_relobj<32, big_endian>* object,
1650 unsigned int data_shndx,
1651 Output_section* output_section,
1652 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1657 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1658 unsigned int r_type);
1661 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1662 unsigned int r_type, Symbol*);
1665 check_non_pic(Relobj*, unsigned int r_type);
1667 // Almost identical to Symbol::needs_plt_entry except that it also
1668 // handles STT_ARM_TFUNC.
1670 symbol_needs_plt_entry(const Symbol* sym)
1672 // An undefined symbol from an executable does not need a PLT entry.
1673 if (sym->is_undefined() && !parameters->options().shared())
1676 return (!parameters->doing_static_link()
1677 && (sym->type() == elfcpp::STT_FUNC
1678 || sym->type() == elfcpp::STT_ARM_TFUNC)
1679 && (sym->is_from_dynobj()
1680 || sym->is_undefined()
1681 || sym->is_preemptible()));
1684 // Whether we have issued an error about a non-PIC compilation.
1685 bool issued_non_pic_error_;
1688 // The class which implements relocation.
1698 // Return whether the static relocation needs to be applied.
1700 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1703 Output_section* output_section);
1705 // Do a relocation. Return false if the caller should not issue
1706 // any warnings about this relocation.
1708 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1709 Output_section*, size_t relnum,
1710 const elfcpp::Rel<32, big_endian>&,
1711 unsigned int r_type, const Sized_symbol<32>*,
1712 const Symbol_value<32>*,
1713 unsigned char*, Arm_address,
1716 // Return whether we want to pass flag NON_PIC_REF for this
1717 // reloc. This means the relocation type accesses a symbol not via
1720 reloc_is_non_pic (unsigned int r_type)
1724 // These relocation types reference GOT or PLT entries explicitly.
1725 case elfcpp::R_ARM_GOT_BREL:
1726 case elfcpp::R_ARM_GOT_ABS:
1727 case elfcpp::R_ARM_GOT_PREL:
1728 case elfcpp::R_ARM_GOT_BREL12:
1729 case elfcpp::R_ARM_PLT32_ABS:
1730 case elfcpp::R_ARM_TLS_GD32:
1731 case elfcpp::R_ARM_TLS_LDM32:
1732 case elfcpp::R_ARM_TLS_IE32:
1733 case elfcpp::R_ARM_TLS_IE12GP:
1735 // These relocate types may use PLT entries.
1736 case elfcpp::R_ARM_CALL:
1737 case elfcpp::R_ARM_THM_CALL:
1738 case elfcpp::R_ARM_JUMP24:
1739 case elfcpp::R_ARM_THM_JUMP24:
1740 case elfcpp::R_ARM_THM_JUMP19:
1741 case elfcpp::R_ARM_PLT32:
1742 case elfcpp::R_ARM_THM_XPC22:
1751 // A class which returns the size required for a relocation type,
1752 // used while scanning relocs during a relocatable link.
1753 class Relocatable_size_for_reloc
1757 get_size_for_reloc(unsigned int, Relobj*);
1760 // Get the GOT section, creating it if necessary.
1761 Output_data_got<32, big_endian>*
1762 got_section(Symbol_table*, Layout*);
1764 // Get the GOT PLT section.
1766 got_plt_section() const
1768 gold_assert(this->got_plt_ != NULL);
1769 return this->got_plt_;
1772 // Create a PLT entry for a global symbol.
1774 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1776 // Get the PLT section.
1777 const Output_data_plt_arm<big_endian>*
1780 gold_assert(this->plt_ != NULL);
1784 // Get the dynamic reloc section, creating it if necessary.
1786 rel_dyn_section(Layout*);
1788 // Return true if the symbol may need a COPY relocation.
1789 // References from an executable object to non-function symbols
1790 // defined in a dynamic object may need a COPY relocation.
1792 may_need_copy_reloc(Symbol* gsym)
1794 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1795 && gsym->may_need_copy_reloc());
1798 // Add a potential copy relocation.
1800 copy_reloc(Symbol_table* symtab, Layout* layout,
1801 Sized_relobj<32, big_endian>* object,
1802 unsigned int shndx, Output_section* output_section,
1803 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1805 this->copy_relocs_.copy_reloc(symtab, layout,
1806 symtab->get_sized_symbol<32>(sym),
1807 object, shndx, output_section, reloc,
1808 this->rel_dyn_section(layout));
1811 // Whether two EABI versions are compatible.
1813 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1815 // Merge processor-specific flags from input object and those in the ELF
1816 // header of the output.
1818 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1820 // Get the secondary compatible architecture.
1822 get_secondary_compatible_arch(const Attributes_section_data*);
1824 // Set the secondary compatible architecture.
1826 set_secondary_compatible_arch(Attributes_section_data*, int);
1829 tag_cpu_arch_combine(const char*, int, int*, int, int);
1831 // Helper to print AEABI enum tag value.
1833 aeabi_enum_name(unsigned int);
1835 // Return string value for TAG_CPU_name.
1837 tag_cpu_name_value(unsigned int);
1839 // Merge object attributes from input object and those in the output.
1841 merge_object_attributes(const char*, const Attributes_section_data*);
1843 // Helper to get an AEABI object attribute
1845 get_aeabi_object_attribute(int tag) const
1847 Attributes_section_data* pasd = this->attributes_section_data_;
1848 gold_assert(pasd != NULL);
1849 Object_attribute* attr =
1850 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
1851 gold_assert(attr != NULL);
1856 // Methods to support stub-generations.
1859 // Group input sections for stub generation.
1861 group_sections(Layout*, section_size_type, bool);
1863 // Scan a relocation for stub generation.
1865 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
1866 const Sized_symbol<32>*, unsigned int,
1867 const Symbol_value<32>*,
1868 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
1870 // Scan a relocation section for stub.
1871 template<int sh_type>
1873 scan_reloc_section_for_stubs(
1874 const Relocate_info<32, big_endian>* relinfo,
1875 const unsigned char* prelocs,
1877 Output_section* output_section,
1878 bool needs_special_offset_handling,
1879 const unsigned char* view,
1880 elfcpp::Elf_types<32>::Elf_Addr view_address,
1883 // Information about this specific target which we pass to the
1884 // general Target structure.
1885 static const Target::Target_info arm_info;
1887 // The types of GOT entries needed for this platform.
1890 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1893 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
1895 // Map input section to Arm_input_section.
1896 typedef Unordered_map<Input_section_specifier,
1897 Arm_input_section<big_endian>*,
1898 Input_section_specifier::hash,
1899 Input_section_specifier::equal_to>
1900 Arm_input_section_map;
1903 Output_data_got<32, big_endian>* got_;
1905 Output_data_plt_arm<big_endian>* plt_;
1906 // The GOT PLT section.
1907 Output_data_space* got_plt_;
1908 // The dynamic reloc section.
1909 Reloc_section* rel_dyn_;
1910 // Relocs saved to avoid a COPY reloc.
1911 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1912 // Space for variables copied with a COPY reloc.
1913 Output_data_space* dynbss_;
1914 // Vector of Stub_tables created.
1915 Stub_table_list stub_tables_;
1917 const Stub_factory &stub_factory_;
1918 // Whether we can use BLX.
1920 // Whether we force PIC branch veneers.
1921 bool should_force_pic_veneer_;
1922 // Map for locating Arm_input_sections.
1923 Arm_input_section_map arm_input_section_map_;
1924 // Attributes section data in output.
1925 Attributes_section_data* attributes_section_data_;
1928 template<bool big_endian>
1929 const Target::Target_info Target_arm<big_endian>::arm_info =
1932 big_endian, // is_big_endian
1933 elfcpp::EM_ARM, // machine_code
1934 false, // has_make_symbol
1935 false, // has_resolve
1936 false, // has_code_fill
1937 true, // is_default_stack_executable
1939 "/usr/lib/libc.so.1", // dynamic_linker
1940 0x8000, // default_text_segment_address
1941 0x1000, // abi_pagesize (overridable by -z max-page-size)
1942 0x1000, // common_pagesize (overridable by -z common-page-size)
1943 elfcpp::SHN_UNDEF, // small_common_shndx
1944 elfcpp::SHN_UNDEF, // large_common_shndx
1945 0, // small_common_section_flags
1946 0, // large_common_section_flags
1947 ".ARM.attributes", // attributes_section
1948 "aeabi" // attributes_vendor
1951 // Arm relocate functions class
1954 template<bool big_endian>
1955 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1960 STATUS_OKAY, // No error during relocation.
1961 STATUS_OVERFLOW, // Relocation oveflow.
1962 STATUS_BAD_RELOC // Relocation cannot be applied.
1966 typedef Relocate_functions<32, big_endian> Base;
1967 typedef Arm_relocate_functions<big_endian> This;
1969 // Encoding of imm16 argument for movt and movw ARM instructions
1972 // imm16 := imm4 | imm12
1974 // 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
1975 // +-------+---------------+-------+-------+-----------------------+
1976 // | | |imm4 | |imm12 |
1977 // +-------+---------------+-------+-------+-----------------------+
1979 // Extract the relocation addend from VAL based on the ARM
1980 // instruction encoding described above.
1981 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1982 extract_arm_movw_movt_addend(
1983 typename elfcpp::Swap<32, big_endian>::Valtype val)
1985 // According to the Elf ABI for ARM Architecture the immediate
1986 // field is sign-extended to form the addend.
1987 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1990 // Insert X into VAL based on the ARM instruction encoding described
1992 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1993 insert_val_arm_movw_movt(
1994 typename elfcpp::Swap<32, big_endian>::Valtype val,
1995 typename elfcpp::Swap<32, big_endian>::Valtype x)
1999 val |= (x & 0xf000) << 4;
2003 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2006 // imm16 := imm4 | i | imm3 | imm8
2008 // 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
2009 // +---------+-+-----------+-------++-+-----+-------+---------------+
2010 // | |i| |imm4 || |imm3 | |imm8 |
2011 // +---------+-+-----------+-------++-+-----+-------+---------------+
2013 // Extract the relocation addend from VAL based on the Thumb2
2014 // instruction encoding described above.
2015 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2016 extract_thumb_movw_movt_addend(
2017 typename elfcpp::Swap<32, big_endian>::Valtype val)
2019 // According to the Elf ABI for ARM Architecture the immediate
2020 // field is sign-extended to form the addend.
2021 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2022 | ((val >> 15) & 0x0800)
2023 | ((val >> 4) & 0x0700)
2027 // Insert X into VAL based on the Thumb2 instruction encoding
2029 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2030 insert_val_thumb_movw_movt(
2031 typename elfcpp::Swap<32, big_endian>::Valtype val,
2032 typename elfcpp::Swap<32, big_endian>::Valtype x)
2035 val |= (x & 0xf000) << 4;
2036 val |= (x & 0x0800) << 15;
2037 val |= (x & 0x0700) << 4;
2038 val |= (x & 0x00ff);
2042 // Handle ARM long branches.
2043 static typename This::Status
2044 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2045 unsigned char *, const Sized_symbol<32>*,
2046 const Arm_relobj<big_endian>*, unsigned int,
2047 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2049 // Handle THUMB long branches.
2050 static typename This::Status
2051 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2052 unsigned char *, const Sized_symbol<32>*,
2053 const Arm_relobj<big_endian>*, unsigned int,
2054 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2058 // Return the branch offset of a 32-bit THUMB branch.
2059 static inline int32_t
2060 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2062 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2063 // involving the J1 and J2 bits.
2064 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2065 uint32_t upper = upper_insn & 0x3ffU;
2066 uint32_t lower = lower_insn & 0x7ffU;
2067 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2068 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2069 uint32_t i1 = j1 ^ s ? 0 : 1;
2070 uint32_t i2 = j2 ^ s ? 0 : 1;
2072 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2073 | (upper << 12) | (lower << 1));
2076 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2077 // UPPER_INSN is the original upper instruction of the branch. Caller is
2078 // responsible for overflow checking and BLX offset adjustment.
2079 static inline uint16_t
2080 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2082 uint32_t s = offset < 0 ? 1 : 0;
2083 uint32_t bits = static_cast<uint32_t>(offset);
2084 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2087 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2088 // LOWER_INSN is the original lower instruction of the branch. Caller is
2089 // responsible for overflow checking and BLX offset adjustment.
2090 static inline uint16_t
2091 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2093 uint32_t s = offset < 0 ? 1 : 0;
2094 uint32_t bits = static_cast<uint32_t>(offset);
2095 return ((lower_insn & ~0x2fffU)
2096 | ((((bits >> 23) & 1) ^ !s) << 13)
2097 | ((((bits >> 22) & 1) ^ !s) << 11)
2098 | ((bits >> 1) & 0x7ffU));
2101 // Return the branch offset of a 32-bit THUMB conditional branch.
2102 static inline int32_t
2103 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2105 uint32_t s = (upper_insn & 0x0400U) >> 10;
2106 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2107 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2108 uint32_t lower = (lower_insn & 0x07ffU);
2109 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2111 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2114 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2115 // instruction. UPPER_INSN is the original upper instruction of the branch.
2116 // Caller is responsible for overflow checking.
2117 static inline uint16_t
2118 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2120 uint32_t s = offset < 0 ? 1 : 0;
2121 uint32_t bits = static_cast<uint32_t>(offset);
2122 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2125 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2126 // instruction. LOWER_INSN is the original lower instruction of the branch.
2127 // Caller is reponsible for overflow checking.
2128 static inline uint16_t
2129 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2131 uint32_t bits = static_cast<uint32_t>(offset);
2132 uint32_t j2 = (bits & 0x00080000U) >> 19;
2133 uint32_t j1 = (bits & 0x00040000U) >> 18;
2134 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2136 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2139 // R_ARM_ABS8: S + A
2140 static inline typename This::Status
2141 abs8(unsigned char *view,
2142 const Sized_relobj<32, big_endian>* object,
2143 const Symbol_value<32>* psymval)
2145 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2146 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2147 Valtype* wv = reinterpret_cast<Valtype*>(view);
2148 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2149 Reltype addend = utils::sign_extend<8>(val);
2150 Reltype x = psymval->value(object, addend);
2151 val = utils::bit_select(val, x, 0xffU);
2152 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2153 return (utils::has_signed_unsigned_overflow<8>(x)
2154 ? This::STATUS_OVERFLOW
2155 : This::STATUS_OKAY);
2158 // R_ARM_THM_ABS5: S + A
2159 static inline typename This::Status
2160 thm_abs5(unsigned char *view,
2161 const Sized_relobj<32, big_endian>* object,
2162 const Symbol_value<32>* psymval)
2164 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2165 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2166 Valtype* wv = reinterpret_cast<Valtype*>(view);
2167 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2168 Reltype addend = (val & 0x7e0U) >> 6;
2169 Reltype x = psymval->value(object, addend);
2170 val = utils::bit_select(val, x << 6, 0x7e0U);
2171 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2172 return (utils::has_overflow<5>(x)
2173 ? This::STATUS_OVERFLOW
2174 : This::STATUS_OKAY);
2177 // R_ARM_ABS12: S + A
2178 static inline typename This::Status
2179 abs12(unsigned char *view,
2180 const Sized_relobj<32, big_endian>* object,
2181 const Symbol_value<32>* psymval)
2183 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2184 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2185 Valtype* wv = reinterpret_cast<Valtype*>(view);
2186 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2187 Reltype addend = val & 0x0fffU;
2188 Reltype x = psymval->value(object, addend);
2189 val = utils::bit_select(val, x, 0x0fffU);
2190 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2191 return (utils::has_overflow<12>(x)
2192 ? This::STATUS_OVERFLOW
2193 : This::STATUS_OKAY);
2196 // R_ARM_ABS16: S + A
2197 static inline typename This::Status
2198 abs16(unsigned char *view,
2199 const Sized_relobj<32, big_endian>* object,
2200 const Symbol_value<32>* psymval)
2202 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2203 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2204 Valtype* wv = reinterpret_cast<Valtype*>(view);
2205 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2206 Reltype addend = utils::sign_extend<16>(val);
2207 Reltype x = psymval->value(object, addend);
2208 val = utils::bit_select(val, x, 0xffffU);
2209 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2210 return (utils::has_signed_unsigned_overflow<16>(x)
2211 ? This::STATUS_OVERFLOW
2212 : This::STATUS_OKAY);
2215 // R_ARM_ABS32: (S + A) | T
2216 static inline typename This::Status
2217 abs32(unsigned char *view,
2218 const Sized_relobj<32, big_endian>* object,
2219 const Symbol_value<32>* psymval,
2220 Arm_address thumb_bit)
2222 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2223 Valtype* wv = reinterpret_cast<Valtype*>(view);
2224 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2225 Valtype x = psymval->value(object, addend) | thumb_bit;
2226 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2227 return This::STATUS_OKAY;
2230 // R_ARM_REL32: (S + A) | T - P
2231 static inline typename This::Status
2232 rel32(unsigned char *view,
2233 const Sized_relobj<32, big_endian>* object,
2234 const Symbol_value<32>* psymval,
2235 Arm_address address,
2236 Arm_address thumb_bit)
2238 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2239 Valtype* wv = reinterpret_cast<Valtype*>(view);
2240 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2241 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2242 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2243 return This::STATUS_OKAY;
2246 // R_ARM_THM_CALL: (S + A) | T - P
2247 static inline typename This::Status
2248 thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2249 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2250 unsigned int r_sym, const Symbol_value<32>* psymval,
2251 Arm_address address, Arm_address thumb_bit,
2252 bool is_weakly_undefined_without_plt)
2254 return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
2255 object, r_sym, psymval, address, thumb_bit,
2256 is_weakly_undefined_without_plt);
2259 // R_ARM_THM_JUMP24: (S + A) | T - P
2260 static inline typename This::Status
2261 thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2262 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2263 unsigned int r_sym, const Symbol_value<32>* psymval,
2264 Arm_address address, Arm_address thumb_bit,
2265 bool is_weakly_undefined_without_plt)
2267 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
2268 object, r_sym, psymval, address, thumb_bit,
2269 is_weakly_undefined_without_plt);
2272 // R_ARM_THM_JUMP24: (S + A) | T - P
2273 static typename This::Status
2274 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
2275 const Symbol_value<32>* psymval, Arm_address address,
2276 Arm_address thumb_bit);
2278 // R_ARM_THM_XPC22: (S + A) | T - P
2279 static inline typename This::Status
2280 thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2281 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2282 unsigned int r_sym, const Symbol_value<32>* psymval,
2283 Arm_address address, Arm_address thumb_bit,
2284 bool is_weakly_undefined_without_plt)
2286 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
2287 object, r_sym, psymval, address, thumb_bit,
2288 is_weakly_undefined_without_plt);
2291 // R_ARM_BASE_PREL: B(S) + A - P
2292 static inline typename This::Status
2293 base_prel(unsigned char* view,
2295 Arm_address address)
2297 Base::rel32(view, origin - address);
2301 // R_ARM_BASE_ABS: B(S) + A
2302 static inline typename This::Status
2303 base_abs(unsigned char* view,
2306 Base::rel32(view, origin);
2310 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2311 static inline typename This::Status
2312 got_brel(unsigned char* view,
2313 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
2315 Base::rel32(view, got_offset);
2316 return This::STATUS_OKAY;
2319 // R_ARM_GOT_PREL: GOT(S) + A - P
2320 static inline typename This::Status
2321 got_prel(unsigned char *view,
2322 Arm_address got_entry,
2323 Arm_address address)
2325 Base::rel32(view, got_entry - address);
2326 return This::STATUS_OKAY;
2329 // R_ARM_PLT32: (S + A) | T - P
2330 static inline typename This::Status
2331 plt32(const Relocate_info<32, big_endian>* relinfo,
2332 unsigned char *view,
2333 const Sized_symbol<32>* gsym,
2334 const Arm_relobj<big_endian>* object,
2336 const Symbol_value<32>* psymval,
2337 Arm_address address,
2338 Arm_address thumb_bit,
2339 bool is_weakly_undefined_without_plt)
2341 return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
2342 object, r_sym, psymval, address, thumb_bit,
2343 is_weakly_undefined_without_plt);
2346 // R_ARM_XPC25: (S + A) | T - P
2347 static inline typename This::Status
2348 xpc25(const Relocate_info<32, big_endian>* relinfo,
2349 unsigned char *view,
2350 const Sized_symbol<32>* gsym,
2351 const Arm_relobj<big_endian>* object,
2353 const Symbol_value<32>* psymval,
2354 Arm_address address,
2355 Arm_address thumb_bit,
2356 bool is_weakly_undefined_without_plt)
2358 return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
2359 object, r_sym, psymval, address, thumb_bit,
2360 is_weakly_undefined_without_plt);
2363 // R_ARM_CALL: (S + A) | T - P
2364 static inline typename This::Status
2365 call(const Relocate_info<32, big_endian>* relinfo,
2366 unsigned char *view,
2367 const Sized_symbol<32>* gsym,
2368 const Arm_relobj<big_endian>* object,
2370 const Symbol_value<32>* psymval,
2371 Arm_address address,
2372 Arm_address thumb_bit,
2373 bool is_weakly_undefined_without_plt)
2375 return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2376 object, r_sym, psymval, address, thumb_bit,
2377 is_weakly_undefined_without_plt);
2380 // R_ARM_JUMP24: (S + A) | T - P
2381 static inline typename This::Status
2382 jump24(const Relocate_info<32, big_endian>* relinfo,
2383 unsigned char *view,
2384 const Sized_symbol<32>* gsym,
2385 const Arm_relobj<big_endian>* object,
2387 const Symbol_value<32>* psymval,
2388 Arm_address address,
2389 Arm_address thumb_bit,
2390 bool is_weakly_undefined_without_plt)
2392 return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2393 object, r_sym, psymval, address, thumb_bit,
2394 is_weakly_undefined_without_plt);
2397 // R_ARM_PREL: (S + A) | T - P
2398 static inline typename This::Status
2399 prel31(unsigned char *view,
2400 const Sized_relobj<32, big_endian>* object,
2401 const Symbol_value<32>* psymval,
2402 Arm_address address,
2403 Arm_address thumb_bit)
2405 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2406 Valtype* wv = reinterpret_cast<Valtype*>(view);
2407 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2408 Valtype addend = utils::sign_extend<31>(val);
2409 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2410 val = utils::bit_select(val, x, 0x7fffffffU);
2411 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2412 return (utils::has_overflow<31>(x) ?
2413 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2416 // R_ARM_MOVW_ABS_NC: (S + A) | T
2417 static inline typename This::Status
2418 movw_abs_nc(unsigned char *view,
2419 const Sized_relobj<32, big_endian>* object,
2420 const Symbol_value<32>* psymval,
2421 Arm_address thumb_bit)
2423 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2424 Valtype* wv = reinterpret_cast<Valtype*>(view);
2425 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2426 Valtype addend = This::extract_arm_movw_movt_addend(val);
2427 Valtype x = psymval->value(object, addend) | thumb_bit;
2428 val = This::insert_val_arm_movw_movt(val, x);
2429 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2430 return This::STATUS_OKAY;
2433 // R_ARM_MOVT_ABS: S + A
2434 static inline typename This::Status
2435 movt_abs(unsigned char *view,
2436 const Sized_relobj<32, big_endian>* object,
2437 const Symbol_value<32>* psymval)
2439 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2440 Valtype* wv = reinterpret_cast<Valtype*>(view);
2441 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2442 Valtype addend = This::extract_arm_movw_movt_addend(val);
2443 Valtype x = psymval->value(object, addend) >> 16;
2444 val = This::insert_val_arm_movw_movt(val, x);
2445 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2446 return This::STATUS_OKAY;
2449 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2450 static inline typename This::Status
2451 thm_movw_abs_nc(unsigned char *view,
2452 const Sized_relobj<32, big_endian>* object,
2453 const Symbol_value<32>* psymval,
2454 Arm_address thumb_bit)
2456 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2457 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2458 Valtype* wv = reinterpret_cast<Valtype*>(view);
2459 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2460 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2461 Reltype addend = extract_thumb_movw_movt_addend(val);
2462 Reltype x = psymval->value(object, addend) | thumb_bit;
2463 val = This::insert_val_thumb_movw_movt(val, x);
2464 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2465 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2466 return This::STATUS_OKAY;
2469 // R_ARM_THM_MOVT_ABS: S + A
2470 static inline typename This::Status
2471 thm_movt_abs(unsigned char *view,
2472 const Sized_relobj<32, big_endian>* object,
2473 const Symbol_value<32>* psymval)
2475 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2476 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2477 Valtype* wv = reinterpret_cast<Valtype*>(view);
2478 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2479 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2480 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2481 Reltype x = psymval->value(object, addend) >> 16;
2482 val = This::insert_val_thumb_movw_movt(val, x);
2483 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2484 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2485 return This::STATUS_OKAY;
2488 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2489 static inline typename This::Status
2490 movw_prel_nc(unsigned char *view,
2491 const Sized_relobj<32, big_endian>* object,
2492 const Symbol_value<32>* psymval,
2493 Arm_address address,
2494 Arm_address thumb_bit)
2496 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2497 Valtype* wv = reinterpret_cast<Valtype*>(view);
2498 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2499 Valtype addend = This::extract_arm_movw_movt_addend(val);
2500 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2501 val = This::insert_val_arm_movw_movt(val, x);
2502 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2503 return This::STATUS_OKAY;
2506 // R_ARM_MOVT_PREL: S + A - P
2507 static inline typename This::Status
2508 movt_prel(unsigned char *view,
2509 const Sized_relobj<32, big_endian>* object,
2510 const Symbol_value<32>* psymval,
2511 Arm_address address)
2513 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2514 Valtype* wv = reinterpret_cast<Valtype*>(view);
2515 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2516 Valtype addend = This::extract_arm_movw_movt_addend(val);
2517 Valtype x = (psymval->value(object, addend) - address) >> 16;
2518 val = This::insert_val_arm_movw_movt(val, x);
2519 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2520 return This::STATUS_OKAY;
2523 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2524 static inline typename This::Status
2525 thm_movw_prel_nc(unsigned char *view,
2526 const Sized_relobj<32, big_endian>* object,
2527 const Symbol_value<32>* psymval,
2528 Arm_address address,
2529 Arm_address thumb_bit)
2531 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2532 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2533 Valtype* wv = reinterpret_cast<Valtype*>(view);
2534 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2535 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2536 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2537 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
2538 val = This::insert_val_thumb_movw_movt(val, x);
2539 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2540 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2541 return This::STATUS_OKAY;
2544 // R_ARM_THM_MOVT_PREL: S + A - P
2545 static inline typename This::Status
2546 thm_movt_prel(unsigned char *view,
2547 const Sized_relobj<32, big_endian>* object,
2548 const Symbol_value<32>* psymval,
2549 Arm_address address)
2551 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2552 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2553 Valtype* wv = reinterpret_cast<Valtype*>(view);
2554 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2555 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2556 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2557 Reltype x = (psymval->value(object, addend) - address) >> 16;
2558 val = This::insert_val_thumb_movw_movt(val, x);
2559 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2560 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2561 return This::STATUS_OKAY;
2565 // Relocate ARM long branches. This handles relocation types
2566 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2567 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2568 // undefined and we do not use PLT in this relocation. In such a case,
2569 // the branch is converted into an NOP.
2571 template<bool big_endian>
2572 typename Arm_relocate_functions<big_endian>::Status
2573 Arm_relocate_functions<big_endian>::arm_branch_common(
2574 unsigned int r_type,
2575 const Relocate_info<32, big_endian>* relinfo,
2576 unsigned char *view,
2577 const Sized_symbol<32>* gsym,
2578 const Arm_relobj<big_endian>* object,
2580 const Symbol_value<32>* psymval,
2581 Arm_address address,
2582 Arm_address thumb_bit,
2583 bool is_weakly_undefined_without_plt)
2585 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2586 Valtype* wv = reinterpret_cast<Valtype*>(view);
2587 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2589 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
2590 && ((val & 0x0f000000UL) == 0x0a000000UL);
2591 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
2592 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
2593 && ((val & 0x0f000000UL) == 0x0b000000UL);
2594 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
2595 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
2597 // Check that the instruction is valid.
2598 if (r_type == elfcpp::R_ARM_CALL)
2600 if (!insn_is_uncond_bl && !insn_is_blx)
2601 return This::STATUS_BAD_RELOC;
2603 else if (r_type == elfcpp::R_ARM_JUMP24)
2605 if (!insn_is_b && !insn_is_cond_bl)
2606 return This::STATUS_BAD_RELOC;
2608 else if (r_type == elfcpp::R_ARM_PLT32)
2610 if (!insn_is_any_branch)
2611 return This::STATUS_BAD_RELOC;
2613 else if (r_type == elfcpp::R_ARM_XPC25)
2615 // FIXME: AAELF document IH0044C does not say much about it other
2616 // than it being obsolete.
2617 if (!insn_is_any_branch)
2618 return This::STATUS_BAD_RELOC;
2623 // A branch to an undefined weak symbol is turned into a jump to
2624 // the next instruction unless a PLT entry will be created.
2625 // Do the same for local undefined symbols.
2626 // The jump to the next instruction is optimized as a NOP depending
2627 // on the architecture.
2628 const Target_arm<big_endian>* arm_target =
2629 Target_arm<big_endian>::default_target();
2630 if (is_weakly_undefined_without_plt)
2632 Valtype cond = val & 0xf0000000U;
2633 if (arm_target->may_use_arm_nop())
2634 val = cond | 0x0320f000;
2636 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2637 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2638 return This::STATUS_OKAY;
2641 Valtype addend = utils::sign_extend<26>(val << 2);
2642 Valtype branch_target = psymval->value(object, addend);
2643 int32_t branch_offset = branch_target - address;
2645 // We need a stub if the branch offset is too large or if we need
2647 bool may_use_blx = arm_target->may_use_blx();
2648 Reloc_stub* stub = NULL;
2649 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
2650 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2651 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
2653 Stub_type stub_type =
2654 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2656 if (stub_type != arm_stub_none)
2658 Stub_table<big_endian>* stub_table =
2659 object->stub_table(relinfo->data_shndx);
2660 gold_assert(stub_table != NULL);
2662 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2663 stub = stub_table->find_reloc_stub(stub_key);
2664 gold_assert(stub != NULL);
2665 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2666 branch_target = stub_table->address() + stub->offset() + addend;
2667 branch_offset = branch_target - address;
2668 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
2669 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
2673 // At this point, if we still need to switch mode, the instruction
2674 // must either be a BLX or a BL that can be converted to a BLX.
2678 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
2679 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
2682 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
2683 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2684 return (utils::has_overflow<26>(branch_offset)
2685 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
2688 // Relocate THUMB long branches. This handles relocation types
2689 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2690 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2691 // undefined and we do not use PLT in this relocation. In such a case,
2692 // the branch is converted into an NOP.
2694 template<bool big_endian>
2695 typename Arm_relocate_functions<big_endian>::Status
2696 Arm_relocate_functions<big_endian>::thumb_branch_common(
2697 unsigned int r_type,
2698 const Relocate_info<32, big_endian>* relinfo,
2699 unsigned char *view,
2700 const Sized_symbol<32>* gsym,
2701 const Arm_relobj<big_endian>* object,
2703 const Symbol_value<32>* psymval,
2704 Arm_address address,
2705 Arm_address thumb_bit,
2706 bool is_weakly_undefined_without_plt)
2708 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2709 Valtype* wv = reinterpret_cast<Valtype*>(view);
2710 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2711 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2713 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2715 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
2716 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
2718 // Check that the instruction is valid.
2719 if (r_type == elfcpp::R_ARM_THM_CALL)
2721 if (!is_bl_insn && !is_blx_insn)
2722 return This::STATUS_BAD_RELOC;
2724 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
2726 // This cannot be a BLX.
2728 return This::STATUS_BAD_RELOC;
2730 else if (r_type == elfcpp::R_ARM_THM_XPC22)
2732 // Check for Thumb to Thumb call.
2734 return This::STATUS_BAD_RELOC;
2737 gold_warning(_("%s: Thumb BLX instruction targets "
2738 "thumb function '%s'."),
2739 object->name().c_str(),
2740 (gsym ? gsym->name() : "(local)"));
2741 // Convert BLX to BL.
2742 lower_insn |= 0x1000U;
2748 // A branch to an undefined weak symbol is turned into a jump to
2749 // the next instruction unless a PLT entry will be created.
2750 // The jump to the next instruction is optimized as a NOP.W for
2751 // Thumb-2 enabled architectures.
2752 const Target_arm<big_endian>* arm_target =
2753 Target_arm<big_endian>::default_target();
2754 if (is_weakly_undefined_without_plt)
2756 if (arm_target->may_use_thumb2_nop())
2758 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
2759 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
2763 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
2764 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
2766 return This::STATUS_OKAY;
2769 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
2770 Arm_address branch_target = psymval->value(object, addend);
2771 int32_t branch_offset = branch_target - address;
2773 // We need a stub if the branch offset is too large or if we need
2775 bool may_use_blx = arm_target->may_use_blx();
2776 bool thumb2 = arm_target->using_thumb2();
2778 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2779 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2781 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2782 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2783 || ((thumb_bit == 0)
2784 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2785 || r_type == elfcpp::R_ARM_THM_JUMP24)))
2787 Stub_type stub_type =
2788 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2790 if (stub_type != arm_stub_none)
2792 Stub_table<big_endian>* stub_table =
2793 object->stub_table(relinfo->data_shndx);
2794 gold_assert(stub_table != NULL);
2796 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2797 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
2798 gold_assert(stub != NULL);
2799 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2800 branch_target = stub_table->address() + stub->offset() + addend;
2801 branch_offset = branch_target - address;
2805 // At this point, if we still need to switch mode, the instruction
2806 // must either be a BLX or a BL that can be converted to a BLX.
2809 gold_assert(may_use_blx
2810 && (r_type == elfcpp::R_ARM_THM_CALL
2811 || r_type == elfcpp::R_ARM_THM_XPC22));
2812 // Make sure this is a BLX.
2813 lower_insn &= ~0x1000U;
2817 // Make sure this is a BL.
2818 lower_insn |= 0x1000U;
2821 if ((lower_insn & 0x5000U) == 0x4000U)
2822 // For a BLX instruction, make sure that the relocation is rounded up
2823 // to a word boundary. This follows the semantics of the instruction
2824 // which specifies that bit 1 of the target address will come from bit
2825 // 1 of the base address.
2826 branch_offset = (branch_offset + 2) & ~3;
2828 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2829 // We use the Thumb-2 encoding, which is safe even if dealing with
2830 // a Thumb-1 instruction by virtue of our overflow check above. */
2831 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
2832 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
2834 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
2835 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
2838 ? utils::has_overflow<25>(branch_offset)
2839 : utils::has_overflow<23>(branch_offset))
2840 ? This::STATUS_OVERFLOW
2841 : This::STATUS_OKAY);
2844 // Relocate THUMB-2 long conditional branches.
2845 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2846 // undefined and we do not use PLT in this relocation. In such a case,
2847 // the branch is converted into an NOP.
2849 template<bool big_endian>
2850 typename Arm_relocate_functions<big_endian>::Status
2851 Arm_relocate_functions<big_endian>::thm_jump19(
2852 unsigned char *view,
2853 const Arm_relobj<big_endian>* object,
2854 const Symbol_value<32>* psymval,
2855 Arm_address address,
2856 Arm_address thumb_bit)
2858 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2859 Valtype* wv = reinterpret_cast<Valtype*>(view);
2860 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2861 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2862 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
2864 Arm_address branch_target = psymval->value(object, addend);
2865 int32_t branch_offset = branch_target - address;
2867 // ??? Should handle interworking? GCC might someday try to
2868 // use this for tail calls.
2869 // FIXME: We do support thumb entry to PLT yet.
2872 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
2873 return This::STATUS_BAD_RELOC;
2876 // Put RELOCATION back into the insn.
2877 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
2878 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
2880 // Put the relocated value back in the object file:
2881 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
2882 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
2884 return (utils::has_overflow<21>(branch_offset)
2885 ? This::STATUS_OVERFLOW
2886 : This::STATUS_OKAY);
2889 // Get the GOT section, creating it if necessary.
2891 template<bool big_endian>
2892 Output_data_got<32, big_endian>*
2893 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2895 if (this->got_ == NULL)
2897 gold_assert(symtab != NULL && layout != NULL);
2899 this->got_ = new Output_data_got<32, big_endian>();
2902 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2904 | elfcpp::SHF_WRITE),
2905 this->got_, false, true, true,
2908 // The old GNU linker creates a .got.plt section. We just
2909 // create another set of data in the .got section. Note that we
2910 // always create a PLT if we create a GOT, although the PLT
2912 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2913 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2915 | elfcpp::SHF_WRITE),
2916 this->got_plt_, false, false,
2919 // The first three entries are reserved.
2920 this->got_plt_->set_current_data_size(3 * 4);
2922 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2923 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2924 Symbol_table::PREDEFINED,
2926 0, 0, elfcpp::STT_OBJECT,
2928 elfcpp::STV_HIDDEN, 0,
2934 // Get the dynamic reloc section, creating it if necessary.
2936 template<bool big_endian>
2937 typename Target_arm<big_endian>::Reloc_section*
2938 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2940 if (this->rel_dyn_ == NULL)
2942 gold_assert(layout != NULL);
2943 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2944 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
2945 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
2946 false, false, false);
2948 return this->rel_dyn_;
2951 // Insn_template methods.
2953 // Return byte size of an instruction template.
2956 Insn_template::size() const
2958 switch (this->type())
2961 case THUMB16_SPECIAL_TYPE:
2972 // Return alignment of an instruction template.
2975 Insn_template::alignment() const
2977 switch (this->type())
2980 case THUMB16_SPECIAL_TYPE:
2991 // Stub_template methods.
2993 Stub_template::Stub_template(
2994 Stub_type type, const Insn_template* insns,
2996 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2997 entry_in_thumb_mode_(false), relocs_()
3001 // Compute byte size and alignment of stub template.
3002 for (size_t i = 0; i < insn_count; i++)
3004 unsigned insn_alignment = insns[i].alignment();
3005 size_t insn_size = insns[i].size();
3006 gold_assert((offset & (insn_alignment - 1)) == 0);
3007 this->alignment_ = std::max(this->alignment_, insn_alignment);
3008 switch (insns[i].type())
3010 case Insn_template::THUMB16_TYPE:
3011 case Insn_template::THUMB16_SPECIAL_TYPE:
3013 this->entry_in_thumb_mode_ = true;
3016 case Insn_template::THUMB32_TYPE:
3017 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
3018 this->relocs_.push_back(Reloc(i, offset));
3020 this->entry_in_thumb_mode_ = true;
3023 case Insn_template::ARM_TYPE:
3024 // Handle cases where the target is encoded within the
3026 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
3027 this->relocs_.push_back(Reloc(i, offset));
3030 case Insn_template::DATA_TYPE:
3031 // Entry point cannot be data.
3032 gold_assert(i != 0);
3033 this->relocs_.push_back(Reloc(i, offset));
3039 offset += insn_size;
3041 this->size_ = offset;
3046 // Template to implement do_write for a specific target endianity.
3048 template<bool big_endian>
3050 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
3052 const Stub_template* stub_template = this->stub_template();
3053 const Insn_template* insns = stub_template->insns();
3055 // FIXME: We do not handle BE8 encoding yet.
3056 unsigned char* pov = view;
3057 for (size_t i = 0; i < stub_template->insn_count(); i++)
3059 switch (insns[i].type())
3061 case Insn_template::THUMB16_TYPE:
3062 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
3064 case Insn_template::THUMB16_SPECIAL_TYPE:
3065 elfcpp::Swap<16, big_endian>::writeval(
3067 this->thumb16_special(i));
3069 case Insn_template::THUMB32_TYPE:
3071 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
3072 uint32_t lo = insns[i].data() & 0xffff;
3073 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
3074 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
3077 case Insn_template::ARM_TYPE:
3078 case Insn_template::DATA_TYPE:
3079 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
3084 pov += insns[i].size();
3086 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
3089 // Reloc_stub::Key methods.
3091 // Dump a Key as a string for debugging.
3094 Reloc_stub::Key::name() const
3096 if (this->r_sym_ == invalid_index)
3098 // Global symbol key name
3099 // <stub-type>:<symbol name>:<addend>.
3100 const std::string sym_name = this->u_.symbol->name();
3101 // We need to print two hex number and two colons. So just add 100 bytes
3102 // to the symbol name size.
3103 size_t len = sym_name.size() + 100;
3104 char* buffer = new char[len];
3105 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
3106 sym_name.c_str(), this->addend_);
3107 gold_assert(c > 0 && c < static_cast<int>(len));
3109 return std::string(buffer);
3113 // local symbol key name
3114 // <stub-type>:<object>:<r_sym>:<addend>.
3115 const size_t len = 200;
3117 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
3118 this->u_.relobj, this->r_sym_, this->addend_);
3119 gold_assert(c > 0 && c < static_cast<int>(len));
3120 return std::string(buffer);
3124 // Reloc_stub methods.
3126 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3127 // LOCATION to DESTINATION.
3128 // This code is based on the arm_type_of_stub function in
3129 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3133 Reloc_stub::stub_type_for_reloc(
3134 unsigned int r_type,
3135 Arm_address location,
3136 Arm_address destination,
3137 bool target_is_thumb)
3139 Stub_type stub_type = arm_stub_none;
3141 // This is a bit ugly but we want to avoid using a templated class for
3142 // big and little endianities.
3144 bool should_force_pic_veneer;
3147 if (parameters->target().is_big_endian())
3149 const Target_arm<true>* big_endian_target =
3150 Target_arm<true>::default_target();
3151 may_use_blx = big_endian_target->may_use_blx();
3152 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
3153 thumb2 = big_endian_target->using_thumb2();
3154 thumb_only = big_endian_target->using_thumb_only();
3158 const Target_arm<false>* little_endian_target =
3159 Target_arm<false>::default_target();
3160 may_use_blx = little_endian_target->may_use_blx();
3161 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
3162 thumb2 = little_endian_target->using_thumb2();
3163 thumb_only = little_endian_target->using_thumb_only();
3166 int64_t branch_offset = (int64_t)destination - location;
3168 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
3170 // Handle cases where:
3171 // - this call goes too far (different Thumb/Thumb2 max
3173 // - it's a Thumb->Arm call and blx is not available, or it's a
3174 // Thumb->Arm branch (not bl). A stub is needed in this case.
3176 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3177 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3179 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3180 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3181 || ((!target_is_thumb)
3182 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3183 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
3185 if (target_is_thumb)
3190 stub_type = (parameters->options().shared()
3191 || should_force_pic_veneer)
3194 && (r_type == elfcpp::R_ARM_THM_CALL))
3195 // V5T and above. Stub starts with ARM code, so
3196 // we must be able to switch mode before
3197 // reaching it, which is only possible for 'bl'
3198 // (ie R_ARM_THM_CALL relocation).
3199 ? arm_stub_long_branch_any_thumb_pic
3200 // On V4T, use Thumb code only.
3201 : arm_stub_long_branch_v4t_thumb_thumb_pic)
3205 && (r_type == elfcpp::R_ARM_THM_CALL))
3206 ? arm_stub_long_branch_any_any // V5T and above.
3207 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
3211 stub_type = (parameters->options().shared()
3212 || should_force_pic_veneer)
3213 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
3214 : arm_stub_long_branch_thumb_only; // non-PIC stub.
3221 // FIXME: We should check that the input section is from an
3222 // object that has interwork enabled.
3224 stub_type = (parameters->options().shared()
3225 || should_force_pic_veneer)
3228 && (r_type == elfcpp::R_ARM_THM_CALL))
3229 ? arm_stub_long_branch_any_arm_pic // V5T and above.
3230 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
3234 && (r_type == elfcpp::R_ARM_THM_CALL))
3235 ? arm_stub_long_branch_any_any // V5T and above.
3236 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
3238 // Handle v4t short branches.
3239 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3240 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3241 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3242 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3246 else if (r_type == elfcpp::R_ARM_CALL
3247 || r_type == elfcpp::R_ARM_JUMP24
3248 || r_type == elfcpp::R_ARM_PLT32)
3250 if (target_is_thumb)
3254 // FIXME: We should check that the input section is from an
3255 // object that has interwork enabled.
3257 // We have an extra 2-bytes reach because of
3258 // the mode change (bit 24 (H) of BLX encoding).
3259 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3260 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3261 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
3262 || (r_type == elfcpp::R_ARM_JUMP24)
3263 || (r_type == elfcpp::R_ARM_PLT32))
3265 stub_type = (parameters->options().shared()
3266 || should_force_pic_veneer)
3269 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
3270 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
3274 ? arm_stub_long_branch_any_any // V5T and above.
3275 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
3281 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3282 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3284 stub_type = (parameters->options().shared()
3285 || should_force_pic_veneer)
3286 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
3287 : arm_stub_long_branch_any_any; /// non-PIC.
3295 // Cortex_a8_stub methods.
3297 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3298 // I is the position of the instruction template in the stub template.
3301 Cortex_a8_stub::do_thumb16_special(size_t i)
3303 // The only use of this is to copy condition code from a conditional
3304 // branch being worked around to the corresponding conditional branch in
3306 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3308 uint16_t data = this->stub_template()->insns()[i].data();
3309 gold_assert((data & 0xff00U) == 0xd000U);
3310 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
3314 // Stub_factory methods.
3316 Stub_factory::Stub_factory()
3318 // The instruction template sequences are declared as static
3319 // objects and initialized first time the constructor runs.
3321 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3322 // to reach the stub if necessary.
3323 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
3325 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3326 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3327 // dcd R_ARM_ABS32(X)
3330 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3332 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
3334 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3335 Insn_template::arm_insn(0xe12fff1c), // bx ip
3336 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3337 // dcd R_ARM_ABS32(X)
3340 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3341 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
3343 Insn_template::thumb16_insn(0xb401), // push {r0}
3344 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3345 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3346 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3347 Insn_template::thumb16_insn(0x4760), // bx ip
3348 Insn_template::thumb16_insn(0xbf00), // nop
3349 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3350 // dcd R_ARM_ABS32(X)
3353 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3355 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
3357 Insn_template::thumb16_insn(0x4778), // bx pc
3358 Insn_template::thumb16_insn(0x46c0), // nop
3359 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3360 Insn_template::arm_insn(0xe12fff1c), // bx ip
3361 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3362 // dcd R_ARM_ABS32(X)
3365 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3367 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
3369 Insn_template::thumb16_insn(0x4778), // bx pc
3370 Insn_template::thumb16_insn(0x46c0), // nop
3371 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3372 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3373 // dcd R_ARM_ABS32(X)
3376 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3377 // one, when the destination is close enough.
3378 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
3380 Insn_template::thumb16_insn(0x4778), // bx pc
3381 Insn_template::thumb16_insn(0x46c0), // nop
3382 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3385 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3386 // blx to reach the stub if necessary.
3387 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
3389 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3390 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3391 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3392 // dcd R_ARM_REL32(X-4)
3395 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3396 // blx to reach the stub if necessary. We can not add into pc;
3397 // it is not guaranteed to mode switch (different in ARMv6 and
3399 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
3401 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3402 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3403 Insn_template::arm_insn(0xe12fff1c), // bx ip
3404 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3405 // dcd R_ARM_REL32(X)
3408 // V4T ARM -> ARM long branch stub, PIC.
3409 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
3411 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3412 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3413 Insn_template::arm_insn(0xe12fff1c), // bx ip
3414 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3415 // dcd R_ARM_REL32(X)
3418 // V4T Thumb -> ARM long branch stub, PIC.
3419 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
3421 Insn_template::thumb16_insn(0x4778), // bx pc
3422 Insn_template::thumb16_insn(0x46c0), // nop
3423 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3424 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3425 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3426 // dcd R_ARM_REL32(X)
3429 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3431 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
3433 Insn_template::thumb16_insn(0xb401), // push {r0}
3434 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3435 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3436 Insn_template::thumb16_insn(0x4484), // add ip, r0
3437 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3438 Insn_template::thumb16_insn(0x4760), // bx ip
3439 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
3440 // dcd R_ARM_REL32(X)
3443 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3445 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
3447 Insn_template::thumb16_insn(0x4778), // bx pc
3448 Insn_template::thumb16_insn(0x46c0), // nop
3449 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3450 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3451 Insn_template::arm_insn(0xe12fff1c), // bx ip
3452 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3453 // dcd R_ARM_REL32(X)
3456 // Cortex-A8 erratum-workaround stubs.
3458 // Stub used for conditional branches (which may be beyond +/-1MB away,
3459 // so we can't use a conditional branch to reach this stub).
3466 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
3468 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3469 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3470 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3474 // Stub used for b.w and bl.w instructions.
3476 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
3478 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3481 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
3483 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3486 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3487 // instruction (which switches to ARM mode) to point to this stub. Jump to
3488 // the real destination using an ARM-mode branch.
3489 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
3491 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3494 // Fill in the stub template look-up table. Stub templates are constructed
3495 // per instance of Stub_factory for fast look-up without locking
3496 // in a thread-enabled environment.
3498 this->stub_templates_[arm_stub_none] =
3499 new Stub_template(arm_stub_none, NULL, 0);
3501 #define DEF_STUB(x) \
3505 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3506 Stub_type type = arm_stub_##x; \
3507 this->stub_templates_[type] = \
3508 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3516 // Stub_table methods.
3518 // Removel all Cortex-A8 stub.
3520 template<bool big_endian>
3522 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
3524 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
3525 p != this->cortex_a8_stubs_.end();
3528 this->cortex_a8_stubs_.clear();
3531 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
3533 template<bool big_endian>
3535 Stub_table<big_endian>::relocate_stub(
3537 const Relocate_info<32, big_endian>* relinfo,
3538 Target_arm<big_endian>* arm_target,
3539 Output_section* output_section,
3540 unsigned char* view,
3541 Arm_address address,
3542 section_size_type view_size)
3544 const Stub_template* stub_template = stub->stub_template();
3545 if (stub_template->reloc_count() != 0)
3547 // Adjust view to cover the stub only.
3548 section_size_type offset = stub->offset();
3549 section_size_type stub_size = stub_template->size();
3550 gold_assert(offset + stub_size <= view_size);
3552 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
3553 address + offset, stub_size);
3557 // Relocate all stubs in this stub table.
3559 template<bool big_endian>
3561 Stub_table<big_endian>::relocate_stubs(
3562 const Relocate_info<32, big_endian>* relinfo,
3563 Target_arm<big_endian>* arm_target,
3564 Output_section* output_section,
3565 unsigned char* view,
3566 Arm_address address,
3567 section_size_type view_size)
3569 // If we are passed a view bigger than the stub table's. we need to
3571 gold_assert(address == this->address()
3573 == static_cast<section_size_type>(this->data_size())));
3575 // Relocate all relocation stubs.
3576 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3577 p != this->reloc_stubs_.end();
3579 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
3580 address, view_size);
3582 // Relocate all Cortex-A8 stubs.
3583 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
3584 p != this->cortex_a8_stubs_.end();
3586 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
3587 address, view_size);
3590 // Write out the stubs to file.
3592 template<bool big_endian>
3594 Stub_table<big_endian>::do_write(Output_file* of)
3596 off_t offset = this->offset();
3597 const section_size_type oview_size =
3598 convert_to_section_size_type(this->data_size());
3599 unsigned char* const oview = of->get_output_view(offset, oview_size);
3601 // Write relocation stubs.
3602 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3603 p != this->reloc_stubs_.end();
3606 Reloc_stub* stub = p->second;
3607 Arm_address address = this->address() + stub->offset();
3609 == align_address(address,
3610 stub->stub_template()->alignment()));
3611 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3615 // Write Cortex-A8 stubs.
3616 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3617 p != this->cortex_a8_stubs_.end();
3620 Cortex_a8_stub* stub = p->second;
3621 Arm_address address = this->address() + stub->offset();
3623 == align_address(address,
3624 stub->stub_template()->alignment()));
3625 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3629 of->write_output_view(this->offset(), oview_size, oview);
3632 // Update the data size and address alignment of the stub table at the end
3633 // of a relaxation pass. Return true if either the data size or the
3634 // alignment changed in this relaxation pass.
3636 template<bool big_endian>
3638 Stub_table<big_endian>::update_data_size_and_addralign()
3641 unsigned addralign = 1;
3643 // Go over all stubs in table to compute data size and address alignment.
3645 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3646 p != this->reloc_stubs_.end();
3649 const Stub_template* stub_template = p->second->stub_template();
3650 addralign = std::max(addralign, stub_template->alignment());
3651 size = (align_address(size, stub_template->alignment())
3652 + stub_template->size());
3655 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3656 p != this->cortex_a8_stubs_.end();
3659 const Stub_template* stub_template = p->second->stub_template();
3660 addralign = std::max(addralign, stub_template->alignment());
3661 size = (align_address(size, stub_template->alignment())
3662 + stub_template->size());
3665 // Check if either data size or alignment changed in this pass.
3666 // Update prev_data_size_ and prev_addralign_. These will be used
3667 // as the current data size and address alignment for the next pass.
3668 bool changed = size != this->prev_data_size_;
3669 this->prev_data_size_ = size;
3671 if (addralign != this->prev_addralign_)
3673 this->prev_addralign_ = addralign;
3678 // Finalize the stubs. This sets the offsets of the stubs within the stub
3679 // table. It also marks all input sections needing Cortex-A8 workaround.
3681 template<bool big_endian>
3683 Stub_table<big_endian>::finalize_stubs()
3686 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3687 p != this->reloc_stubs_.end();
3690 Reloc_stub* stub = p->second;
3691 const Stub_template* stub_template = stub->stub_template();
3692 uint64_t stub_addralign = stub_template->alignment();
3693 off = align_address(off, stub_addralign);
3694 stub->set_offset(off);
3695 off += stub_template->size();
3698 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3699 p != this->cortex_a8_stubs_.end();
3702 Cortex_a8_stub* stub = p->second;
3703 const Stub_template* stub_template = stub->stub_template();
3704 uint64_t stub_addralign = stub_template->alignment();
3705 off = align_address(off, stub_addralign);
3706 stub->set_offset(off);
3707 off += stub_template->size();
3709 // Mark input section so that we can determine later if a code section
3710 // needs the Cortex-A8 workaround quickly.
3711 Arm_relobj<big_endian>* arm_relobj =
3712 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
3713 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
3716 gold_assert(off <= this->prev_data_size_);
3719 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
3720 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
3721 // of the address range seen by the linker.
3723 template<bool big_endian>
3725 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
3726 Target_arm<big_endian>* arm_target,
3727 unsigned char* view,
3728 Arm_address view_address,
3729 section_size_type view_size)
3731 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
3732 for (Cortex_a8_stub_list::const_iterator p =
3733 this->cortex_a8_stubs_.lower_bound(view_address);
3734 ((p != this->cortex_a8_stubs_.end())
3735 && (p->first < (view_address + view_size)));
3738 // We do not store the THUMB bit in the LSB of either the branch address
3739 // or the stub offset. There is no need to strip the LSB.
3740 Arm_address branch_address = p->first;
3741 const Cortex_a8_stub* stub = p->second;
3742 Arm_address stub_address = this->address() + stub->offset();
3744 // Offset of the branch instruction relative to this view.
3745 section_size_type offset =
3746 convert_to_section_size_type(branch_address - view_address);
3747 gold_assert((offset + 4) <= view_size);
3749 arm_target->apply_cortex_a8_workaround(stub, stub_address,
3750 view + offset, branch_address);
3754 // Arm_input_section methods.
3756 // Initialize an Arm_input_section.
3758 template<bool big_endian>
3760 Arm_input_section<big_endian>::init()
3762 Relobj* relobj = this->relobj();
3763 unsigned int shndx = this->shndx();
3765 // Cache these to speed up size and alignment queries. It is too slow
3766 // to call section_addraglin and section_size every time.
3767 this->original_addralign_ = relobj->section_addralign(shndx);
3768 this->original_size_ = relobj->section_size(shndx);
3770 // We want to make this look like the original input section after
3771 // output sections are finalized.
3772 Output_section* os = relobj->output_section(shndx);
3773 off_t offset = relobj->output_section_offset(shndx);
3774 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
3775 this->set_address(os->address() + offset);
3776 this->set_file_offset(os->offset() + offset);
3778 this->set_current_data_size(this->original_size_);
3779 this->finalize_data_size();
3782 template<bool big_endian>
3784 Arm_input_section<big_endian>::do_write(Output_file* of)
3786 // We have to write out the original section content.
3787 section_size_type section_size;
3788 const unsigned char* section_contents =
3789 this->relobj()->section_contents(this->shndx(), §ion_size, false);
3790 of->write(this->offset(), section_contents, section_size);
3792 // If this owns a stub table and it is not empty, write it.
3793 if (this->is_stub_table_owner() && !this->stub_table_->empty())
3794 this->stub_table_->write(of);
3797 // Finalize data size.
3799 template<bool big_endian>
3801 Arm_input_section<big_endian>::set_final_data_size()
3803 // If this owns a stub table, finalize its data size as well.
3804 if (this->is_stub_table_owner())
3806 uint64_t address = this->address();
3808 // The stub table comes after the original section contents.
3809 address += this->original_size_;
3810 address = align_address(address, this->stub_table_->addralign());
3811 off_t offset = this->offset() + (address - this->address());
3812 this->stub_table_->set_address_and_file_offset(address, offset);
3813 address += this->stub_table_->data_size();
3814 gold_assert(address == this->address() + this->current_data_size());
3817 this->set_data_size(this->current_data_size());
3820 // Reset address and file offset.
3822 template<bool big_endian>
3824 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
3826 // Size of the original input section contents.
3827 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
3829 // If this is a stub table owner, account for the stub table size.
3830 if (this->is_stub_table_owner())
3832 Stub_table<big_endian>* stub_table = this->stub_table_;
3834 // Reset the stub table's address and file offset. The
3835 // current data size for child will be updated after that.
3836 stub_table_->reset_address_and_file_offset();
3837 off = align_address(off, stub_table_->addralign());
3838 off += stub_table->current_data_size();
3841 this->set_current_data_size(off);
3844 // Arm_output_section methods.
3846 // Create a stub group for input sections from BEGIN to END. OWNER
3847 // points to the input section to be the owner a new stub table.
3849 template<bool big_endian>
3851 Arm_output_section<big_endian>::create_stub_group(
3852 Input_section_list::const_iterator begin,
3853 Input_section_list::const_iterator end,
3854 Input_section_list::const_iterator owner,
3855 Target_arm<big_endian>* target,
3856 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
3858 // Currently we convert ordinary input sections into relaxed sections only
3859 // at this point but we may want to support creating relaxed input section
3860 // very early. So we check here to see if owner is already a relaxed
3863 Arm_input_section<big_endian>* arm_input_section;
3864 if (owner->is_relaxed_input_section())
3867 Arm_input_section<big_endian>::as_arm_input_section(
3868 owner->relaxed_input_section());
3872 gold_assert(owner->is_input_section());
3873 // Create a new relaxed input section.
3875 target->new_arm_input_section(owner->relobj(), owner->shndx());
3876 new_relaxed_sections->push_back(arm_input_section);
3879 // Create a stub table.
3880 Stub_table<big_endian>* stub_table =
3881 target->new_stub_table(arm_input_section);
3883 arm_input_section->set_stub_table(stub_table);
3885 Input_section_list::const_iterator p = begin;
3886 Input_section_list::const_iterator prev_p;
3888 // Look for input sections or relaxed input sections in [begin ... end].
3891 if (p->is_input_section() || p->is_relaxed_input_section())
3893 // The stub table information for input sections live
3894 // in their objects.
3895 Arm_relobj<big_endian>* arm_relobj =
3896 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
3897 arm_relobj->set_stub_table(p->shndx(), stub_table);
3901 while (prev_p != end);
3904 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3905 // of stub groups. We grow a stub group by adding input section until the
3906 // size is just below GROUP_SIZE. The last input section will be converted
3907 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3908 // input section after the stub table, effectively double the group size.
3910 // This is similar to the group_sections() function in elf32-arm.c but is
3911 // implemented differently.
3913 template<bool big_endian>
3915 Arm_output_section<big_endian>::group_sections(
3916 section_size_type group_size,
3917 bool stubs_always_after_branch,
3918 Target_arm<big_endian>* target)
3920 // We only care about sections containing code.
3921 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
3924 // States for grouping.
3927 // No group is being built.
3929 // A group is being built but the stub table is not found yet.
3930 // We keep group a stub group until the size is just under GROUP_SIZE.
3931 // The last input section in the group will be used as the stub table.
3932 FINDING_STUB_SECTION,
3933 // A group is being built and we have already found a stub table.
3934 // We enter this state to grow a stub group by adding input section
3935 // after the stub table. This effectively doubles the group size.
3939 // Any newly created relaxed sections are stored here.
3940 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
3942 State state = NO_GROUP;
3943 section_size_type off = 0;
3944 section_size_type group_begin_offset = 0;
3945 section_size_type group_end_offset = 0;
3946 section_size_type stub_table_end_offset = 0;
3947 Input_section_list::const_iterator group_begin =
3948 this->input_sections().end();
3949 Input_section_list::const_iterator stub_table =
3950 this->input_sections().end();
3951 Input_section_list::const_iterator group_end = this->input_sections().end();
3952 for (Input_section_list::const_iterator p = this->input_sections().begin();
3953 p != this->input_sections().end();
3956 section_size_type section_begin_offset =
3957 align_address(off, p->addralign());
3958 section_size_type section_end_offset =
3959 section_begin_offset + p->data_size();
3961 // Check to see if we should group the previously seens sections.
3967 case FINDING_STUB_SECTION:
3968 // Adding this section makes the group larger than GROUP_SIZE.
3969 if (section_end_offset - group_begin_offset >= group_size)
3971 if (stubs_always_after_branch)
3973 gold_assert(group_end != this->input_sections().end());
3974 this->create_stub_group(group_begin, group_end, group_end,
3975 target, &new_relaxed_sections);
3980 // But wait, there's more! Input sections up to
3981 // stub_group_size bytes after the stub table can be
3982 // handled by it too.
3983 state = HAS_STUB_SECTION;
3984 stub_table = group_end;
3985 stub_table_end_offset = group_end_offset;
3990 case HAS_STUB_SECTION:
3991 // Adding this section makes the post stub-section group larger
3993 if (section_end_offset - stub_table_end_offset >= group_size)
3995 gold_assert(group_end != this->input_sections().end());
3996 this->create_stub_group(group_begin, group_end, stub_table,
3997 target, &new_relaxed_sections);
4006 // If we see an input section and currently there is no group, start
4007 // a new one. Skip any empty sections.
4008 if ((p->is_input_section() || p->is_relaxed_input_section())
4009 && (p->relobj()->section_size(p->shndx()) != 0))
4011 if (state == NO_GROUP)
4013 state = FINDING_STUB_SECTION;
4015 group_begin_offset = section_begin_offset;
4018 // Keep track of the last input section seen.
4020 group_end_offset = section_end_offset;
4023 off = section_end_offset;
4026 // Create a stub group for any ungrouped sections.
4027 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
4029 gold_assert(group_end != this->input_sections().end());
4030 this->create_stub_group(group_begin, group_end,
4031 (state == FINDING_STUB_SECTION
4034 target, &new_relaxed_sections);
4037 // Convert input section into relaxed input section in a batch.
4038 if (!new_relaxed_sections.empty())
4039 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
4041 // Update the section offsets
4042 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
4044 Arm_relobj<big_endian>* arm_relobj =
4045 Arm_relobj<big_endian>::as_arm_relobj(
4046 new_relaxed_sections[i]->relobj());
4047 unsigned int shndx = new_relaxed_sections[i]->shndx();
4048 // Tell Arm_relobj that this input section is converted.
4049 arm_relobj->convert_input_section_to_relaxed_section(shndx);
4053 // Arm_relobj methods.
4055 // Scan relocations for stub generation.
4057 template<bool big_endian>
4059 Arm_relobj<big_endian>::scan_sections_for_stubs(
4060 Target_arm<big_endian>* arm_target,
4061 const Symbol_table* symtab,
4062 const Layout* layout)
4064 unsigned int shnum = this->shnum();
4065 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4067 // Read the section headers.
4068 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
4072 // To speed up processing, we set up hash tables for fast lookup of
4073 // input offsets to output addresses.
4074 this->initialize_input_to_output_maps();
4076 const Relobj::Output_sections& out_sections(this->output_sections());
4078 Relocate_info<32, big_endian> relinfo;
4079 relinfo.symtab = symtab;
4080 relinfo.layout = layout;
4081 relinfo.object = this;
4083 const unsigned char* p = pshdrs + shdr_size;
4084 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
4086 typename elfcpp::Shdr<32, big_endian> shdr(p);
4088 unsigned int sh_type = shdr.get_sh_type();
4089 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
4092 off_t sh_size = shdr.get_sh_size();
4096 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
4097 if (index >= this->shnum())
4099 // Ignore reloc section with bad info. This error will be
4100 // reported in the final link.
4104 Output_section* os = out_sections[index];
4106 || symtab->is_section_folded(this, index))
4108 // This relocation section is against a section which we
4109 // discarded or if the section is folded into another
4110 // section due to ICF.
4113 Arm_address output_offset = this->get_output_section_offset(index);
4115 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
4117 // Ignore reloc section with unexpected symbol table. The
4118 // error will be reported in the final link.
4122 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
4123 sh_size, true, false);
4125 unsigned int reloc_size;
4126 if (sh_type == elfcpp::SHT_REL)
4127 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
4129 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
4131 if (reloc_size != shdr.get_sh_entsize())
4133 // Ignore reloc section with unexpected entsize. The error
4134 // will be reported in the final link.
4138 size_t reloc_count = sh_size / reloc_size;
4139 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
4141 // Ignore reloc section with uneven size. The error will be
4142 // reported in the final link.
4146 gold_assert(output_offset != invalid_address
4147 || this->relocs_must_follow_section_writes());
4149 // Get the section contents. This does work for the case in which
4150 // we modify the contents of an input section. We need to pass the
4151 // output view under such circumstances.
4152 section_size_type input_view_size = 0;
4153 const unsigned char* input_view =
4154 this->section_contents(index, &input_view_size, false);
4156 relinfo.reloc_shndx = i;
4157 relinfo.data_shndx = index;
4158 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
4160 output_offset == invalid_address,
4166 // After we've done the relocations, we release the hash tables,
4167 // since we no longer need them.
4168 this->free_input_to_output_maps();
4171 // Count the local symbols. The ARM backend needs to know if a symbol
4172 // is a THUMB function or not. For global symbols, it is easy because
4173 // the Symbol object keeps the ELF symbol type. For local symbol it is
4174 // harder because we cannot access this information. So we override the
4175 // do_count_local_symbol in parent and scan local symbols to mark
4176 // THUMB functions. This is not the most efficient way but I do not want to
4177 // slow down other ports by calling a per symbol targer hook inside
4178 // Sized_relobj<size, big_endian>::do_count_local_symbols.
4180 template<bool big_endian>
4182 Arm_relobj<big_endian>::do_count_local_symbols(
4183 Stringpool_template<char>* pool,
4184 Stringpool_template<char>* dynpool)
4186 // We need to fix-up the values of any local symbols whose type are
4189 // Ask parent to count the local symbols.
4190 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
4191 const unsigned int loccount = this->local_symbol_count();
4195 // Intialize the thumb function bit-vector.
4196 std::vector<bool> empty_vector(loccount, false);
4197 this->local_symbol_is_thumb_function_.swap(empty_vector);
4199 // Read the symbol table section header.
4200 const unsigned int symtab_shndx = this->symtab_shndx();
4201 elfcpp::Shdr<32, big_endian>
4202 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
4203 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
4205 // Read the local symbols.
4206 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
4207 gold_assert(loccount == symtabshdr.get_sh_info());
4208 off_t locsize = loccount * sym_size;
4209 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
4210 locsize, true, true);
4212 // Loop over the local symbols and mark any local symbols pointing
4213 // to THUMB functions.
4215 // Skip the first dummy symbol.
4217 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
4218 this->local_values();
4219 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
4221 elfcpp::Sym<32, big_endian> sym(psyms);
4222 elfcpp::STT st_type = sym.get_st_type();
4223 Symbol_value<32>& lv((*plocal_values)[i]);
4224 Arm_address input_value = lv.input_value();
4226 if (st_type == elfcpp::STT_ARM_TFUNC
4227 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
4229 // This is a THUMB function. Mark this and canonicalize the
4230 // symbol value by setting LSB.
4231 this->local_symbol_is_thumb_function_[i] = true;
4232 if ((input_value & 1) == 0)
4233 lv.set_input_value(input_value | 1);
4238 // Relocate sections.
4239 template<bool big_endian>
4241 Arm_relobj<big_endian>::do_relocate_sections(
4242 const Symbol_table* symtab,
4243 const Layout* layout,
4244 const unsigned char* pshdrs,
4245 typename Sized_relobj<32, big_endian>::Views* pviews)
4247 // Call parent to relocate sections.
4248 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
4251 // We do not generate stubs if doing a relocatable link.
4252 if (parameters->options().relocatable())
4255 // Relocate stub tables.
4256 unsigned int shnum = this->shnum();
4258 Target_arm<big_endian>* arm_target =
4259 Target_arm<big_endian>::default_target();
4261 Relocate_info<32, big_endian> relinfo;
4262 relinfo.symtab = symtab;
4263 relinfo.layout = layout;
4264 relinfo.object = this;
4266 for (unsigned int i = 1; i < shnum; ++i)
4268 Arm_input_section<big_endian>* arm_input_section =
4269 arm_target->find_arm_input_section(this, i);
4271 if (arm_input_section == NULL
4272 || !arm_input_section->is_stub_table_owner()
4273 || arm_input_section->stub_table()->empty())
4276 // We cannot discard a section if it owns a stub table.
4277 Output_section* os = this->output_section(i);
4278 gold_assert(os != NULL);
4280 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
4281 relinfo.reloc_shdr = NULL;
4282 relinfo.data_shndx = i;
4283 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
4285 gold_assert((*pviews)[i].view != NULL);
4287 // We are passed the output section view. Adjust it to cover the
4289 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
4290 gold_assert((stub_table->address() >= (*pviews)[i].address)
4291 && ((stub_table->address() + stub_table->data_size())
4292 <= (*pviews)[i].address + (*pviews)[i].view_size));
4294 off_t offset = stub_table->address() - (*pviews)[i].address;
4295 unsigned char* view = (*pviews)[i].view + offset;
4296 Arm_address address = stub_table->address();
4297 section_size_type view_size = stub_table->data_size();
4299 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
4304 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4307 template<bool big_endian>
4308 Attributes_section_data*
4309 read_arm_attributes_section(
4311 Read_symbols_data *sd)
4313 // Read the attributes section if there is one.
4314 // We read from the end because gas seems to put it near the end of
4315 // the section headers.
4316 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4317 const unsigned char *ps =
4318 sd->section_headers->data() + shdr_size * (object->shnum() - 1);
4319 for (unsigned int i = object->shnum(); i > 0; --i, ps -= shdr_size)
4321 elfcpp::Shdr<32, big_endian> shdr(ps);
4322 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
4324 section_offset_type section_offset = shdr.get_sh_offset();
4325 section_size_type section_size =
4326 convert_to_section_size_type(shdr.get_sh_size());
4327 File_view* view = object->get_lasting_view(section_offset,
4328 section_size, true, false);
4329 return new Attributes_section_data(view->data(), section_size);
4335 // Read the symbol information.
4337 template<bool big_endian>
4339 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4341 // Call parent class to read symbol information.
4342 Sized_relobj<32, big_endian>::do_read_symbols(sd);
4344 // Read processor-specific flags in ELF file header.
4345 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4346 elfcpp::Elf_sizes<32>::ehdr_size,
4348 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4349 this->processor_specific_flags_ = ehdr.get_e_flags();
4350 this->attributes_section_data_ =
4351 read_arm_attributes_section<big_endian>(this, sd);
4354 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
4355 // sections for unwinding. These sections are referenced implicitly by
4356 // text sections linked in the section headers. If we ignore these implict
4357 // references, the .ARM.exidx sections and any .ARM.extab sections they use
4358 // will be garbage-collected incorrectly. Hence we override the same function
4359 // in the base class to handle these implicit references.
4361 template<bool big_endian>
4363 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
4365 Read_relocs_data* rd)
4367 // First, call base class method to process relocations in this object.
4368 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
4370 unsigned int shnum = this->shnum();
4371 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4372 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
4376 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
4377 // to these from the linked text sections.
4378 const unsigned char* ps = pshdrs + shdr_size;
4379 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
4381 elfcpp::Shdr<32, big_endian> shdr(ps);
4382 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
4384 // Found an .ARM.exidx section, add it to the set of reachable
4385 // sections from its linked text section.
4386 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
4387 symtab->gc()->add_reference(this, text_shndx, this, i);
4392 // Arm_dynobj methods.
4394 // Read the symbol information.
4396 template<bool big_endian>
4398 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4400 // Call parent class to read symbol information.
4401 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
4403 // Read processor-specific flags in ELF file header.
4404 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4405 elfcpp::Elf_sizes<32>::ehdr_size,
4407 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4408 this->processor_specific_flags_ = ehdr.get_e_flags();
4409 this->attributes_section_data_ =
4410 read_arm_attributes_section<big_endian>(this, sd);
4413 // Stub_addend_reader methods.
4415 // Read the addend of a REL relocation of type R_TYPE at VIEW.
4417 template<bool big_endian>
4418 elfcpp::Elf_types<32>::Elf_Swxword
4419 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
4420 unsigned int r_type,
4421 const unsigned char* view,
4422 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
4424 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
4428 case elfcpp::R_ARM_CALL:
4429 case elfcpp::R_ARM_JUMP24:
4430 case elfcpp::R_ARM_PLT32:
4432 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4433 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4434 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4435 return utils::sign_extend<26>(val << 2);
4438 case elfcpp::R_ARM_THM_CALL:
4439 case elfcpp::R_ARM_THM_JUMP24:
4440 case elfcpp::R_ARM_THM_XPC22:
4442 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4443 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4444 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4445 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4446 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
4449 case elfcpp::R_ARM_THM_JUMP19:
4451 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4452 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4453 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4454 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4455 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
4463 // A class to handle the PLT data.
4465 template<bool big_endian>
4466 class Output_data_plt_arm : public Output_section_data
4469 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
4472 Output_data_plt_arm(Layout*, Output_data_space*);
4474 // Add an entry to the PLT.
4476 add_entry(Symbol* gsym);
4478 // Return the .rel.plt section data.
4479 const Reloc_section*
4481 { return this->rel_; }
4485 do_adjust_output_section(Output_section* os);
4487 // Write to a map file.
4489 do_print_to_mapfile(Mapfile* mapfile) const
4490 { mapfile->print_output_data(this, _("** PLT")); }
4493 // Template for the first PLT entry.
4494 static const uint32_t first_plt_entry[5];
4496 // Template for subsequent PLT entries.
4497 static const uint32_t plt_entry[3];
4499 // Set the final size.
4501 set_final_data_size()
4503 this->set_data_size(sizeof(first_plt_entry)
4504 + this->count_ * sizeof(plt_entry));
4507 // Write out the PLT data.
4509 do_write(Output_file*);
4511 // The reloc section.
4512 Reloc_section* rel_;
4513 // The .got.plt section.
4514 Output_data_space* got_plt_;
4515 // The number of PLT entries.
4516 unsigned int count_;
4519 // Create the PLT section. The ordinary .got section is an argument,
4520 // since we need to refer to the start. We also create our own .got
4521 // section just for PLT entries.
4523 template<bool big_endian>
4524 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
4525 Output_data_space* got_plt)
4526 : Output_section_data(4), got_plt_(got_plt), count_(0)
4528 this->rel_ = new Reloc_section(false);
4529 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
4530 elfcpp::SHF_ALLOC, this->rel_, true, false,
4534 template<bool big_endian>
4536 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
4541 // Add an entry to the PLT.
4543 template<bool big_endian>
4545 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
4547 gold_assert(!gsym->has_plt_offset());
4549 // Note that when setting the PLT offset we skip the initial
4550 // reserved PLT entry.
4551 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
4552 + sizeof(first_plt_entry));
4556 section_offset_type got_offset = this->got_plt_->current_data_size();
4558 // Every PLT entry needs a GOT entry which points back to the PLT
4559 // entry (this will be changed by the dynamic linker, normally
4560 // lazily when the function is called).
4561 this->got_plt_->set_current_data_size(got_offset + 4);
4563 // Every PLT entry needs a reloc.
4564 gsym->set_needs_dynsym_entry();
4565 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
4568 // Note that we don't need to save the symbol. The contents of the
4569 // PLT are independent of which symbols are used. The symbols only
4570 // appear in the relocations.
4574 // FIXME: This is not very flexible. Right now this has only been tested
4575 // on armv5te. If we are to support additional architecture features like
4576 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4578 // The first entry in the PLT.
4579 template<bool big_endian>
4580 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
4582 0xe52de004, // str lr, [sp, #-4]!
4583 0xe59fe004, // ldr lr, [pc, #4]
4584 0xe08fe00e, // add lr, pc, lr
4585 0xe5bef008, // ldr pc, [lr, #8]!
4586 0x00000000, // &GOT[0] - .
4589 // Subsequent entries in the PLT.
4591 template<bool big_endian>
4592 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
4594 0xe28fc600, // add ip, pc, #0xNN00000
4595 0xe28cca00, // add ip, ip, #0xNN000
4596 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4599 // Write out the PLT. This uses the hand-coded instructions above,
4600 // and adjusts them as needed. This is all specified by the arm ELF
4601 // Processor Supplement.
4603 template<bool big_endian>
4605 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
4607 const off_t offset = this->offset();
4608 const section_size_type oview_size =
4609 convert_to_section_size_type(this->data_size());
4610 unsigned char* const oview = of->get_output_view(offset, oview_size);
4612 const off_t got_file_offset = this->got_plt_->offset();
4613 const section_size_type got_size =
4614 convert_to_section_size_type(this->got_plt_->data_size());
4615 unsigned char* const got_view = of->get_output_view(got_file_offset,
4617 unsigned char* pov = oview;
4619 Arm_address plt_address = this->address();
4620 Arm_address got_address = this->got_plt_->address();
4622 // Write first PLT entry. All but the last word are constants.
4623 const size_t num_first_plt_words = (sizeof(first_plt_entry)
4624 / sizeof(plt_entry[0]));
4625 for (size_t i = 0; i < num_first_plt_words - 1; i++)
4626 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
4627 // Last word in first PLT entry is &GOT[0] - .
4628 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
4629 got_address - (plt_address + 16));
4630 pov += sizeof(first_plt_entry);
4632 unsigned char* got_pov = got_view;
4634 memset(got_pov, 0, 12);
4637 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
4638 unsigned int plt_offset = sizeof(first_plt_entry);
4639 unsigned int plt_rel_offset = 0;
4640 unsigned int got_offset = 12;
4641 const unsigned int count = this->count_;
4642 for (unsigned int i = 0;
4645 pov += sizeof(plt_entry),
4647 plt_offset += sizeof(plt_entry),
4648 plt_rel_offset += rel_size,
4651 // Set and adjust the PLT entry itself.
4652 int32_t offset = ((got_address + got_offset)
4653 - (plt_address + plt_offset + 8));
4655 gold_assert(offset >= 0 && offset < 0x0fffffff);
4656 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
4657 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
4658 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
4659 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
4660 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
4661 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
4663 // Set the entry in the GOT.
4664 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
4667 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4668 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4670 of->write_output_view(offset, oview_size, oview);
4671 of->write_output_view(got_file_offset, got_size, got_view);
4674 // Create a PLT entry for a global symbol.
4676 template<bool big_endian>
4678 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
4681 if (gsym->has_plt_offset())
4684 if (this->plt_ == NULL)
4686 // Create the GOT sections first.
4687 this->got_section(symtab, layout);
4689 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
4690 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
4692 | elfcpp::SHF_EXECINSTR),
4693 this->plt_, false, false, false, false);
4695 this->plt_->add_entry(gsym);
4698 // Report an unsupported relocation against a local symbol.
4700 template<bool big_endian>
4702 Target_arm<big_endian>::Scan::unsupported_reloc_local(
4703 Sized_relobj<32, big_endian>* object,
4704 unsigned int r_type)
4706 gold_error(_("%s: unsupported reloc %u against local symbol"),
4707 object->name().c_str(), r_type);
4710 // We are about to emit a dynamic relocation of type R_TYPE. If the
4711 // dynamic linker does not support it, issue an error. The GNU linker
4712 // only issues a non-PIC error for an allocated read-only section.
4713 // Here we know the section is allocated, but we don't know that it is
4714 // read-only. But we check for all the relocation types which the
4715 // glibc dynamic linker supports, so it seems appropriate to issue an
4716 // error even if the section is not read-only.
4718 template<bool big_endian>
4720 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
4721 unsigned int r_type)
4725 // These are the relocation types supported by glibc for ARM.
4726 case elfcpp::R_ARM_RELATIVE:
4727 case elfcpp::R_ARM_COPY:
4728 case elfcpp::R_ARM_GLOB_DAT:
4729 case elfcpp::R_ARM_JUMP_SLOT:
4730 case elfcpp::R_ARM_ABS32:
4731 case elfcpp::R_ARM_ABS32_NOI:
4732 case elfcpp::R_ARM_PC24:
4733 // FIXME: The following 3 types are not supported by Android's dynamic
4735 case elfcpp::R_ARM_TLS_DTPMOD32:
4736 case elfcpp::R_ARM_TLS_DTPOFF32:
4737 case elfcpp::R_ARM_TLS_TPOFF32:
4741 // This prevents us from issuing more than one error per reloc
4742 // section. But we can still wind up issuing more than one
4743 // error per object file.
4744 if (this->issued_non_pic_error_)
4746 object->error(_("requires unsupported dynamic reloc; "
4747 "recompile with -fPIC"));
4748 this->issued_non_pic_error_ = true;
4751 case elfcpp::R_ARM_NONE:
4756 // Scan a relocation for a local symbol.
4757 // FIXME: This only handles a subset of relocation types used by Android
4758 // on ARM v5te devices.
4760 template<bool big_endian>
4762 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
4765 Sized_relobj<32, big_endian>* object,
4766 unsigned int data_shndx,
4767 Output_section* output_section,
4768 const elfcpp::Rel<32, big_endian>& reloc,
4769 unsigned int r_type,
4770 const elfcpp::Sym<32, big_endian>&)
4772 r_type = get_real_reloc_type(r_type);
4775 case elfcpp::R_ARM_NONE:
4778 case elfcpp::R_ARM_ABS32:
4779 case elfcpp::R_ARM_ABS32_NOI:
4780 // If building a shared library (or a position-independent
4781 // executable), we need to create a dynamic relocation for
4782 // this location. The relocation applied at link time will
4783 // apply the link-time value, so we flag the location with
4784 // an R_ARM_RELATIVE relocation so the dynamic loader can
4785 // relocate it easily.
4786 if (parameters->options().output_is_position_independent())
4788 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4789 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4790 // If we are to add more other reloc types than R_ARM_ABS32,
4791 // we need to add check_non_pic(object, r_type) here.
4792 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
4793 output_section, data_shndx,
4794 reloc.get_r_offset());
4798 case elfcpp::R_ARM_REL32:
4799 case elfcpp::R_ARM_THM_CALL:
4800 case elfcpp::R_ARM_CALL:
4801 case elfcpp::R_ARM_PREL31:
4802 case elfcpp::R_ARM_JUMP24:
4803 case elfcpp::R_ARM_PLT32:
4804 case elfcpp::R_ARM_THM_ABS5:
4805 case elfcpp::R_ARM_ABS8:
4806 case elfcpp::R_ARM_ABS12:
4807 case elfcpp::R_ARM_ABS16:
4808 case elfcpp::R_ARM_BASE_ABS:
4809 case elfcpp::R_ARM_MOVW_ABS_NC:
4810 case elfcpp::R_ARM_MOVT_ABS:
4811 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4812 case elfcpp::R_ARM_THM_MOVT_ABS:
4813 case elfcpp::R_ARM_MOVW_PREL_NC:
4814 case elfcpp::R_ARM_MOVT_PREL:
4815 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4816 case elfcpp::R_ARM_THM_MOVT_PREL:
4819 case elfcpp::R_ARM_GOTOFF32:
4820 // We need a GOT section:
4821 target->got_section(symtab, layout);
4824 case elfcpp::R_ARM_BASE_PREL:
4825 // FIXME: What about this?
4828 case elfcpp::R_ARM_GOT_BREL:
4829 case elfcpp::R_ARM_GOT_PREL:
4831 // The symbol requires a GOT entry.
4832 Output_data_got<32, big_endian>* got =
4833 target->got_section(symtab, layout);
4834 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4835 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
4837 // If we are generating a shared object, we need to add a
4838 // dynamic RELATIVE relocation for this symbol's GOT entry.
4839 if (parameters->options().output_is_position_independent())
4841 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4842 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4843 rel_dyn->add_local_relative(
4844 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
4845 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
4851 case elfcpp::R_ARM_TARGET1:
4852 // This should have been mapped to another type already.
4854 case elfcpp::R_ARM_COPY:
4855 case elfcpp::R_ARM_GLOB_DAT:
4856 case elfcpp::R_ARM_JUMP_SLOT:
4857 case elfcpp::R_ARM_RELATIVE:
4858 // These are relocations which should only be seen by the
4859 // dynamic linker, and should never be seen here.
4860 gold_error(_("%s: unexpected reloc %u in object file"),
4861 object->name().c_str(), r_type);
4865 unsupported_reloc_local(object, r_type);
4870 // Report an unsupported relocation against a global symbol.
4872 template<bool big_endian>
4874 Target_arm<big_endian>::Scan::unsupported_reloc_global(
4875 Sized_relobj<32, big_endian>* object,
4876 unsigned int r_type,
4879 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4880 object->name().c_str(), r_type, gsym->demangled_name().c_str());
4883 // Scan a relocation for a global symbol.
4884 // FIXME: This only handles a subset of relocation types used by Android
4885 // on ARM v5te devices.
4887 template<bool big_endian>
4889 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
4892 Sized_relobj<32, big_endian>* object,
4893 unsigned int data_shndx,
4894 Output_section* output_section,
4895 const elfcpp::Rel<32, big_endian>& reloc,
4896 unsigned int r_type,
4899 r_type = get_real_reloc_type(r_type);
4902 case elfcpp::R_ARM_NONE:
4905 case elfcpp::R_ARM_ABS32:
4906 case elfcpp::R_ARM_ABS32_NOI:
4908 // Make a dynamic relocation if necessary.
4909 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
4911 if (target->may_need_copy_reloc(gsym))
4913 target->copy_reloc(symtab, layout, object,
4914 data_shndx, output_section, gsym, reloc);
4916 else if (gsym->can_use_relative_reloc(false))
4918 // If we are to add more other reloc types than R_ARM_ABS32,
4919 // we need to add check_non_pic(object, r_type) here.
4920 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4921 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
4922 output_section, object,
4923 data_shndx, reloc.get_r_offset());
4927 // If we are to add more other reloc types than R_ARM_ABS32,
4928 // we need to add check_non_pic(object, r_type) here.
4929 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4930 rel_dyn->add_global(gsym, r_type, output_section, object,
4931 data_shndx, reloc.get_r_offset());
4937 case elfcpp::R_ARM_MOVW_ABS_NC:
4938 case elfcpp::R_ARM_MOVT_ABS:
4939 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4940 case elfcpp::R_ARM_THM_MOVT_ABS:
4941 case elfcpp::R_ARM_MOVW_PREL_NC:
4942 case elfcpp::R_ARM_MOVT_PREL:
4943 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4944 case elfcpp::R_ARM_THM_MOVT_PREL:
4947 case elfcpp::R_ARM_THM_ABS5:
4948 case elfcpp::R_ARM_ABS8:
4949 case elfcpp::R_ARM_ABS12:
4950 case elfcpp::R_ARM_ABS16:
4951 case elfcpp::R_ARM_BASE_ABS:
4953 // No dynamic relocs of this kinds.
4954 // Report the error in case of PIC.
4955 int flags = Symbol::NON_PIC_REF;
4956 if (gsym->type() == elfcpp::STT_FUNC
4957 || gsym->type() == elfcpp::STT_ARM_TFUNC)
4958 flags |= Symbol::FUNCTION_CALL;
4959 if (gsym->needs_dynamic_reloc(flags))
4960 check_non_pic(object, r_type);
4964 case elfcpp::R_ARM_REL32:
4965 case elfcpp::R_ARM_PREL31:
4967 // Make a dynamic relocation if necessary.
4968 int flags = Symbol::NON_PIC_REF;
4969 if (gsym->needs_dynamic_reloc(flags))
4971 if (target->may_need_copy_reloc(gsym))
4973 target->copy_reloc(symtab, layout, object,
4974 data_shndx, output_section, gsym, reloc);
4978 check_non_pic(object, r_type);
4979 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4980 rel_dyn->add_global(gsym, r_type, output_section, object,
4981 data_shndx, reloc.get_r_offset());
4987 case elfcpp::R_ARM_JUMP24:
4988 case elfcpp::R_ARM_THM_JUMP24:
4989 case elfcpp::R_ARM_CALL:
4990 case elfcpp::R_ARM_THM_CALL:
4992 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
4993 target->make_plt_entry(symtab, layout, gsym);
4996 // Check to see if this is a function that would need a PLT
4997 // but does not get one because the function symbol is untyped.
4998 // This happens in assembly code missing a proper .type directive.
4999 if ((!gsym->is_undefined() || parameters->options().shared())
5000 && !parameters->doing_static_link()
5001 && gsym->type() == elfcpp::STT_NOTYPE
5002 && (gsym->is_from_dynobj()
5003 || gsym->is_undefined()
5004 || gsym->is_preemptible()))
5005 gold_error(_("%s is not a function."),
5006 gsym->demangled_name().c_str());
5010 case elfcpp::R_ARM_PLT32:
5011 // If the symbol is fully resolved, this is just a relative
5012 // local reloc. Otherwise we need a PLT entry.
5013 if (gsym->final_value_is_known())
5015 // If building a shared library, we can also skip the PLT entry
5016 // if the symbol is defined in the output file and is protected
5018 if (gsym->is_defined()
5019 && !gsym->is_from_dynobj()
5020 && !gsym->is_preemptible())
5022 target->make_plt_entry(symtab, layout, gsym);
5025 case elfcpp::R_ARM_GOTOFF32:
5026 // We need a GOT section.
5027 target->got_section(symtab, layout);
5030 case elfcpp::R_ARM_BASE_PREL:
5031 // FIXME: What about this?
5034 case elfcpp::R_ARM_GOT_BREL:
5035 case elfcpp::R_ARM_GOT_PREL:
5037 // The symbol requires a GOT entry.
5038 Output_data_got<32, big_endian>* got =
5039 target->got_section(symtab, layout);
5040 if (gsym->final_value_is_known())
5041 got->add_global(gsym, GOT_TYPE_STANDARD);
5044 // If this symbol is not fully resolved, we need to add a
5045 // GOT entry with a dynamic relocation.
5046 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5047 if (gsym->is_from_dynobj()
5048 || gsym->is_undefined()
5049 || gsym->is_preemptible())
5050 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
5051 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
5054 if (got->add_global(gsym, GOT_TYPE_STANDARD))
5055 rel_dyn->add_global_relative(
5056 gsym, elfcpp::R_ARM_RELATIVE, got,
5057 gsym->got_offset(GOT_TYPE_STANDARD));
5063 case elfcpp::R_ARM_TARGET1:
5064 // This should have been mapped to another type already.
5066 case elfcpp::R_ARM_COPY:
5067 case elfcpp::R_ARM_GLOB_DAT:
5068 case elfcpp::R_ARM_JUMP_SLOT:
5069 case elfcpp::R_ARM_RELATIVE:
5070 // These are relocations which should only be seen by the
5071 // dynamic linker, and should never be seen here.
5072 gold_error(_("%s: unexpected reloc %u in object file"),
5073 object->name().c_str(), r_type);
5077 unsupported_reloc_global(object, r_type, gsym);
5082 // Process relocations for gc.
5084 template<bool big_endian>
5086 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
5088 Sized_relobj<32, big_endian>* object,
5089 unsigned int data_shndx,
5091 const unsigned char* prelocs,
5093 Output_section* output_section,
5094 bool needs_special_offset_handling,
5095 size_t local_symbol_count,
5096 const unsigned char* plocal_symbols)
5098 typedef Target_arm<big_endian> Arm;
5099 typedef typename Target_arm<big_endian>::Scan Scan;
5101 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
5110 needs_special_offset_handling,
5115 // Scan relocations for a section.
5117 template<bool big_endian>
5119 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
5121 Sized_relobj<32, big_endian>* object,
5122 unsigned int data_shndx,
5123 unsigned int sh_type,
5124 const unsigned char* prelocs,
5126 Output_section* output_section,
5127 bool needs_special_offset_handling,
5128 size_t local_symbol_count,
5129 const unsigned char* plocal_symbols)
5131 typedef typename Target_arm<big_endian>::Scan Scan;
5132 if (sh_type == elfcpp::SHT_RELA)
5134 gold_error(_("%s: unsupported RELA reloc section"),
5135 object->name().c_str());
5139 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
5148 needs_special_offset_handling,
5153 // Finalize the sections.
5155 template<bool big_endian>
5157 Target_arm<big_endian>::do_finalize_sections(
5159 const Input_objects* input_objects,
5160 Symbol_table* symtab)
5162 // Merge processor-specific flags.
5163 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
5164 p != input_objects->relobj_end();
5167 Arm_relobj<big_endian>* arm_relobj =
5168 Arm_relobj<big_endian>::as_arm_relobj(*p);
5169 this->merge_processor_specific_flags(
5171 arm_relobj->processor_specific_flags());
5172 this->merge_object_attributes(arm_relobj->name().c_str(),
5173 arm_relobj->attributes_section_data());
5177 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
5178 p != input_objects->dynobj_end();
5181 Arm_dynobj<big_endian>* arm_dynobj =
5182 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
5183 this->merge_processor_specific_flags(
5185 arm_dynobj->processor_specific_flags());
5186 this->merge_object_attributes(arm_dynobj->name().c_str(),
5187 arm_dynobj->attributes_section_data());
5191 Object_attribute* attr =
5192 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
5193 if (attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
5194 this->set_may_use_blx(true);
5196 // Fill in some more dynamic tags.
5197 const Reloc_section* rel_plt = (this->plt_ == NULL
5199 : this->plt_->rel_plt());
5200 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
5201 this->rel_dyn_, true);
5203 // Emit any relocs we saved in an attempt to avoid generating COPY
5205 if (this->copy_relocs_.any_saved_relocs())
5206 this->copy_relocs_.emit(this->rel_dyn_section(layout));
5208 // Handle the .ARM.exidx section.
5209 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
5210 if (exidx_section != NULL
5211 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
5212 && !parameters->options().relocatable())
5214 // Create __exidx_start and __exdix_end symbols.
5215 symtab->define_in_output_data("__exidx_start", NULL,
5216 Symbol_table::PREDEFINED,
5217 exidx_section, 0, 0, elfcpp::STT_OBJECT,
5218 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
5220 symtab->define_in_output_data("__exidx_end", NULL,
5221 Symbol_table::PREDEFINED,
5222 exidx_section, 0, 0, elfcpp::STT_OBJECT,
5223 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
5226 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
5227 // the .ARM.exidx section.
5228 if (!layout->script_options()->saw_phdrs_clause())
5230 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
5232 Output_segment* exidx_segment =
5233 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
5234 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
5239 // Create an .ARM.attributes section if there is not one already.
5240 Output_attributes_section_data* attributes_section =
5241 new Output_attributes_section_data(*this->attributes_section_data_);
5242 layout->add_output_section_data(".ARM.attributes",
5243 elfcpp::SHT_ARM_ATTRIBUTES, 0,
5244 attributes_section, false, false, false,
5248 // Return whether a direct absolute static relocation needs to be applied.
5249 // In cases where Scan::local() or Scan::global() has created
5250 // a dynamic relocation other than R_ARM_RELATIVE, the addend
5251 // of the relocation is carried in the data, and we must not
5252 // apply the static relocation.
5254 template<bool big_endian>
5256 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
5257 const Sized_symbol<32>* gsym,
5260 Output_section* output_section)
5262 // If the output section is not allocated, then we didn't call
5263 // scan_relocs, we didn't create a dynamic reloc, and we must apply
5265 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
5268 // For local symbols, we will have created a non-RELATIVE dynamic
5269 // relocation only if (a) the output is position independent,
5270 // (b) the relocation is absolute (not pc- or segment-relative), and
5271 // (c) the relocation is not 32 bits wide.
5273 return !(parameters->options().output_is_position_independent()
5274 && (ref_flags & Symbol::ABSOLUTE_REF)
5277 // For global symbols, we use the same helper routines used in the
5278 // scan pass. If we did not create a dynamic relocation, or if we
5279 // created a RELATIVE dynamic relocation, we should apply the static
5281 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
5282 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
5283 && gsym->can_use_relative_reloc(ref_flags
5284 & Symbol::FUNCTION_CALL);
5285 return !has_dyn || is_rel;
5288 // Perform a relocation.
5290 template<bool big_endian>
5292 Target_arm<big_endian>::Relocate::relocate(
5293 const Relocate_info<32, big_endian>* relinfo,
5295 Output_section *output_section,
5297 const elfcpp::Rel<32, big_endian>& rel,
5298 unsigned int r_type,
5299 const Sized_symbol<32>* gsym,
5300 const Symbol_value<32>* psymval,
5301 unsigned char* view,
5302 Arm_address address,
5303 section_size_type /* view_size */ )
5305 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
5307 r_type = get_real_reloc_type(r_type);
5309 const Arm_relobj<big_endian>* object =
5310 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
5312 // If the final branch target of a relocation is THUMB instruction, this
5313 // is 1. Otherwise it is 0.
5314 Arm_address thumb_bit = 0;
5315 Symbol_value<32> symval;
5316 bool is_weakly_undefined_without_plt = false;
5317 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
5321 // This is a global symbol. Determine if we use PLT and if the
5322 // final target is THUMB.
5323 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
5325 // This uses a PLT, change the symbol value.
5326 symval.set_output_value(target->plt_section()->address()
5327 + gsym->plt_offset());
5330 else if (gsym->is_weak_undefined())
5332 // This is a weakly undefined symbol and we do not use PLT
5333 // for this relocation. A branch targeting this symbol will
5334 // be converted into an NOP.
5335 is_weakly_undefined_without_plt = true;
5339 // Set thumb bit if symbol:
5340 // -Has type STT_ARM_TFUNC or
5341 // -Has type STT_FUNC, is defined and with LSB in value set.
5343 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
5344 || (gsym->type() == elfcpp::STT_FUNC
5345 && !gsym->is_undefined()
5346 && ((psymval->value(object, 0) & 1) != 0)))
5353 // This is a local symbol. Determine if the final target is THUMB.
5354 // We saved this information when all the local symbols were read.
5355 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
5356 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
5357 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
5362 // This is a fake relocation synthesized for a stub. It does not have
5363 // a real symbol. We just look at the LSB of the symbol value to
5364 // determine if the target is THUMB or not.
5365 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
5368 // Strip LSB if this points to a THUMB target.
5370 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
5371 && ((psymval->value(object, 0) & 1) != 0))
5373 Arm_address stripped_value =
5374 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
5375 symval.set_output_value(stripped_value);
5379 // Get the GOT offset if needed.
5380 // The GOT pointer points to the end of the GOT section.
5381 // We need to subtract the size of the GOT section to get
5382 // the actual offset to use in the relocation.
5383 bool have_got_offset = false;
5384 unsigned int got_offset = 0;
5387 case elfcpp::R_ARM_GOT_BREL:
5388 case elfcpp::R_ARM_GOT_PREL:
5391 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5392 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
5393 - target->got_size());
5397 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5398 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5399 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
5400 - target->got_size());
5402 have_got_offset = true;
5409 // To look up relocation stubs, we need to pass the symbol table index of
5411 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5413 typename Arm_relocate_functions::Status reloc_status =
5414 Arm_relocate_functions::STATUS_OKAY;
5417 case elfcpp::R_ARM_NONE:
5420 case elfcpp::R_ARM_ABS8:
5421 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5423 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
5426 case elfcpp::R_ARM_ABS12:
5427 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5429 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
5432 case elfcpp::R_ARM_ABS16:
5433 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5435 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5438 case elfcpp::R_ARM_ABS32:
5439 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5441 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5445 case elfcpp::R_ARM_ABS32_NOI:
5446 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5448 // No thumb bit for this relocation: (S + A)
5449 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5453 case elfcpp::R_ARM_MOVW_ABS_NC:
5454 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5456 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
5460 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5461 "a shared object; recompile with -fPIC"));
5464 case elfcpp::R_ARM_MOVT_ABS:
5465 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5467 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
5469 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5470 "a shared object; recompile with -fPIC"));
5473 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5474 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5476 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
5480 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5481 "making a shared object; recompile with -fPIC"));
5484 case elfcpp::R_ARM_THM_MOVT_ABS:
5485 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5487 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
5490 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5491 "making a shared object; recompile with -fPIC"));
5494 case elfcpp::R_ARM_MOVW_PREL_NC:
5495 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
5500 case elfcpp::R_ARM_MOVT_PREL:
5501 reloc_status = Arm_relocate_functions::movt_prel(view, object,
5505 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5506 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
5511 case elfcpp::R_ARM_THM_MOVT_PREL:
5512 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
5516 case elfcpp::R_ARM_REL32:
5517 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5518 address, thumb_bit);
5521 case elfcpp::R_ARM_THM_ABS5:
5522 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5524 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
5527 case elfcpp::R_ARM_THM_CALL:
5529 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
5530 psymval, address, thumb_bit,
5531 is_weakly_undefined_without_plt);
5534 case elfcpp::R_ARM_XPC25:
5536 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
5537 psymval, address, thumb_bit,
5538 is_weakly_undefined_without_plt);
5541 case elfcpp::R_ARM_THM_XPC22:
5543 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
5544 psymval, address, thumb_bit,
5545 is_weakly_undefined_without_plt);
5548 case elfcpp::R_ARM_GOTOFF32:
5550 Arm_address got_origin;
5551 got_origin = target->got_plt_section()->address();
5552 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5553 got_origin, thumb_bit);
5557 case elfcpp::R_ARM_BASE_PREL:
5560 // Get the addressing origin of the output segment defining the
5561 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5562 gold_assert(gsym != NULL);
5563 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5564 origin = gsym->output_segment()->vaddr();
5565 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5566 origin = gsym->output_data()->address();
5569 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5570 _("cannot find origin of R_ARM_BASE_PREL"));
5573 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
5577 case elfcpp::R_ARM_BASE_ABS:
5579 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5584 // Get the addressing origin of the output segment defining
5585 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5587 // R_ARM_BASE_ABS with the NULL symbol will give the
5588 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5589 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5590 origin = target->got_plt_section()->address();
5591 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5592 origin = gsym->output_segment()->vaddr();
5593 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5594 origin = gsym->output_data()->address();
5597 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5598 _("cannot find origin of R_ARM_BASE_ABS"));
5602 reloc_status = Arm_relocate_functions::base_abs(view, origin);
5606 case elfcpp::R_ARM_GOT_BREL:
5607 gold_assert(have_got_offset);
5608 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
5611 case elfcpp::R_ARM_GOT_PREL:
5612 gold_assert(have_got_offset);
5613 // Get the address origin for GOT PLT, which is allocated right
5614 // after the GOT section, to calculate an absolute address of
5615 // the symbol GOT entry (got_origin + got_offset).
5616 Arm_address got_origin;
5617 got_origin = target->got_plt_section()->address();
5618 reloc_status = Arm_relocate_functions::got_prel(view,
5619 got_origin + got_offset,
5623 case elfcpp::R_ARM_PLT32:
5624 gold_assert(gsym == NULL
5625 || gsym->has_plt_offset()
5626 || gsym->final_value_is_known()
5627 || (gsym->is_defined()
5628 && !gsym->is_from_dynobj()
5629 && !gsym->is_preemptible()));
5631 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
5632 psymval, address, thumb_bit,
5633 is_weakly_undefined_without_plt);
5636 case elfcpp::R_ARM_CALL:
5638 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
5639 psymval, address, thumb_bit,
5640 is_weakly_undefined_without_plt);
5643 case elfcpp::R_ARM_JUMP24:
5645 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
5646 psymval, address, thumb_bit,
5647 is_weakly_undefined_without_plt);
5650 case elfcpp::R_ARM_THM_JUMP24:
5652 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
5653 psymval, address, thumb_bit,
5654 is_weakly_undefined_without_plt);
5657 case elfcpp::R_ARM_PREL31:
5658 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
5659 address, thumb_bit);
5662 case elfcpp::R_ARM_TARGET1:
5663 // This should have been mapped to another type already.
5665 case elfcpp::R_ARM_COPY:
5666 case elfcpp::R_ARM_GLOB_DAT:
5667 case elfcpp::R_ARM_JUMP_SLOT:
5668 case elfcpp::R_ARM_RELATIVE:
5669 // These are relocations which should only be seen by the
5670 // dynamic linker, and should never be seen here.
5671 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5672 _("unexpected reloc %u in object file"),
5677 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5678 _("unsupported reloc %u"),
5683 // Report any errors.
5684 switch (reloc_status)
5686 case Arm_relocate_functions::STATUS_OKAY:
5688 case Arm_relocate_functions::STATUS_OVERFLOW:
5689 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5690 _("relocation overflow in relocation %u"),
5693 case Arm_relocate_functions::STATUS_BAD_RELOC:
5694 gold_error_at_location(
5698 _("unexpected opcode while processing relocation %u"),
5708 // Relocate section data.
5710 template<bool big_endian>
5712 Target_arm<big_endian>::relocate_section(
5713 const Relocate_info<32, big_endian>* relinfo,
5714 unsigned int sh_type,
5715 const unsigned char* prelocs,
5717 Output_section* output_section,
5718 bool needs_special_offset_handling,
5719 unsigned char* view,
5720 Arm_address address,
5721 section_size_type view_size,
5722 const Reloc_symbol_changes* reloc_symbol_changes)
5724 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
5725 gold_assert(sh_type == elfcpp::SHT_REL);
5727 Arm_input_section<big_endian>* arm_input_section =
5728 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
5730 // This is an ARM input section and the view covers the whole output
5732 if (arm_input_section != NULL)
5734 gold_assert(needs_special_offset_handling);
5735 Arm_address section_address = arm_input_section->address();
5736 section_size_type section_size = arm_input_section->data_size();
5738 gold_assert((arm_input_section->address() >= address)
5739 && ((arm_input_section->address()
5740 + arm_input_section->data_size())
5741 <= (address + view_size)));
5743 off_t offset = section_address - address;
5746 view_size = section_size;
5749 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
5756 needs_special_offset_handling,
5760 reloc_symbol_changes);
5763 // Return the size of a relocation while scanning during a relocatable
5766 template<bool big_endian>
5768 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
5769 unsigned int r_type,
5772 r_type = get_real_reloc_type(r_type);
5775 case elfcpp::R_ARM_NONE:
5778 case elfcpp::R_ARM_ABS8:
5781 case elfcpp::R_ARM_ABS16:
5782 case elfcpp::R_ARM_THM_ABS5:
5785 case elfcpp::R_ARM_ABS32:
5786 case elfcpp::R_ARM_ABS32_NOI:
5787 case elfcpp::R_ARM_ABS12:
5788 case elfcpp::R_ARM_BASE_ABS:
5789 case elfcpp::R_ARM_REL32:
5790 case elfcpp::R_ARM_THM_CALL:
5791 case elfcpp::R_ARM_GOTOFF32:
5792 case elfcpp::R_ARM_BASE_PREL:
5793 case elfcpp::R_ARM_GOT_BREL:
5794 case elfcpp::R_ARM_GOT_PREL:
5795 case elfcpp::R_ARM_PLT32:
5796 case elfcpp::R_ARM_CALL:
5797 case elfcpp::R_ARM_JUMP24:
5798 case elfcpp::R_ARM_PREL31:
5799 case elfcpp::R_ARM_MOVW_ABS_NC:
5800 case elfcpp::R_ARM_MOVT_ABS:
5801 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5802 case elfcpp::R_ARM_THM_MOVT_ABS:
5803 case elfcpp::R_ARM_MOVW_PREL_NC:
5804 case elfcpp::R_ARM_MOVT_PREL:
5805 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5806 case elfcpp::R_ARM_THM_MOVT_PREL:
5809 case elfcpp::R_ARM_TARGET1:
5810 // This should have been mapped to another type already.
5812 case elfcpp::R_ARM_COPY:
5813 case elfcpp::R_ARM_GLOB_DAT:
5814 case elfcpp::R_ARM_JUMP_SLOT:
5815 case elfcpp::R_ARM_RELATIVE:
5816 // These are relocations which should only be seen by the
5817 // dynamic linker, and should never be seen here.
5818 gold_error(_("%s: unexpected reloc %u in object file"),
5819 object->name().c_str(), r_type);
5823 object->error(_("unsupported reloc %u in object file"), r_type);
5828 // Scan the relocs during a relocatable link.
5830 template<bool big_endian>
5832 Target_arm<big_endian>::scan_relocatable_relocs(
5833 Symbol_table* symtab,
5835 Sized_relobj<32, big_endian>* object,
5836 unsigned int data_shndx,
5837 unsigned int sh_type,
5838 const unsigned char* prelocs,
5840 Output_section* output_section,
5841 bool needs_special_offset_handling,
5842 size_t local_symbol_count,
5843 const unsigned char* plocal_symbols,
5844 Relocatable_relocs* rr)
5846 gold_assert(sh_type == elfcpp::SHT_REL);
5848 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
5849 Relocatable_size_for_reloc> Scan_relocatable_relocs;
5851 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
5852 Scan_relocatable_relocs>(
5860 needs_special_offset_handling,
5866 // Relocate a section during a relocatable link.
5868 template<bool big_endian>
5870 Target_arm<big_endian>::relocate_for_relocatable(
5871 const Relocate_info<32, big_endian>* relinfo,
5872 unsigned int sh_type,
5873 const unsigned char* prelocs,
5875 Output_section* output_section,
5876 off_t offset_in_output_section,
5877 const Relocatable_relocs* rr,
5878 unsigned char* view,
5879 Arm_address view_address,
5880 section_size_type view_size,
5881 unsigned char* reloc_view,
5882 section_size_type reloc_view_size)
5884 gold_assert(sh_type == elfcpp::SHT_REL);
5886 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
5891 offset_in_output_section,
5900 // Return the value to use for a dynamic symbol which requires special
5901 // treatment. This is how we support equality comparisons of function
5902 // pointers across shared library boundaries, as described in the
5903 // processor specific ABI supplement.
5905 template<bool big_endian>
5907 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
5909 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5910 return this->plt_section()->address() + gsym->plt_offset();
5913 // Map platform-specific relocs to real relocs
5915 template<bool big_endian>
5917 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
5921 case elfcpp::R_ARM_TARGET1:
5922 // This is either R_ARM_ABS32 or R_ARM_REL32;
5923 return elfcpp::R_ARM_ABS32;
5925 case elfcpp::R_ARM_TARGET2:
5926 // This can be any reloc type but ususally is R_ARM_GOT_PREL
5927 return elfcpp::R_ARM_GOT_PREL;
5934 // Whether if two EABI versions V1 and V2 are compatible.
5936 template<bool big_endian>
5938 Target_arm<big_endian>::are_eabi_versions_compatible(
5939 elfcpp::Elf_Word v1,
5940 elfcpp::Elf_Word v2)
5942 // v4 and v5 are the same spec before and after it was released,
5943 // so allow mixing them.
5944 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
5945 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
5951 // Combine FLAGS from an input object called NAME and the processor-specific
5952 // flags in the ELF header of the output. Much of this is adapted from the
5953 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5954 // in bfd/elf32-arm.c.
5956 template<bool big_endian>
5958 Target_arm<big_endian>::merge_processor_specific_flags(
5959 const std::string& name,
5960 elfcpp::Elf_Word flags)
5962 if (this->are_processor_specific_flags_set())
5964 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
5966 // Nothing to merge if flags equal to those in output.
5967 if (flags == out_flags)
5970 // Complain about various flag mismatches.
5971 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
5972 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
5973 if (!this->are_eabi_versions_compatible(version1, version2))
5974 gold_error(_("Source object %s has EABI version %d but output has "
5975 "EABI version %d."),
5977 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
5978 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
5982 // If the input is the default architecture and had the default
5983 // flags then do not bother setting the flags for the output
5984 // architecture, instead allow future merges to do this. If no
5985 // future merges ever set these flags then they will retain their
5986 // uninitialised values, which surprise surprise, correspond
5987 // to the default values.
5991 // This is the first time, just copy the flags.
5992 // We only copy the EABI version for now.
5993 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
5997 // Adjust ELF file header.
5998 template<bool big_endian>
6000 Target_arm<big_endian>::do_adjust_elf_header(
6001 unsigned char* view,
6004 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
6006 elfcpp::Ehdr<32, big_endian> ehdr(view);
6007 unsigned char e_ident[elfcpp::EI_NIDENT];
6008 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
6010 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
6011 == elfcpp::EF_ARM_EABI_UNKNOWN)
6012 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
6014 e_ident[elfcpp::EI_OSABI] = 0;
6015 e_ident[elfcpp::EI_ABIVERSION] = 0;
6017 // FIXME: Do EF_ARM_BE8 adjustment.
6019 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
6020 oehdr.put_e_ident(e_ident);
6023 // do_make_elf_object to override the same function in the base class.
6024 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
6025 // to store ARM specific information. Hence we need to have our own
6026 // ELF object creation.
6028 template<bool big_endian>
6030 Target_arm<big_endian>::do_make_elf_object(
6031 const std::string& name,
6032 Input_file* input_file,
6033 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
6035 int et = ehdr.get_e_type();
6036 if (et == elfcpp::ET_REL)
6038 Arm_relobj<big_endian>* obj =
6039 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
6043 else if (et == elfcpp::ET_DYN)
6045 Sized_dynobj<32, big_endian>* obj =
6046 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
6052 gold_error(_("%s: unsupported ELF file type %d"),
6058 // Read the architecture from the Tag_also_compatible_with attribute, if any.
6059 // Returns -1 if no architecture could be read.
6060 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
6062 template<bool big_endian>
6064 Target_arm<big_endian>::get_secondary_compatible_arch(
6065 const Attributes_section_data* pasd)
6067 const Object_attribute *known_attributes =
6068 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
6070 // Note: the tag and its argument below are uleb128 values, though
6071 // currently-defined values fit in one byte for each.
6072 const std::string& sv =
6073 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
6075 && sv.data()[0] == elfcpp::Tag_CPU_arch
6076 && (sv.data()[1] & 128) != 128)
6077 return sv.data()[1];
6079 // This tag is "safely ignorable", so don't complain if it looks funny.
6083 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
6084 // The tag is removed if ARCH is -1.
6085 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
6087 template<bool big_endian>
6089 Target_arm<big_endian>::set_secondary_compatible_arch(
6090 Attributes_section_data* pasd,
6093 Object_attribute *known_attributes =
6094 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
6098 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
6102 // Note: the tag and its argument below are uleb128 values, though
6103 // currently-defined values fit in one byte for each.
6105 sv[0] = elfcpp::Tag_CPU_arch;
6106 gold_assert(arch != 0);
6110 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
6113 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
6115 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
6117 template<bool big_endian>
6119 Target_arm<big_endian>::tag_cpu_arch_combine(
6122 int* secondary_compat_out,
6124 int secondary_compat)
6126 #define T(X) elfcpp::TAG_CPU_ARCH_##X
6127 static const int v6t2[] =
6139 static const int v6k[] =
6152 static const int v7[] =
6166 static const int v6_m[] =
6181 static const int v6s_m[] =
6197 static const int v7e_m[] =
6214 static const int v4t_plus_v6_m[] =
6230 T(V4T_PLUS_V6_M) // V4T plus V6_M.
6232 static const int *comb[] =
6240 // Pseudo-architecture.
6244 // Check we've not got a higher architecture than we know about.
6246 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
6248 gold_error(_("%s: unknown CPU architecture"), name);
6252 // Override old tag if we have a Tag_also_compatible_with on the output.
6254 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
6255 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
6256 oldtag = T(V4T_PLUS_V6_M);
6258 // And override the new tag if we have a Tag_also_compatible_with on the
6261 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
6262 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
6263 newtag = T(V4T_PLUS_V6_M);
6265 // Architectures before V6KZ add features monotonically.
6266 int tagh = std::max(oldtag, newtag);
6267 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
6270 int tagl = std::min(oldtag, newtag);
6271 int result = comb[tagh - T(V6T2)][tagl];
6273 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
6274 // as the canonical version.
6275 if (result == T(V4T_PLUS_V6_M))
6278 *secondary_compat_out = T(V6_M);
6281 *secondary_compat_out = -1;
6285 gold_error(_("%s: conflicting CPU architectures %d/%d"),
6286 name, oldtag, newtag);
6294 // Helper to print AEABI enum tag value.
6296 template<bool big_endian>
6298 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
6300 static const char *aeabi_enum_names[] =
6301 { "", "variable-size", "32-bit", "" };
6302 const size_t aeabi_enum_names_size =
6303 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
6305 if (value < aeabi_enum_names_size)
6306 return std::string(aeabi_enum_names[value]);
6310 sprintf(buffer, "<unknown value %u>", value);
6311 return std::string(buffer);
6315 // Return the string value to store in TAG_CPU_name.
6317 template<bool big_endian>
6319 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
6321 static const char *name_table[] = {
6322 // These aren't real CPU names, but we can't guess
6323 // that from the architecture version alone.
6339 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
6341 if (value < name_table_size)
6342 return std::string(name_table[value]);
6346 sprintf(buffer, "<unknown CPU value %u>", value);
6347 return std::string(buffer);
6351 // Merge object attributes from input file called NAME with those of the
6352 // output. The input object attributes are in the object pointed by PASD.
6354 template<bool big_endian>
6356 Target_arm<big_endian>::merge_object_attributes(
6358 const Attributes_section_data* pasd)
6360 // Return if there is no attributes section data.
6364 // If output has no object attributes, just copy.
6365 if (this->attributes_section_data_ == NULL)
6367 this->attributes_section_data_ = new Attributes_section_data(*pasd);
6371 const int vendor = Object_attribute::OBJ_ATTR_PROC;
6372 const Object_attribute* in_attr = pasd->known_attributes(vendor);
6373 Object_attribute* out_attr =
6374 this->attributes_section_data_->known_attributes(vendor);
6376 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6377 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
6378 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
6380 // Ignore mismatches if the object doesn't use floating point. */
6381 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
6382 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
6383 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
6384 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
6385 gold_error(_("%s uses VFP register arguments, output does not"),
6389 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
6391 // Merge this attribute with existing attributes.
6394 case elfcpp::Tag_CPU_raw_name:
6395 case elfcpp::Tag_CPU_name:
6396 // These are merged after Tag_CPU_arch.
6399 case elfcpp::Tag_ABI_optimization_goals:
6400 case elfcpp::Tag_ABI_FP_optimization_goals:
6401 // Use the first value seen.
6404 case elfcpp::Tag_CPU_arch:
6406 unsigned int saved_out_attr = out_attr->int_value();
6407 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6408 int secondary_compat =
6409 this->get_secondary_compatible_arch(pasd);
6410 int secondary_compat_out =
6411 this->get_secondary_compatible_arch(
6412 this->attributes_section_data_);
6413 out_attr[i].set_int_value(
6414 tag_cpu_arch_combine(name, out_attr[i].int_value(),
6415 &secondary_compat_out,
6416 in_attr[i].int_value(),
6418 this->set_secondary_compatible_arch(this->attributes_section_data_,
6419 secondary_compat_out);
6421 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6422 if (out_attr[i].int_value() == saved_out_attr)
6423 ; // Leave the names alone.
6424 else if (out_attr[i].int_value() == in_attr[i].int_value())
6426 // The output architecture has been changed to match the
6427 // input architecture. Use the input names.
6428 out_attr[elfcpp::Tag_CPU_name].set_string_value(
6429 in_attr[elfcpp::Tag_CPU_name].string_value());
6430 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
6431 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
6435 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
6436 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
6439 // If we still don't have a value for Tag_CPU_name,
6440 // make one up now. Tag_CPU_raw_name remains blank.
6441 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
6443 const std::string cpu_name =
6444 this->tag_cpu_name_value(out_attr[i].int_value());
6445 // FIXME: If we see an unknown CPU, this will be set
6446 // to "<unknown CPU n>", where n is the attribute value.
6447 // This is different from BFD, which leaves the name alone.
6448 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
6453 case elfcpp::Tag_ARM_ISA_use:
6454 case elfcpp::Tag_THUMB_ISA_use:
6455 case elfcpp::Tag_WMMX_arch:
6456 case elfcpp::Tag_Advanced_SIMD_arch:
6457 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6458 case elfcpp::Tag_ABI_FP_rounding:
6459 case elfcpp::Tag_ABI_FP_exceptions:
6460 case elfcpp::Tag_ABI_FP_user_exceptions:
6461 case elfcpp::Tag_ABI_FP_number_model:
6462 case elfcpp::Tag_VFP_HP_extension:
6463 case elfcpp::Tag_CPU_unaligned_access:
6464 case elfcpp::Tag_T2EE_use:
6465 case elfcpp::Tag_Virtualization_use:
6466 case elfcpp::Tag_MPextension_use:
6467 // Use the largest value specified.
6468 if (in_attr[i].int_value() > out_attr[i].int_value())
6469 out_attr[i].set_int_value(in_attr[i].int_value());
6472 case elfcpp::Tag_ABI_align8_preserved:
6473 case elfcpp::Tag_ABI_PCS_RO_data:
6474 // Use the smallest value specified.
6475 if (in_attr[i].int_value() < out_attr[i].int_value())
6476 out_attr[i].set_int_value(in_attr[i].int_value());
6479 case elfcpp::Tag_ABI_align8_needed:
6480 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
6481 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
6482 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
6485 // This error message should be enabled once all non-conformant
6486 // binaries in the toolchain have had the attributes set
6488 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6492 case elfcpp::Tag_ABI_FP_denormal:
6493 case elfcpp::Tag_ABI_PCS_GOT_use:
6495 // These tags have 0 = don't care, 1 = strong requirement,
6496 // 2 = weak requirement.
6497 static const int order_021[3] = {0, 2, 1};
6499 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6500 // value if greater than 2 (for future-proofing).
6501 if ((in_attr[i].int_value() > 2
6502 && in_attr[i].int_value() > out_attr[i].int_value())
6503 || (in_attr[i].int_value() <= 2
6504 && out_attr[i].int_value() <= 2
6505 && (order_021[in_attr[i].int_value()]
6506 > order_021[out_attr[i].int_value()])))
6507 out_attr[i].set_int_value(in_attr[i].int_value());
6511 case elfcpp::Tag_CPU_arch_profile:
6512 if (out_attr[i].int_value() != in_attr[i].int_value())
6514 // 0 will merge with anything.
6515 // 'A' and 'S' merge to 'A'.
6516 // 'R' and 'S' merge to 'R'.
6517 // 'M' and 'A|R|S' is an error.
6518 if (out_attr[i].int_value() == 0
6519 || (out_attr[i].int_value() == 'S'
6520 && (in_attr[i].int_value() == 'A'
6521 || in_attr[i].int_value() == 'R')))
6522 out_attr[i].set_int_value(in_attr[i].int_value());
6523 else if (in_attr[i].int_value() == 0
6524 || (in_attr[i].int_value() == 'S'
6525 && (out_attr[i].int_value() == 'A'
6526 || out_attr[i].int_value() == 'R')))
6531 (_("conflicting architecture profiles %c/%c"),
6532 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
6533 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
6537 case elfcpp::Tag_VFP_arch:
6554 // Values greater than 6 aren't defined, so just pick the
6556 if (in_attr[i].int_value() > 6
6557 && in_attr[i].int_value() > out_attr[i].int_value())
6559 *out_attr = *in_attr;
6562 // The output uses the superset of input features
6563 // (ISA version) and registers.
6564 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
6565 vfp_versions[out_attr[i].int_value()].ver);
6566 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
6567 vfp_versions[out_attr[i].int_value()].regs);
6568 // This assumes all possible supersets are also a valid
6571 for (newval = 6; newval > 0; newval--)
6573 if (regs == vfp_versions[newval].regs
6574 && ver == vfp_versions[newval].ver)
6577 out_attr[i].set_int_value(newval);
6580 case elfcpp::Tag_PCS_config:
6581 if (out_attr[i].int_value() == 0)
6582 out_attr[i].set_int_value(in_attr[i].int_value());
6583 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6585 // It's sometimes ok to mix different configs, so this is only
6587 gold_warning(_("%s: conflicting platform configuration"), name);
6590 case elfcpp::Tag_ABI_PCS_R9_use:
6591 if (in_attr[i].int_value() != out_attr[i].int_value()
6592 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
6593 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
6595 gold_error(_("%s: conflicting use of R9"), name);
6597 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
6598 out_attr[i].set_int_value(in_attr[i].int_value());
6600 case elfcpp::Tag_ABI_PCS_RW_data:
6601 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6602 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6603 != elfcpp::AEABI_R9_SB)
6604 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6605 != elfcpp::AEABI_R9_unused))
6607 gold_error(_("%s: SB relative addressing conflicts with use "
6611 // Use the smallest value specified.
6612 if (in_attr[i].int_value() < out_attr[i].int_value())
6613 out_attr[i].set_int_value(in_attr[i].int_value());
6615 case elfcpp::Tag_ABI_PCS_wchar_t:
6616 // FIXME: Make it possible to turn off this warning.
6617 if (out_attr[i].int_value()
6618 && in_attr[i].int_value()
6619 && out_attr[i].int_value() != in_attr[i].int_value())
6621 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6622 "use %u-byte wchar_t; use of wchar_t values "
6623 "across objects may fail"),
6624 name, in_attr[i].int_value(),
6625 out_attr[i].int_value());
6627 else if (in_attr[i].int_value() && !out_attr[i].int_value())
6628 out_attr[i].set_int_value(in_attr[i].int_value());
6630 case elfcpp::Tag_ABI_enum_size:
6631 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
6633 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
6634 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
6636 // The existing object is compatible with anything.
6637 // Use whatever requirements the new object has.
6638 out_attr[i].set_int_value(in_attr[i].int_value());
6640 // FIXME: Make it possible to turn off this warning.
6641 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
6642 && out_attr[i].int_value() != in_attr[i].int_value())
6644 unsigned int in_value = in_attr[i].int_value();
6645 unsigned int out_value = out_attr[i].int_value();
6646 gold_warning(_("%s uses %s enums yet the output is to use "
6647 "%s enums; use of enum values across objects "
6650 this->aeabi_enum_name(in_value).c_str(),
6651 this->aeabi_enum_name(out_value).c_str());
6655 case elfcpp::Tag_ABI_VFP_args:
6658 case elfcpp::Tag_ABI_WMMX_args:
6659 if (in_attr[i].int_value() != out_attr[i].int_value())
6661 gold_error(_("%s uses iWMMXt register arguments, output does "
6666 case Object_attribute::Tag_compatibility:
6667 // Merged in target-independent code.
6669 case elfcpp::Tag_ABI_HardFP_use:
6670 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6671 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
6672 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
6673 out_attr[i].set_int_value(3);
6674 else if (in_attr[i].int_value() > out_attr[i].int_value())
6675 out_attr[i].set_int_value(in_attr[i].int_value());
6677 case elfcpp::Tag_ABI_FP_16bit_format:
6678 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6680 if (in_attr[i].int_value() != out_attr[i].int_value())
6681 gold_error(_("fp16 format mismatch between %s and output"),
6684 if (in_attr[i].int_value() != 0)
6685 out_attr[i].set_int_value(in_attr[i].int_value());
6688 case elfcpp::Tag_nodefaults:
6689 // This tag is set if it exists, but the value is unused (and is
6690 // typically zero). We don't actually need to do anything here -
6691 // the merge happens automatically when the type flags are merged
6694 case elfcpp::Tag_also_compatible_with:
6695 // Already done in Tag_CPU_arch.
6697 case elfcpp::Tag_conformance:
6698 // Keep the attribute if it matches. Throw it away otherwise.
6699 // No attribute means no claim to conform.
6700 if (in_attr[i].string_value() != out_attr[i].string_value())
6701 out_attr[i].set_string_value("");
6706 const char* err_object = NULL;
6708 // The "known_obj_attributes" table does contain some undefined
6709 // attributes. Ensure that there are unused.
6710 if (out_attr[i].int_value() != 0
6711 || out_attr[i].string_value() != "")
6712 err_object = "output";
6713 else if (in_attr[i].int_value() != 0
6714 || in_attr[i].string_value() != "")
6717 if (err_object != NULL)
6719 // Attribute numbers >=64 (mod 128) can be safely ignored.
6721 gold_error(_("%s: unknown mandatory EABI object attribute "
6725 gold_warning(_("%s: unknown EABI object attribute %d"),
6729 // Only pass on attributes that match in both inputs.
6730 if (!in_attr[i].matches(out_attr[i]))
6732 out_attr[i].set_int_value(0);
6733 out_attr[i].set_string_value("");
6738 // If out_attr was copied from in_attr then it won't have a type yet.
6739 if (in_attr[i].type() && !out_attr[i].type())
6740 out_attr[i].set_type(in_attr[i].type());
6743 // Merge Tag_compatibility attributes and any common GNU ones.
6744 this->attributes_section_data_->merge(name, pasd);
6746 // Check for any attributes not known on ARM.
6747 typedef Vendor_object_attributes::Other_attributes Other_attributes;
6748 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
6749 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
6750 Other_attributes* out_other_attributes =
6751 this->attributes_section_data_->other_attributes(vendor);
6752 Other_attributes::iterator out_iter = out_other_attributes->begin();
6754 while (in_iter != in_other_attributes->end()
6755 || out_iter != out_other_attributes->end())
6757 const char* err_object = NULL;
6760 // The tags for each list are in numerical order.
6761 // If the tags are equal, then merge.
6762 if (out_iter != out_other_attributes->end()
6763 && (in_iter == in_other_attributes->end()
6764 || in_iter->first > out_iter->first))
6766 // This attribute only exists in output. We can't merge, and we
6767 // don't know what the tag means, so delete it.
6768 err_object = "output";
6769 err_tag = out_iter->first;
6770 int saved_tag = out_iter->first;
6771 delete out_iter->second;
6772 out_other_attributes->erase(out_iter);
6773 out_iter = out_other_attributes->upper_bound(saved_tag);
6775 else if (in_iter != in_other_attributes->end()
6776 && (out_iter != out_other_attributes->end()
6777 || in_iter->first < out_iter->first))
6779 // This attribute only exists in input. We can't merge, and we
6780 // don't know what the tag means, so ignore it.
6782 err_tag = in_iter->first;
6785 else // The tags are equal.
6787 // As present, all attributes in the list are unknown, and
6788 // therefore can't be merged meaningfully.
6789 err_object = "output";
6790 err_tag = out_iter->first;
6792 // Only pass on attributes that match in both inputs.
6793 if (!in_iter->second->matches(*(out_iter->second)))
6795 // No match. Delete the attribute.
6796 int saved_tag = out_iter->first;
6797 delete out_iter->second;
6798 out_other_attributes->erase(out_iter);
6799 out_iter = out_other_attributes->upper_bound(saved_tag);
6803 // Matched. Keep the attribute and move to the next.
6811 // Attribute numbers >=64 (mod 128) can be safely ignored. */
6812 if ((err_tag & 127) < 64)
6814 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
6815 err_object, err_tag);
6819 gold_warning(_("%s: unknown EABI object attribute %d"),
6820 err_object, err_tag);
6826 // Return whether a relocation type used the LSB to distinguish THUMB
6828 template<bool big_endian>
6830 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
6834 case elfcpp::R_ARM_PC24:
6835 case elfcpp::R_ARM_ABS32:
6836 case elfcpp::R_ARM_REL32:
6837 case elfcpp::R_ARM_SBREL32:
6838 case elfcpp::R_ARM_THM_CALL:
6839 case elfcpp::R_ARM_GLOB_DAT:
6840 case elfcpp::R_ARM_JUMP_SLOT:
6841 case elfcpp::R_ARM_GOTOFF32:
6842 case elfcpp::R_ARM_PLT32:
6843 case elfcpp::R_ARM_CALL:
6844 case elfcpp::R_ARM_JUMP24:
6845 case elfcpp::R_ARM_THM_JUMP24:
6846 case elfcpp::R_ARM_SBREL31:
6847 case elfcpp::R_ARM_PREL31:
6848 case elfcpp::R_ARM_MOVW_ABS_NC:
6849 case elfcpp::R_ARM_MOVW_PREL_NC:
6850 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6851 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6852 case elfcpp::R_ARM_THM_JUMP19:
6853 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
6854 case elfcpp::R_ARM_ALU_PC_G0_NC:
6855 case elfcpp::R_ARM_ALU_PC_G0:
6856 case elfcpp::R_ARM_ALU_PC_G1_NC:
6857 case elfcpp::R_ARM_ALU_PC_G1:
6858 case elfcpp::R_ARM_ALU_PC_G2:
6859 case elfcpp::R_ARM_ALU_SB_G0_NC:
6860 case elfcpp::R_ARM_ALU_SB_G0:
6861 case elfcpp::R_ARM_ALU_SB_G1_NC:
6862 case elfcpp::R_ARM_ALU_SB_G1:
6863 case elfcpp::R_ARM_ALU_SB_G2:
6864 case elfcpp::R_ARM_MOVW_BREL_NC:
6865 case elfcpp::R_ARM_MOVW_BREL:
6866 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
6867 case elfcpp::R_ARM_THM_MOVW_BREL:
6874 // Stub-generation methods for Target_arm.
6876 // Make a new Arm_input_section object.
6878 template<bool big_endian>
6879 Arm_input_section<big_endian>*
6880 Target_arm<big_endian>::new_arm_input_section(
6884 Input_section_specifier iss(relobj, shndx);
6886 Arm_input_section<big_endian>* arm_input_section =
6887 new Arm_input_section<big_endian>(relobj, shndx);
6888 arm_input_section->init();
6890 // Register new Arm_input_section in map for look-up.
6891 std::pair<typename Arm_input_section_map::iterator, bool> ins =
6892 this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
6894 // Make sure that it we have not created another Arm_input_section
6895 // for this input section already.
6896 gold_assert(ins.second);
6898 return arm_input_section;
6901 // Find the Arm_input_section object corresponding to the SHNDX-th input
6902 // section of RELOBJ.
6904 template<bool big_endian>
6905 Arm_input_section<big_endian>*
6906 Target_arm<big_endian>::find_arm_input_section(
6908 unsigned int shndx) const
6910 Input_section_specifier iss(relobj, shndx);
6911 typename Arm_input_section_map::const_iterator p =
6912 this->arm_input_section_map_.find(iss);
6913 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
6916 // Make a new stub table.
6918 template<bool big_endian>
6919 Stub_table<big_endian>*
6920 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
6922 Stub_table<big_endian>* stub_table =
6923 new Stub_table<big_endian>(owner);
6924 this->stub_tables_.push_back(stub_table);
6926 stub_table->set_address(owner->address() + owner->data_size());
6927 stub_table->set_file_offset(owner->offset() + owner->data_size());
6928 stub_table->finalize_data_size();
6933 // Scan a relocation for stub generation.
6935 template<bool big_endian>
6937 Target_arm<big_endian>::scan_reloc_for_stub(
6938 const Relocate_info<32, big_endian>* relinfo,
6939 unsigned int r_type,
6940 const Sized_symbol<32>* gsym,
6942 const Symbol_value<32>* psymval,
6943 elfcpp::Elf_types<32>::Elf_Swxword addend,
6944 Arm_address address)
6946 typedef typename Target_arm<big_endian>::Relocate Relocate;
6948 const Arm_relobj<big_endian>* arm_relobj =
6949 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
6951 bool target_is_thumb;
6952 Symbol_value<32> symval;
6955 // This is a global symbol. Determine if we use PLT and if the
6956 // final target is THUMB.
6957 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
6959 // This uses a PLT, change the symbol value.
6960 symval.set_output_value(this->plt_section()->address()
6961 + gsym->plt_offset());
6963 target_is_thumb = false;
6965 else if (gsym->is_undefined())
6966 // There is no need to generate a stub symbol is undefined.
6971 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
6972 || (gsym->type() == elfcpp::STT_FUNC
6973 && !gsym->is_undefined()
6974 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
6979 // This is a local symbol. Determine if the final target is THUMB.
6980 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
6983 // Strip LSB if this points to a THUMB target.
6985 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
6986 && ((psymval->value(arm_relobj, 0) & 1) != 0))
6988 Arm_address stripped_value =
6989 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
6990 symval.set_output_value(stripped_value);
6994 // Get the symbol value.
6995 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
6997 // Owing to pipelining, the PC relative branches below actually skip
6998 // two instructions when the branch offset is 0.
6999 Arm_address destination;
7002 case elfcpp::R_ARM_CALL:
7003 case elfcpp::R_ARM_JUMP24:
7004 case elfcpp::R_ARM_PLT32:
7006 destination = value + addend + 8;
7008 case elfcpp::R_ARM_THM_CALL:
7009 case elfcpp::R_ARM_THM_XPC22:
7010 case elfcpp::R_ARM_THM_JUMP24:
7011 case elfcpp::R_ARM_THM_JUMP19:
7013 destination = value + addend + 4;
7019 Stub_type stub_type =
7020 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
7023 // This reloc does not need a stub.
7024 if (stub_type == arm_stub_none)
7027 // Try looking up an existing stub from a stub table.
7028 Stub_table<big_endian>* stub_table =
7029 arm_relobj->stub_table(relinfo->data_shndx);
7030 gold_assert(stub_table != NULL);
7032 // Locate stub by destination.
7033 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
7035 // Create a stub if there is not one already
7036 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
7039 // create a new stub and add it to stub table.
7040 stub = this->stub_factory().make_reloc_stub(stub_type);
7041 stub_table->add_reloc_stub(stub, stub_key);
7044 // Record the destination address.
7045 stub->set_destination_address(destination
7046 | (target_is_thumb ? 1 : 0));
7049 // This function scans a relocation sections for stub generation.
7050 // The template parameter Relocate must be a class type which provides
7051 // a single function, relocate(), which implements the machine
7052 // specific part of a relocation.
7054 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
7055 // SHT_REL or SHT_RELA.
7057 // PRELOCS points to the relocation data. RELOC_COUNT is the number
7058 // of relocs. OUTPUT_SECTION is the output section.
7059 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
7060 // mapped to output offsets.
7062 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
7063 // VIEW_SIZE is the size. These refer to the input section, unless
7064 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
7065 // the output section.
7067 template<bool big_endian>
7068 template<int sh_type>
7070 Target_arm<big_endian>::scan_reloc_section_for_stubs(
7071 const Relocate_info<32, big_endian>* relinfo,
7072 const unsigned char* prelocs,
7074 Output_section* output_section,
7075 bool needs_special_offset_handling,
7076 const unsigned char* view,
7077 elfcpp::Elf_types<32>::Elf_Addr view_address,
7080 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
7081 const int reloc_size =
7082 Reloc_types<sh_type, 32, big_endian>::reloc_size;
7084 Arm_relobj<big_endian>* arm_object =
7085 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
7086 unsigned int local_count = arm_object->local_symbol_count();
7088 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
7090 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7092 Reltype reloc(prelocs);
7094 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
7095 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
7096 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
7098 r_type = this->get_real_reloc_type(r_type);
7100 // Only a few relocation types need stubs.
7101 if ((r_type != elfcpp::R_ARM_CALL)
7102 && (r_type != elfcpp::R_ARM_JUMP24)
7103 && (r_type != elfcpp::R_ARM_PLT32)
7104 && (r_type != elfcpp::R_ARM_THM_CALL)
7105 && (r_type != elfcpp::R_ARM_THM_XPC22)
7106 && (r_type != elfcpp::R_ARM_THM_JUMP24)
7107 && (r_type != elfcpp::R_ARM_THM_JUMP19))
7110 section_offset_type offset =
7111 convert_to_section_size_type(reloc.get_r_offset());
7113 if (needs_special_offset_handling)
7115 offset = output_section->output_offset(relinfo->object,
7116 relinfo->data_shndx,
7123 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
7124 elfcpp::Elf_types<32>::Elf_Swxword addend =
7125 stub_addend_reader(r_type, view + offset, reloc);
7127 const Sized_symbol<32>* sym;
7129 Symbol_value<32> symval;
7130 const Symbol_value<32> *psymval;
7131 if (r_sym < local_count)
7134 psymval = arm_object->local_symbol(r_sym);
7136 // If the local symbol belongs to a section we are discarding,
7137 // and that section is a debug section, try to find the
7138 // corresponding kept section and map this symbol to its
7139 // counterpart in the kept section. The symbol must not
7140 // correspond to a section we are folding.
7142 unsigned int shndx = psymval->input_shndx(&is_ordinary);
7144 && shndx != elfcpp::SHN_UNDEF
7145 && !arm_object->is_section_included(shndx)
7146 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
7148 if (comdat_behavior == CB_UNDETERMINED)
7151 arm_object->section_name(relinfo->data_shndx);
7152 comdat_behavior = get_comdat_behavior(name.c_str());
7154 if (comdat_behavior == CB_PRETEND)
7157 typename elfcpp::Elf_types<32>::Elf_Addr value =
7158 arm_object->map_to_kept_section(shndx, &found);
7160 symval.set_output_value(value + psymval->input_value());
7162 symval.set_output_value(0);
7166 symval.set_output_value(0);
7168 symval.set_no_output_symtab_entry();
7174 const Symbol* gsym = arm_object->global_symbol(r_sym);
7175 gold_assert(gsym != NULL);
7176 if (gsym->is_forwarder())
7177 gsym = relinfo->symtab->resolve_forwards(gsym);
7179 sym = static_cast<const Sized_symbol<32>*>(gsym);
7180 if (sym->has_symtab_index())
7181 symval.set_output_symtab_index(sym->symtab_index());
7183 symval.set_no_output_symtab_entry();
7185 // We need to compute the would-be final value of this global
7187 const Symbol_table* symtab = relinfo->symtab;
7188 const Sized_symbol<32>* sized_symbol =
7189 symtab->get_sized_symbol<32>(gsym);
7190 Symbol_table::Compute_final_value_status status;
7192 symtab->compute_final_value<32>(sized_symbol, &status);
7194 // Skip this if the symbol has not output section.
7195 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
7198 symval.set_output_value(value);
7202 // If symbol is a section symbol, we don't know the actual type of
7203 // destination. Give up.
7204 if (psymval->is_section_symbol())
7207 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
7208 addend, view_address + offset);
7212 // Scan an input section for stub generation.
7214 template<bool big_endian>
7216 Target_arm<big_endian>::scan_section_for_stubs(
7217 const Relocate_info<32, big_endian>* relinfo,
7218 unsigned int sh_type,
7219 const unsigned char* prelocs,
7221 Output_section* output_section,
7222 bool needs_special_offset_handling,
7223 const unsigned char* view,
7224 Arm_address view_address,
7225 section_size_type view_size)
7227 if (sh_type == elfcpp::SHT_REL)
7228 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
7233 needs_special_offset_handling,
7237 else if (sh_type == elfcpp::SHT_RELA)
7238 // We do not support RELA type relocations yet. This is provided for
7240 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
7245 needs_special_offset_handling,
7253 // Group input sections for stub generation.
7255 // We goup input sections in an output sections so that the total size,
7256 // including any padding space due to alignment is smaller than GROUP_SIZE
7257 // unless the only input section in group is bigger than GROUP_SIZE already.
7258 // Then an ARM stub table is created to follow the last input section
7259 // in group. For each group an ARM stub table is created an is placed
7260 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
7261 // extend the group after the stub table.
7263 template<bool big_endian>
7265 Target_arm<big_endian>::group_sections(
7267 section_size_type group_size,
7268 bool stubs_always_after_branch)
7270 // Group input sections and insert stub table
7271 Layout::Section_list section_list;
7272 layout->get_allocated_sections(§ion_list);
7273 for (Layout::Section_list::const_iterator p = section_list.begin();
7274 p != section_list.end();
7277 Arm_output_section<big_endian>* output_section =
7278 Arm_output_section<big_endian>::as_arm_output_section(*p);
7279 output_section->group_sections(group_size, stubs_always_after_branch,
7284 // Relaxation hook. This is where we do stub generation.
7286 template<bool big_endian>
7288 Target_arm<big_endian>::do_relax(
7290 const Input_objects* input_objects,
7291 Symbol_table* symtab,
7294 // No need to generate stubs if this is a relocatable link.
7295 gold_assert(!parameters->options().relocatable());
7297 // If this is the first pass, we need to group input sections into
7301 // Determine the stub group size. The group size is the absolute
7302 // value of the parameter --stub-group-size. If --stub-group-size
7303 // is passed a negative value, we restict stubs to be always after
7304 // the stubbed branches.
7305 int32_t stub_group_size_param =
7306 parameters->options().stub_group_size();
7307 bool stubs_always_after_branch = stub_group_size_param < 0;
7308 section_size_type stub_group_size = abs(stub_group_size_param);
7310 if (stub_group_size == 1)
7313 // Thumb branch range is +-4MB has to be used as the default
7314 // maximum size (a given section can contain both ARM and Thumb
7315 // code, so the worst case has to be taken into account).
7317 // This value is 24K less than that, which allows for 2025
7318 // 12-byte stubs. If we exceed that, then we will fail to link.
7319 // The user will have to relink with an explicit group size
7321 stub_group_size = 4170000;
7324 group_sections(layout, stub_group_size, stubs_always_after_branch);
7327 typedef typename Stub_table_list::iterator Stub_table_iterator;
7329 // scan relocs for stubs
7330 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
7331 op != input_objects->relobj_end();
7334 Arm_relobj<big_endian>* arm_relobj =
7335 Arm_relobj<big_endian>::as_arm_relobj(*op);
7336 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
7339 // Check all stub tables to see if any of them have their data sizes
7340 // or addresses alignments changed. These are the only things that
7342 bool any_stub_table_changed = false;
7343 for (Stub_table_iterator sp = this->stub_tables_.begin();
7344 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7347 if ((*sp)->update_data_size_and_addralign())
7348 any_stub_table_changed = true;
7351 // Finalize the stubs in the last relaxation pass.
7352 if (!any_stub_table_changed)
7353 for (Stub_table_iterator sp = this->stub_tables_.begin();
7354 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7356 (*sp)->finalize_stubs();
7358 return any_stub_table_changed;
7363 template<bool big_endian>
7365 Target_arm<big_endian>::relocate_stub(
7367 const Relocate_info<32, big_endian>* relinfo,
7368 Output_section* output_section,
7369 unsigned char* view,
7370 Arm_address address,
7371 section_size_type view_size)
7374 const Stub_template* stub_template = stub->stub_template();
7375 for (size_t i = 0; i < stub_template->reloc_count(); i++)
7377 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
7378 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
7380 unsigned int r_type = insn->r_type();
7381 section_size_type reloc_offset = stub_template->reloc_offset(i);
7382 section_size_type reloc_size = insn->size();
7383 gold_assert(reloc_offset + reloc_size <= view_size);
7385 // This is the address of the stub destination.
7386 Arm_address target = stub->reloc_target(i);
7387 Symbol_value<32> symval;
7388 symval.set_output_value(target);
7390 // Synthesize a fake reloc just in case. We don't have a symbol so
7392 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
7393 memset(reloc_buffer, 0, sizeof(reloc_buffer));
7394 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
7395 reloc_write.put_r_offset(reloc_offset);
7396 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
7397 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
7399 relocate.relocate(relinfo, this, output_section,
7400 this->fake_relnum_for_stubs, rel, r_type,
7401 NULL, &symval, view + reloc_offset,
7402 address + reloc_offset, reloc_size);
7406 // Determine whether an object attribute tag takes an integer, a
7409 template<bool big_endian>
7411 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
7413 if (tag == Object_attribute::Tag_compatibility)
7414 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7415 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
7416 else if (tag == elfcpp::Tag_nodefaults)
7417 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7418 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
7419 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
7420 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
7422 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
7424 return ((tag & 1) != 0
7425 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7426 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
7429 // Reorder attributes.
7431 // The ABI defines that Tag_conformance should be emitted first, and that
7432 // Tag_nodefaults should be second (if either is defined). This sets those
7433 // two positions, and bumps up the position of all the remaining tags to
7436 template<bool big_endian>
7438 Target_arm<big_endian>::do_attributes_order(int num) const
7440 // Reorder the known object attributes in output. We want to move
7441 // Tag_conformance to position 4 and Tag_conformance to position 5
7442 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7444 return elfcpp::Tag_conformance;
7446 return elfcpp::Tag_nodefaults;
7447 if ((num - 2) < elfcpp::Tag_nodefaults)
7449 if ((num - 1) < elfcpp::Tag_conformance)
7454 template<bool big_endian>
7455 class Target_selector_arm : public Target_selector
7458 Target_selector_arm()
7459 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
7460 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
7464 do_instantiate_target()
7465 { return new Target_arm<big_endian>(); }
7468 Target_selector_arm<false> target_selector_arm;
7469 Target_selector_arm<true> target_selector_armbe;
7471 } // End anonymous namespace.