1 // arm.cc -- arm target support for gold.
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
9 // This file is part of gold.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
38 #include "parameters.h"
45 #include "copy-relocs.h"
47 #include "target-reloc.h"
48 #include "target-select.h"
52 #include "attributes.h"
59 template<bool big_endian>
60 class Output_data_plt_arm;
62 template<bool big_endian>
65 template<bool big_endian>
66 class Arm_input_section;
68 class Arm_exidx_cantunwind;
70 class Arm_exidx_merged_section;
72 class Arm_exidx_fixup;
74 template<bool big_endian>
75 class Arm_output_section;
77 class Arm_exidx_input_section;
79 template<bool big_endian>
82 template<bool big_endian>
86 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
88 // Maximum branch offsets for ARM, THUMB and THUMB2.
89 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
90 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
91 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
92 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
93 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
94 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
96 // The arm target class.
98 // This is a very simple port of gold for ARM-EABI. It is intended for
99 // supporting Android only for the time being. Only these relocation types
128 // R_ARM_THM_MOVW_ABS_NC
129 // R_ARM_THM_MOVT_ABS
130 // R_ARM_MOVW_PREL_NC
132 // R_ARM_THM_MOVW_PREL_NC
133 // R_ARM_THM_MOVT_PREL
140 // - Support more relocation types as needed.
141 // - Make PLTs more flexible for different architecture features like
143 // There are probably a lot more.
145 // Instruction template class. This class is similar to the insn_sequence
146 // struct in bfd/elf32-arm.c.
151 // Types of instruction templates.
155 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
156 // templates with class-specific semantics. Currently this is used
157 // only by the Cortex_a8_stub class for handling condition codes in
158 // conditional branches.
159 THUMB16_SPECIAL_TYPE,
165 // Factory methods to create instruction templates in different formats.
167 static const Insn_template
168 thumb16_insn(uint32_t data)
169 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
171 // A Thumb conditional branch, in which the proper condition is inserted
172 // when we build the stub.
173 static const Insn_template
174 thumb16_bcond_insn(uint32_t data)
175 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
177 static const Insn_template
178 thumb32_insn(uint32_t data)
179 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
181 static const Insn_template
182 thumb32_b_insn(uint32_t data, int reloc_addend)
184 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
188 static const Insn_template
189 arm_insn(uint32_t data)
190 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
192 static const Insn_template
193 arm_rel_insn(unsigned data, int reloc_addend)
194 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
196 static const Insn_template
197 data_word(unsigned data, unsigned int r_type, int reloc_addend)
198 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
200 // Accessors. This class is used for read-only objects so no modifiers
205 { return this->data_; }
207 // Return the instruction sequence type of this.
210 { return this->type_; }
212 // Return the ARM relocation type of this.
215 { return this->r_type_; }
219 { return this->reloc_addend_; }
221 // Return size of instruction template in bytes.
225 // Return byte-alignment of instruction template.
230 // We make the constructor private to ensure that only the factory
233 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
234 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
237 // Instruction specific data. This is used to store information like
238 // some of the instruction bits.
240 // Instruction template type.
242 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
243 unsigned int r_type_;
244 // Relocation addend.
245 int32_t reloc_addend_;
248 // Macro for generating code to stub types. One entry per long/short
252 DEF_STUB(long_branch_any_any) \
253 DEF_STUB(long_branch_v4t_arm_thumb) \
254 DEF_STUB(long_branch_thumb_only) \
255 DEF_STUB(long_branch_v4t_thumb_thumb) \
256 DEF_STUB(long_branch_v4t_thumb_arm) \
257 DEF_STUB(short_branch_v4t_thumb_arm) \
258 DEF_STUB(long_branch_any_arm_pic) \
259 DEF_STUB(long_branch_any_thumb_pic) \
260 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
261 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
262 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
263 DEF_STUB(long_branch_thumb_only_pic) \
264 DEF_STUB(a8_veneer_b_cond) \
265 DEF_STUB(a8_veneer_b) \
266 DEF_STUB(a8_veneer_bl) \
267 DEF_STUB(a8_veneer_blx) \
268 DEF_STUB(v4_veneer_bx)
272 #define DEF_STUB(x) arm_stub_##x,
278 // First reloc stub type.
279 arm_stub_reloc_first = arm_stub_long_branch_any_any,
280 // Last reloc stub type.
281 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
283 // First Cortex-A8 stub type.
284 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
285 // Last Cortex-A8 stub type.
286 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
289 arm_stub_type_last = arm_stub_v4_veneer_bx
293 // Stub template class. Templates are meant to be read-only objects.
294 // A stub template for a stub type contains all read-only attributes
295 // common to all stubs of the same type.
300 Stub_template(Stub_type, const Insn_template*, size_t);
308 { return this->type_; }
310 // Return an array of instruction templates.
313 { return this->insns_; }
315 // Return size of template in number of instructions.
318 { return this->insn_count_; }
320 // Return size of template in bytes.
323 { return this->size_; }
325 // Return alignment of the stub template.
328 { return this->alignment_; }
330 // Return whether entry point is in thumb mode.
332 entry_in_thumb_mode() const
333 { return this->entry_in_thumb_mode_; }
335 // Return number of relocations in this template.
338 { return this->relocs_.size(); }
340 // Return index of the I-th instruction with relocation.
342 reloc_insn_index(size_t i) const
344 gold_assert(i < this->relocs_.size());
345 return this->relocs_[i].first;
348 // Return the offset of the I-th instruction with relocation from the
349 // beginning of the stub.
351 reloc_offset(size_t i) const
353 gold_assert(i < this->relocs_.size());
354 return this->relocs_[i].second;
358 // This contains information about an instruction template with a relocation
359 // and its offset from start of stub.
360 typedef std::pair<size_t, section_size_type> Reloc;
362 // A Stub_template may not be copied. We want to share templates as much
364 Stub_template(const Stub_template&);
365 Stub_template& operator=(const Stub_template&);
369 // Points to an array of Insn_templates.
370 const Insn_template* insns_;
371 // Number of Insn_templates in insns_[].
373 // Size of templated instructions in bytes.
375 // Alignment of templated instructions.
377 // Flag to indicate if entry is in thumb mode.
378 bool entry_in_thumb_mode_;
379 // A table of reloc instruction indices and offsets. We can find these by
380 // looking at the instruction templates but we pre-compute and then stash
381 // them here for speed.
382 std::vector<Reloc> relocs_;
386 // A class for code stubs. This is a base class for different type of
387 // stubs used in the ARM target.
393 static const section_offset_type invalid_offset =
394 static_cast<section_offset_type>(-1);
397 Stub(const Stub_template* stub_template)
398 : stub_template_(stub_template), offset_(invalid_offset)
405 // Return the stub template.
407 stub_template() const
408 { return this->stub_template_; }
410 // Return offset of code stub from beginning of its containing stub table.
414 gold_assert(this->offset_ != invalid_offset);
415 return this->offset_;
418 // Set offset of code stub from beginning of its containing stub table.
420 set_offset(section_offset_type offset)
421 { this->offset_ = offset; }
423 // Return the relocation target address of the i-th relocation in the
424 // stub. This must be defined in a child class.
426 reloc_target(size_t i)
427 { return this->do_reloc_target(i); }
429 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
431 write(unsigned char* view, section_size_type view_size, bool big_endian)
432 { this->do_write(view, view_size, big_endian); }
434 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
435 // for the i-th instruction.
437 thumb16_special(size_t i)
438 { return this->do_thumb16_special(i); }
441 // This must be defined in the child class.
443 do_reloc_target(size_t) = 0;
445 // This may be overridden in the child class.
447 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
450 this->do_fixed_endian_write<true>(view, view_size);
452 this->do_fixed_endian_write<false>(view, view_size);
455 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
456 // instruction template.
458 do_thumb16_special(size_t)
459 { gold_unreachable(); }
462 // A template to implement do_write.
463 template<bool big_endian>
465 do_fixed_endian_write(unsigned char*, section_size_type);
468 const Stub_template* stub_template_;
469 // Offset within the section of containing this stub.
470 section_offset_type offset_;
473 // Reloc stub class. These are stubs we use to fix up relocation because
474 // of limited branch ranges.
476 class Reloc_stub : public Stub
479 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
480 // We assume we never jump to this address.
481 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
483 // Return destination address.
485 destination_address() const
487 gold_assert(this->destination_address_ != this->invalid_address);
488 return this->destination_address_;
491 // Set destination address.
493 set_destination_address(Arm_address address)
495 gold_assert(address != this->invalid_address);
496 this->destination_address_ = address;
499 // Reset destination address.
501 reset_destination_address()
502 { this->destination_address_ = this->invalid_address; }
504 // Determine stub type for a branch of a relocation of R_TYPE going
505 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
506 // the branch target is a thumb instruction. TARGET is used for look
507 // up ARM-specific linker settings.
509 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
510 Arm_address branch_target, bool target_is_thumb);
512 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
513 // and an addend. Since we treat global and local symbol differently, we
514 // use a Symbol object for a global symbol and a object-index pair for
519 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
520 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
521 // and R_SYM must not be invalid_index.
522 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
523 unsigned int r_sym, int32_t addend)
524 : stub_type_(stub_type), addend_(addend)
528 this->r_sym_ = Reloc_stub::invalid_index;
529 this->u_.symbol = symbol;
533 gold_assert(relobj != NULL && r_sym != invalid_index);
534 this->r_sym_ = r_sym;
535 this->u_.relobj = relobj;
542 // Accessors: Keys are meant to be read-only object so no modifiers are
548 { return this->stub_type_; }
550 // Return the local symbol index or invalid_index.
553 { return this->r_sym_; }
555 // Return the symbol if there is one.
558 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
560 // Return the relobj if there is one.
563 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
565 // Whether this equals to another key k.
567 eq(const Key& k) const
569 return ((this->stub_type_ == k.stub_type_)
570 && (this->r_sym_ == k.r_sym_)
571 && ((this->r_sym_ != Reloc_stub::invalid_index)
572 ? (this->u_.relobj == k.u_.relobj)
573 : (this->u_.symbol == k.u_.symbol))
574 && (this->addend_ == k.addend_));
577 // Return a hash value.
581 return (this->stub_type_
583 ^ gold::string_hash<char>(
584 (this->r_sym_ != Reloc_stub::invalid_index)
585 ? this->u_.relobj->name().c_str()
586 : this->u_.symbol->name())
590 // Functors for STL associative containers.
594 operator()(const Key& k) const
595 { return k.hash_value(); }
601 operator()(const Key& k1, const Key& k2) const
602 { return k1.eq(k2); }
605 // Name of key. This is mainly for debugging.
611 Stub_type stub_type_;
612 // If this is a local symbol, this is the index in the defining object.
613 // Otherwise, it is invalid_index for a global symbol.
615 // If r_sym_ is invalid index. This points to a global symbol.
616 // Otherwise, this points a relobj. We used the unsized and target
617 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
618 // Arm_relobj. This is done to avoid making the stub class a template
619 // as most of the stub machinery is endianity-neutral. However, it
620 // may require a bit of casting done by users of this class.
623 const Symbol* symbol;
624 const Relobj* relobj;
626 // Addend associated with a reloc.
631 // Reloc_stubs are created via a stub factory. So these are protected.
632 Reloc_stub(const Stub_template* stub_template)
633 : Stub(stub_template), destination_address_(invalid_address)
639 friend class Stub_factory;
641 // Return the relocation target address of the i-th relocation in the
644 do_reloc_target(size_t i)
646 // All reloc stub have only one relocation.
648 return this->destination_address_;
652 // Address of destination.
653 Arm_address destination_address_;
656 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
657 // THUMB branch that meets the following conditions:
659 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
660 // branch address is 0xffe.
661 // 2. The branch target address is in the same page as the first word of the
663 // 3. The branch follows a 32-bit instruction which is not a branch.
665 // To do the fix up, we need to store the address of the branch instruction
666 // and its target at least. We also need to store the original branch
667 // instruction bits for the condition code in a conditional branch. The
668 // condition code is used in a special instruction template. We also want
669 // to identify input sections needing Cortex-A8 workaround quickly. We store
670 // extra information about object and section index of the code section
671 // containing a branch being fixed up. The information is used to mark
672 // the code section when we finalize the Cortex-A8 stubs.
675 class Cortex_a8_stub : public Stub
681 // Return the object of the code section containing the branch being fixed
685 { return this->relobj_; }
687 // Return the section index of the code section containing the branch being
691 { return this->shndx_; }
693 // Return the source address of stub. This is the address of the original
694 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
697 source_address() const
698 { return this->source_address_; }
700 // Return the destination address of the stub. This is the branch taken
701 // address of the original branch instruction. LSB is 1 if it is a THUMB
702 // instruction address.
704 destination_address() const
705 { return this->destination_address_; }
707 // Return the instruction being fixed up.
709 original_insn() const
710 { return this->original_insn_; }
713 // Cortex_a8_stubs are created via a stub factory. So these are protected.
714 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
715 unsigned int shndx, Arm_address source_address,
716 Arm_address destination_address, uint32_t original_insn)
717 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
718 source_address_(source_address | 1U),
719 destination_address_(destination_address),
720 original_insn_(original_insn)
723 friend class Stub_factory;
725 // Return the relocation target address of the i-th relocation in the
728 do_reloc_target(size_t i)
730 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
732 // The conditional branch veneer has two relocations.
734 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
738 // All other Cortex-A8 stubs have only one relocation.
740 return this->destination_address_;
744 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
746 do_thumb16_special(size_t);
749 // Object of the code section containing the branch being fixed up.
751 // Section index of the code section containing the branch begin fixed up.
753 // Source address of original branch.
754 Arm_address source_address_;
755 // Destination address of the original branch.
756 Arm_address destination_address_;
757 // Original branch instruction. This is needed for copying the condition
758 // code from a condition branch to its stub.
759 uint32_t original_insn_;
762 // ARMv4 BX Rx branch relocation stub class.
763 class Arm_v4bx_stub : public Stub
769 // Return the associated register.
772 { return this->reg_; }
775 // Arm V4BX stubs are created via a stub factory. So these are protected.
776 Arm_v4bx_stub(const Stub_template* stub_template, const uint32_t reg)
777 : Stub(stub_template), reg_(reg)
780 friend class Stub_factory;
782 // Return the relocation target address of the i-th relocation in the
785 do_reloc_target(size_t)
786 { gold_unreachable(); }
788 // This may be overridden in the child class.
790 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
793 this->do_fixed_endian_v4bx_write<true>(view, view_size);
795 this->do_fixed_endian_v4bx_write<false>(view, view_size);
799 // A template to implement do_write.
800 template<bool big_endian>
802 do_fixed_endian_v4bx_write(unsigned char* view, section_size_type)
804 const Insn_template* insns = this->stub_template()->insns();
805 elfcpp::Swap<32, big_endian>::writeval(view,
807 + (this->reg_ << 16)));
808 view += insns[0].size();
809 elfcpp::Swap<32, big_endian>::writeval(view,
810 (insns[1].data() + this->reg_));
811 view += insns[1].size();
812 elfcpp::Swap<32, big_endian>::writeval(view,
813 (insns[2].data() + this->reg_));
816 // A register index (r0-r14), which is associated with the stub.
820 // Stub factory class.
825 // Return the unique instance of this class.
826 static const Stub_factory&
829 static Stub_factory singleton;
833 // Make a relocation stub.
835 make_reloc_stub(Stub_type stub_type) const
837 gold_assert(stub_type >= arm_stub_reloc_first
838 && stub_type <= arm_stub_reloc_last);
839 return new Reloc_stub(this->stub_templates_[stub_type]);
842 // Make a Cortex-A8 stub.
844 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
845 Arm_address source, Arm_address destination,
846 uint32_t original_insn) const
848 gold_assert(stub_type >= arm_stub_cortex_a8_first
849 && stub_type <= arm_stub_cortex_a8_last);
850 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
851 source, destination, original_insn);
854 // Make an ARM V4BX relocation stub.
855 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
857 make_arm_v4bx_stub(uint32_t reg) const
859 gold_assert(reg < 0xf);
860 return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
865 // Constructor and destructor are protected since we only return a single
866 // instance created in Stub_factory::get_instance().
870 // A Stub_factory may not be copied since it is a singleton.
871 Stub_factory(const Stub_factory&);
872 Stub_factory& operator=(Stub_factory&);
874 // Stub templates. These are initialized in the constructor.
875 const Stub_template* stub_templates_[arm_stub_type_last+1];
878 // A class to hold stubs for the ARM target.
880 template<bool big_endian>
881 class Stub_table : public Output_data
884 Stub_table(Arm_input_section<big_endian>* owner)
885 : Output_data(), owner_(owner), reloc_stubs_(), cortex_a8_stubs_(),
886 arm_v4bx_stubs_(0xf), prev_data_size_(0), prev_addralign_(1)
892 // Owner of this stub table.
893 Arm_input_section<big_endian>*
895 { return this->owner_; }
897 // Whether this stub table is empty.
901 return (this->reloc_stubs_.empty()
902 && this->cortex_a8_stubs_.empty()
903 && this->arm_v4bx_stubs_.empty());
906 // Return the current data size.
908 current_data_size() const
909 { return this->current_data_size_for_child(); }
911 // Add a STUB with using KEY. Caller is reponsible for avoid adding
912 // if already a STUB with the same key has been added.
914 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
916 const Stub_template* stub_template = stub->stub_template();
917 gold_assert(stub_template->type() == key.stub_type());
918 this->reloc_stubs_[key] = stub;
921 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
922 // Caller is reponsible for avoid adding if already a STUB with the same
923 // address has been added.
925 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
927 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
928 this->cortex_a8_stubs_.insert(value);
931 // Add an ARM V4BX relocation stub. A register index will be retrieved
934 add_arm_v4bx_stub(Arm_v4bx_stub* stub)
936 gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
937 this->arm_v4bx_stubs_[stub->reg()] = stub;
940 // Remove all Cortex-A8 stubs.
942 remove_all_cortex_a8_stubs();
944 // Look up a relocation stub using KEY. Return NULL if there is none.
946 find_reloc_stub(const Reloc_stub::Key& key) const
948 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
949 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
952 // Look up an arm v4bx relocation stub using the register index.
953 // Return NULL if there is none.
955 find_arm_v4bx_stub(const uint32_t reg) const
957 gold_assert(reg < 0xf);
958 return this->arm_v4bx_stubs_[reg];
961 // Relocate stubs in this stub table.
963 relocate_stubs(const Relocate_info<32, big_endian>*,
964 Target_arm<big_endian>*, Output_section*,
965 unsigned char*, Arm_address, section_size_type);
967 // Update data size and alignment at the end of a relaxation pass. Return
968 // true if either data size or alignment is different from that of the
969 // previous relaxation pass.
971 update_data_size_and_addralign();
973 // Finalize stubs. Set the offsets of all stubs and mark input sections
974 // needing the Cortex-A8 workaround.
978 // Apply Cortex-A8 workaround to an address range.
980 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
981 unsigned char*, Arm_address,
985 // Write out section contents.
987 do_write(Output_file*);
989 // Return the required alignment.
992 { return this->prev_addralign_; }
994 // Reset address and file offset.
996 do_reset_address_and_file_offset()
997 { this->set_current_data_size_for_child(this->prev_data_size_); }
999 // Set final data size.
1001 set_final_data_size()
1002 { this->set_data_size(this->current_data_size()); }
1005 // Relocate one stub.
1007 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1008 Target_arm<big_endian>*, Output_section*,
1009 unsigned char*, Arm_address, section_size_type);
1011 // Unordered map of relocation stubs.
1013 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
1014 Reloc_stub::Key::equal_to>
1017 // List of Cortex-A8 stubs ordered by addresses of branches being
1018 // fixed up in output.
1019 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
1020 // List of Arm V4BX relocation stubs ordered by associated registers.
1021 typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
1023 // Owner of this stub table.
1024 Arm_input_section<big_endian>* owner_;
1025 // The relocation stubs.
1026 Reloc_stub_map reloc_stubs_;
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 endianity.
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); }
1168 // Write data to output file.
1170 do_write(Output_file*);
1172 // Return required alignment of this.
1174 do_addralign() const
1176 if (this->is_stub_table_owner())
1177 return std::max(this->stub_table_->addralign(),
1178 this->original_addralign_);
1180 return this->original_addralign_;
1183 // Finalize data size.
1185 set_final_data_size();
1187 // Reset address and file offset.
1189 do_reset_address_and_file_offset();
1193 do_output_offset(const Relobj* object, unsigned int shndx,
1194 section_offset_type offset,
1195 section_offset_type* poutput) const
1197 if ((object == this->relobj())
1198 && (shndx == this->shndx())
1200 && (convert_types<uint64_t, section_offset_type>(offset)
1201 <= this->original_size_))
1211 // Copying is not allowed.
1212 Arm_input_section(const Arm_input_section&);
1213 Arm_input_section& operator=(const Arm_input_section&);
1215 // Address alignment of the original input section.
1216 uint64_t original_addralign_;
1217 // Section size of the original input section.
1218 uint64_t original_size_;
1220 Stub_table<big_endian>* stub_table_;
1223 // Arm_exidx_fixup class. This is used to define a number of methods
1224 // and keep states for fixing up EXIDX coverage.
1226 class Arm_exidx_fixup
1229 Arm_exidx_fixup(Output_section* exidx_output_section)
1230 : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1231 last_inlined_entry_(0), last_input_section_(NULL),
1232 section_offset_map_(NULL)
1236 { delete this->section_offset_map_; }
1238 // Process an EXIDX section for entry merging. Return number of bytes to
1239 // be deleted in output. If parts of the input EXIDX section are merged
1240 // a heap allocated Arm_exidx_section_offset_map is store in the located
1241 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1243 template<bool big_endian>
1245 process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1246 Arm_exidx_section_offset_map** psection_offset_map);
1248 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1249 // input section, if there is not one already.
1251 add_exidx_cantunwind_as_needed();
1254 // Copying is not allowed.
1255 Arm_exidx_fixup(const Arm_exidx_fixup&);
1256 Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1258 // Type of EXIDX unwind entry.
1263 // EXIDX_CANTUNWIND.
1264 UT_EXIDX_CANTUNWIND,
1271 // Process an EXIDX entry. We only care about the second word of the
1272 // entry. Return true if the entry can be deleted.
1274 process_exidx_entry(uint32_t second_word);
1276 // Update the current section offset map during EXIDX section fix-up.
1277 // If there is no map, create one. INPUT_OFFSET is the offset of a
1278 // reference point, DELETED_BYTES is the number of deleted by in the
1279 // section so far. If DELETE_ENTRY is true, the reference point and
1280 // all offsets after the previous reference point are discarded.
1282 update_offset_map(section_offset_type input_offset,
1283 section_size_type deleted_bytes, bool delete_entry);
1285 // EXIDX output section.
1286 Output_section* exidx_output_section_;
1287 // Unwind type of the last EXIDX entry processed.
1288 Unwind_type last_unwind_type_;
1289 // Last seen inlined EXIDX entry.
1290 uint32_t last_inlined_entry_;
1291 // Last processed EXIDX input section.
1292 const Arm_exidx_input_section* last_input_section_;
1293 // Section offset map created in process_exidx_section.
1294 Arm_exidx_section_offset_map* section_offset_map_;
1297 // Arm output section class. This is defined mainly to add a number of
1298 // stub generation methods.
1300 template<bool big_endian>
1301 class Arm_output_section : public Output_section
1304 typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
1306 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1307 elfcpp::Elf_Xword flags)
1308 : Output_section(name, type, flags)
1311 ~Arm_output_section()
1314 // Group input sections for stub generation.
1316 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1318 // Downcast a base pointer to an Arm_output_section pointer. This is
1319 // not type-safe but we only use Arm_output_section not the base class.
1320 static Arm_output_section<big_endian>*
1321 as_arm_output_section(Output_section* os)
1322 { return static_cast<Arm_output_section<big_endian>*>(os); }
1324 // Append all input text sections in this into LIST.
1326 append_text_sections_to_list(Text_section_list* list);
1328 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1329 // is a list of text input sections sorted in ascending order of their
1330 // output addresses.
1332 fix_exidx_coverage(const Text_section_list& sorted_text_section,
1333 Symbol_table* symtab);
1337 typedef Output_section::Input_section Input_section;
1338 typedef Output_section::Input_section_list Input_section_list;
1340 // Create a stub group.
1341 void create_stub_group(Input_section_list::const_iterator,
1342 Input_section_list::const_iterator,
1343 Input_section_list::const_iterator,
1344 Target_arm<big_endian>*,
1345 std::vector<Output_relaxed_input_section*>*);
1348 // Arm_exidx_input_section class. This represents an EXIDX input section.
1350 class Arm_exidx_input_section
1353 static const section_offset_type invalid_offset =
1354 static_cast<section_offset_type>(-1);
1356 Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1357 unsigned int link, uint32_t size, uint32_t addralign)
1358 : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1359 addralign_(addralign)
1362 ~Arm_exidx_input_section()
1365 // Accessors: This is a read-only class.
1367 // Return the object containing this EXIDX input section.
1370 { return this->relobj_; }
1372 // Return the section index of this EXIDX input section.
1375 { return this->shndx_; }
1377 // Return the section index of linked text section in the same object.
1380 { return this->link_; }
1382 // Return size of the EXIDX input section.
1385 { return this->size_; }
1387 // Reutnr address alignment of EXIDX input section.
1390 { return this->addralign_; }
1393 // Object containing this.
1395 // Section index of this.
1396 unsigned int shndx_;
1397 // text section linked to this in the same object.
1399 // Size of this. For ARM 32-bit is sufficient.
1401 // Address alignment of this. For ARM 32-bit is sufficient.
1402 uint32_t addralign_;
1405 // Arm_relobj class.
1407 template<bool big_endian>
1408 class Arm_relobj : public Sized_relobj<32, big_endian>
1411 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1413 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1414 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1415 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1416 stub_tables_(), local_symbol_is_thumb_function_(),
1417 attributes_section_data_(NULL), mapping_symbols_info_(),
1418 section_has_cortex_a8_workaround_(NULL), exidx_section_map_()
1422 { delete this->attributes_section_data_; }
1424 // Return the stub table of the SHNDX-th section if there is one.
1425 Stub_table<big_endian>*
1426 stub_table(unsigned int shndx) const
1428 gold_assert(shndx < this->stub_tables_.size());
1429 return this->stub_tables_[shndx];
1432 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1434 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1436 gold_assert(shndx < this->stub_tables_.size());
1437 this->stub_tables_[shndx] = stub_table;
1440 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1441 // index. This is only valid after do_count_local_symbol is called.
1443 local_symbol_is_thumb_function(unsigned int r_sym) const
1445 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1446 return this->local_symbol_is_thumb_function_[r_sym];
1449 // Scan all relocation sections for stub generation.
1451 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1454 // Convert regular input section with index SHNDX to a relaxed section.
1456 convert_input_section_to_relaxed_section(unsigned shndx)
1458 // The stubs have relocations and we need to process them after writing
1459 // out the stubs. So relocation now must follow section write.
1460 this->set_section_offset(shndx, -1ULL);
1461 this->set_relocs_must_follow_section_writes();
1464 // Downcast a base pointer to an Arm_relobj pointer. This is
1465 // not type-safe but we only use Arm_relobj not the base class.
1466 static Arm_relobj<big_endian>*
1467 as_arm_relobj(Relobj* relobj)
1468 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1470 // Processor-specific flags in ELF file header. This is valid only after
1473 processor_specific_flags() const
1474 { return this->processor_specific_flags_; }
1476 // Attribute section data This is the contents of the .ARM.attribute section
1478 const Attributes_section_data*
1479 attributes_section_data() const
1480 { return this->attributes_section_data_; }
1482 // Mapping symbol location.
1483 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1485 // Functor for STL container.
1486 struct Mapping_symbol_position_less
1489 operator()(const Mapping_symbol_position& p1,
1490 const Mapping_symbol_position& p2) const
1492 return (p1.first < p2.first
1493 || (p1.first == p2.first && p1.second < p2.second));
1497 // We only care about the first character of a mapping symbol, so
1498 // we only store that instead of the whole symbol name.
1499 typedef std::map<Mapping_symbol_position, char,
1500 Mapping_symbol_position_less> Mapping_symbols_info;
1502 // Whether a section contains any Cortex-A8 workaround.
1504 section_has_cortex_a8_workaround(unsigned int shndx) const
1506 return (this->section_has_cortex_a8_workaround_ != NULL
1507 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1510 // Mark a section that has Cortex-A8 workaround.
1512 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1514 if (this->section_has_cortex_a8_workaround_ == NULL)
1515 this->section_has_cortex_a8_workaround_ =
1516 new std::vector<bool>(this->shnum(), false);
1517 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1520 // Return the EXIDX section of an text section with index SHNDX or NULL
1521 // if the text section has no associated EXIDX section.
1522 const Arm_exidx_input_section*
1523 exidx_input_section_by_link(unsigned int shndx) const
1525 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1526 return ((p != this->exidx_section_map_.end()
1527 && p->second->link() == shndx)
1532 // Return the EXIDX section with index SHNDX or NULL if there is none.
1533 const Arm_exidx_input_section*
1534 exidx_input_section_by_shndx(unsigned shndx) const
1536 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1537 return ((p != this->exidx_section_map_.end()
1538 && p->second->shndx() == shndx)
1544 // Post constructor setup.
1548 // Call parent's setup method.
1549 Sized_relobj<32, big_endian>::do_setup();
1551 // Initialize look-up tables.
1552 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1553 this->stub_tables_.swap(empty_stub_table_list);
1556 // Count the local symbols.
1558 do_count_local_symbols(Stringpool_template<char>*,
1559 Stringpool_template<char>*);
1562 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1563 const unsigned char* pshdrs,
1564 typename Sized_relobj<32, big_endian>::Views* pivews);
1566 // Read the symbol information.
1568 do_read_symbols(Read_symbols_data* sd);
1570 // Process relocs for garbage collection.
1572 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1576 // Whether a section needs to be scanned for relocation stubs.
1578 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1579 const Relobj::Output_sections&,
1580 const Symbol_table *, const unsigned char*);
1582 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1584 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1585 unsigned int, Output_section*,
1586 const Symbol_table *);
1588 // Scan a section for the Cortex-A8 erratum.
1590 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1591 unsigned int, Output_section*,
1592 Target_arm<big_endian>*);
1594 // Make a new Arm_exidx_input_section object for EXIDX section with
1595 // index SHNDX and section header SHDR.
1597 make_exidx_input_section(unsigned int shndx,
1598 const elfcpp::Shdr<32, big_endian>& shdr);
1600 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1601 typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1604 // List of stub tables.
1605 Stub_table_list stub_tables_;
1606 // Bit vector to tell if a local symbol is a thumb function or not.
1607 // This is only valid after do_count_local_symbol is called.
1608 std::vector<bool> local_symbol_is_thumb_function_;
1609 // processor-specific flags in ELF file header.
1610 elfcpp::Elf_Word processor_specific_flags_;
1611 // Object attributes if there is an .ARM.attributes section or NULL.
1612 Attributes_section_data* attributes_section_data_;
1613 // Mapping symbols information.
1614 Mapping_symbols_info mapping_symbols_info_;
1615 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1616 std::vector<bool>* section_has_cortex_a8_workaround_;
1617 // Map a text section to its associated .ARM.exidx section, if there is one.
1618 Exidx_section_map exidx_section_map_;
1621 // Arm_dynobj class.
1623 template<bool big_endian>
1624 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1627 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1628 const elfcpp::Ehdr<32, big_endian>& ehdr)
1629 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1630 processor_specific_flags_(0), attributes_section_data_(NULL)
1634 { delete this->attributes_section_data_; }
1636 // Downcast a base pointer to an Arm_relobj pointer. This is
1637 // not type-safe but we only use Arm_relobj not the base class.
1638 static Arm_dynobj<big_endian>*
1639 as_arm_dynobj(Dynobj* dynobj)
1640 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1642 // Processor-specific flags in ELF file header. This is valid only after
1645 processor_specific_flags() const
1646 { return this->processor_specific_flags_; }
1648 // Attributes section data.
1649 const Attributes_section_data*
1650 attributes_section_data() const
1651 { return this->attributes_section_data_; }
1654 // Read the symbol information.
1656 do_read_symbols(Read_symbols_data* sd);
1659 // processor-specific flags in ELF file header.
1660 elfcpp::Elf_Word processor_specific_flags_;
1661 // Object attributes if there is an .ARM.attributes section or NULL.
1662 Attributes_section_data* attributes_section_data_;
1665 // Functor to read reloc addends during stub generation.
1667 template<int sh_type, bool big_endian>
1668 struct Stub_addend_reader
1670 // Return the addend for a relocation of a particular type. Depending
1671 // on whether this is a REL or RELA relocation, read the addend from a
1672 // view or from a Reloc object.
1673 elfcpp::Elf_types<32>::Elf_Swxword
1675 unsigned int /* r_type */,
1676 const unsigned char* /* view */,
1677 const typename Reloc_types<sh_type,
1678 32, big_endian>::Reloc& /* reloc */) const;
1681 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1683 template<bool big_endian>
1684 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1686 elfcpp::Elf_types<32>::Elf_Swxword
1689 const unsigned char*,
1690 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1693 // Specialized Stub_addend_reader for RELA type relocation sections.
1694 // We currently do not handle RELA type relocation sections but it is trivial
1695 // to implement the addend reader. This is provided for completeness and to
1696 // make it easier to add support for RELA relocation sections in the future.
1698 template<bool big_endian>
1699 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1701 elfcpp::Elf_types<32>::Elf_Swxword
1704 const unsigned char*,
1705 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1706 big_endian>::Reloc& reloc) const
1707 { return reloc.get_r_addend(); }
1710 // Cortex_a8_reloc class. We keep record of relocation that may need
1711 // the Cortex-A8 erratum workaround.
1713 class Cortex_a8_reloc
1716 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1717 Arm_address destination)
1718 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1724 // Accessors: This is a read-only class.
1726 // Return the relocation stub associated with this relocation if there is
1730 { return this->reloc_stub_; }
1732 // Return the relocation type.
1735 { return this->r_type_; }
1737 // Return the destination address of the relocation. LSB stores the THUMB
1741 { return this->destination_; }
1744 // Associated relocation stub if there is one, or NULL.
1745 const Reloc_stub* reloc_stub_;
1747 unsigned int r_type_;
1748 // Destination address of this relocation. LSB is used to distinguish
1750 Arm_address destination_;
1753 // Utilities for manipulating integers of up to 32-bits
1757 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1758 // an int32_t. NO_BITS must be between 1 to 32.
1759 template<int no_bits>
1760 static inline int32_t
1761 sign_extend(uint32_t bits)
1763 gold_assert(no_bits >= 0 && no_bits <= 32);
1765 return static_cast<int32_t>(bits);
1766 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1768 uint32_t top_bit = 1U << (no_bits - 1);
1769 int32_t as_signed = static_cast<int32_t>(bits);
1770 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1773 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1774 template<int no_bits>
1776 has_overflow(uint32_t bits)
1778 gold_assert(no_bits >= 0 && no_bits <= 32);
1781 int32_t max = (1 << (no_bits - 1)) - 1;
1782 int32_t min = -(1 << (no_bits - 1));
1783 int32_t as_signed = static_cast<int32_t>(bits);
1784 return as_signed > max || as_signed < min;
1787 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1788 // fits in the given number of bits as either a signed or unsigned value.
1789 // For example, has_signed_unsigned_overflow<8> would check
1790 // -128 <= bits <= 255
1791 template<int no_bits>
1793 has_signed_unsigned_overflow(uint32_t bits)
1795 gold_assert(no_bits >= 2 && no_bits <= 32);
1798 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1799 int32_t min = -(1 << (no_bits - 1));
1800 int32_t as_signed = static_cast<int32_t>(bits);
1801 return as_signed > max || as_signed < min;
1804 // Select bits from A and B using bits in MASK. For each n in [0..31],
1805 // the n-th bit in the result is chosen from the n-th bits of A and B.
1806 // A zero selects A and a one selects B.
1807 static inline uint32_t
1808 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1809 { return (a & ~mask) | (b & mask); }
1812 template<bool big_endian>
1813 class Target_arm : public Sized_target<32, big_endian>
1816 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1819 // When were are relocating a stub, we pass this as the relocation number.
1820 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1823 : Sized_target<32, big_endian>(&arm_info),
1824 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1825 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1826 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1827 should_force_pic_veneer_(false), arm_input_section_map_(),
1828 attributes_section_data_(NULL), fix_cortex_a8_(false),
1829 cortex_a8_relocs_info_()
1832 // Whether we can use BLX.
1835 { return this->may_use_blx_; }
1837 // Set use-BLX flag.
1839 set_may_use_blx(bool value)
1840 { this->may_use_blx_ = value; }
1842 // Whether we force PCI branch veneers.
1844 should_force_pic_veneer() const
1845 { return this->should_force_pic_veneer_; }
1847 // Set PIC veneer flag.
1849 set_should_force_pic_veneer(bool value)
1850 { this->should_force_pic_veneer_ = value; }
1852 // Whether we use THUMB-2 instructions.
1854 using_thumb2() const
1856 Object_attribute* attr =
1857 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1858 int arch = attr->int_value();
1859 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
1862 // Whether we use THUMB/THUMB-2 instructions only.
1864 using_thumb_only() const
1866 Object_attribute* attr =
1867 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1868 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1869 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1871 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1872 return attr->int_value() == 'M';
1875 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1877 may_use_arm_nop() const
1879 Object_attribute* attr =
1880 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1881 int arch = attr->int_value();
1882 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1883 || arch == elfcpp::TAG_CPU_ARCH_V6K
1884 || arch == elfcpp::TAG_CPU_ARCH_V7
1885 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1888 // Whether we have THUMB-2 NOP.W instruction.
1890 may_use_thumb2_nop() const
1892 Object_attribute* attr =
1893 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1894 int arch = attr->int_value();
1895 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1896 || arch == elfcpp::TAG_CPU_ARCH_V7
1897 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1900 // Process the relocations to determine unreferenced sections for
1901 // garbage collection.
1903 gc_process_relocs(Symbol_table* symtab,
1905 Sized_relobj<32, big_endian>* object,
1906 unsigned int data_shndx,
1907 unsigned int sh_type,
1908 const unsigned char* prelocs,
1910 Output_section* output_section,
1911 bool needs_special_offset_handling,
1912 size_t local_symbol_count,
1913 const unsigned char* plocal_symbols);
1915 // Scan the relocations to look for symbol adjustments.
1917 scan_relocs(Symbol_table* symtab,
1919 Sized_relobj<32, big_endian>* object,
1920 unsigned int data_shndx,
1921 unsigned int sh_type,
1922 const unsigned char* prelocs,
1924 Output_section* output_section,
1925 bool needs_special_offset_handling,
1926 size_t local_symbol_count,
1927 const unsigned char* plocal_symbols);
1929 // Finalize the sections.
1931 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1933 // Return the value to use for a dynamic symbol which requires special
1936 do_dynsym_value(const Symbol*) const;
1938 // Relocate a section.
1940 relocate_section(const Relocate_info<32, big_endian>*,
1941 unsigned int sh_type,
1942 const unsigned char* prelocs,
1944 Output_section* output_section,
1945 bool needs_special_offset_handling,
1946 unsigned char* view,
1947 Arm_address view_address,
1948 section_size_type view_size,
1949 const Reloc_symbol_changes*);
1951 // Scan the relocs during a relocatable link.
1953 scan_relocatable_relocs(Symbol_table* symtab,
1955 Sized_relobj<32, big_endian>* object,
1956 unsigned int data_shndx,
1957 unsigned int sh_type,
1958 const unsigned char* prelocs,
1960 Output_section* output_section,
1961 bool needs_special_offset_handling,
1962 size_t local_symbol_count,
1963 const unsigned char* plocal_symbols,
1964 Relocatable_relocs*);
1966 // Relocate a section during a relocatable link.
1968 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1969 unsigned int sh_type,
1970 const unsigned char* prelocs,
1972 Output_section* output_section,
1973 off_t offset_in_output_section,
1974 const Relocatable_relocs*,
1975 unsigned char* view,
1976 Arm_address view_address,
1977 section_size_type view_size,
1978 unsigned char* reloc_view,
1979 section_size_type reloc_view_size);
1981 // Return whether SYM is defined by the ABI.
1983 do_is_defined_by_abi(Symbol* sym) const
1984 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1986 // Return the size of the GOT section.
1990 gold_assert(this->got_ != NULL);
1991 return this->got_->data_size();
1994 // Map platform-specific reloc types
1996 get_real_reloc_type (unsigned int r_type);
1999 // Methods to support stub-generations.
2002 // Return the stub factory
2004 stub_factory() const
2005 { return this->stub_factory_; }
2007 // Make a new Arm_input_section object.
2008 Arm_input_section<big_endian>*
2009 new_arm_input_section(Relobj*, unsigned int);
2011 // Find the Arm_input_section object corresponding to the SHNDX-th input
2012 // section of RELOBJ.
2013 Arm_input_section<big_endian>*
2014 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
2016 // Make a new Stub_table
2017 Stub_table<big_endian>*
2018 new_stub_table(Arm_input_section<big_endian>*);
2020 // Scan a section for stub generation.
2022 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2023 const unsigned char*, size_t, Output_section*,
2024 bool, const unsigned char*, Arm_address,
2029 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
2030 Output_section*, unsigned char*, Arm_address,
2033 // Get the default ARM target.
2034 static Target_arm<big_endian>*
2037 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2038 && parameters->target().is_big_endian() == big_endian);
2039 return static_cast<Target_arm<big_endian>*>(
2040 parameters->sized_target<32, big_endian>());
2043 // Whether relocation type uses LSB to distinguish THUMB addresses.
2045 reloc_uses_thumb_bit(unsigned int r_type);
2047 // Whether NAME belongs to a mapping symbol.
2049 is_mapping_symbol_name(const char* name)
2053 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2054 && (name[2] == '\0' || name[2] == '.'));
2057 // Whether we work around the Cortex-A8 erratum.
2059 fix_cortex_a8() const
2060 { return this->fix_cortex_a8_; }
2062 // Whether we fix R_ARM_V4BX relocation.
2064 // 1 - replace with MOV instruction (armv4 target)
2065 // 2 - make interworking veneer (>= armv4t targets only)
2066 General_options::Fix_v4bx
2068 { return parameters->options().fix_v4bx(); }
2070 // Scan a span of THUMB code section for Cortex-A8 erratum.
2072 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2073 section_size_type, section_size_type,
2074 const unsigned char*, Arm_address);
2076 // Apply Cortex-A8 workaround to a branch.
2078 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2079 unsigned char*, Arm_address);
2082 // Make an ELF object.
2084 do_make_elf_object(const std::string&, Input_file*, off_t,
2085 const elfcpp::Ehdr<32, big_endian>& ehdr);
2088 do_make_elf_object(const std::string&, Input_file*, off_t,
2089 const elfcpp::Ehdr<32, !big_endian>&)
2090 { gold_unreachable(); }
2093 do_make_elf_object(const std::string&, Input_file*, off_t,
2094 const elfcpp::Ehdr<64, false>&)
2095 { gold_unreachable(); }
2098 do_make_elf_object(const std::string&, Input_file*, off_t,
2099 const elfcpp::Ehdr<64, true>&)
2100 { gold_unreachable(); }
2102 // Make an output section.
2104 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2105 elfcpp::Elf_Xword flags)
2106 { return new Arm_output_section<big_endian>(name, type, flags); }
2109 do_adjust_elf_header(unsigned char* view, int len) const;
2111 // We only need to generate stubs, and hence perform relaxation if we are
2112 // not doing relocatable linking.
2114 do_may_relax() const
2115 { return !parameters->options().relocatable(); }
2118 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
2120 // Determine whether an object attribute tag takes an integer, a
2123 do_attribute_arg_type(int tag) const;
2125 // Reorder tags during output.
2127 do_attributes_order(int num) const;
2130 // The class which scans relocations.
2135 : issued_non_pic_error_(false)
2139 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
2140 Sized_relobj<32, big_endian>* object,
2141 unsigned int data_shndx,
2142 Output_section* output_section,
2143 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2144 const elfcpp::Sym<32, big_endian>& lsym);
2147 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
2148 Sized_relobj<32, big_endian>* object,
2149 unsigned int data_shndx,
2150 Output_section* output_section,
2151 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2156 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
2157 unsigned int r_type);
2160 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
2161 unsigned int r_type, Symbol*);
2164 check_non_pic(Relobj*, unsigned int r_type);
2166 // Almost identical to Symbol::needs_plt_entry except that it also
2167 // handles STT_ARM_TFUNC.
2169 symbol_needs_plt_entry(const Symbol* sym)
2171 // An undefined symbol from an executable does not need a PLT entry.
2172 if (sym->is_undefined() && !parameters->options().shared())
2175 return (!parameters->doing_static_link()
2176 && (sym->type() == elfcpp::STT_FUNC
2177 || sym->type() == elfcpp::STT_ARM_TFUNC)
2178 && (sym->is_from_dynobj()
2179 || sym->is_undefined()
2180 || sym->is_preemptible()));
2183 // Whether we have issued an error about a non-PIC compilation.
2184 bool issued_non_pic_error_;
2187 // The class which implements relocation.
2197 // Return whether the static relocation needs to be applied.
2199 should_apply_static_reloc(const Sized_symbol<32>* gsym,
2202 Output_section* output_section);
2204 // Do a relocation. Return false if the caller should not issue
2205 // any warnings about this relocation.
2207 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2208 Output_section*, size_t relnum,
2209 const elfcpp::Rel<32, big_endian>&,
2210 unsigned int r_type, const Sized_symbol<32>*,
2211 const Symbol_value<32>*,
2212 unsigned char*, Arm_address,
2215 // Return whether we want to pass flag NON_PIC_REF for this
2216 // reloc. This means the relocation type accesses a symbol not via
2219 reloc_is_non_pic (unsigned int r_type)
2223 // These relocation types reference GOT or PLT entries explicitly.
2224 case elfcpp::R_ARM_GOT_BREL:
2225 case elfcpp::R_ARM_GOT_ABS:
2226 case elfcpp::R_ARM_GOT_PREL:
2227 case elfcpp::R_ARM_GOT_BREL12:
2228 case elfcpp::R_ARM_PLT32_ABS:
2229 case elfcpp::R_ARM_TLS_GD32:
2230 case elfcpp::R_ARM_TLS_LDM32:
2231 case elfcpp::R_ARM_TLS_IE32:
2232 case elfcpp::R_ARM_TLS_IE12GP:
2234 // These relocate types may use PLT entries.
2235 case elfcpp::R_ARM_CALL:
2236 case elfcpp::R_ARM_THM_CALL:
2237 case elfcpp::R_ARM_JUMP24:
2238 case elfcpp::R_ARM_THM_JUMP24:
2239 case elfcpp::R_ARM_THM_JUMP19:
2240 case elfcpp::R_ARM_PLT32:
2241 case elfcpp::R_ARM_THM_XPC22:
2250 // A class which returns the size required for a relocation type,
2251 // used while scanning relocs during a relocatable link.
2252 class Relocatable_size_for_reloc
2256 get_size_for_reloc(unsigned int, Relobj*);
2259 // Get the GOT section, creating it if necessary.
2260 Output_data_got<32, big_endian>*
2261 got_section(Symbol_table*, Layout*);
2263 // Get the GOT PLT section.
2265 got_plt_section() const
2267 gold_assert(this->got_plt_ != NULL);
2268 return this->got_plt_;
2271 // Create a PLT entry for a global symbol.
2273 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2275 // Get the PLT section.
2276 const Output_data_plt_arm<big_endian>*
2279 gold_assert(this->plt_ != NULL);
2283 // Get the dynamic reloc section, creating it if necessary.
2285 rel_dyn_section(Layout*);
2287 // Return true if the symbol may need a COPY relocation.
2288 // References from an executable object to non-function symbols
2289 // defined in a dynamic object may need a COPY relocation.
2291 may_need_copy_reloc(Symbol* gsym)
2293 return (gsym->type() != elfcpp::STT_ARM_TFUNC
2294 && gsym->may_need_copy_reloc());
2297 // Add a potential copy relocation.
2299 copy_reloc(Symbol_table* symtab, Layout* layout,
2300 Sized_relobj<32, big_endian>* object,
2301 unsigned int shndx, Output_section* output_section,
2302 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2304 this->copy_relocs_.copy_reloc(symtab, layout,
2305 symtab->get_sized_symbol<32>(sym),
2306 object, shndx, output_section, reloc,
2307 this->rel_dyn_section(layout));
2310 // Whether two EABI versions are compatible.
2312 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2314 // Merge processor-specific flags from input object and those in the ELF
2315 // header of the output.
2317 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2319 // Get the secondary compatible architecture.
2321 get_secondary_compatible_arch(const Attributes_section_data*);
2323 // Set the secondary compatible architecture.
2325 set_secondary_compatible_arch(Attributes_section_data*, int);
2328 tag_cpu_arch_combine(const char*, int, int*, int, int);
2330 // Helper to print AEABI enum tag value.
2332 aeabi_enum_name(unsigned int);
2334 // Return string value for TAG_CPU_name.
2336 tag_cpu_name_value(unsigned int);
2338 // Merge object attributes from input object and those in the output.
2340 merge_object_attributes(const char*, const Attributes_section_data*);
2342 // Helper to get an AEABI object attribute
2344 get_aeabi_object_attribute(int tag) const
2346 Attributes_section_data* pasd = this->attributes_section_data_;
2347 gold_assert(pasd != NULL);
2348 Object_attribute* attr =
2349 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2350 gold_assert(attr != NULL);
2355 // Methods to support stub-generations.
2358 // Group input sections for stub generation.
2360 group_sections(Layout*, section_size_type, bool);
2362 // Scan a relocation for stub generation.
2364 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2365 const Sized_symbol<32>*, unsigned int,
2366 const Symbol_value<32>*,
2367 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
2369 // Scan a relocation section for stub.
2370 template<int sh_type>
2372 scan_reloc_section_for_stubs(
2373 const Relocate_info<32, big_endian>* relinfo,
2374 const unsigned char* prelocs,
2376 Output_section* output_section,
2377 bool needs_special_offset_handling,
2378 const unsigned char* view,
2379 elfcpp::Elf_types<32>::Elf_Addr view_address,
2382 // Fix .ARM.exidx section coverage.
2384 fix_exidx_coverage(Layout*, Arm_output_section<big_endian>*, Symbol_table*);
2386 // Functors for STL set.
2387 struct output_section_address_less_than
2390 operator()(const Output_section* s1, const Output_section* s2) const
2391 { return s1->address() < s2->address(); }
2394 // Information about this specific target which we pass to the
2395 // general Target structure.
2396 static const Target::Target_info arm_info;
2398 // The types of GOT entries needed for this platform.
2401 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
2404 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2406 // Map input section to Arm_input_section.
2407 typedef Unordered_map<Section_id,
2408 Arm_input_section<big_endian>*,
2410 Arm_input_section_map;
2412 // Map output addresses to relocs for Cortex-A8 erratum.
2413 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2414 Cortex_a8_relocs_info;
2417 Output_data_got<32, big_endian>* got_;
2419 Output_data_plt_arm<big_endian>* plt_;
2420 // The GOT PLT section.
2421 Output_data_space* got_plt_;
2422 // The dynamic reloc section.
2423 Reloc_section* rel_dyn_;
2424 // Relocs saved to avoid a COPY reloc.
2425 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2426 // Space for variables copied with a COPY reloc.
2427 Output_data_space* dynbss_;
2428 // Vector of Stub_tables created.
2429 Stub_table_list stub_tables_;
2431 const Stub_factory &stub_factory_;
2432 // Whether we can use BLX.
2434 // Whether we force PIC branch veneers.
2435 bool should_force_pic_veneer_;
2436 // Map for locating Arm_input_sections.
2437 Arm_input_section_map arm_input_section_map_;
2438 // Attributes section data in output.
2439 Attributes_section_data* attributes_section_data_;
2440 // Whether we want to fix code for Cortex-A8 erratum.
2441 bool fix_cortex_a8_;
2442 // Map addresses to relocs for Cortex-A8 erratum.
2443 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2446 template<bool big_endian>
2447 const Target::Target_info Target_arm<big_endian>::arm_info =
2450 big_endian, // is_big_endian
2451 elfcpp::EM_ARM, // machine_code
2452 false, // has_make_symbol
2453 false, // has_resolve
2454 false, // has_code_fill
2455 true, // is_default_stack_executable
2457 "/usr/lib/libc.so.1", // dynamic_linker
2458 0x8000, // default_text_segment_address
2459 0x1000, // abi_pagesize (overridable by -z max-page-size)
2460 0x1000, // common_pagesize (overridable by -z common-page-size)
2461 elfcpp::SHN_UNDEF, // small_common_shndx
2462 elfcpp::SHN_UNDEF, // large_common_shndx
2463 0, // small_common_section_flags
2464 0, // large_common_section_flags
2465 ".ARM.attributes", // attributes_section
2466 "aeabi" // attributes_vendor
2469 // Arm relocate functions class
2472 template<bool big_endian>
2473 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2478 STATUS_OKAY, // No error during relocation.
2479 STATUS_OVERFLOW, // Relocation oveflow.
2480 STATUS_BAD_RELOC // Relocation cannot be applied.
2484 typedef Relocate_functions<32, big_endian> Base;
2485 typedef Arm_relocate_functions<big_endian> This;
2487 // Encoding of imm16 argument for movt and movw ARM instructions
2490 // imm16 := imm4 | imm12
2492 // 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
2493 // +-------+---------------+-------+-------+-----------------------+
2494 // | | |imm4 | |imm12 |
2495 // +-------+---------------+-------+-------+-----------------------+
2497 // Extract the relocation addend from VAL based on the ARM
2498 // instruction encoding described above.
2499 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2500 extract_arm_movw_movt_addend(
2501 typename elfcpp::Swap<32, big_endian>::Valtype val)
2503 // According to the Elf ABI for ARM Architecture the immediate
2504 // field is sign-extended to form the addend.
2505 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2508 // Insert X into VAL based on the ARM instruction encoding described
2510 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2511 insert_val_arm_movw_movt(
2512 typename elfcpp::Swap<32, big_endian>::Valtype val,
2513 typename elfcpp::Swap<32, big_endian>::Valtype x)
2517 val |= (x & 0xf000) << 4;
2521 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2524 // imm16 := imm4 | i | imm3 | imm8
2526 // 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
2527 // +---------+-+-----------+-------++-+-----+-------+---------------+
2528 // | |i| |imm4 || |imm3 | |imm8 |
2529 // +---------+-+-----------+-------++-+-----+-------+---------------+
2531 // Extract the relocation addend from VAL based on the Thumb2
2532 // instruction encoding described above.
2533 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2534 extract_thumb_movw_movt_addend(
2535 typename elfcpp::Swap<32, big_endian>::Valtype val)
2537 // According to the Elf ABI for ARM Architecture the immediate
2538 // field is sign-extended to form the addend.
2539 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2540 | ((val >> 15) & 0x0800)
2541 | ((val >> 4) & 0x0700)
2545 // Insert X into VAL based on the Thumb2 instruction encoding
2547 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2548 insert_val_thumb_movw_movt(
2549 typename elfcpp::Swap<32, big_endian>::Valtype val,
2550 typename elfcpp::Swap<32, big_endian>::Valtype x)
2553 val |= (x & 0xf000) << 4;
2554 val |= (x & 0x0800) << 15;
2555 val |= (x & 0x0700) << 4;
2556 val |= (x & 0x00ff);
2560 // Handle ARM long branches.
2561 static typename This::Status
2562 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2563 unsigned char *, const Sized_symbol<32>*,
2564 const Arm_relobj<big_endian>*, unsigned int,
2565 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2567 // Handle THUMB long branches.
2568 static typename This::Status
2569 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2570 unsigned char *, const Sized_symbol<32>*,
2571 const Arm_relobj<big_endian>*, unsigned int,
2572 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2576 // Return the branch offset of a 32-bit THUMB branch.
2577 static inline int32_t
2578 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2580 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2581 // involving the J1 and J2 bits.
2582 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2583 uint32_t upper = upper_insn & 0x3ffU;
2584 uint32_t lower = lower_insn & 0x7ffU;
2585 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2586 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2587 uint32_t i1 = j1 ^ s ? 0 : 1;
2588 uint32_t i2 = j2 ^ s ? 0 : 1;
2590 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2591 | (upper << 12) | (lower << 1));
2594 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2595 // UPPER_INSN is the original upper instruction of the branch. Caller is
2596 // responsible for overflow checking and BLX offset adjustment.
2597 static inline uint16_t
2598 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2600 uint32_t s = offset < 0 ? 1 : 0;
2601 uint32_t bits = static_cast<uint32_t>(offset);
2602 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2605 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2606 // LOWER_INSN is the original lower instruction of the branch. Caller is
2607 // responsible for overflow checking and BLX offset adjustment.
2608 static inline uint16_t
2609 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2611 uint32_t s = offset < 0 ? 1 : 0;
2612 uint32_t bits = static_cast<uint32_t>(offset);
2613 return ((lower_insn & ~0x2fffU)
2614 | ((((bits >> 23) & 1) ^ !s) << 13)
2615 | ((((bits >> 22) & 1) ^ !s) << 11)
2616 | ((bits >> 1) & 0x7ffU));
2619 // Return the branch offset of a 32-bit THUMB conditional branch.
2620 static inline int32_t
2621 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2623 uint32_t s = (upper_insn & 0x0400U) >> 10;
2624 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2625 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2626 uint32_t lower = (lower_insn & 0x07ffU);
2627 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2629 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2632 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2633 // instruction. UPPER_INSN is the original upper instruction of the branch.
2634 // Caller is responsible for overflow checking.
2635 static inline uint16_t
2636 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2638 uint32_t s = offset < 0 ? 1 : 0;
2639 uint32_t bits = static_cast<uint32_t>(offset);
2640 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2643 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2644 // instruction. LOWER_INSN is the original lower instruction of the branch.
2645 // Caller is reponsible for overflow checking.
2646 static inline uint16_t
2647 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2649 uint32_t bits = static_cast<uint32_t>(offset);
2650 uint32_t j2 = (bits & 0x00080000U) >> 19;
2651 uint32_t j1 = (bits & 0x00040000U) >> 18;
2652 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2654 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2657 // R_ARM_ABS8: S + A
2658 static inline typename This::Status
2659 abs8(unsigned char *view,
2660 const Sized_relobj<32, big_endian>* object,
2661 const Symbol_value<32>* psymval)
2663 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2664 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2665 Valtype* wv = reinterpret_cast<Valtype*>(view);
2666 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2667 Reltype addend = utils::sign_extend<8>(val);
2668 Reltype x = psymval->value(object, addend);
2669 val = utils::bit_select(val, x, 0xffU);
2670 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2671 return (utils::has_signed_unsigned_overflow<8>(x)
2672 ? This::STATUS_OVERFLOW
2673 : This::STATUS_OKAY);
2676 // R_ARM_THM_ABS5: S + A
2677 static inline typename This::Status
2678 thm_abs5(unsigned char *view,
2679 const Sized_relobj<32, big_endian>* object,
2680 const Symbol_value<32>* psymval)
2682 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2683 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2684 Valtype* wv = reinterpret_cast<Valtype*>(view);
2685 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2686 Reltype addend = (val & 0x7e0U) >> 6;
2687 Reltype x = psymval->value(object, addend);
2688 val = utils::bit_select(val, x << 6, 0x7e0U);
2689 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2690 return (utils::has_overflow<5>(x)
2691 ? This::STATUS_OVERFLOW
2692 : This::STATUS_OKAY);
2695 // R_ARM_ABS12: S + A
2696 static inline typename This::Status
2697 abs12(unsigned char *view,
2698 const Sized_relobj<32, big_endian>* object,
2699 const Symbol_value<32>* psymval)
2701 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2702 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2703 Valtype* wv = reinterpret_cast<Valtype*>(view);
2704 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2705 Reltype addend = val & 0x0fffU;
2706 Reltype x = psymval->value(object, addend);
2707 val = utils::bit_select(val, x, 0x0fffU);
2708 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2709 return (utils::has_overflow<12>(x)
2710 ? This::STATUS_OVERFLOW
2711 : This::STATUS_OKAY);
2714 // R_ARM_ABS16: S + A
2715 static inline typename This::Status
2716 abs16(unsigned char *view,
2717 const Sized_relobj<32, big_endian>* object,
2718 const Symbol_value<32>* psymval)
2720 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2721 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2722 Valtype* wv = reinterpret_cast<Valtype*>(view);
2723 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2724 Reltype addend = utils::sign_extend<16>(val);
2725 Reltype x = psymval->value(object, addend);
2726 val = utils::bit_select(val, x, 0xffffU);
2727 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2728 return (utils::has_signed_unsigned_overflow<16>(x)
2729 ? This::STATUS_OVERFLOW
2730 : This::STATUS_OKAY);
2733 // R_ARM_ABS32: (S + A) | T
2734 static inline typename This::Status
2735 abs32(unsigned char *view,
2736 const Sized_relobj<32, big_endian>* object,
2737 const Symbol_value<32>* psymval,
2738 Arm_address thumb_bit)
2740 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2741 Valtype* wv = reinterpret_cast<Valtype*>(view);
2742 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2743 Valtype x = psymval->value(object, addend) | thumb_bit;
2744 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2745 return This::STATUS_OKAY;
2748 // R_ARM_REL32: (S + A) | T - P
2749 static inline typename This::Status
2750 rel32(unsigned char *view,
2751 const Sized_relobj<32, big_endian>* object,
2752 const Symbol_value<32>* psymval,
2753 Arm_address address,
2754 Arm_address thumb_bit)
2756 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2757 Valtype* wv = reinterpret_cast<Valtype*>(view);
2758 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2759 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2760 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2761 return This::STATUS_OKAY;
2764 // R_ARM_THM_CALL: (S + A) | T - P
2765 static inline typename This::Status
2766 thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2767 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2768 unsigned int r_sym, const Symbol_value<32>* psymval,
2769 Arm_address address, Arm_address thumb_bit,
2770 bool is_weakly_undefined_without_plt)
2772 return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
2773 object, r_sym, psymval, address, thumb_bit,
2774 is_weakly_undefined_without_plt);
2777 // R_ARM_THM_JUMP24: (S + A) | T - P
2778 static inline typename This::Status
2779 thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2780 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2781 unsigned int r_sym, const Symbol_value<32>* psymval,
2782 Arm_address address, Arm_address thumb_bit,
2783 bool is_weakly_undefined_without_plt)
2785 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
2786 object, r_sym, psymval, address, thumb_bit,
2787 is_weakly_undefined_without_plt);
2790 // R_ARM_THM_JUMP24: (S + A) | T - P
2791 static typename This::Status
2792 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
2793 const Symbol_value<32>* psymval, Arm_address address,
2794 Arm_address thumb_bit);
2796 // R_ARM_THM_XPC22: (S + A) | T - P
2797 static inline typename This::Status
2798 thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2799 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2800 unsigned int r_sym, const Symbol_value<32>* psymval,
2801 Arm_address address, Arm_address thumb_bit,
2802 bool is_weakly_undefined_without_plt)
2804 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
2805 object, r_sym, psymval, address, thumb_bit,
2806 is_weakly_undefined_without_plt);
2809 // R_ARM_THM_JUMP6: S + A – P
2810 static inline typename This::Status
2811 thm_jump6(unsigned char *view,
2812 const Sized_relobj<32, big_endian>* object,
2813 const Symbol_value<32>* psymval,
2814 Arm_address address)
2816 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2817 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2818 Valtype* wv = reinterpret_cast<Valtype*>(view);
2819 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2820 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
2821 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
2822 Reltype x = (psymval->value(object, addend) - address);
2823 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
2824 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2825 // CZB does only forward jumps.
2826 return ((x > 0x007e)
2827 ? This::STATUS_OVERFLOW
2828 : This::STATUS_OKAY);
2831 // R_ARM_THM_JUMP8: S + A – P
2832 static inline typename This::Status
2833 thm_jump8(unsigned char *view,
2834 const Sized_relobj<32, big_endian>* object,
2835 const Symbol_value<32>* psymval,
2836 Arm_address address)
2838 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2839 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2840 Valtype* wv = reinterpret_cast<Valtype*>(view);
2841 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2842 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
2843 Reltype x = (psymval->value(object, addend) - address);
2844 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
2845 return (utils::has_overflow<8>(x)
2846 ? This::STATUS_OVERFLOW
2847 : This::STATUS_OKAY);
2850 // R_ARM_THM_JUMP11: S + A – P
2851 static inline typename This::Status
2852 thm_jump11(unsigned char *view,
2853 const Sized_relobj<32, big_endian>* object,
2854 const Symbol_value<32>* psymval,
2855 Arm_address address)
2857 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2858 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2859 Valtype* wv = reinterpret_cast<Valtype*>(view);
2860 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2861 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
2862 Reltype x = (psymval->value(object, addend) - address);
2863 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
2864 return (utils::has_overflow<11>(x)
2865 ? This::STATUS_OVERFLOW
2866 : This::STATUS_OKAY);
2869 // R_ARM_BASE_PREL: B(S) + A - P
2870 static inline typename This::Status
2871 base_prel(unsigned char* view,
2873 Arm_address address)
2875 Base::rel32(view, origin - address);
2879 // R_ARM_BASE_ABS: B(S) + A
2880 static inline typename This::Status
2881 base_abs(unsigned char* view,
2884 Base::rel32(view, origin);
2888 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2889 static inline typename This::Status
2890 got_brel(unsigned char* view,
2891 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
2893 Base::rel32(view, got_offset);
2894 return This::STATUS_OKAY;
2897 // R_ARM_GOT_PREL: GOT(S) + A - P
2898 static inline typename This::Status
2899 got_prel(unsigned char *view,
2900 Arm_address got_entry,
2901 Arm_address address)
2903 Base::rel32(view, got_entry - address);
2904 return This::STATUS_OKAY;
2907 // R_ARM_PLT32: (S + A) | T - P
2908 static inline typename This::Status
2909 plt32(const Relocate_info<32, big_endian>* relinfo,
2910 unsigned char *view,
2911 const Sized_symbol<32>* gsym,
2912 const Arm_relobj<big_endian>* object,
2914 const Symbol_value<32>* psymval,
2915 Arm_address address,
2916 Arm_address thumb_bit,
2917 bool is_weakly_undefined_without_plt)
2919 return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
2920 object, r_sym, psymval, address, thumb_bit,
2921 is_weakly_undefined_without_plt);
2924 // R_ARM_XPC25: (S + A) | T - P
2925 static inline typename This::Status
2926 xpc25(const Relocate_info<32, big_endian>* relinfo,
2927 unsigned char *view,
2928 const Sized_symbol<32>* gsym,
2929 const Arm_relobj<big_endian>* object,
2931 const Symbol_value<32>* psymval,
2932 Arm_address address,
2933 Arm_address thumb_bit,
2934 bool is_weakly_undefined_without_plt)
2936 return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
2937 object, r_sym, psymval, address, thumb_bit,
2938 is_weakly_undefined_without_plt);
2941 // R_ARM_CALL: (S + A) | T - P
2942 static inline typename This::Status
2943 call(const Relocate_info<32, big_endian>* relinfo,
2944 unsigned char *view,
2945 const Sized_symbol<32>* gsym,
2946 const Arm_relobj<big_endian>* object,
2948 const Symbol_value<32>* psymval,
2949 Arm_address address,
2950 Arm_address thumb_bit,
2951 bool is_weakly_undefined_without_plt)
2953 return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2954 object, r_sym, psymval, address, thumb_bit,
2955 is_weakly_undefined_without_plt);
2958 // R_ARM_JUMP24: (S + A) | T - P
2959 static inline typename This::Status
2960 jump24(const Relocate_info<32, big_endian>* relinfo,
2961 unsigned char *view,
2962 const Sized_symbol<32>* gsym,
2963 const Arm_relobj<big_endian>* object,
2965 const Symbol_value<32>* psymval,
2966 Arm_address address,
2967 Arm_address thumb_bit,
2968 bool is_weakly_undefined_without_plt)
2970 return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2971 object, r_sym, psymval, address, thumb_bit,
2972 is_weakly_undefined_without_plt);
2975 // R_ARM_PREL: (S + A) | T - P
2976 static inline typename This::Status
2977 prel31(unsigned char *view,
2978 const Sized_relobj<32, big_endian>* object,
2979 const Symbol_value<32>* psymval,
2980 Arm_address address,
2981 Arm_address thumb_bit)
2983 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2984 Valtype* wv = reinterpret_cast<Valtype*>(view);
2985 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2986 Valtype addend = utils::sign_extend<31>(val);
2987 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2988 val = utils::bit_select(val, x, 0x7fffffffU);
2989 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2990 return (utils::has_overflow<31>(x) ?
2991 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2994 // R_ARM_MOVW_ABS_NC: (S + A) | T
2995 static inline typename This::Status
2996 movw_abs_nc(unsigned char *view,
2997 const Sized_relobj<32, big_endian>* object,
2998 const Symbol_value<32>* psymval,
2999 Arm_address thumb_bit)
3001 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3002 Valtype* wv = reinterpret_cast<Valtype*>(view);
3003 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3004 Valtype addend = This::extract_arm_movw_movt_addend(val);
3005 Valtype x = psymval->value(object, addend) | thumb_bit;
3006 val = This::insert_val_arm_movw_movt(val, x);
3007 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3008 return This::STATUS_OKAY;
3011 // R_ARM_MOVT_ABS: S + A
3012 static inline typename This::Status
3013 movt_abs(unsigned char *view,
3014 const Sized_relobj<32, big_endian>* object,
3015 const Symbol_value<32>* psymval)
3017 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3018 Valtype* wv = reinterpret_cast<Valtype*>(view);
3019 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3020 Valtype addend = This::extract_arm_movw_movt_addend(val);
3021 Valtype x = psymval->value(object, addend) >> 16;
3022 val = This::insert_val_arm_movw_movt(val, x);
3023 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3024 return This::STATUS_OKAY;
3027 // R_ARM_THM_MOVW_ABS_NC: S + A | T
3028 static inline typename This::Status
3029 thm_movw_abs_nc(unsigned char *view,
3030 const Sized_relobj<32, big_endian>* object,
3031 const Symbol_value<32>* psymval,
3032 Arm_address thumb_bit)
3034 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3035 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3036 Valtype* wv = reinterpret_cast<Valtype*>(view);
3037 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3038 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
3039 Reltype addend = extract_thumb_movw_movt_addend(val);
3040 Reltype x = psymval->value(object, addend) | thumb_bit;
3041 val = This::insert_val_thumb_movw_movt(val, x);
3042 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3043 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3044 return This::STATUS_OKAY;
3047 // R_ARM_THM_MOVT_ABS: S + A
3048 static inline typename This::Status
3049 thm_movt_abs(unsigned char *view,
3050 const Sized_relobj<32, big_endian>* object,
3051 const Symbol_value<32>* psymval)
3053 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3054 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3055 Valtype* wv = reinterpret_cast<Valtype*>(view);
3056 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3057 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
3058 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3059 Reltype x = psymval->value(object, addend) >> 16;
3060 val = This::insert_val_thumb_movw_movt(val, x);
3061 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3062 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3063 return This::STATUS_OKAY;
3066 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3067 static inline typename This::Status
3068 movw_prel_nc(unsigned char *view,
3069 const Sized_relobj<32, big_endian>* object,
3070 const Symbol_value<32>* psymval,
3071 Arm_address address,
3072 Arm_address thumb_bit)
3074 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3075 Valtype* wv = reinterpret_cast<Valtype*>(view);
3076 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3077 Valtype addend = This::extract_arm_movw_movt_addend(val);
3078 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3079 val = This::insert_val_arm_movw_movt(val, x);
3080 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3081 return This::STATUS_OKAY;
3084 // R_ARM_MOVT_PREL: S + A - P
3085 static inline typename This::Status
3086 movt_prel(unsigned char *view,
3087 const Sized_relobj<32, big_endian>* object,
3088 const Symbol_value<32>* psymval,
3089 Arm_address address)
3091 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3092 Valtype* wv = reinterpret_cast<Valtype*>(view);
3093 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3094 Valtype addend = This::extract_arm_movw_movt_addend(val);
3095 Valtype x = (psymval->value(object, addend) - address) >> 16;
3096 val = This::insert_val_arm_movw_movt(val, x);
3097 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3098 return This::STATUS_OKAY;
3101 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3102 static inline typename This::Status
3103 thm_movw_prel_nc(unsigned char *view,
3104 const Sized_relobj<32, big_endian>* object,
3105 const Symbol_value<32>* psymval,
3106 Arm_address address,
3107 Arm_address thumb_bit)
3109 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3110 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3111 Valtype* wv = reinterpret_cast<Valtype*>(view);
3112 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3113 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3114 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3115 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
3116 val = This::insert_val_thumb_movw_movt(val, x);
3117 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3118 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3119 return This::STATUS_OKAY;
3122 // R_ARM_THM_MOVT_PREL: S + A - P
3123 static inline typename This::Status
3124 thm_movt_prel(unsigned char *view,
3125 const Sized_relobj<32, big_endian>* object,
3126 const Symbol_value<32>* psymval,
3127 Arm_address address)
3129 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3130 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3131 Valtype* wv = reinterpret_cast<Valtype*>(view);
3132 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3133 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3134 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3135 Reltype x = (psymval->value(object, addend) - address) >> 16;
3136 val = This::insert_val_thumb_movw_movt(val, x);
3137 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3138 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3139 return This::STATUS_OKAY;
3143 static inline typename This::Status
3144 v4bx(const Relocate_info<32, big_endian>* relinfo,
3145 unsigned char *view,
3146 const Arm_relobj<big_endian>* object,
3147 const Arm_address address,
3148 const bool is_interworking)
3151 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3152 Valtype* wv = reinterpret_cast<Valtype*>(view);
3153 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3155 // Ensure that we have a BX instruction.
3156 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3157 const uint32_t reg = (val & 0xf);
3158 if (is_interworking && reg != 0xf)
3160 Stub_table<big_endian>* stub_table =
3161 object->stub_table(relinfo->data_shndx);
3162 gold_assert(stub_table != NULL);
3164 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3165 gold_assert(stub != NULL);
3167 int32_t veneer_address =
3168 stub_table->address() + stub->offset() - 8 - address;
3169 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3170 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3171 // Replace with a branch to veneer (B <addr>)
3172 val = (val & 0xf0000000) | 0x0a000000
3173 | ((veneer_address >> 2) & 0x00ffffff);
3177 // Preserve Rm (lowest four bits) and the condition code
3178 // (highest four bits). Other bits encode MOV PC,Rm.
3179 val = (val & 0xf000000f) | 0x01a0f000;
3181 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3182 return This::STATUS_OKAY;
3186 // Relocate ARM long branches. This handles relocation types
3187 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3188 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3189 // undefined and we do not use PLT in this relocation. In such a case,
3190 // the branch is converted into an NOP.
3192 template<bool big_endian>
3193 typename Arm_relocate_functions<big_endian>::Status
3194 Arm_relocate_functions<big_endian>::arm_branch_common(
3195 unsigned int r_type,
3196 const Relocate_info<32, big_endian>* relinfo,
3197 unsigned char *view,
3198 const Sized_symbol<32>* gsym,
3199 const Arm_relobj<big_endian>* object,
3201 const Symbol_value<32>* psymval,
3202 Arm_address address,
3203 Arm_address thumb_bit,
3204 bool is_weakly_undefined_without_plt)
3206 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3207 Valtype* wv = reinterpret_cast<Valtype*>(view);
3208 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3210 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3211 && ((val & 0x0f000000UL) == 0x0a000000UL);
3212 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3213 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3214 && ((val & 0x0f000000UL) == 0x0b000000UL);
3215 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3216 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3218 // Check that the instruction is valid.
3219 if (r_type == elfcpp::R_ARM_CALL)
3221 if (!insn_is_uncond_bl && !insn_is_blx)
3222 return This::STATUS_BAD_RELOC;
3224 else if (r_type == elfcpp::R_ARM_JUMP24)
3226 if (!insn_is_b && !insn_is_cond_bl)
3227 return This::STATUS_BAD_RELOC;
3229 else if (r_type == elfcpp::R_ARM_PLT32)
3231 if (!insn_is_any_branch)
3232 return This::STATUS_BAD_RELOC;
3234 else if (r_type == elfcpp::R_ARM_XPC25)
3236 // FIXME: AAELF document IH0044C does not say much about it other
3237 // than it being obsolete.
3238 if (!insn_is_any_branch)
3239 return This::STATUS_BAD_RELOC;
3244 // A branch to an undefined weak symbol is turned into a jump to
3245 // the next instruction unless a PLT entry will be created.
3246 // Do the same for local undefined symbols.
3247 // The jump to the next instruction is optimized as a NOP depending
3248 // on the architecture.
3249 const Target_arm<big_endian>* arm_target =
3250 Target_arm<big_endian>::default_target();
3251 if (is_weakly_undefined_without_plt)
3253 Valtype cond = val & 0xf0000000U;
3254 if (arm_target->may_use_arm_nop())
3255 val = cond | 0x0320f000;
3257 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3258 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3259 return This::STATUS_OKAY;
3262 Valtype addend = utils::sign_extend<26>(val << 2);
3263 Valtype branch_target = psymval->value(object, addend);
3264 int32_t branch_offset = branch_target - address;
3266 // We need a stub if the branch offset is too large or if we need
3268 bool may_use_blx = arm_target->may_use_blx();
3269 Reloc_stub* stub = NULL;
3270 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
3271 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3272 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
3274 Stub_type stub_type =
3275 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
3277 if (stub_type != arm_stub_none)
3279 Stub_table<big_endian>* stub_table =
3280 object->stub_table(relinfo->data_shndx);
3281 gold_assert(stub_table != NULL);
3283 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3284 stub = stub_table->find_reloc_stub(stub_key);
3285 gold_assert(stub != NULL);
3286 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3287 branch_target = stub_table->address() + stub->offset() + addend;
3288 branch_offset = branch_target - address;
3289 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
3290 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
3294 // At this point, if we still need to switch mode, the instruction
3295 // must either be a BLX or a BL that can be converted to a BLX.
3299 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3300 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3303 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3304 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3305 return (utils::has_overflow<26>(branch_offset)
3306 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3309 // Relocate THUMB long branches. This handles relocation types
3310 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3311 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3312 // undefined and we do not use PLT in this relocation. In such a case,
3313 // the branch is converted into an NOP.
3315 template<bool big_endian>
3316 typename Arm_relocate_functions<big_endian>::Status
3317 Arm_relocate_functions<big_endian>::thumb_branch_common(
3318 unsigned int r_type,
3319 const Relocate_info<32, big_endian>* relinfo,
3320 unsigned char *view,
3321 const Sized_symbol<32>* gsym,
3322 const Arm_relobj<big_endian>* object,
3324 const Symbol_value<32>* psymval,
3325 Arm_address address,
3326 Arm_address thumb_bit,
3327 bool is_weakly_undefined_without_plt)
3329 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3330 Valtype* wv = reinterpret_cast<Valtype*>(view);
3331 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3332 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3334 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3336 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
3337 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
3339 // Check that the instruction is valid.
3340 if (r_type == elfcpp::R_ARM_THM_CALL)
3342 if (!is_bl_insn && !is_blx_insn)
3343 return This::STATUS_BAD_RELOC;
3345 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
3347 // This cannot be a BLX.
3349 return This::STATUS_BAD_RELOC;
3351 else if (r_type == elfcpp::R_ARM_THM_XPC22)
3353 // Check for Thumb to Thumb call.
3355 return This::STATUS_BAD_RELOC;
3358 gold_warning(_("%s: Thumb BLX instruction targets "
3359 "thumb function '%s'."),
3360 object->name().c_str(),
3361 (gsym ? gsym->name() : "(local)"));
3362 // Convert BLX to BL.
3363 lower_insn |= 0x1000U;
3369 // A branch to an undefined weak symbol is turned into a jump to
3370 // the next instruction unless a PLT entry will be created.
3371 // The jump to the next instruction is optimized as a NOP.W for
3372 // Thumb-2 enabled architectures.
3373 const Target_arm<big_endian>* arm_target =
3374 Target_arm<big_endian>::default_target();
3375 if (is_weakly_undefined_without_plt)
3377 if (arm_target->may_use_thumb2_nop())
3379 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
3380 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
3384 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
3385 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
3387 return This::STATUS_OKAY;
3390 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
3391 Arm_address branch_target = psymval->value(object, addend);
3392 int32_t branch_offset = branch_target - address;
3394 // We need a stub if the branch offset is too large or if we need
3396 bool may_use_blx = arm_target->may_use_blx();
3397 bool thumb2 = arm_target->using_thumb2();
3399 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3400 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3402 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3403 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3404 || ((thumb_bit == 0)
3405 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3406 || r_type == elfcpp::R_ARM_THM_JUMP24)))
3408 Stub_type stub_type =
3409 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
3411 if (stub_type != arm_stub_none)
3413 Stub_table<big_endian>* stub_table =
3414 object->stub_table(relinfo->data_shndx);
3415 gold_assert(stub_table != NULL);
3417 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3418 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
3419 gold_assert(stub != NULL);
3420 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3421 branch_target = stub_table->address() + stub->offset() + addend;
3422 branch_offset = branch_target - address;
3426 // At this point, if we still need to switch mode, the instruction
3427 // must either be a BLX or a BL that can be converted to a BLX.
3430 gold_assert(may_use_blx
3431 && (r_type == elfcpp::R_ARM_THM_CALL
3432 || r_type == elfcpp::R_ARM_THM_XPC22));
3433 // Make sure this is a BLX.
3434 lower_insn &= ~0x1000U;
3438 // Make sure this is a BL.
3439 lower_insn |= 0x1000U;
3442 if ((lower_insn & 0x5000U) == 0x4000U)
3443 // For a BLX instruction, make sure that the relocation is rounded up
3444 // to a word boundary. This follows the semantics of the instruction
3445 // which specifies that bit 1 of the target address will come from bit
3446 // 1 of the base address.
3447 branch_offset = (branch_offset + 2) & ~3;
3449 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3450 // We use the Thumb-2 encoding, which is safe even if dealing with
3451 // a Thumb-1 instruction by virtue of our overflow check above. */
3452 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
3453 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
3455 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3456 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3459 ? utils::has_overflow<25>(branch_offset)
3460 : utils::has_overflow<23>(branch_offset))
3461 ? This::STATUS_OVERFLOW
3462 : This::STATUS_OKAY);
3465 // Relocate THUMB-2 long conditional branches.
3466 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3467 // undefined and we do not use PLT in this relocation. In such a case,
3468 // the branch is converted into an NOP.
3470 template<bool big_endian>
3471 typename Arm_relocate_functions<big_endian>::Status
3472 Arm_relocate_functions<big_endian>::thm_jump19(
3473 unsigned char *view,
3474 const Arm_relobj<big_endian>* object,
3475 const Symbol_value<32>* psymval,
3476 Arm_address address,
3477 Arm_address thumb_bit)
3479 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3480 Valtype* wv = reinterpret_cast<Valtype*>(view);
3481 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3482 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3483 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
3485 Arm_address branch_target = psymval->value(object, addend);
3486 int32_t branch_offset = branch_target - address;
3488 // ??? Should handle interworking? GCC might someday try to
3489 // use this for tail calls.
3490 // FIXME: We do support thumb entry to PLT yet.
3493 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3494 return This::STATUS_BAD_RELOC;
3497 // Put RELOCATION back into the insn.
3498 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
3499 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
3501 // Put the relocated value back in the object file:
3502 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3503 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3505 return (utils::has_overflow<21>(branch_offset)
3506 ? This::STATUS_OVERFLOW
3507 : This::STATUS_OKAY);
3510 // Get the GOT section, creating it if necessary.
3512 template<bool big_endian>
3513 Output_data_got<32, big_endian>*
3514 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
3516 if (this->got_ == NULL)
3518 gold_assert(symtab != NULL && layout != NULL);
3520 this->got_ = new Output_data_got<32, big_endian>();
3523 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3525 | elfcpp::SHF_WRITE),
3526 this->got_, false, true, true,
3529 // The old GNU linker creates a .got.plt section. We just
3530 // create another set of data in the .got section. Note that we
3531 // always create a PLT if we create a GOT, although the PLT
3533 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
3534 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3536 | elfcpp::SHF_WRITE),
3537 this->got_plt_, false, false,
3540 // The first three entries are reserved.
3541 this->got_plt_->set_current_data_size(3 * 4);
3543 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3544 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3545 Symbol_table::PREDEFINED,
3547 0, 0, elfcpp::STT_OBJECT,
3549 elfcpp::STV_HIDDEN, 0,
3555 // Get the dynamic reloc section, creating it if necessary.
3557 template<bool big_endian>
3558 typename Target_arm<big_endian>::Reloc_section*
3559 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
3561 if (this->rel_dyn_ == NULL)
3563 gold_assert(layout != NULL);
3564 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
3565 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
3566 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
3567 false, false, false);
3569 return this->rel_dyn_;
3572 // Insn_template methods.
3574 // Return byte size of an instruction template.
3577 Insn_template::size() const
3579 switch (this->type())
3582 case THUMB16_SPECIAL_TYPE:
3593 // Return alignment of an instruction template.
3596 Insn_template::alignment() const
3598 switch (this->type())
3601 case THUMB16_SPECIAL_TYPE:
3612 // Stub_template methods.
3614 Stub_template::Stub_template(
3615 Stub_type type, const Insn_template* insns,
3617 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
3618 entry_in_thumb_mode_(false), relocs_()
3622 // Compute byte size and alignment of stub template.
3623 for (size_t i = 0; i < insn_count; i++)
3625 unsigned insn_alignment = insns[i].alignment();
3626 size_t insn_size = insns[i].size();
3627 gold_assert((offset & (insn_alignment - 1)) == 0);
3628 this->alignment_ = std::max(this->alignment_, insn_alignment);
3629 switch (insns[i].type())
3631 case Insn_template::THUMB16_TYPE:
3632 case Insn_template::THUMB16_SPECIAL_TYPE:
3634 this->entry_in_thumb_mode_ = true;
3637 case Insn_template::THUMB32_TYPE:
3638 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
3639 this->relocs_.push_back(Reloc(i, offset));
3641 this->entry_in_thumb_mode_ = true;
3644 case Insn_template::ARM_TYPE:
3645 // Handle cases where the target is encoded within the
3647 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
3648 this->relocs_.push_back(Reloc(i, offset));
3651 case Insn_template::DATA_TYPE:
3652 // Entry point cannot be data.
3653 gold_assert(i != 0);
3654 this->relocs_.push_back(Reloc(i, offset));
3660 offset += insn_size;
3662 this->size_ = offset;
3667 // Template to implement do_write for a specific target endianity.
3669 template<bool big_endian>
3671 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
3673 const Stub_template* stub_template = this->stub_template();
3674 const Insn_template* insns = stub_template->insns();
3676 // FIXME: We do not handle BE8 encoding yet.
3677 unsigned char* pov = view;
3678 for (size_t i = 0; i < stub_template->insn_count(); i++)
3680 switch (insns[i].type())
3682 case Insn_template::THUMB16_TYPE:
3683 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
3685 case Insn_template::THUMB16_SPECIAL_TYPE:
3686 elfcpp::Swap<16, big_endian>::writeval(
3688 this->thumb16_special(i));
3690 case Insn_template::THUMB32_TYPE:
3692 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
3693 uint32_t lo = insns[i].data() & 0xffff;
3694 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
3695 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
3698 case Insn_template::ARM_TYPE:
3699 case Insn_template::DATA_TYPE:
3700 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
3705 pov += insns[i].size();
3707 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
3710 // Reloc_stub::Key methods.
3712 // Dump a Key as a string for debugging.
3715 Reloc_stub::Key::name() const
3717 if (this->r_sym_ == invalid_index)
3719 // Global symbol key name
3720 // <stub-type>:<symbol name>:<addend>.
3721 const std::string sym_name = this->u_.symbol->name();
3722 // We need to print two hex number and two colons. So just add 100 bytes
3723 // to the symbol name size.
3724 size_t len = sym_name.size() + 100;
3725 char* buffer = new char[len];
3726 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
3727 sym_name.c_str(), this->addend_);
3728 gold_assert(c > 0 && c < static_cast<int>(len));
3730 return std::string(buffer);
3734 // local symbol key name
3735 // <stub-type>:<object>:<r_sym>:<addend>.
3736 const size_t len = 200;
3738 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
3739 this->u_.relobj, this->r_sym_, this->addend_);
3740 gold_assert(c > 0 && c < static_cast<int>(len));
3741 return std::string(buffer);
3745 // Reloc_stub methods.
3747 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3748 // LOCATION to DESTINATION.
3749 // This code is based on the arm_type_of_stub function in
3750 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3754 Reloc_stub::stub_type_for_reloc(
3755 unsigned int r_type,
3756 Arm_address location,
3757 Arm_address destination,
3758 bool target_is_thumb)
3760 Stub_type stub_type = arm_stub_none;
3762 // This is a bit ugly but we want to avoid using a templated class for
3763 // big and little endianities.
3765 bool should_force_pic_veneer;
3768 if (parameters->target().is_big_endian())
3770 const Target_arm<true>* big_endian_target =
3771 Target_arm<true>::default_target();
3772 may_use_blx = big_endian_target->may_use_blx();
3773 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
3774 thumb2 = big_endian_target->using_thumb2();
3775 thumb_only = big_endian_target->using_thumb_only();
3779 const Target_arm<false>* little_endian_target =
3780 Target_arm<false>::default_target();
3781 may_use_blx = little_endian_target->may_use_blx();
3782 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
3783 thumb2 = little_endian_target->using_thumb2();
3784 thumb_only = little_endian_target->using_thumb_only();
3787 int64_t branch_offset = (int64_t)destination - location;
3789 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
3791 // Handle cases where:
3792 // - this call goes too far (different Thumb/Thumb2 max
3794 // - it's a Thumb->Arm call and blx is not available, or it's a
3795 // Thumb->Arm branch (not bl). A stub is needed in this case.
3797 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3798 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3800 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3801 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3802 || ((!target_is_thumb)
3803 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3804 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
3806 if (target_is_thumb)
3811 stub_type = (parameters->options().shared()
3812 || should_force_pic_veneer)
3815 && (r_type == elfcpp::R_ARM_THM_CALL))
3816 // V5T and above. Stub starts with ARM code, so
3817 // we must be able to switch mode before
3818 // reaching it, which is only possible for 'bl'
3819 // (ie R_ARM_THM_CALL relocation).
3820 ? arm_stub_long_branch_any_thumb_pic
3821 // On V4T, use Thumb code only.
3822 : arm_stub_long_branch_v4t_thumb_thumb_pic)
3826 && (r_type == elfcpp::R_ARM_THM_CALL))
3827 ? arm_stub_long_branch_any_any // V5T and above.
3828 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
3832 stub_type = (parameters->options().shared()
3833 || should_force_pic_veneer)
3834 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
3835 : arm_stub_long_branch_thumb_only; // non-PIC stub.
3842 // FIXME: We should check that the input section is from an
3843 // object that has interwork enabled.
3845 stub_type = (parameters->options().shared()
3846 || should_force_pic_veneer)
3849 && (r_type == elfcpp::R_ARM_THM_CALL))
3850 ? arm_stub_long_branch_any_arm_pic // V5T and above.
3851 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
3855 && (r_type == elfcpp::R_ARM_THM_CALL))
3856 ? arm_stub_long_branch_any_any // V5T and above.
3857 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
3859 // Handle v4t short branches.
3860 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3861 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3862 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3863 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3867 else if (r_type == elfcpp::R_ARM_CALL
3868 || r_type == elfcpp::R_ARM_JUMP24
3869 || r_type == elfcpp::R_ARM_PLT32)
3871 if (target_is_thumb)
3875 // FIXME: We should check that the input section is from an
3876 // object that has interwork enabled.
3878 // We have an extra 2-bytes reach because of
3879 // the mode change (bit 24 (H) of BLX encoding).
3880 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3881 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3882 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
3883 || (r_type == elfcpp::R_ARM_JUMP24)
3884 || (r_type == elfcpp::R_ARM_PLT32))
3886 stub_type = (parameters->options().shared()
3887 || should_force_pic_veneer)
3890 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
3891 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
3895 ? arm_stub_long_branch_any_any // V5T and above.
3896 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
3902 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3903 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3905 stub_type = (parameters->options().shared()
3906 || should_force_pic_veneer)
3907 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
3908 : arm_stub_long_branch_any_any; /// non-PIC.
3916 // Cortex_a8_stub methods.
3918 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3919 // I is the position of the instruction template in the stub template.
3922 Cortex_a8_stub::do_thumb16_special(size_t i)
3924 // The only use of this is to copy condition code from a conditional
3925 // branch being worked around to the corresponding conditional branch in
3927 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3929 uint16_t data = this->stub_template()->insns()[i].data();
3930 gold_assert((data & 0xff00U) == 0xd000U);
3931 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
3935 // Stub_factory methods.
3937 Stub_factory::Stub_factory()
3939 // The instruction template sequences are declared as static
3940 // objects and initialized first time the constructor runs.
3942 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3943 // to reach the stub if necessary.
3944 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
3946 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3947 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3948 // dcd R_ARM_ABS32(X)
3951 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3953 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
3955 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3956 Insn_template::arm_insn(0xe12fff1c), // bx ip
3957 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3958 // dcd R_ARM_ABS32(X)
3961 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3962 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
3964 Insn_template::thumb16_insn(0xb401), // push {r0}
3965 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3966 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3967 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3968 Insn_template::thumb16_insn(0x4760), // bx ip
3969 Insn_template::thumb16_insn(0xbf00), // nop
3970 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3971 // dcd R_ARM_ABS32(X)
3974 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3976 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
3978 Insn_template::thumb16_insn(0x4778), // bx pc
3979 Insn_template::thumb16_insn(0x46c0), // nop
3980 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3981 Insn_template::arm_insn(0xe12fff1c), // bx ip
3982 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3983 // dcd R_ARM_ABS32(X)
3986 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3988 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
3990 Insn_template::thumb16_insn(0x4778), // bx pc
3991 Insn_template::thumb16_insn(0x46c0), // nop
3992 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3993 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3994 // dcd R_ARM_ABS32(X)
3997 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3998 // one, when the destination is close enough.
3999 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
4001 Insn_template::thumb16_insn(0x4778), // bx pc
4002 Insn_template::thumb16_insn(0x46c0), // nop
4003 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4006 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4007 // blx to reach the stub if necessary.
4008 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
4010 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4011 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4012 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4013 // dcd R_ARM_REL32(X-4)
4016 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4017 // blx to reach the stub if necessary. We can not add into pc;
4018 // it is not guaranteed to mode switch (different in ARMv6 and
4020 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
4022 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4023 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4024 Insn_template::arm_insn(0xe12fff1c), // bx ip
4025 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4026 // dcd R_ARM_REL32(X)
4029 // V4T ARM -> ARM long branch stub, PIC.
4030 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4032 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4033 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4034 Insn_template::arm_insn(0xe12fff1c), // bx ip
4035 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4036 // dcd R_ARM_REL32(X)
4039 // V4T Thumb -> ARM long branch stub, PIC.
4040 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4042 Insn_template::thumb16_insn(0x4778), // bx pc
4043 Insn_template::thumb16_insn(0x46c0), // nop
4044 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4045 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4046 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4047 // dcd R_ARM_REL32(X)
4050 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4052 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4054 Insn_template::thumb16_insn(0xb401), // push {r0}
4055 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4056 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4057 Insn_template::thumb16_insn(0x4484), // add ip, r0
4058 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4059 Insn_template::thumb16_insn(0x4760), // bx ip
4060 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4061 // dcd R_ARM_REL32(X)
4064 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4066 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4068 Insn_template::thumb16_insn(0x4778), // bx pc
4069 Insn_template::thumb16_insn(0x46c0), // nop
4070 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4071 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4072 Insn_template::arm_insn(0xe12fff1c), // bx ip
4073 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4074 // dcd R_ARM_REL32(X)
4077 // Cortex-A8 erratum-workaround stubs.
4079 // Stub used for conditional branches (which may be beyond +/-1MB away,
4080 // so we can't use a conditional branch to reach this stub).
4087 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4089 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4090 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4091 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4095 // Stub used for b.w and bl.w instructions.
4097 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4099 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4102 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4104 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4107 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4108 // instruction (which switches to ARM mode) to point to this stub. Jump to
4109 // the real destination using an ARM-mode branch.
4110 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
4112 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4115 // Stub used to provide an interworking for R_ARM_V4BX relocation
4116 // (bx r[n] instruction).
4117 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4119 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4120 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4121 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4124 // Fill in the stub template look-up table. Stub templates are constructed
4125 // per instance of Stub_factory for fast look-up without locking
4126 // in a thread-enabled environment.
4128 this->stub_templates_[arm_stub_none] =
4129 new Stub_template(arm_stub_none, NULL, 0);
4131 #define DEF_STUB(x) \
4135 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4136 Stub_type type = arm_stub_##x; \
4137 this->stub_templates_[type] = \
4138 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4146 // Stub_table methods.
4148 // Removel all Cortex-A8 stub.
4150 template<bool big_endian>
4152 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4154 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4155 p != this->cortex_a8_stubs_.end();
4158 this->cortex_a8_stubs_.clear();
4161 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4163 template<bool big_endian>
4165 Stub_table<big_endian>::relocate_stub(
4167 const Relocate_info<32, big_endian>* relinfo,
4168 Target_arm<big_endian>* arm_target,
4169 Output_section* output_section,
4170 unsigned char* view,
4171 Arm_address address,
4172 section_size_type view_size)
4174 const Stub_template* stub_template = stub->stub_template();
4175 if (stub_template->reloc_count() != 0)
4177 // Adjust view to cover the stub only.
4178 section_size_type offset = stub->offset();
4179 section_size_type stub_size = stub_template->size();
4180 gold_assert(offset + stub_size <= view_size);
4182 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4183 address + offset, stub_size);
4187 // Relocate all stubs in this stub table.
4189 template<bool big_endian>
4191 Stub_table<big_endian>::relocate_stubs(
4192 const Relocate_info<32, big_endian>* relinfo,
4193 Target_arm<big_endian>* arm_target,
4194 Output_section* output_section,
4195 unsigned char* view,
4196 Arm_address address,
4197 section_size_type view_size)
4199 // If we are passed a view bigger than the stub table's. we need to
4201 gold_assert(address == this->address()
4203 == static_cast<section_size_type>(this->data_size())));
4205 // Relocate all relocation stubs.
4206 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4207 p != this->reloc_stubs_.end();
4209 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4210 address, view_size);
4212 // Relocate all Cortex-A8 stubs.
4213 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4214 p != this->cortex_a8_stubs_.end();
4216 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4217 address, view_size);
4219 // Relocate all ARM V4BX stubs.
4220 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4221 p != this->arm_v4bx_stubs_.end();
4225 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4226 address, view_size);
4230 // Write out the stubs to file.
4232 template<bool big_endian>
4234 Stub_table<big_endian>::do_write(Output_file* of)
4236 off_t offset = this->offset();
4237 const section_size_type oview_size =
4238 convert_to_section_size_type(this->data_size());
4239 unsigned char* const oview = of->get_output_view(offset, oview_size);
4241 // Write relocation stubs.
4242 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4243 p != this->reloc_stubs_.end();
4246 Reloc_stub* stub = p->second;
4247 Arm_address address = this->address() + stub->offset();
4249 == align_address(address,
4250 stub->stub_template()->alignment()));
4251 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4255 // Write Cortex-A8 stubs.
4256 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4257 p != this->cortex_a8_stubs_.end();
4260 Cortex_a8_stub* stub = p->second;
4261 Arm_address address = this->address() + stub->offset();
4263 == align_address(address,
4264 stub->stub_template()->alignment()));
4265 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4269 // Write ARM V4BX relocation stubs.
4270 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4271 p != this->arm_v4bx_stubs_.end();
4277 Arm_address address = this->address() + (*p)->offset();
4279 == align_address(address,
4280 (*p)->stub_template()->alignment()));
4281 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4285 of->write_output_view(this->offset(), oview_size, oview);
4288 // Update the data size and address alignment of the stub table at the end
4289 // of a relaxation pass. Return true if either the data size or the
4290 // alignment changed in this relaxation pass.
4292 template<bool big_endian>
4294 Stub_table<big_endian>::update_data_size_and_addralign()
4297 unsigned addralign = 1;
4299 // Go over all stubs in table to compute data size and address alignment.
4301 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4302 p != this->reloc_stubs_.end();
4305 const Stub_template* stub_template = p->second->stub_template();
4306 addralign = std::max(addralign, stub_template->alignment());
4307 size = (align_address(size, stub_template->alignment())
4308 + stub_template->size());
4311 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4312 p != this->cortex_a8_stubs_.end();
4315 const Stub_template* stub_template = p->second->stub_template();
4316 addralign = std::max(addralign, stub_template->alignment());
4317 size = (align_address(size, stub_template->alignment())
4318 + stub_template->size());
4321 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4322 p != this->arm_v4bx_stubs_.end();
4328 const Stub_template* stub_template = (*p)->stub_template();
4329 addralign = std::max(addralign, stub_template->alignment());
4330 size = (align_address(size, stub_template->alignment())
4331 + stub_template->size());
4334 // Check if either data size or alignment changed in this pass.
4335 // Update prev_data_size_ and prev_addralign_. These will be used
4336 // as the current data size and address alignment for the next pass.
4337 bool changed = size != this->prev_data_size_;
4338 this->prev_data_size_ = size;
4340 if (addralign != this->prev_addralign_)
4342 this->prev_addralign_ = addralign;
4347 // Finalize the stubs. This sets the offsets of the stubs within the stub
4348 // table. It also marks all input sections needing Cortex-A8 workaround.
4350 template<bool big_endian>
4352 Stub_table<big_endian>::finalize_stubs()
4355 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4356 p != this->reloc_stubs_.end();
4359 Reloc_stub* stub = p->second;
4360 const Stub_template* stub_template = stub->stub_template();
4361 uint64_t stub_addralign = stub_template->alignment();
4362 off = align_address(off, stub_addralign);
4363 stub->set_offset(off);
4364 off += stub_template->size();
4367 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4368 p != this->cortex_a8_stubs_.end();
4371 Cortex_a8_stub* stub = p->second;
4372 const Stub_template* stub_template = stub->stub_template();
4373 uint64_t stub_addralign = stub_template->alignment();
4374 off = align_address(off, stub_addralign);
4375 stub->set_offset(off);
4376 off += stub_template->size();
4378 // Mark input section so that we can determine later if a code section
4379 // needs the Cortex-A8 workaround quickly.
4380 Arm_relobj<big_endian>* arm_relobj =
4381 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
4382 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
4385 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4386 p != this->arm_v4bx_stubs_.end();
4392 const Stub_template* stub_template = (*p)->stub_template();
4393 uint64_t stub_addralign = stub_template->alignment();
4394 off = align_address(off, stub_addralign);
4395 (*p)->set_offset(off);
4396 off += stub_template->size();
4399 gold_assert(off <= this->prev_data_size_);
4402 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4403 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4404 // of the address range seen by the linker.
4406 template<bool big_endian>
4408 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
4409 Target_arm<big_endian>* arm_target,
4410 unsigned char* view,
4411 Arm_address view_address,
4412 section_size_type view_size)
4414 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4415 for (Cortex_a8_stub_list::const_iterator p =
4416 this->cortex_a8_stubs_.lower_bound(view_address);
4417 ((p != this->cortex_a8_stubs_.end())
4418 && (p->first < (view_address + view_size)));
4421 // We do not store the THUMB bit in the LSB of either the branch address
4422 // or the stub offset. There is no need to strip the LSB.
4423 Arm_address branch_address = p->first;
4424 const Cortex_a8_stub* stub = p->second;
4425 Arm_address stub_address = this->address() + stub->offset();
4427 // Offset of the branch instruction relative to this view.
4428 section_size_type offset =
4429 convert_to_section_size_type(branch_address - view_address);
4430 gold_assert((offset + 4) <= view_size);
4432 arm_target->apply_cortex_a8_workaround(stub, stub_address,
4433 view + offset, branch_address);
4437 // Arm_input_section methods.
4439 // Initialize an Arm_input_section.
4441 template<bool big_endian>
4443 Arm_input_section<big_endian>::init()
4445 Relobj* relobj = this->relobj();
4446 unsigned int shndx = this->shndx();
4448 // Cache these to speed up size and alignment queries. It is too slow
4449 // to call section_addraglin and section_size every time.
4450 this->original_addralign_ = relobj->section_addralign(shndx);
4451 this->original_size_ = relobj->section_size(shndx);
4453 // We want to make this look like the original input section after
4454 // output sections are finalized.
4455 Output_section* os = relobj->output_section(shndx);
4456 off_t offset = relobj->output_section_offset(shndx);
4457 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
4458 this->set_address(os->address() + offset);
4459 this->set_file_offset(os->offset() + offset);
4461 this->set_current_data_size(this->original_size_);
4462 this->finalize_data_size();
4465 template<bool big_endian>
4467 Arm_input_section<big_endian>::do_write(Output_file* of)
4469 // We have to write out the original section content.
4470 section_size_type section_size;
4471 const unsigned char* section_contents =
4472 this->relobj()->section_contents(this->shndx(), §ion_size, false);
4473 of->write(this->offset(), section_contents, section_size);
4475 // If this owns a stub table and it is not empty, write it.
4476 if (this->is_stub_table_owner() && !this->stub_table_->empty())
4477 this->stub_table_->write(of);
4480 // Finalize data size.
4482 template<bool big_endian>
4484 Arm_input_section<big_endian>::set_final_data_size()
4486 // If this owns a stub table, finalize its data size as well.
4487 if (this->is_stub_table_owner())
4489 uint64_t address = this->address();
4491 // The stub table comes after the original section contents.
4492 address += this->original_size_;
4493 address = align_address(address, this->stub_table_->addralign());
4494 off_t offset = this->offset() + (address - this->address());
4495 this->stub_table_->set_address_and_file_offset(address, offset);
4496 address += this->stub_table_->data_size();
4497 gold_assert(address == this->address() + this->current_data_size());
4500 this->set_data_size(this->current_data_size());
4503 // Reset address and file offset.
4505 template<bool big_endian>
4507 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
4509 // Size of the original input section contents.
4510 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4512 // If this is a stub table owner, account for the stub table size.
4513 if (this->is_stub_table_owner())
4515 Stub_table<big_endian>* stub_table = this->stub_table_;
4517 // Reset the stub table's address and file offset. The
4518 // current data size for child will be updated after that.
4519 stub_table_->reset_address_and_file_offset();
4520 off = align_address(off, stub_table_->addralign());
4521 off += stub_table->current_data_size();
4524 this->set_current_data_size(off);
4527 // Arm_exidx_cantunwind methods.
4529 // Write this to Output file OF for a fixed endianity.
4531 template<bool big_endian>
4533 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
4535 off_t offset = this->offset();
4536 const section_size_type oview_size = 8;
4537 unsigned char* const oview = of->get_output_view(offset, oview_size);
4539 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4540 Valtype* wv = reinterpret_cast<Valtype*>(oview);
4542 Output_section* os = this->relobj_->output_section(this->shndx_);
4543 gold_assert(os != NULL);
4545 Arm_relobj<big_endian>* arm_relobj =
4546 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
4547 Arm_address output_offset =
4548 arm_relobj->get_output_section_offset(this->shndx_);
4549 Arm_address section_start;
4550 if(output_offset != Arm_relobj<big_endian>::invalid_address)
4551 section_start = os->address() + output_offset;
4554 // Currently this only happens for a relaxed section.
4555 const Output_relaxed_input_section* poris =
4556 os->find_relaxed_input_section(this->relobj_, this->shndx_);
4557 gold_assert(poris != NULL);
4558 section_start = poris->address();
4561 // We always append this to the end of an EXIDX section.
4562 Arm_address output_address =
4563 section_start + this->relobj_->section_size(this->shndx_);
4565 // Write out the entry. The first word either points to the beginning
4566 // or after the end of a text section. The second word is the special
4567 // EXIDX_CANTUNWIND value.
4568 elfcpp::Swap<32, big_endian>::writeval(wv, output_address);
4569 elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
4571 of->write_output_view(this->offset(), oview_size, oview);
4574 // Arm_exidx_merged_section methods.
4576 // Constructor for Arm_exidx_merged_section.
4577 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
4578 // SECTION_OFFSET_MAP points to a section offset map describing how
4579 // parts of the input section are mapped to output. DELETED_BYTES is
4580 // the number of bytes deleted from the EXIDX input section.
4582 Arm_exidx_merged_section::Arm_exidx_merged_section(
4583 const Arm_exidx_input_section& exidx_input_section,
4584 const Arm_exidx_section_offset_map& section_offset_map,
4585 uint32_t deleted_bytes)
4586 : Output_relaxed_input_section(exidx_input_section.relobj(),
4587 exidx_input_section.shndx(),
4588 exidx_input_section.addralign()),
4589 exidx_input_section_(exidx_input_section),
4590 section_offset_map_(section_offset_map)
4592 // Fix size here so that we do not need to implement set_final_data_size.
4593 this->set_data_size(exidx_input_section.size() - deleted_bytes);
4594 this->fix_data_size();
4597 // Given an input OBJECT, an input section index SHNDX within that
4598 // object, and an OFFSET relative to the start of that input
4599 // section, return whether or not the corresponding offset within
4600 // the output section is known. If this function returns true, it
4601 // sets *POUTPUT to the output offset. The value -1 indicates that
4602 // this input offset is being discarded.
4605 Arm_exidx_merged_section::do_output_offset(
4606 const Relobj* relobj,
4608 section_offset_type offset,
4609 section_offset_type* poutput) const
4611 // We only handle offsets for the original EXIDX input section.
4612 if (relobj != this->exidx_input_section_.relobj()
4613 || shndx != this->exidx_input_section_.shndx())
4616 section_offset_type section_size =
4617 convert_types<section_offset_type>(this->exidx_input_section_.size());
4618 if (offset < 0 || offset >= section_size)
4619 // Input offset is out of valid range.
4623 // We need to look up the section offset map to determine the output
4624 // offset. Find the reference point in map that is first offset
4625 // bigger than or equal to this offset.
4626 Arm_exidx_section_offset_map::const_iterator p =
4627 this->section_offset_map_.lower_bound(offset);
4629 // The section offset maps are build such that this should not happen if
4630 // input offset is in the valid range.
4631 gold_assert(p != this->section_offset_map_.end());
4633 // We need to check if this is dropped.
4634 section_offset_type ref = p->first;
4635 section_offset_type mapped_ref = p->second;
4637 if (mapped_ref != Arm_exidx_input_section::invalid_offset)
4638 // Offset is present in output.
4639 *poutput = mapped_ref + (offset - ref);
4641 // Offset is discarded owing to EXIDX entry merging.
4648 // Write this to output file OF.
4651 Arm_exidx_merged_section::do_write(Output_file* of)
4653 // If we retain or discard the whole EXIDX input section, we would
4655 gold_assert(this->data_size() != this->exidx_input_section_.size()
4656 && this->data_size() != 0);
4658 off_t offset = this->offset();
4659 const section_size_type oview_size = this->data_size();
4660 unsigned char* const oview = of->get_output_view(offset, oview_size);
4662 Output_section* os = this->relobj()->output_section(this->shndx());
4663 gold_assert(os != NULL);
4665 // Get contents of EXIDX input section.
4666 section_size_type section_size;
4667 const unsigned char* section_contents =
4668 this->relobj()->section_contents(this->shndx(), §ion_size, false);
4669 gold_assert(section_size == this->exidx_input_section_.size());
4671 // Go over spans of input offsets and write only those that are not
4673 section_offset_type in_start = 0;
4674 section_offset_type out_start = 0;
4675 for(Arm_exidx_section_offset_map::const_iterator p =
4676 this->section_offset_map_.begin();
4677 p != this->section_offset_map_.end();
4680 section_offset_type in_end = p->first;
4681 gold_assert(in_end >= in_start);
4682 section_offset_type out_end = p->second;
4683 size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
4686 size_t out_chunk_size =
4687 convert_types<size_t>(out_end - out_start + 1);
4688 gold_assert(out_chunk_size == in_chunk_size);
4689 memcpy(oview + out_start, section_contents + in_start,
4691 out_start += out_chunk_size;
4693 in_start += in_chunk_size;
4696 gold_assert(convert_to_section_size_type(out_start) == oview_size);
4697 of->write_output_view(this->offset(), oview_size, oview);
4700 // Arm_exidx_fixup methods.
4702 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
4703 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
4704 // points to the end of the last seen EXIDX section.
4707 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
4709 if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
4710 && this->last_input_section_ != NULL)
4712 Relobj* relobj = this->last_input_section_->relobj();
4713 unsigned int text_shndx = this->last_input_section_->link();
4714 Arm_exidx_cantunwind* cantunwind =
4715 new Arm_exidx_cantunwind(relobj, text_shndx);
4716 this->exidx_output_section_->add_output_section_data(cantunwind);
4717 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
4721 // Process an EXIDX section entry in input. Return whether this entry
4722 // can be deleted in the output. SECOND_WORD in the second word of the
4726 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
4729 if (second_word == elfcpp::EXIDX_CANTUNWIND)
4731 // Merge if previous entry is also an EXIDX_CANTUNWIND.
4732 delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
4733 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
4735 else if ((second_word & 0x80000000) != 0)
4737 // Inlined unwinding data. Merge if equal to previous.
4738 delete_entry = (this->last_unwind_type_ == UT_INLINED_ENTRY
4739 && this->last_inlined_entry_ == second_word);
4740 this->last_unwind_type_ = UT_INLINED_ENTRY;
4741 this->last_inlined_entry_ = second_word;
4745 // Normal table entry. In theory we could merge these too,
4746 // but duplicate entries are likely to be much less common.
4747 delete_entry = false;
4748 this->last_unwind_type_ = UT_NORMAL_ENTRY;
4750 return delete_entry;
4753 // Update the current section offset map during EXIDX section fix-up.
4754 // If there is no map, create one. INPUT_OFFSET is the offset of a
4755 // reference point, DELETED_BYTES is the number of deleted by in the
4756 // section so far. If DELETE_ENTRY is true, the reference point and
4757 // all offsets after the previous reference point are discarded.
4760 Arm_exidx_fixup::update_offset_map(
4761 section_offset_type input_offset,
4762 section_size_type deleted_bytes,
4765 if (this->section_offset_map_ == NULL)
4766 this->section_offset_map_ = new Arm_exidx_section_offset_map();
4767 section_offset_type output_offset = (delete_entry
4769 : input_offset - deleted_bytes);
4770 (*this->section_offset_map_)[input_offset] = output_offset;
4773 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
4774 // bytes deleted. If some entries are merged, also store a pointer to a newly
4775 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
4776 // caller owns the map and is responsible for releasing it after use.
4778 template<bool big_endian>
4780 Arm_exidx_fixup::process_exidx_section(
4781 const Arm_exidx_input_section* exidx_input_section,
4782 Arm_exidx_section_offset_map** psection_offset_map)
4784 Relobj* relobj = exidx_input_section->relobj();
4785 unsigned shndx = exidx_input_section->shndx();
4786 section_size_type section_size;
4787 const unsigned char* section_contents =
4788 relobj->section_contents(shndx, §ion_size, false);
4790 if ((section_size % 8) != 0)
4792 // Something is wrong with this section. Better not touch it.
4793 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
4794 relobj->name().c_str(), shndx);
4795 this->last_input_section_ = exidx_input_section;
4796 this->last_unwind_type_ = UT_NONE;
4800 uint32_t deleted_bytes = 0;
4801 bool prev_delete_entry = false;
4802 gold_assert(this->section_offset_map_ == NULL);
4804 for (section_size_type i = 0; i < section_size; i += 8)
4806 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4808 reinterpret_cast<const Valtype*>(section_contents + i + 4);
4809 uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
4811 bool delete_entry = this->process_exidx_entry(second_word);
4813 // Entry deletion causes changes in output offsets. We use a std::map
4814 // to record these. And entry (x, y) means input offset x
4815 // is mapped to output offset y. If y is invalid_offset, then x is
4816 // dropped in the output. Because of the way std::map::lower_bound
4817 // works, we record the last offset in a region w.r.t to keeping or
4818 // dropping. If there is no entry (x0, y0) for an input offset x0,
4819 // the output offset y0 of it is determined by the output offset y1 of
4820 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
4821 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
4823 if (delete_entry != prev_delete_entry && i != 0)
4824 this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
4826 // Update total deleted bytes for this entry.
4830 prev_delete_entry = delete_entry;
4833 // If section offset map is not NULL, make an entry for the end of
4835 if (this->section_offset_map_ != NULL)
4836 update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
4838 *psection_offset_map = this->section_offset_map_;
4839 this->section_offset_map_ = NULL;
4840 this->last_input_section_ = exidx_input_section;
4842 return deleted_bytes;
4845 // Arm_output_section methods.
4847 // Create a stub group for input sections from BEGIN to END. OWNER
4848 // points to the input section to be the owner a new stub table.
4850 template<bool big_endian>
4852 Arm_output_section<big_endian>::create_stub_group(
4853 Input_section_list::const_iterator begin,
4854 Input_section_list::const_iterator end,
4855 Input_section_list::const_iterator owner,
4856 Target_arm<big_endian>* target,
4857 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
4859 // We use a different kind of relaxed section in an EXIDX section.
4860 // The static casting from Output_relaxed_input_section to
4861 // Arm_input_section is invalid in an EXIDX section. We are okay
4862 // because we should not be calling this for an EXIDX section.
4863 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
4865 // Currently we convert ordinary input sections into relaxed sections only
4866 // at this point but we may want to support creating relaxed input section
4867 // very early. So we check here to see if owner is already a relaxed
4870 Arm_input_section<big_endian>* arm_input_section;
4871 if (owner->is_relaxed_input_section())
4874 Arm_input_section<big_endian>::as_arm_input_section(
4875 owner->relaxed_input_section());
4879 gold_assert(owner->is_input_section());
4880 // Create a new relaxed input section.
4882 target->new_arm_input_section(owner->relobj(), owner->shndx());
4883 new_relaxed_sections->push_back(arm_input_section);
4886 // Create a stub table.
4887 Stub_table<big_endian>* stub_table =
4888 target->new_stub_table(arm_input_section);
4890 arm_input_section->set_stub_table(stub_table);
4892 Input_section_list::const_iterator p = begin;
4893 Input_section_list::const_iterator prev_p;
4895 // Look for input sections or relaxed input sections in [begin ... end].
4898 if (p->is_input_section() || p->is_relaxed_input_section())
4900 // The stub table information for input sections live
4901 // in their objects.
4902 Arm_relobj<big_endian>* arm_relobj =
4903 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
4904 arm_relobj->set_stub_table(p->shndx(), stub_table);
4908 while (prev_p != end);
4911 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
4912 // of stub groups. We grow a stub group by adding input section until the
4913 // size is just below GROUP_SIZE. The last input section will be converted
4914 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
4915 // input section after the stub table, effectively double the group size.
4917 // This is similar to the group_sections() function in elf32-arm.c but is
4918 // implemented differently.
4920 template<bool big_endian>
4922 Arm_output_section<big_endian>::group_sections(
4923 section_size_type group_size,
4924 bool stubs_always_after_branch,
4925 Target_arm<big_endian>* target)
4927 // We only care about sections containing code.
4928 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
4931 // States for grouping.
4934 // No group is being built.
4936 // A group is being built but the stub table is not found yet.
4937 // We keep group a stub group until the size is just under GROUP_SIZE.
4938 // The last input section in the group will be used as the stub table.
4939 FINDING_STUB_SECTION,
4940 // A group is being built and we have already found a stub table.
4941 // We enter this state to grow a stub group by adding input section
4942 // after the stub table. This effectively doubles the group size.
4946 // Any newly created relaxed sections are stored here.
4947 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
4949 State state = NO_GROUP;
4950 section_size_type off = 0;
4951 section_size_type group_begin_offset = 0;
4952 section_size_type group_end_offset = 0;
4953 section_size_type stub_table_end_offset = 0;
4954 Input_section_list::const_iterator group_begin =
4955 this->input_sections().end();
4956 Input_section_list::const_iterator stub_table =
4957 this->input_sections().end();
4958 Input_section_list::const_iterator group_end = this->input_sections().end();
4959 for (Input_section_list::const_iterator p = this->input_sections().begin();
4960 p != this->input_sections().end();
4963 section_size_type section_begin_offset =
4964 align_address(off, p->addralign());
4965 section_size_type section_end_offset =
4966 section_begin_offset + p->data_size();
4968 // Check to see if we should group the previously seens sections.
4974 case FINDING_STUB_SECTION:
4975 // Adding this section makes the group larger than GROUP_SIZE.
4976 if (section_end_offset - group_begin_offset >= group_size)
4978 if (stubs_always_after_branch)
4980 gold_assert(group_end != this->input_sections().end());
4981 this->create_stub_group(group_begin, group_end, group_end,
4982 target, &new_relaxed_sections);
4987 // But wait, there's more! Input sections up to
4988 // stub_group_size bytes after the stub table can be
4989 // handled by it too.
4990 state = HAS_STUB_SECTION;
4991 stub_table = group_end;
4992 stub_table_end_offset = group_end_offset;
4997 case HAS_STUB_SECTION:
4998 // Adding this section makes the post stub-section group larger
5000 if (section_end_offset - stub_table_end_offset >= group_size)
5002 gold_assert(group_end != this->input_sections().end());
5003 this->create_stub_group(group_begin, group_end, stub_table,
5004 target, &new_relaxed_sections);
5013 // If we see an input section and currently there is no group, start
5014 // a new one. Skip any empty sections.
5015 if ((p->is_input_section() || p->is_relaxed_input_section())
5016 && (p->relobj()->section_size(p->shndx()) != 0))
5018 if (state == NO_GROUP)
5020 state = FINDING_STUB_SECTION;
5022 group_begin_offset = section_begin_offset;
5025 // Keep track of the last input section seen.
5027 group_end_offset = section_end_offset;
5030 off = section_end_offset;
5033 // Create a stub group for any ungrouped sections.
5034 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5036 gold_assert(group_end != this->input_sections().end());
5037 this->create_stub_group(group_begin, group_end,
5038 (state == FINDING_STUB_SECTION
5041 target, &new_relaxed_sections);
5044 // Convert input section into relaxed input section in a batch.
5045 if (!new_relaxed_sections.empty())
5046 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5048 // Update the section offsets
5049 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5051 Arm_relobj<big_endian>* arm_relobj =
5052 Arm_relobj<big_endian>::as_arm_relobj(
5053 new_relaxed_sections[i]->relobj());
5054 unsigned int shndx = new_relaxed_sections[i]->shndx();
5055 // Tell Arm_relobj that this input section is converted.
5056 arm_relobj->convert_input_section_to_relaxed_section(shndx);
5060 // Append non empty text sections in this to LIST in ascending
5061 // order of their position in this.
5063 template<bool big_endian>
5065 Arm_output_section<big_endian>::append_text_sections_to_list(
5066 Text_section_list* list)
5068 // We only care about text sections.
5069 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5072 gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
5074 for (Input_section_list::const_iterator p = this->input_sections().begin();
5075 p != this->input_sections().end();
5078 // We only care about plain or relaxed input sections. We also
5079 // ignore any merged sections.
5080 if ((p->is_input_section() || p->is_relaxed_input_section())
5081 && p->data_size() != 0)
5082 list->push_back(Text_section_list::value_type(p->relobj(),
5087 template<bool big_endian>
5089 Arm_output_section<big_endian>::fix_exidx_coverage(
5090 const Text_section_list& sorted_text_sections,
5091 Symbol_table* symtab)
5093 // We should only do this for the EXIDX output section.
5094 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5096 // We don't want the relaxation loop to undo these changes, so we discard
5097 // the current saved states and take another one after the fix-up.
5098 this->discard_states();
5100 // Remove all input sections.
5101 uint64_t address = this->address();
5102 typedef std::list<Simple_input_section> Simple_input_section_list;
5103 Simple_input_section_list input_sections;
5104 this->reset_address_and_file_offset();
5105 this->get_input_sections(address, std::string(""), &input_sections);
5107 if (!this->input_sections().empty())
5108 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5110 // Go through all the known input sections and record them.
5111 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5112 Section_id_set known_input_sections;
5113 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5114 p != input_sections.end();
5117 // This should never happen. At this point, we should only see
5118 // plain EXIDX input sections.
5119 gold_assert(!p->is_relaxed_input_section());
5120 known_input_sections.insert(Section_id(p->relobj(), p->shndx()));
5123 Arm_exidx_fixup exidx_fixup(this);
5125 // Go over the sorted text sections.
5126 Section_id_set processed_input_sections;
5127 for (Text_section_list::const_iterator p = sorted_text_sections.begin();
5128 p != sorted_text_sections.end();
5131 Relobj* relobj = p->first;
5132 unsigned int shndx = p->second;
5134 Arm_relobj<big_endian>* arm_relobj =
5135 Arm_relobj<big_endian>::as_arm_relobj(relobj);
5136 const Arm_exidx_input_section* exidx_input_section =
5137 arm_relobj->exidx_input_section_by_link(shndx);
5139 // If this text section has no EXIDX section, force an EXIDX_CANTUNWIND
5140 // entry pointing to the end of the last seen EXIDX section.
5141 if (exidx_input_section == NULL)
5143 exidx_fixup.add_exidx_cantunwind_as_needed();
5147 Relobj* exidx_relobj = exidx_input_section->relobj();
5148 unsigned int exidx_shndx = exidx_input_section->shndx();
5149 Section_id sid(exidx_relobj, exidx_shndx);
5150 if (known_input_sections.find(sid) == known_input_sections.end())
5152 // This is odd. We have not seen this EXIDX input section before.
5153 // We cannot do fix-up.
5154 gold_error(_("EXIDX section %u of %s is not in EXIDX output section"),
5155 exidx_shndx, exidx_relobj->name().c_str());
5156 exidx_fixup.add_exidx_cantunwind_as_needed();
5160 // Fix up coverage and append input section to output data list.
5161 Arm_exidx_section_offset_map* section_offset_map = NULL;
5162 uint32_t deleted_bytes =
5163 exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
5164 §ion_offset_map);
5166 if (deleted_bytes == exidx_input_section->size())
5168 // The whole EXIDX section got merged. Remove it from output.
5169 gold_assert(section_offset_map == NULL);
5170 exidx_relobj->set_output_section(exidx_shndx, NULL);
5172 else if (deleted_bytes > 0)
5174 // Some entries are merged. We need to convert this EXIDX input
5175 // section into a relaxed section.
5176 gold_assert(section_offset_map != NULL);
5177 Arm_exidx_merged_section* merged_section =
5178 new Arm_exidx_merged_section(*exidx_input_section,
5179 *section_offset_map, deleted_bytes);
5180 this->add_relaxed_input_section(merged_section);
5181 arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
5185 // Just add back the EXIDX input section.
5186 gold_assert(section_offset_map == NULL);
5187 Output_section::Simple_input_section sis(exidx_relobj, exidx_shndx);
5188 this->add_simple_input_section(sis, exidx_input_section->size(),
5189 exidx_input_section->addralign());
5192 processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx));
5195 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5196 exidx_fixup.add_exidx_cantunwind_as_needed();
5198 // Remove any known EXIDX input sections that are not processed.
5199 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5200 p != input_sections.end();
5203 if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
5204 == processed_input_sections.end())
5206 // We only discard a known EXIDX section because its linked
5207 // text section has been folded by ICF.
5208 Arm_relobj<big_endian>* arm_relobj =
5209 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5210 const Arm_exidx_input_section* exidx_input_section =
5211 arm_relobj->exidx_input_section_by_shndx(p->shndx());
5212 gold_assert(exidx_input_section != NULL);
5213 unsigned int text_shndx = exidx_input_section->link();
5214 gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
5216 // Remove this from link.
5217 p->relobj()->set_output_section(p->shndx(), NULL);
5221 // Make changes permanent.
5222 this->save_states();
5223 this->set_section_offsets_need_adjustment();
5226 // Arm_relobj methods.
5228 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5229 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5231 template<bool big_endian>
5233 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
5234 const elfcpp::Shdr<32, big_endian>& shdr,
5235 const Relobj::Output_sections& out_sections,
5236 const Symbol_table *symtab,
5237 const unsigned char* pshdrs)
5239 unsigned int sh_type = shdr.get_sh_type();
5240 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
5243 // Ignore empty section.
5244 off_t sh_size = shdr.get_sh_size();
5248 // Ignore reloc section with bad info. This error will be
5249 // reported in the final link.
5250 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5251 if (index >= this->shnum())
5254 // This relocation section is against a section which we
5255 // discarded or if the section is folded into another
5256 // section due to ICF.
5257 if (out_sections[index] == NULL || symtab->is_section_folded(this, index))
5260 // Check the section to which relocations are applied. Ignore relocations
5261 // to unallocated sections or EXIDX sections.
5262 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5263 const elfcpp::Shdr<32, big_endian> data_shdr(pshdrs + index * shdr_size);
5264 if ((data_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
5265 || data_shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
5268 // Ignore reloc section with unexpected symbol table. The
5269 // error will be reported in the final link.
5270 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
5273 unsigned int reloc_size;
5274 if (sh_type == elfcpp::SHT_REL)
5275 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5277 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5279 // Ignore reloc section with unexpected entsize or uneven size.
5280 // The error will be reported in the final link.
5281 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
5287 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
5288 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5290 template<bool big_endian>
5292 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
5293 const elfcpp::Shdr<32, big_endian>& shdr,
5296 const Symbol_table* symtab)
5298 // We only scan non-empty code sections.
5299 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) == 0
5300 || shdr.get_sh_size() == 0)
5303 // Ignore discarded or ICF'ed sections.
5304 if (os == NULL || symtab->is_section_folded(this, shndx))
5307 // Find output address of section.
5308 Arm_address address = os->output_address(this, shndx, 0);
5310 // If the section does not cross any 4K-boundaries, it does not need to
5312 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
5318 // Scan a section for Cortex-A8 workaround.
5320 template<bool big_endian>
5322 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
5323 const elfcpp::Shdr<32, big_endian>& shdr,
5326 Target_arm<big_endian>* arm_target)
5328 Arm_address output_address = os->output_address(this, shndx, 0);
5330 // Get the section contents.
5331 section_size_type input_view_size = 0;
5332 const unsigned char* input_view =
5333 this->section_contents(shndx, &input_view_size, false);
5335 // We need to go through the mapping symbols to determine what to
5336 // scan. There are two reasons. First, we should look at THUMB code and
5337 // THUMB code only. Second, we only want to look at the 4K-page boundary
5338 // to speed up the scanning.
5340 // Look for the first mapping symbol in this section. It should be
5342 Mapping_symbol_position section_start(shndx, 0);
5343 typename Mapping_symbols_info::const_iterator p =
5344 this->mapping_symbols_info_.lower_bound(section_start);
5346 if (p == this->mapping_symbols_info_.end()
5347 || p->first != section_start)
5349 gold_warning(_("Cortex-A8 erratum scanning failed because there "
5350 "is no mapping symbols for section %u of %s"),
5351 shndx, this->name().c_str());
5355 while (p != this->mapping_symbols_info_.end()
5356 && p->first.first == shndx)
5358 typename Mapping_symbols_info::const_iterator next =
5359 this->mapping_symbols_info_.upper_bound(p->first);
5361 // Only scan part of a section with THUMB code.
5362 if (p->second == 't')
5364 // Determine the end of this range.
5365 section_size_type span_start =
5366 convert_to_section_size_type(p->first.second);
5367 section_size_type span_end;
5368 if (next != this->mapping_symbols_info_.end()
5369 && next->first.first == shndx)
5370 span_end = convert_to_section_size_type(next->first.second);
5372 span_end = convert_to_section_size_type(shdr.get_sh_size());
5374 if (((span_start + output_address) & ~0xfffUL)
5375 != ((span_end + output_address - 1) & ~0xfffUL))
5377 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
5378 span_start, span_end,
5388 // Scan relocations for stub generation.
5390 template<bool big_endian>
5392 Arm_relobj<big_endian>::scan_sections_for_stubs(
5393 Target_arm<big_endian>* arm_target,
5394 const Symbol_table* symtab,
5395 const Layout* layout)
5397 unsigned int shnum = this->shnum();
5398 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5400 // Read the section headers.
5401 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
5405 // To speed up processing, we set up hash tables for fast lookup of
5406 // input offsets to output addresses.
5407 this->initialize_input_to_output_maps();
5409 const Relobj::Output_sections& out_sections(this->output_sections());
5411 Relocate_info<32, big_endian> relinfo;
5412 relinfo.symtab = symtab;
5413 relinfo.layout = layout;
5414 relinfo.object = this;
5416 // Do relocation stubs scanning.
5417 const unsigned char* p = pshdrs + shdr_size;
5418 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5420 const elfcpp::Shdr<32, big_endian> shdr(p);
5421 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
5424 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5425 Arm_address output_offset = this->get_output_section_offset(index);
5426 Arm_address output_address;
5427 if(output_offset != invalid_address)
5428 output_address = out_sections[index]->address() + output_offset;
5431 // Currently this only happens for a relaxed section.
5432 const Output_relaxed_input_section* poris =
5433 out_sections[index]->find_relaxed_input_section(this, index);
5434 gold_assert(poris != NULL);
5435 output_address = poris->address();
5438 // Get the relocations.
5439 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
5443 // Get the section contents. This does work for the case in which
5444 // we modify the contents of an input section. We need to pass the
5445 // output view under such circumstances.
5446 section_size_type input_view_size = 0;
5447 const unsigned char* input_view =
5448 this->section_contents(index, &input_view_size, false);
5450 relinfo.reloc_shndx = i;
5451 relinfo.data_shndx = index;
5452 unsigned int sh_type = shdr.get_sh_type();
5453 unsigned int reloc_size;
5454 if (sh_type == elfcpp::SHT_REL)
5455 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5457 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5459 Output_section* os = out_sections[index];
5460 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
5461 shdr.get_sh_size() / reloc_size,
5463 output_offset == invalid_address,
5464 input_view, output_address,
5469 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
5470 // after its relocation section, if there is one, is processed for
5471 // relocation stubs. Merging this loop with the one above would have been
5472 // complicated since we would have had to make sure that relocation stub
5473 // scanning is done first.
5474 if (arm_target->fix_cortex_a8())
5476 const unsigned char* p = pshdrs + shdr_size;
5477 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5479 const elfcpp::Shdr<32, big_endian> shdr(p);
5480 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
5483 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
5488 // After we've done the relocations, we release the hash tables,
5489 // since we no longer need them.
5490 this->free_input_to_output_maps();
5493 // Count the local symbols. The ARM backend needs to know if a symbol
5494 // is a THUMB function or not. For global symbols, it is easy because
5495 // the Symbol object keeps the ELF symbol type. For local symbol it is
5496 // harder because we cannot access this information. So we override the
5497 // do_count_local_symbol in parent and scan local symbols to mark
5498 // THUMB functions. This is not the most efficient way but I do not want to
5499 // slow down other ports by calling a per symbol targer hook inside
5500 // Sized_relobj<size, big_endian>::do_count_local_symbols.
5502 template<bool big_endian>
5504 Arm_relobj<big_endian>::do_count_local_symbols(
5505 Stringpool_template<char>* pool,
5506 Stringpool_template<char>* dynpool)
5508 // We need to fix-up the values of any local symbols whose type are
5511 // Ask parent to count the local symbols.
5512 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
5513 const unsigned int loccount = this->local_symbol_count();
5517 // Intialize the thumb function bit-vector.
5518 std::vector<bool> empty_vector(loccount, false);
5519 this->local_symbol_is_thumb_function_.swap(empty_vector);
5521 // Read the symbol table section header.
5522 const unsigned int symtab_shndx = this->symtab_shndx();
5523 elfcpp::Shdr<32, big_endian>
5524 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
5525 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
5527 // Read the local symbols.
5528 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
5529 gold_assert(loccount == symtabshdr.get_sh_info());
5530 off_t locsize = loccount * sym_size;
5531 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
5532 locsize, true, true);
5534 // For mapping symbol processing, we need to read the symbol names.
5535 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
5536 if (strtab_shndx >= this->shnum())
5538 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
5542 elfcpp::Shdr<32, big_endian>
5543 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
5544 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
5546 this->error(_("symbol table name section has wrong type: %u"),
5547 static_cast<unsigned int>(strtabshdr.get_sh_type()));
5550 const char* pnames =
5551 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
5552 strtabshdr.get_sh_size(),
5555 // Loop over the local symbols and mark any local symbols pointing
5556 // to THUMB functions.
5558 // Skip the first dummy symbol.
5560 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
5561 this->local_values();
5562 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
5564 elfcpp::Sym<32, big_endian> sym(psyms);
5565 elfcpp::STT st_type = sym.get_st_type();
5566 Symbol_value<32>& lv((*plocal_values)[i]);
5567 Arm_address input_value = lv.input_value();
5569 // Check to see if this is a mapping symbol.
5570 const char* sym_name = pnames + sym.get_st_name();
5571 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
5573 unsigned int input_shndx = sym.get_st_shndx();
5575 // Strip of LSB in case this is a THUMB symbol.
5576 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
5577 this->mapping_symbols_info_[msp] = sym_name[1];
5580 if (st_type == elfcpp::STT_ARM_TFUNC
5581 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
5583 // This is a THUMB function. Mark this and canonicalize the
5584 // symbol value by setting LSB.
5585 this->local_symbol_is_thumb_function_[i] = true;
5586 if ((input_value & 1) == 0)
5587 lv.set_input_value(input_value | 1);
5592 // Relocate sections.
5593 template<bool big_endian>
5595 Arm_relobj<big_endian>::do_relocate_sections(
5596 const Symbol_table* symtab,
5597 const Layout* layout,
5598 const unsigned char* pshdrs,
5599 typename Sized_relobj<32, big_endian>::Views* pviews)
5601 // Call parent to relocate sections.
5602 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
5605 // We do not generate stubs if doing a relocatable link.
5606 if (parameters->options().relocatable())
5609 // Relocate stub tables.
5610 unsigned int shnum = this->shnum();
5612 Target_arm<big_endian>* arm_target =
5613 Target_arm<big_endian>::default_target();
5615 Relocate_info<32, big_endian> relinfo;
5616 relinfo.symtab = symtab;
5617 relinfo.layout = layout;
5618 relinfo.object = this;
5620 for (unsigned int i = 1; i < shnum; ++i)
5622 Arm_input_section<big_endian>* arm_input_section =
5623 arm_target->find_arm_input_section(this, i);
5625 if (arm_input_section != NULL
5626 && arm_input_section->is_stub_table_owner()
5627 && !arm_input_section->stub_table()->empty())
5629 // We cannot discard a section if it owns a stub table.
5630 Output_section* os = this->output_section(i);
5631 gold_assert(os != NULL);
5633 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
5634 relinfo.reloc_shdr = NULL;
5635 relinfo.data_shndx = i;
5636 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
5638 gold_assert((*pviews)[i].view != NULL);
5640 // We are passed the output section view. Adjust it to cover the
5642 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
5643 gold_assert((stub_table->address() >= (*pviews)[i].address)
5644 && ((stub_table->address() + stub_table->data_size())
5645 <= (*pviews)[i].address + (*pviews)[i].view_size));
5647 off_t offset = stub_table->address() - (*pviews)[i].address;
5648 unsigned char* view = (*pviews)[i].view + offset;
5649 Arm_address address = stub_table->address();
5650 section_size_type view_size = stub_table->data_size();
5652 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
5656 // Apply Cortex A8 workaround if applicable.
5657 if (this->section_has_cortex_a8_workaround(i))
5659 unsigned char* view = (*pviews)[i].view;
5660 Arm_address view_address = (*pviews)[i].address;
5661 section_size_type view_size = (*pviews)[i].view_size;
5662 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
5664 // Adjust view to cover section.
5665 Output_section* os = this->output_section(i);
5666 gold_assert(os != NULL);
5667 Arm_address section_address = os->output_address(this, i, 0);
5668 uint64_t section_size = this->section_size(i);
5670 gold_assert(section_address >= view_address
5671 && ((section_address + section_size)
5672 <= (view_address + view_size)));
5674 unsigned char* section_view = view + (section_address - view_address);
5676 // Apply the Cortex-A8 workaround to the output address range
5677 // corresponding to this input section.
5678 stub_table->apply_cortex_a8_workaround_to_address_range(
5687 // Create a new EXIDX input section object for EXIDX section SHNDX with
5690 template<bool big_endian>
5692 Arm_relobj<big_endian>::make_exidx_input_section(
5694 const elfcpp::Shdr<32, big_endian>& shdr)
5696 // Link .text section to its .ARM.exidx section in the same object.
5697 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
5699 // Issue an error and ignore this EXIDX section if it does not point
5700 // to any text section.
5701 if (text_shndx == elfcpp::SHN_UNDEF)
5703 gold_error(_("EXIDX section %u in %s has no linked text section"),
5704 shndx, this->name().c_str());
5708 // Issue an error and ignore this EXIDX section if it points to a text
5709 // section already has an EXIDX section.
5710 if (this->exidx_section_map_[text_shndx] != NULL)
5712 gold_error(_("EXIDX sections %u and %u both link to text section %u "
5714 shndx, this->exidx_section_map_[text_shndx]->shndx(),
5715 text_shndx, this->name().c_str());
5719 // Create an Arm_exidx_input_section object for this EXIDX section.
5720 Arm_exidx_input_section* exidx_input_section =
5721 new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
5722 shdr.get_sh_addralign());
5723 this->exidx_section_map_[text_shndx] = exidx_input_section;
5725 // Also map the EXIDX section index to this.
5726 gold_assert(this->exidx_section_map_[shndx] == NULL);
5727 this->exidx_section_map_[shndx] = exidx_input_section;
5730 // Read the symbol information.
5732 template<bool big_endian>
5734 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
5736 // Call parent class to read symbol information.
5737 Sized_relobj<32, big_endian>::do_read_symbols(sd);
5739 // Read processor-specific flags in ELF file header.
5740 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
5741 elfcpp::Elf_sizes<32>::ehdr_size,
5743 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
5744 this->processor_specific_flags_ = ehdr.get_e_flags();
5746 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
5748 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5749 const unsigned char *ps =
5750 sd->section_headers->data() + shdr_size;
5751 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
5753 elfcpp::Shdr<32, big_endian> shdr(ps);
5754 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
5756 gold_assert(this->attributes_section_data_ == NULL);
5757 section_offset_type section_offset = shdr.get_sh_offset();
5758 section_size_type section_size =
5759 convert_to_section_size_type(shdr.get_sh_size());
5760 File_view* view = this->get_lasting_view(section_offset,
5761 section_size, true, false);
5762 this->attributes_section_data_ =
5763 new Attributes_section_data(view->data(), section_size);
5765 else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
5766 this->make_exidx_input_section(i, shdr);
5770 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
5771 // sections for unwinding. These sections are referenced implicitly by
5772 // text sections linked in the section headers. If we ignore these implict
5773 // references, the .ARM.exidx sections and any .ARM.extab sections they use
5774 // will be garbage-collected incorrectly. Hence we override the same function
5775 // in the base class to handle these implicit references.
5777 template<bool big_endian>
5779 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
5781 Read_relocs_data* rd)
5783 // First, call base class method to process relocations in this object.
5784 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
5786 unsigned int shnum = this->shnum();
5787 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5788 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
5792 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
5793 // to these from the linked text sections.
5794 const unsigned char* ps = pshdrs + shdr_size;
5795 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
5797 elfcpp::Shdr<32, big_endian> shdr(ps);
5798 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
5800 // Found an .ARM.exidx section, add it to the set of reachable
5801 // sections from its linked text section.
5802 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
5803 symtab->gc()->add_reference(this, text_shndx, this, i);
5808 // Arm_dynobj methods.
5810 // Read the symbol information.
5812 template<bool big_endian>
5814 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
5816 // Call parent class to read symbol information.
5817 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
5819 // Read processor-specific flags in ELF file header.
5820 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
5821 elfcpp::Elf_sizes<32>::ehdr_size,
5823 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
5824 this->processor_specific_flags_ = ehdr.get_e_flags();
5826 // Read the attributes section if there is one.
5827 // We read from the end because gas seems to put it near the end of
5828 // the section headers.
5829 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5830 const unsigned char *ps =
5831 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
5832 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
5834 elfcpp::Shdr<32, big_endian> shdr(ps);
5835 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
5837 section_offset_type section_offset = shdr.get_sh_offset();
5838 section_size_type section_size =
5839 convert_to_section_size_type(shdr.get_sh_size());
5840 File_view* view = this->get_lasting_view(section_offset,
5841 section_size, true, false);
5842 this->attributes_section_data_ =
5843 new Attributes_section_data(view->data(), section_size);
5849 // Stub_addend_reader methods.
5851 // Read the addend of a REL relocation of type R_TYPE at VIEW.
5853 template<bool big_endian>
5854 elfcpp::Elf_types<32>::Elf_Swxword
5855 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
5856 unsigned int r_type,
5857 const unsigned char* view,
5858 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
5860 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
5864 case elfcpp::R_ARM_CALL:
5865 case elfcpp::R_ARM_JUMP24:
5866 case elfcpp::R_ARM_PLT32:
5868 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5869 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
5870 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
5871 return utils::sign_extend<26>(val << 2);
5874 case elfcpp::R_ARM_THM_CALL:
5875 case elfcpp::R_ARM_THM_JUMP24:
5876 case elfcpp::R_ARM_THM_XPC22:
5878 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
5879 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
5880 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
5881 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
5882 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
5885 case elfcpp::R_ARM_THM_JUMP19:
5887 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
5888 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
5889 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
5890 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
5891 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
5899 // A class to handle the PLT data.
5901 template<bool big_endian>
5902 class Output_data_plt_arm : public Output_section_data
5905 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
5908 Output_data_plt_arm(Layout*, Output_data_space*);
5910 // Add an entry to the PLT.
5912 add_entry(Symbol* gsym);
5914 // Return the .rel.plt section data.
5915 const Reloc_section*
5917 { return this->rel_; }
5921 do_adjust_output_section(Output_section* os);
5923 // Write to a map file.
5925 do_print_to_mapfile(Mapfile* mapfile) const
5926 { mapfile->print_output_data(this, _("** PLT")); }
5929 // Template for the first PLT entry.
5930 static const uint32_t first_plt_entry[5];
5932 // Template for subsequent PLT entries.
5933 static const uint32_t plt_entry[3];
5935 // Set the final size.
5937 set_final_data_size()
5939 this->set_data_size(sizeof(first_plt_entry)
5940 + this->count_ * sizeof(plt_entry));
5943 // Write out the PLT data.
5945 do_write(Output_file*);
5947 // The reloc section.
5948 Reloc_section* rel_;
5949 // The .got.plt section.
5950 Output_data_space* got_plt_;
5951 // The number of PLT entries.
5952 unsigned int count_;
5955 // Create the PLT section. The ordinary .got section is an argument,
5956 // since we need to refer to the start. We also create our own .got
5957 // section just for PLT entries.
5959 template<bool big_endian>
5960 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
5961 Output_data_space* got_plt)
5962 : Output_section_data(4), got_plt_(got_plt), count_(0)
5964 this->rel_ = new Reloc_section(false);
5965 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
5966 elfcpp::SHF_ALLOC, this->rel_, true, false,
5970 template<bool big_endian>
5972 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
5977 // Add an entry to the PLT.
5979 template<bool big_endian>
5981 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
5983 gold_assert(!gsym->has_plt_offset());
5985 // Note that when setting the PLT offset we skip the initial
5986 // reserved PLT entry.
5987 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
5988 + sizeof(first_plt_entry));
5992 section_offset_type got_offset = this->got_plt_->current_data_size();
5994 // Every PLT entry needs a GOT entry which points back to the PLT
5995 // entry (this will be changed by the dynamic linker, normally
5996 // lazily when the function is called).
5997 this->got_plt_->set_current_data_size(got_offset + 4);
5999 // Every PLT entry needs a reloc.
6000 gsym->set_needs_dynsym_entry();
6001 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
6004 // Note that we don't need to save the symbol. The contents of the
6005 // PLT are independent of which symbols are used. The symbols only
6006 // appear in the relocations.
6010 // FIXME: This is not very flexible. Right now this has only been tested
6011 // on armv5te. If we are to support additional architecture features like
6012 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
6014 // The first entry in the PLT.
6015 template<bool big_endian>
6016 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
6018 0xe52de004, // str lr, [sp, #-4]!
6019 0xe59fe004, // ldr lr, [pc, #4]
6020 0xe08fe00e, // add lr, pc, lr
6021 0xe5bef008, // ldr pc, [lr, #8]!
6022 0x00000000, // &GOT[0] - .
6025 // Subsequent entries in the PLT.
6027 template<bool big_endian>
6028 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
6030 0xe28fc600, // add ip, pc, #0xNN00000
6031 0xe28cca00, // add ip, ip, #0xNN000
6032 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
6035 // Write out the PLT. This uses the hand-coded instructions above,
6036 // and adjusts them as needed. This is all specified by the arm ELF
6037 // Processor Supplement.
6039 template<bool big_endian>
6041 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
6043 const off_t offset = this->offset();
6044 const section_size_type oview_size =
6045 convert_to_section_size_type(this->data_size());
6046 unsigned char* const oview = of->get_output_view(offset, oview_size);
6048 const off_t got_file_offset = this->got_plt_->offset();
6049 const section_size_type got_size =
6050 convert_to_section_size_type(this->got_plt_->data_size());
6051 unsigned char* const got_view = of->get_output_view(got_file_offset,
6053 unsigned char* pov = oview;
6055 Arm_address plt_address = this->address();
6056 Arm_address got_address = this->got_plt_->address();
6058 // Write first PLT entry. All but the last word are constants.
6059 const size_t num_first_plt_words = (sizeof(first_plt_entry)
6060 / sizeof(plt_entry[0]));
6061 for (size_t i = 0; i < num_first_plt_words - 1; i++)
6062 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
6063 // Last word in first PLT entry is &GOT[0] - .
6064 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
6065 got_address - (plt_address + 16));
6066 pov += sizeof(first_plt_entry);
6068 unsigned char* got_pov = got_view;
6070 memset(got_pov, 0, 12);
6073 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
6074 unsigned int plt_offset = sizeof(first_plt_entry);
6075 unsigned int plt_rel_offset = 0;
6076 unsigned int got_offset = 12;
6077 const unsigned int count = this->count_;
6078 for (unsigned int i = 0;
6081 pov += sizeof(plt_entry),
6083 plt_offset += sizeof(plt_entry),
6084 plt_rel_offset += rel_size,
6087 // Set and adjust the PLT entry itself.
6088 int32_t offset = ((got_address + got_offset)
6089 - (plt_address + plt_offset + 8));
6091 gold_assert(offset >= 0 && offset < 0x0fffffff);
6092 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
6093 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
6094 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
6095 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
6096 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
6097 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
6099 // Set the entry in the GOT.
6100 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
6103 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
6104 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
6106 of->write_output_view(offset, oview_size, oview);
6107 of->write_output_view(got_file_offset, got_size, got_view);
6110 // Create a PLT entry for a global symbol.
6112 template<bool big_endian>
6114 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
6117 if (gsym->has_plt_offset())
6120 if (this->plt_ == NULL)
6122 // Create the GOT sections first.
6123 this->got_section(symtab, layout);
6125 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
6126 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
6128 | elfcpp::SHF_EXECINSTR),
6129 this->plt_, false, false, false, false);
6131 this->plt_->add_entry(gsym);
6134 // Report an unsupported relocation against a local symbol.
6136 template<bool big_endian>
6138 Target_arm<big_endian>::Scan::unsupported_reloc_local(
6139 Sized_relobj<32, big_endian>* object,
6140 unsigned int r_type)
6142 gold_error(_("%s: unsupported reloc %u against local symbol"),
6143 object->name().c_str(), r_type);
6146 // We are about to emit a dynamic relocation of type R_TYPE. If the
6147 // dynamic linker does not support it, issue an error. The GNU linker
6148 // only issues a non-PIC error for an allocated read-only section.
6149 // Here we know the section is allocated, but we don't know that it is
6150 // read-only. But we check for all the relocation types which the
6151 // glibc dynamic linker supports, so it seems appropriate to issue an
6152 // error even if the section is not read-only.
6154 template<bool big_endian>
6156 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
6157 unsigned int r_type)
6161 // These are the relocation types supported by glibc for ARM.
6162 case elfcpp::R_ARM_RELATIVE:
6163 case elfcpp::R_ARM_COPY:
6164 case elfcpp::R_ARM_GLOB_DAT:
6165 case elfcpp::R_ARM_JUMP_SLOT:
6166 case elfcpp::R_ARM_ABS32:
6167 case elfcpp::R_ARM_ABS32_NOI:
6168 case elfcpp::R_ARM_PC24:
6169 // FIXME: The following 3 types are not supported by Android's dynamic
6171 case elfcpp::R_ARM_TLS_DTPMOD32:
6172 case elfcpp::R_ARM_TLS_DTPOFF32:
6173 case elfcpp::R_ARM_TLS_TPOFF32:
6177 // This prevents us from issuing more than one error per reloc
6178 // section. But we can still wind up issuing more than one
6179 // error per object file.
6180 if (this->issued_non_pic_error_)
6182 object->error(_("requires unsupported dynamic reloc; "
6183 "recompile with -fPIC"));
6184 this->issued_non_pic_error_ = true;
6187 case elfcpp::R_ARM_NONE:
6192 // Scan a relocation for a local symbol.
6193 // FIXME: This only handles a subset of relocation types used by Android
6194 // on ARM v5te devices.
6196 template<bool big_endian>
6198 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
6201 Sized_relobj<32, big_endian>* object,
6202 unsigned int data_shndx,
6203 Output_section* output_section,
6204 const elfcpp::Rel<32, big_endian>& reloc,
6205 unsigned int r_type,
6206 const elfcpp::Sym<32, big_endian>&)
6208 r_type = get_real_reloc_type(r_type);
6211 case elfcpp::R_ARM_NONE:
6214 case elfcpp::R_ARM_ABS32:
6215 case elfcpp::R_ARM_ABS32_NOI:
6216 // If building a shared library (or a position-independent
6217 // executable), we need to create a dynamic relocation for
6218 // this location. The relocation applied at link time will
6219 // apply the link-time value, so we flag the location with
6220 // an R_ARM_RELATIVE relocation so the dynamic loader can
6221 // relocate it easily.
6222 if (parameters->options().output_is_position_independent())
6224 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6225 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
6226 // If we are to add more other reloc types than R_ARM_ABS32,
6227 // we need to add check_non_pic(object, r_type) here.
6228 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
6229 output_section, data_shndx,
6230 reloc.get_r_offset());
6234 case elfcpp::R_ARM_REL32:
6235 case elfcpp::R_ARM_THM_CALL:
6236 case elfcpp::R_ARM_CALL:
6237 case elfcpp::R_ARM_PREL31:
6238 case elfcpp::R_ARM_JUMP24:
6239 case elfcpp::R_ARM_THM_JUMP24:
6240 case elfcpp::R_ARM_THM_JUMP19:
6241 case elfcpp::R_ARM_PLT32:
6242 case elfcpp::R_ARM_THM_ABS5:
6243 case elfcpp::R_ARM_ABS8:
6244 case elfcpp::R_ARM_ABS12:
6245 case elfcpp::R_ARM_ABS16:
6246 case elfcpp::R_ARM_BASE_ABS:
6247 case elfcpp::R_ARM_MOVW_ABS_NC:
6248 case elfcpp::R_ARM_MOVT_ABS:
6249 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6250 case elfcpp::R_ARM_THM_MOVT_ABS:
6251 case elfcpp::R_ARM_MOVW_PREL_NC:
6252 case elfcpp::R_ARM_MOVT_PREL:
6253 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6254 case elfcpp::R_ARM_THM_MOVT_PREL:
6255 case elfcpp::R_ARM_THM_JUMP6:
6256 case elfcpp::R_ARM_THM_JUMP8:
6257 case elfcpp::R_ARM_THM_JUMP11:
6258 case elfcpp::R_ARM_V4BX:
6261 case elfcpp::R_ARM_GOTOFF32:
6262 // We need a GOT section:
6263 target->got_section(symtab, layout);
6266 case elfcpp::R_ARM_BASE_PREL:
6267 // FIXME: What about this?
6270 case elfcpp::R_ARM_GOT_BREL:
6271 case elfcpp::R_ARM_GOT_PREL:
6273 // The symbol requires a GOT entry.
6274 Output_data_got<32, big_endian>* got =
6275 target->got_section(symtab, layout);
6276 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
6277 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
6279 // If we are generating a shared object, we need to add a
6280 // dynamic RELATIVE relocation for this symbol's GOT entry.
6281 if (parameters->options().output_is_position_independent())
6283 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6284 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
6285 rel_dyn->add_local_relative(
6286 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
6287 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
6293 case elfcpp::R_ARM_TARGET1:
6294 // This should have been mapped to another type already.
6296 case elfcpp::R_ARM_COPY:
6297 case elfcpp::R_ARM_GLOB_DAT:
6298 case elfcpp::R_ARM_JUMP_SLOT:
6299 case elfcpp::R_ARM_RELATIVE:
6300 // These are relocations which should only be seen by the
6301 // dynamic linker, and should never be seen here.
6302 gold_error(_("%s: unexpected reloc %u in object file"),
6303 object->name().c_str(), r_type);
6307 unsupported_reloc_local(object, r_type);
6312 // Report an unsupported relocation against a global symbol.
6314 template<bool big_endian>
6316 Target_arm<big_endian>::Scan::unsupported_reloc_global(
6317 Sized_relobj<32, big_endian>* object,
6318 unsigned int r_type,
6321 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6322 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6325 // Scan a relocation for a global symbol.
6326 // FIXME: This only handles a subset of relocation types used by Android
6327 // on ARM v5te devices.
6329 template<bool big_endian>
6331 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
6334 Sized_relobj<32, big_endian>* object,
6335 unsigned int data_shndx,
6336 Output_section* output_section,
6337 const elfcpp::Rel<32, big_endian>& reloc,
6338 unsigned int r_type,
6341 r_type = get_real_reloc_type(r_type);
6344 case elfcpp::R_ARM_NONE:
6347 case elfcpp::R_ARM_ABS32:
6348 case elfcpp::R_ARM_ABS32_NOI:
6350 // Make a dynamic relocation if necessary.
6351 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
6353 if (target->may_need_copy_reloc(gsym))
6355 target->copy_reloc(symtab, layout, object,
6356 data_shndx, output_section, gsym, reloc);
6358 else if (gsym->can_use_relative_reloc(false))
6360 // If we are to add more other reloc types than R_ARM_ABS32,
6361 // we need to add check_non_pic(object, r_type) here.
6362 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6363 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
6364 output_section, object,
6365 data_shndx, reloc.get_r_offset());
6369 // If we are to add more other reloc types than R_ARM_ABS32,
6370 // we need to add check_non_pic(object, r_type) here.
6371 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6372 rel_dyn->add_global(gsym, r_type, output_section, object,
6373 data_shndx, reloc.get_r_offset());
6379 case elfcpp::R_ARM_MOVW_ABS_NC:
6380 case elfcpp::R_ARM_MOVT_ABS:
6381 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6382 case elfcpp::R_ARM_THM_MOVT_ABS:
6383 case elfcpp::R_ARM_MOVW_PREL_NC:
6384 case elfcpp::R_ARM_MOVT_PREL:
6385 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6386 case elfcpp::R_ARM_THM_MOVT_PREL:
6387 case elfcpp::R_ARM_THM_JUMP6:
6388 case elfcpp::R_ARM_THM_JUMP8:
6389 case elfcpp::R_ARM_THM_JUMP11:
6390 case elfcpp::R_ARM_V4BX:
6393 case elfcpp::R_ARM_THM_ABS5:
6394 case elfcpp::R_ARM_ABS8:
6395 case elfcpp::R_ARM_ABS12:
6396 case elfcpp::R_ARM_ABS16:
6397 case elfcpp::R_ARM_BASE_ABS:
6399 // No dynamic relocs of this kinds.
6400 // Report the error in case of PIC.
6401 int flags = Symbol::NON_PIC_REF;
6402 if (gsym->type() == elfcpp::STT_FUNC
6403 || gsym->type() == elfcpp::STT_ARM_TFUNC)
6404 flags |= Symbol::FUNCTION_CALL;
6405 if (gsym->needs_dynamic_reloc(flags))
6406 check_non_pic(object, r_type);
6410 case elfcpp::R_ARM_REL32:
6411 case elfcpp::R_ARM_PREL31:
6413 // Make a dynamic relocation if necessary.
6414 int flags = Symbol::NON_PIC_REF;
6415 if (gsym->needs_dynamic_reloc(flags))
6417 if (target->may_need_copy_reloc(gsym))
6419 target->copy_reloc(symtab, layout, object,
6420 data_shndx, output_section, gsym, reloc);
6424 check_non_pic(object, r_type);
6425 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6426 rel_dyn->add_global(gsym, r_type, output_section, object,
6427 data_shndx, reloc.get_r_offset());
6433 case elfcpp::R_ARM_JUMP24:
6434 case elfcpp::R_ARM_THM_JUMP24:
6435 case elfcpp::R_ARM_THM_JUMP19:
6436 case elfcpp::R_ARM_CALL:
6437 case elfcpp::R_ARM_THM_CALL:
6439 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
6440 target->make_plt_entry(symtab, layout, gsym);
6443 // Check to see if this is a function that would need a PLT
6444 // but does not get one because the function symbol is untyped.
6445 // This happens in assembly code missing a proper .type directive.
6446 if ((!gsym->is_undefined() || parameters->options().shared())
6447 && !parameters->doing_static_link()
6448 && gsym->type() == elfcpp::STT_NOTYPE
6449 && (gsym->is_from_dynobj()
6450 || gsym->is_undefined()
6451 || gsym->is_preemptible()))
6452 gold_error(_("%s is not a function."),
6453 gsym->demangled_name().c_str());
6457 case elfcpp::R_ARM_PLT32:
6458 // If the symbol is fully resolved, this is just a relative
6459 // local reloc. Otherwise we need a PLT entry.
6460 if (gsym->final_value_is_known())
6462 // If building a shared library, we can also skip the PLT entry
6463 // if the symbol is defined in the output file and is protected
6465 if (gsym->is_defined()
6466 && !gsym->is_from_dynobj()
6467 && !gsym->is_preemptible())
6469 target->make_plt_entry(symtab, layout, gsym);
6472 case elfcpp::R_ARM_GOTOFF32:
6473 // We need a GOT section.
6474 target->got_section(symtab, layout);
6477 case elfcpp::R_ARM_BASE_PREL:
6478 // FIXME: What about this?
6481 case elfcpp::R_ARM_GOT_BREL:
6482 case elfcpp::R_ARM_GOT_PREL:
6484 // The symbol requires a GOT entry.
6485 Output_data_got<32, big_endian>* got =
6486 target->got_section(symtab, layout);
6487 if (gsym->final_value_is_known())
6488 got->add_global(gsym, GOT_TYPE_STANDARD);
6491 // If this symbol is not fully resolved, we need to add a
6492 // GOT entry with a dynamic relocation.
6493 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
6494 if (gsym->is_from_dynobj()
6495 || gsym->is_undefined()
6496 || gsym->is_preemptible())
6497 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
6498 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
6501 if (got->add_global(gsym, GOT_TYPE_STANDARD))
6502 rel_dyn->add_global_relative(
6503 gsym, elfcpp::R_ARM_RELATIVE, got,
6504 gsym->got_offset(GOT_TYPE_STANDARD));
6510 case elfcpp::R_ARM_TARGET1:
6511 // This should have been mapped to another type already.
6513 case elfcpp::R_ARM_COPY:
6514 case elfcpp::R_ARM_GLOB_DAT:
6515 case elfcpp::R_ARM_JUMP_SLOT:
6516 case elfcpp::R_ARM_RELATIVE:
6517 // These are relocations which should only be seen by the
6518 // dynamic linker, and should never be seen here.
6519 gold_error(_("%s: unexpected reloc %u in object file"),
6520 object->name().c_str(), r_type);
6524 unsupported_reloc_global(object, r_type, gsym);
6529 // Process relocations for gc.
6531 template<bool big_endian>
6533 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
6535 Sized_relobj<32, big_endian>* object,
6536 unsigned int data_shndx,
6538 const unsigned char* prelocs,
6540 Output_section* output_section,
6541 bool needs_special_offset_handling,
6542 size_t local_symbol_count,
6543 const unsigned char* plocal_symbols)
6545 typedef Target_arm<big_endian> Arm;
6546 typedef typename Target_arm<big_endian>::Scan Scan;
6548 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
6557 needs_special_offset_handling,
6562 // Scan relocations for a section.
6564 template<bool big_endian>
6566 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
6568 Sized_relobj<32, big_endian>* object,
6569 unsigned int data_shndx,
6570 unsigned int sh_type,
6571 const unsigned char* prelocs,
6573 Output_section* output_section,
6574 bool needs_special_offset_handling,
6575 size_t local_symbol_count,
6576 const unsigned char* plocal_symbols)
6578 typedef typename Target_arm<big_endian>::Scan Scan;
6579 if (sh_type == elfcpp::SHT_RELA)
6581 gold_error(_("%s: unsupported RELA reloc section"),
6582 object->name().c_str());
6586 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
6595 needs_special_offset_handling,
6600 // Finalize the sections.
6602 template<bool big_endian>
6604 Target_arm<big_endian>::do_finalize_sections(
6606 const Input_objects* input_objects,
6607 Symbol_table* symtab)
6609 // Merge processor-specific flags.
6610 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6611 p != input_objects->relobj_end();
6614 Arm_relobj<big_endian>* arm_relobj =
6615 Arm_relobj<big_endian>::as_arm_relobj(*p);
6616 this->merge_processor_specific_flags(
6618 arm_relobj->processor_specific_flags());
6619 this->merge_object_attributes(arm_relobj->name().c_str(),
6620 arm_relobj->attributes_section_data());
6624 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
6625 p != input_objects->dynobj_end();
6628 Arm_dynobj<big_endian>* arm_dynobj =
6629 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
6630 this->merge_processor_specific_flags(
6632 arm_dynobj->processor_specific_flags());
6633 this->merge_object_attributes(arm_dynobj->name().c_str(),
6634 arm_dynobj->attributes_section_data());
6638 const Object_attribute* cpu_arch_attr =
6639 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
6640 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
6641 this->set_may_use_blx(true);
6643 // Check if we need to use Cortex-A8 workaround.
6644 if (parameters->options().user_set_fix_cortex_a8())
6645 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
6648 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
6649 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
6651 const Object_attribute* cpu_arch_profile_attr =
6652 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
6653 this->fix_cortex_a8_ =
6654 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
6655 && (cpu_arch_profile_attr->int_value() == 'A'
6656 || cpu_arch_profile_attr->int_value() == 0));
6659 // Check if we can use V4BX interworking.
6660 // The V4BX interworking stub contains BX instruction,
6661 // which is not specified for some profiles.
6662 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
6663 && !this->may_use_blx())
6664 gold_error(_("unable to provide V4BX reloc interworking fix up; "
6665 "the target profile does not support BX instruction"));
6667 // Fill in some more dynamic tags.
6668 const Reloc_section* rel_plt = (this->plt_ == NULL
6670 : this->plt_->rel_plt());
6671 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
6672 this->rel_dyn_, true);
6674 // Emit any relocs we saved in an attempt to avoid generating COPY
6676 if (this->copy_relocs_.any_saved_relocs())
6677 this->copy_relocs_.emit(this->rel_dyn_section(layout));
6679 // Handle the .ARM.exidx section.
6680 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
6681 if (exidx_section != NULL
6682 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
6683 && !parameters->options().relocatable())
6685 // Create __exidx_start and __exdix_end symbols.
6686 symtab->define_in_output_data("__exidx_start", NULL,
6687 Symbol_table::PREDEFINED,
6688 exidx_section, 0, 0, elfcpp::STT_OBJECT,
6689 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
6691 symtab->define_in_output_data("__exidx_end", NULL,
6692 Symbol_table::PREDEFINED,
6693 exidx_section, 0, 0, elfcpp::STT_OBJECT,
6694 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
6697 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
6698 // the .ARM.exidx section.
6699 if (!layout->script_options()->saw_phdrs_clause())
6701 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
6703 Output_segment* exidx_segment =
6704 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
6705 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
6710 // Create an .ARM.attributes section if there is not one already.
6711 Output_attributes_section_data* attributes_section =
6712 new Output_attributes_section_data(*this->attributes_section_data_);
6713 layout->add_output_section_data(".ARM.attributes",
6714 elfcpp::SHT_ARM_ATTRIBUTES, 0,
6715 attributes_section, false, false, false,
6719 // Return whether a direct absolute static relocation needs to be applied.
6720 // In cases where Scan::local() or Scan::global() has created
6721 // a dynamic relocation other than R_ARM_RELATIVE, the addend
6722 // of the relocation is carried in the data, and we must not
6723 // apply the static relocation.
6725 template<bool big_endian>
6727 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
6728 const Sized_symbol<32>* gsym,
6731 Output_section* output_section)
6733 // If the output section is not allocated, then we didn't call
6734 // scan_relocs, we didn't create a dynamic reloc, and we must apply
6736 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
6739 // For local symbols, we will have created a non-RELATIVE dynamic
6740 // relocation only if (a) the output is position independent,
6741 // (b) the relocation is absolute (not pc- or segment-relative), and
6742 // (c) the relocation is not 32 bits wide.
6744 return !(parameters->options().output_is_position_independent()
6745 && (ref_flags & Symbol::ABSOLUTE_REF)
6748 // For global symbols, we use the same helper routines used in the
6749 // scan pass. If we did not create a dynamic relocation, or if we
6750 // created a RELATIVE dynamic relocation, we should apply the static
6752 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
6753 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
6754 && gsym->can_use_relative_reloc(ref_flags
6755 & Symbol::FUNCTION_CALL);
6756 return !has_dyn || is_rel;
6759 // Perform a relocation.
6761 template<bool big_endian>
6763 Target_arm<big_endian>::Relocate::relocate(
6764 const Relocate_info<32, big_endian>* relinfo,
6766 Output_section *output_section,
6768 const elfcpp::Rel<32, big_endian>& rel,
6769 unsigned int r_type,
6770 const Sized_symbol<32>* gsym,
6771 const Symbol_value<32>* psymval,
6772 unsigned char* view,
6773 Arm_address address,
6774 section_size_type /* view_size */ )
6776 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
6778 r_type = get_real_reloc_type(r_type);
6780 const Arm_relobj<big_endian>* object =
6781 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
6783 // If the final branch target of a relocation is THUMB instruction, this
6784 // is 1. Otherwise it is 0.
6785 Arm_address thumb_bit = 0;
6786 Symbol_value<32> symval;
6787 bool is_weakly_undefined_without_plt = false;
6788 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
6792 // This is a global symbol. Determine if we use PLT and if the
6793 // final target is THUMB.
6794 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
6796 // This uses a PLT, change the symbol value.
6797 symval.set_output_value(target->plt_section()->address()
6798 + gsym->plt_offset());
6801 else if (gsym->is_weak_undefined())
6803 // This is a weakly undefined symbol and we do not use PLT
6804 // for this relocation. A branch targeting this symbol will
6805 // be converted into an NOP.
6806 is_weakly_undefined_without_plt = true;
6810 // Set thumb bit if symbol:
6811 // -Has type STT_ARM_TFUNC or
6812 // -Has type STT_FUNC, is defined and with LSB in value set.
6814 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
6815 || (gsym->type() == elfcpp::STT_FUNC
6816 && !gsym->is_undefined()
6817 && ((psymval->value(object, 0) & 1) != 0)))
6824 // This is a local symbol. Determine if the final target is THUMB.
6825 // We saved this information when all the local symbols were read.
6826 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
6827 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6828 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
6833 // This is a fake relocation synthesized for a stub. It does not have
6834 // a real symbol. We just look at the LSB of the symbol value to
6835 // determine if the target is THUMB or not.
6836 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
6839 // Strip LSB if this points to a THUMB target.
6841 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
6842 && ((psymval->value(object, 0) & 1) != 0))
6844 Arm_address stripped_value =
6845 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
6846 symval.set_output_value(stripped_value);
6850 // Get the GOT offset if needed.
6851 // The GOT pointer points to the end of the GOT section.
6852 // We need to subtract the size of the GOT section to get
6853 // the actual offset to use in the relocation.
6854 bool have_got_offset = false;
6855 unsigned int got_offset = 0;
6858 case elfcpp::R_ARM_GOT_BREL:
6859 case elfcpp::R_ARM_GOT_PREL:
6862 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6863 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
6864 - target->got_size());
6868 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
6869 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6870 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
6871 - target->got_size());
6873 have_got_offset = true;
6880 // To look up relocation stubs, we need to pass the symbol table index of
6882 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
6884 typename Arm_relocate_functions::Status reloc_status =
6885 Arm_relocate_functions::STATUS_OKAY;
6888 case elfcpp::R_ARM_NONE:
6891 case elfcpp::R_ARM_ABS8:
6892 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6894 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
6897 case elfcpp::R_ARM_ABS12:
6898 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6900 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
6903 case elfcpp::R_ARM_ABS16:
6904 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6906 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
6909 case elfcpp::R_ARM_ABS32:
6910 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6912 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
6916 case elfcpp::R_ARM_ABS32_NOI:
6917 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6919 // No thumb bit for this relocation: (S + A)
6920 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
6924 case elfcpp::R_ARM_MOVW_ABS_NC:
6925 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6927 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
6931 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
6932 "a shared object; recompile with -fPIC"));
6935 case elfcpp::R_ARM_MOVT_ABS:
6936 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6938 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
6940 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
6941 "a shared object; recompile with -fPIC"));
6944 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6945 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6947 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
6951 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
6952 "making a shared object; recompile with -fPIC"));
6955 case elfcpp::R_ARM_THM_MOVT_ABS:
6956 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6958 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
6961 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
6962 "making a shared object; recompile with -fPIC"));
6965 case elfcpp::R_ARM_MOVW_PREL_NC:
6966 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
6971 case elfcpp::R_ARM_MOVT_PREL:
6972 reloc_status = Arm_relocate_functions::movt_prel(view, object,
6976 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6977 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
6982 case elfcpp::R_ARM_THM_MOVT_PREL:
6983 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
6987 case elfcpp::R_ARM_REL32:
6988 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
6989 address, thumb_bit);
6992 case elfcpp::R_ARM_THM_ABS5:
6993 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6995 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
6998 case elfcpp::R_ARM_THM_CALL:
7000 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
7001 psymval, address, thumb_bit,
7002 is_weakly_undefined_without_plt);
7005 case elfcpp::R_ARM_XPC25:
7007 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
7008 psymval, address, thumb_bit,
7009 is_weakly_undefined_without_plt);
7012 case elfcpp::R_ARM_THM_XPC22:
7014 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
7015 psymval, address, thumb_bit,
7016 is_weakly_undefined_without_plt);
7019 case elfcpp::R_ARM_GOTOFF32:
7021 Arm_address got_origin;
7022 got_origin = target->got_plt_section()->address();
7023 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
7024 got_origin, thumb_bit);
7028 case elfcpp::R_ARM_BASE_PREL:
7031 // Get the addressing origin of the output segment defining the
7032 // symbol gsym (AAELF 4.6.1.2 Relocation types)
7033 gold_assert(gsym != NULL);
7034 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
7035 origin = gsym->output_segment()->vaddr();
7036 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
7037 origin = gsym->output_data()->address();
7040 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
7041 _("cannot find origin of R_ARM_BASE_PREL"));
7044 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
7048 case elfcpp::R_ARM_BASE_ABS:
7050 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
7055 // Get the addressing origin of the output segment defining
7056 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
7058 // R_ARM_BASE_ABS with the NULL symbol will give the
7059 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
7060 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
7061 origin = target->got_plt_section()->address();
7062 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
7063 origin = gsym->output_segment()->vaddr();
7064 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
7065 origin = gsym->output_data()->address();
7068 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
7069 _("cannot find origin of R_ARM_BASE_ABS"));
7073 reloc_status = Arm_relocate_functions::base_abs(view, origin);
7077 case elfcpp::R_ARM_GOT_BREL:
7078 gold_assert(have_got_offset);
7079 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
7082 case elfcpp::R_ARM_GOT_PREL:
7083 gold_assert(have_got_offset);
7084 // Get the address origin for GOT PLT, which is allocated right
7085 // after the GOT section, to calculate an absolute address of
7086 // the symbol GOT entry (got_origin + got_offset).
7087 Arm_address got_origin;
7088 got_origin = target->got_plt_section()->address();
7089 reloc_status = Arm_relocate_functions::got_prel(view,
7090 got_origin + got_offset,
7094 case elfcpp::R_ARM_PLT32:
7095 gold_assert(gsym == NULL
7096 || gsym->has_plt_offset()
7097 || gsym->final_value_is_known()
7098 || (gsym->is_defined()
7099 && !gsym->is_from_dynobj()
7100 && !gsym->is_preemptible()));
7102 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
7103 psymval, address, thumb_bit,
7104 is_weakly_undefined_without_plt);
7107 case elfcpp::R_ARM_CALL:
7109 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
7110 psymval, address, thumb_bit,
7111 is_weakly_undefined_without_plt);
7114 case elfcpp::R_ARM_JUMP24:
7116 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
7117 psymval, address, thumb_bit,
7118 is_weakly_undefined_without_plt);
7121 case elfcpp::R_ARM_THM_JUMP24:
7123 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
7124 psymval, address, thumb_bit,
7125 is_weakly_undefined_without_plt);
7128 case elfcpp::R_ARM_THM_JUMP19:
7130 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
7134 case elfcpp::R_ARM_THM_JUMP6:
7136 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
7139 case elfcpp::R_ARM_THM_JUMP8:
7141 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
7144 case elfcpp::R_ARM_THM_JUMP11:
7146 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
7149 case elfcpp::R_ARM_PREL31:
7150 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
7151 address, thumb_bit);
7154 case elfcpp::R_ARM_V4BX:
7155 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
7157 const bool is_v4bx_interworking =
7158 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
7160 Arm_relocate_functions::v4bx(relinfo, view, object, address,
7161 is_v4bx_interworking);
7165 case elfcpp::R_ARM_TARGET1:
7166 // This should have been mapped to another type already.
7168 case elfcpp::R_ARM_COPY:
7169 case elfcpp::R_ARM_GLOB_DAT:
7170 case elfcpp::R_ARM_JUMP_SLOT:
7171 case elfcpp::R_ARM_RELATIVE:
7172 // These are relocations which should only be seen by the
7173 // dynamic linker, and should never be seen here.
7174 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
7175 _("unexpected reloc %u in object file"),
7180 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
7181 _("unsupported reloc %u"),
7186 // Report any errors.
7187 switch (reloc_status)
7189 case Arm_relocate_functions::STATUS_OKAY:
7191 case Arm_relocate_functions::STATUS_OVERFLOW:
7192 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
7193 _("relocation overflow in relocation %u"),
7196 case Arm_relocate_functions::STATUS_BAD_RELOC:
7197 gold_error_at_location(
7201 _("unexpected opcode while processing relocation %u"),
7211 // Relocate section data.
7213 template<bool big_endian>
7215 Target_arm<big_endian>::relocate_section(
7216 const Relocate_info<32, big_endian>* relinfo,
7217 unsigned int sh_type,
7218 const unsigned char* prelocs,
7220 Output_section* output_section,
7221 bool needs_special_offset_handling,
7222 unsigned char* view,
7223 Arm_address address,
7224 section_size_type view_size,
7225 const Reloc_symbol_changes* reloc_symbol_changes)
7227 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
7228 gold_assert(sh_type == elfcpp::SHT_REL);
7230 Arm_input_section<big_endian>* arm_input_section =
7231 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
7233 // This is an ARM input section and the view covers the whole output
7235 if (arm_input_section != NULL)
7237 gold_assert(needs_special_offset_handling);
7238 Arm_address section_address = arm_input_section->address();
7239 section_size_type section_size = arm_input_section->data_size();
7241 gold_assert((arm_input_section->address() >= address)
7242 && ((arm_input_section->address()
7243 + arm_input_section->data_size())
7244 <= (address + view_size)));
7246 off_t offset = section_address - address;
7249 view_size = section_size;
7252 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
7259 needs_special_offset_handling,
7263 reloc_symbol_changes);
7266 // Return the size of a relocation while scanning during a relocatable
7269 template<bool big_endian>
7271 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
7272 unsigned int r_type,
7275 r_type = get_real_reloc_type(r_type);
7278 case elfcpp::R_ARM_NONE:
7281 case elfcpp::R_ARM_ABS8:
7284 case elfcpp::R_ARM_ABS16:
7285 case elfcpp::R_ARM_THM_ABS5:
7286 case elfcpp::R_ARM_THM_JUMP6:
7287 case elfcpp::R_ARM_THM_JUMP8:
7288 case elfcpp::R_ARM_THM_JUMP11:
7291 case elfcpp::R_ARM_ABS32:
7292 case elfcpp::R_ARM_ABS32_NOI:
7293 case elfcpp::R_ARM_ABS12:
7294 case elfcpp::R_ARM_BASE_ABS:
7295 case elfcpp::R_ARM_REL32:
7296 case elfcpp::R_ARM_THM_CALL:
7297 case elfcpp::R_ARM_GOTOFF32:
7298 case elfcpp::R_ARM_BASE_PREL:
7299 case elfcpp::R_ARM_GOT_BREL:
7300 case elfcpp::R_ARM_GOT_PREL:
7301 case elfcpp::R_ARM_PLT32:
7302 case elfcpp::R_ARM_CALL:
7303 case elfcpp::R_ARM_JUMP24:
7304 case elfcpp::R_ARM_PREL31:
7305 case elfcpp::R_ARM_MOVW_ABS_NC:
7306 case elfcpp::R_ARM_MOVT_ABS:
7307 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7308 case elfcpp::R_ARM_THM_MOVT_ABS:
7309 case elfcpp::R_ARM_MOVW_PREL_NC:
7310 case elfcpp::R_ARM_MOVT_PREL:
7311 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7312 case elfcpp::R_ARM_THM_MOVT_PREL:
7313 case elfcpp::R_ARM_V4BX:
7316 case elfcpp::R_ARM_TARGET1:
7317 // This should have been mapped to another type already.
7319 case elfcpp::R_ARM_COPY:
7320 case elfcpp::R_ARM_GLOB_DAT:
7321 case elfcpp::R_ARM_JUMP_SLOT:
7322 case elfcpp::R_ARM_RELATIVE:
7323 // These are relocations which should only be seen by the
7324 // dynamic linker, and should never be seen here.
7325 gold_error(_("%s: unexpected reloc %u in object file"),
7326 object->name().c_str(), r_type);
7330 object->error(_("unsupported reloc %u in object file"), r_type);
7335 // Scan the relocs during a relocatable link.
7337 template<bool big_endian>
7339 Target_arm<big_endian>::scan_relocatable_relocs(
7340 Symbol_table* symtab,
7342 Sized_relobj<32, big_endian>* object,
7343 unsigned int data_shndx,
7344 unsigned int sh_type,
7345 const unsigned char* prelocs,
7347 Output_section* output_section,
7348 bool needs_special_offset_handling,
7349 size_t local_symbol_count,
7350 const unsigned char* plocal_symbols,
7351 Relocatable_relocs* rr)
7353 gold_assert(sh_type == elfcpp::SHT_REL);
7355 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
7356 Relocatable_size_for_reloc> Scan_relocatable_relocs;
7358 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
7359 Scan_relocatable_relocs>(
7367 needs_special_offset_handling,
7373 // Relocate a section during a relocatable link.
7375 template<bool big_endian>
7377 Target_arm<big_endian>::relocate_for_relocatable(
7378 const Relocate_info<32, big_endian>* relinfo,
7379 unsigned int sh_type,
7380 const unsigned char* prelocs,
7382 Output_section* output_section,
7383 off_t offset_in_output_section,
7384 const Relocatable_relocs* rr,
7385 unsigned char* view,
7386 Arm_address view_address,
7387 section_size_type view_size,
7388 unsigned char* reloc_view,
7389 section_size_type reloc_view_size)
7391 gold_assert(sh_type == elfcpp::SHT_REL);
7393 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
7398 offset_in_output_section,
7407 // Return the value to use for a dynamic symbol which requires special
7408 // treatment. This is how we support equality comparisons of function
7409 // pointers across shared library boundaries, as described in the
7410 // processor specific ABI supplement.
7412 template<bool big_endian>
7414 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
7416 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
7417 return this->plt_section()->address() + gsym->plt_offset();
7420 // Map platform-specific relocs to real relocs
7422 template<bool big_endian>
7424 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
7428 case elfcpp::R_ARM_TARGET1:
7429 // This is either R_ARM_ABS32 or R_ARM_REL32;
7430 return elfcpp::R_ARM_ABS32;
7432 case elfcpp::R_ARM_TARGET2:
7433 // This can be any reloc type but ususally is R_ARM_GOT_PREL
7434 return elfcpp::R_ARM_GOT_PREL;
7441 // Whether if two EABI versions V1 and V2 are compatible.
7443 template<bool big_endian>
7445 Target_arm<big_endian>::are_eabi_versions_compatible(
7446 elfcpp::Elf_Word v1,
7447 elfcpp::Elf_Word v2)
7449 // v4 and v5 are the same spec before and after it was released,
7450 // so allow mixing them.
7451 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
7452 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
7458 // Combine FLAGS from an input object called NAME and the processor-specific
7459 // flags in the ELF header of the output. Much of this is adapted from the
7460 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
7461 // in bfd/elf32-arm.c.
7463 template<bool big_endian>
7465 Target_arm<big_endian>::merge_processor_specific_flags(
7466 const std::string& name,
7467 elfcpp::Elf_Word flags)
7469 if (this->are_processor_specific_flags_set())
7471 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
7473 // Nothing to merge if flags equal to those in output.
7474 if (flags == out_flags)
7477 // Complain about various flag mismatches.
7478 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
7479 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
7480 if (!this->are_eabi_versions_compatible(version1, version2))
7481 gold_error(_("Source object %s has EABI version %d but output has "
7482 "EABI version %d."),
7484 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
7485 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
7489 // If the input is the default architecture and had the default
7490 // flags then do not bother setting the flags for the output
7491 // architecture, instead allow future merges to do this. If no
7492 // future merges ever set these flags then they will retain their
7493 // uninitialised values, which surprise surprise, correspond
7494 // to the default values.
7498 // This is the first time, just copy the flags.
7499 // We only copy the EABI version for now.
7500 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
7504 // Adjust ELF file header.
7505 template<bool big_endian>
7507 Target_arm<big_endian>::do_adjust_elf_header(
7508 unsigned char* view,
7511 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
7513 elfcpp::Ehdr<32, big_endian> ehdr(view);
7514 unsigned char e_ident[elfcpp::EI_NIDENT];
7515 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
7517 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
7518 == elfcpp::EF_ARM_EABI_UNKNOWN)
7519 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
7521 e_ident[elfcpp::EI_OSABI] = 0;
7522 e_ident[elfcpp::EI_ABIVERSION] = 0;
7524 // FIXME: Do EF_ARM_BE8 adjustment.
7526 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
7527 oehdr.put_e_ident(e_ident);
7530 // do_make_elf_object to override the same function in the base class.
7531 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
7532 // to store ARM specific information. Hence we need to have our own
7533 // ELF object creation.
7535 template<bool big_endian>
7537 Target_arm<big_endian>::do_make_elf_object(
7538 const std::string& name,
7539 Input_file* input_file,
7540 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
7542 int et = ehdr.get_e_type();
7543 if (et == elfcpp::ET_REL)
7545 Arm_relobj<big_endian>* obj =
7546 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
7550 else if (et == elfcpp::ET_DYN)
7552 Sized_dynobj<32, big_endian>* obj =
7553 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
7559 gold_error(_("%s: unsupported ELF file type %d"),
7565 // Read the architecture from the Tag_also_compatible_with attribute, if any.
7566 // Returns -1 if no architecture could be read.
7567 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
7569 template<bool big_endian>
7571 Target_arm<big_endian>::get_secondary_compatible_arch(
7572 const Attributes_section_data* pasd)
7574 const Object_attribute *known_attributes =
7575 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
7577 // Note: the tag and its argument below are uleb128 values, though
7578 // currently-defined values fit in one byte for each.
7579 const std::string& sv =
7580 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
7582 && sv.data()[0] == elfcpp::Tag_CPU_arch
7583 && (sv.data()[1] & 128) != 128)
7584 return sv.data()[1];
7586 // This tag is "safely ignorable", so don't complain if it looks funny.
7590 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
7591 // The tag is removed if ARCH is -1.
7592 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
7594 template<bool big_endian>
7596 Target_arm<big_endian>::set_secondary_compatible_arch(
7597 Attributes_section_data* pasd,
7600 Object_attribute *known_attributes =
7601 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
7605 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
7609 // Note: the tag and its argument below are uleb128 values, though
7610 // currently-defined values fit in one byte for each.
7612 sv[0] = elfcpp::Tag_CPU_arch;
7613 gold_assert(arch != 0);
7617 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
7620 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
7622 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
7624 template<bool big_endian>
7626 Target_arm<big_endian>::tag_cpu_arch_combine(
7629 int* secondary_compat_out,
7631 int secondary_compat)
7633 #define T(X) elfcpp::TAG_CPU_ARCH_##X
7634 static const int v6t2[] =
7646 static const int v6k[] =
7659 static const int v7[] =
7673 static const int v6_m[] =
7688 static const int v6s_m[] =
7704 static const int v7e_m[] =
7721 static const int v4t_plus_v6_m[] =
7737 T(V4T_PLUS_V6_M) // V4T plus V6_M.
7739 static const int *comb[] =
7747 // Pseudo-architecture.
7751 // Check we've not got a higher architecture than we know about.
7753 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
7755 gold_error(_("%s: unknown CPU architecture"), name);
7759 // Override old tag if we have a Tag_also_compatible_with on the output.
7761 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
7762 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
7763 oldtag = T(V4T_PLUS_V6_M);
7765 // And override the new tag if we have a Tag_also_compatible_with on the
7768 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
7769 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
7770 newtag = T(V4T_PLUS_V6_M);
7772 // Architectures before V6KZ add features monotonically.
7773 int tagh = std::max(oldtag, newtag);
7774 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
7777 int tagl = std::min(oldtag, newtag);
7778 int result = comb[tagh - T(V6T2)][tagl];
7780 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
7781 // as the canonical version.
7782 if (result == T(V4T_PLUS_V6_M))
7785 *secondary_compat_out = T(V6_M);
7788 *secondary_compat_out = -1;
7792 gold_error(_("%s: conflicting CPU architectures %d/%d"),
7793 name, oldtag, newtag);
7801 // Helper to print AEABI enum tag value.
7803 template<bool big_endian>
7805 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
7807 static const char *aeabi_enum_names[] =
7808 { "", "variable-size", "32-bit", "" };
7809 const size_t aeabi_enum_names_size =
7810 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
7812 if (value < aeabi_enum_names_size)
7813 return std::string(aeabi_enum_names[value]);
7817 sprintf(buffer, "<unknown value %u>", value);
7818 return std::string(buffer);
7822 // Return the string value to store in TAG_CPU_name.
7824 template<bool big_endian>
7826 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
7828 static const char *name_table[] = {
7829 // These aren't real CPU names, but we can't guess
7830 // that from the architecture version alone.
7846 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
7848 if (value < name_table_size)
7849 return std::string(name_table[value]);
7853 sprintf(buffer, "<unknown CPU value %u>", value);
7854 return std::string(buffer);
7858 // Merge object attributes from input file called NAME with those of the
7859 // output. The input object attributes are in the object pointed by PASD.
7861 template<bool big_endian>
7863 Target_arm<big_endian>::merge_object_attributes(
7865 const Attributes_section_data* pasd)
7867 // Return if there is no attributes section data.
7871 // If output has no object attributes, just copy.
7872 if (this->attributes_section_data_ == NULL)
7874 this->attributes_section_data_ = new Attributes_section_data(*pasd);
7878 const int vendor = Object_attribute::OBJ_ATTR_PROC;
7879 const Object_attribute* in_attr = pasd->known_attributes(vendor);
7880 Object_attribute* out_attr =
7881 this->attributes_section_data_->known_attributes(vendor);
7883 // This needs to happen before Tag_ABI_FP_number_model is merged. */
7884 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
7885 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
7887 // Ignore mismatches if the object doesn't use floating point. */
7888 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
7889 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
7890 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
7891 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
7892 gold_error(_("%s uses VFP register arguments, output does not"),
7896 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
7898 // Merge this attribute with existing attributes.
7901 case elfcpp::Tag_CPU_raw_name:
7902 case elfcpp::Tag_CPU_name:
7903 // These are merged after Tag_CPU_arch.
7906 case elfcpp::Tag_ABI_optimization_goals:
7907 case elfcpp::Tag_ABI_FP_optimization_goals:
7908 // Use the first value seen.
7911 case elfcpp::Tag_CPU_arch:
7913 unsigned int saved_out_attr = out_attr->int_value();
7914 // Merge Tag_CPU_arch and Tag_also_compatible_with.
7915 int secondary_compat =
7916 this->get_secondary_compatible_arch(pasd);
7917 int secondary_compat_out =
7918 this->get_secondary_compatible_arch(
7919 this->attributes_section_data_);
7920 out_attr[i].set_int_value(
7921 tag_cpu_arch_combine(name, out_attr[i].int_value(),
7922 &secondary_compat_out,
7923 in_attr[i].int_value(),
7925 this->set_secondary_compatible_arch(this->attributes_section_data_,
7926 secondary_compat_out);
7928 // Merge Tag_CPU_name and Tag_CPU_raw_name.
7929 if (out_attr[i].int_value() == saved_out_attr)
7930 ; // Leave the names alone.
7931 else if (out_attr[i].int_value() == in_attr[i].int_value())
7933 // The output architecture has been changed to match the
7934 // input architecture. Use the input names.
7935 out_attr[elfcpp::Tag_CPU_name].set_string_value(
7936 in_attr[elfcpp::Tag_CPU_name].string_value());
7937 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
7938 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
7942 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
7943 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
7946 // If we still don't have a value for Tag_CPU_name,
7947 // make one up now. Tag_CPU_raw_name remains blank.
7948 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
7950 const std::string cpu_name =
7951 this->tag_cpu_name_value(out_attr[i].int_value());
7952 // FIXME: If we see an unknown CPU, this will be set
7953 // to "<unknown CPU n>", where n is the attribute value.
7954 // This is different from BFD, which leaves the name alone.
7955 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
7960 case elfcpp::Tag_ARM_ISA_use:
7961 case elfcpp::Tag_THUMB_ISA_use:
7962 case elfcpp::Tag_WMMX_arch:
7963 case elfcpp::Tag_Advanced_SIMD_arch:
7964 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
7965 case elfcpp::Tag_ABI_FP_rounding:
7966 case elfcpp::Tag_ABI_FP_exceptions:
7967 case elfcpp::Tag_ABI_FP_user_exceptions:
7968 case elfcpp::Tag_ABI_FP_number_model:
7969 case elfcpp::Tag_VFP_HP_extension:
7970 case elfcpp::Tag_CPU_unaligned_access:
7971 case elfcpp::Tag_T2EE_use:
7972 case elfcpp::Tag_Virtualization_use:
7973 case elfcpp::Tag_MPextension_use:
7974 // Use the largest value specified.
7975 if (in_attr[i].int_value() > out_attr[i].int_value())
7976 out_attr[i].set_int_value(in_attr[i].int_value());
7979 case elfcpp::Tag_ABI_align8_preserved:
7980 case elfcpp::Tag_ABI_PCS_RO_data:
7981 // Use the smallest value specified.
7982 if (in_attr[i].int_value() < out_attr[i].int_value())
7983 out_attr[i].set_int_value(in_attr[i].int_value());
7986 case elfcpp::Tag_ABI_align8_needed:
7987 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
7988 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
7989 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
7992 // This error message should be enabled once all non-conformant
7993 // binaries in the toolchain have had the attributes set
7995 // gold_error(_("output 8-byte data alignment conflicts with %s"),
7999 case elfcpp::Tag_ABI_FP_denormal:
8000 case elfcpp::Tag_ABI_PCS_GOT_use:
8002 // These tags have 0 = don't care, 1 = strong requirement,
8003 // 2 = weak requirement.
8004 static const int order_021[3] = {0, 2, 1};
8006 // Use the "greatest" from the sequence 0, 2, 1, or the largest
8007 // value if greater than 2 (for future-proofing).
8008 if ((in_attr[i].int_value() > 2
8009 && in_attr[i].int_value() > out_attr[i].int_value())
8010 || (in_attr[i].int_value() <= 2
8011 && out_attr[i].int_value() <= 2
8012 && (order_021[in_attr[i].int_value()]
8013 > order_021[out_attr[i].int_value()])))
8014 out_attr[i].set_int_value(in_attr[i].int_value());
8018 case elfcpp::Tag_CPU_arch_profile:
8019 if (out_attr[i].int_value() != in_attr[i].int_value())
8021 // 0 will merge with anything.
8022 // 'A' and 'S' merge to 'A'.
8023 // 'R' and 'S' merge to 'R'.
8024 // 'M' and 'A|R|S' is an error.
8025 if (out_attr[i].int_value() == 0
8026 || (out_attr[i].int_value() == 'S'
8027 && (in_attr[i].int_value() == 'A'
8028 || in_attr[i].int_value() == 'R')))
8029 out_attr[i].set_int_value(in_attr[i].int_value());
8030 else if (in_attr[i].int_value() == 0
8031 || (in_attr[i].int_value() == 'S'
8032 && (out_attr[i].int_value() == 'A'
8033 || out_attr[i].int_value() == 'R')))
8038 (_("conflicting architecture profiles %c/%c"),
8039 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
8040 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
8044 case elfcpp::Tag_VFP_arch:
8061 // Values greater than 6 aren't defined, so just pick the
8063 if (in_attr[i].int_value() > 6
8064 && in_attr[i].int_value() > out_attr[i].int_value())
8066 *out_attr = *in_attr;
8069 // The output uses the superset of input features
8070 // (ISA version) and registers.
8071 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
8072 vfp_versions[out_attr[i].int_value()].ver);
8073 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
8074 vfp_versions[out_attr[i].int_value()].regs);
8075 // This assumes all possible supersets are also a valid
8078 for (newval = 6; newval > 0; newval--)
8080 if (regs == vfp_versions[newval].regs
8081 && ver == vfp_versions[newval].ver)
8084 out_attr[i].set_int_value(newval);
8087 case elfcpp::Tag_PCS_config:
8088 if (out_attr[i].int_value() == 0)
8089 out_attr[i].set_int_value(in_attr[i].int_value());
8090 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
8092 // It's sometimes ok to mix different configs, so this is only
8094 gold_warning(_("%s: conflicting platform configuration"), name);
8097 case elfcpp::Tag_ABI_PCS_R9_use:
8098 if (in_attr[i].int_value() != out_attr[i].int_value()
8099 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
8100 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
8102 gold_error(_("%s: conflicting use of R9"), name);
8104 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
8105 out_attr[i].set_int_value(in_attr[i].int_value());
8107 case elfcpp::Tag_ABI_PCS_RW_data:
8108 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
8109 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
8110 != elfcpp::AEABI_R9_SB)
8111 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
8112 != elfcpp::AEABI_R9_unused))
8114 gold_error(_("%s: SB relative addressing conflicts with use "
8118 // Use the smallest value specified.
8119 if (in_attr[i].int_value() < out_attr[i].int_value())
8120 out_attr[i].set_int_value(in_attr[i].int_value());
8122 case elfcpp::Tag_ABI_PCS_wchar_t:
8123 // FIXME: Make it possible to turn off this warning.
8124 if (out_attr[i].int_value()
8125 && in_attr[i].int_value()
8126 && out_attr[i].int_value() != in_attr[i].int_value())
8128 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
8129 "use %u-byte wchar_t; use of wchar_t values "
8130 "across objects may fail"),
8131 name, in_attr[i].int_value(),
8132 out_attr[i].int_value());
8134 else if (in_attr[i].int_value() && !out_attr[i].int_value())
8135 out_attr[i].set_int_value(in_attr[i].int_value());
8137 case elfcpp::Tag_ABI_enum_size:
8138 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
8140 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
8141 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
8143 // The existing object is compatible with anything.
8144 // Use whatever requirements the new object has.
8145 out_attr[i].set_int_value(in_attr[i].int_value());
8147 // FIXME: Make it possible to turn off this warning.
8148 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
8149 && out_attr[i].int_value() != in_attr[i].int_value())
8151 unsigned int in_value = in_attr[i].int_value();
8152 unsigned int out_value = out_attr[i].int_value();
8153 gold_warning(_("%s uses %s enums yet the output is to use "
8154 "%s enums; use of enum values across objects "
8157 this->aeabi_enum_name(in_value).c_str(),
8158 this->aeabi_enum_name(out_value).c_str());
8162 case elfcpp::Tag_ABI_VFP_args:
8165 case elfcpp::Tag_ABI_WMMX_args:
8166 if (in_attr[i].int_value() != out_attr[i].int_value())
8168 gold_error(_("%s uses iWMMXt register arguments, output does "
8173 case Object_attribute::Tag_compatibility:
8174 // Merged in target-independent code.
8176 case elfcpp::Tag_ABI_HardFP_use:
8177 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
8178 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
8179 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
8180 out_attr[i].set_int_value(3);
8181 else if (in_attr[i].int_value() > out_attr[i].int_value())
8182 out_attr[i].set_int_value(in_attr[i].int_value());
8184 case elfcpp::Tag_ABI_FP_16bit_format:
8185 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
8187 if (in_attr[i].int_value() != out_attr[i].int_value())
8188 gold_error(_("fp16 format mismatch between %s and output"),
8191 if (in_attr[i].int_value() != 0)
8192 out_attr[i].set_int_value(in_attr[i].int_value());
8195 case elfcpp::Tag_nodefaults:
8196 // This tag is set if it exists, but the value is unused (and is
8197 // typically zero). We don't actually need to do anything here -
8198 // the merge happens automatically when the type flags are merged
8201 case elfcpp::Tag_also_compatible_with:
8202 // Already done in Tag_CPU_arch.
8204 case elfcpp::Tag_conformance:
8205 // Keep the attribute if it matches. Throw it away otherwise.
8206 // No attribute means no claim to conform.
8207 if (in_attr[i].string_value() != out_attr[i].string_value())
8208 out_attr[i].set_string_value("");
8213 const char* err_object = NULL;
8215 // The "known_obj_attributes" table does contain some undefined
8216 // attributes. Ensure that there are unused.
8217 if (out_attr[i].int_value() != 0
8218 || out_attr[i].string_value() != "")
8219 err_object = "output";
8220 else if (in_attr[i].int_value() != 0
8221 || in_attr[i].string_value() != "")
8224 if (err_object != NULL)
8226 // Attribute numbers >=64 (mod 128) can be safely ignored.
8228 gold_error(_("%s: unknown mandatory EABI object attribute "
8232 gold_warning(_("%s: unknown EABI object attribute %d"),
8236 // Only pass on attributes that match in both inputs.
8237 if (!in_attr[i].matches(out_attr[i]))
8239 out_attr[i].set_int_value(0);
8240 out_attr[i].set_string_value("");
8245 // If out_attr was copied from in_attr then it won't have a type yet.
8246 if (in_attr[i].type() && !out_attr[i].type())
8247 out_attr[i].set_type(in_attr[i].type());
8250 // Merge Tag_compatibility attributes and any common GNU ones.
8251 this->attributes_section_data_->merge(name, pasd);
8253 // Check for any attributes not known on ARM.
8254 typedef Vendor_object_attributes::Other_attributes Other_attributes;
8255 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
8256 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
8257 Other_attributes* out_other_attributes =
8258 this->attributes_section_data_->other_attributes(vendor);
8259 Other_attributes::iterator out_iter = out_other_attributes->begin();
8261 while (in_iter != in_other_attributes->end()
8262 || out_iter != out_other_attributes->end())
8264 const char* err_object = NULL;
8267 // The tags for each list are in numerical order.
8268 // If the tags are equal, then merge.
8269 if (out_iter != out_other_attributes->end()
8270 && (in_iter == in_other_attributes->end()
8271 || in_iter->first > out_iter->first))
8273 // This attribute only exists in output. We can't merge, and we
8274 // don't know what the tag means, so delete it.
8275 err_object = "output";
8276 err_tag = out_iter->first;
8277 int saved_tag = out_iter->first;
8278 delete out_iter->second;
8279 out_other_attributes->erase(out_iter);
8280 out_iter = out_other_attributes->upper_bound(saved_tag);
8282 else if (in_iter != in_other_attributes->end()
8283 && (out_iter != out_other_attributes->end()
8284 || in_iter->first < out_iter->first))
8286 // This attribute only exists in input. We can't merge, and we
8287 // don't know what the tag means, so ignore it.
8289 err_tag = in_iter->first;
8292 else // The tags are equal.
8294 // As present, all attributes in the list are unknown, and
8295 // therefore can't be merged meaningfully.
8296 err_object = "output";
8297 err_tag = out_iter->first;
8299 // Only pass on attributes that match in both inputs.
8300 if (!in_iter->second->matches(*(out_iter->second)))
8302 // No match. Delete the attribute.
8303 int saved_tag = out_iter->first;
8304 delete out_iter->second;
8305 out_other_attributes->erase(out_iter);
8306 out_iter = out_other_attributes->upper_bound(saved_tag);
8310 // Matched. Keep the attribute and move to the next.
8318 // Attribute numbers >=64 (mod 128) can be safely ignored. */
8319 if ((err_tag & 127) < 64)
8321 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
8322 err_object, err_tag);
8326 gold_warning(_("%s: unknown EABI object attribute %d"),
8327 err_object, err_tag);
8333 // Return whether a relocation type used the LSB to distinguish THUMB
8335 template<bool big_endian>
8337 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
8341 case elfcpp::R_ARM_PC24:
8342 case elfcpp::R_ARM_ABS32:
8343 case elfcpp::R_ARM_REL32:
8344 case elfcpp::R_ARM_SBREL32:
8345 case elfcpp::R_ARM_THM_CALL:
8346 case elfcpp::R_ARM_GLOB_DAT:
8347 case elfcpp::R_ARM_JUMP_SLOT:
8348 case elfcpp::R_ARM_GOTOFF32:
8349 case elfcpp::R_ARM_PLT32:
8350 case elfcpp::R_ARM_CALL:
8351 case elfcpp::R_ARM_JUMP24:
8352 case elfcpp::R_ARM_THM_JUMP24:
8353 case elfcpp::R_ARM_SBREL31:
8354 case elfcpp::R_ARM_PREL31:
8355 case elfcpp::R_ARM_MOVW_ABS_NC:
8356 case elfcpp::R_ARM_MOVW_PREL_NC:
8357 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8358 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8359 case elfcpp::R_ARM_THM_JUMP19:
8360 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8361 case elfcpp::R_ARM_ALU_PC_G0_NC:
8362 case elfcpp::R_ARM_ALU_PC_G0:
8363 case elfcpp::R_ARM_ALU_PC_G1_NC:
8364 case elfcpp::R_ARM_ALU_PC_G1:
8365 case elfcpp::R_ARM_ALU_PC_G2:
8366 case elfcpp::R_ARM_ALU_SB_G0_NC:
8367 case elfcpp::R_ARM_ALU_SB_G0:
8368 case elfcpp::R_ARM_ALU_SB_G1_NC:
8369 case elfcpp::R_ARM_ALU_SB_G1:
8370 case elfcpp::R_ARM_ALU_SB_G2:
8371 case elfcpp::R_ARM_MOVW_BREL_NC:
8372 case elfcpp::R_ARM_MOVW_BREL:
8373 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8374 case elfcpp::R_ARM_THM_MOVW_BREL:
8381 // Stub-generation methods for Target_arm.
8383 // Make a new Arm_input_section object.
8385 template<bool big_endian>
8386 Arm_input_section<big_endian>*
8387 Target_arm<big_endian>::new_arm_input_section(
8391 Section_id sid(relobj, shndx);
8393 Arm_input_section<big_endian>* arm_input_section =
8394 new Arm_input_section<big_endian>(relobj, shndx);
8395 arm_input_section->init();
8397 // Register new Arm_input_section in map for look-up.
8398 std::pair<typename Arm_input_section_map::iterator, bool> ins =
8399 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
8401 // Make sure that it we have not created another Arm_input_section
8402 // for this input section already.
8403 gold_assert(ins.second);
8405 return arm_input_section;
8408 // Find the Arm_input_section object corresponding to the SHNDX-th input
8409 // section of RELOBJ.
8411 template<bool big_endian>
8412 Arm_input_section<big_endian>*
8413 Target_arm<big_endian>::find_arm_input_section(
8415 unsigned int shndx) const
8417 Section_id sid(relobj, shndx);
8418 typename Arm_input_section_map::const_iterator p =
8419 this->arm_input_section_map_.find(sid);
8420 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
8423 // Make a new stub table.
8425 template<bool big_endian>
8426 Stub_table<big_endian>*
8427 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
8429 Stub_table<big_endian>* stub_table =
8430 new Stub_table<big_endian>(owner);
8431 this->stub_tables_.push_back(stub_table);
8433 stub_table->set_address(owner->address() + owner->data_size());
8434 stub_table->set_file_offset(owner->offset() + owner->data_size());
8435 stub_table->finalize_data_size();
8440 // Scan a relocation for stub generation.
8442 template<bool big_endian>
8444 Target_arm<big_endian>::scan_reloc_for_stub(
8445 const Relocate_info<32, big_endian>* relinfo,
8446 unsigned int r_type,
8447 const Sized_symbol<32>* gsym,
8449 const Symbol_value<32>* psymval,
8450 elfcpp::Elf_types<32>::Elf_Swxword addend,
8451 Arm_address address)
8453 typedef typename Target_arm<big_endian>::Relocate Relocate;
8455 const Arm_relobj<big_endian>* arm_relobj =
8456 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8458 if (r_type == elfcpp::R_ARM_V4BX)
8460 const uint32_t reg = (addend & 0xf);
8461 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8464 // Try looking up an existing stub from a stub table.
8465 Stub_table<big_endian>* stub_table =
8466 arm_relobj->stub_table(relinfo->data_shndx);
8467 gold_assert(stub_table != NULL);
8469 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
8471 // create a new stub and add it to stub table.
8472 Arm_v4bx_stub* stub =
8473 this->stub_factory().make_arm_v4bx_stub(reg);
8474 gold_assert(stub != NULL);
8475 stub_table->add_arm_v4bx_stub(stub);
8482 bool target_is_thumb;
8483 Symbol_value<32> symval;
8486 // This is a global symbol. Determine if we use PLT and if the
8487 // final target is THUMB.
8488 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
8490 // This uses a PLT, change the symbol value.
8491 symval.set_output_value(this->plt_section()->address()
8492 + gsym->plt_offset());
8494 target_is_thumb = false;
8496 else if (gsym->is_undefined())
8497 // There is no need to generate a stub symbol is undefined.
8502 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
8503 || (gsym->type() == elfcpp::STT_FUNC
8504 && !gsym->is_undefined()
8505 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
8510 // This is a local symbol. Determine if the final target is THUMB.
8511 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
8514 // Strip LSB if this points to a THUMB target.
8516 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
8517 && ((psymval->value(arm_relobj, 0) & 1) != 0))
8519 Arm_address stripped_value =
8520 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
8521 symval.set_output_value(stripped_value);
8525 // Get the symbol value.
8526 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
8528 // Owing to pipelining, the PC relative branches below actually skip
8529 // two instructions when the branch offset is 0.
8530 Arm_address destination;
8533 case elfcpp::R_ARM_CALL:
8534 case elfcpp::R_ARM_JUMP24:
8535 case elfcpp::R_ARM_PLT32:
8537 destination = value + addend + 8;
8539 case elfcpp::R_ARM_THM_CALL:
8540 case elfcpp::R_ARM_THM_XPC22:
8541 case elfcpp::R_ARM_THM_JUMP24:
8542 case elfcpp::R_ARM_THM_JUMP19:
8544 destination = value + addend + 4;
8550 Reloc_stub* stub = NULL;
8551 Stub_type stub_type =
8552 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
8554 if (stub_type != arm_stub_none)
8556 // Try looking up an existing stub from a stub table.
8557 Stub_table<big_endian>* stub_table =
8558 arm_relobj->stub_table(relinfo->data_shndx);
8559 gold_assert(stub_table != NULL);
8561 // Locate stub by destination.
8562 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
8564 // Create a stub if there is not one already
8565 stub = stub_table->find_reloc_stub(stub_key);
8568 // create a new stub and add it to stub table.
8569 stub = this->stub_factory().make_reloc_stub(stub_type);
8570 stub_table->add_reloc_stub(stub, stub_key);
8573 // Record the destination address.
8574 stub->set_destination_address(destination
8575 | (target_is_thumb ? 1 : 0));
8578 // For Cortex-A8, we need to record a relocation at 4K page boundary.
8579 if (this->fix_cortex_a8_
8580 && (r_type == elfcpp::R_ARM_THM_JUMP24
8581 || r_type == elfcpp::R_ARM_THM_JUMP19
8582 || r_type == elfcpp::R_ARM_THM_CALL
8583 || r_type == elfcpp::R_ARM_THM_XPC22)
8584 && (address & 0xfffU) == 0xffeU)
8586 // Found a candidate. Note we haven't checked the destination is
8587 // within 4K here: if we do so (and don't create a record) we can't
8588 // tell that a branch should have been relocated when scanning later.
8589 this->cortex_a8_relocs_info_[address] =
8590 new Cortex_a8_reloc(stub, r_type,
8591 destination | (target_is_thumb ? 1 : 0));
8595 // This function scans a relocation sections for stub generation.
8596 // The template parameter Relocate must be a class type which provides
8597 // a single function, relocate(), which implements the machine
8598 // specific part of a relocation.
8600 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
8601 // SHT_REL or SHT_RELA.
8603 // PRELOCS points to the relocation data. RELOC_COUNT is the number
8604 // of relocs. OUTPUT_SECTION is the output section.
8605 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
8606 // mapped to output offsets.
8608 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
8609 // VIEW_SIZE is the size. These refer to the input section, unless
8610 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
8611 // the output section.
8613 template<bool big_endian>
8614 template<int sh_type>
8616 Target_arm<big_endian>::scan_reloc_section_for_stubs(
8617 const Relocate_info<32, big_endian>* relinfo,
8618 const unsigned char* prelocs,
8620 Output_section* output_section,
8621 bool needs_special_offset_handling,
8622 const unsigned char* view,
8623 elfcpp::Elf_types<32>::Elf_Addr view_address,
8626 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
8627 const int reloc_size =
8628 Reloc_types<sh_type, 32, big_endian>::reloc_size;
8630 Arm_relobj<big_endian>* arm_object =
8631 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8632 unsigned int local_count = arm_object->local_symbol_count();
8634 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
8636 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
8638 Reltype reloc(prelocs);
8640 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
8641 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
8642 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
8644 r_type = this->get_real_reloc_type(r_type);
8646 // Only a few relocation types need stubs.
8647 if ((r_type != elfcpp::R_ARM_CALL)
8648 && (r_type != elfcpp::R_ARM_JUMP24)
8649 && (r_type != elfcpp::R_ARM_PLT32)
8650 && (r_type != elfcpp::R_ARM_THM_CALL)
8651 && (r_type != elfcpp::R_ARM_THM_XPC22)
8652 && (r_type != elfcpp::R_ARM_THM_JUMP24)
8653 && (r_type != elfcpp::R_ARM_THM_JUMP19)
8654 && (r_type != elfcpp::R_ARM_V4BX))
8657 section_offset_type offset =
8658 convert_to_section_size_type(reloc.get_r_offset());
8660 if (needs_special_offset_handling)
8662 offset = output_section->output_offset(relinfo->object,
8663 relinfo->data_shndx,
8669 if (r_type == elfcpp::R_ARM_V4BX)
8671 // Get the BX instruction.
8672 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
8673 const Valtype* wv = reinterpret_cast<const Valtype*>(view + offset);
8674 elfcpp::Elf_types<32>::Elf_Swxword insn =
8675 elfcpp::Swap<32, big_endian>::readval(wv);
8676 this->scan_reloc_for_stub(relinfo, r_type, NULL, 0, NULL,
8682 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
8683 elfcpp::Elf_types<32>::Elf_Swxword addend =
8684 stub_addend_reader(r_type, view + offset, reloc);
8686 const Sized_symbol<32>* sym;
8688 Symbol_value<32> symval;
8689 const Symbol_value<32> *psymval;
8690 if (r_sym < local_count)
8693 psymval = arm_object->local_symbol(r_sym);
8695 // If the local symbol belongs to a section we are discarding,
8696 // and that section is a debug section, try to find the
8697 // corresponding kept section and map this symbol to its
8698 // counterpart in the kept section. The symbol must not
8699 // correspond to a section we are folding.
8701 unsigned int shndx = psymval->input_shndx(&is_ordinary);
8703 && shndx != elfcpp::SHN_UNDEF
8704 && !arm_object->is_section_included(shndx)
8705 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
8707 if (comdat_behavior == CB_UNDETERMINED)
8710 arm_object->section_name(relinfo->data_shndx);
8711 comdat_behavior = get_comdat_behavior(name.c_str());
8713 if (comdat_behavior == CB_PRETEND)
8716 typename elfcpp::Elf_types<32>::Elf_Addr value =
8717 arm_object->map_to_kept_section(shndx, &found);
8719 symval.set_output_value(value + psymval->input_value());
8721 symval.set_output_value(0);
8725 symval.set_output_value(0);
8727 symval.set_no_output_symtab_entry();
8733 const Symbol* gsym = arm_object->global_symbol(r_sym);
8734 gold_assert(gsym != NULL);
8735 if (gsym->is_forwarder())
8736 gsym = relinfo->symtab->resolve_forwards(gsym);
8738 sym = static_cast<const Sized_symbol<32>*>(gsym);
8739 if (sym->has_symtab_index())
8740 symval.set_output_symtab_index(sym->symtab_index());
8742 symval.set_no_output_symtab_entry();
8744 // We need to compute the would-be final value of this global
8746 const Symbol_table* symtab = relinfo->symtab;
8747 const Sized_symbol<32>* sized_symbol =
8748 symtab->get_sized_symbol<32>(gsym);
8749 Symbol_table::Compute_final_value_status status;
8751 symtab->compute_final_value<32>(sized_symbol, &status);
8753 // Skip this if the symbol has not output section.
8754 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
8757 symval.set_output_value(value);
8761 // If symbol is a section symbol, we don't know the actual type of
8762 // destination. Give up.
8763 if (psymval->is_section_symbol())
8766 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
8767 addend, view_address + offset);
8771 // Scan an input section for stub generation.
8773 template<bool big_endian>
8775 Target_arm<big_endian>::scan_section_for_stubs(
8776 const Relocate_info<32, big_endian>* relinfo,
8777 unsigned int sh_type,
8778 const unsigned char* prelocs,
8780 Output_section* output_section,
8781 bool needs_special_offset_handling,
8782 const unsigned char* view,
8783 Arm_address view_address,
8784 section_size_type view_size)
8786 if (sh_type == elfcpp::SHT_REL)
8787 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
8792 needs_special_offset_handling,
8796 else if (sh_type == elfcpp::SHT_RELA)
8797 // We do not support RELA type relocations yet. This is provided for
8799 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
8804 needs_special_offset_handling,
8812 // Group input sections for stub generation.
8814 // We goup input sections in an output sections so that the total size,
8815 // including any padding space due to alignment is smaller than GROUP_SIZE
8816 // unless the only input section in group is bigger than GROUP_SIZE already.
8817 // Then an ARM stub table is created to follow the last input section
8818 // in group. For each group an ARM stub table is created an is placed
8819 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
8820 // extend the group after the stub table.
8822 template<bool big_endian>
8824 Target_arm<big_endian>::group_sections(
8826 section_size_type group_size,
8827 bool stubs_always_after_branch)
8829 // Group input sections and insert stub table
8830 Layout::Section_list section_list;
8831 layout->get_allocated_sections(§ion_list);
8832 for (Layout::Section_list::const_iterator p = section_list.begin();
8833 p != section_list.end();
8836 Arm_output_section<big_endian>* output_section =
8837 Arm_output_section<big_endian>::as_arm_output_section(*p);
8838 output_section->group_sections(group_size, stubs_always_after_branch,
8843 // Relaxation hook. This is where we do stub generation.
8845 template<bool big_endian>
8847 Target_arm<big_endian>::do_relax(
8849 const Input_objects* input_objects,
8850 Symbol_table* symtab,
8853 // No need to generate stubs if this is a relocatable link.
8854 gold_assert(!parameters->options().relocatable());
8856 // If this is the first pass, we need to group input sections into
8858 bool done_exidx_fixup = false;
8861 // Determine the stub group size. The group size is the absolute
8862 // value of the parameter --stub-group-size. If --stub-group-size
8863 // is passed a negative value, we restict stubs to be always after
8864 // the stubbed branches.
8865 int32_t stub_group_size_param =
8866 parameters->options().stub_group_size();
8867 bool stubs_always_after_branch = stub_group_size_param < 0;
8868 section_size_type stub_group_size = abs(stub_group_size_param);
8870 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
8871 // page as the first half of a 32-bit branch straddling two 4K pages.
8872 // This is a crude way of enforcing that.
8873 if (this->fix_cortex_a8_)
8874 stubs_always_after_branch = true;
8876 if (stub_group_size == 1)
8879 // Thumb branch range is +-4MB has to be used as the default
8880 // maximum size (a given section can contain both ARM and Thumb
8881 // code, so the worst case has to be taken into account).
8883 // This value is 24K less than that, which allows for 2025
8884 // 12-byte stubs. If we exceed that, then we will fail to link.
8885 // The user will have to relink with an explicit group size
8887 stub_group_size = 4170000;
8890 group_sections(layout, stub_group_size, stubs_always_after_branch);
8892 // Also fix .ARM.exidx section coverage.
8893 Output_section* os = layout->find_output_section(".ARM.exidx");
8894 if (os != NULL && os->type() == elfcpp::SHT_ARM_EXIDX)
8896 Arm_output_section<big_endian>* exidx_output_section =
8897 Arm_output_section<big_endian>::as_arm_output_section(os);
8898 this->fix_exidx_coverage(layout, exidx_output_section, symtab);
8899 done_exidx_fixup = true;
8903 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
8904 // beginning of each relaxation pass, just blow away all the stubs.
8905 // Alternatively, we could selectively remove only the stubs and reloc
8906 // information for code sections that have moved since the last pass.
8907 // That would require more book-keeping.
8908 typedef typename Stub_table_list::iterator Stub_table_iterator;
8909 if (this->fix_cortex_a8_)
8911 // Clear all Cortex-A8 reloc information.
8912 for (typename Cortex_a8_relocs_info::const_iterator p =
8913 this->cortex_a8_relocs_info_.begin();
8914 p != this->cortex_a8_relocs_info_.end();
8917 this->cortex_a8_relocs_info_.clear();
8919 // Remove all Cortex-A8 stubs.
8920 for (Stub_table_iterator sp = this->stub_tables_.begin();
8921 sp != this->stub_tables_.end();
8923 (*sp)->remove_all_cortex_a8_stubs();
8926 // Scan relocs for relocation stubs
8927 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
8928 op != input_objects->relobj_end();
8931 Arm_relobj<big_endian>* arm_relobj =
8932 Arm_relobj<big_endian>::as_arm_relobj(*op);
8933 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
8936 // Check all stub tables to see if any of them have their data sizes
8937 // or addresses alignments changed. These are the only things that
8939 bool any_stub_table_changed = false;
8940 Unordered_set<const Output_section*> sections_needing_adjustment;
8941 for (Stub_table_iterator sp = this->stub_tables_.begin();
8942 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
8945 if ((*sp)->update_data_size_and_addralign())
8947 // Update data size of stub table owner.
8948 Arm_input_section<big_endian>* owner = (*sp)->owner();
8949 uint64_t address = owner->address();
8950 off_t offset = owner->offset();
8951 owner->reset_address_and_file_offset();
8952 owner->set_address_and_file_offset(address, offset);
8954 sections_needing_adjustment.insert(owner->output_section());
8955 any_stub_table_changed = true;
8959 // Output_section_data::output_section() returns a const pointer but we
8960 // need to update output sections, so we record all output sections needing
8961 // update above and scan the sections here to find out what sections need
8963 for(Layout::Section_list::const_iterator p = layout->section_list().begin();
8964 p != layout->section_list().end();
8967 if (sections_needing_adjustment.find(*p)
8968 != sections_needing_adjustment.end())
8969 (*p)->set_section_offsets_need_adjustment();
8972 // Stop relaxation if no EXIDX fix-up and no stub table change.
8973 bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
8975 // Finalize the stubs in the last relaxation pass.
8976 if (!continue_relaxation)
8977 for (Stub_table_iterator sp = this->stub_tables_.begin();
8978 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
8980 (*sp)->finalize_stubs();
8982 return continue_relaxation;
8987 template<bool big_endian>
8989 Target_arm<big_endian>::relocate_stub(
8991 const Relocate_info<32, big_endian>* relinfo,
8992 Output_section* output_section,
8993 unsigned char* view,
8994 Arm_address address,
8995 section_size_type view_size)
8998 const Stub_template* stub_template = stub->stub_template();
8999 for (size_t i = 0; i < stub_template->reloc_count(); i++)
9001 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
9002 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
9004 unsigned int r_type = insn->r_type();
9005 section_size_type reloc_offset = stub_template->reloc_offset(i);
9006 section_size_type reloc_size = insn->size();
9007 gold_assert(reloc_offset + reloc_size <= view_size);
9009 // This is the address of the stub destination.
9010 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
9011 Symbol_value<32> symval;
9012 symval.set_output_value(target);
9014 // Synthesize a fake reloc just in case. We don't have a symbol so
9016 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
9017 memset(reloc_buffer, 0, sizeof(reloc_buffer));
9018 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
9019 reloc_write.put_r_offset(reloc_offset);
9020 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
9021 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
9023 relocate.relocate(relinfo, this, output_section,
9024 this->fake_relnum_for_stubs, rel, r_type,
9025 NULL, &symval, view + reloc_offset,
9026 address + reloc_offset, reloc_size);
9030 // Determine whether an object attribute tag takes an integer, a
9033 template<bool big_endian>
9035 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
9037 if (tag == Object_attribute::Tag_compatibility)
9038 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
9039 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
9040 else if (tag == elfcpp::Tag_nodefaults)
9041 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
9042 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
9043 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
9044 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
9046 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
9048 return ((tag & 1) != 0
9049 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
9050 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9053 // Reorder attributes.
9055 // The ABI defines that Tag_conformance should be emitted first, and that
9056 // Tag_nodefaults should be second (if either is defined). This sets those
9057 // two positions, and bumps up the position of all the remaining tags to
9060 template<bool big_endian>
9062 Target_arm<big_endian>::do_attributes_order(int num) const
9064 // Reorder the known object attributes in output. We want to move
9065 // Tag_conformance to position 4 and Tag_conformance to position 5
9066 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
9068 return elfcpp::Tag_conformance;
9070 return elfcpp::Tag_nodefaults;
9071 if ((num - 2) < elfcpp::Tag_nodefaults)
9073 if ((num - 1) < elfcpp::Tag_conformance)
9078 // Scan a span of THUMB code for Cortex-A8 erratum.
9080 template<bool big_endian>
9082 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
9083 Arm_relobj<big_endian>* arm_relobj,
9085 section_size_type span_start,
9086 section_size_type span_end,
9087 const unsigned char* view,
9088 Arm_address address)
9090 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
9092 // The opcode is BLX.W, BL.W, B.W, Bcc.W
9093 // The branch target is in the same 4KB region as the
9094 // first half of the branch.
9095 // The instruction before the branch is a 32-bit
9096 // length non-branch instruction.
9097 section_size_type i = span_start;
9098 bool last_was_32bit = false;
9099 bool last_was_branch = false;
9100 while (i < span_end)
9102 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
9103 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
9104 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
9105 bool is_blx = false, is_b = false;
9106 bool is_bl = false, is_bcc = false;
9108 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
9111 // Load the rest of the insn (in manual-friendly order).
9112 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
9114 // Encoding T4: B<c>.W.
9115 is_b = (insn & 0xf800d000U) == 0xf0009000U;
9116 // Encoding T1: BL<c>.W.
9117 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
9118 // Encoding T2: BLX<c>.W.
9119 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
9120 // Encoding T3: B<c>.W (not permitted in IT block).
9121 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
9122 && (insn & 0x07f00000U) != 0x03800000U);
9125 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
9127 // If this instruction is a 32-bit THUMB branch that crosses a 4K
9128 // page boundary and it follows 32-bit non-branch instruction,
9129 // we need to work around.
9131 && ((address + i) & 0xfffU) == 0xffeU
9133 && !last_was_branch)
9135 // Check to see if there is a relocation stub for this branch.
9136 bool force_target_arm = false;
9137 bool force_target_thumb = false;
9138 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
9139 Cortex_a8_relocs_info::const_iterator p =
9140 this->cortex_a8_relocs_info_.find(address + i);
9142 if (p != this->cortex_a8_relocs_info_.end())
9144 cortex_a8_reloc = p->second;
9145 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
9147 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
9148 && !target_is_thumb)
9149 force_target_arm = true;
9150 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
9152 force_target_thumb = true;
9156 Stub_type stub_type = arm_stub_none;
9158 // Check if we have an offending branch instruction.
9159 uint16_t upper_insn = (insn >> 16) & 0xffffU;
9160 uint16_t lower_insn = insn & 0xffffU;
9161 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
9163 if (cortex_a8_reloc != NULL
9164 && cortex_a8_reloc->reloc_stub() != NULL)
9165 // We've already made a stub for this instruction, e.g.
9166 // it's a long branch or a Thumb->ARM stub. Assume that
9167 // stub will suffice to work around the A8 erratum (see
9168 // setting of always_after_branch above).
9172 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
9174 stub_type = arm_stub_a8_veneer_b_cond;
9176 else if (is_b || is_bl || is_blx)
9178 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
9184 ? arm_stub_a8_veneer_blx
9186 ? arm_stub_a8_veneer_bl
9187 : arm_stub_a8_veneer_b));
9190 if (stub_type != arm_stub_none)
9192 Arm_address pc_for_insn = address + i + 4;
9194 // The original instruction is a BL, but the target is
9195 // an ARM instruction. If we were not making a stub,
9196 // the BL would have been converted to a BLX. Use the
9197 // BLX stub instead in that case.
9198 if (this->may_use_blx() && force_target_arm
9199 && stub_type == arm_stub_a8_veneer_bl)
9201 stub_type = arm_stub_a8_veneer_blx;
9205 // Conversely, if the original instruction was
9206 // BLX but the target is Thumb mode, use the BL stub.
9207 else if (force_target_thumb
9208 && stub_type == arm_stub_a8_veneer_blx)
9210 stub_type = arm_stub_a8_veneer_bl;
9218 // If we found a relocation, use the proper destination,
9219 // not the offset in the (unrelocated) instruction.
9220 // Note this is always done if we switched the stub type above.
9221 if (cortex_a8_reloc != NULL)
9222 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
9224 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
9226 // Add a new stub if destination address in in the same page.
9227 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
9229 Cortex_a8_stub* stub =
9230 this->stub_factory_.make_cortex_a8_stub(stub_type,
9234 Stub_table<big_endian>* stub_table =
9235 arm_relobj->stub_table(shndx);
9236 gold_assert(stub_table != NULL);
9237 stub_table->add_cortex_a8_stub(address + i, stub);
9242 i += insn_32bit ? 4 : 2;
9243 last_was_32bit = insn_32bit;
9244 last_was_branch = is_32bit_branch;
9248 // Apply the Cortex-A8 workaround.
9250 template<bool big_endian>
9252 Target_arm<big_endian>::apply_cortex_a8_workaround(
9253 const Cortex_a8_stub* stub,
9254 Arm_address stub_address,
9255 unsigned char* insn_view,
9256 Arm_address insn_address)
9258 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
9259 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
9260 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
9261 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
9262 off_t branch_offset = stub_address - (insn_address + 4);
9264 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
9265 switch (stub->stub_template()->type())
9267 case arm_stub_a8_veneer_b_cond:
9268 gold_assert(!utils::has_overflow<21>(branch_offset));
9269 upper_insn = RelocFuncs::thumb32_cond_branch_upper(upper_insn,
9271 lower_insn = RelocFuncs::thumb32_cond_branch_lower(lower_insn,
9275 case arm_stub_a8_veneer_b:
9276 case arm_stub_a8_veneer_bl:
9277 case arm_stub_a8_veneer_blx:
9278 if ((lower_insn & 0x5000U) == 0x4000U)
9279 // For a BLX instruction, make sure that the relocation is
9280 // rounded up to a word boundary. This follows the semantics of
9281 // the instruction which specifies that bit 1 of the target
9282 // address will come from bit 1 of the base address.
9283 branch_offset = (branch_offset + 2) & ~3;
9285 // Put BRANCH_OFFSET back into the insn.
9286 gold_assert(!utils::has_overflow<25>(branch_offset));
9287 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
9288 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
9295 // Put the relocated value back in the object file:
9296 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
9297 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
9300 template<bool big_endian>
9301 class Target_selector_arm : public Target_selector
9304 Target_selector_arm()
9305 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
9306 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
9310 do_instantiate_target()
9311 { return new Target_arm<big_endian>(); }
9314 // Fix .ARM.exidx section coverage.
9316 template<bool big_endian>
9318 Target_arm<big_endian>::fix_exidx_coverage(
9320 Arm_output_section<big_endian>* exidx_section,
9321 Symbol_table* symtab)
9323 // We need to look at all the input sections in output in ascending
9324 // order of of output address. We do that by building a sorted list
9325 // of output sections by addresses. Then we looks at the output sections
9326 // in order. The input sections in an output section are already sorted
9327 // by addresses within the output section.
9329 typedef std::set<Output_section*, output_section_address_less_than>
9330 Sorted_output_section_list;
9331 Sorted_output_section_list sorted_output_sections;
9332 Layout::Section_list section_list;
9333 layout->get_allocated_sections(§ion_list);
9334 for (Layout::Section_list::const_iterator p = section_list.begin();
9335 p != section_list.end();
9338 // We only care about output sections that contain executable code.
9339 if (((*p)->flags() & elfcpp::SHF_EXECINSTR) != 0)
9340 sorted_output_sections.insert(*p);
9343 // Go over the output sections in ascending order of output addresses.
9344 typedef typename Arm_output_section<big_endian>::Text_section_list
9346 Text_section_list sorted_text_sections;
9347 for(typename Sorted_output_section_list::iterator p =
9348 sorted_output_sections.begin();
9349 p != sorted_output_sections.end();
9352 Arm_output_section<big_endian>* arm_output_section =
9353 Arm_output_section<big_endian>::as_arm_output_section(*p);
9354 arm_output_section->append_text_sections_to_list(&sorted_text_sections);
9357 exidx_section->fix_exidx_coverage(sorted_text_sections, symtab);
9360 Target_selector_arm<false> target_selector_arm;
9361 Target_selector_arm<true> target_selector_armbe;
9363 } // End anonymous namespace.