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>
87 class Arm_output_data_got;
89 template<bool big_endian>
93 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
95 // Maximum branch offsets for ARM, THUMB and THUMB2.
96 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
97 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
98 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
99 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
100 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
101 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
103 // Thread Control Block size.
104 const size_t ARM_TCB_SIZE = 8;
106 // The arm target class.
108 // This is a very simple port of gold for ARM-EABI. It is intended for
109 // supporting Android only for the time being.
112 // - Implement all static relocation types documented in arm-reloc.def.
113 // - Make PLTs more flexible for different architecture features like
115 // There are probably a lot more.
117 // Ideally we would like to avoid using global variables but this is used
118 // very in many places and sometimes in loops. If we use a function
119 // returning a static instance of Arm_reloc_property_table, it will very
120 // slow in an threaded environment since the static instance needs to be
121 // locked. The pointer is below initialized in the
122 // Target::do_select_as_default_target() hook so that we do not spend time
123 // building the table if we are not linking ARM objects.
125 // An alternative is to to process the information in arm-reloc.def in
126 // compilation time and generate a representation of it in PODs only. That
127 // way we can avoid initialization when the linker starts.
129 Arm_reloc_property_table *arm_reloc_property_table = NULL;
131 // Instruction template class. This class is similar to the insn_sequence
132 // struct in bfd/elf32-arm.c.
137 // Types of instruction templates.
141 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
142 // templates with class-specific semantics. Currently this is used
143 // only by the Cortex_a8_stub class for handling condition codes in
144 // conditional branches.
145 THUMB16_SPECIAL_TYPE,
151 // Factory methods to create instruction templates in different formats.
153 static const Insn_template
154 thumb16_insn(uint32_t data)
155 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
157 // A Thumb conditional branch, in which the proper condition is inserted
158 // when we build the stub.
159 static const Insn_template
160 thumb16_bcond_insn(uint32_t data)
161 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
163 static const Insn_template
164 thumb32_insn(uint32_t data)
165 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
167 static const Insn_template
168 thumb32_b_insn(uint32_t data, int reloc_addend)
170 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
174 static const Insn_template
175 arm_insn(uint32_t data)
176 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
178 static const Insn_template
179 arm_rel_insn(unsigned data, int reloc_addend)
180 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
182 static const Insn_template
183 data_word(unsigned data, unsigned int r_type, int reloc_addend)
184 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
186 // Accessors. This class is used for read-only objects so no modifiers
191 { return this->data_; }
193 // Return the instruction sequence type of this.
196 { return this->type_; }
198 // Return the ARM relocation type of this.
201 { return this->r_type_; }
205 { return this->reloc_addend_; }
207 // Return size of instruction template in bytes.
211 // Return byte-alignment of instruction template.
216 // We make the constructor private to ensure that only the factory
219 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
220 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
223 // Instruction specific data. This is used to store information like
224 // some of the instruction bits.
226 // Instruction template type.
228 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
229 unsigned int r_type_;
230 // Relocation addend.
231 int32_t reloc_addend_;
234 // Macro for generating code to stub types. One entry per long/short
238 DEF_STUB(long_branch_any_any) \
239 DEF_STUB(long_branch_v4t_arm_thumb) \
240 DEF_STUB(long_branch_thumb_only) \
241 DEF_STUB(long_branch_v4t_thumb_thumb) \
242 DEF_STUB(long_branch_v4t_thumb_arm) \
243 DEF_STUB(short_branch_v4t_thumb_arm) \
244 DEF_STUB(long_branch_any_arm_pic) \
245 DEF_STUB(long_branch_any_thumb_pic) \
246 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
247 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
248 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
249 DEF_STUB(long_branch_thumb_only_pic) \
250 DEF_STUB(a8_veneer_b_cond) \
251 DEF_STUB(a8_veneer_b) \
252 DEF_STUB(a8_veneer_bl) \
253 DEF_STUB(a8_veneer_blx) \
254 DEF_STUB(v4_veneer_bx)
258 #define DEF_STUB(x) arm_stub_##x,
264 // First reloc stub type.
265 arm_stub_reloc_first = arm_stub_long_branch_any_any,
266 // Last reloc stub type.
267 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
269 // First Cortex-A8 stub type.
270 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
271 // Last Cortex-A8 stub type.
272 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
275 arm_stub_type_last = arm_stub_v4_veneer_bx
279 // Stub template class. Templates are meant to be read-only objects.
280 // A stub template for a stub type contains all read-only attributes
281 // common to all stubs of the same type.
286 Stub_template(Stub_type, const Insn_template*, size_t);
294 { return this->type_; }
296 // Return an array of instruction templates.
299 { return this->insns_; }
301 // Return size of template in number of instructions.
304 { return this->insn_count_; }
306 // Return size of template in bytes.
309 { return this->size_; }
311 // Return alignment of the stub template.
314 { return this->alignment_; }
316 // Return whether entry point is in thumb mode.
318 entry_in_thumb_mode() const
319 { return this->entry_in_thumb_mode_; }
321 // Return number of relocations in this template.
324 { return this->relocs_.size(); }
326 // Return index of the I-th instruction with relocation.
328 reloc_insn_index(size_t i) const
330 gold_assert(i < this->relocs_.size());
331 return this->relocs_[i].first;
334 // Return the offset of the I-th instruction with relocation from the
335 // beginning of the stub.
337 reloc_offset(size_t i) const
339 gold_assert(i < this->relocs_.size());
340 return this->relocs_[i].second;
344 // This contains information about an instruction template with a relocation
345 // and its offset from start of stub.
346 typedef std::pair<size_t, section_size_type> Reloc;
348 // A Stub_template may not be copied. We want to share templates as much
350 Stub_template(const Stub_template&);
351 Stub_template& operator=(const Stub_template&);
355 // Points to an array of Insn_templates.
356 const Insn_template* insns_;
357 // Number of Insn_templates in insns_[].
359 // Size of templated instructions in bytes.
361 // Alignment of templated instructions.
363 // Flag to indicate if entry is in thumb mode.
364 bool entry_in_thumb_mode_;
365 // A table of reloc instruction indices and offsets. We can find these by
366 // looking at the instruction templates but we pre-compute and then stash
367 // them here for speed.
368 std::vector<Reloc> relocs_;
372 // A class for code stubs. This is a base class for different type of
373 // stubs used in the ARM target.
379 static const section_offset_type invalid_offset =
380 static_cast<section_offset_type>(-1);
383 Stub(const Stub_template* stub_template)
384 : stub_template_(stub_template), offset_(invalid_offset)
391 // Return the stub template.
393 stub_template() const
394 { return this->stub_template_; }
396 // Return offset of code stub from beginning of its containing stub table.
400 gold_assert(this->offset_ != invalid_offset);
401 return this->offset_;
404 // Set offset of code stub from beginning of its containing stub table.
406 set_offset(section_offset_type offset)
407 { this->offset_ = offset; }
409 // Return the relocation target address of the i-th relocation in the
410 // stub. This must be defined in a child class.
412 reloc_target(size_t i)
413 { return this->do_reloc_target(i); }
415 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
417 write(unsigned char* view, section_size_type view_size, bool big_endian)
418 { this->do_write(view, view_size, big_endian); }
420 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
421 // for the i-th instruction.
423 thumb16_special(size_t i)
424 { return this->do_thumb16_special(i); }
427 // This must be defined in the child class.
429 do_reloc_target(size_t) = 0;
431 // This may be overridden in the child class.
433 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
436 this->do_fixed_endian_write<true>(view, view_size);
438 this->do_fixed_endian_write<false>(view, view_size);
441 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
442 // instruction template.
444 do_thumb16_special(size_t)
445 { gold_unreachable(); }
448 // A template to implement do_write.
449 template<bool big_endian>
451 do_fixed_endian_write(unsigned char*, section_size_type);
454 const Stub_template* stub_template_;
455 // Offset within the section of containing this stub.
456 section_offset_type offset_;
459 // Reloc stub class. These are stubs we use to fix up relocation because
460 // of limited branch ranges.
462 class Reloc_stub : public Stub
465 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
466 // We assume we never jump to this address.
467 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
469 // Return destination address.
471 destination_address() const
473 gold_assert(this->destination_address_ != this->invalid_address);
474 return this->destination_address_;
477 // Set destination address.
479 set_destination_address(Arm_address address)
481 gold_assert(address != this->invalid_address);
482 this->destination_address_ = address;
485 // Reset destination address.
487 reset_destination_address()
488 { this->destination_address_ = this->invalid_address; }
490 // Determine stub type for a branch of a relocation of R_TYPE going
491 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
492 // the branch target is a thumb instruction. TARGET is used for look
493 // up ARM-specific linker settings.
495 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
496 Arm_address branch_target, bool target_is_thumb);
498 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
499 // and an addend. Since we treat global and local symbol differently, we
500 // use a Symbol object for a global symbol and a object-index pair for
505 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
506 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
507 // and R_SYM must not be invalid_index.
508 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
509 unsigned int r_sym, int32_t addend)
510 : stub_type_(stub_type), addend_(addend)
514 this->r_sym_ = Reloc_stub::invalid_index;
515 this->u_.symbol = symbol;
519 gold_assert(relobj != NULL && r_sym != invalid_index);
520 this->r_sym_ = r_sym;
521 this->u_.relobj = relobj;
528 // Accessors: Keys are meant to be read-only object so no modifiers are
534 { return this->stub_type_; }
536 // Return the local symbol index or invalid_index.
539 { return this->r_sym_; }
541 // Return the symbol if there is one.
544 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
546 // Return the relobj if there is one.
549 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
551 // Whether this equals to another key k.
553 eq(const Key& k) const
555 return ((this->stub_type_ == k.stub_type_)
556 && (this->r_sym_ == k.r_sym_)
557 && ((this->r_sym_ != Reloc_stub::invalid_index)
558 ? (this->u_.relobj == k.u_.relobj)
559 : (this->u_.symbol == k.u_.symbol))
560 && (this->addend_ == k.addend_));
563 // Return a hash value.
567 return (this->stub_type_
569 ^ gold::string_hash<char>(
570 (this->r_sym_ != Reloc_stub::invalid_index)
571 ? this->u_.relobj->name().c_str()
572 : this->u_.symbol->name())
576 // Functors for STL associative containers.
580 operator()(const Key& k) const
581 { return k.hash_value(); }
587 operator()(const Key& k1, const Key& k2) const
588 { return k1.eq(k2); }
591 // Name of key. This is mainly for debugging.
597 Stub_type stub_type_;
598 // If this is a local symbol, this is the index in the defining object.
599 // Otherwise, it is invalid_index for a global symbol.
601 // If r_sym_ is invalid index. This points to a global symbol.
602 // Otherwise, this points a relobj. We used the unsized and target
603 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
604 // Arm_relobj. This is done to avoid making the stub class a template
605 // as most of the stub machinery is endianity-neutral. However, it
606 // may require a bit of casting done by users of this class.
609 const Symbol* symbol;
610 const Relobj* relobj;
612 // Addend associated with a reloc.
617 // Reloc_stubs are created via a stub factory. So these are protected.
618 Reloc_stub(const Stub_template* stub_template)
619 : Stub(stub_template), destination_address_(invalid_address)
625 friend class Stub_factory;
627 // Return the relocation target address of the i-th relocation in the
630 do_reloc_target(size_t i)
632 // All reloc stub have only one relocation.
634 return this->destination_address_;
638 // Address of destination.
639 Arm_address destination_address_;
642 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
643 // THUMB branch that meets the following conditions:
645 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
646 // branch address is 0xffe.
647 // 2. The branch target address is in the same page as the first word of the
649 // 3. The branch follows a 32-bit instruction which is not a branch.
651 // To do the fix up, we need to store the address of the branch instruction
652 // and its target at least. We also need to store the original branch
653 // instruction bits for the condition code in a conditional branch. The
654 // condition code is used in a special instruction template. We also want
655 // to identify input sections needing Cortex-A8 workaround quickly. We store
656 // extra information about object and section index of the code section
657 // containing a branch being fixed up. The information is used to mark
658 // the code section when we finalize the Cortex-A8 stubs.
661 class Cortex_a8_stub : public Stub
667 // Return the object of the code section containing the branch being fixed
671 { return this->relobj_; }
673 // Return the section index of the code section containing the branch being
677 { return this->shndx_; }
679 // Return the source address of stub. This is the address of the original
680 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
683 source_address() const
684 { return this->source_address_; }
686 // Return the destination address of the stub. This is the branch taken
687 // address of the original branch instruction. LSB is 1 if it is a THUMB
688 // instruction address.
690 destination_address() const
691 { return this->destination_address_; }
693 // Return the instruction being fixed up.
695 original_insn() const
696 { return this->original_insn_; }
699 // Cortex_a8_stubs are created via a stub factory. So these are protected.
700 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
701 unsigned int shndx, Arm_address source_address,
702 Arm_address destination_address, uint32_t original_insn)
703 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
704 source_address_(source_address | 1U),
705 destination_address_(destination_address),
706 original_insn_(original_insn)
709 friend class Stub_factory;
711 // Return the relocation target address of the i-th relocation in the
714 do_reloc_target(size_t i)
716 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
718 // The conditional branch veneer has two relocations.
720 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
724 // All other Cortex-A8 stubs have only one relocation.
726 return this->destination_address_;
730 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
732 do_thumb16_special(size_t);
735 // Object of the code section containing the branch being fixed up.
737 // Section index of the code section containing the branch begin fixed up.
739 // Source address of original branch.
740 Arm_address source_address_;
741 // Destination address of the original branch.
742 Arm_address destination_address_;
743 // Original branch instruction. This is needed for copying the condition
744 // code from a condition branch to its stub.
745 uint32_t original_insn_;
748 // ARMv4 BX Rx branch relocation stub class.
749 class Arm_v4bx_stub : public Stub
755 // Return the associated register.
758 { return this->reg_; }
761 // Arm V4BX stubs are created via a stub factory. So these are protected.
762 Arm_v4bx_stub(const Stub_template* stub_template, const uint32_t reg)
763 : Stub(stub_template), reg_(reg)
766 friend class Stub_factory;
768 // Return the relocation target address of the i-th relocation in the
771 do_reloc_target(size_t)
772 { gold_unreachable(); }
774 // This may be overridden in the child class.
776 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
779 this->do_fixed_endian_v4bx_write<true>(view, view_size);
781 this->do_fixed_endian_v4bx_write<false>(view, view_size);
785 // A template to implement do_write.
786 template<bool big_endian>
788 do_fixed_endian_v4bx_write(unsigned char* view, section_size_type)
790 const Insn_template* insns = this->stub_template()->insns();
791 elfcpp::Swap<32, big_endian>::writeval(view,
793 + (this->reg_ << 16)));
794 view += insns[0].size();
795 elfcpp::Swap<32, big_endian>::writeval(view,
796 (insns[1].data() + this->reg_));
797 view += insns[1].size();
798 elfcpp::Swap<32, big_endian>::writeval(view,
799 (insns[2].data() + this->reg_));
802 // A register index (r0-r14), which is associated with the stub.
806 // Stub factory class.
811 // Return the unique instance of this class.
812 static const Stub_factory&
815 static Stub_factory singleton;
819 // Make a relocation stub.
821 make_reloc_stub(Stub_type stub_type) const
823 gold_assert(stub_type >= arm_stub_reloc_first
824 && stub_type <= arm_stub_reloc_last);
825 return new Reloc_stub(this->stub_templates_[stub_type]);
828 // Make a Cortex-A8 stub.
830 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
831 Arm_address source, Arm_address destination,
832 uint32_t original_insn) const
834 gold_assert(stub_type >= arm_stub_cortex_a8_first
835 && stub_type <= arm_stub_cortex_a8_last);
836 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
837 source, destination, original_insn);
840 // Make an ARM V4BX relocation stub.
841 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
843 make_arm_v4bx_stub(uint32_t reg) const
845 gold_assert(reg < 0xf);
846 return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
851 // Constructor and destructor are protected since we only return a single
852 // instance created in Stub_factory::get_instance().
856 // A Stub_factory may not be copied since it is a singleton.
857 Stub_factory(const Stub_factory&);
858 Stub_factory& operator=(Stub_factory&);
860 // Stub templates. These are initialized in the constructor.
861 const Stub_template* stub_templates_[arm_stub_type_last+1];
864 // A class to hold stubs for the ARM target.
866 template<bool big_endian>
867 class Stub_table : public Output_data
870 Stub_table(Arm_input_section<big_endian>* owner)
871 : Output_data(), owner_(owner), reloc_stubs_(), cortex_a8_stubs_(),
872 arm_v4bx_stubs_(0xf), prev_data_size_(0), prev_addralign_(1)
878 // Owner of this stub table.
879 Arm_input_section<big_endian>*
881 { return this->owner_; }
883 // Whether this stub table is empty.
887 return (this->reloc_stubs_.empty()
888 && this->cortex_a8_stubs_.empty()
889 && this->arm_v4bx_stubs_.empty());
892 // Return the current data size.
894 current_data_size() const
895 { return this->current_data_size_for_child(); }
897 // Add a STUB with using KEY. Caller is reponsible for avoid adding
898 // if already a STUB with the same key has been added.
900 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
902 const Stub_template* stub_template = stub->stub_template();
903 gold_assert(stub_template->type() == key.stub_type());
904 this->reloc_stubs_[key] = stub;
907 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
908 // Caller is reponsible for avoid adding if already a STUB with the same
909 // address has been added.
911 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
913 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
914 this->cortex_a8_stubs_.insert(value);
917 // Add an ARM V4BX relocation stub. A register index will be retrieved
920 add_arm_v4bx_stub(Arm_v4bx_stub* stub)
922 gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
923 this->arm_v4bx_stubs_[stub->reg()] = stub;
926 // Remove all Cortex-A8 stubs.
928 remove_all_cortex_a8_stubs();
930 // Look up a relocation stub using KEY. Return NULL if there is none.
932 find_reloc_stub(const Reloc_stub::Key& key) const
934 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
935 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
938 // Look up an arm v4bx relocation stub using the register index.
939 // Return NULL if there is none.
941 find_arm_v4bx_stub(const uint32_t reg) const
943 gold_assert(reg < 0xf);
944 return this->arm_v4bx_stubs_[reg];
947 // Relocate stubs in this stub table.
949 relocate_stubs(const Relocate_info<32, big_endian>*,
950 Target_arm<big_endian>*, Output_section*,
951 unsigned char*, Arm_address, section_size_type);
953 // Update data size and alignment at the end of a relaxation pass. Return
954 // true if either data size or alignment is different from that of the
955 // previous relaxation pass.
957 update_data_size_and_addralign();
959 // Finalize stubs. Set the offsets of all stubs and mark input sections
960 // needing the Cortex-A8 workaround.
964 // Apply Cortex-A8 workaround to an address range.
966 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
967 unsigned char*, Arm_address,
971 // Write out section contents.
973 do_write(Output_file*);
975 // Return the required alignment.
978 { return this->prev_addralign_; }
980 // Reset address and file offset.
982 do_reset_address_and_file_offset()
983 { this->set_current_data_size_for_child(this->prev_data_size_); }
985 // Set final data size.
987 set_final_data_size()
988 { this->set_data_size(this->current_data_size()); }
991 // Relocate one stub.
993 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
994 Target_arm<big_endian>*, Output_section*,
995 unsigned char*, Arm_address, section_size_type);
997 // Unordered map of relocation stubs.
999 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
1000 Reloc_stub::Key::equal_to>
1003 // List of Cortex-A8 stubs ordered by addresses of branches being
1004 // fixed up in output.
1005 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
1006 // List of Arm V4BX relocation stubs ordered by associated registers.
1007 typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
1009 // Owner of this stub table.
1010 Arm_input_section<big_endian>* owner_;
1011 // The relocation stubs.
1012 Reloc_stub_map reloc_stubs_;
1013 // The cortex_a8_stubs.
1014 Cortex_a8_stub_list cortex_a8_stubs_;
1015 // The Arm V4BX relocation stubs.
1016 Arm_v4bx_stub_list arm_v4bx_stubs_;
1017 // data size of this in the previous pass.
1018 off_t prev_data_size_;
1019 // address alignment of this in the previous pass.
1020 uint64_t prev_addralign_;
1023 // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1024 // we add to the end of an EXIDX input section that goes into the output.
1026 class Arm_exidx_cantunwind : public Output_section_data
1029 Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
1030 : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
1033 // Return the object containing the section pointed by this.
1036 { return this->relobj_; }
1038 // Return the section index of the section pointed by this.
1041 { return this->shndx_; }
1045 do_write(Output_file* of)
1047 if (parameters->target().is_big_endian())
1048 this->do_fixed_endian_write<true>(of);
1050 this->do_fixed_endian_write<false>(of);
1054 // Implement do_write for a given endianity.
1055 template<bool big_endian>
1057 do_fixed_endian_write(Output_file*);
1059 // The object containing the section pointed by this.
1061 // The section index of the section pointed by this.
1062 unsigned int shndx_;
1065 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1066 // Offset map is used to map input section offset within the EXIDX section
1067 // to the output offset from the start of this EXIDX section.
1069 typedef std::map<section_offset_type, section_offset_type>
1070 Arm_exidx_section_offset_map;
1072 // Arm_exidx_merged_section class. This represents an EXIDX input section
1073 // with some of its entries merged.
1075 class Arm_exidx_merged_section : public Output_relaxed_input_section
1078 // Constructor for Arm_exidx_merged_section.
1079 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1080 // SECTION_OFFSET_MAP points to a section offset map describing how
1081 // parts of the input section are mapped to output. DELETED_BYTES is
1082 // the number of bytes deleted from the EXIDX input section.
1083 Arm_exidx_merged_section(
1084 const Arm_exidx_input_section& exidx_input_section,
1085 const Arm_exidx_section_offset_map& section_offset_map,
1086 uint32_t deleted_bytes);
1088 // Return the original EXIDX input section.
1089 const Arm_exidx_input_section&
1090 exidx_input_section() const
1091 { return this->exidx_input_section_; }
1093 // Return the section offset map.
1094 const Arm_exidx_section_offset_map&
1095 section_offset_map() const
1096 { return this->section_offset_map_; }
1099 // Write merged section into file OF.
1101 do_write(Output_file* of);
1104 do_output_offset(const Relobj*, unsigned int, section_offset_type,
1105 section_offset_type*) const;
1108 // Original EXIDX input section.
1109 const Arm_exidx_input_section& exidx_input_section_;
1110 // Section offset map.
1111 const Arm_exidx_section_offset_map& section_offset_map_;
1114 // A class to wrap an ordinary input section containing executable code.
1116 template<bool big_endian>
1117 class Arm_input_section : public Output_relaxed_input_section
1120 Arm_input_section(Relobj* relobj, unsigned int shndx)
1121 : Output_relaxed_input_section(relobj, shndx, 1),
1122 original_addralign_(1), original_size_(0), stub_table_(NULL)
1125 ~Arm_input_section()
1132 // Whether this is a stub table owner.
1134 is_stub_table_owner() const
1135 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1137 // Return the stub table.
1138 Stub_table<big_endian>*
1140 { return this->stub_table_; }
1142 // Set the stub_table.
1144 set_stub_table(Stub_table<big_endian>* stub_table)
1145 { this->stub_table_ = stub_table; }
1147 // Downcast a base pointer to an Arm_input_section pointer. This is
1148 // not type-safe but we only use Arm_input_section not the base class.
1149 static Arm_input_section<big_endian>*
1150 as_arm_input_section(Output_relaxed_input_section* poris)
1151 { return static_cast<Arm_input_section<big_endian>*>(poris); }
1154 // Write data to output file.
1156 do_write(Output_file*);
1158 // Return required alignment of this.
1160 do_addralign() const
1162 if (this->is_stub_table_owner())
1163 return std::max(this->stub_table_->addralign(),
1164 this->original_addralign_);
1166 return this->original_addralign_;
1169 // Finalize data size.
1171 set_final_data_size();
1173 // Reset address and file offset.
1175 do_reset_address_and_file_offset();
1179 do_output_offset(const Relobj* object, unsigned int shndx,
1180 section_offset_type offset,
1181 section_offset_type* poutput) const
1183 if ((object == this->relobj())
1184 && (shndx == this->shndx())
1186 && (convert_types<uint64_t, section_offset_type>(offset)
1187 <= this->original_size_))
1197 // Copying is not allowed.
1198 Arm_input_section(const Arm_input_section&);
1199 Arm_input_section& operator=(const Arm_input_section&);
1201 // Address alignment of the original input section.
1202 uint64_t original_addralign_;
1203 // Section size of the original input section.
1204 uint64_t original_size_;
1206 Stub_table<big_endian>* stub_table_;
1209 // Arm_exidx_fixup class. This is used to define a number of methods
1210 // and keep states for fixing up EXIDX coverage.
1212 class Arm_exidx_fixup
1215 Arm_exidx_fixup(Output_section* exidx_output_section)
1216 : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1217 last_inlined_entry_(0), last_input_section_(NULL),
1218 section_offset_map_(NULL), first_output_text_section_(NULL)
1222 { delete this->section_offset_map_; }
1224 // Process an EXIDX section for entry merging. Return number of bytes to
1225 // be deleted in output. If parts of the input EXIDX section are merged
1226 // a heap allocated Arm_exidx_section_offset_map is store in the located
1227 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1229 template<bool big_endian>
1231 process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1232 Arm_exidx_section_offset_map** psection_offset_map);
1234 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1235 // input section, if there is not one already.
1237 add_exidx_cantunwind_as_needed();
1239 // Return the output section for the text section which is linked to the
1240 // first exidx input in output.
1242 first_output_text_section() const
1243 { return this->first_output_text_section_; }
1246 // Copying is not allowed.
1247 Arm_exidx_fixup(const Arm_exidx_fixup&);
1248 Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1250 // Type of EXIDX unwind entry.
1255 // EXIDX_CANTUNWIND.
1256 UT_EXIDX_CANTUNWIND,
1263 // Process an EXIDX entry. We only care about the second word of the
1264 // entry. Return true if the entry can be deleted.
1266 process_exidx_entry(uint32_t second_word);
1268 // Update the current section offset map during EXIDX section fix-up.
1269 // If there is no map, create one. INPUT_OFFSET is the offset of a
1270 // reference point, DELETED_BYTES is the number of deleted by in the
1271 // section so far. If DELETE_ENTRY is true, the reference point and
1272 // all offsets after the previous reference point are discarded.
1274 update_offset_map(section_offset_type input_offset,
1275 section_size_type deleted_bytes, bool delete_entry);
1277 // EXIDX output section.
1278 Output_section* exidx_output_section_;
1279 // Unwind type of the last EXIDX entry processed.
1280 Unwind_type last_unwind_type_;
1281 // Last seen inlined EXIDX entry.
1282 uint32_t last_inlined_entry_;
1283 // Last processed EXIDX input section.
1284 const Arm_exidx_input_section* last_input_section_;
1285 // Section offset map created in process_exidx_section.
1286 Arm_exidx_section_offset_map* section_offset_map_;
1287 // Output section for the text section which is linked to the first exidx
1289 Output_section* first_output_text_section_;
1292 // Arm output section class. This is defined mainly to add a number of
1293 // stub generation methods.
1295 template<bool big_endian>
1296 class Arm_output_section : public Output_section
1299 typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
1301 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1302 elfcpp::Elf_Xword flags)
1303 : Output_section(name, type, flags)
1306 ~Arm_output_section()
1309 // Group input sections for stub generation.
1311 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1313 // Downcast a base pointer to an Arm_output_section pointer. This is
1314 // not type-safe but we only use Arm_output_section not the base class.
1315 static Arm_output_section<big_endian>*
1316 as_arm_output_section(Output_section* os)
1317 { return static_cast<Arm_output_section<big_endian>*>(os); }
1319 // Append all input text sections in this into LIST.
1321 append_text_sections_to_list(Text_section_list* list);
1323 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1324 // is a list of text input sections sorted in ascending order of their
1325 // output addresses.
1327 fix_exidx_coverage(Layout* layout,
1328 const Text_section_list& sorted_text_section,
1329 Symbol_table* symtab);
1333 typedef Output_section::Input_section Input_section;
1334 typedef Output_section::Input_section_list Input_section_list;
1336 // Create a stub group.
1337 void create_stub_group(Input_section_list::const_iterator,
1338 Input_section_list::const_iterator,
1339 Input_section_list::const_iterator,
1340 Target_arm<big_endian>*,
1341 std::vector<Output_relaxed_input_section*>*);
1344 // Arm_exidx_input_section class. This represents an EXIDX input section.
1346 class Arm_exidx_input_section
1349 static const section_offset_type invalid_offset =
1350 static_cast<section_offset_type>(-1);
1352 Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1353 unsigned int link, uint32_t size, uint32_t addralign)
1354 : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1355 addralign_(addralign)
1358 ~Arm_exidx_input_section()
1361 // Accessors: This is a read-only class.
1363 // Return the object containing this EXIDX input section.
1366 { return this->relobj_; }
1368 // Return the section index of this EXIDX input section.
1371 { return this->shndx_; }
1373 // Return the section index of linked text section in the same object.
1376 { return this->link_; }
1378 // Return size of the EXIDX input section.
1381 { return this->size_; }
1383 // Reutnr address alignment of EXIDX input section.
1386 { return this->addralign_; }
1389 // Object containing this.
1391 // Section index of this.
1392 unsigned int shndx_;
1393 // text section linked to this in the same object.
1395 // Size of this. For ARM 32-bit is sufficient.
1397 // Address alignment of this. For ARM 32-bit is sufficient.
1398 uint32_t addralign_;
1401 // Arm_relobj class.
1403 template<bool big_endian>
1404 class Arm_relobj : public Sized_relobj<32, big_endian>
1407 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1409 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1410 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1411 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1412 stub_tables_(), local_symbol_is_thumb_function_(),
1413 attributes_section_data_(NULL), mapping_symbols_info_(),
1414 section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
1415 output_local_symbol_count_needs_update_(false)
1419 { delete this->attributes_section_data_; }
1421 // Return the stub table of the SHNDX-th section if there is one.
1422 Stub_table<big_endian>*
1423 stub_table(unsigned int shndx) const
1425 gold_assert(shndx < this->stub_tables_.size());
1426 return this->stub_tables_[shndx];
1429 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1431 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1433 gold_assert(shndx < this->stub_tables_.size());
1434 this->stub_tables_[shndx] = stub_table;
1437 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1438 // index. This is only valid after do_count_local_symbol is called.
1440 local_symbol_is_thumb_function(unsigned int r_sym) const
1442 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1443 return this->local_symbol_is_thumb_function_[r_sym];
1446 // Scan all relocation sections for stub generation.
1448 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1451 // Convert regular input section with index SHNDX to a relaxed section.
1453 convert_input_section_to_relaxed_section(unsigned shndx)
1455 // The stubs have relocations and we need to process them after writing
1456 // out the stubs. So relocation now must follow section write.
1457 this->set_section_offset(shndx, -1ULL);
1458 this->set_relocs_must_follow_section_writes();
1461 // Downcast a base pointer to an Arm_relobj pointer. This is
1462 // not type-safe but we only use Arm_relobj not the base class.
1463 static Arm_relobj<big_endian>*
1464 as_arm_relobj(Relobj* relobj)
1465 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1467 // Processor-specific flags in ELF file header. This is valid only after
1470 processor_specific_flags() const
1471 { return this->processor_specific_flags_; }
1473 // Attribute section data This is the contents of the .ARM.attribute section
1475 const Attributes_section_data*
1476 attributes_section_data() const
1477 { return this->attributes_section_data_; }
1479 // Mapping symbol location.
1480 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1482 // Functor for STL container.
1483 struct Mapping_symbol_position_less
1486 operator()(const Mapping_symbol_position& p1,
1487 const Mapping_symbol_position& p2) const
1489 return (p1.first < p2.first
1490 || (p1.first == p2.first && p1.second < p2.second));
1494 // We only care about the first character of a mapping symbol, so
1495 // we only store that instead of the whole symbol name.
1496 typedef std::map<Mapping_symbol_position, char,
1497 Mapping_symbol_position_less> Mapping_symbols_info;
1499 // Whether a section contains any Cortex-A8 workaround.
1501 section_has_cortex_a8_workaround(unsigned int shndx) const
1503 return (this->section_has_cortex_a8_workaround_ != NULL
1504 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1507 // Mark a section that has Cortex-A8 workaround.
1509 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1511 if (this->section_has_cortex_a8_workaround_ == NULL)
1512 this->section_has_cortex_a8_workaround_ =
1513 new std::vector<bool>(this->shnum(), false);
1514 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1517 // Return the EXIDX section of an text section with index SHNDX or NULL
1518 // if the text section has no associated EXIDX section.
1519 const Arm_exidx_input_section*
1520 exidx_input_section_by_link(unsigned int shndx) const
1522 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1523 return ((p != this->exidx_section_map_.end()
1524 && p->second->link() == shndx)
1529 // Return the EXIDX section with index SHNDX or NULL if there is none.
1530 const Arm_exidx_input_section*
1531 exidx_input_section_by_shndx(unsigned shndx) const
1533 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1534 return ((p != this->exidx_section_map_.end()
1535 && p->second->shndx() == shndx)
1540 // Whether output local symbol count needs updating.
1542 output_local_symbol_count_needs_update() const
1543 { return this->output_local_symbol_count_needs_update_; }
1545 // Set output_local_symbol_count_needs_update flag to be true.
1547 set_output_local_symbol_count_needs_update()
1548 { this->output_local_symbol_count_needs_update_ = true; }
1550 // Update output local symbol count at the end of relaxation.
1552 update_output_local_symbol_count();
1555 // Post constructor setup.
1559 // Call parent's setup method.
1560 Sized_relobj<32, big_endian>::do_setup();
1562 // Initialize look-up tables.
1563 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1564 this->stub_tables_.swap(empty_stub_table_list);
1567 // Count the local symbols.
1569 do_count_local_symbols(Stringpool_template<char>*,
1570 Stringpool_template<char>*);
1573 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1574 const unsigned char* pshdrs,
1575 typename Sized_relobj<32, big_endian>::Views* pivews);
1577 // Read the symbol information.
1579 do_read_symbols(Read_symbols_data* sd);
1581 // Process relocs for garbage collection.
1583 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1587 // Whether a section needs to be scanned for relocation stubs.
1589 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1590 const Relobj::Output_sections&,
1591 const Symbol_table *, const unsigned char*);
1593 // Whether a section is a scannable text section.
1595 section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
1596 const Output_section*, const Symbol_table *);
1598 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1600 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1601 unsigned int, Output_section*,
1602 const Symbol_table *);
1604 // Scan a section for the Cortex-A8 erratum.
1606 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1607 unsigned int, Output_section*,
1608 Target_arm<big_endian>*);
1610 // Find the linked text section of an EXIDX section by looking at the
1611 // first reloction of the EXIDX section. PSHDR points to the section
1612 // headers of a relocation section and PSYMS points to the local symbols.
1613 // PSHNDX points to a location storing the text section index if found.
1614 // Return whether we can find the linked section.
1616 find_linked_text_section(const unsigned char* pshdr,
1617 const unsigned char* psyms, unsigned int* pshndx);
1620 // Make a new Arm_exidx_input_section object for EXIDX section with
1621 // index SHNDX and section header SHDR. TEXT_SHNDX is the section
1622 // index of the linked text section.
1624 make_exidx_input_section(unsigned int shndx,
1625 const elfcpp::Shdr<32, big_endian>& shdr,
1626 unsigned int text_shndx);
1628 // Return the output address of either a plain input section or a
1629 // relaxed input section. SHNDX is the section index.
1631 simple_input_section_output_address(unsigned int, Output_section*);
1633 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1634 typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1637 // List of stub tables.
1638 Stub_table_list stub_tables_;
1639 // Bit vector to tell if a local symbol is a thumb function or not.
1640 // This is only valid after do_count_local_symbol is called.
1641 std::vector<bool> local_symbol_is_thumb_function_;
1642 // processor-specific flags in ELF file header.
1643 elfcpp::Elf_Word processor_specific_flags_;
1644 // Object attributes if there is an .ARM.attributes section or NULL.
1645 Attributes_section_data* attributes_section_data_;
1646 // Mapping symbols information.
1647 Mapping_symbols_info mapping_symbols_info_;
1648 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1649 std::vector<bool>* section_has_cortex_a8_workaround_;
1650 // Map a text section to its associated .ARM.exidx section, if there is one.
1651 Exidx_section_map exidx_section_map_;
1652 // Whether output local symbol count needs updating.
1653 bool output_local_symbol_count_needs_update_;
1656 // Arm_dynobj class.
1658 template<bool big_endian>
1659 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1662 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1663 const elfcpp::Ehdr<32, big_endian>& ehdr)
1664 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1665 processor_specific_flags_(0), attributes_section_data_(NULL)
1669 { delete this->attributes_section_data_; }
1671 // Downcast a base pointer to an Arm_relobj pointer. This is
1672 // not type-safe but we only use Arm_relobj not the base class.
1673 static Arm_dynobj<big_endian>*
1674 as_arm_dynobj(Dynobj* dynobj)
1675 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1677 // Processor-specific flags in ELF file header. This is valid only after
1680 processor_specific_flags() const
1681 { return this->processor_specific_flags_; }
1683 // Attributes section data.
1684 const Attributes_section_data*
1685 attributes_section_data() const
1686 { return this->attributes_section_data_; }
1689 // Read the symbol information.
1691 do_read_symbols(Read_symbols_data* sd);
1694 // processor-specific flags in ELF file header.
1695 elfcpp::Elf_Word processor_specific_flags_;
1696 // Object attributes if there is an .ARM.attributes section or NULL.
1697 Attributes_section_data* attributes_section_data_;
1700 // Functor to read reloc addends during stub generation.
1702 template<int sh_type, bool big_endian>
1703 struct Stub_addend_reader
1705 // Return the addend for a relocation of a particular type. Depending
1706 // on whether this is a REL or RELA relocation, read the addend from a
1707 // view or from a Reloc object.
1708 elfcpp::Elf_types<32>::Elf_Swxword
1710 unsigned int /* r_type */,
1711 const unsigned char* /* view */,
1712 const typename Reloc_types<sh_type,
1713 32, big_endian>::Reloc& /* reloc */) const;
1716 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1718 template<bool big_endian>
1719 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1721 elfcpp::Elf_types<32>::Elf_Swxword
1724 const unsigned char*,
1725 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1728 // Specialized Stub_addend_reader for RELA type relocation sections.
1729 // We currently do not handle RELA type relocation sections but it is trivial
1730 // to implement the addend reader. This is provided for completeness and to
1731 // make it easier to add support for RELA relocation sections in the future.
1733 template<bool big_endian>
1734 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1736 elfcpp::Elf_types<32>::Elf_Swxword
1739 const unsigned char*,
1740 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1741 big_endian>::Reloc& reloc) const
1742 { return reloc.get_r_addend(); }
1745 // Cortex_a8_reloc class. We keep record of relocation that may need
1746 // the Cortex-A8 erratum workaround.
1748 class Cortex_a8_reloc
1751 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1752 Arm_address destination)
1753 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1759 // Accessors: This is a read-only class.
1761 // Return the relocation stub associated with this relocation if there is
1765 { return this->reloc_stub_; }
1767 // Return the relocation type.
1770 { return this->r_type_; }
1772 // Return the destination address of the relocation. LSB stores the THUMB
1776 { return this->destination_; }
1779 // Associated relocation stub if there is one, or NULL.
1780 const Reloc_stub* reloc_stub_;
1782 unsigned int r_type_;
1783 // Destination address of this relocation. LSB is used to distinguish
1785 Arm_address destination_;
1788 // Arm_output_data_got class. We derive this from Output_data_got to add
1789 // extra methods to handle TLS relocations in a static link.
1791 template<bool big_endian>
1792 class Arm_output_data_got : public Output_data_got<32, big_endian>
1795 Arm_output_data_got(Symbol_table* symtab, Layout* layout)
1796 : Output_data_got<32, big_endian>(), symbol_table_(symtab), layout_(layout)
1799 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1800 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1801 // applied in a static link.
1803 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1804 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
1806 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1807 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1808 // relocation that needs to be applied in a static link.
1810 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1811 Sized_relobj<32, big_endian>* relobj, unsigned int index)
1813 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
1817 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
1818 // The first one is initialized to be 1, which is the module index for
1819 // the main executable and the second one 0. A reloc of the type
1820 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
1821 // be applied by gold. GSYM is a global symbol.
1823 add_tls_gd32_with_static_reloc(unsigned int got_type, Symbol* gsym);
1825 // Same as the above but for a local symbol in OBJECT with INDEX.
1827 add_tls_gd32_with_static_reloc(unsigned int got_type,
1828 Sized_relobj<32, big_endian>* object,
1829 unsigned int index);
1832 // Write out the GOT table.
1834 do_write(Output_file*);
1837 // This class represent dynamic relocations that need to be applied by
1838 // gold because we are using TLS relocations in a static link.
1842 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1843 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
1844 { this->u_.global.symbol = gsym; }
1846 Static_reloc(unsigned int got_offset, unsigned int r_type,
1847 Sized_relobj<32, big_endian>* relobj, unsigned int index)
1848 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
1850 this->u_.local.relobj = relobj;
1851 this->u_.local.index = index;
1854 // Return the GOT offset.
1857 { return this->got_offset_; }
1862 { return this->r_type_; }
1864 // Whether the symbol is global or not.
1866 symbol_is_global() const
1867 { return this->symbol_is_global_; }
1869 // For a relocation against a global symbol, the global symbol.
1873 gold_assert(this->symbol_is_global_);
1874 return this->u_.global.symbol;
1877 // For a relocation against a local symbol, the defining object.
1878 Sized_relobj<32, big_endian>*
1881 gold_assert(!this->symbol_is_global_);
1882 return this->u_.local.relobj;
1885 // For a relocation against a local symbol, the local symbol index.
1889 gold_assert(!this->symbol_is_global_);
1890 return this->u_.local.index;
1894 // GOT offset of the entry to which this relocation is applied.
1895 unsigned int got_offset_;
1896 // Type of relocation.
1897 unsigned int r_type_;
1898 // Whether this relocation is against a global symbol.
1899 bool symbol_is_global_;
1900 // A global or local symbol.
1905 // For a global symbol, the symbol itself.
1910 // For a local symbol, the object defining object.
1911 Sized_relobj<32, big_endian>* relobj;
1912 // For a local symbol, the symbol index.
1918 // Symbol table of the output object.
1919 Symbol_table* symbol_table_;
1920 // Layout of the output object.
1922 // Static relocs to be applied to the GOT.
1923 std::vector<Static_reloc> static_relocs_;
1926 // Utilities for manipulating integers of up to 32-bits
1930 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1931 // an int32_t. NO_BITS must be between 1 to 32.
1932 template<int no_bits>
1933 static inline int32_t
1934 sign_extend(uint32_t bits)
1936 gold_assert(no_bits >= 0 && no_bits <= 32);
1938 return static_cast<int32_t>(bits);
1939 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1941 uint32_t top_bit = 1U << (no_bits - 1);
1942 int32_t as_signed = static_cast<int32_t>(bits);
1943 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1946 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1947 template<int no_bits>
1949 has_overflow(uint32_t bits)
1951 gold_assert(no_bits >= 0 && no_bits <= 32);
1954 int32_t max = (1 << (no_bits - 1)) - 1;
1955 int32_t min = -(1 << (no_bits - 1));
1956 int32_t as_signed = static_cast<int32_t>(bits);
1957 return as_signed > max || as_signed < min;
1960 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1961 // fits in the given number of bits as either a signed or unsigned value.
1962 // For example, has_signed_unsigned_overflow<8> would check
1963 // -128 <= bits <= 255
1964 template<int no_bits>
1966 has_signed_unsigned_overflow(uint32_t bits)
1968 gold_assert(no_bits >= 2 && no_bits <= 32);
1971 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1972 int32_t min = -(1 << (no_bits - 1));
1973 int32_t as_signed = static_cast<int32_t>(bits);
1974 return as_signed > max || as_signed < min;
1977 // Select bits from A and B using bits in MASK. For each n in [0..31],
1978 // the n-th bit in the result is chosen from the n-th bits of A and B.
1979 // A zero selects A and a one selects B.
1980 static inline uint32_t
1981 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1982 { return (a & ~mask) | (b & mask); }
1985 template<bool big_endian>
1986 class Target_arm : public Sized_target<32, big_endian>
1989 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1992 // When were are relocating a stub, we pass this as the relocation number.
1993 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1996 : Sized_target<32, big_endian>(&arm_info),
1997 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1998 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
1999 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
2000 stub_tables_(), stub_factory_(Stub_factory::get_instance()),
2001 may_use_blx_(false), should_force_pic_veneer_(false),
2002 arm_input_section_map_(), attributes_section_data_(NULL),
2003 fix_cortex_a8_(false), cortex_a8_relocs_info_()
2006 // Whether we can use BLX.
2009 { return this->may_use_blx_; }
2011 // Set use-BLX flag.
2013 set_may_use_blx(bool value)
2014 { this->may_use_blx_ = value; }
2016 // Whether we force PCI branch veneers.
2018 should_force_pic_veneer() const
2019 { return this->should_force_pic_veneer_; }
2021 // Set PIC veneer flag.
2023 set_should_force_pic_veneer(bool value)
2024 { this->should_force_pic_veneer_ = value; }
2026 // Whether we use THUMB-2 instructions.
2028 using_thumb2() const
2030 Object_attribute* attr =
2031 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2032 int arch = attr->int_value();
2033 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
2036 // Whether we use THUMB/THUMB-2 instructions only.
2038 using_thumb_only() const
2040 Object_attribute* attr =
2041 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2042 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
2043 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
2045 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
2046 return attr->int_value() == 'M';
2049 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
2051 may_use_arm_nop() const
2053 Object_attribute* attr =
2054 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2055 int arch = attr->int_value();
2056 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2057 || arch == elfcpp::TAG_CPU_ARCH_V6K
2058 || arch == elfcpp::TAG_CPU_ARCH_V7
2059 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2062 // Whether we have THUMB-2 NOP.W instruction.
2064 may_use_thumb2_nop() const
2066 Object_attribute* attr =
2067 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2068 int arch = attr->int_value();
2069 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2070 || arch == elfcpp::TAG_CPU_ARCH_V7
2071 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2074 // Process the relocations to determine unreferenced sections for
2075 // garbage collection.
2077 gc_process_relocs(Symbol_table* symtab,
2079 Sized_relobj<32, big_endian>* object,
2080 unsigned int data_shndx,
2081 unsigned int sh_type,
2082 const unsigned char* prelocs,
2084 Output_section* output_section,
2085 bool needs_special_offset_handling,
2086 size_t local_symbol_count,
2087 const unsigned char* plocal_symbols);
2089 // Scan the relocations to look for symbol adjustments.
2091 scan_relocs(Symbol_table* symtab,
2093 Sized_relobj<32, big_endian>* object,
2094 unsigned int data_shndx,
2095 unsigned int sh_type,
2096 const unsigned char* prelocs,
2098 Output_section* output_section,
2099 bool needs_special_offset_handling,
2100 size_t local_symbol_count,
2101 const unsigned char* plocal_symbols);
2103 // Finalize the sections.
2105 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2107 // Return the value to use for a dynamic symbol which requires special
2110 do_dynsym_value(const Symbol*) const;
2112 // Relocate a section.
2114 relocate_section(const Relocate_info<32, big_endian>*,
2115 unsigned int sh_type,
2116 const unsigned char* prelocs,
2118 Output_section* output_section,
2119 bool needs_special_offset_handling,
2120 unsigned char* view,
2121 Arm_address view_address,
2122 section_size_type view_size,
2123 const Reloc_symbol_changes*);
2125 // Scan the relocs during a relocatable link.
2127 scan_relocatable_relocs(Symbol_table* symtab,
2129 Sized_relobj<32, big_endian>* object,
2130 unsigned int data_shndx,
2131 unsigned int sh_type,
2132 const unsigned char* prelocs,
2134 Output_section* output_section,
2135 bool needs_special_offset_handling,
2136 size_t local_symbol_count,
2137 const unsigned char* plocal_symbols,
2138 Relocatable_relocs*);
2140 // Relocate a section during a relocatable link.
2142 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
2143 unsigned int sh_type,
2144 const unsigned char* prelocs,
2146 Output_section* output_section,
2147 off_t offset_in_output_section,
2148 const Relocatable_relocs*,
2149 unsigned char* view,
2150 Arm_address view_address,
2151 section_size_type view_size,
2152 unsigned char* reloc_view,
2153 section_size_type reloc_view_size);
2155 // Return whether SYM is defined by the ABI.
2157 do_is_defined_by_abi(Symbol* sym) const
2158 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
2160 // Return whether there is a GOT section.
2162 has_got_section() const
2163 { return this->got_ != NULL; }
2165 // Return the size of the GOT section.
2169 gold_assert(this->got_ != NULL);
2170 return this->got_->data_size();
2173 // Map platform-specific reloc types
2175 get_real_reloc_type (unsigned int r_type);
2178 // Methods to support stub-generations.
2181 // Return the stub factory
2183 stub_factory() const
2184 { return this->stub_factory_; }
2186 // Make a new Arm_input_section object.
2187 Arm_input_section<big_endian>*
2188 new_arm_input_section(Relobj*, unsigned int);
2190 // Find the Arm_input_section object corresponding to the SHNDX-th input
2191 // section of RELOBJ.
2192 Arm_input_section<big_endian>*
2193 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
2195 // Make a new Stub_table
2196 Stub_table<big_endian>*
2197 new_stub_table(Arm_input_section<big_endian>*);
2199 // Scan a section for stub generation.
2201 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2202 const unsigned char*, size_t, Output_section*,
2203 bool, const unsigned char*, Arm_address,
2208 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
2209 Output_section*, unsigned char*, Arm_address,
2212 // Get the default ARM target.
2213 static Target_arm<big_endian>*
2216 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2217 && parameters->target().is_big_endian() == big_endian);
2218 return static_cast<Target_arm<big_endian>*>(
2219 parameters->sized_target<32, big_endian>());
2222 // Whether NAME belongs to a mapping symbol.
2224 is_mapping_symbol_name(const char* name)
2228 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2229 && (name[2] == '\0' || name[2] == '.'));
2232 // Whether we work around the Cortex-A8 erratum.
2234 fix_cortex_a8() const
2235 { return this->fix_cortex_a8_; }
2237 // Whether we fix R_ARM_V4BX relocation.
2239 // 1 - replace with MOV instruction (armv4 target)
2240 // 2 - make interworking veneer (>= armv4t targets only)
2241 General_options::Fix_v4bx
2243 { return parameters->options().fix_v4bx(); }
2245 // Scan a span of THUMB code section for Cortex-A8 erratum.
2247 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2248 section_size_type, section_size_type,
2249 const unsigned char*, Arm_address);
2251 // Apply Cortex-A8 workaround to a branch.
2253 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2254 unsigned char*, Arm_address);
2257 // Make an ELF object.
2259 do_make_elf_object(const std::string&, Input_file*, off_t,
2260 const elfcpp::Ehdr<32, big_endian>& ehdr);
2263 do_make_elf_object(const std::string&, Input_file*, off_t,
2264 const elfcpp::Ehdr<32, !big_endian>&)
2265 { gold_unreachable(); }
2268 do_make_elf_object(const std::string&, Input_file*, off_t,
2269 const elfcpp::Ehdr<64, false>&)
2270 { gold_unreachable(); }
2273 do_make_elf_object(const std::string&, Input_file*, off_t,
2274 const elfcpp::Ehdr<64, true>&)
2275 { gold_unreachable(); }
2277 // Make an output section.
2279 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2280 elfcpp::Elf_Xword flags)
2281 { return new Arm_output_section<big_endian>(name, type, flags); }
2284 do_adjust_elf_header(unsigned char* view, int len) const;
2286 // We only need to generate stubs, and hence perform relaxation if we are
2287 // not doing relocatable linking.
2289 do_may_relax() const
2290 { return !parameters->options().relocatable(); }
2293 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
2295 // Determine whether an object attribute tag takes an integer, a
2298 do_attribute_arg_type(int tag) const;
2300 // Reorder tags during output.
2302 do_attributes_order(int num) const;
2304 // This is called when the target is selected as the default.
2306 do_select_as_default_target()
2308 // No locking is required since there should only be one default target.
2309 // We cannot have both the big-endian and little-endian ARM targets
2311 gold_assert(arm_reloc_property_table == NULL);
2312 arm_reloc_property_table = new Arm_reloc_property_table();
2316 // The class which scans relocations.
2321 : issued_non_pic_error_(false)
2325 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
2326 Sized_relobj<32, big_endian>* object,
2327 unsigned int data_shndx,
2328 Output_section* output_section,
2329 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2330 const elfcpp::Sym<32, big_endian>& lsym);
2333 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
2334 Sized_relobj<32, big_endian>* object,
2335 unsigned int data_shndx,
2336 Output_section* output_section,
2337 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2341 local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2342 Sized_relobj<32, big_endian>* ,
2345 const elfcpp::Rel<32, big_endian>& ,
2347 const elfcpp::Sym<32, big_endian>&)
2351 global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2352 Sized_relobj<32, big_endian>* ,
2355 const elfcpp::Rel<32, big_endian>& ,
2356 unsigned int , Symbol*)
2361 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
2362 unsigned int r_type);
2365 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
2366 unsigned int r_type, Symbol*);
2369 check_non_pic(Relobj*, unsigned int r_type);
2371 // Almost identical to Symbol::needs_plt_entry except that it also
2372 // handles STT_ARM_TFUNC.
2374 symbol_needs_plt_entry(const Symbol* sym)
2376 // An undefined symbol from an executable does not need a PLT entry.
2377 if (sym->is_undefined() && !parameters->options().shared())
2380 return (!parameters->doing_static_link()
2381 && (sym->type() == elfcpp::STT_FUNC
2382 || sym->type() == elfcpp::STT_ARM_TFUNC)
2383 && (sym->is_from_dynobj()
2384 || sym->is_undefined()
2385 || sym->is_preemptible()));
2388 // Whether we have issued an error about a non-PIC compilation.
2389 bool issued_non_pic_error_;
2392 // The class which implements relocation.
2402 // Return whether the static relocation needs to be applied.
2404 should_apply_static_reloc(const Sized_symbol<32>* gsym,
2407 Output_section* output_section);
2409 // Do a relocation. Return false if the caller should not issue
2410 // any warnings about this relocation.
2412 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2413 Output_section*, size_t relnum,
2414 const elfcpp::Rel<32, big_endian>&,
2415 unsigned int r_type, const Sized_symbol<32>*,
2416 const Symbol_value<32>*,
2417 unsigned char*, Arm_address,
2420 // Return whether we want to pass flag NON_PIC_REF for this
2421 // reloc. This means the relocation type accesses a symbol not via
2424 reloc_is_non_pic (unsigned int r_type)
2428 // These relocation types reference GOT or PLT entries explicitly.
2429 case elfcpp::R_ARM_GOT_BREL:
2430 case elfcpp::R_ARM_GOT_ABS:
2431 case elfcpp::R_ARM_GOT_PREL:
2432 case elfcpp::R_ARM_GOT_BREL12:
2433 case elfcpp::R_ARM_PLT32_ABS:
2434 case elfcpp::R_ARM_TLS_GD32:
2435 case elfcpp::R_ARM_TLS_LDM32:
2436 case elfcpp::R_ARM_TLS_IE32:
2437 case elfcpp::R_ARM_TLS_IE12GP:
2439 // These relocate types may use PLT entries.
2440 case elfcpp::R_ARM_CALL:
2441 case elfcpp::R_ARM_THM_CALL:
2442 case elfcpp::R_ARM_JUMP24:
2443 case elfcpp::R_ARM_THM_JUMP24:
2444 case elfcpp::R_ARM_THM_JUMP19:
2445 case elfcpp::R_ARM_PLT32:
2446 case elfcpp::R_ARM_THM_XPC22:
2455 // Do a TLS relocation.
2456 inline typename Arm_relocate_functions<big_endian>::Status
2457 relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
2458 size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
2459 const Sized_symbol<32>*, const Symbol_value<32>*,
2460 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
2465 // A class which returns the size required for a relocation type,
2466 // used while scanning relocs during a relocatable link.
2467 class Relocatable_size_for_reloc
2471 get_size_for_reloc(unsigned int, Relobj*);
2474 // Adjust TLS relocation type based on the options and whether this
2475 // is a local symbol.
2476 static tls::Tls_optimization
2477 optimize_tls_reloc(bool is_final, int r_type);
2479 // Get the GOT section, creating it if necessary.
2480 Arm_output_data_got<big_endian>*
2481 got_section(Symbol_table*, Layout*);
2483 // Get the GOT PLT section.
2485 got_plt_section() const
2487 gold_assert(this->got_plt_ != NULL);
2488 return this->got_plt_;
2491 // Create a PLT entry for a global symbol.
2493 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2495 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2497 define_tls_base_symbol(Symbol_table*, Layout*);
2499 // Create a GOT entry for the TLS module index.
2501 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2502 Sized_relobj<32, big_endian>* object);
2504 // Get the PLT section.
2505 const Output_data_plt_arm<big_endian>*
2508 gold_assert(this->plt_ != NULL);
2512 // Get the dynamic reloc section, creating it if necessary.
2514 rel_dyn_section(Layout*);
2516 // Get the section to use for TLS_DESC relocations.
2518 rel_tls_desc_section(Layout*) const;
2520 // Return true if the symbol may need a COPY relocation.
2521 // References from an executable object to non-function symbols
2522 // defined in a dynamic object may need a COPY relocation.
2524 may_need_copy_reloc(Symbol* gsym)
2526 return (gsym->type() != elfcpp::STT_ARM_TFUNC
2527 && gsym->may_need_copy_reloc());
2530 // Add a potential copy relocation.
2532 copy_reloc(Symbol_table* symtab, Layout* layout,
2533 Sized_relobj<32, big_endian>* object,
2534 unsigned int shndx, Output_section* output_section,
2535 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2537 this->copy_relocs_.copy_reloc(symtab, layout,
2538 symtab->get_sized_symbol<32>(sym),
2539 object, shndx, output_section, reloc,
2540 this->rel_dyn_section(layout));
2543 // Whether two EABI versions are compatible.
2545 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2547 // Merge processor-specific flags from input object and those in the ELF
2548 // header of the output.
2550 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2552 // Get the secondary compatible architecture.
2554 get_secondary_compatible_arch(const Attributes_section_data*);
2556 // Set the secondary compatible architecture.
2558 set_secondary_compatible_arch(Attributes_section_data*, int);
2561 tag_cpu_arch_combine(const char*, int, int*, int, int);
2563 // Helper to print AEABI enum tag value.
2565 aeabi_enum_name(unsigned int);
2567 // Return string value for TAG_CPU_name.
2569 tag_cpu_name_value(unsigned int);
2571 // Merge object attributes from input object and those in the output.
2573 merge_object_attributes(const char*, const Attributes_section_data*);
2575 // Helper to get an AEABI object attribute
2577 get_aeabi_object_attribute(int tag) const
2579 Attributes_section_data* pasd = this->attributes_section_data_;
2580 gold_assert(pasd != NULL);
2581 Object_attribute* attr =
2582 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2583 gold_assert(attr != NULL);
2588 // Methods to support stub-generations.
2591 // Group input sections for stub generation.
2593 group_sections(Layout*, section_size_type, bool);
2595 // Scan a relocation for stub generation.
2597 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2598 const Sized_symbol<32>*, unsigned int,
2599 const Symbol_value<32>*,
2600 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
2602 // Scan a relocation section for stub.
2603 template<int sh_type>
2605 scan_reloc_section_for_stubs(
2606 const Relocate_info<32, big_endian>* relinfo,
2607 const unsigned char* prelocs,
2609 Output_section* output_section,
2610 bool needs_special_offset_handling,
2611 const unsigned char* view,
2612 elfcpp::Elf_types<32>::Elf_Addr view_address,
2615 // Fix .ARM.exidx section coverage.
2617 fix_exidx_coverage(Layout*, Arm_output_section<big_endian>*, Symbol_table*);
2619 // Functors for STL set.
2620 struct output_section_address_less_than
2623 operator()(const Output_section* s1, const Output_section* s2) const
2624 { return s1->address() < s2->address(); }
2627 // Information about this specific target which we pass to the
2628 // general Target structure.
2629 static const Target::Target_info arm_info;
2631 // The types of GOT entries needed for this platform.
2634 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2635 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
2636 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
2637 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
2638 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
2641 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2643 // Map input section to Arm_input_section.
2644 typedef Unordered_map<Section_id,
2645 Arm_input_section<big_endian>*,
2647 Arm_input_section_map;
2649 // Map output addresses to relocs for Cortex-A8 erratum.
2650 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2651 Cortex_a8_relocs_info;
2654 Arm_output_data_got<big_endian>* got_;
2656 Output_data_plt_arm<big_endian>* plt_;
2657 // The GOT PLT section.
2658 Output_data_space* got_plt_;
2659 // The dynamic reloc section.
2660 Reloc_section* rel_dyn_;
2661 // Relocs saved to avoid a COPY reloc.
2662 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2663 // Space for variables copied with a COPY reloc.
2664 Output_data_space* dynbss_;
2665 // Offset of the GOT entry for the TLS module index.
2666 unsigned int got_mod_index_offset_;
2667 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2668 bool tls_base_symbol_defined_;
2669 // Vector of Stub_tables created.
2670 Stub_table_list stub_tables_;
2672 const Stub_factory &stub_factory_;
2673 // Whether we can use BLX.
2675 // Whether we force PIC branch veneers.
2676 bool should_force_pic_veneer_;
2677 // Map for locating Arm_input_sections.
2678 Arm_input_section_map arm_input_section_map_;
2679 // Attributes section data in output.
2680 Attributes_section_data* attributes_section_data_;
2681 // Whether we want to fix code for Cortex-A8 erratum.
2682 bool fix_cortex_a8_;
2683 // Map addresses to relocs for Cortex-A8 erratum.
2684 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2687 template<bool big_endian>
2688 const Target::Target_info Target_arm<big_endian>::arm_info =
2691 big_endian, // is_big_endian
2692 elfcpp::EM_ARM, // machine_code
2693 false, // has_make_symbol
2694 false, // has_resolve
2695 false, // has_code_fill
2696 true, // is_default_stack_executable
2698 "/usr/lib/libc.so.1", // dynamic_linker
2699 0x8000, // default_text_segment_address
2700 0x1000, // abi_pagesize (overridable by -z max-page-size)
2701 0x1000, // common_pagesize (overridable by -z common-page-size)
2702 elfcpp::SHN_UNDEF, // small_common_shndx
2703 elfcpp::SHN_UNDEF, // large_common_shndx
2704 0, // small_common_section_flags
2705 0, // large_common_section_flags
2706 ".ARM.attributes", // attributes_section
2707 "aeabi" // attributes_vendor
2710 // Arm relocate functions class
2713 template<bool big_endian>
2714 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2719 STATUS_OKAY, // No error during relocation.
2720 STATUS_OVERFLOW, // Relocation oveflow.
2721 STATUS_BAD_RELOC // Relocation cannot be applied.
2725 typedef Relocate_functions<32, big_endian> Base;
2726 typedef Arm_relocate_functions<big_endian> This;
2728 // Encoding of imm16 argument for movt and movw ARM instructions
2731 // imm16 := imm4 | imm12
2733 // 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
2734 // +-------+---------------+-------+-------+-----------------------+
2735 // | | |imm4 | |imm12 |
2736 // +-------+---------------+-------+-------+-----------------------+
2738 // Extract the relocation addend from VAL based on the ARM
2739 // instruction encoding described above.
2740 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2741 extract_arm_movw_movt_addend(
2742 typename elfcpp::Swap<32, big_endian>::Valtype val)
2744 // According to the Elf ABI for ARM Architecture the immediate
2745 // field is sign-extended to form the addend.
2746 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2749 // Insert X into VAL based on the ARM instruction encoding described
2751 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2752 insert_val_arm_movw_movt(
2753 typename elfcpp::Swap<32, big_endian>::Valtype val,
2754 typename elfcpp::Swap<32, big_endian>::Valtype x)
2758 val |= (x & 0xf000) << 4;
2762 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2765 // imm16 := imm4 | i | imm3 | imm8
2767 // 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
2768 // +---------+-+-----------+-------++-+-----+-------+---------------+
2769 // | |i| |imm4 || |imm3 | |imm8 |
2770 // +---------+-+-----------+-------++-+-----+-------+---------------+
2772 // Extract the relocation addend from VAL based on the Thumb2
2773 // instruction encoding described above.
2774 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2775 extract_thumb_movw_movt_addend(
2776 typename elfcpp::Swap<32, big_endian>::Valtype val)
2778 // According to the Elf ABI for ARM Architecture the immediate
2779 // field is sign-extended to form the addend.
2780 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2781 | ((val >> 15) & 0x0800)
2782 | ((val >> 4) & 0x0700)
2786 // Insert X into VAL based on the Thumb2 instruction encoding
2788 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2789 insert_val_thumb_movw_movt(
2790 typename elfcpp::Swap<32, big_endian>::Valtype val,
2791 typename elfcpp::Swap<32, big_endian>::Valtype x)
2794 val |= (x & 0xf000) << 4;
2795 val |= (x & 0x0800) << 15;
2796 val |= (x & 0x0700) << 4;
2797 val |= (x & 0x00ff);
2801 // Calculate the smallest constant Kn for the specified residual.
2802 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2804 calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
2810 // Determine the most significant bit in the residual and
2811 // align the resulting value to a 2-bit boundary.
2812 for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
2814 // The desired shift is now (msb - 6), or zero, whichever
2816 return (((msb - 6) < 0) ? 0 : (msb - 6));
2819 // Calculate the final residual for the specified group index.
2820 // If the passed group index is less than zero, the method will return
2821 // the value of the specified residual without any change.
2822 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2823 static typename elfcpp::Swap<32, big_endian>::Valtype
2824 calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2827 for (int n = 0; n <= group; n++)
2829 // Calculate which part of the value to mask.
2830 uint32_t shift = calc_grp_kn(residual);
2831 // Calculate the residual for the next time around.
2832 residual &= ~(residual & (0xff << shift));
2838 // Calculate the value of Gn for the specified group index.
2839 // We return it in the form of an encoded constant-and-rotation.
2840 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2841 static typename elfcpp::Swap<32, big_endian>::Valtype
2842 calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2845 typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
2848 for (int n = 0; n <= group; n++)
2850 // Calculate which part of the value to mask.
2851 shift = calc_grp_kn(residual);
2852 // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
2853 gn = residual & (0xff << shift);
2854 // Calculate the residual for the next time around.
2857 // Return Gn in the form of an encoded constant-and-rotation.
2858 return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
2862 // Handle ARM long branches.
2863 static typename This::Status
2864 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2865 unsigned char *, const Sized_symbol<32>*,
2866 const Arm_relobj<big_endian>*, unsigned int,
2867 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2869 // Handle THUMB long branches.
2870 static typename This::Status
2871 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2872 unsigned char *, const Sized_symbol<32>*,
2873 const Arm_relobj<big_endian>*, unsigned int,
2874 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2877 // Return the branch offset of a 32-bit THUMB branch.
2878 static inline int32_t
2879 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2881 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2882 // involving the J1 and J2 bits.
2883 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2884 uint32_t upper = upper_insn & 0x3ffU;
2885 uint32_t lower = lower_insn & 0x7ffU;
2886 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2887 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2888 uint32_t i1 = j1 ^ s ? 0 : 1;
2889 uint32_t i2 = j2 ^ s ? 0 : 1;
2891 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2892 | (upper << 12) | (lower << 1));
2895 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2896 // UPPER_INSN is the original upper instruction of the branch. Caller is
2897 // responsible for overflow checking and BLX offset adjustment.
2898 static inline uint16_t
2899 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2901 uint32_t s = offset < 0 ? 1 : 0;
2902 uint32_t bits = static_cast<uint32_t>(offset);
2903 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2906 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2907 // LOWER_INSN is the original lower instruction of the branch. Caller is
2908 // responsible for overflow checking and BLX offset adjustment.
2909 static inline uint16_t
2910 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2912 uint32_t s = offset < 0 ? 1 : 0;
2913 uint32_t bits = static_cast<uint32_t>(offset);
2914 return ((lower_insn & ~0x2fffU)
2915 | ((((bits >> 23) & 1) ^ !s) << 13)
2916 | ((((bits >> 22) & 1) ^ !s) << 11)
2917 | ((bits >> 1) & 0x7ffU));
2920 // Return the branch offset of a 32-bit THUMB conditional branch.
2921 static inline int32_t
2922 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2924 uint32_t s = (upper_insn & 0x0400U) >> 10;
2925 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2926 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2927 uint32_t lower = (lower_insn & 0x07ffU);
2928 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2930 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2933 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2934 // instruction. UPPER_INSN is the original upper instruction of the branch.
2935 // Caller is responsible for overflow checking.
2936 static inline uint16_t
2937 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2939 uint32_t s = offset < 0 ? 1 : 0;
2940 uint32_t bits = static_cast<uint32_t>(offset);
2941 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2944 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2945 // instruction. LOWER_INSN is the original lower instruction of the branch.
2946 // Caller is reponsible for overflow checking.
2947 static inline uint16_t
2948 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2950 uint32_t bits = static_cast<uint32_t>(offset);
2951 uint32_t j2 = (bits & 0x00080000U) >> 19;
2952 uint32_t j1 = (bits & 0x00040000U) >> 18;
2953 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2955 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2958 // R_ARM_ABS8: S + A
2959 static inline typename This::Status
2960 abs8(unsigned char *view,
2961 const Sized_relobj<32, big_endian>* object,
2962 const Symbol_value<32>* psymval)
2964 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2965 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2966 Valtype* wv = reinterpret_cast<Valtype*>(view);
2967 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2968 Reltype addend = utils::sign_extend<8>(val);
2969 Reltype x = psymval->value(object, addend);
2970 val = utils::bit_select(val, x, 0xffU);
2971 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2972 return (utils::has_signed_unsigned_overflow<8>(x)
2973 ? This::STATUS_OVERFLOW
2974 : This::STATUS_OKAY);
2977 // R_ARM_THM_ABS5: S + A
2978 static inline typename This::Status
2979 thm_abs5(unsigned char *view,
2980 const Sized_relobj<32, big_endian>* object,
2981 const Symbol_value<32>* psymval)
2983 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2984 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2985 Valtype* wv = reinterpret_cast<Valtype*>(view);
2986 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2987 Reltype addend = (val & 0x7e0U) >> 6;
2988 Reltype x = psymval->value(object, addend);
2989 val = utils::bit_select(val, x << 6, 0x7e0U);
2990 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2991 return (utils::has_overflow<5>(x)
2992 ? This::STATUS_OVERFLOW
2993 : This::STATUS_OKAY);
2996 // R_ARM_ABS12: S + A
2997 static inline typename This::Status
2998 abs12(unsigned char *view,
2999 const Sized_relobj<32, big_endian>* object,
3000 const Symbol_value<32>* psymval)
3002 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3003 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3004 Valtype* wv = reinterpret_cast<Valtype*>(view);
3005 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3006 Reltype addend = val & 0x0fffU;
3007 Reltype x = psymval->value(object, addend);
3008 val = utils::bit_select(val, x, 0x0fffU);
3009 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3010 return (utils::has_overflow<12>(x)
3011 ? This::STATUS_OVERFLOW
3012 : This::STATUS_OKAY);
3015 // R_ARM_ABS16: S + A
3016 static inline typename This::Status
3017 abs16(unsigned char *view,
3018 const Sized_relobj<32, big_endian>* object,
3019 const Symbol_value<32>* psymval)
3021 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3022 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3023 Valtype* wv = reinterpret_cast<Valtype*>(view);
3024 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3025 Reltype addend = utils::sign_extend<16>(val);
3026 Reltype x = psymval->value(object, addend);
3027 val = utils::bit_select(val, x, 0xffffU);
3028 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3029 return (utils::has_signed_unsigned_overflow<16>(x)
3030 ? This::STATUS_OVERFLOW
3031 : This::STATUS_OKAY);
3034 // R_ARM_ABS32: (S + A) | T
3035 static inline typename This::Status
3036 abs32(unsigned char *view,
3037 const Sized_relobj<32, big_endian>* object,
3038 const Symbol_value<32>* psymval,
3039 Arm_address thumb_bit)
3041 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3042 Valtype* wv = reinterpret_cast<Valtype*>(view);
3043 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3044 Valtype x = psymval->value(object, addend) | thumb_bit;
3045 elfcpp::Swap<32, big_endian>::writeval(wv, x);
3046 return This::STATUS_OKAY;
3049 // R_ARM_REL32: (S + A) | T - P
3050 static inline typename This::Status
3051 rel32(unsigned char *view,
3052 const Sized_relobj<32, big_endian>* object,
3053 const Symbol_value<32>* psymval,
3054 Arm_address address,
3055 Arm_address thumb_bit)
3057 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3058 Valtype* wv = reinterpret_cast<Valtype*>(view);
3059 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3060 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3061 elfcpp::Swap<32, big_endian>::writeval(wv, x);
3062 return This::STATUS_OKAY;
3065 // R_ARM_THM_JUMP24: (S + A) | T - P
3066 static typename This::Status
3067 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
3068 const Symbol_value<32>* psymval, Arm_address address,
3069 Arm_address thumb_bit);
3071 // R_ARM_THM_JUMP6: S + A – P
3072 static inline typename This::Status
3073 thm_jump6(unsigned char *view,
3074 const Sized_relobj<32, big_endian>* object,
3075 const Symbol_value<32>* psymval,
3076 Arm_address address)
3078 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3079 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3080 Valtype* wv = reinterpret_cast<Valtype*>(view);
3081 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3082 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
3083 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
3084 Reltype x = (psymval->value(object, addend) - address);
3085 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
3086 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3087 // CZB does only forward jumps.
3088 return ((x > 0x007e)
3089 ? This::STATUS_OVERFLOW
3090 : This::STATUS_OKAY);
3093 // R_ARM_THM_JUMP8: S + A – P
3094 static inline typename This::Status
3095 thm_jump8(unsigned char *view,
3096 const Sized_relobj<32, big_endian>* object,
3097 const Symbol_value<32>* psymval,
3098 Arm_address address)
3100 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3101 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3102 Valtype* wv = reinterpret_cast<Valtype*>(view);
3103 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3104 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
3105 Reltype x = (psymval->value(object, addend) - address);
3106 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
3107 return (utils::has_overflow<8>(x)
3108 ? This::STATUS_OVERFLOW
3109 : This::STATUS_OKAY);
3112 // R_ARM_THM_JUMP11: S + A – P
3113 static inline typename This::Status
3114 thm_jump11(unsigned char *view,
3115 const Sized_relobj<32, big_endian>* object,
3116 const Symbol_value<32>* psymval,
3117 Arm_address address)
3119 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3120 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3121 Valtype* wv = reinterpret_cast<Valtype*>(view);
3122 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3123 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
3124 Reltype x = (psymval->value(object, addend) - address);
3125 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
3126 return (utils::has_overflow<11>(x)
3127 ? This::STATUS_OVERFLOW
3128 : This::STATUS_OKAY);
3131 // R_ARM_BASE_PREL: B(S) + A - P
3132 static inline typename This::Status
3133 base_prel(unsigned char* view,
3135 Arm_address address)
3137 Base::rel32(view, origin - address);
3141 // R_ARM_BASE_ABS: B(S) + A
3142 static inline typename This::Status
3143 base_abs(unsigned char* view,
3146 Base::rel32(view, origin);
3150 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3151 static inline typename This::Status
3152 got_brel(unsigned char* view,
3153 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
3155 Base::rel32(view, got_offset);
3156 return This::STATUS_OKAY;
3159 // R_ARM_GOT_PREL: GOT(S) + A - P
3160 static inline typename This::Status
3161 got_prel(unsigned char *view,
3162 Arm_address got_entry,
3163 Arm_address address)
3165 Base::rel32(view, got_entry - address);
3166 return This::STATUS_OKAY;
3169 // R_ARM_PREL: (S + A) | T - P
3170 static inline typename This::Status
3171 prel31(unsigned char *view,
3172 const Sized_relobj<32, big_endian>* object,
3173 const Symbol_value<32>* psymval,
3174 Arm_address address,
3175 Arm_address thumb_bit)
3177 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3178 Valtype* wv = reinterpret_cast<Valtype*>(view);
3179 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3180 Valtype addend = utils::sign_extend<31>(val);
3181 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3182 val = utils::bit_select(val, x, 0x7fffffffU);
3183 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3184 return (utils::has_overflow<31>(x) ?
3185 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3188 // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
3189 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3190 // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3191 // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
3192 static inline typename This::Status
3193 movw(unsigned char* view,
3194 const Sized_relobj<32, big_endian>* object,
3195 const Symbol_value<32>* psymval,
3196 Arm_address relative_address_base,
3197 Arm_address thumb_bit,
3198 bool check_overflow)
3200 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3201 Valtype* wv = reinterpret_cast<Valtype*>(view);
3202 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3203 Valtype addend = This::extract_arm_movw_movt_addend(val);
3204 Valtype x = ((psymval->value(object, addend) | thumb_bit)
3205 - relative_address_base);
3206 val = This::insert_val_arm_movw_movt(val, x);
3207 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3208 return ((check_overflow && utils::has_overflow<16>(x))
3209 ? This::STATUS_OVERFLOW
3210 : This::STATUS_OKAY);
3213 // R_ARM_MOVT_ABS: S + A (relative address base is 0)
3214 // R_ARM_MOVT_PREL: S + A - P
3215 // R_ARM_MOVT_BREL: S + A - B(S)
3216 static inline typename This::Status
3217 movt(unsigned char* view,
3218 const Sized_relobj<32, big_endian>* object,
3219 const Symbol_value<32>* psymval,
3220 Arm_address relative_address_base)
3222 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3223 Valtype* wv = reinterpret_cast<Valtype*>(view);
3224 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3225 Valtype addend = This::extract_arm_movw_movt_addend(val);
3226 Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3227 val = This::insert_val_arm_movw_movt(val, x);
3228 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3229 // FIXME: IHI0044D says that we should check for overflow.
3230 return This::STATUS_OKAY;
3233 // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
3234 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3235 // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3236 // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
3237 static inline typename This::Status
3238 thm_movw(unsigned char *view,
3239 const Sized_relobj<32, big_endian>* object,
3240 const Symbol_value<32>* psymval,
3241 Arm_address relative_address_base,
3242 Arm_address thumb_bit,
3243 bool check_overflow)
3245 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3246 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3247 Valtype* wv = reinterpret_cast<Valtype*>(view);
3248 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3249 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3250 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3252 (psymval->value(object, addend) | thumb_bit) - relative_address_base;
3253 val = This::insert_val_thumb_movw_movt(val, x);
3254 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3255 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3256 return ((check_overflow && utils::has_overflow<16>(x))
3257 ? This::STATUS_OVERFLOW
3258 : This::STATUS_OKAY);
3261 // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
3262 // R_ARM_THM_MOVT_PREL: S + A - P
3263 // R_ARM_THM_MOVT_BREL: S + A - B(S)
3264 static inline typename This::Status
3265 thm_movt(unsigned char* view,
3266 const Sized_relobj<32, big_endian>* object,
3267 const Symbol_value<32>* psymval,
3268 Arm_address relative_address_base)
3270 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3271 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3272 Valtype* wv = reinterpret_cast<Valtype*>(view);
3273 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3274 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3275 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3276 Reltype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3277 val = This::insert_val_thumb_movw_movt(val, x);
3278 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3279 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3280 return This::STATUS_OKAY;
3283 // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3284 static inline typename This::Status
3285 thm_alu11(unsigned char* view,
3286 const Sized_relobj<32, big_endian>* object,
3287 const Symbol_value<32>* psymval,
3288 Arm_address address,
3289 Arm_address thumb_bit)
3291 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3292 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3293 Valtype* wv = reinterpret_cast<Valtype*>(view);
3294 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3295 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3297 // 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
3298 // -----------------------------------------------------------------------
3299 // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
3300 // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
3301 // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
3302 // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
3303 // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
3304 // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
3306 // Determine a sign for the addend.
3307 const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
3308 || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3309 // Thumb2 addend encoding:
3310 // imm12 := i | imm3 | imm8
3311 int32_t addend = (insn & 0xff)
3312 | ((insn & 0x00007000) >> 4)
3313 | ((insn & 0x04000000) >> 15);
3314 // Apply a sign to the added.
3317 int32_t x = (psymval->value(object, addend) | thumb_bit)
3318 - (address & 0xfffffffc);
3319 Reltype val = abs(x);
3320 // Mask out the value and a distinct part of the ADD/SUB opcode
3321 // (bits 7:5 of opword).
3322 insn = (insn & 0xfb0f8f00)
3324 | ((val & 0x700) << 4)
3325 | ((val & 0x800) << 15);
3326 // Set the opcode according to whether the value to go in the
3327 // place is negative.
3331 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3332 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3333 return ((val > 0xfff) ?
3334 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3337 // R_ARM_THM_PC8: S + A - Pa (Thumb)
3338 static inline typename This::Status
3339 thm_pc8(unsigned char* view,
3340 const Sized_relobj<32, big_endian>* object,
3341 const Symbol_value<32>* psymval,
3342 Arm_address address)
3344 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3345 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3346 Valtype* wv = reinterpret_cast<Valtype*>(view);
3347 Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
3348 Reltype addend = ((insn & 0x00ff) << 2);
3349 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3350 Reltype val = abs(x);
3351 insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
3353 elfcpp::Swap<16, big_endian>::writeval(wv, insn);
3354 return ((val > 0x03fc)
3355 ? This::STATUS_OVERFLOW
3356 : This::STATUS_OKAY);
3359 // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3360 static inline typename This::Status
3361 thm_pc12(unsigned char* view,
3362 const Sized_relobj<32, big_endian>* object,
3363 const Symbol_value<32>* psymval,
3364 Arm_address address)
3366 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3367 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3368 Valtype* wv = reinterpret_cast<Valtype*>(view);
3369 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3370 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3371 // Determine a sign for the addend (positive if the U bit is 1).
3372 const int sign = (insn & 0x00800000) ? 1 : -1;
3373 int32_t addend = (insn & 0xfff);
3374 // Apply a sign to the added.
3377 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3378 Reltype val = abs(x);
3379 // Mask out and apply the value and the U bit.
3380 insn = (insn & 0xff7ff000) | (val & 0xfff);
3381 // Set the U bit according to whether the value to go in the
3382 // place is positive.
3386 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3387 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3388 return ((val > 0xfff) ?
3389 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3393 static inline typename This::Status
3394 v4bx(const Relocate_info<32, big_endian>* relinfo,
3395 unsigned char *view,
3396 const Arm_relobj<big_endian>* object,
3397 const Arm_address address,
3398 const bool is_interworking)
3401 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3402 Valtype* wv = reinterpret_cast<Valtype*>(view);
3403 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3405 // Ensure that we have a BX instruction.
3406 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3407 const uint32_t reg = (val & 0xf);
3408 if (is_interworking && reg != 0xf)
3410 Stub_table<big_endian>* stub_table =
3411 object->stub_table(relinfo->data_shndx);
3412 gold_assert(stub_table != NULL);
3414 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3415 gold_assert(stub != NULL);
3417 int32_t veneer_address =
3418 stub_table->address() + stub->offset() - 8 - address;
3419 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3420 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3421 // Replace with a branch to veneer (B <addr>)
3422 val = (val & 0xf0000000) | 0x0a000000
3423 | ((veneer_address >> 2) & 0x00ffffff);
3427 // Preserve Rm (lowest four bits) and the condition code
3428 // (highest four bits). Other bits encode MOV PC,Rm.
3429 val = (val & 0xf000000f) | 0x01a0f000;
3431 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3432 return This::STATUS_OKAY;
3435 // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3436 // R_ARM_ALU_PC_G0: ((S + A) | T) - P
3437 // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3438 // R_ARM_ALU_PC_G1: ((S + A) | T) - P
3439 // R_ARM_ALU_PC_G2: ((S + A) | T) - P
3440 // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3441 // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
3442 // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3443 // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
3444 // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
3445 static inline typename This::Status
3446 arm_grp_alu(unsigned char* view,
3447 const Sized_relobj<32, big_endian>* object,
3448 const Symbol_value<32>* psymval,
3450 Arm_address address,
3451 Arm_address thumb_bit,
3452 bool check_overflow)
3454 gold_assert(group >= 0 && group < 3);
3455 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3456 Valtype* wv = reinterpret_cast<Valtype*>(view);
3457 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3459 // ALU group relocations are allowed only for the ADD/SUB instructions.
3460 // (0x00800000 - ADD, 0x00400000 - SUB)
3461 const Valtype opcode = insn & 0x01e00000;
3462 if (opcode != 0x00800000 && opcode != 0x00400000)
3463 return This::STATUS_BAD_RELOC;
3465 // Determine a sign for the addend.
3466 const int sign = (opcode == 0x00800000) ? 1 : -1;
3467 // shifter = rotate_imm * 2
3468 const uint32_t shifter = (insn & 0xf00) >> 7;
3469 // Initial addend value.
3470 int32_t addend = insn & 0xff;
3471 // Rotate addend right by shifter.
3472 addend = (addend >> shifter) | (addend << (32 - shifter));
3473 // Apply a sign to the added.
3476 int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
3477 Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
3478 // Check for overflow if required
3480 && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
3481 return This::STATUS_OVERFLOW;
3483 // Mask out the value and the ADD/SUB part of the opcode; take care
3484 // not to destroy the S bit.
3486 // Set the opcode according to whether the value to go in the
3487 // place is negative.
3488 insn |= ((x < 0) ? 0x00400000 : 0x00800000);
3489 // Encode the offset (encoded Gn).
3492 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3493 return This::STATUS_OKAY;
3496 // R_ARM_LDR_PC_G0: S + A - P
3497 // R_ARM_LDR_PC_G1: S + A - P
3498 // R_ARM_LDR_PC_G2: S + A - P
3499 // R_ARM_LDR_SB_G0: S + A - B(S)
3500 // R_ARM_LDR_SB_G1: S + A - B(S)
3501 // R_ARM_LDR_SB_G2: S + A - B(S)
3502 static inline typename This::Status
3503 arm_grp_ldr(unsigned char* view,
3504 const Sized_relobj<32, big_endian>* object,
3505 const Symbol_value<32>* psymval,
3507 Arm_address address)
3509 gold_assert(group >= 0 && group < 3);
3510 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3511 Valtype* wv = reinterpret_cast<Valtype*>(view);
3512 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3514 const int sign = (insn & 0x00800000) ? 1 : -1;
3515 int32_t addend = (insn & 0xfff) * sign;
3516 int32_t x = (psymval->value(object, addend) - address);
3517 // Calculate the relevant G(n-1) value to obtain this stage residual.
3519 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3520 if (residual >= 0x1000)
3521 return This::STATUS_OVERFLOW;
3523 // Mask out the value and U bit.
3525 // Set the U bit for non-negative values.
3530 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3531 return This::STATUS_OKAY;
3534 // R_ARM_LDRS_PC_G0: S + A - P
3535 // R_ARM_LDRS_PC_G1: S + A - P
3536 // R_ARM_LDRS_PC_G2: S + A - P
3537 // R_ARM_LDRS_SB_G0: S + A - B(S)
3538 // R_ARM_LDRS_SB_G1: S + A - B(S)
3539 // R_ARM_LDRS_SB_G2: S + A - B(S)
3540 static inline typename This::Status
3541 arm_grp_ldrs(unsigned char* view,
3542 const Sized_relobj<32, big_endian>* object,
3543 const Symbol_value<32>* psymval,
3545 Arm_address address)
3547 gold_assert(group >= 0 && group < 3);
3548 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3549 Valtype* wv = reinterpret_cast<Valtype*>(view);
3550 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3552 const int sign = (insn & 0x00800000) ? 1 : -1;
3553 int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
3554 int32_t x = (psymval->value(object, addend) - address);
3555 // Calculate the relevant G(n-1) value to obtain this stage residual.
3557 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3558 if (residual >= 0x100)
3559 return This::STATUS_OVERFLOW;
3561 // Mask out the value and U bit.
3563 // Set the U bit for non-negative values.
3566 insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
3568 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3569 return This::STATUS_OKAY;
3572 // R_ARM_LDC_PC_G0: S + A - P
3573 // R_ARM_LDC_PC_G1: S + A - P
3574 // R_ARM_LDC_PC_G2: S + A - P
3575 // R_ARM_LDC_SB_G0: S + A - B(S)
3576 // R_ARM_LDC_SB_G1: S + A - B(S)
3577 // R_ARM_LDC_SB_G2: S + A - B(S)
3578 static inline typename This::Status
3579 arm_grp_ldc(unsigned char* view,
3580 const Sized_relobj<32, big_endian>* object,
3581 const Symbol_value<32>* psymval,
3583 Arm_address address)
3585 gold_assert(group >= 0 && group < 3);
3586 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3587 Valtype* wv = reinterpret_cast<Valtype*>(view);
3588 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3590 const int sign = (insn & 0x00800000) ? 1 : -1;
3591 int32_t addend = ((insn & 0xff) << 2) * sign;
3592 int32_t x = (psymval->value(object, addend) - address);
3593 // Calculate the relevant G(n-1) value to obtain this stage residual.
3595 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3596 if ((residual & 0x3) != 0 || residual >= 0x400)
3597 return This::STATUS_OVERFLOW;
3599 // Mask out the value and U bit.
3601 // Set the U bit for non-negative values.
3604 insn |= (residual >> 2);
3606 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3607 return This::STATUS_OKAY;
3611 // Relocate ARM long branches. This handles relocation types
3612 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3613 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3614 // undefined and we do not use PLT in this relocation. In such a case,
3615 // the branch is converted into an NOP.
3617 template<bool big_endian>
3618 typename Arm_relocate_functions<big_endian>::Status
3619 Arm_relocate_functions<big_endian>::arm_branch_common(
3620 unsigned int r_type,
3621 const Relocate_info<32, big_endian>* relinfo,
3622 unsigned char *view,
3623 const Sized_symbol<32>* gsym,
3624 const Arm_relobj<big_endian>* object,
3626 const Symbol_value<32>* psymval,
3627 Arm_address address,
3628 Arm_address thumb_bit,
3629 bool is_weakly_undefined_without_plt)
3631 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3632 Valtype* wv = reinterpret_cast<Valtype*>(view);
3633 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3635 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3636 && ((val & 0x0f000000UL) == 0x0a000000UL);
3637 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3638 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3639 && ((val & 0x0f000000UL) == 0x0b000000UL);
3640 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3641 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3643 // Check that the instruction is valid.
3644 if (r_type == elfcpp::R_ARM_CALL)
3646 if (!insn_is_uncond_bl && !insn_is_blx)
3647 return This::STATUS_BAD_RELOC;
3649 else if (r_type == elfcpp::R_ARM_JUMP24)
3651 if (!insn_is_b && !insn_is_cond_bl)
3652 return This::STATUS_BAD_RELOC;
3654 else if (r_type == elfcpp::R_ARM_PLT32)
3656 if (!insn_is_any_branch)
3657 return This::STATUS_BAD_RELOC;
3659 else if (r_type == elfcpp::R_ARM_XPC25)
3661 // FIXME: AAELF document IH0044C does not say much about it other
3662 // than it being obsolete.
3663 if (!insn_is_any_branch)
3664 return This::STATUS_BAD_RELOC;
3669 // A branch to an undefined weak symbol is turned into a jump to
3670 // the next instruction unless a PLT entry will be created.
3671 // Do the same for local undefined symbols.
3672 // The jump to the next instruction is optimized as a NOP depending
3673 // on the architecture.
3674 const Target_arm<big_endian>* arm_target =
3675 Target_arm<big_endian>::default_target();
3676 if (is_weakly_undefined_without_plt)
3678 Valtype cond = val & 0xf0000000U;
3679 if (arm_target->may_use_arm_nop())
3680 val = cond | 0x0320f000;
3682 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3683 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3684 return This::STATUS_OKAY;
3687 Valtype addend = utils::sign_extend<26>(val << 2);
3688 Valtype branch_target = psymval->value(object, addend);
3689 int32_t branch_offset = branch_target - address;
3691 // We need a stub if the branch offset is too large or if we need
3693 bool may_use_blx = arm_target->may_use_blx();
3694 Reloc_stub* stub = NULL;
3695 if (utils::has_overflow<26>(branch_offset)
3696 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
3698 Valtype unadjusted_branch_target = psymval->value(object, 0);
3700 Stub_type stub_type =
3701 Reloc_stub::stub_type_for_reloc(r_type, address,
3702 unadjusted_branch_target,
3704 if (stub_type != arm_stub_none)
3706 Stub_table<big_endian>* stub_table =
3707 object->stub_table(relinfo->data_shndx);
3708 gold_assert(stub_table != NULL);
3710 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3711 stub = stub_table->find_reloc_stub(stub_key);
3712 gold_assert(stub != NULL);
3713 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3714 branch_target = stub_table->address() + stub->offset() + addend;
3715 branch_offset = branch_target - address;
3716 gold_assert(!utils::has_overflow<26>(branch_offset));
3720 // At this point, if we still need to switch mode, the instruction
3721 // must either be a BLX or a BL that can be converted to a BLX.
3725 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3726 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3729 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3730 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3731 return (utils::has_overflow<26>(branch_offset)
3732 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3735 // Relocate THUMB long branches. This handles relocation types
3736 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3737 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3738 // undefined and we do not use PLT in this relocation. In such a case,
3739 // the branch is converted into an NOP.
3741 template<bool big_endian>
3742 typename Arm_relocate_functions<big_endian>::Status
3743 Arm_relocate_functions<big_endian>::thumb_branch_common(
3744 unsigned int r_type,
3745 const Relocate_info<32, big_endian>* relinfo,
3746 unsigned char *view,
3747 const Sized_symbol<32>* gsym,
3748 const Arm_relobj<big_endian>* object,
3750 const Symbol_value<32>* psymval,
3751 Arm_address address,
3752 Arm_address thumb_bit,
3753 bool is_weakly_undefined_without_plt)
3755 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3756 Valtype* wv = reinterpret_cast<Valtype*>(view);
3757 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3758 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3760 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3762 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
3763 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
3765 // Check that the instruction is valid.
3766 if (r_type == elfcpp::R_ARM_THM_CALL)
3768 if (!is_bl_insn && !is_blx_insn)
3769 return This::STATUS_BAD_RELOC;
3771 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
3773 // This cannot be a BLX.
3775 return This::STATUS_BAD_RELOC;
3777 else if (r_type == elfcpp::R_ARM_THM_XPC22)
3779 // Check for Thumb to Thumb call.
3781 return This::STATUS_BAD_RELOC;
3784 gold_warning(_("%s: Thumb BLX instruction targets "
3785 "thumb function '%s'."),
3786 object->name().c_str(),
3787 (gsym ? gsym->name() : "(local)"));
3788 // Convert BLX to BL.
3789 lower_insn |= 0x1000U;
3795 // A branch to an undefined weak symbol is turned into a jump to
3796 // the next instruction unless a PLT entry will be created.
3797 // The jump to the next instruction is optimized as a NOP.W for
3798 // Thumb-2 enabled architectures.
3799 const Target_arm<big_endian>* arm_target =
3800 Target_arm<big_endian>::default_target();
3801 if (is_weakly_undefined_without_plt)
3803 if (arm_target->may_use_thumb2_nop())
3805 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
3806 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
3810 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
3811 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
3813 return This::STATUS_OKAY;
3816 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
3817 Arm_address branch_target = psymval->value(object, addend);
3818 int32_t branch_offset = branch_target - address;
3820 // We need a stub if the branch offset is too large or if we need
3822 bool may_use_blx = arm_target->may_use_blx();
3823 bool thumb2 = arm_target->using_thumb2();
3824 if ((!thumb2 && utils::has_overflow<23>(branch_offset))
3825 || (thumb2 && utils::has_overflow<25>(branch_offset))
3826 || ((thumb_bit == 0)
3827 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3828 || r_type == elfcpp::R_ARM_THM_JUMP24)))
3830 Arm_address unadjusted_branch_target = psymval->value(object, 0);
3832 Stub_type stub_type =
3833 Reloc_stub::stub_type_for_reloc(r_type, address,
3834 unadjusted_branch_target,
3837 if (stub_type != arm_stub_none)
3839 Stub_table<big_endian>* stub_table =
3840 object->stub_table(relinfo->data_shndx);
3841 gold_assert(stub_table != NULL);
3843 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3844 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
3845 gold_assert(stub != NULL);
3846 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3847 branch_target = stub_table->address() + stub->offset() + addend;
3848 branch_offset = branch_target - address;
3852 // At this point, if we still need to switch mode, the instruction
3853 // must either be a BLX or a BL that can be converted to a BLX.
3856 gold_assert(may_use_blx
3857 && (r_type == elfcpp::R_ARM_THM_CALL
3858 || r_type == elfcpp::R_ARM_THM_XPC22));
3859 // Make sure this is a BLX.
3860 lower_insn &= ~0x1000U;
3864 // Make sure this is a BL.
3865 lower_insn |= 0x1000U;
3868 if ((lower_insn & 0x5000U) == 0x4000U)
3869 // For a BLX instruction, make sure that the relocation is rounded up
3870 // to a word boundary. This follows the semantics of the instruction
3871 // which specifies that bit 1 of the target address will come from bit
3872 // 1 of the base address.
3873 branch_offset = (branch_offset + 2) & ~3;
3875 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3876 // We use the Thumb-2 encoding, which is safe even if dealing with
3877 // a Thumb-1 instruction by virtue of our overflow check above. */
3878 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
3879 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
3881 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3882 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3885 ? utils::has_overflow<25>(branch_offset)
3886 : utils::has_overflow<23>(branch_offset))
3887 ? This::STATUS_OVERFLOW
3888 : This::STATUS_OKAY);
3891 // Relocate THUMB-2 long conditional branches.
3892 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3893 // undefined and we do not use PLT in this relocation. In such a case,
3894 // the branch is converted into an NOP.
3896 template<bool big_endian>
3897 typename Arm_relocate_functions<big_endian>::Status
3898 Arm_relocate_functions<big_endian>::thm_jump19(
3899 unsigned char *view,
3900 const Arm_relobj<big_endian>* object,
3901 const Symbol_value<32>* psymval,
3902 Arm_address address,
3903 Arm_address thumb_bit)
3905 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3906 Valtype* wv = reinterpret_cast<Valtype*>(view);
3907 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3908 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3909 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
3911 Arm_address branch_target = psymval->value(object, addend);
3912 int32_t branch_offset = branch_target - address;
3914 // ??? Should handle interworking? GCC might someday try to
3915 // use this for tail calls.
3916 // FIXME: We do support thumb entry to PLT yet.
3919 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3920 return This::STATUS_BAD_RELOC;
3923 // Put RELOCATION back into the insn.
3924 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
3925 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
3927 // Put the relocated value back in the object file:
3928 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3929 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3931 return (utils::has_overflow<21>(branch_offset)
3932 ? This::STATUS_OVERFLOW
3933 : This::STATUS_OKAY);
3936 // Get the GOT section, creating it if necessary.
3938 template<bool big_endian>
3939 Arm_output_data_got<big_endian>*
3940 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
3942 if (this->got_ == NULL)
3944 gold_assert(symtab != NULL && layout != NULL);
3946 this->got_ = new Arm_output_data_got<big_endian>(symtab, layout);
3949 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3951 | elfcpp::SHF_WRITE),
3952 this->got_, false, true, true,
3955 // The old GNU linker creates a .got.plt section. We just
3956 // create another set of data in the .got section. Note that we
3957 // always create a PLT if we create a GOT, although the PLT
3959 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
3960 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3962 | elfcpp::SHF_WRITE),
3963 this->got_plt_, false, false,
3966 // The first three entries are reserved.
3967 this->got_plt_->set_current_data_size(3 * 4);
3969 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3970 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3971 Symbol_table::PREDEFINED,
3973 0, 0, elfcpp::STT_OBJECT,
3975 elfcpp::STV_HIDDEN, 0,
3981 // Get the dynamic reloc section, creating it if necessary.
3983 template<bool big_endian>
3984 typename Target_arm<big_endian>::Reloc_section*
3985 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
3987 if (this->rel_dyn_ == NULL)
3989 gold_assert(layout != NULL);
3990 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
3991 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
3992 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
3993 false, false, false);
3995 return this->rel_dyn_;
3998 // Insn_template methods.
4000 // Return byte size of an instruction template.
4003 Insn_template::size() const
4005 switch (this->type())
4008 case THUMB16_SPECIAL_TYPE:
4019 // Return alignment of an instruction template.
4022 Insn_template::alignment() const
4024 switch (this->type())
4027 case THUMB16_SPECIAL_TYPE:
4038 // Stub_template methods.
4040 Stub_template::Stub_template(
4041 Stub_type type, const Insn_template* insns,
4043 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
4044 entry_in_thumb_mode_(false), relocs_()
4048 // Compute byte size and alignment of stub template.
4049 for (size_t i = 0; i < insn_count; i++)
4051 unsigned insn_alignment = insns[i].alignment();
4052 size_t insn_size = insns[i].size();
4053 gold_assert((offset & (insn_alignment - 1)) == 0);
4054 this->alignment_ = std::max(this->alignment_, insn_alignment);
4055 switch (insns[i].type())
4057 case Insn_template::THUMB16_TYPE:
4058 case Insn_template::THUMB16_SPECIAL_TYPE:
4060 this->entry_in_thumb_mode_ = true;
4063 case Insn_template::THUMB32_TYPE:
4064 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
4065 this->relocs_.push_back(Reloc(i, offset));
4067 this->entry_in_thumb_mode_ = true;
4070 case Insn_template::ARM_TYPE:
4071 // Handle cases where the target is encoded within the
4073 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
4074 this->relocs_.push_back(Reloc(i, offset));
4077 case Insn_template::DATA_TYPE:
4078 // Entry point cannot be data.
4079 gold_assert(i != 0);
4080 this->relocs_.push_back(Reloc(i, offset));
4086 offset += insn_size;
4088 this->size_ = offset;
4093 // Template to implement do_write for a specific target endianity.
4095 template<bool big_endian>
4097 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
4099 const Stub_template* stub_template = this->stub_template();
4100 const Insn_template* insns = stub_template->insns();
4102 // FIXME: We do not handle BE8 encoding yet.
4103 unsigned char* pov = view;
4104 for (size_t i = 0; i < stub_template->insn_count(); i++)
4106 switch (insns[i].type())
4108 case Insn_template::THUMB16_TYPE:
4109 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
4111 case Insn_template::THUMB16_SPECIAL_TYPE:
4112 elfcpp::Swap<16, big_endian>::writeval(
4114 this->thumb16_special(i));
4116 case Insn_template::THUMB32_TYPE:
4118 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
4119 uint32_t lo = insns[i].data() & 0xffff;
4120 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
4121 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
4124 case Insn_template::ARM_TYPE:
4125 case Insn_template::DATA_TYPE:
4126 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
4131 pov += insns[i].size();
4133 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
4136 // Reloc_stub::Key methods.
4138 // Dump a Key as a string for debugging.
4141 Reloc_stub::Key::name() const
4143 if (this->r_sym_ == invalid_index)
4145 // Global symbol key name
4146 // <stub-type>:<symbol name>:<addend>.
4147 const std::string sym_name = this->u_.symbol->name();
4148 // We need to print two hex number and two colons. So just add 100 bytes
4149 // to the symbol name size.
4150 size_t len = sym_name.size() + 100;
4151 char* buffer = new char[len];
4152 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
4153 sym_name.c_str(), this->addend_);
4154 gold_assert(c > 0 && c < static_cast<int>(len));
4156 return std::string(buffer);
4160 // local symbol key name
4161 // <stub-type>:<object>:<r_sym>:<addend>.
4162 const size_t len = 200;
4164 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
4165 this->u_.relobj, this->r_sym_, this->addend_);
4166 gold_assert(c > 0 && c < static_cast<int>(len));
4167 return std::string(buffer);
4171 // Reloc_stub methods.
4173 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
4174 // LOCATION to DESTINATION.
4175 // This code is based on the arm_type_of_stub function in
4176 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
4180 Reloc_stub::stub_type_for_reloc(
4181 unsigned int r_type,
4182 Arm_address location,
4183 Arm_address destination,
4184 bool target_is_thumb)
4186 Stub_type stub_type = arm_stub_none;
4188 // This is a bit ugly but we want to avoid using a templated class for
4189 // big and little endianities.
4191 bool should_force_pic_veneer;
4194 if (parameters->target().is_big_endian())
4196 const Target_arm<true>* big_endian_target =
4197 Target_arm<true>::default_target();
4198 may_use_blx = big_endian_target->may_use_blx();
4199 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
4200 thumb2 = big_endian_target->using_thumb2();
4201 thumb_only = big_endian_target->using_thumb_only();
4205 const Target_arm<false>* little_endian_target =
4206 Target_arm<false>::default_target();
4207 may_use_blx = little_endian_target->may_use_blx();
4208 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
4209 thumb2 = little_endian_target->using_thumb2();
4210 thumb_only = little_endian_target->using_thumb_only();
4213 int64_t branch_offset = (int64_t)destination - location;
4215 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
4217 // Handle cases where:
4218 // - this call goes too far (different Thumb/Thumb2 max
4220 // - it's a Thumb->Arm call and blx is not available, or it's a
4221 // Thumb->Arm branch (not bl). A stub is needed in this case.
4223 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4224 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4226 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4227 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4228 || ((!target_is_thumb)
4229 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4230 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
4232 if (target_is_thumb)
4237 stub_type = (parameters->options().shared()
4238 || should_force_pic_veneer)
4241 && (r_type == elfcpp::R_ARM_THM_CALL))
4242 // V5T and above. Stub starts with ARM code, so
4243 // we must be able to switch mode before
4244 // reaching it, which is only possible for 'bl'
4245 // (ie R_ARM_THM_CALL relocation).
4246 ? arm_stub_long_branch_any_thumb_pic
4247 // On V4T, use Thumb code only.
4248 : arm_stub_long_branch_v4t_thumb_thumb_pic)
4252 && (r_type == elfcpp::R_ARM_THM_CALL))
4253 ? arm_stub_long_branch_any_any // V5T and above.
4254 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
4258 stub_type = (parameters->options().shared()
4259 || should_force_pic_veneer)
4260 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
4261 : arm_stub_long_branch_thumb_only; // non-PIC stub.
4268 // FIXME: We should check that the input section is from an
4269 // object that has interwork enabled.
4271 stub_type = (parameters->options().shared()
4272 || should_force_pic_veneer)
4275 && (r_type == elfcpp::R_ARM_THM_CALL))
4276 ? arm_stub_long_branch_any_arm_pic // V5T and above.
4277 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
4281 && (r_type == elfcpp::R_ARM_THM_CALL))
4282 ? arm_stub_long_branch_any_any // V5T and above.
4283 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
4285 // Handle v4t short branches.
4286 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4287 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4288 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4289 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4293 else if (r_type == elfcpp::R_ARM_CALL
4294 || r_type == elfcpp::R_ARM_JUMP24
4295 || r_type == elfcpp::R_ARM_PLT32)
4297 if (target_is_thumb)
4301 // FIXME: We should check that the input section is from an
4302 // object that has interwork enabled.
4304 // We have an extra 2-bytes reach because of
4305 // the mode change (bit 24 (H) of BLX encoding).
4306 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4307 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4308 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
4309 || (r_type == elfcpp::R_ARM_JUMP24)
4310 || (r_type == elfcpp::R_ARM_PLT32))
4312 stub_type = (parameters->options().shared()
4313 || should_force_pic_veneer)
4316 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
4317 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
4321 ? arm_stub_long_branch_any_any // V5T and above.
4322 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
4328 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4329 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4331 stub_type = (parameters->options().shared()
4332 || should_force_pic_veneer)
4333 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
4334 : arm_stub_long_branch_any_any; /// non-PIC.
4342 // Cortex_a8_stub methods.
4344 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4345 // I is the position of the instruction template in the stub template.
4348 Cortex_a8_stub::do_thumb16_special(size_t i)
4350 // The only use of this is to copy condition code from a conditional
4351 // branch being worked around to the corresponding conditional branch in
4353 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4355 uint16_t data = this->stub_template()->insns()[i].data();
4356 gold_assert((data & 0xff00U) == 0xd000U);
4357 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
4361 // Stub_factory methods.
4363 Stub_factory::Stub_factory()
4365 // The instruction template sequences are declared as static
4366 // objects and initialized first time the constructor runs.
4368 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4369 // to reach the stub if necessary.
4370 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
4372 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4373 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4374 // dcd R_ARM_ABS32(X)
4377 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4379 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
4381 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4382 Insn_template::arm_insn(0xe12fff1c), // bx ip
4383 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4384 // dcd R_ARM_ABS32(X)
4387 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4388 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
4390 Insn_template::thumb16_insn(0xb401), // push {r0}
4391 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4392 Insn_template::thumb16_insn(0x4684), // mov ip, r0
4393 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4394 Insn_template::thumb16_insn(0x4760), // bx ip
4395 Insn_template::thumb16_insn(0xbf00), // nop
4396 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4397 // dcd R_ARM_ABS32(X)
4400 // V4T Thumb -> Thumb long branch stub. Using the stack is not
4402 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
4404 Insn_template::thumb16_insn(0x4778), // bx pc
4405 Insn_template::thumb16_insn(0x46c0), // nop
4406 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4407 Insn_template::arm_insn(0xe12fff1c), // bx ip
4408 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4409 // dcd R_ARM_ABS32(X)
4412 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4414 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
4416 Insn_template::thumb16_insn(0x4778), // bx pc
4417 Insn_template::thumb16_insn(0x46c0), // nop
4418 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4419 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4420 // dcd R_ARM_ABS32(X)
4423 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4424 // one, when the destination is close enough.
4425 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
4427 Insn_template::thumb16_insn(0x4778), // bx pc
4428 Insn_template::thumb16_insn(0x46c0), // nop
4429 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4432 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4433 // blx to reach the stub if necessary.
4434 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
4436 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4437 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4438 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4439 // dcd R_ARM_REL32(X-4)
4442 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4443 // blx to reach the stub if necessary. We can not add into pc;
4444 // it is not guaranteed to mode switch (different in ARMv6 and
4446 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
4448 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4449 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4450 Insn_template::arm_insn(0xe12fff1c), // bx ip
4451 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4452 // dcd R_ARM_REL32(X)
4455 // V4T ARM -> ARM long branch stub, PIC.
4456 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4458 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4459 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4460 Insn_template::arm_insn(0xe12fff1c), // bx ip
4461 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4462 // dcd R_ARM_REL32(X)
4465 // V4T Thumb -> ARM long branch stub, PIC.
4466 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4468 Insn_template::thumb16_insn(0x4778), // bx pc
4469 Insn_template::thumb16_insn(0x46c0), // nop
4470 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4471 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4472 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4473 // dcd R_ARM_REL32(X)
4476 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4478 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4480 Insn_template::thumb16_insn(0xb401), // push {r0}
4481 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4482 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4483 Insn_template::thumb16_insn(0x4484), // add ip, r0
4484 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4485 Insn_template::thumb16_insn(0x4760), // bx ip
4486 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4487 // dcd R_ARM_REL32(X)
4490 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4492 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4494 Insn_template::thumb16_insn(0x4778), // bx pc
4495 Insn_template::thumb16_insn(0x46c0), // nop
4496 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4497 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4498 Insn_template::arm_insn(0xe12fff1c), // bx ip
4499 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4500 // dcd R_ARM_REL32(X)
4503 // Cortex-A8 erratum-workaround stubs.
4505 // Stub used for conditional branches (which may be beyond +/-1MB away,
4506 // so we can't use a conditional branch to reach this stub).
4513 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4515 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4516 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4517 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4521 // Stub used for b.w and bl.w instructions.
4523 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4525 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4528 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4530 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4533 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4534 // instruction (which switches to ARM mode) to point to this stub. Jump to
4535 // the real destination using an ARM-mode branch.
4536 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
4538 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4541 // Stub used to provide an interworking for R_ARM_V4BX relocation
4542 // (bx r[n] instruction).
4543 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4545 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4546 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4547 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4550 // Fill in the stub template look-up table. Stub templates are constructed
4551 // per instance of Stub_factory for fast look-up without locking
4552 // in a thread-enabled environment.
4554 this->stub_templates_[arm_stub_none] =
4555 new Stub_template(arm_stub_none, NULL, 0);
4557 #define DEF_STUB(x) \
4561 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4562 Stub_type type = arm_stub_##x; \
4563 this->stub_templates_[type] = \
4564 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4572 // Stub_table methods.
4574 // Removel all Cortex-A8 stub.
4576 template<bool big_endian>
4578 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4580 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4581 p != this->cortex_a8_stubs_.end();
4584 this->cortex_a8_stubs_.clear();
4587 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4589 template<bool big_endian>
4591 Stub_table<big_endian>::relocate_stub(
4593 const Relocate_info<32, big_endian>* relinfo,
4594 Target_arm<big_endian>* arm_target,
4595 Output_section* output_section,
4596 unsigned char* view,
4597 Arm_address address,
4598 section_size_type view_size)
4600 const Stub_template* stub_template = stub->stub_template();
4601 if (stub_template->reloc_count() != 0)
4603 // Adjust view to cover the stub only.
4604 section_size_type offset = stub->offset();
4605 section_size_type stub_size = stub_template->size();
4606 gold_assert(offset + stub_size <= view_size);
4608 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4609 address + offset, stub_size);
4613 // Relocate all stubs in this stub table.
4615 template<bool big_endian>
4617 Stub_table<big_endian>::relocate_stubs(
4618 const Relocate_info<32, big_endian>* relinfo,
4619 Target_arm<big_endian>* arm_target,
4620 Output_section* output_section,
4621 unsigned char* view,
4622 Arm_address address,
4623 section_size_type view_size)
4625 // If we are passed a view bigger than the stub table's. we need to
4627 gold_assert(address == this->address()
4629 == static_cast<section_size_type>(this->data_size())));
4631 // Relocate all relocation stubs.
4632 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4633 p != this->reloc_stubs_.end();
4635 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4636 address, view_size);
4638 // Relocate all Cortex-A8 stubs.
4639 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4640 p != this->cortex_a8_stubs_.end();
4642 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4643 address, view_size);
4645 // Relocate all ARM V4BX stubs.
4646 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4647 p != this->arm_v4bx_stubs_.end();
4651 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4652 address, view_size);
4656 // Write out the stubs to file.
4658 template<bool big_endian>
4660 Stub_table<big_endian>::do_write(Output_file* of)
4662 off_t offset = this->offset();
4663 const section_size_type oview_size =
4664 convert_to_section_size_type(this->data_size());
4665 unsigned char* const oview = of->get_output_view(offset, oview_size);
4667 // Write relocation stubs.
4668 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4669 p != this->reloc_stubs_.end();
4672 Reloc_stub* stub = p->second;
4673 Arm_address address = this->address() + stub->offset();
4675 == align_address(address,
4676 stub->stub_template()->alignment()));
4677 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4681 // Write Cortex-A8 stubs.
4682 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4683 p != this->cortex_a8_stubs_.end();
4686 Cortex_a8_stub* stub = p->second;
4687 Arm_address address = this->address() + stub->offset();
4689 == align_address(address,
4690 stub->stub_template()->alignment()));
4691 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4695 // Write ARM V4BX relocation stubs.
4696 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4697 p != this->arm_v4bx_stubs_.end();
4703 Arm_address address = this->address() + (*p)->offset();
4705 == align_address(address,
4706 (*p)->stub_template()->alignment()));
4707 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4711 of->write_output_view(this->offset(), oview_size, oview);
4714 // Update the data size and address alignment of the stub table at the end
4715 // of a relaxation pass. Return true if either the data size or the
4716 // alignment changed in this relaxation pass.
4718 template<bool big_endian>
4720 Stub_table<big_endian>::update_data_size_and_addralign()
4723 unsigned addralign = 1;
4725 // Go over all stubs in table to compute data size and address alignment.
4727 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4728 p != this->reloc_stubs_.end();
4731 const Stub_template* stub_template = p->second->stub_template();
4732 addralign = std::max(addralign, stub_template->alignment());
4733 size = (align_address(size, stub_template->alignment())
4734 + stub_template->size());
4737 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4738 p != this->cortex_a8_stubs_.end();
4741 const Stub_template* stub_template = p->second->stub_template();
4742 addralign = std::max(addralign, stub_template->alignment());
4743 size = (align_address(size, stub_template->alignment())
4744 + stub_template->size());
4747 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4748 p != this->arm_v4bx_stubs_.end();
4754 const Stub_template* stub_template = (*p)->stub_template();
4755 addralign = std::max(addralign, stub_template->alignment());
4756 size = (align_address(size, stub_template->alignment())
4757 + stub_template->size());
4760 // Check if either data size or alignment changed in this pass.
4761 // Update prev_data_size_ and prev_addralign_. These will be used
4762 // as the current data size and address alignment for the next pass.
4763 bool changed = size != this->prev_data_size_;
4764 this->prev_data_size_ = size;
4766 if (addralign != this->prev_addralign_)
4768 this->prev_addralign_ = addralign;
4773 // Finalize the stubs. This sets the offsets of the stubs within the stub
4774 // table. It also marks all input sections needing Cortex-A8 workaround.
4776 template<bool big_endian>
4778 Stub_table<big_endian>::finalize_stubs()
4781 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4782 p != this->reloc_stubs_.end();
4785 Reloc_stub* stub = p->second;
4786 const Stub_template* stub_template = stub->stub_template();
4787 uint64_t stub_addralign = stub_template->alignment();
4788 off = align_address(off, stub_addralign);
4789 stub->set_offset(off);
4790 off += stub_template->size();
4793 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4794 p != this->cortex_a8_stubs_.end();
4797 Cortex_a8_stub* stub = p->second;
4798 const Stub_template* stub_template = stub->stub_template();
4799 uint64_t stub_addralign = stub_template->alignment();
4800 off = align_address(off, stub_addralign);
4801 stub->set_offset(off);
4802 off += stub_template->size();
4804 // Mark input section so that we can determine later if a code section
4805 // needs the Cortex-A8 workaround quickly.
4806 Arm_relobj<big_endian>* arm_relobj =
4807 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
4808 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
4811 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4812 p != this->arm_v4bx_stubs_.end();
4818 const Stub_template* stub_template = (*p)->stub_template();
4819 uint64_t stub_addralign = stub_template->alignment();
4820 off = align_address(off, stub_addralign);
4821 (*p)->set_offset(off);
4822 off += stub_template->size();
4825 gold_assert(off <= this->prev_data_size_);
4828 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4829 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4830 // of the address range seen by the linker.
4832 template<bool big_endian>
4834 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
4835 Target_arm<big_endian>* arm_target,
4836 unsigned char* view,
4837 Arm_address view_address,
4838 section_size_type view_size)
4840 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4841 for (Cortex_a8_stub_list::const_iterator p =
4842 this->cortex_a8_stubs_.lower_bound(view_address);
4843 ((p != this->cortex_a8_stubs_.end())
4844 && (p->first < (view_address + view_size)));
4847 // We do not store the THUMB bit in the LSB of either the branch address
4848 // or the stub offset. There is no need to strip the LSB.
4849 Arm_address branch_address = p->first;
4850 const Cortex_a8_stub* stub = p->second;
4851 Arm_address stub_address = this->address() + stub->offset();
4853 // Offset of the branch instruction relative to this view.
4854 section_size_type offset =
4855 convert_to_section_size_type(branch_address - view_address);
4856 gold_assert((offset + 4) <= view_size);
4858 arm_target->apply_cortex_a8_workaround(stub, stub_address,
4859 view + offset, branch_address);
4863 // Arm_input_section methods.
4865 // Initialize an Arm_input_section.
4867 template<bool big_endian>
4869 Arm_input_section<big_endian>::init()
4871 Relobj* relobj = this->relobj();
4872 unsigned int shndx = this->shndx();
4874 // Cache these to speed up size and alignment queries. It is too slow
4875 // to call section_addraglin and section_size every time.
4876 this->original_addralign_ = relobj->section_addralign(shndx);
4877 this->original_size_ = relobj->section_size(shndx);
4879 // We want to make this look like the original input section after
4880 // output sections are finalized.
4881 Output_section* os = relobj->output_section(shndx);
4882 off_t offset = relobj->output_section_offset(shndx);
4883 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
4884 this->set_address(os->address() + offset);
4885 this->set_file_offset(os->offset() + offset);
4887 this->set_current_data_size(this->original_size_);
4888 this->finalize_data_size();
4891 template<bool big_endian>
4893 Arm_input_section<big_endian>::do_write(Output_file* of)
4895 // We have to write out the original section content.
4896 section_size_type section_size;
4897 const unsigned char* section_contents =
4898 this->relobj()->section_contents(this->shndx(), §ion_size, false);
4899 of->write(this->offset(), section_contents, section_size);
4901 // If this owns a stub table and it is not empty, write it.
4902 if (this->is_stub_table_owner() && !this->stub_table_->empty())
4903 this->stub_table_->write(of);
4906 // Finalize data size.
4908 template<bool big_endian>
4910 Arm_input_section<big_endian>::set_final_data_size()
4912 // If this owns a stub table, finalize its data size as well.
4913 if (this->is_stub_table_owner())
4915 uint64_t address = this->address();
4917 // The stub table comes after the original section contents.
4918 address += this->original_size_;
4919 address = align_address(address, this->stub_table_->addralign());
4920 off_t offset = this->offset() + (address - this->address());
4921 this->stub_table_->set_address_and_file_offset(address, offset);
4922 address += this->stub_table_->data_size();
4923 gold_assert(address == this->address() + this->current_data_size());
4926 this->set_data_size(this->current_data_size());
4929 // Reset address and file offset.
4931 template<bool big_endian>
4933 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
4935 // Size of the original input section contents.
4936 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4938 // If this is a stub table owner, account for the stub table size.
4939 if (this->is_stub_table_owner())
4941 Stub_table<big_endian>* stub_table = this->stub_table_;
4943 // Reset the stub table's address and file offset. The
4944 // current data size for child will be updated after that.
4945 stub_table_->reset_address_and_file_offset();
4946 off = align_address(off, stub_table_->addralign());
4947 off += stub_table->current_data_size();
4950 this->set_current_data_size(off);
4953 // Arm_exidx_cantunwind methods.
4955 // Write this to Output file OF for a fixed endianity.
4957 template<bool big_endian>
4959 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
4961 off_t offset = this->offset();
4962 const section_size_type oview_size = 8;
4963 unsigned char* const oview = of->get_output_view(offset, oview_size);
4965 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4966 Valtype* wv = reinterpret_cast<Valtype*>(oview);
4968 Output_section* os = this->relobj_->output_section(this->shndx_);
4969 gold_assert(os != NULL);
4971 Arm_relobj<big_endian>* arm_relobj =
4972 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
4973 Arm_address output_offset =
4974 arm_relobj->get_output_section_offset(this->shndx_);
4975 Arm_address section_start;
4976 if(output_offset != Arm_relobj<big_endian>::invalid_address)
4977 section_start = os->address() + output_offset;
4980 // Currently this only happens for a relaxed section.
4981 const Output_relaxed_input_section* poris =
4982 os->find_relaxed_input_section(this->relobj_, this->shndx_);
4983 gold_assert(poris != NULL);
4984 section_start = poris->address();
4987 // We always append this to the end of an EXIDX section.
4988 Arm_address output_address =
4989 section_start + this->relobj_->section_size(this->shndx_);
4991 // Write out the entry. The first word either points to the beginning
4992 // or after the end of a text section. The second word is the special
4993 // EXIDX_CANTUNWIND value.
4994 uint32_t prel31_offset = output_address - this->address();
4995 if (utils::has_overflow<31>(offset))
4996 gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
4997 elfcpp::Swap<32, big_endian>::writeval(wv, prel31_offset & 0x7fffffffU);
4998 elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
5000 of->write_output_view(this->offset(), oview_size, oview);
5003 // Arm_exidx_merged_section methods.
5005 // Constructor for Arm_exidx_merged_section.
5006 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
5007 // SECTION_OFFSET_MAP points to a section offset map describing how
5008 // parts of the input section are mapped to output. DELETED_BYTES is
5009 // the number of bytes deleted from the EXIDX input section.
5011 Arm_exidx_merged_section::Arm_exidx_merged_section(
5012 const Arm_exidx_input_section& exidx_input_section,
5013 const Arm_exidx_section_offset_map& section_offset_map,
5014 uint32_t deleted_bytes)
5015 : Output_relaxed_input_section(exidx_input_section.relobj(),
5016 exidx_input_section.shndx(),
5017 exidx_input_section.addralign()),
5018 exidx_input_section_(exidx_input_section),
5019 section_offset_map_(section_offset_map)
5021 // Fix size here so that we do not need to implement set_final_data_size.
5022 this->set_data_size(exidx_input_section.size() - deleted_bytes);
5023 this->fix_data_size();
5026 // Given an input OBJECT, an input section index SHNDX within that
5027 // object, and an OFFSET relative to the start of that input
5028 // section, return whether or not the corresponding offset within
5029 // the output section is known. If this function returns true, it
5030 // sets *POUTPUT to the output offset. The value -1 indicates that
5031 // this input offset is being discarded.
5034 Arm_exidx_merged_section::do_output_offset(
5035 const Relobj* relobj,
5037 section_offset_type offset,
5038 section_offset_type* poutput) const
5040 // We only handle offsets for the original EXIDX input section.
5041 if (relobj != this->exidx_input_section_.relobj()
5042 || shndx != this->exidx_input_section_.shndx())
5045 section_offset_type section_size =
5046 convert_types<section_offset_type>(this->exidx_input_section_.size());
5047 if (offset < 0 || offset >= section_size)
5048 // Input offset is out of valid range.
5052 // We need to look up the section offset map to determine the output
5053 // offset. Find the reference point in map that is first offset
5054 // bigger than or equal to this offset.
5055 Arm_exidx_section_offset_map::const_iterator p =
5056 this->section_offset_map_.lower_bound(offset);
5058 // The section offset maps are build such that this should not happen if
5059 // input offset is in the valid range.
5060 gold_assert(p != this->section_offset_map_.end());
5062 // We need to check if this is dropped.
5063 section_offset_type ref = p->first;
5064 section_offset_type mapped_ref = p->second;
5066 if (mapped_ref != Arm_exidx_input_section::invalid_offset)
5067 // Offset is present in output.
5068 *poutput = mapped_ref + (offset - ref);
5070 // Offset is discarded owing to EXIDX entry merging.
5077 // Write this to output file OF.
5080 Arm_exidx_merged_section::do_write(Output_file* of)
5082 // If we retain or discard the whole EXIDX input section, we would
5084 gold_assert(this->data_size() != this->exidx_input_section_.size()
5085 && this->data_size() != 0);
5087 off_t offset = this->offset();
5088 const section_size_type oview_size = this->data_size();
5089 unsigned char* const oview = of->get_output_view(offset, oview_size);
5091 Output_section* os = this->relobj()->output_section(this->shndx());
5092 gold_assert(os != NULL);
5094 // Get contents of EXIDX input section.
5095 section_size_type section_size;
5096 const unsigned char* section_contents =
5097 this->relobj()->section_contents(this->shndx(), §ion_size, false);
5098 gold_assert(section_size == this->exidx_input_section_.size());
5100 // Go over spans of input offsets and write only those that are not
5102 section_offset_type in_start = 0;
5103 section_offset_type out_start = 0;
5104 for(Arm_exidx_section_offset_map::const_iterator p =
5105 this->section_offset_map_.begin();
5106 p != this->section_offset_map_.end();
5109 section_offset_type in_end = p->first;
5110 gold_assert(in_end >= in_start);
5111 section_offset_type out_end = p->second;
5112 size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
5115 size_t out_chunk_size =
5116 convert_types<size_t>(out_end - out_start + 1);
5117 gold_assert(out_chunk_size == in_chunk_size);
5118 memcpy(oview + out_start, section_contents + in_start,
5120 out_start += out_chunk_size;
5122 in_start += in_chunk_size;
5125 gold_assert(convert_to_section_size_type(out_start) == oview_size);
5126 of->write_output_view(this->offset(), oview_size, oview);
5129 // Arm_exidx_fixup methods.
5131 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
5132 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
5133 // points to the end of the last seen EXIDX section.
5136 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
5138 if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
5139 && this->last_input_section_ != NULL)
5141 Relobj* relobj = this->last_input_section_->relobj();
5142 unsigned int text_shndx = this->last_input_section_->link();
5143 Arm_exidx_cantunwind* cantunwind =
5144 new Arm_exidx_cantunwind(relobj, text_shndx);
5145 this->exidx_output_section_->add_output_section_data(cantunwind);
5146 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5150 // Process an EXIDX section entry in input. Return whether this entry
5151 // can be deleted in the output. SECOND_WORD in the second word of the
5155 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
5158 if (second_word == elfcpp::EXIDX_CANTUNWIND)
5160 // Merge if previous entry is also an EXIDX_CANTUNWIND.
5161 delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
5162 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5164 else if ((second_word & 0x80000000) != 0)
5166 // Inlined unwinding data. Merge if equal to previous.
5167 delete_entry = (this->last_unwind_type_ == UT_INLINED_ENTRY
5168 && this->last_inlined_entry_ == second_word);
5169 this->last_unwind_type_ = UT_INLINED_ENTRY;
5170 this->last_inlined_entry_ = second_word;
5174 // Normal table entry. In theory we could merge these too,
5175 // but duplicate entries are likely to be much less common.
5176 delete_entry = false;
5177 this->last_unwind_type_ = UT_NORMAL_ENTRY;
5179 return delete_entry;
5182 // Update the current section offset map during EXIDX section fix-up.
5183 // If there is no map, create one. INPUT_OFFSET is the offset of a
5184 // reference point, DELETED_BYTES is the number of deleted by in the
5185 // section so far. If DELETE_ENTRY is true, the reference point and
5186 // all offsets after the previous reference point are discarded.
5189 Arm_exidx_fixup::update_offset_map(
5190 section_offset_type input_offset,
5191 section_size_type deleted_bytes,
5194 if (this->section_offset_map_ == NULL)
5195 this->section_offset_map_ = new Arm_exidx_section_offset_map();
5196 section_offset_type output_offset = (delete_entry
5198 : input_offset - deleted_bytes);
5199 (*this->section_offset_map_)[input_offset] = output_offset;
5202 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
5203 // bytes deleted. If some entries are merged, also store a pointer to a newly
5204 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
5205 // caller owns the map and is responsible for releasing it after use.
5207 template<bool big_endian>
5209 Arm_exidx_fixup::process_exidx_section(
5210 const Arm_exidx_input_section* exidx_input_section,
5211 Arm_exidx_section_offset_map** psection_offset_map)
5213 Relobj* relobj = exidx_input_section->relobj();
5214 unsigned shndx = exidx_input_section->shndx();
5215 section_size_type section_size;
5216 const unsigned char* section_contents =
5217 relobj->section_contents(shndx, §ion_size, false);
5219 if ((section_size % 8) != 0)
5221 // Something is wrong with this section. Better not touch it.
5222 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5223 relobj->name().c_str(), shndx);
5224 this->last_input_section_ = exidx_input_section;
5225 this->last_unwind_type_ = UT_NONE;
5229 uint32_t deleted_bytes = 0;
5230 bool prev_delete_entry = false;
5231 gold_assert(this->section_offset_map_ == NULL);
5233 for (section_size_type i = 0; i < section_size; i += 8)
5235 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5237 reinterpret_cast<const Valtype*>(section_contents + i + 4);
5238 uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
5240 bool delete_entry = this->process_exidx_entry(second_word);
5242 // Entry deletion causes changes in output offsets. We use a std::map
5243 // to record these. And entry (x, y) means input offset x
5244 // is mapped to output offset y. If y is invalid_offset, then x is
5245 // dropped in the output. Because of the way std::map::lower_bound
5246 // works, we record the last offset in a region w.r.t to keeping or
5247 // dropping. If there is no entry (x0, y0) for an input offset x0,
5248 // the output offset y0 of it is determined by the output offset y1 of
5249 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5250 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
5252 if (delete_entry != prev_delete_entry && i != 0)
5253 this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
5255 // Update total deleted bytes for this entry.
5259 prev_delete_entry = delete_entry;
5262 // If section offset map is not NULL, make an entry for the end of
5264 if (this->section_offset_map_ != NULL)
5265 update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
5267 *psection_offset_map = this->section_offset_map_;
5268 this->section_offset_map_ = NULL;
5269 this->last_input_section_ = exidx_input_section;
5271 // Set the first output text section so that we can link the EXIDX output
5272 // section to it. Ignore any EXIDX input section that is completely merged.
5273 if (this->first_output_text_section_ == NULL
5274 && deleted_bytes != section_size)
5276 unsigned int link = exidx_input_section->link();
5277 Output_section* os = relobj->output_section(link);
5278 gold_assert(os != NULL);
5279 this->first_output_text_section_ = os;
5282 return deleted_bytes;
5285 // Arm_output_section methods.
5287 // Create a stub group for input sections from BEGIN to END. OWNER
5288 // points to the input section to be the owner a new stub table.
5290 template<bool big_endian>
5292 Arm_output_section<big_endian>::create_stub_group(
5293 Input_section_list::const_iterator begin,
5294 Input_section_list::const_iterator end,
5295 Input_section_list::const_iterator owner,
5296 Target_arm<big_endian>* target,
5297 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
5299 // We use a different kind of relaxed section in an EXIDX section.
5300 // The static casting from Output_relaxed_input_section to
5301 // Arm_input_section is invalid in an EXIDX section. We are okay
5302 // because we should not be calling this for an EXIDX section.
5303 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
5305 // Currently we convert ordinary input sections into relaxed sections only
5306 // at this point but we may want to support creating relaxed input section
5307 // very early. So we check here to see if owner is already a relaxed
5310 Arm_input_section<big_endian>* arm_input_section;
5311 if (owner->is_relaxed_input_section())
5314 Arm_input_section<big_endian>::as_arm_input_section(
5315 owner->relaxed_input_section());
5319 gold_assert(owner->is_input_section());
5320 // Create a new relaxed input section.
5322 target->new_arm_input_section(owner->relobj(), owner->shndx());
5323 new_relaxed_sections->push_back(arm_input_section);
5326 // Create a stub table.
5327 Stub_table<big_endian>* stub_table =
5328 target->new_stub_table(arm_input_section);
5330 arm_input_section->set_stub_table(stub_table);
5332 Input_section_list::const_iterator p = begin;
5333 Input_section_list::const_iterator prev_p;
5335 // Look for input sections or relaxed input sections in [begin ... end].
5338 if (p->is_input_section() || p->is_relaxed_input_section())
5340 // The stub table information for input sections live
5341 // in their objects.
5342 Arm_relobj<big_endian>* arm_relobj =
5343 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5344 arm_relobj->set_stub_table(p->shndx(), stub_table);
5348 while (prev_p != end);
5351 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
5352 // of stub groups. We grow a stub group by adding input section until the
5353 // size is just below GROUP_SIZE. The last input section will be converted
5354 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5355 // input section after the stub table, effectively double the group size.
5357 // This is similar to the group_sections() function in elf32-arm.c but is
5358 // implemented differently.
5360 template<bool big_endian>
5362 Arm_output_section<big_endian>::group_sections(
5363 section_size_type group_size,
5364 bool stubs_always_after_branch,
5365 Target_arm<big_endian>* target)
5367 // We only care about sections containing code.
5368 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5371 // States for grouping.
5374 // No group is being built.
5376 // A group is being built but the stub table is not found yet.
5377 // We keep group a stub group until the size is just under GROUP_SIZE.
5378 // The last input section in the group will be used as the stub table.
5379 FINDING_STUB_SECTION,
5380 // A group is being built and we have already found a stub table.
5381 // We enter this state to grow a stub group by adding input section
5382 // after the stub table. This effectively doubles the group size.
5386 // Any newly created relaxed sections are stored here.
5387 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
5389 State state = NO_GROUP;
5390 section_size_type off = 0;
5391 section_size_type group_begin_offset = 0;
5392 section_size_type group_end_offset = 0;
5393 section_size_type stub_table_end_offset = 0;
5394 Input_section_list::const_iterator group_begin =
5395 this->input_sections().end();
5396 Input_section_list::const_iterator stub_table =
5397 this->input_sections().end();
5398 Input_section_list::const_iterator group_end = this->input_sections().end();
5399 for (Input_section_list::const_iterator p = this->input_sections().begin();
5400 p != this->input_sections().end();
5403 section_size_type section_begin_offset =
5404 align_address(off, p->addralign());
5405 section_size_type section_end_offset =
5406 section_begin_offset + p->data_size();
5408 // Check to see if we should group the previously seens sections.
5414 case FINDING_STUB_SECTION:
5415 // Adding this section makes the group larger than GROUP_SIZE.
5416 if (section_end_offset - group_begin_offset >= group_size)
5418 if (stubs_always_after_branch)
5420 gold_assert(group_end != this->input_sections().end());
5421 this->create_stub_group(group_begin, group_end, group_end,
5422 target, &new_relaxed_sections);
5427 // But wait, there's more! Input sections up to
5428 // stub_group_size bytes after the stub table can be
5429 // handled by it too.
5430 state = HAS_STUB_SECTION;
5431 stub_table = group_end;
5432 stub_table_end_offset = group_end_offset;
5437 case HAS_STUB_SECTION:
5438 // Adding this section makes the post stub-section group larger
5440 if (section_end_offset - stub_table_end_offset >= group_size)
5442 gold_assert(group_end != this->input_sections().end());
5443 this->create_stub_group(group_begin, group_end, stub_table,
5444 target, &new_relaxed_sections);
5453 // If we see an input section and currently there is no group, start
5454 // a new one. Skip any empty sections.
5455 if ((p->is_input_section() || p->is_relaxed_input_section())
5456 && (p->relobj()->section_size(p->shndx()) != 0))
5458 if (state == NO_GROUP)
5460 state = FINDING_STUB_SECTION;
5462 group_begin_offset = section_begin_offset;
5465 // Keep track of the last input section seen.
5467 group_end_offset = section_end_offset;
5470 off = section_end_offset;
5473 // Create a stub group for any ungrouped sections.
5474 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5476 gold_assert(group_end != this->input_sections().end());
5477 this->create_stub_group(group_begin, group_end,
5478 (state == FINDING_STUB_SECTION
5481 target, &new_relaxed_sections);
5484 // Convert input section into relaxed input section in a batch.
5485 if (!new_relaxed_sections.empty())
5486 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5488 // Update the section offsets
5489 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5491 Arm_relobj<big_endian>* arm_relobj =
5492 Arm_relobj<big_endian>::as_arm_relobj(
5493 new_relaxed_sections[i]->relobj());
5494 unsigned int shndx = new_relaxed_sections[i]->shndx();
5495 // Tell Arm_relobj that this input section is converted.
5496 arm_relobj->convert_input_section_to_relaxed_section(shndx);
5500 // Append non empty text sections in this to LIST in ascending
5501 // order of their position in this.
5503 template<bool big_endian>
5505 Arm_output_section<big_endian>::append_text_sections_to_list(
5506 Text_section_list* list)
5508 // We only care about text sections.
5509 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5512 gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
5514 for (Input_section_list::const_iterator p = this->input_sections().begin();
5515 p != this->input_sections().end();
5518 // We only care about plain or relaxed input sections. We also
5519 // ignore any merged sections.
5520 if ((p->is_input_section() || p->is_relaxed_input_section())
5521 && p->data_size() != 0)
5522 list->push_back(Text_section_list::value_type(p->relobj(),
5527 template<bool big_endian>
5529 Arm_output_section<big_endian>::fix_exidx_coverage(
5531 const Text_section_list& sorted_text_sections,
5532 Symbol_table* symtab)
5534 // We should only do this for the EXIDX output section.
5535 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5537 // We don't want the relaxation loop to undo these changes, so we discard
5538 // the current saved states and take another one after the fix-up.
5539 this->discard_states();
5541 // Remove all input sections.
5542 uint64_t address = this->address();
5543 typedef std::list<Simple_input_section> Simple_input_section_list;
5544 Simple_input_section_list input_sections;
5545 this->reset_address_and_file_offset();
5546 this->get_input_sections(address, std::string(""), &input_sections);
5548 if (!this->input_sections().empty())
5549 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5551 // Go through all the known input sections and record them.
5552 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5553 Section_id_set known_input_sections;
5554 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5555 p != input_sections.end();
5558 // This should never happen. At this point, we should only see
5559 // plain EXIDX input sections.
5560 gold_assert(!p->is_relaxed_input_section());
5561 known_input_sections.insert(Section_id(p->relobj(), p->shndx()));
5564 Arm_exidx_fixup exidx_fixup(this);
5566 // Go over the sorted text sections.
5567 Section_id_set processed_input_sections;
5568 for (Text_section_list::const_iterator p = sorted_text_sections.begin();
5569 p != sorted_text_sections.end();
5572 Relobj* relobj = p->first;
5573 unsigned int shndx = p->second;
5575 Arm_relobj<big_endian>* arm_relobj =
5576 Arm_relobj<big_endian>::as_arm_relobj(relobj);
5577 const Arm_exidx_input_section* exidx_input_section =
5578 arm_relobj->exidx_input_section_by_link(shndx);
5580 // If this text section has no EXIDX section, force an EXIDX_CANTUNWIND
5581 // entry pointing to the end of the last seen EXIDX section.
5582 if (exidx_input_section == NULL)
5584 exidx_fixup.add_exidx_cantunwind_as_needed();
5588 Relobj* exidx_relobj = exidx_input_section->relobj();
5589 unsigned int exidx_shndx = exidx_input_section->shndx();
5590 Section_id sid(exidx_relobj, exidx_shndx);
5591 if (known_input_sections.find(sid) == known_input_sections.end())
5593 // This is odd. We have not seen this EXIDX input section before.
5594 // We cannot do fix-up. If we saw a SECTIONS clause in a script,
5595 // issue a warning instead. We assume the user knows what he
5596 // or she is doing. Otherwise, this is an error.
5597 if (layout->script_options()->saw_sections_clause())
5598 gold_warning(_("unwinding may not work because EXIDX input section"
5599 " %u of %s is not in EXIDX output section"),
5600 exidx_shndx, exidx_relobj->name().c_str());
5602 gold_error(_("unwinding may not work because EXIDX input section"
5603 " %u of %s is not in EXIDX output section"),
5604 exidx_shndx, exidx_relobj->name().c_str());
5606 exidx_fixup.add_exidx_cantunwind_as_needed();
5610 // Fix up coverage and append input section to output data list.
5611 Arm_exidx_section_offset_map* section_offset_map = NULL;
5612 uint32_t deleted_bytes =
5613 exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
5614 §ion_offset_map);
5616 if (deleted_bytes == exidx_input_section->size())
5618 // The whole EXIDX section got merged. Remove it from output.
5619 gold_assert(section_offset_map == NULL);
5620 exidx_relobj->set_output_section(exidx_shndx, NULL);
5622 // All local symbols defined in this input section will be dropped.
5623 // We need to adjust output local symbol count.
5624 arm_relobj->set_output_local_symbol_count_needs_update();
5626 else if (deleted_bytes > 0)
5628 // Some entries are merged. We need to convert this EXIDX input
5629 // section into a relaxed section.
5630 gold_assert(section_offset_map != NULL);
5631 Arm_exidx_merged_section* merged_section =
5632 new Arm_exidx_merged_section(*exidx_input_section,
5633 *section_offset_map, deleted_bytes);
5634 this->add_relaxed_input_section(merged_section);
5635 arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
5637 // All local symbols defined in discarded portions of this input
5638 // section will be dropped. We need to adjust output local symbol
5640 arm_relobj->set_output_local_symbol_count_needs_update();
5644 // Just add back the EXIDX input section.
5645 gold_assert(section_offset_map == NULL);
5646 Output_section::Simple_input_section sis(exidx_relobj, exidx_shndx);
5647 this->add_simple_input_section(sis, exidx_input_section->size(),
5648 exidx_input_section->addralign());
5651 processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx));
5654 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5655 exidx_fixup.add_exidx_cantunwind_as_needed();
5657 // Remove any known EXIDX input sections that are not processed.
5658 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5659 p != input_sections.end();
5662 if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
5663 == processed_input_sections.end())
5665 // We only discard a known EXIDX section because its linked
5666 // text section has been folded by ICF.
5667 Arm_relobj<big_endian>* arm_relobj =
5668 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5669 const Arm_exidx_input_section* exidx_input_section =
5670 arm_relobj->exidx_input_section_by_shndx(p->shndx());
5671 gold_assert(exidx_input_section != NULL);
5672 unsigned int text_shndx = exidx_input_section->link();
5673 gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
5675 // Remove this from link.
5676 p->relobj()->set_output_section(p->shndx(), NULL);
5680 // Link exidx output section to the first seen output section and
5681 // set correct entry size.
5682 this->set_link_section(exidx_fixup.first_output_text_section());
5683 this->set_entsize(8);
5685 // Make changes permanent.
5686 this->save_states();
5687 this->set_section_offsets_need_adjustment();
5690 // Arm_relobj methods.
5692 // Determine if an input section is scannable for stub processing. SHDR is
5693 // the header of the section and SHNDX is the section index. OS is the output
5694 // section for the input section and SYMTAB is the global symbol table used to
5695 // look up ICF information.
5697 template<bool big_endian>
5699 Arm_relobj<big_endian>::section_is_scannable(
5700 const elfcpp::Shdr<32, big_endian>& shdr,
5702 const Output_section* os,
5703 const Symbol_table *symtab)
5705 // Skip any empty sections, unallocated sections or sections whose
5706 // type are not SHT_PROGBITS.
5707 if (shdr.get_sh_size() == 0
5708 || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
5709 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
5712 // Skip any discarded or ICF'ed sections.
5713 if (os == NULL || symtab->is_section_folded(this, shndx))
5716 // If this requires special offset handling, check to see if it is
5717 // a relaxed section. If this is not, then it is a merged section that
5718 // we cannot handle.
5719 if (this->is_output_section_offset_invalid(shndx))
5721 const Output_relaxed_input_section* poris =
5722 os->find_relaxed_input_section(this, shndx);
5730 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5731 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5733 template<bool big_endian>
5735 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
5736 const elfcpp::Shdr<32, big_endian>& shdr,
5737 const Relobj::Output_sections& out_sections,
5738 const Symbol_table *symtab,
5739 const unsigned char* pshdrs)
5741 unsigned int sh_type = shdr.get_sh_type();
5742 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
5745 // Ignore empty section.
5746 off_t sh_size = shdr.get_sh_size();
5750 // Ignore reloc section with unexpected symbol table. The
5751 // error will be reported in the final link.
5752 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
5755 unsigned int reloc_size;
5756 if (sh_type == elfcpp::SHT_REL)
5757 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5759 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5761 // Ignore reloc section with unexpected entsize or uneven size.
5762 // The error will be reported in the final link.
5763 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
5766 // Ignore reloc section with bad info. This error will be
5767 // reported in the final link.
5768 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5769 if (index >= this->shnum())
5772 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5773 const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
5774 return this->section_is_scannable(text_shdr, index,
5775 out_sections[index], symtab);
5778 // Return the output address of either a plain input section or a relaxed
5779 // input section. SHNDX is the section index. We define and use this
5780 // instead of calling Output_section::output_address because that is slow
5781 // for large output.
5783 template<bool big_endian>
5785 Arm_relobj<big_endian>::simple_input_section_output_address(
5789 if (this->is_output_section_offset_invalid(shndx))
5791 const Output_relaxed_input_section* poris =
5792 os->find_relaxed_input_section(this, shndx);
5793 // We do not handle merged sections here.
5794 gold_assert(poris != NULL);
5795 return poris->address();
5798 return os->address() + this->get_output_section_offset(shndx);
5801 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
5802 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5804 template<bool big_endian>
5806 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
5807 const elfcpp::Shdr<32, big_endian>& shdr,
5810 const Symbol_table* symtab)
5812 if (!this->section_is_scannable(shdr, shndx, os, symtab))
5815 // If the section does not cross any 4K-boundaries, it does not need to
5817 Arm_address address = this->simple_input_section_output_address(shndx, os);
5818 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
5824 // Scan a section for Cortex-A8 workaround.
5826 template<bool big_endian>
5828 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
5829 const elfcpp::Shdr<32, big_endian>& shdr,
5832 Target_arm<big_endian>* arm_target)
5834 // Look for the first mapping symbol in this section. It should be
5836 Mapping_symbol_position section_start(shndx, 0);
5837 typename Mapping_symbols_info::const_iterator p =
5838 this->mapping_symbols_info_.lower_bound(section_start);
5840 // There are no mapping symbols for this section. Treat it as a data-only
5842 if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
5845 Arm_address output_address =
5846 this->simple_input_section_output_address(shndx, os);
5848 // Get the section contents.
5849 section_size_type input_view_size = 0;
5850 const unsigned char* input_view =
5851 this->section_contents(shndx, &input_view_size, false);
5853 // We need to go through the mapping symbols to determine what to
5854 // scan. There are two reasons. First, we should look at THUMB code and
5855 // THUMB code only. Second, we only want to look at the 4K-page boundary
5856 // to speed up the scanning.
5858 while (p != this->mapping_symbols_info_.end()
5859 && p->first.first == shndx)
5861 typename Mapping_symbols_info::const_iterator next =
5862 this->mapping_symbols_info_.upper_bound(p->first);
5864 // Only scan part of a section with THUMB code.
5865 if (p->second == 't')
5867 // Determine the end of this range.
5868 section_size_type span_start =
5869 convert_to_section_size_type(p->first.second);
5870 section_size_type span_end;
5871 if (next != this->mapping_symbols_info_.end()
5872 && next->first.first == shndx)
5873 span_end = convert_to_section_size_type(next->first.second);
5875 span_end = convert_to_section_size_type(shdr.get_sh_size());
5877 if (((span_start + output_address) & ~0xfffUL)
5878 != ((span_end + output_address - 1) & ~0xfffUL))
5880 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
5881 span_start, span_end,
5891 // Scan relocations for stub generation.
5893 template<bool big_endian>
5895 Arm_relobj<big_endian>::scan_sections_for_stubs(
5896 Target_arm<big_endian>* arm_target,
5897 const Symbol_table* symtab,
5898 const Layout* layout)
5900 unsigned int shnum = this->shnum();
5901 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5903 // Read the section headers.
5904 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
5908 // To speed up processing, we set up hash tables for fast lookup of
5909 // input offsets to output addresses.
5910 this->initialize_input_to_output_maps();
5912 const Relobj::Output_sections& out_sections(this->output_sections());
5914 Relocate_info<32, big_endian> relinfo;
5915 relinfo.symtab = symtab;
5916 relinfo.layout = layout;
5917 relinfo.object = this;
5919 // Do relocation stubs scanning.
5920 const unsigned char* p = pshdrs + shdr_size;
5921 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5923 const elfcpp::Shdr<32, big_endian> shdr(p);
5924 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
5927 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5928 Arm_address output_offset = this->get_output_section_offset(index);
5929 Arm_address output_address;
5930 if(output_offset != invalid_address)
5931 output_address = out_sections[index]->address() + output_offset;
5934 // Currently this only happens for a relaxed section.
5935 const Output_relaxed_input_section* poris =
5936 out_sections[index]->find_relaxed_input_section(this, index);
5937 gold_assert(poris != NULL);
5938 output_address = poris->address();
5941 // Get the relocations.
5942 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
5946 // Get the section contents. This does work for the case in which
5947 // we modify the contents of an input section. We need to pass the
5948 // output view under such circumstances.
5949 section_size_type input_view_size = 0;
5950 const unsigned char* input_view =
5951 this->section_contents(index, &input_view_size, false);
5953 relinfo.reloc_shndx = i;
5954 relinfo.data_shndx = index;
5955 unsigned int sh_type = shdr.get_sh_type();
5956 unsigned int reloc_size;
5957 if (sh_type == elfcpp::SHT_REL)
5958 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5960 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5962 Output_section* os = out_sections[index];
5963 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
5964 shdr.get_sh_size() / reloc_size,
5966 output_offset == invalid_address,
5967 input_view, output_address,
5972 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
5973 // after its relocation section, if there is one, is processed for
5974 // relocation stubs. Merging this loop with the one above would have been
5975 // complicated since we would have had to make sure that relocation stub
5976 // scanning is done first.
5977 if (arm_target->fix_cortex_a8())
5979 const unsigned char* p = pshdrs + shdr_size;
5980 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5982 const elfcpp::Shdr<32, big_endian> shdr(p);
5983 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
5986 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
5991 // After we've done the relocations, we release the hash tables,
5992 // since we no longer need them.
5993 this->free_input_to_output_maps();
5996 // Count the local symbols. The ARM backend needs to know if a symbol
5997 // is a THUMB function or not. For global symbols, it is easy because
5998 // the Symbol object keeps the ELF symbol type. For local symbol it is
5999 // harder because we cannot access this information. So we override the
6000 // do_count_local_symbol in parent and scan local symbols to mark
6001 // THUMB functions. This is not the most efficient way but I do not want to
6002 // slow down other ports by calling a per symbol targer hook inside
6003 // Sized_relobj<size, big_endian>::do_count_local_symbols.
6005 template<bool big_endian>
6007 Arm_relobj<big_endian>::do_count_local_symbols(
6008 Stringpool_template<char>* pool,
6009 Stringpool_template<char>* dynpool)
6011 // We need to fix-up the values of any local symbols whose type are
6014 // Ask parent to count the local symbols.
6015 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
6016 const unsigned int loccount = this->local_symbol_count();
6020 // Intialize the thumb function bit-vector.
6021 std::vector<bool> empty_vector(loccount, false);
6022 this->local_symbol_is_thumb_function_.swap(empty_vector);
6024 // Read the symbol table section header.
6025 const unsigned int symtab_shndx = this->symtab_shndx();
6026 elfcpp::Shdr<32, big_endian>
6027 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6028 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6030 // Read the local symbols.
6031 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6032 gold_assert(loccount == symtabshdr.get_sh_info());
6033 off_t locsize = loccount * sym_size;
6034 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6035 locsize, true, true);
6037 // For mapping symbol processing, we need to read the symbol names.
6038 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
6039 if (strtab_shndx >= this->shnum())
6041 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
6045 elfcpp::Shdr<32, big_endian>
6046 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
6047 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
6049 this->error(_("symbol table name section has wrong type: %u"),
6050 static_cast<unsigned int>(strtabshdr.get_sh_type()));
6053 const char* pnames =
6054 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
6055 strtabshdr.get_sh_size(),
6058 // Loop over the local symbols and mark any local symbols pointing
6059 // to THUMB functions.
6061 // Skip the first dummy symbol.
6063 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
6064 this->local_values();
6065 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6067 elfcpp::Sym<32, big_endian> sym(psyms);
6068 elfcpp::STT st_type = sym.get_st_type();
6069 Symbol_value<32>& lv((*plocal_values)[i]);
6070 Arm_address input_value = lv.input_value();
6072 // Check to see if this is a mapping symbol.
6073 const char* sym_name = pnames + sym.get_st_name();
6074 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
6076 unsigned int input_shndx = sym.get_st_shndx();
6078 // Strip of LSB in case this is a THUMB symbol.
6079 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
6080 this->mapping_symbols_info_[msp] = sym_name[1];
6083 if (st_type == elfcpp::STT_ARM_TFUNC
6084 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
6086 // This is a THUMB function. Mark this and canonicalize the
6087 // symbol value by setting LSB.
6088 this->local_symbol_is_thumb_function_[i] = true;
6089 if ((input_value & 1) == 0)
6090 lv.set_input_value(input_value | 1);
6095 // Relocate sections.
6096 template<bool big_endian>
6098 Arm_relobj<big_endian>::do_relocate_sections(
6099 const Symbol_table* symtab,
6100 const Layout* layout,
6101 const unsigned char* pshdrs,
6102 typename Sized_relobj<32, big_endian>::Views* pviews)
6104 // Call parent to relocate sections.
6105 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
6108 // We do not generate stubs if doing a relocatable link.
6109 if (parameters->options().relocatable())
6112 // Relocate stub tables.
6113 unsigned int shnum = this->shnum();
6115 Target_arm<big_endian>* arm_target =
6116 Target_arm<big_endian>::default_target();
6118 Relocate_info<32, big_endian> relinfo;
6119 relinfo.symtab = symtab;
6120 relinfo.layout = layout;
6121 relinfo.object = this;
6123 for (unsigned int i = 1; i < shnum; ++i)
6125 Arm_input_section<big_endian>* arm_input_section =
6126 arm_target->find_arm_input_section(this, i);
6128 if (arm_input_section != NULL
6129 && arm_input_section->is_stub_table_owner()
6130 && !arm_input_section->stub_table()->empty())
6132 // We cannot discard a section if it owns a stub table.
6133 Output_section* os = this->output_section(i);
6134 gold_assert(os != NULL);
6136 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
6137 relinfo.reloc_shdr = NULL;
6138 relinfo.data_shndx = i;
6139 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
6141 gold_assert((*pviews)[i].view != NULL);
6143 // We are passed the output section view. Adjust it to cover the
6145 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
6146 gold_assert((stub_table->address() >= (*pviews)[i].address)
6147 && ((stub_table->address() + stub_table->data_size())
6148 <= (*pviews)[i].address + (*pviews)[i].view_size));
6150 off_t offset = stub_table->address() - (*pviews)[i].address;
6151 unsigned char* view = (*pviews)[i].view + offset;
6152 Arm_address address = stub_table->address();
6153 section_size_type view_size = stub_table->data_size();
6155 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
6159 // Apply Cortex A8 workaround if applicable.
6160 if (this->section_has_cortex_a8_workaround(i))
6162 unsigned char* view = (*pviews)[i].view;
6163 Arm_address view_address = (*pviews)[i].address;
6164 section_size_type view_size = (*pviews)[i].view_size;
6165 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
6167 // Adjust view to cover section.
6168 Output_section* os = this->output_section(i);
6169 gold_assert(os != NULL);
6170 Arm_address section_address =
6171 this->simple_input_section_output_address(i, os);
6172 uint64_t section_size = this->section_size(i);
6174 gold_assert(section_address >= view_address
6175 && ((section_address + section_size)
6176 <= (view_address + view_size)));
6178 unsigned char* section_view = view + (section_address - view_address);
6180 // Apply the Cortex-A8 workaround to the output address range
6181 // corresponding to this input section.
6182 stub_table->apply_cortex_a8_workaround_to_address_range(
6191 // Find the linked text section of an EXIDX section by looking the the first
6192 // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6193 // must be linked to to its associated code section via the sh_link field of
6194 // its section header. However, some tools are broken and the link is not
6195 // always set. LD just drops such an EXIDX section silently, causing the
6196 // associated code not unwindabled. Here we try a little bit harder to
6197 // discover the linked code section.
6199 // PSHDR points to the section header of a relocation section of an EXIDX
6200 // section. If we can find a linked text section, return true and
6201 // store the text section index in the location PSHNDX. Otherwise
6204 template<bool big_endian>
6206 Arm_relobj<big_endian>::find_linked_text_section(
6207 const unsigned char* pshdr,
6208 const unsigned char* psyms,
6209 unsigned int* pshndx)
6211 elfcpp::Shdr<32, big_endian> shdr(pshdr);
6213 // If there is no relocation, we cannot find the linked text section.
6215 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6216 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6218 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6219 size_t reloc_count = shdr.get_sh_size() / reloc_size;
6221 // Get the relocations.
6222 const unsigned char* prelocs =
6223 this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false);
6225 // Find the REL31 relocation for the first word of the first EXIDX entry.
6226 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6228 Arm_address r_offset;
6229 typename elfcpp::Elf_types<32>::Elf_WXword r_info;
6230 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6232 typename elfcpp::Rel<32, big_endian> reloc(prelocs);
6233 r_info = reloc.get_r_info();
6234 r_offset = reloc.get_r_offset();
6238 typename elfcpp::Rela<32, big_endian> reloc(prelocs);
6239 r_info = reloc.get_r_info();
6240 r_offset = reloc.get_r_offset();
6243 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6244 if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
6247 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6249 || r_sym >= this->local_symbol_count()
6253 // This is the relocation for the first word of the first EXIDX entry.
6254 // We expect to see a local section symbol.
6255 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6256 elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
6257 if (sym.get_st_type() == elfcpp::STT_SECTION)
6259 *pshndx = this->adjust_shndx(sym.get_st_shndx());
6269 // Make an EXIDX input section object for an EXIDX section whose index is
6270 // SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
6271 // is the section index of the linked text section.
6273 template<bool big_endian>
6275 Arm_relobj<big_endian>::make_exidx_input_section(
6277 const elfcpp::Shdr<32, big_endian>& shdr,
6278 unsigned int text_shndx)
6280 // Issue an error and ignore this EXIDX section if it points to a text
6281 // section already has an EXIDX section.
6282 if (this->exidx_section_map_[text_shndx] != NULL)
6284 gold_error(_("EXIDX sections %u and %u both link to text section %u "
6286 shndx, this->exidx_section_map_[text_shndx]->shndx(),
6287 text_shndx, this->name().c_str());
6291 // Create an Arm_exidx_input_section object for this EXIDX section.
6292 Arm_exidx_input_section* exidx_input_section =
6293 new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
6294 shdr.get_sh_addralign());
6295 this->exidx_section_map_[text_shndx] = exidx_input_section;
6297 // Also map the EXIDX section index to this.
6298 gold_assert(this->exidx_section_map_[shndx] == NULL);
6299 this->exidx_section_map_[shndx] = exidx_input_section;
6302 // Read the symbol information.
6304 template<bool big_endian>
6306 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6308 // Call parent class to read symbol information.
6309 Sized_relobj<32, big_endian>::do_read_symbols(sd);
6311 // Read processor-specific flags in ELF file header.
6312 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6313 elfcpp::Elf_sizes<32>::ehdr_size,
6315 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6316 this->processor_specific_flags_ = ehdr.get_e_flags();
6318 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6320 std::vector<unsigned int> deferred_exidx_sections;
6321 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6322 const unsigned char* pshdrs = sd->section_headers->data();
6323 const unsigned char *ps = pshdrs + shdr_size;
6324 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6326 elfcpp::Shdr<32, big_endian> shdr(ps);
6327 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6329 gold_assert(this->attributes_section_data_ == NULL);
6330 section_offset_type section_offset = shdr.get_sh_offset();
6331 section_size_type section_size =
6332 convert_to_section_size_type(shdr.get_sh_size());
6333 File_view* view = this->get_lasting_view(section_offset,
6334 section_size, true, false);
6335 this->attributes_section_data_ =
6336 new Attributes_section_data(view->data(), section_size);
6338 else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6340 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6341 if (text_shndx >= this->shnum())
6342 gold_error(_("EXIDX section %u linked to invalid section %u"),
6344 else if (text_shndx == elfcpp::SHN_UNDEF)
6345 deferred_exidx_sections.push_back(i);
6347 this->make_exidx_input_section(i, shdr, text_shndx);
6351 // Some tools are broken and they do not set the link of EXIDX sections.
6352 // We look at the first relocation to figure out the linked sections.
6353 if (!deferred_exidx_sections.empty())
6355 // We need to go over the section headers again to find the mapping
6356 // from sections being relocated to their relocation sections. This is
6357 // a bit inefficient as we could do that in the loop above. However,
6358 // we do not expect any deferred EXIDX sections normally. So we do not
6359 // want to slow down the most common path.
6360 typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
6361 Reloc_map reloc_map;
6362 ps = pshdrs + shdr_size;
6363 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6365 elfcpp::Shdr<32, big_endian> shdr(ps);
6366 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
6367 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
6369 unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
6370 if (info_shndx >= this->shnum())
6371 gold_error(_("relocation section %u has invalid info %u"),
6373 Reloc_map::value_type value(info_shndx, i);
6374 std::pair<Reloc_map::iterator, bool> result =
6375 reloc_map.insert(value);
6377 gold_error(_("section %u has multiple relocation sections "
6379 info_shndx, i, reloc_map[info_shndx]);
6383 // Read the symbol table section header.
6384 const unsigned int symtab_shndx = this->symtab_shndx();
6385 elfcpp::Shdr<32, big_endian>
6386 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6387 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6389 // Read the local symbols.
6390 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6391 const unsigned int loccount = this->local_symbol_count();
6392 gold_assert(loccount == symtabshdr.get_sh_info());
6393 off_t locsize = loccount * sym_size;
6394 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6395 locsize, true, true);
6397 // Process the deferred EXIDX sections.
6398 for(unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
6400 unsigned int shndx = deferred_exidx_sections[i];
6401 elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
6402 unsigned int text_shndx;
6403 Reloc_map::const_iterator it = reloc_map.find(shndx);
6404 if (it != reloc_map.end()
6405 && find_linked_text_section(pshdrs + it->second * shdr_size,
6406 psyms, &text_shndx))
6407 this->make_exidx_input_section(shndx, shdr, text_shndx);
6409 gold_error(_("EXIDX section %u has no linked text section."),
6415 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
6416 // sections for unwinding. These sections are referenced implicitly by
6417 // text sections linked in the section headers. If we ignore these implict
6418 // references, the .ARM.exidx sections and any .ARM.extab sections they use
6419 // will be garbage-collected incorrectly. Hence we override the same function
6420 // in the base class to handle these implicit references.
6422 template<bool big_endian>
6424 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
6426 Read_relocs_data* rd)
6428 // First, call base class method to process relocations in this object.
6429 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
6431 // If --gc-sections is not specified, there is nothing more to do.
6432 // This happens when --icf is used but --gc-sections is not.
6433 if (!parameters->options().gc_sections())
6436 unsigned int shnum = this->shnum();
6437 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6438 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6442 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
6443 // to these from the linked text sections.
6444 const unsigned char* ps = pshdrs + shdr_size;
6445 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
6447 elfcpp::Shdr<32, big_endian> shdr(ps);
6448 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6450 // Found an .ARM.exidx section, add it to the set of reachable
6451 // sections from its linked text section.
6452 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6453 symtab->gc()->add_reference(this, text_shndx, this, i);
6458 // Update output local symbol count. Owing to EXIDX entry merging, some local
6459 // symbols will be removed in output. Adjust output local symbol count
6460 // accordingly. We can only changed the static output local symbol count. It
6461 // is too late to change the dynamic symbols.
6463 template<bool big_endian>
6465 Arm_relobj<big_endian>::update_output_local_symbol_count()
6467 // Caller should check that this needs updating. We want caller checking
6468 // because output_local_symbol_count_needs_update() is most likely inlined.
6469 gold_assert(this->output_local_symbol_count_needs_update_);
6471 gold_assert(this->symtab_shndx() != -1U);
6472 if (this->symtab_shndx() == 0)
6474 // This object has no symbols. Weird but legal.
6478 // Read the symbol table section header.
6479 const unsigned int symtab_shndx = this->symtab_shndx();
6480 elfcpp::Shdr<32, big_endian>
6481 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6482 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6484 // Read the local symbols.
6485 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6486 const unsigned int loccount = this->local_symbol_count();
6487 gold_assert(loccount == symtabshdr.get_sh_info());
6488 off_t locsize = loccount * sym_size;
6489 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6490 locsize, true, true);
6492 // Loop over the local symbols.
6494 typedef typename Sized_relobj<32, big_endian>::Output_sections
6496 const Output_sections& out_sections(this->output_sections());
6497 unsigned int shnum = this->shnum();
6498 unsigned int count = 0;
6499 // Skip the first, dummy, symbol.
6501 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6503 elfcpp::Sym<32, big_endian> sym(psyms);
6505 Symbol_value<32>& lv((*this->local_values())[i]);
6507 // This local symbol was already discarded by do_count_local_symbols.
6508 if (!lv.needs_output_symtab_entry())
6512 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
6517 Output_section* os = out_sections[shndx];
6519 // This local symbol no longer has an output section. Discard it.
6522 lv.set_no_output_symtab_entry();
6526 // Currently we only discard parts of EXIDX input sections.
6527 // We explicitly check for a merged EXIDX input section to avoid
6528 // calling Output_section_data::output_offset unless necessary.
6529 if ((this->get_output_section_offset(shndx) == invalid_address)
6530 && (this->exidx_input_section_by_shndx(shndx) != NULL))
6532 section_offset_type output_offset =
6533 os->output_offset(this, shndx, lv.input_value());
6534 if (output_offset == -1)
6536 // This symbol is defined in a part of an EXIDX input section
6537 // that is discarded due to entry merging.
6538 lv.set_no_output_symtab_entry();
6547 this->set_output_local_symbol_count(count);
6548 this->output_local_symbol_count_needs_update_ = false;
6551 // Arm_dynobj methods.
6553 // Read the symbol information.
6555 template<bool big_endian>
6557 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6559 // Call parent class to read symbol information.
6560 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
6562 // Read processor-specific flags in ELF file header.
6563 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6564 elfcpp::Elf_sizes<32>::ehdr_size,
6566 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6567 this->processor_specific_flags_ = ehdr.get_e_flags();
6569 // Read the attributes section if there is one.
6570 // We read from the end because gas seems to put it near the end of
6571 // the section headers.
6572 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6573 const unsigned char *ps =
6574 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
6575 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
6577 elfcpp::Shdr<32, big_endian> shdr(ps);
6578 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6580 section_offset_type section_offset = shdr.get_sh_offset();
6581 section_size_type section_size =
6582 convert_to_section_size_type(shdr.get_sh_size());
6583 File_view* view = this->get_lasting_view(section_offset,
6584 section_size, true, false);
6585 this->attributes_section_data_ =
6586 new Attributes_section_data(view->data(), section_size);
6592 // Stub_addend_reader methods.
6594 // Read the addend of a REL relocation of type R_TYPE at VIEW.
6596 template<bool big_endian>
6597 elfcpp::Elf_types<32>::Elf_Swxword
6598 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
6599 unsigned int r_type,
6600 const unsigned char* view,
6601 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
6603 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
6607 case elfcpp::R_ARM_CALL:
6608 case elfcpp::R_ARM_JUMP24:
6609 case elfcpp::R_ARM_PLT32:
6611 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6612 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6613 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
6614 return utils::sign_extend<26>(val << 2);
6617 case elfcpp::R_ARM_THM_CALL:
6618 case elfcpp::R_ARM_THM_JUMP24:
6619 case elfcpp::R_ARM_THM_XPC22:
6621 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6622 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6623 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6624 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6625 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
6628 case elfcpp::R_ARM_THM_JUMP19:
6630 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6631 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6632 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6633 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6634 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
6642 // Arm_output_data_got methods.
6644 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
6645 // The first one is initialized to be 1, which is the module index for
6646 // the main executable and the second one 0. A reloc of the type
6647 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
6648 // be applied by gold. GSYM is a global symbol.
6650 template<bool big_endian>
6652 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
6653 unsigned int got_type,
6656 if (gsym->has_got_offset(got_type))
6659 // We are doing a static link. Just mark it as belong to module 1,
6661 unsigned int got_offset = this->add_constant(1);
6662 gsym->set_got_offset(got_type, got_offset);
6663 got_offset = this->add_constant(0);
6664 this->static_relocs_.push_back(Static_reloc(got_offset,
6665 elfcpp::R_ARM_TLS_DTPOFF32,
6669 // Same as the above but for a local symbol.
6671 template<bool big_endian>
6673 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
6674 unsigned int got_type,
6675 Sized_relobj<32, big_endian>* object,
6678 if (object->local_has_got_offset(index, got_type))
6681 // We are doing a static link. Just mark it as belong to module 1,
6683 unsigned int got_offset = this->add_constant(1);
6684 object->set_local_got_offset(index, got_type, got_offset);
6685 got_offset = this->add_constant(0);
6686 this->static_relocs_.push_back(Static_reloc(got_offset,
6687 elfcpp::R_ARM_TLS_DTPOFF32,
6691 template<bool big_endian>
6693 Arm_output_data_got<big_endian>::do_write(Output_file* of)
6695 // Call parent to write out GOT.
6696 Output_data_got<32, big_endian>::do_write(of);
6698 // We are done if there is no fix up.
6699 if (this->static_relocs_.empty())
6702 gold_assert(parameters->doing_static_link());
6704 const off_t offset = this->offset();
6705 const section_size_type oview_size =
6706 convert_to_section_size_type(this->data_size());
6707 unsigned char* const oview = of->get_output_view(offset, oview_size);
6709 Output_segment* tls_segment = this->layout_->tls_segment();
6710 gold_assert(tls_segment != NULL);
6712 // The thread pointer $tp points to the TCB, which is followed by the
6713 // TLS. So we need to adjust $tp relative addressing by this amount.
6714 Arm_address aligned_tcb_size =
6715 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
6717 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6719 Static_reloc& reloc(this->static_relocs_[i]);
6722 if (!reloc.symbol_is_global())
6724 Sized_relobj<32, big_endian>* object = reloc.relobj();
6725 const Symbol_value<32>* psymval =
6726 reloc.relobj()->local_symbol(reloc.index());
6728 // We are doing static linking. Issue an error and skip this
6729 // relocation if the symbol is undefined or in a discarded_section.
6731 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6732 if ((shndx == elfcpp::SHN_UNDEF)
6734 && shndx != elfcpp::SHN_UNDEF
6735 && !object->is_section_included(shndx)
6736 && !this->symbol_table_->is_section_folded(object, shndx)))
6738 gold_error(_("undefined or discarded local symbol %u from "
6739 " object %s in GOT"),
6740 reloc.index(), reloc.relobj()->name().c_str());
6744 value = psymval->value(object, 0);
6748 const Symbol* gsym = reloc.symbol();
6749 gold_assert(gsym != NULL);
6750 if (gsym->is_forwarder())
6751 gsym = this->symbol_table_->resolve_forwards(gsym);
6753 // We are doing static linking. Issue an error and skip this
6754 // relocation if the symbol is undefined or in a discarded_section
6755 // unless it is a weakly_undefined symbol.
6756 if ((gsym->is_defined_in_discarded_section()
6757 || gsym->is_undefined())
6758 && !gsym->is_weak_undefined())
6760 gold_error(_("undefined or discarded symbol %s in GOT"),
6765 if (!gsym->is_weak_undefined())
6767 const Sized_symbol<32>* sym =
6768 static_cast<const Sized_symbol<32>*>(gsym);
6769 value = sym->value();
6775 unsigned got_offset = reloc.got_offset();
6776 gold_assert(got_offset < oview_size);
6778 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6779 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6781 switch (reloc.r_type())
6783 case elfcpp::R_ARM_TLS_DTPOFF32:
6786 case elfcpp::R_ARM_TLS_TPOFF32:
6787 x = value + aligned_tcb_size;
6792 elfcpp::Swap<32, big_endian>::writeval(wv, x);
6795 of->write_output_view(offset, oview_size, oview);
6798 // A class to handle the PLT data.
6800 template<bool big_endian>
6801 class Output_data_plt_arm : public Output_section_data
6804 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
6807 Output_data_plt_arm(Layout*, Output_data_space*);
6809 // Add an entry to the PLT.
6811 add_entry(Symbol* gsym);
6813 // Return the .rel.plt section data.
6814 const Reloc_section*
6816 { return this->rel_; }
6820 do_adjust_output_section(Output_section* os);
6822 // Write to a map file.
6824 do_print_to_mapfile(Mapfile* mapfile) const
6825 { mapfile->print_output_data(this, _("** PLT")); }
6828 // Template for the first PLT entry.
6829 static const uint32_t first_plt_entry[5];
6831 // Template for subsequent PLT entries.
6832 static const uint32_t plt_entry[3];
6834 // Set the final size.
6836 set_final_data_size()
6838 this->set_data_size(sizeof(first_plt_entry)
6839 + this->count_ * sizeof(plt_entry));
6842 // Write out the PLT data.
6844 do_write(Output_file*);
6846 // The reloc section.
6847 Reloc_section* rel_;
6848 // The .got.plt section.
6849 Output_data_space* got_plt_;
6850 // The number of PLT entries.
6851 unsigned int count_;
6854 // Create the PLT section. The ordinary .got section is an argument,
6855 // since we need to refer to the start. We also create our own .got
6856 // section just for PLT entries.
6858 template<bool big_endian>
6859 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
6860 Output_data_space* got_plt)
6861 : Output_section_data(4), got_plt_(got_plt), count_(0)
6863 this->rel_ = new Reloc_section(false);
6864 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
6865 elfcpp::SHF_ALLOC, this->rel_, true, false,
6869 template<bool big_endian>
6871 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
6876 // Add an entry to the PLT.
6878 template<bool big_endian>
6880 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
6882 gold_assert(!gsym->has_plt_offset());
6884 // Note that when setting the PLT offset we skip the initial
6885 // reserved PLT entry.
6886 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
6887 + sizeof(first_plt_entry));
6891 section_offset_type got_offset = this->got_plt_->current_data_size();
6893 // Every PLT entry needs a GOT entry which points back to the PLT
6894 // entry (this will be changed by the dynamic linker, normally
6895 // lazily when the function is called).
6896 this->got_plt_->set_current_data_size(got_offset + 4);
6898 // Every PLT entry needs a reloc.
6899 gsym->set_needs_dynsym_entry();
6900 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
6903 // Note that we don't need to save the symbol. The contents of the
6904 // PLT are independent of which symbols are used. The symbols only
6905 // appear in the relocations.
6909 // FIXME: This is not very flexible. Right now this has only been tested
6910 // on armv5te. If we are to support additional architecture features like
6911 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
6913 // The first entry in the PLT.
6914 template<bool big_endian>
6915 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
6917 0xe52de004, // str lr, [sp, #-4]!
6918 0xe59fe004, // ldr lr, [pc, #4]
6919 0xe08fe00e, // add lr, pc, lr
6920 0xe5bef008, // ldr pc, [lr, #8]!
6921 0x00000000, // &GOT[0] - .
6924 // Subsequent entries in the PLT.
6926 template<bool big_endian>
6927 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
6929 0xe28fc600, // add ip, pc, #0xNN00000
6930 0xe28cca00, // add ip, ip, #0xNN000
6931 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
6934 // Write out the PLT. This uses the hand-coded instructions above,
6935 // and adjusts them as needed. This is all specified by the arm ELF
6936 // Processor Supplement.
6938 template<bool big_endian>
6940 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
6942 const off_t offset = this->offset();
6943 const section_size_type oview_size =
6944 convert_to_section_size_type(this->data_size());
6945 unsigned char* const oview = of->get_output_view(offset, oview_size);
6947 const off_t got_file_offset = this->got_plt_->offset();
6948 const section_size_type got_size =
6949 convert_to_section_size_type(this->got_plt_->data_size());
6950 unsigned char* const got_view = of->get_output_view(got_file_offset,
6952 unsigned char* pov = oview;
6954 Arm_address plt_address = this->address();
6955 Arm_address got_address = this->got_plt_->address();
6957 // Write first PLT entry. All but the last word are constants.
6958 const size_t num_first_plt_words = (sizeof(first_plt_entry)
6959 / sizeof(plt_entry[0]));
6960 for (size_t i = 0; i < num_first_plt_words - 1; i++)
6961 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
6962 // Last word in first PLT entry is &GOT[0] - .
6963 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
6964 got_address - (plt_address + 16));
6965 pov += sizeof(first_plt_entry);
6967 unsigned char* got_pov = got_view;
6969 memset(got_pov, 0, 12);
6972 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
6973 unsigned int plt_offset = sizeof(first_plt_entry);
6974 unsigned int plt_rel_offset = 0;
6975 unsigned int got_offset = 12;
6976 const unsigned int count = this->count_;
6977 for (unsigned int i = 0;
6980 pov += sizeof(plt_entry),
6982 plt_offset += sizeof(plt_entry),
6983 plt_rel_offset += rel_size,
6986 // Set and adjust the PLT entry itself.
6987 int32_t offset = ((got_address + got_offset)
6988 - (plt_address + plt_offset + 8));
6990 gold_assert(offset >= 0 && offset < 0x0fffffff);
6991 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
6992 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
6993 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
6994 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
6995 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
6996 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
6998 // Set the entry in the GOT.
6999 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
7002 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
7003 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
7005 of->write_output_view(offset, oview_size, oview);
7006 of->write_output_view(got_file_offset, got_size, got_view);
7009 // Create a PLT entry for a global symbol.
7011 template<bool big_endian>
7013 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
7016 if (gsym->has_plt_offset())
7019 if (this->plt_ == NULL)
7021 // Create the GOT sections first.
7022 this->got_section(symtab, layout);
7024 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
7025 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
7027 | elfcpp::SHF_EXECINSTR),
7028 this->plt_, false, false, false, false);
7030 this->plt_->add_entry(gsym);
7033 // Get the section to use for TLS_DESC relocations.
7035 template<bool big_endian>
7036 typename Target_arm<big_endian>::Reloc_section*
7037 Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
7039 return this->plt_section()->rel_tls_desc(layout);
7042 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
7044 template<bool big_endian>
7046 Target_arm<big_endian>::define_tls_base_symbol(
7047 Symbol_table* symtab,
7050 if (this->tls_base_symbol_defined_)
7053 Output_segment* tls_segment = layout->tls_segment();
7054 if (tls_segment != NULL)
7056 bool is_exec = parameters->options().output_is_executable();
7057 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
7058 Symbol_table::PREDEFINED,
7062 elfcpp::STV_HIDDEN, 0,
7064 ? Symbol::SEGMENT_END
7065 : Symbol::SEGMENT_START),
7068 this->tls_base_symbol_defined_ = true;
7071 // Create a GOT entry for the TLS module index.
7073 template<bool big_endian>
7075 Target_arm<big_endian>::got_mod_index_entry(
7076 Symbol_table* symtab,
7078 Sized_relobj<32, big_endian>* object)
7080 if (this->got_mod_index_offset_ == -1U)
7082 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7083 Arm_output_data_got<big_endian>* got = this->got_section(symtab, layout);
7084 unsigned int got_offset;
7085 if (!parameters->doing_static_link())
7087 got_offset = got->add_constant(0);
7088 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
7089 rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
7094 // We are doing a static link. Just mark it as belong to module 1,
7096 got_offset = got->add_constant(1);
7099 got->add_constant(0);
7100 this->got_mod_index_offset_ = got_offset;
7102 return this->got_mod_index_offset_;
7105 // Optimize the TLS relocation type based on what we know about the
7106 // symbol. IS_FINAL is true if the final address of this symbol is
7107 // known at link time.
7109 template<bool big_endian>
7110 tls::Tls_optimization
7111 Target_arm<big_endian>::optimize_tls_reloc(bool, int)
7113 // FIXME: Currently we do not do any TLS optimization.
7114 return tls::TLSOPT_NONE;
7117 // Report an unsupported relocation against a local symbol.
7119 template<bool big_endian>
7121 Target_arm<big_endian>::Scan::unsupported_reloc_local(
7122 Sized_relobj<32, big_endian>* object,
7123 unsigned int r_type)
7125 gold_error(_("%s: unsupported reloc %u against local symbol"),
7126 object->name().c_str(), r_type);
7129 // We are about to emit a dynamic relocation of type R_TYPE. If the
7130 // dynamic linker does not support it, issue an error. The GNU linker
7131 // only issues a non-PIC error for an allocated read-only section.
7132 // Here we know the section is allocated, but we don't know that it is
7133 // read-only. But we check for all the relocation types which the
7134 // glibc dynamic linker supports, so it seems appropriate to issue an
7135 // error even if the section is not read-only.
7137 template<bool big_endian>
7139 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
7140 unsigned int r_type)
7144 // These are the relocation types supported by glibc for ARM.
7145 case elfcpp::R_ARM_RELATIVE:
7146 case elfcpp::R_ARM_COPY:
7147 case elfcpp::R_ARM_GLOB_DAT:
7148 case elfcpp::R_ARM_JUMP_SLOT:
7149 case elfcpp::R_ARM_ABS32:
7150 case elfcpp::R_ARM_ABS32_NOI:
7151 case elfcpp::R_ARM_PC24:
7152 // FIXME: The following 3 types are not supported by Android's dynamic
7154 case elfcpp::R_ARM_TLS_DTPMOD32:
7155 case elfcpp::R_ARM_TLS_DTPOFF32:
7156 case elfcpp::R_ARM_TLS_TPOFF32:
7161 // This prevents us from issuing more than one error per reloc
7162 // section. But we can still wind up issuing more than one
7163 // error per object file.
7164 if (this->issued_non_pic_error_)
7166 const Arm_reloc_property* reloc_property =
7167 arm_reloc_property_table->get_reloc_property(r_type);
7168 gold_assert(reloc_property != NULL);
7169 object->error(_("requires unsupported dynamic reloc %s; "
7170 "recompile with -fPIC"),
7171 reloc_property->name().c_str());
7172 this->issued_non_pic_error_ = true;
7176 case elfcpp::R_ARM_NONE:
7181 // Scan a relocation for a local symbol.
7182 // FIXME: This only handles a subset of relocation types used by Android
7183 // on ARM v5te devices.
7185 template<bool big_endian>
7187 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
7190 Sized_relobj<32, big_endian>* object,
7191 unsigned int data_shndx,
7192 Output_section* output_section,
7193 const elfcpp::Rel<32, big_endian>& reloc,
7194 unsigned int r_type,
7195 const elfcpp::Sym<32, big_endian>& lsym)
7197 r_type = get_real_reloc_type(r_type);
7200 case elfcpp::R_ARM_NONE:
7201 case elfcpp::R_ARM_V4BX:
7202 case elfcpp::R_ARM_GNU_VTENTRY:
7203 case elfcpp::R_ARM_GNU_VTINHERIT:
7206 case elfcpp::R_ARM_ABS32:
7207 case elfcpp::R_ARM_ABS32_NOI:
7208 // If building a shared library (or a position-independent
7209 // executable), we need to create a dynamic relocation for
7210 // this location. The relocation applied at link time will
7211 // apply the link-time value, so we flag the location with
7212 // an R_ARM_RELATIVE relocation so the dynamic loader can
7213 // relocate it easily.
7214 if (parameters->options().output_is_position_independent())
7216 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7217 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7218 // If we are to add more other reloc types than R_ARM_ABS32,
7219 // we need to add check_non_pic(object, r_type) here.
7220 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
7221 output_section, data_shndx,
7222 reloc.get_r_offset());
7226 case elfcpp::R_ARM_ABS16:
7227 case elfcpp::R_ARM_ABS12:
7228 case elfcpp::R_ARM_THM_ABS5:
7229 case elfcpp::R_ARM_ABS8:
7230 case elfcpp::R_ARM_BASE_ABS:
7231 case elfcpp::R_ARM_MOVW_ABS_NC:
7232 case elfcpp::R_ARM_MOVT_ABS:
7233 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7234 case elfcpp::R_ARM_THM_MOVT_ABS:
7235 // If building a shared library (or a position-independent
7236 // executable), we need to create a dynamic relocation for
7237 // this location. Because the addend needs to remain in the
7238 // data section, we need to be careful not to apply this
7239 // relocation statically.
7240 if (parameters->options().output_is_position_independent())
7242 check_non_pic(object, r_type);
7243 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7244 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7245 if (lsym.get_st_type() != elfcpp::STT_SECTION)
7246 rel_dyn->add_local(object, r_sym, r_type, output_section,
7247 data_shndx, reloc.get_r_offset());
7250 gold_assert(lsym.get_st_value() == 0);
7251 unsigned int shndx = lsym.get_st_shndx();
7253 shndx = object->adjust_sym_shndx(r_sym, shndx,
7256 object->error(_("section symbol %u has bad shndx %u"),
7259 rel_dyn->add_local_section(object, shndx,
7260 r_type, output_section,
7261 data_shndx, reloc.get_r_offset());
7266 case elfcpp::R_ARM_PC24:
7267 case elfcpp::R_ARM_REL32:
7268 case elfcpp::R_ARM_LDR_PC_G0:
7269 case elfcpp::R_ARM_SBREL32:
7270 case elfcpp::R_ARM_THM_CALL:
7271 case elfcpp::R_ARM_THM_PC8:
7272 case elfcpp::R_ARM_BASE_PREL:
7273 case elfcpp::R_ARM_PLT32:
7274 case elfcpp::R_ARM_CALL:
7275 case elfcpp::R_ARM_JUMP24:
7276 case elfcpp::R_ARM_THM_JUMP24:
7277 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7278 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7279 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7280 case elfcpp::R_ARM_SBREL31:
7281 case elfcpp::R_ARM_PREL31:
7282 case elfcpp::R_ARM_MOVW_PREL_NC:
7283 case elfcpp::R_ARM_MOVT_PREL:
7284 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7285 case elfcpp::R_ARM_THM_MOVT_PREL:
7286 case elfcpp::R_ARM_THM_JUMP19:
7287 case elfcpp::R_ARM_THM_JUMP6:
7288 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7289 case elfcpp::R_ARM_THM_PC12:
7290 case elfcpp::R_ARM_REL32_NOI:
7291 case elfcpp::R_ARM_ALU_PC_G0_NC:
7292 case elfcpp::R_ARM_ALU_PC_G0:
7293 case elfcpp::R_ARM_ALU_PC_G1_NC:
7294 case elfcpp::R_ARM_ALU_PC_G1:
7295 case elfcpp::R_ARM_ALU_PC_G2:
7296 case elfcpp::R_ARM_LDR_PC_G1:
7297 case elfcpp::R_ARM_LDR_PC_G2:
7298 case elfcpp::R_ARM_LDRS_PC_G0:
7299 case elfcpp::R_ARM_LDRS_PC_G1:
7300 case elfcpp::R_ARM_LDRS_PC_G2:
7301 case elfcpp::R_ARM_LDC_PC_G0:
7302 case elfcpp::R_ARM_LDC_PC_G1:
7303 case elfcpp::R_ARM_LDC_PC_G2:
7304 case elfcpp::R_ARM_ALU_SB_G0_NC:
7305 case elfcpp::R_ARM_ALU_SB_G0:
7306 case elfcpp::R_ARM_ALU_SB_G1_NC:
7307 case elfcpp::R_ARM_ALU_SB_G1:
7308 case elfcpp::R_ARM_ALU_SB_G2:
7309 case elfcpp::R_ARM_LDR_SB_G0:
7310 case elfcpp::R_ARM_LDR_SB_G1:
7311 case elfcpp::R_ARM_LDR_SB_G2:
7312 case elfcpp::R_ARM_LDRS_SB_G0:
7313 case elfcpp::R_ARM_LDRS_SB_G1:
7314 case elfcpp::R_ARM_LDRS_SB_G2:
7315 case elfcpp::R_ARM_LDC_SB_G0:
7316 case elfcpp::R_ARM_LDC_SB_G1:
7317 case elfcpp::R_ARM_LDC_SB_G2:
7318 case elfcpp::R_ARM_MOVW_BREL_NC:
7319 case elfcpp::R_ARM_MOVT_BREL:
7320 case elfcpp::R_ARM_MOVW_BREL:
7321 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7322 case elfcpp::R_ARM_THM_MOVT_BREL:
7323 case elfcpp::R_ARM_THM_MOVW_BREL:
7324 case elfcpp::R_ARM_THM_JUMP11:
7325 case elfcpp::R_ARM_THM_JUMP8:
7326 // We don't need to do anything for a relative addressing relocation
7327 // against a local symbol if it does not reference the GOT.
7330 case elfcpp::R_ARM_GOTOFF32:
7331 case elfcpp::R_ARM_GOTOFF12:
7332 // We need a GOT section:
7333 target->got_section(symtab, layout);
7336 case elfcpp::R_ARM_GOT_BREL:
7337 case elfcpp::R_ARM_GOT_PREL:
7339 // The symbol requires a GOT entry.
7340 Arm_output_data_got<big_endian>* got =
7341 target->got_section(symtab, layout);
7342 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7343 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
7345 // If we are generating a shared object, we need to add a
7346 // dynamic RELATIVE relocation for this symbol's GOT entry.
7347 if (parameters->options().output_is_position_independent())
7349 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7350 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7351 rel_dyn->add_local_relative(
7352 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
7353 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
7359 case elfcpp::R_ARM_TARGET1:
7360 case elfcpp::R_ARM_TARGET2:
7361 // This should have been mapped to another type already.
7363 case elfcpp::R_ARM_COPY:
7364 case elfcpp::R_ARM_GLOB_DAT:
7365 case elfcpp::R_ARM_JUMP_SLOT:
7366 case elfcpp::R_ARM_RELATIVE:
7367 // These are relocations which should only be seen by the
7368 // dynamic linker, and should never be seen here.
7369 gold_error(_("%s: unexpected reloc %u in object file"),
7370 object->name().c_str(), r_type);
7374 // These are initial TLS relocs, which are expected when
7376 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7377 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7378 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7379 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7380 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7382 bool output_is_shared = parameters->options().shared();
7383 const tls::Tls_optimization optimized_type
7384 = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
7388 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7389 if (optimized_type == tls::TLSOPT_NONE)
7391 // Create a pair of GOT entries for the module index and
7392 // dtv-relative offset.
7393 Arm_output_data_got<big_endian>* got
7394 = target->got_section(symtab, layout);
7395 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7396 unsigned int shndx = lsym.get_st_shndx();
7398 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7401 object->error(_("local symbol %u has bad shndx %u"),
7406 if (!parameters->doing_static_link())
7407 got->add_local_pair_with_rel(object, r_sym, shndx,
7409 target->rel_dyn_section(layout),
7410 elfcpp::R_ARM_TLS_DTPMOD32, 0);
7412 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR,
7416 // FIXME: TLS optimization not supported yet.
7420 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7421 if (optimized_type == tls::TLSOPT_NONE)
7423 // Create a GOT entry for the module index.
7424 target->got_mod_index_entry(symtab, layout, object);
7427 // FIXME: TLS optimization not supported yet.
7431 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7434 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7435 layout->set_has_static_tls();
7436 if (optimized_type == tls::TLSOPT_NONE)
7438 // Create a GOT entry for the tp-relative offset.
7439 Arm_output_data_got<big_endian>* got
7440 = target->got_section(symtab, layout);
7441 unsigned int r_sym =
7442 elfcpp::elf_r_sym<32>(reloc.get_r_info());
7443 if (!parameters->doing_static_link())
7444 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
7445 target->rel_dyn_section(layout),
7446 elfcpp::R_ARM_TLS_TPOFF32);
7447 else if (!object->local_has_got_offset(r_sym,
7448 GOT_TYPE_TLS_OFFSET))
7450 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
7451 unsigned int got_offset =
7452 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
7453 got->add_static_reloc(got_offset,
7454 elfcpp::R_ARM_TLS_TPOFF32, object,
7459 // FIXME: TLS optimization not supported yet.
7463 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7464 layout->set_has_static_tls();
7465 if (output_is_shared)
7467 // We need to create a dynamic relocation.
7468 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
7469 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7470 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7471 rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
7472 output_section, data_shndx,
7473 reloc.get_r_offset());
7484 unsupported_reloc_local(object, r_type);
7489 // Report an unsupported relocation against a global symbol.
7491 template<bool big_endian>
7493 Target_arm<big_endian>::Scan::unsupported_reloc_global(
7494 Sized_relobj<32, big_endian>* object,
7495 unsigned int r_type,
7498 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7499 object->name().c_str(), r_type, gsym->demangled_name().c_str());
7502 // Scan a relocation for a global symbol.
7504 template<bool big_endian>
7506 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
7509 Sized_relobj<32, big_endian>* object,
7510 unsigned int data_shndx,
7511 Output_section* output_section,
7512 const elfcpp::Rel<32, big_endian>& reloc,
7513 unsigned int r_type,
7516 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
7517 // section. We check here to avoid creating a dynamic reloc against
7518 // _GLOBAL_OFFSET_TABLE_.
7519 if (!target->has_got_section()
7520 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7521 target->got_section(symtab, layout);
7523 r_type = get_real_reloc_type(r_type);
7526 case elfcpp::R_ARM_NONE:
7527 case elfcpp::R_ARM_V4BX:
7528 case elfcpp::R_ARM_GNU_VTENTRY:
7529 case elfcpp::R_ARM_GNU_VTINHERIT:
7532 case elfcpp::R_ARM_ABS32:
7533 case elfcpp::R_ARM_ABS16:
7534 case elfcpp::R_ARM_ABS12:
7535 case elfcpp::R_ARM_THM_ABS5:
7536 case elfcpp::R_ARM_ABS8:
7537 case elfcpp::R_ARM_BASE_ABS:
7538 case elfcpp::R_ARM_MOVW_ABS_NC:
7539 case elfcpp::R_ARM_MOVT_ABS:
7540 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7541 case elfcpp::R_ARM_THM_MOVT_ABS:
7542 case elfcpp::R_ARM_ABS32_NOI:
7543 // Absolute addressing relocations.
7545 // Make a PLT entry if necessary.
7546 if (this->symbol_needs_plt_entry(gsym))
7548 target->make_plt_entry(symtab, layout, gsym);
7549 // Since this is not a PC-relative relocation, we may be
7550 // taking the address of a function. In that case we need to
7551 // set the entry in the dynamic symbol table to the address of
7553 if (gsym->is_from_dynobj() && !parameters->options().shared())
7554 gsym->set_needs_dynsym_value();
7556 // Make a dynamic relocation if necessary.
7557 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
7559 if (gsym->may_need_copy_reloc())
7561 target->copy_reloc(symtab, layout, object,
7562 data_shndx, output_section, gsym, reloc);
7564 else if ((r_type == elfcpp::R_ARM_ABS32
7565 || r_type == elfcpp::R_ARM_ABS32_NOI)
7566 && gsym->can_use_relative_reloc(false))
7568 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7569 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
7570 output_section, object,
7571 data_shndx, reloc.get_r_offset());
7575 check_non_pic(object, r_type);
7576 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7577 rel_dyn->add_global(gsym, r_type, output_section, object,
7578 data_shndx, reloc.get_r_offset());
7584 case elfcpp::R_ARM_GOTOFF32:
7585 case elfcpp::R_ARM_GOTOFF12:
7586 // We need a GOT section.
7587 target->got_section(symtab, layout);
7590 case elfcpp::R_ARM_REL32:
7591 case elfcpp::R_ARM_LDR_PC_G0:
7592 case elfcpp::R_ARM_SBREL32:
7593 case elfcpp::R_ARM_THM_PC8:
7594 case elfcpp::R_ARM_BASE_PREL:
7595 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7596 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7597 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7598 case elfcpp::R_ARM_MOVW_PREL_NC:
7599 case elfcpp::R_ARM_MOVT_PREL:
7600 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7601 case elfcpp::R_ARM_THM_MOVT_PREL:
7602 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7603 case elfcpp::R_ARM_THM_PC12:
7604 case elfcpp::R_ARM_REL32_NOI:
7605 case elfcpp::R_ARM_ALU_PC_G0_NC:
7606 case elfcpp::R_ARM_ALU_PC_G0:
7607 case elfcpp::R_ARM_ALU_PC_G1_NC:
7608 case elfcpp::R_ARM_ALU_PC_G1:
7609 case elfcpp::R_ARM_ALU_PC_G2:
7610 case elfcpp::R_ARM_LDR_PC_G1:
7611 case elfcpp::R_ARM_LDR_PC_G2:
7612 case elfcpp::R_ARM_LDRS_PC_G0:
7613 case elfcpp::R_ARM_LDRS_PC_G1:
7614 case elfcpp::R_ARM_LDRS_PC_G2:
7615 case elfcpp::R_ARM_LDC_PC_G0:
7616 case elfcpp::R_ARM_LDC_PC_G1:
7617 case elfcpp::R_ARM_LDC_PC_G2:
7618 case elfcpp::R_ARM_ALU_SB_G0_NC:
7619 case elfcpp::R_ARM_ALU_SB_G0:
7620 case elfcpp::R_ARM_ALU_SB_G1_NC:
7621 case elfcpp::R_ARM_ALU_SB_G1:
7622 case elfcpp::R_ARM_ALU_SB_G2:
7623 case elfcpp::R_ARM_LDR_SB_G0:
7624 case elfcpp::R_ARM_LDR_SB_G1:
7625 case elfcpp::R_ARM_LDR_SB_G2:
7626 case elfcpp::R_ARM_LDRS_SB_G0:
7627 case elfcpp::R_ARM_LDRS_SB_G1:
7628 case elfcpp::R_ARM_LDRS_SB_G2:
7629 case elfcpp::R_ARM_LDC_SB_G0:
7630 case elfcpp::R_ARM_LDC_SB_G1:
7631 case elfcpp::R_ARM_LDC_SB_G2:
7632 case elfcpp::R_ARM_MOVW_BREL_NC:
7633 case elfcpp::R_ARM_MOVT_BREL:
7634 case elfcpp::R_ARM_MOVW_BREL:
7635 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7636 case elfcpp::R_ARM_THM_MOVT_BREL:
7637 case elfcpp::R_ARM_THM_MOVW_BREL:
7638 // Relative addressing relocations.
7640 // Make a dynamic relocation if necessary.
7641 int flags = Symbol::NON_PIC_REF;
7642 if (gsym->needs_dynamic_reloc(flags))
7644 if (target->may_need_copy_reloc(gsym))
7646 target->copy_reloc(symtab, layout, object,
7647 data_shndx, output_section, gsym, reloc);
7651 check_non_pic(object, r_type);
7652 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7653 rel_dyn->add_global(gsym, r_type, output_section, object,
7654 data_shndx, reloc.get_r_offset());
7660 case elfcpp::R_ARM_PC24:
7661 case elfcpp::R_ARM_THM_CALL:
7662 case elfcpp::R_ARM_PLT32:
7663 case elfcpp::R_ARM_CALL:
7664 case elfcpp::R_ARM_JUMP24:
7665 case elfcpp::R_ARM_THM_JUMP24:
7666 case elfcpp::R_ARM_SBREL31:
7667 case elfcpp::R_ARM_PREL31:
7668 case elfcpp::R_ARM_THM_JUMP19:
7669 case elfcpp::R_ARM_THM_JUMP6:
7670 case elfcpp::R_ARM_THM_JUMP11:
7671 case elfcpp::R_ARM_THM_JUMP8:
7672 // All the relocation above are branches except for the PREL31 ones.
7673 // A PREL31 relocation can point to a personality function in a shared
7674 // library. In that case we want to use a PLT because we want to
7675 // call the personality routine and the dyanmic linkers we care about
7676 // do not support dynamic PREL31 relocations. An REL31 relocation may
7677 // point to a function whose unwinding behaviour is being described but
7678 // we will not mistakenly generate a PLT for that because we should use
7679 // a local section symbol.
7681 // If the symbol is fully resolved, this is just a relative
7682 // local reloc. Otherwise we need a PLT entry.
7683 if (gsym->final_value_is_known())
7685 // If building a shared library, we can also skip the PLT entry
7686 // if the symbol is defined in the output file and is protected
7688 if (gsym->is_defined()
7689 && !gsym->is_from_dynobj()
7690 && !gsym->is_preemptible())
7692 target->make_plt_entry(symtab, layout, gsym);
7695 case elfcpp::R_ARM_GOT_BREL:
7696 case elfcpp::R_ARM_GOT_ABS:
7697 case elfcpp::R_ARM_GOT_PREL:
7699 // The symbol requires a GOT entry.
7700 Arm_output_data_got<big_endian>* got =
7701 target->got_section(symtab, layout);
7702 if (gsym->final_value_is_known())
7703 got->add_global(gsym, GOT_TYPE_STANDARD);
7706 // If this symbol is not fully resolved, we need to add a
7707 // GOT entry with a dynamic relocation.
7708 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7709 if (gsym->is_from_dynobj()
7710 || gsym->is_undefined()
7711 || gsym->is_preemptible())
7712 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
7713 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
7716 if (got->add_global(gsym, GOT_TYPE_STANDARD))
7717 rel_dyn->add_global_relative(
7718 gsym, elfcpp::R_ARM_RELATIVE, got,
7719 gsym->got_offset(GOT_TYPE_STANDARD));
7725 case elfcpp::R_ARM_TARGET1:
7726 case elfcpp::R_ARM_TARGET2:
7727 // These should have been mapped to other types already.
7729 case elfcpp::R_ARM_COPY:
7730 case elfcpp::R_ARM_GLOB_DAT:
7731 case elfcpp::R_ARM_JUMP_SLOT:
7732 case elfcpp::R_ARM_RELATIVE:
7733 // These are relocations which should only be seen by the
7734 // dynamic linker, and should never be seen here.
7735 gold_error(_("%s: unexpected reloc %u in object file"),
7736 object->name().c_str(), r_type);
7739 // These are initial tls relocs, which are expected when
7741 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7742 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7743 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7744 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7745 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7747 const bool is_final = gsym->final_value_is_known();
7748 const tls::Tls_optimization optimized_type
7749 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
7752 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7753 if (optimized_type == tls::TLSOPT_NONE)
7755 // Create a pair of GOT entries for the module index and
7756 // dtv-relative offset.
7757 Arm_output_data_got<big_endian>* got
7758 = target->got_section(symtab, layout);
7759 if (!parameters->doing_static_link())
7760 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
7761 target->rel_dyn_section(layout),
7762 elfcpp::R_ARM_TLS_DTPMOD32,
7763 elfcpp::R_ARM_TLS_DTPOFF32);
7765 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
7768 // FIXME: TLS optimization not supported yet.
7772 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7773 if (optimized_type == tls::TLSOPT_NONE)
7775 // Create a GOT entry for the module index.
7776 target->got_mod_index_entry(symtab, layout, object);
7779 // FIXME: TLS optimization not supported yet.
7783 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7786 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7787 layout->set_has_static_tls();
7788 if (optimized_type == tls::TLSOPT_NONE)
7790 // Create a GOT entry for the tp-relative offset.
7791 Arm_output_data_got<big_endian>* got
7792 = target->got_section(symtab, layout);
7793 if (!parameters->doing_static_link())
7794 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
7795 target->rel_dyn_section(layout),
7796 elfcpp::R_ARM_TLS_TPOFF32);
7797 else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
7799 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
7800 unsigned int got_offset =
7801 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
7802 got->add_static_reloc(got_offset,
7803 elfcpp::R_ARM_TLS_TPOFF32, gsym);
7807 // FIXME: TLS optimization not supported yet.
7811 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7812 layout->set_has_static_tls();
7813 if (parameters->options().shared())
7815 // We need to create a dynamic relocation.
7816 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7817 rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
7818 output_section, object,
7819 data_shndx, reloc.get_r_offset());
7830 unsupported_reloc_global(object, r_type, gsym);
7835 // Process relocations for gc.
7837 template<bool big_endian>
7839 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
7841 Sized_relobj<32, big_endian>* object,
7842 unsigned int data_shndx,
7844 const unsigned char* prelocs,
7846 Output_section* output_section,
7847 bool needs_special_offset_handling,
7848 size_t local_symbol_count,
7849 const unsigned char* plocal_symbols)
7851 typedef Target_arm<big_endian> Arm;
7852 typedef typename Target_arm<big_endian>::Scan Scan;
7854 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
7863 needs_special_offset_handling,
7868 // Scan relocations for a section.
7870 template<bool big_endian>
7872 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
7874 Sized_relobj<32, big_endian>* object,
7875 unsigned int data_shndx,
7876 unsigned int sh_type,
7877 const unsigned char* prelocs,
7879 Output_section* output_section,
7880 bool needs_special_offset_handling,
7881 size_t local_symbol_count,
7882 const unsigned char* plocal_symbols)
7884 typedef typename Target_arm<big_endian>::Scan Scan;
7885 if (sh_type == elfcpp::SHT_RELA)
7887 gold_error(_("%s: unsupported RELA reloc section"),
7888 object->name().c_str());
7892 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
7901 needs_special_offset_handling,
7906 // Finalize the sections.
7908 template<bool big_endian>
7910 Target_arm<big_endian>::do_finalize_sections(
7912 const Input_objects* input_objects,
7913 Symbol_table* symtab)
7915 // Create an empty uninitialized attribute section if we still don't have it
7917 if (this->attributes_section_data_ == NULL)
7918 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
7920 // Merge processor-specific flags.
7921 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
7922 p != input_objects->relobj_end();
7925 Arm_relobj<big_endian>* arm_relobj =
7926 Arm_relobj<big_endian>::as_arm_relobj(*p);
7927 this->merge_processor_specific_flags(
7929 arm_relobj->processor_specific_flags());
7930 this->merge_object_attributes(arm_relobj->name().c_str(),
7931 arm_relobj->attributes_section_data());
7935 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
7936 p != input_objects->dynobj_end();
7939 Arm_dynobj<big_endian>* arm_dynobj =
7940 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
7941 this->merge_processor_specific_flags(
7943 arm_dynobj->processor_specific_flags());
7944 this->merge_object_attributes(arm_dynobj->name().c_str(),
7945 arm_dynobj->attributes_section_data());
7949 const Object_attribute* cpu_arch_attr =
7950 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
7951 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
7952 this->set_may_use_blx(true);
7954 // Check if we need to use Cortex-A8 workaround.
7955 if (parameters->options().user_set_fix_cortex_a8())
7956 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
7959 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
7960 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
7962 const Object_attribute* cpu_arch_profile_attr =
7963 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
7964 this->fix_cortex_a8_ =
7965 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
7966 && (cpu_arch_profile_attr->int_value() == 'A'
7967 || cpu_arch_profile_attr->int_value() == 0));
7970 // Check if we can use V4BX interworking.
7971 // The V4BX interworking stub contains BX instruction,
7972 // which is not specified for some profiles.
7973 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
7974 && !this->may_use_blx())
7975 gold_error(_("unable to provide V4BX reloc interworking fix up; "
7976 "the target profile does not support BX instruction"));
7978 // Fill in some more dynamic tags.
7979 const Reloc_section* rel_plt = (this->plt_ == NULL
7981 : this->plt_->rel_plt());
7982 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
7983 this->rel_dyn_, true, false);
7985 // Emit any relocs we saved in an attempt to avoid generating COPY
7987 if (this->copy_relocs_.any_saved_relocs())
7988 this->copy_relocs_.emit(this->rel_dyn_section(layout));
7990 // Handle the .ARM.exidx section.
7991 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
7992 if (exidx_section != NULL
7993 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
7994 && !parameters->options().relocatable())
7996 // Create __exidx_start and __exdix_end symbols.
7997 symtab->define_in_output_data("__exidx_start", NULL,
7998 Symbol_table::PREDEFINED,
7999 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8000 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8002 symtab->define_in_output_data("__exidx_end", NULL,
8003 Symbol_table::PREDEFINED,
8004 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8005 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8008 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
8009 // the .ARM.exidx section.
8010 if (!layout->script_options()->saw_phdrs_clause())
8012 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
8014 Output_segment* exidx_segment =
8015 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
8016 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
8021 // Create an .ARM.attributes section if there is not one already.
8022 Output_attributes_section_data* attributes_section =
8023 new Output_attributes_section_data(*this->attributes_section_data_);
8024 layout->add_output_section_data(".ARM.attributes",
8025 elfcpp::SHT_ARM_ATTRIBUTES, 0,
8026 attributes_section, false, false, false,
8030 // Return whether a direct absolute static relocation needs to be applied.
8031 // In cases where Scan::local() or Scan::global() has created
8032 // a dynamic relocation other than R_ARM_RELATIVE, the addend
8033 // of the relocation is carried in the data, and we must not
8034 // apply the static relocation.
8036 template<bool big_endian>
8038 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
8039 const Sized_symbol<32>* gsym,
8042 Output_section* output_section)
8044 // If the output section is not allocated, then we didn't call
8045 // scan_relocs, we didn't create a dynamic reloc, and we must apply
8047 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
8050 // For local symbols, we will have created a non-RELATIVE dynamic
8051 // relocation only if (a) the output is position independent,
8052 // (b) the relocation is absolute (not pc- or segment-relative), and
8053 // (c) the relocation is not 32 bits wide.
8055 return !(parameters->options().output_is_position_independent()
8056 && (ref_flags & Symbol::ABSOLUTE_REF)
8059 // For global symbols, we use the same helper routines used in the
8060 // scan pass. If we did not create a dynamic relocation, or if we
8061 // created a RELATIVE dynamic relocation, we should apply the static
8063 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
8064 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
8065 && gsym->can_use_relative_reloc(ref_flags
8066 & Symbol::FUNCTION_CALL);
8067 return !has_dyn || is_rel;
8070 // Perform a relocation.
8072 template<bool big_endian>
8074 Target_arm<big_endian>::Relocate::relocate(
8075 const Relocate_info<32, big_endian>* relinfo,
8077 Output_section *output_section,
8079 const elfcpp::Rel<32, big_endian>& rel,
8080 unsigned int r_type,
8081 const Sized_symbol<32>* gsym,
8082 const Symbol_value<32>* psymval,
8083 unsigned char* view,
8084 Arm_address address,
8085 section_size_type view_size)
8087 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
8089 r_type = get_real_reloc_type(r_type);
8090 const Arm_reloc_property* reloc_property =
8091 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8092 if (reloc_property == NULL)
8094 std::string reloc_name =
8095 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8096 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8097 _("cannot relocate %s in object file"),
8098 reloc_name.c_str());
8102 const Arm_relobj<big_endian>* object =
8103 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8105 // If the final branch target of a relocation is THUMB instruction, this
8106 // is 1. Otherwise it is 0.
8107 Arm_address thumb_bit = 0;
8108 Symbol_value<32> symval;
8109 bool is_weakly_undefined_without_plt = false;
8110 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
8114 // This is a global symbol. Determine if we use PLT and if the
8115 // final target is THUMB.
8116 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
8118 // This uses a PLT, change the symbol value.
8119 symval.set_output_value(target->plt_section()->address()
8120 + gsym->plt_offset());
8123 else if (gsym->is_weak_undefined())
8125 // This is a weakly undefined symbol and we do not use PLT
8126 // for this relocation. A branch targeting this symbol will
8127 // be converted into an NOP.
8128 is_weakly_undefined_without_plt = true;
8132 // Set thumb bit if symbol:
8133 // -Has type STT_ARM_TFUNC or
8134 // -Has type STT_FUNC, is defined and with LSB in value set.
8136 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
8137 || (gsym->type() == elfcpp::STT_FUNC
8138 && !gsym->is_undefined()
8139 && ((psymval->value(object, 0) & 1) != 0)))
8146 // This is a local symbol. Determine if the final target is THUMB.
8147 // We saved this information when all the local symbols were read.
8148 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
8149 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
8150 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
8155 // This is a fake relocation synthesized for a stub. It does not have
8156 // a real symbol. We just look at the LSB of the symbol value to
8157 // determine if the target is THUMB or not.
8158 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
8161 // Strip LSB if this points to a THUMB target.
8163 && reloc_property->uses_thumb_bit()
8164 && ((psymval->value(object, 0) & 1) != 0))
8166 Arm_address stripped_value =
8167 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
8168 symval.set_output_value(stripped_value);
8172 // Get the GOT offset if needed.
8173 // The GOT pointer points to the end of the GOT section.
8174 // We need to subtract the size of the GOT section to get
8175 // the actual offset to use in the relocation.
8176 bool have_got_offset = false;
8177 unsigned int got_offset = 0;
8180 case elfcpp::R_ARM_GOT_BREL:
8181 case elfcpp::R_ARM_GOT_PREL:
8184 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8185 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
8186 - target->got_size());
8190 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8191 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8192 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
8193 - target->got_size());
8195 have_got_offset = true;
8202 // To look up relocation stubs, we need to pass the symbol table index of
8204 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8206 // Get the addressing origin of the output segment defining the
8207 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
8208 Arm_address sym_origin = 0;
8209 if (reloc_property->uses_symbol_base())
8211 if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
8212 // R_ARM_BASE_ABS with the NULL symbol will give the
8213 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
8214 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
8215 sym_origin = target->got_plt_section()->address();
8216 else if (gsym == NULL)
8218 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
8219 sym_origin = gsym->output_segment()->vaddr();
8220 else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
8221 sym_origin = gsym->output_data()->address();
8223 // TODO: Assumes the segment base to be zero for the global symbols
8224 // till the proper support for the segment-base-relative addressing
8225 // will be implemented. This is consistent with GNU ld.
8228 // For relative addressing relocation, find out the relative address base.
8229 Arm_address relative_address_base = 0;
8230 switch(reloc_property->relative_address_base())
8232 case Arm_reloc_property::RAB_NONE:
8233 // Relocations with relative address bases RAB_TLS and RAB_tp are
8234 // handled by relocate_tls. So we do not need to do anything here.
8235 case Arm_reloc_property::RAB_TLS:
8236 case Arm_reloc_property::RAB_tp:
8238 case Arm_reloc_property::RAB_B_S:
8239 relative_address_base = sym_origin;
8241 case Arm_reloc_property::RAB_GOT_ORG:
8242 relative_address_base = target->got_plt_section()->address();
8244 case Arm_reloc_property::RAB_P:
8245 relative_address_base = address;
8247 case Arm_reloc_property::RAB_Pa:
8248 relative_address_base = address & 0xfffffffcU;
8254 typename Arm_relocate_functions::Status reloc_status =
8255 Arm_relocate_functions::STATUS_OKAY;
8256 bool check_overflow = reloc_property->checks_overflow();
8259 case elfcpp::R_ARM_NONE:
8262 case elfcpp::R_ARM_ABS8:
8263 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8265 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
8268 case elfcpp::R_ARM_ABS12:
8269 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8271 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
8274 case elfcpp::R_ARM_ABS16:
8275 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8277 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
8280 case elfcpp::R_ARM_ABS32:
8281 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
8283 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8287 case elfcpp::R_ARM_ABS32_NOI:
8288 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
8290 // No thumb bit for this relocation: (S + A)
8291 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8295 case elfcpp::R_ARM_MOVW_ABS_NC:
8296 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8298 reloc_status = Arm_relocate_functions::movw(view, object, psymval,
8303 case elfcpp::R_ARM_MOVT_ABS:
8304 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8306 reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
8309 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8310 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8312 reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
8313 0, thumb_bit, false);
8316 case elfcpp::R_ARM_THM_MOVT_ABS:
8317 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8319 reloc_status = Arm_relocate_functions::thm_movt(view, object,
8323 case elfcpp::R_ARM_MOVW_PREL_NC:
8324 case elfcpp::R_ARM_MOVW_BREL_NC:
8325 case elfcpp::R_ARM_MOVW_BREL:
8327 Arm_relocate_functions::movw(view, object, psymval,
8328 relative_address_base, thumb_bit,
8332 case elfcpp::R_ARM_MOVT_PREL:
8333 case elfcpp::R_ARM_MOVT_BREL:
8335 Arm_relocate_functions::movt(view, object, psymval,
8336 relative_address_base);
8339 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8340 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8341 case elfcpp::R_ARM_THM_MOVW_BREL:
8343 Arm_relocate_functions::thm_movw(view, object, psymval,
8344 relative_address_base,
8345 thumb_bit, check_overflow);
8348 case elfcpp::R_ARM_THM_MOVT_PREL:
8349 case elfcpp::R_ARM_THM_MOVT_BREL:
8351 Arm_relocate_functions::thm_movt(view, object, psymval,
8352 relative_address_base);
8355 case elfcpp::R_ARM_REL32:
8356 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8357 address, thumb_bit);
8360 case elfcpp::R_ARM_THM_ABS5:
8361 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8363 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
8366 // Thumb long branches.
8367 case elfcpp::R_ARM_THM_CALL:
8368 case elfcpp::R_ARM_THM_XPC22:
8369 case elfcpp::R_ARM_THM_JUMP24:
8371 Arm_relocate_functions::thumb_branch_common(
8372 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8373 thumb_bit, is_weakly_undefined_without_plt);
8376 case elfcpp::R_ARM_GOTOFF32:
8378 Arm_address got_origin;
8379 got_origin = target->got_plt_section()->address();
8380 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8381 got_origin, thumb_bit);
8385 case elfcpp::R_ARM_BASE_PREL:
8386 gold_assert(gsym != NULL);
8388 Arm_relocate_functions::base_prel(view, sym_origin, address);
8391 case elfcpp::R_ARM_BASE_ABS:
8393 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8397 reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
8401 case elfcpp::R_ARM_GOT_BREL:
8402 gold_assert(have_got_offset);
8403 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
8406 case elfcpp::R_ARM_GOT_PREL:
8407 gold_assert(have_got_offset);
8408 // Get the address origin for GOT PLT, which is allocated right
8409 // after the GOT section, to calculate an absolute address of
8410 // the symbol GOT entry (got_origin + got_offset).
8411 Arm_address got_origin;
8412 got_origin = target->got_plt_section()->address();
8413 reloc_status = Arm_relocate_functions::got_prel(view,
8414 got_origin + got_offset,
8418 case elfcpp::R_ARM_PLT32:
8419 case elfcpp::R_ARM_CALL:
8420 case elfcpp::R_ARM_JUMP24:
8421 case elfcpp::R_ARM_XPC25:
8422 gold_assert(gsym == NULL
8423 || gsym->has_plt_offset()
8424 || gsym->final_value_is_known()
8425 || (gsym->is_defined()
8426 && !gsym->is_from_dynobj()
8427 && !gsym->is_preemptible()));
8429 Arm_relocate_functions::arm_branch_common(
8430 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8431 thumb_bit, is_weakly_undefined_without_plt);
8434 case elfcpp::R_ARM_THM_JUMP19:
8436 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
8440 case elfcpp::R_ARM_THM_JUMP6:
8442 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
8445 case elfcpp::R_ARM_THM_JUMP8:
8447 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
8450 case elfcpp::R_ARM_THM_JUMP11:
8452 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
8455 case elfcpp::R_ARM_PREL31:
8456 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
8457 address, thumb_bit);
8460 case elfcpp::R_ARM_V4BX:
8461 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
8463 const bool is_v4bx_interworking =
8464 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
8466 Arm_relocate_functions::v4bx(relinfo, view, object, address,
8467 is_v4bx_interworking);
8471 case elfcpp::R_ARM_THM_PC8:
8473 Arm_relocate_functions::thm_pc8(view, object, psymval, address);
8476 case elfcpp::R_ARM_THM_PC12:
8478 Arm_relocate_functions::thm_pc12(view, object, psymval, address);
8481 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8483 Arm_relocate_functions::thm_alu11(view, object, psymval, address,
8487 case elfcpp::R_ARM_ALU_PC_G0_NC:
8488 case elfcpp::R_ARM_ALU_PC_G0:
8489 case elfcpp::R_ARM_ALU_PC_G1_NC:
8490 case elfcpp::R_ARM_ALU_PC_G1:
8491 case elfcpp::R_ARM_ALU_PC_G2:
8492 case elfcpp::R_ARM_ALU_SB_G0_NC:
8493 case elfcpp::R_ARM_ALU_SB_G0:
8494 case elfcpp::R_ARM_ALU_SB_G1_NC:
8495 case elfcpp::R_ARM_ALU_SB_G1:
8496 case elfcpp::R_ARM_ALU_SB_G2:
8498 Arm_relocate_functions::arm_grp_alu(view, object, psymval,
8499 reloc_property->group_index(),
8500 relative_address_base,
8501 thumb_bit, check_overflow);
8504 case elfcpp::R_ARM_LDR_PC_G0:
8505 case elfcpp::R_ARM_LDR_PC_G1:
8506 case elfcpp::R_ARM_LDR_PC_G2:
8507 case elfcpp::R_ARM_LDR_SB_G0:
8508 case elfcpp::R_ARM_LDR_SB_G1:
8509 case elfcpp::R_ARM_LDR_SB_G2:
8511 Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
8512 reloc_property->group_index(),
8513 relative_address_base);
8516 case elfcpp::R_ARM_LDRS_PC_G0:
8517 case elfcpp::R_ARM_LDRS_PC_G1:
8518 case elfcpp::R_ARM_LDRS_PC_G2:
8519 case elfcpp::R_ARM_LDRS_SB_G0:
8520 case elfcpp::R_ARM_LDRS_SB_G1:
8521 case elfcpp::R_ARM_LDRS_SB_G2:
8523 Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
8524 reloc_property->group_index(),
8525 relative_address_base);
8528 case elfcpp::R_ARM_LDC_PC_G0:
8529 case elfcpp::R_ARM_LDC_PC_G1:
8530 case elfcpp::R_ARM_LDC_PC_G2:
8531 case elfcpp::R_ARM_LDC_SB_G0:
8532 case elfcpp::R_ARM_LDC_SB_G1:
8533 case elfcpp::R_ARM_LDC_SB_G2:
8535 Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
8536 reloc_property->group_index(),
8537 relative_address_base);
8540 // These are initial tls relocs, which are expected when
8542 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8543 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8544 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8545 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8546 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8548 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
8549 view, address, view_size);
8556 // Report any errors.
8557 switch (reloc_status)
8559 case Arm_relocate_functions::STATUS_OKAY:
8561 case Arm_relocate_functions::STATUS_OVERFLOW:
8562 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8563 _("relocation overflow in relocation %u"),
8566 case Arm_relocate_functions::STATUS_BAD_RELOC:
8567 gold_error_at_location(
8571 _("unexpected opcode while processing relocation %u"),
8581 // Perform a TLS relocation.
8583 template<bool big_endian>
8584 inline typename Arm_relocate_functions<big_endian>::Status
8585 Target_arm<big_endian>::Relocate::relocate_tls(
8586 const Relocate_info<32, big_endian>* relinfo,
8587 Target_arm<big_endian>* target,
8589 const elfcpp::Rel<32, big_endian>& rel,
8590 unsigned int r_type,
8591 const Sized_symbol<32>* gsym,
8592 const Symbol_value<32>* psymval,
8593 unsigned char* view,
8594 elfcpp::Elf_types<32>::Elf_Addr address,
8595 section_size_type /*view_size*/ )
8597 typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
8598 typedef Relocate_functions<32, big_endian> RelocFuncs;
8599 Output_segment* tls_segment = relinfo->layout->tls_segment();
8601 const Sized_relobj<32, big_endian>* object = relinfo->object;
8603 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
8605 const bool is_final = (gsym == NULL
8606 ? !parameters->options().shared()
8607 : gsym->final_value_is_known());
8608 const tls::Tls_optimization optimized_type
8609 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
8612 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8614 unsigned int got_type = GOT_TYPE_TLS_PAIR;
8615 unsigned int got_offset;
8618 gold_assert(gsym->has_got_offset(got_type));
8619 got_offset = gsym->got_offset(got_type) - target->got_size();
8623 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8624 gold_assert(object->local_has_got_offset(r_sym, got_type));
8625 got_offset = (object->local_got_offset(r_sym, got_type)
8626 - target->got_size());
8628 if (optimized_type == tls::TLSOPT_NONE)
8630 Arm_address got_entry =
8631 target->got_plt_section()->address() + got_offset;
8633 // Relocate the field with the PC relative offset of the pair of
8635 RelocFuncs::pcrel32(view, got_entry, address);
8636 return ArmRelocFuncs::STATUS_OKAY;
8641 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8642 if (optimized_type == tls::TLSOPT_NONE)
8644 // Relocate the field with the offset of the GOT entry for
8645 // the module index.
8646 unsigned int got_offset;
8647 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
8648 - target->got_size());
8649 Arm_address got_entry =
8650 target->got_plt_section()->address() + got_offset;
8652 // Relocate the field with the PC relative offset of the pair of
8654 RelocFuncs::pcrel32(view, got_entry, address);
8655 return ArmRelocFuncs::STATUS_OKAY;
8659 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8660 RelocFuncs::rel32(view, value);
8661 return ArmRelocFuncs::STATUS_OKAY;
8663 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8664 if (optimized_type == tls::TLSOPT_NONE)
8666 // Relocate the field with the offset of the GOT entry for
8667 // the tp-relative offset of the symbol.
8668 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
8669 unsigned int got_offset;
8672 gold_assert(gsym->has_got_offset(got_type));
8673 got_offset = gsym->got_offset(got_type);
8677 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8678 gold_assert(object->local_has_got_offset(r_sym, got_type));
8679 got_offset = object->local_got_offset(r_sym, got_type);
8682 // All GOT offsets are relative to the end of the GOT.
8683 got_offset -= target->got_size();
8685 Arm_address got_entry =
8686 target->got_plt_section()->address() + got_offset;
8688 // Relocate the field with the PC relative offset of the GOT entry.
8689 RelocFuncs::pcrel32(view, got_entry, address);
8690 return ArmRelocFuncs::STATUS_OKAY;
8694 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8695 // If we're creating a shared library, a dynamic relocation will
8696 // have been created for this location, so do not apply it now.
8697 if (!parameters->options().shared())
8699 gold_assert(tls_segment != NULL);
8701 // $tp points to the TCB, which is followed by the TLS, so we
8702 // need to add TCB size to the offset.
8703 Arm_address aligned_tcb_size =
8704 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
8705 RelocFuncs::rel32(view, value + aligned_tcb_size);
8708 return ArmRelocFuncs::STATUS_OKAY;
8714 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8715 _("unsupported reloc %u"),
8717 return ArmRelocFuncs::STATUS_BAD_RELOC;
8720 // Relocate section data.
8722 template<bool big_endian>
8724 Target_arm<big_endian>::relocate_section(
8725 const Relocate_info<32, big_endian>* relinfo,
8726 unsigned int sh_type,
8727 const unsigned char* prelocs,
8729 Output_section* output_section,
8730 bool needs_special_offset_handling,
8731 unsigned char* view,
8732 Arm_address address,
8733 section_size_type view_size,
8734 const Reloc_symbol_changes* reloc_symbol_changes)
8736 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
8737 gold_assert(sh_type == elfcpp::SHT_REL);
8739 // See if we are relocating a relaxed input section. If so, the view
8740 // covers the whole output section and we need to adjust accordingly.
8741 if (needs_special_offset_handling)
8743 const Output_relaxed_input_section* poris =
8744 output_section->find_relaxed_input_section(relinfo->object,
8745 relinfo->data_shndx);
8748 Arm_address section_address = poris->address();
8749 section_size_type section_size = poris->data_size();
8751 gold_assert((section_address >= address)
8752 && ((section_address + section_size)
8753 <= (address + view_size)));
8755 off_t offset = section_address - address;
8758 view_size = section_size;
8762 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
8769 needs_special_offset_handling,
8773 reloc_symbol_changes);
8776 // Return the size of a relocation while scanning during a relocatable
8779 template<bool big_endian>
8781 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
8782 unsigned int r_type,
8785 r_type = get_real_reloc_type(r_type);
8786 const Arm_reloc_property* arp =
8787 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8792 std::string reloc_name =
8793 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8794 gold_error(_("%s: unexpected %s in object file"),
8795 object->name().c_str(), reloc_name.c_str());
8800 // Scan the relocs during a relocatable link.
8802 template<bool big_endian>
8804 Target_arm<big_endian>::scan_relocatable_relocs(
8805 Symbol_table* symtab,
8807 Sized_relobj<32, big_endian>* object,
8808 unsigned int data_shndx,
8809 unsigned int sh_type,
8810 const unsigned char* prelocs,
8812 Output_section* output_section,
8813 bool needs_special_offset_handling,
8814 size_t local_symbol_count,
8815 const unsigned char* plocal_symbols,
8816 Relocatable_relocs* rr)
8818 gold_assert(sh_type == elfcpp::SHT_REL);
8820 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
8821 Relocatable_size_for_reloc> Scan_relocatable_relocs;
8823 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
8824 Scan_relocatable_relocs>(
8832 needs_special_offset_handling,
8838 // Relocate a section during a relocatable link.
8840 template<bool big_endian>
8842 Target_arm<big_endian>::relocate_for_relocatable(
8843 const Relocate_info<32, big_endian>* relinfo,
8844 unsigned int sh_type,
8845 const unsigned char* prelocs,
8847 Output_section* output_section,
8848 off_t offset_in_output_section,
8849 const Relocatable_relocs* rr,
8850 unsigned char* view,
8851 Arm_address view_address,
8852 section_size_type view_size,
8853 unsigned char* reloc_view,
8854 section_size_type reloc_view_size)
8856 gold_assert(sh_type == elfcpp::SHT_REL);
8858 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
8863 offset_in_output_section,
8872 // Return the value to use for a dynamic symbol which requires special
8873 // treatment. This is how we support equality comparisons of function
8874 // pointers across shared library boundaries, as described in the
8875 // processor specific ABI supplement.
8877 template<bool big_endian>
8879 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
8881 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
8882 return this->plt_section()->address() + gsym->plt_offset();
8885 // Map platform-specific relocs to real relocs
8887 template<bool big_endian>
8889 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
8893 case elfcpp::R_ARM_TARGET1:
8894 // This is either R_ARM_ABS32 or R_ARM_REL32;
8895 return elfcpp::R_ARM_ABS32;
8897 case elfcpp::R_ARM_TARGET2:
8898 // This can be any reloc type but ususally is R_ARM_GOT_PREL
8899 return elfcpp::R_ARM_GOT_PREL;
8906 // Whether if two EABI versions V1 and V2 are compatible.
8908 template<bool big_endian>
8910 Target_arm<big_endian>::are_eabi_versions_compatible(
8911 elfcpp::Elf_Word v1,
8912 elfcpp::Elf_Word v2)
8914 // v4 and v5 are the same spec before and after it was released,
8915 // so allow mixing them.
8916 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
8917 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
8923 // Combine FLAGS from an input object called NAME and the processor-specific
8924 // flags in the ELF header of the output. Much of this is adapted from the
8925 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
8926 // in bfd/elf32-arm.c.
8928 template<bool big_endian>
8930 Target_arm<big_endian>::merge_processor_specific_flags(
8931 const std::string& name,
8932 elfcpp::Elf_Word flags)
8934 if (this->are_processor_specific_flags_set())
8936 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
8938 // Nothing to merge if flags equal to those in output.
8939 if (flags == out_flags)
8942 // Complain about various flag mismatches.
8943 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
8944 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
8945 if (!this->are_eabi_versions_compatible(version1, version2))
8946 gold_error(_("Source object %s has EABI version %d but output has "
8947 "EABI version %d."),
8949 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
8950 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
8954 // If the input is the default architecture and had the default
8955 // flags then do not bother setting the flags for the output
8956 // architecture, instead allow future merges to do this. If no
8957 // future merges ever set these flags then they will retain their
8958 // uninitialised values, which surprise surprise, correspond
8959 // to the default values.
8963 // This is the first time, just copy the flags.
8964 // We only copy the EABI version for now.
8965 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
8969 // Adjust ELF file header.
8970 template<bool big_endian>
8972 Target_arm<big_endian>::do_adjust_elf_header(
8973 unsigned char* view,
8976 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
8978 elfcpp::Ehdr<32, big_endian> ehdr(view);
8979 unsigned char e_ident[elfcpp::EI_NIDENT];
8980 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
8982 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
8983 == elfcpp::EF_ARM_EABI_UNKNOWN)
8984 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
8986 e_ident[elfcpp::EI_OSABI] = 0;
8987 e_ident[elfcpp::EI_ABIVERSION] = 0;
8989 // FIXME: Do EF_ARM_BE8 adjustment.
8991 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
8992 oehdr.put_e_ident(e_ident);
8995 // do_make_elf_object to override the same function in the base class.
8996 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
8997 // to store ARM specific information. Hence we need to have our own
8998 // ELF object creation.
9000 template<bool big_endian>
9002 Target_arm<big_endian>::do_make_elf_object(
9003 const std::string& name,
9004 Input_file* input_file,
9005 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
9007 int et = ehdr.get_e_type();
9008 if (et == elfcpp::ET_REL)
9010 Arm_relobj<big_endian>* obj =
9011 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
9015 else if (et == elfcpp::ET_DYN)
9017 Sized_dynobj<32, big_endian>* obj =
9018 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
9024 gold_error(_("%s: unsupported ELF file type %d"),
9030 // Read the architecture from the Tag_also_compatible_with attribute, if any.
9031 // Returns -1 if no architecture could be read.
9032 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
9034 template<bool big_endian>
9036 Target_arm<big_endian>::get_secondary_compatible_arch(
9037 const Attributes_section_data* pasd)
9039 const Object_attribute *known_attributes =
9040 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
9042 // Note: the tag and its argument below are uleb128 values, though
9043 // currently-defined values fit in one byte for each.
9044 const std::string& sv =
9045 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
9047 && sv.data()[0] == elfcpp::Tag_CPU_arch
9048 && (sv.data()[1] & 128) != 128)
9049 return sv.data()[1];
9051 // This tag is "safely ignorable", so don't complain if it looks funny.
9055 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
9056 // The tag is removed if ARCH is -1.
9057 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
9059 template<bool big_endian>
9061 Target_arm<big_endian>::set_secondary_compatible_arch(
9062 Attributes_section_data* pasd,
9065 Object_attribute *known_attributes =
9066 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
9070 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
9074 // Note: the tag and its argument below are uleb128 values, though
9075 // currently-defined values fit in one byte for each.
9077 sv[0] = elfcpp::Tag_CPU_arch;
9078 gold_assert(arch != 0);
9082 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
9085 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
9087 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
9089 template<bool big_endian>
9091 Target_arm<big_endian>::tag_cpu_arch_combine(
9094 int* secondary_compat_out,
9096 int secondary_compat)
9098 #define T(X) elfcpp::TAG_CPU_ARCH_##X
9099 static const int v6t2[] =
9111 static const int v6k[] =
9124 static const int v7[] =
9138 static const int v6_m[] =
9153 static const int v6s_m[] =
9169 static const int v7e_m[] =
9186 static const int v4t_plus_v6_m[] =
9202 T(V4T_PLUS_V6_M) // V4T plus V6_M.
9204 static const int *comb[] =
9212 // Pseudo-architecture.
9216 // Check we've not got a higher architecture than we know about.
9218 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
9220 gold_error(_("%s: unknown CPU architecture"), name);
9224 // Override old tag if we have a Tag_also_compatible_with on the output.
9226 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
9227 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
9228 oldtag = T(V4T_PLUS_V6_M);
9230 // And override the new tag if we have a Tag_also_compatible_with on the
9233 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
9234 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
9235 newtag = T(V4T_PLUS_V6_M);
9237 // Architectures before V6KZ add features monotonically.
9238 int tagh = std::max(oldtag, newtag);
9239 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
9242 int tagl = std::min(oldtag, newtag);
9243 int result = comb[tagh - T(V6T2)][tagl];
9245 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
9246 // as the canonical version.
9247 if (result == T(V4T_PLUS_V6_M))
9250 *secondary_compat_out = T(V6_M);
9253 *secondary_compat_out = -1;
9257 gold_error(_("%s: conflicting CPU architectures %d/%d"),
9258 name, oldtag, newtag);
9266 // Helper to print AEABI enum tag value.
9268 template<bool big_endian>
9270 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
9272 static const char *aeabi_enum_names[] =
9273 { "", "variable-size", "32-bit", "" };
9274 const size_t aeabi_enum_names_size =
9275 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
9277 if (value < aeabi_enum_names_size)
9278 return std::string(aeabi_enum_names[value]);
9282 sprintf(buffer, "<unknown value %u>", value);
9283 return std::string(buffer);
9287 // Return the string value to store in TAG_CPU_name.
9289 template<bool big_endian>
9291 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
9293 static const char *name_table[] = {
9294 // These aren't real CPU names, but we can't guess
9295 // that from the architecture version alone.
9311 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
9313 if (value < name_table_size)
9314 return std::string(name_table[value]);
9318 sprintf(buffer, "<unknown CPU value %u>", value);
9319 return std::string(buffer);
9323 // Merge object attributes from input file called NAME with those of the
9324 // output. The input object attributes are in the object pointed by PASD.
9326 template<bool big_endian>
9328 Target_arm<big_endian>::merge_object_attributes(
9330 const Attributes_section_data* pasd)
9332 // Return if there is no attributes section data.
9336 // If output has no object attributes, just copy.
9337 if (this->attributes_section_data_ == NULL)
9339 this->attributes_section_data_ = new Attributes_section_data(*pasd);
9343 const int vendor = Object_attribute::OBJ_ATTR_PROC;
9344 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9345 Object_attribute* out_attr =
9346 this->attributes_section_data_->known_attributes(vendor);
9348 // This needs to happen before Tag_ABI_FP_number_model is merged. */
9349 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
9350 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
9352 // Ignore mismatches if the object doesn't use floating point. */
9353 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
9354 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
9355 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
9356 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
9357 gold_error(_("%s uses VFP register arguments, output does not"),
9361 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
9363 // Merge this attribute with existing attributes.
9366 case elfcpp::Tag_CPU_raw_name:
9367 case elfcpp::Tag_CPU_name:
9368 // These are merged after Tag_CPU_arch.
9371 case elfcpp::Tag_ABI_optimization_goals:
9372 case elfcpp::Tag_ABI_FP_optimization_goals:
9373 // Use the first value seen.
9376 case elfcpp::Tag_CPU_arch:
9378 unsigned int saved_out_attr = out_attr->int_value();
9379 // Merge Tag_CPU_arch and Tag_also_compatible_with.
9380 int secondary_compat =
9381 this->get_secondary_compatible_arch(pasd);
9382 int secondary_compat_out =
9383 this->get_secondary_compatible_arch(
9384 this->attributes_section_data_);
9385 out_attr[i].set_int_value(
9386 tag_cpu_arch_combine(name, out_attr[i].int_value(),
9387 &secondary_compat_out,
9388 in_attr[i].int_value(),
9390 this->set_secondary_compatible_arch(this->attributes_section_data_,
9391 secondary_compat_out);
9393 // Merge Tag_CPU_name and Tag_CPU_raw_name.
9394 if (out_attr[i].int_value() == saved_out_attr)
9395 ; // Leave the names alone.
9396 else if (out_attr[i].int_value() == in_attr[i].int_value())
9398 // The output architecture has been changed to match the
9399 // input architecture. Use the input names.
9400 out_attr[elfcpp::Tag_CPU_name].set_string_value(
9401 in_attr[elfcpp::Tag_CPU_name].string_value());
9402 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
9403 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
9407 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
9408 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
9411 // If we still don't have a value for Tag_CPU_name,
9412 // make one up now. Tag_CPU_raw_name remains blank.
9413 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
9415 const std::string cpu_name =
9416 this->tag_cpu_name_value(out_attr[i].int_value());
9417 // FIXME: If we see an unknown CPU, this will be set
9418 // to "<unknown CPU n>", where n is the attribute value.
9419 // This is different from BFD, which leaves the name alone.
9420 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
9425 case elfcpp::Tag_ARM_ISA_use:
9426 case elfcpp::Tag_THUMB_ISA_use:
9427 case elfcpp::Tag_WMMX_arch:
9428 case elfcpp::Tag_Advanced_SIMD_arch:
9429 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
9430 case elfcpp::Tag_ABI_FP_rounding:
9431 case elfcpp::Tag_ABI_FP_exceptions:
9432 case elfcpp::Tag_ABI_FP_user_exceptions:
9433 case elfcpp::Tag_ABI_FP_number_model:
9434 case elfcpp::Tag_VFP_HP_extension:
9435 case elfcpp::Tag_CPU_unaligned_access:
9436 case elfcpp::Tag_T2EE_use:
9437 case elfcpp::Tag_Virtualization_use:
9438 case elfcpp::Tag_MPextension_use:
9439 // Use the largest value specified.
9440 if (in_attr[i].int_value() > out_attr[i].int_value())
9441 out_attr[i].set_int_value(in_attr[i].int_value());
9444 case elfcpp::Tag_ABI_align8_preserved:
9445 case elfcpp::Tag_ABI_PCS_RO_data:
9446 // Use the smallest value specified.
9447 if (in_attr[i].int_value() < out_attr[i].int_value())
9448 out_attr[i].set_int_value(in_attr[i].int_value());
9451 case elfcpp::Tag_ABI_align8_needed:
9452 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
9453 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
9454 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
9457 // This error message should be enabled once all non-conformant
9458 // binaries in the toolchain have had the attributes set
9460 // gold_error(_("output 8-byte data alignment conflicts with %s"),
9464 case elfcpp::Tag_ABI_FP_denormal:
9465 case elfcpp::Tag_ABI_PCS_GOT_use:
9467 // These tags have 0 = don't care, 1 = strong requirement,
9468 // 2 = weak requirement.
9469 static const int order_021[3] = {0, 2, 1};
9471 // Use the "greatest" from the sequence 0, 2, 1, or the largest
9472 // value if greater than 2 (for future-proofing).
9473 if ((in_attr[i].int_value() > 2
9474 && in_attr[i].int_value() > out_attr[i].int_value())
9475 || (in_attr[i].int_value() <= 2
9476 && out_attr[i].int_value() <= 2
9477 && (order_021[in_attr[i].int_value()]
9478 > order_021[out_attr[i].int_value()])))
9479 out_attr[i].set_int_value(in_attr[i].int_value());
9483 case elfcpp::Tag_CPU_arch_profile:
9484 if (out_attr[i].int_value() != in_attr[i].int_value())
9486 // 0 will merge with anything.
9487 // 'A' and 'S' merge to 'A'.
9488 // 'R' and 'S' merge to 'R'.
9489 // 'M' and 'A|R|S' is an error.
9490 if (out_attr[i].int_value() == 0
9491 || (out_attr[i].int_value() == 'S'
9492 && (in_attr[i].int_value() == 'A'
9493 || in_attr[i].int_value() == 'R')))
9494 out_attr[i].set_int_value(in_attr[i].int_value());
9495 else if (in_attr[i].int_value() == 0
9496 || (in_attr[i].int_value() == 'S'
9497 && (out_attr[i].int_value() == 'A'
9498 || out_attr[i].int_value() == 'R')))
9503 (_("conflicting architecture profiles %c/%c"),
9504 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
9505 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
9509 case elfcpp::Tag_VFP_arch:
9526 // Values greater than 6 aren't defined, so just pick the
9528 if (in_attr[i].int_value() > 6
9529 && in_attr[i].int_value() > out_attr[i].int_value())
9531 *out_attr = *in_attr;
9534 // The output uses the superset of input features
9535 // (ISA version) and registers.
9536 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
9537 vfp_versions[out_attr[i].int_value()].ver);
9538 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
9539 vfp_versions[out_attr[i].int_value()].regs);
9540 // This assumes all possible supersets are also a valid
9543 for (newval = 6; newval > 0; newval--)
9545 if (regs == vfp_versions[newval].regs
9546 && ver == vfp_versions[newval].ver)
9549 out_attr[i].set_int_value(newval);
9552 case elfcpp::Tag_PCS_config:
9553 if (out_attr[i].int_value() == 0)
9554 out_attr[i].set_int_value(in_attr[i].int_value());
9555 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
9557 // It's sometimes ok to mix different configs, so this is only
9559 gold_warning(_("%s: conflicting platform configuration"), name);
9562 case elfcpp::Tag_ABI_PCS_R9_use:
9563 if (in_attr[i].int_value() != out_attr[i].int_value()
9564 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
9565 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
9567 gold_error(_("%s: conflicting use of R9"), name);
9569 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
9570 out_attr[i].set_int_value(in_attr[i].int_value());
9572 case elfcpp::Tag_ABI_PCS_RW_data:
9573 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
9574 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9575 != elfcpp::AEABI_R9_SB)
9576 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9577 != elfcpp::AEABI_R9_unused))
9579 gold_error(_("%s: SB relative addressing conflicts with use "
9583 // Use the smallest value specified.
9584 if (in_attr[i].int_value() < out_attr[i].int_value())
9585 out_attr[i].set_int_value(in_attr[i].int_value());
9587 case elfcpp::Tag_ABI_PCS_wchar_t:
9588 // FIXME: Make it possible to turn off this warning.
9589 if (out_attr[i].int_value()
9590 && in_attr[i].int_value()
9591 && out_attr[i].int_value() != in_attr[i].int_value())
9593 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
9594 "use %u-byte wchar_t; use of wchar_t values "
9595 "across objects may fail"),
9596 name, in_attr[i].int_value(),
9597 out_attr[i].int_value());
9599 else if (in_attr[i].int_value() && !out_attr[i].int_value())
9600 out_attr[i].set_int_value(in_attr[i].int_value());
9602 case elfcpp::Tag_ABI_enum_size:
9603 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
9605 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
9606 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
9608 // The existing object is compatible with anything.
9609 // Use whatever requirements the new object has.
9610 out_attr[i].set_int_value(in_attr[i].int_value());
9612 // FIXME: Make it possible to turn off this warning.
9613 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
9614 && out_attr[i].int_value() != in_attr[i].int_value())
9616 unsigned int in_value = in_attr[i].int_value();
9617 unsigned int out_value = out_attr[i].int_value();
9618 gold_warning(_("%s uses %s enums yet the output is to use "
9619 "%s enums; use of enum values across objects "
9622 this->aeabi_enum_name(in_value).c_str(),
9623 this->aeabi_enum_name(out_value).c_str());
9627 case elfcpp::Tag_ABI_VFP_args:
9630 case elfcpp::Tag_ABI_WMMX_args:
9631 if (in_attr[i].int_value() != out_attr[i].int_value())
9633 gold_error(_("%s uses iWMMXt register arguments, output does "
9638 case Object_attribute::Tag_compatibility:
9639 // Merged in target-independent code.
9641 case elfcpp::Tag_ABI_HardFP_use:
9642 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
9643 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
9644 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
9645 out_attr[i].set_int_value(3);
9646 else if (in_attr[i].int_value() > out_attr[i].int_value())
9647 out_attr[i].set_int_value(in_attr[i].int_value());
9649 case elfcpp::Tag_ABI_FP_16bit_format:
9650 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
9652 if (in_attr[i].int_value() != out_attr[i].int_value())
9653 gold_error(_("fp16 format mismatch between %s and output"),
9656 if (in_attr[i].int_value() != 0)
9657 out_attr[i].set_int_value(in_attr[i].int_value());
9660 case elfcpp::Tag_nodefaults:
9661 // This tag is set if it exists, but the value is unused (and is
9662 // typically zero). We don't actually need to do anything here -
9663 // the merge happens automatically when the type flags are merged
9666 case elfcpp::Tag_also_compatible_with:
9667 // Already done in Tag_CPU_arch.
9669 case elfcpp::Tag_conformance:
9670 // Keep the attribute if it matches. Throw it away otherwise.
9671 // No attribute means no claim to conform.
9672 if (in_attr[i].string_value() != out_attr[i].string_value())
9673 out_attr[i].set_string_value("");
9678 const char* err_object = NULL;
9680 // The "known_obj_attributes" table does contain some undefined
9681 // attributes. Ensure that there are unused.
9682 if (out_attr[i].int_value() != 0
9683 || out_attr[i].string_value() != "")
9684 err_object = "output";
9685 else if (in_attr[i].int_value() != 0
9686 || in_attr[i].string_value() != "")
9689 if (err_object != NULL)
9691 // Attribute numbers >=64 (mod 128) can be safely ignored.
9693 gold_error(_("%s: unknown mandatory EABI object attribute "
9697 gold_warning(_("%s: unknown EABI object attribute %d"),
9701 // Only pass on attributes that match in both inputs.
9702 if (!in_attr[i].matches(out_attr[i]))
9704 out_attr[i].set_int_value(0);
9705 out_attr[i].set_string_value("");
9710 // If out_attr was copied from in_attr then it won't have a type yet.
9711 if (in_attr[i].type() && !out_attr[i].type())
9712 out_attr[i].set_type(in_attr[i].type());
9715 // Merge Tag_compatibility attributes and any common GNU ones.
9716 this->attributes_section_data_->merge(name, pasd);
9718 // Check for any attributes not known on ARM.
9719 typedef Vendor_object_attributes::Other_attributes Other_attributes;
9720 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
9721 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
9722 Other_attributes* out_other_attributes =
9723 this->attributes_section_data_->other_attributes(vendor);
9724 Other_attributes::iterator out_iter = out_other_attributes->begin();
9726 while (in_iter != in_other_attributes->end()
9727 || out_iter != out_other_attributes->end())
9729 const char* err_object = NULL;
9732 // The tags for each list are in numerical order.
9733 // If the tags are equal, then merge.
9734 if (out_iter != out_other_attributes->end()
9735 && (in_iter == in_other_attributes->end()
9736 || in_iter->first > out_iter->first))
9738 // This attribute only exists in output. We can't merge, and we
9739 // don't know what the tag means, so delete it.
9740 err_object = "output";
9741 err_tag = out_iter->first;
9742 int saved_tag = out_iter->first;
9743 delete out_iter->second;
9744 out_other_attributes->erase(out_iter);
9745 out_iter = out_other_attributes->upper_bound(saved_tag);
9747 else if (in_iter != in_other_attributes->end()
9748 && (out_iter != out_other_attributes->end()
9749 || in_iter->first < out_iter->first))
9751 // This attribute only exists in input. We can't merge, and we
9752 // don't know what the tag means, so ignore it.
9754 err_tag = in_iter->first;
9757 else // The tags are equal.
9759 // As present, all attributes in the list are unknown, and
9760 // therefore can't be merged meaningfully.
9761 err_object = "output";
9762 err_tag = out_iter->first;
9764 // Only pass on attributes that match in both inputs.
9765 if (!in_iter->second->matches(*(out_iter->second)))
9767 // No match. Delete the attribute.
9768 int saved_tag = out_iter->first;
9769 delete out_iter->second;
9770 out_other_attributes->erase(out_iter);
9771 out_iter = out_other_attributes->upper_bound(saved_tag);
9775 // Matched. Keep the attribute and move to the next.
9783 // Attribute numbers >=64 (mod 128) can be safely ignored. */
9784 if ((err_tag & 127) < 64)
9786 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
9787 err_object, err_tag);
9791 gold_warning(_("%s: unknown EABI object attribute %d"),
9792 err_object, err_tag);
9798 // Stub-generation methods for Target_arm.
9800 // Make a new Arm_input_section object.
9802 template<bool big_endian>
9803 Arm_input_section<big_endian>*
9804 Target_arm<big_endian>::new_arm_input_section(
9808 Section_id sid(relobj, shndx);
9810 Arm_input_section<big_endian>* arm_input_section =
9811 new Arm_input_section<big_endian>(relobj, shndx);
9812 arm_input_section->init();
9814 // Register new Arm_input_section in map for look-up.
9815 std::pair<typename Arm_input_section_map::iterator, bool> ins =
9816 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
9818 // Make sure that it we have not created another Arm_input_section
9819 // for this input section already.
9820 gold_assert(ins.second);
9822 return arm_input_section;
9825 // Find the Arm_input_section object corresponding to the SHNDX-th input
9826 // section of RELOBJ.
9828 template<bool big_endian>
9829 Arm_input_section<big_endian>*
9830 Target_arm<big_endian>::find_arm_input_section(
9832 unsigned int shndx) const
9834 Section_id sid(relobj, shndx);
9835 typename Arm_input_section_map::const_iterator p =
9836 this->arm_input_section_map_.find(sid);
9837 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
9840 // Make a new stub table.
9842 template<bool big_endian>
9843 Stub_table<big_endian>*
9844 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
9846 Stub_table<big_endian>* stub_table =
9847 new Stub_table<big_endian>(owner);
9848 this->stub_tables_.push_back(stub_table);
9850 stub_table->set_address(owner->address() + owner->data_size());
9851 stub_table->set_file_offset(owner->offset() + owner->data_size());
9852 stub_table->finalize_data_size();
9857 // Scan a relocation for stub generation.
9859 template<bool big_endian>
9861 Target_arm<big_endian>::scan_reloc_for_stub(
9862 const Relocate_info<32, big_endian>* relinfo,
9863 unsigned int r_type,
9864 const Sized_symbol<32>* gsym,
9866 const Symbol_value<32>* psymval,
9867 elfcpp::Elf_types<32>::Elf_Swxword addend,
9868 Arm_address address)
9870 typedef typename Target_arm<big_endian>::Relocate Relocate;
9872 const Arm_relobj<big_endian>* arm_relobj =
9873 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
9875 if (r_type == elfcpp::R_ARM_V4BX)
9877 const uint32_t reg = (addend & 0xf);
9878 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
9881 // Try looking up an existing stub from a stub table.
9882 Stub_table<big_endian>* stub_table =
9883 arm_relobj->stub_table(relinfo->data_shndx);
9884 gold_assert(stub_table != NULL);
9886 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
9888 // create a new stub and add it to stub table.
9889 Arm_v4bx_stub* stub =
9890 this->stub_factory().make_arm_v4bx_stub(reg);
9891 gold_assert(stub != NULL);
9892 stub_table->add_arm_v4bx_stub(stub);
9899 bool target_is_thumb;
9900 Symbol_value<32> symval;
9903 // This is a global symbol. Determine if we use PLT and if the
9904 // final target is THUMB.
9905 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
9907 // This uses a PLT, change the symbol value.
9908 symval.set_output_value(this->plt_section()->address()
9909 + gsym->plt_offset());
9911 target_is_thumb = false;
9913 else if (gsym->is_undefined())
9914 // There is no need to generate a stub symbol is undefined.
9919 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
9920 || (gsym->type() == elfcpp::STT_FUNC
9921 && !gsym->is_undefined()
9922 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
9927 // This is a local symbol. Determine if the final target is THUMB.
9928 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
9931 // Strip LSB if this points to a THUMB target.
9932 const Arm_reloc_property* reloc_property =
9933 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
9934 gold_assert(reloc_property != NULL);
9936 && reloc_property->uses_thumb_bit()
9937 && ((psymval->value(arm_relobj, 0) & 1) != 0))
9939 Arm_address stripped_value =
9940 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
9941 symval.set_output_value(stripped_value);
9945 // Get the symbol value.
9946 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
9948 // Owing to pipelining, the PC relative branches below actually skip
9949 // two instructions when the branch offset is 0.
9950 Arm_address destination;
9953 case elfcpp::R_ARM_CALL:
9954 case elfcpp::R_ARM_JUMP24:
9955 case elfcpp::R_ARM_PLT32:
9957 destination = value + addend + 8;
9959 case elfcpp::R_ARM_THM_CALL:
9960 case elfcpp::R_ARM_THM_XPC22:
9961 case elfcpp::R_ARM_THM_JUMP24:
9962 case elfcpp::R_ARM_THM_JUMP19:
9964 destination = value + addend + 4;
9970 Reloc_stub* stub = NULL;
9971 Stub_type stub_type =
9972 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
9974 if (stub_type != arm_stub_none)
9976 // Try looking up an existing stub from a stub table.
9977 Stub_table<big_endian>* stub_table =
9978 arm_relobj->stub_table(relinfo->data_shndx);
9979 gold_assert(stub_table != NULL);
9981 // Locate stub by destination.
9982 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
9984 // Create a stub if there is not one already
9985 stub = stub_table->find_reloc_stub(stub_key);
9988 // create a new stub and add it to stub table.
9989 stub = this->stub_factory().make_reloc_stub(stub_type);
9990 stub_table->add_reloc_stub(stub, stub_key);
9993 // Record the destination address.
9994 stub->set_destination_address(destination
9995 | (target_is_thumb ? 1 : 0));
9998 // For Cortex-A8, we need to record a relocation at 4K page boundary.
9999 if (this->fix_cortex_a8_
10000 && (r_type == elfcpp::R_ARM_THM_JUMP24
10001 || r_type == elfcpp::R_ARM_THM_JUMP19
10002 || r_type == elfcpp::R_ARM_THM_CALL
10003 || r_type == elfcpp::R_ARM_THM_XPC22)
10004 && (address & 0xfffU) == 0xffeU)
10006 // Found a candidate. Note we haven't checked the destination is
10007 // within 4K here: if we do so (and don't create a record) we can't
10008 // tell that a branch should have been relocated when scanning later.
10009 this->cortex_a8_relocs_info_[address] =
10010 new Cortex_a8_reloc(stub, r_type,
10011 destination | (target_is_thumb ? 1 : 0));
10015 // This function scans a relocation sections for stub generation.
10016 // The template parameter Relocate must be a class type which provides
10017 // a single function, relocate(), which implements the machine
10018 // specific part of a relocation.
10020 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
10021 // SHT_REL or SHT_RELA.
10023 // PRELOCS points to the relocation data. RELOC_COUNT is the number
10024 // of relocs. OUTPUT_SECTION is the output section.
10025 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
10026 // mapped to output offsets.
10028 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
10029 // VIEW_SIZE is the size. These refer to the input section, unless
10030 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
10031 // the output section.
10033 template<bool big_endian>
10034 template<int sh_type>
10036 Target_arm<big_endian>::scan_reloc_section_for_stubs(
10037 const Relocate_info<32, big_endian>* relinfo,
10038 const unsigned char* prelocs,
10039 size_t reloc_count,
10040 Output_section* output_section,
10041 bool needs_special_offset_handling,
10042 const unsigned char* view,
10043 elfcpp::Elf_types<32>::Elf_Addr view_address,
10046 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
10047 const int reloc_size =
10048 Reloc_types<sh_type, 32, big_endian>::reloc_size;
10050 Arm_relobj<big_endian>* arm_object =
10051 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10052 unsigned int local_count = arm_object->local_symbol_count();
10054 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
10056 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
10058 Reltype reloc(prelocs);
10060 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10061 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
10062 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
10064 r_type = this->get_real_reloc_type(r_type);
10066 // Only a few relocation types need stubs.
10067 if ((r_type != elfcpp::R_ARM_CALL)
10068 && (r_type != elfcpp::R_ARM_JUMP24)
10069 && (r_type != elfcpp::R_ARM_PLT32)
10070 && (r_type != elfcpp::R_ARM_THM_CALL)
10071 && (r_type != elfcpp::R_ARM_THM_XPC22)
10072 && (r_type != elfcpp::R_ARM_THM_JUMP24)
10073 && (r_type != elfcpp::R_ARM_THM_JUMP19)
10074 && (r_type != elfcpp::R_ARM_V4BX))
10077 section_offset_type offset =
10078 convert_to_section_size_type(reloc.get_r_offset());
10080 if (needs_special_offset_handling)
10082 offset = output_section->output_offset(relinfo->object,
10083 relinfo->data_shndx,
10089 if (r_type == elfcpp::R_ARM_V4BX)
10091 // Get the BX instruction.
10092 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10093 const Valtype* wv = reinterpret_cast<const Valtype*>(view + offset);
10094 elfcpp::Elf_types<32>::Elf_Swxword insn =
10095 elfcpp::Swap<32, big_endian>::readval(wv);
10096 this->scan_reloc_for_stub(relinfo, r_type, NULL, 0, NULL,
10102 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
10103 elfcpp::Elf_types<32>::Elf_Swxword addend =
10104 stub_addend_reader(r_type, view + offset, reloc);
10106 const Sized_symbol<32>* sym;
10108 Symbol_value<32> symval;
10109 const Symbol_value<32> *psymval;
10110 if (r_sym < local_count)
10113 psymval = arm_object->local_symbol(r_sym);
10115 // If the local symbol belongs to a section we are discarding,
10116 // and that section is a debug section, try to find the
10117 // corresponding kept section and map this symbol to its
10118 // counterpart in the kept section. The symbol must not
10119 // correspond to a section we are folding.
10121 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10123 && shndx != elfcpp::SHN_UNDEF
10124 && !arm_object->is_section_included(shndx)
10125 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
10127 if (comdat_behavior == CB_UNDETERMINED)
10130 arm_object->section_name(relinfo->data_shndx);
10131 comdat_behavior = get_comdat_behavior(name.c_str());
10133 if (comdat_behavior == CB_PRETEND)
10136 typename elfcpp::Elf_types<32>::Elf_Addr value =
10137 arm_object->map_to_kept_section(shndx, &found);
10139 symval.set_output_value(value + psymval->input_value());
10141 symval.set_output_value(0);
10145 symval.set_output_value(0);
10147 symval.set_no_output_symtab_entry();
10153 const Symbol* gsym = arm_object->global_symbol(r_sym);
10154 gold_assert(gsym != NULL);
10155 if (gsym->is_forwarder())
10156 gsym = relinfo->symtab->resolve_forwards(gsym);
10158 sym = static_cast<const Sized_symbol<32>*>(gsym);
10159 if (sym->has_symtab_index())
10160 symval.set_output_symtab_index(sym->symtab_index());
10162 symval.set_no_output_symtab_entry();
10164 // We need to compute the would-be final value of this global
10166 const Symbol_table* symtab = relinfo->symtab;
10167 const Sized_symbol<32>* sized_symbol =
10168 symtab->get_sized_symbol<32>(gsym);
10169 Symbol_table::Compute_final_value_status status;
10170 Arm_address value =
10171 symtab->compute_final_value<32>(sized_symbol, &status);
10173 // Skip this if the symbol has not output section.
10174 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
10177 symval.set_output_value(value);
10181 // If symbol is a section symbol, we don't know the actual type of
10182 // destination. Give up.
10183 if (psymval->is_section_symbol())
10186 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
10187 addend, view_address + offset);
10191 // Scan an input section for stub generation.
10193 template<bool big_endian>
10195 Target_arm<big_endian>::scan_section_for_stubs(
10196 const Relocate_info<32, big_endian>* relinfo,
10197 unsigned int sh_type,
10198 const unsigned char* prelocs,
10199 size_t reloc_count,
10200 Output_section* output_section,
10201 bool needs_special_offset_handling,
10202 const unsigned char* view,
10203 Arm_address view_address,
10204 section_size_type view_size)
10206 if (sh_type == elfcpp::SHT_REL)
10207 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
10212 needs_special_offset_handling,
10216 else if (sh_type == elfcpp::SHT_RELA)
10217 // We do not support RELA type relocations yet. This is provided for
10219 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
10224 needs_special_offset_handling,
10229 gold_unreachable();
10232 // Group input sections for stub generation.
10234 // We goup input sections in an output sections so that the total size,
10235 // including any padding space due to alignment is smaller than GROUP_SIZE
10236 // unless the only input section in group is bigger than GROUP_SIZE already.
10237 // Then an ARM stub table is created to follow the last input section
10238 // in group. For each group an ARM stub table is created an is placed
10239 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
10240 // extend the group after the stub table.
10242 template<bool big_endian>
10244 Target_arm<big_endian>::group_sections(
10246 section_size_type group_size,
10247 bool stubs_always_after_branch)
10249 // Group input sections and insert stub table
10250 Layout::Section_list section_list;
10251 layout->get_allocated_sections(§ion_list);
10252 for (Layout::Section_list::const_iterator p = section_list.begin();
10253 p != section_list.end();
10256 Arm_output_section<big_endian>* output_section =
10257 Arm_output_section<big_endian>::as_arm_output_section(*p);
10258 output_section->group_sections(group_size, stubs_always_after_branch,
10263 // Relaxation hook. This is where we do stub generation.
10265 template<bool big_endian>
10267 Target_arm<big_endian>::do_relax(
10269 const Input_objects* input_objects,
10270 Symbol_table* symtab,
10273 // No need to generate stubs if this is a relocatable link.
10274 gold_assert(!parameters->options().relocatable());
10276 // If this is the first pass, we need to group input sections into
10278 bool done_exidx_fixup = false;
10281 // Determine the stub group size. The group size is the absolute
10282 // value of the parameter --stub-group-size. If --stub-group-size
10283 // is passed a negative value, we restict stubs to be always after
10284 // the stubbed branches.
10285 int32_t stub_group_size_param =
10286 parameters->options().stub_group_size();
10287 bool stubs_always_after_branch = stub_group_size_param < 0;
10288 section_size_type stub_group_size = abs(stub_group_size_param);
10290 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
10291 // page as the first half of a 32-bit branch straddling two 4K pages.
10292 // This is a crude way of enforcing that.
10293 if (this->fix_cortex_a8_)
10294 stubs_always_after_branch = true;
10296 if (stub_group_size == 1)
10299 // Thumb branch range is +-4MB has to be used as the default
10300 // maximum size (a given section can contain both ARM and Thumb
10301 // code, so the worst case has to be taken into account).
10303 // This value is 24K less than that, which allows for 2025
10304 // 12-byte stubs. If we exceed that, then we will fail to link.
10305 // The user will have to relink with an explicit group size
10307 stub_group_size = 4170000;
10310 group_sections(layout, stub_group_size, stubs_always_after_branch);
10312 // Also fix .ARM.exidx section coverage.
10313 Output_section* os = layout->find_output_section(".ARM.exidx");
10314 if (os != NULL && os->type() == elfcpp::SHT_ARM_EXIDX)
10316 Arm_output_section<big_endian>* exidx_output_section =
10317 Arm_output_section<big_endian>::as_arm_output_section(os);
10318 this->fix_exidx_coverage(layout, exidx_output_section, symtab);
10319 done_exidx_fixup = true;
10323 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
10324 // beginning of each relaxation pass, just blow away all the stubs.
10325 // Alternatively, we could selectively remove only the stubs and reloc
10326 // information for code sections that have moved since the last pass.
10327 // That would require more book-keeping.
10328 typedef typename Stub_table_list::iterator Stub_table_iterator;
10329 if (this->fix_cortex_a8_)
10331 // Clear all Cortex-A8 reloc information.
10332 for (typename Cortex_a8_relocs_info::const_iterator p =
10333 this->cortex_a8_relocs_info_.begin();
10334 p != this->cortex_a8_relocs_info_.end();
10337 this->cortex_a8_relocs_info_.clear();
10339 // Remove all Cortex-A8 stubs.
10340 for (Stub_table_iterator sp = this->stub_tables_.begin();
10341 sp != this->stub_tables_.end();
10343 (*sp)->remove_all_cortex_a8_stubs();
10346 // Scan relocs for relocation stubs
10347 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
10348 op != input_objects->relobj_end();
10351 Arm_relobj<big_endian>* arm_relobj =
10352 Arm_relobj<big_endian>::as_arm_relobj(*op);
10353 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
10356 // Check all stub tables to see if any of them have their data sizes
10357 // or addresses alignments changed. These are the only things that
10359 bool any_stub_table_changed = false;
10360 Unordered_set<const Output_section*> sections_needing_adjustment;
10361 for (Stub_table_iterator sp = this->stub_tables_.begin();
10362 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
10365 if ((*sp)->update_data_size_and_addralign())
10367 // Update data size of stub table owner.
10368 Arm_input_section<big_endian>* owner = (*sp)->owner();
10369 uint64_t address = owner->address();
10370 off_t offset = owner->offset();
10371 owner->reset_address_and_file_offset();
10372 owner->set_address_and_file_offset(address, offset);
10374 sections_needing_adjustment.insert(owner->output_section());
10375 any_stub_table_changed = true;
10379 // Output_section_data::output_section() returns a const pointer but we
10380 // need to update output sections, so we record all output sections needing
10381 // update above and scan the sections here to find out what sections need
10383 for(Layout::Section_list::const_iterator p = layout->section_list().begin();
10384 p != layout->section_list().end();
10387 if (sections_needing_adjustment.find(*p)
10388 != sections_needing_adjustment.end())
10389 (*p)->set_section_offsets_need_adjustment();
10392 // Stop relaxation if no EXIDX fix-up and no stub table change.
10393 bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
10395 // Finalize the stubs in the last relaxation pass.
10396 if (!continue_relaxation)
10398 for (Stub_table_iterator sp = this->stub_tables_.begin();
10399 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
10401 (*sp)->finalize_stubs();
10403 // Update output local symbol counts of objects if necessary.
10404 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
10405 op != input_objects->relobj_end();
10408 Arm_relobj<big_endian>* arm_relobj =
10409 Arm_relobj<big_endian>::as_arm_relobj(*op);
10411 // Update output local symbol counts. We need to discard local
10412 // symbols defined in parts of input sections that are discarded by
10414 if (arm_relobj->output_local_symbol_count_needs_update())
10415 arm_relobj->update_output_local_symbol_count();
10419 return continue_relaxation;
10422 // Relocate a stub.
10424 template<bool big_endian>
10426 Target_arm<big_endian>::relocate_stub(
10428 const Relocate_info<32, big_endian>* relinfo,
10429 Output_section* output_section,
10430 unsigned char* view,
10431 Arm_address address,
10432 section_size_type view_size)
10435 const Stub_template* stub_template = stub->stub_template();
10436 for (size_t i = 0; i < stub_template->reloc_count(); i++)
10438 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
10439 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
10441 unsigned int r_type = insn->r_type();
10442 section_size_type reloc_offset = stub_template->reloc_offset(i);
10443 section_size_type reloc_size = insn->size();
10444 gold_assert(reloc_offset + reloc_size <= view_size);
10446 // This is the address of the stub destination.
10447 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
10448 Symbol_value<32> symval;
10449 symval.set_output_value(target);
10451 // Synthesize a fake reloc just in case. We don't have a symbol so
10453 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
10454 memset(reloc_buffer, 0, sizeof(reloc_buffer));
10455 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
10456 reloc_write.put_r_offset(reloc_offset);
10457 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
10458 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
10460 relocate.relocate(relinfo, this, output_section,
10461 this->fake_relnum_for_stubs, rel, r_type,
10462 NULL, &symval, view + reloc_offset,
10463 address + reloc_offset, reloc_size);
10467 // Determine whether an object attribute tag takes an integer, a
10470 template<bool big_endian>
10472 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
10474 if (tag == Object_attribute::Tag_compatibility)
10475 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10476 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
10477 else if (tag == elfcpp::Tag_nodefaults)
10478 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10479 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
10480 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
10481 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
10483 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
10485 return ((tag & 1) != 0
10486 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
10487 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10490 // Reorder attributes.
10492 // The ABI defines that Tag_conformance should be emitted first, and that
10493 // Tag_nodefaults should be second (if either is defined). This sets those
10494 // two positions, and bumps up the position of all the remaining tags to
10497 template<bool big_endian>
10499 Target_arm<big_endian>::do_attributes_order(int num) const
10501 // Reorder the known object attributes in output. We want to move
10502 // Tag_conformance to position 4 and Tag_conformance to position 5
10503 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
10505 return elfcpp::Tag_conformance;
10507 return elfcpp::Tag_nodefaults;
10508 if ((num - 2) < elfcpp::Tag_nodefaults)
10510 if ((num - 1) < elfcpp::Tag_conformance)
10515 // Scan a span of THUMB code for Cortex-A8 erratum.
10517 template<bool big_endian>
10519 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
10520 Arm_relobj<big_endian>* arm_relobj,
10521 unsigned int shndx,
10522 section_size_type span_start,
10523 section_size_type span_end,
10524 const unsigned char* view,
10525 Arm_address address)
10527 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
10529 // The opcode is BLX.W, BL.W, B.W, Bcc.W
10530 // The branch target is in the same 4KB region as the
10531 // first half of the branch.
10532 // The instruction before the branch is a 32-bit
10533 // length non-branch instruction.
10534 section_size_type i = span_start;
10535 bool last_was_32bit = false;
10536 bool last_was_branch = false;
10537 while (i < span_end)
10539 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10540 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
10541 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
10542 bool is_blx = false, is_b = false;
10543 bool is_bl = false, is_bcc = false;
10545 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
10548 // Load the rest of the insn (in manual-friendly order).
10549 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
10551 // Encoding T4: B<c>.W.
10552 is_b = (insn & 0xf800d000U) == 0xf0009000U;
10553 // Encoding T1: BL<c>.W.
10554 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
10555 // Encoding T2: BLX<c>.W.
10556 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
10557 // Encoding T3: B<c>.W (not permitted in IT block).
10558 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
10559 && (insn & 0x07f00000U) != 0x03800000U);
10562 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
10564 // If this instruction is a 32-bit THUMB branch that crosses a 4K
10565 // page boundary and it follows 32-bit non-branch instruction,
10566 // we need to work around.
10567 if (is_32bit_branch
10568 && ((address + i) & 0xfffU) == 0xffeU
10570 && !last_was_branch)
10572 // Check to see if there is a relocation stub for this branch.
10573 bool force_target_arm = false;
10574 bool force_target_thumb = false;
10575 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
10576 Cortex_a8_relocs_info::const_iterator p =
10577 this->cortex_a8_relocs_info_.find(address + i);
10579 if (p != this->cortex_a8_relocs_info_.end())
10581 cortex_a8_reloc = p->second;
10582 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
10584 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10585 && !target_is_thumb)
10586 force_target_arm = true;
10587 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10588 && target_is_thumb)
10589 force_target_thumb = true;
10593 Stub_type stub_type = arm_stub_none;
10595 // Check if we have an offending branch instruction.
10596 uint16_t upper_insn = (insn >> 16) & 0xffffU;
10597 uint16_t lower_insn = insn & 0xffffU;
10598 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10600 if (cortex_a8_reloc != NULL
10601 && cortex_a8_reloc->reloc_stub() != NULL)
10602 // We've already made a stub for this instruction, e.g.
10603 // it's a long branch or a Thumb->ARM stub. Assume that
10604 // stub will suffice to work around the A8 erratum (see
10605 // setting of always_after_branch above).
10609 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
10611 stub_type = arm_stub_a8_veneer_b_cond;
10613 else if (is_b || is_bl || is_blx)
10615 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
10620 stub_type = (is_blx
10621 ? arm_stub_a8_veneer_blx
10623 ? arm_stub_a8_veneer_bl
10624 : arm_stub_a8_veneer_b));
10627 if (stub_type != arm_stub_none)
10629 Arm_address pc_for_insn = address + i + 4;
10631 // The original instruction is a BL, but the target is
10632 // an ARM instruction. If we were not making a stub,
10633 // the BL would have been converted to a BLX. Use the
10634 // BLX stub instead in that case.
10635 if (this->may_use_blx() && force_target_arm
10636 && stub_type == arm_stub_a8_veneer_bl)
10638 stub_type = arm_stub_a8_veneer_blx;
10642 // Conversely, if the original instruction was
10643 // BLX but the target is Thumb mode, use the BL stub.
10644 else if (force_target_thumb
10645 && stub_type == arm_stub_a8_veneer_blx)
10647 stub_type = arm_stub_a8_veneer_bl;
10655 // If we found a relocation, use the proper destination,
10656 // not the offset in the (unrelocated) instruction.
10657 // Note this is always done if we switched the stub type above.
10658 if (cortex_a8_reloc != NULL)
10659 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
10661 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
10663 // Add a new stub if destination address in in the same page.
10664 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
10666 Cortex_a8_stub* stub =
10667 this->stub_factory_.make_cortex_a8_stub(stub_type,
10671 Stub_table<big_endian>* stub_table =
10672 arm_relobj->stub_table(shndx);
10673 gold_assert(stub_table != NULL);
10674 stub_table->add_cortex_a8_stub(address + i, stub);
10679 i += insn_32bit ? 4 : 2;
10680 last_was_32bit = insn_32bit;
10681 last_was_branch = is_32bit_branch;
10685 // Apply the Cortex-A8 workaround.
10687 template<bool big_endian>
10689 Target_arm<big_endian>::apply_cortex_a8_workaround(
10690 const Cortex_a8_stub* stub,
10691 Arm_address stub_address,
10692 unsigned char* insn_view,
10693 Arm_address insn_address)
10695 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10696 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
10697 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
10698 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
10699 off_t branch_offset = stub_address - (insn_address + 4);
10701 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10702 switch (stub->stub_template()->type())
10704 case arm_stub_a8_veneer_b_cond:
10705 gold_assert(!utils::has_overflow<21>(branch_offset));
10706 upper_insn = RelocFuncs::thumb32_cond_branch_upper(upper_insn,
10708 lower_insn = RelocFuncs::thumb32_cond_branch_lower(lower_insn,
10712 case arm_stub_a8_veneer_b:
10713 case arm_stub_a8_veneer_bl:
10714 case arm_stub_a8_veneer_blx:
10715 if ((lower_insn & 0x5000U) == 0x4000U)
10716 // For a BLX instruction, make sure that the relocation is
10717 // rounded up to a word boundary. This follows the semantics of
10718 // the instruction which specifies that bit 1 of the target
10719 // address will come from bit 1 of the base address.
10720 branch_offset = (branch_offset + 2) & ~3;
10722 // Put BRANCH_OFFSET back into the insn.
10723 gold_assert(!utils::has_overflow<25>(branch_offset));
10724 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
10725 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
10729 gold_unreachable();
10732 // Put the relocated value back in the object file:
10733 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
10734 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
10737 template<bool big_endian>
10738 class Target_selector_arm : public Target_selector
10741 Target_selector_arm()
10742 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
10743 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
10747 do_instantiate_target()
10748 { return new Target_arm<big_endian>(); }
10751 // Fix .ARM.exidx section coverage.
10753 template<bool big_endian>
10755 Target_arm<big_endian>::fix_exidx_coverage(
10757 Arm_output_section<big_endian>* exidx_section,
10758 Symbol_table* symtab)
10760 // We need to look at all the input sections in output in ascending
10761 // order of of output address. We do that by building a sorted list
10762 // of output sections by addresses. Then we looks at the output sections
10763 // in order. The input sections in an output section are already sorted
10764 // by addresses within the output section.
10766 typedef std::set<Output_section*, output_section_address_less_than>
10767 Sorted_output_section_list;
10768 Sorted_output_section_list sorted_output_sections;
10769 Layout::Section_list section_list;
10770 layout->get_allocated_sections(§ion_list);
10771 for (Layout::Section_list::const_iterator p = section_list.begin();
10772 p != section_list.end();
10775 // We only care about output sections that contain executable code.
10776 if (((*p)->flags() & elfcpp::SHF_EXECINSTR) != 0)
10777 sorted_output_sections.insert(*p);
10780 // Go over the output sections in ascending order of output addresses.
10781 typedef typename Arm_output_section<big_endian>::Text_section_list
10783 Text_section_list sorted_text_sections;
10784 for(typename Sorted_output_section_list::iterator p =
10785 sorted_output_sections.begin();
10786 p != sorted_output_sections.end();
10789 Arm_output_section<big_endian>* arm_output_section =
10790 Arm_output_section<big_endian>::as_arm_output_section(*p);
10791 arm_output_section->append_text_sections_to_list(&sorted_text_sections);
10794 exidx_section->fix_exidx_coverage(layout, sorted_text_sections, symtab);
10797 Target_selector_arm<false> target_selector_arm;
10798 Target_selector_arm<true> target_selector_armbe;
10800 } // End anonymous namespace.