1 // arm.cc -- arm target support for gold.
3 // Copyright 2009, 2010 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.
38 #include "parameters.h"
45 #include "copy-relocs.h"
47 #include "target-reloc.h"
48 #include "target-select.h"
52 #include "attributes.h"
53 #include "arm-reloc-property.h"
60 template<bool big_endian>
61 class Output_data_plt_arm;
63 template<bool big_endian>
66 template<bool big_endian>
67 class Arm_input_section;
69 class Arm_exidx_cantunwind;
71 class Arm_exidx_merged_section;
73 class Arm_exidx_fixup;
75 template<bool big_endian>
76 class Arm_output_section;
78 class Arm_exidx_input_section;
80 template<bool big_endian>
83 template<bool big_endian>
84 class Arm_relocate_functions;
86 template<bool big_endian>
90 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
92 // Maximum branch offsets for ARM, THUMB and THUMB2.
93 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
94 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
95 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
96 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
97 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
98 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
100 // The arm target class.
102 // This is a very simple port of gold for ARM-EABI. It is intended for
103 // supporting Android only for the time being.
106 // - Implement all static relocation types documented in arm-reloc.def.
107 // - Make PLTs more flexible for different architecture features like
109 // There are probably a lot more.
111 // Ideally we would like to avoid using global variables but this is used
112 // very in many places and sometimes in loops. If we use a function
113 // returning a static instance of Arm_reloc_property_table, it will very
114 // slow in an threaded environment since the static instance needs to be
115 // locked. The pointer is below initialized in the
116 // Target::do_select_as_default_target() hook so that we do not spend time
117 // building the table if we are not linking ARM objects.
119 // An alternative is to to process the information in arm-reloc.def in
120 // compilation time and generate a representation of it in PODs only. That
121 // way we can avoid initialization when the linker starts.
123 Arm_reloc_property_table *arm_reloc_property_table = NULL;
125 // Instruction template class. This class is similar to the insn_sequence
126 // struct in bfd/elf32-arm.c.
131 // Types of instruction templates.
135 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
136 // templates with class-specific semantics. Currently this is used
137 // only by the Cortex_a8_stub class for handling condition codes in
138 // conditional branches.
139 THUMB16_SPECIAL_TYPE,
145 // Factory methods to create instruction templates in different formats.
147 static const Insn_template
148 thumb16_insn(uint32_t data)
149 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
151 // A Thumb conditional branch, in which the proper condition is inserted
152 // when we build the stub.
153 static const Insn_template
154 thumb16_bcond_insn(uint32_t data)
155 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
157 static const Insn_template
158 thumb32_insn(uint32_t data)
159 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
161 static const Insn_template
162 thumb32_b_insn(uint32_t data, int reloc_addend)
164 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
168 static const Insn_template
169 arm_insn(uint32_t data)
170 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
172 static const Insn_template
173 arm_rel_insn(unsigned data, int reloc_addend)
174 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
176 static const Insn_template
177 data_word(unsigned data, unsigned int r_type, int reloc_addend)
178 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
180 // Accessors. This class is used for read-only objects so no modifiers
185 { return this->data_; }
187 // Return the instruction sequence type of this.
190 { return this->type_; }
192 // Return the ARM relocation type of this.
195 { return this->r_type_; }
199 { return this->reloc_addend_; }
201 // Return size of instruction template in bytes.
205 // Return byte-alignment of instruction template.
210 // We make the constructor private to ensure that only the factory
213 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
214 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
217 // Instruction specific data. This is used to store information like
218 // some of the instruction bits.
220 // Instruction template type.
222 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
223 unsigned int r_type_;
224 // Relocation addend.
225 int32_t reloc_addend_;
228 // Macro for generating code to stub types. One entry per long/short
232 DEF_STUB(long_branch_any_any) \
233 DEF_STUB(long_branch_v4t_arm_thumb) \
234 DEF_STUB(long_branch_thumb_only) \
235 DEF_STUB(long_branch_v4t_thumb_thumb) \
236 DEF_STUB(long_branch_v4t_thumb_arm) \
237 DEF_STUB(short_branch_v4t_thumb_arm) \
238 DEF_STUB(long_branch_any_arm_pic) \
239 DEF_STUB(long_branch_any_thumb_pic) \
240 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
241 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
242 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
243 DEF_STUB(long_branch_thumb_only_pic) \
244 DEF_STUB(a8_veneer_b_cond) \
245 DEF_STUB(a8_veneer_b) \
246 DEF_STUB(a8_veneer_bl) \
247 DEF_STUB(a8_veneer_blx) \
248 DEF_STUB(v4_veneer_bx)
252 #define DEF_STUB(x) arm_stub_##x,
258 // First reloc stub type.
259 arm_stub_reloc_first = arm_stub_long_branch_any_any,
260 // Last reloc stub type.
261 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
263 // First Cortex-A8 stub type.
264 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
265 // Last Cortex-A8 stub type.
266 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
269 arm_stub_type_last = arm_stub_v4_veneer_bx
273 // Stub template class. Templates are meant to be read-only objects.
274 // A stub template for a stub type contains all read-only attributes
275 // common to all stubs of the same type.
280 Stub_template(Stub_type, const Insn_template*, size_t);
288 { return this->type_; }
290 // Return an array of instruction templates.
293 { return this->insns_; }
295 // Return size of template in number of instructions.
298 { return this->insn_count_; }
300 // Return size of template in bytes.
303 { return this->size_; }
305 // Return alignment of the stub template.
308 { return this->alignment_; }
310 // Return whether entry point is in thumb mode.
312 entry_in_thumb_mode() const
313 { return this->entry_in_thumb_mode_; }
315 // Return number of relocations in this template.
318 { return this->relocs_.size(); }
320 // Return index of the I-th instruction with relocation.
322 reloc_insn_index(size_t i) const
324 gold_assert(i < this->relocs_.size());
325 return this->relocs_[i].first;
328 // Return the offset of the I-th instruction with relocation from the
329 // beginning of the stub.
331 reloc_offset(size_t i) const
333 gold_assert(i < this->relocs_.size());
334 return this->relocs_[i].second;
338 // This contains information about an instruction template with a relocation
339 // and its offset from start of stub.
340 typedef std::pair<size_t, section_size_type> Reloc;
342 // A Stub_template may not be copied. We want to share templates as much
344 Stub_template(const Stub_template&);
345 Stub_template& operator=(const Stub_template&);
349 // Points to an array of Insn_templates.
350 const Insn_template* insns_;
351 // Number of Insn_templates in insns_[].
353 // Size of templated instructions in bytes.
355 // Alignment of templated instructions.
357 // Flag to indicate if entry is in thumb mode.
358 bool entry_in_thumb_mode_;
359 // A table of reloc instruction indices and offsets. We can find these by
360 // looking at the instruction templates but we pre-compute and then stash
361 // them here for speed.
362 std::vector<Reloc> relocs_;
366 // A class for code stubs. This is a base class for different type of
367 // stubs used in the ARM target.
373 static const section_offset_type invalid_offset =
374 static_cast<section_offset_type>(-1);
377 Stub(const Stub_template* stub_template)
378 : stub_template_(stub_template), offset_(invalid_offset)
385 // Return the stub template.
387 stub_template() const
388 { return this->stub_template_; }
390 // Return offset of code stub from beginning of its containing stub table.
394 gold_assert(this->offset_ != invalid_offset);
395 return this->offset_;
398 // Set offset of code stub from beginning of its containing stub table.
400 set_offset(section_offset_type offset)
401 { this->offset_ = offset; }
403 // Return the relocation target address of the i-th relocation in the
404 // stub. This must be defined in a child class.
406 reloc_target(size_t i)
407 { return this->do_reloc_target(i); }
409 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
411 write(unsigned char* view, section_size_type view_size, bool big_endian)
412 { this->do_write(view, view_size, big_endian); }
414 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
415 // for the i-th instruction.
417 thumb16_special(size_t i)
418 { return this->do_thumb16_special(i); }
421 // This must be defined in the child class.
423 do_reloc_target(size_t) = 0;
425 // This may be overridden in the child class.
427 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
430 this->do_fixed_endian_write<true>(view, view_size);
432 this->do_fixed_endian_write<false>(view, view_size);
435 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
436 // instruction template.
438 do_thumb16_special(size_t)
439 { gold_unreachable(); }
442 // A template to implement do_write.
443 template<bool big_endian>
445 do_fixed_endian_write(unsigned char*, section_size_type);
448 const Stub_template* stub_template_;
449 // Offset within the section of containing this stub.
450 section_offset_type offset_;
453 // Reloc stub class. These are stubs we use to fix up relocation because
454 // of limited branch ranges.
456 class Reloc_stub : public Stub
459 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
460 // We assume we never jump to this address.
461 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
463 // Return destination address.
465 destination_address() const
467 gold_assert(this->destination_address_ != this->invalid_address);
468 return this->destination_address_;
471 // Set destination address.
473 set_destination_address(Arm_address address)
475 gold_assert(address != this->invalid_address);
476 this->destination_address_ = address;
479 // Reset destination address.
481 reset_destination_address()
482 { this->destination_address_ = this->invalid_address; }
484 // Determine stub type for a branch of a relocation of R_TYPE going
485 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
486 // the branch target is a thumb instruction. TARGET is used for look
487 // up ARM-specific linker settings.
489 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
490 Arm_address branch_target, bool target_is_thumb);
492 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
493 // and an addend. Since we treat global and local symbol differently, we
494 // use a Symbol object for a global symbol and a object-index pair for
499 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
500 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
501 // and R_SYM must not be invalid_index.
502 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
503 unsigned int r_sym, int32_t addend)
504 : stub_type_(stub_type), addend_(addend)
508 this->r_sym_ = Reloc_stub::invalid_index;
509 this->u_.symbol = symbol;
513 gold_assert(relobj != NULL && r_sym != invalid_index);
514 this->r_sym_ = r_sym;
515 this->u_.relobj = relobj;
522 // Accessors: Keys are meant to be read-only object so no modifiers are
528 { return this->stub_type_; }
530 // Return the local symbol index or invalid_index.
533 { return this->r_sym_; }
535 // Return the symbol if there is one.
538 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
540 // Return the relobj if there is one.
543 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
545 // Whether this equals to another key k.
547 eq(const Key& k) const
549 return ((this->stub_type_ == k.stub_type_)
550 && (this->r_sym_ == k.r_sym_)
551 && ((this->r_sym_ != Reloc_stub::invalid_index)
552 ? (this->u_.relobj == k.u_.relobj)
553 : (this->u_.symbol == k.u_.symbol))
554 && (this->addend_ == k.addend_));
557 // Return a hash value.
561 return (this->stub_type_
563 ^ gold::string_hash<char>(
564 (this->r_sym_ != Reloc_stub::invalid_index)
565 ? this->u_.relobj->name().c_str()
566 : this->u_.symbol->name())
570 // Functors for STL associative containers.
574 operator()(const Key& k) const
575 { return k.hash_value(); }
581 operator()(const Key& k1, const Key& k2) const
582 { return k1.eq(k2); }
585 // Name of key. This is mainly for debugging.
591 Stub_type stub_type_;
592 // If this is a local symbol, this is the index in the defining object.
593 // Otherwise, it is invalid_index for a global symbol.
595 // If r_sym_ is invalid index. This points to a global symbol.
596 // Otherwise, this points a relobj. We used the unsized and target
597 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
598 // Arm_relobj. This is done to avoid making the stub class a template
599 // as most of the stub machinery is endianity-neutral. However, it
600 // may require a bit of casting done by users of this class.
603 const Symbol* symbol;
604 const Relobj* relobj;
606 // Addend associated with a reloc.
611 // Reloc_stubs are created via a stub factory. So these are protected.
612 Reloc_stub(const Stub_template* stub_template)
613 : Stub(stub_template), destination_address_(invalid_address)
619 friend class Stub_factory;
621 // Return the relocation target address of the i-th relocation in the
624 do_reloc_target(size_t i)
626 // All reloc stub have only one relocation.
628 return this->destination_address_;
632 // Address of destination.
633 Arm_address destination_address_;
636 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
637 // THUMB branch that meets the following conditions:
639 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
640 // branch address is 0xffe.
641 // 2. The branch target address is in the same page as the first word of the
643 // 3. The branch follows a 32-bit instruction which is not a branch.
645 // To do the fix up, we need to store the address of the branch instruction
646 // and its target at least. We also need to store the original branch
647 // instruction bits for the condition code in a conditional branch. The
648 // condition code is used in a special instruction template. We also want
649 // to identify input sections needing Cortex-A8 workaround quickly. We store
650 // extra information about object and section index of the code section
651 // containing a branch being fixed up. The information is used to mark
652 // the code section when we finalize the Cortex-A8 stubs.
655 class Cortex_a8_stub : public Stub
661 // Return the object of the code section containing the branch being fixed
665 { return this->relobj_; }
667 // Return the section index of the code section containing the branch being
671 { return this->shndx_; }
673 // Return the source address of stub. This is the address of the original
674 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
677 source_address() const
678 { return this->source_address_; }
680 // Return the destination address of the stub. This is the branch taken
681 // address of the original branch instruction. LSB is 1 if it is a THUMB
682 // instruction address.
684 destination_address() const
685 { return this->destination_address_; }
687 // Return the instruction being fixed up.
689 original_insn() const
690 { return this->original_insn_; }
693 // Cortex_a8_stubs are created via a stub factory. So these are protected.
694 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
695 unsigned int shndx, Arm_address source_address,
696 Arm_address destination_address, uint32_t original_insn)
697 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
698 source_address_(source_address | 1U),
699 destination_address_(destination_address),
700 original_insn_(original_insn)
703 friend class Stub_factory;
705 // Return the relocation target address of the i-th relocation in the
708 do_reloc_target(size_t i)
710 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
712 // The conditional branch veneer has two relocations.
714 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
718 // All other Cortex-A8 stubs have only one relocation.
720 return this->destination_address_;
724 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
726 do_thumb16_special(size_t);
729 // Object of the code section containing the branch being fixed up.
731 // Section index of the code section containing the branch begin fixed up.
733 // Source address of original branch.
734 Arm_address source_address_;
735 // Destination address of the original branch.
736 Arm_address destination_address_;
737 // Original branch instruction. This is needed for copying the condition
738 // code from a condition branch to its stub.
739 uint32_t original_insn_;
742 // ARMv4 BX Rx branch relocation stub class.
743 class Arm_v4bx_stub : public Stub
749 // Return the associated register.
752 { return this->reg_; }
755 // Arm V4BX stubs are created via a stub factory. So these are protected.
756 Arm_v4bx_stub(const Stub_template* stub_template, const uint32_t reg)
757 : Stub(stub_template), reg_(reg)
760 friend class Stub_factory;
762 // Return the relocation target address of the i-th relocation in the
765 do_reloc_target(size_t)
766 { gold_unreachable(); }
768 // This may be overridden in the child class.
770 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
773 this->do_fixed_endian_v4bx_write<true>(view, view_size);
775 this->do_fixed_endian_v4bx_write<false>(view, view_size);
779 // A template to implement do_write.
780 template<bool big_endian>
782 do_fixed_endian_v4bx_write(unsigned char* view, section_size_type)
784 const Insn_template* insns = this->stub_template()->insns();
785 elfcpp::Swap<32, big_endian>::writeval(view,
787 + (this->reg_ << 16)));
788 view += insns[0].size();
789 elfcpp::Swap<32, big_endian>::writeval(view,
790 (insns[1].data() + this->reg_));
791 view += insns[1].size();
792 elfcpp::Swap<32, big_endian>::writeval(view,
793 (insns[2].data() + this->reg_));
796 // A register index (r0-r14), which is associated with the stub.
800 // Stub factory class.
805 // Return the unique instance of this class.
806 static const Stub_factory&
809 static Stub_factory singleton;
813 // Make a relocation stub.
815 make_reloc_stub(Stub_type stub_type) const
817 gold_assert(stub_type >= arm_stub_reloc_first
818 && stub_type <= arm_stub_reloc_last);
819 return new Reloc_stub(this->stub_templates_[stub_type]);
822 // Make a Cortex-A8 stub.
824 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
825 Arm_address source, Arm_address destination,
826 uint32_t original_insn) const
828 gold_assert(stub_type >= arm_stub_cortex_a8_first
829 && stub_type <= arm_stub_cortex_a8_last);
830 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
831 source, destination, original_insn);
834 // Make an ARM V4BX relocation stub.
835 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
837 make_arm_v4bx_stub(uint32_t reg) const
839 gold_assert(reg < 0xf);
840 return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
845 // Constructor and destructor are protected since we only return a single
846 // instance created in Stub_factory::get_instance().
850 // A Stub_factory may not be copied since it is a singleton.
851 Stub_factory(const Stub_factory&);
852 Stub_factory& operator=(Stub_factory&);
854 // Stub templates. These are initialized in the constructor.
855 const Stub_template* stub_templates_[arm_stub_type_last+1];
858 // A class to hold stubs for the ARM target.
860 template<bool big_endian>
861 class Stub_table : public Output_data
864 Stub_table(Arm_input_section<big_endian>* owner)
865 : Output_data(), owner_(owner), reloc_stubs_(), cortex_a8_stubs_(),
866 arm_v4bx_stubs_(0xf), prev_data_size_(0), prev_addralign_(1)
872 // Owner of this stub table.
873 Arm_input_section<big_endian>*
875 { return this->owner_; }
877 // Whether this stub table is empty.
881 return (this->reloc_stubs_.empty()
882 && this->cortex_a8_stubs_.empty()
883 && this->arm_v4bx_stubs_.empty());
886 // Return the current data size.
888 current_data_size() const
889 { return this->current_data_size_for_child(); }
891 // Add a STUB with using KEY. Caller is reponsible for avoid adding
892 // if already a STUB with the same key has been added.
894 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
896 const Stub_template* stub_template = stub->stub_template();
897 gold_assert(stub_template->type() == key.stub_type());
898 this->reloc_stubs_[key] = stub;
901 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
902 // Caller is reponsible for avoid adding if already a STUB with the same
903 // address has been added.
905 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
907 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
908 this->cortex_a8_stubs_.insert(value);
911 // Add an ARM V4BX relocation stub. A register index will be retrieved
914 add_arm_v4bx_stub(Arm_v4bx_stub* stub)
916 gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
917 this->arm_v4bx_stubs_[stub->reg()] = stub;
920 // Remove all Cortex-A8 stubs.
922 remove_all_cortex_a8_stubs();
924 // Look up a relocation stub using KEY. Return NULL if there is none.
926 find_reloc_stub(const Reloc_stub::Key& key) const
928 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
929 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
932 // Look up an arm v4bx relocation stub using the register index.
933 // Return NULL if there is none.
935 find_arm_v4bx_stub(const uint32_t reg) const
937 gold_assert(reg < 0xf);
938 return this->arm_v4bx_stubs_[reg];
941 // Relocate stubs in this stub table.
943 relocate_stubs(const Relocate_info<32, big_endian>*,
944 Target_arm<big_endian>*, Output_section*,
945 unsigned char*, Arm_address, section_size_type);
947 // Update data size and alignment at the end of a relaxation pass. Return
948 // true if either data size or alignment is different from that of the
949 // previous relaxation pass.
951 update_data_size_and_addralign();
953 // Finalize stubs. Set the offsets of all stubs and mark input sections
954 // needing the Cortex-A8 workaround.
958 // Apply Cortex-A8 workaround to an address range.
960 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
961 unsigned char*, Arm_address,
965 // Write out section contents.
967 do_write(Output_file*);
969 // Return the required alignment.
972 { return this->prev_addralign_; }
974 // Reset address and file offset.
976 do_reset_address_and_file_offset()
977 { this->set_current_data_size_for_child(this->prev_data_size_); }
979 // Set final data size.
981 set_final_data_size()
982 { this->set_data_size(this->current_data_size()); }
985 // Relocate one stub.
987 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
988 Target_arm<big_endian>*, Output_section*,
989 unsigned char*, Arm_address, section_size_type);
991 // Unordered map of relocation stubs.
993 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
994 Reloc_stub::Key::equal_to>
997 // List of Cortex-A8 stubs ordered by addresses of branches being
998 // fixed up in output.
999 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
1000 // List of Arm V4BX relocation stubs ordered by associated registers.
1001 typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
1003 // Owner of this stub table.
1004 Arm_input_section<big_endian>* owner_;
1005 // The relocation stubs.
1006 Reloc_stub_map reloc_stubs_;
1007 // The cortex_a8_stubs.
1008 Cortex_a8_stub_list cortex_a8_stubs_;
1009 // The Arm V4BX relocation stubs.
1010 Arm_v4bx_stub_list arm_v4bx_stubs_;
1011 // data size of this in the previous pass.
1012 off_t prev_data_size_;
1013 // address alignment of this in the previous pass.
1014 uint64_t prev_addralign_;
1017 // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1018 // we add to the end of an EXIDX input section that goes into the output.
1020 class Arm_exidx_cantunwind : public Output_section_data
1023 Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
1024 : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
1027 // Return the object containing the section pointed by this.
1030 { return this->relobj_; }
1032 // Return the section index of the section pointed by this.
1035 { return this->shndx_; }
1039 do_write(Output_file* of)
1041 if (parameters->target().is_big_endian())
1042 this->do_fixed_endian_write<true>(of);
1044 this->do_fixed_endian_write<false>(of);
1048 // Implement do_write for a given endianity.
1049 template<bool big_endian>
1051 do_fixed_endian_write(Output_file*);
1053 // The object containing the section pointed by this.
1055 // The section index of the section pointed by this.
1056 unsigned int shndx_;
1059 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1060 // Offset map is used to map input section offset within the EXIDX section
1061 // to the output offset from the start of this EXIDX section.
1063 typedef std::map<section_offset_type, section_offset_type>
1064 Arm_exidx_section_offset_map;
1066 // Arm_exidx_merged_section class. This represents an EXIDX input section
1067 // with some of its entries merged.
1069 class Arm_exidx_merged_section : public Output_relaxed_input_section
1072 // Constructor for Arm_exidx_merged_section.
1073 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1074 // SECTION_OFFSET_MAP points to a section offset map describing how
1075 // parts of the input section are mapped to output. DELETED_BYTES is
1076 // the number of bytes deleted from the EXIDX input section.
1077 Arm_exidx_merged_section(
1078 const Arm_exidx_input_section& exidx_input_section,
1079 const Arm_exidx_section_offset_map& section_offset_map,
1080 uint32_t deleted_bytes);
1082 // Return the original EXIDX input section.
1083 const Arm_exidx_input_section&
1084 exidx_input_section() const
1085 { return this->exidx_input_section_; }
1087 // Return the section offset map.
1088 const Arm_exidx_section_offset_map&
1089 section_offset_map() const
1090 { return this->section_offset_map_; }
1093 // Write merged section into file OF.
1095 do_write(Output_file* of);
1098 do_output_offset(const Relobj*, unsigned int, section_offset_type,
1099 section_offset_type*) const;
1102 // Original EXIDX input section.
1103 const Arm_exidx_input_section& exidx_input_section_;
1104 // Section offset map.
1105 const Arm_exidx_section_offset_map& section_offset_map_;
1108 // A class to wrap an ordinary input section containing executable code.
1110 template<bool big_endian>
1111 class Arm_input_section : public Output_relaxed_input_section
1114 Arm_input_section(Relobj* relobj, unsigned int shndx)
1115 : Output_relaxed_input_section(relobj, shndx, 1),
1116 original_addralign_(1), original_size_(0), stub_table_(NULL)
1119 ~Arm_input_section()
1126 // Whether this is a stub table owner.
1128 is_stub_table_owner() const
1129 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1131 // Return the stub table.
1132 Stub_table<big_endian>*
1134 { return this->stub_table_; }
1136 // Set the stub_table.
1138 set_stub_table(Stub_table<big_endian>* stub_table)
1139 { this->stub_table_ = stub_table; }
1141 // Downcast a base pointer to an Arm_input_section pointer. This is
1142 // not type-safe but we only use Arm_input_section not the base class.
1143 static Arm_input_section<big_endian>*
1144 as_arm_input_section(Output_relaxed_input_section* poris)
1145 { return static_cast<Arm_input_section<big_endian>*>(poris); }
1148 // Write data to output file.
1150 do_write(Output_file*);
1152 // Return required alignment of this.
1154 do_addralign() const
1156 if (this->is_stub_table_owner())
1157 return std::max(this->stub_table_->addralign(),
1158 this->original_addralign_);
1160 return this->original_addralign_;
1163 // Finalize data size.
1165 set_final_data_size();
1167 // Reset address and file offset.
1169 do_reset_address_and_file_offset();
1173 do_output_offset(const Relobj* object, unsigned int shndx,
1174 section_offset_type offset,
1175 section_offset_type* poutput) const
1177 if ((object == this->relobj())
1178 && (shndx == this->shndx())
1180 && (convert_types<uint64_t, section_offset_type>(offset)
1181 <= this->original_size_))
1191 // Copying is not allowed.
1192 Arm_input_section(const Arm_input_section&);
1193 Arm_input_section& operator=(const Arm_input_section&);
1195 // Address alignment of the original input section.
1196 uint64_t original_addralign_;
1197 // Section size of the original input section.
1198 uint64_t original_size_;
1200 Stub_table<big_endian>* stub_table_;
1203 // Arm_exidx_fixup class. This is used to define a number of methods
1204 // and keep states for fixing up EXIDX coverage.
1206 class Arm_exidx_fixup
1209 Arm_exidx_fixup(Output_section* exidx_output_section)
1210 : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1211 last_inlined_entry_(0), last_input_section_(NULL),
1212 section_offset_map_(NULL), first_output_text_section_(NULL)
1216 { delete this->section_offset_map_; }
1218 // Process an EXIDX section for entry merging. Return number of bytes to
1219 // be deleted in output. If parts of the input EXIDX section are merged
1220 // a heap allocated Arm_exidx_section_offset_map is store in the located
1221 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1223 template<bool big_endian>
1225 process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1226 Arm_exidx_section_offset_map** psection_offset_map);
1228 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1229 // input section, if there is not one already.
1231 add_exidx_cantunwind_as_needed();
1233 // Return the output section for the text section which is linked to the
1234 // first exidx input in output.
1236 first_output_text_section() const
1237 { return this->first_output_text_section_; }
1240 // Copying is not allowed.
1241 Arm_exidx_fixup(const Arm_exidx_fixup&);
1242 Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1244 // Type of EXIDX unwind entry.
1249 // EXIDX_CANTUNWIND.
1250 UT_EXIDX_CANTUNWIND,
1257 // Process an EXIDX entry. We only care about the second word of the
1258 // entry. Return true if the entry can be deleted.
1260 process_exidx_entry(uint32_t second_word);
1262 // Update the current section offset map during EXIDX section fix-up.
1263 // If there is no map, create one. INPUT_OFFSET is the offset of a
1264 // reference point, DELETED_BYTES is the number of deleted by in the
1265 // section so far. If DELETE_ENTRY is true, the reference point and
1266 // all offsets after the previous reference point are discarded.
1268 update_offset_map(section_offset_type input_offset,
1269 section_size_type deleted_bytes, bool delete_entry);
1271 // EXIDX output section.
1272 Output_section* exidx_output_section_;
1273 // Unwind type of the last EXIDX entry processed.
1274 Unwind_type last_unwind_type_;
1275 // Last seen inlined EXIDX entry.
1276 uint32_t last_inlined_entry_;
1277 // Last processed EXIDX input section.
1278 const Arm_exidx_input_section* last_input_section_;
1279 // Section offset map created in process_exidx_section.
1280 Arm_exidx_section_offset_map* section_offset_map_;
1281 // Output section for the text section which is linked to the first exidx
1283 Output_section* first_output_text_section_;
1286 // Arm output section class. This is defined mainly to add a number of
1287 // stub generation methods.
1289 template<bool big_endian>
1290 class Arm_output_section : public Output_section
1293 typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
1295 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1296 elfcpp::Elf_Xword flags)
1297 : Output_section(name, type, flags)
1300 ~Arm_output_section()
1303 // Group input sections for stub generation.
1305 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1307 // Downcast a base pointer to an Arm_output_section pointer. This is
1308 // not type-safe but we only use Arm_output_section not the base class.
1309 static Arm_output_section<big_endian>*
1310 as_arm_output_section(Output_section* os)
1311 { return static_cast<Arm_output_section<big_endian>*>(os); }
1313 // Append all input text sections in this into LIST.
1315 append_text_sections_to_list(Text_section_list* list);
1317 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1318 // is a list of text input sections sorted in ascending order of their
1319 // output addresses.
1321 fix_exidx_coverage(const Text_section_list& sorted_text_section,
1322 Symbol_table* symtab);
1326 typedef Output_section::Input_section Input_section;
1327 typedef Output_section::Input_section_list Input_section_list;
1329 // Create a stub group.
1330 void create_stub_group(Input_section_list::const_iterator,
1331 Input_section_list::const_iterator,
1332 Input_section_list::const_iterator,
1333 Target_arm<big_endian>*,
1334 std::vector<Output_relaxed_input_section*>*);
1337 // Arm_exidx_input_section class. This represents an EXIDX input section.
1339 class Arm_exidx_input_section
1342 static const section_offset_type invalid_offset =
1343 static_cast<section_offset_type>(-1);
1345 Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1346 unsigned int link, uint32_t size, uint32_t addralign)
1347 : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1348 addralign_(addralign)
1351 ~Arm_exidx_input_section()
1354 // Accessors: This is a read-only class.
1356 // Return the object containing this EXIDX input section.
1359 { return this->relobj_; }
1361 // Return the section index of this EXIDX input section.
1364 { return this->shndx_; }
1366 // Return the section index of linked text section in the same object.
1369 { return this->link_; }
1371 // Return size of the EXIDX input section.
1374 { return this->size_; }
1376 // Reutnr address alignment of EXIDX input section.
1379 { return this->addralign_; }
1382 // Object containing this.
1384 // Section index of this.
1385 unsigned int shndx_;
1386 // text section linked to this in the same object.
1388 // Size of this. For ARM 32-bit is sufficient.
1390 // Address alignment of this. For ARM 32-bit is sufficient.
1391 uint32_t addralign_;
1394 // Arm_relobj class.
1396 template<bool big_endian>
1397 class Arm_relobj : public Sized_relobj<32, big_endian>
1400 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1402 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1403 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1404 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1405 stub_tables_(), local_symbol_is_thumb_function_(),
1406 attributes_section_data_(NULL), mapping_symbols_info_(),
1407 section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
1408 output_local_symbol_count_needs_update_(false)
1412 { delete this->attributes_section_data_; }
1414 // Return the stub table of the SHNDX-th section if there is one.
1415 Stub_table<big_endian>*
1416 stub_table(unsigned int shndx) const
1418 gold_assert(shndx < this->stub_tables_.size());
1419 return this->stub_tables_[shndx];
1422 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1424 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1426 gold_assert(shndx < this->stub_tables_.size());
1427 this->stub_tables_[shndx] = stub_table;
1430 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1431 // index. This is only valid after do_count_local_symbol is called.
1433 local_symbol_is_thumb_function(unsigned int r_sym) const
1435 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1436 return this->local_symbol_is_thumb_function_[r_sym];
1439 // Scan all relocation sections for stub generation.
1441 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1444 // Convert regular input section with index SHNDX to a relaxed section.
1446 convert_input_section_to_relaxed_section(unsigned shndx)
1448 // The stubs have relocations and we need to process them after writing
1449 // out the stubs. So relocation now must follow section write.
1450 this->set_section_offset(shndx, -1ULL);
1451 this->set_relocs_must_follow_section_writes();
1454 // Downcast a base pointer to an Arm_relobj pointer. This is
1455 // not type-safe but we only use Arm_relobj not the base class.
1456 static Arm_relobj<big_endian>*
1457 as_arm_relobj(Relobj* relobj)
1458 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1460 // Processor-specific flags in ELF file header. This is valid only after
1463 processor_specific_flags() const
1464 { return this->processor_specific_flags_; }
1466 // Attribute section data This is the contents of the .ARM.attribute section
1468 const Attributes_section_data*
1469 attributes_section_data() const
1470 { return this->attributes_section_data_; }
1472 // Mapping symbol location.
1473 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1475 // Functor for STL container.
1476 struct Mapping_symbol_position_less
1479 operator()(const Mapping_symbol_position& p1,
1480 const Mapping_symbol_position& p2) const
1482 return (p1.first < p2.first
1483 || (p1.first == p2.first && p1.second < p2.second));
1487 // We only care about the first character of a mapping symbol, so
1488 // we only store that instead of the whole symbol name.
1489 typedef std::map<Mapping_symbol_position, char,
1490 Mapping_symbol_position_less> Mapping_symbols_info;
1492 // Whether a section contains any Cortex-A8 workaround.
1494 section_has_cortex_a8_workaround(unsigned int shndx) const
1496 return (this->section_has_cortex_a8_workaround_ != NULL
1497 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1500 // Mark a section that has Cortex-A8 workaround.
1502 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1504 if (this->section_has_cortex_a8_workaround_ == NULL)
1505 this->section_has_cortex_a8_workaround_ =
1506 new std::vector<bool>(this->shnum(), false);
1507 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1510 // Return the EXIDX section of an text section with index SHNDX or NULL
1511 // if the text section has no associated EXIDX section.
1512 const Arm_exidx_input_section*
1513 exidx_input_section_by_link(unsigned int shndx) const
1515 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1516 return ((p != this->exidx_section_map_.end()
1517 && p->second->link() == shndx)
1522 // Return the EXIDX section with index SHNDX or NULL if there is none.
1523 const Arm_exidx_input_section*
1524 exidx_input_section_by_shndx(unsigned shndx) const
1526 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1527 return ((p != this->exidx_section_map_.end()
1528 && p->second->shndx() == shndx)
1533 // Whether output local symbol count needs updating.
1535 output_local_symbol_count_needs_update() const
1536 { return this->output_local_symbol_count_needs_update_; }
1538 // Set output_local_symbol_count_needs_update flag to be true.
1540 set_output_local_symbol_count_needs_update()
1541 { this->output_local_symbol_count_needs_update_ = true; }
1543 // Update output local symbol count at the end of relaxation.
1545 update_output_local_symbol_count();
1548 // Post constructor setup.
1552 // Call parent's setup method.
1553 Sized_relobj<32, big_endian>::do_setup();
1555 // Initialize look-up tables.
1556 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1557 this->stub_tables_.swap(empty_stub_table_list);
1560 // Count the local symbols.
1562 do_count_local_symbols(Stringpool_template<char>*,
1563 Stringpool_template<char>*);
1566 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1567 const unsigned char* pshdrs,
1568 typename Sized_relobj<32, big_endian>::Views* pivews);
1570 // Read the symbol information.
1572 do_read_symbols(Read_symbols_data* sd);
1574 // Process relocs for garbage collection.
1576 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1580 // Whether a section needs to be scanned for relocation stubs.
1582 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1583 const Relobj::Output_sections&,
1584 const Symbol_table *, const unsigned char*);
1586 // Whether a section is a scannable text section.
1588 section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
1589 const Output_section*, const Symbol_table *);
1591 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1593 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1594 unsigned int, Output_section*,
1595 const Symbol_table *);
1597 // Scan a section for the Cortex-A8 erratum.
1599 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1600 unsigned int, Output_section*,
1601 Target_arm<big_endian>*);
1603 // Find the linked text section of an EXIDX section by looking at the
1604 // first reloction of the EXIDX section. PSHDR points to the section
1605 // headers of a relocation section and PSYMS points to the local symbols.
1606 // PSHNDX points to a location storing the text section index if found.
1607 // Return whether we can find the linked section.
1609 find_linked_text_section(const unsigned char* pshdr,
1610 const unsigned char* psyms, unsigned int* pshndx);
1613 // Make a new Arm_exidx_input_section object for EXIDX section with
1614 // index SHNDX and section header SHDR. TEXT_SHNDX is the section
1615 // index of the linked text section.
1617 make_exidx_input_section(unsigned int shndx,
1618 const elfcpp::Shdr<32, big_endian>& shdr,
1619 unsigned int text_shndx);
1621 // Return the output address of either a plain input section or a
1622 // relaxed input section. SHNDX is the section index.
1624 simple_input_section_output_address(unsigned int, Output_section*);
1626 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1627 typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1630 // List of stub tables.
1631 Stub_table_list stub_tables_;
1632 // Bit vector to tell if a local symbol is a thumb function or not.
1633 // This is only valid after do_count_local_symbol is called.
1634 std::vector<bool> local_symbol_is_thumb_function_;
1635 // processor-specific flags in ELF file header.
1636 elfcpp::Elf_Word processor_specific_flags_;
1637 // Object attributes if there is an .ARM.attributes section or NULL.
1638 Attributes_section_data* attributes_section_data_;
1639 // Mapping symbols information.
1640 Mapping_symbols_info mapping_symbols_info_;
1641 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1642 std::vector<bool>* section_has_cortex_a8_workaround_;
1643 // Map a text section to its associated .ARM.exidx section, if there is one.
1644 Exidx_section_map exidx_section_map_;
1645 // Whether output local symbol count needs updating.
1646 bool output_local_symbol_count_needs_update_;
1649 // Arm_dynobj class.
1651 template<bool big_endian>
1652 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1655 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1656 const elfcpp::Ehdr<32, big_endian>& ehdr)
1657 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1658 processor_specific_flags_(0), attributes_section_data_(NULL)
1662 { delete this->attributes_section_data_; }
1664 // Downcast a base pointer to an Arm_relobj pointer. This is
1665 // not type-safe but we only use Arm_relobj not the base class.
1666 static Arm_dynobj<big_endian>*
1667 as_arm_dynobj(Dynobj* dynobj)
1668 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1670 // Processor-specific flags in ELF file header. This is valid only after
1673 processor_specific_flags() const
1674 { return this->processor_specific_flags_; }
1676 // Attributes section data.
1677 const Attributes_section_data*
1678 attributes_section_data() const
1679 { return this->attributes_section_data_; }
1682 // Read the symbol information.
1684 do_read_symbols(Read_symbols_data* sd);
1687 // processor-specific flags in ELF file header.
1688 elfcpp::Elf_Word processor_specific_flags_;
1689 // Object attributes if there is an .ARM.attributes section or NULL.
1690 Attributes_section_data* attributes_section_data_;
1693 // Functor to read reloc addends during stub generation.
1695 template<int sh_type, bool big_endian>
1696 struct Stub_addend_reader
1698 // Return the addend for a relocation of a particular type. Depending
1699 // on whether this is a REL or RELA relocation, read the addend from a
1700 // view or from a Reloc object.
1701 elfcpp::Elf_types<32>::Elf_Swxword
1703 unsigned int /* r_type */,
1704 const unsigned char* /* view */,
1705 const typename Reloc_types<sh_type,
1706 32, big_endian>::Reloc& /* reloc */) const;
1709 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1711 template<bool big_endian>
1712 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1714 elfcpp::Elf_types<32>::Elf_Swxword
1717 const unsigned char*,
1718 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1721 // Specialized Stub_addend_reader for RELA type relocation sections.
1722 // We currently do not handle RELA type relocation sections but it is trivial
1723 // to implement the addend reader. This is provided for completeness and to
1724 // make it easier to add support for RELA relocation sections in the future.
1726 template<bool big_endian>
1727 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1729 elfcpp::Elf_types<32>::Elf_Swxword
1732 const unsigned char*,
1733 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1734 big_endian>::Reloc& reloc) const
1735 { return reloc.get_r_addend(); }
1738 // Cortex_a8_reloc class. We keep record of relocation that may need
1739 // the Cortex-A8 erratum workaround.
1741 class Cortex_a8_reloc
1744 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1745 Arm_address destination)
1746 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1752 // Accessors: This is a read-only class.
1754 // Return the relocation stub associated with this relocation if there is
1758 { return this->reloc_stub_; }
1760 // Return the relocation type.
1763 { return this->r_type_; }
1765 // Return the destination address of the relocation. LSB stores the THUMB
1769 { return this->destination_; }
1772 // Associated relocation stub if there is one, or NULL.
1773 const Reloc_stub* reloc_stub_;
1775 unsigned int r_type_;
1776 // Destination address of this relocation. LSB is used to distinguish
1778 Arm_address destination_;
1781 // Utilities for manipulating integers of up to 32-bits
1785 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1786 // an int32_t. NO_BITS must be between 1 to 32.
1787 template<int no_bits>
1788 static inline int32_t
1789 sign_extend(uint32_t bits)
1791 gold_assert(no_bits >= 0 && no_bits <= 32);
1793 return static_cast<int32_t>(bits);
1794 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1796 uint32_t top_bit = 1U << (no_bits - 1);
1797 int32_t as_signed = static_cast<int32_t>(bits);
1798 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1801 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1802 template<int no_bits>
1804 has_overflow(uint32_t bits)
1806 gold_assert(no_bits >= 0 && no_bits <= 32);
1809 int32_t max = (1 << (no_bits - 1)) - 1;
1810 int32_t min = -(1 << (no_bits - 1));
1811 int32_t as_signed = static_cast<int32_t>(bits);
1812 return as_signed > max || as_signed < min;
1815 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1816 // fits in the given number of bits as either a signed or unsigned value.
1817 // For example, has_signed_unsigned_overflow<8> would check
1818 // -128 <= bits <= 255
1819 template<int no_bits>
1821 has_signed_unsigned_overflow(uint32_t bits)
1823 gold_assert(no_bits >= 2 && no_bits <= 32);
1826 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1827 int32_t min = -(1 << (no_bits - 1));
1828 int32_t as_signed = static_cast<int32_t>(bits);
1829 return as_signed > max || as_signed < min;
1832 // Select bits from A and B using bits in MASK. For each n in [0..31],
1833 // the n-th bit in the result is chosen from the n-th bits of A and B.
1834 // A zero selects A and a one selects B.
1835 static inline uint32_t
1836 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1837 { return (a & ~mask) | (b & mask); }
1840 template<bool big_endian>
1841 class Target_arm : public Sized_target<32, big_endian>
1844 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1847 // When were are relocating a stub, we pass this as the relocation number.
1848 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1851 : Sized_target<32, big_endian>(&arm_info),
1852 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1853 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
1854 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
1855 stub_tables_(), stub_factory_(Stub_factory::get_instance()),
1856 may_use_blx_(false), should_force_pic_veneer_(false),
1857 arm_input_section_map_(), attributes_section_data_(NULL),
1858 fix_cortex_a8_(false), cortex_a8_relocs_info_()
1861 // Whether we can use BLX.
1864 { return this->may_use_blx_; }
1866 // Set use-BLX flag.
1868 set_may_use_blx(bool value)
1869 { this->may_use_blx_ = value; }
1871 // Whether we force PCI branch veneers.
1873 should_force_pic_veneer() const
1874 { return this->should_force_pic_veneer_; }
1876 // Set PIC veneer flag.
1878 set_should_force_pic_veneer(bool value)
1879 { this->should_force_pic_veneer_ = value; }
1881 // Whether we use THUMB-2 instructions.
1883 using_thumb2() const
1885 Object_attribute* attr =
1886 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1887 int arch = attr->int_value();
1888 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
1891 // Whether we use THUMB/THUMB-2 instructions only.
1893 using_thumb_only() const
1895 Object_attribute* attr =
1896 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1897 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1898 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1900 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1901 return attr->int_value() == 'M';
1904 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1906 may_use_arm_nop() const
1908 Object_attribute* attr =
1909 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1910 int arch = attr->int_value();
1911 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1912 || arch == elfcpp::TAG_CPU_ARCH_V6K
1913 || arch == elfcpp::TAG_CPU_ARCH_V7
1914 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1917 // Whether we have THUMB-2 NOP.W instruction.
1919 may_use_thumb2_nop() const
1921 Object_attribute* attr =
1922 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1923 int arch = attr->int_value();
1924 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1925 || arch == elfcpp::TAG_CPU_ARCH_V7
1926 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1929 // Process the relocations to determine unreferenced sections for
1930 // garbage collection.
1932 gc_process_relocs(Symbol_table* symtab,
1934 Sized_relobj<32, big_endian>* object,
1935 unsigned int data_shndx,
1936 unsigned int sh_type,
1937 const unsigned char* prelocs,
1939 Output_section* output_section,
1940 bool needs_special_offset_handling,
1941 size_t local_symbol_count,
1942 const unsigned char* plocal_symbols);
1944 // Scan the relocations to look for symbol adjustments.
1946 scan_relocs(Symbol_table* symtab,
1948 Sized_relobj<32, big_endian>* object,
1949 unsigned int data_shndx,
1950 unsigned int sh_type,
1951 const unsigned char* prelocs,
1953 Output_section* output_section,
1954 bool needs_special_offset_handling,
1955 size_t local_symbol_count,
1956 const unsigned char* plocal_symbols);
1958 // Finalize the sections.
1960 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1962 // Return the value to use for a dynamic symbol which requires special
1965 do_dynsym_value(const Symbol*) const;
1967 // Relocate a section.
1969 relocate_section(const Relocate_info<32, big_endian>*,
1970 unsigned int sh_type,
1971 const unsigned char* prelocs,
1973 Output_section* output_section,
1974 bool needs_special_offset_handling,
1975 unsigned char* view,
1976 Arm_address view_address,
1977 section_size_type view_size,
1978 const Reloc_symbol_changes*);
1980 // Scan the relocs during a relocatable link.
1982 scan_relocatable_relocs(Symbol_table* symtab,
1984 Sized_relobj<32, big_endian>* object,
1985 unsigned int data_shndx,
1986 unsigned int sh_type,
1987 const unsigned char* prelocs,
1989 Output_section* output_section,
1990 bool needs_special_offset_handling,
1991 size_t local_symbol_count,
1992 const unsigned char* plocal_symbols,
1993 Relocatable_relocs*);
1995 // Relocate a section during a relocatable link.
1997 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1998 unsigned int sh_type,
1999 const unsigned char* prelocs,
2001 Output_section* output_section,
2002 off_t offset_in_output_section,
2003 const Relocatable_relocs*,
2004 unsigned char* view,
2005 Arm_address view_address,
2006 section_size_type view_size,
2007 unsigned char* reloc_view,
2008 section_size_type reloc_view_size);
2010 // Return whether SYM is defined by the ABI.
2012 do_is_defined_by_abi(Symbol* sym) const
2013 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
2015 // Return whether there is a GOT section.
2017 has_got_section() const
2018 { return this->got_ != NULL; }
2020 // Return the size of the GOT section.
2024 gold_assert(this->got_ != NULL);
2025 return this->got_->data_size();
2028 // Map platform-specific reloc types
2030 get_real_reloc_type (unsigned int r_type);
2033 // Methods to support stub-generations.
2036 // Return the stub factory
2038 stub_factory() const
2039 { return this->stub_factory_; }
2041 // Make a new Arm_input_section object.
2042 Arm_input_section<big_endian>*
2043 new_arm_input_section(Relobj*, unsigned int);
2045 // Find the Arm_input_section object corresponding to the SHNDX-th input
2046 // section of RELOBJ.
2047 Arm_input_section<big_endian>*
2048 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
2050 // Make a new Stub_table
2051 Stub_table<big_endian>*
2052 new_stub_table(Arm_input_section<big_endian>*);
2054 // Scan a section for stub generation.
2056 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2057 const unsigned char*, size_t, Output_section*,
2058 bool, const unsigned char*, Arm_address,
2063 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
2064 Output_section*, unsigned char*, Arm_address,
2067 // Get the default ARM target.
2068 static Target_arm<big_endian>*
2071 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2072 && parameters->target().is_big_endian() == big_endian);
2073 return static_cast<Target_arm<big_endian>*>(
2074 parameters->sized_target<32, big_endian>());
2077 // Whether NAME belongs to a mapping symbol.
2079 is_mapping_symbol_name(const char* name)
2083 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2084 && (name[2] == '\0' || name[2] == '.'));
2087 // Whether we work around the Cortex-A8 erratum.
2089 fix_cortex_a8() const
2090 { return this->fix_cortex_a8_; }
2092 // Whether we fix R_ARM_V4BX relocation.
2094 // 1 - replace with MOV instruction (armv4 target)
2095 // 2 - make interworking veneer (>= armv4t targets only)
2096 General_options::Fix_v4bx
2098 { return parameters->options().fix_v4bx(); }
2100 // Scan a span of THUMB code section for Cortex-A8 erratum.
2102 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2103 section_size_type, section_size_type,
2104 const unsigned char*, Arm_address);
2106 // Apply Cortex-A8 workaround to a branch.
2108 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2109 unsigned char*, Arm_address);
2112 // Make an ELF object.
2114 do_make_elf_object(const std::string&, Input_file*, off_t,
2115 const elfcpp::Ehdr<32, big_endian>& ehdr);
2118 do_make_elf_object(const std::string&, Input_file*, off_t,
2119 const elfcpp::Ehdr<32, !big_endian>&)
2120 { gold_unreachable(); }
2123 do_make_elf_object(const std::string&, Input_file*, off_t,
2124 const elfcpp::Ehdr<64, false>&)
2125 { gold_unreachable(); }
2128 do_make_elf_object(const std::string&, Input_file*, off_t,
2129 const elfcpp::Ehdr<64, true>&)
2130 { gold_unreachable(); }
2132 // Make an output section.
2134 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2135 elfcpp::Elf_Xword flags)
2136 { return new Arm_output_section<big_endian>(name, type, flags); }
2139 do_adjust_elf_header(unsigned char* view, int len) const;
2141 // We only need to generate stubs, and hence perform relaxation if we are
2142 // not doing relocatable linking.
2144 do_may_relax() const
2145 { return !parameters->options().relocatable(); }
2148 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
2150 // Determine whether an object attribute tag takes an integer, a
2153 do_attribute_arg_type(int tag) const;
2155 // Reorder tags during output.
2157 do_attributes_order(int num) const;
2159 // This is called when the target is selected as the default.
2161 do_select_as_default_target()
2163 // No locking is required since there should only be one default target.
2164 // We cannot have both the big-endian and little-endian ARM targets
2166 gold_assert(arm_reloc_property_table == NULL);
2167 arm_reloc_property_table = new Arm_reloc_property_table();
2171 // The class which scans relocations.
2176 : issued_non_pic_error_(false)
2180 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
2181 Sized_relobj<32, big_endian>* object,
2182 unsigned int data_shndx,
2183 Output_section* output_section,
2184 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2185 const elfcpp::Sym<32, big_endian>& lsym);
2188 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
2189 Sized_relobj<32, big_endian>* object,
2190 unsigned int data_shndx,
2191 Output_section* output_section,
2192 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2196 local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2197 Sized_relobj<32, big_endian>* ,
2200 const elfcpp::Rel<32, big_endian>& ,
2202 const elfcpp::Sym<32, big_endian>&)
2206 global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2207 Sized_relobj<32, big_endian>* ,
2210 const elfcpp::Rel<32, big_endian>& ,
2211 unsigned int , Symbol*)
2216 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
2217 unsigned int r_type);
2220 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
2221 unsigned int r_type, Symbol*);
2224 check_non_pic(Relobj*, unsigned int r_type);
2226 // Almost identical to Symbol::needs_plt_entry except that it also
2227 // handles STT_ARM_TFUNC.
2229 symbol_needs_plt_entry(const Symbol* sym)
2231 // An undefined symbol from an executable does not need a PLT entry.
2232 if (sym->is_undefined() && !parameters->options().shared())
2235 return (!parameters->doing_static_link()
2236 && (sym->type() == elfcpp::STT_FUNC
2237 || sym->type() == elfcpp::STT_ARM_TFUNC)
2238 && (sym->is_from_dynobj()
2239 || sym->is_undefined()
2240 || sym->is_preemptible()));
2243 // Whether we have issued an error about a non-PIC compilation.
2244 bool issued_non_pic_error_;
2247 // The class which implements relocation.
2257 // Return whether the static relocation needs to be applied.
2259 should_apply_static_reloc(const Sized_symbol<32>* gsym,
2262 Output_section* output_section);
2264 // Do a relocation. Return false if the caller should not issue
2265 // any warnings about this relocation.
2267 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2268 Output_section*, size_t relnum,
2269 const elfcpp::Rel<32, big_endian>&,
2270 unsigned int r_type, const Sized_symbol<32>*,
2271 const Symbol_value<32>*,
2272 unsigned char*, Arm_address,
2275 // Return whether we want to pass flag NON_PIC_REF for this
2276 // reloc. This means the relocation type accesses a symbol not via
2279 reloc_is_non_pic (unsigned int r_type)
2283 // These relocation types reference GOT or PLT entries explicitly.
2284 case elfcpp::R_ARM_GOT_BREL:
2285 case elfcpp::R_ARM_GOT_ABS:
2286 case elfcpp::R_ARM_GOT_PREL:
2287 case elfcpp::R_ARM_GOT_BREL12:
2288 case elfcpp::R_ARM_PLT32_ABS:
2289 case elfcpp::R_ARM_TLS_GD32:
2290 case elfcpp::R_ARM_TLS_LDM32:
2291 case elfcpp::R_ARM_TLS_IE32:
2292 case elfcpp::R_ARM_TLS_IE12GP:
2294 // These relocate types may use PLT entries.
2295 case elfcpp::R_ARM_CALL:
2296 case elfcpp::R_ARM_THM_CALL:
2297 case elfcpp::R_ARM_JUMP24:
2298 case elfcpp::R_ARM_THM_JUMP24:
2299 case elfcpp::R_ARM_THM_JUMP19:
2300 case elfcpp::R_ARM_PLT32:
2301 case elfcpp::R_ARM_THM_XPC22:
2310 // Do a TLS relocation.
2311 inline typename Arm_relocate_functions<big_endian>::Status
2312 relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
2313 size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
2314 const Sized_symbol<32>*, const Symbol_value<32>*,
2315 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
2320 // A class which returns the size required for a relocation type,
2321 // used while scanning relocs during a relocatable link.
2322 class Relocatable_size_for_reloc
2326 get_size_for_reloc(unsigned int, Relobj*);
2329 // Adjust TLS relocation type based on the options and whether this
2330 // is a local symbol.
2331 static tls::Tls_optimization
2332 optimize_tls_reloc(bool is_final, int r_type);
2334 // Get the GOT section, creating it if necessary.
2335 Output_data_got<32, big_endian>*
2336 got_section(Symbol_table*, Layout*);
2338 // Get the GOT PLT section.
2340 got_plt_section() const
2342 gold_assert(this->got_plt_ != NULL);
2343 return this->got_plt_;
2346 // Create a PLT entry for a global symbol.
2348 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2350 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2352 define_tls_base_symbol(Symbol_table*, Layout*);
2354 // Create a GOT entry for the TLS module index.
2356 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2357 Sized_relobj<32, big_endian>* object);
2359 // Get the PLT section.
2360 const Output_data_plt_arm<big_endian>*
2363 gold_assert(this->plt_ != NULL);
2367 // Get the dynamic reloc section, creating it if necessary.
2369 rel_dyn_section(Layout*);
2371 // Get the section to use for TLS_DESC relocations.
2373 rel_tls_desc_section(Layout*) const;
2375 // Return true if the symbol may need a COPY relocation.
2376 // References from an executable object to non-function symbols
2377 // defined in a dynamic object may need a COPY relocation.
2379 may_need_copy_reloc(Symbol* gsym)
2381 return (gsym->type() != elfcpp::STT_ARM_TFUNC
2382 && gsym->may_need_copy_reloc());
2385 // Add a potential copy relocation.
2387 copy_reloc(Symbol_table* symtab, Layout* layout,
2388 Sized_relobj<32, big_endian>* object,
2389 unsigned int shndx, Output_section* output_section,
2390 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2392 this->copy_relocs_.copy_reloc(symtab, layout,
2393 symtab->get_sized_symbol<32>(sym),
2394 object, shndx, output_section, reloc,
2395 this->rel_dyn_section(layout));
2398 // Whether two EABI versions are compatible.
2400 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2402 // Merge processor-specific flags from input object and those in the ELF
2403 // header of the output.
2405 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2407 // Get the secondary compatible architecture.
2409 get_secondary_compatible_arch(const Attributes_section_data*);
2411 // Set the secondary compatible architecture.
2413 set_secondary_compatible_arch(Attributes_section_data*, int);
2416 tag_cpu_arch_combine(const char*, int, int*, int, int);
2418 // Helper to print AEABI enum tag value.
2420 aeabi_enum_name(unsigned int);
2422 // Return string value for TAG_CPU_name.
2424 tag_cpu_name_value(unsigned int);
2426 // Merge object attributes from input object and those in the output.
2428 merge_object_attributes(const char*, const Attributes_section_data*);
2430 // Helper to get an AEABI object attribute
2432 get_aeabi_object_attribute(int tag) const
2434 Attributes_section_data* pasd = this->attributes_section_data_;
2435 gold_assert(pasd != NULL);
2436 Object_attribute* attr =
2437 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2438 gold_assert(attr != NULL);
2443 // Methods to support stub-generations.
2446 // Group input sections for stub generation.
2448 group_sections(Layout*, section_size_type, bool);
2450 // Scan a relocation for stub generation.
2452 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2453 const Sized_symbol<32>*, unsigned int,
2454 const Symbol_value<32>*,
2455 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
2457 // Scan a relocation section for stub.
2458 template<int sh_type>
2460 scan_reloc_section_for_stubs(
2461 const Relocate_info<32, big_endian>* relinfo,
2462 const unsigned char* prelocs,
2464 Output_section* output_section,
2465 bool needs_special_offset_handling,
2466 const unsigned char* view,
2467 elfcpp::Elf_types<32>::Elf_Addr view_address,
2470 // Fix .ARM.exidx section coverage.
2472 fix_exidx_coverage(Layout*, Arm_output_section<big_endian>*, Symbol_table*);
2474 // Functors for STL set.
2475 struct output_section_address_less_than
2478 operator()(const Output_section* s1, const Output_section* s2) const
2479 { return s1->address() < s2->address(); }
2482 // Information about this specific target which we pass to the
2483 // general Target structure.
2484 static const Target::Target_info arm_info;
2486 // The types of GOT entries needed for this platform.
2489 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2490 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
2491 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
2492 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
2493 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
2496 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2498 // Map input section to Arm_input_section.
2499 typedef Unordered_map<Section_id,
2500 Arm_input_section<big_endian>*,
2502 Arm_input_section_map;
2504 // Map output addresses to relocs for Cortex-A8 erratum.
2505 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2506 Cortex_a8_relocs_info;
2509 Output_data_got<32, big_endian>* got_;
2511 Output_data_plt_arm<big_endian>* plt_;
2512 // The GOT PLT section.
2513 Output_data_space* got_plt_;
2514 // The dynamic reloc section.
2515 Reloc_section* rel_dyn_;
2516 // Relocs saved to avoid a COPY reloc.
2517 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2518 // Space for variables copied with a COPY reloc.
2519 Output_data_space* dynbss_;
2520 // Offset of the GOT entry for the TLS module index.
2521 unsigned int got_mod_index_offset_;
2522 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2523 bool tls_base_symbol_defined_;
2524 // Vector of Stub_tables created.
2525 Stub_table_list stub_tables_;
2527 const Stub_factory &stub_factory_;
2528 // Whether we can use BLX.
2530 // Whether we force PIC branch veneers.
2531 bool should_force_pic_veneer_;
2532 // Map for locating Arm_input_sections.
2533 Arm_input_section_map arm_input_section_map_;
2534 // Attributes section data in output.
2535 Attributes_section_data* attributes_section_data_;
2536 // Whether we want to fix code for Cortex-A8 erratum.
2537 bool fix_cortex_a8_;
2538 // Map addresses to relocs for Cortex-A8 erratum.
2539 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2542 template<bool big_endian>
2543 const Target::Target_info Target_arm<big_endian>::arm_info =
2546 big_endian, // is_big_endian
2547 elfcpp::EM_ARM, // machine_code
2548 false, // has_make_symbol
2549 false, // has_resolve
2550 false, // has_code_fill
2551 true, // is_default_stack_executable
2553 "/usr/lib/libc.so.1", // dynamic_linker
2554 0x8000, // default_text_segment_address
2555 0x1000, // abi_pagesize (overridable by -z max-page-size)
2556 0x1000, // common_pagesize (overridable by -z common-page-size)
2557 elfcpp::SHN_UNDEF, // small_common_shndx
2558 elfcpp::SHN_UNDEF, // large_common_shndx
2559 0, // small_common_section_flags
2560 0, // large_common_section_flags
2561 ".ARM.attributes", // attributes_section
2562 "aeabi" // attributes_vendor
2565 // Arm relocate functions class
2568 template<bool big_endian>
2569 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2574 STATUS_OKAY, // No error during relocation.
2575 STATUS_OVERFLOW, // Relocation oveflow.
2576 STATUS_BAD_RELOC // Relocation cannot be applied.
2580 typedef Relocate_functions<32, big_endian> Base;
2581 typedef Arm_relocate_functions<big_endian> This;
2583 // Encoding of imm16 argument for movt and movw ARM instructions
2586 // imm16 := imm4 | imm12
2588 // 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
2589 // +-------+---------------+-------+-------+-----------------------+
2590 // | | |imm4 | |imm12 |
2591 // +-------+---------------+-------+-------+-----------------------+
2593 // Extract the relocation addend from VAL based on the ARM
2594 // instruction encoding described above.
2595 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2596 extract_arm_movw_movt_addend(
2597 typename elfcpp::Swap<32, big_endian>::Valtype val)
2599 // According to the Elf ABI for ARM Architecture the immediate
2600 // field is sign-extended to form the addend.
2601 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2604 // Insert X into VAL based on the ARM instruction encoding described
2606 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2607 insert_val_arm_movw_movt(
2608 typename elfcpp::Swap<32, big_endian>::Valtype val,
2609 typename elfcpp::Swap<32, big_endian>::Valtype x)
2613 val |= (x & 0xf000) << 4;
2617 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2620 // imm16 := imm4 | i | imm3 | imm8
2622 // 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
2623 // +---------+-+-----------+-------++-+-----+-------+---------------+
2624 // | |i| |imm4 || |imm3 | |imm8 |
2625 // +---------+-+-----------+-------++-+-----+-------+---------------+
2627 // Extract the relocation addend from VAL based on the Thumb2
2628 // instruction encoding described above.
2629 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2630 extract_thumb_movw_movt_addend(
2631 typename elfcpp::Swap<32, big_endian>::Valtype val)
2633 // According to the Elf ABI for ARM Architecture the immediate
2634 // field is sign-extended to form the addend.
2635 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2636 | ((val >> 15) & 0x0800)
2637 | ((val >> 4) & 0x0700)
2641 // Insert X into VAL based on the Thumb2 instruction encoding
2643 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2644 insert_val_thumb_movw_movt(
2645 typename elfcpp::Swap<32, big_endian>::Valtype val,
2646 typename elfcpp::Swap<32, big_endian>::Valtype x)
2649 val |= (x & 0xf000) << 4;
2650 val |= (x & 0x0800) << 15;
2651 val |= (x & 0x0700) << 4;
2652 val |= (x & 0x00ff);
2656 // Calculate the smallest constant Kn for the specified residual.
2657 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2659 calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
2665 // Determine the most significant bit in the residual and
2666 // align the resulting value to a 2-bit boundary.
2667 for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
2669 // The desired shift is now (msb - 6), or zero, whichever
2671 return (((msb - 6) < 0) ? 0 : (msb - 6));
2674 // Calculate the final residual for the specified group index.
2675 // If the passed group index is less than zero, the method will return
2676 // the value of the specified residual without any change.
2677 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2678 static typename elfcpp::Swap<32, big_endian>::Valtype
2679 calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2682 for (int n = 0; n <= group; n++)
2684 // Calculate which part of the value to mask.
2685 uint32_t shift = calc_grp_kn(residual);
2686 // Calculate the residual for the next time around.
2687 residual &= ~(residual & (0xff << shift));
2693 // Calculate the value of Gn for the specified group index.
2694 // We return it in the form of an encoded constant-and-rotation.
2695 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2696 static typename elfcpp::Swap<32, big_endian>::Valtype
2697 calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2700 typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
2703 for (int n = 0; n <= group; n++)
2705 // Calculate which part of the value to mask.
2706 shift = calc_grp_kn(residual);
2707 // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
2708 gn = residual & (0xff << shift);
2709 // Calculate the residual for the next time around.
2712 // Return Gn in the form of an encoded constant-and-rotation.
2713 return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
2717 // Handle ARM long branches.
2718 static typename This::Status
2719 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2720 unsigned char *, const Sized_symbol<32>*,
2721 const Arm_relobj<big_endian>*, unsigned int,
2722 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2724 // Handle THUMB long branches.
2725 static typename This::Status
2726 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2727 unsigned char *, const Sized_symbol<32>*,
2728 const Arm_relobj<big_endian>*, unsigned int,
2729 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2732 // Return the branch offset of a 32-bit THUMB branch.
2733 static inline int32_t
2734 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2736 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2737 // involving the J1 and J2 bits.
2738 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2739 uint32_t upper = upper_insn & 0x3ffU;
2740 uint32_t lower = lower_insn & 0x7ffU;
2741 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2742 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2743 uint32_t i1 = j1 ^ s ? 0 : 1;
2744 uint32_t i2 = j2 ^ s ? 0 : 1;
2746 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2747 | (upper << 12) | (lower << 1));
2750 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2751 // UPPER_INSN is the original upper instruction of the branch. Caller is
2752 // responsible for overflow checking and BLX offset adjustment.
2753 static inline uint16_t
2754 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2756 uint32_t s = offset < 0 ? 1 : 0;
2757 uint32_t bits = static_cast<uint32_t>(offset);
2758 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2761 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2762 // LOWER_INSN is the original lower instruction of the branch. Caller is
2763 // responsible for overflow checking and BLX offset adjustment.
2764 static inline uint16_t
2765 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2767 uint32_t s = offset < 0 ? 1 : 0;
2768 uint32_t bits = static_cast<uint32_t>(offset);
2769 return ((lower_insn & ~0x2fffU)
2770 | ((((bits >> 23) & 1) ^ !s) << 13)
2771 | ((((bits >> 22) & 1) ^ !s) << 11)
2772 | ((bits >> 1) & 0x7ffU));
2775 // Return the branch offset of a 32-bit THUMB conditional branch.
2776 static inline int32_t
2777 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2779 uint32_t s = (upper_insn & 0x0400U) >> 10;
2780 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2781 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2782 uint32_t lower = (lower_insn & 0x07ffU);
2783 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2785 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2788 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2789 // instruction. UPPER_INSN is the original upper instruction of the branch.
2790 // Caller is responsible for overflow checking.
2791 static inline uint16_t
2792 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2794 uint32_t s = offset < 0 ? 1 : 0;
2795 uint32_t bits = static_cast<uint32_t>(offset);
2796 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2799 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2800 // instruction. LOWER_INSN is the original lower instruction of the branch.
2801 // Caller is reponsible for overflow checking.
2802 static inline uint16_t
2803 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2805 uint32_t bits = static_cast<uint32_t>(offset);
2806 uint32_t j2 = (bits & 0x00080000U) >> 19;
2807 uint32_t j1 = (bits & 0x00040000U) >> 18;
2808 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2810 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2813 // R_ARM_ABS8: S + A
2814 static inline typename This::Status
2815 abs8(unsigned char *view,
2816 const Sized_relobj<32, big_endian>* object,
2817 const Symbol_value<32>* psymval)
2819 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2820 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2821 Valtype* wv = reinterpret_cast<Valtype*>(view);
2822 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2823 Reltype addend = utils::sign_extend<8>(val);
2824 Reltype x = psymval->value(object, addend);
2825 val = utils::bit_select(val, x, 0xffU);
2826 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2827 return (utils::has_signed_unsigned_overflow<8>(x)
2828 ? This::STATUS_OVERFLOW
2829 : This::STATUS_OKAY);
2832 // R_ARM_THM_ABS5: S + A
2833 static inline typename This::Status
2834 thm_abs5(unsigned char *view,
2835 const Sized_relobj<32, big_endian>* object,
2836 const Symbol_value<32>* psymval)
2838 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2839 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2840 Valtype* wv = reinterpret_cast<Valtype*>(view);
2841 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2842 Reltype addend = (val & 0x7e0U) >> 6;
2843 Reltype x = psymval->value(object, addend);
2844 val = utils::bit_select(val, x << 6, 0x7e0U);
2845 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2846 return (utils::has_overflow<5>(x)
2847 ? This::STATUS_OVERFLOW
2848 : This::STATUS_OKAY);
2851 // R_ARM_ABS12: S + A
2852 static inline typename This::Status
2853 abs12(unsigned char *view,
2854 const Sized_relobj<32, big_endian>* object,
2855 const Symbol_value<32>* psymval)
2857 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2858 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2859 Valtype* wv = reinterpret_cast<Valtype*>(view);
2860 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2861 Reltype addend = val & 0x0fffU;
2862 Reltype x = psymval->value(object, addend);
2863 val = utils::bit_select(val, x, 0x0fffU);
2864 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2865 return (utils::has_overflow<12>(x)
2866 ? This::STATUS_OVERFLOW
2867 : This::STATUS_OKAY);
2870 // R_ARM_ABS16: S + A
2871 static inline typename This::Status
2872 abs16(unsigned char *view,
2873 const Sized_relobj<32, big_endian>* object,
2874 const Symbol_value<32>* psymval)
2876 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2877 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2878 Valtype* wv = reinterpret_cast<Valtype*>(view);
2879 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2880 Reltype addend = utils::sign_extend<16>(val);
2881 Reltype x = psymval->value(object, addend);
2882 val = utils::bit_select(val, x, 0xffffU);
2883 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2884 return (utils::has_signed_unsigned_overflow<16>(x)
2885 ? This::STATUS_OVERFLOW
2886 : This::STATUS_OKAY);
2889 // R_ARM_ABS32: (S + A) | T
2890 static inline typename This::Status
2891 abs32(unsigned char *view,
2892 const Sized_relobj<32, big_endian>* object,
2893 const Symbol_value<32>* psymval,
2894 Arm_address thumb_bit)
2896 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2897 Valtype* wv = reinterpret_cast<Valtype*>(view);
2898 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2899 Valtype x = psymval->value(object, addend) | thumb_bit;
2900 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2901 return This::STATUS_OKAY;
2904 // R_ARM_REL32: (S + A) | T - P
2905 static inline typename This::Status
2906 rel32(unsigned char *view,
2907 const Sized_relobj<32, big_endian>* object,
2908 const Symbol_value<32>* psymval,
2909 Arm_address address,
2910 Arm_address thumb_bit)
2912 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2913 Valtype* wv = reinterpret_cast<Valtype*>(view);
2914 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2915 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2916 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2917 return This::STATUS_OKAY;
2920 // R_ARM_THM_JUMP24: (S + A) | T - P
2921 static typename This::Status
2922 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
2923 const Symbol_value<32>* psymval, Arm_address address,
2924 Arm_address thumb_bit);
2926 // R_ARM_THM_JUMP6: S + A – P
2927 static inline typename This::Status
2928 thm_jump6(unsigned char *view,
2929 const Sized_relobj<32, big_endian>* object,
2930 const Symbol_value<32>* psymval,
2931 Arm_address address)
2933 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2934 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2935 Valtype* wv = reinterpret_cast<Valtype*>(view);
2936 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2937 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
2938 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
2939 Reltype x = (psymval->value(object, addend) - address);
2940 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
2941 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2942 // CZB does only forward jumps.
2943 return ((x > 0x007e)
2944 ? This::STATUS_OVERFLOW
2945 : This::STATUS_OKAY);
2948 // R_ARM_THM_JUMP8: S + A – P
2949 static inline typename This::Status
2950 thm_jump8(unsigned char *view,
2951 const Sized_relobj<32, big_endian>* object,
2952 const Symbol_value<32>* psymval,
2953 Arm_address address)
2955 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2956 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2957 Valtype* wv = reinterpret_cast<Valtype*>(view);
2958 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2959 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
2960 Reltype x = (psymval->value(object, addend) - address);
2961 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
2962 return (utils::has_overflow<8>(x)
2963 ? This::STATUS_OVERFLOW
2964 : This::STATUS_OKAY);
2967 // R_ARM_THM_JUMP11: S + A – P
2968 static inline typename This::Status
2969 thm_jump11(unsigned char *view,
2970 const Sized_relobj<32, big_endian>* object,
2971 const Symbol_value<32>* psymval,
2972 Arm_address address)
2974 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2975 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2976 Valtype* wv = reinterpret_cast<Valtype*>(view);
2977 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2978 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
2979 Reltype x = (psymval->value(object, addend) - address);
2980 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
2981 return (utils::has_overflow<11>(x)
2982 ? This::STATUS_OVERFLOW
2983 : This::STATUS_OKAY);
2986 // R_ARM_BASE_PREL: B(S) + A - P
2987 static inline typename This::Status
2988 base_prel(unsigned char* view,
2990 Arm_address address)
2992 Base::rel32(view, origin - address);
2996 // R_ARM_BASE_ABS: B(S) + A
2997 static inline typename This::Status
2998 base_abs(unsigned char* view,
3001 Base::rel32(view, origin);
3005 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3006 static inline typename This::Status
3007 got_brel(unsigned char* view,
3008 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
3010 Base::rel32(view, got_offset);
3011 return This::STATUS_OKAY;
3014 // R_ARM_GOT_PREL: GOT(S) + A - P
3015 static inline typename This::Status
3016 got_prel(unsigned char *view,
3017 Arm_address got_entry,
3018 Arm_address address)
3020 Base::rel32(view, got_entry - address);
3021 return This::STATUS_OKAY;
3024 // R_ARM_PREL: (S + A) | T - P
3025 static inline typename This::Status
3026 prel31(unsigned char *view,
3027 const Sized_relobj<32, big_endian>* object,
3028 const Symbol_value<32>* psymval,
3029 Arm_address address,
3030 Arm_address thumb_bit)
3032 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3033 Valtype* wv = reinterpret_cast<Valtype*>(view);
3034 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3035 Valtype addend = utils::sign_extend<31>(val);
3036 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3037 val = utils::bit_select(val, x, 0x7fffffffU);
3038 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3039 return (utils::has_overflow<31>(x) ?
3040 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3043 // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
3044 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3045 // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3046 // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
3047 static inline typename This::Status
3048 movw(unsigned char* view,
3049 const Sized_relobj<32, big_endian>* object,
3050 const Symbol_value<32>* psymval,
3051 Arm_address relative_address_base,
3052 Arm_address thumb_bit,
3053 bool check_overflow)
3055 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3056 Valtype* wv = reinterpret_cast<Valtype*>(view);
3057 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3058 Valtype addend = This::extract_arm_movw_movt_addend(val);
3059 Valtype x = ((psymval->value(object, addend) | thumb_bit)
3060 - relative_address_base);
3061 val = This::insert_val_arm_movw_movt(val, x);
3062 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3063 return ((check_overflow && utils::has_overflow<16>(x))
3064 ? This::STATUS_OVERFLOW
3065 : This::STATUS_OKAY);
3068 // R_ARM_MOVT_ABS: S + A (relative address base is 0)
3069 // R_ARM_MOVT_PREL: S + A - P
3070 // R_ARM_MOVT_BREL: S + A - B(S)
3071 static inline typename This::Status
3072 movt(unsigned char* view,
3073 const Sized_relobj<32, big_endian>* object,
3074 const Symbol_value<32>* psymval,
3075 Arm_address relative_address_base)
3077 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3078 Valtype* wv = reinterpret_cast<Valtype*>(view);
3079 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3080 Valtype addend = This::extract_arm_movw_movt_addend(val);
3081 Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3082 val = This::insert_val_arm_movw_movt(val, x);
3083 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3084 // FIXME: IHI0044D says that we should check for overflow.
3085 return This::STATUS_OKAY;
3088 // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
3089 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3090 // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3091 // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
3092 static inline typename This::Status
3093 thm_movw(unsigned char *view,
3094 const Sized_relobj<32, big_endian>* object,
3095 const Symbol_value<32>* psymval,
3096 Arm_address relative_address_base,
3097 Arm_address thumb_bit,
3098 bool check_overflow)
3100 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3101 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3102 Valtype* wv = reinterpret_cast<Valtype*>(view);
3103 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3104 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3105 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3107 (psymval->value(object, addend) | thumb_bit) - relative_address_base;
3108 val = This::insert_val_thumb_movw_movt(val, x);
3109 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3110 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3111 return ((check_overflow && utils::has_overflow<16>(x))
3112 ? This::STATUS_OVERFLOW
3113 : This::STATUS_OKAY);
3116 // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
3117 // R_ARM_THM_MOVT_PREL: S + A - P
3118 // R_ARM_THM_MOVT_BREL: S + A - B(S)
3119 static inline typename This::Status
3120 thm_movt(unsigned char* view,
3121 const Sized_relobj<32, big_endian>* object,
3122 const Symbol_value<32>* psymval,
3123 Arm_address relative_address_base)
3125 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3126 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3127 Valtype* wv = reinterpret_cast<Valtype*>(view);
3128 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3129 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3130 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3131 Reltype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3132 val = This::insert_val_thumb_movw_movt(val, x);
3133 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3134 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3135 return This::STATUS_OKAY;
3138 // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3139 static inline typename This::Status
3140 thm_alu11(unsigned char* view,
3141 const Sized_relobj<32, big_endian>* object,
3142 const Symbol_value<32>* psymval,
3143 Arm_address address,
3144 Arm_address thumb_bit)
3146 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3147 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3148 Valtype* wv = reinterpret_cast<Valtype*>(view);
3149 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3150 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3152 // 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
3153 // -----------------------------------------------------------------------
3154 // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
3155 // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
3156 // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
3157 // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
3158 // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
3159 // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
3161 // Determine a sign for the addend.
3162 const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
3163 || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3164 // Thumb2 addend encoding:
3165 // imm12 := i | imm3 | imm8
3166 int32_t addend = (insn & 0xff)
3167 | ((insn & 0x00007000) >> 4)
3168 | ((insn & 0x04000000) >> 15);
3169 // Apply a sign to the added.
3172 int32_t x = (psymval->value(object, addend) | thumb_bit)
3173 - (address & 0xfffffffc);
3174 Reltype val = abs(x);
3175 // Mask out the value and a distinct part of the ADD/SUB opcode
3176 // (bits 7:5 of opword).
3177 insn = (insn & 0xfb0f8f00)
3179 | ((val & 0x700) << 4)
3180 | ((val & 0x800) << 15);
3181 // Set the opcode according to whether the value to go in the
3182 // place is negative.
3186 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3187 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3188 return ((val > 0xfff) ?
3189 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3192 // R_ARM_THM_PC8: S + A - Pa (Thumb)
3193 static inline typename This::Status
3194 thm_pc8(unsigned char* view,
3195 const Sized_relobj<32, big_endian>* object,
3196 const Symbol_value<32>* psymval,
3197 Arm_address address)
3199 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3200 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3201 Valtype* wv = reinterpret_cast<Valtype*>(view);
3202 Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
3203 Reltype addend = ((insn & 0x00ff) << 2);
3204 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3205 Reltype val = abs(x);
3206 insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
3208 elfcpp::Swap<16, big_endian>::writeval(wv, insn);
3209 return ((val > 0x03fc)
3210 ? This::STATUS_OVERFLOW
3211 : This::STATUS_OKAY);
3214 // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3215 static inline typename This::Status
3216 thm_pc12(unsigned char* view,
3217 const Sized_relobj<32, big_endian>* object,
3218 const Symbol_value<32>* psymval,
3219 Arm_address address)
3221 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3222 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3223 Valtype* wv = reinterpret_cast<Valtype*>(view);
3224 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3225 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3226 // Determine a sign for the addend (positive if the U bit is 1).
3227 const int sign = (insn & 0x00800000) ? 1 : -1;
3228 int32_t addend = (insn & 0xfff);
3229 // Apply a sign to the added.
3232 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3233 Reltype val = abs(x);
3234 // Mask out and apply the value and the U bit.
3235 insn = (insn & 0xff7ff000) | (val & 0xfff);
3236 // Set the U bit according to whether the value to go in the
3237 // place is positive.
3241 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3242 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3243 return ((val > 0xfff) ?
3244 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3248 static inline typename This::Status
3249 v4bx(const Relocate_info<32, big_endian>* relinfo,
3250 unsigned char *view,
3251 const Arm_relobj<big_endian>* object,
3252 const Arm_address address,
3253 const bool is_interworking)
3256 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3257 Valtype* wv = reinterpret_cast<Valtype*>(view);
3258 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3260 // Ensure that we have a BX instruction.
3261 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3262 const uint32_t reg = (val & 0xf);
3263 if (is_interworking && reg != 0xf)
3265 Stub_table<big_endian>* stub_table =
3266 object->stub_table(relinfo->data_shndx);
3267 gold_assert(stub_table != NULL);
3269 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3270 gold_assert(stub != NULL);
3272 int32_t veneer_address =
3273 stub_table->address() + stub->offset() - 8 - address;
3274 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3275 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3276 // Replace with a branch to veneer (B <addr>)
3277 val = (val & 0xf0000000) | 0x0a000000
3278 | ((veneer_address >> 2) & 0x00ffffff);
3282 // Preserve Rm (lowest four bits) and the condition code
3283 // (highest four bits). Other bits encode MOV PC,Rm.
3284 val = (val & 0xf000000f) | 0x01a0f000;
3286 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3287 return This::STATUS_OKAY;
3290 // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3291 // R_ARM_ALU_PC_G0: ((S + A) | T) - P
3292 // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3293 // R_ARM_ALU_PC_G1: ((S + A) | T) - P
3294 // R_ARM_ALU_PC_G2: ((S + A) | T) - P
3295 // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3296 // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
3297 // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3298 // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
3299 // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
3300 static inline typename This::Status
3301 arm_grp_alu(unsigned char* view,
3302 const Sized_relobj<32, big_endian>* object,
3303 const Symbol_value<32>* psymval,
3305 Arm_address address,
3306 Arm_address thumb_bit,
3307 bool check_overflow)
3309 gold_assert(group >= 0 && group < 3);
3310 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3311 Valtype* wv = reinterpret_cast<Valtype*>(view);
3312 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3314 // ALU group relocations are allowed only for the ADD/SUB instructions.
3315 // (0x00800000 - ADD, 0x00400000 - SUB)
3316 const Valtype opcode = insn & 0x01e00000;
3317 if (opcode != 0x00800000 && opcode != 0x00400000)
3318 return This::STATUS_BAD_RELOC;
3320 // Determine a sign for the addend.
3321 const int sign = (opcode == 0x00800000) ? 1 : -1;
3322 // shifter = rotate_imm * 2
3323 const uint32_t shifter = (insn & 0xf00) >> 7;
3324 // Initial addend value.
3325 int32_t addend = insn & 0xff;
3326 // Rotate addend right by shifter.
3327 addend = (addend >> shifter) | (addend << (32 - shifter));
3328 // Apply a sign to the added.
3331 int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
3332 Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
3333 // Check for overflow if required
3335 && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
3336 return This::STATUS_OVERFLOW;
3338 // Mask out the value and the ADD/SUB part of the opcode; take care
3339 // not to destroy the S bit.
3341 // Set the opcode according to whether the value to go in the
3342 // place is negative.
3343 insn |= ((x < 0) ? 0x00400000 : 0x00800000);
3344 // Encode the offset (encoded Gn).
3347 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3348 return This::STATUS_OKAY;
3351 // R_ARM_LDR_PC_G0: S + A - P
3352 // R_ARM_LDR_PC_G1: S + A - P
3353 // R_ARM_LDR_PC_G2: S + A - P
3354 // R_ARM_LDR_SB_G0: S + A - B(S)
3355 // R_ARM_LDR_SB_G1: S + A - B(S)
3356 // R_ARM_LDR_SB_G2: S + A - B(S)
3357 static inline typename This::Status
3358 arm_grp_ldr(unsigned char* view,
3359 const Sized_relobj<32, big_endian>* object,
3360 const Symbol_value<32>* psymval,
3362 Arm_address address)
3364 gold_assert(group >= 0 && group < 3);
3365 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3366 Valtype* wv = reinterpret_cast<Valtype*>(view);
3367 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3369 const int sign = (insn & 0x00800000) ? 1 : -1;
3370 int32_t addend = (insn & 0xfff) * sign;
3371 int32_t x = (psymval->value(object, addend) - address);
3372 // Calculate the relevant G(n-1) value to obtain this stage residual.
3374 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3375 if (residual >= 0x1000)
3376 return This::STATUS_OVERFLOW;
3378 // Mask out the value and U bit.
3380 // Set the U bit for non-negative values.
3385 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3386 return This::STATUS_OKAY;
3389 // R_ARM_LDRS_PC_G0: S + A - P
3390 // R_ARM_LDRS_PC_G1: S + A - P
3391 // R_ARM_LDRS_PC_G2: S + A - P
3392 // R_ARM_LDRS_SB_G0: S + A - B(S)
3393 // R_ARM_LDRS_SB_G1: S + A - B(S)
3394 // R_ARM_LDRS_SB_G2: S + A - B(S)
3395 static inline typename This::Status
3396 arm_grp_ldrs(unsigned char* view,
3397 const Sized_relobj<32, big_endian>* object,
3398 const Symbol_value<32>* psymval,
3400 Arm_address address)
3402 gold_assert(group >= 0 && group < 3);
3403 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3404 Valtype* wv = reinterpret_cast<Valtype*>(view);
3405 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3407 const int sign = (insn & 0x00800000) ? 1 : -1;
3408 int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
3409 int32_t x = (psymval->value(object, addend) - address);
3410 // Calculate the relevant G(n-1) value to obtain this stage residual.
3412 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3413 if (residual >= 0x100)
3414 return This::STATUS_OVERFLOW;
3416 // Mask out the value and U bit.
3418 // Set the U bit for non-negative values.
3421 insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
3423 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3424 return This::STATUS_OKAY;
3427 // R_ARM_LDC_PC_G0: S + A - P
3428 // R_ARM_LDC_PC_G1: S + A - P
3429 // R_ARM_LDC_PC_G2: S + A - P
3430 // R_ARM_LDC_SB_G0: S + A - B(S)
3431 // R_ARM_LDC_SB_G1: S + A - B(S)
3432 // R_ARM_LDC_SB_G2: S + A - B(S)
3433 static inline typename This::Status
3434 arm_grp_ldc(unsigned char* view,
3435 const Sized_relobj<32, big_endian>* object,
3436 const Symbol_value<32>* psymval,
3438 Arm_address address)
3440 gold_assert(group >= 0 && group < 3);
3441 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3442 Valtype* wv = reinterpret_cast<Valtype*>(view);
3443 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3445 const int sign = (insn & 0x00800000) ? 1 : -1;
3446 int32_t addend = ((insn & 0xff) << 2) * sign;
3447 int32_t x = (psymval->value(object, addend) - address);
3448 // Calculate the relevant G(n-1) value to obtain this stage residual.
3450 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3451 if ((residual & 0x3) != 0 || residual >= 0x400)
3452 return This::STATUS_OVERFLOW;
3454 // Mask out the value and U bit.
3456 // Set the U bit for non-negative values.
3459 insn |= (residual >> 2);
3461 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3462 return This::STATUS_OKAY;
3466 // Relocate ARM long branches. This handles relocation types
3467 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3468 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3469 // undefined and we do not use PLT in this relocation. In such a case,
3470 // the branch is converted into an NOP.
3472 template<bool big_endian>
3473 typename Arm_relocate_functions<big_endian>::Status
3474 Arm_relocate_functions<big_endian>::arm_branch_common(
3475 unsigned int r_type,
3476 const Relocate_info<32, big_endian>* relinfo,
3477 unsigned char *view,
3478 const Sized_symbol<32>* gsym,
3479 const Arm_relobj<big_endian>* object,
3481 const Symbol_value<32>* psymval,
3482 Arm_address address,
3483 Arm_address thumb_bit,
3484 bool is_weakly_undefined_without_plt)
3486 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3487 Valtype* wv = reinterpret_cast<Valtype*>(view);
3488 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3490 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3491 && ((val & 0x0f000000UL) == 0x0a000000UL);
3492 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3493 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3494 && ((val & 0x0f000000UL) == 0x0b000000UL);
3495 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3496 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3498 // Check that the instruction is valid.
3499 if (r_type == elfcpp::R_ARM_CALL)
3501 if (!insn_is_uncond_bl && !insn_is_blx)
3502 return This::STATUS_BAD_RELOC;
3504 else if (r_type == elfcpp::R_ARM_JUMP24)
3506 if (!insn_is_b && !insn_is_cond_bl)
3507 return This::STATUS_BAD_RELOC;
3509 else if (r_type == elfcpp::R_ARM_PLT32)
3511 if (!insn_is_any_branch)
3512 return This::STATUS_BAD_RELOC;
3514 else if (r_type == elfcpp::R_ARM_XPC25)
3516 // FIXME: AAELF document IH0044C does not say much about it other
3517 // than it being obsolete.
3518 if (!insn_is_any_branch)
3519 return This::STATUS_BAD_RELOC;
3524 // A branch to an undefined weak symbol is turned into a jump to
3525 // the next instruction unless a PLT entry will be created.
3526 // Do the same for local undefined symbols.
3527 // The jump to the next instruction is optimized as a NOP depending
3528 // on the architecture.
3529 const Target_arm<big_endian>* arm_target =
3530 Target_arm<big_endian>::default_target();
3531 if (is_weakly_undefined_without_plt)
3533 Valtype cond = val & 0xf0000000U;
3534 if (arm_target->may_use_arm_nop())
3535 val = cond | 0x0320f000;
3537 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3538 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3539 return This::STATUS_OKAY;
3542 Valtype addend = utils::sign_extend<26>(val << 2);
3543 Valtype branch_target = psymval->value(object, addend);
3544 int32_t branch_offset = branch_target - address;
3546 // We need a stub if the branch offset is too large or if we need
3548 bool may_use_blx = arm_target->may_use_blx();
3549 Reloc_stub* stub = NULL;
3550 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
3551 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3552 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
3554 Stub_type stub_type =
3555 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
3557 if (stub_type != arm_stub_none)
3559 Stub_table<big_endian>* stub_table =
3560 object->stub_table(relinfo->data_shndx);
3561 gold_assert(stub_table != NULL);
3563 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3564 stub = stub_table->find_reloc_stub(stub_key);
3565 gold_assert(stub != NULL);
3566 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3567 branch_target = stub_table->address() + stub->offset() + addend;
3568 branch_offset = branch_target - address;
3569 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
3570 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
3574 // At this point, if we still need to switch mode, the instruction
3575 // must either be a BLX or a BL that can be converted to a BLX.
3579 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3580 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3583 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3584 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3585 return (utils::has_overflow<26>(branch_offset)
3586 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3589 // Relocate THUMB long branches. This handles relocation types
3590 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3591 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3592 // undefined and we do not use PLT in this relocation. In such a case,
3593 // the branch is converted into an NOP.
3595 template<bool big_endian>
3596 typename Arm_relocate_functions<big_endian>::Status
3597 Arm_relocate_functions<big_endian>::thumb_branch_common(
3598 unsigned int r_type,
3599 const Relocate_info<32, big_endian>* relinfo,
3600 unsigned char *view,
3601 const Sized_symbol<32>* gsym,
3602 const Arm_relobj<big_endian>* object,
3604 const Symbol_value<32>* psymval,
3605 Arm_address address,
3606 Arm_address thumb_bit,
3607 bool is_weakly_undefined_without_plt)
3609 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3610 Valtype* wv = reinterpret_cast<Valtype*>(view);
3611 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3612 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3614 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3616 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
3617 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
3619 // Check that the instruction is valid.
3620 if (r_type == elfcpp::R_ARM_THM_CALL)
3622 if (!is_bl_insn && !is_blx_insn)
3623 return This::STATUS_BAD_RELOC;
3625 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
3627 // This cannot be a BLX.
3629 return This::STATUS_BAD_RELOC;
3631 else if (r_type == elfcpp::R_ARM_THM_XPC22)
3633 // Check for Thumb to Thumb call.
3635 return This::STATUS_BAD_RELOC;
3638 gold_warning(_("%s: Thumb BLX instruction targets "
3639 "thumb function '%s'."),
3640 object->name().c_str(),
3641 (gsym ? gsym->name() : "(local)"));
3642 // Convert BLX to BL.
3643 lower_insn |= 0x1000U;
3649 // A branch to an undefined weak symbol is turned into a jump to
3650 // the next instruction unless a PLT entry will be created.
3651 // The jump to the next instruction is optimized as a NOP.W for
3652 // Thumb-2 enabled architectures.
3653 const Target_arm<big_endian>* arm_target =
3654 Target_arm<big_endian>::default_target();
3655 if (is_weakly_undefined_without_plt)
3657 if (arm_target->may_use_thumb2_nop())
3659 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
3660 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
3664 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
3665 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
3667 return This::STATUS_OKAY;
3670 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
3671 Arm_address branch_target = psymval->value(object, addend);
3672 int32_t branch_offset = branch_target - address;
3674 // We need a stub if the branch offset is too large or if we need
3676 bool may_use_blx = arm_target->may_use_blx();
3677 bool thumb2 = arm_target->using_thumb2();
3679 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3680 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3682 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3683 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3684 || ((thumb_bit == 0)
3685 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3686 || r_type == elfcpp::R_ARM_THM_JUMP24)))
3688 Stub_type stub_type =
3689 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
3691 if (stub_type != arm_stub_none)
3693 Stub_table<big_endian>* stub_table =
3694 object->stub_table(relinfo->data_shndx);
3695 gold_assert(stub_table != NULL);
3697 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3698 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
3699 gold_assert(stub != NULL);
3700 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3701 branch_target = stub_table->address() + stub->offset() + addend;
3702 branch_offset = branch_target - address;
3706 // At this point, if we still need to switch mode, the instruction
3707 // must either be a BLX or a BL that can be converted to a BLX.
3710 gold_assert(may_use_blx
3711 && (r_type == elfcpp::R_ARM_THM_CALL
3712 || r_type == elfcpp::R_ARM_THM_XPC22));
3713 // Make sure this is a BLX.
3714 lower_insn &= ~0x1000U;
3718 // Make sure this is a BL.
3719 lower_insn |= 0x1000U;
3722 if ((lower_insn & 0x5000U) == 0x4000U)
3723 // For a BLX instruction, make sure that the relocation is rounded up
3724 // to a word boundary. This follows the semantics of the instruction
3725 // which specifies that bit 1 of the target address will come from bit
3726 // 1 of the base address.
3727 branch_offset = (branch_offset + 2) & ~3;
3729 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3730 // We use the Thumb-2 encoding, which is safe even if dealing with
3731 // a Thumb-1 instruction by virtue of our overflow check above. */
3732 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
3733 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
3735 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3736 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3739 ? utils::has_overflow<25>(branch_offset)
3740 : utils::has_overflow<23>(branch_offset))
3741 ? This::STATUS_OVERFLOW
3742 : This::STATUS_OKAY);
3745 // Relocate THUMB-2 long conditional branches.
3746 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3747 // undefined and we do not use PLT in this relocation. In such a case,
3748 // the branch is converted into an NOP.
3750 template<bool big_endian>
3751 typename Arm_relocate_functions<big_endian>::Status
3752 Arm_relocate_functions<big_endian>::thm_jump19(
3753 unsigned char *view,
3754 const Arm_relobj<big_endian>* object,
3755 const Symbol_value<32>* psymval,
3756 Arm_address address,
3757 Arm_address thumb_bit)
3759 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3760 Valtype* wv = reinterpret_cast<Valtype*>(view);
3761 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3762 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3763 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
3765 Arm_address branch_target = psymval->value(object, addend);
3766 int32_t branch_offset = branch_target - address;
3768 // ??? Should handle interworking? GCC might someday try to
3769 // use this for tail calls.
3770 // FIXME: We do support thumb entry to PLT yet.
3773 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3774 return This::STATUS_BAD_RELOC;
3777 // Put RELOCATION back into the insn.
3778 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
3779 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
3781 // Put the relocated value back in the object file:
3782 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3783 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3785 return (utils::has_overflow<21>(branch_offset)
3786 ? This::STATUS_OVERFLOW
3787 : This::STATUS_OKAY);
3790 // Get the GOT section, creating it if necessary.
3792 template<bool big_endian>
3793 Output_data_got<32, big_endian>*
3794 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
3796 if (this->got_ == NULL)
3798 gold_assert(symtab != NULL && layout != NULL);
3800 this->got_ = new Output_data_got<32, big_endian>();
3803 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3805 | elfcpp::SHF_WRITE),
3806 this->got_, false, true, true,
3809 // The old GNU linker creates a .got.plt section. We just
3810 // create another set of data in the .got section. Note that we
3811 // always create a PLT if we create a GOT, although the PLT
3813 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
3814 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3816 | elfcpp::SHF_WRITE),
3817 this->got_plt_, false, false,
3820 // The first three entries are reserved.
3821 this->got_plt_->set_current_data_size(3 * 4);
3823 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3824 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3825 Symbol_table::PREDEFINED,
3827 0, 0, elfcpp::STT_OBJECT,
3829 elfcpp::STV_HIDDEN, 0,
3835 // Get the dynamic reloc section, creating it if necessary.
3837 template<bool big_endian>
3838 typename Target_arm<big_endian>::Reloc_section*
3839 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
3841 if (this->rel_dyn_ == NULL)
3843 gold_assert(layout != NULL);
3844 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
3845 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
3846 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
3847 false, false, false);
3849 return this->rel_dyn_;
3852 // Insn_template methods.
3854 // Return byte size of an instruction template.
3857 Insn_template::size() const
3859 switch (this->type())
3862 case THUMB16_SPECIAL_TYPE:
3873 // Return alignment of an instruction template.
3876 Insn_template::alignment() const
3878 switch (this->type())
3881 case THUMB16_SPECIAL_TYPE:
3892 // Stub_template methods.
3894 Stub_template::Stub_template(
3895 Stub_type type, const Insn_template* insns,
3897 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
3898 entry_in_thumb_mode_(false), relocs_()
3902 // Compute byte size and alignment of stub template.
3903 for (size_t i = 0; i < insn_count; i++)
3905 unsigned insn_alignment = insns[i].alignment();
3906 size_t insn_size = insns[i].size();
3907 gold_assert((offset & (insn_alignment - 1)) == 0);
3908 this->alignment_ = std::max(this->alignment_, insn_alignment);
3909 switch (insns[i].type())
3911 case Insn_template::THUMB16_TYPE:
3912 case Insn_template::THUMB16_SPECIAL_TYPE:
3914 this->entry_in_thumb_mode_ = true;
3917 case Insn_template::THUMB32_TYPE:
3918 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
3919 this->relocs_.push_back(Reloc(i, offset));
3921 this->entry_in_thumb_mode_ = true;
3924 case Insn_template::ARM_TYPE:
3925 // Handle cases where the target is encoded within the
3927 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
3928 this->relocs_.push_back(Reloc(i, offset));
3931 case Insn_template::DATA_TYPE:
3932 // Entry point cannot be data.
3933 gold_assert(i != 0);
3934 this->relocs_.push_back(Reloc(i, offset));
3940 offset += insn_size;
3942 this->size_ = offset;
3947 // Template to implement do_write for a specific target endianity.
3949 template<bool big_endian>
3951 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
3953 const Stub_template* stub_template = this->stub_template();
3954 const Insn_template* insns = stub_template->insns();
3956 // FIXME: We do not handle BE8 encoding yet.
3957 unsigned char* pov = view;
3958 for (size_t i = 0; i < stub_template->insn_count(); i++)
3960 switch (insns[i].type())
3962 case Insn_template::THUMB16_TYPE:
3963 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
3965 case Insn_template::THUMB16_SPECIAL_TYPE:
3966 elfcpp::Swap<16, big_endian>::writeval(
3968 this->thumb16_special(i));
3970 case Insn_template::THUMB32_TYPE:
3972 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
3973 uint32_t lo = insns[i].data() & 0xffff;
3974 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
3975 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
3978 case Insn_template::ARM_TYPE:
3979 case Insn_template::DATA_TYPE:
3980 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
3985 pov += insns[i].size();
3987 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
3990 // Reloc_stub::Key methods.
3992 // Dump a Key as a string for debugging.
3995 Reloc_stub::Key::name() const
3997 if (this->r_sym_ == invalid_index)
3999 // Global symbol key name
4000 // <stub-type>:<symbol name>:<addend>.
4001 const std::string sym_name = this->u_.symbol->name();
4002 // We need to print two hex number and two colons. So just add 100 bytes
4003 // to the symbol name size.
4004 size_t len = sym_name.size() + 100;
4005 char* buffer = new char[len];
4006 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
4007 sym_name.c_str(), this->addend_);
4008 gold_assert(c > 0 && c < static_cast<int>(len));
4010 return std::string(buffer);
4014 // local symbol key name
4015 // <stub-type>:<object>:<r_sym>:<addend>.
4016 const size_t len = 200;
4018 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
4019 this->u_.relobj, this->r_sym_, this->addend_);
4020 gold_assert(c > 0 && c < static_cast<int>(len));
4021 return std::string(buffer);
4025 // Reloc_stub methods.
4027 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
4028 // LOCATION to DESTINATION.
4029 // This code is based on the arm_type_of_stub function in
4030 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
4034 Reloc_stub::stub_type_for_reloc(
4035 unsigned int r_type,
4036 Arm_address location,
4037 Arm_address destination,
4038 bool target_is_thumb)
4040 Stub_type stub_type = arm_stub_none;
4042 // This is a bit ugly but we want to avoid using a templated class for
4043 // big and little endianities.
4045 bool should_force_pic_veneer;
4048 if (parameters->target().is_big_endian())
4050 const Target_arm<true>* big_endian_target =
4051 Target_arm<true>::default_target();
4052 may_use_blx = big_endian_target->may_use_blx();
4053 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
4054 thumb2 = big_endian_target->using_thumb2();
4055 thumb_only = big_endian_target->using_thumb_only();
4059 const Target_arm<false>* little_endian_target =
4060 Target_arm<false>::default_target();
4061 may_use_blx = little_endian_target->may_use_blx();
4062 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
4063 thumb2 = little_endian_target->using_thumb2();
4064 thumb_only = little_endian_target->using_thumb_only();
4067 int64_t branch_offset = (int64_t)destination - location;
4069 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
4071 // Handle cases where:
4072 // - this call goes too far (different Thumb/Thumb2 max
4074 // - it's a Thumb->Arm call and blx is not available, or it's a
4075 // Thumb->Arm branch (not bl). A stub is needed in this case.
4077 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4078 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4080 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4081 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4082 || ((!target_is_thumb)
4083 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4084 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
4086 if (target_is_thumb)
4091 stub_type = (parameters->options().shared()
4092 || should_force_pic_veneer)
4095 && (r_type == elfcpp::R_ARM_THM_CALL))
4096 // V5T and above. Stub starts with ARM code, so
4097 // we must be able to switch mode before
4098 // reaching it, which is only possible for 'bl'
4099 // (ie R_ARM_THM_CALL relocation).
4100 ? arm_stub_long_branch_any_thumb_pic
4101 // On V4T, use Thumb code only.
4102 : arm_stub_long_branch_v4t_thumb_thumb_pic)
4106 && (r_type == elfcpp::R_ARM_THM_CALL))
4107 ? arm_stub_long_branch_any_any // V5T and above.
4108 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
4112 stub_type = (parameters->options().shared()
4113 || should_force_pic_veneer)
4114 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
4115 : arm_stub_long_branch_thumb_only; // non-PIC stub.
4122 // FIXME: We should check that the input section is from an
4123 // object that has interwork enabled.
4125 stub_type = (parameters->options().shared()
4126 || should_force_pic_veneer)
4129 && (r_type == elfcpp::R_ARM_THM_CALL))
4130 ? arm_stub_long_branch_any_arm_pic // V5T and above.
4131 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
4135 && (r_type == elfcpp::R_ARM_THM_CALL))
4136 ? arm_stub_long_branch_any_any // V5T and above.
4137 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
4139 // Handle v4t short branches.
4140 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4141 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4142 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4143 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4147 else if (r_type == elfcpp::R_ARM_CALL
4148 || r_type == elfcpp::R_ARM_JUMP24
4149 || r_type == elfcpp::R_ARM_PLT32)
4151 if (target_is_thumb)
4155 // FIXME: We should check that the input section is from an
4156 // object that has interwork enabled.
4158 // We have an extra 2-bytes reach because of
4159 // the mode change (bit 24 (H) of BLX encoding).
4160 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4161 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4162 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
4163 || (r_type == elfcpp::R_ARM_JUMP24)
4164 || (r_type == elfcpp::R_ARM_PLT32))
4166 stub_type = (parameters->options().shared()
4167 || should_force_pic_veneer)
4170 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
4171 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
4175 ? arm_stub_long_branch_any_any // V5T and above.
4176 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
4182 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4183 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4185 stub_type = (parameters->options().shared()
4186 || should_force_pic_veneer)
4187 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
4188 : arm_stub_long_branch_any_any; /// non-PIC.
4196 // Cortex_a8_stub methods.
4198 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4199 // I is the position of the instruction template in the stub template.
4202 Cortex_a8_stub::do_thumb16_special(size_t i)
4204 // The only use of this is to copy condition code from a conditional
4205 // branch being worked around to the corresponding conditional branch in
4207 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4209 uint16_t data = this->stub_template()->insns()[i].data();
4210 gold_assert((data & 0xff00U) == 0xd000U);
4211 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
4215 // Stub_factory methods.
4217 Stub_factory::Stub_factory()
4219 // The instruction template sequences are declared as static
4220 // objects and initialized first time the constructor runs.
4222 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4223 // to reach the stub if necessary.
4224 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
4226 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4227 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4228 // dcd R_ARM_ABS32(X)
4231 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4233 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
4235 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4236 Insn_template::arm_insn(0xe12fff1c), // bx ip
4237 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4238 // dcd R_ARM_ABS32(X)
4241 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4242 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
4244 Insn_template::thumb16_insn(0xb401), // push {r0}
4245 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4246 Insn_template::thumb16_insn(0x4684), // mov ip, r0
4247 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4248 Insn_template::thumb16_insn(0x4760), // bx ip
4249 Insn_template::thumb16_insn(0xbf00), // nop
4250 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4251 // dcd R_ARM_ABS32(X)
4254 // V4T Thumb -> Thumb long branch stub. Using the stack is not
4256 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
4258 Insn_template::thumb16_insn(0x4778), // bx pc
4259 Insn_template::thumb16_insn(0x46c0), // nop
4260 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4261 Insn_template::arm_insn(0xe12fff1c), // bx ip
4262 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4263 // dcd R_ARM_ABS32(X)
4266 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4268 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
4270 Insn_template::thumb16_insn(0x4778), // bx pc
4271 Insn_template::thumb16_insn(0x46c0), // nop
4272 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4273 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4274 // dcd R_ARM_ABS32(X)
4277 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4278 // one, when the destination is close enough.
4279 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
4281 Insn_template::thumb16_insn(0x4778), // bx pc
4282 Insn_template::thumb16_insn(0x46c0), // nop
4283 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4286 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4287 // blx to reach the stub if necessary.
4288 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
4290 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4291 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4292 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4293 // dcd R_ARM_REL32(X-4)
4296 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4297 // blx to reach the stub if necessary. We can not add into pc;
4298 // it is not guaranteed to mode switch (different in ARMv6 and
4300 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
4302 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4303 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4304 Insn_template::arm_insn(0xe12fff1c), // bx ip
4305 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4306 // dcd R_ARM_REL32(X)
4309 // V4T ARM -> ARM long branch stub, PIC.
4310 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4312 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4313 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4314 Insn_template::arm_insn(0xe12fff1c), // bx ip
4315 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4316 // dcd R_ARM_REL32(X)
4319 // V4T Thumb -> ARM long branch stub, PIC.
4320 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4322 Insn_template::thumb16_insn(0x4778), // bx pc
4323 Insn_template::thumb16_insn(0x46c0), // nop
4324 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4325 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4326 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4327 // dcd R_ARM_REL32(X)
4330 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4332 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4334 Insn_template::thumb16_insn(0xb401), // push {r0}
4335 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4336 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4337 Insn_template::thumb16_insn(0x4484), // add ip, r0
4338 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4339 Insn_template::thumb16_insn(0x4760), // bx ip
4340 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4341 // dcd R_ARM_REL32(X)
4344 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4346 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4348 Insn_template::thumb16_insn(0x4778), // bx pc
4349 Insn_template::thumb16_insn(0x46c0), // nop
4350 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4351 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4352 Insn_template::arm_insn(0xe12fff1c), // bx ip
4353 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4354 // dcd R_ARM_REL32(X)
4357 // Cortex-A8 erratum-workaround stubs.
4359 // Stub used for conditional branches (which may be beyond +/-1MB away,
4360 // so we can't use a conditional branch to reach this stub).
4367 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4369 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4370 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4371 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4375 // Stub used for b.w and bl.w instructions.
4377 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4379 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4382 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4384 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4387 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4388 // instruction (which switches to ARM mode) to point to this stub. Jump to
4389 // the real destination using an ARM-mode branch.
4390 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
4392 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4395 // Stub used to provide an interworking for R_ARM_V4BX relocation
4396 // (bx r[n] instruction).
4397 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4399 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4400 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4401 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4404 // Fill in the stub template look-up table. Stub templates are constructed
4405 // per instance of Stub_factory for fast look-up without locking
4406 // in a thread-enabled environment.
4408 this->stub_templates_[arm_stub_none] =
4409 new Stub_template(arm_stub_none, NULL, 0);
4411 #define DEF_STUB(x) \
4415 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4416 Stub_type type = arm_stub_##x; \
4417 this->stub_templates_[type] = \
4418 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4426 // Stub_table methods.
4428 // Removel all Cortex-A8 stub.
4430 template<bool big_endian>
4432 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4434 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4435 p != this->cortex_a8_stubs_.end();
4438 this->cortex_a8_stubs_.clear();
4441 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4443 template<bool big_endian>
4445 Stub_table<big_endian>::relocate_stub(
4447 const Relocate_info<32, big_endian>* relinfo,
4448 Target_arm<big_endian>* arm_target,
4449 Output_section* output_section,
4450 unsigned char* view,
4451 Arm_address address,
4452 section_size_type view_size)
4454 const Stub_template* stub_template = stub->stub_template();
4455 if (stub_template->reloc_count() != 0)
4457 // Adjust view to cover the stub only.
4458 section_size_type offset = stub->offset();
4459 section_size_type stub_size = stub_template->size();
4460 gold_assert(offset + stub_size <= view_size);
4462 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4463 address + offset, stub_size);
4467 // Relocate all stubs in this stub table.
4469 template<bool big_endian>
4471 Stub_table<big_endian>::relocate_stubs(
4472 const Relocate_info<32, big_endian>* relinfo,
4473 Target_arm<big_endian>* arm_target,
4474 Output_section* output_section,
4475 unsigned char* view,
4476 Arm_address address,
4477 section_size_type view_size)
4479 // If we are passed a view bigger than the stub table's. we need to
4481 gold_assert(address == this->address()
4483 == static_cast<section_size_type>(this->data_size())));
4485 // Relocate all relocation stubs.
4486 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4487 p != this->reloc_stubs_.end();
4489 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4490 address, view_size);
4492 // Relocate all Cortex-A8 stubs.
4493 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4494 p != this->cortex_a8_stubs_.end();
4496 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4497 address, view_size);
4499 // Relocate all ARM V4BX stubs.
4500 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4501 p != this->arm_v4bx_stubs_.end();
4505 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4506 address, view_size);
4510 // Write out the stubs to file.
4512 template<bool big_endian>
4514 Stub_table<big_endian>::do_write(Output_file* of)
4516 off_t offset = this->offset();
4517 const section_size_type oview_size =
4518 convert_to_section_size_type(this->data_size());
4519 unsigned char* const oview = of->get_output_view(offset, oview_size);
4521 // Write relocation stubs.
4522 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4523 p != this->reloc_stubs_.end();
4526 Reloc_stub* stub = p->second;
4527 Arm_address address = this->address() + stub->offset();
4529 == align_address(address,
4530 stub->stub_template()->alignment()));
4531 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4535 // Write Cortex-A8 stubs.
4536 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4537 p != this->cortex_a8_stubs_.end();
4540 Cortex_a8_stub* stub = p->second;
4541 Arm_address address = this->address() + stub->offset();
4543 == align_address(address,
4544 stub->stub_template()->alignment()));
4545 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4549 // Write ARM V4BX relocation stubs.
4550 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4551 p != this->arm_v4bx_stubs_.end();
4557 Arm_address address = this->address() + (*p)->offset();
4559 == align_address(address,
4560 (*p)->stub_template()->alignment()));
4561 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4565 of->write_output_view(this->offset(), oview_size, oview);
4568 // Update the data size and address alignment of the stub table at the end
4569 // of a relaxation pass. Return true if either the data size or the
4570 // alignment changed in this relaxation pass.
4572 template<bool big_endian>
4574 Stub_table<big_endian>::update_data_size_and_addralign()
4577 unsigned addralign = 1;
4579 // Go over all stubs in table to compute data size and address alignment.
4581 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4582 p != this->reloc_stubs_.end();
4585 const Stub_template* stub_template = p->second->stub_template();
4586 addralign = std::max(addralign, stub_template->alignment());
4587 size = (align_address(size, stub_template->alignment())
4588 + stub_template->size());
4591 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4592 p != this->cortex_a8_stubs_.end();
4595 const Stub_template* stub_template = p->second->stub_template();
4596 addralign = std::max(addralign, stub_template->alignment());
4597 size = (align_address(size, stub_template->alignment())
4598 + stub_template->size());
4601 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4602 p != this->arm_v4bx_stubs_.end();
4608 const Stub_template* stub_template = (*p)->stub_template();
4609 addralign = std::max(addralign, stub_template->alignment());
4610 size = (align_address(size, stub_template->alignment())
4611 + stub_template->size());
4614 // Check if either data size or alignment changed in this pass.
4615 // Update prev_data_size_ and prev_addralign_. These will be used
4616 // as the current data size and address alignment for the next pass.
4617 bool changed = size != this->prev_data_size_;
4618 this->prev_data_size_ = size;
4620 if (addralign != this->prev_addralign_)
4622 this->prev_addralign_ = addralign;
4627 // Finalize the stubs. This sets the offsets of the stubs within the stub
4628 // table. It also marks all input sections needing Cortex-A8 workaround.
4630 template<bool big_endian>
4632 Stub_table<big_endian>::finalize_stubs()
4635 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4636 p != this->reloc_stubs_.end();
4639 Reloc_stub* stub = p->second;
4640 const Stub_template* stub_template = stub->stub_template();
4641 uint64_t stub_addralign = stub_template->alignment();
4642 off = align_address(off, stub_addralign);
4643 stub->set_offset(off);
4644 off += stub_template->size();
4647 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4648 p != this->cortex_a8_stubs_.end();
4651 Cortex_a8_stub* stub = p->second;
4652 const Stub_template* stub_template = stub->stub_template();
4653 uint64_t stub_addralign = stub_template->alignment();
4654 off = align_address(off, stub_addralign);
4655 stub->set_offset(off);
4656 off += stub_template->size();
4658 // Mark input section so that we can determine later if a code section
4659 // needs the Cortex-A8 workaround quickly.
4660 Arm_relobj<big_endian>* arm_relobj =
4661 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
4662 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
4665 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4666 p != this->arm_v4bx_stubs_.end();
4672 const Stub_template* stub_template = (*p)->stub_template();
4673 uint64_t stub_addralign = stub_template->alignment();
4674 off = align_address(off, stub_addralign);
4675 (*p)->set_offset(off);
4676 off += stub_template->size();
4679 gold_assert(off <= this->prev_data_size_);
4682 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4683 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4684 // of the address range seen by the linker.
4686 template<bool big_endian>
4688 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
4689 Target_arm<big_endian>* arm_target,
4690 unsigned char* view,
4691 Arm_address view_address,
4692 section_size_type view_size)
4694 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4695 for (Cortex_a8_stub_list::const_iterator p =
4696 this->cortex_a8_stubs_.lower_bound(view_address);
4697 ((p != this->cortex_a8_stubs_.end())
4698 && (p->first < (view_address + view_size)));
4701 // We do not store the THUMB bit in the LSB of either the branch address
4702 // or the stub offset. There is no need to strip the LSB.
4703 Arm_address branch_address = p->first;
4704 const Cortex_a8_stub* stub = p->second;
4705 Arm_address stub_address = this->address() + stub->offset();
4707 // Offset of the branch instruction relative to this view.
4708 section_size_type offset =
4709 convert_to_section_size_type(branch_address - view_address);
4710 gold_assert((offset + 4) <= view_size);
4712 arm_target->apply_cortex_a8_workaround(stub, stub_address,
4713 view + offset, branch_address);
4717 // Arm_input_section methods.
4719 // Initialize an Arm_input_section.
4721 template<bool big_endian>
4723 Arm_input_section<big_endian>::init()
4725 Relobj* relobj = this->relobj();
4726 unsigned int shndx = this->shndx();
4728 // Cache these to speed up size and alignment queries. It is too slow
4729 // to call section_addraglin and section_size every time.
4730 this->original_addralign_ = relobj->section_addralign(shndx);
4731 this->original_size_ = relobj->section_size(shndx);
4733 // We want to make this look like the original input section after
4734 // output sections are finalized.
4735 Output_section* os = relobj->output_section(shndx);
4736 off_t offset = relobj->output_section_offset(shndx);
4737 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
4738 this->set_address(os->address() + offset);
4739 this->set_file_offset(os->offset() + offset);
4741 this->set_current_data_size(this->original_size_);
4742 this->finalize_data_size();
4745 template<bool big_endian>
4747 Arm_input_section<big_endian>::do_write(Output_file* of)
4749 // We have to write out the original section content.
4750 section_size_type section_size;
4751 const unsigned char* section_contents =
4752 this->relobj()->section_contents(this->shndx(), §ion_size, false);
4753 of->write(this->offset(), section_contents, section_size);
4755 // If this owns a stub table and it is not empty, write it.
4756 if (this->is_stub_table_owner() && !this->stub_table_->empty())
4757 this->stub_table_->write(of);
4760 // Finalize data size.
4762 template<bool big_endian>
4764 Arm_input_section<big_endian>::set_final_data_size()
4766 // If this owns a stub table, finalize its data size as well.
4767 if (this->is_stub_table_owner())
4769 uint64_t address = this->address();
4771 // The stub table comes after the original section contents.
4772 address += this->original_size_;
4773 address = align_address(address, this->stub_table_->addralign());
4774 off_t offset = this->offset() + (address - this->address());
4775 this->stub_table_->set_address_and_file_offset(address, offset);
4776 address += this->stub_table_->data_size();
4777 gold_assert(address == this->address() + this->current_data_size());
4780 this->set_data_size(this->current_data_size());
4783 // Reset address and file offset.
4785 template<bool big_endian>
4787 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
4789 // Size of the original input section contents.
4790 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4792 // If this is a stub table owner, account for the stub table size.
4793 if (this->is_stub_table_owner())
4795 Stub_table<big_endian>* stub_table = this->stub_table_;
4797 // Reset the stub table's address and file offset. The
4798 // current data size for child will be updated after that.
4799 stub_table_->reset_address_and_file_offset();
4800 off = align_address(off, stub_table_->addralign());
4801 off += stub_table->current_data_size();
4804 this->set_current_data_size(off);
4807 // Arm_exidx_cantunwind methods.
4809 // Write this to Output file OF for a fixed endianity.
4811 template<bool big_endian>
4813 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
4815 off_t offset = this->offset();
4816 const section_size_type oview_size = 8;
4817 unsigned char* const oview = of->get_output_view(offset, oview_size);
4819 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4820 Valtype* wv = reinterpret_cast<Valtype*>(oview);
4822 Output_section* os = this->relobj_->output_section(this->shndx_);
4823 gold_assert(os != NULL);
4825 Arm_relobj<big_endian>* arm_relobj =
4826 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
4827 Arm_address output_offset =
4828 arm_relobj->get_output_section_offset(this->shndx_);
4829 Arm_address section_start;
4830 if(output_offset != Arm_relobj<big_endian>::invalid_address)
4831 section_start = os->address() + output_offset;
4834 // Currently this only happens for a relaxed section.
4835 const Output_relaxed_input_section* poris =
4836 os->find_relaxed_input_section(this->relobj_, this->shndx_);
4837 gold_assert(poris != NULL);
4838 section_start = poris->address();
4841 // We always append this to the end of an EXIDX section.
4842 Arm_address output_address =
4843 section_start + this->relobj_->section_size(this->shndx_);
4845 // Write out the entry. The first word either points to the beginning
4846 // or after the end of a text section. The second word is the special
4847 // EXIDX_CANTUNWIND value.
4848 uint32_t prel31_offset = output_address - this->address();
4849 if (utils::has_overflow<31>(offset))
4850 gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
4851 elfcpp::Swap<32, big_endian>::writeval(wv, prel31_offset & 0x7fffffffU);
4852 elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
4854 of->write_output_view(this->offset(), oview_size, oview);
4857 // Arm_exidx_merged_section methods.
4859 // Constructor for Arm_exidx_merged_section.
4860 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
4861 // SECTION_OFFSET_MAP points to a section offset map describing how
4862 // parts of the input section are mapped to output. DELETED_BYTES is
4863 // the number of bytes deleted from the EXIDX input section.
4865 Arm_exidx_merged_section::Arm_exidx_merged_section(
4866 const Arm_exidx_input_section& exidx_input_section,
4867 const Arm_exidx_section_offset_map& section_offset_map,
4868 uint32_t deleted_bytes)
4869 : Output_relaxed_input_section(exidx_input_section.relobj(),
4870 exidx_input_section.shndx(),
4871 exidx_input_section.addralign()),
4872 exidx_input_section_(exidx_input_section),
4873 section_offset_map_(section_offset_map)
4875 // Fix size here so that we do not need to implement set_final_data_size.
4876 this->set_data_size(exidx_input_section.size() - deleted_bytes);
4877 this->fix_data_size();
4880 // Given an input OBJECT, an input section index SHNDX within that
4881 // object, and an OFFSET relative to the start of that input
4882 // section, return whether or not the corresponding offset within
4883 // the output section is known. If this function returns true, it
4884 // sets *POUTPUT to the output offset. The value -1 indicates that
4885 // this input offset is being discarded.
4888 Arm_exidx_merged_section::do_output_offset(
4889 const Relobj* relobj,
4891 section_offset_type offset,
4892 section_offset_type* poutput) const
4894 // We only handle offsets for the original EXIDX input section.
4895 if (relobj != this->exidx_input_section_.relobj()
4896 || shndx != this->exidx_input_section_.shndx())
4899 section_offset_type section_size =
4900 convert_types<section_offset_type>(this->exidx_input_section_.size());
4901 if (offset < 0 || offset >= section_size)
4902 // Input offset is out of valid range.
4906 // We need to look up the section offset map to determine the output
4907 // offset. Find the reference point in map that is first offset
4908 // bigger than or equal to this offset.
4909 Arm_exidx_section_offset_map::const_iterator p =
4910 this->section_offset_map_.lower_bound(offset);
4912 // The section offset maps are build such that this should not happen if
4913 // input offset is in the valid range.
4914 gold_assert(p != this->section_offset_map_.end());
4916 // We need to check if this is dropped.
4917 section_offset_type ref = p->first;
4918 section_offset_type mapped_ref = p->second;
4920 if (mapped_ref != Arm_exidx_input_section::invalid_offset)
4921 // Offset is present in output.
4922 *poutput = mapped_ref + (offset - ref);
4924 // Offset is discarded owing to EXIDX entry merging.
4931 // Write this to output file OF.
4934 Arm_exidx_merged_section::do_write(Output_file* of)
4936 // If we retain or discard the whole EXIDX input section, we would
4938 gold_assert(this->data_size() != this->exidx_input_section_.size()
4939 && this->data_size() != 0);
4941 off_t offset = this->offset();
4942 const section_size_type oview_size = this->data_size();
4943 unsigned char* const oview = of->get_output_view(offset, oview_size);
4945 Output_section* os = this->relobj()->output_section(this->shndx());
4946 gold_assert(os != NULL);
4948 // Get contents of EXIDX input section.
4949 section_size_type section_size;
4950 const unsigned char* section_contents =
4951 this->relobj()->section_contents(this->shndx(), §ion_size, false);
4952 gold_assert(section_size == this->exidx_input_section_.size());
4954 // Go over spans of input offsets and write only those that are not
4956 section_offset_type in_start = 0;
4957 section_offset_type out_start = 0;
4958 for(Arm_exidx_section_offset_map::const_iterator p =
4959 this->section_offset_map_.begin();
4960 p != this->section_offset_map_.end();
4963 section_offset_type in_end = p->first;
4964 gold_assert(in_end >= in_start);
4965 section_offset_type out_end = p->second;
4966 size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
4969 size_t out_chunk_size =
4970 convert_types<size_t>(out_end - out_start + 1);
4971 gold_assert(out_chunk_size == in_chunk_size);
4972 memcpy(oview + out_start, section_contents + in_start,
4974 out_start += out_chunk_size;
4976 in_start += in_chunk_size;
4979 gold_assert(convert_to_section_size_type(out_start) == oview_size);
4980 of->write_output_view(this->offset(), oview_size, oview);
4983 // Arm_exidx_fixup methods.
4985 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
4986 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
4987 // points to the end of the last seen EXIDX section.
4990 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
4992 if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
4993 && this->last_input_section_ != NULL)
4995 Relobj* relobj = this->last_input_section_->relobj();
4996 unsigned int text_shndx = this->last_input_section_->link();
4997 Arm_exidx_cantunwind* cantunwind =
4998 new Arm_exidx_cantunwind(relobj, text_shndx);
4999 this->exidx_output_section_->add_output_section_data(cantunwind);
5000 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5004 // Process an EXIDX section entry in input. Return whether this entry
5005 // can be deleted in the output. SECOND_WORD in the second word of the
5009 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
5012 if (second_word == elfcpp::EXIDX_CANTUNWIND)
5014 // Merge if previous entry is also an EXIDX_CANTUNWIND.
5015 delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
5016 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5018 else if ((second_word & 0x80000000) != 0)
5020 // Inlined unwinding data. Merge if equal to previous.
5021 delete_entry = (this->last_unwind_type_ == UT_INLINED_ENTRY
5022 && this->last_inlined_entry_ == second_word);
5023 this->last_unwind_type_ = UT_INLINED_ENTRY;
5024 this->last_inlined_entry_ = second_word;
5028 // Normal table entry. In theory we could merge these too,
5029 // but duplicate entries are likely to be much less common.
5030 delete_entry = false;
5031 this->last_unwind_type_ = UT_NORMAL_ENTRY;
5033 return delete_entry;
5036 // Update the current section offset map during EXIDX section fix-up.
5037 // If there is no map, create one. INPUT_OFFSET is the offset of a
5038 // reference point, DELETED_BYTES is the number of deleted by in the
5039 // section so far. If DELETE_ENTRY is true, the reference point and
5040 // all offsets after the previous reference point are discarded.
5043 Arm_exidx_fixup::update_offset_map(
5044 section_offset_type input_offset,
5045 section_size_type deleted_bytes,
5048 if (this->section_offset_map_ == NULL)
5049 this->section_offset_map_ = new Arm_exidx_section_offset_map();
5050 section_offset_type output_offset = (delete_entry
5052 : input_offset - deleted_bytes);
5053 (*this->section_offset_map_)[input_offset] = output_offset;
5056 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
5057 // bytes deleted. If some entries are merged, also store a pointer to a newly
5058 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
5059 // caller owns the map and is responsible for releasing it after use.
5061 template<bool big_endian>
5063 Arm_exidx_fixup::process_exidx_section(
5064 const Arm_exidx_input_section* exidx_input_section,
5065 Arm_exidx_section_offset_map** psection_offset_map)
5067 Relobj* relobj = exidx_input_section->relobj();
5068 unsigned shndx = exidx_input_section->shndx();
5069 section_size_type section_size;
5070 const unsigned char* section_contents =
5071 relobj->section_contents(shndx, §ion_size, false);
5073 if ((section_size % 8) != 0)
5075 // Something is wrong with this section. Better not touch it.
5076 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5077 relobj->name().c_str(), shndx);
5078 this->last_input_section_ = exidx_input_section;
5079 this->last_unwind_type_ = UT_NONE;
5083 uint32_t deleted_bytes = 0;
5084 bool prev_delete_entry = false;
5085 gold_assert(this->section_offset_map_ == NULL);
5087 for (section_size_type i = 0; i < section_size; i += 8)
5089 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5091 reinterpret_cast<const Valtype*>(section_contents + i + 4);
5092 uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
5094 bool delete_entry = this->process_exidx_entry(second_word);
5096 // Entry deletion causes changes in output offsets. We use a std::map
5097 // to record these. And entry (x, y) means input offset x
5098 // is mapped to output offset y. If y is invalid_offset, then x is
5099 // dropped in the output. Because of the way std::map::lower_bound
5100 // works, we record the last offset in a region w.r.t to keeping or
5101 // dropping. If there is no entry (x0, y0) for an input offset x0,
5102 // the output offset y0 of it is determined by the output offset y1 of
5103 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5104 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
5106 if (delete_entry != prev_delete_entry && i != 0)
5107 this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
5109 // Update total deleted bytes for this entry.
5113 prev_delete_entry = delete_entry;
5116 // If section offset map is not NULL, make an entry for the end of
5118 if (this->section_offset_map_ != NULL)
5119 update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
5121 *psection_offset_map = this->section_offset_map_;
5122 this->section_offset_map_ = NULL;
5123 this->last_input_section_ = exidx_input_section;
5125 // Set the first output text section so that we can link the EXIDX output
5126 // section to it. Ignore any EXIDX input section that is completely merged.
5127 if (this->first_output_text_section_ == NULL
5128 && deleted_bytes != section_size)
5130 unsigned int link = exidx_input_section->link();
5131 Output_section* os = relobj->output_section(link);
5132 gold_assert(os != NULL);
5133 this->first_output_text_section_ = os;
5136 return deleted_bytes;
5139 // Arm_output_section methods.
5141 // Create a stub group for input sections from BEGIN to END. OWNER
5142 // points to the input section to be the owner a new stub table.
5144 template<bool big_endian>
5146 Arm_output_section<big_endian>::create_stub_group(
5147 Input_section_list::const_iterator begin,
5148 Input_section_list::const_iterator end,
5149 Input_section_list::const_iterator owner,
5150 Target_arm<big_endian>* target,
5151 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
5153 // We use a different kind of relaxed section in an EXIDX section.
5154 // The static casting from Output_relaxed_input_section to
5155 // Arm_input_section is invalid in an EXIDX section. We are okay
5156 // because we should not be calling this for an EXIDX section.
5157 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
5159 // Currently we convert ordinary input sections into relaxed sections only
5160 // at this point but we may want to support creating relaxed input section
5161 // very early. So we check here to see if owner is already a relaxed
5164 Arm_input_section<big_endian>* arm_input_section;
5165 if (owner->is_relaxed_input_section())
5168 Arm_input_section<big_endian>::as_arm_input_section(
5169 owner->relaxed_input_section());
5173 gold_assert(owner->is_input_section());
5174 // Create a new relaxed input section.
5176 target->new_arm_input_section(owner->relobj(), owner->shndx());
5177 new_relaxed_sections->push_back(arm_input_section);
5180 // Create a stub table.
5181 Stub_table<big_endian>* stub_table =
5182 target->new_stub_table(arm_input_section);
5184 arm_input_section->set_stub_table(stub_table);
5186 Input_section_list::const_iterator p = begin;
5187 Input_section_list::const_iterator prev_p;
5189 // Look for input sections or relaxed input sections in [begin ... end].
5192 if (p->is_input_section() || p->is_relaxed_input_section())
5194 // The stub table information for input sections live
5195 // in their objects.
5196 Arm_relobj<big_endian>* arm_relobj =
5197 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5198 arm_relobj->set_stub_table(p->shndx(), stub_table);
5202 while (prev_p != end);
5205 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
5206 // of stub groups. We grow a stub group by adding input section until the
5207 // size is just below GROUP_SIZE. The last input section will be converted
5208 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5209 // input section after the stub table, effectively double the group size.
5211 // This is similar to the group_sections() function in elf32-arm.c but is
5212 // implemented differently.
5214 template<bool big_endian>
5216 Arm_output_section<big_endian>::group_sections(
5217 section_size_type group_size,
5218 bool stubs_always_after_branch,
5219 Target_arm<big_endian>* target)
5221 // We only care about sections containing code.
5222 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5225 // States for grouping.
5228 // No group is being built.
5230 // A group is being built but the stub table is not found yet.
5231 // We keep group a stub group until the size is just under GROUP_SIZE.
5232 // The last input section in the group will be used as the stub table.
5233 FINDING_STUB_SECTION,
5234 // A group is being built and we have already found a stub table.
5235 // We enter this state to grow a stub group by adding input section
5236 // after the stub table. This effectively doubles the group size.
5240 // Any newly created relaxed sections are stored here.
5241 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
5243 State state = NO_GROUP;
5244 section_size_type off = 0;
5245 section_size_type group_begin_offset = 0;
5246 section_size_type group_end_offset = 0;
5247 section_size_type stub_table_end_offset = 0;
5248 Input_section_list::const_iterator group_begin =
5249 this->input_sections().end();
5250 Input_section_list::const_iterator stub_table =
5251 this->input_sections().end();
5252 Input_section_list::const_iterator group_end = this->input_sections().end();
5253 for (Input_section_list::const_iterator p = this->input_sections().begin();
5254 p != this->input_sections().end();
5257 section_size_type section_begin_offset =
5258 align_address(off, p->addralign());
5259 section_size_type section_end_offset =
5260 section_begin_offset + p->data_size();
5262 // Check to see if we should group the previously seens sections.
5268 case FINDING_STUB_SECTION:
5269 // Adding this section makes the group larger than GROUP_SIZE.
5270 if (section_end_offset - group_begin_offset >= group_size)
5272 if (stubs_always_after_branch)
5274 gold_assert(group_end != this->input_sections().end());
5275 this->create_stub_group(group_begin, group_end, group_end,
5276 target, &new_relaxed_sections);
5281 // But wait, there's more! Input sections up to
5282 // stub_group_size bytes after the stub table can be
5283 // handled by it too.
5284 state = HAS_STUB_SECTION;
5285 stub_table = group_end;
5286 stub_table_end_offset = group_end_offset;
5291 case HAS_STUB_SECTION:
5292 // Adding this section makes the post stub-section group larger
5294 if (section_end_offset - stub_table_end_offset >= group_size)
5296 gold_assert(group_end != this->input_sections().end());
5297 this->create_stub_group(group_begin, group_end, stub_table,
5298 target, &new_relaxed_sections);
5307 // If we see an input section and currently there is no group, start
5308 // a new one. Skip any empty sections.
5309 if ((p->is_input_section() || p->is_relaxed_input_section())
5310 && (p->relobj()->section_size(p->shndx()) != 0))
5312 if (state == NO_GROUP)
5314 state = FINDING_STUB_SECTION;
5316 group_begin_offset = section_begin_offset;
5319 // Keep track of the last input section seen.
5321 group_end_offset = section_end_offset;
5324 off = section_end_offset;
5327 // Create a stub group for any ungrouped sections.
5328 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5330 gold_assert(group_end != this->input_sections().end());
5331 this->create_stub_group(group_begin, group_end,
5332 (state == FINDING_STUB_SECTION
5335 target, &new_relaxed_sections);
5338 // Convert input section into relaxed input section in a batch.
5339 if (!new_relaxed_sections.empty())
5340 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5342 // Update the section offsets
5343 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5345 Arm_relobj<big_endian>* arm_relobj =
5346 Arm_relobj<big_endian>::as_arm_relobj(
5347 new_relaxed_sections[i]->relobj());
5348 unsigned int shndx = new_relaxed_sections[i]->shndx();
5349 // Tell Arm_relobj that this input section is converted.
5350 arm_relobj->convert_input_section_to_relaxed_section(shndx);
5354 // Append non empty text sections in this to LIST in ascending
5355 // order of their position in this.
5357 template<bool big_endian>
5359 Arm_output_section<big_endian>::append_text_sections_to_list(
5360 Text_section_list* list)
5362 // We only care about text sections.
5363 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5366 gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
5368 for (Input_section_list::const_iterator p = this->input_sections().begin();
5369 p != this->input_sections().end();
5372 // We only care about plain or relaxed input sections. We also
5373 // ignore any merged sections.
5374 if ((p->is_input_section() || p->is_relaxed_input_section())
5375 && p->data_size() != 0)
5376 list->push_back(Text_section_list::value_type(p->relobj(),
5381 template<bool big_endian>
5383 Arm_output_section<big_endian>::fix_exidx_coverage(
5384 const Text_section_list& sorted_text_sections,
5385 Symbol_table* symtab)
5387 // We should only do this for the EXIDX output section.
5388 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5390 // We don't want the relaxation loop to undo these changes, so we discard
5391 // the current saved states and take another one after the fix-up.
5392 this->discard_states();
5394 // Remove all input sections.
5395 uint64_t address = this->address();
5396 typedef std::list<Simple_input_section> Simple_input_section_list;
5397 Simple_input_section_list input_sections;
5398 this->reset_address_and_file_offset();
5399 this->get_input_sections(address, std::string(""), &input_sections);
5401 if (!this->input_sections().empty())
5402 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5404 // Go through all the known input sections and record them.
5405 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5406 Section_id_set known_input_sections;
5407 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5408 p != input_sections.end();
5411 // This should never happen. At this point, we should only see
5412 // plain EXIDX input sections.
5413 gold_assert(!p->is_relaxed_input_section());
5414 known_input_sections.insert(Section_id(p->relobj(), p->shndx()));
5417 Arm_exidx_fixup exidx_fixup(this);
5419 // Go over the sorted text sections.
5420 Section_id_set processed_input_sections;
5421 for (Text_section_list::const_iterator p = sorted_text_sections.begin();
5422 p != sorted_text_sections.end();
5425 Relobj* relobj = p->first;
5426 unsigned int shndx = p->second;
5428 Arm_relobj<big_endian>* arm_relobj =
5429 Arm_relobj<big_endian>::as_arm_relobj(relobj);
5430 const Arm_exidx_input_section* exidx_input_section =
5431 arm_relobj->exidx_input_section_by_link(shndx);
5433 // If this text section has no EXIDX section, force an EXIDX_CANTUNWIND
5434 // entry pointing to the end of the last seen EXIDX section.
5435 if (exidx_input_section == NULL)
5437 exidx_fixup.add_exidx_cantunwind_as_needed();
5441 Relobj* exidx_relobj = exidx_input_section->relobj();
5442 unsigned int exidx_shndx = exidx_input_section->shndx();
5443 Section_id sid(exidx_relobj, exidx_shndx);
5444 if (known_input_sections.find(sid) == known_input_sections.end())
5446 // This is odd. We have not seen this EXIDX input section before.
5447 // We cannot do fix-up.
5448 gold_error(_("EXIDX section %u of %s is not in EXIDX output section"),
5449 exidx_shndx, exidx_relobj->name().c_str());
5450 exidx_fixup.add_exidx_cantunwind_as_needed();
5454 // Fix up coverage and append input section to output data list.
5455 Arm_exidx_section_offset_map* section_offset_map = NULL;
5456 uint32_t deleted_bytes =
5457 exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
5458 §ion_offset_map);
5460 if (deleted_bytes == exidx_input_section->size())
5462 // The whole EXIDX section got merged. Remove it from output.
5463 gold_assert(section_offset_map == NULL);
5464 exidx_relobj->set_output_section(exidx_shndx, NULL);
5466 // All local symbols defined in this input section will be dropped.
5467 // We need to adjust output local symbol count.
5468 arm_relobj->set_output_local_symbol_count_needs_update();
5470 else if (deleted_bytes > 0)
5472 // Some entries are merged. We need to convert this EXIDX input
5473 // section into a relaxed section.
5474 gold_assert(section_offset_map != NULL);
5475 Arm_exidx_merged_section* merged_section =
5476 new Arm_exidx_merged_section(*exidx_input_section,
5477 *section_offset_map, deleted_bytes);
5478 this->add_relaxed_input_section(merged_section);
5479 arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
5481 // All local symbols defined in discarded portions of this input
5482 // section will be dropped. We need to adjust output local symbol
5484 arm_relobj->set_output_local_symbol_count_needs_update();
5488 // Just add back the EXIDX input section.
5489 gold_assert(section_offset_map == NULL);
5490 Output_section::Simple_input_section sis(exidx_relobj, exidx_shndx);
5491 this->add_simple_input_section(sis, exidx_input_section->size(),
5492 exidx_input_section->addralign());
5495 processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx));
5498 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5499 exidx_fixup.add_exidx_cantunwind_as_needed();
5501 // Remove any known EXIDX input sections that are not processed.
5502 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5503 p != input_sections.end();
5506 if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
5507 == processed_input_sections.end())
5509 // We only discard a known EXIDX section because its linked
5510 // text section has been folded by ICF.
5511 Arm_relobj<big_endian>* arm_relobj =
5512 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5513 const Arm_exidx_input_section* exidx_input_section =
5514 arm_relobj->exidx_input_section_by_shndx(p->shndx());
5515 gold_assert(exidx_input_section != NULL);
5516 unsigned int text_shndx = exidx_input_section->link();
5517 gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
5519 // Remove this from link.
5520 p->relobj()->set_output_section(p->shndx(), NULL);
5524 // Link exidx output section to the first seen output section and
5525 // set correct entry size.
5526 this->set_link_section(exidx_fixup.first_output_text_section());
5527 this->set_entsize(8);
5529 // Make changes permanent.
5530 this->save_states();
5531 this->set_section_offsets_need_adjustment();
5534 // Arm_relobj methods.
5536 // Determine if an input section is scannable for stub processing. SHDR is
5537 // the header of the section and SHNDX is the section index. OS is the output
5538 // section for the input section and SYMTAB is the global symbol table used to
5539 // look up ICF information.
5541 template<bool big_endian>
5543 Arm_relobj<big_endian>::section_is_scannable(
5544 const elfcpp::Shdr<32, big_endian>& shdr,
5546 const Output_section* os,
5547 const Symbol_table *symtab)
5549 // Skip any empty sections, unallocated sections or sections whose
5550 // type are not SHT_PROGBITS.
5551 if (shdr.get_sh_size() == 0
5552 || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
5553 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
5556 // Skip any discarded or ICF'ed sections.
5557 if (os == NULL || symtab->is_section_folded(this, shndx))
5560 // If this requires special offset handling, check to see if it is
5561 // a relaxed section. If this is not, then it is a merged section that
5562 // we cannot handle.
5563 if (this->is_output_section_offset_invalid(shndx))
5565 const Output_relaxed_input_section* poris =
5566 os->find_relaxed_input_section(this, shndx);
5574 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5575 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5577 template<bool big_endian>
5579 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
5580 const elfcpp::Shdr<32, big_endian>& shdr,
5581 const Relobj::Output_sections& out_sections,
5582 const Symbol_table *symtab,
5583 const unsigned char* pshdrs)
5585 unsigned int sh_type = shdr.get_sh_type();
5586 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
5589 // Ignore empty section.
5590 off_t sh_size = shdr.get_sh_size();
5594 // Ignore reloc section with unexpected symbol table. The
5595 // error will be reported in the final link.
5596 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
5599 unsigned int reloc_size;
5600 if (sh_type == elfcpp::SHT_REL)
5601 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5603 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5605 // Ignore reloc section with unexpected entsize or uneven size.
5606 // The error will be reported in the final link.
5607 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
5610 // Ignore reloc section with bad info. This error will be
5611 // reported in the final link.
5612 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5613 if (index >= this->shnum())
5616 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5617 const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
5618 return this->section_is_scannable(text_shdr, index,
5619 out_sections[index], symtab);
5622 // Return the output address of either a plain input section or a relaxed
5623 // input section. SHNDX is the section index. We define and use this
5624 // instead of calling Output_section::output_address because that is slow
5625 // for large output.
5627 template<bool big_endian>
5629 Arm_relobj<big_endian>::simple_input_section_output_address(
5633 if (this->is_output_section_offset_invalid(shndx))
5635 const Output_relaxed_input_section* poris =
5636 os->find_relaxed_input_section(this, shndx);
5637 // We do not handle merged sections here.
5638 gold_assert(poris != NULL);
5639 return poris->address();
5642 return os->address() + this->get_output_section_offset(shndx);
5645 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
5646 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5648 template<bool big_endian>
5650 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
5651 const elfcpp::Shdr<32, big_endian>& shdr,
5654 const Symbol_table* symtab)
5656 if (!this->section_is_scannable(shdr, shndx, os, symtab))
5659 // If the section does not cross any 4K-boundaries, it does not need to
5661 Arm_address address = this->simple_input_section_output_address(shndx, os);
5662 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
5668 // Scan a section for Cortex-A8 workaround.
5670 template<bool big_endian>
5672 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
5673 const elfcpp::Shdr<32, big_endian>& shdr,
5676 Target_arm<big_endian>* arm_target)
5678 // Look for the first mapping symbol in this section. It should be
5680 Mapping_symbol_position section_start(shndx, 0);
5681 typename Mapping_symbols_info::const_iterator p =
5682 this->mapping_symbols_info_.lower_bound(section_start);
5684 // There are no mapping symbols for this section. Treat it as a data-only
5686 if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
5689 Arm_address output_address =
5690 this->simple_input_section_output_address(shndx, os);
5692 // Get the section contents.
5693 section_size_type input_view_size = 0;
5694 const unsigned char* input_view =
5695 this->section_contents(shndx, &input_view_size, false);
5697 // We need to go through the mapping symbols to determine what to
5698 // scan. There are two reasons. First, we should look at THUMB code and
5699 // THUMB code only. Second, we only want to look at the 4K-page boundary
5700 // to speed up the scanning.
5702 while (p != this->mapping_symbols_info_.end()
5703 && p->first.first == shndx)
5705 typename Mapping_symbols_info::const_iterator next =
5706 this->mapping_symbols_info_.upper_bound(p->first);
5708 // Only scan part of a section with THUMB code.
5709 if (p->second == 't')
5711 // Determine the end of this range.
5712 section_size_type span_start =
5713 convert_to_section_size_type(p->first.second);
5714 section_size_type span_end;
5715 if (next != this->mapping_symbols_info_.end()
5716 && next->first.first == shndx)
5717 span_end = convert_to_section_size_type(next->first.second);
5719 span_end = convert_to_section_size_type(shdr.get_sh_size());
5721 if (((span_start + output_address) & ~0xfffUL)
5722 != ((span_end + output_address - 1) & ~0xfffUL))
5724 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
5725 span_start, span_end,
5735 // Scan relocations for stub generation.
5737 template<bool big_endian>
5739 Arm_relobj<big_endian>::scan_sections_for_stubs(
5740 Target_arm<big_endian>* arm_target,
5741 const Symbol_table* symtab,
5742 const Layout* layout)
5744 unsigned int shnum = this->shnum();
5745 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5747 // Read the section headers.
5748 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
5752 // To speed up processing, we set up hash tables for fast lookup of
5753 // input offsets to output addresses.
5754 this->initialize_input_to_output_maps();
5756 const Relobj::Output_sections& out_sections(this->output_sections());
5758 Relocate_info<32, big_endian> relinfo;
5759 relinfo.symtab = symtab;
5760 relinfo.layout = layout;
5761 relinfo.object = this;
5763 // Do relocation stubs scanning.
5764 const unsigned char* p = pshdrs + shdr_size;
5765 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5767 const elfcpp::Shdr<32, big_endian> shdr(p);
5768 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
5771 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5772 Arm_address output_offset = this->get_output_section_offset(index);
5773 Arm_address output_address;
5774 if(output_offset != invalid_address)
5775 output_address = out_sections[index]->address() + output_offset;
5778 // Currently this only happens for a relaxed section.
5779 const Output_relaxed_input_section* poris =
5780 out_sections[index]->find_relaxed_input_section(this, index);
5781 gold_assert(poris != NULL);
5782 output_address = poris->address();
5785 // Get the relocations.
5786 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
5790 // Get the section contents. This does work for the case in which
5791 // we modify the contents of an input section. We need to pass the
5792 // output view under such circumstances.
5793 section_size_type input_view_size = 0;
5794 const unsigned char* input_view =
5795 this->section_contents(index, &input_view_size, false);
5797 relinfo.reloc_shndx = i;
5798 relinfo.data_shndx = index;
5799 unsigned int sh_type = shdr.get_sh_type();
5800 unsigned int reloc_size;
5801 if (sh_type == elfcpp::SHT_REL)
5802 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5804 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5806 Output_section* os = out_sections[index];
5807 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
5808 shdr.get_sh_size() / reloc_size,
5810 output_offset == invalid_address,
5811 input_view, output_address,
5816 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
5817 // after its relocation section, if there is one, is processed for
5818 // relocation stubs. Merging this loop with the one above would have been
5819 // complicated since we would have had to make sure that relocation stub
5820 // scanning is done first.
5821 if (arm_target->fix_cortex_a8())
5823 const unsigned char* p = pshdrs + shdr_size;
5824 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5826 const elfcpp::Shdr<32, big_endian> shdr(p);
5827 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
5830 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
5835 // After we've done the relocations, we release the hash tables,
5836 // since we no longer need them.
5837 this->free_input_to_output_maps();
5840 // Count the local symbols. The ARM backend needs to know if a symbol
5841 // is a THUMB function or not. For global symbols, it is easy because
5842 // the Symbol object keeps the ELF symbol type. For local symbol it is
5843 // harder because we cannot access this information. So we override the
5844 // do_count_local_symbol in parent and scan local symbols to mark
5845 // THUMB functions. This is not the most efficient way but I do not want to
5846 // slow down other ports by calling a per symbol targer hook inside
5847 // Sized_relobj<size, big_endian>::do_count_local_symbols.
5849 template<bool big_endian>
5851 Arm_relobj<big_endian>::do_count_local_symbols(
5852 Stringpool_template<char>* pool,
5853 Stringpool_template<char>* dynpool)
5855 // We need to fix-up the values of any local symbols whose type are
5858 // Ask parent to count the local symbols.
5859 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
5860 const unsigned int loccount = this->local_symbol_count();
5864 // Intialize the thumb function bit-vector.
5865 std::vector<bool> empty_vector(loccount, false);
5866 this->local_symbol_is_thumb_function_.swap(empty_vector);
5868 // Read the symbol table section header.
5869 const unsigned int symtab_shndx = this->symtab_shndx();
5870 elfcpp::Shdr<32, big_endian>
5871 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
5872 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
5874 // Read the local symbols.
5875 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
5876 gold_assert(loccount == symtabshdr.get_sh_info());
5877 off_t locsize = loccount * sym_size;
5878 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
5879 locsize, true, true);
5881 // For mapping symbol processing, we need to read the symbol names.
5882 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
5883 if (strtab_shndx >= this->shnum())
5885 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
5889 elfcpp::Shdr<32, big_endian>
5890 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
5891 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
5893 this->error(_("symbol table name section has wrong type: %u"),
5894 static_cast<unsigned int>(strtabshdr.get_sh_type()));
5897 const char* pnames =
5898 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
5899 strtabshdr.get_sh_size(),
5902 // Loop over the local symbols and mark any local symbols pointing
5903 // to THUMB functions.
5905 // Skip the first dummy symbol.
5907 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
5908 this->local_values();
5909 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
5911 elfcpp::Sym<32, big_endian> sym(psyms);
5912 elfcpp::STT st_type = sym.get_st_type();
5913 Symbol_value<32>& lv((*plocal_values)[i]);
5914 Arm_address input_value = lv.input_value();
5916 // Check to see if this is a mapping symbol.
5917 const char* sym_name = pnames + sym.get_st_name();
5918 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
5920 unsigned int input_shndx = sym.get_st_shndx();
5922 // Strip of LSB in case this is a THUMB symbol.
5923 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
5924 this->mapping_symbols_info_[msp] = sym_name[1];
5927 if (st_type == elfcpp::STT_ARM_TFUNC
5928 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
5930 // This is a THUMB function. Mark this and canonicalize the
5931 // symbol value by setting LSB.
5932 this->local_symbol_is_thumb_function_[i] = true;
5933 if ((input_value & 1) == 0)
5934 lv.set_input_value(input_value | 1);
5939 // Relocate sections.
5940 template<bool big_endian>
5942 Arm_relobj<big_endian>::do_relocate_sections(
5943 const Symbol_table* symtab,
5944 const Layout* layout,
5945 const unsigned char* pshdrs,
5946 typename Sized_relobj<32, big_endian>::Views* pviews)
5948 // Call parent to relocate sections.
5949 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
5952 // We do not generate stubs if doing a relocatable link.
5953 if (parameters->options().relocatable())
5956 // Relocate stub tables.
5957 unsigned int shnum = this->shnum();
5959 Target_arm<big_endian>* arm_target =
5960 Target_arm<big_endian>::default_target();
5962 Relocate_info<32, big_endian> relinfo;
5963 relinfo.symtab = symtab;
5964 relinfo.layout = layout;
5965 relinfo.object = this;
5967 for (unsigned int i = 1; i < shnum; ++i)
5969 Arm_input_section<big_endian>* arm_input_section =
5970 arm_target->find_arm_input_section(this, i);
5972 if (arm_input_section != NULL
5973 && arm_input_section->is_stub_table_owner()
5974 && !arm_input_section->stub_table()->empty())
5976 // We cannot discard a section if it owns a stub table.
5977 Output_section* os = this->output_section(i);
5978 gold_assert(os != NULL);
5980 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
5981 relinfo.reloc_shdr = NULL;
5982 relinfo.data_shndx = i;
5983 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
5985 gold_assert((*pviews)[i].view != NULL);
5987 // We are passed the output section view. Adjust it to cover the
5989 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
5990 gold_assert((stub_table->address() >= (*pviews)[i].address)
5991 && ((stub_table->address() + stub_table->data_size())
5992 <= (*pviews)[i].address + (*pviews)[i].view_size));
5994 off_t offset = stub_table->address() - (*pviews)[i].address;
5995 unsigned char* view = (*pviews)[i].view + offset;
5996 Arm_address address = stub_table->address();
5997 section_size_type view_size = stub_table->data_size();
5999 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
6003 // Apply Cortex A8 workaround if applicable.
6004 if (this->section_has_cortex_a8_workaround(i))
6006 unsigned char* view = (*pviews)[i].view;
6007 Arm_address view_address = (*pviews)[i].address;
6008 section_size_type view_size = (*pviews)[i].view_size;
6009 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
6011 // Adjust view to cover section.
6012 Output_section* os = this->output_section(i);
6013 gold_assert(os != NULL);
6014 Arm_address section_address =
6015 this->simple_input_section_output_address(i, os);
6016 uint64_t section_size = this->section_size(i);
6018 gold_assert(section_address >= view_address
6019 && ((section_address + section_size)
6020 <= (view_address + view_size)));
6022 unsigned char* section_view = view + (section_address - view_address);
6024 // Apply the Cortex-A8 workaround to the output address range
6025 // corresponding to this input section.
6026 stub_table->apply_cortex_a8_workaround_to_address_range(
6035 // Find the linked text section of an EXIDX section by looking the the first
6036 // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6037 // must be linked to to its associated code section via the sh_link field of
6038 // its section header. However, some tools are broken and the link is not
6039 // always set. LD just drops such an EXIDX section silently, causing the
6040 // associated code not unwindabled. Here we try a little bit harder to
6041 // discover the linked code section.
6043 // PSHDR points to the section header of a relocation section of an EXIDX
6044 // section. If we can find a linked text section, return true and
6045 // store the text section index in the location PSHNDX. Otherwise
6048 template<bool big_endian>
6050 Arm_relobj<big_endian>::find_linked_text_section(
6051 const unsigned char* pshdr,
6052 const unsigned char* psyms,
6053 unsigned int* pshndx)
6055 elfcpp::Shdr<32, big_endian> shdr(pshdr);
6057 // If there is no relocation, we cannot find the linked text section.
6059 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6060 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6062 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6063 size_t reloc_count = shdr.get_sh_size() / reloc_size;
6065 // Get the relocations.
6066 const unsigned char* prelocs =
6067 this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false);
6069 // Find the REL31 relocation for the first word of the first EXIDX entry.
6070 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6072 Arm_address r_offset;
6073 typename elfcpp::Elf_types<32>::Elf_WXword r_info;
6074 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6076 typename elfcpp::Rel<32, big_endian> reloc(prelocs);
6077 r_info = reloc.get_r_info();
6078 r_offset = reloc.get_r_offset();
6082 typename elfcpp::Rela<32, big_endian> reloc(prelocs);
6083 r_info = reloc.get_r_info();
6084 r_offset = reloc.get_r_offset();
6087 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6088 if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
6091 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6093 || r_sym >= this->local_symbol_count()
6097 // This is the relocation for the first word of the first EXIDX entry.
6098 // We expect to see a local section symbol.
6099 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6100 elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
6101 if (sym.get_st_type() == elfcpp::STT_SECTION)
6103 *pshndx = this->adjust_shndx(sym.get_st_shndx());
6113 // Make an EXIDX input section object for an EXIDX section whose index is
6114 // SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
6115 // is the section index of the linked text section.
6117 template<bool big_endian>
6119 Arm_relobj<big_endian>::make_exidx_input_section(
6121 const elfcpp::Shdr<32, big_endian>& shdr,
6122 unsigned int text_shndx)
6124 // Issue an error and ignore this EXIDX section if it points to a text
6125 // section already has an EXIDX section.
6126 if (this->exidx_section_map_[text_shndx] != NULL)
6128 gold_error(_("EXIDX sections %u and %u both link to text section %u "
6130 shndx, this->exidx_section_map_[text_shndx]->shndx(),
6131 text_shndx, this->name().c_str());
6135 // Create an Arm_exidx_input_section object for this EXIDX section.
6136 Arm_exidx_input_section* exidx_input_section =
6137 new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
6138 shdr.get_sh_addralign());
6139 this->exidx_section_map_[text_shndx] = exidx_input_section;
6141 // Also map the EXIDX section index to this.
6142 gold_assert(this->exidx_section_map_[shndx] == NULL);
6143 this->exidx_section_map_[shndx] = exidx_input_section;
6146 // Read the symbol information.
6148 template<bool big_endian>
6150 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6152 // Call parent class to read symbol information.
6153 Sized_relobj<32, big_endian>::do_read_symbols(sd);
6155 // Read processor-specific flags in ELF file header.
6156 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6157 elfcpp::Elf_sizes<32>::ehdr_size,
6159 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6160 this->processor_specific_flags_ = ehdr.get_e_flags();
6162 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6164 std::vector<unsigned int> deferred_exidx_sections;
6165 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6166 const unsigned char* pshdrs = sd->section_headers->data();
6167 const unsigned char *ps = pshdrs + shdr_size;
6168 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6170 elfcpp::Shdr<32, big_endian> shdr(ps);
6171 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6173 gold_assert(this->attributes_section_data_ == NULL);
6174 section_offset_type section_offset = shdr.get_sh_offset();
6175 section_size_type section_size =
6176 convert_to_section_size_type(shdr.get_sh_size());
6177 File_view* view = this->get_lasting_view(section_offset,
6178 section_size, true, false);
6179 this->attributes_section_data_ =
6180 new Attributes_section_data(view->data(), section_size);
6182 else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6184 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6185 if (text_shndx >= this->shnum())
6186 gold_error(_("EXIDX section %u linked to invalid section %u"),
6188 else if (text_shndx == elfcpp::SHN_UNDEF)
6189 deferred_exidx_sections.push_back(i);
6191 this->make_exidx_input_section(i, shdr, text_shndx);
6195 // Some tools are broken and they do not set the link of EXIDX sections.
6196 // We look at the first relocation to figure out the linked sections.
6197 if (!deferred_exidx_sections.empty())
6199 // We need to go over the section headers again to find the mapping
6200 // from sections being relocated to their relocation sections. This is
6201 // a bit inefficient as we could do that in the loop above. However,
6202 // we do not expect any deferred EXIDX sections normally. So we do not
6203 // want to slow down the most common path.
6204 typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
6205 Reloc_map reloc_map;
6206 ps = pshdrs + shdr_size;
6207 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6209 elfcpp::Shdr<32, big_endian> shdr(ps);
6210 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
6211 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
6213 unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
6214 if (info_shndx >= this->shnum())
6215 gold_error(_("relocation section %u has invalid info %u"),
6217 Reloc_map::value_type value(info_shndx, i);
6218 std::pair<Reloc_map::iterator, bool> result =
6219 reloc_map.insert(value);
6221 gold_error(_("section %u has multiple relocation sections "
6223 info_shndx, i, reloc_map[info_shndx]);
6227 // Read the symbol table section header.
6228 const unsigned int symtab_shndx = this->symtab_shndx();
6229 elfcpp::Shdr<32, big_endian>
6230 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6231 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6233 // Read the local symbols.
6234 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6235 const unsigned int loccount = this->local_symbol_count();
6236 gold_assert(loccount == symtabshdr.get_sh_info());
6237 off_t locsize = loccount * sym_size;
6238 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6239 locsize, true, true);
6241 // Process the deferred EXIDX sections.
6242 for(unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
6244 unsigned int shndx = deferred_exidx_sections[i];
6245 elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
6246 unsigned int text_shndx;
6247 Reloc_map::const_iterator it = reloc_map.find(shndx);
6248 if (it != reloc_map.end()
6249 && find_linked_text_section(pshdrs + it->second * shdr_size,
6250 psyms, &text_shndx))
6251 this->make_exidx_input_section(shndx, shdr, text_shndx);
6253 gold_error(_("EXIDX section %u has no linked text section."),
6259 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
6260 // sections for unwinding. These sections are referenced implicitly by
6261 // text sections linked in the section headers. If we ignore these implict
6262 // references, the .ARM.exidx sections and any .ARM.extab sections they use
6263 // will be garbage-collected incorrectly. Hence we override the same function
6264 // in the base class to handle these implicit references.
6266 template<bool big_endian>
6268 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
6270 Read_relocs_data* rd)
6272 // First, call base class method to process relocations in this object.
6273 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
6275 unsigned int shnum = this->shnum();
6276 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6277 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6281 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
6282 // to these from the linked text sections.
6283 const unsigned char* ps = pshdrs + shdr_size;
6284 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
6286 elfcpp::Shdr<32, big_endian> shdr(ps);
6287 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6289 // Found an .ARM.exidx section, add it to the set of reachable
6290 // sections from its linked text section.
6291 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6292 symtab->gc()->add_reference(this, text_shndx, this, i);
6297 // Update output local symbol count. Owing to EXIDX entry merging, some local
6298 // symbols will be removed in output. Adjust output local symbol count
6299 // accordingly. We can only changed the static output local symbol count. It
6300 // is too late to change the dynamic symbols.
6302 template<bool big_endian>
6304 Arm_relobj<big_endian>::update_output_local_symbol_count()
6306 // Caller should check that this needs updating. We want caller checking
6307 // because output_local_symbol_count_needs_update() is most likely inlined.
6308 gold_assert(this->output_local_symbol_count_needs_update_);
6310 gold_assert(this->symtab_shndx() != -1U);
6311 if (this->symtab_shndx() == 0)
6313 // This object has no symbols. Weird but legal.
6317 // Read the symbol table section header.
6318 const unsigned int symtab_shndx = this->symtab_shndx();
6319 elfcpp::Shdr<32, big_endian>
6320 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6321 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6323 // Read the local symbols.
6324 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6325 const unsigned int loccount = this->local_symbol_count();
6326 gold_assert(loccount == symtabshdr.get_sh_info());
6327 off_t locsize = loccount * sym_size;
6328 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6329 locsize, true, true);
6331 // Loop over the local symbols.
6333 typedef typename Sized_relobj<32, big_endian>::Output_sections
6335 const Output_sections& out_sections(this->output_sections());
6336 unsigned int shnum = this->shnum();
6337 unsigned int count = 0;
6338 // Skip the first, dummy, symbol.
6340 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6342 elfcpp::Sym<32, big_endian> sym(psyms);
6344 Symbol_value<32>& lv((*this->local_values())[i]);
6346 // This local symbol was already discarded by do_count_local_symbols.
6347 if (!lv.needs_output_symtab_entry())
6351 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
6356 Output_section* os = out_sections[shndx];
6358 // This local symbol no longer has an output section. Discard it.
6361 lv.set_no_output_symtab_entry();
6365 // Currently we only discard parts of EXIDX input sections.
6366 // We explicitly check for a merged EXIDX input section to avoid
6367 // calling Output_section_data::output_offset unless necessary.
6368 if ((this->get_output_section_offset(shndx) == invalid_address)
6369 && (this->exidx_input_section_by_shndx(shndx) != NULL))
6371 section_offset_type output_offset =
6372 os->output_offset(this, shndx, lv.input_value());
6373 if (output_offset == -1)
6375 // This symbol is defined in a part of an EXIDX input section
6376 // that is discarded due to entry merging.
6377 lv.set_no_output_symtab_entry();
6386 this->set_output_local_symbol_count(count);
6387 this->output_local_symbol_count_needs_update_ = false;
6390 // Arm_dynobj methods.
6392 // Read the symbol information.
6394 template<bool big_endian>
6396 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6398 // Call parent class to read symbol information.
6399 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
6401 // Read processor-specific flags in ELF file header.
6402 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6403 elfcpp::Elf_sizes<32>::ehdr_size,
6405 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6406 this->processor_specific_flags_ = ehdr.get_e_flags();
6408 // Read the attributes section if there is one.
6409 // We read from the end because gas seems to put it near the end of
6410 // the section headers.
6411 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6412 const unsigned char *ps =
6413 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
6414 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
6416 elfcpp::Shdr<32, big_endian> shdr(ps);
6417 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6419 section_offset_type section_offset = shdr.get_sh_offset();
6420 section_size_type section_size =
6421 convert_to_section_size_type(shdr.get_sh_size());
6422 File_view* view = this->get_lasting_view(section_offset,
6423 section_size, true, false);
6424 this->attributes_section_data_ =
6425 new Attributes_section_data(view->data(), section_size);
6431 // Stub_addend_reader methods.
6433 // Read the addend of a REL relocation of type R_TYPE at VIEW.
6435 template<bool big_endian>
6436 elfcpp::Elf_types<32>::Elf_Swxword
6437 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
6438 unsigned int r_type,
6439 const unsigned char* view,
6440 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
6442 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
6446 case elfcpp::R_ARM_CALL:
6447 case elfcpp::R_ARM_JUMP24:
6448 case elfcpp::R_ARM_PLT32:
6450 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6451 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6452 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
6453 return utils::sign_extend<26>(val << 2);
6456 case elfcpp::R_ARM_THM_CALL:
6457 case elfcpp::R_ARM_THM_JUMP24:
6458 case elfcpp::R_ARM_THM_XPC22:
6460 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6461 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6462 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6463 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6464 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
6467 case elfcpp::R_ARM_THM_JUMP19:
6469 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6470 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6471 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6472 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6473 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
6481 // A class to handle the PLT data.
6483 template<bool big_endian>
6484 class Output_data_plt_arm : public Output_section_data
6487 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
6490 Output_data_plt_arm(Layout*, Output_data_space*);
6492 // Add an entry to the PLT.
6494 add_entry(Symbol* gsym);
6496 // Return the .rel.plt section data.
6497 const Reloc_section*
6499 { return this->rel_; }
6503 do_adjust_output_section(Output_section* os);
6505 // Write to a map file.
6507 do_print_to_mapfile(Mapfile* mapfile) const
6508 { mapfile->print_output_data(this, _("** PLT")); }
6511 // Template for the first PLT entry.
6512 static const uint32_t first_plt_entry[5];
6514 // Template for subsequent PLT entries.
6515 static const uint32_t plt_entry[3];
6517 // Set the final size.
6519 set_final_data_size()
6521 this->set_data_size(sizeof(first_plt_entry)
6522 + this->count_ * sizeof(plt_entry));
6525 // Write out the PLT data.
6527 do_write(Output_file*);
6529 // The reloc section.
6530 Reloc_section* rel_;
6531 // The .got.plt section.
6532 Output_data_space* got_plt_;
6533 // The number of PLT entries.
6534 unsigned int count_;
6537 // Create the PLT section. The ordinary .got section is an argument,
6538 // since we need to refer to the start. We also create our own .got
6539 // section just for PLT entries.
6541 template<bool big_endian>
6542 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
6543 Output_data_space* got_plt)
6544 : Output_section_data(4), got_plt_(got_plt), count_(0)
6546 this->rel_ = new Reloc_section(false);
6547 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
6548 elfcpp::SHF_ALLOC, this->rel_, true, false,
6552 template<bool big_endian>
6554 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
6559 // Add an entry to the PLT.
6561 template<bool big_endian>
6563 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
6565 gold_assert(!gsym->has_plt_offset());
6567 // Note that when setting the PLT offset we skip the initial
6568 // reserved PLT entry.
6569 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
6570 + sizeof(first_plt_entry));
6574 section_offset_type got_offset = this->got_plt_->current_data_size();
6576 // Every PLT entry needs a GOT entry which points back to the PLT
6577 // entry (this will be changed by the dynamic linker, normally
6578 // lazily when the function is called).
6579 this->got_plt_->set_current_data_size(got_offset + 4);
6581 // Every PLT entry needs a reloc.
6582 gsym->set_needs_dynsym_entry();
6583 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
6586 // Note that we don't need to save the symbol. The contents of the
6587 // PLT are independent of which symbols are used. The symbols only
6588 // appear in the relocations.
6592 // FIXME: This is not very flexible. Right now this has only been tested
6593 // on armv5te. If we are to support additional architecture features like
6594 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
6596 // The first entry in the PLT.
6597 template<bool big_endian>
6598 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
6600 0xe52de004, // str lr, [sp, #-4]!
6601 0xe59fe004, // ldr lr, [pc, #4]
6602 0xe08fe00e, // add lr, pc, lr
6603 0xe5bef008, // ldr pc, [lr, #8]!
6604 0x00000000, // &GOT[0] - .
6607 // Subsequent entries in the PLT.
6609 template<bool big_endian>
6610 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
6612 0xe28fc600, // add ip, pc, #0xNN00000
6613 0xe28cca00, // add ip, ip, #0xNN000
6614 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
6617 // Write out the PLT. This uses the hand-coded instructions above,
6618 // and adjusts them as needed. This is all specified by the arm ELF
6619 // Processor Supplement.
6621 template<bool big_endian>
6623 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
6625 const off_t offset = this->offset();
6626 const section_size_type oview_size =
6627 convert_to_section_size_type(this->data_size());
6628 unsigned char* const oview = of->get_output_view(offset, oview_size);
6630 const off_t got_file_offset = this->got_plt_->offset();
6631 const section_size_type got_size =
6632 convert_to_section_size_type(this->got_plt_->data_size());
6633 unsigned char* const got_view = of->get_output_view(got_file_offset,
6635 unsigned char* pov = oview;
6637 Arm_address plt_address = this->address();
6638 Arm_address got_address = this->got_plt_->address();
6640 // Write first PLT entry. All but the last word are constants.
6641 const size_t num_first_plt_words = (sizeof(first_plt_entry)
6642 / sizeof(plt_entry[0]));
6643 for (size_t i = 0; i < num_first_plt_words - 1; i++)
6644 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
6645 // Last word in first PLT entry is &GOT[0] - .
6646 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
6647 got_address - (plt_address + 16));
6648 pov += sizeof(first_plt_entry);
6650 unsigned char* got_pov = got_view;
6652 memset(got_pov, 0, 12);
6655 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
6656 unsigned int plt_offset = sizeof(first_plt_entry);
6657 unsigned int plt_rel_offset = 0;
6658 unsigned int got_offset = 12;
6659 const unsigned int count = this->count_;
6660 for (unsigned int i = 0;
6663 pov += sizeof(plt_entry),
6665 plt_offset += sizeof(plt_entry),
6666 plt_rel_offset += rel_size,
6669 // Set and adjust the PLT entry itself.
6670 int32_t offset = ((got_address + got_offset)
6671 - (plt_address + plt_offset + 8));
6673 gold_assert(offset >= 0 && offset < 0x0fffffff);
6674 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
6675 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
6676 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
6677 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
6678 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
6679 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
6681 // Set the entry in the GOT.
6682 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
6685 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
6686 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
6688 of->write_output_view(offset, oview_size, oview);
6689 of->write_output_view(got_file_offset, got_size, got_view);
6692 // Create a PLT entry for a global symbol.
6694 template<bool big_endian>
6696 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
6699 if (gsym->has_plt_offset())
6702 if (this->plt_ == NULL)
6704 // Create the GOT sections first.
6705 this->got_section(symtab, layout);
6707 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
6708 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
6710 | elfcpp::SHF_EXECINSTR),
6711 this->plt_, false, false, false, false);
6713 this->plt_->add_entry(gsym);
6716 // Get the section to use for TLS_DESC relocations.
6718 template<bool big_endian>
6719 typename Target_arm<big_endian>::Reloc_section*
6720 Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
6722 return this->plt_section()->rel_tls_desc(layout);
6725 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
6727 template<bool big_endian>
6729 Target_arm<big_endian>::define_tls_base_symbol(
6730 Symbol_table* symtab,
6733 if (this->tls_base_symbol_defined_)
6736 Output_segment* tls_segment = layout->tls_segment();
6737 if (tls_segment != NULL)
6739 bool is_exec = parameters->options().output_is_executable();
6740 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
6741 Symbol_table::PREDEFINED,
6745 elfcpp::STV_HIDDEN, 0,
6747 ? Symbol::SEGMENT_END
6748 : Symbol::SEGMENT_START),
6751 this->tls_base_symbol_defined_ = true;
6754 // Create a GOT entry for the TLS module index.
6756 template<bool big_endian>
6758 Target_arm<big_endian>::got_mod_index_entry(
6759 Symbol_table* symtab,
6761 Sized_relobj<32, big_endian>* object)
6763 if (this->got_mod_index_offset_ == -1U)
6765 gold_assert(symtab != NULL && layout != NULL && object != NULL);
6766 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
6767 Output_data_got<32, big_endian>* got = this->got_section(symtab, layout);
6768 unsigned int got_offset = got->add_constant(0);
6769 rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
6771 got->add_constant(0);
6772 this->got_mod_index_offset_ = got_offset;
6774 return this->got_mod_index_offset_;
6777 // Optimize the TLS relocation type based on what we know about the
6778 // symbol. IS_FINAL is true if the final address of this symbol is
6779 // known at link time.
6781 template<bool big_endian>
6782 tls::Tls_optimization
6783 Target_arm<big_endian>::optimize_tls_reloc(bool, int)
6785 // FIXME: Currently we do not do any TLS optimization.
6786 return tls::TLSOPT_NONE;
6789 // Report an unsupported relocation against a local symbol.
6791 template<bool big_endian>
6793 Target_arm<big_endian>::Scan::unsupported_reloc_local(
6794 Sized_relobj<32, big_endian>* object,
6795 unsigned int r_type)
6797 gold_error(_("%s: unsupported reloc %u against local symbol"),
6798 object->name().c_str(), r_type);
6801 // We are about to emit a dynamic relocation of type R_TYPE. If the
6802 // dynamic linker does not support it, issue an error. The GNU linker
6803 // only issues a non-PIC error for an allocated read-only section.
6804 // Here we know the section is allocated, but we don't know that it is
6805 // read-only. But we check for all the relocation types which the
6806 // glibc dynamic linker supports, so it seems appropriate to issue an
6807 // error even if the section is not read-only.
6809 template<bool big_endian>
6811 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
6812 unsigned int r_type)
6816 // These are the relocation types supported by glibc for ARM.
6817 case elfcpp::R_ARM_RELATIVE:
6818 case elfcpp::R_ARM_COPY:
6819 case elfcpp::R_ARM_GLOB_DAT:
6820 case elfcpp::R_ARM_JUMP_SLOT:
6821 case elfcpp::R_ARM_ABS32:
6822 case elfcpp::R_ARM_ABS32_NOI:
6823 case elfcpp::R_ARM_PC24:
6824 // FIXME: The following 3 types are not supported by Android's dynamic
6826 case elfcpp::R_ARM_TLS_DTPMOD32:
6827 case elfcpp::R_ARM_TLS_DTPOFF32:
6828 case elfcpp::R_ARM_TLS_TPOFF32:
6833 // This prevents us from issuing more than one error per reloc
6834 // section. But we can still wind up issuing more than one
6835 // error per object file.
6836 if (this->issued_non_pic_error_)
6838 const Arm_reloc_property* reloc_property =
6839 arm_reloc_property_table->get_reloc_property(r_type);
6840 gold_assert(reloc_property != NULL);
6841 object->error(_("requires unsupported dynamic reloc %s; "
6842 "recompile with -fPIC"),
6843 reloc_property->name().c_str());
6844 this->issued_non_pic_error_ = true;
6848 case elfcpp::R_ARM_NONE:
6853 // Scan a relocation for a local symbol.
6854 // FIXME: This only handles a subset of relocation types used by Android
6855 // on ARM v5te devices.
6857 template<bool big_endian>
6859 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
6862 Sized_relobj<32, big_endian>* object,
6863 unsigned int data_shndx,
6864 Output_section* output_section,
6865 const elfcpp::Rel<32, big_endian>& reloc,
6866 unsigned int r_type,
6867 const elfcpp::Sym<32, big_endian>& lsym)
6869 r_type = get_real_reloc_type(r_type);
6872 case elfcpp::R_ARM_NONE:
6873 case elfcpp::R_ARM_V4BX:
6874 case elfcpp::R_ARM_GNU_VTENTRY:
6875 case elfcpp::R_ARM_GNU_VTINHERIT:
6878 case elfcpp::R_ARM_ABS32:
6879 case elfcpp::R_ARM_ABS32_NOI:
6880 // If building a shared library (or a position-independent
6881 // executable), we need to create a dynamic relocation for
6882 // this location. The relocation applied at link time will
6883 // apply the link-time value, so we flag the location with
6884 // an R_ARM_RELATIVE relocation so the dynamic loader can
6885 // relocate it easily.
6886 if (parameters->options().output_is_position_independent())
6888 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6889 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
6890 // If we are to add more other reloc types than R_ARM_ABS32,
6891 // we need to add check_non_pic(object, r_type) here.
6892 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
6893 output_section, data_shndx,
6894 reloc.get_r_offset());
6898 case elfcpp::R_ARM_ABS16:
6899 case elfcpp::R_ARM_ABS12:
6900 case elfcpp::R_ARM_THM_ABS5:
6901 case elfcpp::R_ARM_ABS8:
6902 case elfcpp::R_ARM_BASE_ABS:
6903 case elfcpp::R_ARM_MOVW_ABS_NC:
6904 case elfcpp::R_ARM_MOVT_ABS:
6905 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6906 case elfcpp::R_ARM_THM_MOVT_ABS:
6907 // If building a shared library (or a position-independent
6908 // executable), we need to create a dynamic relocation for
6909 // this location. Because the addend needs to remain in the
6910 // data section, we need to be careful not to apply this
6911 // relocation statically.
6912 if (parameters->options().output_is_position_independent())
6914 check_non_pic(object, r_type);
6915 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6916 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
6917 if (lsym.get_st_type() != elfcpp::STT_SECTION)
6918 rel_dyn->add_local(object, r_sym, r_type, output_section,
6919 data_shndx, reloc.get_r_offset());
6922 gold_assert(lsym.get_st_value() == 0);
6923 unsigned int shndx = lsym.get_st_shndx();
6925 shndx = object->adjust_sym_shndx(r_sym, shndx,
6928 object->error(_("section symbol %u has bad shndx %u"),
6931 rel_dyn->add_local_section(object, shndx,
6932 r_type, output_section,
6933 data_shndx, reloc.get_r_offset());
6938 case elfcpp::R_ARM_PC24:
6939 case elfcpp::R_ARM_REL32:
6940 case elfcpp::R_ARM_LDR_PC_G0:
6941 case elfcpp::R_ARM_SBREL32:
6942 case elfcpp::R_ARM_THM_CALL:
6943 case elfcpp::R_ARM_THM_PC8:
6944 case elfcpp::R_ARM_BASE_PREL:
6945 case elfcpp::R_ARM_PLT32:
6946 case elfcpp::R_ARM_CALL:
6947 case elfcpp::R_ARM_JUMP24:
6948 case elfcpp::R_ARM_THM_JUMP24:
6949 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
6950 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
6951 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
6952 case elfcpp::R_ARM_SBREL31:
6953 case elfcpp::R_ARM_PREL31:
6954 case elfcpp::R_ARM_MOVW_PREL_NC:
6955 case elfcpp::R_ARM_MOVT_PREL:
6956 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6957 case elfcpp::R_ARM_THM_MOVT_PREL:
6958 case elfcpp::R_ARM_THM_JUMP19:
6959 case elfcpp::R_ARM_THM_JUMP6:
6960 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
6961 case elfcpp::R_ARM_THM_PC12:
6962 case elfcpp::R_ARM_REL32_NOI:
6963 case elfcpp::R_ARM_ALU_PC_G0_NC:
6964 case elfcpp::R_ARM_ALU_PC_G0:
6965 case elfcpp::R_ARM_ALU_PC_G1_NC:
6966 case elfcpp::R_ARM_ALU_PC_G1:
6967 case elfcpp::R_ARM_ALU_PC_G2:
6968 case elfcpp::R_ARM_LDR_PC_G1:
6969 case elfcpp::R_ARM_LDR_PC_G2:
6970 case elfcpp::R_ARM_LDRS_PC_G0:
6971 case elfcpp::R_ARM_LDRS_PC_G1:
6972 case elfcpp::R_ARM_LDRS_PC_G2:
6973 case elfcpp::R_ARM_LDC_PC_G0:
6974 case elfcpp::R_ARM_LDC_PC_G1:
6975 case elfcpp::R_ARM_LDC_PC_G2:
6976 case elfcpp::R_ARM_ALU_SB_G0_NC:
6977 case elfcpp::R_ARM_ALU_SB_G0:
6978 case elfcpp::R_ARM_ALU_SB_G1_NC:
6979 case elfcpp::R_ARM_ALU_SB_G1:
6980 case elfcpp::R_ARM_ALU_SB_G2:
6981 case elfcpp::R_ARM_LDR_SB_G0:
6982 case elfcpp::R_ARM_LDR_SB_G1:
6983 case elfcpp::R_ARM_LDR_SB_G2:
6984 case elfcpp::R_ARM_LDRS_SB_G0:
6985 case elfcpp::R_ARM_LDRS_SB_G1:
6986 case elfcpp::R_ARM_LDRS_SB_G2:
6987 case elfcpp::R_ARM_LDC_SB_G0:
6988 case elfcpp::R_ARM_LDC_SB_G1:
6989 case elfcpp::R_ARM_LDC_SB_G2:
6990 case elfcpp::R_ARM_MOVW_BREL_NC:
6991 case elfcpp::R_ARM_MOVT_BREL:
6992 case elfcpp::R_ARM_MOVW_BREL:
6993 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
6994 case elfcpp::R_ARM_THM_MOVT_BREL:
6995 case elfcpp::R_ARM_THM_MOVW_BREL:
6996 case elfcpp::R_ARM_THM_JUMP11:
6997 case elfcpp::R_ARM_THM_JUMP8:
6998 // We don't need to do anything for a relative addressing relocation
6999 // against a local symbol if it does not reference the GOT.
7002 case elfcpp::R_ARM_GOTOFF32:
7003 case elfcpp::R_ARM_GOTOFF12:
7004 // We need a GOT section:
7005 target->got_section(symtab, layout);
7008 case elfcpp::R_ARM_GOT_BREL:
7009 case elfcpp::R_ARM_GOT_PREL:
7011 // The symbol requires a GOT entry.
7012 Output_data_got<32, big_endian>* got =
7013 target->got_section(symtab, layout);
7014 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7015 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
7017 // If we are generating a shared object, we need to add a
7018 // dynamic RELATIVE relocation for this symbol's GOT entry.
7019 if (parameters->options().output_is_position_independent())
7021 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7022 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7023 rel_dyn->add_local_relative(
7024 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
7025 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
7031 case elfcpp::R_ARM_TARGET1:
7032 case elfcpp::R_ARM_TARGET2:
7033 // This should have been mapped to another type already.
7035 case elfcpp::R_ARM_COPY:
7036 case elfcpp::R_ARM_GLOB_DAT:
7037 case elfcpp::R_ARM_JUMP_SLOT:
7038 case elfcpp::R_ARM_RELATIVE:
7039 // These are relocations which should only be seen by the
7040 // dynamic linker, and should never be seen here.
7041 gold_error(_("%s: unexpected reloc %u in object file"),
7042 object->name().c_str(), r_type);
7046 // These are initial TLS relocs, which are expected when
7048 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7049 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7050 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7051 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7052 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7054 bool output_is_shared = parameters->options().shared();
7055 const tls::Tls_optimization optimized_type
7056 = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
7060 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7061 if (optimized_type == tls::TLSOPT_NONE)
7063 // Create a pair of GOT entries for the module index and
7064 // dtv-relative offset.
7065 Output_data_got<32, big_endian>* got
7066 = target->got_section(symtab, layout);
7067 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7068 unsigned int shndx = lsym.get_st_shndx();
7070 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7072 object->error(_("local symbol %u has bad shndx %u"),
7075 got->add_local_pair_with_rel(object, r_sym, shndx,
7077 target->rel_dyn_section(layout),
7078 elfcpp::R_ARM_TLS_DTPMOD32, 0);
7081 // FIXME: TLS optimization not supported yet.
7085 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7086 if (optimized_type == tls::TLSOPT_NONE)
7088 // Create a GOT entry for the module index.
7089 target->got_mod_index_entry(symtab, layout, object);
7092 // FIXME: TLS optimization not supported yet.
7096 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7099 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7100 layout->set_has_static_tls();
7101 if (optimized_type == tls::TLSOPT_NONE)
7103 // Create a GOT entry for the tp-relative offset.
7104 Output_data_got<32, big_endian>* got
7105 = target->got_section(symtab, layout);
7106 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7107 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
7108 target->rel_dyn_section(layout),
7109 elfcpp::R_ARM_TLS_TPOFF32);
7112 // FIXME: TLS optimization not supported yet.
7116 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7117 layout->set_has_static_tls();
7118 if (output_is_shared)
7120 // We need to create a dynamic relocation.
7121 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
7122 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7123 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7124 rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
7125 output_section, data_shndx,
7126 reloc.get_r_offset());
7137 unsupported_reloc_local(object, r_type);
7142 // Report an unsupported relocation against a global symbol.
7144 template<bool big_endian>
7146 Target_arm<big_endian>::Scan::unsupported_reloc_global(
7147 Sized_relobj<32, big_endian>* object,
7148 unsigned int r_type,
7151 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7152 object->name().c_str(), r_type, gsym->demangled_name().c_str());
7155 // Scan a relocation for a global symbol.
7157 template<bool big_endian>
7159 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
7162 Sized_relobj<32, big_endian>* object,
7163 unsigned int data_shndx,
7164 Output_section* output_section,
7165 const elfcpp::Rel<32, big_endian>& reloc,
7166 unsigned int r_type,
7169 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
7170 // section. We check here to avoid creating a dynamic reloc against
7171 // _GLOBAL_OFFSET_TABLE_.
7172 if (!target->has_got_section()
7173 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7174 target->got_section(symtab, layout);
7176 r_type = get_real_reloc_type(r_type);
7179 case elfcpp::R_ARM_NONE:
7180 case elfcpp::R_ARM_V4BX:
7181 case elfcpp::R_ARM_GNU_VTENTRY:
7182 case elfcpp::R_ARM_GNU_VTINHERIT:
7185 case elfcpp::R_ARM_ABS32:
7186 case elfcpp::R_ARM_ABS16:
7187 case elfcpp::R_ARM_ABS12:
7188 case elfcpp::R_ARM_THM_ABS5:
7189 case elfcpp::R_ARM_ABS8:
7190 case elfcpp::R_ARM_BASE_ABS:
7191 case elfcpp::R_ARM_MOVW_ABS_NC:
7192 case elfcpp::R_ARM_MOVT_ABS:
7193 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7194 case elfcpp::R_ARM_THM_MOVT_ABS:
7195 case elfcpp::R_ARM_ABS32_NOI:
7196 // Absolute addressing relocations.
7198 // Make a PLT entry if necessary.
7199 if (this->symbol_needs_plt_entry(gsym))
7201 target->make_plt_entry(symtab, layout, gsym);
7202 // Since this is not a PC-relative relocation, we may be
7203 // taking the address of a function. In that case we need to
7204 // set the entry in the dynamic symbol table to the address of
7206 if (gsym->is_from_dynobj() && !parameters->options().shared())
7207 gsym->set_needs_dynsym_value();
7209 // Make a dynamic relocation if necessary.
7210 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
7212 if (gsym->may_need_copy_reloc())
7214 target->copy_reloc(symtab, layout, object,
7215 data_shndx, output_section, gsym, reloc);
7217 else if ((r_type == elfcpp::R_ARM_ABS32
7218 || r_type == elfcpp::R_ARM_ABS32_NOI)
7219 && gsym->can_use_relative_reloc(false))
7221 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7222 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
7223 output_section, object,
7224 data_shndx, reloc.get_r_offset());
7228 check_non_pic(object, r_type);
7229 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7230 rel_dyn->add_global(gsym, r_type, output_section, object,
7231 data_shndx, reloc.get_r_offset());
7237 case elfcpp::R_ARM_GOTOFF32:
7238 case elfcpp::R_ARM_GOTOFF12:
7239 // We need a GOT section.
7240 target->got_section(symtab, layout);
7243 case elfcpp::R_ARM_REL32:
7244 case elfcpp::R_ARM_LDR_PC_G0:
7245 case elfcpp::R_ARM_SBREL32:
7246 case elfcpp::R_ARM_THM_PC8:
7247 case elfcpp::R_ARM_BASE_PREL:
7248 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7249 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7250 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7251 case elfcpp::R_ARM_MOVW_PREL_NC:
7252 case elfcpp::R_ARM_MOVT_PREL:
7253 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7254 case elfcpp::R_ARM_THM_MOVT_PREL:
7255 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7256 case elfcpp::R_ARM_THM_PC12:
7257 case elfcpp::R_ARM_REL32_NOI:
7258 case elfcpp::R_ARM_ALU_PC_G0_NC:
7259 case elfcpp::R_ARM_ALU_PC_G0:
7260 case elfcpp::R_ARM_ALU_PC_G1_NC:
7261 case elfcpp::R_ARM_ALU_PC_G1:
7262 case elfcpp::R_ARM_ALU_PC_G2:
7263 case elfcpp::R_ARM_LDR_PC_G1:
7264 case elfcpp::R_ARM_LDR_PC_G2:
7265 case elfcpp::R_ARM_LDRS_PC_G0:
7266 case elfcpp::R_ARM_LDRS_PC_G1:
7267 case elfcpp::R_ARM_LDRS_PC_G2:
7268 case elfcpp::R_ARM_LDC_PC_G0:
7269 case elfcpp::R_ARM_LDC_PC_G1:
7270 case elfcpp::R_ARM_LDC_PC_G2:
7271 case elfcpp::R_ARM_ALU_SB_G0_NC:
7272 case elfcpp::R_ARM_ALU_SB_G0:
7273 case elfcpp::R_ARM_ALU_SB_G1_NC:
7274 case elfcpp::R_ARM_ALU_SB_G1:
7275 case elfcpp::R_ARM_ALU_SB_G2:
7276 case elfcpp::R_ARM_LDR_SB_G0:
7277 case elfcpp::R_ARM_LDR_SB_G1:
7278 case elfcpp::R_ARM_LDR_SB_G2:
7279 case elfcpp::R_ARM_LDRS_SB_G0:
7280 case elfcpp::R_ARM_LDRS_SB_G1:
7281 case elfcpp::R_ARM_LDRS_SB_G2:
7282 case elfcpp::R_ARM_LDC_SB_G0:
7283 case elfcpp::R_ARM_LDC_SB_G1:
7284 case elfcpp::R_ARM_LDC_SB_G2:
7285 case elfcpp::R_ARM_MOVW_BREL_NC:
7286 case elfcpp::R_ARM_MOVT_BREL:
7287 case elfcpp::R_ARM_MOVW_BREL:
7288 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7289 case elfcpp::R_ARM_THM_MOVT_BREL:
7290 case elfcpp::R_ARM_THM_MOVW_BREL:
7291 // Relative addressing relocations.
7293 // Make a dynamic relocation if necessary.
7294 int flags = Symbol::NON_PIC_REF;
7295 if (gsym->needs_dynamic_reloc(flags))
7297 if (target->may_need_copy_reloc(gsym))
7299 target->copy_reloc(symtab, layout, object,
7300 data_shndx, output_section, gsym, reloc);
7304 check_non_pic(object, r_type);
7305 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7306 rel_dyn->add_global(gsym, r_type, output_section, object,
7307 data_shndx, reloc.get_r_offset());
7313 case elfcpp::R_ARM_PC24:
7314 case elfcpp::R_ARM_THM_CALL:
7315 case elfcpp::R_ARM_PLT32:
7316 case elfcpp::R_ARM_CALL:
7317 case elfcpp::R_ARM_JUMP24:
7318 case elfcpp::R_ARM_THM_JUMP24:
7319 case elfcpp::R_ARM_SBREL31:
7320 case elfcpp::R_ARM_PREL31:
7321 case elfcpp::R_ARM_THM_JUMP19:
7322 case elfcpp::R_ARM_THM_JUMP6:
7323 case elfcpp::R_ARM_THM_JUMP11:
7324 case elfcpp::R_ARM_THM_JUMP8:
7325 // All the relocation above are branches except for the PREL31 ones.
7326 // A PREL31 relocation can point to a personality function in a shared
7327 // library. In that case we want to use a PLT because we want to
7328 // call the personality routine and the dyanmic linkers we care about
7329 // do not support dynamic PREL31 relocations. An REL31 relocation may
7330 // point to a function whose unwinding behaviour is being described but
7331 // we will not mistakenly generate a PLT for that because we should use
7332 // a local section symbol.
7334 // If the symbol is fully resolved, this is just a relative
7335 // local reloc. Otherwise we need a PLT entry.
7336 if (gsym->final_value_is_known())
7338 // If building a shared library, we can also skip the PLT entry
7339 // if the symbol is defined in the output file and is protected
7341 if (gsym->is_defined()
7342 && !gsym->is_from_dynobj()
7343 && !gsym->is_preemptible())
7345 target->make_plt_entry(symtab, layout, gsym);
7348 case elfcpp::R_ARM_GOT_BREL:
7349 case elfcpp::R_ARM_GOT_ABS:
7350 case elfcpp::R_ARM_GOT_PREL:
7352 // The symbol requires a GOT entry.
7353 Output_data_got<32, big_endian>* got =
7354 target->got_section(symtab, layout);
7355 if (gsym->final_value_is_known())
7356 got->add_global(gsym, GOT_TYPE_STANDARD);
7359 // If this symbol is not fully resolved, we need to add a
7360 // GOT entry with a dynamic relocation.
7361 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7362 if (gsym->is_from_dynobj()
7363 || gsym->is_undefined()
7364 || gsym->is_preemptible())
7365 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
7366 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
7369 if (got->add_global(gsym, GOT_TYPE_STANDARD))
7370 rel_dyn->add_global_relative(
7371 gsym, elfcpp::R_ARM_RELATIVE, got,
7372 gsym->got_offset(GOT_TYPE_STANDARD));
7378 case elfcpp::R_ARM_TARGET1:
7379 case elfcpp::R_ARM_TARGET2:
7380 // These should have been mapped to other types already.
7382 case elfcpp::R_ARM_COPY:
7383 case elfcpp::R_ARM_GLOB_DAT:
7384 case elfcpp::R_ARM_JUMP_SLOT:
7385 case elfcpp::R_ARM_RELATIVE:
7386 // These are relocations which should only be seen by the
7387 // dynamic linker, and should never be seen here.
7388 gold_error(_("%s: unexpected reloc %u in object file"),
7389 object->name().c_str(), r_type);
7392 // These are initial tls relocs, which are expected when
7394 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7395 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7396 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7397 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7398 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7400 const bool is_final = gsym->final_value_is_known();
7401 const tls::Tls_optimization optimized_type
7402 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
7405 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7406 if (optimized_type == tls::TLSOPT_NONE)
7408 // Create a pair of GOT entries for the module index and
7409 // dtv-relative offset.
7410 Output_data_got<32, big_endian>* got
7411 = target->got_section(symtab, layout);
7412 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
7413 target->rel_dyn_section(layout),
7414 elfcpp::R_ARM_TLS_DTPMOD32,
7415 elfcpp::R_ARM_TLS_DTPOFF32);
7418 // FIXME: TLS optimization not supported yet.
7422 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7423 if (optimized_type == tls::TLSOPT_NONE)
7425 // Create a GOT entry for the module index.
7426 target->got_mod_index_entry(symtab, layout, object);
7429 // FIXME: TLS optimization not supported yet.
7433 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7436 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7437 layout->set_has_static_tls();
7438 if (optimized_type == tls::TLSOPT_NONE)
7440 // Create a GOT entry for the tp-relative offset.
7441 Output_data_got<32, big_endian>* got
7442 = target->got_section(symtab, layout);
7443 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
7444 target->rel_dyn_section(layout),
7445 elfcpp::R_ARM_TLS_TPOFF32);
7448 // FIXME: TLS optimization not supported yet.
7452 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7453 layout->set_has_static_tls();
7454 if (parameters->options().shared())
7456 // We need to create a dynamic relocation.
7457 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7458 rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
7459 output_section, object,
7460 data_shndx, reloc.get_r_offset());
7471 unsupported_reloc_global(object, r_type, gsym);
7476 // Process relocations for gc.
7478 template<bool big_endian>
7480 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
7482 Sized_relobj<32, big_endian>* object,
7483 unsigned int data_shndx,
7485 const unsigned char* prelocs,
7487 Output_section* output_section,
7488 bool needs_special_offset_handling,
7489 size_t local_symbol_count,
7490 const unsigned char* plocal_symbols)
7492 typedef Target_arm<big_endian> Arm;
7493 typedef typename Target_arm<big_endian>::Scan Scan;
7495 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
7504 needs_special_offset_handling,
7509 // Scan relocations for a section.
7511 template<bool big_endian>
7513 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
7515 Sized_relobj<32, big_endian>* object,
7516 unsigned int data_shndx,
7517 unsigned int sh_type,
7518 const unsigned char* prelocs,
7520 Output_section* output_section,
7521 bool needs_special_offset_handling,
7522 size_t local_symbol_count,
7523 const unsigned char* plocal_symbols)
7525 typedef typename Target_arm<big_endian>::Scan Scan;
7526 if (sh_type == elfcpp::SHT_RELA)
7528 gold_error(_("%s: unsupported RELA reloc section"),
7529 object->name().c_str());
7533 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
7542 needs_special_offset_handling,
7547 // Finalize the sections.
7549 template<bool big_endian>
7551 Target_arm<big_endian>::do_finalize_sections(
7553 const Input_objects* input_objects,
7554 Symbol_table* symtab)
7556 // Merge processor-specific flags.
7557 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
7558 p != input_objects->relobj_end();
7561 Arm_relobj<big_endian>* arm_relobj =
7562 Arm_relobj<big_endian>::as_arm_relobj(*p);
7563 this->merge_processor_specific_flags(
7565 arm_relobj->processor_specific_flags());
7566 this->merge_object_attributes(arm_relobj->name().c_str(),
7567 arm_relobj->attributes_section_data());
7571 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
7572 p != input_objects->dynobj_end();
7575 Arm_dynobj<big_endian>* arm_dynobj =
7576 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
7577 this->merge_processor_specific_flags(
7579 arm_dynobj->processor_specific_flags());
7580 this->merge_object_attributes(arm_dynobj->name().c_str(),
7581 arm_dynobj->attributes_section_data());
7585 const Object_attribute* cpu_arch_attr =
7586 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
7587 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
7588 this->set_may_use_blx(true);
7590 // Check if we need to use Cortex-A8 workaround.
7591 if (parameters->options().user_set_fix_cortex_a8())
7592 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
7595 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
7596 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
7598 const Object_attribute* cpu_arch_profile_attr =
7599 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
7600 this->fix_cortex_a8_ =
7601 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
7602 && (cpu_arch_profile_attr->int_value() == 'A'
7603 || cpu_arch_profile_attr->int_value() == 0));
7606 // Check if we can use V4BX interworking.
7607 // The V4BX interworking stub contains BX instruction,
7608 // which is not specified for some profiles.
7609 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
7610 && !this->may_use_blx())
7611 gold_error(_("unable to provide V4BX reloc interworking fix up; "
7612 "the target profile does not support BX instruction"));
7614 // Fill in some more dynamic tags.
7615 const Reloc_section* rel_plt = (this->plt_ == NULL
7617 : this->plt_->rel_plt());
7618 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
7619 this->rel_dyn_, true, false);
7621 // Emit any relocs we saved in an attempt to avoid generating COPY
7623 if (this->copy_relocs_.any_saved_relocs())
7624 this->copy_relocs_.emit(this->rel_dyn_section(layout));
7626 // Handle the .ARM.exidx section.
7627 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
7628 if (exidx_section != NULL
7629 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
7630 && !parameters->options().relocatable())
7632 // Create __exidx_start and __exdix_end symbols.
7633 symtab->define_in_output_data("__exidx_start", NULL,
7634 Symbol_table::PREDEFINED,
7635 exidx_section, 0, 0, elfcpp::STT_OBJECT,
7636 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
7638 symtab->define_in_output_data("__exidx_end", NULL,
7639 Symbol_table::PREDEFINED,
7640 exidx_section, 0, 0, elfcpp::STT_OBJECT,
7641 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
7644 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
7645 // the .ARM.exidx section.
7646 if (!layout->script_options()->saw_phdrs_clause())
7648 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
7650 Output_segment* exidx_segment =
7651 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
7652 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
7657 // Create an .ARM.attributes section if there is not one already.
7658 Output_attributes_section_data* attributes_section =
7659 new Output_attributes_section_data(*this->attributes_section_data_);
7660 layout->add_output_section_data(".ARM.attributes",
7661 elfcpp::SHT_ARM_ATTRIBUTES, 0,
7662 attributes_section, false, false, false,
7666 // Return whether a direct absolute static relocation needs to be applied.
7667 // In cases where Scan::local() or Scan::global() has created
7668 // a dynamic relocation other than R_ARM_RELATIVE, the addend
7669 // of the relocation is carried in the data, and we must not
7670 // apply the static relocation.
7672 template<bool big_endian>
7674 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
7675 const Sized_symbol<32>* gsym,
7678 Output_section* output_section)
7680 // If the output section is not allocated, then we didn't call
7681 // scan_relocs, we didn't create a dynamic reloc, and we must apply
7683 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
7686 // For local symbols, we will have created a non-RELATIVE dynamic
7687 // relocation only if (a) the output is position independent,
7688 // (b) the relocation is absolute (not pc- or segment-relative), and
7689 // (c) the relocation is not 32 bits wide.
7691 return !(parameters->options().output_is_position_independent()
7692 && (ref_flags & Symbol::ABSOLUTE_REF)
7695 // For global symbols, we use the same helper routines used in the
7696 // scan pass. If we did not create a dynamic relocation, or if we
7697 // created a RELATIVE dynamic relocation, we should apply the static
7699 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
7700 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
7701 && gsym->can_use_relative_reloc(ref_flags
7702 & Symbol::FUNCTION_CALL);
7703 return !has_dyn || is_rel;
7706 // Perform a relocation.
7708 template<bool big_endian>
7710 Target_arm<big_endian>::Relocate::relocate(
7711 const Relocate_info<32, big_endian>* relinfo,
7713 Output_section *output_section,
7715 const elfcpp::Rel<32, big_endian>& rel,
7716 unsigned int r_type,
7717 const Sized_symbol<32>* gsym,
7718 const Symbol_value<32>* psymval,
7719 unsigned char* view,
7720 Arm_address address,
7721 section_size_type view_size)
7723 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
7725 r_type = get_real_reloc_type(r_type);
7726 const Arm_reloc_property* reloc_property =
7727 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
7728 if (reloc_property == NULL)
7730 std::string reloc_name =
7731 arm_reloc_property_table->reloc_name_in_error_message(r_type);
7732 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
7733 _("cannot relocate %s in object file"),
7734 reloc_name.c_str());
7738 const Arm_relobj<big_endian>* object =
7739 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
7741 // If the final branch target of a relocation is THUMB instruction, this
7742 // is 1. Otherwise it is 0.
7743 Arm_address thumb_bit = 0;
7744 Symbol_value<32> symval;
7745 bool is_weakly_undefined_without_plt = false;
7746 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
7750 // This is a global symbol. Determine if we use PLT and if the
7751 // final target is THUMB.
7752 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
7754 // This uses a PLT, change the symbol value.
7755 symval.set_output_value(target->plt_section()->address()
7756 + gsym->plt_offset());
7759 else if (gsym->is_weak_undefined())
7761 // This is a weakly undefined symbol and we do not use PLT
7762 // for this relocation. A branch targeting this symbol will
7763 // be converted into an NOP.
7764 is_weakly_undefined_without_plt = true;
7768 // Set thumb bit if symbol:
7769 // -Has type STT_ARM_TFUNC or
7770 // -Has type STT_FUNC, is defined and with LSB in value set.
7772 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
7773 || (gsym->type() == elfcpp::STT_FUNC
7774 && !gsym->is_undefined()
7775 && ((psymval->value(object, 0) & 1) != 0)))
7782 // This is a local symbol. Determine if the final target is THUMB.
7783 // We saved this information when all the local symbols were read.
7784 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
7785 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
7786 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
7791 // This is a fake relocation synthesized for a stub. It does not have
7792 // a real symbol. We just look at the LSB of the symbol value to
7793 // determine if the target is THUMB or not.
7794 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
7797 // Strip LSB if this points to a THUMB target.
7799 && reloc_property->uses_thumb_bit()
7800 && ((psymval->value(object, 0) & 1) != 0))
7802 Arm_address stripped_value =
7803 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
7804 symval.set_output_value(stripped_value);
7808 // Get the GOT offset if needed.
7809 // The GOT pointer points to the end of the GOT section.
7810 // We need to subtract the size of the GOT section to get
7811 // the actual offset to use in the relocation.
7812 bool have_got_offset = false;
7813 unsigned int got_offset = 0;
7816 case elfcpp::R_ARM_GOT_BREL:
7817 case elfcpp::R_ARM_GOT_PREL:
7820 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
7821 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
7822 - target->got_size());
7826 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
7827 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
7828 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
7829 - target->got_size());
7831 have_got_offset = true;
7838 // To look up relocation stubs, we need to pass the symbol table index of
7840 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
7842 // Get the addressing origin of the output segment defining the
7843 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
7844 Arm_address sym_origin = 0;
7845 if (reloc_property->uses_symbol_base())
7847 if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
7848 // R_ARM_BASE_ABS with the NULL symbol will give the
7849 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
7850 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
7851 sym_origin = target->got_plt_section()->address();
7852 else if (gsym == NULL)
7854 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
7855 sym_origin = gsym->output_segment()->vaddr();
7856 else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
7857 sym_origin = gsym->output_data()->address();
7859 // TODO: Assumes the segment base to be zero for the global symbols
7860 // till the proper support for the segment-base-relative addressing
7861 // will be implemented. This is consistent with GNU ld.
7864 // For relative addressing relocation, find out the relative address base.
7865 Arm_address relative_address_base = 0;
7866 switch(reloc_property->relative_address_base())
7868 case Arm_reloc_property::RAB_NONE:
7869 // Relocations with relative address bases RAB_TLS and RAB_tp are
7870 // handled by relocate_tls. So we do not need to do anything here.
7871 case Arm_reloc_property::RAB_TLS:
7872 case Arm_reloc_property::RAB_tp:
7874 case Arm_reloc_property::RAB_B_S:
7875 relative_address_base = sym_origin;
7877 case Arm_reloc_property::RAB_GOT_ORG:
7878 relative_address_base = target->got_plt_section()->address();
7880 case Arm_reloc_property::RAB_P:
7881 relative_address_base = address;
7883 case Arm_reloc_property::RAB_Pa:
7884 relative_address_base = address & 0xfffffffcU;
7890 typename Arm_relocate_functions::Status reloc_status =
7891 Arm_relocate_functions::STATUS_OKAY;
7892 bool check_overflow = reloc_property->checks_overflow();
7895 case elfcpp::R_ARM_NONE:
7898 case elfcpp::R_ARM_ABS8:
7899 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7901 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
7904 case elfcpp::R_ARM_ABS12:
7905 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7907 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
7910 case elfcpp::R_ARM_ABS16:
7911 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7913 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
7916 case elfcpp::R_ARM_ABS32:
7917 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
7919 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
7923 case elfcpp::R_ARM_ABS32_NOI:
7924 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
7926 // No thumb bit for this relocation: (S + A)
7927 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
7931 case elfcpp::R_ARM_MOVW_ABS_NC:
7932 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7934 reloc_status = Arm_relocate_functions::movw(view, object, psymval,
7939 case elfcpp::R_ARM_MOVT_ABS:
7940 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7942 reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
7945 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7946 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7948 reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
7949 0, thumb_bit, false);
7952 case elfcpp::R_ARM_THM_MOVT_ABS:
7953 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7955 reloc_status = Arm_relocate_functions::thm_movt(view, object,
7959 case elfcpp::R_ARM_MOVW_PREL_NC:
7960 case elfcpp::R_ARM_MOVW_BREL_NC:
7961 case elfcpp::R_ARM_MOVW_BREL:
7963 Arm_relocate_functions::movw(view, object, psymval,
7964 relative_address_base, thumb_bit,
7968 case elfcpp::R_ARM_MOVT_PREL:
7969 case elfcpp::R_ARM_MOVT_BREL:
7971 Arm_relocate_functions::movt(view, object, psymval,
7972 relative_address_base);
7975 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7976 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7977 case elfcpp::R_ARM_THM_MOVW_BREL:
7979 Arm_relocate_functions::thm_movw(view, object, psymval,
7980 relative_address_base,
7981 thumb_bit, check_overflow);
7984 case elfcpp::R_ARM_THM_MOVT_PREL:
7985 case elfcpp::R_ARM_THM_MOVT_BREL:
7987 Arm_relocate_functions::thm_movt(view, object, psymval,
7988 relative_address_base);
7991 case elfcpp::R_ARM_REL32:
7992 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
7993 address, thumb_bit);
7996 case elfcpp::R_ARM_THM_ABS5:
7997 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
7999 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
8002 // Thumb long branches.
8003 case elfcpp::R_ARM_THM_CALL:
8004 case elfcpp::R_ARM_THM_XPC22:
8005 case elfcpp::R_ARM_THM_JUMP24:
8007 Arm_relocate_functions::thumb_branch_common(
8008 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8009 thumb_bit, is_weakly_undefined_without_plt);
8012 case elfcpp::R_ARM_GOTOFF32:
8014 Arm_address got_origin;
8015 got_origin = target->got_plt_section()->address();
8016 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8017 got_origin, thumb_bit);
8021 case elfcpp::R_ARM_BASE_PREL:
8022 gold_assert(gsym != NULL);
8024 Arm_relocate_functions::base_prel(view, sym_origin, address);
8027 case elfcpp::R_ARM_BASE_ABS:
8029 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8033 reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
8037 case elfcpp::R_ARM_GOT_BREL:
8038 gold_assert(have_got_offset);
8039 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
8042 case elfcpp::R_ARM_GOT_PREL:
8043 gold_assert(have_got_offset);
8044 // Get the address origin for GOT PLT, which is allocated right
8045 // after the GOT section, to calculate an absolute address of
8046 // the symbol GOT entry (got_origin + got_offset).
8047 Arm_address got_origin;
8048 got_origin = target->got_plt_section()->address();
8049 reloc_status = Arm_relocate_functions::got_prel(view,
8050 got_origin + got_offset,
8054 case elfcpp::R_ARM_PLT32:
8055 case elfcpp::R_ARM_CALL:
8056 case elfcpp::R_ARM_JUMP24:
8057 case elfcpp::R_ARM_XPC25:
8058 gold_assert(gsym == NULL
8059 || gsym->has_plt_offset()
8060 || gsym->final_value_is_known()
8061 || (gsym->is_defined()
8062 && !gsym->is_from_dynobj()
8063 && !gsym->is_preemptible()));
8065 Arm_relocate_functions::arm_branch_common(
8066 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8067 thumb_bit, is_weakly_undefined_without_plt);
8070 case elfcpp::R_ARM_THM_JUMP19:
8072 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
8076 case elfcpp::R_ARM_THM_JUMP6:
8078 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
8081 case elfcpp::R_ARM_THM_JUMP8:
8083 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
8086 case elfcpp::R_ARM_THM_JUMP11:
8088 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
8091 case elfcpp::R_ARM_PREL31:
8092 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
8093 address, thumb_bit);
8096 case elfcpp::R_ARM_V4BX:
8097 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
8099 const bool is_v4bx_interworking =
8100 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
8102 Arm_relocate_functions::v4bx(relinfo, view, object, address,
8103 is_v4bx_interworking);
8107 case elfcpp::R_ARM_THM_PC8:
8109 Arm_relocate_functions::thm_pc8(view, object, psymval, address);
8112 case elfcpp::R_ARM_THM_PC12:
8114 Arm_relocate_functions::thm_pc12(view, object, psymval, address);
8117 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8119 Arm_relocate_functions::thm_alu11(view, object, psymval, address,
8123 case elfcpp::R_ARM_ALU_PC_G0_NC:
8124 case elfcpp::R_ARM_ALU_PC_G0:
8125 case elfcpp::R_ARM_ALU_PC_G1_NC:
8126 case elfcpp::R_ARM_ALU_PC_G1:
8127 case elfcpp::R_ARM_ALU_PC_G2:
8128 case elfcpp::R_ARM_ALU_SB_G0_NC:
8129 case elfcpp::R_ARM_ALU_SB_G0:
8130 case elfcpp::R_ARM_ALU_SB_G1_NC:
8131 case elfcpp::R_ARM_ALU_SB_G1:
8132 case elfcpp::R_ARM_ALU_SB_G2:
8134 Arm_relocate_functions::arm_grp_alu(view, object, psymval,
8135 reloc_property->group_index(),
8136 relative_address_base,
8137 thumb_bit, check_overflow);
8140 case elfcpp::R_ARM_LDR_PC_G0:
8141 case elfcpp::R_ARM_LDR_PC_G1:
8142 case elfcpp::R_ARM_LDR_PC_G2:
8143 case elfcpp::R_ARM_LDR_SB_G0:
8144 case elfcpp::R_ARM_LDR_SB_G1:
8145 case elfcpp::R_ARM_LDR_SB_G2:
8147 Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
8148 reloc_property->group_index(),
8149 relative_address_base);
8152 case elfcpp::R_ARM_LDRS_PC_G0:
8153 case elfcpp::R_ARM_LDRS_PC_G1:
8154 case elfcpp::R_ARM_LDRS_PC_G2:
8155 case elfcpp::R_ARM_LDRS_SB_G0:
8156 case elfcpp::R_ARM_LDRS_SB_G1:
8157 case elfcpp::R_ARM_LDRS_SB_G2:
8159 Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
8160 reloc_property->group_index(),
8161 relative_address_base);
8164 case elfcpp::R_ARM_LDC_PC_G0:
8165 case elfcpp::R_ARM_LDC_PC_G1:
8166 case elfcpp::R_ARM_LDC_PC_G2:
8167 case elfcpp::R_ARM_LDC_SB_G0:
8168 case elfcpp::R_ARM_LDC_SB_G1:
8169 case elfcpp::R_ARM_LDC_SB_G2:
8171 Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
8172 reloc_property->group_index(),
8173 relative_address_base);
8176 // These are initial tls relocs, which are expected when
8178 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8179 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8180 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8181 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8182 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8184 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
8185 view, address, view_size);
8192 // Report any errors.
8193 switch (reloc_status)
8195 case Arm_relocate_functions::STATUS_OKAY:
8197 case Arm_relocate_functions::STATUS_OVERFLOW:
8198 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8199 _("relocation overflow in relocation %u"),
8202 case Arm_relocate_functions::STATUS_BAD_RELOC:
8203 gold_error_at_location(
8207 _("unexpected opcode while processing relocation %u"),
8217 // Perform a TLS relocation.
8219 template<bool big_endian>
8220 inline typename Arm_relocate_functions<big_endian>::Status
8221 Target_arm<big_endian>::Relocate::relocate_tls(
8222 const Relocate_info<32, big_endian>* relinfo,
8223 Target_arm<big_endian>* target,
8225 const elfcpp::Rel<32, big_endian>& rel,
8226 unsigned int r_type,
8227 const Sized_symbol<32>* gsym,
8228 const Symbol_value<32>* psymval,
8229 unsigned char* view,
8230 elfcpp::Elf_types<32>::Elf_Addr,
8231 section_size_type /*view_size*/ )
8233 typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
8234 Output_segment* tls_segment = relinfo->layout->tls_segment();
8236 const Sized_relobj<32, big_endian>* object = relinfo->object;
8238 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
8240 const bool is_final = (gsym == NULL
8241 ? !parameters->options().shared()
8242 : gsym->final_value_is_known());
8243 const tls::Tls_optimization optimized_type
8244 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
8247 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8249 unsigned int got_type = GOT_TYPE_TLS_PAIR;
8250 unsigned int got_offset;
8253 gold_assert(gsym->has_got_offset(got_type));
8254 got_offset = gsym->got_offset(got_type) - target->got_size();
8258 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8259 gold_assert(object->local_has_got_offset(r_sym, got_type));
8260 got_offset = (object->local_got_offset(r_sym, got_type)
8261 - target->got_size());
8263 if (optimized_type == tls::TLSOPT_NONE)
8265 // Relocate the field with the offset of the pair of GOT
8267 Relocate_functions<32, big_endian>::rel32(view, got_offset);
8268 return ArmRelocFuncs::STATUS_OKAY;
8273 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8274 if (optimized_type == tls::TLSOPT_NONE)
8276 // Relocate the field with the offset of the GOT entry for
8277 // the module index.
8278 unsigned int got_offset;
8279 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
8280 - target->got_size());
8281 Relocate_functions<32, big_endian>::rel32(view, got_offset);
8282 return ArmRelocFuncs::STATUS_OKAY;
8286 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8287 Relocate_functions<32, big_endian>::rel32(view, value);
8288 return ArmRelocFuncs::STATUS_OKAY;
8290 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8291 if (optimized_type == tls::TLSOPT_NONE)
8293 // Relocate the field with the offset of the GOT entry for
8294 // the tp-relative offset of the symbol.
8295 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
8296 unsigned int got_offset;
8299 gold_assert(gsym->has_got_offset(got_type));
8300 got_offset = gsym->got_offset(got_type);
8304 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8305 gold_assert(object->local_has_got_offset(r_sym, got_type));
8306 got_offset = object->local_got_offset(r_sym, got_type);
8308 // All GOT offsets are relative to the end of the GOT.
8309 got_offset -= target->got_size();
8310 Relocate_functions<32, big_endian>::rel32(view, got_offset);
8311 return ArmRelocFuncs::STATUS_OKAY;
8315 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8316 // If we're creating a shared library, a dynamic relocation will
8317 // have been created for this location, so do not apply it now.
8318 if (!parameters->options().shared())
8320 gold_assert(tls_segment != NULL);
8321 value = tls_segment->memsz() - value;
8322 Relocate_functions<32, false>::rel32(view, value);
8324 return ArmRelocFuncs::STATUS_OKAY;
8330 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8331 _("unsupported reloc %u"),
8333 return ArmRelocFuncs::STATUS_BAD_RELOC;
8336 // Relocate section data.
8338 template<bool big_endian>
8340 Target_arm<big_endian>::relocate_section(
8341 const Relocate_info<32, big_endian>* relinfo,
8342 unsigned int sh_type,
8343 const unsigned char* prelocs,
8345 Output_section* output_section,
8346 bool needs_special_offset_handling,
8347 unsigned char* view,
8348 Arm_address address,
8349 section_size_type view_size,
8350 const Reloc_symbol_changes* reloc_symbol_changes)
8352 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
8353 gold_assert(sh_type == elfcpp::SHT_REL);
8355 // See if we are relocating a relaxed input section. If so, the view
8356 // covers the whole output section and we need to adjust accordingly.
8357 if (needs_special_offset_handling)
8359 const Output_relaxed_input_section* poris =
8360 output_section->find_relaxed_input_section(relinfo->object,
8361 relinfo->data_shndx);
8364 Arm_address section_address = poris->address();
8365 section_size_type section_size = poris->data_size();
8367 gold_assert((section_address >= address)
8368 && ((section_address + section_size)
8369 <= (address + view_size)));
8371 off_t offset = section_address - address;
8374 view_size = section_size;
8378 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
8385 needs_special_offset_handling,
8389 reloc_symbol_changes);
8392 // Return the size of a relocation while scanning during a relocatable
8395 template<bool big_endian>
8397 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
8398 unsigned int r_type,
8401 r_type = get_real_reloc_type(r_type);
8402 const Arm_reloc_property* arp =
8403 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8408 std::string reloc_name =
8409 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8410 gold_error(_("%s: unexpected %s in object file"),
8411 object->name().c_str(), reloc_name.c_str());
8416 // Scan the relocs during a relocatable link.
8418 template<bool big_endian>
8420 Target_arm<big_endian>::scan_relocatable_relocs(
8421 Symbol_table* symtab,
8423 Sized_relobj<32, big_endian>* object,
8424 unsigned int data_shndx,
8425 unsigned int sh_type,
8426 const unsigned char* prelocs,
8428 Output_section* output_section,
8429 bool needs_special_offset_handling,
8430 size_t local_symbol_count,
8431 const unsigned char* plocal_symbols,
8432 Relocatable_relocs* rr)
8434 gold_assert(sh_type == elfcpp::SHT_REL);
8436 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
8437 Relocatable_size_for_reloc> Scan_relocatable_relocs;
8439 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
8440 Scan_relocatable_relocs>(
8448 needs_special_offset_handling,
8454 // Relocate a section during a relocatable link.
8456 template<bool big_endian>
8458 Target_arm<big_endian>::relocate_for_relocatable(
8459 const Relocate_info<32, big_endian>* relinfo,
8460 unsigned int sh_type,
8461 const unsigned char* prelocs,
8463 Output_section* output_section,
8464 off_t offset_in_output_section,
8465 const Relocatable_relocs* rr,
8466 unsigned char* view,
8467 Arm_address view_address,
8468 section_size_type view_size,
8469 unsigned char* reloc_view,
8470 section_size_type reloc_view_size)
8472 gold_assert(sh_type == elfcpp::SHT_REL);
8474 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
8479 offset_in_output_section,
8488 // Return the value to use for a dynamic symbol which requires special
8489 // treatment. This is how we support equality comparisons of function
8490 // pointers across shared library boundaries, as described in the
8491 // processor specific ABI supplement.
8493 template<bool big_endian>
8495 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
8497 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
8498 return this->plt_section()->address() + gsym->plt_offset();
8501 // Map platform-specific relocs to real relocs
8503 template<bool big_endian>
8505 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
8509 case elfcpp::R_ARM_TARGET1:
8510 // This is either R_ARM_ABS32 or R_ARM_REL32;
8511 return elfcpp::R_ARM_ABS32;
8513 case elfcpp::R_ARM_TARGET2:
8514 // This can be any reloc type but ususally is R_ARM_GOT_PREL
8515 return elfcpp::R_ARM_GOT_PREL;
8522 // Whether if two EABI versions V1 and V2 are compatible.
8524 template<bool big_endian>
8526 Target_arm<big_endian>::are_eabi_versions_compatible(
8527 elfcpp::Elf_Word v1,
8528 elfcpp::Elf_Word v2)
8530 // v4 and v5 are the same spec before and after it was released,
8531 // so allow mixing them.
8532 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
8533 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
8539 // Combine FLAGS from an input object called NAME and the processor-specific
8540 // flags in the ELF header of the output. Much of this is adapted from the
8541 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
8542 // in bfd/elf32-arm.c.
8544 template<bool big_endian>
8546 Target_arm<big_endian>::merge_processor_specific_flags(
8547 const std::string& name,
8548 elfcpp::Elf_Word flags)
8550 if (this->are_processor_specific_flags_set())
8552 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
8554 // Nothing to merge if flags equal to those in output.
8555 if (flags == out_flags)
8558 // Complain about various flag mismatches.
8559 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
8560 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
8561 if (!this->are_eabi_versions_compatible(version1, version2))
8562 gold_error(_("Source object %s has EABI version %d but output has "
8563 "EABI version %d."),
8565 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
8566 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
8570 // If the input is the default architecture and had the default
8571 // flags then do not bother setting the flags for the output
8572 // architecture, instead allow future merges to do this. If no
8573 // future merges ever set these flags then they will retain their
8574 // uninitialised values, which surprise surprise, correspond
8575 // to the default values.
8579 // This is the first time, just copy the flags.
8580 // We only copy the EABI version for now.
8581 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
8585 // Adjust ELF file header.
8586 template<bool big_endian>
8588 Target_arm<big_endian>::do_adjust_elf_header(
8589 unsigned char* view,
8592 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
8594 elfcpp::Ehdr<32, big_endian> ehdr(view);
8595 unsigned char e_ident[elfcpp::EI_NIDENT];
8596 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
8598 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
8599 == elfcpp::EF_ARM_EABI_UNKNOWN)
8600 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
8602 e_ident[elfcpp::EI_OSABI] = 0;
8603 e_ident[elfcpp::EI_ABIVERSION] = 0;
8605 // FIXME: Do EF_ARM_BE8 adjustment.
8607 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
8608 oehdr.put_e_ident(e_ident);
8611 // do_make_elf_object to override the same function in the base class.
8612 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
8613 // to store ARM specific information. Hence we need to have our own
8614 // ELF object creation.
8616 template<bool big_endian>
8618 Target_arm<big_endian>::do_make_elf_object(
8619 const std::string& name,
8620 Input_file* input_file,
8621 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
8623 int et = ehdr.get_e_type();
8624 if (et == elfcpp::ET_REL)
8626 Arm_relobj<big_endian>* obj =
8627 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
8631 else if (et == elfcpp::ET_DYN)
8633 Sized_dynobj<32, big_endian>* obj =
8634 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
8640 gold_error(_("%s: unsupported ELF file type %d"),
8646 // Read the architecture from the Tag_also_compatible_with attribute, if any.
8647 // Returns -1 if no architecture could be read.
8648 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
8650 template<bool big_endian>
8652 Target_arm<big_endian>::get_secondary_compatible_arch(
8653 const Attributes_section_data* pasd)
8655 const Object_attribute *known_attributes =
8656 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
8658 // Note: the tag and its argument below are uleb128 values, though
8659 // currently-defined values fit in one byte for each.
8660 const std::string& sv =
8661 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
8663 && sv.data()[0] == elfcpp::Tag_CPU_arch
8664 && (sv.data()[1] & 128) != 128)
8665 return sv.data()[1];
8667 // This tag is "safely ignorable", so don't complain if it looks funny.
8671 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
8672 // The tag is removed if ARCH is -1.
8673 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
8675 template<bool big_endian>
8677 Target_arm<big_endian>::set_secondary_compatible_arch(
8678 Attributes_section_data* pasd,
8681 Object_attribute *known_attributes =
8682 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
8686 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
8690 // Note: the tag and its argument below are uleb128 values, though
8691 // currently-defined values fit in one byte for each.
8693 sv[0] = elfcpp::Tag_CPU_arch;
8694 gold_assert(arch != 0);
8698 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
8701 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
8703 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
8705 template<bool big_endian>
8707 Target_arm<big_endian>::tag_cpu_arch_combine(
8710 int* secondary_compat_out,
8712 int secondary_compat)
8714 #define T(X) elfcpp::TAG_CPU_ARCH_##X
8715 static const int v6t2[] =
8727 static const int v6k[] =
8740 static const int v7[] =
8754 static const int v6_m[] =
8769 static const int v6s_m[] =
8785 static const int v7e_m[] =
8802 static const int v4t_plus_v6_m[] =
8818 T(V4T_PLUS_V6_M) // V4T plus V6_M.
8820 static const int *comb[] =
8828 // Pseudo-architecture.
8832 // Check we've not got a higher architecture than we know about.
8834 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
8836 gold_error(_("%s: unknown CPU architecture"), name);
8840 // Override old tag if we have a Tag_also_compatible_with on the output.
8842 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
8843 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
8844 oldtag = T(V4T_PLUS_V6_M);
8846 // And override the new tag if we have a Tag_also_compatible_with on the
8849 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
8850 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
8851 newtag = T(V4T_PLUS_V6_M);
8853 // Architectures before V6KZ add features monotonically.
8854 int tagh = std::max(oldtag, newtag);
8855 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
8858 int tagl = std::min(oldtag, newtag);
8859 int result = comb[tagh - T(V6T2)][tagl];
8861 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
8862 // as the canonical version.
8863 if (result == T(V4T_PLUS_V6_M))
8866 *secondary_compat_out = T(V6_M);
8869 *secondary_compat_out = -1;
8873 gold_error(_("%s: conflicting CPU architectures %d/%d"),
8874 name, oldtag, newtag);
8882 // Helper to print AEABI enum tag value.
8884 template<bool big_endian>
8886 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
8888 static const char *aeabi_enum_names[] =
8889 { "", "variable-size", "32-bit", "" };
8890 const size_t aeabi_enum_names_size =
8891 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
8893 if (value < aeabi_enum_names_size)
8894 return std::string(aeabi_enum_names[value]);
8898 sprintf(buffer, "<unknown value %u>", value);
8899 return std::string(buffer);
8903 // Return the string value to store in TAG_CPU_name.
8905 template<bool big_endian>
8907 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
8909 static const char *name_table[] = {
8910 // These aren't real CPU names, but we can't guess
8911 // that from the architecture version alone.
8927 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
8929 if (value < name_table_size)
8930 return std::string(name_table[value]);
8934 sprintf(buffer, "<unknown CPU value %u>", value);
8935 return std::string(buffer);
8939 // Merge object attributes from input file called NAME with those of the
8940 // output. The input object attributes are in the object pointed by PASD.
8942 template<bool big_endian>
8944 Target_arm<big_endian>::merge_object_attributes(
8946 const Attributes_section_data* pasd)
8948 // Return if there is no attributes section data.
8952 // If output has no object attributes, just copy.
8953 if (this->attributes_section_data_ == NULL)
8955 this->attributes_section_data_ = new Attributes_section_data(*pasd);
8959 const int vendor = Object_attribute::OBJ_ATTR_PROC;
8960 const Object_attribute* in_attr = pasd->known_attributes(vendor);
8961 Object_attribute* out_attr =
8962 this->attributes_section_data_->known_attributes(vendor);
8964 // This needs to happen before Tag_ABI_FP_number_model is merged. */
8965 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
8966 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
8968 // Ignore mismatches if the object doesn't use floating point. */
8969 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
8970 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
8971 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
8972 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
8973 gold_error(_("%s uses VFP register arguments, output does not"),
8977 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
8979 // Merge this attribute with existing attributes.
8982 case elfcpp::Tag_CPU_raw_name:
8983 case elfcpp::Tag_CPU_name:
8984 // These are merged after Tag_CPU_arch.
8987 case elfcpp::Tag_ABI_optimization_goals:
8988 case elfcpp::Tag_ABI_FP_optimization_goals:
8989 // Use the first value seen.
8992 case elfcpp::Tag_CPU_arch:
8994 unsigned int saved_out_attr = out_attr->int_value();
8995 // Merge Tag_CPU_arch and Tag_also_compatible_with.
8996 int secondary_compat =
8997 this->get_secondary_compatible_arch(pasd);
8998 int secondary_compat_out =
8999 this->get_secondary_compatible_arch(
9000 this->attributes_section_data_);
9001 out_attr[i].set_int_value(
9002 tag_cpu_arch_combine(name, out_attr[i].int_value(),
9003 &secondary_compat_out,
9004 in_attr[i].int_value(),
9006 this->set_secondary_compatible_arch(this->attributes_section_data_,
9007 secondary_compat_out);
9009 // Merge Tag_CPU_name and Tag_CPU_raw_name.
9010 if (out_attr[i].int_value() == saved_out_attr)
9011 ; // Leave the names alone.
9012 else if (out_attr[i].int_value() == in_attr[i].int_value())
9014 // The output architecture has been changed to match the
9015 // input architecture. Use the input names.
9016 out_attr[elfcpp::Tag_CPU_name].set_string_value(
9017 in_attr[elfcpp::Tag_CPU_name].string_value());
9018 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
9019 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
9023 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
9024 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
9027 // If we still don't have a value for Tag_CPU_name,
9028 // make one up now. Tag_CPU_raw_name remains blank.
9029 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
9031 const std::string cpu_name =
9032 this->tag_cpu_name_value(out_attr[i].int_value());
9033 // FIXME: If we see an unknown CPU, this will be set
9034 // to "<unknown CPU n>", where n is the attribute value.
9035 // This is different from BFD, which leaves the name alone.
9036 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
9041 case elfcpp::Tag_ARM_ISA_use:
9042 case elfcpp::Tag_THUMB_ISA_use:
9043 case elfcpp::Tag_WMMX_arch:
9044 case elfcpp::Tag_Advanced_SIMD_arch:
9045 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
9046 case elfcpp::Tag_ABI_FP_rounding:
9047 case elfcpp::Tag_ABI_FP_exceptions:
9048 case elfcpp::Tag_ABI_FP_user_exceptions:
9049 case elfcpp::Tag_ABI_FP_number_model:
9050 case elfcpp::Tag_VFP_HP_extension:
9051 case elfcpp::Tag_CPU_unaligned_access:
9052 case elfcpp::Tag_T2EE_use:
9053 case elfcpp::Tag_Virtualization_use:
9054 case elfcpp::Tag_MPextension_use:
9055 // Use the largest value specified.
9056 if (in_attr[i].int_value() > out_attr[i].int_value())
9057 out_attr[i].set_int_value(in_attr[i].int_value());
9060 case elfcpp::Tag_ABI_align8_preserved:
9061 case elfcpp::Tag_ABI_PCS_RO_data:
9062 // Use the smallest value specified.
9063 if (in_attr[i].int_value() < out_attr[i].int_value())
9064 out_attr[i].set_int_value(in_attr[i].int_value());
9067 case elfcpp::Tag_ABI_align8_needed:
9068 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
9069 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
9070 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
9073 // This error message should be enabled once all non-conformant
9074 // binaries in the toolchain have had the attributes set
9076 // gold_error(_("output 8-byte data alignment conflicts with %s"),
9080 case elfcpp::Tag_ABI_FP_denormal:
9081 case elfcpp::Tag_ABI_PCS_GOT_use:
9083 // These tags have 0 = don't care, 1 = strong requirement,
9084 // 2 = weak requirement.
9085 static const int order_021[3] = {0, 2, 1};
9087 // Use the "greatest" from the sequence 0, 2, 1, or the largest
9088 // value if greater than 2 (for future-proofing).
9089 if ((in_attr[i].int_value() > 2
9090 && in_attr[i].int_value() > out_attr[i].int_value())
9091 || (in_attr[i].int_value() <= 2
9092 && out_attr[i].int_value() <= 2
9093 && (order_021[in_attr[i].int_value()]
9094 > order_021[out_attr[i].int_value()])))
9095 out_attr[i].set_int_value(in_attr[i].int_value());
9099 case elfcpp::Tag_CPU_arch_profile:
9100 if (out_attr[i].int_value() != in_attr[i].int_value())
9102 // 0 will merge with anything.
9103 // 'A' and 'S' merge to 'A'.
9104 // 'R' and 'S' merge to 'R'.
9105 // 'M' and 'A|R|S' is an error.
9106 if (out_attr[i].int_value() == 0
9107 || (out_attr[i].int_value() == 'S'
9108 && (in_attr[i].int_value() == 'A'
9109 || in_attr[i].int_value() == 'R')))
9110 out_attr[i].set_int_value(in_attr[i].int_value());
9111 else if (in_attr[i].int_value() == 0
9112 || (in_attr[i].int_value() == 'S'
9113 && (out_attr[i].int_value() == 'A'
9114 || out_attr[i].int_value() == 'R')))
9119 (_("conflicting architecture profiles %c/%c"),
9120 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
9121 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
9125 case elfcpp::Tag_VFP_arch:
9142 // Values greater than 6 aren't defined, so just pick the
9144 if (in_attr[i].int_value() > 6
9145 && in_attr[i].int_value() > out_attr[i].int_value())
9147 *out_attr = *in_attr;
9150 // The output uses the superset of input features
9151 // (ISA version) and registers.
9152 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
9153 vfp_versions[out_attr[i].int_value()].ver);
9154 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
9155 vfp_versions[out_attr[i].int_value()].regs);
9156 // This assumes all possible supersets are also a valid
9159 for (newval = 6; newval > 0; newval--)
9161 if (regs == vfp_versions[newval].regs
9162 && ver == vfp_versions[newval].ver)
9165 out_attr[i].set_int_value(newval);
9168 case elfcpp::Tag_PCS_config:
9169 if (out_attr[i].int_value() == 0)
9170 out_attr[i].set_int_value(in_attr[i].int_value());
9171 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
9173 // It's sometimes ok to mix different configs, so this is only
9175 gold_warning(_("%s: conflicting platform configuration"), name);
9178 case elfcpp::Tag_ABI_PCS_R9_use:
9179 if (in_attr[i].int_value() != out_attr[i].int_value()
9180 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
9181 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
9183 gold_error(_("%s: conflicting use of R9"), name);
9185 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
9186 out_attr[i].set_int_value(in_attr[i].int_value());
9188 case elfcpp::Tag_ABI_PCS_RW_data:
9189 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
9190 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9191 != elfcpp::AEABI_R9_SB)
9192 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9193 != elfcpp::AEABI_R9_unused))
9195 gold_error(_("%s: SB relative addressing conflicts with use "
9199 // Use the smallest value specified.
9200 if (in_attr[i].int_value() < out_attr[i].int_value())
9201 out_attr[i].set_int_value(in_attr[i].int_value());
9203 case elfcpp::Tag_ABI_PCS_wchar_t:
9204 // FIXME: Make it possible to turn off this warning.
9205 if (out_attr[i].int_value()
9206 && in_attr[i].int_value()
9207 && out_attr[i].int_value() != in_attr[i].int_value())
9209 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
9210 "use %u-byte wchar_t; use of wchar_t values "
9211 "across objects may fail"),
9212 name, in_attr[i].int_value(),
9213 out_attr[i].int_value());
9215 else if (in_attr[i].int_value() && !out_attr[i].int_value())
9216 out_attr[i].set_int_value(in_attr[i].int_value());
9218 case elfcpp::Tag_ABI_enum_size:
9219 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
9221 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
9222 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
9224 // The existing object is compatible with anything.
9225 // Use whatever requirements the new object has.
9226 out_attr[i].set_int_value(in_attr[i].int_value());
9228 // FIXME: Make it possible to turn off this warning.
9229 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
9230 && out_attr[i].int_value() != in_attr[i].int_value())
9232 unsigned int in_value = in_attr[i].int_value();
9233 unsigned int out_value = out_attr[i].int_value();
9234 gold_warning(_("%s uses %s enums yet the output is to use "
9235 "%s enums; use of enum values across objects "
9238 this->aeabi_enum_name(in_value).c_str(),
9239 this->aeabi_enum_name(out_value).c_str());
9243 case elfcpp::Tag_ABI_VFP_args:
9246 case elfcpp::Tag_ABI_WMMX_args:
9247 if (in_attr[i].int_value() != out_attr[i].int_value())
9249 gold_error(_("%s uses iWMMXt register arguments, output does "
9254 case Object_attribute::Tag_compatibility:
9255 // Merged in target-independent code.
9257 case elfcpp::Tag_ABI_HardFP_use:
9258 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
9259 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
9260 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
9261 out_attr[i].set_int_value(3);
9262 else if (in_attr[i].int_value() > out_attr[i].int_value())
9263 out_attr[i].set_int_value(in_attr[i].int_value());
9265 case elfcpp::Tag_ABI_FP_16bit_format:
9266 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
9268 if (in_attr[i].int_value() != out_attr[i].int_value())
9269 gold_error(_("fp16 format mismatch between %s and output"),
9272 if (in_attr[i].int_value() != 0)
9273 out_attr[i].set_int_value(in_attr[i].int_value());
9276 case elfcpp::Tag_nodefaults:
9277 // This tag is set if it exists, but the value is unused (and is
9278 // typically zero). We don't actually need to do anything here -
9279 // the merge happens automatically when the type flags are merged
9282 case elfcpp::Tag_also_compatible_with:
9283 // Already done in Tag_CPU_arch.
9285 case elfcpp::Tag_conformance:
9286 // Keep the attribute if it matches. Throw it away otherwise.
9287 // No attribute means no claim to conform.
9288 if (in_attr[i].string_value() != out_attr[i].string_value())
9289 out_attr[i].set_string_value("");
9294 const char* err_object = NULL;
9296 // The "known_obj_attributes" table does contain some undefined
9297 // attributes. Ensure that there are unused.
9298 if (out_attr[i].int_value() != 0
9299 || out_attr[i].string_value() != "")
9300 err_object = "output";
9301 else if (in_attr[i].int_value() != 0
9302 || in_attr[i].string_value() != "")
9305 if (err_object != NULL)
9307 // Attribute numbers >=64 (mod 128) can be safely ignored.
9309 gold_error(_("%s: unknown mandatory EABI object attribute "
9313 gold_warning(_("%s: unknown EABI object attribute %d"),
9317 // Only pass on attributes that match in both inputs.
9318 if (!in_attr[i].matches(out_attr[i]))
9320 out_attr[i].set_int_value(0);
9321 out_attr[i].set_string_value("");
9326 // If out_attr was copied from in_attr then it won't have a type yet.
9327 if (in_attr[i].type() && !out_attr[i].type())
9328 out_attr[i].set_type(in_attr[i].type());
9331 // Merge Tag_compatibility attributes and any common GNU ones.
9332 this->attributes_section_data_->merge(name, pasd);
9334 // Check for any attributes not known on ARM.
9335 typedef Vendor_object_attributes::Other_attributes Other_attributes;
9336 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
9337 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
9338 Other_attributes* out_other_attributes =
9339 this->attributes_section_data_->other_attributes(vendor);
9340 Other_attributes::iterator out_iter = out_other_attributes->begin();
9342 while (in_iter != in_other_attributes->end()
9343 || out_iter != out_other_attributes->end())
9345 const char* err_object = NULL;
9348 // The tags for each list are in numerical order.
9349 // If the tags are equal, then merge.
9350 if (out_iter != out_other_attributes->end()
9351 && (in_iter == in_other_attributes->end()
9352 || in_iter->first > out_iter->first))
9354 // This attribute only exists in output. We can't merge, and we
9355 // don't know what the tag means, so delete it.
9356 err_object = "output";
9357 err_tag = out_iter->first;
9358 int saved_tag = out_iter->first;
9359 delete out_iter->second;
9360 out_other_attributes->erase(out_iter);
9361 out_iter = out_other_attributes->upper_bound(saved_tag);
9363 else if (in_iter != in_other_attributes->end()
9364 && (out_iter != out_other_attributes->end()
9365 || in_iter->first < out_iter->first))
9367 // This attribute only exists in input. We can't merge, and we
9368 // don't know what the tag means, so ignore it.
9370 err_tag = in_iter->first;
9373 else // The tags are equal.
9375 // As present, all attributes in the list are unknown, and
9376 // therefore can't be merged meaningfully.
9377 err_object = "output";
9378 err_tag = out_iter->first;
9380 // Only pass on attributes that match in both inputs.
9381 if (!in_iter->second->matches(*(out_iter->second)))
9383 // No match. Delete the attribute.
9384 int saved_tag = out_iter->first;
9385 delete out_iter->second;
9386 out_other_attributes->erase(out_iter);
9387 out_iter = out_other_attributes->upper_bound(saved_tag);
9391 // Matched. Keep the attribute and move to the next.
9399 // Attribute numbers >=64 (mod 128) can be safely ignored. */
9400 if ((err_tag & 127) < 64)
9402 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
9403 err_object, err_tag);
9407 gold_warning(_("%s: unknown EABI object attribute %d"),
9408 err_object, err_tag);
9414 // Stub-generation methods for Target_arm.
9416 // Make a new Arm_input_section object.
9418 template<bool big_endian>
9419 Arm_input_section<big_endian>*
9420 Target_arm<big_endian>::new_arm_input_section(
9424 Section_id sid(relobj, shndx);
9426 Arm_input_section<big_endian>* arm_input_section =
9427 new Arm_input_section<big_endian>(relobj, shndx);
9428 arm_input_section->init();
9430 // Register new Arm_input_section in map for look-up.
9431 std::pair<typename Arm_input_section_map::iterator, bool> ins =
9432 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
9434 // Make sure that it we have not created another Arm_input_section
9435 // for this input section already.
9436 gold_assert(ins.second);
9438 return arm_input_section;
9441 // Find the Arm_input_section object corresponding to the SHNDX-th input
9442 // section of RELOBJ.
9444 template<bool big_endian>
9445 Arm_input_section<big_endian>*
9446 Target_arm<big_endian>::find_arm_input_section(
9448 unsigned int shndx) const
9450 Section_id sid(relobj, shndx);
9451 typename Arm_input_section_map::const_iterator p =
9452 this->arm_input_section_map_.find(sid);
9453 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
9456 // Make a new stub table.
9458 template<bool big_endian>
9459 Stub_table<big_endian>*
9460 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
9462 Stub_table<big_endian>* stub_table =
9463 new Stub_table<big_endian>(owner);
9464 this->stub_tables_.push_back(stub_table);
9466 stub_table->set_address(owner->address() + owner->data_size());
9467 stub_table->set_file_offset(owner->offset() + owner->data_size());
9468 stub_table->finalize_data_size();
9473 // Scan a relocation for stub generation.
9475 template<bool big_endian>
9477 Target_arm<big_endian>::scan_reloc_for_stub(
9478 const Relocate_info<32, big_endian>* relinfo,
9479 unsigned int r_type,
9480 const Sized_symbol<32>* gsym,
9482 const Symbol_value<32>* psymval,
9483 elfcpp::Elf_types<32>::Elf_Swxword addend,
9484 Arm_address address)
9486 typedef typename Target_arm<big_endian>::Relocate Relocate;
9488 const Arm_relobj<big_endian>* arm_relobj =
9489 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
9491 if (r_type == elfcpp::R_ARM_V4BX)
9493 const uint32_t reg = (addend & 0xf);
9494 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
9497 // Try looking up an existing stub from a stub table.
9498 Stub_table<big_endian>* stub_table =
9499 arm_relobj->stub_table(relinfo->data_shndx);
9500 gold_assert(stub_table != NULL);
9502 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
9504 // create a new stub and add it to stub table.
9505 Arm_v4bx_stub* stub =
9506 this->stub_factory().make_arm_v4bx_stub(reg);
9507 gold_assert(stub != NULL);
9508 stub_table->add_arm_v4bx_stub(stub);
9515 bool target_is_thumb;
9516 Symbol_value<32> symval;
9519 // This is a global symbol. Determine if we use PLT and if the
9520 // final target is THUMB.
9521 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
9523 // This uses a PLT, change the symbol value.
9524 symval.set_output_value(this->plt_section()->address()
9525 + gsym->plt_offset());
9527 target_is_thumb = false;
9529 else if (gsym->is_undefined())
9530 // There is no need to generate a stub symbol is undefined.
9535 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
9536 || (gsym->type() == elfcpp::STT_FUNC
9537 && !gsym->is_undefined()
9538 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
9543 // This is a local symbol. Determine if the final target is THUMB.
9544 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
9547 // Strip LSB if this points to a THUMB target.
9548 const Arm_reloc_property* reloc_property =
9549 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
9550 gold_assert(reloc_property != NULL);
9552 && reloc_property->uses_thumb_bit()
9553 && ((psymval->value(arm_relobj, 0) & 1) != 0))
9555 Arm_address stripped_value =
9556 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
9557 symval.set_output_value(stripped_value);
9561 // Get the symbol value.
9562 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
9564 // Owing to pipelining, the PC relative branches below actually skip
9565 // two instructions when the branch offset is 0.
9566 Arm_address destination;
9569 case elfcpp::R_ARM_CALL:
9570 case elfcpp::R_ARM_JUMP24:
9571 case elfcpp::R_ARM_PLT32:
9573 destination = value + addend + 8;
9575 case elfcpp::R_ARM_THM_CALL:
9576 case elfcpp::R_ARM_THM_XPC22:
9577 case elfcpp::R_ARM_THM_JUMP24:
9578 case elfcpp::R_ARM_THM_JUMP19:
9580 destination = value + addend + 4;
9586 Reloc_stub* stub = NULL;
9587 Stub_type stub_type =
9588 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
9590 if (stub_type != arm_stub_none)
9592 // Try looking up an existing stub from a stub table.
9593 Stub_table<big_endian>* stub_table =
9594 arm_relobj->stub_table(relinfo->data_shndx);
9595 gold_assert(stub_table != NULL);
9597 // Locate stub by destination.
9598 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
9600 // Create a stub if there is not one already
9601 stub = stub_table->find_reloc_stub(stub_key);
9604 // create a new stub and add it to stub table.
9605 stub = this->stub_factory().make_reloc_stub(stub_type);
9606 stub_table->add_reloc_stub(stub, stub_key);
9609 // Record the destination address.
9610 stub->set_destination_address(destination
9611 | (target_is_thumb ? 1 : 0));
9614 // For Cortex-A8, we need to record a relocation at 4K page boundary.
9615 if (this->fix_cortex_a8_
9616 && (r_type == elfcpp::R_ARM_THM_JUMP24
9617 || r_type == elfcpp::R_ARM_THM_JUMP19
9618 || r_type == elfcpp::R_ARM_THM_CALL
9619 || r_type == elfcpp::R_ARM_THM_XPC22)
9620 && (address & 0xfffU) == 0xffeU)
9622 // Found a candidate. Note we haven't checked the destination is
9623 // within 4K here: if we do so (and don't create a record) we can't
9624 // tell that a branch should have been relocated when scanning later.
9625 this->cortex_a8_relocs_info_[address] =
9626 new Cortex_a8_reloc(stub, r_type,
9627 destination | (target_is_thumb ? 1 : 0));
9631 // This function scans a relocation sections for stub generation.
9632 // The template parameter Relocate must be a class type which provides
9633 // a single function, relocate(), which implements the machine
9634 // specific part of a relocation.
9636 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
9637 // SHT_REL or SHT_RELA.
9639 // PRELOCS points to the relocation data. RELOC_COUNT is the number
9640 // of relocs. OUTPUT_SECTION is the output section.
9641 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
9642 // mapped to output offsets.
9644 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
9645 // VIEW_SIZE is the size. These refer to the input section, unless
9646 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
9647 // the output section.
9649 template<bool big_endian>
9650 template<int sh_type>
9652 Target_arm<big_endian>::scan_reloc_section_for_stubs(
9653 const Relocate_info<32, big_endian>* relinfo,
9654 const unsigned char* prelocs,
9656 Output_section* output_section,
9657 bool needs_special_offset_handling,
9658 const unsigned char* view,
9659 elfcpp::Elf_types<32>::Elf_Addr view_address,
9662 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
9663 const int reloc_size =
9664 Reloc_types<sh_type, 32, big_endian>::reloc_size;
9666 Arm_relobj<big_endian>* arm_object =
9667 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
9668 unsigned int local_count = arm_object->local_symbol_count();
9670 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
9672 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
9674 Reltype reloc(prelocs);
9676 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
9677 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
9678 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
9680 r_type = this->get_real_reloc_type(r_type);
9682 // Only a few relocation types need stubs.
9683 if ((r_type != elfcpp::R_ARM_CALL)
9684 && (r_type != elfcpp::R_ARM_JUMP24)
9685 && (r_type != elfcpp::R_ARM_PLT32)
9686 && (r_type != elfcpp::R_ARM_THM_CALL)
9687 && (r_type != elfcpp::R_ARM_THM_XPC22)
9688 && (r_type != elfcpp::R_ARM_THM_JUMP24)
9689 && (r_type != elfcpp::R_ARM_THM_JUMP19)
9690 && (r_type != elfcpp::R_ARM_V4BX))
9693 section_offset_type offset =
9694 convert_to_section_size_type(reloc.get_r_offset());
9696 if (needs_special_offset_handling)
9698 offset = output_section->output_offset(relinfo->object,
9699 relinfo->data_shndx,
9705 if (r_type == elfcpp::R_ARM_V4BX)
9707 // Get the BX instruction.
9708 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
9709 const Valtype* wv = reinterpret_cast<const Valtype*>(view + offset);
9710 elfcpp::Elf_types<32>::Elf_Swxword insn =
9711 elfcpp::Swap<32, big_endian>::readval(wv);
9712 this->scan_reloc_for_stub(relinfo, r_type, NULL, 0, NULL,
9718 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
9719 elfcpp::Elf_types<32>::Elf_Swxword addend =
9720 stub_addend_reader(r_type, view + offset, reloc);
9722 const Sized_symbol<32>* sym;
9724 Symbol_value<32> symval;
9725 const Symbol_value<32> *psymval;
9726 if (r_sym < local_count)
9729 psymval = arm_object->local_symbol(r_sym);
9731 // If the local symbol belongs to a section we are discarding,
9732 // and that section is a debug section, try to find the
9733 // corresponding kept section and map this symbol to its
9734 // counterpart in the kept section. The symbol must not
9735 // correspond to a section we are folding.
9737 unsigned int shndx = psymval->input_shndx(&is_ordinary);
9739 && shndx != elfcpp::SHN_UNDEF
9740 && !arm_object->is_section_included(shndx)
9741 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
9743 if (comdat_behavior == CB_UNDETERMINED)
9746 arm_object->section_name(relinfo->data_shndx);
9747 comdat_behavior = get_comdat_behavior(name.c_str());
9749 if (comdat_behavior == CB_PRETEND)
9752 typename elfcpp::Elf_types<32>::Elf_Addr value =
9753 arm_object->map_to_kept_section(shndx, &found);
9755 symval.set_output_value(value + psymval->input_value());
9757 symval.set_output_value(0);
9761 symval.set_output_value(0);
9763 symval.set_no_output_symtab_entry();
9769 const Symbol* gsym = arm_object->global_symbol(r_sym);
9770 gold_assert(gsym != NULL);
9771 if (gsym->is_forwarder())
9772 gsym = relinfo->symtab->resolve_forwards(gsym);
9774 sym = static_cast<const Sized_symbol<32>*>(gsym);
9775 if (sym->has_symtab_index())
9776 symval.set_output_symtab_index(sym->symtab_index());
9778 symval.set_no_output_symtab_entry();
9780 // We need to compute the would-be final value of this global
9782 const Symbol_table* symtab = relinfo->symtab;
9783 const Sized_symbol<32>* sized_symbol =
9784 symtab->get_sized_symbol<32>(gsym);
9785 Symbol_table::Compute_final_value_status status;
9787 symtab->compute_final_value<32>(sized_symbol, &status);
9789 // Skip this if the symbol has not output section.
9790 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
9793 symval.set_output_value(value);
9797 // If symbol is a section symbol, we don't know the actual type of
9798 // destination. Give up.
9799 if (psymval->is_section_symbol())
9802 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
9803 addend, view_address + offset);
9807 // Scan an input section for stub generation.
9809 template<bool big_endian>
9811 Target_arm<big_endian>::scan_section_for_stubs(
9812 const Relocate_info<32, big_endian>* relinfo,
9813 unsigned int sh_type,
9814 const unsigned char* prelocs,
9816 Output_section* output_section,
9817 bool needs_special_offset_handling,
9818 const unsigned char* view,
9819 Arm_address view_address,
9820 section_size_type view_size)
9822 if (sh_type == elfcpp::SHT_REL)
9823 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
9828 needs_special_offset_handling,
9832 else if (sh_type == elfcpp::SHT_RELA)
9833 // We do not support RELA type relocations yet. This is provided for
9835 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
9840 needs_special_offset_handling,
9848 // Group input sections for stub generation.
9850 // We goup input sections in an output sections so that the total size,
9851 // including any padding space due to alignment is smaller than GROUP_SIZE
9852 // unless the only input section in group is bigger than GROUP_SIZE already.
9853 // Then an ARM stub table is created to follow the last input section
9854 // in group. For each group an ARM stub table is created an is placed
9855 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
9856 // extend the group after the stub table.
9858 template<bool big_endian>
9860 Target_arm<big_endian>::group_sections(
9862 section_size_type group_size,
9863 bool stubs_always_after_branch)
9865 // Group input sections and insert stub table
9866 Layout::Section_list section_list;
9867 layout->get_allocated_sections(§ion_list);
9868 for (Layout::Section_list::const_iterator p = section_list.begin();
9869 p != section_list.end();
9872 Arm_output_section<big_endian>* output_section =
9873 Arm_output_section<big_endian>::as_arm_output_section(*p);
9874 output_section->group_sections(group_size, stubs_always_after_branch,
9879 // Relaxation hook. This is where we do stub generation.
9881 template<bool big_endian>
9883 Target_arm<big_endian>::do_relax(
9885 const Input_objects* input_objects,
9886 Symbol_table* symtab,
9889 // No need to generate stubs if this is a relocatable link.
9890 gold_assert(!parameters->options().relocatable());
9892 // If this is the first pass, we need to group input sections into
9894 bool done_exidx_fixup = false;
9897 // Determine the stub group size. The group size is the absolute
9898 // value of the parameter --stub-group-size. If --stub-group-size
9899 // is passed a negative value, we restict stubs to be always after
9900 // the stubbed branches.
9901 int32_t stub_group_size_param =
9902 parameters->options().stub_group_size();
9903 bool stubs_always_after_branch = stub_group_size_param < 0;
9904 section_size_type stub_group_size = abs(stub_group_size_param);
9906 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
9907 // page as the first half of a 32-bit branch straddling two 4K pages.
9908 // This is a crude way of enforcing that.
9909 if (this->fix_cortex_a8_)
9910 stubs_always_after_branch = true;
9912 if (stub_group_size == 1)
9915 // Thumb branch range is +-4MB has to be used as the default
9916 // maximum size (a given section can contain both ARM and Thumb
9917 // code, so the worst case has to be taken into account).
9919 // This value is 24K less than that, which allows for 2025
9920 // 12-byte stubs. If we exceed that, then we will fail to link.
9921 // The user will have to relink with an explicit group size
9923 stub_group_size = 4170000;
9926 group_sections(layout, stub_group_size, stubs_always_after_branch);
9928 // Also fix .ARM.exidx section coverage.
9929 Output_section* os = layout->find_output_section(".ARM.exidx");
9930 if (os != NULL && os->type() == elfcpp::SHT_ARM_EXIDX)
9932 Arm_output_section<big_endian>* exidx_output_section =
9933 Arm_output_section<big_endian>::as_arm_output_section(os);
9934 this->fix_exidx_coverage(layout, exidx_output_section, symtab);
9935 done_exidx_fixup = true;
9939 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
9940 // beginning of each relaxation pass, just blow away all the stubs.
9941 // Alternatively, we could selectively remove only the stubs and reloc
9942 // information for code sections that have moved since the last pass.
9943 // That would require more book-keeping.
9944 typedef typename Stub_table_list::iterator Stub_table_iterator;
9945 if (this->fix_cortex_a8_)
9947 // Clear all Cortex-A8 reloc information.
9948 for (typename Cortex_a8_relocs_info::const_iterator p =
9949 this->cortex_a8_relocs_info_.begin();
9950 p != this->cortex_a8_relocs_info_.end();
9953 this->cortex_a8_relocs_info_.clear();
9955 // Remove all Cortex-A8 stubs.
9956 for (Stub_table_iterator sp = this->stub_tables_.begin();
9957 sp != this->stub_tables_.end();
9959 (*sp)->remove_all_cortex_a8_stubs();
9962 // Scan relocs for relocation stubs
9963 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
9964 op != input_objects->relobj_end();
9967 Arm_relobj<big_endian>* arm_relobj =
9968 Arm_relobj<big_endian>::as_arm_relobj(*op);
9969 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
9972 // Check all stub tables to see if any of them have their data sizes
9973 // or addresses alignments changed. These are the only things that
9975 bool any_stub_table_changed = false;
9976 Unordered_set<const Output_section*> sections_needing_adjustment;
9977 for (Stub_table_iterator sp = this->stub_tables_.begin();
9978 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
9981 if ((*sp)->update_data_size_and_addralign())
9983 // Update data size of stub table owner.
9984 Arm_input_section<big_endian>* owner = (*sp)->owner();
9985 uint64_t address = owner->address();
9986 off_t offset = owner->offset();
9987 owner->reset_address_and_file_offset();
9988 owner->set_address_and_file_offset(address, offset);
9990 sections_needing_adjustment.insert(owner->output_section());
9991 any_stub_table_changed = true;
9995 // Output_section_data::output_section() returns a const pointer but we
9996 // need to update output sections, so we record all output sections needing
9997 // update above and scan the sections here to find out what sections need
9999 for(Layout::Section_list::const_iterator p = layout->section_list().begin();
10000 p != layout->section_list().end();
10003 if (sections_needing_adjustment.find(*p)
10004 != sections_needing_adjustment.end())
10005 (*p)->set_section_offsets_need_adjustment();
10008 // Stop relaxation if no EXIDX fix-up and no stub table change.
10009 bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
10011 // Finalize the stubs in the last relaxation pass.
10012 if (!continue_relaxation)
10014 for (Stub_table_iterator sp = this->stub_tables_.begin();
10015 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
10017 (*sp)->finalize_stubs();
10019 // Update output local symbol counts of objects if necessary.
10020 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
10021 op != input_objects->relobj_end();
10024 Arm_relobj<big_endian>* arm_relobj =
10025 Arm_relobj<big_endian>::as_arm_relobj(*op);
10027 // Update output local symbol counts. We need to discard local
10028 // symbols defined in parts of input sections that are discarded by
10030 if (arm_relobj->output_local_symbol_count_needs_update())
10031 arm_relobj->update_output_local_symbol_count();
10035 return continue_relaxation;
10038 // Relocate a stub.
10040 template<bool big_endian>
10042 Target_arm<big_endian>::relocate_stub(
10044 const Relocate_info<32, big_endian>* relinfo,
10045 Output_section* output_section,
10046 unsigned char* view,
10047 Arm_address address,
10048 section_size_type view_size)
10051 const Stub_template* stub_template = stub->stub_template();
10052 for (size_t i = 0; i < stub_template->reloc_count(); i++)
10054 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
10055 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
10057 unsigned int r_type = insn->r_type();
10058 section_size_type reloc_offset = stub_template->reloc_offset(i);
10059 section_size_type reloc_size = insn->size();
10060 gold_assert(reloc_offset + reloc_size <= view_size);
10062 // This is the address of the stub destination.
10063 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
10064 Symbol_value<32> symval;
10065 symval.set_output_value(target);
10067 // Synthesize a fake reloc just in case. We don't have a symbol so
10069 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
10070 memset(reloc_buffer, 0, sizeof(reloc_buffer));
10071 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
10072 reloc_write.put_r_offset(reloc_offset);
10073 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
10074 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
10076 relocate.relocate(relinfo, this, output_section,
10077 this->fake_relnum_for_stubs, rel, r_type,
10078 NULL, &symval, view + reloc_offset,
10079 address + reloc_offset, reloc_size);
10083 // Determine whether an object attribute tag takes an integer, a
10086 template<bool big_endian>
10088 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
10090 if (tag == Object_attribute::Tag_compatibility)
10091 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10092 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
10093 else if (tag == elfcpp::Tag_nodefaults)
10094 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10095 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
10096 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
10097 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
10099 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
10101 return ((tag & 1) != 0
10102 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
10103 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10106 // Reorder attributes.
10108 // The ABI defines that Tag_conformance should be emitted first, and that
10109 // Tag_nodefaults should be second (if either is defined). This sets those
10110 // two positions, and bumps up the position of all the remaining tags to
10113 template<bool big_endian>
10115 Target_arm<big_endian>::do_attributes_order(int num) const
10117 // Reorder the known object attributes in output. We want to move
10118 // Tag_conformance to position 4 and Tag_conformance to position 5
10119 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
10121 return elfcpp::Tag_conformance;
10123 return elfcpp::Tag_nodefaults;
10124 if ((num - 2) < elfcpp::Tag_nodefaults)
10126 if ((num - 1) < elfcpp::Tag_conformance)
10131 // Scan a span of THUMB code for Cortex-A8 erratum.
10133 template<bool big_endian>
10135 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
10136 Arm_relobj<big_endian>* arm_relobj,
10137 unsigned int shndx,
10138 section_size_type span_start,
10139 section_size_type span_end,
10140 const unsigned char* view,
10141 Arm_address address)
10143 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
10145 // The opcode is BLX.W, BL.W, B.W, Bcc.W
10146 // The branch target is in the same 4KB region as the
10147 // first half of the branch.
10148 // The instruction before the branch is a 32-bit
10149 // length non-branch instruction.
10150 section_size_type i = span_start;
10151 bool last_was_32bit = false;
10152 bool last_was_branch = false;
10153 while (i < span_end)
10155 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10156 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
10157 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
10158 bool is_blx = false, is_b = false;
10159 bool is_bl = false, is_bcc = false;
10161 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
10164 // Load the rest of the insn (in manual-friendly order).
10165 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
10167 // Encoding T4: B<c>.W.
10168 is_b = (insn & 0xf800d000U) == 0xf0009000U;
10169 // Encoding T1: BL<c>.W.
10170 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
10171 // Encoding T2: BLX<c>.W.
10172 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
10173 // Encoding T3: B<c>.W (not permitted in IT block).
10174 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
10175 && (insn & 0x07f00000U) != 0x03800000U);
10178 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
10180 // If this instruction is a 32-bit THUMB branch that crosses a 4K
10181 // page boundary and it follows 32-bit non-branch instruction,
10182 // we need to work around.
10183 if (is_32bit_branch
10184 && ((address + i) & 0xfffU) == 0xffeU
10186 && !last_was_branch)
10188 // Check to see if there is a relocation stub for this branch.
10189 bool force_target_arm = false;
10190 bool force_target_thumb = false;
10191 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
10192 Cortex_a8_relocs_info::const_iterator p =
10193 this->cortex_a8_relocs_info_.find(address + i);
10195 if (p != this->cortex_a8_relocs_info_.end())
10197 cortex_a8_reloc = p->second;
10198 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
10200 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10201 && !target_is_thumb)
10202 force_target_arm = true;
10203 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10204 && target_is_thumb)
10205 force_target_thumb = true;
10209 Stub_type stub_type = arm_stub_none;
10211 // Check if we have an offending branch instruction.
10212 uint16_t upper_insn = (insn >> 16) & 0xffffU;
10213 uint16_t lower_insn = insn & 0xffffU;
10214 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10216 if (cortex_a8_reloc != NULL
10217 && cortex_a8_reloc->reloc_stub() != NULL)
10218 // We've already made a stub for this instruction, e.g.
10219 // it's a long branch or a Thumb->ARM stub. Assume that
10220 // stub will suffice to work around the A8 erratum (see
10221 // setting of always_after_branch above).
10225 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
10227 stub_type = arm_stub_a8_veneer_b_cond;
10229 else if (is_b || is_bl || is_blx)
10231 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
10236 stub_type = (is_blx
10237 ? arm_stub_a8_veneer_blx
10239 ? arm_stub_a8_veneer_bl
10240 : arm_stub_a8_veneer_b));
10243 if (stub_type != arm_stub_none)
10245 Arm_address pc_for_insn = address + i + 4;
10247 // The original instruction is a BL, but the target is
10248 // an ARM instruction. If we were not making a stub,
10249 // the BL would have been converted to a BLX. Use the
10250 // BLX stub instead in that case.
10251 if (this->may_use_blx() && force_target_arm
10252 && stub_type == arm_stub_a8_veneer_bl)
10254 stub_type = arm_stub_a8_veneer_blx;
10258 // Conversely, if the original instruction was
10259 // BLX but the target is Thumb mode, use the BL stub.
10260 else if (force_target_thumb
10261 && stub_type == arm_stub_a8_veneer_blx)
10263 stub_type = arm_stub_a8_veneer_bl;
10271 // If we found a relocation, use the proper destination,
10272 // not the offset in the (unrelocated) instruction.
10273 // Note this is always done if we switched the stub type above.
10274 if (cortex_a8_reloc != NULL)
10275 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
10277 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
10279 // Add a new stub if destination address in in the same page.
10280 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
10282 Cortex_a8_stub* stub =
10283 this->stub_factory_.make_cortex_a8_stub(stub_type,
10287 Stub_table<big_endian>* stub_table =
10288 arm_relobj->stub_table(shndx);
10289 gold_assert(stub_table != NULL);
10290 stub_table->add_cortex_a8_stub(address + i, stub);
10295 i += insn_32bit ? 4 : 2;
10296 last_was_32bit = insn_32bit;
10297 last_was_branch = is_32bit_branch;
10301 // Apply the Cortex-A8 workaround.
10303 template<bool big_endian>
10305 Target_arm<big_endian>::apply_cortex_a8_workaround(
10306 const Cortex_a8_stub* stub,
10307 Arm_address stub_address,
10308 unsigned char* insn_view,
10309 Arm_address insn_address)
10311 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10312 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
10313 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
10314 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
10315 off_t branch_offset = stub_address - (insn_address + 4);
10317 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10318 switch (stub->stub_template()->type())
10320 case arm_stub_a8_veneer_b_cond:
10321 gold_assert(!utils::has_overflow<21>(branch_offset));
10322 upper_insn = RelocFuncs::thumb32_cond_branch_upper(upper_insn,
10324 lower_insn = RelocFuncs::thumb32_cond_branch_lower(lower_insn,
10328 case arm_stub_a8_veneer_b:
10329 case arm_stub_a8_veneer_bl:
10330 case arm_stub_a8_veneer_blx:
10331 if ((lower_insn & 0x5000U) == 0x4000U)
10332 // For a BLX instruction, make sure that the relocation is
10333 // rounded up to a word boundary. This follows the semantics of
10334 // the instruction which specifies that bit 1 of the target
10335 // address will come from bit 1 of the base address.
10336 branch_offset = (branch_offset + 2) & ~3;
10338 // Put BRANCH_OFFSET back into the insn.
10339 gold_assert(!utils::has_overflow<25>(branch_offset));
10340 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
10341 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
10345 gold_unreachable();
10348 // Put the relocated value back in the object file:
10349 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
10350 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
10353 template<bool big_endian>
10354 class Target_selector_arm : public Target_selector
10357 Target_selector_arm()
10358 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
10359 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
10363 do_instantiate_target()
10364 { return new Target_arm<big_endian>(); }
10367 // Fix .ARM.exidx section coverage.
10369 template<bool big_endian>
10371 Target_arm<big_endian>::fix_exidx_coverage(
10373 Arm_output_section<big_endian>* exidx_section,
10374 Symbol_table* symtab)
10376 // We need to look at all the input sections in output in ascending
10377 // order of of output address. We do that by building a sorted list
10378 // of output sections by addresses. Then we looks at the output sections
10379 // in order. The input sections in an output section are already sorted
10380 // by addresses within the output section.
10382 typedef std::set<Output_section*, output_section_address_less_than>
10383 Sorted_output_section_list;
10384 Sorted_output_section_list sorted_output_sections;
10385 Layout::Section_list section_list;
10386 layout->get_allocated_sections(§ion_list);
10387 for (Layout::Section_list::const_iterator p = section_list.begin();
10388 p != section_list.end();
10391 // We only care about output sections that contain executable code.
10392 if (((*p)->flags() & elfcpp::SHF_EXECINSTR) != 0)
10393 sorted_output_sections.insert(*p);
10396 // Go over the output sections in ascending order of output addresses.
10397 typedef typename Arm_output_section<big_endian>::Text_section_list
10399 Text_section_list sorted_text_sections;
10400 for(typename Sorted_output_section_list::iterator p =
10401 sorted_output_sections.begin();
10402 p != sorted_output_sections.end();
10405 Arm_output_section<big_endian>* arm_output_section =
10406 Arm_output_section<big_endian>::as_arm_output_section(*p);
10407 arm_output_section->append_text_sections_to_list(&sorted_text_sections);
10410 exidx_section->fix_exidx_coverage(sorted_text_sections, symtab);
10413 Target_selector_arm<false> target_selector_arm;
10414 Target_selector_arm<true> target_selector_armbe;
10416 } // End anonymous namespace.