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.
37 #include "parameters.h"
44 #include "copy-relocs.h"
46 #include "target-reloc.h"
47 #include "target-select.h"
51 #include "attributes.h"
58 template<bool big_endian>
59 class Output_data_plt_arm;
61 template<bool big_endian>
64 template<bool big_endian>
65 class Arm_input_section;
67 class Arm_exidx_cantunwind;
69 class Arm_exidx_merged_section;
71 class Arm_exidx_fixup;
73 template<bool big_endian>
74 class Arm_output_section;
76 class Arm_exidx_input_section;
78 template<bool big_endian>
81 template<bool big_endian>
85 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
87 // Maximum branch offsets for ARM, THUMB and THUMB2.
88 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
89 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
90 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
91 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
92 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
93 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
95 // The arm target class.
97 // This is a very simple port of gold for ARM-EABI. It is intended for
98 // supporting Android only for the time being. Only these relocation types
127 // R_ARM_THM_MOVW_ABS_NC
128 // R_ARM_THM_MOVT_ABS
129 // R_ARM_MOVW_PREL_NC
131 // R_ARM_THM_MOVW_PREL_NC
132 // R_ARM_THM_MOVT_PREL
139 // - Support more relocation types as needed.
140 // - Make PLTs more flexible for different architecture features like
142 // There are probably a lot more.
144 // Instruction template class. This class is similar to the insn_sequence
145 // struct in bfd/elf32-arm.c.
150 // Types of instruction templates.
154 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
155 // templates with class-specific semantics. Currently this is used
156 // only by the Cortex_a8_stub class for handling condition codes in
157 // conditional branches.
158 THUMB16_SPECIAL_TYPE,
164 // Factory methods to create instruction templates in different formats.
166 static const Insn_template
167 thumb16_insn(uint32_t data)
168 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
170 // A Thumb conditional branch, in which the proper condition is inserted
171 // when we build the stub.
172 static const Insn_template
173 thumb16_bcond_insn(uint32_t data)
174 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
176 static const Insn_template
177 thumb32_insn(uint32_t data)
178 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
180 static const Insn_template
181 thumb32_b_insn(uint32_t data, int reloc_addend)
183 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
187 static const Insn_template
188 arm_insn(uint32_t data)
189 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
191 static const Insn_template
192 arm_rel_insn(unsigned data, int reloc_addend)
193 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
195 static const Insn_template
196 data_word(unsigned data, unsigned int r_type, int reloc_addend)
197 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
199 // Accessors. This class is used for read-only objects so no modifiers
204 { return this->data_; }
206 // Return the instruction sequence type of this.
209 { return this->type_; }
211 // Return the ARM relocation type of this.
214 { return this->r_type_; }
218 { return this->reloc_addend_; }
220 // Return size of instruction template in bytes.
224 // Return byte-alignment of instruction template.
229 // We make the constructor private to ensure that only the factory
232 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
233 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
236 // Instruction specific data. This is used to store information like
237 // some of the instruction bits.
239 // Instruction template type.
241 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
242 unsigned int r_type_;
243 // Relocation addend.
244 int32_t reloc_addend_;
247 // Macro for generating code to stub types. One entry per long/short
251 DEF_STUB(long_branch_any_any) \
252 DEF_STUB(long_branch_v4t_arm_thumb) \
253 DEF_STUB(long_branch_thumb_only) \
254 DEF_STUB(long_branch_v4t_thumb_thumb) \
255 DEF_STUB(long_branch_v4t_thumb_arm) \
256 DEF_STUB(short_branch_v4t_thumb_arm) \
257 DEF_STUB(long_branch_any_arm_pic) \
258 DEF_STUB(long_branch_any_thumb_pic) \
259 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
260 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
261 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
262 DEF_STUB(long_branch_thumb_only_pic) \
263 DEF_STUB(a8_veneer_b_cond) \
264 DEF_STUB(a8_veneer_b) \
265 DEF_STUB(a8_veneer_bl) \
266 DEF_STUB(a8_veneer_blx) \
267 DEF_STUB(v4_veneer_bx)
271 #define DEF_STUB(x) arm_stub_##x,
277 // First reloc stub type.
278 arm_stub_reloc_first = arm_stub_long_branch_any_any,
279 // Last reloc stub type.
280 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
282 // First Cortex-A8 stub type.
283 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
284 // Last Cortex-A8 stub type.
285 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
288 arm_stub_type_last = arm_stub_v4_veneer_bx
292 // Stub template class. Templates are meant to be read-only objects.
293 // A stub template for a stub type contains all read-only attributes
294 // common to all stubs of the same type.
299 Stub_template(Stub_type, const Insn_template*, size_t);
307 { return this->type_; }
309 // Return an array of instruction templates.
312 { return this->insns_; }
314 // Return size of template in number of instructions.
317 { return this->insn_count_; }
319 // Return size of template in bytes.
322 { return this->size_; }
324 // Return alignment of the stub template.
327 { return this->alignment_; }
329 // Return whether entry point is in thumb mode.
331 entry_in_thumb_mode() const
332 { return this->entry_in_thumb_mode_; }
334 // Return number of relocations in this template.
337 { return this->relocs_.size(); }
339 // Return index of the I-th instruction with relocation.
341 reloc_insn_index(size_t i) const
343 gold_assert(i < this->relocs_.size());
344 return this->relocs_[i].first;
347 // Return the offset of the I-th instruction with relocation from the
348 // beginning of the stub.
350 reloc_offset(size_t i) const
352 gold_assert(i < this->relocs_.size());
353 return this->relocs_[i].second;
357 // This contains information about an instruction template with a relocation
358 // and its offset from start of stub.
359 typedef std::pair<size_t, section_size_type> Reloc;
361 // A Stub_template may not be copied. We want to share templates as much
363 Stub_template(const Stub_template&);
364 Stub_template& operator=(const Stub_template&);
368 // Points to an array of Insn_templates.
369 const Insn_template* insns_;
370 // Number of Insn_templates in insns_[].
372 // Size of templated instructions in bytes.
374 // Alignment of templated instructions.
376 // Flag to indicate if entry is in thumb mode.
377 bool entry_in_thumb_mode_;
378 // A table of reloc instruction indices and offsets. We can find these by
379 // looking at the instruction templates but we pre-compute and then stash
380 // them here for speed.
381 std::vector<Reloc> relocs_;
385 // A class for code stubs. This is a base class for different type of
386 // stubs used in the ARM target.
392 static const section_offset_type invalid_offset =
393 static_cast<section_offset_type>(-1);
396 Stub(const Stub_template* stub_template)
397 : stub_template_(stub_template), offset_(invalid_offset)
404 // Return the stub template.
406 stub_template() const
407 { return this->stub_template_; }
409 // Return offset of code stub from beginning of its containing stub table.
413 gold_assert(this->offset_ != invalid_offset);
414 return this->offset_;
417 // Set offset of code stub from beginning of its containing stub table.
419 set_offset(section_offset_type offset)
420 { this->offset_ = offset; }
422 // Return the relocation target address of the i-th relocation in the
423 // stub. This must be defined in a child class.
425 reloc_target(size_t i)
426 { return this->do_reloc_target(i); }
428 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
430 write(unsigned char* view, section_size_type view_size, bool big_endian)
431 { this->do_write(view, view_size, big_endian); }
433 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
434 // for the i-th instruction.
436 thumb16_special(size_t i)
437 { return this->do_thumb16_special(i); }
440 // This must be defined in the child class.
442 do_reloc_target(size_t) = 0;
444 // This may be overridden in the child class.
446 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
449 this->do_fixed_endian_write<true>(view, view_size);
451 this->do_fixed_endian_write<false>(view, view_size);
454 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
455 // instruction template.
457 do_thumb16_special(size_t)
458 { gold_unreachable(); }
461 // A template to implement do_write.
462 template<bool big_endian>
464 do_fixed_endian_write(unsigned char*, section_size_type);
467 const Stub_template* stub_template_;
468 // Offset within the section of containing this stub.
469 section_offset_type offset_;
472 // Reloc stub class. These are stubs we use to fix up relocation because
473 // of limited branch ranges.
475 class Reloc_stub : public Stub
478 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
479 // We assume we never jump to this address.
480 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
482 // Return destination address.
484 destination_address() const
486 gold_assert(this->destination_address_ != this->invalid_address);
487 return this->destination_address_;
490 // Set destination address.
492 set_destination_address(Arm_address address)
494 gold_assert(address != this->invalid_address);
495 this->destination_address_ = address;
498 // Reset destination address.
500 reset_destination_address()
501 { this->destination_address_ = this->invalid_address; }
503 // Determine stub type for a branch of a relocation of R_TYPE going
504 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
505 // the branch target is a thumb instruction. TARGET is used for look
506 // up ARM-specific linker settings.
508 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
509 Arm_address branch_target, bool target_is_thumb);
511 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
512 // and an addend. Since we treat global and local symbol differently, we
513 // use a Symbol object for a global symbol and a object-index pair for
518 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
519 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
520 // and R_SYM must not be invalid_index.
521 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
522 unsigned int r_sym, int32_t addend)
523 : stub_type_(stub_type), addend_(addend)
527 this->r_sym_ = Reloc_stub::invalid_index;
528 this->u_.symbol = symbol;
532 gold_assert(relobj != NULL && r_sym != invalid_index);
533 this->r_sym_ = r_sym;
534 this->u_.relobj = relobj;
541 // Accessors: Keys are meant to be read-only object so no modifiers are
547 { return this->stub_type_; }
549 // Return the local symbol index or invalid_index.
552 { return this->r_sym_; }
554 // Return the symbol if there is one.
557 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
559 // Return the relobj if there is one.
562 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
564 // Whether this equals to another key k.
566 eq(const Key& k) const
568 return ((this->stub_type_ == k.stub_type_)
569 && (this->r_sym_ == k.r_sym_)
570 && ((this->r_sym_ != Reloc_stub::invalid_index)
571 ? (this->u_.relobj == k.u_.relobj)
572 : (this->u_.symbol == k.u_.symbol))
573 && (this->addend_ == k.addend_));
576 // Return a hash value.
580 return (this->stub_type_
582 ^ gold::string_hash<char>(
583 (this->r_sym_ != Reloc_stub::invalid_index)
584 ? this->u_.relobj->name().c_str()
585 : this->u_.symbol->name())
589 // Functors for STL associative containers.
593 operator()(const Key& k) const
594 { return k.hash_value(); }
600 operator()(const Key& k1, const Key& k2) const
601 { return k1.eq(k2); }
604 // Name of key. This is mainly for debugging.
610 Stub_type stub_type_;
611 // If this is a local symbol, this is the index in the defining object.
612 // Otherwise, it is invalid_index for a global symbol.
614 // If r_sym_ is invalid index. This points to a global symbol.
615 // Otherwise, this points a relobj. We used the unsized and target
616 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
617 // Arm_relobj. This is done to avoid making the stub class a template
618 // as most of the stub machinery is endianity-neutral. However, it
619 // may require a bit of casting done by users of this class.
622 const Symbol* symbol;
623 const Relobj* relobj;
625 // Addend associated with a reloc.
630 // Reloc_stubs are created via a stub factory. So these are protected.
631 Reloc_stub(const Stub_template* stub_template)
632 : Stub(stub_template), destination_address_(invalid_address)
638 friend class Stub_factory;
640 // Return the relocation target address of the i-th relocation in the
643 do_reloc_target(size_t i)
645 // All reloc stub have only one relocation.
647 return this->destination_address_;
651 // Address of destination.
652 Arm_address destination_address_;
655 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
656 // THUMB branch that meets the following conditions:
658 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
659 // branch address is 0xffe.
660 // 2. The branch target address is in the same page as the first word of the
662 // 3. The branch follows a 32-bit instruction which is not a branch.
664 // To do the fix up, we need to store the address of the branch instruction
665 // and its target at least. We also need to store the original branch
666 // instruction bits for the condition code in a conditional branch. The
667 // condition code is used in a special instruction template. We also want
668 // to identify input sections needing Cortex-A8 workaround quickly. We store
669 // extra information about object and section index of the code section
670 // containing a branch being fixed up. The information is used to mark
671 // the code section when we finalize the Cortex-A8 stubs.
674 class Cortex_a8_stub : public Stub
680 // Return the object of the code section containing the branch being fixed
684 { return this->relobj_; }
686 // Return the section index of the code section containing the branch being
690 { return this->shndx_; }
692 // Return the source address of stub. This is the address of the original
693 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
696 source_address() const
697 { return this->source_address_; }
699 // Return the destination address of the stub. This is the branch taken
700 // address of the original branch instruction. LSB is 1 if it is a THUMB
701 // instruction address.
703 destination_address() const
704 { return this->destination_address_; }
706 // Return the instruction being fixed up.
708 original_insn() const
709 { return this->original_insn_; }
712 // Cortex_a8_stubs are created via a stub factory. So these are protected.
713 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
714 unsigned int shndx, Arm_address source_address,
715 Arm_address destination_address, uint32_t original_insn)
716 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
717 source_address_(source_address | 1U),
718 destination_address_(destination_address),
719 original_insn_(original_insn)
722 friend class Stub_factory;
724 // Return the relocation target address of the i-th relocation in the
727 do_reloc_target(size_t i)
729 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
731 // The conditional branch veneer has two relocations.
733 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
737 // All other Cortex-A8 stubs have only one relocation.
739 return this->destination_address_;
743 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
745 do_thumb16_special(size_t);
748 // Object of the code section containing the branch being fixed up.
750 // Section index of the code section containing the branch begin fixed up.
752 // Source address of original branch.
753 Arm_address source_address_;
754 // Destination address of the original branch.
755 Arm_address destination_address_;
756 // Original branch instruction. This is needed for copying the condition
757 // code from a condition branch to its stub.
758 uint32_t original_insn_;
761 // ARMv4 BX Rx branch relocation stub class.
762 class Arm_v4bx_stub : public Stub
768 // Return the associated register.
771 { return this->reg_; }
774 // Arm V4BX stubs are created via a stub factory. So these are protected.
775 Arm_v4bx_stub(const Stub_template* stub_template, const uint32_t reg)
776 : Stub(stub_template), reg_(reg)
779 friend class Stub_factory;
781 // Return the relocation target address of the i-th relocation in the
784 do_reloc_target(size_t)
785 { gold_unreachable(); }
787 // This may be overridden in the child class.
789 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
792 this->do_fixed_endian_v4bx_write<true>(view, view_size);
794 this->do_fixed_endian_v4bx_write<false>(view, view_size);
798 // A template to implement do_write.
799 template<bool big_endian>
801 do_fixed_endian_v4bx_write(unsigned char* view, section_size_type)
803 const Insn_template* insns = this->stub_template()->insns();
804 elfcpp::Swap<32, big_endian>::writeval(view,
806 + (this->reg_ << 16)));
807 view += insns[0].size();
808 elfcpp::Swap<32, big_endian>::writeval(view,
809 (insns[1].data() + this->reg_));
810 view += insns[1].size();
811 elfcpp::Swap<32, big_endian>::writeval(view,
812 (insns[2].data() + this->reg_));
815 // A register index (r0-r14), which is associated with the stub.
819 // Stub factory class.
824 // Return the unique instance of this class.
825 static const Stub_factory&
828 static Stub_factory singleton;
832 // Make a relocation stub.
834 make_reloc_stub(Stub_type stub_type) const
836 gold_assert(stub_type >= arm_stub_reloc_first
837 && stub_type <= arm_stub_reloc_last);
838 return new Reloc_stub(this->stub_templates_[stub_type]);
841 // Make a Cortex-A8 stub.
843 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
844 Arm_address source, Arm_address destination,
845 uint32_t original_insn) const
847 gold_assert(stub_type >= arm_stub_cortex_a8_first
848 && stub_type <= arm_stub_cortex_a8_last);
849 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
850 source, destination, original_insn);
853 // Make an ARM V4BX relocation stub.
854 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
856 make_arm_v4bx_stub(uint32_t reg) const
858 gold_assert(reg < 0xf);
859 return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
864 // Constructor and destructor are protected since we only return a single
865 // instance created in Stub_factory::get_instance().
869 // A Stub_factory may not be copied since it is a singleton.
870 Stub_factory(const Stub_factory&);
871 Stub_factory& operator=(Stub_factory&);
873 // Stub templates. These are initialized in the constructor.
874 const Stub_template* stub_templates_[arm_stub_type_last+1];
877 // A class to hold stubs for the ARM target.
879 template<bool big_endian>
880 class Stub_table : public Output_data
883 Stub_table(Arm_input_section<big_endian>* owner)
884 : Output_data(), owner_(owner), reloc_stubs_(), cortex_a8_stubs_(),
885 arm_v4bx_stubs_(0xf), prev_data_size_(0), prev_addralign_(1)
891 // Owner of this stub table.
892 Arm_input_section<big_endian>*
894 { return this->owner_; }
896 // Whether this stub table is empty.
900 return (this->reloc_stubs_.empty()
901 && this->cortex_a8_stubs_.empty()
902 && this->arm_v4bx_stubs_.empty());
905 // Return the current data size.
907 current_data_size() const
908 { return this->current_data_size_for_child(); }
910 // Add a STUB with using KEY. Caller is reponsible for avoid adding
911 // if already a STUB with the same key has been added.
913 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
915 const Stub_template* stub_template = stub->stub_template();
916 gold_assert(stub_template->type() == key.stub_type());
917 this->reloc_stubs_[key] = stub;
920 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
921 // Caller is reponsible for avoid adding if already a STUB with the same
922 // address has been added.
924 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
926 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
927 this->cortex_a8_stubs_.insert(value);
930 // Add an ARM V4BX relocation stub. A register index will be retrieved
933 add_arm_v4bx_stub(Arm_v4bx_stub* stub)
935 gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
936 this->arm_v4bx_stubs_[stub->reg()] = stub;
939 // Remove all Cortex-A8 stubs.
941 remove_all_cortex_a8_stubs();
943 // Look up a relocation stub using KEY. Return NULL if there is none.
945 find_reloc_stub(const Reloc_stub::Key& key) const
947 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
948 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
951 // Look up an arm v4bx relocation stub using the register index.
952 // Return NULL if there is none.
954 find_arm_v4bx_stub(const uint32_t reg) const
956 gold_assert(reg < 0xf);
957 return this->arm_v4bx_stubs_[reg];
960 // Relocate stubs in this stub table.
962 relocate_stubs(const Relocate_info<32, big_endian>*,
963 Target_arm<big_endian>*, Output_section*,
964 unsigned char*, Arm_address, section_size_type);
966 // Update data size and alignment at the end of a relaxation pass. Return
967 // true if either data size or alignment is different from that of the
968 // previous relaxation pass.
970 update_data_size_and_addralign();
972 // Finalize stubs. Set the offsets of all stubs and mark input sections
973 // needing the Cortex-A8 workaround.
977 // Apply Cortex-A8 workaround to an address range.
979 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
980 unsigned char*, Arm_address,
984 // Write out section contents.
986 do_write(Output_file*);
988 // Return the required alignment.
991 { return this->prev_addralign_; }
993 // Reset address and file offset.
995 do_reset_address_and_file_offset()
996 { this->set_current_data_size_for_child(this->prev_data_size_); }
998 // Set final data size.
1000 set_final_data_size()
1001 { this->set_data_size(this->current_data_size()); }
1004 // Relocate one stub.
1006 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1007 Target_arm<big_endian>*, Output_section*,
1008 unsigned char*, Arm_address, section_size_type);
1010 // Unordered map of relocation stubs.
1012 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
1013 Reloc_stub::Key::equal_to>
1016 // List of Cortex-A8 stubs ordered by addresses of branches being
1017 // fixed up in output.
1018 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
1019 // List of Arm V4BX relocation stubs ordered by associated registers.
1020 typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
1022 // Owner of this stub table.
1023 Arm_input_section<big_endian>* owner_;
1024 // The relocation stubs.
1025 Reloc_stub_map reloc_stubs_;
1026 // The cortex_a8_stubs.
1027 Cortex_a8_stub_list cortex_a8_stubs_;
1028 // The Arm V4BX relocation stubs.
1029 Arm_v4bx_stub_list arm_v4bx_stubs_;
1030 // data size of this in the previous pass.
1031 off_t prev_data_size_;
1032 // address alignment of this in the previous pass.
1033 uint64_t prev_addralign_;
1036 // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1037 // we add to the end of an EXIDX input section that goes into the output.
1039 class Arm_exidx_cantunwind : public Output_section_data
1042 Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
1043 : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
1046 // Return the object containing the section pointed by this.
1049 { return this->relobj_; }
1051 // Return the section index of the section pointed by this.
1054 { return this->shndx_; }
1058 do_write(Output_file* of)
1060 if (parameters->target().is_big_endian())
1061 this->do_fixed_endian_write<true>(of);
1063 this->do_fixed_endian_write<false>(of);
1067 // Implement do_write for a given endianity.
1068 template<bool big_endian>
1070 do_fixed_endian_write(Output_file*);
1072 // The object containing the section pointed by this.
1074 // The section index of the section pointed by this.
1075 unsigned int shndx_;
1078 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1079 // Offset map is used to map input section offset within the EXIDX section
1080 // to the output offset from the start of this EXIDX section.
1082 typedef std::map<section_offset_type, section_offset_type>
1083 Arm_exidx_section_offset_map;
1085 // Arm_exidx_merged_section class. This represents an EXIDX input section
1086 // with some of its entries merged.
1088 class Arm_exidx_merged_section : public Output_relaxed_input_section
1091 // Constructor for Arm_exidx_merged_section.
1092 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1093 // SECTION_OFFSET_MAP points to a section offset map describing how
1094 // parts of the input section are mapped to output. DELETED_BYTES is
1095 // the number of bytes deleted from the EXIDX input section.
1096 Arm_exidx_merged_section(
1097 const Arm_exidx_input_section& exidx_input_section,
1098 const Arm_exidx_section_offset_map& section_offset_map,
1099 uint32_t deleted_bytes);
1101 // Return the original EXIDX input section.
1102 const Arm_exidx_input_section&
1103 exidx_input_section() const
1104 { return this->exidx_input_section_; }
1106 // Return the section offset map.
1107 const Arm_exidx_section_offset_map&
1108 section_offset_map() const
1109 { return this->section_offset_map_; }
1112 // Write merged section into file OF.
1114 do_write(Output_file* of);
1117 do_output_offset(const Relobj*, unsigned int, section_offset_type,
1118 section_offset_type*) const;
1121 // Original EXIDX input section.
1122 const Arm_exidx_input_section& exidx_input_section_;
1123 // Section offset map.
1124 const Arm_exidx_section_offset_map& section_offset_map_;
1127 // A class to wrap an ordinary input section containing executable code.
1129 template<bool big_endian>
1130 class Arm_input_section : public Output_relaxed_input_section
1133 Arm_input_section(Relobj* relobj, unsigned int shndx)
1134 : Output_relaxed_input_section(relobj, shndx, 1),
1135 original_addralign_(1), original_size_(0), stub_table_(NULL)
1138 ~Arm_input_section()
1145 // Whether this is a stub table owner.
1147 is_stub_table_owner() const
1148 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1150 // Return the stub table.
1151 Stub_table<big_endian>*
1153 { return this->stub_table_; }
1155 // Set the stub_table.
1157 set_stub_table(Stub_table<big_endian>* stub_table)
1158 { this->stub_table_ = stub_table; }
1160 // Downcast a base pointer to an Arm_input_section pointer. This is
1161 // not type-safe but we only use Arm_input_section not the base class.
1162 static Arm_input_section<big_endian>*
1163 as_arm_input_section(Output_relaxed_input_section* poris)
1164 { return static_cast<Arm_input_section<big_endian>*>(poris); }
1167 // Write data to output file.
1169 do_write(Output_file*);
1171 // Return required alignment of this.
1173 do_addralign() const
1175 if (this->is_stub_table_owner())
1176 return std::max(this->stub_table_->addralign(),
1177 this->original_addralign_);
1179 return this->original_addralign_;
1182 // Finalize data size.
1184 set_final_data_size();
1186 // Reset address and file offset.
1188 do_reset_address_and_file_offset();
1192 do_output_offset(const Relobj* object, unsigned int shndx,
1193 section_offset_type offset,
1194 section_offset_type* poutput) const
1196 if ((object == this->relobj())
1197 && (shndx == this->shndx())
1199 && (convert_types<uint64_t, section_offset_type>(offset)
1200 <= this->original_size_))
1210 // Copying is not allowed.
1211 Arm_input_section(const Arm_input_section&);
1212 Arm_input_section& operator=(const Arm_input_section&);
1214 // Address alignment of the original input section.
1215 uint64_t original_addralign_;
1216 // Section size of the original input section.
1217 uint64_t original_size_;
1219 Stub_table<big_endian>* stub_table_;
1222 // Arm_exidx_fixup class. This is used to define a number of methods
1223 // and keep states for fixing up EXIDX coverage.
1225 class Arm_exidx_fixup
1228 Arm_exidx_fixup(Output_section* exidx_output_section)
1229 : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1230 last_inlined_entry_(0), last_input_section_(NULL),
1231 section_offset_map_(NULL)
1235 { delete this->section_offset_map_; }
1237 // Process an EXIDX section for entry merging. Return number of bytes to
1238 // be deleted in output. If parts of the input EXIDX section are merged
1239 // a heap allocated Arm_exidx_section_offset_map is store in the located
1240 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1242 template<bool big_endian>
1244 process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1245 Arm_exidx_section_offset_map** psection_offset_map);
1247 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1248 // input section, if there is not one already.
1250 add_exidx_cantunwind_as_needed();
1253 // Copying is not allowed.
1254 Arm_exidx_fixup(const Arm_exidx_fixup&);
1255 Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1257 // Type of EXIDX unwind entry.
1262 // EXIDX_CANTUNWIND.
1263 UT_EXIDX_CANTUNWIND,
1270 // Process an EXIDX entry. We only care about the second word of the
1271 // entry. Return true if the entry can be deleted.
1273 process_exidx_entry(uint32_t second_word);
1275 // Update the current section offset map during EXIDX section fix-up.
1276 // If there is no map, create one. INPUT_OFFSET is the offset of a
1277 // reference point, DELETED_BYTES is the number of deleted by in the
1278 // section so far. If DELETE_ENTRY is true, the reference point and
1279 // all offsets after the previous reference point are discarded.
1281 update_offset_map(section_offset_type input_offset,
1282 section_size_type deleted_bytes, bool delete_entry);
1284 // EXIDX output section.
1285 Output_section* exidx_output_section_;
1286 // Unwind type of the last EXIDX entry processed.
1287 Unwind_type last_unwind_type_;
1288 // Last seen inlined EXIDX entry.
1289 uint32_t last_inlined_entry_;
1290 // Last processed EXIDX input section.
1291 Arm_exidx_input_section* last_input_section_;
1292 // Section offset map created in process_exidx_section.
1293 Arm_exidx_section_offset_map* section_offset_map_;
1296 // Arm output section class. This is defined mainly to add a number of
1297 // stub generation methods.
1299 template<bool big_endian>
1300 class Arm_output_section : public Output_section
1303 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1304 elfcpp::Elf_Xword flags)
1305 : Output_section(name, type, flags)
1308 ~Arm_output_section()
1311 // Group input sections for stub generation.
1313 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1315 // Downcast a base pointer to an Arm_output_section pointer. This is
1316 // not type-safe but we only use Arm_output_section not the base class.
1317 static Arm_output_section<big_endian>*
1318 as_arm_output_section(Output_section* os)
1319 { return static_cast<Arm_output_section<big_endian>*>(os); }
1323 typedef Output_section::Input_section Input_section;
1324 typedef Output_section::Input_section_list Input_section_list;
1326 // Create a stub group.
1327 void create_stub_group(Input_section_list::const_iterator,
1328 Input_section_list::const_iterator,
1329 Input_section_list::const_iterator,
1330 Target_arm<big_endian>*,
1331 std::vector<Output_relaxed_input_section*>*);
1334 // Arm_exidx_input_section class. This represents an EXIDX input section.
1336 class Arm_exidx_input_section
1339 static const section_offset_type invalid_offset =
1340 static_cast<section_offset_type>(-1);
1342 Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1343 unsigned int link, uint32_t size, uint32_t addralign)
1344 : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1345 addralign_(addralign)
1348 ~Arm_exidx_input_section()
1351 // Accessors: This is a read-only class.
1353 // Return the object containing this EXIDX input section.
1356 { return this->relobj_; }
1358 // Return the section index of this EXIDX input section.
1361 { return this->shndx_; }
1363 // Return the section index of linked text section in the same object.
1366 { return this->link_; }
1368 // Return size of the EXIDX input section.
1371 { return this->size_; }
1373 // Reutnr address alignment of EXIDX input section.
1376 { return this->addralign_; }
1379 // Object containing this.
1381 // Section index of this.
1382 unsigned int shndx_;
1383 // text section linked to this in the same object.
1385 // Size of this. For ARM 32-bit is sufficient.
1387 // Address alignment of this. For ARM 32-bit is sufficient.
1388 uint32_t addralign_;
1391 // Arm_relobj class.
1393 template<bool big_endian>
1394 class Arm_relobj : public Sized_relobj<32, big_endian>
1397 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1399 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1400 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1401 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1402 stub_tables_(), local_symbol_is_thumb_function_(),
1403 attributes_section_data_(NULL), mapping_symbols_info_(),
1404 section_has_cortex_a8_workaround_(NULL)
1408 { delete this->attributes_section_data_; }
1410 // Return the stub table of the SHNDX-th section if there is one.
1411 Stub_table<big_endian>*
1412 stub_table(unsigned int shndx) const
1414 gold_assert(shndx < this->stub_tables_.size());
1415 return this->stub_tables_[shndx];
1418 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1420 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1422 gold_assert(shndx < this->stub_tables_.size());
1423 this->stub_tables_[shndx] = stub_table;
1426 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1427 // index. This is only valid after do_count_local_symbol is called.
1429 local_symbol_is_thumb_function(unsigned int r_sym) const
1431 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1432 return this->local_symbol_is_thumb_function_[r_sym];
1435 // Scan all relocation sections for stub generation.
1437 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1440 // Convert regular input section with index SHNDX to a relaxed section.
1442 convert_input_section_to_relaxed_section(unsigned shndx)
1444 // The stubs have relocations and we need to process them after writing
1445 // out the stubs. So relocation now must follow section write.
1446 this->invalidate_section_offset(shndx);
1447 this->set_relocs_must_follow_section_writes();
1450 // Downcast a base pointer to an Arm_relobj pointer. This is
1451 // not type-safe but we only use Arm_relobj not the base class.
1452 static Arm_relobj<big_endian>*
1453 as_arm_relobj(Relobj* relobj)
1454 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1456 // Processor-specific flags in ELF file header. This is valid only after
1459 processor_specific_flags() const
1460 { return this->processor_specific_flags_; }
1462 // Attribute section data This is the contents of the .ARM.attribute section
1464 const Attributes_section_data*
1465 attributes_section_data() const
1466 { return this->attributes_section_data_; }
1468 // Mapping symbol location.
1469 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1471 // Functor for STL container.
1472 struct Mapping_symbol_position_less
1475 operator()(const Mapping_symbol_position& p1,
1476 const Mapping_symbol_position& p2) const
1478 return (p1.first < p2.first
1479 || (p1.first == p2.first && p1.second < p2.second));
1483 // We only care about the first character of a mapping symbol, so
1484 // we only store that instead of the whole symbol name.
1485 typedef std::map<Mapping_symbol_position, char,
1486 Mapping_symbol_position_less> Mapping_symbols_info;
1488 // Whether a section contains any Cortex-A8 workaround.
1490 section_has_cortex_a8_workaround(unsigned int shndx) const
1492 return (this->section_has_cortex_a8_workaround_ != NULL
1493 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1496 // Mark a section that has Cortex-A8 workaround.
1498 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1500 if (this->section_has_cortex_a8_workaround_ == NULL)
1501 this->section_has_cortex_a8_workaround_ =
1502 new std::vector<bool>(this->shnum(), false);
1503 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1506 // Return the EXIDX section of an text section with index SHNDX or NULL
1507 // if the text section has no associated EXIDX section.
1508 const Arm_exidx_input_section*
1509 exidx_input_section_by_link(unsigned int shndx) const
1511 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1512 return ((p != this->exidx_section_map_.end()
1513 && p->second->link() == shndx)
1518 // Return the EXIDX section with index SHNDX or NULL if there is none.
1519 const Arm_exidx_input_section*
1520 exidx_input_section_by_shndx(unsigned shndx) const
1522 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1523 return ((p != this->exidx_section_map_.end()
1524 && p->second->shndx() == shndx)
1530 // Post constructor setup.
1534 // Call parent's setup method.
1535 Sized_relobj<32, big_endian>::do_setup();
1537 // Initialize look-up tables.
1538 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1539 this->stub_tables_.swap(empty_stub_table_list);
1542 // Count the local symbols.
1544 do_count_local_symbols(Stringpool_template<char>*,
1545 Stringpool_template<char>*);
1548 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1549 const unsigned char* pshdrs,
1550 typename Sized_relobj<32, big_endian>::Views* pivews);
1552 // Read the symbol information.
1554 do_read_symbols(Read_symbols_data* sd);
1556 // Process relocs for garbage collection.
1558 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1562 // Whether a section needs to be scanned for relocation stubs.
1564 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1565 const Relobj::Output_sections&,
1566 const Symbol_table *);
1568 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1570 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1571 unsigned int, Output_section*,
1572 const Symbol_table *);
1574 // Scan a section for the Cortex-A8 erratum.
1576 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1577 unsigned int, Output_section*,
1578 Target_arm<big_endian>*);
1580 // Make a new Arm_exidx_input_section object for EXIDX section with
1581 // index SHNDX and section header SHDR.
1583 make_exidx_input_section(unsigned int shndx,
1584 const elfcpp::Shdr<32, big_endian>& shdr);
1586 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1587 typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1590 // List of stub tables.
1591 Stub_table_list stub_tables_;
1592 // Bit vector to tell if a local symbol is a thumb function or not.
1593 // This is only valid after do_count_local_symbol is called.
1594 std::vector<bool> local_symbol_is_thumb_function_;
1595 // processor-specific flags in ELF file header.
1596 elfcpp::Elf_Word processor_specific_flags_;
1597 // Object attributes if there is an .ARM.attributes section or NULL.
1598 Attributes_section_data* attributes_section_data_;
1599 // Mapping symbols information.
1600 Mapping_symbols_info mapping_symbols_info_;
1601 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1602 std::vector<bool>* section_has_cortex_a8_workaround_;
1603 // Map a text section to its associated .ARM.exidx section, if there is one.
1604 Exidx_section_map exidx_section_map_;
1607 // Arm_dynobj class.
1609 template<bool big_endian>
1610 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1613 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1614 const elfcpp::Ehdr<32, big_endian>& ehdr)
1615 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1616 processor_specific_flags_(0), attributes_section_data_(NULL)
1620 { delete this->attributes_section_data_; }
1622 // Downcast a base pointer to an Arm_relobj pointer. This is
1623 // not type-safe but we only use Arm_relobj not the base class.
1624 static Arm_dynobj<big_endian>*
1625 as_arm_dynobj(Dynobj* dynobj)
1626 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1628 // Processor-specific flags in ELF file header. This is valid only after
1631 processor_specific_flags() const
1632 { return this->processor_specific_flags_; }
1634 // Attributes section data.
1635 const Attributes_section_data*
1636 attributes_section_data() const
1637 { return this->attributes_section_data_; }
1640 // Read the symbol information.
1642 do_read_symbols(Read_symbols_data* sd);
1645 // processor-specific flags in ELF file header.
1646 elfcpp::Elf_Word processor_specific_flags_;
1647 // Object attributes if there is an .ARM.attributes section or NULL.
1648 Attributes_section_data* attributes_section_data_;
1651 // Functor to read reloc addends during stub generation.
1653 template<int sh_type, bool big_endian>
1654 struct Stub_addend_reader
1656 // Return the addend for a relocation of a particular type. Depending
1657 // on whether this is a REL or RELA relocation, read the addend from a
1658 // view or from a Reloc object.
1659 elfcpp::Elf_types<32>::Elf_Swxword
1661 unsigned int /* r_type */,
1662 const unsigned char* /* view */,
1663 const typename Reloc_types<sh_type,
1664 32, big_endian>::Reloc& /* reloc */) const;
1667 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1669 template<bool big_endian>
1670 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1672 elfcpp::Elf_types<32>::Elf_Swxword
1675 const unsigned char*,
1676 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1679 // Specialized Stub_addend_reader for RELA type relocation sections.
1680 // We currently do not handle RELA type relocation sections but it is trivial
1681 // to implement the addend reader. This is provided for completeness and to
1682 // make it easier to add support for RELA relocation sections in the future.
1684 template<bool big_endian>
1685 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1687 elfcpp::Elf_types<32>::Elf_Swxword
1690 const unsigned char*,
1691 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1692 big_endian>::Reloc& reloc) const
1693 { return reloc.get_r_addend(); }
1696 // Cortex_a8_reloc class. We keep record of relocation that may need
1697 // the Cortex-A8 erratum workaround.
1699 class Cortex_a8_reloc
1702 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1703 Arm_address destination)
1704 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1710 // Accessors: This is a read-only class.
1712 // Return the relocation stub associated with this relocation if there is
1716 { return this->reloc_stub_; }
1718 // Return the relocation type.
1721 { return this->r_type_; }
1723 // Return the destination address of the relocation. LSB stores the THUMB
1727 { return this->destination_; }
1730 // Associated relocation stub if there is one, or NULL.
1731 const Reloc_stub* reloc_stub_;
1733 unsigned int r_type_;
1734 // Destination address of this relocation. LSB is used to distinguish
1736 Arm_address destination_;
1739 // Utilities for manipulating integers of up to 32-bits
1743 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1744 // an int32_t. NO_BITS must be between 1 to 32.
1745 template<int no_bits>
1746 static inline int32_t
1747 sign_extend(uint32_t bits)
1749 gold_assert(no_bits >= 0 && no_bits <= 32);
1751 return static_cast<int32_t>(bits);
1752 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1754 uint32_t top_bit = 1U << (no_bits - 1);
1755 int32_t as_signed = static_cast<int32_t>(bits);
1756 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1759 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1760 template<int no_bits>
1762 has_overflow(uint32_t bits)
1764 gold_assert(no_bits >= 0 && no_bits <= 32);
1767 int32_t max = (1 << (no_bits - 1)) - 1;
1768 int32_t min = -(1 << (no_bits - 1));
1769 int32_t as_signed = static_cast<int32_t>(bits);
1770 return as_signed > max || as_signed < min;
1773 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1774 // fits in the given number of bits as either a signed or unsigned value.
1775 // For example, has_signed_unsigned_overflow<8> would check
1776 // -128 <= bits <= 255
1777 template<int no_bits>
1779 has_signed_unsigned_overflow(uint32_t bits)
1781 gold_assert(no_bits >= 2 && no_bits <= 32);
1784 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1785 int32_t min = -(1 << (no_bits - 1));
1786 int32_t as_signed = static_cast<int32_t>(bits);
1787 return as_signed > max || as_signed < min;
1790 // Select bits from A and B using bits in MASK. For each n in [0..31],
1791 // the n-th bit in the result is chosen from the n-th bits of A and B.
1792 // A zero selects A and a one selects B.
1793 static inline uint32_t
1794 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1795 { return (a & ~mask) | (b & mask); }
1798 template<bool big_endian>
1799 class Target_arm : public Sized_target<32, big_endian>
1802 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1805 // When were are relocating a stub, we pass this as the relocation number.
1806 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1809 : Sized_target<32, big_endian>(&arm_info),
1810 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1811 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1812 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1813 should_force_pic_veneer_(false), arm_input_section_map_(),
1814 attributes_section_data_(NULL), fix_cortex_a8_(false),
1815 cortex_a8_relocs_info_()
1818 // Whether we can use BLX.
1821 { return this->may_use_blx_; }
1823 // Set use-BLX flag.
1825 set_may_use_blx(bool value)
1826 { this->may_use_blx_ = value; }
1828 // Whether we force PCI branch veneers.
1830 should_force_pic_veneer() const
1831 { return this->should_force_pic_veneer_; }
1833 // Set PIC veneer flag.
1835 set_should_force_pic_veneer(bool value)
1836 { this->should_force_pic_veneer_ = value; }
1838 // Whether we use THUMB-2 instructions.
1840 using_thumb2() const
1842 Object_attribute* attr =
1843 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1844 int arch = attr->int_value();
1845 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
1848 // Whether we use THUMB/THUMB-2 instructions only.
1850 using_thumb_only() const
1852 Object_attribute* attr =
1853 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1854 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1855 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1857 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1858 return attr->int_value() == 'M';
1861 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1863 may_use_arm_nop() const
1865 Object_attribute* attr =
1866 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1867 int arch = attr->int_value();
1868 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1869 || arch == elfcpp::TAG_CPU_ARCH_V6K
1870 || arch == elfcpp::TAG_CPU_ARCH_V7
1871 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1874 // Whether we have THUMB-2 NOP.W instruction.
1876 may_use_thumb2_nop() const
1878 Object_attribute* attr =
1879 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1880 int arch = attr->int_value();
1881 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1882 || arch == elfcpp::TAG_CPU_ARCH_V7
1883 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1886 // Process the relocations to determine unreferenced sections for
1887 // garbage collection.
1889 gc_process_relocs(Symbol_table* symtab,
1891 Sized_relobj<32, big_endian>* object,
1892 unsigned int data_shndx,
1893 unsigned int sh_type,
1894 const unsigned char* prelocs,
1896 Output_section* output_section,
1897 bool needs_special_offset_handling,
1898 size_t local_symbol_count,
1899 const unsigned char* plocal_symbols);
1901 // Scan the relocations to look for symbol adjustments.
1903 scan_relocs(Symbol_table* symtab,
1905 Sized_relobj<32, big_endian>* object,
1906 unsigned int data_shndx,
1907 unsigned int sh_type,
1908 const unsigned char* prelocs,
1910 Output_section* output_section,
1911 bool needs_special_offset_handling,
1912 size_t local_symbol_count,
1913 const unsigned char* plocal_symbols);
1915 // Finalize the sections.
1917 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1919 // Return the value to use for a dynamic symbol which requires special
1922 do_dynsym_value(const Symbol*) const;
1924 // Relocate a section.
1926 relocate_section(const Relocate_info<32, big_endian>*,
1927 unsigned int sh_type,
1928 const unsigned char* prelocs,
1930 Output_section* output_section,
1931 bool needs_special_offset_handling,
1932 unsigned char* view,
1933 Arm_address view_address,
1934 section_size_type view_size,
1935 const Reloc_symbol_changes*);
1937 // Scan the relocs during a relocatable link.
1939 scan_relocatable_relocs(Symbol_table* symtab,
1941 Sized_relobj<32, big_endian>* object,
1942 unsigned int data_shndx,
1943 unsigned int sh_type,
1944 const unsigned char* prelocs,
1946 Output_section* output_section,
1947 bool needs_special_offset_handling,
1948 size_t local_symbol_count,
1949 const unsigned char* plocal_symbols,
1950 Relocatable_relocs*);
1952 // Relocate a section during a relocatable link.
1954 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1955 unsigned int sh_type,
1956 const unsigned char* prelocs,
1958 Output_section* output_section,
1959 off_t offset_in_output_section,
1960 const Relocatable_relocs*,
1961 unsigned char* view,
1962 Arm_address view_address,
1963 section_size_type view_size,
1964 unsigned char* reloc_view,
1965 section_size_type reloc_view_size);
1967 // Return whether SYM is defined by the ABI.
1969 do_is_defined_by_abi(Symbol* sym) const
1970 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1972 // Return the size of the GOT section.
1976 gold_assert(this->got_ != NULL);
1977 return this->got_->data_size();
1980 // Map platform-specific reloc types
1982 get_real_reloc_type (unsigned int r_type);
1985 // Methods to support stub-generations.
1988 // Return the stub factory
1990 stub_factory() const
1991 { return this->stub_factory_; }
1993 // Make a new Arm_input_section object.
1994 Arm_input_section<big_endian>*
1995 new_arm_input_section(Relobj*, unsigned int);
1997 // Find the Arm_input_section object corresponding to the SHNDX-th input
1998 // section of RELOBJ.
1999 Arm_input_section<big_endian>*
2000 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
2002 // Make a new Stub_table
2003 Stub_table<big_endian>*
2004 new_stub_table(Arm_input_section<big_endian>*);
2006 // Scan a section for stub generation.
2008 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2009 const unsigned char*, size_t, Output_section*,
2010 bool, const unsigned char*, Arm_address,
2015 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
2016 Output_section*, unsigned char*, Arm_address,
2019 // Get the default ARM target.
2020 static Target_arm<big_endian>*
2023 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2024 && parameters->target().is_big_endian() == big_endian);
2025 return static_cast<Target_arm<big_endian>*>(
2026 parameters->sized_target<32, big_endian>());
2029 // Whether relocation type uses LSB to distinguish THUMB addresses.
2031 reloc_uses_thumb_bit(unsigned int r_type);
2033 // Whether NAME belongs to a mapping symbol.
2035 is_mapping_symbol_name(const char* name)
2039 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2040 && (name[2] == '\0' || name[2] == '.'));
2043 // Whether we work around the Cortex-A8 erratum.
2045 fix_cortex_a8() const
2046 { return this->fix_cortex_a8_; }
2048 // Whether we fix R_ARM_V4BX relocation.
2050 // 1 - replace with MOV instruction (armv4 target)
2051 // 2 - make interworking veneer (>= armv4t targets only)
2052 General_options::Fix_v4bx
2054 { return parameters->options().fix_v4bx(); }
2056 // Scan a span of THUMB code section for Cortex-A8 erratum.
2058 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2059 section_size_type, section_size_type,
2060 const unsigned char*, Arm_address);
2062 // Apply Cortex-A8 workaround to a branch.
2064 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2065 unsigned char*, Arm_address);
2068 // Make an ELF object.
2070 do_make_elf_object(const std::string&, Input_file*, off_t,
2071 const elfcpp::Ehdr<32, big_endian>& ehdr);
2074 do_make_elf_object(const std::string&, Input_file*, off_t,
2075 const elfcpp::Ehdr<32, !big_endian>&)
2076 { gold_unreachable(); }
2079 do_make_elf_object(const std::string&, Input_file*, off_t,
2080 const elfcpp::Ehdr<64, false>&)
2081 { gold_unreachable(); }
2084 do_make_elf_object(const std::string&, Input_file*, off_t,
2085 const elfcpp::Ehdr<64, true>&)
2086 { gold_unreachable(); }
2088 // Make an output section.
2090 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2091 elfcpp::Elf_Xword flags)
2092 { return new Arm_output_section<big_endian>(name, type, flags); }
2095 do_adjust_elf_header(unsigned char* view, int len) const;
2097 // We only need to generate stubs, and hence perform relaxation if we are
2098 // not doing relocatable linking.
2100 do_may_relax() const
2101 { return !parameters->options().relocatable(); }
2104 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
2106 // Determine whether an object attribute tag takes an integer, a
2109 do_attribute_arg_type(int tag) const;
2111 // Reorder tags during output.
2113 do_attributes_order(int num) const;
2116 // The class which scans relocations.
2121 : issued_non_pic_error_(false)
2125 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
2126 Sized_relobj<32, big_endian>* object,
2127 unsigned int data_shndx,
2128 Output_section* output_section,
2129 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2130 const elfcpp::Sym<32, big_endian>& lsym);
2133 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
2134 Sized_relobj<32, big_endian>* object,
2135 unsigned int data_shndx,
2136 Output_section* output_section,
2137 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2142 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
2143 unsigned int r_type);
2146 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
2147 unsigned int r_type, Symbol*);
2150 check_non_pic(Relobj*, unsigned int r_type);
2152 // Almost identical to Symbol::needs_plt_entry except that it also
2153 // handles STT_ARM_TFUNC.
2155 symbol_needs_plt_entry(const Symbol* sym)
2157 // An undefined symbol from an executable does not need a PLT entry.
2158 if (sym->is_undefined() && !parameters->options().shared())
2161 return (!parameters->doing_static_link()
2162 && (sym->type() == elfcpp::STT_FUNC
2163 || sym->type() == elfcpp::STT_ARM_TFUNC)
2164 && (sym->is_from_dynobj()
2165 || sym->is_undefined()
2166 || sym->is_preemptible()));
2169 // Whether we have issued an error about a non-PIC compilation.
2170 bool issued_non_pic_error_;
2173 // The class which implements relocation.
2183 // Return whether the static relocation needs to be applied.
2185 should_apply_static_reloc(const Sized_symbol<32>* gsym,
2188 Output_section* output_section);
2190 // Do a relocation. Return false if the caller should not issue
2191 // any warnings about this relocation.
2193 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2194 Output_section*, size_t relnum,
2195 const elfcpp::Rel<32, big_endian>&,
2196 unsigned int r_type, const Sized_symbol<32>*,
2197 const Symbol_value<32>*,
2198 unsigned char*, Arm_address,
2201 // Return whether we want to pass flag NON_PIC_REF for this
2202 // reloc. This means the relocation type accesses a symbol not via
2205 reloc_is_non_pic (unsigned int r_type)
2209 // These relocation types reference GOT or PLT entries explicitly.
2210 case elfcpp::R_ARM_GOT_BREL:
2211 case elfcpp::R_ARM_GOT_ABS:
2212 case elfcpp::R_ARM_GOT_PREL:
2213 case elfcpp::R_ARM_GOT_BREL12:
2214 case elfcpp::R_ARM_PLT32_ABS:
2215 case elfcpp::R_ARM_TLS_GD32:
2216 case elfcpp::R_ARM_TLS_LDM32:
2217 case elfcpp::R_ARM_TLS_IE32:
2218 case elfcpp::R_ARM_TLS_IE12GP:
2220 // These relocate types may use PLT entries.
2221 case elfcpp::R_ARM_CALL:
2222 case elfcpp::R_ARM_THM_CALL:
2223 case elfcpp::R_ARM_JUMP24:
2224 case elfcpp::R_ARM_THM_JUMP24:
2225 case elfcpp::R_ARM_THM_JUMP19:
2226 case elfcpp::R_ARM_PLT32:
2227 case elfcpp::R_ARM_THM_XPC22:
2236 // A class which returns the size required for a relocation type,
2237 // used while scanning relocs during a relocatable link.
2238 class Relocatable_size_for_reloc
2242 get_size_for_reloc(unsigned int, Relobj*);
2245 // Get the GOT section, creating it if necessary.
2246 Output_data_got<32, big_endian>*
2247 got_section(Symbol_table*, Layout*);
2249 // Get the GOT PLT section.
2251 got_plt_section() const
2253 gold_assert(this->got_plt_ != NULL);
2254 return this->got_plt_;
2257 // Create a PLT entry for a global symbol.
2259 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2261 // Get the PLT section.
2262 const Output_data_plt_arm<big_endian>*
2265 gold_assert(this->plt_ != NULL);
2269 // Get the dynamic reloc section, creating it if necessary.
2271 rel_dyn_section(Layout*);
2273 // Return true if the symbol may need a COPY relocation.
2274 // References from an executable object to non-function symbols
2275 // defined in a dynamic object may need a COPY relocation.
2277 may_need_copy_reloc(Symbol* gsym)
2279 return (gsym->type() != elfcpp::STT_ARM_TFUNC
2280 && gsym->may_need_copy_reloc());
2283 // Add a potential copy relocation.
2285 copy_reloc(Symbol_table* symtab, Layout* layout,
2286 Sized_relobj<32, big_endian>* object,
2287 unsigned int shndx, Output_section* output_section,
2288 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2290 this->copy_relocs_.copy_reloc(symtab, layout,
2291 symtab->get_sized_symbol<32>(sym),
2292 object, shndx, output_section, reloc,
2293 this->rel_dyn_section(layout));
2296 // Whether two EABI versions are compatible.
2298 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2300 // Merge processor-specific flags from input object and those in the ELF
2301 // header of the output.
2303 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2305 // Get the secondary compatible architecture.
2307 get_secondary_compatible_arch(const Attributes_section_data*);
2309 // Set the secondary compatible architecture.
2311 set_secondary_compatible_arch(Attributes_section_data*, int);
2314 tag_cpu_arch_combine(const char*, int, int*, int, int);
2316 // Helper to print AEABI enum tag value.
2318 aeabi_enum_name(unsigned int);
2320 // Return string value for TAG_CPU_name.
2322 tag_cpu_name_value(unsigned int);
2324 // Merge object attributes from input object and those in the output.
2326 merge_object_attributes(const char*, const Attributes_section_data*);
2328 // Helper to get an AEABI object attribute
2330 get_aeabi_object_attribute(int tag) const
2332 Attributes_section_data* pasd = this->attributes_section_data_;
2333 gold_assert(pasd != NULL);
2334 Object_attribute* attr =
2335 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2336 gold_assert(attr != NULL);
2341 // Methods to support stub-generations.
2344 // Group input sections for stub generation.
2346 group_sections(Layout*, section_size_type, bool);
2348 // Scan a relocation for stub generation.
2350 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2351 const Sized_symbol<32>*, unsigned int,
2352 const Symbol_value<32>*,
2353 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
2355 // Scan a relocation section for stub.
2356 template<int sh_type>
2358 scan_reloc_section_for_stubs(
2359 const Relocate_info<32, big_endian>* relinfo,
2360 const unsigned char* prelocs,
2362 Output_section* output_section,
2363 bool needs_special_offset_handling,
2364 const unsigned char* view,
2365 elfcpp::Elf_types<32>::Elf_Addr view_address,
2368 // Information about this specific target which we pass to the
2369 // general Target structure.
2370 static const Target::Target_info arm_info;
2372 // The types of GOT entries needed for this platform.
2375 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
2378 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2380 // Map input section to Arm_input_section.
2381 typedef Unordered_map<Section_id,
2382 Arm_input_section<big_endian>*,
2384 Arm_input_section_map;
2386 // Map output addresses to relocs for Cortex-A8 erratum.
2387 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2388 Cortex_a8_relocs_info;
2391 Output_data_got<32, big_endian>* got_;
2393 Output_data_plt_arm<big_endian>* plt_;
2394 // The GOT PLT section.
2395 Output_data_space* got_plt_;
2396 // The dynamic reloc section.
2397 Reloc_section* rel_dyn_;
2398 // Relocs saved to avoid a COPY reloc.
2399 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2400 // Space for variables copied with a COPY reloc.
2401 Output_data_space* dynbss_;
2402 // Vector of Stub_tables created.
2403 Stub_table_list stub_tables_;
2405 const Stub_factory &stub_factory_;
2406 // Whether we can use BLX.
2408 // Whether we force PIC branch veneers.
2409 bool should_force_pic_veneer_;
2410 // Map for locating Arm_input_sections.
2411 Arm_input_section_map arm_input_section_map_;
2412 // Attributes section data in output.
2413 Attributes_section_data* attributes_section_data_;
2414 // Whether we want to fix code for Cortex-A8 erratum.
2415 bool fix_cortex_a8_;
2416 // Map addresses to relocs for Cortex-A8 erratum.
2417 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2420 template<bool big_endian>
2421 const Target::Target_info Target_arm<big_endian>::arm_info =
2424 big_endian, // is_big_endian
2425 elfcpp::EM_ARM, // machine_code
2426 false, // has_make_symbol
2427 false, // has_resolve
2428 false, // has_code_fill
2429 true, // is_default_stack_executable
2431 "/usr/lib/libc.so.1", // dynamic_linker
2432 0x8000, // default_text_segment_address
2433 0x1000, // abi_pagesize (overridable by -z max-page-size)
2434 0x1000, // common_pagesize (overridable by -z common-page-size)
2435 elfcpp::SHN_UNDEF, // small_common_shndx
2436 elfcpp::SHN_UNDEF, // large_common_shndx
2437 0, // small_common_section_flags
2438 0, // large_common_section_flags
2439 ".ARM.attributes", // attributes_section
2440 "aeabi" // attributes_vendor
2443 // Arm relocate functions class
2446 template<bool big_endian>
2447 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2452 STATUS_OKAY, // No error during relocation.
2453 STATUS_OVERFLOW, // Relocation oveflow.
2454 STATUS_BAD_RELOC // Relocation cannot be applied.
2458 typedef Relocate_functions<32, big_endian> Base;
2459 typedef Arm_relocate_functions<big_endian> This;
2461 // Encoding of imm16 argument for movt and movw ARM instructions
2464 // imm16 := imm4 | imm12
2466 // 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
2467 // +-------+---------------+-------+-------+-----------------------+
2468 // | | |imm4 | |imm12 |
2469 // +-------+---------------+-------+-------+-----------------------+
2471 // Extract the relocation addend from VAL based on the ARM
2472 // instruction encoding described above.
2473 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2474 extract_arm_movw_movt_addend(
2475 typename elfcpp::Swap<32, big_endian>::Valtype val)
2477 // According to the Elf ABI for ARM Architecture the immediate
2478 // field is sign-extended to form the addend.
2479 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2482 // Insert X into VAL based on the ARM instruction encoding described
2484 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2485 insert_val_arm_movw_movt(
2486 typename elfcpp::Swap<32, big_endian>::Valtype val,
2487 typename elfcpp::Swap<32, big_endian>::Valtype x)
2491 val |= (x & 0xf000) << 4;
2495 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2498 // imm16 := imm4 | i | imm3 | imm8
2500 // 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
2501 // +---------+-+-----------+-------++-+-----+-------+---------------+
2502 // | |i| |imm4 || |imm3 | |imm8 |
2503 // +---------+-+-----------+-------++-+-----+-------+---------------+
2505 // Extract the relocation addend from VAL based on the Thumb2
2506 // instruction encoding described above.
2507 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2508 extract_thumb_movw_movt_addend(
2509 typename elfcpp::Swap<32, big_endian>::Valtype val)
2511 // According to the Elf ABI for ARM Architecture the immediate
2512 // field is sign-extended to form the addend.
2513 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2514 | ((val >> 15) & 0x0800)
2515 | ((val >> 4) & 0x0700)
2519 // Insert X into VAL based on the Thumb2 instruction encoding
2521 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2522 insert_val_thumb_movw_movt(
2523 typename elfcpp::Swap<32, big_endian>::Valtype val,
2524 typename elfcpp::Swap<32, big_endian>::Valtype x)
2527 val |= (x & 0xf000) << 4;
2528 val |= (x & 0x0800) << 15;
2529 val |= (x & 0x0700) << 4;
2530 val |= (x & 0x00ff);
2534 // Handle ARM long branches.
2535 static typename This::Status
2536 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2537 unsigned char *, const Sized_symbol<32>*,
2538 const Arm_relobj<big_endian>*, unsigned int,
2539 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2541 // Handle THUMB long branches.
2542 static typename This::Status
2543 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2544 unsigned char *, const Sized_symbol<32>*,
2545 const Arm_relobj<big_endian>*, unsigned int,
2546 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2550 // Return the branch offset of a 32-bit THUMB branch.
2551 static inline int32_t
2552 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2554 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2555 // involving the J1 and J2 bits.
2556 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2557 uint32_t upper = upper_insn & 0x3ffU;
2558 uint32_t lower = lower_insn & 0x7ffU;
2559 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2560 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2561 uint32_t i1 = j1 ^ s ? 0 : 1;
2562 uint32_t i2 = j2 ^ s ? 0 : 1;
2564 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2565 | (upper << 12) | (lower << 1));
2568 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2569 // UPPER_INSN is the original upper instruction of the branch. Caller is
2570 // responsible for overflow checking and BLX offset adjustment.
2571 static inline uint16_t
2572 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2574 uint32_t s = offset < 0 ? 1 : 0;
2575 uint32_t bits = static_cast<uint32_t>(offset);
2576 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2579 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2580 // LOWER_INSN is the original lower instruction of the branch. Caller is
2581 // responsible for overflow checking and BLX offset adjustment.
2582 static inline uint16_t
2583 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2585 uint32_t s = offset < 0 ? 1 : 0;
2586 uint32_t bits = static_cast<uint32_t>(offset);
2587 return ((lower_insn & ~0x2fffU)
2588 | ((((bits >> 23) & 1) ^ !s) << 13)
2589 | ((((bits >> 22) & 1) ^ !s) << 11)
2590 | ((bits >> 1) & 0x7ffU));
2593 // Return the branch offset of a 32-bit THUMB conditional branch.
2594 static inline int32_t
2595 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2597 uint32_t s = (upper_insn & 0x0400U) >> 10;
2598 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2599 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2600 uint32_t lower = (lower_insn & 0x07ffU);
2601 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2603 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2606 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2607 // instruction. UPPER_INSN is the original upper instruction of the branch.
2608 // Caller is responsible for overflow checking.
2609 static inline uint16_t
2610 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2612 uint32_t s = offset < 0 ? 1 : 0;
2613 uint32_t bits = static_cast<uint32_t>(offset);
2614 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2617 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2618 // instruction. LOWER_INSN is the original lower instruction of the branch.
2619 // Caller is reponsible for overflow checking.
2620 static inline uint16_t
2621 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2623 uint32_t bits = static_cast<uint32_t>(offset);
2624 uint32_t j2 = (bits & 0x00080000U) >> 19;
2625 uint32_t j1 = (bits & 0x00040000U) >> 18;
2626 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2628 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2631 // R_ARM_ABS8: S + A
2632 static inline typename This::Status
2633 abs8(unsigned char *view,
2634 const Sized_relobj<32, big_endian>* object,
2635 const Symbol_value<32>* psymval)
2637 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2638 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2639 Valtype* wv = reinterpret_cast<Valtype*>(view);
2640 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2641 Reltype addend = utils::sign_extend<8>(val);
2642 Reltype x = psymval->value(object, addend);
2643 val = utils::bit_select(val, x, 0xffU);
2644 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2645 return (utils::has_signed_unsigned_overflow<8>(x)
2646 ? This::STATUS_OVERFLOW
2647 : This::STATUS_OKAY);
2650 // R_ARM_THM_ABS5: S + A
2651 static inline typename This::Status
2652 thm_abs5(unsigned char *view,
2653 const Sized_relobj<32, big_endian>* object,
2654 const Symbol_value<32>* psymval)
2656 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2657 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2658 Valtype* wv = reinterpret_cast<Valtype*>(view);
2659 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2660 Reltype addend = (val & 0x7e0U) >> 6;
2661 Reltype x = psymval->value(object, addend);
2662 val = utils::bit_select(val, x << 6, 0x7e0U);
2663 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2664 return (utils::has_overflow<5>(x)
2665 ? This::STATUS_OVERFLOW
2666 : This::STATUS_OKAY);
2669 // R_ARM_ABS12: S + A
2670 static inline typename This::Status
2671 abs12(unsigned char *view,
2672 const Sized_relobj<32, big_endian>* object,
2673 const Symbol_value<32>* psymval)
2675 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2676 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2677 Valtype* wv = reinterpret_cast<Valtype*>(view);
2678 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2679 Reltype addend = val & 0x0fffU;
2680 Reltype x = psymval->value(object, addend);
2681 val = utils::bit_select(val, x, 0x0fffU);
2682 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2683 return (utils::has_overflow<12>(x)
2684 ? This::STATUS_OVERFLOW
2685 : This::STATUS_OKAY);
2688 // R_ARM_ABS16: S + A
2689 static inline typename This::Status
2690 abs16(unsigned char *view,
2691 const Sized_relobj<32, big_endian>* object,
2692 const Symbol_value<32>* psymval)
2694 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2695 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2696 Valtype* wv = reinterpret_cast<Valtype*>(view);
2697 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2698 Reltype addend = utils::sign_extend<16>(val);
2699 Reltype x = psymval->value(object, addend);
2700 val = utils::bit_select(val, x, 0xffffU);
2701 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2702 return (utils::has_signed_unsigned_overflow<16>(x)
2703 ? This::STATUS_OVERFLOW
2704 : This::STATUS_OKAY);
2707 // R_ARM_ABS32: (S + A) | T
2708 static inline typename This::Status
2709 abs32(unsigned char *view,
2710 const Sized_relobj<32, big_endian>* object,
2711 const Symbol_value<32>* psymval,
2712 Arm_address thumb_bit)
2714 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2715 Valtype* wv = reinterpret_cast<Valtype*>(view);
2716 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2717 Valtype x = psymval->value(object, addend) | thumb_bit;
2718 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2719 return This::STATUS_OKAY;
2722 // R_ARM_REL32: (S + A) | T - P
2723 static inline typename This::Status
2724 rel32(unsigned char *view,
2725 const Sized_relobj<32, big_endian>* object,
2726 const Symbol_value<32>* psymval,
2727 Arm_address address,
2728 Arm_address thumb_bit)
2730 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2731 Valtype* wv = reinterpret_cast<Valtype*>(view);
2732 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2733 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2734 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2735 return This::STATUS_OKAY;
2738 // R_ARM_THM_CALL: (S + A) | T - P
2739 static inline typename This::Status
2740 thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2741 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2742 unsigned int r_sym, const Symbol_value<32>* psymval,
2743 Arm_address address, Arm_address thumb_bit,
2744 bool is_weakly_undefined_without_plt)
2746 return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
2747 object, r_sym, psymval, address, thumb_bit,
2748 is_weakly_undefined_without_plt);
2751 // R_ARM_THM_JUMP24: (S + A) | T - P
2752 static inline typename This::Status
2753 thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2754 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2755 unsigned int r_sym, const Symbol_value<32>* psymval,
2756 Arm_address address, Arm_address thumb_bit,
2757 bool is_weakly_undefined_without_plt)
2759 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
2760 object, r_sym, psymval, address, thumb_bit,
2761 is_weakly_undefined_without_plt);
2764 // R_ARM_THM_JUMP24: (S + A) | T - P
2765 static typename This::Status
2766 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
2767 const Symbol_value<32>* psymval, Arm_address address,
2768 Arm_address thumb_bit);
2770 // R_ARM_THM_XPC22: (S + A) | T - P
2771 static inline typename This::Status
2772 thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2773 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2774 unsigned int r_sym, const Symbol_value<32>* psymval,
2775 Arm_address address, Arm_address thumb_bit,
2776 bool is_weakly_undefined_without_plt)
2778 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
2779 object, r_sym, psymval, address, thumb_bit,
2780 is_weakly_undefined_without_plt);
2783 // R_ARM_THM_JUMP6: S + A – P
2784 static inline typename This::Status
2785 thm_jump6(unsigned char *view,
2786 const Sized_relobj<32, big_endian>* object,
2787 const Symbol_value<32>* psymval,
2788 Arm_address address)
2790 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2791 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2792 Valtype* wv = reinterpret_cast<Valtype*>(view);
2793 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2794 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
2795 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
2796 Reltype x = (psymval->value(object, addend) - address);
2797 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
2798 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2799 // CZB does only forward jumps.
2800 return ((x > 0x007e)
2801 ? This::STATUS_OVERFLOW
2802 : This::STATUS_OKAY);
2805 // R_ARM_THM_JUMP8: S + A – P
2806 static inline typename This::Status
2807 thm_jump8(unsigned char *view,
2808 const Sized_relobj<32, big_endian>* object,
2809 const Symbol_value<32>* psymval,
2810 Arm_address address)
2812 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2813 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2814 Valtype* wv = reinterpret_cast<Valtype*>(view);
2815 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2816 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
2817 Reltype x = (psymval->value(object, addend) - address);
2818 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
2819 return (utils::has_overflow<8>(x)
2820 ? This::STATUS_OVERFLOW
2821 : This::STATUS_OKAY);
2824 // R_ARM_THM_JUMP11: S + A – P
2825 static inline typename This::Status
2826 thm_jump11(unsigned char *view,
2827 const Sized_relobj<32, big_endian>* object,
2828 const Symbol_value<32>* psymval,
2829 Arm_address address)
2831 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2832 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2833 Valtype* wv = reinterpret_cast<Valtype*>(view);
2834 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2835 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
2836 Reltype x = (psymval->value(object, addend) - address);
2837 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
2838 return (utils::has_overflow<11>(x)
2839 ? This::STATUS_OVERFLOW
2840 : This::STATUS_OKAY);
2843 // R_ARM_BASE_PREL: B(S) + A - P
2844 static inline typename This::Status
2845 base_prel(unsigned char* view,
2847 Arm_address address)
2849 Base::rel32(view, origin - address);
2853 // R_ARM_BASE_ABS: B(S) + A
2854 static inline typename This::Status
2855 base_abs(unsigned char* view,
2858 Base::rel32(view, origin);
2862 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2863 static inline typename This::Status
2864 got_brel(unsigned char* view,
2865 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
2867 Base::rel32(view, got_offset);
2868 return This::STATUS_OKAY;
2871 // R_ARM_GOT_PREL: GOT(S) + A - P
2872 static inline typename This::Status
2873 got_prel(unsigned char *view,
2874 Arm_address got_entry,
2875 Arm_address address)
2877 Base::rel32(view, got_entry - address);
2878 return This::STATUS_OKAY;
2881 // R_ARM_PLT32: (S + A) | T - P
2882 static inline typename This::Status
2883 plt32(const Relocate_info<32, big_endian>* relinfo,
2884 unsigned char *view,
2885 const Sized_symbol<32>* gsym,
2886 const Arm_relobj<big_endian>* object,
2888 const Symbol_value<32>* psymval,
2889 Arm_address address,
2890 Arm_address thumb_bit,
2891 bool is_weakly_undefined_without_plt)
2893 return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
2894 object, r_sym, psymval, address, thumb_bit,
2895 is_weakly_undefined_without_plt);
2898 // R_ARM_XPC25: (S + A) | T - P
2899 static inline typename This::Status
2900 xpc25(const Relocate_info<32, big_endian>* relinfo,
2901 unsigned char *view,
2902 const Sized_symbol<32>* gsym,
2903 const Arm_relobj<big_endian>* object,
2905 const Symbol_value<32>* psymval,
2906 Arm_address address,
2907 Arm_address thumb_bit,
2908 bool is_weakly_undefined_without_plt)
2910 return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
2911 object, r_sym, psymval, address, thumb_bit,
2912 is_weakly_undefined_without_plt);
2915 // R_ARM_CALL: (S + A) | T - P
2916 static inline typename This::Status
2917 call(const Relocate_info<32, big_endian>* relinfo,
2918 unsigned char *view,
2919 const Sized_symbol<32>* gsym,
2920 const Arm_relobj<big_endian>* object,
2922 const Symbol_value<32>* psymval,
2923 Arm_address address,
2924 Arm_address thumb_bit,
2925 bool is_weakly_undefined_without_plt)
2927 return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2928 object, r_sym, psymval, address, thumb_bit,
2929 is_weakly_undefined_without_plt);
2932 // R_ARM_JUMP24: (S + A) | T - P
2933 static inline typename This::Status
2934 jump24(const Relocate_info<32, big_endian>* relinfo,
2935 unsigned char *view,
2936 const Sized_symbol<32>* gsym,
2937 const Arm_relobj<big_endian>* object,
2939 const Symbol_value<32>* psymval,
2940 Arm_address address,
2941 Arm_address thumb_bit,
2942 bool is_weakly_undefined_without_plt)
2944 return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2945 object, r_sym, psymval, address, thumb_bit,
2946 is_weakly_undefined_without_plt);
2949 // R_ARM_PREL: (S + A) | T - P
2950 static inline typename This::Status
2951 prel31(unsigned char *view,
2952 const Sized_relobj<32, big_endian>* object,
2953 const Symbol_value<32>* psymval,
2954 Arm_address address,
2955 Arm_address thumb_bit)
2957 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2958 Valtype* wv = reinterpret_cast<Valtype*>(view);
2959 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2960 Valtype addend = utils::sign_extend<31>(val);
2961 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2962 val = utils::bit_select(val, x, 0x7fffffffU);
2963 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2964 return (utils::has_overflow<31>(x) ?
2965 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2968 // R_ARM_MOVW_ABS_NC: (S + A) | T
2969 static inline typename This::Status
2970 movw_abs_nc(unsigned char *view,
2971 const Sized_relobj<32, big_endian>* object,
2972 const Symbol_value<32>* psymval,
2973 Arm_address thumb_bit)
2975 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2976 Valtype* wv = reinterpret_cast<Valtype*>(view);
2977 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2978 Valtype addend = This::extract_arm_movw_movt_addend(val);
2979 Valtype x = psymval->value(object, addend) | thumb_bit;
2980 val = This::insert_val_arm_movw_movt(val, x);
2981 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2982 return This::STATUS_OKAY;
2985 // R_ARM_MOVT_ABS: S + A
2986 static inline typename This::Status
2987 movt_abs(unsigned char *view,
2988 const Sized_relobj<32, big_endian>* object,
2989 const Symbol_value<32>* psymval)
2991 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2992 Valtype* wv = reinterpret_cast<Valtype*>(view);
2993 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2994 Valtype addend = This::extract_arm_movw_movt_addend(val);
2995 Valtype x = psymval->value(object, addend) >> 16;
2996 val = This::insert_val_arm_movw_movt(val, x);
2997 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2998 return This::STATUS_OKAY;
3001 // R_ARM_THM_MOVW_ABS_NC: S + A | T
3002 static inline typename This::Status
3003 thm_movw_abs_nc(unsigned char *view,
3004 const Sized_relobj<32, big_endian>* object,
3005 const Symbol_value<32>* psymval,
3006 Arm_address thumb_bit)
3008 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3009 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3010 Valtype* wv = reinterpret_cast<Valtype*>(view);
3011 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3012 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
3013 Reltype addend = extract_thumb_movw_movt_addend(val);
3014 Reltype x = psymval->value(object, addend) | thumb_bit;
3015 val = This::insert_val_thumb_movw_movt(val, x);
3016 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3017 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3018 return This::STATUS_OKAY;
3021 // R_ARM_THM_MOVT_ABS: S + A
3022 static inline typename This::Status
3023 thm_movt_abs(unsigned char *view,
3024 const Sized_relobj<32, big_endian>* object,
3025 const Symbol_value<32>* psymval)
3027 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3028 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3029 Valtype* wv = reinterpret_cast<Valtype*>(view);
3030 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3031 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
3032 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3033 Reltype x = psymval->value(object, addend) >> 16;
3034 val = This::insert_val_thumb_movw_movt(val, x);
3035 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3036 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3037 return This::STATUS_OKAY;
3040 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3041 static inline typename This::Status
3042 movw_prel_nc(unsigned char *view,
3043 const Sized_relobj<32, big_endian>* object,
3044 const Symbol_value<32>* psymval,
3045 Arm_address address,
3046 Arm_address thumb_bit)
3048 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3049 Valtype* wv = reinterpret_cast<Valtype*>(view);
3050 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3051 Valtype addend = This::extract_arm_movw_movt_addend(val);
3052 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3053 val = This::insert_val_arm_movw_movt(val, x);
3054 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3055 return This::STATUS_OKAY;
3058 // R_ARM_MOVT_PREL: S + A - P
3059 static inline typename This::Status
3060 movt_prel(unsigned char *view,
3061 const Sized_relobj<32, big_endian>* object,
3062 const Symbol_value<32>* psymval,
3063 Arm_address address)
3065 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3066 Valtype* wv = reinterpret_cast<Valtype*>(view);
3067 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3068 Valtype addend = This::extract_arm_movw_movt_addend(val);
3069 Valtype x = (psymval->value(object, addend) - address) >> 16;
3070 val = This::insert_val_arm_movw_movt(val, x);
3071 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3072 return This::STATUS_OKAY;
3075 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3076 static inline typename This::Status
3077 thm_movw_prel_nc(unsigned char *view,
3078 const Sized_relobj<32, big_endian>* object,
3079 const Symbol_value<32>* psymval,
3080 Arm_address address,
3081 Arm_address thumb_bit)
3083 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3084 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3085 Valtype* wv = reinterpret_cast<Valtype*>(view);
3086 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3087 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3088 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3089 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
3090 val = This::insert_val_thumb_movw_movt(val, x);
3091 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3092 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3093 return This::STATUS_OKAY;
3096 // R_ARM_THM_MOVT_PREL: S + A - P
3097 static inline typename This::Status
3098 thm_movt_prel(unsigned char *view,
3099 const Sized_relobj<32, big_endian>* object,
3100 const Symbol_value<32>* psymval,
3101 Arm_address address)
3103 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3104 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3105 Valtype* wv = reinterpret_cast<Valtype*>(view);
3106 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3107 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3108 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3109 Reltype x = (psymval->value(object, addend) - address) >> 16;
3110 val = This::insert_val_thumb_movw_movt(val, x);
3111 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3112 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3113 return This::STATUS_OKAY;
3117 static inline typename This::Status
3118 v4bx(const Relocate_info<32, big_endian>* relinfo,
3119 unsigned char *view,
3120 const Arm_relobj<big_endian>* object,
3121 const Arm_address address,
3122 const bool is_interworking)
3125 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3126 Valtype* wv = reinterpret_cast<Valtype*>(view);
3127 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3129 // Ensure that we have a BX instruction.
3130 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3131 const uint32_t reg = (val & 0xf);
3132 if (is_interworking && reg != 0xf)
3134 Stub_table<big_endian>* stub_table =
3135 object->stub_table(relinfo->data_shndx);
3136 gold_assert(stub_table != NULL);
3138 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3139 gold_assert(stub != NULL);
3141 int32_t veneer_address =
3142 stub_table->address() + stub->offset() - 8 - address;
3143 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3144 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3145 // Replace with a branch to veneer (B <addr>)
3146 val = (val & 0xf0000000) | 0x0a000000
3147 | ((veneer_address >> 2) & 0x00ffffff);
3151 // Preserve Rm (lowest four bits) and the condition code
3152 // (highest four bits). Other bits encode MOV PC,Rm.
3153 val = (val & 0xf000000f) | 0x01a0f000;
3155 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3156 return This::STATUS_OKAY;
3160 // Relocate ARM long branches. This handles relocation types
3161 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3162 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3163 // undefined and we do not use PLT in this relocation. In such a case,
3164 // the branch is converted into an NOP.
3166 template<bool big_endian>
3167 typename Arm_relocate_functions<big_endian>::Status
3168 Arm_relocate_functions<big_endian>::arm_branch_common(
3169 unsigned int r_type,
3170 const Relocate_info<32, big_endian>* relinfo,
3171 unsigned char *view,
3172 const Sized_symbol<32>* gsym,
3173 const Arm_relobj<big_endian>* object,
3175 const Symbol_value<32>* psymval,
3176 Arm_address address,
3177 Arm_address thumb_bit,
3178 bool is_weakly_undefined_without_plt)
3180 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3181 Valtype* wv = reinterpret_cast<Valtype*>(view);
3182 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3184 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3185 && ((val & 0x0f000000UL) == 0x0a000000UL);
3186 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3187 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3188 && ((val & 0x0f000000UL) == 0x0b000000UL);
3189 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3190 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3192 // Check that the instruction is valid.
3193 if (r_type == elfcpp::R_ARM_CALL)
3195 if (!insn_is_uncond_bl && !insn_is_blx)
3196 return This::STATUS_BAD_RELOC;
3198 else if (r_type == elfcpp::R_ARM_JUMP24)
3200 if (!insn_is_b && !insn_is_cond_bl)
3201 return This::STATUS_BAD_RELOC;
3203 else if (r_type == elfcpp::R_ARM_PLT32)
3205 if (!insn_is_any_branch)
3206 return This::STATUS_BAD_RELOC;
3208 else if (r_type == elfcpp::R_ARM_XPC25)
3210 // FIXME: AAELF document IH0044C does not say much about it other
3211 // than it being obsolete.
3212 if (!insn_is_any_branch)
3213 return This::STATUS_BAD_RELOC;
3218 // A branch to an undefined weak symbol is turned into a jump to
3219 // the next instruction unless a PLT entry will be created.
3220 // Do the same for local undefined symbols.
3221 // The jump to the next instruction is optimized as a NOP depending
3222 // on the architecture.
3223 const Target_arm<big_endian>* arm_target =
3224 Target_arm<big_endian>::default_target();
3225 if (is_weakly_undefined_without_plt)
3227 Valtype cond = val & 0xf0000000U;
3228 if (arm_target->may_use_arm_nop())
3229 val = cond | 0x0320f000;
3231 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3232 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3233 return This::STATUS_OKAY;
3236 Valtype addend = utils::sign_extend<26>(val << 2);
3237 Valtype branch_target = psymval->value(object, addend);
3238 int32_t branch_offset = branch_target - address;
3240 // We need a stub if the branch offset is too large or if we need
3242 bool may_use_blx = arm_target->may_use_blx();
3243 Reloc_stub* stub = NULL;
3244 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
3245 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3246 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
3248 Stub_type stub_type =
3249 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
3251 if (stub_type != arm_stub_none)
3253 Stub_table<big_endian>* stub_table =
3254 object->stub_table(relinfo->data_shndx);
3255 gold_assert(stub_table != NULL);
3257 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3258 stub = stub_table->find_reloc_stub(stub_key);
3259 gold_assert(stub != NULL);
3260 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3261 branch_target = stub_table->address() + stub->offset() + addend;
3262 branch_offset = branch_target - address;
3263 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
3264 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
3268 // At this point, if we still need to switch mode, the instruction
3269 // must either be a BLX or a BL that can be converted to a BLX.
3273 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3274 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3277 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3278 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3279 return (utils::has_overflow<26>(branch_offset)
3280 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3283 // Relocate THUMB long branches. This handles relocation types
3284 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3285 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3286 // undefined and we do not use PLT in this relocation. In such a case,
3287 // the branch is converted into an NOP.
3289 template<bool big_endian>
3290 typename Arm_relocate_functions<big_endian>::Status
3291 Arm_relocate_functions<big_endian>::thumb_branch_common(
3292 unsigned int r_type,
3293 const Relocate_info<32, big_endian>* relinfo,
3294 unsigned char *view,
3295 const Sized_symbol<32>* gsym,
3296 const Arm_relobj<big_endian>* object,
3298 const Symbol_value<32>* psymval,
3299 Arm_address address,
3300 Arm_address thumb_bit,
3301 bool is_weakly_undefined_without_plt)
3303 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3304 Valtype* wv = reinterpret_cast<Valtype*>(view);
3305 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3306 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3308 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3310 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
3311 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
3313 // Check that the instruction is valid.
3314 if (r_type == elfcpp::R_ARM_THM_CALL)
3316 if (!is_bl_insn && !is_blx_insn)
3317 return This::STATUS_BAD_RELOC;
3319 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
3321 // This cannot be a BLX.
3323 return This::STATUS_BAD_RELOC;
3325 else if (r_type == elfcpp::R_ARM_THM_XPC22)
3327 // Check for Thumb to Thumb call.
3329 return This::STATUS_BAD_RELOC;
3332 gold_warning(_("%s: Thumb BLX instruction targets "
3333 "thumb function '%s'."),
3334 object->name().c_str(),
3335 (gsym ? gsym->name() : "(local)"));
3336 // Convert BLX to BL.
3337 lower_insn |= 0x1000U;
3343 // A branch to an undefined weak symbol is turned into a jump to
3344 // the next instruction unless a PLT entry will be created.
3345 // The jump to the next instruction is optimized as a NOP.W for
3346 // Thumb-2 enabled architectures.
3347 const Target_arm<big_endian>* arm_target =
3348 Target_arm<big_endian>::default_target();
3349 if (is_weakly_undefined_without_plt)
3351 if (arm_target->may_use_thumb2_nop())
3353 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
3354 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
3358 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
3359 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
3361 return This::STATUS_OKAY;
3364 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
3365 Arm_address branch_target = psymval->value(object, addend);
3366 int32_t branch_offset = branch_target - address;
3368 // We need a stub if the branch offset is too large or if we need
3370 bool may_use_blx = arm_target->may_use_blx();
3371 bool thumb2 = arm_target->using_thumb2();
3373 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3374 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3376 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3377 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3378 || ((thumb_bit == 0)
3379 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3380 || r_type == elfcpp::R_ARM_THM_JUMP24)))
3382 Stub_type stub_type =
3383 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
3385 if (stub_type != arm_stub_none)
3387 Stub_table<big_endian>* stub_table =
3388 object->stub_table(relinfo->data_shndx);
3389 gold_assert(stub_table != NULL);
3391 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3392 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
3393 gold_assert(stub != NULL);
3394 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3395 branch_target = stub_table->address() + stub->offset() + addend;
3396 branch_offset = branch_target - address;
3400 // At this point, if we still need to switch mode, the instruction
3401 // must either be a BLX or a BL that can be converted to a BLX.
3404 gold_assert(may_use_blx
3405 && (r_type == elfcpp::R_ARM_THM_CALL
3406 || r_type == elfcpp::R_ARM_THM_XPC22));
3407 // Make sure this is a BLX.
3408 lower_insn &= ~0x1000U;
3412 // Make sure this is a BL.
3413 lower_insn |= 0x1000U;
3416 if ((lower_insn & 0x5000U) == 0x4000U)
3417 // For a BLX instruction, make sure that the relocation is rounded up
3418 // to a word boundary. This follows the semantics of the instruction
3419 // which specifies that bit 1 of the target address will come from bit
3420 // 1 of the base address.
3421 branch_offset = (branch_offset + 2) & ~3;
3423 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3424 // We use the Thumb-2 encoding, which is safe even if dealing with
3425 // a Thumb-1 instruction by virtue of our overflow check above. */
3426 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
3427 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
3429 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3430 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3433 ? utils::has_overflow<25>(branch_offset)
3434 : utils::has_overflow<23>(branch_offset))
3435 ? This::STATUS_OVERFLOW
3436 : This::STATUS_OKAY);
3439 // Relocate THUMB-2 long conditional branches.
3440 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3441 // undefined and we do not use PLT in this relocation. In such a case,
3442 // the branch is converted into an NOP.
3444 template<bool big_endian>
3445 typename Arm_relocate_functions<big_endian>::Status
3446 Arm_relocate_functions<big_endian>::thm_jump19(
3447 unsigned char *view,
3448 const Arm_relobj<big_endian>* object,
3449 const Symbol_value<32>* psymval,
3450 Arm_address address,
3451 Arm_address thumb_bit)
3453 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3454 Valtype* wv = reinterpret_cast<Valtype*>(view);
3455 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3456 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3457 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
3459 Arm_address branch_target = psymval->value(object, addend);
3460 int32_t branch_offset = branch_target - address;
3462 // ??? Should handle interworking? GCC might someday try to
3463 // use this for tail calls.
3464 // FIXME: We do support thumb entry to PLT yet.
3467 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3468 return This::STATUS_BAD_RELOC;
3471 // Put RELOCATION back into the insn.
3472 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
3473 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
3475 // Put the relocated value back in the object file:
3476 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3477 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3479 return (utils::has_overflow<21>(branch_offset)
3480 ? This::STATUS_OVERFLOW
3481 : This::STATUS_OKAY);
3484 // Get the GOT section, creating it if necessary.
3486 template<bool big_endian>
3487 Output_data_got<32, big_endian>*
3488 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
3490 if (this->got_ == NULL)
3492 gold_assert(symtab != NULL && layout != NULL);
3494 this->got_ = new Output_data_got<32, big_endian>();
3497 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3499 | elfcpp::SHF_WRITE),
3500 this->got_, false, true, true,
3503 // The old GNU linker creates a .got.plt section. We just
3504 // create another set of data in the .got section. Note that we
3505 // always create a PLT if we create a GOT, although the PLT
3507 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
3508 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3510 | elfcpp::SHF_WRITE),
3511 this->got_plt_, false, false,
3514 // The first three entries are reserved.
3515 this->got_plt_->set_current_data_size(3 * 4);
3517 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3518 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3519 Symbol_table::PREDEFINED,
3521 0, 0, elfcpp::STT_OBJECT,
3523 elfcpp::STV_HIDDEN, 0,
3529 // Get the dynamic reloc section, creating it if necessary.
3531 template<bool big_endian>
3532 typename Target_arm<big_endian>::Reloc_section*
3533 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
3535 if (this->rel_dyn_ == NULL)
3537 gold_assert(layout != NULL);
3538 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
3539 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
3540 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
3541 false, false, false);
3543 return this->rel_dyn_;
3546 // Insn_template methods.
3548 // Return byte size of an instruction template.
3551 Insn_template::size() const
3553 switch (this->type())
3556 case THUMB16_SPECIAL_TYPE:
3567 // Return alignment of an instruction template.
3570 Insn_template::alignment() const
3572 switch (this->type())
3575 case THUMB16_SPECIAL_TYPE:
3586 // Stub_template methods.
3588 Stub_template::Stub_template(
3589 Stub_type type, const Insn_template* insns,
3591 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
3592 entry_in_thumb_mode_(false), relocs_()
3596 // Compute byte size and alignment of stub template.
3597 for (size_t i = 0; i < insn_count; i++)
3599 unsigned insn_alignment = insns[i].alignment();
3600 size_t insn_size = insns[i].size();
3601 gold_assert((offset & (insn_alignment - 1)) == 0);
3602 this->alignment_ = std::max(this->alignment_, insn_alignment);
3603 switch (insns[i].type())
3605 case Insn_template::THUMB16_TYPE:
3606 case Insn_template::THUMB16_SPECIAL_TYPE:
3608 this->entry_in_thumb_mode_ = true;
3611 case Insn_template::THUMB32_TYPE:
3612 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
3613 this->relocs_.push_back(Reloc(i, offset));
3615 this->entry_in_thumb_mode_ = true;
3618 case Insn_template::ARM_TYPE:
3619 // Handle cases where the target is encoded within the
3621 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
3622 this->relocs_.push_back(Reloc(i, offset));
3625 case Insn_template::DATA_TYPE:
3626 // Entry point cannot be data.
3627 gold_assert(i != 0);
3628 this->relocs_.push_back(Reloc(i, offset));
3634 offset += insn_size;
3636 this->size_ = offset;
3641 // Template to implement do_write for a specific target endianity.
3643 template<bool big_endian>
3645 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
3647 const Stub_template* stub_template = this->stub_template();
3648 const Insn_template* insns = stub_template->insns();
3650 // FIXME: We do not handle BE8 encoding yet.
3651 unsigned char* pov = view;
3652 for (size_t i = 0; i < stub_template->insn_count(); i++)
3654 switch (insns[i].type())
3656 case Insn_template::THUMB16_TYPE:
3657 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
3659 case Insn_template::THUMB16_SPECIAL_TYPE:
3660 elfcpp::Swap<16, big_endian>::writeval(
3662 this->thumb16_special(i));
3664 case Insn_template::THUMB32_TYPE:
3666 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
3667 uint32_t lo = insns[i].data() & 0xffff;
3668 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
3669 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
3672 case Insn_template::ARM_TYPE:
3673 case Insn_template::DATA_TYPE:
3674 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
3679 pov += insns[i].size();
3681 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
3684 // Reloc_stub::Key methods.
3686 // Dump a Key as a string for debugging.
3689 Reloc_stub::Key::name() const
3691 if (this->r_sym_ == invalid_index)
3693 // Global symbol key name
3694 // <stub-type>:<symbol name>:<addend>.
3695 const std::string sym_name = this->u_.symbol->name();
3696 // We need to print two hex number and two colons. So just add 100 bytes
3697 // to the symbol name size.
3698 size_t len = sym_name.size() + 100;
3699 char* buffer = new char[len];
3700 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
3701 sym_name.c_str(), this->addend_);
3702 gold_assert(c > 0 && c < static_cast<int>(len));
3704 return std::string(buffer);
3708 // local symbol key name
3709 // <stub-type>:<object>:<r_sym>:<addend>.
3710 const size_t len = 200;
3712 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
3713 this->u_.relobj, this->r_sym_, this->addend_);
3714 gold_assert(c > 0 && c < static_cast<int>(len));
3715 return std::string(buffer);
3719 // Reloc_stub methods.
3721 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3722 // LOCATION to DESTINATION.
3723 // This code is based on the arm_type_of_stub function in
3724 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3728 Reloc_stub::stub_type_for_reloc(
3729 unsigned int r_type,
3730 Arm_address location,
3731 Arm_address destination,
3732 bool target_is_thumb)
3734 Stub_type stub_type = arm_stub_none;
3736 // This is a bit ugly but we want to avoid using a templated class for
3737 // big and little endianities.
3739 bool should_force_pic_veneer;
3742 if (parameters->target().is_big_endian())
3744 const Target_arm<true>* big_endian_target =
3745 Target_arm<true>::default_target();
3746 may_use_blx = big_endian_target->may_use_blx();
3747 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
3748 thumb2 = big_endian_target->using_thumb2();
3749 thumb_only = big_endian_target->using_thumb_only();
3753 const Target_arm<false>* little_endian_target =
3754 Target_arm<false>::default_target();
3755 may_use_blx = little_endian_target->may_use_blx();
3756 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
3757 thumb2 = little_endian_target->using_thumb2();
3758 thumb_only = little_endian_target->using_thumb_only();
3761 int64_t branch_offset = (int64_t)destination - location;
3763 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
3765 // Handle cases where:
3766 // - this call goes too far (different Thumb/Thumb2 max
3768 // - it's a Thumb->Arm call and blx is not available, or it's a
3769 // Thumb->Arm branch (not bl). A stub is needed in this case.
3771 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3772 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3774 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3775 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3776 || ((!target_is_thumb)
3777 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3778 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
3780 if (target_is_thumb)
3785 stub_type = (parameters->options().shared()
3786 || should_force_pic_veneer)
3789 && (r_type == elfcpp::R_ARM_THM_CALL))
3790 // V5T and above. Stub starts with ARM code, so
3791 // we must be able to switch mode before
3792 // reaching it, which is only possible for 'bl'
3793 // (ie R_ARM_THM_CALL relocation).
3794 ? arm_stub_long_branch_any_thumb_pic
3795 // On V4T, use Thumb code only.
3796 : arm_stub_long_branch_v4t_thumb_thumb_pic)
3800 && (r_type == elfcpp::R_ARM_THM_CALL))
3801 ? arm_stub_long_branch_any_any // V5T and above.
3802 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
3806 stub_type = (parameters->options().shared()
3807 || should_force_pic_veneer)
3808 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
3809 : arm_stub_long_branch_thumb_only; // non-PIC stub.
3816 // FIXME: We should check that the input section is from an
3817 // object that has interwork enabled.
3819 stub_type = (parameters->options().shared()
3820 || should_force_pic_veneer)
3823 && (r_type == elfcpp::R_ARM_THM_CALL))
3824 ? arm_stub_long_branch_any_arm_pic // V5T and above.
3825 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
3829 && (r_type == elfcpp::R_ARM_THM_CALL))
3830 ? arm_stub_long_branch_any_any // V5T and above.
3831 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
3833 // Handle v4t short branches.
3834 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3835 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3836 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3837 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3841 else if (r_type == elfcpp::R_ARM_CALL
3842 || r_type == elfcpp::R_ARM_JUMP24
3843 || r_type == elfcpp::R_ARM_PLT32)
3845 if (target_is_thumb)
3849 // FIXME: We should check that the input section is from an
3850 // object that has interwork enabled.
3852 // We have an extra 2-bytes reach because of
3853 // the mode change (bit 24 (H) of BLX encoding).
3854 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3855 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3856 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
3857 || (r_type == elfcpp::R_ARM_JUMP24)
3858 || (r_type == elfcpp::R_ARM_PLT32))
3860 stub_type = (parameters->options().shared()
3861 || should_force_pic_veneer)
3864 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
3865 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
3869 ? arm_stub_long_branch_any_any // V5T and above.
3870 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
3876 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3877 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3879 stub_type = (parameters->options().shared()
3880 || should_force_pic_veneer)
3881 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
3882 : arm_stub_long_branch_any_any; /// non-PIC.
3890 // Cortex_a8_stub methods.
3892 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3893 // I is the position of the instruction template in the stub template.
3896 Cortex_a8_stub::do_thumb16_special(size_t i)
3898 // The only use of this is to copy condition code from a conditional
3899 // branch being worked around to the corresponding conditional branch in
3901 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3903 uint16_t data = this->stub_template()->insns()[i].data();
3904 gold_assert((data & 0xff00U) == 0xd000U);
3905 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
3909 // Stub_factory methods.
3911 Stub_factory::Stub_factory()
3913 // The instruction template sequences are declared as static
3914 // objects and initialized first time the constructor runs.
3916 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3917 // to reach the stub if necessary.
3918 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
3920 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3921 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3922 // dcd R_ARM_ABS32(X)
3925 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3927 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
3929 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3930 Insn_template::arm_insn(0xe12fff1c), // bx ip
3931 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3932 // dcd R_ARM_ABS32(X)
3935 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3936 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
3938 Insn_template::thumb16_insn(0xb401), // push {r0}
3939 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3940 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3941 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3942 Insn_template::thumb16_insn(0x4760), // bx ip
3943 Insn_template::thumb16_insn(0xbf00), // nop
3944 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3945 // dcd R_ARM_ABS32(X)
3948 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3950 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
3952 Insn_template::thumb16_insn(0x4778), // bx pc
3953 Insn_template::thumb16_insn(0x46c0), // nop
3954 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3955 Insn_template::arm_insn(0xe12fff1c), // bx ip
3956 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3957 // dcd R_ARM_ABS32(X)
3960 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3962 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
3964 Insn_template::thumb16_insn(0x4778), // bx pc
3965 Insn_template::thumb16_insn(0x46c0), // nop
3966 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3967 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3968 // dcd R_ARM_ABS32(X)
3971 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3972 // one, when the destination is close enough.
3973 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
3975 Insn_template::thumb16_insn(0x4778), // bx pc
3976 Insn_template::thumb16_insn(0x46c0), // nop
3977 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3980 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3981 // blx to reach the stub if necessary.
3982 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
3984 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3985 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3986 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3987 // dcd R_ARM_REL32(X-4)
3990 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3991 // blx to reach the stub if necessary. We can not add into pc;
3992 // it is not guaranteed to mode switch (different in ARMv6 and
3994 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
3996 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3997 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3998 Insn_template::arm_insn(0xe12fff1c), // bx ip
3999 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4000 // dcd R_ARM_REL32(X)
4003 // V4T ARM -> ARM long branch stub, PIC.
4004 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4006 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4007 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4008 Insn_template::arm_insn(0xe12fff1c), // bx ip
4009 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4010 // dcd R_ARM_REL32(X)
4013 // V4T Thumb -> ARM long branch stub, PIC.
4014 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4016 Insn_template::thumb16_insn(0x4778), // bx pc
4017 Insn_template::thumb16_insn(0x46c0), // nop
4018 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4019 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4020 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4021 // dcd R_ARM_REL32(X)
4024 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4026 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4028 Insn_template::thumb16_insn(0xb401), // push {r0}
4029 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4030 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4031 Insn_template::thumb16_insn(0x4484), // add ip, r0
4032 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4033 Insn_template::thumb16_insn(0x4760), // bx ip
4034 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4035 // dcd R_ARM_REL32(X)
4038 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4040 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4042 Insn_template::thumb16_insn(0x4778), // bx pc
4043 Insn_template::thumb16_insn(0x46c0), // nop
4044 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4045 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4046 Insn_template::arm_insn(0xe12fff1c), // bx ip
4047 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4048 // dcd R_ARM_REL32(X)
4051 // Cortex-A8 erratum-workaround stubs.
4053 // Stub used for conditional branches (which may be beyond +/-1MB away,
4054 // so we can't use a conditional branch to reach this stub).
4061 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4063 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4064 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4065 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4069 // Stub used for b.w and bl.w instructions.
4071 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4073 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4076 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4078 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4081 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4082 // instruction (which switches to ARM mode) to point to this stub. Jump to
4083 // the real destination using an ARM-mode branch.
4084 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
4086 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4089 // Stub used to provide an interworking for R_ARM_V4BX relocation
4090 // (bx r[n] instruction).
4091 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4093 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4094 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4095 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4098 // Fill in the stub template look-up table. Stub templates are constructed
4099 // per instance of Stub_factory for fast look-up without locking
4100 // in a thread-enabled environment.
4102 this->stub_templates_[arm_stub_none] =
4103 new Stub_template(arm_stub_none, NULL, 0);
4105 #define DEF_STUB(x) \
4109 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4110 Stub_type type = arm_stub_##x; \
4111 this->stub_templates_[type] = \
4112 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4120 // Stub_table methods.
4122 // Removel all Cortex-A8 stub.
4124 template<bool big_endian>
4126 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4128 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4129 p != this->cortex_a8_stubs_.end();
4132 this->cortex_a8_stubs_.clear();
4135 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4137 template<bool big_endian>
4139 Stub_table<big_endian>::relocate_stub(
4141 const Relocate_info<32, big_endian>* relinfo,
4142 Target_arm<big_endian>* arm_target,
4143 Output_section* output_section,
4144 unsigned char* view,
4145 Arm_address address,
4146 section_size_type view_size)
4148 const Stub_template* stub_template = stub->stub_template();
4149 if (stub_template->reloc_count() != 0)
4151 // Adjust view to cover the stub only.
4152 section_size_type offset = stub->offset();
4153 section_size_type stub_size = stub_template->size();
4154 gold_assert(offset + stub_size <= view_size);
4156 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4157 address + offset, stub_size);
4161 // Relocate all stubs in this stub table.
4163 template<bool big_endian>
4165 Stub_table<big_endian>::relocate_stubs(
4166 const Relocate_info<32, big_endian>* relinfo,
4167 Target_arm<big_endian>* arm_target,
4168 Output_section* output_section,
4169 unsigned char* view,
4170 Arm_address address,
4171 section_size_type view_size)
4173 // If we are passed a view bigger than the stub table's. we need to
4175 gold_assert(address == this->address()
4177 == static_cast<section_size_type>(this->data_size())));
4179 // Relocate all relocation stubs.
4180 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4181 p != this->reloc_stubs_.end();
4183 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4184 address, view_size);
4186 // Relocate all Cortex-A8 stubs.
4187 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4188 p != this->cortex_a8_stubs_.end();
4190 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4191 address, view_size);
4193 // Relocate all ARM V4BX stubs.
4194 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4195 p != this->arm_v4bx_stubs_.end();
4199 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4200 address, view_size);
4204 // Write out the stubs to file.
4206 template<bool big_endian>
4208 Stub_table<big_endian>::do_write(Output_file* of)
4210 off_t offset = this->offset();
4211 const section_size_type oview_size =
4212 convert_to_section_size_type(this->data_size());
4213 unsigned char* const oview = of->get_output_view(offset, oview_size);
4215 // Write relocation stubs.
4216 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4217 p != this->reloc_stubs_.end();
4220 Reloc_stub* stub = p->second;
4221 Arm_address address = this->address() + stub->offset();
4223 == align_address(address,
4224 stub->stub_template()->alignment()));
4225 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4229 // Write Cortex-A8 stubs.
4230 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4231 p != this->cortex_a8_stubs_.end();
4234 Cortex_a8_stub* stub = p->second;
4235 Arm_address address = this->address() + stub->offset();
4237 == align_address(address,
4238 stub->stub_template()->alignment()));
4239 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4243 // Write ARM V4BX relocation stubs.
4244 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4245 p != this->arm_v4bx_stubs_.end();
4251 Arm_address address = this->address() + (*p)->offset();
4253 == align_address(address,
4254 (*p)->stub_template()->alignment()));
4255 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4259 of->write_output_view(this->offset(), oview_size, oview);
4262 // Update the data size and address alignment of the stub table at the end
4263 // of a relaxation pass. Return true if either the data size or the
4264 // alignment changed in this relaxation pass.
4266 template<bool big_endian>
4268 Stub_table<big_endian>::update_data_size_and_addralign()
4271 unsigned addralign = 1;
4273 // Go over all stubs in table to compute data size and address alignment.
4275 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4276 p != this->reloc_stubs_.end();
4279 const Stub_template* stub_template = p->second->stub_template();
4280 addralign = std::max(addralign, stub_template->alignment());
4281 size = (align_address(size, stub_template->alignment())
4282 + stub_template->size());
4285 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4286 p != this->cortex_a8_stubs_.end();
4289 const Stub_template* stub_template = p->second->stub_template();
4290 addralign = std::max(addralign, stub_template->alignment());
4291 size = (align_address(size, stub_template->alignment())
4292 + stub_template->size());
4295 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4296 p != this->arm_v4bx_stubs_.end();
4302 const Stub_template* stub_template = (*p)->stub_template();
4303 addralign = std::max(addralign, stub_template->alignment());
4304 size = (align_address(size, stub_template->alignment())
4305 + stub_template->size());
4308 // Check if either data size or alignment changed in this pass.
4309 // Update prev_data_size_ and prev_addralign_. These will be used
4310 // as the current data size and address alignment for the next pass.
4311 bool changed = size != this->prev_data_size_;
4312 this->prev_data_size_ = size;
4314 if (addralign != this->prev_addralign_)
4316 this->prev_addralign_ = addralign;
4321 // Finalize the stubs. This sets the offsets of the stubs within the stub
4322 // table. It also marks all input sections needing Cortex-A8 workaround.
4324 template<bool big_endian>
4326 Stub_table<big_endian>::finalize_stubs()
4329 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4330 p != this->reloc_stubs_.end();
4333 Reloc_stub* stub = p->second;
4334 const Stub_template* stub_template = stub->stub_template();
4335 uint64_t stub_addralign = stub_template->alignment();
4336 off = align_address(off, stub_addralign);
4337 stub->set_offset(off);
4338 off += stub_template->size();
4341 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4342 p != this->cortex_a8_stubs_.end();
4345 Cortex_a8_stub* stub = p->second;
4346 const Stub_template* stub_template = stub->stub_template();
4347 uint64_t stub_addralign = stub_template->alignment();
4348 off = align_address(off, stub_addralign);
4349 stub->set_offset(off);
4350 off += stub_template->size();
4352 // Mark input section so that we can determine later if a code section
4353 // needs the Cortex-A8 workaround quickly.
4354 Arm_relobj<big_endian>* arm_relobj =
4355 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
4356 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
4359 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4360 p != this->arm_v4bx_stubs_.end();
4366 const Stub_template* stub_template = (*p)->stub_template();
4367 uint64_t stub_addralign = stub_template->alignment();
4368 off = align_address(off, stub_addralign);
4369 (*p)->set_offset(off);
4370 off += stub_template->size();
4373 gold_assert(off <= this->prev_data_size_);
4376 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4377 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4378 // of the address range seen by the linker.
4380 template<bool big_endian>
4382 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
4383 Target_arm<big_endian>* arm_target,
4384 unsigned char* view,
4385 Arm_address view_address,
4386 section_size_type view_size)
4388 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4389 for (Cortex_a8_stub_list::const_iterator p =
4390 this->cortex_a8_stubs_.lower_bound(view_address);
4391 ((p != this->cortex_a8_stubs_.end())
4392 && (p->first < (view_address + view_size)));
4395 // We do not store the THUMB bit in the LSB of either the branch address
4396 // or the stub offset. There is no need to strip the LSB.
4397 Arm_address branch_address = p->first;
4398 const Cortex_a8_stub* stub = p->second;
4399 Arm_address stub_address = this->address() + stub->offset();
4401 // Offset of the branch instruction relative to this view.
4402 section_size_type offset =
4403 convert_to_section_size_type(branch_address - view_address);
4404 gold_assert((offset + 4) <= view_size);
4406 arm_target->apply_cortex_a8_workaround(stub, stub_address,
4407 view + offset, branch_address);
4411 // Arm_input_section methods.
4413 // Initialize an Arm_input_section.
4415 template<bool big_endian>
4417 Arm_input_section<big_endian>::init()
4419 Relobj* relobj = this->relobj();
4420 unsigned int shndx = this->shndx();
4422 // Cache these to speed up size and alignment queries. It is too slow
4423 // to call section_addraglin and section_size every time.
4424 this->original_addralign_ = relobj->section_addralign(shndx);
4425 this->original_size_ = relobj->section_size(shndx);
4427 // We want to make this look like the original input section after
4428 // output sections are finalized.
4429 Output_section* os = relobj->output_section(shndx);
4430 off_t offset = relobj->output_section_offset(shndx);
4431 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
4432 this->set_address(os->address() + offset);
4433 this->set_file_offset(os->offset() + offset);
4435 this->set_current_data_size(this->original_size_);
4436 this->finalize_data_size();
4439 template<bool big_endian>
4441 Arm_input_section<big_endian>::do_write(Output_file* of)
4443 // We have to write out the original section content.
4444 section_size_type section_size;
4445 const unsigned char* section_contents =
4446 this->relobj()->section_contents(this->shndx(), §ion_size, false);
4447 of->write(this->offset(), section_contents, section_size);
4449 // If this owns a stub table and it is not empty, write it.
4450 if (this->is_stub_table_owner() && !this->stub_table_->empty())
4451 this->stub_table_->write(of);
4454 // Finalize data size.
4456 template<bool big_endian>
4458 Arm_input_section<big_endian>::set_final_data_size()
4460 // If this owns a stub table, finalize its data size as well.
4461 if (this->is_stub_table_owner())
4463 uint64_t address = this->address();
4465 // The stub table comes after the original section contents.
4466 address += this->original_size_;
4467 address = align_address(address, this->stub_table_->addralign());
4468 off_t offset = this->offset() + (address - this->address());
4469 this->stub_table_->set_address_and_file_offset(address, offset);
4470 address += this->stub_table_->data_size();
4471 gold_assert(address == this->address() + this->current_data_size());
4474 this->set_data_size(this->current_data_size());
4477 // Reset address and file offset.
4479 template<bool big_endian>
4481 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
4483 // Size of the original input section contents.
4484 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4486 // If this is a stub table owner, account for the stub table size.
4487 if (this->is_stub_table_owner())
4489 Stub_table<big_endian>* stub_table = this->stub_table_;
4491 // Reset the stub table's address and file offset. The
4492 // current data size for child will be updated after that.
4493 stub_table_->reset_address_and_file_offset();
4494 off = align_address(off, stub_table_->addralign());
4495 off += stub_table->current_data_size();
4498 this->set_current_data_size(off);
4501 // Arm_exidx_cantunwind methods.
4503 // Write this to Output file OF for a fixed endianity.
4505 template<bool big_endian>
4507 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
4509 off_t offset = this->offset();
4510 const section_size_type oview_size = 8;
4511 unsigned char* const oview = of->get_output_view(offset, oview_size);
4513 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4514 Valtype* wv = reinterpret_cast<Valtype*>(oview);
4516 Output_section* os = this->relobj_->output_section(this->shndx_);
4517 gold_assert(os != NULL);
4519 Arm_relobj<big_endian>* arm_relobj =
4520 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
4521 Arm_address output_offset =
4522 arm_relobj->get_output_section_offset(this->shndx_);
4523 Arm_address section_start;
4524 if(output_offset != Arm_relobj<big_endian>::invalid_address)
4525 section_start = os->address() + output_offset;
4528 // Currently this only happens for a relaxed section.
4529 const Output_relaxed_input_section* poris =
4530 os->find_relaxed_input_section(this->relobj_, this->shndx_);
4531 gold_assert(poris != NULL);
4532 section_start = poris->address();
4535 // We always append this to the end of an EXIDX section.
4536 Arm_address output_address =
4537 section_start + this->relobj_->section_size(this->shndx_);
4539 // Write out the entry. The first word either points to the beginning
4540 // or after the end of a text section. The second word is the special
4541 // EXIDX_CANTUNWIND value.
4542 elfcpp::Swap<32, big_endian>::writeval(wv, output_address);
4543 elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
4545 of->write_output_view(this->offset(), oview_size, oview);
4548 // Arm_exidx_merged_section methods.
4550 // Constructor for Arm_exidx_merged_section.
4551 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
4552 // SECTION_OFFSET_MAP points to a section offset map describing how
4553 // parts of the input section are mapped to output. DELETED_BYTES is
4554 // the number of bytes deleted from the EXIDX input section.
4556 Arm_exidx_merged_section::Arm_exidx_merged_section(
4557 const Arm_exidx_input_section& exidx_input_section,
4558 const Arm_exidx_section_offset_map& section_offset_map,
4559 uint32_t deleted_bytes)
4560 : Output_relaxed_input_section(exidx_input_section.relobj(),
4561 exidx_input_section.shndx(),
4562 exidx_input_section.addralign()),
4563 exidx_input_section_(exidx_input_section),
4564 section_offset_map_(section_offset_map)
4566 // Fix size here so that we do not need to implement set_final_data_size.
4567 this->set_data_size(exidx_input_section.size() - deleted_bytes);
4568 this->fix_data_size();
4571 // Given an input OBJECT, an input section index SHNDX within that
4572 // object, and an OFFSET relative to the start of that input
4573 // section, return whether or not the corresponding offset within
4574 // the output section is known. If this function returns true, it
4575 // sets *POUTPUT to the output offset. The value -1 indicates that
4576 // this input offset is being discarded.
4579 Arm_exidx_merged_section::do_output_offset(
4580 const Relobj* relobj,
4582 section_offset_type offset,
4583 section_offset_type* poutput) const
4585 // We only handle offsets for the original EXIDX input section.
4586 if (relobj != this->exidx_input_section_.relobj()
4587 || shndx != this->exidx_input_section_.shndx())
4590 if (offset < 0 || offset >= this->exidx_input_section_.size())
4591 // Input offset is out of valid range.
4595 // We need to look up the section offset map to determine the output
4596 // offset. Find the reference point in map that is first offset
4597 // bigger than or equal to this offset.
4598 Arm_exidx_section_offset_map::const_iterator p =
4599 this->section_offset_map_.lower_bound(offset);
4601 // The section offset maps are build such that this should not happen if
4602 // input offset is in the valid range.
4603 gold_assert(p != this->section_offset_map_.end());
4605 // We need to check if this is dropped.
4606 section_offset_type ref = p->first;
4607 section_offset_type mapped_ref = p->second;
4609 if (mapped_ref != Arm_exidx_input_section::invalid_offset)
4610 // Offset is present in output.
4611 *poutput = mapped_ref + (offset - ref);
4613 // Offset is discarded owing to EXIDX entry merging.
4620 // Write this to output file OF.
4623 Arm_exidx_merged_section::do_write(Output_file* of)
4625 // If we retain or discard the whole EXIDX input section, we would
4627 gold_assert(this->data_size() != this->exidx_input_section_.size()
4628 && this->data_size() != 0);
4630 off_t offset = this->offset();
4631 const section_size_type oview_size = this->data_size();
4632 unsigned char* const oview = of->get_output_view(offset, oview_size);
4634 Output_section* os = this->relobj()->output_section(this->shndx());
4635 gold_assert(os != NULL);
4637 // Get contents of EXIDX input section.
4638 section_size_type section_size;
4639 const unsigned char* section_contents =
4640 this->relobj()->section_contents(this->shndx(), §ion_size, false);
4641 gold_assert(section_size == this->exidx_input_section_.size());
4643 // Go over spans of input offsets and write only those that are not
4645 section_offset_type in_start = 0;
4646 section_offset_type out_start = 0;
4647 for(Arm_exidx_section_offset_map::const_iterator p =
4648 this->section_offset_map_.begin();
4649 p != this->section_offset_map_.end();
4652 section_offset_type in_end = p->first;
4653 gold_assert(in_end >= in_start);
4654 section_offset_type out_end = p->second;
4655 size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
4658 size_t out_chunk_size =
4659 convert_types<size_t>(out_end - out_start + 1);
4660 gold_assert(out_chunk_size == in_chunk_size);
4661 memcpy(oview + out_start, section_contents + in_start,
4663 out_start += out_chunk_size;
4665 in_start += in_chunk_size;
4668 gold_assert(convert_to_section_size_type(out_start) == oview_size);
4669 of->write_output_view(this->offset(), oview_size, oview);
4672 // Arm_exidx_fixup methods.
4674 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
4675 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
4676 // points to the end of the last seen EXIDX section.
4679 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
4681 if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
4682 && this->last_input_section_ != NULL)
4684 Relobj* relobj = this->last_input_section_->relobj();
4685 unsigned int shndx = this->last_input_section_->shndx();
4686 Arm_exidx_cantunwind* cantunwind =
4687 new Arm_exidx_cantunwind(relobj, shndx);
4688 this->exidx_output_section_->add_output_section_data(cantunwind);
4689 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
4693 // Process an EXIDX section entry in input. Return whether this entry
4694 // can be deleted in the output. SECOND_WORD in the second word of the
4698 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
4701 if (second_word == elfcpp::EXIDX_CANTUNWIND)
4703 // Merge if previous entry is also an EXIDX_CANTUNWIND.
4704 delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
4705 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
4707 else if ((second_word & 0x80000000) != 0)
4709 // Inlined unwinding data. Merge if equal to previous.
4710 delete_entry = (this->last_unwind_type_ == UT_INLINED_ENTRY
4711 && this->last_inlined_entry_ == second_word);
4712 this->last_unwind_type_ = UT_INLINED_ENTRY;
4713 this->last_inlined_entry_ = second_word;
4717 // Normal table entry. In theory we could merge these too,
4718 // but duplicate entries are likely to be much less common.
4719 delete_entry = false;
4720 this->last_unwind_type_ = UT_NORMAL_ENTRY;
4722 return delete_entry;
4725 // Update the current section offset map during EXIDX section fix-up.
4726 // If there is no map, create one. INPUT_OFFSET is the offset of a
4727 // reference point, DELETED_BYTES is the number of deleted by in the
4728 // section so far. If DELETE_ENTRY is true, the reference point and
4729 // all offsets after the previous reference point are discarded.
4732 Arm_exidx_fixup::update_offset_map(
4733 section_offset_type input_offset,
4734 section_size_type deleted_bytes,
4737 if (this->section_offset_map_ == NULL)
4738 this->section_offset_map_ = new Arm_exidx_section_offset_map();
4739 section_offset_type output_offset = (delete_entry
4741 : input_offset - deleted_bytes);
4742 (*this->section_offset_map_)[input_offset] = output_offset;
4745 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
4746 // bytes deleted. If some entries are merged, also store a pointer to a newly
4747 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
4748 // caller owns the map and is responsible for releasing it after use.
4750 template<bool big_endian>
4752 Arm_exidx_fixup::process_exidx_section(
4753 const Arm_exidx_input_section* exidx_input_section,
4754 Arm_exidx_section_offset_map** psection_offset_map)
4756 Relobj* relobj = exidx_input_section->relobj();
4757 unsigned shndx = exidx_input_section->shndx();
4758 section_size_type section_size;
4759 const unsigned char* section_contents =
4760 relobj->section_contents(shndx, §ion_size, false);
4762 if ((section_size % 8) != 0)
4764 // Something is wrong with this section. Better not touch it.
4765 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
4766 relobj->name().c_str(), shndx);
4767 this->last_input_section_ = exidx_input_section;
4768 this->last_unwind_type_ = UT_NONE;
4772 uint32_t deleted_bytes = 0;
4773 bool prev_delete_entry = false;
4774 gold_assert(this->section_offset_map_ == NULL);
4776 for (section_size_type i = 0; i < section_size; i += 8)
4778 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4780 reinterpret_cast<const Valtype*>(section_contents + i + 4);
4781 uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
4783 bool delete_entry = this->process_exidx_entry(second_word);
4785 // Entry deletion causes changes in output offsets. We use a std::map
4786 // to record these. And entry (x, y) means input offset x
4787 // is mapped to output offset y. If y is invalid_offset, then x is
4788 // dropped in the output. Because of the way std::map::lower_bound
4789 // works, we record the last offset in a region w.r.t to keeping or
4790 // dropping. If there is no entry (x0, y0) for an input offset x0,
4791 // the output offset y0 of it is determined by the output offset y1 of
4792 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
4793 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
4795 if (delete_entry != prev_delete_entry && i != 0)
4796 this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
4798 // Update total deleted bytes for this entry.
4802 prev_delete_entry = delete_entry;
4805 // If section offset map is not NULL, make an entry for the end of
4807 if (this->section_offset_map_ != NULL)
4808 update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
4810 *psection_offset_map = this->section_offset_map_;
4811 this->section_offset_map_ = NULL;
4812 this->last_input_section_ = exidx_input_section;
4814 return deleted_bytes;
4817 // Arm_output_section methods.
4819 // Create a stub group for input sections from BEGIN to END. OWNER
4820 // points to the input section to be the owner a new stub table.
4822 template<bool big_endian>
4824 Arm_output_section<big_endian>::create_stub_group(
4825 Input_section_list::const_iterator begin,
4826 Input_section_list::const_iterator end,
4827 Input_section_list::const_iterator owner,
4828 Target_arm<big_endian>* target,
4829 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
4831 // Currently we convert ordinary input sections into relaxed sections only
4832 // at this point but we may want to support creating relaxed input section
4833 // very early. So we check here to see if owner is already a relaxed
4836 Arm_input_section<big_endian>* arm_input_section;
4837 if (owner->is_relaxed_input_section())
4840 Arm_input_section<big_endian>::as_arm_input_section(
4841 owner->relaxed_input_section());
4845 gold_assert(owner->is_input_section());
4846 // Create a new relaxed input section.
4848 target->new_arm_input_section(owner->relobj(), owner->shndx());
4849 new_relaxed_sections->push_back(arm_input_section);
4852 // Create a stub table.
4853 Stub_table<big_endian>* stub_table =
4854 target->new_stub_table(arm_input_section);
4856 arm_input_section->set_stub_table(stub_table);
4858 Input_section_list::const_iterator p = begin;
4859 Input_section_list::const_iterator prev_p;
4861 // Look for input sections or relaxed input sections in [begin ... end].
4864 if (p->is_input_section() || p->is_relaxed_input_section())
4866 // The stub table information for input sections live
4867 // in their objects.
4868 Arm_relobj<big_endian>* arm_relobj =
4869 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
4870 arm_relobj->set_stub_table(p->shndx(), stub_table);
4874 while (prev_p != end);
4877 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
4878 // of stub groups. We grow a stub group by adding input section until the
4879 // size is just below GROUP_SIZE. The last input section will be converted
4880 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
4881 // input section after the stub table, effectively double the group size.
4883 // This is similar to the group_sections() function in elf32-arm.c but is
4884 // implemented differently.
4886 template<bool big_endian>
4888 Arm_output_section<big_endian>::group_sections(
4889 section_size_type group_size,
4890 bool stubs_always_after_branch,
4891 Target_arm<big_endian>* target)
4893 // We only care about sections containing code.
4894 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
4897 // States for grouping.
4900 // No group is being built.
4902 // A group is being built but the stub table is not found yet.
4903 // We keep group a stub group until the size is just under GROUP_SIZE.
4904 // The last input section in the group will be used as the stub table.
4905 FINDING_STUB_SECTION,
4906 // A group is being built and we have already found a stub table.
4907 // We enter this state to grow a stub group by adding input section
4908 // after the stub table. This effectively doubles the group size.
4912 // Any newly created relaxed sections are stored here.
4913 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
4915 State state = NO_GROUP;
4916 section_size_type off = 0;
4917 section_size_type group_begin_offset = 0;
4918 section_size_type group_end_offset = 0;
4919 section_size_type stub_table_end_offset = 0;
4920 Input_section_list::const_iterator group_begin =
4921 this->input_sections().end();
4922 Input_section_list::const_iterator stub_table =
4923 this->input_sections().end();
4924 Input_section_list::const_iterator group_end = this->input_sections().end();
4925 for (Input_section_list::const_iterator p = this->input_sections().begin();
4926 p != this->input_sections().end();
4929 section_size_type section_begin_offset =
4930 align_address(off, p->addralign());
4931 section_size_type section_end_offset =
4932 section_begin_offset + p->data_size();
4934 // Check to see if we should group the previously seens sections.
4940 case FINDING_STUB_SECTION:
4941 // Adding this section makes the group larger than GROUP_SIZE.
4942 if (section_end_offset - group_begin_offset >= group_size)
4944 if (stubs_always_after_branch)
4946 gold_assert(group_end != this->input_sections().end());
4947 this->create_stub_group(group_begin, group_end, group_end,
4948 target, &new_relaxed_sections);
4953 // But wait, there's more! Input sections up to
4954 // stub_group_size bytes after the stub table can be
4955 // handled by it too.
4956 state = HAS_STUB_SECTION;
4957 stub_table = group_end;
4958 stub_table_end_offset = group_end_offset;
4963 case HAS_STUB_SECTION:
4964 // Adding this section makes the post stub-section group larger
4966 if (section_end_offset - stub_table_end_offset >= group_size)
4968 gold_assert(group_end != this->input_sections().end());
4969 this->create_stub_group(group_begin, group_end, stub_table,
4970 target, &new_relaxed_sections);
4979 // If we see an input section and currently there is no group, start
4980 // a new one. Skip any empty sections.
4981 if ((p->is_input_section() || p->is_relaxed_input_section())
4982 && (p->relobj()->section_size(p->shndx()) != 0))
4984 if (state == NO_GROUP)
4986 state = FINDING_STUB_SECTION;
4988 group_begin_offset = section_begin_offset;
4991 // Keep track of the last input section seen.
4993 group_end_offset = section_end_offset;
4996 off = section_end_offset;
4999 // Create a stub group for any ungrouped sections.
5000 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5002 gold_assert(group_end != this->input_sections().end());
5003 this->create_stub_group(group_begin, group_end,
5004 (state == FINDING_STUB_SECTION
5007 target, &new_relaxed_sections);
5010 // Convert input section into relaxed input section in a batch.
5011 if (!new_relaxed_sections.empty())
5012 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5014 // Update the section offsets
5015 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5017 Arm_relobj<big_endian>* arm_relobj =
5018 Arm_relobj<big_endian>::as_arm_relobj(
5019 new_relaxed_sections[i]->relobj());
5020 unsigned int shndx = new_relaxed_sections[i]->shndx();
5021 // Tell Arm_relobj that this input section is converted.
5022 arm_relobj->convert_input_section_to_relaxed_section(shndx);
5026 // Arm_relobj methods.
5028 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5029 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5031 template<bool big_endian>
5033 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
5034 const elfcpp::Shdr<32, big_endian>& shdr,
5035 const Relobj::Output_sections& out_sections,
5036 const Symbol_table *symtab)
5038 unsigned int sh_type = shdr.get_sh_type();
5039 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
5042 // Ignore empty section.
5043 off_t sh_size = shdr.get_sh_size();
5047 // Ignore reloc section with bad info. This error will be
5048 // reported in the final link.
5049 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5050 if (index >= this->shnum())
5053 // This relocation section is against a section which we
5054 // discarded or if the section is folded into another
5055 // section due to ICF.
5056 if (out_sections[index] == NULL || symtab->is_section_folded(this, index))
5059 // Ignore reloc section with unexpected symbol table. The
5060 // error will be reported in the final link.
5061 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
5064 unsigned int reloc_size;
5065 if (sh_type == elfcpp::SHT_REL)
5066 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5068 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5070 // Ignore reloc section with unexpected entsize or uneven size.
5071 // The error will be reported in the final link.
5072 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
5078 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
5079 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5081 template<bool big_endian>
5083 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
5084 const elfcpp::Shdr<32, big_endian>& shdr,
5087 const Symbol_table* symtab)
5089 // We only scan non-empty code sections.
5090 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) == 0
5091 || shdr.get_sh_size() == 0)
5094 // Ignore discarded or ICF'ed sections.
5095 if (os == NULL || symtab->is_section_folded(this, shndx))
5098 // Find output address of section.
5099 Arm_address address = os->output_address(this, shndx, 0);
5101 // If the section does not cross any 4K-boundaries, it does not need to
5103 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
5109 // Scan a section for Cortex-A8 workaround.
5111 template<bool big_endian>
5113 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
5114 const elfcpp::Shdr<32, big_endian>& shdr,
5117 Target_arm<big_endian>* arm_target)
5119 Arm_address output_address = os->output_address(this, shndx, 0);
5121 // Get the section contents.
5122 section_size_type input_view_size = 0;
5123 const unsigned char* input_view =
5124 this->section_contents(shndx, &input_view_size, false);
5126 // We need to go through the mapping symbols to determine what to
5127 // scan. There are two reasons. First, we should look at THUMB code and
5128 // THUMB code only. Second, we only want to look at the 4K-page boundary
5129 // to speed up the scanning.
5131 // Look for the first mapping symbol in this section. It should be
5133 Mapping_symbol_position section_start(shndx, 0);
5134 typename Mapping_symbols_info::const_iterator p =
5135 this->mapping_symbols_info_.lower_bound(section_start);
5137 if (p == this->mapping_symbols_info_.end()
5138 || p->first != section_start)
5140 gold_warning(_("Cortex-A8 erratum scanning failed because there "
5141 "is no mapping symbols for section %u of %s"),
5142 shndx, this->name().c_str());
5146 while (p != this->mapping_symbols_info_.end()
5147 && p->first.first == shndx)
5149 typename Mapping_symbols_info::const_iterator next =
5150 this->mapping_symbols_info_.upper_bound(p->first);
5152 // Only scan part of a section with THUMB code.
5153 if (p->second == 't')
5155 // Determine the end of this range.
5156 section_size_type span_start =
5157 convert_to_section_size_type(p->first.second);
5158 section_size_type span_end;
5159 if (next != this->mapping_symbols_info_.end()
5160 && next->first.first == shndx)
5161 span_end = convert_to_section_size_type(next->first.second);
5163 span_end = convert_to_section_size_type(shdr.get_sh_size());
5165 if (((span_start + output_address) & ~0xfffUL)
5166 != ((span_end + output_address - 1) & ~0xfffUL))
5168 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
5169 span_start, span_end,
5179 // Scan relocations for stub generation.
5181 template<bool big_endian>
5183 Arm_relobj<big_endian>::scan_sections_for_stubs(
5184 Target_arm<big_endian>* arm_target,
5185 const Symbol_table* symtab,
5186 const Layout* layout)
5188 unsigned int shnum = this->shnum();
5189 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5191 // Read the section headers.
5192 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
5196 // To speed up processing, we set up hash tables for fast lookup of
5197 // input offsets to output addresses.
5198 this->initialize_input_to_output_maps();
5200 const Relobj::Output_sections& out_sections(this->output_sections());
5202 Relocate_info<32, big_endian> relinfo;
5203 relinfo.symtab = symtab;
5204 relinfo.layout = layout;
5205 relinfo.object = this;
5207 // Do relocation stubs scanning.
5208 const unsigned char* p = pshdrs + shdr_size;
5209 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5211 const elfcpp::Shdr<32, big_endian> shdr(p);
5212 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab))
5214 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5215 Arm_address output_offset = this->get_output_section_offset(index);
5216 Arm_address output_address;
5217 if(output_offset != invalid_address)
5218 output_address = out_sections[index]->address() + output_offset;
5221 // Currently this only happens for a relaxed section.
5222 const Output_relaxed_input_section* poris =
5223 out_sections[index]->find_relaxed_input_section(this, index);
5224 gold_assert(poris != NULL);
5225 output_address = poris->address();
5228 // Get the relocations.
5229 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
5233 // Get the section contents. This does work for the case in which
5234 // we modify the contents of an input section. We need to pass the
5235 // output view under such circumstances.
5236 section_size_type input_view_size = 0;
5237 const unsigned char* input_view =
5238 this->section_contents(index, &input_view_size, false);
5240 relinfo.reloc_shndx = i;
5241 relinfo.data_shndx = index;
5242 unsigned int sh_type = shdr.get_sh_type();
5243 unsigned int reloc_size;
5244 if (sh_type == elfcpp::SHT_REL)
5245 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5247 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5249 Output_section* os = out_sections[index];
5250 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
5251 shdr.get_sh_size() / reloc_size,
5253 output_offset == invalid_address,
5254 input_view, output_address,
5259 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
5260 // after its relocation section, if there is one, is processed for
5261 // relocation stubs. Merging this loop with the one above would have been
5262 // complicated since we would have had to make sure that relocation stub
5263 // scanning is done first.
5264 if (arm_target->fix_cortex_a8())
5266 const unsigned char* p = pshdrs + shdr_size;
5267 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5269 const elfcpp::Shdr<32, big_endian> shdr(p);
5270 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
5273 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
5278 // After we've done the relocations, we release the hash tables,
5279 // since we no longer need them.
5280 this->free_input_to_output_maps();
5283 // Count the local symbols. The ARM backend needs to know if a symbol
5284 // is a THUMB function or not. For global symbols, it is easy because
5285 // the Symbol object keeps the ELF symbol type. For local symbol it is
5286 // harder because we cannot access this information. So we override the
5287 // do_count_local_symbol in parent and scan local symbols to mark
5288 // THUMB functions. This is not the most efficient way but I do not want to
5289 // slow down other ports by calling a per symbol targer hook inside
5290 // Sized_relobj<size, big_endian>::do_count_local_symbols.
5292 template<bool big_endian>
5294 Arm_relobj<big_endian>::do_count_local_symbols(
5295 Stringpool_template<char>* pool,
5296 Stringpool_template<char>* dynpool)
5298 // We need to fix-up the values of any local symbols whose type are
5301 // Ask parent to count the local symbols.
5302 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
5303 const unsigned int loccount = this->local_symbol_count();
5307 // Intialize the thumb function bit-vector.
5308 std::vector<bool> empty_vector(loccount, false);
5309 this->local_symbol_is_thumb_function_.swap(empty_vector);
5311 // Read the symbol table section header.
5312 const unsigned int symtab_shndx = this->symtab_shndx();
5313 elfcpp::Shdr<32, big_endian>
5314 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
5315 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
5317 // Read the local symbols.
5318 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
5319 gold_assert(loccount == symtabshdr.get_sh_info());
5320 off_t locsize = loccount * sym_size;
5321 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
5322 locsize, true, true);
5324 // For mapping symbol processing, we need to read the symbol names.
5325 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
5326 if (strtab_shndx >= this->shnum())
5328 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
5332 elfcpp::Shdr<32, big_endian>
5333 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
5334 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
5336 this->error(_("symbol table name section has wrong type: %u"),
5337 static_cast<unsigned int>(strtabshdr.get_sh_type()));
5340 const char* pnames =
5341 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
5342 strtabshdr.get_sh_size(),
5345 // Loop over the local symbols and mark any local symbols pointing
5346 // to THUMB functions.
5348 // Skip the first dummy symbol.
5350 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
5351 this->local_values();
5352 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
5354 elfcpp::Sym<32, big_endian> sym(psyms);
5355 elfcpp::STT st_type = sym.get_st_type();
5356 Symbol_value<32>& lv((*plocal_values)[i]);
5357 Arm_address input_value = lv.input_value();
5359 // Check to see if this is a mapping symbol.
5360 const char* sym_name = pnames + sym.get_st_name();
5361 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
5363 unsigned int input_shndx = sym.get_st_shndx();
5365 // Strip of LSB in case this is a THUMB symbol.
5366 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
5367 this->mapping_symbols_info_[msp] = sym_name[1];
5370 if (st_type == elfcpp::STT_ARM_TFUNC
5371 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
5373 // This is a THUMB function. Mark this and canonicalize the
5374 // symbol value by setting LSB.
5375 this->local_symbol_is_thumb_function_[i] = true;
5376 if ((input_value & 1) == 0)
5377 lv.set_input_value(input_value | 1);
5382 // Relocate sections.
5383 template<bool big_endian>
5385 Arm_relobj<big_endian>::do_relocate_sections(
5386 const Symbol_table* symtab,
5387 const Layout* layout,
5388 const unsigned char* pshdrs,
5389 typename Sized_relobj<32, big_endian>::Views* pviews)
5391 // Call parent to relocate sections.
5392 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
5395 // We do not generate stubs if doing a relocatable link.
5396 if (parameters->options().relocatable())
5399 // Relocate stub tables.
5400 unsigned int shnum = this->shnum();
5402 Target_arm<big_endian>* arm_target =
5403 Target_arm<big_endian>::default_target();
5405 Relocate_info<32, big_endian> relinfo;
5406 relinfo.symtab = symtab;
5407 relinfo.layout = layout;
5408 relinfo.object = this;
5410 for (unsigned int i = 1; i < shnum; ++i)
5412 Arm_input_section<big_endian>* arm_input_section =
5413 arm_target->find_arm_input_section(this, i);
5415 if (arm_input_section != NULL
5416 && arm_input_section->is_stub_table_owner()
5417 && !arm_input_section->stub_table()->empty())
5419 // We cannot discard a section if it owns a stub table.
5420 Output_section* os = this->output_section(i);
5421 gold_assert(os != NULL);
5423 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
5424 relinfo.reloc_shdr = NULL;
5425 relinfo.data_shndx = i;
5426 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
5428 gold_assert((*pviews)[i].view != NULL);
5430 // We are passed the output section view. Adjust it to cover the
5432 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
5433 gold_assert((stub_table->address() >= (*pviews)[i].address)
5434 && ((stub_table->address() + stub_table->data_size())
5435 <= (*pviews)[i].address + (*pviews)[i].view_size));
5437 off_t offset = stub_table->address() - (*pviews)[i].address;
5438 unsigned char* view = (*pviews)[i].view + offset;
5439 Arm_address address = stub_table->address();
5440 section_size_type view_size = stub_table->data_size();
5442 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
5446 // Apply Cortex A8 workaround if applicable.
5447 if (this->section_has_cortex_a8_workaround(i))
5449 unsigned char* view = (*pviews)[i].view;
5450 Arm_address view_address = (*pviews)[i].address;
5451 section_size_type view_size = (*pviews)[i].view_size;
5452 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
5454 // Adjust view to cover section.
5455 Output_section* os = this->output_section(i);
5456 gold_assert(os != NULL);
5457 Arm_address section_address = os->output_address(this, i, 0);
5458 uint64_t section_size = this->section_size(i);
5460 gold_assert(section_address >= view_address
5461 && ((section_address + section_size)
5462 <= (view_address + view_size)));
5464 unsigned char* section_view = view + (section_address - view_address);
5466 // Apply the Cortex-A8 workaround to the output address range
5467 // corresponding to this input section.
5468 stub_table->apply_cortex_a8_workaround_to_address_range(
5477 // Create a new EXIDX input section object for EXIDX section SHNDX with
5480 template<bool big_endian>
5482 Arm_relobj<big_endian>::make_exidx_input_section(
5484 const elfcpp::Shdr<32, big_endian>& shdr)
5486 // Link .text section to its .ARM.exidx section in the same object.
5487 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
5489 // Issue an error and ignore this EXIDX section if it does not point
5490 // to any text section.
5491 if (text_shndx == elfcpp::SHN_UNDEF)
5493 gold_error(_("EXIDX section %u in %s has no linked text section"),
5494 shndx, this->name().c_str());
5498 // Issue an error and ignore this EXIDX section if it points to a text
5499 // section already has an EXIDX section.
5500 if (this->exidx_section_map_[text_shndx] != NULL)
5502 gold_error(_("EXIDX sections %u and %u both link to text section %u "
5504 shndx, this->exidx_section_map_[text_shndx]->shndx(),
5505 text_shndx, this->name().c_str());
5509 // Create an Arm_exidx_input_section object for this EXIDX section.
5510 Arm_exidx_input_section* exidx_input_section =
5511 new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
5512 shdr.get_sh_addralign());
5513 this->exidx_section_map_[text_shndx] = exidx_input_section;
5515 // Also map the EXIDX section index to this.
5516 gold_assert(this->exidx_section_map_[shndx] == NULL);
5517 this->exidx_section_map_[shndx] = exidx_input_section;
5520 // Read the symbol information.
5522 template<bool big_endian>
5524 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
5526 // Call parent class to read symbol information.
5527 Sized_relobj<32, big_endian>::do_read_symbols(sd);
5529 // Read processor-specific flags in ELF file header.
5530 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
5531 elfcpp::Elf_sizes<32>::ehdr_size,
5533 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
5534 this->processor_specific_flags_ = ehdr.get_e_flags();
5536 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
5538 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5539 const unsigned char *ps =
5540 sd->section_headers->data() + shdr_size;
5541 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
5543 elfcpp::Shdr<32, big_endian> shdr(ps);
5544 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
5546 gold_assert(this->attributes_section_data_ == NULL);
5547 section_offset_type section_offset = shdr.get_sh_offset();
5548 section_size_type section_size =
5549 convert_to_section_size_type(shdr.get_sh_size());
5550 File_view* view = this->get_lasting_view(section_offset,
5551 section_size, true, false);
5552 this->attributes_section_data_ =
5553 new Attributes_section_data(view->data(), section_size);
5555 else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
5556 this->make_exidx_input_section(i, shdr);
5560 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
5561 // sections for unwinding. These sections are referenced implicitly by
5562 // text sections linked in the section headers. If we ignore these implict
5563 // references, the .ARM.exidx sections and any .ARM.extab sections they use
5564 // will be garbage-collected incorrectly. Hence we override the same function
5565 // in the base class to handle these implicit references.
5567 template<bool big_endian>
5569 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
5571 Read_relocs_data* rd)
5573 // First, call base class method to process relocations in this object.
5574 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
5576 unsigned int shnum = this->shnum();
5577 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5578 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
5582 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
5583 // to these from the linked text sections.
5584 const unsigned char* ps = pshdrs + shdr_size;
5585 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
5587 elfcpp::Shdr<32, big_endian> shdr(ps);
5588 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
5590 // Found an .ARM.exidx section, add it to the set of reachable
5591 // sections from its linked text section.
5592 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
5593 symtab->gc()->add_reference(this, text_shndx, this, i);
5598 // Arm_dynobj methods.
5600 // Read the symbol information.
5602 template<bool big_endian>
5604 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
5606 // Call parent class to read symbol information.
5607 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
5609 // Read processor-specific flags in ELF file header.
5610 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
5611 elfcpp::Elf_sizes<32>::ehdr_size,
5613 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
5614 this->processor_specific_flags_ = ehdr.get_e_flags();
5616 // Read the attributes section if there is one.
5617 // We read from the end because gas seems to put it near the end of
5618 // the section headers.
5619 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5620 const unsigned char *ps =
5621 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
5622 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
5624 elfcpp::Shdr<32, big_endian> shdr(ps);
5625 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
5627 section_offset_type section_offset = shdr.get_sh_offset();
5628 section_size_type section_size =
5629 convert_to_section_size_type(shdr.get_sh_size());
5630 File_view* view = this->get_lasting_view(section_offset,
5631 section_size, true, false);
5632 this->attributes_section_data_ =
5633 new Attributes_section_data(view->data(), section_size);
5639 // Stub_addend_reader methods.
5641 // Read the addend of a REL relocation of type R_TYPE at VIEW.
5643 template<bool big_endian>
5644 elfcpp::Elf_types<32>::Elf_Swxword
5645 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
5646 unsigned int r_type,
5647 const unsigned char* view,
5648 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
5650 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
5654 case elfcpp::R_ARM_CALL:
5655 case elfcpp::R_ARM_JUMP24:
5656 case elfcpp::R_ARM_PLT32:
5658 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5659 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
5660 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
5661 return utils::sign_extend<26>(val << 2);
5664 case elfcpp::R_ARM_THM_CALL:
5665 case elfcpp::R_ARM_THM_JUMP24:
5666 case elfcpp::R_ARM_THM_XPC22:
5668 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
5669 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
5670 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
5671 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
5672 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
5675 case elfcpp::R_ARM_THM_JUMP19:
5677 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
5678 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
5679 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
5680 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
5681 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
5689 // A class to handle the PLT data.
5691 template<bool big_endian>
5692 class Output_data_plt_arm : public Output_section_data
5695 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
5698 Output_data_plt_arm(Layout*, Output_data_space*);
5700 // Add an entry to the PLT.
5702 add_entry(Symbol* gsym);
5704 // Return the .rel.plt section data.
5705 const Reloc_section*
5707 { return this->rel_; }
5711 do_adjust_output_section(Output_section* os);
5713 // Write to a map file.
5715 do_print_to_mapfile(Mapfile* mapfile) const
5716 { mapfile->print_output_data(this, _("** PLT")); }
5719 // Template for the first PLT entry.
5720 static const uint32_t first_plt_entry[5];
5722 // Template for subsequent PLT entries.
5723 static const uint32_t plt_entry[3];
5725 // Set the final size.
5727 set_final_data_size()
5729 this->set_data_size(sizeof(first_plt_entry)
5730 + this->count_ * sizeof(plt_entry));
5733 // Write out the PLT data.
5735 do_write(Output_file*);
5737 // The reloc section.
5738 Reloc_section* rel_;
5739 // The .got.plt section.
5740 Output_data_space* got_plt_;
5741 // The number of PLT entries.
5742 unsigned int count_;
5745 // Create the PLT section. The ordinary .got section is an argument,
5746 // since we need to refer to the start. We also create our own .got
5747 // section just for PLT entries.
5749 template<bool big_endian>
5750 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
5751 Output_data_space* got_plt)
5752 : Output_section_data(4), got_plt_(got_plt), count_(0)
5754 this->rel_ = new Reloc_section(false);
5755 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
5756 elfcpp::SHF_ALLOC, this->rel_, true, false,
5760 template<bool big_endian>
5762 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
5767 // Add an entry to the PLT.
5769 template<bool big_endian>
5771 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
5773 gold_assert(!gsym->has_plt_offset());
5775 // Note that when setting the PLT offset we skip the initial
5776 // reserved PLT entry.
5777 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
5778 + sizeof(first_plt_entry));
5782 section_offset_type got_offset = this->got_plt_->current_data_size();
5784 // Every PLT entry needs a GOT entry which points back to the PLT
5785 // entry (this will be changed by the dynamic linker, normally
5786 // lazily when the function is called).
5787 this->got_plt_->set_current_data_size(got_offset + 4);
5789 // Every PLT entry needs a reloc.
5790 gsym->set_needs_dynsym_entry();
5791 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
5794 // Note that we don't need to save the symbol. The contents of the
5795 // PLT are independent of which symbols are used. The symbols only
5796 // appear in the relocations.
5800 // FIXME: This is not very flexible. Right now this has only been tested
5801 // on armv5te. If we are to support additional architecture features like
5802 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
5804 // The first entry in the PLT.
5805 template<bool big_endian>
5806 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
5808 0xe52de004, // str lr, [sp, #-4]!
5809 0xe59fe004, // ldr lr, [pc, #4]
5810 0xe08fe00e, // add lr, pc, lr
5811 0xe5bef008, // ldr pc, [lr, #8]!
5812 0x00000000, // &GOT[0] - .
5815 // Subsequent entries in the PLT.
5817 template<bool big_endian>
5818 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
5820 0xe28fc600, // add ip, pc, #0xNN00000
5821 0xe28cca00, // add ip, ip, #0xNN000
5822 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
5825 // Write out the PLT. This uses the hand-coded instructions above,
5826 // and adjusts them as needed. This is all specified by the arm ELF
5827 // Processor Supplement.
5829 template<bool big_endian>
5831 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
5833 const off_t offset = this->offset();
5834 const section_size_type oview_size =
5835 convert_to_section_size_type(this->data_size());
5836 unsigned char* const oview = of->get_output_view(offset, oview_size);
5838 const off_t got_file_offset = this->got_plt_->offset();
5839 const section_size_type got_size =
5840 convert_to_section_size_type(this->got_plt_->data_size());
5841 unsigned char* const got_view = of->get_output_view(got_file_offset,
5843 unsigned char* pov = oview;
5845 Arm_address plt_address = this->address();
5846 Arm_address got_address = this->got_plt_->address();
5848 // Write first PLT entry. All but the last word are constants.
5849 const size_t num_first_plt_words = (sizeof(first_plt_entry)
5850 / sizeof(plt_entry[0]));
5851 for (size_t i = 0; i < num_first_plt_words - 1; i++)
5852 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
5853 // Last word in first PLT entry is &GOT[0] - .
5854 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
5855 got_address - (plt_address + 16));
5856 pov += sizeof(first_plt_entry);
5858 unsigned char* got_pov = got_view;
5860 memset(got_pov, 0, 12);
5863 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
5864 unsigned int plt_offset = sizeof(first_plt_entry);
5865 unsigned int plt_rel_offset = 0;
5866 unsigned int got_offset = 12;
5867 const unsigned int count = this->count_;
5868 for (unsigned int i = 0;
5871 pov += sizeof(plt_entry),
5873 plt_offset += sizeof(plt_entry),
5874 plt_rel_offset += rel_size,
5877 // Set and adjust the PLT entry itself.
5878 int32_t offset = ((got_address + got_offset)
5879 - (plt_address + plt_offset + 8));
5881 gold_assert(offset >= 0 && offset < 0x0fffffff);
5882 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
5883 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
5884 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
5885 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
5886 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
5887 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
5889 // Set the entry in the GOT.
5890 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
5893 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
5894 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
5896 of->write_output_view(offset, oview_size, oview);
5897 of->write_output_view(got_file_offset, got_size, got_view);
5900 // Create a PLT entry for a global symbol.
5902 template<bool big_endian>
5904 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
5907 if (gsym->has_plt_offset())
5910 if (this->plt_ == NULL)
5912 // Create the GOT sections first.
5913 this->got_section(symtab, layout);
5915 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
5916 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
5918 | elfcpp::SHF_EXECINSTR),
5919 this->plt_, false, false, false, false);
5921 this->plt_->add_entry(gsym);
5924 // Report an unsupported relocation against a local symbol.
5926 template<bool big_endian>
5928 Target_arm<big_endian>::Scan::unsupported_reloc_local(
5929 Sized_relobj<32, big_endian>* object,
5930 unsigned int r_type)
5932 gold_error(_("%s: unsupported reloc %u against local symbol"),
5933 object->name().c_str(), r_type);
5936 // We are about to emit a dynamic relocation of type R_TYPE. If the
5937 // dynamic linker does not support it, issue an error. The GNU linker
5938 // only issues a non-PIC error for an allocated read-only section.
5939 // Here we know the section is allocated, but we don't know that it is
5940 // read-only. But we check for all the relocation types which the
5941 // glibc dynamic linker supports, so it seems appropriate to issue an
5942 // error even if the section is not read-only.
5944 template<bool big_endian>
5946 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
5947 unsigned int r_type)
5951 // These are the relocation types supported by glibc for ARM.
5952 case elfcpp::R_ARM_RELATIVE:
5953 case elfcpp::R_ARM_COPY:
5954 case elfcpp::R_ARM_GLOB_DAT:
5955 case elfcpp::R_ARM_JUMP_SLOT:
5956 case elfcpp::R_ARM_ABS32:
5957 case elfcpp::R_ARM_ABS32_NOI:
5958 case elfcpp::R_ARM_PC24:
5959 // FIXME: The following 3 types are not supported by Android's dynamic
5961 case elfcpp::R_ARM_TLS_DTPMOD32:
5962 case elfcpp::R_ARM_TLS_DTPOFF32:
5963 case elfcpp::R_ARM_TLS_TPOFF32:
5967 // This prevents us from issuing more than one error per reloc
5968 // section. But we can still wind up issuing more than one
5969 // error per object file.
5970 if (this->issued_non_pic_error_)
5972 object->error(_("requires unsupported dynamic reloc; "
5973 "recompile with -fPIC"));
5974 this->issued_non_pic_error_ = true;
5977 case elfcpp::R_ARM_NONE:
5982 // Scan a relocation for a local symbol.
5983 // FIXME: This only handles a subset of relocation types used by Android
5984 // on ARM v5te devices.
5986 template<bool big_endian>
5988 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
5991 Sized_relobj<32, big_endian>* object,
5992 unsigned int data_shndx,
5993 Output_section* output_section,
5994 const elfcpp::Rel<32, big_endian>& reloc,
5995 unsigned int r_type,
5996 const elfcpp::Sym<32, big_endian>&)
5998 r_type = get_real_reloc_type(r_type);
6001 case elfcpp::R_ARM_NONE:
6004 case elfcpp::R_ARM_ABS32:
6005 case elfcpp::R_ARM_ABS32_NOI:
6006 // If building a shared library (or a position-independent
6007 // executable), we need to create a dynamic relocation for
6008 // this location. The relocation applied at link time will
6009 // apply the link-time value, so we flag the location with
6010 // an R_ARM_RELATIVE relocation so the dynamic loader can
6011 // relocate it easily.
6012 if (parameters->options().output_is_position_independent())
6014 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6015 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
6016 // If we are to add more other reloc types than R_ARM_ABS32,
6017 // we need to add check_non_pic(object, r_type) here.
6018 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
6019 output_section, data_shndx,
6020 reloc.get_r_offset());
6024 case elfcpp::R_ARM_REL32:
6025 case elfcpp::R_ARM_THM_CALL:
6026 case elfcpp::R_ARM_CALL:
6027 case elfcpp::R_ARM_PREL31:
6028 case elfcpp::R_ARM_JUMP24:
6029 case elfcpp::R_ARM_THM_JUMP24:
6030 case elfcpp::R_ARM_THM_JUMP19:
6031 case elfcpp::R_ARM_PLT32:
6032 case elfcpp::R_ARM_THM_ABS5:
6033 case elfcpp::R_ARM_ABS8:
6034 case elfcpp::R_ARM_ABS12:
6035 case elfcpp::R_ARM_ABS16:
6036 case elfcpp::R_ARM_BASE_ABS:
6037 case elfcpp::R_ARM_MOVW_ABS_NC:
6038 case elfcpp::R_ARM_MOVT_ABS:
6039 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6040 case elfcpp::R_ARM_THM_MOVT_ABS:
6041 case elfcpp::R_ARM_MOVW_PREL_NC:
6042 case elfcpp::R_ARM_MOVT_PREL:
6043 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6044 case elfcpp::R_ARM_THM_MOVT_PREL:
6045 case elfcpp::R_ARM_THM_JUMP6:
6046 case elfcpp::R_ARM_THM_JUMP8:
6047 case elfcpp::R_ARM_THM_JUMP11:
6048 case elfcpp::R_ARM_V4BX:
6051 case elfcpp::R_ARM_GOTOFF32:
6052 // We need a GOT section:
6053 target->got_section(symtab, layout);
6056 case elfcpp::R_ARM_BASE_PREL:
6057 // FIXME: What about this?
6060 case elfcpp::R_ARM_GOT_BREL:
6061 case elfcpp::R_ARM_GOT_PREL:
6063 // The symbol requires a GOT entry.
6064 Output_data_got<32, big_endian>* got =
6065 target->got_section(symtab, layout);
6066 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
6067 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
6069 // If we are generating a shared object, we need to add a
6070 // dynamic RELATIVE relocation for this symbol's GOT entry.
6071 if (parameters->options().output_is_position_independent())
6073 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6074 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
6075 rel_dyn->add_local_relative(
6076 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
6077 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
6083 case elfcpp::R_ARM_TARGET1:
6084 // This should have been mapped to another type already.
6086 case elfcpp::R_ARM_COPY:
6087 case elfcpp::R_ARM_GLOB_DAT:
6088 case elfcpp::R_ARM_JUMP_SLOT:
6089 case elfcpp::R_ARM_RELATIVE:
6090 // These are relocations which should only be seen by the
6091 // dynamic linker, and should never be seen here.
6092 gold_error(_("%s: unexpected reloc %u in object file"),
6093 object->name().c_str(), r_type);
6097 unsupported_reloc_local(object, r_type);
6102 // Report an unsupported relocation against a global symbol.
6104 template<bool big_endian>
6106 Target_arm<big_endian>::Scan::unsupported_reloc_global(
6107 Sized_relobj<32, big_endian>* object,
6108 unsigned int r_type,
6111 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6112 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6115 // Scan a relocation for a global symbol.
6116 // FIXME: This only handles a subset of relocation types used by Android
6117 // on ARM v5te devices.
6119 template<bool big_endian>
6121 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
6124 Sized_relobj<32, big_endian>* object,
6125 unsigned int data_shndx,
6126 Output_section* output_section,
6127 const elfcpp::Rel<32, big_endian>& reloc,
6128 unsigned int r_type,
6131 r_type = get_real_reloc_type(r_type);
6134 case elfcpp::R_ARM_NONE:
6137 case elfcpp::R_ARM_ABS32:
6138 case elfcpp::R_ARM_ABS32_NOI:
6140 // Make a dynamic relocation if necessary.
6141 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
6143 if (target->may_need_copy_reloc(gsym))
6145 target->copy_reloc(symtab, layout, object,
6146 data_shndx, output_section, gsym, reloc);
6148 else if (gsym->can_use_relative_reloc(false))
6150 // If we are to add more other reloc types than R_ARM_ABS32,
6151 // we need to add check_non_pic(object, r_type) here.
6152 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6153 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
6154 output_section, object,
6155 data_shndx, reloc.get_r_offset());
6159 // If we are to add more other reloc types than R_ARM_ABS32,
6160 // we need to add check_non_pic(object, r_type) here.
6161 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6162 rel_dyn->add_global(gsym, r_type, output_section, object,
6163 data_shndx, reloc.get_r_offset());
6169 case elfcpp::R_ARM_MOVW_ABS_NC:
6170 case elfcpp::R_ARM_MOVT_ABS:
6171 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6172 case elfcpp::R_ARM_THM_MOVT_ABS:
6173 case elfcpp::R_ARM_MOVW_PREL_NC:
6174 case elfcpp::R_ARM_MOVT_PREL:
6175 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6176 case elfcpp::R_ARM_THM_MOVT_PREL:
6177 case elfcpp::R_ARM_THM_JUMP6:
6178 case elfcpp::R_ARM_THM_JUMP8:
6179 case elfcpp::R_ARM_THM_JUMP11:
6180 case elfcpp::R_ARM_V4BX:
6183 case elfcpp::R_ARM_THM_ABS5:
6184 case elfcpp::R_ARM_ABS8:
6185 case elfcpp::R_ARM_ABS12:
6186 case elfcpp::R_ARM_ABS16:
6187 case elfcpp::R_ARM_BASE_ABS:
6189 // No dynamic relocs of this kinds.
6190 // Report the error in case of PIC.
6191 int flags = Symbol::NON_PIC_REF;
6192 if (gsym->type() == elfcpp::STT_FUNC
6193 || gsym->type() == elfcpp::STT_ARM_TFUNC)
6194 flags |= Symbol::FUNCTION_CALL;
6195 if (gsym->needs_dynamic_reloc(flags))
6196 check_non_pic(object, r_type);
6200 case elfcpp::R_ARM_REL32:
6201 case elfcpp::R_ARM_PREL31:
6203 // Make a dynamic relocation if necessary.
6204 int flags = Symbol::NON_PIC_REF;
6205 if (gsym->needs_dynamic_reloc(flags))
6207 if (target->may_need_copy_reloc(gsym))
6209 target->copy_reloc(symtab, layout, object,
6210 data_shndx, output_section, gsym, reloc);
6214 check_non_pic(object, r_type);
6215 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6216 rel_dyn->add_global(gsym, r_type, output_section, object,
6217 data_shndx, reloc.get_r_offset());
6223 case elfcpp::R_ARM_JUMP24:
6224 case elfcpp::R_ARM_THM_JUMP24:
6225 case elfcpp::R_ARM_THM_JUMP19:
6226 case elfcpp::R_ARM_CALL:
6227 case elfcpp::R_ARM_THM_CALL:
6229 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
6230 target->make_plt_entry(symtab, layout, gsym);
6233 // Check to see if this is a function that would need a PLT
6234 // but does not get one because the function symbol is untyped.
6235 // This happens in assembly code missing a proper .type directive.
6236 if ((!gsym->is_undefined() || parameters->options().shared())
6237 && !parameters->doing_static_link()
6238 && gsym->type() == elfcpp::STT_NOTYPE
6239 && (gsym->is_from_dynobj()
6240 || gsym->is_undefined()
6241 || gsym->is_preemptible()))
6242 gold_error(_("%s is not a function."),
6243 gsym->demangled_name().c_str());
6247 case elfcpp::R_ARM_PLT32:
6248 // If the symbol is fully resolved, this is just a relative
6249 // local reloc. Otherwise we need a PLT entry.
6250 if (gsym->final_value_is_known())
6252 // If building a shared library, we can also skip the PLT entry
6253 // if the symbol is defined in the output file and is protected
6255 if (gsym->is_defined()
6256 && !gsym->is_from_dynobj()
6257 && !gsym->is_preemptible())
6259 target->make_plt_entry(symtab, layout, gsym);
6262 case elfcpp::R_ARM_GOTOFF32:
6263 // We need a GOT section.
6264 target->got_section(symtab, layout);
6267 case elfcpp::R_ARM_BASE_PREL:
6268 // FIXME: What about this?
6271 case elfcpp::R_ARM_GOT_BREL:
6272 case elfcpp::R_ARM_GOT_PREL:
6274 // The symbol requires a GOT entry.
6275 Output_data_got<32, big_endian>* got =
6276 target->got_section(symtab, layout);
6277 if (gsym->final_value_is_known())
6278 got->add_global(gsym, GOT_TYPE_STANDARD);
6281 // If this symbol is not fully resolved, we need to add a
6282 // GOT entry with a dynamic relocation.
6283 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6284 if (gsym->is_from_dynobj()
6285 || gsym->is_undefined()
6286 || gsym->is_preemptible())
6287 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
6288 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
6291 if (got->add_global(gsym, GOT_TYPE_STANDARD))
6292 rel_dyn->add_global_relative(
6293 gsym, elfcpp::R_ARM_RELATIVE, got,
6294 gsym->got_offset(GOT_TYPE_STANDARD));
6300 case elfcpp::R_ARM_TARGET1:
6301 // This should have been mapped to another type already.
6303 case elfcpp::R_ARM_COPY:
6304 case elfcpp::R_ARM_GLOB_DAT:
6305 case elfcpp::R_ARM_JUMP_SLOT:
6306 case elfcpp::R_ARM_RELATIVE:
6307 // These are relocations which should only be seen by the
6308 // dynamic linker, and should never be seen here.
6309 gold_error(_("%s: unexpected reloc %u in object file"),
6310 object->name().c_str(), r_type);
6314 unsupported_reloc_global(object, r_type, gsym);
6319 // Process relocations for gc.
6321 template<bool big_endian>
6323 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
6325 Sized_relobj<32, big_endian>* object,
6326 unsigned int data_shndx,
6328 const unsigned char* prelocs,
6330 Output_section* output_section,
6331 bool needs_special_offset_handling,
6332 size_t local_symbol_count,
6333 const unsigned char* plocal_symbols)
6335 typedef Target_arm<big_endian> Arm;
6336 typedef typename Target_arm<big_endian>::Scan Scan;
6338 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
6347 needs_special_offset_handling,
6352 // Scan relocations for a section.
6354 template<bool big_endian>
6356 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
6358 Sized_relobj<32, big_endian>* object,
6359 unsigned int data_shndx,
6360 unsigned int sh_type,
6361 const unsigned char* prelocs,
6363 Output_section* output_section,
6364 bool needs_special_offset_handling,
6365 size_t local_symbol_count,
6366 const unsigned char* plocal_symbols)
6368 typedef typename Target_arm<big_endian>::Scan Scan;
6369 if (sh_type == elfcpp::SHT_RELA)
6371 gold_error(_("%s: unsupported RELA reloc section"),
6372 object->name().c_str());
6376 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
6385 needs_special_offset_handling,
6390 // Finalize the sections.
6392 template<bool big_endian>
6394 Target_arm<big_endian>::do_finalize_sections(
6396 const Input_objects* input_objects,
6397 Symbol_table* symtab)
6399 // Merge processor-specific flags.
6400 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6401 p != input_objects->relobj_end();
6404 Arm_relobj<big_endian>* arm_relobj =
6405 Arm_relobj<big_endian>::as_arm_relobj(*p);
6406 this->merge_processor_specific_flags(
6408 arm_relobj->processor_specific_flags());
6409 this->merge_object_attributes(arm_relobj->name().c_str(),
6410 arm_relobj->attributes_section_data());
6414 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
6415 p != input_objects->dynobj_end();
6418 Arm_dynobj<big_endian>* arm_dynobj =
6419 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
6420 this->merge_processor_specific_flags(
6422 arm_dynobj->processor_specific_flags());
6423 this->merge_object_attributes(arm_dynobj->name().c_str(),
6424 arm_dynobj->attributes_section_data());
6428 const Object_attribute* cpu_arch_attr =
6429 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
6430 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
6431 this->set_may_use_blx(true);
6433 // Check if we need to use Cortex-A8 workaround.
6434 if (parameters->options().user_set_fix_cortex_a8())
6435 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
6438 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
6439 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
6441 const Object_attribute* cpu_arch_profile_attr =
6442 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
6443 this->fix_cortex_a8_ =
6444 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
6445 && (cpu_arch_profile_attr->int_value() == 'A'
6446 || cpu_arch_profile_attr->int_value() == 0));
6449 // Check if we can use V4BX interworking.
6450 // The V4BX interworking stub contains BX instruction,
6451 // which is not specified for some profiles.
6452 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
6453 && !this->may_use_blx())
6454 gold_error(_("unable to provide V4BX reloc interworking fix up; "
6455 "the target profile does not support BX instruction"));
6457 // Fill in some more dynamic tags.
6458 const Reloc_section* rel_plt = (this->plt_ == NULL
6460 : this->plt_->rel_plt());
6461 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
6462 this->rel_dyn_, true);
6464 // Emit any relocs we saved in an attempt to avoid generating COPY
6466 if (this->copy_relocs_.any_saved_relocs())
6467 this->copy_relocs_.emit(this->rel_dyn_section(layout));
6469 // Handle the .ARM.exidx section.
6470 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
6471 if (exidx_section != NULL
6472 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
6473 && !parameters->options().relocatable())
6475 // Create __exidx_start and __exdix_end symbols.
6476 symtab->define_in_output_data("__exidx_start", NULL,
6477 Symbol_table::PREDEFINED,
6478 exidx_section, 0, 0, elfcpp::STT_OBJECT,
6479 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
6481 symtab->define_in_output_data("__exidx_end", NULL,
6482 Symbol_table::PREDEFINED,
6483 exidx_section, 0, 0, elfcpp::STT_OBJECT,
6484 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
6487 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
6488 // the .ARM.exidx section.
6489 if (!layout->script_options()->saw_phdrs_clause())
6491 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
6493 Output_segment* exidx_segment =
6494 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
6495 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
6500 // Create an .ARM.attributes section if there is not one already.
6501 Output_attributes_section_data* attributes_section =
6502 new Output_attributes_section_data(*this->attributes_section_data_);
6503 layout->add_output_section_data(".ARM.attributes",
6504 elfcpp::SHT_ARM_ATTRIBUTES, 0,
6505 attributes_section, false, false, false,
6509 // Return whether a direct absolute static relocation needs to be applied.
6510 // In cases where Scan::local() or Scan::global() has created
6511 // a dynamic relocation other than R_ARM_RELATIVE, the addend
6512 // of the relocation is carried in the data, and we must not
6513 // apply the static relocation.
6515 template<bool big_endian>
6517 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
6518 const Sized_symbol<32>* gsym,
6521 Output_section* output_section)
6523 // If the output section is not allocated, then we didn't call
6524 // scan_relocs, we didn't create a dynamic reloc, and we must apply
6526 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
6529 // For local symbols, we will have created a non-RELATIVE dynamic
6530 // relocation only if (a) the output is position independent,
6531 // (b) the relocation is absolute (not pc- or segment-relative), and
6532 // (c) the relocation is not 32 bits wide.
6534 return !(parameters->options().output_is_position_independent()
6535 && (ref_flags & Symbol::ABSOLUTE_REF)
6538 // For global symbols, we use the same helper routines used in the
6539 // scan pass. If we did not create a dynamic relocation, or if we
6540 // created a RELATIVE dynamic relocation, we should apply the static
6542 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
6543 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
6544 && gsym->can_use_relative_reloc(ref_flags
6545 & Symbol::FUNCTION_CALL);
6546 return !has_dyn || is_rel;
6549 // Perform a relocation.
6551 template<bool big_endian>
6553 Target_arm<big_endian>::Relocate::relocate(
6554 const Relocate_info<32, big_endian>* relinfo,
6556 Output_section *output_section,
6558 const elfcpp::Rel<32, big_endian>& rel,
6559 unsigned int r_type,
6560 const Sized_symbol<32>* gsym,
6561 const Symbol_value<32>* psymval,
6562 unsigned char* view,
6563 Arm_address address,
6564 section_size_type /* view_size */ )
6566 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
6568 r_type = get_real_reloc_type(r_type);
6570 const Arm_relobj<big_endian>* object =
6571 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
6573 // If the final branch target of a relocation is THUMB instruction, this
6574 // is 1. Otherwise it is 0.
6575 Arm_address thumb_bit = 0;
6576 Symbol_value<32> symval;
6577 bool is_weakly_undefined_without_plt = false;
6578 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
6582 // This is a global symbol. Determine if we use PLT and if the
6583 // final target is THUMB.
6584 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
6586 // This uses a PLT, change the symbol value.
6587 symval.set_output_value(target->plt_section()->address()
6588 + gsym->plt_offset());
6591 else if (gsym->is_weak_undefined())
6593 // This is a weakly undefined symbol and we do not use PLT
6594 // for this relocation. A branch targeting this symbol will
6595 // be converted into an NOP.
6596 is_weakly_undefined_without_plt = true;
6600 // Set thumb bit if symbol:
6601 // -Has type STT_ARM_TFUNC or
6602 // -Has type STT_FUNC, is defined and with LSB in value set.
6604 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
6605 || (gsym->type() == elfcpp::STT_FUNC
6606 && !gsym->is_undefined()
6607 && ((psymval->value(object, 0) & 1) != 0)))
6614 // This is a local symbol. Determine if the final target is THUMB.
6615 // We saved this information when all the local symbols were read.
6616 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
6617 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6618 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
6623 // This is a fake relocation synthesized for a stub. It does not have
6624 // a real symbol. We just look at the LSB of the symbol value to
6625 // determine if the target is THUMB or not.
6626 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
6629 // Strip LSB if this points to a THUMB target.
6631 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
6632 && ((psymval->value(object, 0) & 1) != 0))
6634 Arm_address stripped_value =
6635 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
6636 symval.set_output_value(stripped_value);
6640 // Get the GOT offset if needed.
6641 // The GOT pointer points to the end of the GOT section.
6642 // We need to subtract the size of the GOT section to get
6643 // the actual offset to use in the relocation.
6644 bool have_got_offset = false;
6645 unsigned int got_offset = 0;
6648 case elfcpp::R_ARM_GOT_BREL:
6649 case elfcpp::R_ARM_GOT_PREL:
6652 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6653 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
6654 - target->got_size());
6658 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
6659 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6660 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
6661 - target->got_size());
6663 have_got_offset = true;
6670 // To look up relocation stubs, we need to pass the symbol table index of
6672 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
6674 typename Arm_relocate_functions::Status reloc_status =
6675 Arm_relocate_functions::STATUS_OKAY;
6678 case elfcpp::R_ARM_NONE:
6681 case elfcpp::R_ARM_ABS8:
6682 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6684 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
6687 case elfcpp::R_ARM_ABS12:
6688 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6690 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
6693 case elfcpp::R_ARM_ABS16:
6694 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6696 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
6699 case elfcpp::R_ARM_ABS32:
6700 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6702 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
6706 case elfcpp::R_ARM_ABS32_NOI:
6707 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6709 // No thumb bit for this relocation: (S + A)
6710 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
6714 case elfcpp::R_ARM_MOVW_ABS_NC:
6715 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6717 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
6721 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
6722 "a shared object; recompile with -fPIC"));
6725 case elfcpp::R_ARM_MOVT_ABS:
6726 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6728 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
6730 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
6731 "a shared object; recompile with -fPIC"));
6734 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6735 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6737 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
6741 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
6742 "making a shared object; recompile with -fPIC"));
6745 case elfcpp::R_ARM_THM_MOVT_ABS:
6746 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6748 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
6751 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
6752 "making a shared object; recompile with -fPIC"));
6755 case elfcpp::R_ARM_MOVW_PREL_NC:
6756 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
6761 case elfcpp::R_ARM_MOVT_PREL:
6762 reloc_status = Arm_relocate_functions::movt_prel(view, object,
6766 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6767 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
6772 case elfcpp::R_ARM_THM_MOVT_PREL:
6773 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
6777 case elfcpp::R_ARM_REL32:
6778 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
6779 address, thumb_bit);
6782 case elfcpp::R_ARM_THM_ABS5:
6783 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6785 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
6788 case elfcpp::R_ARM_THM_CALL:
6790 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
6791 psymval, address, thumb_bit,
6792 is_weakly_undefined_without_plt);
6795 case elfcpp::R_ARM_XPC25:
6797 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
6798 psymval, address, thumb_bit,
6799 is_weakly_undefined_without_plt);
6802 case elfcpp::R_ARM_THM_XPC22:
6804 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
6805 psymval, address, thumb_bit,
6806 is_weakly_undefined_without_plt);
6809 case elfcpp::R_ARM_GOTOFF32:
6811 Arm_address got_origin;
6812 got_origin = target->got_plt_section()->address();
6813 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
6814 got_origin, thumb_bit);
6818 case elfcpp::R_ARM_BASE_PREL:
6821 // Get the addressing origin of the output segment defining the
6822 // symbol gsym (AAELF 4.6.1.2 Relocation types)
6823 gold_assert(gsym != NULL);
6824 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
6825 origin = gsym->output_segment()->vaddr();
6826 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
6827 origin = gsym->output_data()->address();
6830 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6831 _("cannot find origin of R_ARM_BASE_PREL"));
6834 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
6838 case elfcpp::R_ARM_BASE_ABS:
6840 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6845 // Get the addressing origin of the output segment defining
6846 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
6848 // R_ARM_BASE_ABS with the NULL symbol will give the
6849 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
6850 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
6851 origin = target->got_plt_section()->address();
6852 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
6853 origin = gsym->output_segment()->vaddr();
6854 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
6855 origin = gsym->output_data()->address();
6858 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6859 _("cannot find origin of R_ARM_BASE_ABS"));
6863 reloc_status = Arm_relocate_functions::base_abs(view, origin);
6867 case elfcpp::R_ARM_GOT_BREL:
6868 gold_assert(have_got_offset);
6869 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
6872 case elfcpp::R_ARM_GOT_PREL:
6873 gold_assert(have_got_offset);
6874 // Get the address origin for GOT PLT, which is allocated right
6875 // after the GOT section, to calculate an absolute address of
6876 // the symbol GOT entry (got_origin + got_offset).
6877 Arm_address got_origin;
6878 got_origin = target->got_plt_section()->address();
6879 reloc_status = Arm_relocate_functions::got_prel(view,
6880 got_origin + got_offset,
6884 case elfcpp::R_ARM_PLT32:
6885 gold_assert(gsym == NULL
6886 || gsym->has_plt_offset()
6887 || gsym->final_value_is_known()
6888 || (gsym->is_defined()
6889 && !gsym->is_from_dynobj()
6890 && !gsym->is_preemptible()));
6892 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
6893 psymval, address, thumb_bit,
6894 is_weakly_undefined_without_plt);
6897 case elfcpp::R_ARM_CALL:
6899 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
6900 psymval, address, thumb_bit,
6901 is_weakly_undefined_without_plt);
6904 case elfcpp::R_ARM_JUMP24:
6906 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
6907 psymval, address, thumb_bit,
6908 is_weakly_undefined_without_plt);
6911 case elfcpp::R_ARM_THM_JUMP24:
6913 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
6914 psymval, address, thumb_bit,
6915 is_weakly_undefined_without_plt);
6918 case elfcpp::R_ARM_THM_JUMP19:
6920 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
6924 case elfcpp::R_ARM_THM_JUMP6:
6926 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
6929 case elfcpp::R_ARM_THM_JUMP8:
6931 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
6934 case elfcpp::R_ARM_THM_JUMP11:
6936 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
6939 case elfcpp::R_ARM_PREL31:
6940 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
6941 address, thumb_bit);
6944 case elfcpp::R_ARM_V4BX:
6945 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
6947 const bool is_v4bx_interworking =
6948 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
6950 Arm_relocate_functions::v4bx(relinfo, view, object, address,
6951 is_v4bx_interworking);
6955 case elfcpp::R_ARM_TARGET1:
6956 // This should have been mapped to another type already.
6958 case elfcpp::R_ARM_COPY:
6959 case elfcpp::R_ARM_GLOB_DAT:
6960 case elfcpp::R_ARM_JUMP_SLOT:
6961 case elfcpp::R_ARM_RELATIVE:
6962 // These are relocations which should only be seen by the
6963 // dynamic linker, and should never be seen here.
6964 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6965 _("unexpected reloc %u in object file"),
6970 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6971 _("unsupported reloc %u"),
6976 // Report any errors.
6977 switch (reloc_status)
6979 case Arm_relocate_functions::STATUS_OKAY:
6981 case Arm_relocate_functions::STATUS_OVERFLOW:
6982 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6983 _("relocation overflow in relocation %u"),
6986 case Arm_relocate_functions::STATUS_BAD_RELOC:
6987 gold_error_at_location(
6991 _("unexpected opcode while processing relocation %u"),
7001 // Relocate section data.
7003 template<bool big_endian>
7005 Target_arm<big_endian>::relocate_section(
7006 const Relocate_info<32, big_endian>* relinfo,
7007 unsigned int sh_type,
7008 const unsigned char* prelocs,
7010 Output_section* output_section,
7011 bool needs_special_offset_handling,
7012 unsigned char* view,
7013 Arm_address address,
7014 section_size_type view_size,
7015 const Reloc_symbol_changes* reloc_symbol_changes)
7017 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
7018 gold_assert(sh_type == elfcpp::SHT_REL);
7020 Arm_input_section<big_endian>* arm_input_section =
7021 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
7023 // This is an ARM input section and the view covers the whole output
7025 if (arm_input_section != NULL)
7027 gold_assert(needs_special_offset_handling);
7028 Arm_address section_address = arm_input_section->address();
7029 section_size_type section_size = arm_input_section->data_size();
7031 gold_assert((arm_input_section->address() >= address)
7032 && ((arm_input_section->address()
7033 + arm_input_section->data_size())
7034 <= (address + view_size)));
7036 off_t offset = section_address - address;
7039 view_size = section_size;
7042 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
7049 needs_special_offset_handling,
7053 reloc_symbol_changes);
7056 // Return the size of a relocation while scanning during a relocatable
7059 template<bool big_endian>
7061 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
7062 unsigned int r_type,
7065 r_type = get_real_reloc_type(r_type);
7068 case elfcpp::R_ARM_NONE:
7071 case elfcpp::R_ARM_ABS8:
7074 case elfcpp::R_ARM_ABS16:
7075 case elfcpp::R_ARM_THM_ABS5:
7076 case elfcpp::R_ARM_THM_JUMP6:
7077 case elfcpp::R_ARM_THM_JUMP8:
7078 case elfcpp::R_ARM_THM_JUMP11:
7081 case elfcpp::R_ARM_ABS32:
7082 case elfcpp::R_ARM_ABS32_NOI:
7083 case elfcpp::R_ARM_ABS12:
7084 case elfcpp::R_ARM_BASE_ABS:
7085 case elfcpp::R_ARM_REL32:
7086 case elfcpp::R_ARM_THM_CALL:
7087 case elfcpp::R_ARM_GOTOFF32:
7088 case elfcpp::R_ARM_BASE_PREL:
7089 case elfcpp::R_ARM_GOT_BREL:
7090 case elfcpp::R_ARM_GOT_PREL:
7091 case elfcpp::R_ARM_PLT32:
7092 case elfcpp::R_ARM_CALL:
7093 case elfcpp::R_ARM_JUMP24:
7094 case elfcpp::R_ARM_PREL31:
7095 case elfcpp::R_ARM_MOVW_ABS_NC:
7096 case elfcpp::R_ARM_MOVT_ABS:
7097 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7098 case elfcpp::R_ARM_THM_MOVT_ABS:
7099 case elfcpp::R_ARM_MOVW_PREL_NC:
7100 case elfcpp::R_ARM_MOVT_PREL:
7101 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7102 case elfcpp::R_ARM_THM_MOVT_PREL:
7103 case elfcpp::R_ARM_V4BX:
7106 case elfcpp::R_ARM_TARGET1:
7107 // This should have been mapped to another type already.
7109 case elfcpp::R_ARM_COPY:
7110 case elfcpp::R_ARM_GLOB_DAT:
7111 case elfcpp::R_ARM_JUMP_SLOT:
7112 case elfcpp::R_ARM_RELATIVE:
7113 // These are relocations which should only be seen by the
7114 // dynamic linker, and should never be seen here.
7115 gold_error(_("%s: unexpected reloc %u in object file"),
7116 object->name().c_str(), r_type);
7120 object->error(_("unsupported reloc %u in object file"), r_type);
7125 // Scan the relocs during a relocatable link.
7127 template<bool big_endian>
7129 Target_arm<big_endian>::scan_relocatable_relocs(
7130 Symbol_table* symtab,
7132 Sized_relobj<32, big_endian>* object,
7133 unsigned int data_shndx,
7134 unsigned int sh_type,
7135 const unsigned char* prelocs,
7137 Output_section* output_section,
7138 bool needs_special_offset_handling,
7139 size_t local_symbol_count,
7140 const unsigned char* plocal_symbols,
7141 Relocatable_relocs* rr)
7143 gold_assert(sh_type == elfcpp::SHT_REL);
7145 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
7146 Relocatable_size_for_reloc> Scan_relocatable_relocs;
7148 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
7149 Scan_relocatable_relocs>(
7157 needs_special_offset_handling,
7163 // Relocate a section during a relocatable link.
7165 template<bool big_endian>
7167 Target_arm<big_endian>::relocate_for_relocatable(
7168 const Relocate_info<32, big_endian>* relinfo,
7169 unsigned int sh_type,
7170 const unsigned char* prelocs,
7172 Output_section* output_section,
7173 off_t offset_in_output_section,
7174 const Relocatable_relocs* rr,
7175 unsigned char* view,
7176 Arm_address view_address,
7177 section_size_type view_size,
7178 unsigned char* reloc_view,
7179 section_size_type reloc_view_size)
7181 gold_assert(sh_type == elfcpp::SHT_REL);
7183 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
7188 offset_in_output_section,
7197 // Return the value to use for a dynamic symbol which requires special
7198 // treatment. This is how we support equality comparisons of function
7199 // pointers across shared library boundaries, as described in the
7200 // processor specific ABI supplement.
7202 template<bool big_endian>
7204 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
7206 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
7207 return this->plt_section()->address() + gsym->plt_offset();
7210 // Map platform-specific relocs to real relocs
7212 template<bool big_endian>
7214 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
7218 case elfcpp::R_ARM_TARGET1:
7219 // This is either R_ARM_ABS32 or R_ARM_REL32;
7220 return elfcpp::R_ARM_ABS32;
7222 case elfcpp::R_ARM_TARGET2:
7223 // This can be any reloc type but ususally is R_ARM_GOT_PREL
7224 return elfcpp::R_ARM_GOT_PREL;
7231 // Whether if two EABI versions V1 and V2 are compatible.
7233 template<bool big_endian>
7235 Target_arm<big_endian>::are_eabi_versions_compatible(
7236 elfcpp::Elf_Word v1,
7237 elfcpp::Elf_Word v2)
7239 // v4 and v5 are the same spec before and after it was released,
7240 // so allow mixing them.
7241 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
7242 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
7248 // Combine FLAGS from an input object called NAME and the processor-specific
7249 // flags in the ELF header of the output. Much of this is adapted from the
7250 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
7251 // in bfd/elf32-arm.c.
7253 template<bool big_endian>
7255 Target_arm<big_endian>::merge_processor_specific_flags(
7256 const std::string& name,
7257 elfcpp::Elf_Word flags)
7259 if (this->are_processor_specific_flags_set())
7261 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
7263 // Nothing to merge if flags equal to those in output.
7264 if (flags == out_flags)
7267 // Complain about various flag mismatches.
7268 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
7269 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
7270 if (!this->are_eabi_versions_compatible(version1, version2))
7271 gold_error(_("Source object %s has EABI version %d but output has "
7272 "EABI version %d."),
7274 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
7275 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
7279 // If the input is the default architecture and had the default
7280 // flags then do not bother setting the flags for the output
7281 // architecture, instead allow future merges to do this. If no
7282 // future merges ever set these flags then they will retain their
7283 // uninitialised values, which surprise surprise, correspond
7284 // to the default values.
7288 // This is the first time, just copy the flags.
7289 // We only copy the EABI version for now.
7290 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
7294 // Adjust ELF file header.
7295 template<bool big_endian>
7297 Target_arm<big_endian>::do_adjust_elf_header(
7298 unsigned char* view,
7301 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
7303 elfcpp::Ehdr<32, big_endian> ehdr(view);
7304 unsigned char e_ident[elfcpp::EI_NIDENT];
7305 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
7307 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
7308 == elfcpp::EF_ARM_EABI_UNKNOWN)
7309 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
7311 e_ident[elfcpp::EI_OSABI] = 0;
7312 e_ident[elfcpp::EI_ABIVERSION] = 0;
7314 // FIXME: Do EF_ARM_BE8 adjustment.
7316 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
7317 oehdr.put_e_ident(e_ident);
7320 // do_make_elf_object to override the same function in the base class.
7321 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
7322 // to store ARM specific information. Hence we need to have our own
7323 // ELF object creation.
7325 template<bool big_endian>
7327 Target_arm<big_endian>::do_make_elf_object(
7328 const std::string& name,
7329 Input_file* input_file,
7330 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
7332 int et = ehdr.get_e_type();
7333 if (et == elfcpp::ET_REL)
7335 Arm_relobj<big_endian>* obj =
7336 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
7340 else if (et == elfcpp::ET_DYN)
7342 Sized_dynobj<32, big_endian>* obj =
7343 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
7349 gold_error(_("%s: unsupported ELF file type %d"),
7355 // Read the architecture from the Tag_also_compatible_with attribute, if any.
7356 // Returns -1 if no architecture could be read.
7357 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
7359 template<bool big_endian>
7361 Target_arm<big_endian>::get_secondary_compatible_arch(
7362 const Attributes_section_data* pasd)
7364 const Object_attribute *known_attributes =
7365 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
7367 // Note: the tag and its argument below are uleb128 values, though
7368 // currently-defined values fit in one byte for each.
7369 const std::string& sv =
7370 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
7372 && sv.data()[0] == elfcpp::Tag_CPU_arch
7373 && (sv.data()[1] & 128) != 128)
7374 return sv.data()[1];
7376 // This tag is "safely ignorable", so don't complain if it looks funny.
7380 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
7381 // The tag is removed if ARCH is -1.
7382 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
7384 template<bool big_endian>
7386 Target_arm<big_endian>::set_secondary_compatible_arch(
7387 Attributes_section_data* pasd,
7390 Object_attribute *known_attributes =
7391 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
7395 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
7399 // Note: the tag and its argument below are uleb128 values, though
7400 // currently-defined values fit in one byte for each.
7402 sv[0] = elfcpp::Tag_CPU_arch;
7403 gold_assert(arch != 0);
7407 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
7410 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
7412 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
7414 template<bool big_endian>
7416 Target_arm<big_endian>::tag_cpu_arch_combine(
7419 int* secondary_compat_out,
7421 int secondary_compat)
7423 #define T(X) elfcpp::TAG_CPU_ARCH_##X
7424 static const int v6t2[] =
7436 static const int v6k[] =
7449 static const int v7[] =
7463 static const int v6_m[] =
7478 static const int v6s_m[] =
7494 static const int v7e_m[] =
7511 static const int v4t_plus_v6_m[] =
7527 T(V4T_PLUS_V6_M) // V4T plus V6_M.
7529 static const int *comb[] =
7537 // Pseudo-architecture.
7541 // Check we've not got a higher architecture than we know about.
7543 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
7545 gold_error(_("%s: unknown CPU architecture"), name);
7549 // Override old tag if we have a Tag_also_compatible_with on the output.
7551 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
7552 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
7553 oldtag = T(V4T_PLUS_V6_M);
7555 // And override the new tag if we have a Tag_also_compatible_with on the
7558 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
7559 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
7560 newtag = T(V4T_PLUS_V6_M);
7562 // Architectures before V6KZ add features monotonically.
7563 int tagh = std::max(oldtag, newtag);
7564 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
7567 int tagl = std::min(oldtag, newtag);
7568 int result = comb[tagh - T(V6T2)][tagl];
7570 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
7571 // as the canonical version.
7572 if (result == T(V4T_PLUS_V6_M))
7575 *secondary_compat_out = T(V6_M);
7578 *secondary_compat_out = -1;
7582 gold_error(_("%s: conflicting CPU architectures %d/%d"),
7583 name, oldtag, newtag);
7591 // Helper to print AEABI enum tag value.
7593 template<bool big_endian>
7595 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
7597 static const char *aeabi_enum_names[] =
7598 { "", "variable-size", "32-bit", "" };
7599 const size_t aeabi_enum_names_size =
7600 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
7602 if (value < aeabi_enum_names_size)
7603 return std::string(aeabi_enum_names[value]);
7607 sprintf(buffer, "<unknown value %u>", value);
7608 return std::string(buffer);
7612 // Return the string value to store in TAG_CPU_name.
7614 template<bool big_endian>
7616 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
7618 static const char *name_table[] = {
7619 // These aren't real CPU names, but we can't guess
7620 // that from the architecture version alone.
7636 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
7638 if (value < name_table_size)
7639 return std::string(name_table[value]);
7643 sprintf(buffer, "<unknown CPU value %u>", value);
7644 return std::string(buffer);
7648 // Merge object attributes from input file called NAME with those of the
7649 // output. The input object attributes are in the object pointed by PASD.
7651 template<bool big_endian>
7653 Target_arm<big_endian>::merge_object_attributes(
7655 const Attributes_section_data* pasd)
7657 // Return if there is no attributes section data.
7661 // If output has no object attributes, just copy.
7662 if (this->attributes_section_data_ == NULL)
7664 this->attributes_section_data_ = new Attributes_section_data(*pasd);
7668 const int vendor = Object_attribute::OBJ_ATTR_PROC;
7669 const Object_attribute* in_attr = pasd->known_attributes(vendor);
7670 Object_attribute* out_attr =
7671 this->attributes_section_data_->known_attributes(vendor);
7673 // This needs to happen before Tag_ABI_FP_number_model is merged. */
7674 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
7675 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
7677 // Ignore mismatches if the object doesn't use floating point. */
7678 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
7679 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
7680 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
7681 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
7682 gold_error(_("%s uses VFP register arguments, output does not"),
7686 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
7688 // Merge this attribute with existing attributes.
7691 case elfcpp::Tag_CPU_raw_name:
7692 case elfcpp::Tag_CPU_name:
7693 // These are merged after Tag_CPU_arch.
7696 case elfcpp::Tag_ABI_optimization_goals:
7697 case elfcpp::Tag_ABI_FP_optimization_goals:
7698 // Use the first value seen.
7701 case elfcpp::Tag_CPU_arch:
7703 unsigned int saved_out_attr = out_attr->int_value();
7704 // Merge Tag_CPU_arch and Tag_also_compatible_with.
7705 int secondary_compat =
7706 this->get_secondary_compatible_arch(pasd);
7707 int secondary_compat_out =
7708 this->get_secondary_compatible_arch(
7709 this->attributes_section_data_);
7710 out_attr[i].set_int_value(
7711 tag_cpu_arch_combine(name, out_attr[i].int_value(),
7712 &secondary_compat_out,
7713 in_attr[i].int_value(),
7715 this->set_secondary_compatible_arch(this->attributes_section_data_,
7716 secondary_compat_out);
7718 // Merge Tag_CPU_name and Tag_CPU_raw_name.
7719 if (out_attr[i].int_value() == saved_out_attr)
7720 ; // Leave the names alone.
7721 else if (out_attr[i].int_value() == in_attr[i].int_value())
7723 // The output architecture has been changed to match the
7724 // input architecture. Use the input names.
7725 out_attr[elfcpp::Tag_CPU_name].set_string_value(
7726 in_attr[elfcpp::Tag_CPU_name].string_value());
7727 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
7728 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
7732 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
7733 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
7736 // If we still don't have a value for Tag_CPU_name,
7737 // make one up now. Tag_CPU_raw_name remains blank.
7738 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
7740 const std::string cpu_name =
7741 this->tag_cpu_name_value(out_attr[i].int_value());
7742 // FIXME: If we see an unknown CPU, this will be set
7743 // to "<unknown CPU n>", where n is the attribute value.
7744 // This is different from BFD, which leaves the name alone.
7745 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
7750 case elfcpp::Tag_ARM_ISA_use:
7751 case elfcpp::Tag_THUMB_ISA_use:
7752 case elfcpp::Tag_WMMX_arch:
7753 case elfcpp::Tag_Advanced_SIMD_arch:
7754 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
7755 case elfcpp::Tag_ABI_FP_rounding:
7756 case elfcpp::Tag_ABI_FP_exceptions:
7757 case elfcpp::Tag_ABI_FP_user_exceptions:
7758 case elfcpp::Tag_ABI_FP_number_model:
7759 case elfcpp::Tag_VFP_HP_extension:
7760 case elfcpp::Tag_CPU_unaligned_access:
7761 case elfcpp::Tag_T2EE_use:
7762 case elfcpp::Tag_Virtualization_use:
7763 case elfcpp::Tag_MPextension_use:
7764 // Use the largest value specified.
7765 if (in_attr[i].int_value() > out_attr[i].int_value())
7766 out_attr[i].set_int_value(in_attr[i].int_value());
7769 case elfcpp::Tag_ABI_align8_preserved:
7770 case elfcpp::Tag_ABI_PCS_RO_data:
7771 // Use the smallest value specified.
7772 if (in_attr[i].int_value() < out_attr[i].int_value())
7773 out_attr[i].set_int_value(in_attr[i].int_value());
7776 case elfcpp::Tag_ABI_align8_needed:
7777 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
7778 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
7779 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
7782 // This error message should be enabled once all non-conformant
7783 // binaries in the toolchain have had the attributes set
7785 // gold_error(_("output 8-byte data alignment conflicts with %s"),
7789 case elfcpp::Tag_ABI_FP_denormal:
7790 case elfcpp::Tag_ABI_PCS_GOT_use:
7792 // These tags have 0 = don't care, 1 = strong requirement,
7793 // 2 = weak requirement.
7794 static const int order_021[3] = {0, 2, 1};
7796 // Use the "greatest" from the sequence 0, 2, 1, or the largest
7797 // value if greater than 2 (for future-proofing).
7798 if ((in_attr[i].int_value() > 2
7799 && in_attr[i].int_value() > out_attr[i].int_value())
7800 || (in_attr[i].int_value() <= 2
7801 && out_attr[i].int_value() <= 2
7802 && (order_021[in_attr[i].int_value()]
7803 > order_021[out_attr[i].int_value()])))
7804 out_attr[i].set_int_value(in_attr[i].int_value());
7808 case elfcpp::Tag_CPU_arch_profile:
7809 if (out_attr[i].int_value() != in_attr[i].int_value())
7811 // 0 will merge with anything.
7812 // 'A' and 'S' merge to 'A'.
7813 // 'R' and 'S' merge to 'R'.
7814 // 'M' and 'A|R|S' is an error.
7815 if (out_attr[i].int_value() == 0
7816 || (out_attr[i].int_value() == 'S'
7817 && (in_attr[i].int_value() == 'A'
7818 || in_attr[i].int_value() == 'R')))
7819 out_attr[i].set_int_value(in_attr[i].int_value());
7820 else if (in_attr[i].int_value() == 0
7821 || (in_attr[i].int_value() == 'S'
7822 && (out_attr[i].int_value() == 'A'
7823 || out_attr[i].int_value() == 'R')))
7828 (_("conflicting architecture profiles %c/%c"),
7829 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
7830 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
7834 case elfcpp::Tag_VFP_arch:
7851 // Values greater than 6 aren't defined, so just pick the
7853 if (in_attr[i].int_value() > 6
7854 && in_attr[i].int_value() > out_attr[i].int_value())
7856 *out_attr = *in_attr;
7859 // The output uses the superset of input features
7860 // (ISA version) and registers.
7861 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
7862 vfp_versions[out_attr[i].int_value()].ver);
7863 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
7864 vfp_versions[out_attr[i].int_value()].regs);
7865 // This assumes all possible supersets are also a valid
7868 for (newval = 6; newval > 0; newval--)
7870 if (regs == vfp_versions[newval].regs
7871 && ver == vfp_versions[newval].ver)
7874 out_attr[i].set_int_value(newval);
7877 case elfcpp::Tag_PCS_config:
7878 if (out_attr[i].int_value() == 0)
7879 out_attr[i].set_int_value(in_attr[i].int_value());
7880 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
7882 // It's sometimes ok to mix different configs, so this is only
7884 gold_warning(_("%s: conflicting platform configuration"), name);
7887 case elfcpp::Tag_ABI_PCS_R9_use:
7888 if (in_attr[i].int_value() != out_attr[i].int_value()
7889 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
7890 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
7892 gold_error(_("%s: conflicting use of R9"), name);
7894 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
7895 out_attr[i].set_int_value(in_attr[i].int_value());
7897 case elfcpp::Tag_ABI_PCS_RW_data:
7898 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
7899 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
7900 != elfcpp::AEABI_R9_SB)
7901 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
7902 != elfcpp::AEABI_R9_unused))
7904 gold_error(_("%s: SB relative addressing conflicts with use "
7908 // Use the smallest value specified.
7909 if (in_attr[i].int_value() < out_attr[i].int_value())
7910 out_attr[i].set_int_value(in_attr[i].int_value());
7912 case elfcpp::Tag_ABI_PCS_wchar_t:
7913 // FIXME: Make it possible to turn off this warning.
7914 if (out_attr[i].int_value()
7915 && in_attr[i].int_value()
7916 && out_attr[i].int_value() != in_attr[i].int_value())
7918 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
7919 "use %u-byte wchar_t; use of wchar_t values "
7920 "across objects may fail"),
7921 name, in_attr[i].int_value(),
7922 out_attr[i].int_value());
7924 else if (in_attr[i].int_value() && !out_attr[i].int_value())
7925 out_attr[i].set_int_value(in_attr[i].int_value());
7927 case elfcpp::Tag_ABI_enum_size:
7928 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
7930 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
7931 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
7933 // The existing object is compatible with anything.
7934 // Use whatever requirements the new object has.
7935 out_attr[i].set_int_value(in_attr[i].int_value());
7937 // FIXME: Make it possible to turn off this warning.
7938 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
7939 && out_attr[i].int_value() != in_attr[i].int_value())
7941 unsigned int in_value = in_attr[i].int_value();
7942 unsigned int out_value = out_attr[i].int_value();
7943 gold_warning(_("%s uses %s enums yet the output is to use "
7944 "%s enums; use of enum values across objects "
7947 this->aeabi_enum_name(in_value).c_str(),
7948 this->aeabi_enum_name(out_value).c_str());
7952 case elfcpp::Tag_ABI_VFP_args:
7955 case elfcpp::Tag_ABI_WMMX_args:
7956 if (in_attr[i].int_value() != out_attr[i].int_value())
7958 gold_error(_("%s uses iWMMXt register arguments, output does "
7963 case Object_attribute::Tag_compatibility:
7964 // Merged in target-independent code.
7966 case elfcpp::Tag_ABI_HardFP_use:
7967 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
7968 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
7969 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
7970 out_attr[i].set_int_value(3);
7971 else if (in_attr[i].int_value() > out_attr[i].int_value())
7972 out_attr[i].set_int_value(in_attr[i].int_value());
7974 case elfcpp::Tag_ABI_FP_16bit_format:
7975 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
7977 if (in_attr[i].int_value() != out_attr[i].int_value())
7978 gold_error(_("fp16 format mismatch between %s and output"),
7981 if (in_attr[i].int_value() != 0)
7982 out_attr[i].set_int_value(in_attr[i].int_value());
7985 case elfcpp::Tag_nodefaults:
7986 // This tag is set if it exists, but the value is unused (and is
7987 // typically zero). We don't actually need to do anything here -
7988 // the merge happens automatically when the type flags are merged
7991 case elfcpp::Tag_also_compatible_with:
7992 // Already done in Tag_CPU_arch.
7994 case elfcpp::Tag_conformance:
7995 // Keep the attribute if it matches. Throw it away otherwise.
7996 // No attribute means no claim to conform.
7997 if (in_attr[i].string_value() != out_attr[i].string_value())
7998 out_attr[i].set_string_value("");
8003 const char* err_object = NULL;
8005 // The "known_obj_attributes" table does contain some undefined
8006 // attributes. Ensure that there are unused.
8007 if (out_attr[i].int_value() != 0
8008 || out_attr[i].string_value() != "")
8009 err_object = "output";
8010 else if (in_attr[i].int_value() != 0
8011 || in_attr[i].string_value() != "")
8014 if (err_object != NULL)
8016 // Attribute numbers >=64 (mod 128) can be safely ignored.
8018 gold_error(_("%s: unknown mandatory EABI object attribute "
8022 gold_warning(_("%s: unknown EABI object attribute %d"),
8026 // Only pass on attributes that match in both inputs.
8027 if (!in_attr[i].matches(out_attr[i]))
8029 out_attr[i].set_int_value(0);
8030 out_attr[i].set_string_value("");
8035 // If out_attr was copied from in_attr then it won't have a type yet.
8036 if (in_attr[i].type() && !out_attr[i].type())
8037 out_attr[i].set_type(in_attr[i].type());
8040 // Merge Tag_compatibility attributes and any common GNU ones.
8041 this->attributes_section_data_->merge(name, pasd);
8043 // Check for any attributes not known on ARM.
8044 typedef Vendor_object_attributes::Other_attributes Other_attributes;
8045 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
8046 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
8047 Other_attributes* out_other_attributes =
8048 this->attributes_section_data_->other_attributes(vendor);
8049 Other_attributes::iterator out_iter = out_other_attributes->begin();
8051 while (in_iter != in_other_attributes->end()
8052 || out_iter != out_other_attributes->end())
8054 const char* err_object = NULL;
8057 // The tags for each list are in numerical order.
8058 // If the tags are equal, then merge.
8059 if (out_iter != out_other_attributes->end()
8060 && (in_iter == in_other_attributes->end()
8061 || in_iter->first > out_iter->first))
8063 // This attribute only exists in output. We can't merge, and we
8064 // don't know what the tag means, so delete it.
8065 err_object = "output";
8066 err_tag = out_iter->first;
8067 int saved_tag = out_iter->first;
8068 delete out_iter->second;
8069 out_other_attributes->erase(out_iter);
8070 out_iter = out_other_attributes->upper_bound(saved_tag);
8072 else if (in_iter != in_other_attributes->end()
8073 && (out_iter != out_other_attributes->end()
8074 || in_iter->first < out_iter->first))
8076 // This attribute only exists in input. We can't merge, and we
8077 // don't know what the tag means, so ignore it.
8079 err_tag = in_iter->first;
8082 else // The tags are equal.
8084 // As present, all attributes in the list are unknown, and
8085 // therefore can't be merged meaningfully.
8086 err_object = "output";
8087 err_tag = out_iter->first;
8089 // Only pass on attributes that match in both inputs.
8090 if (!in_iter->second->matches(*(out_iter->second)))
8092 // No match. Delete the attribute.
8093 int saved_tag = out_iter->first;
8094 delete out_iter->second;
8095 out_other_attributes->erase(out_iter);
8096 out_iter = out_other_attributes->upper_bound(saved_tag);
8100 // Matched. Keep the attribute and move to the next.
8108 // Attribute numbers >=64 (mod 128) can be safely ignored. */
8109 if ((err_tag & 127) < 64)
8111 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
8112 err_object, err_tag);
8116 gold_warning(_("%s: unknown EABI object attribute %d"),
8117 err_object, err_tag);
8123 // Return whether a relocation type used the LSB to distinguish THUMB
8125 template<bool big_endian>
8127 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
8131 case elfcpp::R_ARM_PC24:
8132 case elfcpp::R_ARM_ABS32:
8133 case elfcpp::R_ARM_REL32:
8134 case elfcpp::R_ARM_SBREL32:
8135 case elfcpp::R_ARM_THM_CALL:
8136 case elfcpp::R_ARM_GLOB_DAT:
8137 case elfcpp::R_ARM_JUMP_SLOT:
8138 case elfcpp::R_ARM_GOTOFF32:
8139 case elfcpp::R_ARM_PLT32:
8140 case elfcpp::R_ARM_CALL:
8141 case elfcpp::R_ARM_JUMP24:
8142 case elfcpp::R_ARM_THM_JUMP24:
8143 case elfcpp::R_ARM_SBREL31:
8144 case elfcpp::R_ARM_PREL31:
8145 case elfcpp::R_ARM_MOVW_ABS_NC:
8146 case elfcpp::R_ARM_MOVW_PREL_NC:
8147 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8148 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8149 case elfcpp::R_ARM_THM_JUMP19:
8150 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8151 case elfcpp::R_ARM_ALU_PC_G0_NC:
8152 case elfcpp::R_ARM_ALU_PC_G0:
8153 case elfcpp::R_ARM_ALU_PC_G1_NC:
8154 case elfcpp::R_ARM_ALU_PC_G1:
8155 case elfcpp::R_ARM_ALU_PC_G2:
8156 case elfcpp::R_ARM_ALU_SB_G0_NC:
8157 case elfcpp::R_ARM_ALU_SB_G0:
8158 case elfcpp::R_ARM_ALU_SB_G1_NC:
8159 case elfcpp::R_ARM_ALU_SB_G1:
8160 case elfcpp::R_ARM_ALU_SB_G2:
8161 case elfcpp::R_ARM_MOVW_BREL_NC:
8162 case elfcpp::R_ARM_MOVW_BREL:
8163 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8164 case elfcpp::R_ARM_THM_MOVW_BREL:
8171 // Stub-generation methods for Target_arm.
8173 // Make a new Arm_input_section object.
8175 template<bool big_endian>
8176 Arm_input_section<big_endian>*
8177 Target_arm<big_endian>::new_arm_input_section(
8181 Section_id sid(relobj, shndx);
8183 Arm_input_section<big_endian>* arm_input_section =
8184 new Arm_input_section<big_endian>(relobj, shndx);
8185 arm_input_section->init();
8187 // Register new Arm_input_section in map for look-up.
8188 std::pair<typename Arm_input_section_map::iterator, bool> ins =
8189 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
8191 // Make sure that it we have not created another Arm_input_section
8192 // for this input section already.
8193 gold_assert(ins.second);
8195 return arm_input_section;
8198 // Find the Arm_input_section object corresponding to the SHNDX-th input
8199 // section of RELOBJ.
8201 template<bool big_endian>
8202 Arm_input_section<big_endian>*
8203 Target_arm<big_endian>::find_arm_input_section(
8205 unsigned int shndx) const
8207 Section_id sid(relobj, shndx);
8208 typename Arm_input_section_map::const_iterator p =
8209 this->arm_input_section_map_.find(sid);
8210 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
8213 // Make a new stub table.
8215 template<bool big_endian>
8216 Stub_table<big_endian>*
8217 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
8219 Stub_table<big_endian>* stub_table =
8220 new Stub_table<big_endian>(owner);
8221 this->stub_tables_.push_back(stub_table);
8223 stub_table->set_address(owner->address() + owner->data_size());
8224 stub_table->set_file_offset(owner->offset() + owner->data_size());
8225 stub_table->finalize_data_size();
8230 // Scan a relocation for stub generation.
8232 template<bool big_endian>
8234 Target_arm<big_endian>::scan_reloc_for_stub(
8235 const Relocate_info<32, big_endian>* relinfo,
8236 unsigned int r_type,
8237 const Sized_symbol<32>* gsym,
8239 const Symbol_value<32>* psymval,
8240 elfcpp::Elf_types<32>::Elf_Swxword addend,
8241 Arm_address address)
8243 typedef typename Target_arm<big_endian>::Relocate Relocate;
8245 const Arm_relobj<big_endian>* arm_relobj =
8246 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8248 if (r_type == elfcpp::R_ARM_V4BX)
8250 const uint32_t reg = (addend & 0xf);
8251 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8254 // Try looking up an existing stub from a stub table.
8255 Stub_table<big_endian>* stub_table =
8256 arm_relobj->stub_table(relinfo->data_shndx);
8257 gold_assert(stub_table != NULL);
8259 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
8261 // create a new stub and add it to stub table.
8262 Arm_v4bx_stub* stub =
8263 this->stub_factory().make_arm_v4bx_stub(reg);
8264 gold_assert(stub != NULL);
8265 stub_table->add_arm_v4bx_stub(stub);
8272 bool target_is_thumb;
8273 Symbol_value<32> symval;
8276 // This is a global symbol. Determine if we use PLT and if the
8277 // final target is THUMB.
8278 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
8280 // This uses a PLT, change the symbol value.
8281 symval.set_output_value(this->plt_section()->address()
8282 + gsym->plt_offset());
8284 target_is_thumb = false;
8286 else if (gsym->is_undefined())
8287 // There is no need to generate a stub symbol is undefined.
8292 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
8293 || (gsym->type() == elfcpp::STT_FUNC
8294 && !gsym->is_undefined()
8295 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
8300 // This is a local symbol. Determine if the final target is THUMB.
8301 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
8304 // Strip LSB if this points to a THUMB target.
8306 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
8307 && ((psymval->value(arm_relobj, 0) & 1) != 0))
8309 Arm_address stripped_value =
8310 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
8311 symval.set_output_value(stripped_value);
8315 // Get the symbol value.
8316 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
8318 // Owing to pipelining, the PC relative branches below actually skip
8319 // two instructions when the branch offset is 0.
8320 Arm_address destination;
8323 case elfcpp::R_ARM_CALL:
8324 case elfcpp::R_ARM_JUMP24:
8325 case elfcpp::R_ARM_PLT32:
8327 destination = value + addend + 8;
8329 case elfcpp::R_ARM_THM_CALL:
8330 case elfcpp::R_ARM_THM_XPC22:
8331 case elfcpp::R_ARM_THM_JUMP24:
8332 case elfcpp::R_ARM_THM_JUMP19:
8334 destination = value + addend + 4;
8340 Reloc_stub* stub = NULL;
8341 Stub_type stub_type =
8342 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
8344 if (stub_type != arm_stub_none)
8346 // Try looking up an existing stub from a stub table.
8347 Stub_table<big_endian>* stub_table =
8348 arm_relobj->stub_table(relinfo->data_shndx);
8349 gold_assert(stub_table != NULL);
8351 // Locate stub by destination.
8352 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
8354 // Create a stub if there is not one already
8355 stub = stub_table->find_reloc_stub(stub_key);
8358 // create a new stub and add it to stub table.
8359 stub = this->stub_factory().make_reloc_stub(stub_type);
8360 stub_table->add_reloc_stub(stub, stub_key);
8363 // Record the destination address.
8364 stub->set_destination_address(destination
8365 | (target_is_thumb ? 1 : 0));
8368 // For Cortex-A8, we need to record a relocation at 4K page boundary.
8369 if (this->fix_cortex_a8_
8370 && (r_type == elfcpp::R_ARM_THM_JUMP24
8371 || r_type == elfcpp::R_ARM_THM_JUMP19
8372 || r_type == elfcpp::R_ARM_THM_CALL
8373 || r_type == elfcpp::R_ARM_THM_XPC22)
8374 && (address & 0xfffU) == 0xffeU)
8376 // Found a candidate. Note we haven't checked the destination is
8377 // within 4K here: if we do so (and don't create a record) we can't
8378 // tell that a branch should have been relocated when scanning later.
8379 this->cortex_a8_relocs_info_[address] =
8380 new Cortex_a8_reloc(stub, r_type,
8381 destination | (target_is_thumb ? 1 : 0));
8385 // This function scans a relocation sections for stub generation.
8386 // The template parameter Relocate must be a class type which provides
8387 // a single function, relocate(), which implements the machine
8388 // specific part of a relocation.
8390 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
8391 // SHT_REL or SHT_RELA.
8393 // PRELOCS points to the relocation data. RELOC_COUNT is the number
8394 // of relocs. OUTPUT_SECTION is the output section.
8395 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
8396 // mapped to output offsets.
8398 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
8399 // VIEW_SIZE is the size. These refer to the input section, unless
8400 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
8401 // the output section.
8403 template<bool big_endian>
8404 template<int sh_type>
8406 Target_arm<big_endian>::scan_reloc_section_for_stubs(
8407 const Relocate_info<32, big_endian>* relinfo,
8408 const unsigned char* prelocs,
8410 Output_section* output_section,
8411 bool needs_special_offset_handling,
8412 const unsigned char* view,
8413 elfcpp::Elf_types<32>::Elf_Addr view_address,
8416 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
8417 const int reloc_size =
8418 Reloc_types<sh_type, 32, big_endian>::reloc_size;
8420 Arm_relobj<big_endian>* arm_object =
8421 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8422 unsigned int local_count = arm_object->local_symbol_count();
8424 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
8426 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
8428 Reltype reloc(prelocs);
8430 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
8431 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
8432 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
8434 r_type = this->get_real_reloc_type(r_type);
8436 // Only a few relocation types need stubs.
8437 if ((r_type != elfcpp::R_ARM_CALL)
8438 && (r_type != elfcpp::R_ARM_JUMP24)
8439 && (r_type != elfcpp::R_ARM_PLT32)
8440 && (r_type != elfcpp::R_ARM_THM_CALL)
8441 && (r_type != elfcpp::R_ARM_THM_XPC22)
8442 && (r_type != elfcpp::R_ARM_THM_JUMP24)
8443 && (r_type != elfcpp::R_ARM_THM_JUMP19)
8444 && (r_type != elfcpp::R_ARM_V4BX))
8447 section_offset_type offset =
8448 convert_to_section_size_type(reloc.get_r_offset());
8450 if (needs_special_offset_handling)
8452 offset = output_section->output_offset(relinfo->object,
8453 relinfo->data_shndx,
8459 if (r_type == elfcpp::R_ARM_V4BX)
8461 // Get the BX instruction.
8462 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
8463 const Valtype* wv = reinterpret_cast<const Valtype*>(view + offset);
8464 elfcpp::Elf_types<32>::Elf_Swxword insn =
8465 elfcpp::Swap<32, big_endian>::readval(wv);
8466 this->scan_reloc_for_stub(relinfo, r_type, NULL, 0, NULL,
8472 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
8473 elfcpp::Elf_types<32>::Elf_Swxword addend =
8474 stub_addend_reader(r_type, view + offset, reloc);
8476 const Sized_symbol<32>* sym;
8478 Symbol_value<32> symval;
8479 const Symbol_value<32> *psymval;
8480 if (r_sym < local_count)
8483 psymval = arm_object->local_symbol(r_sym);
8485 // If the local symbol belongs to a section we are discarding,
8486 // and that section is a debug section, try to find the
8487 // corresponding kept section and map this symbol to its
8488 // counterpart in the kept section. The symbol must not
8489 // correspond to a section we are folding.
8491 unsigned int shndx = psymval->input_shndx(&is_ordinary);
8493 && shndx != elfcpp::SHN_UNDEF
8494 && !arm_object->is_section_included(shndx)
8495 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
8497 if (comdat_behavior == CB_UNDETERMINED)
8500 arm_object->section_name(relinfo->data_shndx);
8501 comdat_behavior = get_comdat_behavior(name.c_str());
8503 if (comdat_behavior == CB_PRETEND)
8506 typename elfcpp::Elf_types<32>::Elf_Addr value =
8507 arm_object->map_to_kept_section(shndx, &found);
8509 symval.set_output_value(value + psymval->input_value());
8511 symval.set_output_value(0);
8515 symval.set_output_value(0);
8517 symval.set_no_output_symtab_entry();
8523 const Symbol* gsym = arm_object->global_symbol(r_sym);
8524 gold_assert(gsym != NULL);
8525 if (gsym->is_forwarder())
8526 gsym = relinfo->symtab->resolve_forwards(gsym);
8528 sym = static_cast<const Sized_symbol<32>*>(gsym);
8529 if (sym->has_symtab_index())
8530 symval.set_output_symtab_index(sym->symtab_index());
8532 symval.set_no_output_symtab_entry();
8534 // We need to compute the would-be final value of this global
8536 const Symbol_table* symtab = relinfo->symtab;
8537 const Sized_symbol<32>* sized_symbol =
8538 symtab->get_sized_symbol<32>(gsym);
8539 Symbol_table::Compute_final_value_status status;
8541 symtab->compute_final_value<32>(sized_symbol, &status);
8543 // Skip this if the symbol has not output section.
8544 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
8547 symval.set_output_value(value);
8551 // If symbol is a section symbol, we don't know the actual type of
8552 // destination. Give up.
8553 if (psymval->is_section_symbol())
8556 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
8557 addend, view_address + offset);
8561 // Scan an input section for stub generation.
8563 template<bool big_endian>
8565 Target_arm<big_endian>::scan_section_for_stubs(
8566 const Relocate_info<32, big_endian>* relinfo,
8567 unsigned int sh_type,
8568 const unsigned char* prelocs,
8570 Output_section* output_section,
8571 bool needs_special_offset_handling,
8572 const unsigned char* view,
8573 Arm_address view_address,
8574 section_size_type view_size)
8576 if (sh_type == elfcpp::SHT_REL)
8577 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
8582 needs_special_offset_handling,
8586 else if (sh_type == elfcpp::SHT_RELA)
8587 // We do not support RELA type relocations yet. This is provided for
8589 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
8594 needs_special_offset_handling,
8602 // Group input sections for stub generation.
8604 // We goup input sections in an output sections so that the total size,
8605 // including any padding space due to alignment is smaller than GROUP_SIZE
8606 // unless the only input section in group is bigger than GROUP_SIZE already.
8607 // Then an ARM stub table is created to follow the last input section
8608 // in group. For each group an ARM stub table is created an is placed
8609 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
8610 // extend the group after the stub table.
8612 template<bool big_endian>
8614 Target_arm<big_endian>::group_sections(
8616 section_size_type group_size,
8617 bool stubs_always_after_branch)
8619 // Group input sections and insert stub table
8620 Layout::Section_list section_list;
8621 layout->get_allocated_sections(§ion_list);
8622 for (Layout::Section_list::const_iterator p = section_list.begin();
8623 p != section_list.end();
8626 Arm_output_section<big_endian>* output_section =
8627 Arm_output_section<big_endian>::as_arm_output_section(*p);
8628 output_section->group_sections(group_size, stubs_always_after_branch,
8633 // Relaxation hook. This is where we do stub generation.
8635 template<bool big_endian>
8637 Target_arm<big_endian>::do_relax(
8639 const Input_objects* input_objects,
8640 Symbol_table* symtab,
8643 // No need to generate stubs if this is a relocatable link.
8644 gold_assert(!parameters->options().relocatable());
8646 // If this is the first pass, we need to group input sections into
8650 // Determine the stub group size. The group size is the absolute
8651 // value of the parameter --stub-group-size. If --stub-group-size
8652 // is passed a negative value, we restict stubs to be always after
8653 // the stubbed branches.
8654 int32_t stub_group_size_param =
8655 parameters->options().stub_group_size();
8656 bool stubs_always_after_branch = stub_group_size_param < 0;
8657 section_size_type stub_group_size = abs(stub_group_size_param);
8659 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
8660 // page as the first half of a 32-bit branch straddling two 4K pages.
8661 // This is a crude way of enforcing that.
8662 if (this->fix_cortex_a8_)
8663 stubs_always_after_branch = true;
8665 if (stub_group_size == 1)
8668 // Thumb branch range is +-4MB has to be used as the default
8669 // maximum size (a given section can contain both ARM and Thumb
8670 // code, so the worst case has to be taken into account).
8672 // This value is 24K less than that, which allows for 2025
8673 // 12-byte stubs. If we exceed that, then we will fail to link.
8674 // The user will have to relink with an explicit group size
8676 stub_group_size = 4170000;
8679 group_sections(layout, stub_group_size, stubs_always_after_branch);
8682 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
8683 // beginning of each relaxation pass, just blow away all the stubs.
8684 // Alternatively, we could selectively remove only the stubs and reloc
8685 // information for code sections that have moved since the last pass.
8686 // That would require more book-keeping.
8687 typedef typename Stub_table_list::iterator Stub_table_iterator;
8688 if (this->fix_cortex_a8_)
8690 // Clear all Cortex-A8 reloc information.
8691 for (typename Cortex_a8_relocs_info::const_iterator p =
8692 this->cortex_a8_relocs_info_.begin();
8693 p != this->cortex_a8_relocs_info_.end();
8696 this->cortex_a8_relocs_info_.clear();
8698 // Remove all Cortex-A8 stubs.
8699 for (Stub_table_iterator sp = this->stub_tables_.begin();
8700 sp != this->stub_tables_.end();
8702 (*sp)->remove_all_cortex_a8_stubs();
8705 // Scan relocs for relocation stubs
8706 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
8707 op != input_objects->relobj_end();
8710 Arm_relobj<big_endian>* arm_relobj =
8711 Arm_relobj<big_endian>::as_arm_relobj(*op);
8712 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
8715 // Check all stub tables to see if any of them have their data sizes
8716 // or addresses alignments changed. These are the only things that
8718 bool any_stub_table_changed = false;
8719 for (Stub_table_iterator sp = this->stub_tables_.begin();
8720 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
8723 if ((*sp)->update_data_size_and_addralign())
8724 any_stub_table_changed = true;
8727 // Finalize the stubs in the last relaxation pass.
8728 if (!any_stub_table_changed)
8729 for (Stub_table_iterator sp = this->stub_tables_.begin();
8730 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
8732 (*sp)->finalize_stubs();
8734 return any_stub_table_changed;
8739 template<bool big_endian>
8741 Target_arm<big_endian>::relocate_stub(
8743 const Relocate_info<32, big_endian>* relinfo,
8744 Output_section* output_section,
8745 unsigned char* view,
8746 Arm_address address,
8747 section_size_type view_size)
8750 const Stub_template* stub_template = stub->stub_template();
8751 for (size_t i = 0; i < stub_template->reloc_count(); i++)
8753 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
8754 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
8756 unsigned int r_type = insn->r_type();
8757 section_size_type reloc_offset = stub_template->reloc_offset(i);
8758 section_size_type reloc_size = insn->size();
8759 gold_assert(reloc_offset + reloc_size <= view_size);
8761 // This is the address of the stub destination.
8762 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
8763 Symbol_value<32> symval;
8764 symval.set_output_value(target);
8766 // Synthesize a fake reloc just in case. We don't have a symbol so
8768 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
8769 memset(reloc_buffer, 0, sizeof(reloc_buffer));
8770 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
8771 reloc_write.put_r_offset(reloc_offset);
8772 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
8773 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
8775 relocate.relocate(relinfo, this, output_section,
8776 this->fake_relnum_for_stubs, rel, r_type,
8777 NULL, &symval, view + reloc_offset,
8778 address + reloc_offset, reloc_size);
8782 // Determine whether an object attribute tag takes an integer, a
8785 template<bool big_endian>
8787 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
8789 if (tag == Object_attribute::Tag_compatibility)
8790 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
8791 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
8792 else if (tag == elfcpp::Tag_nodefaults)
8793 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
8794 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
8795 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
8796 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
8798 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
8800 return ((tag & 1) != 0
8801 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
8802 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
8805 // Reorder attributes.
8807 // The ABI defines that Tag_conformance should be emitted first, and that
8808 // Tag_nodefaults should be second (if either is defined). This sets those
8809 // two positions, and bumps up the position of all the remaining tags to
8812 template<bool big_endian>
8814 Target_arm<big_endian>::do_attributes_order(int num) const
8816 // Reorder the known object attributes in output. We want to move
8817 // Tag_conformance to position 4 and Tag_conformance to position 5
8818 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
8820 return elfcpp::Tag_conformance;
8822 return elfcpp::Tag_nodefaults;
8823 if ((num - 2) < elfcpp::Tag_nodefaults)
8825 if ((num - 1) < elfcpp::Tag_conformance)
8830 // Scan a span of THUMB code for Cortex-A8 erratum.
8832 template<bool big_endian>
8834 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
8835 Arm_relobj<big_endian>* arm_relobj,
8837 section_size_type span_start,
8838 section_size_type span_end,
8839 const unsigned char* view,
8840 Arm_address address)
8842 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
8844 // The opcode is BLX.W, BL.W, B.W, Bcc.W
8845 // The branch target is in the same 4KB region as the
8846 // first half of the branch.
8847 // The instruction before the branch is a 32-bit
8848 // length non-branch instruction.
8849 section_size_type i = span_start;
8850 bool last_was_32bit = false;
8851 bool last_was_branch = false;
8852 while (i < span_end)
8854 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
8855 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
8856 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
8857 bool is_blx = false, is_b = false;
8858 bool is_bl = false, is_bcc = false;
8860 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
8863 // Load the rest of the insn (in manual-friendly order).
8864 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
8866 // Encoding T4: B<c>.W.
8867 is_b = (insn & 0xf800d000U) == 0xf0009000U;
8868 // Encoding T1: BL<c>.W.
8869 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
8870 // Encoding T2: BLX<c>.W.
8871 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
8872 // Encoding T3: B<c>.W (not permitted in IT block).
8873 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
8874 && (insn & 0x07f00000U) != 0x03800000U);
8877 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
8879 // If this instruction is a 32-bit THUMB branch that crosses a 4K
8880 // page boundary and it follows 32-bit non-branch instruction,
8881 // we need to work around.
8883 && ((address + i) & 0xfffU) == 0xffeU
8885 && !last_was_branch)
8887 // Check to see if there is a relocation stub for this branch.
8888 bool force_target_arm = false;
8889 bool force_target_thumb = false;
8890 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
8891 Cortex_a8_relocs_info::const_iterator p =
8892 this->cortex_a8_relocs_info_.find(address + i);
8894 if (p != this->cortex_a8_relocs_info_.end())
8896 cortex_a8_reloc = p->second;
8897 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
8899 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
8900 && !target_is_thumb)
8901 force_target_arm = true;
8902 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
8904 force_target_thumb = true;
8908 Stub_type stub_type = arm_stub_none;
8910 // Check if we have an offending branch instruction.
8911 uint16_t upper_insn = (insn >> 16) & 0xffffU;
8912 uint16_t lower_insn = insn & 0xffffU;
8913 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
8915 if (cortex_a8_reloc != NULL
8916 && cortex_a8_reloc->reloc_stub() != NULL)
8917 // We've already made a stub for this instruction, e.g.
8918 // it's a long branch or a Thumb->ARM stub. Assume that
8919 // stub will suffice to work around the A8 erratum (see
8920 // setting of always_after_branch above).
8924 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
8926 stub_type = arm_stub_a8_veneer_b_cond;
8928 else if (is_b || is_bl || is_blx)
8930 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
8936 ? arm_stub_a8_veneer_blx
8938 ? arm_stub_a8_veneer_bl
8939 : arm_stub_a8_veneer_b));
8942 if (stub_type != arm_stub_none)
8944 Arm_address pc_for_insn = address + i + 4;
8946 // The original instruction is a BL, but the target is
8947 // an ARM instruction. If we were not making a stub,
8948 // the BL would have been converted to a BLX. Use the
8949 // BLX stub instead in that case.
8950 if (this->may_use_blx() && force_target_arm
8951 && stub_type == arm_stub_a8_veneer_bl)
8953 stub_type = arm_stub_a8_veneer_blx;
8957 // Conversely, if the original instruction was
8958 // BLX but the target is Thumb mode, use the BL stub.
8959 else if (force_target_thumb
8960 && stub_type == arm_stub_a8_veneer_blx)
8962 stub_type = arm_stub_a8_veneer_bl;
8970 // If we found a relocation, use the proper destination,
8971 // not the offset in the (unrelocated) instruction.
8972 // Note this is always done if we switched the stub type above.
8973 if (cortex_a8_reloc != NULL)
8974 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
8976 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
8978 // Add a new stub if destination address in in the same page.
8979 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
8981 Cortex_a8_stub* stub =
8982 this->stub_factory_.make_cortex_a8_stub(stub_type,
8986 Stub_table<big_endian>* stub_table =
8987 arm_relobj->stub_table(shndx);
8988 gold_assert(stub_table != NULL);
8989 stub_table->add_cortex_a8_stub(address + i, stub);
8994 i += insn_32bit ? 4 : 2;
8995 last_was_32bit = insn_32bit;
8996 last_was_branch = is_32bit_branch;
9000 // Apply the Cortex-A8 workaround.
9002 template<bool big_endian>
9004 Target_arm<big_endian>::apply_cortex_a8_workaround(
9005 const Cortex_a8_stub* stub,
9006 Arm_address stub_address,
9007 unsigned char* insn_view,
9008 Arm_address insn_address)
9010 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
9011 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
9012 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
9013 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
9014 off_t branch_offset = stub_address - (insn_address + 4);
9016 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
9017 switch (stub->stub_template()->type())
9019 case arm_stub_a8_veneer_b_cond:
9020 gold_assert(!utils::has_overflow<21>(branch_offset));
9021 upper_insn = RelocFuncs::thumb32_cond_branch_upper(upper_insn,
9023 lower_insn = RelocFuncs::thumb32_cond_branch_lower(lower_insn,
9027 case arm_stub_a8_veneer_b:
9028 case arm_stub_a8_veneer_bl:
9029 case arm_stub_a8_veneer_blx:
9030 if ((lower_insn & 0x5000U) == 0x4000U)
9031 // For a BLX instruction, make sure that the relocation is
9032 // rounded up to a word boundary. This follows the semantics of
9033 // the instruction which specifies that bit 1 of the target
9034 // address will come from bit 1 of the base address.
9035 branch_offset = (branch_offset + 2) & ~3;
9037 // Put BRANCH_OFFSET back into the insn.
9038 gold_assert(!utils::has_overflow<25>(branch_offset));
9039 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
9040 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
9047 // Put the relocated value back in the object file:
9048 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
9049 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
9052 template<bool big_endian>
9053 class Target_selector_arm : public Target_selector
9056 Target_selector_arm()
9057 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
9058 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
9062 do_instantiate_target()
9063 { return new Target_arm<big_endian>(); }
9066 Target_selector_arm<false> target_selector_arm;
9067 Target_selector_arm<true> target_selector_armbe;
9069 } // End anonymous namespace.