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 endianness-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_(), reloc_stubs_size_(0),
872 reloc_stubs_addralign_(1), cortex_a8_stubs_(), arm_v4bx_stubs_(0xf),
873 prev_data_size_(0), prev_addralign_(1)
879 // Owner of this stub table.
880 Arm_input_section<big_endian>*
882 { return this->owner_; }
884 // Whether this stub table is empty.
888 return (this->reloc_stubs_.empty()
889 && this->cortex_a8_stubs_.empty()
890 && this->arm_v4bx_stubs_.empty());
893 // Return the current data size.
895 current_data_size() const
896 { return this->current_data_size_for_child(); }
898 // Add a STUB with using KEY. Caller is reponsible for avoid adding
899 // if already a STUB with the same key has been added.
901 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
903 const Stub_template* stub_template = stub->stub_template();
904 gold_assert(stub_template->type() == key.stub_type());
905 this->reloc_stubs_[key] = stub;
907 // Assign stub offset early. We can do this because we never remove
908 // reloc stubs and they are in the beginning of the stub table.
909 uint64_t align = stub_template->alignment();
910 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_, align);
911 stub->set_offset(this->reloc_stubs_size_);
912 this->reloc_stubs_size_ += stub_template->size();
913 this->reloc_stubs_addralign_ =
914 std::max(this->reloc_stubs_addralign_, align);
917 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
918 // Caller is reponsible for avoid adding if already a STUB with the same
919 // address has been added.
921 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
923 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
924 this->cortex_a8_stubs_.insert(value);
927 // Add an ARM V4BX relocation stub. A register index will be retrieved
930 add_arm_v4bx_stub(Arm_v4bx_stub* stub)
932 gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
933 this->arm_v4bx_stubs_[stub->reg()] = stub;
936 // Remove all Cortex-A8 stubs.
938 remove_all_cortex_a8_stubs();
940 // Look up a relocation stub using KEY. Return NULL if there is none.
942 find_reloc_stub(const Reloc_stub::Key& key) const
944 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
945 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
948 // Look up an arm v4bx relocation stub using the register index.
949 // Return NULL if there is none.
951 find_arm_v4bx_stub(const uint32_t reg) const
953 gold_assert(reg < 0xf);
954 return this->arm_v4bx_stubs_[reg];
957 // Relocate stubs in this stub table.
959 relocate_stubs(const Relocate_info<32, big_endian>*,
960 Target_arm<big_endian>*, Output_section*,
961 unsigned char*, Arm_address, section_size_type);
963 // Update data size and alignment at the end of a relaxation pass. Return
964 // true if either data size or alignment is different from that of the
965 // previous relaxation pass.
967 update_data_size_and_addralign();
969 // Finalize stubs. Set the offsets of all stubs and mark input sections
970 // needing the Cortex-A8 workaround.
974 // Apply Cortex-A8 workaround to an address range.
976 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
977 unsigned char*, Arm_address,
981 // Write out section contents.
983 do_write(Output_file*);
985 // Return the required alignment.
988 { return this->prev_addralign_; }
990 // Reset address and file offset.
992 do_reset_address_and_file_offset()
993 { this->set_current_data_size_for_child(this->prev_data_size_); }
995 // Set final data size.
997 set_final_data_size()
998 { this->set_data_size(this->current_data_size()); }
1001 // Relocate one stub.
1003 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1004 Target_arm<big_endian>*, Output_section*,
1005 unsigned char*, Arm_address, section_size_type);
1007 // Unordered map of relocation stubs.
1009 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
1010 Reloc_stub::Key::equal_to>
1013 // List of Cortex-A8 stubs ordered by addresses of branches being
1014 // fixed up in output.
1015 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
1016 // List of Arm V4BX relocation stubs ordered by associated registers.
1017 typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
1019 // Owner of this stub table.
1020 Arm_input_section<big_endian>* owner_;
1021 // The relocation stubs.
1022 Reloc_stub_map reloc_stubs_;
1023 // Size of reloc stubs.
1024 off_t reloc_stubs_size_;
1025 // Maximum address alignment of reloc stubs.
1026 uint64_t reloc_stubs_addralign_;
1027 // The cortex_a8_stubs.
1028 Cortex_a8_stub_list cortex_a8_stubs_;
1029 // The Arm V4BX relocation stubs.
1030 Arm_v4bx_stub_list arm_v4bx_stubs_;
1031 // data size of this in the previous pass.
1032 off_t prev_data_size_;
1033 // address alignment of this in the previous pass.
1034 uint64_t prev_addralign_;
1037 // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1038 // we add to the end of an EXIDX input section that goes into the output.
1040 class Arm_exidx_cantunwind : public Output_section_data
1043 Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
1044 : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
1047 // Return the object containing the section pointed by this.
1050 { return this->relobj_; }
1052 // Return the section index of the section pointed by this.
1055 { return this->shndx_; }
1059 do_write(Output_file* of)
1061 if (parameters->target().is_big_endian())
1062 this->do_fixed_endian_write<true>(of);
1064 this->do_fixed_endian_write<false>(of);
1068 // Implement do_write for a given endianness.
1069 template<bool big_endian>
1071 do_fixed_endian_write(Output_file*);
1073 // The object containing the section pointed by this.
1075 // The section index of the section pointed by this.
1076 unsigned int shndx_;
1079 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1080 // Offset map is used to map input section offset within the EXIDX section
1081 // to the output offset from the start of this EXIDX section.
1083 typedef std::map<section_offset_type, section_offset_type>
1084 Arm_exidx_section_offset_map;
1086 // Arm_exidx_merged_section class. This represents an EXIDX input section
1087 // with some of its entries merged.
1089 class Arm_exidx_merged_section : public Output_relaxed_input_section
1092 // Constructor for Arm_exidx_merged_section.
1093 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1094 // SECTION_OFFSET_MAP points to a section offset map describing how
1095 // parts of the input section are mapped to output. DELETED_BYTES is
1096 // the number of bytes deleted from the EXIDX input section.
1097 Arm_exidx_merged_section(
1098 const Arm_exidx_input_section& exidx_input_section,
1099 const Arm_exidx_section_offset_map& section_offset_map,
1100 uint32_t deleted_bytes);
1102 // Return the original EXIDX input section.
1103 const Arm_exidx_input_section&
1104 exidx_input_section() const
1105 { return this->exidx_input_section_; }
1107 // Return the section offset map.
1108 const Arm_exidx_section_offset_map&
1109 section_offset_map() const
1110 { return this->section_offset_map_; }
1113 // Write merged section into file OF.
1115 do_write(Output_file* of);
1118 do_output_offset(const Relobj*, unsigned int, section_offset_type,
1119 section_offset_type*) const;
1122 // Original EXIDX input section.
1123 const Arm_exidx_input_section& exidx_input_section_;
1124 // Section offset map.
1125 const Arm_exidx_section_offset_map& section_offset_map_;
1128 // A class to wrap an ordinary input section containing executable code.
1130 template<bool big_endian>
1131 class Arm_input_section : public Output_relaxed_input_section
1134 Arm_input_section(Relobj* relobj, unsigned int shndx)
1135 : Output_relaxed_input_section(relobj, shndx, 1),
1136 original_addralign_(1), original_size_(0), stub_table_(NULL)
1139 ~Arm_input_section()
1146 // Whether this is a stub table owner.
1148 is_stub_table_owner() const
1149 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1151 // Return the stub table.
1152 Stub_table<big_endian>*
1154 { return this->stub_table_; }
1156 // Set the stub_table.
1158 set_stub_table(Stub_table<big_endian>* stub_table)
1159 { this->stub_table_ = stub_table; }
1161 // Downcast a base pointer to an Arm_input_section pointer. This is
1162 // not type-safe but we only use Arm_input_section not the base class.
1163 static Arm_input_section<big_endian>*
1164 as_arm_input_section(Output_relaxed_input_section* poris)
1165 { return static_cast<Arm_input_section<big_endian>*>(poris); }
1167 // Return the original size of the section.
1169 original_size() const
1170 { return this->original_size_; }
1173 // Write data to output file.
1175 do_write(Output_file*);
1177 // Return required alignment of this.
1179 do_addralign() const
1181 if (this->is_stub_table_owner())
1182 return std::max(this->stub_table_->addralign(),
1183 static_cast<uint64_t>(this->original_addralign_));
1185 return this->original_addralign_;
1188 // Finalize data size.
1190 set_final_data_size();
1192 // Reset address and file offset.
1194 do_reset_address_and_file_offset();
1198 do_output_offset(const Relobj* object, unsigned int shndx,
1199 section_offset_type offset,
1200 section_offset_type* poutput) const
1202 if ((object == this->relobj())
1203 && (shndx == this->shndx())
1206 convert_types<section_offset_type, uint32_t>(this->original_size_)))
1216 // Copying is not allowed.
1217 Arm_input_section(const Arm_input_section&);
1218 Arm_input_section& operator=(const Arm_input_section&);
1220 // Address alignment of the original input section.
1221 uint32_t original_addralign_;
1222 // Section size of the original input section.
1223 uint32_t original_size_;
1225 Stub_table<big_endian>* stub_table_;
1228 // Arm_exidx_fixup class. This is used to define a number of methods
1229 // and keep states for fixing up EXIDX coverage.
1231 class Arm_exidx_fixup
1234 Arm_exidx_fixup(Output_section* exidx_output_section,
1235 bool merge_exidx_entries = true)
1236 : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1237 last_inlined_entry_(0), last_input_section_(NULL),
1238 section_offset_map_(NULL), first_output_text_section_(NULL),
1239 merge_exidx_entries_(merge_exidx_entries)
1243 { delete this->section_offset_map_; }
1245 // Process an EXIDX section for entry merging. Return number of bytes to
1246 // be deleted in output. If parts of the input EXIDX section are merged
1247 // a heap allocated Arm_exidx_section_offset_map is store in the located
1248 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1250 template<bool big_endian>
1252 process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1253 Arm_exidx_section_offset_map** psection_offset_map);
1255 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1256 // input section, if there is not one already.
1258 add_exidx_cantunwind_as_needed();
1260 // Return the output section for the text section which is linked to the
1261 // first exidx input in output.
1263 first_output_text_section() const
1264 { return this->first_output_text_section_; }
1267 // Copying is not allowed.
1268 Arm_exidx_fixup(const Arm_exidx_fixup&);
1269 Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1271 // Type of EXIDX unwind entry.
1276 // EXIDX_CANTUNWIND.
1277 UT_EXIDX_CANTUNWIND,
1284 // Process an EXIDX entry. We only care about the second word of the
1285 // entry. Return true if the entry can be deleted.
1287 process_exidx_entry(uint32_t second_word);
1289 // Update the current section offset map during EXIDX section fix-up.
1290 // If there is no map, create one. INPUT_OFFSET is the offset of a
1291 // reference point, DELETED_BYTES is the number of deleted by in the
1292 // section so far. If DELETE_ENTRY is true, the reference point and
1293 // all offsets after the previous reference point are discarded.
1295 update_offset_map(section_offset_type input_offset,
1296 section_size_type deleted_bytes, bool delete_entry);
1298 // EXIDX output section.
1299 Output_section* exidx_output_section_;
1300 // Unwind type of the last EXIDX entry processed.
1301 Unwind_type last_unwind_type_;
1302 // Last seen inlined EXIDX entry.
1303 uint32_t last_inlined_entry_;
1304 // Last processed EXIDX input section.
1305 const Arm_exidx_input_section* last_input_section_;
1306 // Section offset map created in process_exidx_section.
1307 Arm_exidx_section_offset_map* section_offset_map_;
1308 // Output section for the text section which is linked to the first exidx
1310 Output_section* first_output_text_section_;
1312 bool merge_exidx_entries_;
1315 // Arm output section class. This is defined mainly to add a number of
1316 // stub generation methods.
1318 template<bool big_endian>
1319 class Arm_output_section : public Output_section
1322 typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
1324 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1325 elfcpp::Elf_Xword flags)
1326 : Output_section(name, type, flags)
1329 ~Arm_output_section()
1332 // Group input sections for stub generation.
1334 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1336 // Downcast a base pointer to an Arm_output_section pointer. This is
1337 // not type-safe but we only use Arm_output_section not the base class.
1338 static Arm_output_section<big_endian>*
1339 as_arm_output_section(Output_section* os)
1340 { return static_cast<Arm_output_section<big_endian>*>(os); }
1342 // Append all input text sections in this into LIST.
1344 append_text_sections_to_list(Text_section_list* list);
1346 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1347 // is a list of text input sections sorted in ascending order of their
1348 // output addresses.
1350 fix_exidx_coverage(Layout* layout,
1351 const Text_section_list& sorted_text_section,
1352 Symbol_table* symtab,
1353 bool merge_exidx_entries);
1357 typedef Output_section::Input_section Input_section;
1358 typedef Output_section::Input_section_list Input_section_list;
1360 // Create a stub group.
1361 void create_stub_group(Input_section_list::const_iterator,
1362 Input_section_list::const_iterator,
1363 Input_section_list::const_iterator,
1364 Target_arm<big_endian>*,
1365 std::vector<Output_relaxed_input_section*>*);
1368 // Arm_exidx_input_section class. This represents an EXIDX input section.
1370 class Arm_exidx_input_section
1373 static const section_offset_type invalid_offset =
1374 static_cast<section_offset_type>(-1);
1376 Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1377 unsigned int link, uint32_t size, uint32_t addralign)
1378 : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1379 addralign_(addralign)
1382 ~Arm_exidx_input_section()
1385 // Accessors: This is a read-only class.
1387 // Return the object containing this EXIDX input section.
1390 { return this->relobj_; }
1392 // Return the section index of this EXIDX input section.
1395 { return this->shndx_; }
1397 // Return the section index of linked text section in the same object.
1400 { return this->link_; }
1402 // Return size of the EXIDX input section.
1405 { return this->size_; }
1407 // Reutnr address alignment of EXIDX input section.
1410 { return this->addralign_; }
1413 // Object containing this.
1415 // Section index of this.
1416 unsigned int shndx_;
1417 // text section linked to this in the same object.
1419 // Size of this. For ARM 32-bit is sufficient.
1421 // Address alignment of this. For ARM 32-bit is sufficient.
1422 uint32_t addralign_;
1425 // Arm_relobj class.
1427 template<bool big_endian>
1428 class Arm_relobj : public Sized_relobj<32, big_endian>
1431 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1433 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1434 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1435 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1436 stub_tables_(), local_symbol_is_thumb_function_(),
1437 attributes_section_data_(NULL), mapping_symbols_info_(),
1438 section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
1439 output_local_symbol_count_needs_update_(false),
1440 merge_flags_and_attributes_(true)
1444 { delete this->attributes_section_data_; }
1446 // Return the stub table of the SHNDX-th section if there is one.
1447 Stub_table<big_endian>*
1448 stub_table(unsigned int shndx) const
1450 gold_assert(shndx < this->stub_tables_.size());
1451 return this->stub_tables_[shndx];
1454 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1456 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1458 gold_assert(shndx < this->stub_tables_.size());
1459 this->stub_tables_[shndx] = stub_table;
1462 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1463 // index. This is only valid after do_count_local_symbol is called.
1465 local_symbol_is_thumb_function(unsigned int r_sym) const
1467 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1468 return this->local_symbol_is_thumb_function_[r_sym];
1471 // Scan all relocation sections for stub generation.
1473 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1476 // Convert regular input section with index SHNDX to a relaxed section.
1478 convert_input_section_to_relaxed_section(unsigned shndx)
1480 // The stubs have relocations and we need to process them after writing
1481 // out the stubs. So relocation now must follow section write.
1482 this->set_section_offset(shndx, -1ULL);
1483 this->set_relocs_must_follow_section_writes();
1486 // Downcast a base pointer to an Arm_relobj pointer. This is
1487 // not type-safe but we only use Arm_relobj not the base class.
1488 static Arm_relobj<big_endian>*
1489 as_arm_relobj(Relobj* relobj)
1490 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1492 // Processor-specific flags in ELF file header. This is valid only after
1495 processor_specific_flags() const
1496 { return this->processor_specific_flags_; }
1498 // Attribute section data This is the contents of the .ARM.attribute section
1500 const Attributes_section_data*
1501 attributes_section_data() const
1502 { return this->attributes_section_data_; }
1504 // Mapping symbol location.
1505 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1507 // Functor for STL container.
1508 struct Mapping_symbol_position_less
1511 operator()(const Mapping_symbol_position& p1,
1512 const Mapping_symbol_position& p2) const
1514 return (p1.first < p2.first
1515 || (p1.first == p2.first && p1.second < p2.second));
1519 // We only care about the first character of a mapping symbol, so
1520 // we only store that instead of the whole symbol name.
1521 typedef std::map<Mapping_symbol_position, char,
1522 Mapping_symbol_position_less> Mapping_symbols_info;
1524 // Whether a section contains any Cortex-A8 workaround.
1526 section_has_cortex_a8_workaround(unsigned int shndx) const
1528 return (this->section_has_cortex_a8_workaround_ != NULL
1529 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1532 // Mark a section that has Cortex-A8 workaround.
1534 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1536 if (this->section_has_cortex_a8_workaround_ == NULL)
1537 this->section_has_cortex_a8_workaround_ =
1538 new std::vector<bool>(this->shnum(), false);
1539 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1542 // Return the EXIDX section of an text section with index SHNDX or NULL
1543 // if the text section has no associated EXIDX section.
1544 const Arm_exidx_input_section*
1545 exidx_input_section_by_link(unsigned int shndx) const
1547 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1548 return ((p != this->exidx_section_map_.end()
1549 && p->second->link() == shndx)
1554 // Return the EXIDX section with index SHNDX or NULL if there is none.
1555 const Arm_exidx_input_section*
1556 exidx_input_section_by_shndx(unsigned shndx) const
1558 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1559 return ((p != this->exidx_section_map_.end()
1560 && p->second->shndx() == shndx)
1565 // Whether output local symbol count needs updating.
1567 output_local_symbol_count_needs_update() const
1568 { return this->output_local_symbol_count_needs_update_; }
1570 // Set output_local_symbol_count_needs_update flag to be true.
1572 set_output_local_symbol_count_needs_update()
1573 { this->output_local_symbol_count_needs_update_ = true; }
1575 // Update output local symbol count at the end of relaxation.
1577 update_output_local_symbol_count();
1579 // Whether we want to merge processor-specific flags and attributes.
1581 merge_flags_and_attributes() const
1582 { return this->merge_flags_and_attributes_; }
1585 // Post constructor setup.
1589 // Call parent's setup method.
1590 Sized_relobj<32, big_endian>::do_setup();
1592 // Initialize look-up tables.
1593 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1594 this->stub_tables_.swap(empty_stub_table_list);
1597 // Count the local symbols.
1599 do_count_local_symbols(Stringpool_template<char>*,
1600 Stringpool_template<char>*);
1603 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1604 const unsigned char* pshdrs,
1605 typename Sized_relobj<32, big_endian>::Views* pivews);
1607 // Read the symbol information.
1609 do_read_symbols(Read_symbols_data* sd);
1611 // Process relocs for garbage collection.
1613 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1617 // Whether a section needs to be scanned for relocation stubs.
1619 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1620 const Relobj::Output_sections&,
1621 const Symbol_table *, const unsigned char*);
1623 // Whether a section is a scannable text section.
1625 section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
1626 const Output_section*, const Symbol_table *);
1628 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1630 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1631 unsigned int, Output_section*,
1632 const Symbol_table *);
1634 // Scan a section for the Cortex-A8 erratum.
1636 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1637 unsigned int, Output_section*,
1638 Target_arm<big_endian>*);
1640 // Find the linked text section of an EXIDX section by looking at the
1641 // first reloction of the EXIDX section. PSHDR points to the section
1642 // headers of a relocation section and PSYMS points to the local symbols.
1643 // PSHNDX points to a location storing the text section index if found.
1644 // Return whether we can find the linked section.
1646 find_linked_text_section(const unsigned char* pshdr,
1647 const unsigned char* psyms, unsigned int* pshndx);
1650 // Make a new Arm_exidx_input_section object for EXIDX section with
1651 // index SHNDX and section header SHDR. TEXT_SHNDX is the section
1652 // index of the linked text section.
1654 make_exidx_input_section(unsigned int shndx,
1655 const elfcpp::Shdr<32, big_endian>& shdr,
1656 unsigned int text_shndx);
1658 // Return the output address of either a plain input section or a
1659 // relaxed input section. SHNDX is the section index.
1661 simple_input_section_output_address(unsigned int, Output_section*);
1663 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1664 typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1667 // List of stub tables.
1668 Stub_table_list stub_tables_;
1669 // Bit vector to tell if a local symbol is a thumb function or not.
1670 // This is only valid after do_count_local_symbol is called.
1671 std::vector<bool> local_symbol_is_thumb_function_;
1672 // processor-specific flags in ELF file header.
1673 elfcpp::Elf_Word processor_specific_flags_;
1674 // Object attributes if there is an .ARM.attributes section or NULL.
1675 Attributes_section_data* attributes_section_data_;
1676 // Mapping symbols information.
1677 Mapping_symbols_info mapping_symbols_info_;
1678 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1679 std::vector<bool>* section_has_cortex_a8_workaround_;
1680 // Map a text section to its associated .ARM.exidx section, if there is one.
1681 Exidx_section_map exidx_section_map_;
1682 // Whether output local symbol count needs updating.
1683 bool output_local_symbol_count_needs_update_;
1684 // Whether we merge processor flags and attributes of this object to
1686 bool merge_flags_and_attributes_;
1689 // Arm_dynobj class.
1691 template<bool big_endian>
1692 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1695 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1696 const elfcpp::Ehdr<32, big_endian>& ehdr)
1697 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1698 processor_specific_flags_(0), attributes_section_data_(NULL)
1702 { delete this->attributes_section_data_; }
1704 // Downcast a base pointer to an Arm_relobj pointer. This is
1705 // not type-safe but we only use Arm_relobj not the base class.
1706 static Arm_dynobj<big_endian>*
1707 as_arm_dynobj(Dynobj* dynobj)
1708 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1710 // Processor-specific flags in ELF file header. This is valid only after
1713 processor_specific_flags() const
1714 { return this->processor_specific_flags_; }
1716 // Attributes section data.
1717 const Attributes_section_data*
1718 attributes_section_data() const
1719 { return this->attributes_section_data_; }
1722 // Read the symbol information.
1724 do_read_symbols(Read_symbols_data* sd);
1727 // processor-specific flags in ELF file header.
1728 elfcpp::Elf_Word processor_specific_flags_;
1729 // Object attributes if there is an .ARM.attributes section or NULL.
1730 Attributes_section_data* attributes_section_data_;
1733 // Functor to read reloc addends during stub generation.
1735 template<int sh_type, bool big_endian>
1736 struct Stub_addend_reader
1738 // Return the addend for a relocation of a particular type. Depending
1739 // on whether this is a REL or RELA relocation, read the addend from a
1740 // view or from a Reloc object.
1741 elfcpp::Elf_types<32>::Elf_Swxword
1743 unsigned int /* r_type */,
1744 const unsigned char* /* view */,
1745 const typename Reloc_types<sh_type,
1746 32, big_endian>::Reloc& /* reloc */) const;
1749 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1751 template<bool big_endian>
1752 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1754 elfcpp::Elf_types<32>::Elf_Swxword
1757 const unsigned char*,
1758 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1761 // Specialized Stub_addend_reader for RELA type relocation sections.
1762 // We currently do not handle RELA type relocation sections but it is trivial
1763 // to implement the addend reader. This is provided for completeness and to
1764 // make it easier to add support for RELA relocation sections in the future.
1766 template<bool big_endian>
1767 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1769 elfcpp::Elf_types<32>::Elf_Swxword
1772 const unsigned char*,
1773 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1774 big_endian>::Reloc& reloc) const
1775 { return reloc.get_r_addend(); }
1778 // Cortex_a8_reloc class. We keep record of relocation that may need
1779 // the Cortex-A8 erratum workaround.
1781 class Cortex_a8_reloc
1784 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1785 Arm_address destination)
1786 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1792 // Accessors: This is a read-only class.
1794 // Return the relocation stub associated with this relocation if there is
1798 { return this->reloc_stub_; }
1800 // Return the relocation type.
1803 { return this->r_type_; }
1805 // Return the destination address of the relocation. LSB stores the THUMB
1809 { return this->destination_; }
1812 // Associated relocation stub if there is one, or NULL.
1813 const Reloc_stub* reloc_stub_;
1815 unsigned int r_type_;
1816 // Destination address of this relocation. LSB is used to distinguish
1818 Arm_address destination_;
1821 // Arm_output_data_got class. We derive this from Output_data_got to add
1822 // extra methods to handle TLS relocations in a static link.
1824 template<bool big_endian>
1825 class Arm_output_data_got : public Output_data_got<32, big_endian>
1828 Arm_output_data_got(Symbol_table* symtab, Layout* layout)
1829 : Output_data_got<32, big_endian>(), symbol_table_(symtab), layout_(layout)
1832 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1833 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1834 // applied in a static link.
1836 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1837 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
1839 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1840 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1841 // relocation that needs to be applied in a static link.
1843 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1844 Sized_relobj<32, big_endian>* relobj, unsigned int index)
1846 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
1850 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
1851 // The first one is initialized to be 1, which is the module index for
1852 // the main executable and the second one 0. A reloc of the type
1853 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
1854 // be applied by gold. GSYM is a global symbol.
1856 add_tls_gd32_with_static_reloc(unsigned int got_type, Symbol* gsym);
1858 // Same as the above but for a local symbol in OBJECT with INDEX.
1860 add_tls_gd32_with_static_reloc(unsigned int got_type,
1861 Sized_relobj<32, big_endian>* object,
1862 unsigned int index);
1865 // Write out the GOT table.
1867 do_write(Output_file*);
1870 // This class represent dynamic relocations that need to be applied by
1871 // gold because we are using TLS relocations in a static link.
1875 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1876 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
1877 { this->u_.global.symbol = gsym; }
1879 Static_reloc(unsigned int got_offset, unsigned int r_type,
1880 Sized_relobj<32, big_endian>* relobj, unsigned int index)
1881 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
1883 this->u_.local.relobj = relobj;
1884 this->u_.local.index = index;
1887 // Return the GOT offset.
1890 { return this->got_offset_; }
1895 { return this->r_type_; }
1897 // Whether the symbol is global or not.
1899 symbol_is_global() const
1900 { return this->symbol_is_global_; }
1902 // For a relocation against a global symbol, the global symbol.
1906 gold_assert(this->symbol_is_global_);
1907 return this->u_.global.symbol;
1910 // For a relocation against a local symbol, the defining object.
1911 Sized_relobj<32, big_endian>*
1914 gold_assert(!this->symbol_is_global_);
1915 return this->u_.local.relobj;
1918 // For a relocation against a local symbol, the local symbol index.
1922 gold_assert(!this->symbol_is_global_);
1923 return this->u_.local.index;
1927 // GOT offset of the entry to which this relocation is applied.
1928 unsigned int got_offset_;
1929 // Type of relocation.
1930 unsigned int r_type_;
1931 // Whether this relocation is against a global symbol.
1932 bool symbol_is_global_;
1933 // A global or local symbol.
1938 // For a global symbol, the symbol itself.
1943 // For a local symbol, the object defining object.
1944 Sized_relobj<32, big_endian>* relobj;
1945 // For a local symbol, the symbol index.
1951 // Symbol table of the output object.
1952 Symbol_table* symbol_table_;
1953 // Layout of the output object.
1955 // Static relocs to be applied to the GOT.
1956 std::vector<Static_reloc> static_relocs_;
1959 // Utilities for manipulating integers of up to 32-bits
1963 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1964 // an int32_t. NO_BITS must be between 1 to 32.
1965 template<int no_bits>
1966 static inline int32_t
1967 sign_extend(uint32_t bits)
1969 gold_assert(no_bits >= 0 && no_bits <= 32);
1971 return static_cast<int32_t>(bits);
1972 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1974 uint32_t top_bit = 1U << (no_bits - 1);
1975 int32_t as_signed = static_cast<int32_t>(bits);
1976 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1979 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1980 template<int no_bits>
1982 has_overflow(uint32_t bits)
1984 gold_assert(no_bits >= 0 && no_bits <= 32);
1987 int32_t max = (1 << (no_bits - 1)) - 1;
1988 int32_t min = -(1 << (no_bits - 1));
1989 int32_t as_signed = static_cast<int32_t>(bits);
1990 return as_signed > max || as_signed < min;
1993 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1994 // fits in the given number of bits as either a signed or unsigned value.
1995 // For example, has_signed_unsigned_overflow<8> would check
1996 // -128 <= bits <= 255
1997 template<int no_bits>
1999 has_signed_unsigned_overflow(uint32_t bits)
2001 gold_assert(no_bits >= 2 && no_bits <= 32);
2004 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
2005 int32_t min = -(1 << (no_bits - 1));
2006 int32_t as_signed = static_cast<int32_t>(bits);
2007 return as_signed > max || as_signed < min;
2010 // Select bits from A and B using bits in MASK. For each n in [0..31],
2011 // the n-th bit in the result is chosen from the n-th bits of A and B.
2012 // A zero selects A and a one selects B.
2013 static inline uint32_t
2014 bit_select(uint32_t a, uint32_t b, uint32_t mask)
2015 { return (a & ~mask) | (b & mask); }
2018 template<bool big_endian>
2019 class Target_arm : public Sized_target<32, big_endian>
2022 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
2025 // When were are relocating a stub, we pass this as the relocation number.
2026 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
2029 : Sized_target<32, big_endian>(&arm_info),
2030 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
2031 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
2032 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
2033 stub_tables_(), stub_factory_(Stub_factory::get_instance()),
2034 may_use_blx_(false), should_force_pic_veneer_(false),
2035 arm_input_section_map_(), attributes_section_data_(NULL),
2036 fix_cortex_a8_(false), cortex_a8_relocs_info_()
2039 // Whether we can use BLX.
2042 { return this->may_use_blx_; }
2044 // Set use-BLX flag.
2046 set_may_use_blx(bool value)
2047 { this->may_use_blx_ = value; }
2049 // Whether we force PCI branch veneers.
2051 should_force_pic_veneer() const
2052 { return this->should_force_pic_veneer_; }
2054 // Set PIC veneer flag.
2056 set_should_force_pic_veneer(bool value)
2057 { this->should_force_pic_veneer_ = value; }
2059 // Whether we use THUMB-2 instructions.
2061 using_thumb2() const
2063 Object_attribute* attr =
2064 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2065 int arch = attr->int_value();
2066 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
2069 // Whether we use THUMB/THUMB-2 instructions only.
2071 using_thumb_only() const
2073 Object_attribute* attr =
2074 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2076 if (attr->int_value() == elfcpp::TAG_CPU_ARCH_V6_M
2077 || attr->int_value() == elfcpp::TAG_CPU_ARCH_V6S_M)
2079 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
2080 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
2082 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
2083 return attr->int_value() == 'M';
2086 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
2088 may_use_arm_nop() const
2090 Object_attribute* attr =
2091 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2092 int arch = attr->int_value();
2093 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2094 || arch == elfcpp::TAG_CPU_ARCH_V6K
2095 || arch == elfcpp::TAG_CPU_ARCH_V7
2096 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2099 // Whether we have THUMB-2 NOP.W instruction.
2101 may_use_thumb2_nop() const
2103 Object_attribute* attr =
2104 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2105 int arch = attr->int_value();
2106 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2107 || arch == elfcpp::TAG_CPU_ARCH_V7
2108 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2111 // Process the relocations to determine unreferenced sections for
2112 // garbage collection.
2114 gc_process_relocs(Symbol_table* symtab,
2116 Sized_relobj<32, big_endian>* object,
2117 unsigned int data_shndx,
2118 unsigned int sh_type,
2119 const unsigned char* prelocs,
2121 Output_section* output_section,
2122 bool needs_special_offset_handling,
2123 size_t local_symbol_count,
2124 const unsigned char* plocal_symbols);
2126 // Scan the relocations to look for symbol adjustments.
2128 scan_relocs(Symbol_table* symtab,
2130 Sized_relobj<32, big_endian>* object,
2131 unsigned int data_shndx,
2132 unsigned int sh_type,
2133 const unsigned char* prelocs,
2135 Output_section* output_section,
2136 bool needs_special_offset_handling,
2137 size_t local_symbol_count,
2138 const unsigned char* plocal_symbols);
2140 // Finalize the sections.
2142 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2144 // Return the value to use for a dynamic symbol which requires special
2147 do_dynsym_value(const Symbol*) const;
2149 // Relocate a section.
2151 relocate_section(const Relocate_info<32, big_endian>*,
2152 unsigned int sh_type,
2153 const unsigned char* prelocs,
2155 Output_section* output_section,
2156 bool needs_special_offset_handling,
2157 unsigned char* view,
2158 Arm_address view_address,
2159 section_size_type view_size,
2160 const Reloc_symbol_changes*);
2162 // Scan the relocs during a relocatable link.
2164 scan_relocatable_relocs(Symbol_table* symtab,
2166 Sized_relobj<32, big_endian>* object,
2167 unsigned int data_shndx,
2168 unsigned int sh_type,
2169 const unsigned char* prelocs,
2171 Output_section* output_section,
2172 bool needs_special_offset_handling,
2173 size_t local_symbol_count,
2174 const unsigned char* plocal_symbols,
2175 Relocatable_relocs*);
2177 // Relocate a section during a relocatable link.
2179 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
2180 unsigned int sh_type,
2181 const unsigned char* prelocs,
2183 Output_section* output_section,
2184 off_t offset_in_output_section,
2185 const Relocatable_relocs*,
2186 unsigned char* view,
2187 Arm_address view_address,
2188 section_size_type view_size,
2189 unsigned char* reloc_view,
2190 section_size_type reloc_view_size);
2192 // Return whether SYM is defined by the ABI.
2194 do_is_defined_by_abi(Symbol* sym) const
2195 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
2197 // Return whether there is a GOT section.
2199 has_got_section() const
2200 { return this->got_ != NULL; }
2202 // Return the size of the GOT section.
2206 gold_assert(this->got_ != NULL);
2207 return this->got_->data_size();
2210 // Map platform-specific reloc types
2212 get_real_reloc_type (unsigned int r_type);
2215 // Methods to support stub-generations.
2218 // Return the stub factory
2220 stub_factory() const
2221 { return this->stub_factory_; }
2223 // Make a new Arm_input_section object.
2224 Arm_input_section<big_endian>*
2225 new_arm_input_section(Relobj*, unsigned int);
2227 // Find the Arm_input_section object corresponding to the SHNDX-th input
2228 // section of RELOBJ.
2229 Arm_input_section<big_endian>*
2230 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
2232 // Make a new Stub_table
2233 Stub_table<big_endian>*
2234 new_stub_table(Arm_input_section<big_endian>*);
2236 // Scan a section for stub generation.
2238 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2239 const unsigned char*, size_t, Output_section*,
2240 bool, const unsigned char*, Arm_address,
2245 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
2246 Output_section*, unsigned char*, Arm_address,
2249 // Get the default ARM target.
2250 static Target_arm<big_endian>*
2253 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2254 && parameters->target().is_big_endian() == big_endian);
2255 return static_cast<Target_arm<big_endian>*>(
2256 parameters->sized_target<32, big_endian>());
2259 // Whether NAME belongs to a mapping symbol.
2261 is_mapping_symbol_name(const char* name)
2265 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2266 && (name[2] == '\0' || name[2] == '.'));
2269 // Whether we work around the Cortex-A8 erratum.
2271 fix_cortex_a8() const
2272 { return this->fix_cortex_a8_; }
2274 // Whether we merge exidx entries in debuginfo.
2276 merge_exidx_entries() const
2277 { return parameters->options().merge_exidx_entries(); }
2279 // Whether we fix R_ARM_V4BX relocation.
2281 // 1 - replace with MOV instruction (armv4 target)
2282 // 2 - make interworking veneer (>= armv4t targets only)
2283 General_options::Fix_v4bx
2285 { return parameters->options().fix_v4bx(); }
2287 // Scan a span of THUMB code section for Cortex-A8 erratum.
2289 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2290 section_size_type, section_size_type,
2291 const unsigned char*, Arm_address);
2293 // Apply Cortex-A8 workaround to a branch.
2295 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2296 unsigned char*, Arm_address);
2299 // Make an ELF object.
2301 do_make_elf_object(const std::string&, Input_file*, off_t,
2302 const elfcpp::Ehdr<32, big_endian>& ehdr);
2305 do_make_elf_object(const std::string&, Input_file*, off_t,
2306 const elfcpp::Ehdr<32, !big_endian>&)
2307 { gold_unreachable(); }
2310 do_make_elf_object(const std::string&, Input_file*, off_t,
2311 const elfcpp::Ehdr<64, false>&)
2312 { gold_unreachable(); }
2315 do_make_elf_object(const std::string&, Input_file*, off_t,
2316 const elfcpp::Ehdr<64, true>&)
2317 { gold_unreachable(); }
2319 // Make an output section.
2321 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2322 elfcpp::Elf_Xword flags)
2323 { return new Arm_output_section<big_endian>(name, type, flags); }
2326 do_adjust_elf_header(unsigned char* view, int len) const;
2328 // We only need to generate stubs, and hence perform relaxation if we are
2329 // not doing relocatable linking.
2331 do_may_relax() const
2332 { return !parameters->options().relocatable(); }
2335 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
2337 // Determine whether an object attribute tag takes an integer, a
2340 do_attribute_arg_type(int tag) const;
2342 // Reorder tags during output.
2344 do_attributes_order(int num) const;
2346 // This is called when the target is selected as the default.
2348 do_select_as_default_target()
2350 // No locking is required since there should only be one default target.
2351 // We cannot have both the big-endian and little-endian ARM targets
2353 gold_assert(arm_reloc_property_table == NULL);
2354 arm_reloc_property_table = new Arm_reloc_property_table();
2358 // The class which scans relocations.
2363 : issued_non_pic_error_(false)
2367 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
2368 Sized_relobj<32, big_endian>* object,
2369 unsigned int data_shndx,
2370 Output_section* output_section,
2371 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2372 const elfcpp::Sym<32, big_endian>& lsym);
2375 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
2376 Sized_relobj<32, big_endian>* object,
2377 unsigned int data_shndx,
2378 Output_section* output_section,
2379 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2383 local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2384 Sized_relobj<32, big_endian>* ,
2387 const elfcpp::Rel<32, big_endian>& ,
2389 const elfcpp::Sym<32, big_endian>&)
2393 global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2394 Sized_relobj<32, big_endian>* ,
2397 const elfcpp::Rel<32, big_endian>& ,
2398 unsigned int , Symbol*)
2403 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
2404 unsigned int r_type);
2407 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
2408 unsigned int r_type, Symbol*);
2411 check_non_pic(Relobj*, unsigned int r_type);
2413 // Almost identical to Symbol::needs_plt_entry except that it also
2414 // handles STT_ARM_TFUNC.
2416 symbol_needs_plt_entry(const Symbol* sym)
2418 // An undefined symbol from an executable does not need a PLT entry.
2419 if (sym->is_undefined() && !parameters->options().shared())
2422 return (!parameters->doing_static_link()
2423 && (sym->type() == elfcpp::STT_FUNC
2424 || sym->type() == elfcpp::STT_ARM_TFUNC)
2425 && (sym->is_from_dynobj()
2426 || sym->is_undefined()
2427 || sym->is_preemptible()));
2430 // Whether we have issued an error about a non-PIC compilation.
2431 bool issued_non_pic_error_;
2434 // The class which implements relocation.
2444 // Return whether the static relocation needs to be applied.
2446 should_apply_static_reloc(const Sized_symbol<32>* gsym,
2449 Output_section* output_section);
2451 // Do a relocation. Return false if the caller should not issue
2452 // any warnings about this relocation.
2454 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2455 Output_section*, size_t relnum,
2456 const elfcpp::Rel<32, big_endian>&,
2457 unsigned int r_type, const Sized_symbol<32>*,
2458 const Symbol_value<32>*,
2459 unsigned char*, Arm_address,
2462 // Return whether we want to pass flag NON_PIC_REF for this
2463 // reloc. This means the relocation type accesses a symbol not via
2466 reloc_is_non_pic (unsigned int r_type)
2470 // These relocation types reference GOT or PLT entries explicitly.
2471 case elfcpp::R_ARM_GOT_BREL:
2472 case elfcpp::R_ARM_GOT_ABS:
2473 case elfcpp::R_ARM_GOT_PREL:
2474 case elfcpp::R_ARM_GOT_BREL12:
2475 case elfcpp::R_ARM_PLT32_ABS:
2476 case elfcpp::R_ARM_TLS_GD32:
2477 case elfcpp::R_ARM_TLS_LDM32:
2478 case elfcpp::R_ARM_TLS_IE32:
2479 case elfcpp::R_ARM_TLS_IE12GP:
2481 // These relocate types may use PLT entries.
2482 case elfcpp::R_ARM_CALL:
2483 case elfcpp::R_ARM_THM_CALL:
2484 case elfcpp::R_ARM_JUMP24:
2485 case elfcpp::R_ARM_THM_JUMP24:
2486 case elfcpp::R_ARM_THM_JUMP19:
2487 case elfcpp::R_ARM_PLT32:
2488 case elfcpp::R_ARM_THM_XPC22:
2489 case elfcpp::R_ARM_PREL31:
2490 case elfcpp::R_ARM_SBREL31:
2499 // Do a TLS relocation.
2500 inline typename Arm_relocate_functions<big_endian>::Status
2501 relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
2502 size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
2503 const Sized_symbol<32>*, const Symbol_value<32>*,
2504 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
2509 // A class which returns the size required for a relocation type,
2510 // used while scanning relocs during a relocatable link.
2511 class Relocatable_size_for_reloc
2515 get_size_for_reloc(unsigned int, Relobj*);
2518 // Adjust TLS relocation type based on the options and whether this
2519 // is a local symbol.
2520 static tls::Tls_optimization
2521 optimize_tls_reloc(bool is_final, int r_type);
2523 // Get the GOT section, creating it if necessary.
2524 Arm_output_data_got<big_endian>*
2525 got_section(Symbol_table*, Layout*);
2527 // Get the GOT PLT section.
2529 got_plt_section() const
2531 gold_assert(this->got_plt_ != NULL);
2532 return this->got_plt_;
2535 // Create a PLT entry for a global symbol.
2537 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2539 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2541 define_tls_base_symbol(Symbol_table*, Layout*);
2543 // Create a GOT entry for the TLS module index.
2545 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2546 Sized_relobj<32, big_endian>* object);
2548 // Get the PLT section.
2549 const Output_data_plt_arm<big_endian>*
2552 gold_assert(this->plt_ != NULL);
2556 // Get the dynamic reloc section, creating it if necessary.
2558 rel_dyn_section(Layout*);
2560 // Get the section to use for TLS_DESC relocations.
2562 rel_tls_desc_section(Layout*) const;
2564 // Return true if the symbol may need a COPY relocation.
2565 // References from an executable object to non-function symbols
2566 // defined in a dynamic object may need a COPY relocation.
2568 may_need_copy_reloc(Symbol* gsym)
2570 return (gsym->type() != elfcpp::STT_ARM_TFUNC
2571 && gsym->may_need_copy_reloc());
2574 // Add a potential copy relocation.
2576 copy_reloc(Symbol_table* symtab, Layout* layout,
2577 Sized_relobj<32, big_endian>* object,
2578 unsigned int shndx, Output_section* output_section,
2579 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2581 this->copy_relocs_.copy_reloc(symtab, layout,
2582 symtab->get_sized_symbol<32>(sym),
2583 object, shndx, output_section, reloc,
2584 this->rel_dyn_section(layout));
2587 // Whether two EABI versions are compatible.
2589 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2591 // Merge processor-specific flags from input object and those in the ELF
2592 // header of the output.
2594 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2596 // Get the secondary compatible architecture.
2598 get_secondary_compatible_arch(const Attributes_section_data*);
2600 // Set the secondary compatible architecture.
2602 set_secondary_compatible_arch(Attributes_section_data*, int);
2605 tag_cpu_arch_combine(const char*, int, int*, int, int);
2607 // Helper to print AEABI enum tag value.
2609 aeabi_enum_name(unsigned int);
2611 // Return string value for TAG_CPU_name.
2613 tag_cpu_name_value(unsigned int);
2615 // Merge object attributes from input object and those in the output.
2617 merge_object_attributes(const char*, const Attributes_section_data*);
2619 // Helper to get an AEABI object attribute
2621 get_aeabi_object_attribute(int tag) const
2623 Attributes_section_data* pasd = this->attributes_section_data_;
2624 gold_assert(pasd != NULL);
2625 Object_attribute* attr =
2626 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2627 gold_assert(attr != NULL);
2632 // Methods to support stub-generations.
2635 // Group input sections for stub generation.
2637 group_sections(Layout*, section_size_type, bool);
2639 // Scan a relocation for stub generation.
2641 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2642 const Sized_symbol<32>*, unsigned int,
2643 const Symbol_value<32>*,
2644 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
2646 // Scan a relocation section for stub.
2647 template<int sh_type>
2649 scan_reloc_section_for_stubs(
2650 const Relocate_info<32, big_endian>* relinfo,
2651 const unsigned char* prelocs,
2653 Output_section* output_section,
2654 bool needs_special_offset_handling,
2655 const unsigned char* view,
2656 elfcpp::Elf_types<32>::Elf_Addr view_address,
2659 // Fix .ARM.exidx section coverage.
2661 fix_exidx_coverage(Layout*, Arm_output_section<big_endian>*, Symbol_table*);
2663 // Functors for STL set.
2664 struct output_section_address_less_than
2667 operator()(const Output_section* s1, const Output_section* s2) const
2668 { return s1->address() < s2->address(); }
2671 // Information about this specific target which we pass to the
2672 // general Target structure.
2673 static const Target::Target_info arm_info;
2675 // The types of GOT entries needed for this platform.
2678 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2679 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
2680 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
2681 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
2682 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
2685 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2687 // Map input section to Arm_input_section.
2688 typedef Unordered_map<Section_id,
2689 Arm_input_section<big_endian>*,
2691 Arm_input_section_map;
2693 // Map output addresses to relocs for Cortex-A8 erratum.
2694 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2695 Cortex_a8_relocs_info;
2698 Arm_output_data_got<big_endian>* got_;
2700 Output_data_plt_arm<big_endian>* plt_;
2701 // The GOT PLT section.
2702 Output_data_space* got_plt_;
2703 // The dynamic reloc section.
2704 Reloc_section* rel_dyn_;
2705 // Relocs saved to avoid a COPY reloc.
2706 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2707 // Space for variables copied with a COPY reloc.
2708 Output_data_space* dynbss_;
2709 // Offset of the GOT entry for the TLS module index.
2710 unsigned int got_mod_index_offset_;
2711 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2712 bool tls_base_symbol_defined_;
2713 // Vector of Stub_tables created.
2714 Stub_table_list stub_tables_;
2716 const Stub_factory &stub_factory_;
2717 // Whether we can use BLX.
2719 // Whether we force PIC branch veneers.
2720 bool should_force_pic_veneer_;
2721 // Map for locating Arm_input_sections.
2722 Arm_input_section_map arm_input_section_map_;
2723 // Attributes section data in output.
2724 Attributes_section_data* attributes_section_data_;
2725 // Whether we want to fix code for Cortex-A8 erratum.
2726 bool fix_cortex_a8_;
2727 // Map addresses to relocs for Cortex-A8 erratum.
2728 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2731 template<bool big_endian>
2732 const Target::Target_info Target_arm<big_endian>::arm_info =
2735 big_endian, // is_big_endian
2736 elfcpp::EM_ARM, // machine_code
2737 false, // has_make_symbol
2738 false, // has_resolve
2739 false, // has_code_fill
2740 true, // is_default_stack_executable
2742 "/usr/lib/libc.so.1", // dynamic_linker
2743 0x8000, // default_text_segment_address
2744 0x1000, // abi_pagesize (overridable by -z max-page-size)
2745 0x1000, // common_pagesize (overridable by -z common-page-size)
2746 elfcpp::SHN_UNDEF, // small_common_shndx
2747 elfcpp::SHN_UNDEF, // large_common_shndx
2748 0, // small_common_section_flags
2749 0, // large_common_section_flags
2750 ".ARM.attributes", // attributes_section
2751 "aeabi" // attributes_vendor
2754 // Arm relocate functions class
2757 template<bool big_endian>
2758 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2763 STATUS_OKAY, // No error during relocation.
2764 STATUS_OVERFLOW, // Relocation oveflow.
2765 STATUS_BAD_RELOC // Relocation cannot be applied.
2769 typedef Relocate_functions<32, big_endian> Base;
2770 typedef Arm_relocate_functions<big_endian> This;
2772 // Encoding of imm16 argument for movt and movw ARM instructions
2775 // imm16 := imm4 | imm12
2777 // 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
2778 // +-------+---------------+-------+-------+-----------------------+
2779 // | | |imm4 | |imm12 |
2780 // +-------+---------------+-------+-------+-----------------------+
2782 // Extract the relocation addend from VAL based on the ARM
2783 // instruction encoding described above.
2784 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2785 extract_arm_movw_movt_addend(
2786 typename elfcpp::Swap<32, big_endian>::Valtype val)
2788 // According to the Elf ABI for ARM Architecture the immediate
2789 // field is sign-extended to form the addend.
2790 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2793 // Insert X into VAL based on the ARM instruction encoding described
2795 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2796 insert_val_arm_movw_movt(
2797 typename elfcpp::Swap<32, big_endian>::Valtype val,
2798 typename elfcpp::Swap<32, big_endian>::Valtype x)
2802 val |= (x & 0xf000) << 4;
2806 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2809 // imm16 := imm4 | i | imm3 | imm8
2811 // 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
2812 // +---------+-+-----------+-------++-+-----+-------+---------------+
2813 // | |i| |imm4 || |imm3 | |imm8 |
2814 // +---------+-+-----------+-------++-+-----+-------+---------------+
2816 // Extract the relocation addend from VAL based on the Thumb2
2817 // instruction encoding described above.
2818 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2819 extract_thumb_movw_movt_addend(
2820 typename elfcpp::Swap<32, big_endian>::Valtype val)
2822 // According to the Elf ABI for ARM Architecture the immediate
2823 // field is sign-extended to form the addend.
2824 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2825 | ((val >> 15) & 0x0800)
2826 | ((val >> 4) & 0x0700)
2830 // Insert X into VAL based on the Thumb2 instruction encoding
2832 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2833 insert_val_thumb_movw_movt(
2834 typename elfcpp::Swap<32, big_endian>::Valtype val,
2835 typename elfcpp::Swap<32, big_endian>::Valtype x)
2838 val |= (x & 0xf000) << 4;
2839 val |= (x & 0x0800) << 15;
2840 val |= (x & 0x0700) << 4;
2841 val |= (x & 0x00ff);
2845 // Calculate the smallest constant Kn for the specified residual.
2846 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2848 calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
2854 // Determine the most significant bit in the residual and
2855 // align the resulting value to a 2-bit boundary.
2856 for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
2858 // The desired shift is now (msb - 6), or zero, whichever
2860 return (((msb - 6) < 0) ? 0 : (msb - 6));
2863 // Calculate the final residual for the specified group index.
2864 // If the passed group index is less than zero, the method will return
2865 // the value of the specified residual without any change.
2866 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2867 static typename elfcpp::Swap<32, big_endian>::Valtype
2868 calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2871 for (int n = 0; n <= group; n++)
2873 // Calculate which part of the value to mask.
2874 uint32_t shift = calc_grp_kn(residual);
2875 // Calculate the residual for the next time around.
2876 residual &= ~(residual & (0xff << shift));
2882 // Calculate the value of Gn for the specified group index.
2883 // We return it in the form of an encoded constant-and-rotation.
2884 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2885 static typename elfcpp::Swap<32, big_endian>::Valtype
2886 calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2889 typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
2892 for (int n = 0; n <= group; n++)
2894 // Calculate which part of the value to mask.
2895 shift = calc_grp_kn(residual);
2896 // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
2897 gn = residual & (0xff << shift);
2898 // Calculate the residual for the next time around.
2901 // Return Gn in the form of an encoded constant-and-rotation.
2902 return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
2906 // Handle ARM long branches.
2907 static typename This::Status
2908 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2909 unsigned char *, const Sized_symbol<32>*,
2910 const Arm_relobj<big_endian>*, unsigned int,
2911 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2913 // Handle THUMB long branches.
2914 static typename This::Status
2915 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2916 unsigned char *, const Sized_symbol<32>*,
2917 const Arm_relobj<big_endian>*, unsigned int,
2918 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2921 // Return the branch offset of a 32-bit THUMB branch.
2922 static inline int32_t
2923 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2925 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2926 // involving the J1 and J2 bits.
2927 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2928 uint32_t upper = upper_insn & 0x3ffU;
2929 uint32_t lower = lower_insn & 0x7ffU;
2930 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2931 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2932 uint32_t i1 = j1 ^ s ? 0 : 1;
2933 uint32_t i2 = j2 ^ s ? 0 : 1;
2935 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2936 | (upper << 12) | (lower << 1));
2939 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2940 // UPPER_INSN is the original upper instruction of the branch. Caller is
2941 // responsible for overflow checking and BLX offset adjustment.
2942 static inline uint16_t
2943 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2945 uint32_t s = offset < 0 ? 1 : 0;
2946 uint32_t bits = static_cast<uint32_t>(offset);
2947 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2950 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2951 // LOWER_INSN is the original lower instruction of the branch. Caller is
2952 // responsible for overflow checking and BLX offset adjustment.
2953 static inline uint16_t
2954 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2956 uint32_t s = offset < 0 ? 1 : 0;
2957 uint32_t bits = static_cast<uint32_t>(offset);
2958 return ((lower_insn & ~0x2fffU)
2959 | ((((bits >> 23) & 1) ^ !s) << 13)
2960 | ((((bits >> 22) & 1) ^ !s) << 11)
2961 | ((bits >> 1) & 0x7ffU));
2964 // Return the branch offset of a 32-bit THUMB conditional branch.
2965 static inline int32_t
2966 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2968 uint32_t s = (upper_insn & 0x0400U) >> 10;
2969 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2970 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2971 uint32_t lower = (lower_insn & 0x07ffU);
2972 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2974 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2977 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2978 // instruction. UPPER_INSN is the original upper instruction of the branch.
2979 // Caller is responsible for overflow checking.
2980 static inline uint16_t
2981 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2983 uint32_t s = offset < 0 ? 1 : 0;
2984 uint32_t bits = static_cast<uint32_t>(offset);
2985 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2988 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2989 // instruction. LOWER_INSN is the original lower instruction of the branch.
2990 // Caller is reponsible for overflow checking.
2991 static inline uint16_t
2992 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2994 uint32_t bits = static_cast<uint32_t>(offset);
2995 uint32_t j2 = (bits & 0x00080000U) >> 19;
2996 uint32_t j1 = (bits & 0x00040000U) >> 18;
2997 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2999 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
3002 // R_ARM_ABS8: S + A
3003 static inline typename This::Status
3004 abs8(unsigned char *view,
3005 const Sized_relobj<32, big_endian>* object,
3006 const Symbol_value<32>* psymval)
3008 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
3009 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3010 Valtype* wv = reinterpret_cast<Valtype*>(view);
3011 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
3012 Reltype addend = utils::sign_extend<8>(val);
3013 Reltype x = psymval->value(object, addend);
3014 val = utils::bit_select(val, x, 0xffU);
3015 elfcpp::Swap<8, big_endian>::writeval(wv, val);
3017 // R_ARM_ABS8 permits signed or unsigned results.
3018 int signed_x = static_cast<int32_t>(x);
3019 return ((signed_x < -128 || signed_x > 255)
3020 ? This::STATUS_OVERFLOW
3021 : This::STATUS_OKAY);
3024 // R_ARM_THM_ABS5: S + A
3025 static inline typename This::Status
3026 thm_abs5(unsigned char *view,
3027 const Sized_relobj<32, big_endian>* object,
3028 const Symbol_value<32>* psymval)
3030 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3031 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3032 Valtype* wv = reinterpret_cast<Valtype*>(view);
3033 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3034 Reltype addend = (val & 0x7e0U) >> 6;
3035 Reltype x = psymval->value(object, addend);
3036 val = utils::bit_select(val, x << 6, 0x7e0U);
3037 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3039 // R_ARM_ABS16 permits signed or unsigned results.
3040 int signed_x = static_cast<int32_t>(x);
3041 return ((signed_x < -32768 || signed_x > 65535)
3042 ? This::STATUS_OVERFLOW
3043 : This::STATUS_OKAY);
3046 // R_ARM_ABS12: S + A
3047 static inline typename This::Status
3048 abs12(unsigned char *view,
3049 const Sized_relobj<32, big_endian>* object,
3050 const Symbol_value<32>* psymval)
3052 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3053 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3054 Valtype* wv = reinterpret_cast<Valtype*>(view);
3055 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3056 Reltype addend = val & 0x0fffU;
3057 Reltype x = psymval->value(object, addend);
3058 val = utils::bit_select(val, x, 0x0fffU);
3059 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3060 return (utils::has_overflow<12>(x)
3061 ? This::STATUS_OVERFLOW
3062 : This::STATUS_OKAY);
3065 // R_ARM_ABS16: S + A
3066 static inline typename This::Status
3067 abs16(unsigned char *view,
3068 const Sized_relobj<32, big_endian>* object,
3069 const Symbol_value<32>* psymval)
3071 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3072 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3073 Valtype* wv = reinterpret_cast<Valtype*>(view);
3074 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3075 Reltype addend = utils::sign_extend<16>(val);
3076 Reltype x = psymval->value(object, addend);
3077 val = utils::bit_select(val, x, 0xffffU);
3078 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3079 return (utils::has_signed_unsigned_overflow<16>(x)
3080 ? This::STATUS_OVERFLOW
3081 : This::STATUS_OKAY);
3084 // R_ARM_ABS32: (S + A) | T
3085 static inline typename This::Status
3086 abs32(unsigned char *view,
3087 const Sized_relobj<32, big_endian>* object,
3088 const Symbol_value<32>* psymval,
3089 Arm_address thumb_bit)
3091 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3092 Valtype* wv = reinterpret_cast<Valtype*>(view);
3093 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3094 Valtype x = psymval->value(object, addend) | thumb_bit;
3095 elfcpp::Swap<32, big_endian>::writeval(wv, x);
3096 return This::STATUS_OKAY;
3099 // R_ARM_REL32: (S + A) | T - P
3100 static inline typename This::Status
3101 rel32(unsigned char *view,
3102 const Sized_relobj<32, big_endian>* object,
3103 const Symbol_value<32>* psymval,
3104 Arm_address address,
3105 Arm_address thumb_bit)
3107 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3108 Valtype* wv = reinterpret_cast<Valtype*>(view);
3109 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3110 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3111 elfcpp::Swap<32, big_endian>::writeval(wv, x);
3112 return This::STATUS_OKAY;
3115 // R_ARM_THM_JUMP24: (S + A) | T - P
3116 static typename This::Status
3117 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
3118 const Symbol_value<32>* psymval, Arm_address address,
3119 Arm_address thumb_bit);
3121 // R_ARM_THM_JUMP6: S + A – P
3122 static inline typename This::Status
3123 thm_jump6(unsigned char *view,
3124 const Sized_relobj<32, big_endian>* object,
3125 const Symbol_value<32>* psymval,
3126 Arm_address address)
3128 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3129 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3130 Valtype* wv = reinterpret_cast<Valtype*>(view);
3131 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3132 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
3133 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
3134 Reltype x = (psymval->value(object, addend) - address);
3135 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
3136 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3137 // CZB does only forward jumps.
3138 return ((x > 0x007e)
3139 ? This::STATUS_OVERFLOW
3140 : This::STATUS_OKAY);
3143 // R_ARM_THM_JUMP8: S + A – P
3144 static inline typename This::Status
3145 thm_jump8(unsigned char *view,
3146 const Sized_relobj<32, big_endian>* object,
3147 const Symbol_value<32>* psymval,
3148 Arm_address address)
3150 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3151 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3152 Valtype* wv = reinterpret_cast<Valtype*>(view);
3153 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3154 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
3155 Reltype x = (psymval->value(object, addend) - address);
3156 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
3157 return (utils::has_overflow<8>(x)
3158 ? This::STATUS_OVERFLOW
3159 : This::STATUS_OKAY);
3162 // R_ARM_THM_JUMP11: S + A – P
3163 static inline typename This::Status
3164 thm_jump11(unsigned char *view,
3165 const Sized_relobj<32, big_endian>* object,
3166 const Symbol_value<32>* psymval,
3167 Arm_address address)
3169 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3170 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3171 Valtype* wv = reinterpret_cast<Valtype*>(view);
3172 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3173 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
3174 Reltype x = (psymval->value(object, addend) - address);
3175 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
3176 return (utils::has_overflow<11>(x)
3177 ? This::STATUS_OVERFLOW
3178 : This::STATUS_OKAY);
3181 // R_ARM_BASE_PREL: B(S) + A - P
3182 static inline typename This::Status
3183 base_prel(unsigned char* view,
3185 Arm_address address)
3187 Base::rel32(view, origin - address);
3191 // R_ARM_BASE_ABS: B(S) + A
3192 static inline typename This::Status
3193 base_abs(unsigned char* view,
3196 Base::rel32(view, origin);
3200 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3201 static inline typename This::Status
3202 got_brel(unsigned char* view,
3203 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
3205 Base::rel32(view, got_offset);
3206 return This::STATUS_OKAY;
3209 // R_ARM_GOT_PREL: GOT(S) + A - P
3210 static inline typename This::Status
3211 got_prel(unsigned char *view,
3212 Arm_address got_entry,
3213 Arm_address address)
3215 Base::rel32(view, got_entry - address);
3216 return This::STATUS_OKAY;
3219 // R_ARM_PREL: (S + A) | T - P
3220 static inline typename This::Status
3221 prel31(unsigned char *view,
3222 const Sized_relobj<32, big_endian>* object,
3223 const Symbol_value<32>* psymval,
3224 Arm_address address,
3225 Arm_address thumb_bit)
3227 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3228 Valtype* wv = reinterpret_cast<Valtype*>(view);
3229 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3230 Valtype addend = utils::sign_extend<31>(val);
3231 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3232 val = utils::bit_select(val, x, 0x7fffffffU);
3233 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3234 return (utils::has_overflow<31>(x) ?
3235 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3238 // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
3239 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3240 // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3241 // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
3242 static inline typename This::Status
3243 movw(unsigned char* view,
3244 const Sized_relobj<32, big_endian>* object,
3245 const Symbol_value<32>* psymval,
3246 Arm_address relative_address_base,
3247 Arm_address thumb_bit,
3248 bool check_overflow)
3250 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3251 Valtype* wv = reinterpret_cast<Valtype*>(view);
3252 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3253 Valtype addend = This::extract_arm_movw_movt_addend(val);
3254 Valtype x = ((psymval->value(object, addend) | thumb_bit)
3255 - relative_address_base);
3256 val = This::insert_val_arm_movw_movt(val, x);
3257 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3258 return ((check_overflow && utils::has_overflow<16>(x))
3259 ? This::STATUS_OVERFLOW
3260 : This::STATUS_OKAY);
3263 // R_ARM_MOVT_ABS: S + A (relative address base is 0)
3264 // R_ARM_MOVT_PREL: S + A - P
3265 // R_ARM_MOVT_BREL: S + A - B(S)
3266 static inline typename This::Status
3267 movt(unsigned char* view,
3268 const Sized_relobj<32, big_endian>* object,
3269 const Symbol_value<32>* psymval,
3270 Arm_address relative_address_base)
3272 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3273 Valtype* wv = reinterpret_cast<Valtype*>(view);
3274 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3275 Valtype addend = This::extract_arm_movw_movt_addend(val);
3276 Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3277 val = This::insert_val_arm_movw_movt(val, x);
3278 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3279 // FIXME: IHI0044D says that we should check for overflow.
3280 return This::STATUS_OKAY;
3283 // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
3284 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3285 // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3286 // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
3287 static inline typename This::Status
3288 thm_movw(unsigned char *view,
3289 const Sized_relobj<32, big_endian>* object,
3290 const Symbol_value<32>* psymval,
3291 Arm_address relative_address_base,
3292 Arm_address thumb_bit,
3293 bool check_overflow)
3295 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3296 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3297 Valtype* wv = reinterpret_cast<Valtype*>(view);
3298 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3299 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3300 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3302 (psymval->value(object, addend) | thumb_bit) - relative_address_base;
3303 val = This::insert_val_thumb_movw_movt(val, x);
3304 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3305 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3306 return ((check_overflow && utils::has_overflow<16>(x))
3307 ? This::STATUS_OVERFLOW
3308 : This::STATUS_OKAY);
3311 // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
3312 // R_ARM_THM_MOVT_PREL: S + A - P
3313 // R_ARM_THM_MOVT_BREL: S + A - B(S)
3314 static inline typename This::Status
3315 thm_movt(unsigned char* view,
3316 const Sized_relobj<32, big_endian>* object,
3317 const Symbol_value<32>* psymval,
3318 Arm_address relative_address_base)
3320 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3321 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3322 Valtype* wv = reinterpret_cast<Valtype*>(view);
3323 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3324 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3325 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3326 Reltype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3327 val = This::insert_val_thumb_movw_movt(val, x);
3328 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3329 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3330 return This::STATUS_OKAY;
3333 // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3334 static inline typename This::Status
3335 thm_alu11(unsigned char* view,
3336 const Sized_relobj<32, big_endian>* object,
3337 const Symbol_value<32>* psymval,
3338 Arm_address address,
3339 Arm_address thumb_bit)
3341 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3342 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3343 Valtype* wv = reinterpret_cast<Valtype*>(view);
3344 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3345 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3347 // 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
3348 // -----------------------------------------------------------------------
3349 // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
3350 // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
3351 // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
3352 // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
3353 // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
3354 // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
3356 // Determine a sign for the addend.
3357 const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
3358 || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3359 // Thumb2 addend encoding:
3360 // imm12 := i | imm3 | imm8
3361 int32_t addend = (insn & 0xff)
3362 | ((insn & 0x00007000) >> 4)
3363 | ((insn & 0x04000000) >> 15);
3364 // Apply a sign to the added.
3367 int32_t x = (psymval->value(object, addend) | thumb_bit)
3368 - (address & 0xfffffffc);
3369 Reltype val = abs(x);
3370 // Mask out the value and a distinct part of the ADD/SUB opcode
3371 // (bits 7:5 of opword).
3372 insn = (insn & 0xfb0f8f00)
3374 | ((val & 0x700) << 4)
3375 | ((val & 0x800) << 15);
3376 // Set the opcode according to whether the value to go in the
3377 // place is negative.
3381 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3382 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3383 return ((val > 0xfff) ?
3384 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3387 // R_ARM_THM_PC8: S + A - Pa (Thumb)
3388 static inline typename This::Status
3389 thm_pc8(unsigned char* view,
3390 const Sized_relobj<32, big_endian>* object,
3391 const Symbol_value<32>* psymval,
3392 Arm_address address)
3394 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3395 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3396 Valtype* wv = reinterpret_cast<Valtype*>(view);
3397 Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
3398 Reltype addend = ((insn & 0x00ff) << 2);
3399 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3400 Reltype val = abs(x);
3401 insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
3403 elfcpp::Swap<16, big_endian>::writeval(wv, insn);
3404 return ((val > 0x03fc)
3405 ? This::STATUS_OVERFLOW
3406 : This::STATUS_OKAY);
3409 // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3410 static inline typename This::Status
3411 thm_pc12(unsigned char* view,
3412 const Sized_relobj<32, big_endian>* object,
3413 const Symbol_value<32>* psymval,
3414 Arm_address address)
3416 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3417 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3418 Valtype* wv = reinterpret_cast<Valtype*>(view);
3419 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3420 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3421 // Determine a sign for the addend (positive if the U bit is 1).
3422 const int sign = (insn & 0x00800000) ? 1 : -1;
3423 int32_t addend = (insn & 0xfff);
3424 // Apply a sign to the added.
3427 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3428 Reltype val = abs(x);
3429 // Mask out and apply the value and the U bit.
3430 insn = (insn & 0xff7ff000) | (val & 0xfff);
3431 // Set the U bit according to whether the value to go in the
3432 // place is positive.
3436 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3437 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3438 return ((val > 0xfff) ?
3439 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3443 static inline typename This::Status
3444 v4bx(const Relocate_info<32, big_endian>* relinfo,
3445 unsigned char *view,
3446 const Arm_relobj<big_endian>* object,
3447 const Arm_address address,
3448 const bool is_interworking)
3451 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3452 Valtype* wv = reinterpret_cast<Valtype*>(view);
3453 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3455 // Ensure that we have a BX instruction.
3456 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3457 const uint32_t reg = (val & 0xf);
3458 if (is_interworking && reg != 0xf)
3460 Stub_table<big_endian>* stub_table =
3461 object->stub_table(relinfo->data_shndx);
3462 gold_assert(stub_table != NULL);
3464 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3465 gold_assert(stub != NULL);
3467 int32_t veneer_address =
3468 stub_table->address() + stub->offset() - 8 - address;
3469 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3470 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3471 // Replace with a branch to veneer (B <addr>)
3472 val = (val & 0xf0000000) | 0x0a000000
3473 | ((veneer_address >> 2) & 0x00ffffff);
3477 // Preserve Rm (lowest four bits) and the condition code
3478 // (highest four bits). Other bits encode MOV PC,Rm.
3479 val = (val & 0xf000000f) | 0x01a0f000;
3481 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3482 return This::STATUS_OKAY;
3485 // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3486 // R_ARM_ALU_PC_G0: ((S + A) | T) - P
3487 // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3488 // R_ARM_ALU_PC_G1: ((S + A) | T) - P
3489 // R_ARM_ALU_PC_G2: ((S + A) | T) - P
3490 // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3491 // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
3492 // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3493 // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
3494 // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
3495 static inline typename This::Status
3496 arm_grp_alu(unsigned char* view,
3497 const Sized_relobj<32, big_endian>* object,
3498 const Symbol_value<32>* psymval,
3500 Arm_address address,
3501 Arm_address thumb_bit,
3502 bool check_overflow)
3504 gold_assert(group >= 0 && group < 3);
3505 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3506 Valtype* wv = reinterpret_cast<Valtype*>(view);
3507 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3509 // ALU group relocations are allowed only for the ADD/SUB instructions.
3510 // (0x00800000 - ADD, 0x00400000 - SUB)
3511 const Valtype opcode = insn & 0x01e00000;
3512 if (opcode != 0x00800000 && opcode != 0x00400000)
3513 return This::STATUS_BAD_RELOC;
3515 // Determine a sign for the addend.
3516 const int sign = (opcode == 0x00800000) ? 1 : -1;
3517 // shifter = rotate_imm * 2
3518 const uint32_t shifter = (insn & 0xf00) >> 7;
3519 // Initial addend value.
3520 int32_t addend = insn & 0xff;
3521 // Rotate addend right by shifter.
3522 addend = (addend >> shifter) | (addend << (32 - shifter));
3523 // Apply a sign to the added.
3526 int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
3527 Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
3528 // Check for overflow if required
3530 && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
3531 return This::STATUS_OVERFLOW;
3533 // Mask out the value and the ADD/SUB part of the opcode; take care
3534 // not to destroy the S bit.
3536 // Set the opcode according to whether the value to go in the
3537 // place is negative.
3538 insn |= ((x < 0) ? 0x00400000 : 0x00800000);
3539 // Encode the offset (encoded Gn).
3542 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3543 return This::STATUS_OKAY;
3546 // R_ARM_LDR_PC_G0: S + A - P
3547 // R_ARM_LDR_PC_G1: S + A - P
3548 // R_ARM_LDR_PC_G2: S + A - P
3549 // R_ARM_LDR_SB_G0: S + A - B(S)
3550 // R_ARM_LDR_SB_G1: S + A - B(S)
3551 // R_ARM_LDR_SB_G2: S + A - B(S)
3552 static inline typename This::Status
3553 arm_grp_ldr(unsigned char* view,
3554 const Sized_relobj<32, big_endian>* object,
3555 const Symbol_value<32>* psymval,
3557 Arm_address address)
3559 gold_assert(group >= 0 && group < 3);
3560 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3561 Valtype* wv = reinterpret_cast<Valtype*>(view);
3562 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3564 const int sign = (insn & 0x00800000) ? 1 : -1;
3565 int32_t addend = (insn & 0xfff) * sign;
3566 int32_t x = (psymval->value(object, addend) - address);
3567 // Calculate the relevant G(n-1) value to obtain this stage residual.
3569 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3570 if (residual >= 0x1000)
3571 return This::STATUS_OVERFLOW;
3573 // Mask out the value and U bit.
3575 // Set the U bit for non-negative values.
3580 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3581 return This::STATUS_OKAY;
3584 // R_ARM_LDRS_PC_G0: S + A - P
3585 // R_ARM_LDRS_PC_G1: S + A - P
3586 // R_ARM_LDRS_PC_G2: S + A - P
3587 // R_ARM_LDRS_SB_G0: S + A - B(S)
3588 // R_ARM_LDRS_SB_G1: S + A - B(S)
3589 // R_ARM_LDRS_SB_G2: S + A - B(S)
3590 static inline typename This::Status
3591 arm_grp_ldrs(unsigned char* view,
3592 const Sized_relobj<32, big_endian>* object,
3593 const Symbol_value<32>* psymval,
3595 Arm_address address)
3597 gold_assert(group >= 0 && group < 3);
3598 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3599 Valtype* wv = reinterpret_cast<Valtype*>(view);
3600 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3602 const int sign = (insn & 0x00800000) ? 1 : -1;
3603 int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
3604 int32_t x = (psymval->value(object, addend) - address);
3605 // Calculate the relevant G(n-1) value to obtain this stage residual.
3607 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3608 if (residual >= 0x100)
3609 return This::STATUS_OVERFLOW;
3611 // Mask out the value and U bit.
3613 // Set the U bit for non-negative values.
3616 insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
3618 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3619 return This::STATUS_OKAY;
3622 // R_ARM_LDC_PC_G0: S + A - P
3623 // R_ARM_LDC_PC_G1: S + A - P
3624 // R_ARM_LDC_PC_G2: S + A - P
3625 // R_ARM_LDC_SB_G0: S + A - B(S)
3626 // R_ARM_LDC_SB_G1: S + A - B(S)
3627 // R_ARM_LDC_SB_G2: S + A - B(S)
3628 static inline typename This::Status
3629 arm_grp_ldc(unsigned char* view,
3630 const Sized_relobj<32, big_endian>* object,
3631 const Symbol_value<32>* psymval,
3633 Arm_address address)
3635 gold_assert(group >= 0 && group < 3);
3636 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3637 Valtype* wv = reinterpret_cast<Valtype*>(view);
3638 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3640 const int sign = (insn & 0x00800000) ? 1 : -1;
3641 int32_t addend = ((insn & 0xff) << 2) * sign;
3642 int32_t x = (psymval->value(object, addend) - address);
3643 // Calculate the relevant G(n-1) value to obtain this stage residual.
3645 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3646 if ((residual & 0x3) != 0 || residual >= 0x400)
3647 return This::STATUS_OVERFLOW;
3649 // Mask out the value and U bit.
3651 // Set the U bit for non-negative values.
3654 insn |= (residual >> 2);
3656 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3657 return This::STATUS_OKAY;
3661 // Relocate ARM long branches. This handles relocation types
3662 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3663 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3664 // undefined and we do not use PLT in this relocation. In such a case,
3665 // the branch is converted into an NOP.
3667 template<bool big_endian>
3668 typename Arm_relocate_functions<big_endian>::Status
3669 Arm_relocate_functions<big_endian>::arm_branch_common(
3670 unsigned int r_type,
3671 const Relocate_info<32, big_endian>* relinfo,
3672 unsigned char *view,
3673 const Sized_symbol<32>* gsym,
3674 const Arm_relobj<big_endian>* object,
3676 const Symbol_value<32>* psymval,
3677 Arm_address address,
3678 Arm_address thumb_bit,
3679 bool is_weakly_undefined_without_plt)
3681 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3682 Valtype* wv = reinterpret_cast<Valtype*>(view);
3683 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3685 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3686 && ((val & 0x0f000000UL) == 0x0a000000UL);
3687 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3688 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3689 && ((val & 0x0f000000UL) == 0x0b000000UL);
3690 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3691 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3693 // Check that the instruction is valid.
3694 if (r_type == elfcpp::R_ARM_CALL)
3696 if (!insn_is_uncond_bl && !insn_is_blx)
3697 return This::STATUS_BAD_RELOC;
3699 else if (r_type == elfcpp::R_ARM_JUMP24)
3701 if (!insn_is_b && !insn_is_cond_bl)
3702 return This::STATUS_BAD_RELOC;
3704 else if (r_type == elfcpp::R_ARM_PLT32)
3706 if (!insn_is_any_branch)
3707 return This::STATUS_BAD_RELOC;
3709 else if (r_type == elfcpp::R_ARM_XPC25)
3711 // FIXME: AAELF document IH0044C does not say much about it other
3712 // than it being obsolete.
3713 if (!insn_is_any_branch)
3714 return This::STATUS_BAD_RELOC;
3719 // A branch to an undefined weak symbol is turned into a jump to
3720 // the next instruction unless a PLT entry will be created.
3721 // Do the same for local undefined symbols.
3722 // The jump to the next instruction is optimized as a NOP depending
3723 // on the architecture.
3724 const Target_arm<big_endian>* arm_target =
3725 Target_arm<big_endian>::default_target();
3726 if (is_weakly_undefined_without_plt)
3728 Valtype cond = val & 0xf0000000U;
3729 if (arm_target->may_use_arm_nop())
3730 val = cond | 0x0320f000;
3732 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3733 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3734 return This::STATUS_OKAY;
3737 Valtype addend = utils::sign_extend<26>(val << 2);
3738 Valtype branch_target = psymval->value(object, addend);
3739 int32_t branch_offset = branch_target - address;
3741 // We need a stub if the branch offset is too large or if we need
3743 bool may_use_blx = arm_target->may_use_blx();
3744 Reloc_stub* stub = NULL;
3745 if (utils::has_overflow<26>(branch_offset)
3746 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
3748 Valtype unadjusted_branch_target = psymval->value(object, 0);
3750 Stub_type stub_type =
3751 Reloc_stub::stub_type_for_reloc(r_type, address,
3752 unadjusted_branch_target,
3754 if (stub_type != arm_stub_none)
3756 Stub_table<big_endian>* stub_table =
3757 object->stub_table(relinfo->data_shndx);
3758 gold_assert(stub_table != NULL);
3760 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3761 stub = stub_table->find_reloc_stub(stub_key);
3762 gold_assert(stub != NULL);
3763 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3764 branch_target = stub_table->address() + stub->offset() + addend;
3765 branch_offset = branch_target - address;
3766 gold_assert(!utils::has_overflow<26>(branch_offset));
3770 // At this point, if we still need to switch mode, the instruction
3771 // must either be a BLX or a BL that can be converted to a BLX.
3775 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3776 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3779 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3780 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3781 return (utils::has_overflow<26>(branch_offset)
3782 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3785 // Relocate THUMB long branches. This handles relocation types
3786 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3787 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3788 // undefined and we do not use PLT in this relocation. In such a case,
3789 // the branch is converted into an NOP.
3791 template<bool big_endian>
3792 typename Arm_relocate_functions<big_endian>::Status
3793 Arm_relocate_functions<big_endian>::thumb_branch_common(
3794 unsigned int r_type,
3795 const Relocate_info<32, big_endian>* relinfo,
3796 unsigned char *view,
3797 const Sized_symbol<32>* gsym,
3798 const Arm_relobj<big_endian>* object,
3800 const Symbol_value<32>* psymval,
3801 Arm_address address,
3802 Arm_address thumb_bit,
3803 bool is_weakly_undefined_without_plt)
3805 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3806 Valtype* wv = reinterpret_cast<Valtype*>(view);
3807 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3808 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3810 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3812 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
3813 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
3815 // Check that the instruction is valid.
3816 if (r_type == elfcpp::R_ARM_THM_CALL)
3818 if (!is_bl_insn && !is_blx_insn)
3819 return This::STATUS_BAD_RELOC;
3821 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
3823 // This cannot be a BLX.
3825 return This::STATUS_BAD_RELOC;
3827 else if (r_type == elfcpp::R_ARM_THM_XPC22)
3829 // Check for Thumb to Thumb call.
3831 return This::STATUS_BAD_RELOC;
3834 gold_warning(_("%s: Thumb BLX instruction targets "
3835 "thumb function '%s'."),
3836 object->name().c_str(),
3837 (gsym ? gsym->name() : "(local)"));
3838 // Convert BLX to BL.
3839 lower_insn |= 0x1000U;
3845 // A branch to an undefined weak symbol is turned into a jump to
3846 // the next instruction unless a PLT entry will be created.
3847 // The jump to the next instruction is optimized as a NOP.W for
3848 // Thumb-2 enabled architectures.
3849 const Target_arm<big_endian>* arm_target =
3850 Target_arm<big_endian>::default_target();
3851 if (is_weakly_undefined_without_plt)
3853 if (arm_target->may_use_thumb2_nop())
3855 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
3856 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
3860 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
3861 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
3863 return This::STATUS_OKAY;
3866 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
3867 Arm_address branch_target = psymval->value(object, addend);
3869 // For BLX, bit 1 of target address comes from bit 1 of base address.
3870 bool may_use_blx = arm_target->may_use_blx();
3871 if (thumb_bit == 0 && may_use_blx)
3872 branch_target = utils::bit_select(branch_target, address, 0x2);
3874 int32_t branch_offset = branch_target - address;
3876 // We need a stub if the branch offset is too large or if we need
3878 bool thumb2 = arm_target->using_thumb2();
3879 if ((!thumb2 && utils::has_overflow<23>(branch_offset))
3880 || (thumb2 && utils::has_overflow<25>(branch_offset))
3881 || ((thumb_bit == 0)
3882 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3883 || r_type == elfcpp::R_ARM_THM_JUMP24)))
3885 Arm_address unadjusted_branch_target = psymval->value(object, 0);
3887 Stub_type stub_type =
3888 Reloc_stub::stub_type_for_reloc(r_type, address,
3889 unadjusted_branch_target,
3892 if (stub_type != arm_stub_none)
3894 Stub_table<big_endian>* stub_table =
3895 object->stub_table(relinfo->data_shndx);
3896 gold_assert(stub_table != NULL);
3898 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3899 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
3900 gold_assert(stub != NULL);
3901 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3902 branch_target = stub_table->address() + stub->offset() + addend;
3903 if (thumb_bit == 0 && may_use_blx)
3904 branch_target = utils::bit_select(branch_target, address, 0x2);
3905 branch_offset = branch_target - address;
3909 // At this point, if we still need to switch mode, the instruction
3910 // must either be a BLX or a BL that can be converted to a BLX.
3913 gold_assert(may_use_blx
3914 && (r_type == elfcpp::R_ARM_THM_CALL
3915 || r_type == elfcpp::R_ARM_THM_XPC22));
3916 // Make sure this is a BLX.
3917 lower_insn &= ~0x1000U;
3921 // Make sure this is a BL.
3922 lower_insn |= 0x1000U;
3925 // For a BLX instruction, make sure that the relocation is rounded up
3926 // to a word boundary. This follows the semantics of the instruction
3927 // which specifies that bit 1 of the target address will come from bit
3928 // 1 of the base address.
3929 if ((lower_insn & 0x5000U) == 0x4000U)
3930 gold_assert((branch_offset & 3) == 0);
3932 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3933 // We use the Thumb-2 encoding, which is safe even if dealing with
3934 // a Thumb-1 instruction by virtue of our overflow check above. */
3935 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
3936 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
3938 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3939 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3941 gold_assert(!utils::has_overflow<25>(branch_offset));
3944 ? utils::has_overflow<25>(branch_offset)
3945 : utils::has_overflow<23>(branch_offset))
3946 ? This::STATUS_OVERFLOW
3947 : This::STATUS_OKAY);
3950 // Relocate THUMB-2 long conditional branches.
3951 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3952 // undefined and we do not use PLT in this relocation. In such a case,
3953 // the branch is converted into an NOP.
3955 template<bool big_endian>
3956 typename Arm_relocate_functions<big_endian>::Status
3957 Arm_relocate_functions<big_endian>::thm_jump19(
3958 unsigned char *view,
3959 const Arm_relobj<big_endian>* object,
3960 const Symbol_value<32>* psymval,
3961 Arm_address address,
3962 Arm_address thumb_bit)
3964 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3965 Valtype* wv = reinterpret_cast<Valtype*>(view);
3966 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3967 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3968 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
3970 Arm_address branch_target = psymval->value(object, addend);
3971 int32_t branch_offset = branch_target - address;
3973 // ??? Should handle interworking? GCC might someday try to
3974 // use this for tail calls.
3975 // FIXME: We do support thumb entry to PLT yet.
3978 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3979 return This::STATUS_BAD_RELOC;
3982 // Put RELOCATION back into the insn.
3983 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
3984 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
3986 // Put the relocated value back in the object file:
3987 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3988 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3990 return (utils::has_overflow<21>(branch_offset)
3991 ? This::STATUS_OVERFLOW
3992 : This::STATUS_OKAY);
3995 // Get the GOT section, creating it if necessary.
3997 template<bool big_endian>
3998 Arm_output_data_got<big_endian>*
3999 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
4001 if (this->got_ == NULL)
4003 gold_assert(symtab != NULL && layout != NULL);
4005 this->got_ = new Arm_output_data_got<big_endian>(symtab, layout);
4008 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4010 | elfcpp::SHF_WRITE),
4011 this->got_, false, false, false,
4013 // The old GNU linker creates a .got.plt section. We just
4014 // create another set of data in the .got section. Note that we
4015 // always create a PLT if we create a GOT, although the PLT
4017 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
4018 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4020 | elfcpp::SHF_WRITE),
4021 this->got_plt_, false, false,
4024 // The first three entries are reserved.
4025 this->got_plt_->set_current_data_size(3 * 4);
4027 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
4028 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
4029 Symbol_table::PREDEFINED,
4031 0, 0, elfcpp::STT_OBJECT,
4033 elfcpp::STV_HIDDEN, 0,
4039 // Get the dynamic reloc section, creating it if necessary.
4041 template<bool big_endian>
4042 typename Target_arm<big_endian>::Reloc_section*
4043 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
4045 if (this->rel_dyn_ == NULL)
4047 gold_assert(layout != NULL);
4048 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
4049 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
4050 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
4051 false, false, false);
4053 return this->rel_dyn_;
4056 // Insn_template methods.
4058 // Return byte size of an instruction template.
4061 Insn_template::size() const
4063 switch (this->type())
4066 case THUMB16_SPECIAL_TYPE:
4077 // Return alignment of an instruction template.
4080 Insn_template::alignment() const
4082 switch (this->type())
4085 case THUMB16_SPECIAL_TYPE:
4096 // Stub_template methods.
4098 Stub_template::Stub_template(
4099 Stub_type type, const Insn_template* insns,
4101 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
4102 entry_in_thumb_mode_(false), relocs_()
4106 // Compute byte size and alignment of stub template.
4107 for (size_t i = 0; i < insn_count; i++)
4109 unsigned insn_alignment = insns[i].alignment();
4110 size_t insn_size = insns[i].size();
4111 gold_assert((offset & (insn_alignment - 1)) == 0);
4112 this->alignment_ = std::max(this->alignment_, insn_alignment);
4113 switch (insns[i].type())
4115 case Insn_template::THUMB16_TYPE:
4116 case Insn_template::THUMB16_SPECIAL_TYPE:
4118 this->entry_in_thumb_mode_ = true;
4121 case Insn_template::THUMB32_TYPE:
4122 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
4123 this->relocs_.push_back(Reloc(i, offset));
4125 this->entry_in_thumb_mode_ = true;
4128 case Insn_template::ARM_TYPE:
4129 // Handle cases where the target is encoded within the
4131 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
4132 this->relocs_.push_back(Reloc(i, offset));
4135 case Insn_template::DATA_TYPE:
4136 // Entry point cannot be data.
4137 gold_assert(i != 0);
4138 this->relocs_.push_back(Reloc(i, offset));
4144 offset += insn_size;
4146 this->size_ = offset;
4151 // Template to implement do_write for a specific target endianness.
4153 template<bool big_endian>
4155 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
4157 const Stub_template* stub_template = this->stub_template();
4158 const Insn_template* insns = stub_template->insns();
4160 // FIXME: We do not handle BE8 encoding yet.
4161 unsigned char* pov = view;
4162 for (size_t i = 0; i < stub_template->insn_count(); i++)
4164 switch (insns[i].type())
4166 case Insn_template::THUMB16_TYPE:
4167 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
4169 case Insn_template::THUMB16_SPECIAL_TYPE:
4170 elfcpp::Swap<16, big_endian>::writeval(
4172 this->thumb16_special(i));
4174 case Insn_template::THUMB32_TYPE:
4176 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
4177 uint32_t lo = insns[i].data() & 0xffff;
4178 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
4179 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
4182 case Insn_template::ARM_TYPE:
4183 case Insn_template::DATA_TYPE:
4184 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
4189 pov += insns[i].size();
4191 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
4194 // Reloc_stub::Key methods.
4196 // Dump a Key as a string for debugging.
4199 Reloc_stub::Key::name() const
4201 if (this->r_sym_ == invalid_index)
4203 // Global symbol key name
4204 // <stub-type>:<symbol name>:<addend>.
4205 const std::string sym_name = this->u_.symbol->name();
4206 // We need to print two hex number and two colons. So just add 100 bytes
4207 // to the symbol name size.
4208 size_t len = sym_name.size() + 100;
4209 char* buffer = new char[len];
4210 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
4211 sym_name.c_str(), this->addend_);
4212 gold_assert(c > 0 && c < static_cast<int>(len));
4214 return std::string(buffer);
4218 // local symbol key name
4219 // <stub-type>:<object>:<r_sym>:<addend>.
4220 const size_t len = 200;
4222 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
4223 this->u_.relobj, this->r_sym_, this->addend_);
4224 gold_assert(c > 0 && c < static_cast<int>(len));
4225 return std::string(buffer);
4229 // Reloc_stub methods.
4231 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
4232 // LOCATION to DESTINATION.
4233 // This code is based on the arm_type_of_stub function in
4234 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
4238 Reloc_stub::stub_type_for_reloc(
4239 unsigned int r_type,
4240 Arm_address location,
4241 Arm_address destination,
4242 bool target_is_thumb)
4244 Stub_type stub_type = arm_stub_none;
4246 // This is a bit ugly but we want to avoid using a templated class for
4247 // big and little endianities.
4249 bool should_force_pic_veneer;
4252 if (parameters->target().is_big_endian())
4254 const Target_arm<true>* big_endian_target =
4255 Target_arm<true>::default_target();
4256 may_use_blx = big_endian_target->may_use_blx();
4257 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
4258 thumb2 = big_endian_target->using_thumb2();
4259 thumb_only = big_endian_target->using_thumb_only();
4263 const Target_arm<false>* little_endian_target =
4264 Target_arm<false>::default_target();
4265 may_use_blx = little_endian_target->may_use_blx();
4266 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
4267 thumb2 = little_endian_target->using_thumb2();
4268 thumb_only = little_endian_target->using_thumb_only();
4271 int64_t branch_offset;
4272 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
4274 // For THUMB BLX instruction, bit 1 of target comes from bit 1 of the
4275 // base address (instruction address + 4).
4276 if ((r_type == elfcpp::R_ARM_THM_CALL) && may_use_blx && !target_is_thumb)
4277 destination = utils::bit_select(destination, location, 0x2);
4278 branch_offset = static_cast<int64_t>(destination) - location;
4280 // Handle cases where:
4281 // - this call goes too far (different Thumb/Thumb2 max
4283 // - it's a Thumb->Arm call and blx is not available, or it's a
4284 // Thumb->Arm branch (not bl). A stub is needed in this case.
4286 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4287 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4289 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4290 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4291 || ((!target_is_thumb)
4292 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4293 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
4295 if (target_is_thumb)
4300 stub_type = (parameters->options().shared()
4301 || should_force_pic_veneer)
4304 && (r_type == elfcpp::R_ARM_THM_CALL))
4305 // V5T and above. Stub starts with ARM code, so
4306 // we must be able to switch mode before
4307 // reaching it, which is only possible for 'bl'
4308 // (ie R_ARM_THM_CALL relocation).
4309 ? arm_stub_long_branch_any_thumb_pic
4310 // On V4T, use Thumb code only.
4311 : arm_stub_long_branch_v4t_thumb_thumb_pic)
4315 && (r_type == elfcpp::R_ARM_THM_CALL))
4316 ? arm_stub_long_branch_any_any // V5T and above.
4317 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
4321 stub_type = (parameters->options().shared()
4322 || should_force_pic_veneer)
4323 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
4324 : arm_stub_long_branch_thumb_only; // non-PIC stub.
4331 // FIXME: We should check that the input section is from an
4332 // object that has interwork enabled.
4334 stub_type = (parameters->options().shared()
4335 || should_force_pic_veneer)
4338 && (r_type == elfcpp::R_ARM_THM_CALL))
4339 ? arm_stub_long_branch_any_arm_pic // V5T and above.
4340 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
4344 && (r_type == elfcpp::R_ARM_THM_CALL))
4345 ? arm_stub_long_branch_any_any // V5T and above.
4346 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
4348 // Handle v4t short branches.
4349 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4350 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4351 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4352 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4356 else if (r_type == elfcpp::R_ARM_CALL
4357 || r_type == elfcpp::R_ARM_JUMP24
4358 || r_type == elfcpp::R_ARM_PLT32)
4360 branch_offset = static_cast<int64_t>(destination) - location;
4361 if (target_is_thumb)
4365 // FIXME: We should check that the input section is from an
4366 // object that has interwork enabled.
4368 // We have an extra 2-bytes reach because of
4369 // the mode change (bit 24 (H) of BLX encoding).
4370 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4371 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4372 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
4373 || (r_type == elfcpp::R_ARM_JUMP24)
4374 || (r_type == elfcpp::R_ARM_PLT32))
4376 stub_type = (parameters->options().shared()
4377 || should_force_pic_veneer)
4380 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
4381 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
4385 ? arm_stub_long_branch_any_any // V5T and above.
4386 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
4392 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4393 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4395 stub_type = (parameters->options().shared()
4396 || should_force_pic_veneer)
4397 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
4398 : arm_stub_long_branch_any_any; /// non-PIC.
4406 // Cortex_a8_stub methods.
4408 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4409 // I is the position of the instruction template in the stub template.
4412 Cortex_a8_stub::do_thumb16_special(size_t i)
4414 // The only use of this is to copy condition code from a conditional
4415 // branch being worked around to the corresponding conditional branch in
4417 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4419 uint16_t data = this->stub_template()->insns()[i].data();
4420 gold_assert((data & 0xff00U) == 0xd000U);
4421 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
4425 // Stub_factory methods.
4427 Stub_factory::Stub_factory()
4429 // The instruction template sequences are declared as static
4430 // objects and initialized first time the constructor runs.
4432 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4433 // to reach the stub if necessary.
4434 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
4436 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4437 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4438 // dcd R_ARM_ABS32(X)
4441 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4443 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
4445 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4446 Insn_template::arm_insn(0xe12fff1c), // bx ip
4447 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4448 // dcd R_ARM_ABS32(X)
4451 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4452 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
4454 Insn_template::thumb16_insn(0xb401), // push {r0}
4455 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4456 Insn_template::thumb16_insn(0x4684), // mov ip, r0
4457 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4458 Insn_template::thumb16_insn(0x4760), // bx ip
4459 Insn_template::thumb16_insn(0xbf00), // nop
4460 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4461 // dcd R_ARM_ABS32(X)
4464 // V4T Thumb -> Thumb long branch stub. Using the stack is not
4466 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
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(0xe12fff1c), // bx ip
4472 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4473 // dcd R_ARM_ABS32(X)
4476 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4478 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
4480 Insn_template::thumb16_insn(0x4778), // bx pc
4481 Insn_template::thumb16_insn(0x46c0), // nop
4482 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4483 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4484 // dcd R_ARM_ABS32(X)
4487 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4488 // one, when the destination is close enough.
4489 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
4491 Insn_template::thumb16_insn(0x4778), // bx pc
4492 Insn_template::thumb16_insn(0x46c0), // nop
4493 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4496 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4497 // blx to reach the stub if necessary.
4498 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
4500 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4501 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4502 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4503 // dcd R_ARM_REL32(X-4)
4506 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4507 // blx to reach the stub if necessary. We can not add into pc;
4508 // it is not guaranteed to mode switch (different in ARMv6 and
4510 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
4512 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4513 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4514 Insn_template::arm_insn(0xe12fff1c), // bx ip
4515 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4516 // dcd R_ARM_REL32(X)
4519 // V4T ARM -> ARM long branch stub, PIC.
4520 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4522 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4523 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4524 Insn_template::arm_insn(0xe12fff1c), // bx ip
4525 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4526 // dcd R_ARM_REL32(X)
4529 // V4T Thumb -> ARM long branch stub, PIC.
4530 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4532 Insn_template::thumb16_insn(0x4778), // bx pc
4533 Insn_template::thumb16_insn(0x46c0), // nop
4534 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4535 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4536 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4537 // dcd R_ARM_REL32(X)
4540 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4542 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4544 Insn_template::thumb16_insn(0xb401), // push {r0}
4545 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4546 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4547 Insn_template::thumb16_insn(0x4484), // add ip, r0
4548 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4549 Insn_template::thumb16_insn(0x4760), // bx ip
4550 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4551 // dcd R_ARM_REL32(X)
4554 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4556 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4558 Insn_template::thumb16_insn(0x4778), // bx pc
4559 Insn_template::thumb16_insn(0x46c0), // nop
4560 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4561 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4562 Insn_template::arm_insn(0xe12fff1c), // bx ip
4563 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4564 // dcd R_ARM_REL32(X)
4567 // Cortex-A8 erratum-workaround stubs.
4569 // Stub used for conditional branches (which may be beyond +/-1MB away,
4570 // so we can't use a conditional branch to reach this stub).
4577 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4579 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4580 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4581 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4585 // Stub used for b.w and bl.w instructions.
4587 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4589 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4592 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4594 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4597 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4598 // instruction (which switches to ARM mode) to point to this stub. Jump to
4599 // the real destination using an ARM-mode branch.
4600 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
4602 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4605 // Stub used to provide an interworking for R_ARM_V4BX relocation
4606 // (bx r[n] instruction).
4607 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4609 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4610 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4611 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4614 // Fill in the stub template look-up table. Stub templates are constructed
4615 // per instance of Stub_factory for fast look-up without locking
4616 // in a thread-enabled environment.
4618 this->stub_templates_[arm_stub_none] =
4619 new Stub_template(arm_stub_none, NULL, 0);
4621 #define DEF_STUB(x) \
4625 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4626 Stub_type type = arm_stub_##x; \
4627 this->stub_templates_[type] = \
4628 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4636 // Stub_table methods.
4638 // Removel all Cortex-A8 stub.
4640 template<bool big_endian>
4642 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4644 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4645 p != this->cortex_a8_stubs_.end();
4648 this->cortex_a8_stubs_.clear();
4651 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4653 template<bool big_endian>
4655 Stub_table<big_endian>::relocate_stub(
4657 const Relocate_info<32, big_endian>* relinfo,
4658 Target_arm<big_endian>* arm_target,
4659 Output_section* output_section,
4660 unsigned char* view,
4661 Arm_address address,
4662 section_size_type view_size)
4664 const Stub_template* stub_template = stub->stub_template();
4665 if (stub_template->reloc_count() != 0)
4667 // Adjust view to cover the stub only.
4668 section_size_type offset = stub->offset();
4669 section_size_type stub_size = stub_template->size();
4670 gold_assert(offset + stub_size <= view_size);
4672 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4673 address + offset, stub_size);
4677 // Relocate all stubs in this stub table.
4679 template<bool big_endian>
4681 Stub_table<big_endian>::relocate_stubs(
4682 const Relocate_info<32, big_endian>* relinfo,
4683 Target_arm<big_endian>* arm_target,
4684 Output_section* output_section,
4685 unsigned char* view,
4686 Arm_address address,
4687 section_size_type view_size)
4689 // If we are passed a view bigger than the stub table's. we need to
4691 gold_assert(address == this->address()
4693 == static_cast<section_size_type>(this->data_size())));
4695 // Relocate all relocation stubs.
4696 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4697 p != this->reloc_stubs_.end();
4699 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4700 address, view_size);
4702 // Relocate all Cortex-A8 stubs.
4703 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4704 p != this->cortex_a8_stubs_.end();
4706 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4707 address, view_size);
4709 // Relocate all ARM V4BX stubs.
4710 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4711 p != this->arm_v4bx_stubs_.end();
4715 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4716 address, view_size);
4720 // Write out the stubs to file.
4722 template<bool big_endian>
4724 Stub_table<big_endian>::do_write(Output_file* of)
4726 off_t offset = this->offset();
4727 const section_size_type oview_size =
4728 convert_to_section_size_type(this->data_size());
4729 unsigned char* const oview = of->get_output_view(offset, oview_size);
4731 // Write relocation stubs.
4732 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4733 p != this->reloc_stubs_.end();
4736 Reloc_stub* stub = p->second;
4737 Arm_address address = this->address() + stub->offset();
4739 == align_address(address,
4740 stub->stub_template()->alignment()));
4741 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4745 // Write Cortex-A8 stubs.
4746 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4747 p != this->cortex_a8_stubs_.end();
4750 Cortex_a8_stub* stub = p->second;
4751 Arm_address address = this->address() + stub->offset();
4753 == align_address(address,
4754 stub->stub_template()->alignment()));
4755 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4759 // Write ARM V4BX relocation stubs.
4760 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4761 p != this->arm_v4bx_stubs_.end();
4767 Arm_address address = this->address() + (*p)->offset();
4769 == align_address(address,
4770 (*p)->stub_template()->alignment()));
4771 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4775 of->write_output_view(this->offset(), oview_size, oview);
4778 // Update the data size and address alignment of the stub table at the end
4779 // of a relaxation pass. Return true if either the data size or the
4780 // alignment changed in this relaxation pass.
4782 template<bool big_endian>
4784 Stub_table<big_endian>::update_data_size_and_addralign()
4786 // Go over all stubs in table to compute data size and address alignment.
4787 off_t size = this->reloc_stubs_size_;
4788 unsigned addralign = this->reloc_stubs_addralign_;
4790 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4791 p != this->cortex_a8_stubs_.end();
4794 const Stub_template* stub_template = p->second->stub_template();
4795 addralign = std::max(addralign, stub_template->alignment());
4796 size = (align_address(size, stub_template->alignment())
4797 + stub_template->size());
4800 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4801 p != this->arm_v4bx_stubs_.end();
4807 const Stub_template* stub_template = (*p)->stub_template();
4808 addralign = std::max(addralign, stub_template->alignment());
4809 size = (align_address(size, stub_template->alignment())
4810 + stub_template->size());
4813 // Check if either data size or alignment changed in this pass.
4814 // Update prev_data_size_ and prev_addralign_. These will be used
4815 // as the current data size and address alignment for the next pass.
4816 bool changed = size != this->prev_data_size_;
4817 this->prev_data_size_ = size;
4819 if (addralign != this->prev_addralign_)
4821 this->prev_addralign_ = addralign;
4826 // Finalize the stubs. This sets the offsets of the stubs within the stub
4827 // table. It also marks all input sections needing Cortex-A8 workaround.
4829 template<bool big_endian>
4831 Stub_table<big_endian>::finalize_stubs()
4833 off_t off = this->reloc_stubs_size_;
4834 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4835 p != this->cortex_a8_stubs_.end();
4838 Cortex_a8_stub* stub = p->second;
4839 const Stub_template* stub_template = stub->stub_template();
4840 uint64_t stub_addralign = stub_template->alignment();
4841 off = align_address(off, stub_addralign);
4842 stub->set_offset(off);
4843 off += stub_template->size();
4845 // Mark input section so that we can determine later if a code section
4846 // needs the Cortex-A8 workaround quickly.
4847 Arm_relobj<big_endian>* arm_relobj =
4848 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
4849 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
4852 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4853 p != this->arm_v4bx_stubs_.end();
4859 const Stub_template* stub_template = (*p)->stub_template();
4860 uint64_t stub_addralign = stub_template->alignment();
4861 off = align_address(off, stub_addralign);
4862 (*p)->set_offset(off);
4863 off += stub_template->size();
4866 gold_assert(off <= this->prev_data_size_);
4869 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4870 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4871 // of the address range seen by the linker.
4873 template<bool big_endian>
4875 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
4876 Target_arm<big_endian>* arm_target,
4877 unsigned char* view,
4878 Arm_address view_address,
4879 section_size_type view_size)
4881 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4882 for (Cortex_a8_stub_list::const_iterator p =
4883 this->cortex_a8_stubs_.lower_bound(view_address);
4884 ((p != this->cortex_a8_stubs_.end())
4885 && (p->first < (view_address + view_size)));
4888 // We do not store the THUMB bit in the LSB of either the branch address
4889 // or the stub offset. There is no need to strip the LSB.
4890 Arm_address branch_address = p->first;
4891 const Cortex_a8_stub* stub = p->second;
4892 Arm_address stub_address = this->address() + stub->offset();
4894 // Offset of the branch instruction relative to this view.
4895 section_size_type offset =
4896 convert_to_section_size_type(branch_address - view_address);
4897 gold_assert((offset + 4) <= view_size);
4899 arm_target->apply_cortex_a8_workaround(stub, stub_address,
4900 view + offset, branch_address);
4904 // Arm_input_section methods.
4906 // Initialize an Arm_input_section.
4908 template<bool big_endian>
4910 Arm_input_section<big_endian>::init()
4912 Relobj* relobj = this->relobj();
4913 unsigned int shndx = this->shndx();
4915 // Cache these to speed up size and alignment queries. It is too slow
4916 // to call section_addraglin and section_size every time.
4917 this->original_addralign_ =
4918 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
4919 this->original_size_ =
4920 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
4922 // We want to make this look like the original input section after
4923 // output sections are finalized.
4924 Output_section* os = relobj->output_section(shndx);
4925 off_t offset = relobj->output_section_offset(shndx);
4926 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
4927 this->set_address(os->address() + offset);
4928 this->set_file_offset(os->offset() + offset);
4930 this->set_current_data_size(this->original_size_);
4931 this->finalize_data_size();
4934 template<bool big_endian>
4936 Arm_input_section<big_endian>::do_write(Output_file* of)
4938 // We have to write out the original section content.
4939 section_size_type section_size;
4940 const unsigned char* section_contents =
4941 this->relobj()->section_contents(this->shndx(), §ion_size, false);
4942 of->write(this->offset(), section_contents, section_size);
4944 // If this owns a stub table and it is not empty, write it.
4945 if (this->is_stub_table_owner() && !this->stub_table_->empty())
4946 this->stub_table_->write(of);
4949 // Finalize data size.
4951 template<bool big_endian>
4953 Arm_input_section<big_endian>::set_final_data_size()
4955 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4957 if (this->is_stub_table_owner())
4959 this->stub_table_->finalize_data_size();
4960 off = align_address(off, this->stub_table_->addralign());
4961 off += this->stub_table_->data_size();
4963 this->set_data_size(off);
4966 // Reset address and file offset.
4968 template<bool big_endian>
4970 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
4972 // Size of the original input section contents.
4973 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4975 // If this is a stub table owner, account for the stub table size.
4976 if (this->is_stub_table_owner())
4978 Stub_table<big_endian>* stub_table = this->stub_table_;
4980 // Reset the stub table's address and file offset. The
4981 // current data size for child will be updated after that.
4982 stub_table_->reset_address_and_file_offset();
4983 off = align_address(off, stub_table_->addralign());
4984 off += stub_table->current_data_size();
4987 this->set_current_data_size(off);
4990 // Arm_exidx_cantunwind methods.
4992 // Write this to Output file OF for a fixed endianness.
4994 template<bool big_endian>
4996 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
4998 off_t offset = this->offset();
4999 const section_size_type oview_size = 8;
5000 unsigned char* const oview = of->get_output_view(offset, oview_size);
5002 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5003 Valtype* wv = reinterpret_cast<Valtype*>(oview);
5005 Output_section* os = this->relobj_->output_section(this->shndx_);
5006 gold_assert(os != NULL);
5008 Arm_relobj<big_endian>* arm_relobj =
5009 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
5010 Arm_address output_offset =
5011 arm_relobj->get_output_section_offset(this->shndx_);
5012 Arm_address section_start;
5013 if (output_offset != Arm_relobj<big_endian>::invalid_address)
5014 section_start = os->address() + output_offset;
5017 // Currently this only happens for a relaxed section.
5018 const Output_relaxed_input_section* poris =
5019 os->find_relaxed_input_section(this->relobj_, this->shndx_);
5020 gold_assert(poris != NULL);
5021 section_start = poris->address();
5024 // We always append this to the end of an EXIDX section.
5025 Arm_address output_address =
5026 section_start + this->relobj_->section_size(this->shndx_);
5028 // Write out the entry. The first word either points to the beginning
5029 // or after the end of a text section. The second word is the special
5030 // EXIDX_CANTUNWIND value.
5031 uint32_t prel31_offset = output_address - this->address();
5032 if (utils::has_overflow<31>(offset))
5033 gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
5034 elfcpp::Swap<32, big_endian>::writeval(wv, prel31_offset & 0x7fffffffU);
5035 elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
5037 of->write_output_view(this->offset(), oview_size, oview);
5040 // Arm_exidx_merged_section methods.
5042 // Constructor for Arm_exidx_merged_section.
5043 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
5044 // SECTION_OFFSET_MAP points to a section offset map describing how
5045 // parts of the input section are mapped to output. DELETED_BYTES is
5046 // the number of bytes deleted from the EXIDX input section.
5048 Arm_exidx_merged_section::Arm_exidx_merged_section(
5049 const Arm_exidx_input_section& exidx_input_section,
5050 const Arm_exidx_section_offset_map& section_offset_map,
5051 uint32_t deleted_bytes)
5052 : Output_relaxed_input_section(exidx_input_section.relobj(),
5053 exidx_input_section.shndx(),
5054 exidx_input_section.addralign()),
5055 exidx_input_section_(exidx_input_section),
5056 section_offset_map_(section_offset_map)
5058 // Fix size here so that we do not need to implement set_final_data_size.
5059 this->set_data_size(exidx_input_section.size() - deleted_bytes);
5060 this->fix_data_size();
5063 // Given an input OBJECT, an input section index SHNDX within that
5064 // object, and an OFFSET relative to the start of that input
5065 // section, return whether or not the corresponding offset within
5066 // the output section is known. If this function returns true, it
5067 // sets *POUTPUT to the output offset. The value -1 indicates that
5068 // this input offset is being discarded.
5071 Arm_exidx_merged_section::do_output_offset(
5072 const Relobj* relobj,
5074 section_offset_type offset,
5075 section_offset_type* poutput) const
5077 // We only handle offsets for the original EXIDX input section.
5078 if (relobj != this->exidx_input_section_.relobj()
5079 || shndx != this->exidx_input_section_.shndx())
5082 section_offset_type section_size =
5083 convert_types<section_offset_type>(this->exidx_input_section_.size());
5084 if (offset < 0 || offset >= section_size)
5085 // Input offset is out of valid range.
5089 // We need to look up the section offset map to determine the output
5090 // offset. Find the reference point in map that is first offset
5091 // bigger than or equal to this offset.
5092 Arm_exidx_section_offset_map::const_iterator p =
5093 this->section_offset_map_.lower_bound(offset);
5095 // The section offset maps are build such that this should not happen if
5096 // input offset is in the valid range.
5097 gold_assert(p != this->section_offset_map_.end());
5099 // We need to check if this is dropped.
5100 section_offset_type ref = p->first;
5101 section_offset_type mapped_ref = p->second;
5103 if (mapped_ref != Arm_exidx_input_section::invalid_offset)
5104 // Offset is present in output.
5105 *poutput = mapped_ref + (offset - ref);
5107 // Offset is discarded owing to EXIDX entry merging.
5114 // Write this to output file OF.
5117 Arm_exidx_merged_section::do_write(Output_file* of)
5119 // If we retain or discard the whole EXIDX input section, we would
5121 gold_assert(this->data_size() != this->exidx_input_section_.size()
5122 && this->data_size() != 0);
5124 off_t offset = this->offset();
5125 const section_size_type oview_size = this->data_size();
5126 unsigned char* const oview = of->get_output_view(offset, oview_size);
5128 Output_section* os = this->relobj()->output_section(this->shndx());
5129 gold_assert(os != NULL);
5131 // Get contents of EXIDX input section.
5132 section_size_type section_size;
5133 const unsigned char* section_contents =
5134 this->relobj()->section_contents(this->shndx(), §ion_size, false);
5135 gold_assert(section_size == this->exidx_input_section_.size());
5137 // Go over spans of input offsets and write only those that are not
5139 section_offset_type in_start = 0;
5140 section_offset_type out_start = 0;
5141 for(Arm_exidx_section_offset_map::const_iterator p =
5142 this->section_offset_map_.begin();
5143 p != this->section_offset_map_.end();
5146 section_offset_type in_end = p->first;
5147 gold_assert(in_end >= in_start);
5148 section_offset_type out_end = p->second;
5149 size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
5152 size_t out_chunk_size =
5153 convert_types<size_t>(out_end - out_start + 1);
5154 gold_assert(out_chunk_size == in_chunk_size);
5155 memcpy(oview + out_start, section_contents + in_start,
5157 out_start += out_chunk_size;
5159 in_start += in_chunk_size;
5162 gold_assert(convert_to_section_size_type(out_start) == oview_size);
5163 of->write_output_view(this->offset(), oview_size, oview);
5166 // Arm_exidx_fixup methods.
5168 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
5169 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
5170 // points to the end of the last seen EXIDX section.
5173 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
5175 if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
5176 && this->last_input_section_ != NULL)
5178 Relobj* relobj = this->last_input_section_->relobj();
5179 unsigned int text_shndx = this->last_input_section_->link();
5180 Arm_exidx_cantunwind* cantunwind =
5181 new Arm_exidx_cantunwind(relobj, text_shndx);
5182 this->exidx_output_section_->add_output_section_data(cantunwind);
5183 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5187 // Process an EXIDX section entry in input. Return whether this entry
5188 // can be deleted in the output. SECOND_WORD in the second word of the
5192 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
5195 if (second_word == elfcpp::EXIDX_CANTUNWIND)
5197 // Merge if previous entry is also an EXIDX_CANTUNWIND.
5198 delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
5199 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5201 else if ((second_word & 0x80000000) != 0)
5203 // Inlined unwinding data. Merge if equal to previous.
5204 delete_entry = (merge_exidx_entries_
5205 && this->last_unwind_type_ == UT_INLINED_ENTRY
5206 && this->last_inlined_entry_ == second_word);
5207 this->last_unwind_type_ = UT_INLINED_ENTRY;
5208 this->last_inlined_entry_ = second_word;
5212 // Normal table entry. In theory we could merge these too,
5213 // but duplicate entries are likely to be much less common.
5214 delete_entry = false;
5215 this->last_unwind_type_ = UT_NORMAL_ENTRY;
5217 return delete_entry;
5220 // Update the current section offset map during EXIDX section fix-up.
5221 // If there is no map, create one. INPUT_OFFSET is the offset of a
5222 // reference point, DELETED_BYTES is the number of deleted by in the
5223 // section so far. If DELETE_ENTRY is true, the reference point and
5224 // all offsets after the previous reference point are discarded.
5227 Arm_exidx_fixup::update_offset_map(
5228 section_offset_type input_offset,
5229 section_size_type deleted_bytes,
5232 if (this->section_offset_map_ == NULL)
5233 this->section_offset_map_ = new Arm_exidx_section_offset_map();
5234 section_offset_type output_offset;
5236 output_offset = Arm_exidx_input_section::invalid_offset;
5238 output_offset = input_offset - deleted_bytes;
5239 (*this->section_offset_map_)[input_offset] = output_offset;
5242 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
5243 // bytes deleted. If some entries are merged, also store a pointer to a newly
5244 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
5245 // caller owns the map and is responsible for releasing it after use.
5247 template<bool big_endian>
5249 Arm_exidx_fixup::process_exidx_section(
5250 const Arm_exidx_input_section* exidx_input_section,
5251 Arm_exidx_section_offset_map** psection_offset_map)
5253 Relobj* relobj = exidx_input_section->relobj();
5254 unsigned shndx = exidx_input_section->shndx();
5255 section_size_type section_size;
5256 const unsigned char* section_contents =
5257 relobj->section_contents(shndx, §ion_size, false);
5259 if ((section_size % 8) != 0)
5261 // Something is wrong with this section. Better not touch it.
5262 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5263 relobj->name().c_str(), shndx);
5264 this->last_input_section_ = exidx_input_section;
5265 this->last_unwind_type_ = UT_NONE;
5269 uint32_t deleted_bytes = 0;
5270 bool prev_delete_entry = false;
5271 gold_assert(this->section_offset_map_ == NULL);
5273 for (section_size_type i = 0; i < section_size; i += 8)
5275 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5277 reinterpret_cast<const Valtype*>(section_contents + i + 4);
5278 uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
5280 bool delete_entry = this->process_exidx_entry(second_word);
5282 // Entry deletion causes changes in output offsets. We use a std::map
5283 // to record these. And entry (x, y) means input offset x
5284 // is mapped to output offset y. If y is invalid_offset, then x is
5285 // dropped in the output. Because of the way std::map::lower_bound
5286 // works, we record the last offset in a region w.r.t to keeping or
5287 // dropping. If there is no entry (x0, y0) for an input offset x0,
5288 // the output offset y0 of it is determined by the output offset y1 of
5289 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5290 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
5292 if (delete_entry != prev_delete_entry && i != 0)
5293 this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
5295 // Update total deleted bytes for this entry.
5299 prev_delete_entry = delete_entry;
5302 // If section offset map is not NULL, make an entry for the end of
5304 if (this->section_offset_map_ != NULL)
5305 update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
5307 *psection_offset_map = this->section_offset_map_;
5308 this->section_offset_map_ = NULL;
5309 this->last_input_section_ = exidx_input_section;
5311 // Set the first output text section so that we can link the EXIDX output
5312 // section to it. Ignore any EXIDX input section that is completely merged.
5313 if (this->first_output_text_section_ == NULL
5314 && deleted_bytes != section_size)
5316 unsigned int link = exidx_input_section->link();
5317 Output_section* os = relobj->output_section(link);
5318 gold_assert(os != NULL);
5319 this->first_output_text_section_ = os;
5322 return deleted_bytes;
5325 // Arm_output_section methods.
5327 // Create a stub group for input sections from BEGIN to END. OWNER
5328 // points to the input section to be the owner a new stub table.
5330 template<bool big_endian>
5332 Arm_output_section<big_endian>::create_stub_group(
5333 Input_section_list::const_iterator begin,
5334 Input_section_list::const_iterator end,
5335 Input_section_list::const_iterator owner,
5336 Target_arm<big_endian>* target,
5337 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
5339 // We use a different kind of relaxed section in an EXIDX section.
5340 // The static casting from Output_relaxed_input_section to
5341 // Arm_input_section is invalid in an EXIDX section. We are okay
5342 // because we should not be calling this for an EXIDX section.
5343 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
5345 // Currently we convert ordinary input sections into relaxed sections only
5346 // at this point but we may want to support creating relaxed input section
5347 // very early. So we check here to see if owner is already a relaxed
5350 Arm_input_section<big_endian>* arm_input_section;
5351 if (owner->is_relaxed_input_section())
5354 Arm_input_section<big_endian>::as_arm_input_section(
5355 owner->relaxed_input_section());
5359 gold_assert(owner->is_input_section());
5360 // Create a new relaxed input section.
5362 target->new_arm_input_section(owner->relobj(), owner->shndx());
5363 new_relaxed_sections->push_back(arm_input_section);
5366 // Create a stub table.
5367 Stub_table<big_endian>* stub_table =
5368 target->new_stub_table(arm_input_section);
5370 arm_input_section->set_stub_table(stub_table);
5372 Input_section_list::const_iterator p = begin;
5373 Input_section_list::const_iterator prev_p;
5375 // Look for input sections or relaxed input sections in [begin ... end].
5378 if (p->is_input_section() || p->is_relaxed_input_section())
5380 // The stub table information for input sections live
5381 // in their objects.
5382 Arm_relobj<big_endian>* arm_relobj =
5383 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5384 arm_relobj->set_stub_table(p->shndx(), stub_table);
5388 while (prev_p != end);
5391 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
5392 // of stub groups. We grow a stub group by adding input section until the
5393 // size is just below GROUP_SIZE. The last input section will be converted
5394 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5395 // input section after the stub table, effectively double the group size.
5397 // This is similar to the group_sections() function in elf32-arm.c but is
5398 // implemented differently.
5400 template<bool big_endian>
5402 Arm_output_section<big_endian>::group_sections(
5403 section_size_type group_size,
5404 bool stubs_always_after_branch,
5405 Target_arm<big_endian>* target)
5407 // We only care about sections containing code.
5408 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5411 // States for grouping.
5414 // No group is being built.
5416 // A group is being built but the stub table is not found yet.
5417 // We keep group a stub group until the size is just under GROUP_SIZE.
5418 // The last input section in the group will be used as the stub table.
5419 FINDING_STUB_SECTION,
5420 // A group is being built and we have already found a stub table.
5421 // We enter this state to grow a stub group by adding input section
5422 // after the stub table. This effectively doubles the group size.
5426 // Any newly created relaxed sections are stored here.
5427 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
5429 State state = NO_GROUP;
5430 section_size_type off = 0;
5431 section_size_type group_begin_offset = 0;
5432 section_size_type group_end_offset = 0;
5433 section_size_type stub_table_end_offset = 0;
5434 Input_section_list::const_iterator group_begin =
5435 this->input_sections().end();
5436 Input_section_list::const_iterator stub_table =
5437 this->input_sections().end();
5438 Input_section_list::const_iterator group_end = this->input_sections().end();
5439 for (Input_section_list::const_iterator p = this->input_sections().begin();
5440 p != this->input_sections().end();
5443 section_size_type section_begin_offset =
5444 align_address(off, p->addralign());
5445 section_size_type section_end_offset =
5446 section_begin_offset + p->data_size();
5448 // Check to see if we should group the previously seens sections.
5454 case FINDING_STUB_SECTION:
5455 // Adding this section makes the group larger than GROUP_SIZE.
5456 if (section_end_offset - group_begin_offset >= group_size)
5458 if (stubs_always_after_branch)
5460 gold_assert(group_end != this->input_sections().end());
5461 this->create_stub_group(group_begin, group_end, group_end,
5462 target, &new_relaxed_sections);
5467 // But wait, there's more! Input sections up to
5468 // stub_group_size bytes after the stub table can be
5469 // handled by it too.
5470 state = HAS_STUB_SECTION;
5471 stub_table = group_end;
5472 stub_table_end_offset = group_end_offset;
5477 case HAS_STUB_SECTION:
5478 // Adding this section makes the post stub-section group larger
5480 if (section_end_offset - stub_table_end_offset >= group_size)
5482 gold_assert(group_end != this->input_sections().end());
5483 this->create_stub_group(group_begin, group_end, stub_table,
5484 target, &new_relaxed_sections);
5493 // If we see an input section and currently there is no group, start
5494 // a new one. Skip any empty sections.
5495 if ((p->is_input_section() || p->is_relaxed_input_section())
5496 && (p->relobj()->section_size(p->shndx()) != 0))
5498 if (state == NO_GROUP)
5500 state = FINDING_STUB_SECTION;
5502 group_begin_offset = section_begin_offset;
5505 // Keep track of the last input section seen.
5507 group_end_offset = section_end_offset;
5510 off = section_end_offset;
5513 // Create a stub group for any ungrouped sections.
5514 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5516 gold_assert(group_end != this->input_sections().end());
5517 this->create_stub_group(group_begin, group_end,
5518 (state == FINDING_STUB_SECTION
5521 target, &new_relaxed_sections);
5524 // Convert input section into relaxed input section in a batch.
5525 if (!new_relaxed_sections.empty())
5526 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5528 // Update the section offsets
5529 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5531 Arm_relobj<big_endian>* arm_relobj =
5532 Arm_relobj<big_endian>::as_arm_relobj(
5533 new_relaxed_sections[i]->relobj());
5534 unsigned int shndx = new_relaxed_sections[i]->shndx();
5535 // Tell Arm_relobj that this input section is converted.
5536 arm_relobj->convert_input_section_to_relaxed_section(shndx);
5540 // Append non empty text sections in this to LIST in ascending
5541 // order of their position in this.
5543 template<bool big_endian>
5545 Arm_output_section<big_endian>::append_text_sections_to_list(
5546 Text_section_list* list)
5548 // We only care about text sections.
5549 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5552 gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
5554 for (Input_section_list::const_iterator p = this->input_sections().begin();
5555 p != this->input_sections().end();
5558 // We only care about plain or relaxed input sections. We also
5559 // ignore any merged sections.
5560 if ((p->is_input_section() || p->is_relaxed_input_section())
5561 && p->data_size() != 0)
5562 list->push_back(Text_section_list::value_type(p->relobj(),
5567 template<bool big_endian>
5569 Arm_output_section<big_endian>::fix_exidx_coverage(
5571 const Text_section_list& sorted_text_sections,
5572 Symbol_table* symtab,
5573 bool merge_exidx_entries)
5575 // We should only do this for the EXIDX output section.
5576 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5578 // We don't want the relaxation loop to undo these changes, so we discard
5579 // the current saved states and take another one after the fix-up.
5580 this->discard_states();
5582 // Remove all input sections.
5583 uint64_t address = this->address();
5584 typedef std::list<Output_section::Input_section> Input_section_list;
5585 Input_section_list input_sections;
5586 this->reset_address_and_file_offset();
5587 this->get_input_sections(address, std::string(""), &input_sections);
5589 if (!this->input_sections().empty())
5590 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5592 // Go through all the known input sections and record them.
5593 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5594 typedef Unordered_map<Section_id, const Output_section::Input_section*,
5595 Section_id_hash> Text_to_exidx_map;
5596 Text_to_exidx_map text_to_exidx_map;
5597 for (Input_section_list::const_iterator p = input_sections.begin();
5598 p != input_sections.end();
5601 // This should never happen. At this point, we should only see
5602 // plain EXIDX input sections.
5603 gold_assert(!p->is_relaxed_input_section());
5604 text_to_exidx_map[Section_id(p->relobj(), p->shndx())] = &(*p);
5607 Arm_exidx_fixup exidx_fixup(this, merge_exidx_entries);
5609 // Go over the sorted text sections.
5610 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5611 Section_id_set processed_input_sections;
5612 for (Text_section_list::const_iterator p = sorted_text_sections.begin();
5613 p != sorted_text_sections.end();
5616 Relobj* relobj = p->first;
5617 unsigned int shndx = p->second;
5619 Arm_relobj<big_endian>* arm_relobj =
5620 Arm_relobj<big_endian>::as_arm_relobj(relobj);
5621 const Arm_exidx_input_section* exidx_input_section =
5622 arm_relobj->exidx_input_section_by_link(shndx);
5624 // If this text section has no EXIDX section, force an EXIDX_CANTUNWIND
5625 // entry pointing to the end of the last seen EXIDX section.
5626 if (exidx_input_section == NULL)
5628 exidx_fixup.add_exidx_cantunwind_as_needed();
5632 Relobj* exidx_relobj = exidx_input_section->relobj();
5633 unsigned int exidx_shndx = exidx_input_section->shndx();
5634 Section_id sid(exidx_relobj, exidx_shndx);
5635 Text_to_exidx_map::const_iterator iter = text_to_exidx_map.find(sid);
5636 if (iter == text_to_exidx_map.end())
5638 // This is odd. We have not seen this EXIDX input section before.
5639 // We cannot do fix-up. If we saw a SECTIONS clause in a script,
5640 // issue a warning instead. We assume the user knows what he
5641 // or she is doing. Otherwise, this is an error.
5642 if (layout->script_options()->saw_sections_clause())
5643 gold_warning(_("unwinding may not work because EXIDX input section"
5644 " %u of %s is not in EXIDX output section"),
5645 exidx_shndx, exidx_relobj->name().c_str());
5647 gold_error(_("unwinding may not work because EXIDX input section"
5648 " %u of %s is not in EXIDX output section"),
5649 exidx_shndx, exidx_relobj->name().c_str());
5651 exidx_fixup.add_exidx_cantunwind_as_needed();
5655 // Fix up coverage and append input section to output data list.
5656 Arm_exidx_section_offset_map* section_offset_map = NULL;
5657 uint32_t deleted_bytes =
5658 exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
5659 §ion_offset_map);
5661 if (deleted_bytes == exidx_input_section->size())
5663 // The whole EXIDX section got merged. Remove it from output.
5664 gold_assert(section_offset_map == NULL);
5665 exidx_relobj->set_output_section(exidx_shndx, NULL);
5667 // All local symbols defined in this input section will be dropped.
5668 // We need to adjust output local symbol count.
5669 arm_relobj->set_output_local_symbol_count_needs_update();
5671 else if (deleted_bytes > 0)
5673 // Some entries are merged. We need to convert this EXIDX input
5674 // section into a relaxed section.
5675 gold_assert(section_offset_map != NULL);
5676 Arm_exidx_merged_section* merged_section =
5677 new Arm_exidx_merged_section(*exidx_input_section,
5678 *section_offset_map, deleted_bytes);
5679 this->add_relaxed_input_section(merged_section);
5680 arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
5682 // All local symbols defined in discarded portions of this input
5683 // section will be dropped. We need to adjust output local symbol
5685 arm_relobj->set_output_local_symbol_count_needs_update();
5689 // Just add back the EXIDX input section.
5690 gold_assert(section_offset_map == NULL);
5691 const Output_section::Input_section* pis = iter->second;
5692 gold_assert(pis->is_input_section());
5693 this->add_script_input_section(*pis);
5696 processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx));
5699 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5700 exidx_fixup.add_exidx_cantunwind_as_needed();
5702 // Remove any known EXIDX input sections that are not processed.
5703 for (Input_section_list::const_iterator p = input_sections.begin();
5704 p != input_sections.end();
5707 if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
5708 == processed_input_sections.end())
5710 // We only discard a known EXIDX section because its linked
5711 // text section has been folded by ICF.
5712 Arm_relobj<big_endian>* arm_relobj =
5713 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5714 const Arm_exidx_input_section* exidx_input_section =
5715 arm_relobj->exidx_input_section_by_shndx(p->shndx());
5716 gold_assert(exidx_input_section != NULL);
5717 unsigned int text_shndx = exidx_input_section->link();
5718 gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
5720 // Remove this from link. We also need to recount the
5722 p->relobj()->set_output_section(p->shndx(), NULL);
5723 arm_relobj->set_output_local_symbol_count_needs_update();
5727 // Link exidx output section to the first seen output section and
5728 // set correct entry size.
5729 this->set_link_section(exidx_fixup.first_output_text_section());
5730 this->set_entsize(8);
5732 // Make changes permanent.
5733 this->save_states();
5734 this->set_section_offsets_need_adjustment();
5737 // Arm_relobj methods.
5739 // Determine if an input section is scannable for stub processing. SHDR is
5740 // the header of the section and SHNDX is the section index. OS is the output
5741 // section for the input section and SYMTAB is the global symbol table used to
5742 // look up ICF information.
5744 template<bool big_endian>
5746 Arm_relobj<big_endian>::section_is_scannable(
5747 const elfcpp::Shdr<32, big_endian>& shdr,
5749 const Output_section* os,
5750 const Symbol_table *symtab)
5752 // Skip any empty sections, unallocated sections or sections whose
5753 // type are not SHT_PROGBITS.
5754 if (shdr.get_sh_size() == 0
5755 || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
5756 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
5759 // Skip any discarded or ICF'ed sections.
5760 if (os == NULL || symtab->is_section_folded(this, shndx))
5763 // If this requires special offset handling, check to see if it is
5764 // a relaxed section. If this is not, then it is a merged section that
5765 // we cannot handle.
5766 if (this->is_output_section_offset_invalid(shndx))
5768 const Output_relaxed_input_section* poris =
5769 os->find_relaxed_input_section(this, shndx);
5777 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5778 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5780 template<bool big_endian>
5782 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
5783 const elfcpp::Shdr<32, big_endian>& shdr,
5784 const Relobj::Output_sections& out_sections,
5785 const Symbol_table *symtab,
5786 const unsigned char* pshdrs)
5788 unsigned int sh_type = shdr.get_sh_type();
5789 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
5792 // Ignore empty section.
5793 off_t sh_size = shdr.get_sh_size();
5797 // Ignore reloc section with unexpected symbol table. The
5798 // error will be reported in the final link.
5799 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
5802 unsigned int reloc_size;
5803 if (sh_type == elfcpp::SHT_REL)
5804 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5806 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5808 // Ignore reloc section with unexpected entsize or uneven size.
5809 // The error will be reported in the final link.
5810 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
5813 // Ignore reloc section with bad info. This error will be
5814 // reported in the final link.
5815 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5816 if (index >= this->shnum())
5819 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5820 const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
5821 return this->section_is_scannable(text_shdr, index,
5822 out_sections[index], symtab);
5825 // Return the output address of either a plain input section or a relaxed
5826 // input section. SHNDX is the section index. We define and use this
5827 // instead of calling Output_section::output_address because that is slow
5828 // for large output.
5830 template<bool big_endian>
5832 Arm_relobj<big_endian>::simple_input_section_output_address(
5836 if (this->is_output_section_offset_invalid(shndx))
5838 const Output_relaxed_input_section* poris =
5839 os->find_relaxed_input_section(this, shndx);
5840 // We do not handle merged sections here.
5841 gold_assert(poris != NULL);
5842 return poris->address();
5845 return os->address() + this->get_output_section_offset(shndx);
5848 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
5849 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5851 template<bool big_endian>
5853 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
5854 const elfcpp::Shdr<32, big_endian>& shdr,
5857 const Symbol_table* symtab)
5859 if (!this->section_is_scannable(shdr, shndx, os, symtab))
5862 // If the section does not cross any 4K-boundaries, it does not need to
5864 Arm_address address = this->simple_input_section_output_address(shndx, os);
5865 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
5871 // Scan a section for Cortex-A8 workaround.
5873 template<bool big_endian>
5875 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
5876 const elfcpp::Shdr<32, big_endian>& shdr,
5879 Target_arm<big_endian>* arm_target)
5881 // Look for the first mapping symbol in this section. It should be
5883 Mapping_symbol_position section_start(shndx, 0);
5884 typename Mapping_symbols_info::const_iterator p =
5885 this->mapping_symbols_info_.lower_bound(section_start);
5887 // There are no mapping symbols for this section. Treat it as a data-only
5888 // section. Issue a warning if section is marked as containing
5890 if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
5892 if ((this->section_flags(shndx) & elfcpp::SHF_EXECINSTR) != 0)
5893 gold_warning(_("cannot scan executable section %u of %s for Cortex-A8 "
5894 "erratum because it has no mapping symbols."),
5895 shndx, this->name().c_str());
5899 Arm_address output_address =
5900 this->simple_input_section_output_address(shndx, os);
5902 // Get the section contents.
5903 section_size_type input_view_size = 0;
5904 const unsigned char* input_view =
5905 this->section_contents(shndx, &input_view_size, false);
5907 // We need to go through the mapping symbols to determine what to
5908 // scan. There are two reasons. First, we should look at THUMB code and
5909 // THUMB code only. Second, we only want to look at the 4K-page boundary
5910 // to speed up the scanning.
5912 while (p != this->mapping_symbols_info_.end()
5913 && p->first.first == shndx)
5915 typename Mapping_symbols_info::const_iterator next =
5916 this->mapping_symbols_info_.upper_bound(p->first);
5918 // Only scan part of a section with THUMB code.
5919 if (p->second == 't')
5921 // Determine the end of this range.
5922 section_size_type span_start =
5923 convert_to_section_size_type(p->first.second);
5924 section_size_type span_end;
5925 if (next != this->mapping_symbols_info_.end()
5926 && next->first.first == shndx)
5927 span_end = convert_to_section_size_type(next->first.second);
5929 span_end = convert_to_section_size_type(shdr.get_sh_size());
5931 if (((span_start + output_address) & ~0xfffUL)
5932 != ((span_end + output_address - 1) & ~0xfffUL))
5934 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
5935 span_start, span_end,
5945 // Scan relocations for stub generation.
5947 template<bool big_endian>
5949 Arm_relobj<big_endian>::scan_sections_for_stubs(
5950 Target_arm<big_endian>* arm_target,
5951 const Symbol_table* symtab,
5952 const Layout* layout)
5954 unsigned int shnum = this->shnum();
5955 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5957 // Read the section headers.
5958 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
5962 // To speed up processing, we set up hash tables for fast lookup of
5963 // input offsets to output addresses.
5964 this->initialize_input_to_output_maps();
5966 const Relobj::Output_sections& out_sections(this->output_sections());
5968 Relocate_info<32, big_endian> relinfo;
5969 relinfo.symtab = symtab;
5970 relinfo.layout = layout;
5971 relinfo.object = this;
5973 // Do relocation stubs scanning.
5974 const unsigned char* p = pshdrs + shdr_size;
5975 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5977 const elfcpp::Shdr<32, big_endian> shdr(p);
5978 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
5981 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5982 Arm_address output_offset = this->get_output_section_offset(index);
5983 Arm_address output_address;
5984 if (output_offset != invalid_address)
5985 output_address = out_sections[index]->address() + output_offset;
5988 // Currently this only happens for a relaxed section.
5989 const Output_relaxed_input_section* poris =
5990 out_sections[index]->find_relaxed_input_section(this, index);
5991 gold_assert(poris != NULL);
5992 output_address = poris->address();
5995 // Get the relocations.
5996 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
6000 // Get the section contents. This does work for the case in which
6001 // we modify the contents of an input section. We need to pass the
6002 // output view under such circumstances.
6003 section_size_type input_view_size = 0;
6004 const unsigned char* input_view =
6005 this->section_contents(index, &input_view_size, false);
6007 relinfo.reloc_shndx = i;
6008 relinfo.data_shndx = index;
6009 unsigned int sh_type = shdr.get_sh_type();
6010 unsigned int reloc_size;
6011 if (sh_type == elfcpp::SHT_REL)
6012 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6014 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6016 Output_section* os = out_sections[index];
6017 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
6018 shdr.get_sh_size() / reloc_size,
6020 output_offset == invalid_address,
6021 input_view, output_address,
6026 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
6027 // after its relocation section, if there is one, is processed for
6028 // relocation stubs. Merging this loop with the one above would have been
6029 // complicated since we would have had to make sure that relocation stub
6030 // scanning is done first.
6031 if (arm_target->fix_cortex_a8())
6033 const unsigned char* p = pshdrs + shdr_size;
6034 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
6036 const elfcpp::Shdr<32, big_endian> shdr(p);
6037 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
6040 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
6045 // After we've done the relocations, we release the hash tables,
6046 // since we no longer need them.
6047 this->free_input_to_output_maps();
6050 // Count the local symbols. The ARM backend needs to know if a symbol
6051 // is a THUMB function or not. For global symbols, it is easy because
6052 // the Symbol object keeps the ELF symbol type. For local symbol it is
6053 // harder because we cannot access this information. So we override the
6054 // do_count_local_symbol in parent and scan local symbols to mark
6055 // THUMB functions. This is not the most efficient way but I do not want to
6056 // slow down other ports by calling a per symbol targer hook inside
6057 // Sized_relobj<size, big_endian>::do_count_local_symbols.
6059 template<bool big_endian>
6061 Arm_relobj<big_endian>::do_count_local_symbols(
6062 Stringpool_template<char>* pool,
6063 Stringpool_template<char>* dynpool)
6065 // We need to fix-up the values of any local symbols whose type are
6068 // Ask parent to count the local symbols.
6069 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
6070 const unsigned int loccount = this->local_symbol_count();
6074 // Intialize the thumb function bit-vector.
6075 std::vector<bool> empty_vector(loccount, false);
6076 this->local_symbol_is_thumb_function_.swap(empty_vector);
6078 // Read the symbol table section header.
6079 const unsigned int symtab_shndx = this->symtab_shndx();
6080 elfcpp::Shdr<32, big_endian>
6081 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6082 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6084 // Read the local symbols.
6085 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6086 gold_assert(loccount == symtabshdr.get_sh_info());
6087 off_t locsize = loccount * sym_size;
6088 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6089 locsize, true, true);
6091 // For mapping symbol processing, we need to read the symbol names.
6092 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
6093 if (strtab_shndx >= this->shnum())
6095 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
6099 elfcpp::Shdr<32, big_endian>
6100 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
6101 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
6103 this->error(_("symbol table name section has wrong type: %u"),
6104 static_cast<unsigned int>(strtabshdr.get_sh_type()));
6107 const char* pnames =
6108 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
6109 strtabshdr.get_sh_size(),
6112 // Loop over the local symbols and mark any local symbols pointing
6113 // to THUMB functions.
6115 // Skip the first dummy symbol.
6117 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
6118 this->local_values();
6119 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6121 elfcpp::Sym<32, big_endian> sym(psyms);
6122 elfcpp::STT st_type = sym.get_st_type();
6123 Symbol_value<32>& lv((*plocal_values)[i]);
6124 Arm_address input_value = lv.input_value();
6126 // Check to see if this is a mapping symbol.
6127 const char* sym_name = pnames + sym.get_st_name();
6128 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
6131 unsigned int input_shndx =
6132 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
6133 gold_assert(is_ordinary);
6135 // Strip of LSB in case this is a THUMB symbol.
6136 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
6137 this->mapping_symbols_info_[msp] = sym_name[1];
6140 if (st_type == elfcpp::STT_ARM_TFUNC
6141 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
6143 // This is a THUMB function. Mark this and canonicalize the
6144 // symbol value by setting LSB.
6145 this->local_symbol_is_thumb_function_[i] = true;
6146 if ((input_value & 1) == 0)
6147 lv.set_input_value(input_value | 1);
6152 // Relocate sections.
6153 template<bool big_endian>
6155 Arm_relobj<big_endian>::do_relocate_sections(
6156 const Symbol_table* symtab,
6157 const Layout* layout,
6158 const unsigned char* pshdrs,
6159 typename Sized_relobj<32, big_endian>::Views* pviews)
6161 // Call parent to relocate sections.
6162 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
6165 // We do not generate stubs if doing a relocatable link.
6166 if (parameters->options().relocatable())
6169 // Relocate stub tables.
6170 unsigned int shnum = this->shnum();
6172 Target_arm<big_endian>* arm_target =
6173 Target_arm<big_endian>::default_target();
6175 Relocate_info<32, big_endian> relinfo;
6176 relinfo.symtab = symtab;
6177 relinfo.layout = layout;
6178 relinfo.object = this;
6180 for (unsigned int i = 1; i < shnum; ++i)
6182 Arm_input_section<big_endian>* arm_input_section =
6183 arm_target->find_arm_input_section(this, i);
6185 if (arm_input_section != NULL
6186 && arm_input_section->is_stub_table_owner()
6187 && !arm_input_section->stub_table()->empty())
6189 // We cannot discard a section if it owns a stub table.
6190 Output_section* os = this->output_section(i);
6191 gold_assert(os != NULL);
6193 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
6194 relinfo.reloc_shdr = NULL;
6195 relinfo.data_shndx = i;
6196 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
6198 gold_assert((*pviews)[i].view != NULL);
6200 // We are passed the output section view. Adjust it to cover the
6202 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
6203 gold_assert((stub_table->address() >= (*pviews)[i].address)
6204 && ((stub_table->address() + stub_table->data_size())
6205 <= (*pviews)[i].address + (*pviews)[i].view_size));
6207 off_t offset = stub_table->address() - (*pviews)[i].address;
6208 unsigned char* view = (*pviews)[i].view + offset;
6209 Arm_address address = stub_table->address();
6210 section_size_type view_size = stub_table->data_size();
6212 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
6216 // Apply Cortex A8 workaround if applicable.
6217 if (this->section_has_cortex_a8_workaround(i))
6219 unsigned char* view = (*pviews)[i].view;
6220 Arm_address view_address = (*pviews)[i].address;
6221 section_size_type view_size = (*pviews)[i].view_size;
6222 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
6224 // Adjust view to cover section.
6225 Output_section* os = this->output_section(i);
6226 gold_assert(os != NULL);
6227 Arm_address section_address =
6228 this->simple_input_section_output_address(i, os);
6229 uint64_t section_size = this->section_size(i);
6231 gold_assert(section_address >= view_address
6232 && ((section_address + section_size)
6233 <= (view_address + view_size)));
6235 unsigned char* section_view = view + (section_address - view_address);
6237 // Apply the Cortex-A8 workaround to the output address range
6238 // corresponding to this input section.
6239 stub_table->apply_cortex_a8_workaround_to_address_range(
6248 // Find the linked text section of an EXIDX section by looking the the first
6249 // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6250 // must be linked to to its associated code section via the sh_link field of
6251 // its section header. However, some tools are broken and the link is not
6252 // always set. LD just drops such an EXIDX section silently, causing the
6253 // associated code not unwindabled. Here we try a little bit harder to
6254 // discover the linked code section.
6256 // PSHDR points to the section header of a relocation section of an EXIDX
6257 // section. If we can find a linked text section, return true and
6258 // store the text section index in the location PSHNDX. Otherwise
6261 template<bool big_endian>
6263 Arm_relobj<big_endian>::find_linked_text_section(
6264 const unsigned char* pshdr,
6265 const unsigned char* psyms,
6266 unsigned int* pshndx)
6268 elfcpp::Shdr<32, big_endian> shdr(pshdr);
6270 // If there is no relocation, we cannot find the linked text section.
6272 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6273 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6275 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6276 size_t reloc_count = shdr.get_sh_size() / reloc_size;
6278 // Get the relocations.
6279 const unsigned char* prelocs =
6280 this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false);
6282 // Find the REL31 relocation for the first word of the first EXIDX entry.
6283 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6285 Arm_address r_offset;
6286 typename elfcpp::Elf_types<32>::Elf_WXword r_info;
6287 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6289 typename elfcpp::Rel<32, big_endian> reloc(prelocs);
6290 r_info = reloc.get_r_info();
6291 r_offset = reloc.get_r_offset();
6295 typename elfcpp::Rela<32, big_endian> reloc(prelocs);
6296 r_info = reloc.get_r_info();
6297 r_offset = reloc.get_r_offset();
6300 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6301 if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
6304 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6306 || r_sym >= this->local_symbol_count()
6310 // This is the relocation for the first word of the first EXIDX entry.
6311 // We expect to see a local section symbol.
6312 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6313 elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
6314 if (sym.get_st_type() == elfcpp::STT_SECTION)
6318 this->adjust_sym_shndx(r_sym, sym.get_st_shndx(), &is_ordinary);
6319 gold_assert(is_ordinary);
6329 // Make an EXIDX input section object for an EXIDX section whose index is
6330 // SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
6331 // is the section index of the linked text section.
6333 template<bool big_endian>
6335 Arm_relobj<big_endian>::make_exidx_input_section(
6337 const elfcpp::Shdr<32, big_endian>& shdr,
6338 unsigned int text_shndx)
6340 // Issue an error and ignore this EXIDX section if it points to a text
6341 // section already has an EXIDX section.
6342 if (this->exidx_section_map_[text_shndx] != NULL)
6344 gold_error(_("EXIDX sections %u and %u both link to text section %u "
6346 shndx, this->exidx_section_map_[text_shndx]->shndx(),
6347 text_shndx, this->name().c_str());
6351 // Create an Arm_exidx_input_section object for this EXIDX section.
6352 Arm_exidx_input_section* exidx_input_section =
6353 new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
6354 shdr.get_sh_addralign());
6355 this->exidx_section_map_[text_shndx] = exidx_input_section;
6357 // Also map the EXIDX section index to this.
6358 gold_assert(this->exidx_section_map_[shndx] == NULL);
6359 this->exidx_section_map_[shndx] = exidx_input_section;
6362 // Read the symbol information.
6364 template<bool big_endian>
6366 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6368 // Call parent class to read symbol information.
6369 Sized_relobj<32, big_endian>::do_read_symbols(sd);
6371 // If this input file is a binary file, it has no processor
6372 // specific flags and attributes section.
6373 Input_file::Format format = this->input_file()->format();
6374 if (format != Input_file::FORMAT_ELF)
6376 gold_assert(format == Input_file::FORMAT_BINARY);
6377 this->merge_flags_and_attributes_ = false;
6381 // Read processor-specific flags in ELF file header.
6382 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6383 elfcpp::Elf_sizes<32>::ehdr_size,
6385 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6386 this->processor_specific_flags_ = ehdr.get_e_flags();
6388 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6390 std::vector<unsigned int> deferred_exidx_sections;
6391 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6392 const unsigned char* pshdrs = sd->section_headers->data();
6393 const unsigned char *ps = pshdrs + shdr_size;
6394 bool must_merge_flags_and_attributes = false;
6395 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6397 elfcpp::Shdr<32, big_endian> shdr(ps);
6399 // Sometimes an object has no contents except the section name string
6400 // table and an empty symbol table with the undefined symbol. We
6401 // don't want to merge processor-specific flags from such an object.
6402 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6404 // Symbol table is not empty.
6405 const elfcpp::Elf_types<32>::Elf_WXword sym_size =
6406 elfcpp::Elf_sizes<32>::sym_size;
6407 if (shdr.get_sh_size() > sym_size)
6408 must_merge_flags_and_attributes = true;
6410 else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6411 // If this is neither an empty symbol table nor a string table,
6413 must_merge_flags_and_attributes = true;
6415 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6417 gold_assert(this->attributes_section_data_ == NULL);
6418 section_offset_type section_offset = shdr.get_sh_offset();
6419 section_size_type section_size =
6420 convert_to_section_size_type(shdr.get_sh_size());
6421 File_view* view = this->get_lasting_view(section_offset,
6422 section_size, true, false);
6423 this->attributes_section_data_ =
6424 new Attributes_section_data(view->data(), section_size);
6426 else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6428 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6429 if (text_shndx >= this->shnum())
6430 gold_error(_("EXIDX section %u linked to invalid section %u"),
6432 else if (text_shndx == elfcpp::SHN_UNDEF)
6433 deferred_exidx_sections.push_back(i);
6435 this->make_exidx_input_section(i, shdr, text_shndx);
6440 if (!must_merge_flags_and_attributes)
6442 this->merge_flags_and_attributes_ = false;
6446 // Some tools are broken and they do not set the link of EXIDX sections.
6447 // We look at the first relocation to figure out the linked sections.
6448 if (!deferred_exidx_sections.empty())
6450 // We need to go over the section headers again to find the mapping
6451 // from sections being relocated to their relocation sections. This is
6452 // a bit inefficient as we could do that in the loop above. However,
6453 // we do not expect any deferred EXIDX sections normally. So we do not
6454 // want to slow down the most common path.
6455 typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
6456 Reloc_map reloc_map;
6457 ps = pshdrs + shdr_size;
6458 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6460 elfcpp::Shdr<32, big_endian> shdr(ps);
6461 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
6462 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
6464 unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
6465 if (info_shndx >= this->shnum())
6466 gold_error(_("relocation section %u has invalid info %u"),
6468 Reloc_map::value_type value(info_shndx, i);
6469 std::pair<Reloc_map::iterator, bool> result =
6470 reloc_map.insert(value);
6472 gold_error(_("section %u has multiple relocation sections "
6474 info_shndx, i, reloc_map[info_shndx]);
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 // Process the deferred EXIDX sections.
6493 for(unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
6495 unsigned int shndx = deferred_exidx_sections[i];
6496 elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
6497 unsigned int text_shndx;
6498 Reloc_map::const_iterator it = reloc_map.find(shndx);
6499 if (it != reloc_map.end()
6500 && find_linked_text_section(pshdrs + it->second * shdr_size,
6501 psyms, &text_shndx))
6502 this->make_exidx_input_section(shndx, shdr, text_shndx);
6504 gold_error(_("EXIDX section %u has no linked text section."),
6510 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
6511 // sections for unwinding. These sections are referenced implicitly by
6512 // text sections linked in the section headers. If we ignore these implict
6513 // references, the .ARM.exidx sections and any .ARM.extab sections they use
6514 // will be garbage-collected incorrectly. Hence we override the same function
6515 // in the base class to handle these implicit references.
6517 template<bool big_endian>
6519 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
6521 Read_relocs_data* rd)
6523 // First, call base class method to process relocations in this object.
6524 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
6526 // If --gc-sections is not specified, there is nothing more to do.
6527 // This happens when --icf is used but --gc-sections is not.
6528 if (!parameters->options().gc_sections())
6531 unsigned int shnum = this->shnum();
6532 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6533 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6537 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
6538 // to these from the linked text sections.
6539 const unsigned char* ps = pshdrs + shdr_size;
6540 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
6542 elfcpp::Shdr<32, big_endian> shdr(ps);
6543 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6545 // Found an .ARM.exidx section, add it to the set of reachable
6546 // sections from its linked text section.
6547 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6548 symtab->gc()->add_reference(this, text_shndx, this, i);
6553 // Update output local symbol count. Owing to EXIDX entry merging, some local
6554 // symbols will be removed in output. Adjust output local symbol count
6555 // accordingly. We can only changed the static output local symbol count. It
6556 // is too late to change the dynamic symbols.
6558 template<bool big_endian>
6560 Arm_relobj<big_endian>::update_output_local_symbol_count()
6562 // Caller should check that this needs updating. We want caller checking
6563 // because output_local_symbol_count_needs_update() is most likely inlined.
6564 gold_assert(this->output_local_symbol_count_needs_update_);
6566 gold_assert(this->symtab_shndx() != -1U);
6567 if (this->symtab_shndx() == 0)
6569 // This object has no symbols. Weird but legal.
6573 // Read the symbol table section header.
6574 const unsigned int symtab_shndx = this->symtab_shndx();
6575 elfcpp::Shdr<32, big_endian>
6576 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6577 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6579 // Read the local symbols.
6580 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6581 const unsigned int loccount = this->local_symbol_count();
6582 gold_assert(loccount == symtabshdr.get_sh_info());
6583 off_t locsize = loccount * sym_size;
6584 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6585 locsize, true, true);
6587 // Loop over the local symbols.
6589 typedef typename Sized_relobj<32, big_endian>::Output_sections
6591 const Output_sections& out_sections(this->output_sections());
6592 unsigned int shnum = this->shnum();
6593 unsigned int count = 0;
6594 // Skip the first, dummy, symbol.
6596 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6598 elfcpp::Sym<32, big_endian> sym(psyms);
6600 Symbol_value<32>& lv((*this->local_values())[i]);
6602 // This local symbol was already discarded by do_count_local_symbols.
6603 if (lv.is_output_symtab_index_set() && !lv.has_output_symtab_entry())
6607 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
6612 Output_section* os = out_sections[shndx];
6614 // This local symbol no longer has an output section. Discard it.
6617 lv.set_no_output_symtab_entry();
6621 // Currently we only discard parts of EXIDX input sections.
6622 // We explicitly check for a merged EXIDX input section to avoid
6623 // calling Output_section_data::output_offset unless necessary.
6624 if ((this->get_output_section_offset(shndx) == invalid_address)
6625 && (this->exidx_input_section_by_shndx(shndx) != NULL))
6627 section_offset_type output_offset =
6628 os->output_offset(this, shndx, lv.input_value());
6629 if (output_offset == -1)
6631 // This symbol is defined in a part of an EXIDX input section
6632 // that is discarded due to entry merging.
6633 lv.set_no_output_symtab_entry();
6642 this->set_output_local_symbol_count(count);
6643 this->output_local_symbol_count_needs_update_ = false;
6646 // Arm_dynobj methods.
6648 // Read the symbol information.
6650 template<bool big_endian>
6652 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6654 // Call parent class to read symbol information.
6655 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
6657 // Read processor-specific flags in ELF file header.
6658 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6659 elfcpp::Elf_sizes<32>::ehdr_size,
6661 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6662 this->processor_specific_flags_ = ehdr.get_e_flags();
6664 // Read the attributes section if there is one.
6665 // We read from the end because gas seems to put it near the end of
6666 // the section headers.
6667 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6668 const unsigned char *ps =
6669 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
6670 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
6672 elfcpp::Shdr<32, big_endian> shdr(ps);
6673 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6675 section_offset_type section_offset = shdr.get_sh_offset();
6676 section_size_type section_size =
6677 convert_to_section_size_type(shdr.get_sh_size());
6678 File_view* view = this->get_lasting_view(section_offset,
6679 section_size, true, false);
6680 this->attributes_section_data_ =
6681 new Attributes_section_data(view->data(), section_size);
6687 // Stub_addend_reader methods.
6689 // Read the addend of a REL relocation of type R_TYPE at VIEW.
6691 template<bool big_endian>
6692 elfcpp::Elf_types<32>::Elf_Swxword
6693 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
6694 unsigned int r_type,
6695 const unsigned char* view,
6696 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
6698 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
6702 case elfcpp::R_ARM_CALL:
6703 case elfcpp::R_ARM_JUMP24:
6704 case elfcpp::R_ARM_PLT32:
6706 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6707 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6708 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
6709 return utils::sign_extend<26>(val << 2);
6712 case elfcpp::R_ARM_THM_CALL:
6713 case elfcpp::R_ARM_THM_JUMP24:
6714 case elfcpp::R_ARM_THM_XPC22:
6716 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6717 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6718 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6719 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6720 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
6723 case elfcpp::R_ARM_THM_JUMP19:
6725 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6726 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6727 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6728 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6729 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
6737 // Arm_output_data_got methods.
6739 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
6740 // The first one is initialized to be 1, which is the module index for
6741 // the main executable and the second one 0. A reloc of the type
6742 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
6743 // be applied by gold. GSYM is a global symbol.
6745 template<bool big_endian>
6747 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
6748 unsigned int got_type,
6751 if (gsym->has_got_offset(got_type))
6754 // We are doing a static link. Just mark it as belong to module 1,
6756 unsigned int got_offset = this->add_constant(1);
6757 gsym->set_got_offset(got_type, got_offset);
6758 got_offset = this->add_constant(0);
6759 this->static_relocs_.push_back(Static_reloc(got_offset,
6760 elfcpp::R_ARM_TLS_DTPOFF32,
6764 // Same as the above but for a local symbol.
6766 template<bool big_endian>
6768 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
6769 unsigned int got_type,
6770 Sized_relobj<32, big_endian>* object,
6773 if (object->local_has_got_offset(index, got_type))
6776 // We are doing a static link. Just mark it as belong to module 1,
6778 unsigned int got_offset = this->add_constant(1);
6779 object->set_local_got_offset(index, got_type, got_offset);
6780 got_offset = this->add_constant(0);
6781 this->static_relocs_.push_back(Static_reloc(got_offset,
6782 elfcpp::R_ARM_TLS_DTPOFF32,
6786 template<bool big_endian>
6788 Arm_output_data_got<big_endian>::do_write(Output_file* of)
6790 // Call parent to write out GOT.
6791 Output_data_got<32, big_endian>::do_write(of);
6793 // We are done if there is no fix up.
6794 if (this->static_relocs_.empty())
6797 gold_assert(parameters->doing_static_link());
6799 const off_t offset = this->offset();
6800 const section_size_type oview_size =
6801 convert_to_section_size_type(this->data_size());
6802 unsigned char* const oview = of->get_output_view(offset, oview_size);
6804 Output_segment* tls_segment = this->layout_->tls_segment();
6805 gold_assert(tls_segment != NULL);
6807 // The thread pointer $tp points to the TCB, which is followed by the
6808 // TLS. So we need to adjust $tp relative addressing by this amount.
6809 Arm_address aligned_tcb_size =
6810 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
6812 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6814 Static_reloc& reloc(this->static_relocs_[i]);
6817 if (!reloc.symbol_is_global())
6819 Sized_relobj<32, big_endian>* object = reloc.relobj();
6820 const Symbol_value<32>* psymval =
6821 reloc.relobj()->local_symbol(reloc.index());
6823 // We are doing static linking. Issue an error and skip this
6824 // relocation if the symbol is undefined or in a discarded_section.
6826 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6827 if ((shndx == elfcpp::SHN_UNDEF)
6829 && shndx != elfcpp::SHN_UNDEF
6830 && !object->is_section_included(shndx)
6831 && !this->symbol_table_->is_section_folded(object, shndx)))
6833 gold_error(_("undefined or discarded local symbol %u from "
6834 " object %s in GOT"),
6835 reloc.index(), reloc.relobj()->name().c_str());
6839 value = psymval->value(object, 0);
6843 const Symbol* gsym = reloc.symbol();
6844 gold_assert(gsym != NULL);
6845 if (gsym->is_forwarder())
6846 gsym = this->symbol_table_->resolve_forwards(gsym);
6848 // We are doing static linking. Issue an error and skip this
6849 // relocation if the symbol is undefined or in a discarded_section
6850 // unless it is a weakly_undefined symbol.
6851 if ((gsym->is_defined_in_discarded_section()
6852 || gsym->is_undefined())
6853 && !gsym->is_weak_undefined())
6855 gold_error(_("undefined or discarded symbol %s in GOT"),
6860 if (!gsym->is_weak_undefined())
6862 const Sized_symbol<32>* sym =
6863 static_cast<const Sized_symbol<32>*>(gsym);
6864 value = sym->value();
6870 unsigned got_offset = reloc.got_offset();
6871 gold_assert(got_offset < oview_size);
6873 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6874 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6876 switch (reloc.r_type())
6878 case elfcpp::R_ARM_TLS_DTPOFF32:
6881 case elfcpp::R_ARM_TLS_TPOFF32:
6882 x = value + aligned_tcb_size;
6887 elfcpp::Swap<32, big_endian>::writeval(wv, x);
6890 of->write_output_view(offset, oview_size, oview);
6893 // A class to handle the PLT data.
6895 template<bool big_endian>
6896 class Output_data_plt_arm : public Output_section_data
6899 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
6902 Output_data_plt_arm(Layout*, Output_data_space*);
6904 // Add an entry to the PLT.
6906 add_entry(Symbol* gsym);
6908 // Return the .rel.plt section data.
6909 const Reloc_section*
6911 { return this->rel_; }
6915 do_adjust_output_section(Output_section* os);
6917 // Write to a map file.
6919 do_print_to_mapfile(Mapfile* mapfile) const
6920 { mapfile->print_output_data(this, _("** PLT")); }
6923 // Template for the first PLT entry.
6924 static const uint32_t first_plt_entry[5];
6926 // Template for subsequent PLT entries.
6927 static const uint32_t plt_entry[3];
6929 // Set the final size.
6931 set_final_data_size()
6933 this->set_data_size(sizeof(first_plt_entry)
6934 + this->count_ * sizeof(plt_entry));
6937 // Write out the PLT data.
6939 do_write(Output_file*);
6941 // The reloc section.
6942 Reloc_section* rel_;
6943 // The .got.plt section.
6944 Output_data_space* got_plt_;
6945 // The number of PLT entries.
6946 unsigned int count_;
6949 // Create the PLT section. The ordinary .got section is an argument,
6950 // since we need to refer to the start. We also create our own .got
6951 // section just for PLT entries.
6953 template<bool big_endian>
6954 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
6955 Output_data_space* got_plt)
6956 : Output_section_data(4), got_plt_(got_plt), count_(0)
6958 this->rel_ = new Reloc_section(false);
6959 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
6960 elfcpp::SHF_ALLOC, this->rel_, true, false,
6964 template<bool big_endian>
6966 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
6971 // Add an entry to the PLT.
6973 template<bool big_endian>
6975 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
6977 gold_assert(!gsym->has_plt_offset());
6979 // Note that when setting the PLT offset we skip the initial
6980 // reserved PLT entry.
6981 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
6982 + sizeof(first_plt_entry));
6986 section_offset_type got_offset = this->got_plt_->current_data_size();
6988 // Every PLT entry needs a GOT entry which points back to the PLT
6989 // entry (this will be changed by the dynamic linker, normally
6990 // lazily when the function is called).
6991 this->got_plt_->set_current_data_size(got_offset + 4);
6993 // Every PLT entry needs a reloc.
6994 gsym->set_needs_dynsym_entry();
6995 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
6998 // Note that we don't need to save the symbol. The contents of the
6999 // PLT are independent of which symbols are used. The symbols only
7000 // appear in the relocations.
7004 // FIXME: This is not very flexible. Right now this has only been tested
7005 // on armv5te. If we are to support additional architecture features like
7006 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
7008 // The first entry in the PLT.
7009 template<bool big_endian>
7010 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
7012 0xe52de004, // str lr, [sp, #-4]!
7013 0xe59fe004, // ldr lr, [pc, #4]
7014 0xe08fe00e, // add lr, pc, lr
7015 0xe5bef008, // ldr pc, [lr, #8]!
7016 0x00000000, // &GOT[0] - .
7019 // Subsequent entries in the PLT.
7021 template<bool big_endian>
7022 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
7024 0xe28fc600, // add ip, pc, #0xNN00000
7025 0xe28cca00, // add ip, ip, #0xNN000
7026 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
7029 // Write out the PLT. This uses the hand-coded instructions above,
7030 // and adjusts them as needed. This is all specified by the arm ELF
7031 // Processor Supplement.
7033 template<bool big_endian>
7035 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
7037 const off_t offset = this->offset();
7038 const section_size_type oview_size =
7039 convert_to_section_size_type(this->data_size());
7040 unsigned char* const oview = of->get_output_view(offset, oview_size);
7042 const off_t got_file_offset = this->got_plt_->offset();
7043 const section_size_type got_size =
7044 convert_to_section_size_type(this->got_plt_->data_size());
7045 unsigned char* const got_view = of->get_output_view(got_file_offset,
7047 unsigned char* pov = oview;
7049 Arm_address plt_address = this->address();
7050 Arm_address got_address = this->got_plt_->address();
7052 // Write first PLT entry. All but the last word are constants.
7053 const size_t num_first_plt_words = (sizeof(first_plt_entry)
7054 / sizeof(plt_entry[0]));
7055 for (size_t i = 0; i < num_first_plt_words - 1; i++)
7056 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
7057 // Last word in first PLT entry is &GOT[0] - .
7058 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
7059 got_address - (plt_address + 16));
7060 pov += sizeof(first_plt_entry);
7062 unsigned char* got_pov = got_view;
7064 memset(got_pov, 0, 12);
7067 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
7068 unsigned int plt_offset = sizeof(first_plt_entry);
7069 unsigned int plt_rel_offset = 0;
7070 unsigned int got_offset = 12;
7071 const unsigned int count = this->count_;
7072 for (unsigned int i = 0;
7075 pov += sizeof(plt_entry),
7077 plt_offset += sizeof(plt_entry),
7078 plt_rel_offset += rel_size,
7081 // Set and adjust the PLT entry itself.
7082 int32_t offset = ((got_address + got_offset)
7083 - (plt_address + plt_offset + 8));
7085 gold_assert(offset >= 0 && offset < 0x0fffffff);
7086 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
7087 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
7088 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
7089 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
7090 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
7091 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
7093 // Set the entry in the GOT.
7094 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
7097 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
7098 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
7100 of->write_output_view(offset, oview_size, oview);
7101 of->write_output_view(got_file_offset, got_size, got_view);
7104 // Create a PLT entry for a global symbol.
7106 template<bool big_endian>
7108 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
7111 if (gsym->has_plt_offset())
7114 if (this->plt_ == NULL)
7116 // Create the GOT sections first.
7117 this->got_section(symtab, layout);
7119 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
7120 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
7122 | elfcpp::SHF_EXECINSTR),
7123 this->plt_, false, false, false, false);
7125 this->plt_->add_entry(gsym);
7128 // Get the section to use for TLS_DESC relocations.
7130 template<bool big_endian>
7131 typename Target_arm<big_endian>::Reloc_section*
7132 Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
7134 return this->plt_section()->rel_tls_desc(layout);
7137 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
7139 template<bool big_endian>
7141 Target_arm<big_endian>::define_tls_base_symbol(
7142 Symbol_table* symtab,
7145 if (this->tls_base_symbol_defined_)
7148 Output_segment* tls_segment = layout->tls_segment();
7149 if (tls_segment != NULL)
7151 bool is_exec = parameters->options().output_is_executable();
7152 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
7153 Symbol_table::PREDEFINED,
7157 elfcpp::STV_HIDDEN, 0,
7159 ? Symbol::SEGMENT_END
7160 : Symbol::SEGMENT_START),
7163 this->tls_base_symbol_defined_ = true;
7166 // Create a GOT entry for the TLS module index.
7168 template<bool big_endian>
7170 Target_arm<big_endian>::got_mod_index_entry(
7171 Symbol_table* symtab,
7173 Sized_relobj<32, big_endian>* object)
7175 if (this->got_mod_index_offset_ == -1U)
7177 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7178 Arm_output_data_got<big_endian>* got = this->got_section(symtab, layout);
7179 unsigned int got_offset;
7180 if (!parameters->doing_static_link())
7182 got_offset = got->add_constant(0);
7183 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
7184 rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
7189 // We are doing a static link. Just mark it as belong to module 1,
7191 got_offset = got->add_constant(1);
7194 got->add_constant(0);
7195 this->got_mod_index_offset_ = got_offset;
7197 return this->got_mod_index_offset_;
7200 // Optimize the TLS relocation type based on what we know about the
7201 // symbol. IS_FINAL is true if the final address of this symbol is
7202 // known at link time.
7204 template<bool big_endian>
7205 tls::Tls_optimization
7206 Target_arm<big_endian>::optimize_tls_reloc(bool, int)
7208 // FIXME: Currently we do not do any TLS optimization.
7209 return tls::TLSOPT_NONE;
7212 // Report an unsupported relocation against a local symbol.
7214 template<bool big_endian>
7216 Target_arm<big_endian>::Scan::unsupported_reloc_local(
7217 Sized_relobj<32, big_endian>* object,
7218 unsigned int r_type)
7220 gold_error(_("%s: unsupported reloc %u against local symbol"),
7221 object->name().c_str(), r_type);
7224 // We are about to emit a dynamic relocation of type R_TYPE. If the
7225 // dynamic linker does not support it, issue an error. The GNU linker
7226 // only issues a non-PIC error for an allocated read-only section.
7227 // Here we know the section is allocated, but we don't know that it is
7228 // read-only. But we check for all the relocation types which the
7229 // glibc dynamic linker supports, so it seems appropriate to issue an
7230 // error even if the section is not read-only.
7232 template<bool big_endian>
7234 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
7235 unsigned int r_type)
7239 // These are the relocation types supported by glibc for ARM.
7240 case elfcpp::R_ARM_RELATIVE:
7241 case elfcpp::R_ARM_COPY:
7242 case elfcpp::R_ARM_GLOB_DAT:
7243 case elfcpp::R_ARM_JUMP_SLOT:
7244 case elfcpp::R_ARM_ABS32:
7245 case elfcpp::R_ARM_ABS32_NOI:
7246 case elfcpp::R_ARM_PC24:
7247 // FIXME: The following 3 types are not supported by Android's dynamic
7249 case elfcpp::R_ARM_TLS_DTPMOD32:
7250 case elfcpp::R_ARM_TLS_DTPOFF32:
7251 case elfcpp::R_ARM_TLS_TPOFF32:
7256 // This prevents us from issuing more than one error per reloc
7257 // section. But we can still wind up issuing more than one
7258 // error per object file.
7259 if (this->issued_non_pic_error_)
7261 const Arm_reloc_property* reloc_property =
7262 arm_reloc_property_table->get_reloc_property(r_type);
7263 gold_assert(reloc_property != NULL);
7264 object->error(_("requires unsupported dynamic reloc %s; "
7265 "recompile with -fPIC"),
7266 reloc_property->name().c_str());
7267 this->issued_non_pic_error_ = true;
7271 case elfcpp::R_ARM_NONE:
7276 // Scan a relocation for a local symbol.
7277 // FIXME: This only handles a subset of relocation types used by Android
7278 // on ARM v5te devices.
7280 template<bool big_endian>
7282 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
7285 Sized_relobj<32, big_endian>* object,
7286 unsigned int data_shndx,
7287 Output_section* output_section,
7288 const elfcpp::Rel<32, big_endian>& reloc,
7289 unsigned int r_type,
7290 const elfcpp::Sym<32, big_endian>& lsym)
7292 r_type = get_real_reloc_type(r_type);
7295 case elfcpp::R_ARM_NONE:
7296 case elfcpp::R_ARM_V4BX:
7297 case elfcpp::R_ARM_GNU_VTENTRY:
7298 case elfcpp::R_ARM_GNU_VTINHERIT:
7301 case elfcpp::R_ARM_ABS32:
7302 case elfcpp::R_ARM_ABS32_NOI:
7303 // If building a shared library (or a position-independent
7304 // executable), we need to create a dynamic relocation for
7305 // this location. The relocation applied at link time will
7306 // apply the link-time value, so we flag the location with
7307 // an R_ARM_RELATIVE relocation so the dynamic loader can
7308 // relocate it easily.
7309 if (parameters->options().output_is_position_independent())
7311 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7312 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7313 // If we are to add more other reloc types than R_ARM_ABS32,
7314 // we need to add check_non_pic(object, r_type) here.
7315 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
7316 output_section, data_shndx,
7317 reloc.get_r_offset());
7321 case elfcpp::R_ARM_ABS16:
7322 case elfcpp::R_ARM_ABS12:
7323 case elfcpp::R_ARM_THM_ABS5:
7324 case elfcpp::R_ARM_ABS8:
7325 case elfcpp::R_ARM_BASE_ABS:
7326 case elfcpp::R_ARM_MOVW_ABS_NC:
7327 case elfcpp::R_ARM_MOVT_ABS:
7328 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7329 case elfcpp::R_ARM_THM_MOVT_ABS:
7330 // If building a shared library (or a position-independent
7331 // executable), we need to create a dynamic relocation for
7332 // this location. Because the addend needs to remain in the
7333 // data section, we need to be careful not to apply this
7334 // relocation statically.
7335 if (parameters->options().output_is_position_independent())
7337 check_non_pic(object, r_type);
7338 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7339 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7340 if (lsym.get_st_type() != elfcpp::STT_SECTION)
7341 rel_dyn->add_local(object, r_sym, r_type, output_section,
7342 data_shndx, reloc.get_r_offset());
7345 gold_assert(lsym.get_st_value() == 0);
7346 unsigned int shndx = lsym.get_st_shndx();
7348 shndx = object->adjust_sym_shndx(r_sym, shndx,
7351 object->error(_("section symbol %u has bad shndx %u"),
7354 rel_dyn->add_local_section(object, shndx,
7355 r_type, output_section,
7356 data_shndx, reloc.get_r_offset());
7361 case elfcpp::R_ARM_PC24:
7362 case elfcpp::R_ARM_REL32:
7363 case elfcpp::R_ARM_LDR_PC_G0:
7364 case elfcpp::R_ARM_SBREL32:
7365 case elfcpp::R_ARM_THM_CALL:
7366 case elfcpp::R_ARM_THM_PC8:
7367 case elfcpp::R_ARM_BASE_PREL:
7368 case elfcpp::R_ARM_PLT32:
7369 case elfcpp::R_ARM_CALL:
7370 case elfcpp::R_ARM_JUMP24:
7371 case elfcpp::R_ARM_THM_JUMP24:
7372 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7373 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7374 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7375 case elfcpp::R_ARM_SBREL31:
7376 case elfcpp::R_ARM_PREL31:
7377 case elfcpp::R_ARM_MOVW_PREL_NC:
7378 case elfcpp::R_ARM_MOVT_PREL:
7379 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7380 case elfcpp::R_ARM_THM_MOVT_PREL:
7381 case elfcpp::R_ARM_THM_JUMP19:
7382 case elfcpp::R_ARM_THM_JUMP6:
7383 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7384 case elfcpp::R_ARM_THM_PC12:
7385 case elfcpp::R_ARM_REL32_NOI:
7386 case elfcpp::R_ARM_ALU_PC_G0_NC:
7387 case elfcpp::R_ARM_ALU_PC_G0:
7388 case elfcpp::R_ARM_ALU_PC_G1_NC:
7389 case elfcpp::R_ARM_ALU_PC_G1:
7390 case elfcpp::R_ARM_ALU_PC_G2:
7391 case elfcpp::R_ARM_LDR_PC_G1:
7392 case elfcpp::R_ARM_LDR_PC_G2:
7393 case elfcpp::R_ARM_LDRS_PC_G0:
7394 case elfcpp::R_ARM_LDRS_PC_G1:
7395 case elfcpp::R_ARM_LDRS_PC_G2:
7396 case elfcpp::R_ARM_LDC_PC_G0:
7397 case elfcpp::R_ARM_LDC_PC_G1:
7398 case elfcpp::R_ARM_LDC_PC_G2:
7399 case elfcpp::R_ARM_ALU_SB_G0_NC:
7400 case elfcpp::R_ARM_ALU_SB_G0:
7401 case elfcpp::R_ARM_ALU_SB_G1_NC:
7402 case elfcpp::R_ARM_ALU_SB_G1:
7403 case elfcpp::R_ARM_ALU_SB_G2:
7404 case elfcpp::R_ARM_LDR_SB_G0:
7405 case elfcpp::R_ARM_LDR_SB_G1:
7406 case elfcpp::R_ARM_LDR_SB_G2:
7407 case elfcpp::R_ARM_LDRS_SB_G0:
7408 case elfcpp::R_ARM_LDRS_SB_G1:
7409 case elfcpp::R_ARM_LDRS_SB_G2:
7410 case elfcpp::R_ARM_LDC_SB_G0:
7411 case elfcpp::R_ARM_LDC_SB_G1:
7412 case elfcpp::R_ARM_LDC_SB_G2:
7413 case elfcpp::R_ARM_MOVW_BREL_NC:
7414 case elfcpp::R_ARM_MOVT_BREL:
7415 case elfcpp::R_ARM_MOVW_BREL:
7416 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7417 case elfcpp::R_ARM_THM_MOVT_BREL:
7418 case elfcpp::R_ARM_THM_MOVW_BREL:
7419 case elfcpp::R_ARM_THM_JUMP11:
7420 case elfcpp::R_ARM_THM_JUMP8:
7421 // We don't need to do anything for a relative addressing relocation
7422 // against a local symbol if it does not reference the GOT.
7425 case elfcpp::R_ARM_GOTOFF32:
7426 case elfcpp::R_ARM_GOTOFF12:
7427 // We need a GOT section:
7428 target->got_section(symtab, layout);
7431 case elfcpp::R_ARM_GOT_BREL:
7432 case elfcpp::R_ARM_GOT_PREL:
7434 // The symbol requires a GOT entry.
7435 Arm_output_data_got<big_endian>* got =
7436 target->got_section(symtab, layout);
7437 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7438 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
7440 // If we are generating a shared object, we need to add a
7441 // dynamic RELATIVE relocation for this symbol's GOT entry.
7442 if (parameters->options().output_is_position_independent())
7444 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7445 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7446 rel_dyn->add_local_relative(
7447 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
7448 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
7454 case elfcpp::R_ARM_TARGET1:
7455 case elfcpp::R_ARM_TARGET2:
7456 // This should have been mapped to another type already.
7458 case elfcpp::R_ARM_COPY:
7459 case elfcpp::R_ARM_GLOB_DAT:
7460 case elfcpp::R_ARM_JUMP_SLOT:
7461 case elfcpp::R_ARM_RELATIVE:
7462 // These are relocations which should only be seen by the
7463 // dynamic linker, and should never be seen here.
7464 gold_error(_("%s: unexpected reloc %u in object file"),
7465 object->name().c_str(), r_type);
7469 // These are initial TLS relocs, which are expected when
7471 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7472 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7473 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7474 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7475 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7477 bool output_is_shared = parameters->options().shared();
7478 const tls::Tls_optimization optimized_type
7479 = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
7483 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7484 if (optimized_type == tls::TLSOPT_NONE)
7486 // Create a pair of GOT entries for the module index and
7487 // dtv-relative offset.
7488 Arm_output_data_got<big_endian>* got
7489 = target->got_section(symtab, layout);
7490 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7491 unsigned int shndx = lsym.get_st_shndx();
7493 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7496 object->error(_("local symbol %u has bad shndx %u"),
7501 if (!parameters->doing_static_link())
7502 got->add_local_pair_with_rel(object, r_sym, shndx,
7504 target->rel_dyn_section(layout),
7505 elfcpp::R_ARM_TLS_DTPMOD32, 0);
7507 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR,
7511 // FIXME: TLS optimization not supported yet.
7515 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7516 if (optimized_type == tls::TLSOPT_NONE)
7518 // Create a GOT entry for the module index.
7519 target->got_mod_index_entry(symtab, layout, object);
7522 // FIXME: TLS optimization not supported yet.
7526 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7529 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7530 layout->set_has_static_tls();
7531 if (optimized_type == tls::TLSOPT_NONE)
7533 // Create a GOT entry for the tp-relative offset.
7534 Arm_output_data_got<big_endian>* got
7535 = target->got_section(symtab, layout);
7536 unsigned int r_sym =
7537 elfcpp::elf_r_sym<32>(reloc.get_r_info());
7538 if (!parameters->doing_static_link())
7539 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
7540 target->rel_dyn_section(layout),
7541 elfcpp::R_ARM_TLS_TPOFF32);
7542 else if (!object->local_has_got_offset(r_sym,
7543 GOT_TYPE_TLS_OFFSET))
7545 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
7546 unsigned int got_offset =
7547 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
7548 got->add_static_reloc(got_offset,
7549 elfcpp::R_ARM_TLS_TPOFF32, object,
7554 // FIXME: TLS optimization not supported yet.
7558 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7559 layout->set_has_static_tls();
7560 if (output_is_shared)
7562 // We need to create a dynamic relocation.
7563 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
7564 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7565 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7566 rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
7567 output_section, data_shndx,
7568 reloc.get_r_offset());
7579 unsupported_reloc_local(object, r_type);
7584 // Report an unsupported relocation against a global symbol.
7586 template<bool big_endian>
7588 Target_arm<big_endian>::Scan::unsupported_reloc_global(
7589 Sized_relobj<32, big_endian>* object,
7590 unsigned int r_type,
7593 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7594 object->name().c_str(), r_type, gsym->demangled_name().c_str());
7597 // Scan a relocation for a global symbol.
7599 template<bool big_endian>
7601 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
7604 Sized_relobj<32, big_endian>* object,
7605 unsigned int data_shndx,
7606 Output_section* output_section,
7607 const elfcpp::Rel<32, big_endian>& reloc,
7608 unsigned int r_type,
7611 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
7612 // section. We check here to avoid creating a dynamic reloc against
7613 // _GLOBAL_OFFSET_TABLE_.
7614 if (!target->has_got_section()
7615 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7616 target->got_section(symtab, layout);
7618 r_type = get_real_reloc_type(r_type);
7621 case elfcpp::R_ARM_NONE:
7622 case elfcpp::R_ARM_V4BX:
7623 case elfcpp::R_ARM_GNU_VTENTRY:
7624 case elfcpp::R_ARM_GNU_VTINHERIT:
7627 case elfcpp::R_ARM_ABS32:
7628 case elfcpp::R_ARM_ABS16:
7629 case elfcpp::R_ARM_ABS12:
7630 case elfcpp::R_ARM_THM_ABS5:
7631 case elfcpp::R_ARM_ABS8:
7632 case elfcpp::R_ARM_BASE_ABS:
7633 case elfcpp::R_ARM_MOVW_ABS_NC:
7634 case elfcpp::R_ARM_MOVT_ABS:
7635 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7636 case elfcpp::R_ARM_THM_MOVT_ABS:
7637 case elfcpp::R_ARM_ABS32_NOI:
7638 // Absolute addressing relocations.
7640 // Make a PLT entry if necessary.
7641 if (this->symbol_needs_plt_entry(gsym))
7643 target->make_plt_entry(symtab, layout, gsym);
7644 // Since this is not a PC-relative relocation, we may be
7645 // taking the address of a function. In that case we need to
7646 // set the entry in the dynamic symbol table to the address of
7648 if (gsym->is_from_dynobj() && !parameters->options().shared())
7649 gsym->set_needs_dynsym_value();
7651 // Make a dynamic relocation if necessary.
7652 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
7654 if (gsym->may_need_copy_reloc())
7656 target->copy_reloc(symtab, layout, object,
7657 data_shndx, output_section, gsym, reloc);
7659 else if ((r_type == elfcpp::R_ARM_ABS32
7660 || r_type == elfcpp::R_ARM_ABS32_NOI)
7661 && gsym->can_use_relative_reloc(false))
7663 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7664 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
7665 output_section, object,
7666 data_shndx, reloc.get_r_offset());
7670 check_non_pic(object, r_type);
7671 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7672 rel_dyn->add_global(gsym, r_type, output_section, object,
7673 data_shndx, reloc.get_r_offset());
7679 case elfcpp::R_ARM_GOTOFF32:
7680 case elfcpp::R_ARM_GOTOFF12:
7681 // We need a GOT section.
7682 target->got_section(symtab, layout);
7685 case elfcpp::R_ARM_REL32:
7686 case elfcpp::R_ARM_LDR_PC_G0:
7687 case elfcpp::R_ARM_SBREL32:
7688 case elfcpp::R_ARM_THM_PC8:
7689 case elfcpp::R_ARM_BASE_PREL:
7690 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7691 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7692 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7693 case elfcpp::R_ARM_MOVW_PREL_NC:
7694 case elfcpp::R_ARM_MOVT_PREL:
7695 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7696 case elfcpp::R_ARM_THM_MOVT_PREL:
7697 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7698 case elfcpp::R_ARM_THM_PC12:
7699 case elfcpp::R_ARM_REL32_NOI:
7700 case elfcpp::R_ARM_ALU_PC_G0_NC:
7701 case elfcpp::R_ARM_ALU_PC_G0:
7702 case elfcpp::R_ARM_ALU_PC_G1_NC:
7703 case elfcpp::R_ARM_ALU_PC_G1:
7704 case elfcpp::R_ARM_ALU_PC_G2:
7705 case elfcpp::R_ARM_LDR_PC_G1:
7706 case elfcpp::R_ARM_LDR_PC_G2:
7707 case elfcpp::R_ARM_LDRS_PC_G0:
7708 case elfcpp::R_ARM_LDRS_PC_G1:
7709 case elfcpp::R_ARM_LDRS_PC_G2:
7710 case elfcpp::R_ARM_LDC_PC_G0:
7711 case elfcpp::R_ARM_LDC_PC_G1:
7712 case elfcpp::R_ARM_LDC_PC_G2:
7713 case elfcpp::R_ARM_ALU_SB_G0_NC:
7714 case elfcpp::R_ARM_ALU_SB_G0:
7715 case elfcpp::R_ARM_ALU_SB_G1_NC:
7716 case elfcpp::R_ARM_ALU_SB_G1:
7717 case elfcpp::R_ARM_ALU_SB_G2:
7718 case elfcpp::R_ARM_LDR_SB_G0:
7719 case elfcpp::R_ARM_LDR_SB_G1:
7720 case elfcpp::R_ARM_LDR_SB_G2:
7721 case elfcpp::R_ARM_LDRS_SB_G0:
7722 case elfcpp::R_ARM_LDRS_SB_G1:
7723 case elfcpp::R_ARM_LDRS_SB_G2:
7724 case elfcpp::R_ARM_LDC_SB_G0:
7725 case elfcpp::R_ARM_LDC_SB_G1:
7726 case elfcpp::R_ARM_LDC_SB_G2:
7727 case elfcpp::R_ARM_MOVW_BREL_NC:
7728 case elfcpp::R_ARM_MOVT_BREL:
7729 case elfcpp::R_ARM_MOVW_BREL:
7730 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7731 case elfcpp::R_ARM_THM_MOVT_BREL:
7732 case elfcpp::R_ARM_THM_MOVW_BREL:
7733 // Relative addressing relocations.
7735 // Make a dynamic relocation if necessary.
7736 int flags = Symbol::NON_PIC_REF;
7737 if (gsym->needs_dynamic_reloc(flags))
7739 if (target->may_need_copy_reloc(gsym))
7741 target->copy_reloc(symtab, layout, object,
7742 data_shndx, output_section, gsym, reloc);
7746 check_non_pic(object, r_type);
7747 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7748 rel_dyn->add_global(gsym, r_type, output_section, object,
7749 data_shndx, reloc.get_r_offset());
7755 case elfcpp::R_ARM_PC24:
7756 case elfcpp::R_ARM_THM_CALL:
7757 case elfcpp::R_ARM_PLT32:
7758 case elfcpp::R_ARM_CALL:
7759 case elfcpp::R_ARM_JUMP24:
7760 case elfcpp::R_ARM_THM_JUMP24:
7761 case elfcpp::R_ARM_SBREL31:
7762 case elfcpp::R_ARM_PREL31:
7763 case elfcpp::R_ARM_THM_JUMP19:
7764 case elfcpp::R_ARM_THM_JUMP6:
7765 case elfcpp::R_ARM_THM_JUMP11:
7766 case elfcpp::R_ARM_THM_JUMP8:
7767 // All the relocation above are branches except for the PREL31 ones.
7768 // A PREL31 relocation can point to a personality function in a shared
7769 // library. In that case we want to use a PLT because we want to
7770 // call the personality routine and the dyanmic linkers we care about
7771 // do not support dynamic PREL31 relocations. An REL31 relocation may
7772 // point to a function whose unwinding behaviour is being described but
7773 // we will not mistakenly generate a PLT for that because we should use
7774 // a local section symbol.
7776 // If the symbol is fully resolved, this is just a relative
7777 // local reloc. Otherwise we need a PLT entry.
7778 if (gsym->final_value_is_known())
7780 // If building a shared library, we can also skip the PLT entry
7781 // if the symbol is defined in the output file and is protected
7783 if (gsym->is_defined()
7784 && !gsym->is_from_dynobj()
7785 && !gsym->is_preemptible())
7787 target->make_plt_entry(symtab, layout, gsym);
7790 case elfcpp::R_ARM_GOT_BREL:
7791 case elfcpp::R_ARM_GOT_ABS:
7792 case elfcpp::R_ARM_GOT_PREL:
7794 // The symbol requires a GOT entry.
7795 Arm_output_data_got<big_endian>* got =
7796 target->got_section(symtab, layout);
7797 if (gsym->final_value_is_known())
7798 got->add_global(gsym, GOT_TYPE_STANDARD);
7801 // If this symbol is not fully resolved, we need to add a
7802 // GOT entry with a dynamic relocation.
7803 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7804 if (gsym->is_from_dynobj()
7805 || gsym->is_undefined()
7806 || gsym->is_preemptible())
7807 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
7808 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
7811 if (got->add_global(gsym, GOT_TYPE_STANDARD))
7812 rel_dyn->add_global_relative(
7813 gsym, elfcpp::R_ARM_RELATIVE, got,
7814 gsym->got_offset(GOT_TYPE_STANDARD));
7820 case elfcpp::R_ARM_TARGET1:
7821 case elfcpp::R_ARM_TARGET2:
7822 // These should have been mapped to other types already.
7824 case elfcpp::R_ARM_COPY:
7825 case elfcpp::R_ARM_GLOB_DAT:
7826 case elfcpp::R_ARM_JUMP_SLOT:
7827 case elfcpp::R_ARM_RELATIVE:
7828 // These are relocations which should only be seen by the
7829 // dynamic linker, and should never be seen here.
7830 gold_error(_("%s: unexpected reloc %u in object file"),
7831 object->name().c_str(), r_type);
7834 // These are initial tls relocs, which are expected when
7836 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7837 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7838 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7839 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7840 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7842 const bool is_final = gsym->final_value_is_known();
7843 const tls::Tls_optimization optimized_type
7844 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
7847 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7848 if (optimized_type == tls::TLSOPT_NONE)
7850 // Create a pair of GOT entries for the module index and
7851 // dtv-relative offset.
7852 Arm_output_data_got<big_endian>* got
7853 = target->got_section(symtab, layout);
7854 if (!parameters->doing_static_link())
7855 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
7856 target->rel_dyn_section(layout),
7857 elfcpp::R_ARM_TLS_DTPMOD32,
7858 elfcpp::R_ARM_TLS_DTPOFF32);
7860 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
7863 // FIXME: TLS optimization not supported yet.
7867 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7868 if (optimized_type == tls::TLSOPT_NONE)
7870 // Create a GOT entry for the module index.
7871 target->got_mod_index_entry(symtab, layout, object);
7874 // FIXME: TLS optimization not supported yet.
7878 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7881 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7882 layout->set_has_static_tls();
7883 if (optimized_type == tls::TLSOPT_NONE)
7885 // Create a GOT entry for the tp-relative offset.
7886 Arm_output_data_got<big_endian>* got
7887 = target->got_section(symtab, layout);
7888 if (!parameters->doing_static_link())
7889 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
7890 target->rel_dyn_section(layout),
7891 elfcpp::R_ARM_TLS_TPOFF32);
7892 else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
7894 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
7895 unsigned int got_offset =
7896 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
7897 got->add_static_reloc(got_offset,
7898 elfcpp::R_ARM_TLS_TPOFF32, gsym);
7902 // FIXME: TLS optimization not supported yet.
7906 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7907 layout->set_has_static_tls();
7908 if (parameters->options().shared())
7910 // We need to create a dynamic relocation.
7911 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7912 rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
7913 output_section, object,
7914 data_shndx, reloc.get_r_offset());
7925 unsupported_reloc_global(object, r_type, gsym);
7930 // Process relocations for gc.
7932 template<bool big_endian>
7934 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
7936 Sized_relobj<32, big_endian>* object,
7937 unsigned int data_shndx,
7939 const unsigned char* prelocs,
7941 Output_section* output_section,
7942 bool needs_special_offset_handling,
7943 size_t local_symbol_count,
7944 const unsigned char* plocal_symbols)
7946 typedef Target_arm<big_endian> Arm;
7947 typedef typename Target_arm<big_endian>::Scan Scan;
7949 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
7958 needs_special_offset_handling,
7963 // Scan relocations for a section.
7965 template<bool big_endian>
7967 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
7969 Sized_relobj<32, big_endian>* object,
7970 unsigned int data_shndx,
7971 unsigned int sh_type,
7972 const unsigned char* prelocs,
7974 Output_section* output_section,
7975 bool needs_special_offset_handling,
7976 size_t local_symbol_count,
7977 const unsigned char* plocal_symbols)
7979 typedef typename Target_arm<big_endian>::Scan Scan;
7980 if (sh_type == elfcpp::SHT_RELA)
7982 gold_error(_("%s: unsupported RELA reloc section"),
7983 object->name().c_str());
7987 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
7996 needs_special_offset_handling,
8001 // Finalize the sections.
8003 template<bool big_endian>
8005 Target_arm<big_endian>::do_finalize_sections(
8007 const Input_objects* input_objects,
8008 Symbol_table* symtab)
8010 // Merge processor-specific flags.
8011 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8012 p != input_objects->relobj_end();
8015 Arm_relobj<big_endian>* arm_relobj =
8016 Arm_relobj<big_endian>::as_arm_relobj(*p);
8017 if (arm_relobj->merge_flags_and_attributes())
8019 this->merge_processor_specific_flags(
8021 arm_relobj->processor_specific_flags());
8022 this->merge_object_attributes(arm_relobj->name().c_str(),
8023 arm_relobj->attributes_section_data());
8027 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
8028 p != input_objects->dynobj_end();
8031 Arm_dynobj<big_endian>* arm_dynobj =
8032 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
8033 this->merge_processor_specific_flags(
8035 arm_dynobj->processor_specific_flags());
8036 this->merge_object_attributes(arm_dynobj->name().c_str(),
8037 arm_dynobj->attributes_section_data());
8040 // Create an empty uninitialized attribute section if we still don't have it
8041 // at this moment. This happens if there is no attributes sections in all
8043 if (this->attributes_section_data_ == NULL)
8044 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
8047 const Object_attribute* cpu_arch_attr =
8048 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
8049 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
8050 this->set_may_use_blx(true);
8052 // Check if we need to use Cortex-A8 workaround.
8053 if (parameters->options().user_set_fix_cortex_a8())
8054 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
8057 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
8058 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
8060 const Object_attribute* cpu_arch_profile_attr =
8061 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
8062 this->fix_cortex_a8_ =
8063 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
8064 && (cpu_arch_profile_attr->int_value() == 'A'
8065 || cpu_arch_profile_attr->int_value() == 0));
8068 // Check if we can use V4BX interworking.
8069 // The V4BX interworking stub contains BX instruction,
8070 // which is not specified for some profiles.
8071 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8072 && !this->may_use_blx())
8073 gold_error(_("unable to provide V4BX reloc interworking fix up; "
8074 "the target profile does not support BX instruction"));
8076 // Fill in some more dynamic tags.
8077 const Reloc_section* rel_plt = (this->plt_ == NULL
8079 : this->plt_->rel_plt());
8080 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
8081 this->rel_dyn_, true, false);
8083 // Emit any relocs we saved in an attempt to avoid generating COPY
8085 if (this->copy_relocs_.any_saved_relocs())
8086 this->copy_relocs_.emit(this->rel_dyn_section(layout));
8088 // Handle the .ARM.exidx section.
8089 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
8090 if (exidx_section != NULL
8091 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
8092 && !parameters->options().relocatable())
8094 // Create __exidx_start and __exdix_end symbols.
8095 symtab->define_in_output_data("__exidx_start", NULL,
8096 Symbol_table::PREDEFINED,
8097 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8098 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8100 symtab->define_in_output_data("__exidx_end", NULL,
8101 Symbol_table::PREDEFINED,
8102 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8103 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8106 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
8107 // the .ARM.exidx section.
8108 if (!layout->script_options()->saw_phdrs_clause())
8110 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
8112 Output_segment* exidx_segment =
8113 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
8114 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
8119 // Create an .ARM.attributes section unless we have no regular input
8120 // object. In that case the output will be empty.
8121 if (input_objects->number_of_relobjs() != 0)
8123 Output_attributes_section_data* attributes_section =
8124 new Output_attributes_section_data(*this->attributes_section_data_);
8125 layout->add_output_section_data(".ARM.attributes",
8126 elfcpp::SHT_ARM_ATTRIBUTES, 0,
8127 attributes_section, false, false, false,
8132 // Return whether a direct absolute static relocation needs to be applied.
8133 // In cases where Scan::local() or Scan::global() has created
8134 // a dynamic relocation other than R_ARM_RELATIVE, the addend
8135 // of the relocation is carried in the data, and we must not
8136 // apply the static relocation.
8138 template<bool big_endian>
8140 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
8141 const Sized_symbol<32>* gsym,
8144 Output_section* output_section)
8146 // If the output section is not allocated, then we didn't call
8147 // scan_relocs, we didn't create a dynamic reloc, and we must apply
8149 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
8152 // For local symbols, we will have created a non-RELATIVE dynamic
8153 // relocation only if (a) the output is position independent,
8154 // (b) the relocation is absolute (not pc- or segment-relative), and
8155 // (c) the relocation is not 32 bits wide.
8157 return !(parameters->options().output_is_position_independent()
8158 && (ref_flags & Symbol::ABSOLUTE_REF)
8161 // For global symbols, we use the same helper routines used in the
8162 // scan pass. If we did not create a dynamic relocation, or if we
8163 // created a RELATIVE dynamic relocation, we should apply the static
8165 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
8166 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
8167 && gsym->can_use_relative_reloc(ref_flags
8168 & Symbol::FUNCTION_CALL);
8169 return !has_dyn || is_rel;
8172 // Perform a relocation.
8174 template<bool big_endian>
8176 Target_arm<big_endian>::Relocate::relocate(
8177 const Relocate_info<32, big_endian>* relinfo,
8179 Output_section *output_section,
8181 const elfcpp::Rel<32, big_endian>& rel,
8182 unsigned int r_type,
8183 const Sized_symbol<32>* gsym,
8184 const Symbol_value<32>* psymval,
8185 unsigned char* view,
8186 Arm_address address,
8187 section_size_type view_size)
8189 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
8191 r_type = get_real_reloc_type(r_type);
8192 const Arm_reloc_property* reloc_property =
8193 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8194 if (reloc_property == NULL)
8196 std::string reloc_name =
8197 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8198 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8199 _("cannot relocate %s in object file"),
8200 reloc_name.c_str());
8204 const Arm_relobj<big_endian>* object =
8205 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8207 // If the final branch target of a relocation is THUMB instruction, this
8208 // is 1. Otherwise it is 0.
8209 Arm_address thumb_bit = 0;
8210 Symbol_value<32> symval;
8211 bool is_weakly_undefined_without_plt = false;
8212 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
8216 // This is a global symbol. Determine if we use PLT and if the
8217 // final target is THUMB.
8218 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
8220 // This uses a PLT, change the symbol value.
8221 symval.set_output_value(target->plt_section()->address()
8222 + gsym->plt_offset());
8225 else if (gsym->is_weak_undefined())
8227 // This is a weakly undefined symbol and we do not use PLT
8228 // for this relocation. A branch targeting this symbol will
8229 // be converted into an NOP.
8230 is_weakly_undefined_without_plt = true;
8234 // Set thumb bit if symbol:
8235 // -Has type STT_ARM_TFUNC or
8236 // -Has type STT_FUNC, is defined and with LSB in value set.
8238 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
8239 || (gsym->type() == elfcpp::STT_FUNC
8240 && !gsym->is_undefined()
8241 && ((psymval->value(object, 0) & 1) != 0)))
8248 // This is a local symbol. Determine if the final target is THUMB.
8249 // We saved this information when all the local symbols were read.
8250 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
8251 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
8252 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
8257 // This is a fake relocation synthesized for a stub. It does not have
8258 // a real symbol. We just look at the LSB of the symbol value to
8259 // determine if the target is THUMB or not.
8260 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
8263 // Strip LSB if this points to a THUMB target.
8265 && reloc_property->uses_thumb_bit()
8266 && ((psymval->value(object, 0) & 1) != 0))
8268 Arm_address stripped_value =
8269 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
8270 symval.set_output_value(stripped_value);
8274 // Get the GOT offset if needed.
8275 // The GOT pointer points to the end of the GOT section.
8276 // We need to subtract the size of the GOT section to get
8277 // the actual offset to use in the relocation.
8278 bool have_got_offset = false;
8279 unsigned int got_offset = 0;
8282 case elfcpp::R_ARM_GOT_BREL:
8283 case elfcpp::R_ARM_GOT_PREL:
8286 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8287 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
8288 - target->got_size());
8292 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8293 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8294 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
8295 - target->got_size());
8297 have_got_offset = true;
8304 // To look up relocation stubs, we need to pass the symbol table index of
8306 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8308 // Get the addressing origin of the output segment defining the
8309 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
8310 Arm_address sym_origin = 0;
8311 if (reloc_property->uses_symbol_base())
8313 if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
8314 // R_ARM_BASE_ABS with the NULL symbol will give the
8315 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
8316 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
8317 sym_origin = target->got_plt_section()->address();
8318 else if (gsym == NULL)
8320 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
8321 sym_origin = gsym->output_segment()->vaddr();
8322 else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
8323 sym_origin = gsym->output_data()->address();
8325 // TODO: Assumes the segment base to be zero for the global symbols
8326 // till the proper support for the segment-base-relative addressing
8327 // will be implemented. This is consistent with GNU ld.
8330 // For relative addressing relocation, find out the relative address base.
8331 Arm_address relative_address_base = 0;
8332 switch(reloc_property->relative_address_base())
8334 case Arm_reloc_property::RAB_NONE:
8335 // Relocations with relative address bases RAB_TLS and RAB_tp are
8336 // handled by relocate_tls. So we do not need to do anything here.
8337 case Arm_reloc_property::RAB_TLS:
8338 case Arm_reloc_property::RAB_tp:
8340 case Arm_reloc_property::RAB_B_S:
8341 relative_address_base = sym_origin;
8343 case Arm_reloc_property::RAB_GOT_ORG:
8344 relative_address_base = target->got_plt_section()->address();
8346 case Arm_reloc_property::RAB_P:
8347 relative_address_base = address;
8349 case Arm_reloc_property::RAB_Pa:
8350 relative_address_base = address & 0xfffffffcU;
8356 typename Arm_relocate_functions::Status reloc_status =
8357 Arm_relocate_functions::STATUS_OKAY;
8358 bool check_overflow = reloc_property->checks_overflow();
8361 case elfcpp::R_ARM_NONE:
8364 case elfcpp::R_ARM_ABS8:
8365 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8367 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
8370 case elfcpp::R_ARM_ABS12:
8371 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8373 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
8376 case elfcpp::R_ARM_ABS16:
8377 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8379 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
8382 case elfcpp::R_ARM_ABS32:
8383 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
8385 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8389 case elfcpp::R_ARM_ABS32_NOI:
8390 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
8392 // No thumb bit for this relocation: (S + A)
8393 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8397 case elfcpp::R_ARM_MOVW_ABS_NC:
8398 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8400 reloc_status = Arm_relocate_functions::movw(view, object, psymval,
8405 case elfcpp::R_ARM_MOVT_ABS:
8406 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8408 reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
8411 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8412 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8414 reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
8415 0, thumb_bit, false);
8418 case elfcpp::R_ARM_THM_MOVT_ABS:
8419 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8421 reloc_status = Arm_relocate_functions::thm_movt(view, object,
8425 case elfcpp::R_ARM_MOVW_PREL_NC:
8426 case elfcpp::R_ARM_MOVW_BREL_NC:
8427 case elfcpp::R_ARM_MOVW_BREL:
8429 Arm_relocate_functions::movw(view, object, psymval,
8430 relative_address_base, thumb_bit,
8434 case elfcpp::R_ARM_MOVT_PREL:
8435 case elfcpp::R_ARM_MOVT_BREL:
8437 Arm_relocate_functions::movt(view, object, psymval,
8438 relative_address_base);
8441 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8442 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8443 case elfcpp::R_ARM_THM_MOVW_BREL:
8445 Arm_relocate_functions::thm_movw(view, object, psymval,
8446 relative_address_base,
8447 thumb_bit, check_overflow);
8450 case elfcpp::R_ARM_THM_MOVT_PREL:
8451 case elfcpp::R_ARM_THM_MOVT_BREL:
8453 Arm_relocate_functions::thm_movt(view, object, psymval,
8454 relative_address_base);
8457 case elfcpp::R_ARM_REL32:
8458 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8459 address, thumb_bit);
8462 case elfcpp::R_ARM_THM_ABS5:
8463 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8465 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
8468 // Thumb long branches.
8469 case elfcpp::R_ARM_THM_CALL:
8470 case elfcpp::R_ARM_THM_XPC22:
8471 case elfcpp::R_ARM_THM_JUMP24:
8473 Arm_relocate_functions::thumb_branch_common(
8474 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8475 thumb_bit, is_weakly_undefined_without_plt);
8478 case elfcpp::R_ARM_GOTOFF32:
8480 Arm_address got_origin;
8481 got_origin = target->got_plt_section()->address();
8482 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8483 got_origin, thumb_bit);
8487 case elfcpp::R_ARM_BASE_PREL:
8488 gold_assert(gsym != NULL);
8490 Arm_relocate_functions::base_prel(view, sym_origin, address);
8493 case elfcpp::R_ARM_BASE_ABS:
8495 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8499 reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
8503 case elfcpp::R_ARM_GOT_BREL:
8504 gold_assert(have_got_offset);
8505 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
8508 case elfcpp::R_ARM_GOT_PREL:
8509 gold_assert(have_got_offset);
8510 // Get the address origin for GOT PLT, which is allocated right
8511 // after the GOT section, to calculate an absolute address of
8512 // the symbol GOT entry (got_origin + got_offset).
8513 Arm_address got_origin;
8514 got_origin = target->got_plt_section()->address();
8515 reloc_status = Arm_relocate_functions::got_prel(view,
8516 got_origin + got_offset,
8520 case elfcpp::R_ARM_PLT32:
8521 case elfcpp::R_ARM_CALL:
8522 case elfcpp::R_ARM_JUMP24:
8523 case elfcpp::R_ARM_XPC25:
8524 gold_assert(gsym == NULL
8525 || gsym->has_plt_offset()
8526 || gsym->final_value_is_known()
8527 || (gsym->is_defined()
8528 && !gsym->is_from_dynobj()
8529 && !gsym->is_preemptible()));
8531 Arm_relocate_functions::arm_branch_common(
8532 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8533 thumb_bit, is_weakly_undefined_without_plt);
8536 case elfcpp::R_ARM_THM_JUMP19:
8538 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
8542 case elfcpp::R_ARM_THM_JUMP6:
8544 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
8547 case elfcpp::R_ARM_THM_JUMP8:
8549 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
8552 case elfcpp::R_ARM_THM_JUMP11:
8554 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
8557 case elfcpp::R_ARM_PREL31:
8558 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
8559 address, thumb_bit);
8562 case elfcpp::R_ARM_V4BX:
8563 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
8565 const bool is_v4bx_interworking =
8566 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
8568 Arm_relocate_functions::v4bx(relinfo, view, object, address,
8569 is_v4bx_interworking);
8573 case elfcpp::R_ARM_THM_PC8:
8575 Arm_relocate_functions::thm_pc8(view, object, psymval, address);
8578 case elfcpp::R_ARM_THM_PC12:
8580 Arm_relocate_functions::thm_pc12(view, object, psymval, address);
8583 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8585 Arm_relocate_functions::thm_alu11(view, object, psymval, address,
8589 case elfcpp::R_ARM_ALU_PC_G0_NC:
8590 case elfcpp::R_ARM_ALU_PC_G0:
8591 case elfcpp::R_ARM_ALU_PC_G1_NC:
8592 case elfcpp::R_ARM_ALU_PC_G1:
8593 case elfcpp::R_ARM_ALU_PC_G2:
8594 case elfcpp::R_ARM_ALU_SB_G0_NC:
8595 case elfcpp::R_ARM_ALU_SB_G0:
8596 case elfcpp::R_ARM_ALU_SB_G1_NC:
8597 case elfcpp::R_ARM_ALU_SB_G1:
8598 case elfcpp::R_ARM_ALU_SB_G2:
8600 Arm_relocate_functions::arm_grp_alu(view, object, psymval,
8601 reloc_property->group_index(),
8602 relative_address_base,
8603 thumb_bit, check_overflow);
8606 case elfcpp::R_ARM_LDR_PC_G0:
8607 case elfcpp::R_ARM_LDR_PC_G1:
8608 case elfcpp::R_ARM_LDR_PC_G2:
8609 case elfcpp::R_ARM_LDR_SB_G0:
8610 case elfcpp::R_ARM_LDR_SB_G1:
8611 case elfcpp::R_ARM_LDR_SB_G2:
8613 Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
8614 reloc_property->group_index(),
8615 relative_address_base);
8618 case elfcpp::R_ARM_LDRS_PC_G0:
8619 case elfcpp::R_ARM_LDRS_PC_G1:
8620 case elfcpp::R_ARM_LDRS_PC_G2:
8621 case elfcpp::R_ARM_LDRS_SB_G0:
8622 case elfcpp::R_ARM_LDRS_SB_G1:
8623 case elfcpp::R_ARM_LDRS_SB_G2:
8625 Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
8626 reloc_property->group_index(),
8627 relative_address_base);
8630 case elfcpp::R_ARM_LDC_PC_G0:
8631 case elfcpp::R_ARM_LDC_PC_G1:
8632 case elfcpp::R_ARM_LDC_PC_G2:
8633 case elfcpp::R_ARM_LDC_SB_G0:
8634 case elfcpp::R_ARM_LDC_SB_G1:
8635 case elfcpp::R_ARM_LDC_SB_G2:
8637 Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
8638 reloc_property->group_index(),
8639 relative_address_base);
8642 // These are initial tls relocs, which are expected when
8644 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8645 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8646 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8647 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8648 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8650 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
8651 view, address, view_size);
8658 // Report any errors.
8659 switch (reloc_status)
8661 case Arm_relocate_functions::STATUS_OKAY:
8663 case Arm_relocate_functions::STATUS_OVERFLOW:
8664 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8665 _("relocation overflow in %s"),
8666 reloc_property->name().c_str());
8668 case Arm_relocate_functions::STATUS_BAD_RELOC:
8669 gold_error_at_location(
8673 _("unexpected opcode while processing relocation %s"),
8674 reloc_property->name().c_str());
8683 // Perform a TLS relocation.
8685 template<bool big_endian>
8686 inline typename Arm_relocate_functions<big_endian>::Status
8687 Target_arm<big_endian>::Relocate::relocate_tls(
8688 const Relocate_info<32, big_endian>* relinfo,
8689 Target_arm<big_endian>* target,
8691 const elfcpp::Rel<32, big_endian>& rel,
8692 unsigned int r_type,
8693 const Sized_symbol<32>* gsym,
8694 const Symbol_value<32>* psymval,
8695 unsigned char* view,
8696 elfcpp::Elf_types<32>::Elf_Addr address,
8697 section_size_type /*view_size*/ )
8699 typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
8700 typedef Relocate_functions<32, big_endian> RelocFuncs;
8701 Output_segment* tls_segment = relinfo->layout->tls_segment();
8703 const Sized_relobj<32, big_endian>* object = relinfo->object;
8705 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
8707 const bool is_final = (gsym == NULL
8708 ? !parameters->options().shared()
8709 : gsym->final_value_is_known());
8710 const tls::Tls_optimization optimized_type
8711 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
8714 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8716 unsigned int got_type = GOT_TYPE_TLS_PAIR;
8717 unsigned int got_offset;
8720 gold_assert(gsym->has_got_offset(got_type));
8721 got_offset = gsym->got_offset(got_type) - target->got_size();
8725 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8726 gold_assert(object->local_has_got_offset(r_sym, got_type));
8727 got_offset = (object->local_got_offset(r_sym, got_type)
8728 - target->got_size());
8730 if (optimized_type == tls::TLSOPT_NONE)
8732 Arm_address got_entry =
8733 target->got_plt_section()->address() + got_offset;
8735 // Relocate the field with the PC relative offset of the pair of
8737 RelocFuncs::pcrel32(view, got_entry, address);
8738 return ArmRelocFuncs::STATUS_OKAY;
8743 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8744 if (optimized_type == tls::TLSOPT_NONE)
8746 // Relocate the field with the offset of the GOT entry for
8747 // the module index.
8748 unsigned int got_offset;
8749 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
8750 - target->got_size());
8751 Arm_address got_entry =
8752 target->got_plt_section()->address() + got_offset;
8754 // Relocate the field with the PC relative offset of the pair of
8756 RelocFuncs::pcrel32(view, got_entry, address);
8757 return ArmRelocFuncs::STATUS_OKAY;
8761 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8762 RelocFuncs::rel32(view, value);
8763 return ArmRelocFuncs::STATUS_OKAY;
8765 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8766 if (optimized_type == tls::TLSOPT_NONE)
8768 // Relocate the field with the offset of the GOT entry for
8769 // the tp-relative offset of the symbol.
8770 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
8771 unsigned int got_offset;
8774 gold_assert(gsym->has_got_offset(got_type));
8775 got_offset = gsym->got_offset(got_type);
8779 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8780 gold_assert(object->local_has_got_offset(r_sym, got_type));
8781 got_offset = object->local_got_offset(r_sym, got_type);
8784 // All GOT offsets are relative to the end of the GOT.
8785 got_offset -= target->got_size();
8787 Arm_address got_entry =
8788 target->got_plt_section()->address() + got_offset;
8790 // Relocate the field with the PC relative offset of the GOT entry.
8791 RelocFuncs::pcrel32(view, got_entry, address);
8792 return ArmRelocFuncs::STATUS_OKAY;
8796 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8797 // If we're creating a shared library, a dynamic relocation will
8798 // have been created for this location, so do not apply it now.
8799 if (!parameters->options().shared())
8801 gold_assert(tls_segment != NULL);
8803 // $tp points to the TCB, which is followed by the TLS, so we
8804 // need to add TCB size to the offset.
8805 Arm_address aligned_tcb_size =
8806 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
8807 RelocFuncs::rel32(view, value + aligned_tcb_size);
8810 return ArmRelocFuncs::STATUS_OKAY;
8816 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8817 _("unsupported reloc %u"),
8819 return ArmRelocFuncs::STATUS_BAD_RELOC;
8822 // Relocate section data.
8824 template<bool big_endian>
8826 Target_arm<big_endian>::relocate_section(
8827 const Relocate_info<32, big_endian>* relinfo,
8828 unsigned int sh_type,
8829 const unsigned char* prelocs,
8831 Output_section* output_section,
8832 bool needs_special_offset_handling,
8833 unsigned char* view,
8834 Arm_address address,
8835 section_size_type view_size,
8836 const Reloc_symbol_changes* reloc_symbol_changes)
8838 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
8839 gold_assert(sh_type == elfcpp::SHT_REL);
8841 // See if we are relocating a relaxed input section. If so, the view
8842 // covers the whole output section and we need to adjust accordingly.
8843 if (needs_special_offset_handling)
8845 const Output_relaxed_input_section* poris =
8846 output_section->find_relaxed_input_section(relinfo->object,
8847 relinfo->data_shndx);
8850 Arm_address section_address = poris->address();
8851 section_size_type section_size = poris->data_size();
8853 gold_assert((section_address >= address)
8854 && ((section_address + section_size)
8855 <= (address + view_size)));
8857 off_t offset = section_address - address;
8860 view_size = section_size;
8864 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
8871 needs_special_offset_handling,
8875 reloc_symbol_changes);
8878 // Return the size of a relocation while scanning during a relocatable
8881 template<bool big_endian>
8883 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
8884 unsigned int r_type,
8887 r_type = get_real_reloc_type(r_type);
8888 const Arm_reloc_property* arp =
8889 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8894 std::string reloc_name =
8895 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8896 gold_error(_("%s: unexpected %s in object file"),
8897 object->name().c_str(), reloc_name.c_str());
8902 // Scan the relocs during a relocatable link.
8904 template<bool big_endian>
8906 Target_arm<big_endian>::scan_relocatable_relocs(
8907 Symbol_table* symtab,
8909 Sized_relobj<32, big_endian>* object,
8910 unsigned int data_shndx,
8911 unsigned int sh_type,
8912 const unsigned char* prelocs,
8914 Output_section* output_section,
8915 bool needs_special_offset_handling,
8916 size_t local_symbol_count,
8917 const unsigned char* plocal_symbols,
8918 Relocatable_relocs* rr)
8920 gold_assert(sh_type == elfcpp::SHT_REL);
8922 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
8923 Relocatable_size_for_reloc> Scan_relocatable_relocs;
8925 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
8926 Scan_relocatable_relocs>(
8934 needs_special_offset_handling,
8940 // Relocate a section during a relocatable link.
8942 template<bool big_endian>
8944 Target_arm<big_endian>::relocate_for_relocatable(
8945 const Relocate_info<32, big_endian>* relinfo,
8946 unsigned int sh_type,
8947 const unsigned char* prelocs,
8949 Output_section* output_section,
8950 off_t offset_in_output_section,
8951 const Relocatable_relocs* rr,
8952 unsigned char* view,
8953 Arm_address view_address,
8954 section_size_type view_size,
8955 unsigned char* reloc_view,
8956 section_size_type reloc_view_size)
8958 gold_assert(sh_type == elfcpp::SHT_REL);
8960 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
8965 offset_in_output_section,
8974 // Return the value to use for a dynamic symbol which requires special
8975 // treatment. This is how we support equality comparisons of function
8976 // pointers across shared library boundaries, as described in the
8977 // processor specific ABI supplement.
8979 template<bool big_endian>
8981 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
8983 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
8984 return this->plt_section()->address() + gsym->plt_offset();
8987 // Map platform-specific relocs to real relocs
8989 template<bool big_endian>
8991 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
8995 case elfcpp::R_ARM_TARGET1:
8996 // This is either R_ARM_ABS32 or R_ARM_REL32;
8997 return elfcpp::R_ARM_ABS32;
8999 case elfcpp::R_ARM_TARGET2:
9000 // This can be any reloc type but ususally is R_ARM_GOT_PREL
9001 return elfcpp::R_ARM_GOT_PREL;
9008 // Whether if two EABI versions V1 and V2 are compatible.
9010 template<bool big_endian>
9012 Target_arm<big_endian>::are_eabi_versions_compatible(
9013 elfcpp::Elf_Word v1,
9014 elfcpp::Elf_Word v2)
9016 // v4 and v5 are the same spec before and after it was released,
9017 // so allow mixing them.
9018 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
9019 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
9025 // Combine FLAGS from an input object called NAME and the processor-specific
9026 // flags in the ELF header of the output. Much of this is adapted from the
9027 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
9028 // in bfd/elf32-arm.c.
9030 template<bool big_endian>
9032 Target_arm<big_endian>::merge_processor_specific_flags(
9033 const std::string& name,
9034 elfcpp::Elf_Word flags)
9036 if (this->are_processor_specific_flags_set())
9038 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
9040 // Nothing to merge if flags equal to those in output.
9041 if (flags == out_flags)
9044 // Complain about various flag mismatches.
9045 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
9046 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
9047 if (!this->are_eabi_versions_compatible(version1, version2)
9048 && parameters->options().warn_mismatch())
9049 gold_error(_("Source object %s has EABI version %d but output has "
9050 "EABI version %d."),
9052 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
9053 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
9057 // If the input is the default architecture and had the default
9058 // flags then do not bother setting the flags for the output
9059 // architecture, instead allow future merges to do this. If no
9060 // future merges ever set these flags then they will retain their
9061 // uninitialised values, which surprise surprise, correspond
9062 // to the default values.
9066 // This is the first time, just copy the flags.
9067 // We only copy the EABI version for now.
9068 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
9072 // Adjust ELF file header.
9073 template<bool big_endian>
9075 Target_arm<big_endian>::do_adjust_elf_header(
9076 unsigned char* view,
9079 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
9081 elfcpp::Ehdr<32, big_endian> ehdr(view);
9082 unsigned char e_ident[elfcpp::EI_NIDENT];
9083 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9085 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
9086 == elfcpp::EF_ARM_EABI_UNKNOWN)
9087 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
9089 e_ident[elfcpp::EI_OSABI] = 0;
9090 e_ident[elfcpp::EI_ABIVERSION] = 0;
9092 // FIXME: Do EF_ARM_BE8 adjustment.
9094 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
9095 oehdr.put_e_ident(e_ident);
9098 // do_make_elf_object to override the same function in the base class.
9099 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
9100 // to store ARM specific information. Hence we need to have our own
9101 // ELF object creation.
9103 template<bool big_endian>
9105 Target_arm<big_endian>::do_make_elf_object(
9106 const std::string& name,
9107 Input_file* input_file,
9108 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
9110 int et = ehdr.get_e_type();
9111 if (et == elfcpp::ET_REL)
9113 Arm_relobj<big_endian>* obj =
9114 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
9118 else if (et == elfcpp::ET_DYN)
9120 Sized_dynobj<32, big_endian>* obj =
9121 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
9127 gold_error(_("%s: unsupported ELF file type %d"),
9133 // Read the architecture from the Tag_also_compatible_with attribute, if any.
9134 // Returns -1 if no architecture could be read.
9135 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
9137 template<bool big_endian>
9139 Target_arm<big_endian>::get_secondary_compatible_arch(
9140 const Attributes_section_data* pasd)
9142 const Object_attribute *known_attributes =
9143 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
9145 // Note: the tag and its argument below are uleb128 values, though
9146 // currently-defined values fit in one byte for each.
9147 const std::string& sv =
9148 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
9150 && sv.data()[0] == elfcpp::Tag_CPU_arch
9151 && (sv.data()[1] & 128) != 128)
9152 return sv.data()[1];
9154 // This tag is "safely ignorable", so don't complain if it looks funny.
9158 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
9159 // The tag is removed if ARCH is -1.
9160 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
9162 template<bool big_endian>
9164 Target_arm<big_endian>::set_secondary_compatible_arch(
9165 Attributes_section_data* pasd,
9168 Object_attribute *known_attributes =
9169 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
9173 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
9177 // Note: the tag and its argument below are uleb128 values, though
9178 // currently-defined values fit in one byte for each.
9180 sv[0] = elfcpp::Tag_CPU_arch;
9181 gold_assert(arch != 0);
9185 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
9188 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
9190 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
9192 template<bool big_endian>
9194 Target_arm<big_endian>::tag_cpu_arch_combine(
9197 int* secondary_compat_out,
9199 int secondary_compat)
9201 #define T(X) elfcpp::TAG_CPU_ARCH_##X
9202 static const int v6t2[] =
9214 static const int v6k[] =
9227 static const int v7[] =
9241 static const int v6_m[] =
9256 static const int v6s_m[] =
9272 static const int v7e_m[] =
9289 static const int v4t_plus_v6_m[] =
9305 T(V4T_PLUS_V6_M) // V4T plus V6_M.
9307 static const int *comb[] =
9315 // Pseudo-architecture.
9319 // Check we've not got a higher architecture than we know about.
9321 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
9323 gold_error(_("%s: unknown CPU architecture"), name);
9327 // Override old tag if we have a Tag_also_compatible_with on the output.
9329 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
9330 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
9331 oldtag = T(V4T_PLUS_V6_M);
9333 // And override the new tag if we have a Tag_also_compatible_with on the
9336 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
9337 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
9338 newtag = T(V4T_PLUS_V6_M);
9340 // Architectures before V6KZ add features monotonically.
9341 int tagh = std::max(oldtag, newtag);
9342 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
9345 int tagl = std::min(oldtag, newtag);
9346 int result = comb[tagh - T(V6T2)][tagl];
9348 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
9349 // as the canonical version.
9350 if (result == T(V4T_PLUS_V6_M))
9353 *secondary_compat_out = T(V6_M);
9356 *secondary_compat_out = -1;
9360 gold_error(_("%s: conflicting CPU architectures %d/%d"),
9361 name, oldtag, newtag);
9369 // Helper to print AEABI enum tag value.
9371 template<bool big_endian>
9373 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
9375 static const char *aeabi_enum_names[] =
9376 { "", "variable-size", "32-bit", "" };
9377 const size_t aeabi_enum_names_size =
9378 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
9380 if (value < aeabi_enum_names_size)
9381 return std::string(aeabi_enum_names[value]);
9385 sprintf(buffer, "<unknown value %u>", value);
9386 return std::string(buffer);
9390 // Return the string value to store in TAG_CPU_name.
9392 template<bool big_endian>
9394 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
9396 static const char *name_table[] = {
9397 // These aren't real CPU names, but we can't guess
9398 // that from the architecture version alone.
9414 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
9416 if (value < name_table_size)
9417 return std::string(name_table[value]);
9421 sprintf(buffer, "<unknown CPU value %u>", value);
9422 return std::string(buffer);
9426 // Merge object attributes from input file called NAME with those of the
9427 // output. The input object attributes are in the object pointed by PASD.
9429 template<bool big_endian>
9431 Target_arm<big_endian>::merge_object_attributes(
9433 const Attributes_section_data* pasd)
9435 // Return if there is no attributes section data.
9439 // If output has no object attributes, just copy.
9440 const int vendor = Object_attribute::OBJ_ATTR_PROC;
9441 if (this->attributes_section_data_ == NULL)
9443 this->attributes_section_data_ = new Attributes_section_data(*pasd);
9444 Object_attribute* out_attr =
9445 this->attributes_section_data_->known_attributes(vendor);
9447 // We do not output objects with Tag_MPextension_use_legacy - we move
9448 // the attribute's value to Tag_MPextension_use. */
9449 if (out_attr[elfcpp::Tag_MPextension_use_legacy].int_value() != 0)
9451 if (out_attr[elfcpp::Tag_MPextension_use].int_value() != 0
9452 && out_attr[elfcpp::Tag_MPextension_use_legacy].int_value()
9453 != out_attr[elfcpp::Tag_MPextension_use].int_value())
9455 gold_error(_("%s has both the current and legacy "
9456 "Tag_MPextension_use attributes"),
9460 out_attr[elfcpp::Tag_MPextension_use] =
9461 out_attr[elfcpp::Tag_MPextension_use_legacy];
9462 out_attr[elfcpp::Tag_MPextension_use_legacy].set_type(0);
9463 out_attr[elfcpp::Tag_MPextension_use_legacy].set_int_value(0);
9469 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9470 Object_attribute* out_attr =
9471 this->attributes_section_data_->known_attributes(vendor);
9473 // This needs to happen before Tag_ABI_FP_number_model is merged. */
9474 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
9475 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
9477 // Ignore mismatches if the object doesn't use floating point. */
9478 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
9479 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
9480 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
9481 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0
9482 && parameters->options().warn_mismatch())
9483 gold_error(_("%s uses VFP register arguments, output does not"),
9487 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
9489 // Merge this attribute with existing attributes.
9492 case elfcpp::Tag_CPU_raw_name:
9493 case elfcpp::Tag_CPU_name:
9494 // These are merged after Tag_CPU_arch.
9497 case elfcpp::Tag_ABI_optimization_goals:
9498 case elfcpp::Tag_ABI_FP_optimization_goals:
9499 // Use the first value seen.
9502 case elfcpp::Tag_CPU_arch:
9504 unsigned int saved_out_attr = out_attr->int_value();
9505 // Merge Tag_CPU_arch and Tag_also_compatible_with.
9506 int secondary_compat =
9507 this->get_secondary_compatible_arch(pasd);
9508 int secondary_compat_out =
9509 this->get_secondary_compatible_arch(
9510 this->attributes_section_data_);
9511 out_attr[i].set_int_value(
9512 tag_cpu_arch_combine(name, out_attr[i].int_value(),
9513 &secondary_compat_out,
9514 in_attr[i].int_value(),
9516 this->set_secondary_compatible_arch(this->attributes_section_data_,
9517 secondary_compat_out);
9519 // Merge Tag_CPU_name and Tag_CPU_raw_name.
9520 if (out_attr[i].int_value() == saved_out_attr)
9521 ; // Leave the names alone.
9522 else if (out_attr[i].int_value() == in_attr[i].int_value())
9524 // The output architecture has been changed to match the
9525 // input architecture. Use the input names.
9526 out_attr[elfcpp::Tag_CPU_name].set_string_value(
9527 in_attr[elfcpp::Tag_CPU_name].string_value());
9528 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
9529 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
9533 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
9534 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
9537 // If we still don't have a value for Tag_CPU_name,
9538 // make one up now. Tag_CPU_raw_name remains blank.
9539 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
9541 const std::string cpu_name =
9542 this->tag_cpu_name_value(out_attr[i].int_value());
9543 // FIXME: If we see an unknown CPU, this will be set
9544 // to "<unknown CPU n>", where n is the attribute value.
9545 // This is different from BFD, which leaves the name alone.
9546 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
9551 case elfcpp::Tag_ARM_ISA_use:
9552 case elfcpp::Tag_THUMB_ISA_use:
9553 case elfcpp::Tag_WMMX_arch:
9554 case elfcpp::Tag_Advanced_SIMD_arch:
9555 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
9556 case elfcpp::Tag_ABI_FP_rounding:
9557 case elfcpp::Tag_ABI_FP_exceptions:
9558 case elfcpp::Tag_ABI_FP_user_exceptions:
9559 case elfcpp::Tag_ABI_FP_number_model:
9560 case elfcpp::Tag_VFP_HP_extension:
9561 case elfcpp::Tag_CPU_unaligned_access:
9562 case elfcpp::Tag_T2EE_use:
9563 case elfcpp::Tag_Virtualization_use:
9564 case elfcpp::Tag_MPextension_use:
9565 // Use the largest value specified.
9566 if (in_attr[i].int_value() > out_attr[i].int_value())
9567 out_attr[i].set_int_value(in_attr[i].int_value());
9570 case elfcpp::Tag_ABI_align8_preserved:
9571 case elfcpp::Tag_ABI_PCS_RO_data:
9572 // Use the smallest value specified.
9573 if (in_attr[i].int_value() < out_attr[i].int_value())
9574 out_attr[i].set_int_value(in_attr[i].int_value());
9577 case elfcpp::Tag_ABI_align8_needed:
9578 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
9579 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
9580 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
9583 // This error message should be enabled once all non-conformant
9584 // binaries in the toolchain have had the attributes set
9586 // gold_error(_("output 8-byte data alignment conflicts with %s"),
9590 case elfcpp::Tag_ABI_FP_denormal:
9591 case elfcpp::Tag_ABI_PCS_GOT_use:
9593 // These tags have 0 = don't care, 1 = strong requirement,
9594 // 2 = weak requirement.
9595 static const int order_021[3] = {0, 2, 1};
9597 // Use the "greatest" from the sequence 0, 2, 1, or the largest
9598 // value if greater than 2 (for future-proofing).
9599 if ((in_attr[i].int_value() > 2
9600 && in_attr[i].int_value() > out_attr[i].int_value())
9601 || (in_attr[i].int_value() <= 2
9602 && out_attr[i].int_value() <= 2
9603 && (order_021[in_attr[i].int_value()]
9604 > order_021[out_attr[i].int_value()])))
9605 out_attr[i].set_int_value(in_attr[i].int_value());
9609 case elfcpp::Tag_CPU_arch_profile:
9610 if (out_attr[i].int_value() != in_attr[i].int_value())
9612 // 0 will merge with anything.
9613 // 'A' and 'S' merge to 'A'.
9614 // 'R' and 'S' merge to 'R'.
9615 // 'M' and 'A|R|S' is an error.
9616 if (out_attr[i].int_value() == 0
9617 || (out_attr[i].int_value() == 'S'
9618 && (in_attr[i].int_value() == 'A'
9619 || in_attr[i].int_value() == 'R')))
9620 out_attr[i].set_int_value(in_attr[i].int_value());
9621 else if (in_attr[i].int_value() == 0
9622 || (in_attr[i].int_value() == 'S'
9623 && (out_attr[i].int_value() == 'A'
9624 || out_attr[i].int_value() == 'R')))
9626 else if (parameters->options().warn_mismatch())
9629 (_("conflicting architecture profiles %c/%c"),
9630 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
9631 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
9635 case elfcpp::Tag_VFP_arch:
9652 // Values greater than 6 aren't defined, so just pick the
9654 if (in_attr[i].int_value() > 6
9655 && in_attr[i].int_value() > out_attr[i].int_value())
9657 *out_attr = *in_attr;
9660 // The output uses the superset of input features
9661 // (ISA version) and registers.
9662 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
9663 vfp_versions[out_attr[i].int_value()].ver);
9664 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
9665 vfp_versions[out_attr[i].int_value()].regs);
9666 // This assumes all possible supersets are also a valid
9669 for (newval = 6; newval > 0; newval--)
9671 if (regs == vfp_versions[newval].regs
9672 && ver == vfp_versions[newval].ver)
9675 out_attr[i].set_int_value(newval);
9678 case elfcpp::Tag_PCS_config:
9679 if (out_attr[i].int_value() == 0)
9680 out_attr[i].set_int_value(in_attr[i].int_value());
9681 else if (in_attr[i].int_value() != 0
9682 && out_attr[i].int_value() != 0
9683 && parameters->options().warn_mismatch())
9685 // It's sometimes ok to mix different configs, so this is only
9687 gold_warning(_("%s: conflicting platform configuration"), name);
9690 case elfcpp::Tag_ABI_PCS_R9_use:
9691 if (in_attr[i].int_value() != out_attr[i].int_value()
9692 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
9693 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused
9694 && parameters->options().warn_mismatch())
9696 gold_error(_("%s: conflicting use of R9"), name);
9698 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
9699 out_attr[i].set_int_value(in_attr[i].int_value());
9701 case elfcpp::Tag_ABI_PCS_RW_data:
9702 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
9703 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9704 != elfcpp::AEABI_R9_SB)
9705 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9706 != elfcpp::AEABI_R9_unused)
9707 && parameters->options().warn_mismatch())
9709 gold_error(_("%s: SB relative addressing conflicts with use "
9713 // Use the smallest value specified.
9714 if (in_attr[i].int_value() < out_attr[i].int_value())
9715 out_attr[i].set_int_value(in_attr[i].int_value());
9717 case elfcpp::Tag_ABI_PCS_wchar_t:
9718 // FIXME: Make it possible to turn off this warning.
9719 if (out_attr[i].int_value()
9720 && in_attr[i].int_value()
9721 && out_attr[i].int_value() != in_attr[i].int_value()
9722 && parameters->options().warn_mismatch())
9724 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
9725 "use %u-byte wchar_t; use of wchar_t values "
9726 "across objects may fail"),
9727 name, in_attr[i].int_value(),
9728 out_attr[i].int_value());
9730 else if (in_attr[i].int_value() && !out_attr[i].int_value())
9731 out_attr[i].set_int_value(in_attr[i].int_value());
9733 case elfcpp::Tag_ABI_enum_size:
9734 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
9736 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
9737 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
9739 // The existing object is compatible with anything.
9740 // Use whatever requirements the new object has.
9741 out_attr[i].set_int_value(in_attr[i].int_value());
9743 // FIXME: Make it possible to turn off this warning.
9744 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
9745 && out_attr[i].int_value() != in_attr[i].int_value()
9746 && parameters->options().warn_mismatch())
9748 unsigned int in_value = in_attr[i].int_value();
9749 unsigned int out_value = out_attr[i].int_value();
9750 gold_warning(_("%s uses %s enums yet the output is to use "
9751 "%s enums; use of enum values across objects "
9754 this->aeabi_enum_name(in_value).c_str(),
9755 this->aeabi_enum_name(out_value).c_str());
9759 case elfcpp::Tag_ABI_VFP_args:
9762 case elfcpp::Tag_ABI_WMMX_args:
9763 if (in_attr[i].int_value() != out_attr[i].int_value()
9764 && parameters->options().warn_mismatch())
9766 gold_error(_("%s uses iWMMXt register arguments, output does "
9771 case Object_attribute::Tag_compatibility:
9772 // Merged in target-independent code.
9774 case elfcpp::Tag_ABI_HardFP_use:
9775 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
9776 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
9777 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
9778 out_attr[i].set_int_value(3);
9779 else if (in_attr[i].int_value() > out_attr[i].int_value())
9780 out_attr[i].set_int_value(in_attr[i].int_value());
9782 case elfcpp::Tag_ABI_FP_16bit_format:
9783 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
9785 if (in_attr[i].int_value() != out_attr[i].int_value()
9786 && parameters->options().warn_mismatch())
9787 gold_error(_("fp16 format mismatch between %s and output"),
9790 if (in_attr[i].int_value() != 0)
9791 out_attr[i].set_int_value(in_attr[i].int_value());
9794 case elfcpp::Tag_DIV_use:
9795 // This tag is set to zero if we can use UDIV and SDIV in Thumb
9796 // mode on a v7-M or v7-R CPU; to one if we can not use UDIV or
9797 // SDIV at all; and to two if we can use UDIV or SDIV on a v7-A
9798 // CPU. We will merge as follows: If the input attribute's value
9799 // is one then the output attribute's value remains unchanged. If
9800 // the input attribute's value is zero or two then if the output
9801 // attribute's value is one the output value is set to the input
9802 // value, otherwise the output value must be the same as the
9804 if (in_attr[i].int_value() != 1 && out_attr[i].int_value() != 1)
9806 if (in_attr[i].int_value() != out_attr[i].int_value())
9808 gold_error(_("DIV usage mismatch between %s and output"),
9813 if (in_attr[i].int_value() != 1)
9814 out_attr[i].set_int_value(in_attr[i].int_value());
9818 case elfcpp::Tag_MPextension_use_legacy:
9819 // We don't output objects with Tag_MPextension_use_legacy - we
9820 // move the value to Tag_MPextension_use.
9821 if (in_attr[i].int_value() != 0
9822 && in_attr[elfcpp::Tag_MPextension_use].int_value() != 0)
9824 if (in_attr[elfcpp::Tag_MPextension_use].int_value()
9825 != in_attr[i].int_value())
9827 gold_error(_("%s has has both the current and legacy "
9828 "Tag_MPextension_use attributes"),
9833 if (in_attr[i].int_value()
9834 > out_attr[elfcpp::Tag_MPextension_use].int_value())
9835 out_attr[elfcpp::Tag_MPextension_use] = in_attr[i];
9839 case elfcpp::Tag_nodefaults:
9840 // This tag is set if it exists, but the value is unused (and is
9841 // typically zero). We don't actually need to do anything here -
9842 // the merge happens automatically when the type flags are merged
9845 case elfcpp::Tag_also_compatible_with:
9846 // Already done in Tag_CPU_arch.
9848 case elfcpp::Tag_conformance:
9849 // Keep the attribute if it matches. Throw it away otherwise.
9850 // No attribute means no claim to conform.
9851 if (in_attr[i].string_value() != out_attr[i].string_value())
9852 out_attr[i].set_string_value("");
9857 const char* err_object = NULL;
9859 // The "known_obj_attributes" table does contain some undefined
9860 // attributes. Ensure that there are unused.
9861 if (out_attr[i].int_value() != 0
9862 || out_attr[i].string_value() != "")
9863 err_object = "output";
9864 else if (in_attr[i].int_value() != 0
9865 || in_attr[i].string_value() != "")
9868 if (err_object != NULL
9869 && parameters->options().warn_mismatch())
9871 // Attribute numbers >=64 (mod 128) can be safely ignored.
9873 gold_error(_("%s: unknown mandatory EABI object attribute "
9877 gold_warning(_("%s: unknown EABI object attribute %d"),
9881 // Only pass on attributes that match in both inputs.
9882 if (!in_attr[i].matches(out_attr[i]))
9884 out_attr[i].set_int_value(0);
9885 out_attr[i].set_string_value("");
9890 // If out_attr was copied from in_attr then it won't have a type yet.
9891 if (in_attr[i].type() && !out_attr[i].type())
9892 out_attr[i].set_type(in_attr[i].type());
9895 // Merge Tag_compatibility attributes and any common GNU ones.
9896 this->attributes_section_data_->merge(name, pasd);
9898 // Check for any attributes not known on ARM.
9899 typedef Vendor_object_attributes::Other_attributes Other_attributes;
9900 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
9901 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
9902 Other_attributes* out_other_attributes =
9903 this->attributes_section_data_->other_attributes(vendor);
9904 Other_attributes::iterator out_iter = out_other_attributes->begin();
9906 while (in_iter != in_other_attributes->end()
9907 || out_iter != out_other_attributes->end())
9909 const char* err_object = NULL;
9912 // The tags for each list are in numerical order.
9913 // If the tags are equal, then merge.
9914 if (out_iter != out_other_attributes->end()
9915 && (in_iter == in_other_attributes->end()
9916 || in_iter->first > out_iter->first))
9918 // This attribute only exists in output. We can't merge, and we
9919 // don't know what the tag means, so delete it.
9920 err_object = "output";
9921 err_tag = out_iter->first;
9922 int saved_tag = out_iter->first;
9923 delete out_iter->second;
9924 out_other_attributes->erase(out_iter);
9925 out_iter = out_other_attributes->upper_bound(saved_tag);
9927 else if (in_iter != in_other_attributes->end()
9928 && (out_iter != out_other_attributes->end()
9929 || in_iter->first < out_iter->first))
9931 // This attribute only exists in input. We can't merge, and we
9932 // don't know what the tag means, so ignore it.
9934 err_tag = in_iter->first;
9937 else // The tags are equal.
9939 // As present, all attributes in the list are unknown, and
9940 // therefore can't be merged meaningfully.
9941 err_object = "output";
9942 err_tag = out_iter->first;
9944 // Only pass on attributes that match in both inputs.
9945 if (!in_iter->second->matches(*(out_iter->second)))
9947 // No match. Delete the attribute.
9948 int saved_tag = out_iter->first;
9949 delete out_iter->second;
9950 out_other_attributes->erase(out_iter);
9951 out_iter = out_other_attributes->upper_bound(saved_tag);
9955 // Matched. Keep the attribute and move to the next.
9961 if (err_object && parameters->options().warn_mismatch())
9963 // Attribute numbers >=64 (mod 128) can be safely ignored. */
9964 if ((err_tag & 127) < 64)
9966 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
9967 err_object, err_tag);
9971 gold_warning(_("%s: unknown EABI object attribute %d"),
9972 err_object, err_tag);
9978 // Stub-generation methods for Target_arm.
9980 // Make a new Arm_input_section object.
9982 template<bool big_endian>
9983 Arm_input_section<big_endian>*
9984 Target_arm<big_endian>::new_arm_input_section(
9988 Section_id sid(relobj, shndx);
9990 Arm_input_section<big_endian>* arm_input_section =
9991 new Arm_input_section<big_endian>(relobj, shndx);
9992 arm_input_section->init();
9994 // Register new Arm_input_section in map for look-up.
9995 std::pair<typename Arm_input_section_map::iterator, bool> ins =
9996 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
9998 // Make sure that it we have not created another Arm_input_section
9999 // for this input section already.
10000 gold_assert(ins.second);
10002 return arm_input_section;
10005 // Find the Arm_input_section object corresponding to the SHNDX-th input
10006 // section of RELOBJ.
10008 template<bool big_endian>
10009 Arm_input_section<big_endian>*
10010 Target_arm<big_endian>::find_arm_input_section(
10012 unsigned int shndx) const
10014 Section_id sid(relobj, shndx);
10015 typename Arm_input_section_map::const_iterator p =
10016 this->arm_input_section_map_.find(sid);
10017 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
10020 // Make a new stub table.
10022 template<bool big_endian>
10023 Stub_table<big_endian>*
10024 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
10026 Stub_table<big_endian>* stub_table =
10027 new Stub_table<big_endian>(owner);
10028 this->stub_tables_.push_back(stub_table);
10030 stub_table->set_address(owner->address() + owner->data_size());
10031 stub_table->set_file_offset(owner->offset() + owner->data_size());
10032 stub_table->finalize_data_size();
10037 // Scan a relocation for stub generation.
10039 template<bool big_endian>
10041 Target_arm<big_endian>::scan_reloc_for_stub(
10042 const Relocate_info<32, big_endian>* relinfo,
10043 unsigned int r_type,
10044 const Sized_symbol<32>* gsym,
10045 unsigned int r_sym,
10046 const Symbol_value<32>* psymval,
10047 elfcpp::Elf_types<32>::Elf_Swxword addend,
10048 Arm_address address)
10050 typedef typename Target_arm<big_endian>::Relocate Relocate;
10052 const Arm_relobj<big_endian>* arm_relobj =
10053 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10055 bool target_is_thumb;
10056 Symbol_value<32> symval;
10059 // This is a global symbol. Determine if we use PLT and if the
10060 // final target is THUMB.
10061 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
10063 // This uses a PLT, change the symbol value.
10064 symval.set_output_value(this->plt_section()->address()
10065 + gsym->plt_offset());
10067 target_is_thumb = false;
10069 else if (gsym->is_undefined())
10070 // There is no need to generate a stub symbol is undefined.
10075 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
10076 || (gsym->type() == elfcpp::STT_FUNC
10077 && !gsym->is_undefined()
10078 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
10083 // This is a local symbol. Determine if the final target is THUMB.
10084 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
10087 // Strip LSB if this points to a THUMB target.
10088 const Arm_reloc_property* reloc_property =
10089 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
10090 gold_assert(reloc_property != NULL);
10091 if (target_is_thumb
10092 && reloc_property->uses_thumb_bit()
10093 && ((psymval->value(arm_relobj, 0) & 1) != 0))
10095 Arm_address stripped_value =
10096 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
10097 symval.set_output_value(stripped_value);
10101 // Get the symbol value.
10102 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
10104 // Owing to pipelining, the PC relative branches below actually skip
10105 // two instructions when the branch offset is 0.
10106 Arm_address destination;
10109 case elfcpp::R_ARM_CALL:
10110 case elfcpp::R_ARM_JUMP24:
10111 case elfcpp::R_ARM_PLT32:
10113 destination = value + addend + 8;
10115 case elfcpp::R_ARM_THM_CALL:
10116 case elfcpp::R_ARM_THM_XPC22:
10117 case elfcpp::R_ARM_THM_JUMP24:
10118 case elfcpp::R_ARM_THM_JUMP19:
10120 destination = value + addend + 4;
10123 gold_unreachable();
10126 Reloc_stub* stub = NULL;
10127 Stub_type stub_type =
10128 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
10130 if (stub_type != arm_stub_none)
10132 // Try looking up an existing stub from a stub table.
10133 Stub_table<big_endian>* stub_table =
10134 arm_relobj->stub_table(relinfo->data_shndx);
10135 gold_assert(stub_table != NULL);
10137 // Locate stub by destination.
10138 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
10140 // Create a stub if there is not one already
10141 stub = stub_table->find_reloc_stub(stub_key);
10144 // create a new stub and add it to stub table.
10145 stub = this->stub_factory().make_reloc_stub(stub_type);
10146 stub_table->add_reloc_stub(stub, stub_key);
10149 // Record the destination address.
10150 stub->set_destination_address(destination
10151 | (target_is_thumb ? 1 : 0));
10154 // For Cortex-A8, we need to record a relocation at 4K page boundary.
10155 if (this->fix_cortex_a8_
10156 && (r_type == elfcpp::R_ARM_THM_JUMP24
10157 || r_type == elfcpp::R_ARM_THM_JUMP19
10158 || r_type == elfcpp::R_ARM_THM_CALL
10159 || r_type == elfcpp::R_ARM_THM_XPC22)
10160 && (address & 0xfffU) == 0xffeU)
10162 // Found a candidate. Note we haven't checked the destination is
10163 // within 4K here: if we do so (and don't create a record) we can't
10164 // tell that a branch should have been relocated when scanning later.
10165 this->cortex_a8_relocs_info_[address] =
10166 new Cortex_a8_reloc(stub, r_type,
10167 destination | (target_is_thumb ? 1 : 0));
10171 // This function scans a relocation sections for stub generation.
10172 // The template parameter Relocate must be a class type which provides
10173 // a single function, relocate(), which implements the machine
10174 // specific part of a relocation.
10176 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
10177 // SHT_REL or SHT_RELA.
10179 // PRELOCS points to the relocation data. RELOC_COUNT is the number
10180 // of relocs. OUTPUT_SECTION is the output section.
10181 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
10182 // mapped to output offsets.
10184 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
10185 // VIEW_SIZE is the size. These refer to the input section, unless
10186 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
10187 // the output section.
10189 template<bool big_endian>
10190 template<int sh_type>
10192 Target_arm<big_endian>::scan_reloc_section_for_stubs(
10193 const Relocate_info<32, big_endian>* relinfo,
10194 const unsigned char* prelocs,
10195 size_t reloc_count,
10196 Output_section* output_section,
10197 bool needs_special_offset_handling,
10198 const unsigned char* view,
10199 elfcpp::Elf_types<32>::Elf_Addr view_address,
10202 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
10203 const int reloc_size =
10204 Reloc_types<sh_type, 32, big_endian>::reloc_size;
10206 Arm_relobj<big_endian>* arm_object =
10207 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10208 unsigned int local_count = arm_object->local_symbol_count();
10210 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
10212 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
10214 Reltype reloc(prelocs);
10216 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10217 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
10218 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
10220 r_type = this->get_real_reloc_type(r_type);
10222 // Only a few relocation types need stubs.
10223 if ((r_type != elfcpp::R_ARM_CALL)
10224 && (r_type != elfcpp::R_ARM_JUMP24)
10225 && (r_type != elfcpp::R_ARM_PLT32)
10226 && (r_type != elfcpp::R_ARM_THM_CALL)
10227 && (r_type != elfcpp::R_ARM_THM_XPC22)
10228 && (r_type != elfcpp::R_ARM_THM_JUMP24)
10229 && (r_type != elfcpp::R_ARM_THM_JUMP19)
10230 && (r_type != elfcpp::R_ARM_V4BX))
10233 section_offset_type offset =
10234 convert_to_section_size_type(reloc.get_r_offset());
10236 if (needs_special_offset_handling)
10238 offset = output_section->output_offset(relinfo->object,
10239 relinfo->data_shndx,
10245 // Create a v4bx stub if --fix-v4bx-interworking is used.
10246 if (r_type == elfcpp::R_ARM_V4BX)
10248 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING)
10250 // Get the BX instruction.
10251 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10252 const Valtype* wv =
10253 reinterpret_cast<const Valtype*>(view + offset);
10254 elfcpp::Elf_types<32>::Elf_Swxword insn =
10255 elfcpp::Swap<32, big_endian>::readval(wv);
10256 const uint32_t reg = (insn & 0xf);
10260 // Try looking up an existing stub from a stub table.
10261 Stub_table<big_endian>* stub_table =
10262 arm_object->stub_table(relinfo->data_shndx);
10263 gold_assert(stub_table != NULL);
10265 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
10267 // create a new stub and add it to stub table.
10268 Arm_v4bx_stub* stub =
10269 this->stub_factory().make_arm_v4bx_stub(reg);
10270 gold_assert(stub != NULL);
10271 stub_table->add_arm_v4bx_stub(stub);
10279 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
10280 elfcpp::Elf_types<32>::Elf_Swxword addend =
10281 stub_addend_reader(r_type, view + offset, reloc);
10283 const Sized_symbol<32>* sym;
10285 Symbol_value<32> symval;
10286 const Symbol_value<32> *psymval;
10287 if (r_sym < local_count)
10290 psymval = arm_object->local_symbol(r_sym);
10292 // If the local symbol belongs to a section we are discarding,
10293 // and that section is a debug section, try to find the
10294 // corresponding kept section and map this symbol to its
10295 // counterpart in the kept section. The symbol must not
10296 // correspond to a section we are folding.
10298 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10300 && shndx != elfcpp::SHN_UNDEF
10301 && !arm_object->is_section_included(shndx)
10302 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
10304 if (comdat_behavior == CB_UNDETERMINED)
10307 arm_object->section_name(relinfo->data_shndx);
10308 comdat_behavior = get_comdat_behavior(name.c_str());
10310 if (comdat_behavior == CB_PRETEND)
10313 typename elfcpp::Elf_types<32>::Elf_Addr value =
10314 arm_object->map_to_kept_section(shndx, &found);
10316 symval.set_output_value(value + psymval->input_value());
10318 symval.set_output_value(0);
10322 symval.set_output_value(0);
10324 symval.set_no_output_symtab_entry();
10330 const Symbol* gsym = arm_object->global_symbol(r_sym);
10331 gold_assert(gsym != NULL);
10332 if (gsym->is_forwarder())
10333 gsym = relinfo->symtab->resolve_forwards(gsym);
10335 sym = static_cast<const Sized_symbol<32>*>(gsym);
10336 if (sym->has_symtab_index())
10337 symval.set_output_symtab_index(sym->symtab_index());
10339 symval.set_no_output_symtab_entry();
10341 // We need to compute the would-be final value of this global
10343 const Symbol_table* symtab = relinfo->symtab;
10344 const Sized_symbol<32>* sized_symbol =
10345 symtab->get_sized_symbol<32>(gsym);
10346 Symbol_table::Compute_final_value_status status;
10347 Arm_address value =
10348 symtab->compute_final_value<32>(sized_symbol, &status);
10350 // Skip this if the symbol has not output section.
10351 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
10354 symval.set_output_value(value);
10358 // If symbol is a section symbol, we don't know the actual type of
10359 // destination. Give up.
10360 if (psymval->is_section_symbol())
10363 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
10364 addend, view_address + offset);
10368 // Scan an input section for stub generation.
10370 template<bool big_endian>
10372 Target_arm<big_endian>::scan_section_for_stubs(
10373 const Relocate_info<32, big_endian>* relinfo,
10374 unsigned int sh_type,
10375 const unsigned char* prelocs,
10376 size_t reloc_count,
10377 Output_section* output_section,
10378 bool needs_special_offset_handling,
10379 const unsigned char* view,
10380 Arm_address view_address,
10381 section_size_type view_size)
10383 if (sh_type == elfcpp::SHT_REL)
10384 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
10389 needs_special_offset_handling,
10393 else if (sh_type == elfcpp::SHT_RELA)
10394 // We do not support RELA type relocations yet. This is provided for
10396 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
10401 needs_special_offset_handling,
10406 gold_unreachable();
10409 // Group input sections for stub generation.
10411 // We goup input sections in an output sections so that the total size,
10412 // including any padding space due to alignment is smaller than GROUP_SIZE
10413 // unless the only input section in group is bigger than GROUP_SIZE already.
10414 // Then an ARM stub table is created to follow the last input section
10415 // in group. For each group an ARM stub table is created an is placed
10416 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
10417 // extend the group after the stub table.
10419 template<bool big_endian>
10421 Target_arm<big_endian>::group_sections(
10423 section_size_type group_size,
10424 bool stubs_always_after_branch)
10426 // Group input sections and insert stub table
10427 Layout::Section_list section_list;
10428 layout->get_allocated_sections(§ion_list);
10429 for (Layout::Section_list::const_iterator p = section_list.begin();
10430 p != section_list.end();
10433 Arm_output_section<big_endian>* output_section =
10434 Arm_output_section<big_endian>::as_arm_output_section(*p);
10435 output_section->group_sections(group_size, stubs_always_after_branch,
10440 // Relaxation hook. This is where we do stub generation.
10442 template<bool big_endian>
10444 Target_arm<big_endian>::do_relax(
10446 const Input_objects* input_objects,
10447 Symbol_table* symtab,
10450 // No need to generate stubs if this is a relocatable link.
10451 gold_assert(!parameters->options().relocatable());
10453 // If this is the first pass, we need to group input sections into
10455 bool done_exidx_fixup = false;
10456 typedef typename Stub_table_list::iterator Stub_table_iterator;
10459 // Determine the stub group size. The group size is the absolute
10460 // value of the parameter --stub-group-size. If --stub-group-size
10461 // is passed a negative value, we restict stubs to be always after
10462 // the stubbed branches.
10463 int32_t stub_group_size_param =
10464 parameters->options().stub_group_size();
10465 bool stubs_always_after_branch = stub_group_size_param < 0;
10466 section_size_type stub_group_size = abs(stub_group_size_param);
10468 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
10469 // page as the first half of a 32-bit branch straddling two 4K pages.
10470 // This is a crude way of enforcing that.
10471 if (this->fix_cortex_a8_)
10472 stubs_always_after_branch = true;
10474 if (stub_group_size == 1)
10477 // Thumb branch range is +-4MB has to be used as the default
10478 // maximum size (a given section can contain both ARM and Thumb
10479 // code, so the worst case has to be taken into account). If we are
10480 // fixing cortex-a8 errata, the branch range has to be even smaller,
10481 // since wide conditional branch has a range of +-1MB only.
10483 // This value is 24K less than that, which allows for 2025
10484 // 12-byte stubs. If we exceed that, then we will fail to link.
10485 // The user will have to relink with an explicit group size
10487 if (this->fix_cortex_a8_)
10488 stub_group_size = 1024276;
10490 stub_group_size = 4170000;
10493 group_sections(layout, stub_group_size, stubs_always_after_branch);
10495 // Also fix .ARM.exidx section coverage.
10496 Output_section* os = layout->find_output_section(".ARM.exidx");
10497 if (os != NULL && os->type() == elfcpp::SHT_ARM_EXIDX)
10499 Arm_output_section<big_endian>* exidx_output_section =
10500 Arm_output_section<big_endian>::as_arm_output_section(os);
10501 this->fix_exidx_coverage(layout, exidx_output_section, symtab);
10502 done_exidx_fixup = true;
10507 // If this is not the first pass, addresses and file offsets have
10508 // been reset at this point, set them here.
10509 for (Stub_table_iterator sp = this->stub_tables_.begin();
10510 sp != this->stub_tables_.end();
10513 Arm_input_section<big_endian>* owner = (*sp)->owner();
10514 off_t off = align_address(owner->original_size(),
10515 (*sp)->addralign());
10516 (*sp)->set_address_and_file_offset(owner->address() + off,
10517 owner->offset() + off);
10521 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
10522 // beginning of each relaxation pass, just blow away all the stubs.
10523 // Alternatively, we could selectively remove only the stubs and reloc
10524 // information for code sections that have moved since the last pass.
10525 // That would require more book-keeping.
10526 if (this->fix_cortex_a8_)
10528 // Clear all Cortex-A8 reloc information.
10529 for (typename Cortex_a8_relocs_info::const_iterator p =
10530 this->cortex_a8_relocs_info_.begin();
10531 p != this->cortex_a8_relocs_info_.end();
10534 this->cortex_a8_relocs_info_.clear();
10536 // Remove all Cortex-A8 stubs.
10537 for (Stub_table_iterator sp = this->stub_tables_.begin();
10538 sp != this->stub_tables_.end();
10540 (*sp)->remove_all_cortex_a8_stubs();
10543 // Scan relocs for relocation stubs
10544 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
10545 op != input_objects->relobj_end();
10548 Arm_relobj<big_endian>* arm_relobj =
10549 Arm_relobj<big_endian>::as_arm_relobj(*op);
10550 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
10553 // Check all stub tables to see if any of them have their data sizes
10554 // or addresses alignments changed. These are the only things that
10556 bool any_stub_table_changed = false;
10557 Unordered_set<const Output_section*> sections_needing_adjustment;
10558 for (Stub_table_iterator sp = this->stub_tables_.begin();
10559 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
10562 if ((*sp)->update_data_size_and_addralign())
10564 // Update data size of stub table owner.
10565 Arm_input_section<big_endian>* owner = (*sp)->owner();
10566 uint64_t address = owner->address();
10567 off_t offset = owner->offset();
10568 owner->reset_address_and_file_offset();
10569 owner->set_address_and_file_offset(address, offset);
10571 sections_needing_adjustment.insert(owner->output_section());
10572 any_stub_table_changed = true;
10576 // Output_section_data::output_section() returns a const pointer but we
10577 // need to update output sections, so we record all output sections needing
10578 // update above and scan the sections here to find out what sections need
10580 for(Layout::Section_list::const_iterator p = layout->section_list().begin();
10581 p != layout->section_list().end();
10584 if (sections_needing_adjustment.find(*p)
10585 != sections_needing_adjustment.end())
10586 (*p)->set_section_offsets_need_adjustment();
10589 // Stop relaxation if no EXIDX fix-up and no stub table change.
10590 bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
10592 // Finalize the stubs in the last relaxation pass.
10593 if (!continue_relaxation)
10595 for (Stub_table_iterator sp = this->stub_tables_.begin();
10596 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
10598 (*sp)->finalize_stubs();
10600 // Update output local symbol counts of objects if necessary.
10601 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
10602 op != input_objects->relobj_end();
10605 Arm_relobj<big_endian>* arm_relobj =
10606 Arm_relobj<big_endian>::as_arm_relobj(*op);
10608 // Update output local symbol counts. We need to discard local
10609 // symbols defined in parts of input sections that are discarded by
10611 if (arm_relobj->output_local_symbol_count_needs_update())
10612 arm_relobj->update_output_local_symbol_count();
10616 return continue_relaxation;
10619 // Relocate a stub.
10621 template<bool big_endian>
10623 Target_arm<big_endian>::relocate_stub(
10625 const Relocate_info<32, big_endian>* relinfo,
10626 Output_section* output_section,
10627 unsigned char* view,
10628 Arm_address address,
10629 section_size_type view_size)
10632 const Stub_template* stub_template = stub->stub_template();
10633 for (size_t i = 0; i < stub_template->reloc_count(); i++)
10635 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
10636 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
10638 unsigned int r_type = insn->r_type();
10639 section_size_type reloc_offset = stub_template->reloc_offset(i);
10640 section_size_type reloc_size = insn->size();
10641 gold_assert(reloc_offset + reloc_size <= view_size);
10643 // This is the address of the stub destination.
10644 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
10645 Symbol_value<32> symval;
10646 symval.set_output_value(target);
10648 // Synthesize a fake reloc just in case. We don't have a symbol so
10650 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
10651 memset(reloc_buffer, 0, sizeof(reloc_buffer));
10652 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
10653 reloc_write.put_r_offset(reloc_offset);
10654 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
10655 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
10657 relocate.relocate(relinfo, this, output_section,
10658 this->fake_relnum_for_stubs, rel, r_type,
10659 NULL, &symval, view + reloc_offset,
10660 address + reloc_offset, reloc_size);
10664 // Determine whether an object attribute tag takes an integer, a
10667 template<bool big_endian>
10669 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
10671 if (tag == Object_attribute::Tag_compatibility)
10672 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10673 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
10674 else if (tag == elfcpp::Tag_nodefaults)
10675 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10676 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
10677 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
10678 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
10680 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
10682 return ((tag & 1) != 0
10683 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
10684 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10687 // Reorder attributes.
10689 // The ABI defines that Tag_conformance should be emitted first, and that
10690 // Tag_nodefaults should be second (if either is defined). This sets those
10691 // two positions, and bumps up the position of all the remaining tags to
10694 template<bool big_endian>
10696 Target_arm<big_endian>::do_attributes_order(int num) const
10698 // Reorder the known object attributes in output. We want to move
10699 // Tag_conformance to position 4 and Tag_conformance to position 5
10700 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
10702 return elfcpp::Tag_conformance;
10704 return elfcpp::Tag_nodefaults;
10705 if ((num - 2) < elfcpp::Tag_nodefaults)
10707 if ((num - 1) < elfcpp::Tag_conformance)
10712 // Scan a span of THUMB code for Cortex-A8 erratum.
10714 template<bool big_endian>
10716 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
10717 Arm_relobj<big_endian>* arm_relobj,
10718 unsigned int shndx,
10719 section_size_type span_start,
10720 section_size_type span_end,
10721 const unsigned char* view,
10722 Arm_address address)
10724 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
10726 // The opcode is BLX.W, BL.W, B.W, Bcc.W
10727 // The branch target is in the same 4KB region as the
10728 // first half of the branch.
10729 // The instruction before the branch is a 32-bit
10730 // length non-branch instruction.
10731 section_size_type i = span_start;
10732 bool last_was_32bit = false;
10733 bool last_was_branch = false;
10734 while (i < span_end)
10736 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10737 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
10738 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
10739 bool is_blx = false, is_b = false;
10740 bool is_bl = false, is_bcc = false;
10742 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
10745 // Load the rest of the insn (in manual-friendly order).
10746 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
10748 // Encoding T4: B<c>.W.
10749 is_b = (insn & 0xf800d000U) == 0xf0009000U;
10750 // Encoding T1: BL<c>.W.
10751 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
10752 // Encoding T2: BLX<c>.W.
10753 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
10754 // Encoding T3: B<c>.W (not permitted in IT block).
10755 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
10756 && (insn & 0x07f00000U) != 0x03800000U);
10759 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
10761 // If this instruction is a 32-bit THUMB branch that crosses a 4K
10762 // page boundary and it follows 32-bit non-branch instruction,
10763 // we need to work around.
10764 if (is_32bit_branch
10765 && ((address + i) & 0xfffU) == 0xffeU
10767 && !last_was_branch)
10769 // Check to see if there is a relocation stub for this branch.
10770 bool force_target_arm = false;
10771 bool force_target_thumb = false;
10772 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
10773 Cortex_a8_relocs_info::const_iterator p =
10774 this->cortex_a8_relocs_info_.find(address + i);
10776 if (p != this->cortex_a8_relocs_info_.end())
10778 cortex_a8_reloc = p->second;
10779 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
10781 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10782 && !target_is_thumb)
10783 force_target_arm = true;
10784 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10785 && target_is_thumb)
10786 force_target_thumb = true;
10790 Stub_type stub_type = arm_stub_none;
10792 // Check if we have an offending branch instruction.
10793 uint16_t upper_insn = (insn >> 16) & 0xffffU;
10794 uint16_t lower_insn = insn & 0xffffU;
10795 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10797 if (cortex_a8_reloc != NULL
10798 && cortex_a8_reloc->reloc_stub() != NULL)
10799 // We've already made a stub for this instruction, e.g.
10800 // it's a long branch or a Thumb->ARM stub. Assume that
10801 // stub will suffice to work around the A8 erratum (see
10802 // setting of always_after_branch above).
10806 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
10808 stub_type = arm_stub_a8_veneer_b_cond;
10810 else if (is_b || is_bl || is_blx)
10812 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
10817 stub_type = (is_blx
10818 ? arm_stub_a8_veneer_blx
10820 ? arm_stub_a8_veneer_bl
10821 : arm_stub_a8_veneer_b));
10824 if (stub_type != arm_stub_none)
10826 Arm_address pc_for_insn = address + i + 4;
10828 // The original instruction is a BL, but the target is
10829 // an ARM instruction. If we were not making a stub,
10830 // the BL would have been converted to a BLX. Use the
10831 // BLX stub instead in that case.
10832 if (this->may_use_blx() && force_target_arm
10833 && stub_type == arm_stub_a8_veneer_bl)
10835 stub_type = arm_stub_a8_veneer_blx;
10839 // Conversely, if the original instruction was
10840 // BLX but the target is Thumb mode, use the BL stub.
10841 else if (force_target_thumb
10842 && stub_type == arm_stub_a8_veneer_blx)
10844 stub_type = arm_stub_a8_veneer_bl;
10852 // If we found a relocation, use the proper destination,
10853 // not the offset in the (unrelocated) instruction.
10854 // Note this is always done if we switched the stub type above.
10855 if (cortex_a8_reloc != NULL)
10856 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
10858 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
10860 // Add a new stub if destination address in in the same page.
10861 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
10863 Cortex_a8_stub* stub =
10864 this->stub_factory_.make_cortex_a8_stub(stub_type,
10868 Stub_table<big_endian>* stub_table =
10869 arm_relobj->stub_table(shndx);
10870 gold_assert(stub_table != NULL);
10871 stub_table->add_cortex_a8_stub(address + i, stub);
10876 i += insn_32bit ? 4 : 2;
10877 last_was_32bit = insn_32bit;
10878 last_was_branch = is_32bit_branch;
10882 // Apply the Cortex-A8 workaround.
10884 template<bool big_endian>
10886 Target_arm<big_endian>::apply_cortex_a8_workaround(
10887 const Cortex_a8_stub* stub,
10888 Arm_address stub_address,
10889 unsigned char* insn_view,
10890 Arm_address insn_address)
10892 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10893 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
10894 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
10895 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
10896 off_t branch_offset = stub_address - (insn_address + 4);
10898 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10899 switch (stub->stub_template()->type())
10901 case arm_stub_a8_veneer_b_cond:
10902 // For a conditional branch, we re-write it to be a uncondition
10903 // branch to the stub. We use the THUMB-2 encoding here.
10904 upper_insn = 0xf000U;
10905 lower_insn = 0xb800U;
10907 case arm_stub_a8_veneer_b:
10908 case arm_stub_a8_veneer_bl:
10909 case arm_stub_a8_veneer_blx:
10910 if ((lower_insn & 0x5000U) == 0x4000U)
10911 // For a BLX instruction, make sure that the relocation is
10912 // rounded up to a word boundary. This follows the semantics of
10913 // the instruction which specifies that bit 1 of the target
10914 // address will come from bit 1 of the base address.
10915 branch_offset = (branch_offset + 2) & ~3;
10917 // Put BRANCH_OFFSET back into the insn.
10918 gold_assert(!utils::has_overflow<25>(branch_offset));
10919 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
10920 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
10924 gold_unreachable();
10927 // Put the relocated value back in the object file:
10928 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
10929 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
10932 template<bool big_endian>
10933 class Target_selector_arm : public Target_selector
10936 Target_selector_arm()
10937 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
10938 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
10942 do_instantiate_target()
10943 { return new Target_arm<big_endian>(); }
10946 // Fix .ARM.exidx section coverage.
10948 template<bool big_endian>
10950 Target_arm<big_endian>::fix_exidx_coverage(
10952 Arm_output_section<big_endian>* exidx_section,
10953 Symbol_table* symtab)
10955 // We need to look at all the input sections in output in ascending
10956 // order of of output address. We do that by building a sorted list
10957 // of output sections by addresses. Then we looks at the output sections
10958 // in order. The input sections in an output section are already sorted
10959 // by addresses within the output section.
10961 typedef std::set<Output_section*, output_section_address_less_than>
10962 Sorted_output_section_list;
10963 Sorted_output_section_list sorted_output_sections;
10964 Layout::Section_list section_list;
10965 layout->get_allocated_sections(§ion_list);
10966 for (Layout::Section_list::const_iterator p = section_list.begin();
10967 p != section_list.end();
10970 // We only care about output sections that contain executable code.
10971 if (((*p)->flags() & elfcpp::SHF_EXECINSTR) != 0)
10972 sorted_output_sections.insert(*p);
10975 // Go over the output sections in ascending order of output addresses.
10976 typedef typename Arm_output_section<big_endian>::Text_section_list
10978 Text_section_list sorted_text_sections;
10979 for(typename Sorted_output_section_list::iterator p =
10980 sorted_output_sections.begin();
10981 p != sorted_output_sections.end();
10984 Arm_output_section<big_endian>* arm_output_section =
10985 Arm_output_section<big_endian>::as_arm_output_section(*p);
10986 arm_output_section->append_text_sections_to_list(&sorted_text_sections);
10989 exidx_section->fix_exidx_coverage(layout, sorted_text_sections, symtab,
10990 merge_exidx_entries());
10993 Target_selector_arm<false> target_selector_arm;
10994 Target_selector_arm<true> target_selector_armbe;
10996 } // End anonymous namespace.