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), mapping_symbols_info_(),
1073 section_has_cortex_a8_workaround_(NULL)
1077 { delete this->attributes_section_data_; }
1079 // Return the stub table of the SHNDX-th section if there is one.
1080 Stub_table<big_endian>*
1081 stub_table(unsigned int shndx) const
1083 gold_assert(shndx < this->stub_tables_.size());
1084 return this->stub_tables_[shndx];
1087 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1089 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1091 gold_assert(shndx < this->stub_tables_.size());
1092 this->stub_tables_[shndx] = stub_table;
1095 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1096 // index. This is only valid after do_count_local_symbol is called.
1098 local_symbol_is_thumb_function(unsigned int r_sym) const
1100 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1101 return this->local_symbol_is_thumb_function_[r_sym];
1104 // Scan all relocation sections for stub generation.
1106 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1109 // Convert regular input section with index SHNDX to a relaxed section.
1111 convert_input_section_to_relaxed_section(unsigned shndx)
1113 // The stubs have relocations and we need to process them after writing
1114 // out the stubs. So relocation now must follow section write.
1115 this->invalidate_section_offset(shndx);
1116 this->set_relocs_must_follow_section_writes();
1119 // Downcast a base pointer to an Arm_relobj pointer. This is
1120 // not type-safe but we only use Arm_relobj not the base class.
1121 static Arm_relobj<big_endian>*
1122 as_arm_relobj(Relobj* relobj)
1123 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1125 // Processor-specific flags in ELF file header. This is valid only after
1128 processor_specific_flags() const
1129 { return this->processor_specific_flags_; }
1131 // Attribute section data This is the contents of the .ARM.attribute section
1133 const Attributes_section_data*
1134 attributes_section_data() const
1135 { return this->attributes_section_data_; }
1137 // Mapping symbol location.
1138 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1140 // Functor for STL container.
1141 struct Mapping_symbol_position_less
1144 operator()(const Mapping_symbol_position& p1,
1145 const Mapping_symbol_position& p2) const
1147 return (p1.first < p2.first
1148 || (p1.first == p2.first && p1.second < p2.second));
1152 // We only care about the first character of a mapping symbol, so
1153 // we only store that instead of the whole symbol name.
1154 typedef std::map<Mapping_symbol_position, char,
1155 Mapping_symbol_position_less> Mapping_symbols_info;
1157 // Whether a section contains any Cortex-A8 workaround.
1159 section_has_cortex_a8_workaround(unsigned int shndx) const
1161 return (this->section_has_cortex_a8_workaround_ != NULL
1162 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1165 // Mark a section that has Cortex-A8 workaround.
1167 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1169 if (this->section_has_cortex_a8_workaround_ == NULL)
1170 this->section_has_cortex_a8_workaround_ =
1171 new std::vector<bool>(this->shnum(), false);
1172 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1176 // Post constructor setup.
1180 // Call parent's setup method.
1181 Sized_relobj<32, big_endian>::do_setup();
1183 // Initialize look-up tables.
1184 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1185 this->stub_tables_.swap(empty_stub_table_list);
1188 // Count the local symbols.
1190 do_count_local_symbols(Stringpool_template<char>*,
1191 Stringpool_template<char>*);
1194 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1195 const unsigned char* pshdrs,
1196 typename Sized_relobj<32, big_endian>::Views* pivews);
1198 // Read the symbol information.
1200 do_read_symbols(Read_symbols_data* sd);
1202 // Process relocs for garbage collection.
1204 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1208 // Whether a section needs to be scanned for relocation stubs.
1210 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1211 const Relobj::Output_sections&,
1212 const Symbol_table *);
1214 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1216 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1217 unsigned int, Output_section*,
1218 const Symbol_table *);
1220 // Scan a section for the Cortex-A8 erratum.
1222 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1223 unsigned int, Output_section*,
1224 Target_arm<big_endian>*);
1226 // List of stub tables.
1227 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1228 Stub_table_list stub_tables_;
1229 // Bit vector to tell if a local symbol is a thumb function or not.
1230 // This is only valid after do_count_local_symbol is called.
1231 std::vector<bool> local_symbol_is_thumb_function_;
1232 // processor-specific flags in ELF file header.
1233 elfcpp::Elf_Word processor_specific_flags_;
1234 // Object attributes if there is an .ARM.attributes section or NULL.
1235 Attributes_section_data* attributes_section_data_;
1236 // Mapping symbols information.
1237 Mapping_symbols_info mapping_symbols_info_;
1238 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1239 std::vector<bool>* section_has_cortex_a8_workaround_;
1242 // Arm_dynobj class.
1244 template<bool big_endian>
1245 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1248 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1249 const elfcpp::Ehdr<32, big_endian>& ehdr)
1250 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1251 processor_specific_flags_(0), attributes_section_data_(NULL)
1255 { delete this->attributes_section_data_; }
1257 // Downcast a base pointer to an Arm_relobj pointer. This is
1258 // not type-safe but we only use Arm_relobj not the base class.
1259 static Arm_dynobj<big_endian>*
1260 as_arm_dynobj(Dynobj* dynobj)
1261 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1263 // Processor-specific flags in ELF file header. This is valid only after
1266 processor_specific_flags() const
1267 { return this->processor_specific_flags_; }
1269 // Attributes section data.
1270 const Attributes_section_data*
1271 attributes_section_data() const
1272 { return this->attributes_section_data_; }
1275 // Read the symbol information.
1277 do_read_symbols(Read_symbols_data* sd);
1280 // processor-specific flags in ELF file header.
1281 elfcpp::Elf_Word processor_specific_flags_;
1282 // Object attributes if there is an .ARM.attributes section or NULL.
1283 Attributes_section_data* attributes_section_data_;
1286 // Functor to read reloc addends during stub generation.
1288 template<int sh_type, bool big_endian>
1289 struct Stub_addend_reader
1291 // Return the addend for a relocation of a particular type. Depending
1292 // on whether this is a REL or RELA relocation, read the addend from a
1293 // view or from a Reloc object.
1294 elfcpp::Elf_types<32>::Elf_Swxword
1296 unsigned int /* r_type */,
1297 const unsigned char* /* view */,
1298 const typename Reloc_types<sh_type,
1299 32, big_endian>::Reloc& /* reloc */) const;
1302 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1304 template<bool big_endian>
1305 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1307 elfcpp::Elf_types<32>::Elf_Swxword
1310 const unsigned char*,
1311 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1314 // Specialized Stub_addend_reader for RELA type relocation sections.
1315 // We currently do not handle RELA type relocation sections but it is trivial
1316 // to implement the addend reader. This is provided for completeness and to
1317 // make it easier to add support for RELA relocation sections in the future.
1319 template<bool big_endian>
1320 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1322 elfcpp::Elf_types<32>::Elf_Swxword
1325 const unsigned char*,
1326 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1327 big_endian>::Reloc& reloc) const
1328 { return reloc.get_r_addend(); }
1331 // Cortex_a8_reloc class. We keep record of relocation that may need
1332 // the Cortex-A8 erratum workaround.
1334 class Cortex_a8_reloc
1337 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1338 Arm_address destination)
1339 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1345 // Accessors: This is a read-only class.
1347 // Return the relocation stub associated with this relocation if there is
1351 { return this->reloc_stub_; }
1353 // Return the relocation type.
1356 { return this->r_type_; }
1358 // Return the destination address of the relocation. LSB stores the THUMB
1362 { return this->destination_; }
1365 // Associated relocation stub if there is one, or NULL.
1366 const Reloc_stub* reloc_stub_;
1368 unsigned int r_type_;
1369 // Destination address of this relocation. LSB is used to distinguish
1371 Arm_address destination_;
1374 // Utilities for manipulating integers of up to 32-bits
1378 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1379 // an int32_t. NO_BITS must be between 1 to 32.
1380 template<int no_bits>
1381 static inline int32_t
1382 sign_extend(uint32_t bits)
1384 gold_assert(no_bits >= 0 && no_bits <= 32);
1386 return static_cast<int32_t>(bits);
1387 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1389 uint32_t top_bit = 1U << (no_bits - 1);
1390 int32_t as_signed = static_cast<int32_t>(bits);
1391 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1394 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1395 template<int no_bits>
1397 has_overflow(uint32_t bits)
1399 gold_assert(no_bits >= 0 && no_bits <= 32);
1402 int32_t max = (1 << (no_bits - 1)) - 1;
1403 int32_t min = -(1 << (no_bits - 1));
1404 int32_t as_signed = static_cast<int32_t>(bits);
1405 return as_signed > max || as_signed < min;
1408 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1409 // fits in the given number of bits as either a signed or unsigned value.
1410 // For example, has_signed_unsigned_overflow<8> would check
1411 // -128 <= bits <= 255
1412 template<int no_bits>
1414 has_signed_unsigned_overflow(uint32_t bits)
1416 gold_assert(no_bits >= 2 && no_bits <= 32);
1419 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1420 int32_t min = -(1 << (no_bits - 1));
1421 int32_t as_signed = static_cast<int32_t>(bits);
1422 return as_signed > max || as_signed < min;
1425 // Select bits from A and B using bits in MASK. For each n in [0..31],
1426 // the n-th bit in the result is chosen from the n-th bits of A and B.
1427 // A zero selects A and a one selects B.
1428 static inline uint32_t
1429 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1430 { return (a & ~mask) | (b & mask); }
1433 template<bool big_endian>
1434 class Target_arm : public Sized_target<32, big_endian>
1437 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1440 // When were are relocating a stub, we pass this as the relocation number.
1441 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1444 : Sized_target<32, big_endian>(&arm_info),
1445 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1446 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1447 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1448 should_force_pic_veneer_(false), arm_input_section_map_(),
1449 attributes_section_data_(NULL), fix_cortex_a8_(false),
1450 cortex_a8_relocs_info_()
1453 // Whether we can use BLX.
1456 { return this->may_use_blx_; }
1458 // Set use-BLX flag.
1460 set_may_use_blx(bool value)
1461 { this->may_use_blx_ = value; }
1463 // Whether we force PCI branch veneers.
1465 should_force_pic_veneer() const
1466 { return this->should_force_pic_veneer_; }
1468 // Set PIC veneer flag.
1470 set_should_force_pic_veneer(bool value)
1471 { this->should_force_pic_veneer_ = value; }
1473 // Whether we use THUMB-2 instructions.
1475 using_thumb2() const
1477 Object_attribute* attr =
1478 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1479 int arch = attr->int_value();
1480 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
1483 // Whether we use THUMB/THUMB-2 instructions only.
1485 using_thumb_only() const
1487 Object_attribute* attr =
1488 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1489 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1490 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1492 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1493 return attr->int_value() == 'M';
1496 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1498 may_use_arm_nop() const
1500 Object_attribute* attr =
1501 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1502 int arch = attr->int_value();
1503 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1504 || arch == elfcpp::TAG_CPU_ARCH_V6K
1505 || arch == elfcpp::TAG_CPU_ARCH_V7
1506 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1509 // Whether we have THUMB-2 NOP.W instruction.
1511 may_use_thumb2_nop() const
1513 Object_attribute* attr =
1514 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1515 int arch = attr->int_value();
1516 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1517 || arch == elfcpp::TAG_CPU_ARCH_V7
1518 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1521 // Process the relocations to determine unreferenced sections for
1522 // garbage collection.
1524 gc_process_relocs(Symbol_table* symtab,
1526 Sized_relobj<32, big_endian>* object,
1527 unsigned int data_shndx,
1528 unsigned int sh_type,
1529 const unsigned char* prelocs,
1531 Output_section* output_section,
1532 bool needs_special_offset_handling,
1533 size_t local_symbol_count,
1534 const unsigned char* plocal_symbols);
1536 // Scan the relocations to look for symbol adjustments.
1538 scan_relocs(Symbol_table* symtab,
1540 Sized_relobj<32, big_endian>* object,
1541 unsigned int data_shndx,
1542 unsigned int sh_type,
1543 const unsigned char* prelocs,
1545 Output_section* output_section,
1546 bool needs_special_offset_handling,
1547 size_t local_symbol_count,
1548 const unsigned char* plocal_symbols);
1550 // Finalize the sections.
1552 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1554 // Return the value to use for a dynamic symbol which requires special
1557 do_dynsym_value(const Symbol*) const;
1559 // Relocate a section.
1561 relocate_section(const Relocate_info<32, big_endian>*,
1562 unsigned int sh_type,
1563 const unsigned char* prelocs,
1565 Output_section* output_section,
1566 bool needs_special_offset_handling,
1567 unsigned char* view,
1568 Arm_address view_address,
1569 section_size_type view_size,
1570 const Reloc_symbol_changes*);
1572 // Scan the relocs during a relocatable link.
1574 scan_relocatable_relocs(Symbol_table* symtab,
1576 Sized_relobj<32, big_endian>* object,
1577 unsigned int data_shndx,
1578 unsigned int sh_type,
1579 const unsigned char* prelocs,
1581 Output_section* output_section,
1582 bool needs_special_offset_handling,
1583 size_t local_symbol_count,
1584 const unsigned char* plocal_symbols,
1585 Relocatable_relocs*);
1587 // Relocate a section during a relocatable link.
1589 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1590 unsigned int sh_type,
1591 const unsigned char* prelocs,
1593 Output_section* output_section,
1594 off_t offset_in_output_section,
1595 const Relocatable_relocs*,
1596 unsigned char* view,
1597 Arm_address view_address,
1598 section_size_type view_size,
1599 unsigned char* reloc_view,
1600 section_size_type reloc_view_size);
1602 // Return whether SYM is defined by the ABI.
1604 do_is_defined_by_abi(Symbol* sym) const
1605 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1607 // Return the size of the GOT section.
1611 gold_assert(this->got_ != NULL);
1612 return this->got_->data_size();
1615 // Map platform-specific reloc types
1617 get_real_reloc_type (unsigned int r_type);
1620 // Methods to support stub-generations.
1623 // Return the stub factory
1625 stub_factory() const
1626 { return this->stub_factory_; }
1628 // Make a new Arm_input_section object.
1629 Arm_input_section<big_endian>*
1630 new_arm_input_section(Relobj*, unsigned int);
1632 // Find the Arm_input_section object corresponding to the SHNDX-th input
1633 // section of RELOBJ.
1634 Arm_input_section<big_endian>*
1635 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
1637 // Make a new Stub_table
1638 Stub_table<big_endian>*
1639 new_stub_table(Arm_input_section<big_endian>*);
1641 // Scan a section for stub generation.
1643 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
1644 const unsigned char*, size_t, Output_section*,
1645 bool, const unsigned char*, Arm_address,
1650 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1651 Output_section*, unsigned char*, Arm_address,
1654 // Get the default ARM target.
1655 static Target_arm<big_endian>*
1658 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1659 && parameters->target().is_big_endian() == big_endian);
1660 return static_cast<Target_arm<big_endian>*>(
1661 parameters->sized_target<32, big_endian>());
1664 // Whether relocation type uses LSB to distinguish THUMB addresses.
1666 reloc_uses_thumb_bit(unsigned int r_type);
1668 // Whether NAME belongs to a mapping symbol.
1670 is_mapping_symbol_name(const char* name)
1674 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
1675 && (name[2] == '\0' || name[2] == '.'));
1678 // Whether we work around the Cortex-A8 erratum.
1680 fix_cortex_a8() const
1681 { return this->fix_cortex_a8_; }
1683 // Scan a span of THUMB code section for Cortex-A8 erratum.
1685 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
1686 section_size_type, section_size_type,
1687 const unsigned char*, Arm_address);
1690 // Make an ELF object.
1692 do_make_elf_object(const std::string&, Input_file*, off_t,
1693 const elfcpp::Ehdr<32, big_endian>& ehdr);
1696 do_make_elf_object(const std::string&, Input_file*, off_t,
1697 const elfcpp::Ehdr<32, !big_endian>&)
1698 { gold_unreachable(); }
1701 do_make_elf_object(const std::string&, Input_file*, off_t,
1702 const elfcpp::Ehdr<64, false>&)
1703 { gold_unreachable(); }
1706 do_make_elf_object(const std::string&, Input_file*, off_t,
1707 const elfcpp::Ehdr<64, true>&)
1708 { gold_unreachable(); }
1710 // Make an output section.
1712 do_make_output_section(const char* name, elfcpp::Elf_Word type,
1713 elfcpp::Elf_Xword flags)
1714 { return new Arm_output_section<big_endian>(name, type, flags); }
1717 do_adjust_elf_header(unsigned char* view, int len) const;
1719 // We only need to generate stubs, and hence perform relaxation if we are
1720 // not doing relocatable linking.
1722 do_may_relax() const
1723 { return !parameters->options().relocatable(); }
1726 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
1728 // Determine whether an object attribute tag takes an integer, a
1731 do_attribute_arg_type(int tag) const;
1733 // Reorder tags during output.
1735 do_attributes_order(int num) const;
1738 // The class which scans relocations.
1743 : issued_non_pic_error_(false)
1747 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1748 Sized_relobj<32, big_endian>* object,
1749 unsigned int data_shndx,
1750 Output_section* output_section,
1751 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1752 const elfcpp::Sym<32, big_endian>& lsym);
1755 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1756 Sized_relobj<32, big_endian>* object,
1757 unsigned int data_shndx,
1758 Output_section* output_section,
1759 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1764 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1765 unsigned int r_type);
1768 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1769 unsigned int r_type, Symbol*);
1772 check_non_pic(Relobj*, unsigned int r_type);
1774 // Almost identical to Symbol::needs_plt_entry except that it also
1775 // handles STT_ARM_TFUNC.
1777 symbol_needs_plt_entry(const Symbol* sym)
1779 // An undefined symbol from an executable does not need a PLT entry.
1780 if (sym->is_undefined() && !parameters->options().shared())
1783 return (!parameters->doing_static_link()
1784 && (sym->type() == elfcpp::STT_FUNC
1785 || sym->type() == elfcpp::STT_ARM_TFUNC)
1786 && (sym->is_from_dynobj()
1787 || sym->is_undefined()
1788 || sym->is_preemptible()));
1791 // Whether we have issued an error about a non-PIC compilation.
1792 bool issued_non_pic_error_;
1795 // The class which implements relocation.
1805 // Return whether the static relocation needs to be applied.
1807 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1810 Output_section* output_section);
1812 // Do a relocation. Return false if the caller should not issue
1813 // any warnings about this relocation.
1815 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1816 Output_section*, size_t relnum,
1817 const elfcpp::Rel<32, big_endian>&,
1818 unsigned int r_type, const Sized_symbol<32>*,
1819 const Symbol_value<32>*,
1820 unsigned char*, Arm_address,
1823 // Return whether we want to pass flag NON_PIC_REF for this
1824 // reloc. This means the relocation type accesses a symbol not via
1827 reloc_is_non_pic (unsigned int r_type)
1831 // These relocation types reference GOT or PLT entries explicitly.
1832 case elfcpp::R_ARM_GOT_BREL:
1833 case elfcpp::R_ARM_GOT_ABS:
1834 case elfcpp::R_ARM_GOT_PREL:
1835 case elfcpp::R_ARM_GOT_BREL12:
1836 case elfcpp::R_ARM_PLT32_ABS:
1837 case elfcpp::R_ARM_TLS_GD32:
1838 case elfcpp::R_ARM_TLS_LDM32:
1839 case elfcpp::R_ARM_TLS_IE32:
1840 case elfcpp::R_ARM_TLS_IE12GP:
1842 // These relocate types may use PLT entries.
1843 case elfcpp::R_ARM_CALL:
1844 case elfcpp::R_ARM_THM_CALL:
1845 case elfcpp::R_ARM_JUMP24:
1846 case elfcpp::R_ARM_THM_JUMP24:
1847 case elfcpp::R_ARM_THM_JUMP19:
1848 case elfcpp::R_ARM_PLT32:
1849 case elfcpp::R_ARM_THM_XPC22:
1858 // A class which returns the size required for a relocation type,
1859 // used while scanning relocs during a relocatable link.
1860 class Relocatable_size_for_reloc
1864 get_size_for_reloc(unsigned int, Relobj*);
1867 // Get the GOT section, creating it if necessary.
1868 Output_data_got<32, big_endian>*
1869 got_section(Symbol_table*, Layout*);
1871 // Get the GOT PLT section.
1873 got_plt_section() const
1875 gold_assert(this->got_plt_ != NULL);
1876 return this->got_plt_;
1879 // Create a PLT entry for a global symbol.
1881 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1883 // Get the PLT section.
1884 const Output_data_plt_arm<big_endian>*
1887 gold_assert(this->plt_ != NULL);
1891 // Get the dynamic reloc section, creating it if necessary.
1893 rel_dyn_section(Layout*);
1895 // Return true if the symbol may need a COPY relocation.
1896 // References from an executable object to non-function symbols
1897 // defined in a dynamic object may need a COPY relocation.
1899 may_need_copy_reloc(Symbol* gsym)
1901 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1902 && gsym->may_need_copy_reloc());
1905 // Add a potential copy relocation.
1907 copy_reloc(Symbol_table* symtab, Layout* layout,
1908 Sized_relobj<32, big_endian>* object,
1909 unsigned int shndx, Output_section* output_section,
1910 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1912 this->copy_relocs_.copy_reloc(symtab, layout,
1913 symtab->get_sized_symbol<32>(sym),
1914 object, shndx, output_section, reloc,
1915 this->rel_dyn_section(layout));
1918 // Whether two EABI versions are compatible.
1920 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1922 // Merge processor-specific flags from input object and those in the ELF
1923 // header of the output.
1925 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1927 // Get the secondary compatible architecture.
1929 get_secondary_compatible_arch(const Attributes_section_data*);
1931 // Set the secondary compatible architecture.
1933 set_secondary_compatible_arch(Attributes_section_data*, int);
1936 tag_cpu_arch_combine(const char*, int, int*, int, int);
1938 // Helper to print AEABI enum tag value.
1940 aeabi_enum_name(unsigned int);
1942 // Return string value for TAG_CPU_name.
1944 tag_cpu_name_value(unsigned int);
1946 // Merge object attributes from input object and those in the output.
1948 merge_object_attributes(const char*, const Attributes_section_data*);
1950 // Helper to get an AEABI object attribute
1952 get_aeabi_object_attribute(int tag) const
1954 Attributes_section_data* pasd = this->attributes_section_data_;
1955 gold_assert(pasd != NULL);
1956 Object_attribute* attr =
1957 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
1958 gold_assert(attr != NULL);
1963 // Methods to support stub-generations.
1966 // Group input sections for stub generation.
1968 group_sections(Layout*, section_size_type, bool);
1970 // Scan a relocation for stub generation.
1972 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
1973 const Sized_symbol<32>*, unsigned int,
1974 const Symbol_value<32>*,
1975 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
1977 // Scan a relocation section for stub.
1978 template<int sh_type>
1980 scan_reloc_section_for_stubs(
1981 const Relocate_info<32, big_endian>* relinfo,
1982 const unsigned char* prelocs,
1984 Output_section* output_section,
1985 bool needs_special_offset_handling,
1986 const unsigned char* view,
1987 elfcpp::Elf_types<32>::Elf_Addr view_address,
1990 // Information about this specific target which we pass to the
1991 // general Target structure.
1992 static const Target::Target_info arm_info;
1994 // The types of GOT entries needed for this platform.
1997 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
2000 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2002 // Map input section to Arm_input_section.
2003 typedef Unordered_map<Input_section_specifier,
2004 Arm_input_section<big_endian>*,
2005 Input_section_specifier::hash,
2006 Input_section_specifier::equal_to>
2007 Arm_input_section_map;
2009 // Map output addresses to relocs for Cortex-A8 erratum.
2010 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2011 Cortex_a8_relocs_info;
2014 Output_data_got<32, big_endian>* got_;
2016 Output_data_plt_arm<big_endian>* plt_;
2017 // The GOT PLT section.
2018 Output_data_space* got_plt_;
2019 // The dynamic reloc section.
2020 Reloc_section* rel_dyn_;
2021 // Relocs saved to avoid a COPY reloc.
2022 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2023 // Space for variables copied with a COPY reloc.
2024 Output_data_space* dynbss_;
2025 // Vector of Stub_tables created.
2026 Stub_table_list stub_tables_;
2028 const Stub_factory &stub_factory_;
2029 // Whether we can use BLX.
2031 // Whether we force PIC branch veneers.
2032 bool should_force_pic_veneer_;
2033 // Map for locating Arm_input_sections.
2034 Arm_input_section_map arm_input_section_map_;
2035 // Attributes section data in output.
2036 Attributes_section_data* attributes_section_data_;
2037 // Whether we want to fix code for Cortex-A8 erratum.
2038 bool fix_cortex_a8_;
2039 // Map addresses to relocs for Cortex-A8 erratum.
2040 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2043 template<bool big_endian>
2044 const Target::Target_info Target_arm<big_endian>::arm_info =
2047 big_endian, // is_big_endian
2048 elfcpp::EM_ARM, // machine_code
2049 false, // has_make_symbol
2050 false, // has_resolve
2051 false, // has_code_fill
2052 true, // is_default_stack_executable
2054 "/usr/lib/libc.so.1", // dynamic_linker
2055 0x8000, // default_text_segment_address
2056 0x1000, // abi_pagesize (overridable by -z max-page-size)
2057 0x1000, // common_pagesize (overridable by -z common-page-size)
2058 elfcpp::SHN_UNDEF, // small_common_shndx
2059 elfcpp::SHN_UNDEF, // large_common_shndx
2060 0, // small_common_section_flags
2061 0, // large_common_section_flags
2062 ".ARM.attributes", // attributes_section
2063 "aeabi" // attributes_vendor
2066 // Arm relocate functions class
2069 template<bool big_endian>
2070 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2075 STATUS_OKAY, // No error during relocation.
2076 STATUS_OVERFLOW, // Relocation oveflow.
2077 STATUS_BAD_RELOC // Relocation cannot be applied.
2081 typedef Relocate_functions<32, big_endian> Base;
2082 typedef Arm_relocate_functions<big_endian> This;
2084 // Encoding of imm16 argument for movt and movw ARM instructions
2087 // imm16 := imm4 | imm12
2089 // 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
2090 // +-------+---------------+-------+-------+-----------------------+
2091 // | | |imm4 | |imm12 |
2092 // +-------+---------------+-------+-------+-----------------------+
2094 // Extract the relocation addend from VAL based on the ARM
2095 // instruction encoding described above.
2096 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2097 extract_arm_movw_movt_addend(
2098 typename elfcpp::Swap<32, big_endian>::Valtype val)
2100 // According to the Elf ABI for ARM Architecture the immediate
2101 // field is sign-extended to form the addend.
2102 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2105 // Insert X into VAL based on the ARM instruction encoding described
2107 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2108 insert_val_arm_movw_movt(
2109 typename elfcpp::Swap<32, big_endian>::Valtype val,
2110 typename elfcpp::Swap<32, big_endian>::Valtype x)
2114 val |= (x & 0xf000) << 4;
2118 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2121 // imm16 := imm4 | i | imm3 | imm8
2123 // 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
2124 // +---------+-+-----------+-------++-+-----+-------+---------------+
2125 // | |i| |imm4 || |imm3 | |imm8 |
2126 // +---------+-+-----------+-------++-+-----+-------+---------------+
2128 // Extract the relocation addend from VAL based on the Thumb2
2129 // instruction encoding described above.
2130 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2131 extract_thumb_movw_movt_addend(
2132 typename elfcpp::Swap<32, big_endian>::Valtype val)
2134 // According to the Elf ABI for ARM Architecture the immediate
2135 // field is sign-extended to form the addend.
2136 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2137 | ((val >> 15) & 0x0800)
2138 | ((val >> 4) & 0x0700)
2142 // Insert X into VAL based on the Thumb2 instruction encoding
2144 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2145 insert_val_thumb_movw_movt(
2146 typename elfcpp::Swap<32, big_endian>::Valtype val,
2147 typename elfcpp::Swap<32, big_endian>::Valtype x)
2150 val |= (x & 0xf000) << 4;
2151 val |= (x & 0x0800) << 15;
2152 val |= (x & 0x0700) << 4;
2153 val |= (x & 0x00ff);
2157 // Handle ARM long branches.
2158 static typename This::Status
2159 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2160 unsigned char *, const Sized_symbol<32>*,
2161 const Arm_relobj<big_endian>*, unsigned int,
2162 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2164 // Handle THUMB long branches.
2165 static typename This::Status
2166 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2167 unsigned char *, const Sized_symbol<32>*,
2168 const Arm_relobj<big_endian>*, unsigned int,
2169 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2173 // Return the branch offset of a 32-bit THUMB branch.
2174 static inline int32_t
2175 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2177 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2178 // involving the J1 and J2 bits.
2179 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2180 uint32_t upper = upper_insn & 0x3ffU;
2181 uint32_t lower = lower_insn & 0x7ffU;
2182 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2183 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2184 uint32_t i1 = j1 ^ s ? 0 : 1;
2185 uint32_t i2 = j2 ^ s ? 0 : 1;
2187 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2188 | (upper << 12) | (lower << 1));
2191 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2192 // UPPER_INSN is the original upper instruction of the branch. Caller is
2193 // responsible for overflow checking and BLX offset adjustment.
2194 static inline uint16_t
2195 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2197 uint32_t s = offset < 0 ? 1 : 0;
2198 uint32_t bits = static_cast<uint32_t>(offset);
2199 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2202 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2203 // LOWER_INSN is the original lower instruction of the branch. Caller is
2204 // responsible for overflow checking and BLX offset adjustment.
2205 static inline uint16_t
2206 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2208 uint32_t s = offset < 0 ? 1 : 0;
2209 uint32_t bits = static_cast<uint32_t>(offset);
2210 return ((lower_insn & ~0x2fffU)
2211 | ((((bits >> 23) & 1) ^ !s) << 13)
2212 | ((((bits >> 22) & 1) ^ !s) << 11)
2213 | ((bits >> 1) & 0x7ffU));
2216 // Return the branch offset of a 32-bit THUMB conditional branch.
2217 static inline int32_t
2218 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2220 uint32_t s = (upper_insn & 0x0400U) >> 10;
2221 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2222 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2223 uint32_t lower = (lower_insn & 0x07ffU);
2224 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2226 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2229 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2230 // instruction. UPPER_INSN is the original upper instruction of the branch.
2231 // Caller is responsible for overflow checking.
2232 static inline uint16_t
2233 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2235 uint32_t s = offset < 0 ? 1 : 0;
2236 uint32_t bits = static_cast<uint32_t>(offset);
2237 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2240 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2241 // instruction. LOWER_INSN is the original lower instruction of the branch.
2242 // Caller is reponsible for overflow checking.
2243 static inline uint16_t
2244 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2246 uint32_t bits = static_cast<uint32_t>(offset);
2247 uint32_t j2 = (bits & 0x00080000U) >> 19;
2248 uint32_t j1 = (bits & 0x00040000U) >> 18;
2249 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2251 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2254 // R_ARM_ABS8: S + A
2255 static inline typename This::Status
2256 abs8(unsigned char *view,
2257 const Sized_relobj<32, big_endian>* object,
2258 const Symbol_value<32>* psymval)
2260 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2261 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2262 Valtype* wv = reinterpret_cast<Valtype*>(view);
2263 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2264 Reltype addend = utils::sign_extend<8>(val);
2265 Reltype x = psymval->value(object, addend);
2266 val = utils::bit_select(val, x, 0xffU);
2267 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2268 return (utils::has_signed_unsigned_overflow<8>(x)
2269 ? This::STATUS_OVERFLOW
2270 : This::STATUS_OKAY);
2273 // R_ARM_THM_ABS5: S + A
2274 static inline typename This::Status
2275 thm_abs5(unsigned char *view,
2276 const Sized_relobj<32, big_endian>* object,
2277 const Symbol_value<32>* psymval)
2279 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2280 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2281 Valtype* wv = reinterpret_cast<Valtype*>(view);
2282 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2283 Reltype addend = (val & 0x7e0U) >> 6;
2284 Reltype x = psymval->value(object, addend);
2285 val = utils::bit_select(val, x << 6, 0x7e0U);
2286 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2287 return (utils::has_overflow<5>(x)
2288 ? This::STATUS_OVERFLOW
2289 : This::STATUS_OKAY);
2292 // R_ARM_ABS12: S + A
2293 static inline typename This::Status
2294 abs12(unsigned char *view,
2295 const Sized_relobj<32, big_endian>* object,
2296 const Symbol_value<32>* psymval)
2298 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2299 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2300 Valtype* wv = reinterpret_cast<Valtype*>(view);
2301 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2302 Reltype addend = val & 0x0fffU;
2303 Reltype x = psymval->value(object, addend);
2304 val = utils::bit_select(val, x, 0x0fffU);
2305 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2306 return (utils::has_overflow<12>(x)
2307 ? This::STATUS_OVERFLOW
2308 : This::STATUS_OKAY);
2311 // R_ARM_ABS16: S + A
2312 static inline typename This::Status
2313 abs16(unsigned char *view,
2314 const Sized_relobj<32, big_endian>* object,
2315 const Symbol_value<32>* psymval)
2317 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2318 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2319 Valtype* wv = reinterpret_cast<Valtype*>(view);
2320 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2321 Reltype addend = utils::sign_extend<16>(val);
2322 Reltype x = psymval->value(object, addend);
2323 val = utils::bit_select(val, x, 0xffffU);
2324 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2325 return (utils::has_signed_unsigned_overflow<16>(x)
2326 ? This::STATUS_OVERFLOW
2327 : This::STATUS_OKAY);
2330 // R_ARM_ABS32: (S + A) | T
2331 static inline typename This::Status
2332 abs32(unsigned char *view,
2333 const Sized_relobj<32, big_endian>* object,
2334 const Symbol_value<32>* psymval,
2335 Arm_address thumb_bit)
2337 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2338 Valtype* wv = reinterpret_cast<Valtype*>(view);
2339 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2340 Valtype x = psymval->value(object, addend) | thumb_bit;
2341 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2342 return This::STATUS_OKAY;
2345 // R_ARM_REL32: (S + A) | T - P
2346 static inline typename This::Status
2347 rel32(unsigned char *view,
2348 const Sized_relobj<32, big_endian>* object,
2349 const Symbol_value<32>* psymval,
2350 Arm_address address,
2351 Arm_address thumb_bit)
2353 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2354 Valtype* wv = reinterpret_cast<Valtype*>(view);
2355 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2356 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2357 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2358 return This::STATUS_OKAY;
2361 // R_ARM_THM_CALL: (S + A) | T - P
2362 static inline typename This::Status
2363 thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2364 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2365 unsigned int r_sym, const Symbol_value<32>* psymval,
2366 Arm_address address, Arm_address thumb_bit,
2367 bool is_weakly_undefined_without_plt)
2369 return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
2370 object, r_sym, psymval, address, thumb_bit,
2371 is_weakly_undefined_without_plt);
2374 // R_ARM_THM_JUMP24: (S + A) | T - P
2375 static inline typename This::Status
2376 thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2377 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2378 unsigned int r_sym, const Symbol_value<32>* psymval,
2379 Arm_address address, Arm_address thumb_bit,
2380 bool is_weakly_undefined_without_plt)
2382 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
2383 object, r_sym, psymval, address, thumb_bit,
2384 is_weakly_undefined_without_plt);
2387 // R_ARM_THM_JUMP24: (S + A) | T - P
2388 static typename This::Status
2389 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
2390 const Symbol_value<32>* psymval, Arm_address address,
2391 Arm_address thumb_bit);
2393 // R_ARM_THM_XPC22: (S + A) | T - P
2394 static inline typename This::Status
2395 thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2396 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2397 unsigned int r_sym, const Symbol_value<32>* psymval,
2398 Arm_address address, Arm_address thumb_bit,
2399 bool is_weakly_undefined_without_plt)
2401 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
2402 object, r_sym, psymval, address, thumb_bit,
2403 is_weakly_undefined_without_plt);
2406 // R_ARM_BASE_PREL: B(S) + A - P
2407 static inline typename This::Status
2408 base_prel(unsigned char* view,
2410 Arm_address address)
2412 Base::rel32(view, origin - address);
2416 // R_ARM_BASE_ABS: B(S) + A
2417 static inline typename This::Status
2418 base_abs(unsigned char* view,
2421 Base::rel32(view, origin);
2425 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2426 static inline typename This::Status
2427 got_brel(unsigned char* view,
2428 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
2430 Base::rel32(view, got_offset);
2431 return This::STATUS_OKAY;
2434 // R_ARM_GOT_PREL: GOT(S) + A - P
2435 static inline typename This::Status
2436 got_prel(unsigned char *view,
2437 Arm_address got_entry,
2438 Arm_address address)
2440 Base::rel32(view, got_entry - address);
2441 return This::STATUS_OKAY;
2444 // R_ARM_PLT32: (S + A) | T - P
2445 static inline typename This::Status
2446 plt32(const Relocate_info<32, big_endian>* relinfo,
2447 unsigned char *view,
2448 const Sized_symbol<32>* gsym,
2449 const Arm_relobj<big_endian>* object,
2451 const Symbol_value<32>* psymval,
2452 Arm_address address,
2453 Arm_address thumb_bit,
2454 bool is_weakly_undefined_without_plt)
2456 return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
2457 object, r_sym, psymval, address, thumb_bit,
2458 is_weakly_undefined_without_plt);
2461 // R_ARM_XPC25: (S + A) | T - P
2462 static inline typename This::Status
2463 xpc25(const Relocate_info<32, big_endian>* relinfo,
2464 unsigned char *view,
2465 const Sized_symbol<32>* gsym,
2466 const Arm_relobj<big_endian>* object,
2468 const Symbol_value<32>* psymval,
2469 Arm_address address,
2470 Arm_address thumb_bit,
2471 bool is_weakly_undefined_without_plt)
2473 return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
2474 object, r_sym, psymval, address, thumb_bit,
2475 is_weakly_undefined_without_plt);
2478 // R_ARM_CALL: (S + A) | T - P
2479 static inline typename This::Status
2480 call(const Relocate_info<32, big_endian>* relinfo,
2481 unsigned char *view,
2482 const Sized_symbol<32>* gsym,
2483 const Arm_relobj<big_endian>* object,
2485 const Symbol_value<32>* psymval,
2486 Arm_address address,
2487 Arm_address thumb_bit,
2488 bool is_weakly_undefined_without_plt)
2490 return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2491 object, r_sym, psymval, address, thumb_bit,
2492 is_weakly_undefined_without_plt);
2495 // R_ARM_JUMP24: (S + A) | T - P
2496 static inline typename This::Status
2497 jump24(const Relocate_info<32, big_endian>* relinfo,
2498 unsigned char *view,
2499 const Sized_symbol<32>* gsym,
2500 const Arm_relobj<big_endian>* object,
2502 const Symbol_value<32>* psymval,
2503 Arm_address address,
2504 Arm_address thumb_bit,
2505 bool is_weakly_undefined_without_plt)
2507 return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2508 object, r_sym, psymval, address, thumb_bit,
2509 is_weakly_undefined_without_plt);
2512 // R_ARM_PREL: (S + A) | T - P
2513 static inline typename This::Status
2514 prel31(unsigned char *view,
2515 const Sized_relobj<32, big_endian>* object,
2516 const Symbol_value<32>* psymval,
2517 Arm_address address,
2518 Arm_address thumb_bit)
2520 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2521 Valtype* wv = reinterpret_cast<Valtype*>(view);
2522 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2523 Valtype addend = utils::sign_extend<31>(val);
2524 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2525 val = utils::bit_select(val, x, 0x7fffffffU);
2526 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2527 return (utils::has_overflow<31>(x) ?
2528 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2531 // R_ARM_MOVW_ABS_NC: (S + A) | T
2532 static inline typename This::Status
2533 movw_abs_nc(unsigned char *view,
2534 const Sized_relobj<32, big_endian>* object,
2535 const Symbol_value<32>* psymval,
2536 Arm_address thumb_bit)
2538 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2539 Valtype* wv = reinterpret_cast<Valtype*>(view);
2540 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2541 Valtype addend = This::extract_arm_movw_movt_addend(val);
2542 Valtype x = psymval->value(object, addend) | thumb_bit;
2543 val = This::insert_val_arm_movw_movt(val, x);
2544 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2545 return This::STATUS_OKAY;
2548 // R_ARM_MOVT_ABS: S + A
2549 static inline typename This::Status
2550 movt_abs(unsigned char *view,
2551 const Sized_relobj<32, big_endian>* object,
2552 const Symbol_value<32>* psymval)
2554 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2555 Valtype* wv = reinterpret_cast<Valtype*>(view);
2556 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2557 Valtype addend = This::extract_arm_movw_movt_addend(val);
2558 Valtype x = psymval->value(object, addend) >> 16;
2559 val = This::insert_val_arm_movw_movt(val, x);
2560 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2561 return This::STATUS_OKAY;
2564 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2565 static inline typename This::Status
2566 thm_movw_abs_nc(unsigned char *view,
2567 const Sized_relobj<32, big_endian>* object,
2568 const Symbol_value<32>* psymval,
2569 Arm_address thumb_bit)
2571 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2572 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2573 Valtype* wv = reinterpret_cast<Valtype*>(view);
2574 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2575 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2576 Reltype addend = extract_thumb_movw_movt_addend(val);
2577 Reltype x = psymval->value(object, addend) | thumb_bit;
2578 val = This::insert_val_thumb_movw_movt(val, x);
2579 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2580 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2581 return This::STATUS_OKAY;
2584 // R_ARM_THM_MOVT_ABS: S + A
2585 static inline typename This::Status
2586 thm_movt_abs(unsigned char *view,
2587 const Sized_relobj<32, big_endian>* object,
2588 const Symbol_value<32>* psymval)
2590 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2591 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2592 Valtype* wv = reinterpret_cast<Valtype*>(view);
2593 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2594 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2595 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2596 Reltype x = psymval->value(object, addend) >> 16;
2597 val = This::insert_val_thumb_movw_movt(val, x);
2598 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2599 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2600 return This::STATUS_OKAY;
2603 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2604 static inline typename This::Status
2605 movw_prel_nc(unsigned char *view,
2606 const Sized_relobj<32, big_endian>* object,
2607 const Symbol_value<32>* psymval,
2608 Arm_address address,
2609 Arm_address thumb_bit)
2611 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2612 Valtype* wv = reinterpret_cast<Valtype*>(view);
2613 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2614 Valtype addend = This::extract_arm_movw_movt_addend(val);
2615 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2616 val = This::insert_val_arm_movw_movt(val, x);
2617 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2618 return This::STATUS_OKAY;
2621 // R_ARM_MOVT_PREL: S + A - P
2622 static inline typename This::Status
2623 movt_prel(unsigned char *view,
2624 const Sized_relobj<32, big_endian>* object,
2625 const Symbol_value<32>* psymval,
2626 Arm_address address)
2628 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2629 Valtype* wv = reinterpret_cast<Valtype*>(view);
2630 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2631 Valtype addend = This::extract_arm_movw_movt_addend(val);
2632 Valtype x = (psymval->value(object, addend) - address) >> 16;
2633 val = This::insert_val_arm_movw_movt(val, x);
2634 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2635 return This::STATUS_OKAY;
2638 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2639 static inline typename This::Status
2640 thm_movw_prel_nc(unsigned char *view,
2641 const Sized_relobj<32, big_endian>* object,
2642 const Symbol_value<32>* psymval,
2643 Arm_address address,
2644 Arm_address thumb_bit)
2646 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2647 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2648 Valtype* wv = reinterpret_cast<Valtype*>(view);
2649 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2650 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2651 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2652 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
2653 val = This::insert_val_thumb_movw_movt(val, x);
2654 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2655 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2656 return This::STATUS_OKAY;
2659 // R_ARM_THM_MOVT_PREL: S + A - P
2660 static inline typename This::Status
2661 thm_movt_prel(unsigned char *view,
2662 const Sized_relobj<32, big_endian>* object,
2663 const Symbol_value<32>* psymval,
2664 Arm_address address)
2666 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2667 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2668 Valtype* wv = reinterpret_cast<Valtype*>(view);
2669 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2670 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2671 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2672 Reltype x = (psymval->value(object, addend) - address) >> 16;
2673 val = This::insert_val_thumb_movw_movt(val, x);
2674 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2675 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2676 return This::STATUS_OKAY;
2680 // Relocate ARM long branches. This handles relocation types
2681 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2682 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2683 // undefined and we do not use PLT in this relocation. In such a case,
2684 // the branch is converted into an NOP.
2686 template<bool big_endian>
2687 typename Arm_relocate_functions<big_endian>::Status
2688 Arm_relocate_functions<big_endian>::arm_branch_common(
2689 unsigned int r_type,
2690 const Relocate_info<32, big_endian>* relinfo,
2691 unsigned char *view,
2692 const Sized_symbol<32>* gsym,
2693 const Arm_relobj<big_endian>* object,
2695 const Symbol_value<32>* psymval,
2696 Arm_address address,
2697 Arm_address thumb_bit,
2698 bool is_weakly_undefined_without_plt)
2700 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2701 Valtype* wv = reinterpret_cast<Valtype*>(view);
2702 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2704 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
2705 && ((val & 0x0f000000UL) == 0x0a000000UL);
2706 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
2707 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
2708 && ((val & 0x0f000000UL) == 0x0b000000UL);
2709 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
2710 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
2712 // Check that the instruction is valid.
2713 if (r_type == elfcpp::R_ARM_CALL)
2715 if (!insn_is_uncond_bl && !insn_is_blx)
2716 return This::STATUS_BAD_RELOC;
2718 else if (r_type == elfcpp::R_ARM_JUMP24)
2720 if (!insn_is_b && !insn_is_cond_bl)
2721 return This::STATUS_BAD_RELOC;
2723 else if (r_type == elfcpp::R_ARM_PLT32)
2725 if (!insn_is_any_branch)
2726 return This::STATUS_BAD_RELOC;
2728 else if (r_type == elfcpp::R_ARM_XPC25)
2730 // FIXME: AAELF document IH0044C does not say much about it other
2731 // than it being obsolete.
2732 if (!insn_is_any_branch)
2733 return This::STATUS_BAD_RELOC;
2738 // A branch to an undefined weak symbol is turned into a jump to
2739 // the next instruction unless a PLT entry will be created.
2740 // Do the same for local undefined symbols.
2741 // The jump to the next instruction is optimized as a NOP depending
2742 // on the architecture.
2743 const Target_arm<big_endian>* arm_target =
2744 Target_arm<big_endian>::default_target();
2745 if (is_weakly_undefined_without_plt)
2747 Valtype cond = val & 0xf0000000U;
2748 if (arm_target->may_use_arm_nop())
2749 val = cond | 0x0320f000;
2751 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2752 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2753 return This::STATUS_OKAY;
2756 Valtype addend = utils::sign_extend<26>(val << 2);
2757 Valtype branch_target = psymval->value(object, addend);
2758 int32_t branch_offset = branch_target - address;
2760 // We need a stub if the branch offset is too large or if we need
2762 bool may_use_blx = arm_target->may_use_blx();
2763 Reloc_stub* stub = NULL;
2764 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
2765 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2766 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
2768 Stub_type stub_type =
2769 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2771 if (stub_type != arm_stub_none)
2773 Stub_table<big_endian>* stub_table =
2774 object->stub_table(relinfo->data_shndx);
2775 gold_assert(stub_table != NULL);
2777 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2778 stub = stub_table->find_reloc_stub(stub_key);
2779 gold_assert(stub != NULL);
2780 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2781 branch_target = stub_table->address() + stub->offset() + addend;
2782 branch_offset = branch_target - address;
2783 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
2784 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
2788 // At this point, if we still need to switch mode, the instruction
2789 // must either be a BLX or a BL that can be converted to a BLX.
2793 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
2794 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
2797 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
2798 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2799 return (utils::has_overflow<26>(branch_offset)
2800 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
2803 // Relocate THUMB long branches. This handles relocation types
2804 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2805 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2806 // undefined and we do not use PLT in this relocation. In such a case,
2807 // the branch is converted into an NOP.
2809 template<bool big_endian>
2810 typename Arm_relocate_functions<big_endian>::Status
2811 Arm_relocate_functions<big_endian>::thumb_branch_common(
2812 unsigned int r_type,
2813 const Relocate_info<32, big_endian>* relinfo,
2814 unsigned char *view,
2815 const Sized_symbol<32>* gsym,
2816 const Arm_relobj<big_endian>* object,
2818 const Symbol_value<32>* psymval,
2819 Arm_address address,
2820 Arm_address thumb_bit,
2821 bool is_weakly_undefined_without_plt)
2823 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2824 Valtype* wv = reinterpret_cast<Valtype*>(view);
2825 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2826 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2828 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2830 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
2831 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
2833 // Check that the instruction is valid.
2834 if (r_type == elfcpp::R_ARM_THM_CALL)
2836 if (!is_bl_insn && !is_blx_insn)
2837 return This::STATUS_BAD_RELOC;
2839 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
2841 // This cannot be a BLX.
2843 return This::STATUS_BAD_RELOC;
2845 else if (r_type == elfcpp::R_ARM_THM_XPC22)
2847 // Check for Thumb to Thumb call.
2849 return This::STATUS_BAD_RELOC;
2852 gold_warning(_("%s: Thumb BLX instruction targets "
2853 "thumb function '%s'."),
2854 object->name().c_str(),
2855 (gsym ? gsym->name() : "(local)"));
2856 // Convert BLX to BL.
2857 lower_insn |= 0x1000U;
2863 // A branch to an undefined weak symbol is turned into a jump to
2864 // the next instruction unless a PLT entry will be created.
2865 // The jump to the next instruction is optimized as a NOP.W for
2866 // Thumb-2 enabled architectures.
2867 const Target_arm<big_endian>* arm_target =
2868 Target_arm<big_endian>::default_target();
2869 if (is_weakly_undefined_without_plt)
2871 if (arm_target->may_use_thumb2_nop())
2873 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
2874 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
2878 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
2879 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
2881 return This::STATUS_OKAY;
2884 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
2885 Arm_address branch_target = psymval->value(object, addend);
2886 int32_t branch_offset = branch_target - address;
2888 // We need a stub if the branch offset is too large or if we need
2890 bool may_use_blx = arm_target->may_use_blx();
2891 bool thumb2 = arm_target->using_thumb2();
2893 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2894 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2896 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2897 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2898 || ((thumb_bit == 0)
2899 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2900 || r_type == elfcpp::R_ARM_THM_JUMP24)))
2902 Stub_type stub_type =
2903 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2905 if (stub_type != arm_stub_none)
2907 Stub_table<big_endian>* stub_table =
2908 object->stub_table(relinfo->data_shndx);
2909 gold_assert(stub_table != NULL);
2911 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2912 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
2913 gold_assert(stub != NULL);
2914 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2915 branch_target = stub_table->address() + stub->offset() + addend;
2916 branch_offset = branch_target - address;
2920 // At this point, if we still need to switch mode, the instruction
2921 // must either be a BLX or a BL that can be converted to a BLX.
2924 gold_assert(may_use_blx
2925 && (r_type == elfcpp::R_ARM_THM_CALL
2926 || r_type == elfcpp::R_ARM_THM_XPC22));
2927 // Make sure this is a BLX.
2928 lower_insn &= ~0x1000U;
2932 // Make sure this is a BL.
2933 lower_insn |= 0x1000U;
2936 if ((lower_insn & 0x5000U) == 0x4000U)
2937 // For a BLX instruction, make sure that the relocation is rounded up
2938 // to a word boundary. This follows the semantics of the instruction
2939 // which specifies that bit 1 of the target address will come from bit
2940 // 1 of the base address.
2941 branch_offset = (branch_offset + 2) & ~3;
2943 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2944 // We use the Thumb-2 encoding, which is safe even if dealing with
2945 // a Thumb-1 instruction by virtue of our overflow check above. */
2946 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
2947 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
2949 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
2950 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
2953 ? utils::has_overflow<25>(branch_offset)
2954 : utils::has_overflow<23>(branch_offset))
2955 ? This::STATUS_OVERFLOW
2956 : This::STATUS_OKAY);
2959 // Relocate THUMB-2 long conditional branches.
2960 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2961 // undefined and we do not use PLT in this relocation. In such a case,
2962 // the branch is converted into an NOP.
2964 template<bool big_endian>
2965 typename Arm_relocate_functions<big_endian>::Status
2966 Arm_relocate_functions<big_endian>::thm_jump19(
2967 unsigned char *view,
2968 const Arm_relobj<big_endian>* object,
2969 const Symbol_value<32>* psymval,
2970 Arm_address address,
2971 Arm_address thumb_bit)
2973 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2974 Valtype* wv = reinterpret_cast<Valtype*>(view);
2975 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2976 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2977 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
2979 Arm_address branch_target = psymval->value(object, addend);
2980 int32_t branch_offset = branch_target - address;
2982 // ??? Should handle interworking? GCC might someday try to
2983 // use this for tail calls.
2984 // FIXME: We do support thumb entry to PLT yet.
2987 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
2988 return This::STATUS_BAD_RELOC;
2991 // Put RELOCATION back into the insn.
2992 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
2993 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
2995 // Put the relocated value back in the object file:
2996 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
2997 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
2999 return (utils::has_overflow<21>(branch_offset)
3000 ? This::STATUS_OVERFLOW
3001 : This::STATUS_OKAY);
3004 // Get the GOT section, creating it if necessary.
3006 template<bool big_endian>
3007 Output_data_got<32, big_endian>*
3008 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
3010 if (this->got_ == NULL)
3012 gold_assert(symtab != NULL && layout != NULL);
3014 this->got_ = new Output_data_got<32, big_endian>();
3017 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3019 | elfcpp::SHF_WRITE),
3020 this->got_, false, true, true,
3023 // The old GNU linker creates a .got.plt section. We just
3024 // create another set of data in the .got section. Note that we
3025 // always create a PLT if we create a GOT, although the PLT
3027 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
3028 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3030 | elfcpp::SHF_WRITE),
3031 this->got_plt_, false, false,
3034 // The first three entries are reserved.
3035 this->got_plt_->set_current_data_size(3 * 4);
3037 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3038 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3039 Symbol_table::PREDEFINED,
3041 0, 0, elfcpp::STT_OBJECT,
3043 elfcpp::STV_HIDDEN, 0,
3049 // Get the dynamic reloc section, creating it if necessary.
3051 template<bool big_endian>
3052 typename Target_arm<big_endian>::Reloc_section*
3053 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
3055 if (this->rel_dyn_ == NULL)
3057 gold_assert(layout != NULL);
3058 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
3059 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
3060 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
3061 false, false, false);
3063 return this->rel_dyn_;
3066 // Insn_template methods.
3068 // Return byte size of an instruction template.
3071 Insn_template::size() const
3073 switch (this->type())
3076 case THUMB16_SPECIAL_TYPE:
3087 // Return alignment of an instruction template.
3090 Insn_template::alignment() const
3092 switch (this->type())
3095 case THUMB16_SPECIAL_TYPE:
3106 // Stub_template methods.
3108 Stub_template::Stub_template(
3109 Stub_type type, const Insn_template* insns,
3111 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
3112 entry_in_thumb_mode_(false), relocs_()
3116 // Compute byte size and alignment of stub template.
3117 for (size_t i = 0; i < insn_count; i++)
3119 unsigned insn_alignment = insns[i].alignment();
3120 size_t insn_size = insns[i].size();
3121 gold_assert((offset & (insn_alignment - 1)) == 0);
3122 this->alignment_ = std::max(this->alignment_, insn_alignment);
3123 switch (insns[i].type())
3125 case Insn_template::THUMB16_TYPE:
3126 case Insn_template::THUMB16_SPECIAL_TYPE:
3128 this->entry_in_thumb_mode_ = true;
3131 case Insn_template::THUMB32_TYPE:
3132 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
3133 this->relocs_.push_back(Reloc(i, offset));
3135 this->entry_in_thumb_mode_ = true;
3138 case Insn_template::ARM_TYPE:
3139 // Handle cases where the target is encoded within the
3141 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
3142 this->relocs_.push_back(Reloc(i, offset));
3145 case Insn_template::DATA_TYPE:
3146 // Entry point cannot be data.
3147 gold_assert(i != 0);
3148 this->relocs_.push_back(Reloc(i, offset));
3154 offset += insn_size;
3156 this->size_ = offset;
3161 // Template to implement do_write for a specific target endianity.
3163 template<bool big_endian>
3165 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
3167 const Stub_template* stub_template = this->stub_template();
3168 const Insn_template* insns = stub_template->insns();
3170 // FIXME: We do not handle BE8 encoding yet.
3171 unsigned char* pov = view;
3172 for (size_t i = 0; i < stub_template->insn_count(); i++)
3174 switch (insns[i].type())
3176 case Insn_template::THUMB16_TYPE:
3177 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
3179 case Insn_template::THUMB16_SPECIAL_TYPE:
3180 elfcpp::Swap<16, big_endian>::writeval(
3182 this->thumb16_special(i));
3184 case Insn_template::THUMB32_TYPE:
3186 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
3187 uint32_t lo = insns[i].data() & 0xffff;
3188 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
3189 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
3192 case Insn_template::ARM_TYPE:
3193 case Insn_template::DATA_TYPE:
3194 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
3199 pov += insns[i].size();
3201 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
3204 // Reloc_stub::Key methods.
3206 // Dump a Key as a string for debugging.
3209 Reloc_stub::Key::name() const
3211 if (this->r_sym_ == invalid_index)
3213 // Global symbol key name
3214 // <stub-type>:<symbol name>:<addend>.
3215 const std::string sym_name = this->u_.symbol->name();
3216 // We need to print two hex number and two colons. So just add 100 bytes
3217 // to the symbol name size.
3218 size_t len = sym_name.size() + 100;
3219 char* buffer = new char[len];
3220 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
3221 sym_name.c_str(), this->addend_);
3222 gold_assert(c > 0 && c < static_cast<int>(len));
3224 return std::string(buffer);
3228 // local symbol key name
3229 // <stub-type>:<object>:<r_sym>:<addend>.
3230 const size_t len = 200;
3232 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
3233 this->u_.relobj, this->r_sym_, this->addend_);
3234 gold_assert(c > 0 && c < static_cast<int>(len));
3235 return std::string(buffer);
3239 // Reloc_stub methods.
3241 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3242 // LOCATION to DESTINATION.
3243 // This code is based on the arm_type_of_stub function in
3244 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3248 Reloc_stub::stub_type_for_reloc(
3249 unsigned int r_type,
3250 Arm_address location,
3251 Arm_address destination,
3252 bool target_is_thumb)
3254 Stub_type stub_type = arm_stub_none;
3256 // This is a bit ugly but we want to avoid using a templated class for
3257 // big and little endianities.
3259 bool should_force_pic_veneer;
3262 if (parameters->target().is_big_endian())
3264 const Target_arm<true>* big_endian_target =
3265 Target_arm<true>::default_target();
3266 may_use_blx = big_endian_target->may_use_blx();
3267 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
3268 thumb2 = big_endian_target->using_thumb2();
3269 thumb_only = big_endian_target->using_thumb_only();
3273 const Target_arm<false>* little_endian_target =
3274 Target_arm<false>::default_target();
3275 may_use_blx = little_endian_target->may_use_blx();
3276 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
3277 thumb2 = little_endian_target->using_thumb2();
3278 thumb_only = little_endian_target->using_thumb_only();
3281 int64_t branch_offset = (int64_t)destination - location;
3283 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
3285 // Handle cases where:
3286 // - this call goes too far (different Thumb/Thumb2 max
3288 // - it's a Thumb->Arm call and blx is not available, or it's a
3289 // Thumb->Arm branch (not bl). A stub is needed in this case.
3291 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3292 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3294 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3295 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3296 || ((!target_is_thumb)
3297 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3298 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
3300 if (target_is_thumb)
3305 stub_type = (parameters->options().shared()
3306 || should_force_pic_veneer)
3309 && (r_type == elfcpp::R_ARM_THM_CALL))
3310 // V5T and above. Stub starts with ARM code, so
3311 // we must be able to switch mode before
3312 // reaching it, which is only possible for 'bl'
3313 // (ie R_ARM_THM_CALL relocation).
3314 ? arm_stub_long_branch_any_thumb_pic
3315 // On V4T, use Thumb code only.
3316 : arm_stub_long_branch_v4t_thumb_thumb_pic)
3320 && (r_type == elfcpp::R_ARM_THM_CALL))
3321 ? arm_stub_long_branch_any_any // V5T and above.
3322 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
3326 stub_type = (parameters->options().shared()
3327 || should_force_pic_veneer)
3328 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
3329 : arm_stub_long_branch_thumb_only; // non-PIC stub.
3336 // FIXME: We should check that the input section is from an
3337 // object that has interwork enabled.
3339 stub_type = (parameters->options().shared()
3340 || should_force_pic_veneer)
3343 && (r_type == elfcpp::R_ARM_THM_CALL))
3344 ? arm_stub_long_branch_any_arm_pic // V5T and above.
3345 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
3349 && (r_type == elfcpp::R_ARM_THM_CALL))
3350 ? arm_stub_long_branch_any_any // V5T and above.
3351 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
3353 // Handle v4t short branches.
3354 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3355 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3356 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3357 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3361 else if (r_type == elfcpp::R_ARM_CALL
3362 || r_type == elfcpp::R_ARM_JUMP24
3363 || r_type == elfcpp::R_ARM_PLT32)
3365 if (target_is_thumb)
3369 // FIXME: We should check that the input section is from an
3370 // object that has interwork enabled.
3372 // We have an extra 2-bytes reach because of
3373 // the mode change (bit 24 (H) of BLX encoding).
3374 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3375 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3376 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
3377 || (r_type == elfcpp::R_ARM_JUMP24)
3378 || (r_type == elfcpp::R_ARM_PLT32))
3380 stub_type = (parameters->options().shared()
3381 || should_force_pic_veneer)
3384 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
3385 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
3389 ? arm_stub_long_branch_any_any // V5T and above.
3390 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
3396 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3397 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3399 stub_type = (parameters->options().shared()
3400 || should_force_pic_veneer)
3401 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
3402 : arm_stub_long_branch_any_any; /// non-PIC.
3410 // Cortex_a8_stub methods.
3412 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3413 // I is the position of the instruction template in the stub template.
3416 Cortex_a8_stub::do_thumb16_special(size_t i)
3418 // The only use of this is to copy condition code from a conditional
3419 // branch being worked around to the corresponding conditional branch in
3421 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3423 uint16_t data = this->stub_template()->insns()[i].data();
3424 gold_assert((data & 0xff00U) == 0xd000U);
3425 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
3429 // Stub_factory methods.
3431 Stub_factory::Stub_factory()
3433 // The instruction template sequences are declared as static
3434 // objects and initialized first time the constructor runs.
3436 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3437 // to reach the stub if necessary.
3438 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
3440 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3441 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3442 // dcd R_ARM_ABS32(X)
3445 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3447 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
3449 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3450 Insn_template::arm_insn(0xe12fff1c), // bx ip
3451 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3452 // dcd R_ARM_ABS32(X)
3455 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3456 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
3458 Insn_template::thumb16_insn(0xb401), // push {r0}
3459 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3460 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3461 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3462 Insn_template::thumb16_insn(0x4760), // bx ip
3463 Insn_template::thumb16_insn(0xbf00), // nop
3464 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3465 // dcd R_ARM_ABS32(X)
3468 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3470 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
3472 Insn_template::thumb16_insn(0x4778), // bx pc
3473 Insn_template::thumb16_insn(0x46c0), // nop
3474 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3475 Insn_template::arm_insn(0xe12fff1c), // bx ip
3476 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3477 // dcd R_ARM_ABS32(X)
3480 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3482 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
3484 Insn_template::thumb16_insn(0x4778), // bx pc
3485 Insn_template::thumb16_insn(0x46c0), // nop
3486 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3487 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3488 // dcd R_ARM_ABS32(X)
3491 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3492 // one, when the destination is close enough.
3493 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
3495 Insn_template::thumb16_insn(0x4778), // bx pc
3496 Insn_template::thumb16_insn(0x46c0), // nop
3497 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3500 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3501 // blx to reach the stub if necessary.
3502 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
3504 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3505 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3506 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3507 // dcd R_ARM_REL32(X-4)
3510 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3511 // blx to reach the stub if necessary. We can not add into pc;
3512 // it is not guaranteed to mode switch (different in ARMv6 and
3514 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
3516 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3517 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3518 Insn_template::arm_insn(0xe12fff1c), // bx ip
3519 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3520 // dcd R_ARM_REL32(X)
3523 // V4T ARM -> ARM long branch stub, PIC.
3524 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
3526 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3527 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3528 Insn_template::arm_insn(0xe12fff1c), // bx ip
3529 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3530 // dcd R_ARM_REL32(X)
3533 // V4T Thumb -> ARM long branch stub, PIC.
3534 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
3536 Insn_template::thumb16_insn(0x4778), // bx pc
3537 Insn_template::thumb16_insn(0x46c0), // nop
3538 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3539 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3540 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3541 // dcd R_ARM_REL32(X)
3544 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3546 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
3548 Insn_template::thumb16_insn(0xb401), // push {r0}
3549 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3550 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3551 Insn_template::thumb16_insn(0x4484), // add ip, r0
3552 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3553 Insn_template::thumb16_insn(0x4760), // bx ip
3554 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
3555 // dcd R_ARM_REL32(X)
3558 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3560 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
3562 Insn_template::thumb16_insn(0x4778), // bx pc
3563 Insn_template::thumb16_insn(0x46c0), // nop
3564 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3565 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3566 Insn_template::arm_insn(0xe12fff1c), // bx ip
3567 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3568 // dcd R_ARM_REL32(X)
3571 // Cortex-A8 erratum-workaround stubs.
3573 // Stub used for conditional branches (which may be beyond +/-1MB away,
3574 // so we can't use a conditional branch to reach this stub).
3581 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
3583 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3584 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3585 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3589 // Stub used for b.w and bl.w instructions.
3591 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
3593 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3596 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
3598 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3601 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3602 // instruction (which switches to ARM mode) to point to this stub. Jump to
3603 // the real destination using an ARM-mode branch.
3604 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
3606 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3609 // Fill in the stub template look-up table. Stub templates are constructed
3610 // per instance of Stub_factory for fast look-up without locking
3611 // in a thread-enabled environment.
3613 this->stub_templates_[arm_stub_none] =
3614 new Stub_template(arm_stub_none, NULL, 0);
3616 #define DEF_STUB(x) \
3620 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3621 Stub_type type = arm_stub_##x; \
3622 this->stub_templates_[type] = \
3623 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3631 // Stub_table methods.
3633 // Removel all Cortex-A8 stub.
3635 template<bool big_endian>
3637 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
3639 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
3640 p != this->cortex_a8_stubs_.end();
3643 this->cortex_a8_stubs_.clear();
3646 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
3648 template<bool big_endian>
3650 Stub_table<big_endian>::relocate_stub(
3652 const Relocate_info<32, big_endian>* relinfo,
3653 Target_arm<big_endian>* arm_target,
3654 Output_section* output_section,
3655 unsigned char* view,
3656 Arm_address address,
3657 section_size_type view_size)
3659 const Stub_template* stub_template = stub->stub_template();
3660 if (stub_template->reloc_count() != 0)
3662 // Adjust view to cover the stub only.
3663 section_size_type offset = stub->offset();
3664 section_size_type stub_size = stub_template->size();
3665 gold_assert(offset + stub_size <= view_size);
3667 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
3668 address + offset, stub_size);
3672 // Relocate all stubs in this stub table.
3674 template<bool big_endian>
3676 Stub_table<big_endian>::relocate_stubs(
3677 const Relocate_info<32, big_endian>* relinfo,
3678 Target_arm<big_endian>* arm_target,
3679 Output_section* output_section,
3680 unsigned char* view,
3681 Arm_address address,
3682 section_size_type view_size)
3684 // If we are passed a view bigger than the stub table's. we need to
3686 gold_assert(address == this->address()
3688 == static_cast<section_size_type>(this->data_size())));
3690 // Relocate all relocation stubs.
3691 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3692 p != this->reloc_stubs_.end();
3694 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
3695 address, view_size);
3697 // Relocate all Cortex-A8 stubs.
3698 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
3699 p != this->cortex_a8_stubs_.end();
3701 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
3702 address, view_size);
3705 // Write out the stubs to file.
3707 template<bool big_endian>
3709 Stub_table<big_endian>::do_write(Output_file* of)
3711 off_t offset = this->offset();
3712 const section_size_type oview_size =
3713 convert_to_section_size_type(this->data_size());
3714 unsigned char* const oview = of->get_output_view(offset, oview_size);
3716 // Write relocation stubs.
3717 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3718 p != this->reloc_stubs_.end();
3721 Reloc_stub* stub = p->second;
3722 Arm_address address = this->address() + stub->offset();
3724 == align_address(address,
3725 stub->stub_template()->alignment()));
3726 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3730 // Write Cortex-A8 stubs.
3731 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3732 p != this->cortex_a8_stubs_.end();
3735 Cortex_a8_stub* stub = p->second;
3736 Arm_address address = this->address() + stub->offset();
3738 == align_address(address,
3739 stub->stub_template()->alignment()));
3740 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3744 of->write_output_view(this->offset(), oview_size, oview);
3747 // Update the data size and address alignment of the stub table at the end
3748 // of a relaxation pass. Return true if either the data size or the
3749 // alignment changed in this relaxation pass.
3751 template<bool big_endian>
3753 Stub_table<big_endian>::update_data_size_and_addralign()
3756 unsigned addralign = 1;
3758 // Go over all stubs in table to compute data size and address alignment.
3760 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3761 p != this->reloc_stubs_.end();
3764 const Stub_template* stub_template = p->second->stub_template();
3765 addralign = std::max(addralign, stub_template->alignment());
3766 size = (align_address(size, stub_template->alignment())
3767 + stub_template->size());
3770 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3771 p != this->cortex_a8_stubs_.end();
3774 const Stub_template* stub_template = p->second->stub_template();
3775 addralign = std::max(addralign, stub_template->alignment());
3776 size = (align_address(size, stub_template->alignment())
3777 + stub_template->size());
3780 // Check if either data size or alignment changed in this pass.
3781 // Update prev_data_size_ and prev_addralign_. These will be used
3782 // as the current data size and address alignment for the next pass.
3783 bool changed = size != this->prev_data_size_;
3784 this->prev_data_size_ = size;
3786 if (addralign != this->prev_addralign_)
3788 this->prev_addralign_ = addralign;
3793 // Finalize the stubs. This sets the offsets of the stubs within the stub
3794 // table. It also marks all input sections needing Cortex-A8 workaround.
3796 template<bool big_endian>
3798 Stub_table<big_endian>::finalize_stubs()
3801 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3802 p != this->reloc_stubs_.end();
3805 Reloc_stub* stub = p->second;
3806 const Stub_template* stub_template = stub->stub_template();
3807 uint64_t stub_addralign = stub_template->alignment();
3808 off = align_address(off, stub_addralign);
3809 stub->set_offset(off);
3810 off += stub_template->size();
3813 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3814 p != this->cortex_a8_stubs_.end();
3817 Cortex_a8_stub* stub = p->second;
3818 const Stub_template* stub_template = stub->stub_template();
3819 uint64_t stub_addralign = stub_template->alignment();
3820 off = align_address(off, stub_addralign);
3821 stub->set_offset(off);
3822 off += stub_template->size();
3824 // Mark input section so that we can determine later if a code section
3825 // needs the Cortex-A8 workaround quickly.
3826 Arm_relobj<big_endian>* arm_relobj =
3827 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
3828 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
3831 gold_assert(off <= this->prev_data_size_);
3834 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
3835 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
3836 // of the address range seen by the linker.
3838 template<bool big_endian>
3840 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
3841 Target_arm<big_endian>* arm_target,
3842 unsigned char* view,
3843 Arm_address view_address,
3844 section_size_type view_size)
3846 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
3847 for (Cortex_a8_stub_list::const_iterator p =
3848 this->cortex_a8_stubs_.lower_bound(view_address);
3849 ((p != this->cortex_a8_stubs_.end())
3850 && (p->first < (view_address + view_size)));
3853 // We do not store the THUMB bit in the LSB of either the branch address
3854 // or the stub offset. There is no need to strip the LSB.
3855 Arm_address branch_address = p->first;
3856 const Cortex_a8_stub* stub = p->second;
3857 Arm_address stub_address = this->address() + stub->offset();
3859 // Offset of the branch instruction relative to this view.
3860 section_size_type offset =
3861 convert_to_section_size_type(branch_address - view_address);
3862 gold_assert((offset + 4) <= view_size);
3864 arm_target->apply_cortex_a8_workaround(stub, stub_address,
3865 view + offset, branch_address);
3869 // Arm_input_section methods.
3871 // Initialize an Arm_input_section.
3873 template<bool big_endian>
3875 Arm_input_section<big_endian>::init()
3877 Relobj* relobj = this->relobj();
3878 unsigned int shndx = this->shndx();
3880 // Cache these to speed up size and alignment queries. It is too slow
3881 // to call section_addraglin and section_size every time.
3882 this->original_addralign_ = relobj->section_addralign(shndx);
3883 this->original_size_ = relobj->section_size(shndx);
3885 // We want to make this look like the original input section after
3886 // output sections are finalized.
3887 Output_section* os = relobj->output_section(shndx);
3888 off_t offset = relobj->output_section_offset(shndx);
3889 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
3890 this->set_address(os->address() + offset);
3891 this->set_file_offset(os->offset() + offset);
3893 this->set_current_data_size(this->original_size_);
3894 this->finalize_data_size();
3897 template<bool big_endian>
3899 Arm_input_section<big_endian>::do_write(Output_file* of)
3901 // We have to write out the original section content.
3902 section_size_type section_size;
3903 const unsigned char* section_contents =
3904 this->relobj()->section_contents(this->shndx(), §ion_size, false);
3905 of->write(this->offset(), section_contents, section_size);
3907 // If this owns a stub table and it is not empty, write it.
3908 if (this->is_stub_table_owner() && !this->stub_table_->empty())
3909 this->stub_table_->write(of);
3912 // Finalize data size.
3914 template<bool big_endian>
3916 Arm_input_section<big_endian>::set_final_data_size()
3918 // If this owns a stub table, finalize its data size as well.
3919 if (this->is_stub_table_owner())
3921 uint64_t address = this->address();
3923 // The stub table comes after the original section contents.
3924 address += this->original_size_;
3925 address = align_address(address, this->stub_table_->addralign());
3926 off_t offset = this->offset() + (address - this->address());
3927 this->stub_table_->set_address_and_file_offset(address, offset);
3928 address += this->stub_table_->data_size();
3929 gold_assert(address == this->address() + this->current_data_size());
3932 this->set_data_size(this->current_data_size());
3935 // Reset address and file offset.
3937 template<bool big_endian>
3939 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
3941 // Size of the original input section contents.
3942 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
3944 // If this is a stub table owner, account for the stub table size.
3945 if (this->is_stub_table_owner())
3947 Stub_table<big_endian>* stub_table = this->stub_table_;
3949 // Reset the stub table's address and file offset. The
3950 // current data size for child will be updated after that.
3951 stub_table_->reset_address_and_file_offset();
3952 off = align_address(off, stub_table_->addralign());
3953 off += stub_table->current_data_size();
3956 this->set_current_data_size(off);
3959 // Arm_output_section methods.
3961 // Create a stub group for input sections from BEGIN to END. OWNER
3962 // points to the input section to be the owner a new stub table.
3964 template<bool big_endian>
3966 Arm_output_section<big_endian>::create_stub_group(
3967 Input_section_list::const_iterator begin,
3968 Input_section_list::const_iterator end,
3969 Input_section_list::const_iterator owner,
3970 Target_arm<big_endian>* target,
3971 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
3973 // Currently we convert ordinary input sections into relaxed sections only
3974 // at this point but we may want to support creating relaxed input section
3975 // very early. So we check here to see if owner is already a relaxed
3978 Arm_input_section<big_endian>* arm_input_section;
3979 if (owner->is_relaxed_input_section())
3982 Arm_input_section<big_endian>::as_arm_input_section(
3983 owner->relaxed_input_section());
3987 gold_assert(owner->is_input_section());
3988 // Create a new relaxed input section.
3990 target->new_arm_input_section(owner->relobj(), owner->shndx());
3991 new_relaxed_sections->push_back(arm_input_section);
3994 // Create a stub table.
3995 Stub_table<big_endian>* stub_table =
3996 target->new_stub_table(arm_input_section);
3998 arm_input_section->set_stub_table(stub_table);
4000 Input_section_list::const_iterator p = begin;
4001 Input_section_list::const_iterator prev_p;
4003 // Look for input sections or relaxed input sections in [begin ... end].
4006 if (p->is_input_section() || p->is_relaxed_input_section())
4008 // The stub table information for input sections live
4009 // in their objects.
4010 Arm_relobj<big_endian>* arm_relobj =
4011 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
4012 arm_relobj->set_stub_table(p->shndx(), stub_table);
4016 while (prev_p != end);
4019 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
4020 // of stub groups. We grow a stub group by adding input section until the
4021 // size is just below GROUP_SIZE. The last input section will be converted
4022 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
4023 // input section after the stub table, effectively double the group size.
4025 // This is similar to the group_sections() function in elf32-arm.c but is
4026 // implemented differently.
4028 template<bool big_endian>
4030 Arm_output_section<big_endian>::group_sections(
4031 section_size_type group_size,
4032 bool stubs_always_after_branch,
4033 Target_arm<big_endian>* target)
4035 // We only care about sections containing code.
4036 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
4039 // States for grouping.
4042 // No group is being built.
4044 // A group is being built but the stub table is not found yet.
4045 // We keep group a stub group until the size is just under GROUP_SIZE.
4046 // The last input section in the group will be used as the stub table.
4047 FINDING_STUB_SECTION,
4048 // A group is being built and we have already found a stub table.
4049 // We enter this state to grow a stub group by adding input section
4050 // after the stub table. This effectively doubles the group size.
4054 // Any newly created relaxed sections are stored here.
4055 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
4057 State state = NO_GROUP;
4058 section_size_type off = 0;
4059 section_size_type group_begin_offset = 0;
4060 section_size_type group_end_offset = 0;
4061 section_size_type stub_table_end_offset = 0;
4062 Input_section_list::const_iterator group_begin =
4063 this->input_sections().end();
4064 Input_section_list::const_iterator stub_table =
4065 this->input_sections().end();
4066 Input_section_list::const_iterator group_end = this->input_sections().end();
4067 for (Input_section_list::const_iterator p = this->input_sections().begin();
4068 p != this->input_sections().end();
4071 section_size_type section_begin_offset =
4072 align_address(off, p->addralign());
4073 section_size_type section_end_offset =
4074 section_begin_offset + p->data_size();
4076 // Check to see if we should group the previously seens sections.
4082 case FINDING_STUB_SECTION:
4083 // Adding this section makes the group larger than GROUP_SIZE.
4084 if (section_end_offset - group_begin_offset >= group_size)
4086 if (stubs_always_after_branch)
4088 gold_assert(group_end != this->input_sections().end());
4089 this->create_stub_group(group_begin, group_end, group_end,
4090 target, &new_relaxed_sections);
4095 // But wait, there's more! Input sections up to
4096 // stub_group_size bytes after the stub table can be
4097 // handled by it too.
4098 state = HAS_STUB_SECTION;
4099 stub_table = group_end;
4100 stub_table_end_offset = group_end_offset;
4105 case HAS_STUB_SECTION:
4106 // Adding this section makes the post stub-section group larger
4108 if (section_end_offset - stub_table_end_offset >= group_size)
4110 gold_assert(group_end != this->input_sections().end());
4111 this->create_stub_group(group_begin, group_end, stub_table,
4112 target, &new_relaxed_sections);
4121 // If we see an input section and currently there is no group, start
4122 // a new one. Skip any empty sections.
4123 if ((p->is_input_section() || p->is_relaxed_input_section())
4124 && (p->relobj()->section_size(p->shndx()) != 0))
4126 if (state == NO_GROUP)
4128 state = FINDING_STUB_SECTION;
4130 group_begin_offset = section_begin_offset;
4133 // Keep track of the last input section seen.
4135 group_end_offset = section_end_offset;
4138 off = section_end_offset;
4141 // Create a stub group for any ungrouped sections.
4142 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
4144 gold_assert(group_end != this->input_sections().end());
4145 this->create_stub_group(group_begin, group_end,
4146 (state == FINDING_STUB_SECTION
4149 target, &new_relaxed_sections);
4152 // Convert input section into relaxed input section in a batch.
4153 if (!new_relaxed_sections.empty())
4154 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
4156 // Update the section offsets
4157 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
4159 Arm_relobj<big_endian>* arm_relobj =
4160 Arm_relobj<big_endian>::as_arm_relobj(
4161 new_relaxed_sections[i]->relobj());
4162 unsigned int shndx = new_relaxed_sections[i]->shndx();
4163 // Tell Arm_relobj that this input section is converted.
4164 arm_relobj->convert_input_section_to_relaxed_section(shndx);
4168 // Arm_relobj methods.
4170 // Determine if we want to scan the SHNDX-th section for relocation stubs.
4171 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4173 template<bool big_endian>
4175 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
4176 const elfcpp::Shdr<32, big_endian>& shdr,
4177 const Relobj::Output_sections& out_sections,
4178 const Symbol_table *symtab)
4180 unsigned int sh_type = shdr.get_sh_type();
4181 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
4184 // Ignore empty section.
4185 off_t sh_size = shdr.get_sh_size();
4189 // Ignore reloc section with bad info. This error will be
4190 // reported in the final link.
4191 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
4192 if (index >= this->shnum())
4195 // This relocation section is against a section which we
4196 // discarded or if the section is folded into another
4197 // section due to ICF.
4198 if (out_sections[index] == NULL || symtab->is_section_folded(this, index))
4201 // Ignore reloc section with unexpected symbol table. The
4202 // error will be reported in the final link.
4203 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
4206 const unsigned int reloc_size = (sh_type == elfcpp::SHT_REL
4207 ? elfcpp::Elf_sizes<32>::rel_size
4208 : elfcpp::Elf_sizes<32>::rela_size);
4210 // Ignore reloc section with unexpected entsize or uneven size.
4211 // The error will be reported in the final link.
4212 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
4218 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
4219 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4221 template<bool big_endian>
4223 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
4224 const elfcpp::Shdr<32, big_endian>& shdr,
4227 const Symbol_table* symtab)
4229 // We only scan non-empty code sections.
4230 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) == 0
4231 || shdr.get_sh_size() == 0)
4234 // Ignore discarded or ICF'ed sections.
4235 if (os == NULL || symtab->is_section_folded(this, shndx))
4238 // Find output address of section.
4239 Arm_address address = os->output_address(this, shndx, 0);
4241 // If the section does not cross any 4K-boundaries, it does not need to
4243 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
4249 // Scan a section for Cortex-A8 workaround.
4251 template<bool big_endian>
4253 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
4254 const elfcpp::Shdr<32, big_endian>& shdr,
4257 Target_arm<big_endian>* arm_target)
4259 Arm_address output_address = os->output_address(this, shndx, 0);
4261 // Get the section contents.
4262 section_size_type input_view_size = 0;
4263 const unsigned char* input_view =
4264 this->section_contents(shndx, &input_view_size, false);
4266 // We need to go through the mapping symbols to determine what to
4267 // scan. There are two reasons. First, we should look at THUMB code and
4268 // THUMB code only. Second, we only want to look at the 4K-page boundary
4269 // to speed up the scanning.
4271 // Look for the first mapping symbol in this section. It should be
4273 Mapping_symbol_position section_start(shndx, 0);
4274 typename Mapping_symbols_info::const_iterator p =
4275 this->mapping_symbols_info_.lower_bound(section_start);
4277 if (p == this->mapping_symbols_info_.end()
4278 || p->first != section_start)
4280 gold_warning(_("Cortex-A8 erratum scanning failed because there "
4281 "is no mapping symbols for section %u of %s"),
4282 shndx, this->name().c_str());
4286 while (p != this->mapping_symbols_info_.end()
4287 && p->first.first == shndx)
4289 typename Mapping_symbols_info::const_iterator next =
4290 this->mapping_symbols_info_.upper_bound(p->first);
4292 // Only scan part of a section with THUMB code.
4293 if (p->second == 't')
4295 // Determine the end of this range.
4296 section_size_type span_start =
4297 convert_to_section_size_type(p->first.second);
4298 section_size_type span_end;
4299 if (next != this->mapping_symbols_info_.end()
4300 && next->first.first == shndx)
4301 span_end = convert_to_section_size_type(next->first.second);
4303 span_end = convert_to_section_size_type(shdr.get_sh_size());
4305 if (((span_start + output_address) & ~0xfffUL)
4306 != ((span_end + output_address - 1) & ~0xfffUL))
4308 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
4309 span_start, span_end,
4319 // Scan relocations for stub generation.
4321 template<bool big_endian>
4323 Arm_relobj<big_endian>::scan_sections_for_stubs(
4324 Target_arm<big_endian>* arm_target,
4325 const Symbol_table* symtab,
4326 const Layout* layout)
4328 unsigned int shnum = this->shnum();
4329 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4331 // Read the section headers.
4332 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
4336 // To speed up processing, we set up hash tables for fast lookup of
4337 // input offsets to output addresses.
4338 this->initialize_input_to_output_maps();
4340 const Relobj::Output_sections& out_sections(this->output_sections());
4342 Relocate_info<32, big_endian> relinfo;
4343 relinfo.symtab = symtab;
4344 relinfo.layout = layout;
4345 relinfo.object = this;
4347 // Do relocation stubs scanning.
4348 const unsigned char* p = pshdrs + shdr_size;
4349 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
4351 const elfcpp::Shdr<32, big_endian> shdr(p);
4352 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab))
4354 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
4355 Arm_address output_offset = this->get_output_section_offset(index);
4356 Arm_address output_address;
4357 if(output_offset != invalid_address)
4358 output_address = out_sections[index]->address() + output_offset;
4361 // Currently this only happens for a relaxed section.
4362 const Output_relaxed_input_section* poris =
4363 out_sections[index]->find_relaxed_input_section(this, index);
4364 gold_assert(poris != NULL);
4365 output_address = poris->address();
4368 // Get the relocations.
4369 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
4373 // Get the section contents. This does work for the case in which
4374 // we modify the contents of an input section. We need to pass the
4375 // output view under such circumstances.
4376 section_size_type input_view_size = 0;
4377 const unsigned char* input_view =
4378 this->section_contents(index, &input_view_size, false);
4380 relinfo.reloc_shndx = i;
4381 relinfo.data_shndx = index;
4382 unsigned int sh_type = shdr.get_sh_type();
4383 const unsigned int reloc_size = (sh_type == elfcpp::SHT_REL
4384 ? elfcpp::Elf_sizes<32>::rel_size
4385 : elfcpp::Elf_sizes<32>::rela_size);
4387 Output_section* os = out_sections[index];
4388 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
4389 shdr.get_sh_size() / reloc_size,
4391 output_offset == invalid_address,
4392 input_view, output_address,
4397 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
4398 // after its relocation section, if there is one, is processed for
4399 // relocation stubs. Merging this loop with the one above would have been
4400 // complicated since we would have had to make sure that relocation stub
4401 // scanning is done first.
4402 if (arm_target->fix_cortex_a8())
4404 const unsigned char* p = pshdrs + shdr_size;
4405 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
4407 const elfcpp::Shdr<32, big_endian> shdr(p);
4408 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
4411 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
4416 // After we've done the relocations, we release the hash tables,
4417 // since we no longer need them.
4418 this->free_input_to_output_maps();
4421 // Count the local symbols. The ARM backend needs to know if a symbol
4422 // is a THUMB function or not. For global symbols, it is easy because
4423 // the Symbol object keeps the ELF symbol type. For local symbol it is
4424 // harder because we cannot access this information. So we override the
4425 // do_count_local_symbol in parent and scan local symbols to mark
4426 // THUMB functions. This is not the most efficient way but I do not want to
4427 // slow down other ports by calling a per symbol targer hook inside
4428 // Sized_relobj<size, big_endian>::do_count_local_symbols.
4430 template<bool big_endian>
4432 Arm_relobj<big_endian>::do_count_local_symbols(
4433 Stringpool_template<char>* pool,
4434 Stringpool_template<char>* dynpool)
4436 // We need to fix-up the values of any local symbols whose type are
4439 // Ask parent to count the local symbols.
4440 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
4441 const unsigned int loccount = this->local_symbol_count();
4445 // Intialize the thumb function bit-vector.
4446 std::vector<bool> empty_vector(loccount, false);
4447 this->local_symbol_is_thumb_function_.swap(empty_vector);
4449 // Read the symbol table section header.
4450 const unsigned int symtab_shndx = this->symtab_shndx();
4451 elfcpp::Shdr<32, big_endian>
4452 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
4453 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
4455 // Read the local symbols.
4456 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
4457 gold_assert(loccount == symtabshdr.get_sh_info());
4458 off_t locsize = loccount * sym_size;
4459 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
4460 locsize, true, true);
4462 // For mapping symbol processing, we need to read the symbol names.
4463 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
4464 if (strtab_shndx >= this->shnum())
4466 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
4470 elfcpp::Shdr<32, big_endian>
4471 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
4472 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
4474 this->error(_("symbol table name section has wrong type: %u"),
4475 static_cast<unsigned int>(strtabshdr.get_sh_type()));
4478 const char* pnames =
4479 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
4480 strtabshdr.get_sh_size(),
4483 // Loop over the local symbols and mark any local symbols pointing
4484 // to THUMB functions.
4486 // Skip the first dummy symbol.
4488 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
4489 this->local_values();
4490 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
4492 elfcpp::Sym<32, big_endian> sym(psyms);
4493 elfcpp::STT st_type = sym.get_st_type();
4494 Symbol_value<32>& lv((*plocal_values)[i]);
4495 Arm_address input_value = lv.input_value();
4497 // Check to see if this is a mapping symbol.
4498 const char* sym_name = pnames + sym.get_st_name();
4499 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
4501 unsigned int input_shndx = sym.get_st_shndx();
4503 // Strip of LSB in case this is a THUMB symbol.
4504 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
4505 this->mapping_symbols_info_[msp] = sym_name[1];
4508 if (st_type == elfcpp::STT_ARM_TFUNC
4509 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
4511 // This is a THUMB function. Mark this and canonicalize the
4512 // symbol value by setting LSB.
4513 this->local_symbol_is_thumb_function_[i] = true;
4514 if ((input_value & 1) == 0)
4515 lv.set_input_value(input_value | 1);
4520 // Relocate sections.
4521 template<bool big_endian>
4523 Arm_relobj<big_endian>::do_relocate_sections(
4524 const Symbol_table* symtab,
4525 const Layout* layout,
4526 const unsigned char* pshdrs,
4527 typename Sized_relobj<32, big_endian>::Views* pviews)
4529 // Call parent to relocate sections.
4530 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
4533 // We do not generate stubs if doing a relocatable link.
4534 if (parameters->options().relocatable())
4537 // Relocate stub tables.
4538 unsigned int shnum = this->shnum();
4540 Target_arm<big_endian>* arm_target =
4541 Target_arm<big_endian>::default_target();
4543 Relocate_info<32, big_endian> relinfo;
4544 relinfo.symtab = symtab;
4545 relinfo.layout = layout;
4546 relinfo.object = this;
4548 for (unsigned int i = 1; i < shnum; ++i)
4550 Arm_input_section<big_endian>* arm_input_section =
4551 arm_target->find_arm_input_section(this, i);
4553 if (arm_input_section == NULL
4554 || !arm_input_section->is_stub_table_owner()
4555 || arm_input_section->stub_table()->empty())
4558 // We cannot discard a section if it owns a stub table.
4559 Output_section* os = this->output_section(i);
4560 gold_assert(os != NULL);
4562 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
4563 relinfo.reloc_shdr = NULL;
4564 relinfo.data_shndx = i;
4565 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
4567 gold_assert((*pviews)[i].view != NULL);
4569 // We are passed the output section view. Adjust it to cover the
4571 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
4572 gold_assert((stub_table->address() >= (*pviews)[i].address)
4573 && ((stub_table->address() + stub_table->data_size())
4574 <= (*pviews)[i].address + (*pviews)[i].view_size));
4576 off_t offset = stub_table->address() - (*pviews)[i].address;
4577 unsigned char* view = (*pviews)[i].view + offset;
4578 Arm_address address = stub_table->address();
4579 section_size_type view_size = stub_table->data_size();
4581 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
4586 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4589 template<bool big_endian>
4590 Attributes_section_data*
4591 read_arm_attributes_section(
4593 Read_symbols_data *sd)
4595 // Read the attributes section if there is one.
4596 // We read from the end because gas seems to put it near the end of
4597 // the section headers.
4598 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4599 const unsigned char *ps =
4600 sd->section_headers->data() + shdr_size * (object->shnum() - 1);
4601 for (unsigned int i = object->shnum(); i > 0; --i, ps -= shdr_size)
4603 elfcpp::Shdr<32, big_endian> shdr(ps);
4604 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
4606 section_offset_type section_offset = shdr.get_sh_offset();
4607 section_size_type section_size =
4608 convert_to_section_size_type(shdr.get_sh_size());
4609 File_view* view = object->get_lasting_view(section_offset,
4610 section_size, true, false);
4611 return new Attributes_section_data(view->data(), section_size);
4617 // Read the symbol information.
4619 template<bool big_endian>
4621 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4623 // Call parent class to read symbol information.
4624 Sized_relobj<32, big_endian>::do_read_symbols(sd);
4626 // Read processor-specific flags in ELF file header.
4627 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4628 elfcpp::Elf_sizes<32>::ehdr_size,
4630 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4631 this->processor_specific_flags_ = ehdr.get_e_flags();
4632 this->attributes_section_data_ =
4633 read_arm_attributes_section<big_endian>(this, sd);
4636 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
4637 // sections for unwinding. These sections are referenced implicitly by
4638 // text sections linked in the section headers. If we ignore these implict
4639 // references, the .ARM.exidx sections and any .ARM.extab sections they use
4640 // will be garbage-collected incorrectly. Hence we override the same function
4641 // in the base class to handle these implicit references.
4643 template<bool big_endian>
4645 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
4647 Read_relocs_data* rd)
4649 // First, call base class method to process relocations in this object.
4650 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
4652 unsigned int shnum = this->shnum();
4653 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4654 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
4658 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
4659 // to these from the linked text sections.
4660 const unsigned char* ps = pshdrs + shdr_size;
4661 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
4663 elfcpp::Shdr<32, big_endian> shdr(ps);
4664 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
4666 // Found an .ARM.exidx section, add it to the set of reachable
4667 // sections from its linked text section.
4668 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
4669 symtab->gc()->add_reference(this, text_shndx, this, i);
4674 // Arm_dynobj methods.
4676 // Read the symbol information.
4678 template<bool big_endian>
4680 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4682 // Call parent class to read symbol information.
4683 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
4685 // Read processor-specific flags in ELF file header.
4686 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4687 elfcpp::Elf_sizes<32>::ehdr_size,
4689 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4690 this->processor_specific_flags_ = ehdr.get_e_flags();
4691 this->attributes_section_data_ =
4692 read_arm_attributes_section<big_endian>(this, sd);
4695 // Stub_addend_reader methods.
4697 // Read the addend of a REL relocation of type R_TYPE at VIEW.
4699 template<bool big_endian>
4700 elfcpp::Elf_types<32>::Elf_Swxword
4701 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
4702 unsigned int r_type,
4703 const unsigned char* view,
4704 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
4706 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
4710 case elfcpp::R_ARM_CALL:
4711 case elfcpp::R_ARM_JUMP24:
4712 case elfcpp::R_ARM_PLT32:
4714 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4715 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4716 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4717 return utils::sign_extend<26>(val << 2);
4720 case elfcpp::R_ARM_THM_CALL:
4721 case elfcpp::R_ARM_THM_JUMP24:
4722 case elfcpp::R_ARM_THM_XPC22:
4724 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4725 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4726 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4727 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4728 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
4731 case elfcpp::R_ARM_THM_JUMP19:
4733 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4734 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4735 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4736 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4737 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
4745 // A class to handle the PLT data.
4747 template<bool big_endian>
4748 class Output_data_plt_arm : public Output_section_data
4751 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
4754 Output_data_plt_arm(Layout*, Output_data_space*);
4756 // Add an entry to the PLT.
4758 add_entry(Symbol* gsym);
4760 // Return the .rel.plt section data.
4761 const Reloc_section*
4763 { return this->rel_; }
4767 do_adjust_output_section(Output_section* os);
4769 // Write to a map file.
4771 do_print_to_mapfile(Mapfile* mapfile) const
4772 { mapfile->print_output_data(this, _("** PLT")); }
4775 // Template for the first PLT entry.
4776 static const uint32_t first_plt_entry[5];
4778 // Template for subsequent PLT entries.
4779 static const uint32_t plt_entry[3];
4781 // Set the final size.
4783 set_final_data_size()
4785 this->set_data_size(sizeof(first_plt_entry)
4786 + this->count_ * sizeof(plt_entry));
4789 // Write out the PLT data.
4791 do_write(Output_file*);
4793 // The reloc section.
4794 Reloc_section* rel_;
4795 // The .got.plt section.
4796 Output_data_space* got_plt_;
4797 // The number of PLT entries.
4798 unsigned int count_;
4801 // Create the PLT section. The ordinary .got section is an argument,
4802 // since we need to refer to the start. We also create our own .got
4803 // section just for PLT entries.
4805 template<bool big_endian>
4806 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
4807 Output_data_space* got_plt)
4808 : Output_section_data(4), got_plt_(got_plt), count_(0)
4810 this->rel_ = new Reloc_section(false);
4811 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
4812 elfcpp::SHF_ALLOC, this->rel_, true, false,
4816 template<bool big_endian>
4818 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
4823 // Add an entry to the PLT.
4825 template<bool big_endian>
4827 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
4829 gold_assert(!gsym->has_plt_offset());
4831 // Note that when setting the PLT offset we skip the initial
4832 // reserved PLT entry.
4833 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
4834 + sizeof(first_plt_entry));
4838 section_offset_type got_offset = this->got_plt_->current_data_size();
4840 // Every PLT entry needs a GOT entry which points back to the PLT
4841 // entry (this will be changed by the dynamic linker, normally
4842 // lazily when the function is called).
4843 this->got_plt_->set_current_data_size(got_offset + 4);
4845 // Every PLT entry needs a reloc.
4846 gsym->set_needs_dynsym_entry();
4847 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
4850 // Note that we don't need to save the symbol. The contents of the
4851 // PLT are independent of which symbols are used. The symbols only
4852 // appear in the relocations.
4856 // FIXME: This is not very flexible. Right now this has only been tested
4857 // on armv5te. If we are to support additional architecture features like
4858 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4860 // The first entry in the PLT.
4861 template<bool big_endian>
4862 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
4864 0xe52de004, // str lr, [sp, #-4]!
4865 0xe59fe004, // ldr lr, [pc, #4]
4866 0xe08fe00e, // add lr, pc, lr
4867 0xe5bef008, // ldr pc, [lr, #8]!
4868 0x00000000, // &GOT[0] - .
4871 // Subsequent entries in the PLT.
4873 template<bool big_endian>
4874 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
4876 0xe28fc600, // add ip, pc, #0xNN00000
4877 0xe28cca00, // add ip, ip, #0xNN000
4878 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4881 // Write out the PLT. This uses the hand-coded instructions above,
4882 // and adjusts them as needed. This is all specified by the arm ELF
4883 // Processor Supplement.
4885 template<bool big_endian>
4887 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
4889 const off_t offset = this->offset();
4890 const section_size_type oview_size =
4891 convert_to_section_size_type(this->data_size());
4892 unsigned char* const oview = of->get_output_view(offset, oview_size);
4894 const off_t got_file_offset = this->got_plt_->offset();
4895 const section_size_type got_size =
4896 convert_to_section_size_type(this->got_plt_->data_size());
4897 unsigned char* const got_view = of->get_output_view(got_file_offset,
4899 unsigned char* pov = oview;
4901 Arm_address plt_address = this->address();
4902 Arm_address got_address = this->got_plt_->address();
4904 // Write first PLT entry. All but the last word are constants.
4905 const size_t num_first_plt_words = (sizeof(first_plt_entry)
4906 / sizeof(plt_entry[0]));
4907 for (size_t i = 0; i < num_first_plt_words - 1; i++)
4908 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
4909 // Last word in first PLT entry is &GOT[0] - .
4910 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
4911 got_address - (plt_address + 16));
4912 pov += sizeof(first_plt_entry);
4914 unsigned char* got_pov = got_view;
4916 memset(got_pov, 0, 12);
4919 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
4920 unsigned int plt_offset = sizeof(first_plt_entry);
4921 unsigned int plt_rel_offset = 0;
4922 unsigned int got_offset = 12;
4923 const unsigned int count = this->count_;
4924 for (unsigned int i = 0;
4927 pov += sizeof(plt_entry),
4929 plt_offset += sizeof(plt_entry),
4930 plt_rel_offset += rel_size,
4933 // Set and adjust the PLT entry itself.
4934 int32_t offset = ((got_address + got_offset)
4935 - (plt_address + plt_offset + 8));
4937 gold_assert(offset >= 0 && offset < 0x0fffffff);
4938 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
4939 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
4940 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
4941 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
4942 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
4943 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
4945 // Set the entry in the GOT.
4946 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
4949 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4950 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4952 of->write_output_view(offset, oview_size, oview);
4953 of->write_output_view(got_file_offset, got_size, got_view);
4956 // Create a PLT entry for a global symbol.
4958 template<bool big_endian>
4960 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
4963 if (gsym->has_plt_offset())
4966 if (this->plt_ == NULL)
4968 // Create the GOT sections first.
4969 this->got_section(symtab, layout);
4971 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
4972 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
4974 | elfcpp::SHF_EXECINSTR),
4975 this->plt_, false, false, false, false);
4977 this->plt_->add_entry(gsym);
4980 // Report an unsupported relocation against a local symbol.
4982 template<bool big_endian>
4984 Target_arm<big_endian>::Scan::unsupported_reloc_local(
4985 Sized_relobj<32, big_endian>* object,
4986 unsigned int r_type)
4988 gold_error(_("%s: unsupported reloc %u against local symbol"),
4989 object->name().c_str(), r_type);
4992 // We are about to emit a dynamic relocation of type R_TYPE. If the
4993 // dynamic linker does not support it, issue an error. The GNU linker
4994 // only issues a non-PIC error for an allocated read-only section.
4995 // Here we know the section is allocated, but we don't know that it is
4996 // read-only. But we check for all the relocation types which the
4997 // glibc dynamic linker supports, so it seems appropriate to issue an
4998 // error even if the section is not read-only.
5000 template<bool big_endian>
5002 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
5003 unsigned int r_type)
5007 // These are the relocation types supported by glibc for ARM.
5008 case elfcpp::R_ARM_RELATIVE:
5009 case elfcpp::R_ARM_COPY:
5010 case elfcpp::R_ARM_GLOB_DAT:
5011 case elfcpp::R_ARM_JUMP_SLOT:
5012 case elfcpp::R_ARM_ABS32:
5013 case elfcpp::R_ARM_ABS32_NOI:
5014 case elfcpp::R_ARM_PC24:
5015 // FIXME: The following 3 types are not supported by Android's dynamic
5017 case elfcpp::R_ARM_TLS_DTPMOD32:
5018 case elfcpp::R_ARM_TLS_DTPOFF32:
5019 case elfcpp::R_ARM_TLS_TPOFF32:
5023 // This prevents us from issuing more than one error per reloc
5024 // section. But we can still wind up issuing more than one
5025 // error per object file.
5026 if (this->issued_non_pic_error_)
5028 object->error(_("requires unsupported dynamic reloc; "
5029 "recompile with -fPIC"));
5030 this->issued_non_pic_error_ = true;
5033 case elfcpp::R_ARM_NONE:
5038 // Scan a relocation for a local symbol.
5039 // FIXME: This only handles a subset of relocation types used by Android
5040 // on ARM v5te devices.
5042 template<bool big_endian>
5044 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
5047 Sized_relobj<32, big_endian>* object,
5048 unsigned int data_shndx,
5049 Output_section* output_section,
5050 const elfcpp::Rel<32, big_endian>& reloc,
5051 unsigned int r_type,
5052 const elfcpp::Sym<32, big_endian>&)
5054 r_type = get_real_reloc_type(r_type);
5057 case elfcpp::R_ARM_NONE:
5060 case elfcpp::R_ARM_ABS32:
5061 case elfcpp::R_ARM_ABS32_NOI:
5062 // If building a shared library (or a position-independent
5063 // executable), we need to create a dynamic relocation for
5064 // this location. The relocation applied at link time will
5065 // apply the link-time value, so we flag the location with
5066 // an R_ARM_RELATIVE relocation so the dynamic loader can
5067 // relocate it easily.
5068 if (parameters->options().output_is_position_independent())
5070 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5071 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
5072 // If we are to add more other reloc types than R_ARM_ABS32,
5073 // we need to add check_non_pic(object, r_type) here.
5074 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
5075 output_section, data_shndx,
5076 reloc.get_r_offset());
5080 case elfcpp::R_ARM_REL32:
5081 case elfcpp::R_ARM_THM_CALL:
5082 case elfcpp::R_ARM_CALL:
5083 case elfcpp::R_ARM_PREL31:
5084 case elfcpp::R_ARM_JUMP24:
5085 case elfcpp::R_ARM_PLT32:
5086 case elfcpp::R_ARM_THM_ABS5:
5087 case elfcpp::R_ARM_ABS8:
5088 case elfcpp::R_ARM_ABS12:
5089 case elfcpp::R_ARM_ABS16:
5090 case elfcpp::R_ARM_BASE_ABS:
5091 case elfcpp::R_ARM_MOVW_ABS_NC:
5092 case elfcpp::R_ARM_MOVT_ABS:
5093 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5094 case elfcpp::R_ARM_THM_MOVT_ABS:
5095 case elfcpp::R_ARM_MOVW_PREL_NC:
5096 case elfcpp::R_ARM_MOVT_PREL:
5097 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5098 case elfcpp::R_ARM_THM_MOVT_PREL:
5101 case elfcpp::R_ARM_GOTOFF32:
5102 // We need a GOT section:
5103 target->got_section(symtab, layout);
5106 case elfcpp::R_ARM_BASE_PREL:
5107 // FIXME: What about this?
5110 case elfcpp::R_ARM_GOT_BREL:
5111 case elfcpp::R_ARM_GOT_PREL:
5113 // The symbol requires a GOT entry.
5114 Output_data_got<32, big_endian>* got =
5115 target->got_section(symtab, layout);
5116 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
5117 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
5119 // If we are generating a shared object, we need to add a
5120 // dynamic RELATIVE relocation for this symbol's GOT entry.
5121 if (parameters->options().output_is_position_independent())
5123 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5124 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
5125 rel_dyn->add_local_relative(
5126 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
5127 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
5133 case elfcpp::R_ARM_TARGET1:
5134 // This should have been mapped to another type already.
5136 case elfcpp::R_ARM_COPY:
5137 case elfcpp::R_ARM_GLOB_DAT:
5138 case elfcpp::R_ARM_JUMP_SLOT:
5139 case elfcpp::R_ARM_RELATIVE:
5140 // These are relocations which should only be seen by the
5141 // dynamic linker, and should never be seen here.
5142 gold_error(_("%s: unexpected reloc %u in object file"),
5143 object->name().c_str(), r_type);
5147 unsupported_reloc_local(object, r_type);
5152 // Report an unsupported relocation against a global symbol.
5154 template<bool big_endian>
5156 Target_arm<big_endian>::Scan::unsupported_reloc_global(
5157 Sized_relobj<32, big_endian>* object,
5158 unsigned int r_type,
5161 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5162 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5165 // Scan a relocation for a global symbol.
5166 // FIXME: This only handles a subset of relocation types used by Android
5167 // on ARM v5te devices.
5169 template<bool big_endian>
5171 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
5174 Sized_relobj<32, big_endian>* object,
5175 unsigned int data_shndx,
5176 Output_section* output_section,
5177 const elfcpp::Rel<32, big_endian>& reloc,
5178 unsigned int r_type,
5181 r_type = get_real_reloc_type(r_type);
5184 case elfcpp::R_ARM_NONE:
5187 case elfcpp::R_ARM_ABS32:
5188 case elfcpp::R_ARM_ABS32_NOI:
5190 // Make a dynamic relocation if necessary.
5191 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
5193 if (target->may_need_copy_reloc(gsym))
5195 target->copy_reloc(symtab, layout, object,
5196 data_shndx, output_section, gsym, reloc);
5198 else if (gsym->can_use_relative_reloc(false))
5200 // If we are to add more other reloc types than R_ARM_ABS32,
5201 // we need to add check_non_pic(object, r_type) here.
5202 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5203 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
5204 output_section, object,
5205 data_shndx, reloc.get_r_offset());
5209 // If we are to add more other reloc types than R_ARM_ABS32,
5210 // we need to add check_non_pic(object, r_type) here.
5211 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5212 rel_dyn->add_global(gsym, r_type, output_section, object,
5213 data_shndx, reloc.get_r_offset());
5219 case elfcpp::R_ARM_MOVW_ABS_NC:
5220 case elfcpp::R_ARM_MOVT_ABS:
5221 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5222 case elfcpp::R_ARM_THM_MOVT_ABS:
5223 case elfcpp::R_ARM_MOVW_PREL_NC:
5224 case elfcpp::R_ARM_MOVT_PREL:
5225 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5226 case elfcpp::R_ARM_THM_MOVT_PREL:
5229 case elfcpp::R_ARM_THM_ABS5:
5230 case elfcpp::R_ARM_ABS8:
5231 case elfcpp::R_ARM_ABS12:
5232 case elfcpp::R_ARM_ABS16:
5233 case elfcpp::R_ARM_BASE_ABS:
5235 // No dynamic relocs of this kinds.
5236 // Report the error in case of PIC.
5237 int flags = Symbol::NON_PIC_REF;
5238 if (gsym->type() == elfcpp::STT_FUNC
5239 || gsym->type() == elfcpp::STT_ARM_TFUNC)
5240 flags |= Symbol::FUNCTION_CALL;
5241 if (gsym->needs_dynamic_reloc(flags))
5242 check_non_pic(object, r_type);
5246 case elfcpp::R_ARM_REL32:
5247 case elfcpp::R_ARM_PREL31:
5249 // Make a dynamic relocation if necessary.
5250 int flags = Symbol::NON_PIC_REF;
5251 if (gsym->needs_dynamic_reloc(flags))
5253 if (target->may_need_copy_reloc(gsym))
5255 target->copy_reloc(symtab, layout, object,
5256 data_shndx, output_section, gsym, reloc);
5260 check_non_pic(object, r_type);
5261 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5262 rel_dyn->add_global(gsym, r_type, output_section, object,
5263 data_shndx, reloc.get_r_offset());
5269 case elfcpp::R_ARM_JUMP24:
5270 case elfcpp::R_ARM_THM_JUMP24:
5271 case elfcpp::R_ARM_CALL:
5272 case elfcpp::R_ARM_THM_CALL:
5274 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
5275 target->make_plt_entry(symtab, layout, gsym);
5278 // Check to see if this is a function that would need a PLT
5279 // but does not get one because the function symbol is untyped.
5280 // This happens in assembly code missing a proper .type directive.
5281 if ((!gsym->is_undefined() || parameters->options().shared())
5282 && !parameters->doing_static_link()
5283 && gsym->type() == elfcpp::STT_NOTYPE
5284 && (gsym->is_from_dynobj()
5285 || gsym->is_undefined()
5286 || gsym->is_preemptible()))
5287 gold_error(_("%s is not a function."),
5288 gsym->demangled_name().c_str());
5292 case elfcpp::R_ARM_PLT32:
5293 // If the symbol is fully resolved, this is just a relative
5294 // local reloc. Otherwise we need a PLT entry.
5295 if (gsym->final_value_is_known())
5297 // If building a shared library, we can also skip the PLT entry
5298 // if the symbol is defined in the output file and is protected
5300 if (gsym->is_defined()
5301 && !gsym->is_from_dynobj()
5302 && !gsym->is_preemptible())
5304 target->make_plt_entry(symtab, layout, gsym);
5307 case elfcpp::R_ARM_GOTOFF32:
5308 // We need a GOT section.
5309 target->got_section(symtab, layout);
5312 case elfcpp::R_ARM_BASE_PREL:
5313 // FIXME: What about this?
5316 case elfcpp::R_ARM_GOT_BREL:
5317 case elfcpp::R_ARM_GOT_PREL:
5319 // The symbol requires a GOT entry.
5320 Output_data_got<32, big_endian>* got =
5321 target->got_section(symtab, layout);
5322 if (gsym->final_value_is_known())
5323 got->add_global(gsym, GOT_TYPE_STANDARD);
5326 // If this symbol is not fully resolved, we need to add a
5327 // GOT entry with a dynamic relocation.
5328 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5329 if (gsym->is_from_dynobj()
5330 || gsym->is_undefined()
5331 || gsym->is_preemptible())
5332 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
5333 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
5336 if (got->add_global(gsym, GOT_TYPE_STANDARD))
5337 rel_dyn->add_global_relative(
5338 gsym, elfcpp::R_ARM_RELATIVE, got,
5339 gsym->got_offset(GOT_TYPE_STANDARD));
5345 case elfcpp::R_ARM_TARGET1:
5346 // This should have been mapped to another type already.
5348 case elfcpp::R_ARM_COPY:
5349 case elfcpp::R_ARM_GLOB_DAT:
5350 case elfcpp::R_ARM_JUMP_SLOT:
5351 case elfcpp::R_ARM_RELATIVE:
5352 // These are relocations which should only be seen by the
5353 // dynamic linker, and should never be seen here.
5354 gold_error(_("%s: unexpected reloc %u in object file"),
5355 object->name().c_str(), r_type);
5359 unsupported_reloc_global(object, r_type, gsym);
5364 // Process relocations for gc.
5366 template<bool big_endian>
5368 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
5370 Sized_relobj<32, big_endian>* object,
5371 unsigned int data_shndx,
5373 const unsigned char* prelocs,
5375 Output_section* output_section,
5376 bool needs_special_offset_handling,
5377 size_t local_symbol_count,
5378 const unsigned char* plocal_symbols)
5380 typedef Target_arm<big_endian> Arm;
5381 typedef typename Target_arm<big_endian>::Scan Scan;
5383 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
5392 needs_special_offset_handling,
5397 // Scan relocations for a section.
5399 template<bool big_endian>
5401 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
5403 Sized_relobj<32, big_endian>* object,
5404 unsigned int data_shndx,
5405 unsigned int sh_type,
5406 const unsigned char* prelocs,
5408 Output_section* output_section,
5409 bool needs_special_offset_handling,
5410 size_t local_symbol_count,
5411 const unsigned char* plocal_symbols)
5413 typedef typename Target_arm<big_endian>::Scan Scan;
5414 if (sh_type == elfcpp::SHT_RELA)
5416 gold_error(_("%s: unsupported RELA reloc section"),
5417 object->name().c_str());
5421 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
5430 needs_special_offset_handling,
5435 // Finalize the sections.
5437 template<bool big_endian>
5439 Target_arm<big_endian>::do_finalize_sections(
5441 const Input_objects* input_objects,
5442 Symbol_table* symtab)
5444 // Merge processor-specific flags.
5445 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
5446 p != input_objects->relobj_end();
5449 Arm_relobj<big_endian>* arm_relobj =
5450 Arm_relobj<big_endian>::as_arm_relobj(*p);
5451 this->merge_processor_specific_flags(
5453 arm_relobj->processor_specific_flags());
5454 this->merge_object_attributes(arm_relobj->name().c_str(),
5455 arm_relobj->attributes_section_data());
5459 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
5460 p != input_objects->dynobj_end();
5463 Arm_dynobj<big_endian>* arm_dynobj =
5464 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
5465 this->merge_processor_specific_flags(
5467 arm_dynobj->processor_specific_flags());
5468 this->merge_object_attributes(arm_dynobj->name().c_str(),
5469 arm_dynobj->attributes_section_data());
5473 Object_attribute* attr =
5474 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
5475 if (attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
5476 this->set_may_use_blx(true);
5478 // Fill in some more dynamic tags.
5479 const Reloc_section* rel_plt = (this->plt_ == NULL
5481 : this->plt_->rel_plt());
5482 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
5483 this->rel_dyn_, true);
5485 // Emit any relocs we saved in an attempt to avoid generating COPY
5487 if (this->copy_relocs_.any_saved_relocs())
5488 this->copy_relocs_.emit(this->rel_dyn_section(layout));
5490 // Handle the .ARM.exidx section.
5491 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
5492 if (exidx_section != NULL
5493 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
5494 && !parameters->options().relocatable())
5496 // Create __exidx_start and __exdix_end symbols.
5497 symtab->define_in_output_data("__exidx_start", NULL,
5498 Symbol_table::PREDEFINED,
5499 exidx_section, 0, 0, elfcpp::STT_OBJECT,
5500 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
5502 symtab->define_in_output_data("__exidx_end", NULL,
5503 Symbol_table::PREDEFINED,
5504 exidx_section, 0, 0, elfcpp::STT_OBJECT,
5505 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
5508 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
5509 // the .ARM.exidx section.
5510 if (!layout->script_options()->saw_phdrs_clause())
5512 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
5514 Output_segment* exidx_segment =
5515 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
5516 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
5521 // Create an .ARM.attributes section if there is not one already.
5522 Output_attributes_section_data* attributes_section =
5523 new Output_attributes_section_data(*this->attributes_section_data_);
5524 layout->add_output_section_data(".ARM.attributes",
5525 elfcpp::SHT_ARM_ATTRIBUTES, 0,
5526 attributes_section, false, false, false,
5530 // Return whether a direct absolute static relocation needs to be applied.
5531 // In cases where Scan::local() or Scan::global() has created
5532 // a dynamic relocation other than R_ARM_RELATIVE, the addend
5533 // of the relocation is carried in the data, and we must not
5534 // apply the static relocation.
5536 template<bool big_endian>
5538 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
5539 const Sized_symbol<32>* gsym,
5542 Output_section* output_section)
5544 // If the output section is not allocated, then we didn't call
5545 // scan_relocs, we didn't create a dynamic reloc, and we must apply
5547 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
5550 // For local symbols, we will have created a non-RELATIVE dynamic
5551 // relocation only if (a) the output is position independent,
5552 // (b) the relocation is absolute (not pc- or segment-relative), and
5553 // (c) the relocation is not 32 bits wide.
5555 return !(parameters->options().output_is_position_independent()
5556 && (ref_flags & Symbol::ABSOLUTE_REF)
5559 // For global symbols, we use the same helper routines used in the
5560 // scan pass. If we did not create a dynamic relocation, or if we
5561 // created a RELATIVE dynamic relocation, we should apply the static
5563 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
5564 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
5565 && gsym->can_use_relative_reloc(ref_flags
5566 & Symbol::FUNCTION_CALL);
5567 return !has_dyn || is_rel;
5570 // Perform a relocation.
5572 template<bool big_endian>
5574 Target_arm<big_endian>::Relocate::relocate(
5575 const Relocate_info<32, big_endian>* relinfo,
5577 Output_section *output_section,
5579 const elfcpp::Rel<32, big_endian>& rel,
5580 unsigned int r_type,
5581 const Sized_symbol<32>* gsym,
5582 const Symbol_value<32>* psymval,
5583 unsigned char* view,
5584 Arm_address address,
5585 section_size_type /* view_size */ )
5587 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
5589 r_type = get_real_reloc_type(r_type);
5591 const Arm_relobj<big_endian>* object =
5592 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
5594 // If the final branch target of a relocation is THUMB instruction, this
5595 // is 1. Otherwise it is 0.
5596 Arm_address thumb_bit = 0;
5597 Symbol_value<32> symval;
5598 bool is_weakly_undefined_without_plt = false;
5599 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
5603 // This is a global symbol. Determine if we use PLT and if the
5604 // final target is THUMB.
5605 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
5607 // This uses a PLT, change the symbol value.
5608 symval.set_output_value(target->plt_section()->address()
5609 + gsym->plt_offset());
5612 else if (gsym->is_weak_undefined())
5614 // This is a weakly undefined symbol and we do not use PLT
5615 // for this relocation. A branch targeting this symbol will
5616 // be converted into an NOP.
5617 is_weakly_undefined_without_plt = true;
5621 // Set thumb bit if symbol:
5622 // -Has type STT_ARM_TFUNC or
5623 // -Has type STT_FUNC, is defined and with LSB in value set.
5625 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
5626 || (gsym->type() == elfcpp::STT_FUNC
5627 && !gsym->is_undefined()
5628 && ((psymval->value(object, 0) & 1) != 0)))
5635 // This is a local symbol. Determine if the final target is THUMB.
5636 // We saved this information when all the local symbols were read.
5637 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
5638 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
5639 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
5644 // This is a fake relocation synthesized for a stub. It does not have
5645 // a real symbol. We just look at the LSB of the symbol value to
5646 // determine if the target is THUMB or not.
5647 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
5650 // Strip LSB if this points to a THUMB target.
5652 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
5653 && ((psymval->value(object, 0) & 1) != 0))
5655 Arm_address stripped_value =
5656 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
5657 symval.set_output_value(stripped_value);
5661 // Get the GOT offset if needed.
5662 // The GOT pointer points to the end of the GOT section.
5663 // We need to subtract the size of the GOT section to get
5664 // the actual offset to use in the relocation.
5665 bool have_got_offset = false;
5666 unsigned int got_offset = 0;
5669 case elfcpp::R_ARM_GOT_BREL:
5670 case elfcpp::R_ARM_GOT_PREL:
5673 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5674 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
5675 - target->got_size());
5679 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5680 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5681 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
5682 - target->got_size());
5684 have_got_offset = true;
5691 // To look up relocation stubs, we need to pass the symbol table index of
5693 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5695 typename Arm_relocate_functions::Status reloc_status =
5696 Arm_relocate_functions::STATUS_OKAY;
5699 case elfcpp::R_ARM_NONE:
5702 case elfcpp::R_ARM_ABS8:
5703 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5705 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
5708 case elfcpp::R_ARM_ABS12:
5709 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5711 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
5714 case elfcpp::R_ARM_ABS16:
5715 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5717 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5720 case elfcpp::R_ARM_ABS32:
5721 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5723 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5727 case elfcpp::R_ARM_ABS32_NOI:
5728 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5730 // No thumb bit for this relocation: (S + A)
5731 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5735 case elfcpp::R_ARM_MOVW_ABS_NC:
5736 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5738 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
5742 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5743 "a shared object; recompile with -fPIC"));
5746 case elfcpp::R_ARM_MOVT_ABS:
5747 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5749 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
5751 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5752 "a shared object; recompile with -fPIC"));
5755 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5756 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5758 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
5762 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5763 "making a shared object; recompile with -fPIC"));
5766 case elfcpp::R_ARM_THM_MOVT_ABS:
5767 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5769 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
5772 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5773 "making a shared object; recompile with -fPIC"));
5776 case elfcpp::R_ARM_MOVW_PREL_NC:
5777 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
5782 case elfcpp::R_ARM_MOVT_PREL:
5783 reloc_status = Arm_relocate_functions::movt_prel(view, object,
5787 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5788 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
5793 case elfcpp::R_ARM_THM_MOVT_PREL:
5794 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
5798 case elfcpp::R_ARM_REL32:
5799 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5800 address, thumb_bit);
5803 case elfcpp::R_ARM_THM_ABS5:
5804 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5806 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
5809 case elfcpp::R_ARM_THM_CALL:
5811 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
5812 psymval, address, thumb_bit,
5813 is_weakly_undefined_without_plt);
5816 case elfcpp::R_ARM_XPC25:
5818 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
5819 psymval, address, thumb_bit,
5820 is_weakly_undefined_without_plt);
5823 case elfcpp::R_ARM_THM_XPC22:
5825 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
5826 psymval, address, thumb_bit,
5827 is_weakly_undefined_without_plt);
5830 case elfcpp::R_ARM_GOTOFF32:
5832 Arm_address got_origin;
5833 got_origin = target->got_plt_section()->address();
5834 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5835 got_origin, thumb_bit);
5839 case elfcpp::R_ARM_BASE_PREL:
5842 // Get the addressing origin of the output segment defining the
5843 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5844 gold_assert(gsym != NULL);
5845 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5846 origin = gsym->output_segment()->vaddr();
5847 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5848 origin = gsym->output_data()->address();
5851 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5852 _("cannot find origin of R_ARM_BASE_PREL"));
5855 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
5859 case elfcpp::R_ARM_BASE_ABS:
5861 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5866 // Get the addressing origin of the output segment defining
5867 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5869 // R_ARM_BASE_ABS with the NULL symbol will give the
5870 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5871 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5872 origin = target->got_plt_section()->address();
5873 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5874 origin = gsym->output_segment()->vaddr();
5875 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5876 origin = gsym->output_data()->address();
5879 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5880 _("cannot find origin of R_ARM_BASE_ABS"));
5884 reloc_status = Arm_relocate_functions::base_abs(view, origin);
5888 case elfcpp::R_ARM_GOT_BREL:
5889 gold_assert(have_got_offset);
5890 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
5893 case elfcpp::R_ARM_GOT_PREL:
5894 gold_assert(have_got_offset);
5895 // Get the address origin for GOT PLT, which is allocated right
5896 // after the GOT section, to calculate an absolute address of
5897 // the symbol GOT entry (got_origin + got_offset).
5898 Arm_address got_origin;
5899 got_origin = target->got_plt_section()->address();
5900 reloc_status = Arm_relocate_functions::got_prel(view,
5901 got_origin + got_offset,
5905 case elfcpp::R_ARM_PLT32:
5906 gold_assert(gsym == NULL
5907 || gsym->has_plt_offset()
5908 || gsym->final_value_is_known()
5909 || (gsym->is_defined()
5910 && !gsym->is_from_dynobj()
5911 && !gsym->is_preemptible()));
5913 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
5914 psymval, address, thumb_bit,
5915 is_weakly_undefined_without_plt);
5918 case elfcpp::R_ARM_CALL:
5920 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
5921 psymval, address, thumb_bit,
5922 is_weakly_undefined_without_plt);
5925 case elfcpp::R_ARM_JUMP24:
5927 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
5928 psymval, address, thumb_bit,
5929 is_weakly_undefined_without_plt);
5932 case elfcpp::R_ARM_THM_JUMP24:
5934 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
5935 psymval, address, thumb_bit,
5936 is_weakly_undefined_without_plt);
5939 case elfcpp::R_ARM_PREL31:
5940 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
5941 address, thumb_bit);
5944 case elfcpp::R_ARM_TARGET1:
5945 // This should have been mapped to another type already.
5947 case elfcpp::R_ARM_COPY:
5948 case elfcpp::R_ARM_GLOB_DAT:
5949 case elfcpp::R_ARM_JUMP_SLOT:
5950 case elfcpp::R_ARM_RELATIVE:
5951 // These are relocations which should only be seen by the
5952 // dynamic linker, and should never be seen here.
5953 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5954 _("unexpected reloc %u in object file"),
5959 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5960 _("unsupported reloc %u"),
5965 // Report any errors.
5966 switch (reloc_status)
5968 case Arm_relocate_functions::STATUS_OKAY:
5970 case Arm_relocate_functions::STATUS_OVERFLOW:
5971 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5972 _("relocation overflow in relocation %u"),
5975 case Arm_relocate_functions::STATUS_BAD_RELOC:
5976 gold_error_at_location(
5980 _("unexpected opcode while processing relocation %u"),
5990 // Relocate section data.
5992 template<bool big_endian>
5994 Target_arm<big_endian>::relocate_section(
5995 const Relocate_info<32, big_endian>* relinfo,
5996 unsigned int sh_type,
5997 const unsigned char* prelocs,
5999 Output_section* output_section,
6000 bool needs_special_offset_handling,
6001 unsigned char* view,
6002 Arm_address address,
6003 section_size_type view_size,
6004 const Reloc_symbol_changes* reloc_symbol_changes)
6006 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
6007 gold_assert(sh_type == elfcpp::SHT_REL);
6009 Arm_input_section<big_endian>* arm_input_section =
6010 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
6012 // This is an ARM input section and the view covers the whole output
6014 if (arm_input_section != NULL)
6016 gold_assert(needs_special_offset_handling);
6017 Arm_address section_address = arm_input_section->address();
6018 section_size_type section_size = arm_input_section->data_size();
6020 gold_assert((arm_input_section->address() >= address)
6021 && ((arm_input_section->address()
6022 + arm_input_section->data_size())
6023 <= (address + view_size)));
6025 off_t offset = section_address - address;
6028 view_size = section_size;
6031 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
6038 needs_special_offset_handling,
6042 reloc_symbol_changes);
6045 // Return the size of a relocation while scanning during a relocatable
6048 template<bool big_endian>
6050 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
6051 unsigned int r_type,
6054 r_type = get_real_reloc_type(r_type);
6057 case elfcpp::R_ARM_NONE:
6060 case elfcpp::R_ARM_ABS8:
6063 case elfcpp::R_ARM_ABS16:
6064 case elfcpp::R_ARM_THM_ABS5:
6067 case elfcpp::R_ARM_ABS32:
6068 case elfcpp::R_ARM_ABS32_NOI:
6069 case elfcpp::R_ARM_ABS12:
6070 case elfcpp::R_ARM_BASE_ABS:
6071 case elfcpp::R_ARM_REL32:
6072 case elfcpp::R_ARM_THM_CALL:
6073 case elfcpp::R_ARM_GOTOFF32:
6074 case elfcpp::R_ARM_BASE_PREL:
6075 case elfcpp::R_ARM_GOT_BREL:
6076 case elfcpp::R_ARM_GOT_PREL:
6077 case elfcpp::R_ARM_PLT32:
6078 case elfcpp::R_ARM_CALL:
6079 case elfcpp::R_ARM_JUMP24:
6080 case elfcpp::R_ARM_PREL31:
6081 case elfcpp::R_ARM_MOVW_ABS_NC:
6082 case elfcpp::R_ARM_MOVT_ABS:
6083 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6084 case elfcpp::R_ARM_THM_MOVT_ABS:
6085 case elfcpp::R_ARM_MOVW_PREL_NC:
6086 case elfcpp::R_ARM_MOVT_PREL:
6087 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6088 case elfcpp::R_ARM_THM_MOVT_PREL:
6091 case elfcpp::R_ARM_TARGET1:
6092 // This should have been mapped to another type already.
6094 case elfcpp::R_ARM_COPY:
6095 case elfcpp::R_ARM_GLOB_DAT:
6096 case elfcpp::R_ARM_JUMP_SLOT:
6097 case elfcpp::R_ARM_RELATIVE:
6098 // These are relocations which should only be seen by the
6099 // dynamic linker, and should never be seen here.
6100 gold_error(_("%s: unexpected reloc %u in object file"),
6101 object->name().c_str(), r_type);
6105 object->error(_("unsupported reloc %u in object file"), r_type);
6110 // Scan the relocs during a relocatable link.
6112 template<bool big_endian>
6114 Target_arm<big_endian>::scan_relocatable_relocs(
6115 Symbol_table* symtab,
6117 Sized_relobj<32, big_endian>* object,
6118 unsigned int data_shndx,
6119 unsigned int sh_type,
6120 const unsigned char* prelocs,
6122 Output_section* output_section,
6123 bool needs_special_offset_handling,
6124 size_t local_symbol_count,
6125 const unsigned char* plocal_symbols,
6126 Relocatable_relocs* rr)
6128 gold_assert(sh_type == elfcpp::SHT_REL);
6130 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
6131 Relocatable_size_for_reloc> Scan_relocatable_relocs;
6133 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
6134 Scan_relocatable_relocs>(
6142 needs_special_offset_handling,
6148 // Relocate a section during a relocatable link.
6150 template<bool big_endian>
6152 Target_arm<big_endian>::relocate_for_relocatable(
6153 const Relocate_info<32, big_endian>* relinfo,
6154 unsigned int sh_type,
6155 const unsigned char* prelocs,
6157 Output_section* output_section,
6158 off_t offset_in_output_section,
6159 const Relocatable_relocs* rr,
6160 unsigned char* view,
6161 Arm_address view_address,
6162 section_size_type view_size,
6163 unsigned char* reloc_view,
6164 section_size_type reloc_view_size)
6166 gold_assert(sh_type == elfcpp::SHT_REL);
6168 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
6173 offset_in_output_section,
6182 // Return the value to use for a dynamic symbol which requires special
6183 // treatment. This is how we support equality comparisons of function
6184 // pointers across shared library boundaries, as described in the
6185 // processor specific ABI supplement.
6187 template<bool big_endian>
6189 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
6191 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6192 return this->plt_section()->address() + gsym->plt_offset();
6195 // Map platform-specific relocs to real relocs
6197 template<bool big_endian>
6199 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
6203 case elfcpp::R_ARM_TARGET1:
6204 // This is either R_ARM_ABS32 or R_ARM_REL32;
6205 return elfcpp::R_ARM_ABS32;
6207 case elfcpp::R_ARM_TARGET2:
6208 // This can be any reloc type but ususally is R_ARM_GOT_PREL
6209 return elfcpp::R_ARM_GOT_PREL;
6216 // Whether if two EABI versions V1 and V2 are compatible.
6218 template<bool big_endian>
6220 Target_arm<big_endian>::are_eabi_versions_compatible(
6221 elfcpp::Elf_Word v1,
6222 elfcpp::Elf_Word v2)
6224 // v4 and v5 are the same spec before and after it was released,
6225 // so allow mixing them.
6226 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
6227 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
6233 // Combine FLAGS from an input object called NAME and the processor-specific
6234 // flags in the ELF header of the output. Much of this is adapted from the
6235 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
6236 // in bfd/elf32-arm.c.
6238 template<bool big_endian>
6240 Target_arm<big_endian>::merge_processor_specific_flags(
6241 const std::string& name,
6242 elfcpp::Elf_Word flags)
6244 if (this->are_processor_specific_flags_set())
6246 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
6248 // Nothing to merge if flags equal to those in output.
6249 if (flags == out_flags)
6252 // Complain about various flag mismatches.
6253 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
6254 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
6255 if (!this->are_eabi_versions_compatible(version1, version2))
6256 gold_error(_("Source object %s has EABI version %d but output has "
6257 "EABI version %d."),
6259 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
6260 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
6264 // If the input is the default architecture and had the default
6265 // flags then do not bother setting the flags for the output
6266 // architecture, instead allow future merges to do this. If no
6267 // future merges ever set these flags then they will retain their
6268 // uninitialised values, which surprise surprise, correspond
6269 // to the default values.
6273 // This is the first time, just copy the flags.
6274 // We only copy the EABI version for now.
6275 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
6279 // Adjust ELF file header.
6280 template<bool big_endian>
6282 Target_arm<big_endian>::do_adjust_elf_header(
6283 unsigned char* view,
6286 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
6288 elfcpp::Ehdr<32, big_endian> ehdr(view);
6289 unsigned char e_ident[elfcpp::EI_NIDENT];
6290 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
6292 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
6293 == elfcpp::EF_ARM_EABI_UNKNOWN)
6294 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
6296 e_ident[elfcpp::EI_OSABI] = 0;
6297 e_ident[elfcpp::EI_ABIVERSION] = 0;
6299 // FIXME: Do EF_ARM_BE8 adjustment.
6301 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
6302 oehdr.put_e_ident(e_ident);
6305 // do_make_elf_object to override the same function in the base class.
6306 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
6307 // to store ARM specific information. Hence we need to have our own
6308 // ELF object creation.
6310 template<bool big_endian>
6312 Target_arm<big_endian>::do_make_elf_object(
6313 const std::string& name,
6314 Input_file* input_file,
6315 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
6317 int et = ehdr.get_e_type();
6318 if (et == elfcpp::ET_REL)
6320 Arm_relobj<big_endian>* obj =
6321 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
6325 else if (et == elfcpp::ET_DYN)
6327 Sized_dynobj<32, big_endian>* obj =
6328 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
6334 gold_error(_("%s: unsupported ELF file type %d"),
6340 // Read the architecture from the Tag_also_compatible_with attribute, if any.
6341 // Returns -1 if no architecture could be read.
6342 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
6344 template<bool big_endian>
6346 Target_arm<big_endian>::get_secondary_compatible_arch(
6347 const Attributes_section_data* pasd)
6349 const Object_attribute *known_attributes =
6350 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
6352 // Note: the tag and its argument below are uleb128 values, though
6353 // currently-defined values fit in one byte for each.
6354 const std::string& sv =
6355 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
6357 && sv.data()[0] == elfcpp::Tag_CPU_arch
6358 && (sv.data()[1] & 128) != 128)
6359 return sv.data()[1];
6361 // This tag is "safely ignorable", so don't complain if it looks funny.
6365 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
6366 // The tag is removed if ARCH is -1.
6367 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
6369 template<bool big_endian>
6371 Target_arm<big_endian>::set_secondary_compatible_arch(
6372 Attributes_section_data* pasd,
6375 Object_attribute *known_attributes =
6376 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
6380 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
6384 // Note: the tag and its argument below are uleb128 values, though
6385 // currently-defined values fit in one byte for each.
6387 sv[0] = elfcpp::Tag_CPU_arch;
6388 gold_assert(arch != 0);
6392 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
6395 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
6397 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
6399 template<bool big_endian>
6401 Target_arm<big_endian>::tag_cpu_arch_combine(
6404 int* secondary_compat_out,
6406 int secondary_compat)
6408 #define T(X) elfcpp::TAG_CPU_ARCH_##X
6409 static const int v6t2[] =
6421 static const int v6k[] =
6434 static const int v7[] =
6448 static const int v6_m[] =
6463 static const int v6s_m[] =
6479 static const int v7e_m[] =
6496 static const int v4t_plus_v6_m[] =
6512 T(V4T_PLUS_V6_M) // V4T plus V6_M.
6514 static const int *comb[] =
6522 // Pseudo-architecture.
6526 // Check we've not got a higher architecture than we know about.
6528 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
6530 gold_error(_("%s: unknown CPU architecture"), name);
6534 // Override old tag if we have a Tag_also_compatible_with on the output.
6536 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
6537 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
6538 oldtag = T(V4T_PLUS_V6_M);
6540 // And override the new tag if we have a Tag_also_compatible_with on the
6543 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
6544 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
6545 newtag = T(V4T_PLUS_V6_M);
6547 // Architectures before V6KZ add features monotonically.
6548 int tagh = std::max(oldtag, newtag);
6549 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
6552 int tagl = std::min(oldtag, newtag);
6553 int result = comb[tagh - T(V6T2)][tagl];
6555 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
6556 // as the canonical version.
6557 if (result == T(V4T_PLUS_V6_M))
6560 *secondary_compat_out = T(V6_M);
6563 *secondary_compat_out = -1;
6567 gold_error(_("%s: conflicting CPU architectures %d/%d"),
6568 name, oldtag, newtag);
6576 // Helper to print AEABI enum tag value.
6578 template<bool big_endian>
6580 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
6582 static const char *aeabi_enum_names[] =
6583 { "", "variable-size", "32-bit", "" };
6584 const size_t aeabi_enum_names_size =
6585 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
6587 if (value < aeabi_enum_names_size)
6588 return std::string(aeabi_enum_names[value]);
6592 sprintf(buffer, "<unknown value %u>", value);
6593 return std::string(buffer);
6597 // Return the string value to store in TAG_CPU_name.
6599 template<bool big_endian>
6601 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
6603 static const char *name_table[] = {
6604 // These aren't real CPU names, but we can't guess
6605 // that from the architecture version alone.
6621 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
6623 if (value < name_table_size)
6624 return std::string(name_table[value]);
6628 sprintf(buffer, "<unknown CPU value %u>", value);
6629 return std::string(buffer);
6633 // Merge object attributes from input file called NAME with those of the
6634 // output. The input object attributes are in the object pointed by PASD.
6636 template<bool big_endian>
6638 Target_arm<big_endian>::merge_object_attributes(
6640 const Attributes_section_data* pasd)
6642 // Return if there is no attributes section data.
6646 // If output has no object attributes, just copy.
6647 if (this->attributes_section_data_ == NULL)
6649 this->attributes_section_data_ = new Attributes_section_data(*pasd);
6653 const int vendor = Object_attribute::OBJ_ATTR_PROC;
6654 const Object_attribute* in_attr = pasd->known_attributes(vendor);
6655 Object_attribute* out_attr =
6656 this->attributes_section_data_->known_attributes(vendor);
6658 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6659 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
6660 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
6662 // Ignore mismatches if the object doesn't use floating point. */
6663 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
6664 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
6665 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
6666 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
6667 gold_error(_("%s uses VFP register arguments, output does not"),
6671 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
6673 // Merge this attribute with existing attributes.
6676 case elfcpp::Tag_CPU_raw_name:
6677 case elfcpp::Tag_CPU_name:
6678 // These are merged after Tag_CPU_arch.
6681 case elfcpp::Tag_ABI_optimization_goals:
6682 case elfcpp::Tag_ABI_FP_optimization_goals:
6683 // Use the first value seen.
6686 case elfcpp::Tag_CPU_arch:
6688 unsigned int saved_out_attr = out_attr->int_value();
6689 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6690 int secondary_compat =
6691 this->get_secondary_compatible_arch(pasd);
6692 int secondary_compat_out =
6693 this->get_secondary_compatible_arch(
6694 this->attributes_section_data_);
6695 out_attr[i].set_int_value(
6696 tag_cpu_arch_combine(name, out_attr[i].int_value(),
6697 &secondary_compat_out,
6698 in_attr[i].int_value(),
6700 this->set_secondary_compatible_arch(this->attributes_section_data_,
6701 secondary_compat_out);
6703 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6704 if (out_attr[i].int_value() == saved_out_attr)
6705 ; // Leave the names alone.
6706 else if (out_attr[i].int_value() == in_attr[i].int_value())
6708 // The output architecture has been changed to match the
6709 // input architecture. Use the input names.
6710 out_attr[elfcpp::Tag_CPU_name].set_string_value(
6711 in_attr[elfcpp::Tag_CPU_name].string_value());
6712 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
6713 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
6717 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
6718 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
6721 // If we still don't have a value for Tag_CPU_name,
6722 // make one up now. Tag_CPU_raw_name remains blank.
6723 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
6725 const std::string cpu_name =
6726 this->tag_cpu_name_value(out_attr[i].int_value());
6727 // FIXME: If we see an unknown CPU, this will be set
6728 // to "<unknown CPU n>", where n is the attribute value.
6729 // This is different from BFD, which leaves the name alone.
6730 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
6735 case elfcpp::Tag_ARM_ISA_use:
6736 case elfcpp::Tag_THUMB_ISA_use:
6737 case elfcpp::Tag_WMMX_arch:
6738 case elfcpp::Tag_Advanced_SIMD_arch:
6739 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6740 case elfcpp::Tag_ABI_FP_rounding:
6741 case elfcpp::Tag_ABI_FP_exceptions:
6742 case elfcpp::Tag_ABI_FP_user_exceptions:
6743 case elfcpp::Tag_ABI_FP_number_model:
6744 case elfcpp::Tag_VFP_HP_extension:
6745 case elfcpp::Tag_CPU_unaligned_access:
6746 case elfcpp::Tag_T2EE_use:
6747 case elfcpp::Tag_Virtualization_use:
6748 case elfcpp::Tag_MPextension_use:
6749 // Use the largest value specified.
6750 if (in_attr[i].int_value() > out_attr[i].int_value())
6751 out_attr[i].set_int_value(in_attr[i].int_value());
6754 case elfcpp::Tag_ABI_align8_preserved:
6755 case elfcpp::Tag_ABI_PCS_RO_data:
6756 // Use the smallest value specified.
6757 if (in_attr[i].int_value() < out_attr[i].int_value())
6758 out_attr[i].set_int_value(in_attr[i].int_value());
6761 case elfcpp::Tag_ABI_align8_needed:
6762 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
6763 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
6764 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
6767 // This error message should be enabled once all non-conformant
6768 // binaries in the toolchain have had the attributes set
6770 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6774 case elfcpp::Tag_ABI_FP_denormal:
6775 case elfcpp::Tag_ABI_PCS_GOT_use:
6777 // These tags have 0 = don't care, 1 = strong requirement,
6778 // 2 = weak requirement.
6779 static const int order_021[3] = {0, 2, 1};
6781 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6782 // value if greater than 2 (for future-proofing).
6783 if ((in_attr[i].int_value() > 2
6784 && in_attr[i].int_value() > out_attr[i].int_value())
6785 || (in_attr[i].int_value() <= 2
6786 && out_attr[i].int_value() <= 2
6787 && (order_021[in_attr[i].int_value()]
6788 > order_021[out_attr[i].int_value()])))
6789 out_attr[i].set_int_value(in_attr[i].int_value());
6793 case elfcpp::Tag_CPU_arch_profile:
6794 if (out_attr[i].int_value() != in_attr[i].int_value())
6796 // 0 will merge with anything.
6797 // 'A' and 'S' merge to 'A'.
6798 // 'R' and 'S' merge to 'R'.
6799 // 'M' and 'A|R|S' is an error.
6800 if (out_attr[i].int_value() == 0
6801 || (out_attr[i].int_value() == 'S'
6802 && (in_attr[i].int_value() == 'A'
6803 || in_attr[i].int_value() == 'R')))
6804 out_attr[i].set_int_value(in_attr[i].int_value());
6805 else if (in_attr[i].int_value() == 0
6806 || (in_attr[i].int_value() == 'S'
6807 && (out_attr[i].int_value() == 'A'
6808 || out_attr[i].int_value() == 'R')))
6813 (_("conflicting architecture profiles %c/%c"),
6814 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
6815 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
6819 case elfcpp::Tag_VFP_arch:
6836 // Values greater than 6 aren't defined, so just pick the
6838 if (in_attr[i].int_value() > 6
6839 && in_attr[i].int_value() > out_attr[i].int_value())
6841 *out_attr = *in_attr;
6844 // The output uses the superset of input features
6845 // (ISA version) and registers.
6846 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
6847 vfp_versions[out_attr[i].int_value()].ver);
6848 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
6849 vfp_versions[out_attr[i].int_value()].regs);
6850 // This assumes all possible supersets are also a valid
6853 for (newval = 6; newval > 0; newval--)
6855 if (regs == vfp_versions[newval].regs
6856 && ver == vfp_versions[newval].ver)
6859 out_attr[i].set_int_value(newval);
6862 case elfcpp::Tag_PCS_config:
6863 if (out_attr[i].int_value() == 0)
6864 out_attr[i].set_int_value(in_attr[i].int_value());
6865 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6867 // It's sometimes ok to mix different configs, so this is only
6869 gold_warning(_("%s: conflicting platform configuration"), name);
6872 case elfcpp::Tag_ABI_PCS_R9_use:
6873 if (in_attr[i].int_value() != out_attr[i].int_value()
6874 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
6875 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
6877 gold_error(_("%s: conflicting use of R9"), name);
6879 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
6880 out_attr[i].set_int_value(in_attr[i].int_value());
6882 case elfcpp::Tag_ABI_PCS_RW_data:
6883 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6884 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6885 != elfcpp::AEABI_R9_SB)
6886 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6887 != elfcpp::AEABI_R9_unused))
6889 gold_error(_("%s: SB relative addressing conflicts with use "
6893 // Use the smallest value specified.
6894 if (in_attr[i].int_value() < out_attr[i].int_value())
6895 out_attr[i].set_int_value(in_attr[i].int_value());
6897 case elfcpp::Tag_ABI_PCS_wchar_t:
6898 // FIXME: Make it possible to turn off this warning.
6899 if (out_attr[i].int_value()
6900 && in_attr[i].int_value()
6901 && out_attr[i].int_value() != in_attr[i].int_value())
6903 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6904 "use %u-byte wchar_t; use of wchar_t values "
6905 "across objects may fail"),
6906 name, in_attr[i].int_value(),
6907 out_attr[i].int_value());
6909 else if (in_attr[i].int_value() && !out_attr[i].int_value())
6910 out_attr[i].set_int_value(in_attr[i].int_value());
6912 case elfcpp::Tag_ABI_enum_size:
6913 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
6915 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
6916 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
6918 // The existing object is compatible with anything.
6919 // Use whatever requirements the new object has.
6920 out_attr[i].set_int_value(in_attr[i].int_value());
6922 // FIXME: Make it possible to turn off this warning.
6923 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
6924 && out_attr[i].int_value() != in_attr[i].int_value())
6926 unsigned int in_value = in_attr[i].int_value();
6927 unsigned int out_value = out_attr[i].int_value();
6928 gold_warning(_("%s uses %s enums yet the output is to use "
6929 "%s enums; use of enum values across objects "
6932 this->aeabi_enum_name(in_value).c_str(),
6933 this->aeabi_enum_name(out_value).c_str());
6937 case elfcpp::Tag_ABI_VFP_args:
6940 case elfcpp::Tag_ABI_WMMX_args:
6941 if (in_attr[i].int_value() != out_attr[i].int_value())
6943 gold_error(_("%s uses iWMMXt register arguments, output does "
6948 case Object_attribute::Tag_compatibility:
6949 // Merged in target-independent code.
6951 case elfcpp::Tag_ABI_HardFP_use:
6952 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6953 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
6954 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
6955 out_attr[i].set_int_value(3);
6956 else if (in_attr[i].int_value() > out_attr[i].int_value())
6957 out_attr[i].set_int_value(in_attr[i].int_value());
6959 case elfcpp::Tag_ABI_FP_16bit_format:
6960 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6962 if (in_attr[i].int_value() != out_attr[i].int_value())
6963 gold_error(_("fp16 format mismatch between %s and output"),
6966 if (in_attr[i].int_value() != 0)
6967 out_attr[i].set_int_value(in_attr[i].int_value());
6970 case elfcpp::Tag_nodefaults:
6971 // This tag is set if it exists, but the value is unused (and is
6972 // typically zero). We don't actually need to do anything here -
6973 // the merge happens automatically when the type flags are merged
6976 case elfcpp::Tag_also_compatible_with:
6977 // Already done in Tag_CPU_arch.
6979 case elfcpp::Tag_conformance:
6980 // Keep the attribute if it matches. Throw it away otherwise.
6981 // No attribute means no claim to conform.
6982 if (in_attr[i].string_value() != out_attr[i].string_value())
6983 out_attr[i].set_string_value("");
6988 const char* err_object = NULL;
6990 // The "known_obj_attributes" table does contain some undefined
6991 // attributes. Ensure that there are unused.
6992 if (out_attr[i].int_value() != 0
6993 || out_attr[i].string_value() != "")
6994 err_object = "output";
6995 else if (in_attr[i].int_value() != 0
6996 || in_attr[i].string_value() != "")
6999 if (err_object != NULL)
7001 // Attribute numbers >=64 (mod 128) can be safely ignored.
7003 gold_error(_("%s: unknown mandatory EABI object attribute "
7007 gold_warning(_("%s: unknown EABI object attribute %d"),
7011 // Only pass on attributes that match in both inputs.
7012 if (!in_attr[i].matches(out_attr[i]))
7014 out_attr[i].set_int_value(0);
7015 out_attr[i].set_string_value("");
7020 // If out_attr was copied from in_attr then it won't have a type yet.
7021 if (in_attr[i].type() && !out_attr[i].type())
7022 out_attr[i].set_type(in_attr[i].type());
7025 // Merge Tag_compatibility attributes and any common GNU ones.
7026 this->attributes_section_data_->merge(name, pasd);
7028 // Check for any attributes not known on ARM.
7029 typedef Vendor_object_attributes::Other_attributes Other_attributes;
7030 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
7031 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
7032 Other_attributes* out_other_attributes =
7033 this->attributes_section_data_->other_attributes(vendor);
7034 Other_attributes::iterator out_iter = out_other_attributes->begin();
7036 while (in_iter != in_other_attributes->end()
7037 || out_iter != out_other_attributes->end())
7039 const char* err_object = NULL;
7042 // The tags for each list are in numerical order.
7043 // If the tags are equal, then merge.
7044 if (out_iter != out_other_attributes->end()
7045 && (in_iter == in_other_attributes->end()
7046 || in_iter->first > out_iter->first))
7048 // This attribute only exists in output. We can't merge, and we
7049 // don't know what the tag means, so delete it.
7050 err_object = "output";
7051 err_tag = out_iter->first;
7052 int saved_tag = out_iter->first;
7053 delete out_iter->second;
7054 out_other_attributes->erase(out_iter);
7055 out_iter = out_other_attributes->upper_bound(saved_tag);
7057 else if (in_iter != in_other_attributes->end()
7058 && (out_iter != out_other_attributes->end()
7059 || in_iter->first < out_iter->first))
7061 // This attribute only exists in input. We can't merge, and we
7062 // don't know what the tag means, so ignore it.
7064 err_tag = in_iter->first;
7067 else // The tags are equal.
7069 // As present, all attributes in the list are unknown, and
7070 // therefore can't be merged meaningfully.
7071 err_object = "output";
7072 err_tag = out_iter->first;
7074 // Only pass on attributes that match in both inputs.
7075 if (!in_iter->second->matches(*(out_iter->second)))
7077 // No match. Delete the attribute.
7078 int saved_tag = out_iter->first;
7079 delete out_iter->second;
7080 out_other_attributes->erase(out_iter);
7081 out_iter = out_other_attributes->upper_bound(saved_tag);
7085 // Matched. Keep the attribute and move to the next.
7093 // Attribute numbers >=64 (mod 128) can be safely ignored. */
7094 if ((err_tag & 127) < 64)
7096 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
7097 err_object, err_tag);
7101 gold_warning(_("%s: unknown EABI object attribute %d"),
7102 err_object, err_tag);
7108 // Return whether a relocation type used the LSB to distinguish THUMB
7110 template<bool big_endian>
7112 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
7116 case elfcpp::R_ARM_PC24:
7117 case elfcpp::R_ARM_ABS32:
7118 case elfcpp::R_ARM_REL32:
7119 case elfcpp::R_ARM_SBREL32:
7120 case elfcpp::R_ARM_THM_CALL:
7121 case elfcpp::R_ARM_GLOB_DAT:
7122 case elfcpp::R_ARM_JUMP_SLOT:
7123 case elfcpp::R_ARM_GOTOFF32:
7124 case elfcpp::R_ARM_PLT32:
7125 case elfcpp::R_ARM_CALL:
7126 case elfcpp::R_ARM_JUMP24:
7127 case elfcpp::R_ARM_THM_JUMP24:
7128 case elfcpp::R_ARM_SBREL31:
7129 case elfcpp::R_ARM_PREL31:
7130 case elfcpp::R_ARM_MOVW_ABS_NC:
7131 case elfcpp::R_ARM_MOVW_PREL_NC:
7132 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7133 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7134 case elfcpp::R_ARM_THM_JUMP19:
7135 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7136 case elfcpp::R_ARM_ALU_PC_G0_NC:
7137 case elfcpp::R_ARM_ALU_PC_G0:
7138 case elfcpp::R_ARM_ALU_PC_G1_NC:
7139 case elfcpp::R_ARM_ALU_PC_G1:
7140 case elfcpp::R_ARM_ALU_PC_G2:
7141 case elfcpp::R_ARM_ALU_SB_G0_NC:
7142 case elfcpp::R_ARM_ALU_SB_G0:
7143 case elfcpp::R_ARM_ALU_SB_G1_NC:
7144 case elfcpp::R_ARM_ALU_SB_G1:
7145 case elfcpp::R_ARM_ALU_SB_G2:
7146 case elfcpp::R_ARM_MOVW_BREL_NC:
7147 case elfcpp::R_ARM_MOVW_BREL:
7148 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7149 case elfcpp::R_ARM_THM_MOVW_BREL:
7156 // Stub-generation methods for Target_arm.
7158 // Make a new Arm_input_section object.
7160 template<bool big_endian>
7161 Arm_input_section<big_endian>*
7162 Target_arm<big_endian>::new_arm_input_section(
7166 Input_section_specifier iss(relobj, shndx);
7168 Arm_input_section<big_endian>* arm_input_section =
7169 new Arm_input_section<big_endian>(relobj, shndx);
7170 arm_input_section->init();
7172 // Register new Arm_input_section in map for look-up.
7173 std::pair<typename Arm_input_section_map::iterator, bool> ins =
7174 this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
7176 // Make sure that it we have not created another Arm_input_section
7177 // for this input section already.
7178 gold_assert(ins.second);
7180 return arm_input_section;
7183 // Find the Arm_input_section object corresponding to the SHNDX-th input
7184 // section of RELOBJ.
7186 template<bool big_endian>
7187 Arm_input_section<big_endian>*
7188 Target_arm<big_endian>::find_arm_input_section(
7190 unsigned int shndx) const
7192 Input_section_specifier iss(relobj, shndx);
7193 typename Arm_input_section_map::const_iterator p =
7194 this->arm_input_section_map_.find(iss);
7195 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
7198 // Make a new stub table.
7200 template<bool big_endian>
7201 Stub_table<big_endian>*
7202 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
7204 Stub_table<big_endian>* stub_table =
7205 new Stub_table<big_endian>(owner);
7206 this->stub_tables_.push_back(stub_table);
7208 stub_table->set_address(owner->address() + owner->data_size());
7209 stub_table->set_file_offset(owner->offset() + owner->data_size());
7210 stub_table->finalize_data_size();
7215 // Scan a relocation for stub generation.
7217 template<bool big_endian>
7219 Target_arm<big_endian>::scan_reloc_for_stub(
7220 const Relocate_info<32, big_endian>* relinfo,
7221 unsigned int r_type,
7222 const Sized_symbol<32>* gsym,
7224 const Symbol_value<32>* psymval,
7225 elfcpp::Elf_types<32>::Elf_Swxword addend,
7226 Arm_address address)
7228 typedef typename Target_arm<big_endian>::Relocate Relocate;
7230 const Arm_relobj<big_endian>* arm_relobj =
7231 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
7233 bool target_is_thumb;
7234 Symbol_value<32> symval;
7237 // This is a global symbol. Determine if we use PLT and if the
7238 // final target is THUMB.
7239 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
7241 // This uses a PLT, change the symbol value.
7242 symval.set_output_value(this->plt_section()->address()
7243 + gsym->plt_offset());
7245 target_is_thumb = false;
7247 else if (gsym->is_undefined())
7248 // There is no need to generate a stub symbol is undefined.
7253 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
7254 || (gsym->type() == elfcpp::STT_FUNC
7255 && !gsym->is_undefined()
7256 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
7261 // This is a local symbol. Determine if the final target is THUMB.
7262 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
7265 // Strip LSB if this points to a THUMB target.
7267 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
7268 && ((psymval->value(arm_relobj, 0) & 1) != 0))
7270 Arm_address stripped_value =
7271 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
7272 symval.set_output_value(stripped_value);
7276 // Get the symbol value.
7277 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
7279 // Owing to pipelining, the PC relative branches below actually skip
7280 // two instructions when the branch offset is 0.
7281 Arm_address destination;
7284 case elfcpp::R_ARM_CALL:
7285 case elfcpp::R_ARM_JUMP24:
7286 case elfcpp::R_ARM_PLT32:
7288 destination = value + addend + 8;
7290 case elfcpp::R_ARM_THM_CALL:
7291 case elfcpp::R_ARM_THM_XPC22:
7292 case elfcpp::R_ARM_THM_JUMP24:
7293 case elfcpp::R_ARM_THM_JUMP19:
7295 destination = value + addend + 4;
7301 Reloc_stub* stub = NULL;
7302 Stub_type stub_type =
7303 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
7305 if (stub_type != arm_stub_none)
7307 // Try looking up an existing stub from a stub table.
7308 Stub_table<big_endian>* stub_table =
7309 arm_relobj->stub_table(relinfo->data_shndx);
7310 gold_assert(stub_table != NULL);
7312 // Locate stub by destination.
7313 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
7315 // Create a stub if there is not one already
7316 stub = stub_table->find_reloc_stub(stub_key);
7319 // create a new stub and add it to stub table.
7320 stub = this->stub_factory().make_reloc_stub(stub_type);
7321 stub_table->add_reloc_stub(stub, stub_key);
7324 // Record the destination address.
7325 stub->set_destination_address(destination
7326 | (target_is_thumb ? 1 : 0));
7329 // For Cortex-A8, we need to record a relocation at 4K page boundary.
7330 if (this->fix_cortex_a8_
7331 && (r_type == elfcpp::R_ARM_THM_JUMP24
7332 || r_type == elfcpp::R_ARM_THM_JUMP19
7333 || r_type == elfcpp::R_ARM_THM_CALL
7334 || r_type == elfcpp::R_ARM_THM_XPC22)
7335 && (address & 0xfffU) == 0xffeU)
7337 // Found a candidate. Note we haven't checked the destination is
7338 // within 4K here: if we do so (and don't create a record) we can't
7339 // tell that a branch should have been relocated when scanning later.
7340 this->cortex_a8_relocs_info_[address] =
7341 new Cortex_a8_reloc(stub, r_type,
7342 destination | (target_is_thumb ? 1 : 0));
7346 // This function scans a relocation sections for stub generation.
7347 // The template parameter Relocate must be a class type which provides
7348 // a single function, relocate(), which implements the machine
7349 // specific part of a relocation.
7351 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
7352 // SHT_REL or SHT_RELA.
7354 // PRELOCS points to the relocation data. RELOC_COUNT is the number
7355 // of relocs. OUTPUT_SECTION is the output section.
7356 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
7357 // mapped to output offsets.
7359 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
7360 // VIEW_SIZE is the size. These refer to the input section, unless
7361 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
7362 // the output section.
7364 template<bool big_endian>
7365 template<int sh_type>
7367 Target_arm<big_endian>::scan_reloc_section_for_stubs(
7368 const Relocate_info<32, big_endian>* relinfo,
7369 const unsigned char* prelocs,
7371 Output_section* output_section,
7372 bool needs_special_offset_handling,
7373 const unsigned char* view,
7374 elfcpp::Elf_types<32>::Elf_Addr view_address,
7377 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
7378 const int reloc_size =
7379 Reloc_types<sh_type, 32, big_endian>::reloc_size;
7381 Arm_relobj<big_endian>* arm_object =
7382 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
7383 unsigned int local_count = arm_object->local_symbol_count();
7385 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
7387 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7389 Reltype reloc(prelocs);
7391 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
7392 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
7393 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
7395 r_type = this->get_real_reloc_type(r_type);
7397 // Only a few relocation types need stubs.
7398 if ((r_type != elfcpp::R_ARM_CALL)
7399 && (r_type != elfcpp::R_ARM_JUMP24)
7400 && (r_type != elfcpp::R_ARM_PLT32)
7401 && (r_type != elfcpp::R_ARM_THM_CALL)
7402 && (r_type != elfcpp::R_ARM_THM_XPC22)
7403 && (r_type != elfcpp::R_ARM_THM_JUMP24)
7404 && (r_type != elfcpp::R_ARM_THM_JUMP19))
7407 section_offset_type offset =
7408 convert_to_section_size_type(reloc.get_r_offset());
7410 if (needs_special_offset_handling)
7412 offset = output_section->output_offset(relinfo->object,
7413 relinfo->data_shndx,
7420 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
7421 elfcpp::Elf_types<32>::Elf_Swxword addend =
7422 stub_addend_reader(r_type, view + offset, reloc);
7424 const Sized_symbol<32>* sym;
7426 Symbol_value<32> symval;
7427 const Symbol_value<32> *psymval;
7428 if (r_sym < local_count)
7431 psymval = arm_object->local_symbol(r_sym);
7433 // If the local symbol belongs to a section we are discarding,
7434 // and that section is a debug section, try to find the
7435 // corresponding kept section and map this symbol to its
7436 // counterpart in the kept section. The symbol must not
7437 // correspond to a section we are folding.
7439 unsigned int shndx = psymval->input_shndx(&is_ordinary);
7441 && shndx != elfcpp::SHN_UNDEF
7442 && !arm_object->is_section_included(shndx)
7443 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
7445 if (comdat_behavior == CB_UNDETERMINED)
7448 arm_object->section_name(relinfo->data_shndx);
7449 comdat_behavior = get_comdat_behavior(name.c_str());
7451 if (comdat_behavior == CB_PRETEND)
7454 typename elfcpp::Elf_types<32>::Elf_Addr value =
7455 arm_object->map_to_kept_section(shndx, &found);
7457 symval.set_output_value(value + psymval->input_value());
7459 symval.set_output_value(0);
7463 symval.set_output_value(0);
7465 symval.set_no_output_symtab_entry();
7471 const Symbol* gsym = arm_object->global_symbol(r_sym);
7472 gold_assert(gsym != NULL);
7473 if (gsym->is_forwarder())
7474 gsym = relinfo->symtab->resolve_forwards(gsym);
7476 sym = static_cast<const Sized_symbol<32>*>(gsym);
7477 if (sym->has_symtab_index())
7478 symval.set_output_symtab_index(sym->symtab_index());
7480 symval.set_no_output_symtab_entry();
7482 // We need to compute the would-be final value of this global
7484 const Symbol_table* symtab = relinfo->symtab;
7485 const Sized_symbol<32>* sized_symbol =
7486 symtab->get_sized_symbol<32>(gsym);
7487 Symbol_table::Compute_final_value_status status;
7489 symtab->compute_final_value<32>(sized_symbol, &status);
7491 // Skip this if the symbol has not output section.
7492 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
7495 symval.set_output_value(value);
7499 // If symbol is a section symbol, we don't know the actual type of
7500 // destination. Give up.
7501 if (psymval->is_section_symbol())
7504 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
7505 addend, view_address + offset);
7509 // Scan an input section for stub generation.
7511 template<bool big_endian>
7513 Target_arm<big_endian>::scan_section_for_stubs(
7514 const Relocate_info<32, big_endian>* relinfo,
7515 unsigned int sh_type,
7516 const unsigned char* prelocs,
7518 Output_section* output_section,
7519 bool needs_special_offset_handling,
7520 const unsigned char* view,
7521 Arm_address view_address,
7522 section_size_type view_size)
7524 if (sh_type == elfcpp::SHT_REL)
7525 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
7530 needs_special_offset_handling,
7534 else if (sh_type == elfcpp::SHT_RELA)
7535 // We do not support RELA type relocations yet. This is provided for
7537 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
7542 needs_special_offset_handling,
7550 // Group input sections for stub generation.
7552 // We goup input sections in an output sections so that the total size,
7553 // including any padding space due to alignment is smaller than GROUP_SIZE
7554 // unless the only input section in group is bigger than GROUP_SIZE already.
7555 // Then an ARM stub table is created to follow the last input section
7556 // in group. For each group an ARM stub table is created an is placed
7557 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
7558 // extend the group after the stub table.
7560 template<bool big_endian>
7562 Target_arm<big_endian>::group_sections(
7564 section_size_type group_size,
7565 bool stubs_always_after_branch)
7567 // Group input sections and insert stub table
7568 Layout::Section_list section_list;
7569 layout->get_allocated_sections(§ion_list);
7570 for (Layout::Section_list::const_iterator p = section_list.begin();
7571 p != section_list.end();
7574 Arm_output_section<big_endian>* output_section =
7575 Arm_output_section<big_endian>::as_arm_output_section(*p);
7576 output_section->group_sections(group_size, stubs_always_after_branch,
7581 // Relaxation hook. This is where we do stub generation.
7583 template<bool big_endian>
7585 Target_arm<big_endian>::do_relax(
7587 const Input_objects* input_objects,
7588 Symbol_table* symtab,
7591 // No need to generate stubs if this is a relocatable link.
7592 gold_assert(!parameters->options().relocatable());
7594 // If this is the first pass, we need to group input sections into
7598 // Determine the stub group size. The group size is the absolute
7599 // value of the parameter --stub-group-size. If --stub-group-size
7600 // is passed a negative value, we restict stubs to be always after
7601 // the stubbed branches.
7602 int32_t stub_group_size_param =
7603 parameters->options().stub_group_size();
7604 bool stubs_always_after_branch = stub_group_size_param < 0;
7605 section_size_type stub_group_size = abs(stub_group_size_param);
7607 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
7608 // page as the first half of a 32-bit branch straddling two 4K pages.
7609 // This is a crude way of enforcing that.
7610 if (this->fix_cortex_a8_)
7611 stubs_always_after_branch = true;
7613 if (stub_group_size == 1)
7616 // Thumb branch range is +-4MB has to be used as the default
7617 // maximum size (a given section can contain both ARM and Thumb
7618 // code, so the worst case has to be taken into account).
7620 // This value is 24K less than that, which allows for 2025
7621 // 12-byte stubs. If we exceed that, then we will fail to link.
7622 // The user will have to relink with an explicit group size
7624 stub_group_size = 4170000;
7627 group_sections(layout, stub_group_size, stubs_always_after_branch);
7630 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
7631 // beginning of each relaxation pass, just blow away all the stubs.
7632 // Alternatively, we could selectively remove only the stubs and reloc
7633 // information for code sections that have moved since the last pass.
7634 // That would require more book-keeping.
7635 typedef typename Stub_table_list::iterator Stub_table_iterator;
7636 if (this->fix_cortex_a8_)
7638 // Clear all Cortex-A8 reloc information.
7639 for (typename Cortex_a8_relocs_info::const_iterator p =
7640 this->cortex_a8_relocs_info_.begin();
7641 p != this->cortex_a8_relocs_info_.end();
7644 this->cortex_a8_relocs_info_.clear();
7646 // Remove all Cortex-A8 stubs.
7647 for (Stub_table_iterator sp = this->stub_tables_.begin();
7648 sp != this->stub_tables_.end();
7650 (*sp)->remove_all_cortex_a8_stubs();
7653 // Scan relocs for relocation stubs
7654 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
7655 op != input_objects->relobj_end();
7658 Arm_relobj<big_endian>* arm_relobj =
7659 Arm_relobj<big_endian>::as_arm_relobj(*op);
7660 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
7663 // Check all stub tables to see if any of them have their data sizes
7664 // or addresses alignments changed. These are the only things that
7666 bool any_stub_table_changed = false;
7667 for (Stub_table_iterator sp = this->stub_tables_.begin();
7668 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7671 if ((*sp)->update_data_size_and_addralign())
7672 any_stub_table_changed = true;
7675 // Finalize the stubs in the last relaxation pass.
7676 if (!any_stub_table_changed)
7677 for (Stub_table_iterator sp = this->stub_tables_.begin();
7678 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7680 (*sp)->finalize_stubs();
7682 return any_stub_table_changed;
7687 template<bool big_endian>
7689 Target_arm<big_endian>::relocate_stub(
7691 const Relocate_info<32, big_endian>* relinfo,
7692 Output_section* output_section,
7693 unsigned char* view,
7694 Arm_address address,
7695 section_size_type view_size)
7698 const Stub_template* stub_template = stub->stub_template();
7699 for (size_t i = 0; i < stub_template->reloc_count(); i++)
7701 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
7702 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
7704 unsigned int r_type = insn->r_type();
7705 section_size_type reloc_offset = stub_template->reloc_offset(i);
7706 section_size_type reloc_size = insn->size();
7707 gold_assert(reloc_offset + reloc_size <= view_size);
7709 // This is the address of the stub destination.
7710 Arm_address target = stub->reloc_target(i);
7711 Symbol_value<32> symval;
7712 symval.set_output_value(target);
7714 // Synthesize a fake reloc just in case. We don't have a symbol so
7716 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
7717 memset(reloc_buffer, 0, sizeof(reloc_buffer));
7718 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
7719 reloc_write.put_r_offset(reloc_offset);
7720 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
7721 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
7723 relocate.relocate(relinfo, this, output_section,
7724 this->fake_relnum_for_stubs, rel, r_type,
7725 NULL, &symval, view + reloc_offset,
7726 address + reloc_offset, reloc_size);
7730 // Determine whether an object attribute tag takes an integer, a
7733 template<bool big_endian>
7735 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
7737 if (tag == Object_attribute::Tag_compatibility)
7738 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7739 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
7740 else if (tag == elfcpp::Tag_nodefaults)
7741 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7742 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
7743 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
7744 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
7746 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
7748 return ((tag & 1) != 0
7749 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7750 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
7753 // Reorder attributes.
7755 // The ABI defines that Tag_conformance should be emitted first, and that
7756 // Tag_nodefaults should be second (if either is defined). This sets those
7757 // two positions, and bumps up the position of all the remaining tags to
7760 template<bool big_endian>
7762 Target_arm<big_endian>::do_attributes_order(int num) const
7764 // Reorder the known object attributes in output. We want to move
7765 // Tag_conformance to position 4 and Tag_conformance to position 5
7766 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7768 return elfcpp::Tag_conformance;
7770 return elfcpp::Tag_nodefaults;
7771 if ((num - 2) < elfcpp::Tag_nodefaults)
7773 if ((num - 1) < elfcpp::Tag_conformance)
7778 // Scan a span of THUMB code for Cortex-A8 erratum.
7780 template<bool big_endian>
7782 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
7783 Arm_relobj<big_endian>* arm_relobj,
7785 section_size_type span_start,
7786 section_size_type span_end,
7787 const unsigned char* view,
7788 Arm_address address)
7790 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
7792 // The opcode is BLX.W, BL.W, B.W, Bcc.W
7793 // The branch target is in the same 4KB region as the
7794 // first half of the branch.
7795 // The instruction before the branch is a 32-bit
7796 // length non-branch instruction.
7797 section_size_type i = span_start;
7798 bool last_was_32bit = false;
7799 bool last_was_branch = false;
7800 while (i < span_end)
7802 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
7803 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
7804 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
7805 bool is_blx = false, is_b = false;
7806 bool is_bl = false, is_bcc = false;
7808 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
7811 // Load the rest of the insn (in manual-friendly order).
7812 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
7814 // Encoding T4: B<c>.W.
7815 is_b = (insn & 0xf800d000U) == 0xf0009000U;
7816 // Encoding T1: BL<c>.W.
7817 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
7818 // Encoding T2: BLX<c>.W.
7819 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
7820 // Encoding T3: B<c>.W (not permitted in IT block).
7821 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
7822 && (insn & 0x07f00000U) != 0x03800000U);
7825 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
7827 // If this instruction is a 32-bit THUMB branch that crosses a 4K
7828 // page boundary and it follows 32-bit non-branch instruction,
7829 // we need to work around.
7831 && ((address + i) & 0xfffU) == 0xffeU
7833 && !last_was_branch)
7835 // Check to see if there is a relocation stub for this branch.
7836 bool force_target_arm = false;
7837 bool force_target_thumb = false;
7838 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
7839 Cortex_a8_relocs_info::const_iterator p =
7840 this->cortex_a8_relocs_info_.find(address + i);
7842 if (p != this->cortex_a8_relocs_info_.end())
7844 cortex_a8_reloc = p->second;
7845 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
7847 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
7848 && !target_is_thumb)
7849 force_target_arm = true;
7850 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
7852 force_target_thumb = true;
7856 Stub_type stub_type = arm_stub_none;
7858 // Check if we have an offending branch instruction.
7859 uint16_t upper_insn = (insn >> 16) & 0xffffU;
7860 uint16_t lower_insn = insn & 0xffffU;
7861 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
7863 if (cortex_a8_reloc != NULL
7864 && cortex_a8_reloc->reloc_stub() != NULL)
7865 // We've already made a stub for this instruction, e.g.
7866 // it's a long branch or a Thumb->ARM stub. Assume that
7867 // stub will suffice to work around the A8 erratum (see
7868 // setting of always_after_branch above).
7872 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
7874 stub_type = arm_stub_a8_veneer_b_cond;
7876 else if (is_b || is_bl || is_blx)
7878 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
7884 ? arm_stub_a8_veneer_blx
7886 ? arm_stub_a8_veneer_bl
7887 : arm_stub_a8_veneer_b));
7890 if (stub_type != arm_stub_none)
7892 Arm_address pc_for_insn = address + i + 4;
7894 // The original instruction is a BL, but the target is
7895 // an ARM instruction. If we were not making a stub,
7896 // the BL would have been converted to a BLX. Use the
7897 // BLX stub instead in that case.
7898 if (this->may_use_blx() && force_target_arm
7899 && stub_type == arm_stub_a8_veneer_bl)
7901 stub_type = arm_stub_a8_veneer_blx;
7905 // Conversely, if the original instruction was
7906 // BLX but the target is Thumb mode, use the BL stub.
7907 else if (force_target_thumb
7908 && stub_type == arm_stub_a8_veneer_blx)
7910 stub_type = arm_stub_a8_veneer_bl;
7918 // If we found a relocation, use the proper destination,
7919 // not the offset in the (unrelocated) instruction.
7920 // Note this is always done if we switched the stub type above.
7921 if (cortex_a8_reloc != NULL)
7922 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
7924 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
7926 // Add a new stub if destination address in in the same page.
7927 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
7929 Cortex_a8_stub* stub =
7930 this->stub_factory_.make_cortex_a8_stub(stub_type,
7934 Stub_table<big_endian>* stub_table =
7935 arm_relobj->stub_table(shndx);
7936 gold_assert(stub_table != NULL);
7937 stub_table->add_cortex_a8_stub(address + i, stub);
7942 i += insn_32bit ? 4 : 2;
7943 last_was_32bit = insn_32bit;
7944 last_was_branch = is_32bit_branch;
7948 template<bool big_endian>
7949 class Target_selector_arm : public Target_selector
7952 Target_selector_arm()
7953 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
7954 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
7958 do_instantiate_target()
7959 { return new Target_arm<big_endian>(); }
7962 Target_selector_arm<false> target_selector_arm;
7963 Target_selector_arm<true> target_selector_armbe;
7965 } // End anonymous namespace.