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.
35 #include "parameters.h"
42 #include "copy-relocs.h"
44 #include "target-reloc.h"
45 #include "target-select.h"
55 template<bool big_endian>
56 class Output_data_plt_arm;
58 template<bool big_endian>
61 template<bool big_endian>
62 class Arm_input_section;
64 template<bool big_endian>
65 class Arm_output_section;
67 template<bool big_endian>
70 template<bool big_endian>
74 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
76 // Maximum branch offsets for ARM, THUMB and THUMB2.
77 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
78 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
79 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
80 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
81 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
82 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
84 // The arm target class.
86 // This is a very simple port of gold for ARM-EABI. It is intended for
87 // supporting Android only for the time being. Only these relocation types
116 // R_ARM_THM_MOVW_ABS_NC
117 // R_ARM_THM_MOVT_ABS
118 // R_ARM_MOVW_PREL_NC
120 // R_ARM_THM_MOVW_PREL_NC
121 // R_ARM_THM_MOVT_PREL
124 // - Generate various branch stubs.
125 // - Support interworking.
126 // - Define section symbols __exidx_start and __exidx_stop.
127 // - Support more relocation types as needed.
128 // - Make PLTs more flexible for different architecture features like
130 // There are probably a lot more.
132 // Instruction template class. This class is similar to the insn_sequence
133 // struct in bfd/elf32-arm.c.
138 // Types of instruction templates.
147 // Factory methods to create instrunction templates in different formats.
149 static const Insn_template
150 thumb16_insn(uint32_t data)
151 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
153 // A bit of a hack. A Thumb conditional branch, in which the proper
154 // condition is inserted when we build the stub.
155 static const Insn_template
156 thumb16_bcond_insn(uint32_t data)
157 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 1); }
159 static const Insn_template
160 thumb32_insn(uint32_t data)
161 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
163 static const Insn_template
164 thumb32_b_insn(uint32_t data, int reloc_addend)
166 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
170 static const Insn_template
171 arm_insn(uint32_t data)
172 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
174 static const Insn_template
175 arm_rel_insn(unsigned data, int reloc_addend)
176 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
178 static const Insn_template
179 data_word(unsigned data, unsigned int r_type, int reloc_addend)
180 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
182 // Accessors. This class is used for read-only objects so no modifiers
187 { return this->data_; }
189 // Return the instruction sequence type of this.
192 { return this->type_; }
194 // Return the ARM relocation type of this.
197 { return this->r_type_; }
201 { return this->reloc_addend_; }
203 // Return size of instrunction template in bytes.
207 // Return byte-alignment of instrunction template.
212 // We make the constructor private to ensure that only the factory
215 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
216 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
219 // Instruction specific data. This is used to store information like
220 // some of the instruction bits.
222 // Instruction template type.
224 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
225 unsigned int r_type_;
226 // Relocation addend.
227 int32_t reloc_addend_;
230 // Macro for generating code to stub types. One entry per long/short
234 DEF_STUB(long_branch_any_any) \
235 DEF_STUB(long_branch_v4t_arm_thumb) \
236 DEF_STUB(long_branch_thumb_only) \
237 DEF_STUB(long_branch_v4t_thumb_thumb) \
238 DEF_STUB(long_branch_v4t_thumb_arm) \
239 DEF_STUB(short_branch_v4t_thumb_arm) \
240 DEF_STUB(long_branch_any_arm_pic) \
241 DEF_STUB(long_branch_any_thumb_pic) \
242 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
243 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
244 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
245 DEF_STUB(long_branch_thumb_only_pic) \
246 DEF_STUB(a8_veneer_b_cond) \
247 DEF_STUB(a8_veneer_b) \
248 DEF_STUB(a8_veneer_bl) \
249 DEF_STUB(a8_veneer_blx)
253 #define DEF_STUB(x) arm_stub_##x,
259 // First reloc stub type.
260 arm_stub_reloc_first = arm_stub_long_branch_any_any,
261 // Last reloc stub type.
262 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
264 // First Cortex-A8 stub type.
265 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
266 // Last Cortex-A8 stub type.
267 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
270 arm_stub_type_last = arm_stub_a8_veneer_blx
274 // Stub template class. Templates are meant to be read-only objects.
275 // A stub template for a stub type contains all read-only attributes
276 // common to all stubs of the same type.
281 Stub_template(Stub_type, const Insn_template*, size_t);
289 { return this->type_; }
291 // Return an array of instruction templates.
294 { return this->insns_; }
296 // Return size of template in number of instructions.
299 { return this->insn_count_; }
301 // Return size of template in bytes.
304 { return this->size_; }
306 // Return alignment of the stub template.
309 { return this->alignment_; }
311 // Return whether entry point is in thumb mode.
313 entry_in_thumb_mode() const
314 { return this->entry_in_thumb_mode_; }
316 // Return number of relocations in this template.
319 { return this->relocs_.size(); }
321 // Return index of the I-th instruction with relocation.
323 reloc_insn_index(size_t i) const
325 gold_assert(i < this->relocs_.size());
326 return this->relocs_[i].first;
329 // Return the offset of the I-th instruction with relocation from the
330 // beginning of the stub.
332 reloc_offset(size_t i) const
334 gold_assert(i < this->relocs_.size());
335 return this->relocs_[i].second;
339 // This contains information about an instruction template with a relocation
340 // and its offset from start of stub.
341 typedef std::pair<size_t, section_size_type> Reloc;
343 // A Stub_template may not be copied. We want to share templates as much
345 Stub_template(const Stub_template&);
346 Stub_template& operator=(const Stub_template&);
350 // Points to an array of Insn_templates.
351 const Insn_template* insns_;
352 // Number of Insn_templates in insns_[].
354 // Size of templated instructions in bytes.
356 // Alignment of templated instructions.
358 // Flag to indicate if entry is in thumb mode.
359 bool entry_in_thumb_mode_;
360 // A table of reloc instruction indices and offsets. We can find these by
361 // looking at the instruction templates but we pre-compute and then stash
362 // them here for speed.
363 std::vector<Reloc> relocs_;
367 // A class for code stubs. This is a base class for different type of
368 // stubs used in the ARM target.
374 static const section_offset_type invalid_offset =
375 static_cast<section_offset_type>(-1);
378 Stub(const Stub_template* stub_template)
379 : stub_template_(stub_template), offset_(invalid_offset)
386 // Return the stub template.
388 stub_template() const
389 { return this->stub_template_; }
391 // Return offset of code stub from beginning of its containing stub table.
395 gold_assert(this->offset_ != invalid_offset);
396 return this->offset_;
399 // Set offset of code stub from beginning of its containing stub table.
401 set_offset(section_offset_type offset)
402 { this->offset_ = offset; }
404 // Return the relocation target address of the i-th relocation in the
405 // stub. This must be defined in a child class.
407 reloc_target(size_t i)
408 { return this->do_reloc_target(i); }
410 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
412 write(unsigned char* view, section_size_type view_size, bool big_endian)
413 { this->do_write(view, view_size, big_endian); }
416 // This must be defined in the child class.
418 do_reloc_target(size_t) = 0;
420 // This must be defined in the child class.
422 do_write(unsigned char*, section_size_type, bool) = 0;
426 const Stub_template* stub_template_;
427 // Offset within the section of containing this stub.
428 section_offset_type offset_;
431 // Reloc stub class. These are stubs we use to fix up relocation because
432 // of limited branch ranges.
434 class Reloc_stub : public Stub
437 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
438 // We assume we never jump to this address.
439 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
441 // Return destination address.
443 destination_address() const
445 gold_assert(this->destination_address_ != this->invalid_address);
446 return this->destination_address_;
449 // Set destination address.
451 set_destination_address(Arm_address address)
453 gold_assert(address != this->invalid_address);
454 this->destination_address_ = address;
457 // Reset destination address.
459 reset_destination_address()
460 { this->destination_address_ = this->invalid_address; }
462 // Determine stub type for a branch of a relocation of R_TYPE going
463 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
464 // the branch target is a thumb instruction. TARGET is used for look
465 // up ARM-specific linker settings.
467 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
468 Arm_address branch_target, bool target_is_thumb);
470 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
471 // and an addend. Since we treat global and local symbol differently, we
472 // use a Symbol object for a global symbol and a object-index pair for
477 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
478 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
479 // and R_SYM must not be invalid_index.
480 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
481 unsigned int r_sym, int32_t addend)
482 : stub_type_(stub_type), addend_(addend)
486 this->r_sym_ = Reloc_stub::invalid_index;
487 this->u_.symbol = symbol;
491 gold_assert(relobj != NULL && r_sym != invalid_index);
492 this->r_sym_ = r_sym;
493 this->u_.relobj = relobj;
500 // Accessors: Keys are meant to be read-only object so no modifiers are
506 { return this->stub_type_; }
508 // Return the local symbol index or invalid_index.
511 { return this->r_sym_; }
513 // Return the symbol if there is one.
516 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
518 // Return the relobj if there is one.
521 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
523 // Whether this equals to another key k.
525 eq(const Key& k) const
527 return ((this->stub_type_ == k.stub_type_)
528 && (this->r_sym_ == k.r_sym_)
529 && ((this->r_sym_ != Reloc_stub::invalid_index)
530 ? (this->u_.relobj == k.u_.relobj)
531 : (this->u_.symbol == k.u_.symbol))
532 && (this->addend_ == k.addend_));
535 // Return a hash value.
539 return (this->stub_type_
541 ^ gold::string_hash<char>(
542 (this->r_sym_ != Reloc_stub::invalid_index)
543 ? this->u_.relobj->name().c_str()
544 : this->u_.symbol->name())
548 // Functors for STL associative containers.
552 operator()(const Key& k) const
553 { return k.hash_value(); }
559 operator()(const Key& k1, const Key& k2) const
560 { return k1.eq(k2); }
563 // Name of key. This is mainly for debugging.
569 Stub_type stub_type_;
570 // If this is a local symbol, this is the index in the defining object.
571 // Otherwise, it is invalid_index for a global symbol.
573 // If r_sym_ is invalid index. This points to a global symbol.
574 // Otherwise, this points a relobj. We used the unsized and target
575 // independent Symbol and Relobj classes instead of Arm_symbol and
576 // Arm_relobj. This is done to avoid making the stub class a template
577 // as most of the stub machinery is endianity-neutral. However, it
578 // may require a bit of casting done by users of this class.
581 const Symbol* symbol;
582 const Relobj* relobj;
584 // Addend associated with a reloc.
589 // Reloc_stubs are created via a stub factory. So these are protected.
590 Reloc_stub(const Stub_template* stub_template)
591 : Stub(stub_template), destination_address_(invalid_address)
597 friend class Stub_factory;
600 // Return the relocation target address of the i-th relocation in the
603 do_reloc_target(size_t i)
605 // All reloc stub have only one relocation.
607 return this->destination_address_;
610 // A template to implement do_write below.
611 template<bool big_endian>
613 do_fixed_endian_write(unsigned char*, section_size_type);
617 do_write(unsigned char* view, section_size_type view_size, bool big_endian);
619 // Address of destination.
620 Arm_address destination_address_;
623 // Stub factory class.
628 // Return the unique instance of this class.
629 static const Stub_factory&
632 static Stub_factory singleton;
636 // Make a relocation stub.
638 make_reloc_stub(Stub_type stub_type) const
640 gold_assert(stub_type >= arm_stub_reloc_first
641 && stub_type <= arm_stub_reloc_last);
642 return new Reloc_stub(this->stub_templates_[stub_type]);
646 // Constructor and destructor are protected since we only return a single
647 // instance created in Stub_factory::get_instance().
651 // A Stub_factory may not be copied since it is a singleton.
652 Stub_factory(const Stub_factory&);
653 Stub_factory& operator=(Stub_factory&);
655 // Stub templates. These are initialized in the constructor.
656 const Stub_template* stub_templates_[arm_stub_type_last+1];
659 // A class to hold stubs for the ARM target.
661 template<bool big_endian>
662 class Stub_table : public Output_data
665 Stub_table(Arm_input_section<big_endian>* owner)
666 : Output_data(), addralign_(1), owner_(owner), has_been_changed_(false),
673 // Owner of this stub table.
674 Arm_input_section<big_endian>*
676 { return this->owner_; }
678 // Whether this stub table is empty.
681 { return this->reloc_stubs_.empty(); }
683 // Whether this has been changed.
685 has_been_changed() const
686 { return this->has_been_changed_; }
688 // Set the has-been-changed flag.
690 set_has_been_changed(bool value)
691 { this->has_been_changed_ = value; }
693 // Return the current data size.
695 current_data_size() const
696 { return this->current_data_size_for_child(); }
698 // Add a STUB with using KEY. Caller is reponsible for avoid adding
699 // if already a STUB with the same key has been added.
701 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key);
703 // Look up a relocation stub using KEY. Return NULL if there is none.
705 find_reloc_stub(const Reloc_stub::Key& key) const
707 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
708 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
711 // Relocate stubs in this stub table.
713 relocate_stubs(const Relocate_info<32, big_endian>*,
714 Target_arm<big_endian>*, Output_section*,
715 unsigned char*, Arm_address, section_size_type);
718 // Write out section contents.
720 do_write(Output_file*);
722 // Return the required alignment.
725 { return this->addralign_; }
727 // Finalize data size.
729 set_final_data_size()
730 { this->set_data_size(this->current_data_size_for_child()); }
732 // Reset address and file offset.
734 do_reset_address_and_file_offset();
737 // Unordered map of stubs.
739 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
740 Reloc_stub::Key::equal_to>
745 // Owner of this stub table.
746 Arm_input_section<big_endian>* owner_;
747 // This is set to true during relaxiong if the size of the stub table
749 bool has_been_changed_;
750 // The relocation stubs.
751 Reloc_stub_map reloc_stubs_;
754 // A class to wrap an ordinary input section containing executable code.
756 template<bool big_endian>
757 class Arm_input_section : public Output_relaxed_input_section
760 Arm_input_section(Relobj* relobj, unsigned int shndx)
761 : Output_relaxed_input_section(relobj, shndx, 1),
762 original_addralign_(1), original_size_(0), stub_table_(NULL)
772 // Whether this is a stub table owner.
774 is_stub_table_owner() const
775 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
777 // Return the stub table.
778 Stub_table<big_endian>*
780 { return this->stub_table_; }
782 // Set the stub_table.
784 set_stub_table(Stub_table<big_endian>* stub_table)
785 { this->stub_table_ = stub_table; }
787 // Downcast a base pointer to an Arm_input_section pointer. This is
788 // not type-safe but we only use Arm_input_section not the base class.
789 static Arm_input_section<big_endian>*
790 as_arm_input_section(Output_relaxed_input_section* poris)
791 { return static_cast<Arm_input_section<big_endian>*>(poris); }
794 // Write data to output file.
796 do_write(Output_file*);
798 // Return required alignment of this.
802 if (this->is_stub_table_owner())
803 return std::max(this->stub_table_->addralign(),
804 this->original_addralign_);
806 return this->original_addralign_;
809 // Finalize data size.
811 set_final_data_size();
813 // Reset address and file offset.
815 do_reset_address_and_file_offset();
819 do_output_offset(const Relobj* object, unsigned int shndx,
820 section_offset_type offset,
821 section_offset_type* poutput) const
823 if ((object == this->relobj())
824 && (shndx == this->shndx())
826 && (convert_types<uint64_t, section_offset_type>(offset)
827 <= this->original_size_))
837 // Copying is not allowed.
838 Arm_input_section(const Arm_input_section&);
839 Arm_input_section& operator=(const Arm_input_section&);
841 // Address alignment of the original input section.
842 uint64_t original_addralign_;
843 // Section size of the original input section.
844 uint64_t original_size_;
846 Stub_table<big_endian>* stub_table_;
849 // Arm output section class. This is defined mainly to add a number of
850 // stub generation methods.
852 template<bool big_endian>
853 class Arm_output_section : public Output_section
856 Arm_output_section(const char* name, elfcpp::Elf_Word type,
857 elfcpp::Elf_Xword flags)
858 : Output_section(name, type, flags)
861 ~Arm_output_section()
864 // Group input sections for stub generation.
866 group_sections(section_size_type, bool, Target_arm<big_endian>*);
868 // Downcast a base pointer to an Arm_output_section pointer. This is
869 // not type-safe but we only use Arm_output_section not the base class.
870 static Arm_output_section<big_endian>*
871 as_arm_output_section(Output_section* os)
872 { return static_cast<Arm_output_section<big_endian>*>(os); }
876 typedef Output_section::Input_section Input_section;
877 typedef Output_section::Input_section_list Input_section_list;
879 // Create a stub group.
880 void create_stub_group(Input_section_list::const_iterator,
881 Input_section_list::const_iterator,
882 Input_section_list::const_iterator,
883 Target_arm<big_endian>*,
884 std::vector<Output_relaxed_input_section*>*);
889 template<bool big_endian>
890 class Arm_relobj : public Sized_relobj<32, big_endian>
893 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
895 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
896 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
897 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
898 stub_tables_(), local_symbol_is_thumb_function_()
904 // Return the stub table of the SHNDX-th section if there is one.
905 Stub_table<big_endian>*
906 stub_table(unsigned int shndx) const
908 gold_assert(shndx < this->stub_tables_.size());
909 return this->stub_tables_[shndx];
912 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
914 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
916 gold_assert(shndx < this->stub_tables_.size());
917 this->stub_tables_[shndx] = stub_table;
920 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
921 // index. This is only valid after do_count_local_symbol is called.
923 local_symbol_is_thumb_function(unsigned int r_sym) const
925 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
926 return this->local_symbol_is_thumb_function_[r_sym];
929 // Scan all relocation sections for stub generation.
931 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
934 // Convert regular input section with index SHNDX to a relaxed section.
936 convert_input_section_to_relaxed_section(unsigned shndx)
938 // The stubs have relocations and we need to process them after writing
939 // out the stubs. So relocation now must follow section write.
940 this->invalidate_section_offset(shndx);
941 this->set_relocs_must_follow_section_writes();
944 // Downcast a base pointer to an Arm_relobj pointer. This is
945 // not type-safe but we only use Arm_relobj not the base class.
946 static Arm_relobj<big_endian>*
947 as_arm_relobj(Relobj* relobj)
948 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
950 // Processor-specific flags in ELF file header. This is valid only after
953 processor_specific_flags() const
954 { return this->processor_specific_flags_; }
957 // Post constructor setup.
961 // Call parent's setup method.
962 Sized_relobj<32, big_endian>::do_setup();
964 // Initialize look-up tables.
965 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
966 this->stub_tables_.swap(empty_stub_table_list);
969 // Count the local symbols.
971 do_count_local_symbols(Stringpool_template<char>*,
972 Stringpool_template<char>*);
975 do_relocate_sections(const General_options& options,
976 const Symbol_table* symtab, const Layout* layout,
977 const unsigned char* pshdrs,
978 typename Sized_relobj<32, big_endian>::Views* pivews);
980 // Read the symbol information.
982 do_read_symbols(Read_symbols_data* sd);
985 // List of stub tables.
986 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
987 Stub_table_list stub_tables_;
988 // Bit vector to tell if a local symbol is a thumb function or not.
989 // This is only valid after do_count_local_symbol is called.
990 std::vector<bool> local_symbol_is_thumb_function_;
991 // processor-specific flags in ELF file header.
992 elfcpp::Elf_Word processor_specific_flags_;
997 template<bool big_endian>
998 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1001 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1002 const elfcpp::Ehdr<32, big_endian>& ehdr)
1003 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1004 processor_specific_flags_(0)
1010 // Downcast a base pointer to an Arm_relobj pointer. This is
1011 // not type-safe but we only use Arm_relobj not the base class.
1012 static Arm_dynobj<big_endian>*
1013 as_arm_dynobj(Dynobj* dynobj)
1014 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1016 // Processor-specific flags in ELF file header. This is valid only after
1019 processor_specific_flags() const
1020 { return this->processor_specific_flags_; }
1023 // Read the symbol information.
1025 do_read_symbols(Read_symbols_data* sd);
1028 // processor-specific flags in ELF file header.
1029 elfcpp::Elf_Word processor_specific_flags_;
1032 // Functor to read reloc addends during stub generation.
1034 template<int sh_type, bool big_endian>
1035 struct Stub_addend_reader
1037 // Return the addend for a relocation of a particular type. Depending
1038 // on whether this is a REL or RELA relocation, read the addend from a
1039 // view or from a Reloc object.
1040 elfcpp::Elf_types<32>::Elf_Swxword
1042 unsigned int /* r_type */,
1043 const unsigned char* /* view */,
1044 const typename Reloc_types<sh_type,
1045 32, big_endian>::Reloc& /* reloc */) const;
1048 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1050 template<bool big_endian>
1051 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1053 elfcpp::Elf_types<32>::Elf_Swxword
1056 const unsigned char*,
1057 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1060 // Specialized Stub_addend_reader for RELA type relocation sections.
1061 // We currently do not handle RELA type relocation sections but it is trivial
1062 // to implement the addend reader. This is provided for completeness and to
1063 // make it easier to add support for RELA relocation sections in the future.
1065 template<bool big_endian>
1066 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1068 elfcpp::Elf_types<32>::Elf_Swxword
1071 const unsigned char*,
1072 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1073 big_endian>::Reloc& reloc) const
1074 { return reloc.get_r_addend(); }
1077 // Utilities for manipulating integers of up to 32-bits
1081 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1082 // an int32_t. NO_BITS must be between 1 to 32.
1083 template<int no_bits>
1084 static inline int32_t
1085 sign_extend(uint32_t bits)
1087 gold_assert(no_bits >= 0 && no_bits <= 32);
1089 return static_cast<int32_t>(bits);
1090 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1092 uint32_t top_bit = 1U << (no_bits - 1);
1093 int32_t as_signed = static_cast<int32_t>(bits);
1094 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1097 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1098 template<int no_bits>
1100 has_overflow(uint32_t bits)
1102 gold_assert(no_bits >= 0 && no_bits <= 32);
1105 int32_t max = (1 << (no_bits - 1)) - 1;
1106 int32_t min = -(1 << (no_bits - 1));
1107 int32_t as_signed = static_cast<int32_t>(bits);
1108 return as_signed > max || as_signed < min;
1111 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1112 // fits in the given number of bits as either a signed or unsigned value.
1113 // For example, has_signed_unsigned_overflow<8> would check
1114 // -128 <= bits <= 255
1115 template<int no_bits>
1117 has_signed_unsigned_overflow(uint32_t bits)
1119 gold_assert(no_bits >= 2 && no_bits <= 32);
1122 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1123 int32_t min = -(1 << (no_bits - 1));
1124 int32_t as_signed = static_cast<int32_t>(bits);
1125 return as_signed > max || as_signed < min;
1128 // Select bits from A and B using bits in MASK. For each n in [0..31],
1129 // the n-th bit in the result is chosen from the n-th bits of A and B.
1130 // A zero selects A and a one selects B.
1131 static inline uint32_t
1132 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1133 { return (a & ~mask) | (b & mask); }
1136 template<bool big_endian>
1137 class Target_arm : public Sized_target<32, big_endian>
1140 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1144 : Sized_target<32, big_endian>(&arm_info),
1145 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1146 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1147 stub_factory_(Stub_factory::get_instance()),
1148 may_use_blx_(true), should_force_pic_veneer_(false)
1151 // Whether we can use BLX.
1154 { return this->may_use_blx_; }
1156 // Set use-BLX flag.
1158 set_may_use_blx(bool value)
1159 { this->may_use_blx_ = value; }
1161 // Whether we force PCI branch veneers.
1163 should_force_pic_veneer() const
1164 { return this->should_force_pic_veneer_; }
1166 // Set PIC veneer flag.
1168 set_should_force_pic_veneer(bool value)
1169 { this->should_force_pic_veneer_ = value; }
1171 // Whether we use THUMB-2 instructions.
1173 using_thumb2() const
1175 // FIXME: This should not hard-coded.
1179 // Whether we use THUMB/THUMB-2 instructions only.
1181 using_thumb_only() const
1183 // FIXME: This should not hard-coded.
1187 // Process the relocations to determine unreferenced sections for
1188 // garbage collection.
1190 gc_process_relocs(Symbol_table* symtab,
1192 Sized_relobj<32, big_endian>* object,
1193 unsigned int data_shndx,
1194 unsigned int sh_type,
1195 const unsigned char* prelocs,
1197 Output_section* output_section,
1198 bool needs_special_offset_handling,
1199 size_t local_symbol_count,
1200 const unsigned char* plocal_symbols);
1202 // Scan the relocations to look for symbol adjustments.
1204 scan_relocs(Symbol_table* symtab,
1206 Sized_relobj<32, big_endian>* object,
1207 unsigned int data_shndx,
1208 unsigned int sh_type,
1209 const unsigned char* prelocs,
1211 Output_section* output_section,
1212 bool needs_special_offset_handling,
1213 size_t local_symbol_count,
1214 const unsigned char* plocal_symbols);
1216 // Finalize the sections.
1218 do_finalize_sections(Layout*, const Input_objects*);
1220 // Return the value to use for a dynamic symbol which requires special
1223 do_dynsym_value(const Symbol*) const;
1225 // Relocate a section.
1227 relocate_section(const Relocate_info<32, big_endian>*,
1228 unsigned int sh_type,
1229 const unsigned char* prelocs,
1231 Output_section* output_section,
1232 bool needs_special_offset_handling,
1233 unsigned char* view,
1234 Arm_address view_address,
1235 section_size_type view_size,
1236 const Reloc_symbol_changes*);
1238 // Scan the relocs during a relocatable link.
1240 scan_relocatable_relocs(Symbol_table* symtab,
1242 Sized_relobj<32, big_endian>* object,
1243 unsigned int data_shndx,
1244 unsigned int sh_type,
1245 const unsigned char* prelocs,
1247 Output_section* output_section,
1248 bool needs_special_offset_handling,
1249 size_t local_symbol_count,
1250 const unsigned char* plocal_symbols,
1251 Relocatable_relocs*);
1253 // Relocate a section during a relocatable link.
1255 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1256 unsigned int sh_type,
1257 const unsigned char* prelocs,
1259 Output_section* output_section,
1260 off_t offset_in_output_section,
1261 const Relocatable_relocs*,
1262 unsigned char* view,
1263 Arm_address view_address,
1264 section_size_type view_size,
1265 unsigned char* reloc_view,
1266 section_size_type reloc_view_size);
1268 // Return whether SYM is defined by the ABI.
1270 do_is_defined_by_abi(Symbol* sym) const
1271 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1273 // Return the size of the GOT section.
1277 gold_assert(this->got_ != NULL);
1278 return this->got_->data_size();
1281 // Map platform-specific reloc types
1283 get_real_reloc_type (unsigned int r_type);
1286 // Methods to support stub-generations.
1289 // Return the stub factory
1291 stub_factory() const
1292 { return this->stub_factory_; }
1294 // Make a new Arm_input_section object.
1295 Arm_input_section<big_endian>*
1296 new_arm_input_section(Relobj*, unsigned int);
1298 // Find the Arm_input_section object corresponding to the SHNDX-th input
1299 // section of RELOBJ.
1300 Arm_input_section<big_endian>*
1301 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
1303 // Make a new Stub_table
1304 Stub_table<big_endian>*
1305 new_stub_table(Arm_input_section<big_endian>*);
1307 // Get the default ARM target.
1308 static const Target_arm<big_endian>&
1311 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1312 && parameters->target().is_big_endian() == big_endian);
1313 return static_cast<const Target_arm<big_endian>&>(parameters->target());
1316 // Whether relocation type uses LSB to distinguish THUMB addresses.
1318 reloc_uses_thumb_bit(unsigned int r_type);
1322 do_adjust_elf_header(unsigned char* view, int len) const;
1325 // The class which scans relocations.
1330 : issued_non_pic_error_(false)
1334 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1335 Sized_relobj<32, big_endian>* object,
1336 unsigned int data_shndx,
1337 Output_section* output_section,
1338 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1339 const elfcpp::Sym<32, big_endian>& lsym);
1342 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1343 Sized_relobj<32, big_endian>* object,
1344 unsigned int data_shndx,
1345 Output_section* output_section,
1346 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1351 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1352 unsigned int r_type);
1355 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1356 unsigned int r_type, Symbol*);
1359 check_non_pic(Relobj*, unsigned int r_type);
1361 // Almost identical to Symbol::needs_plt_entry except that it also
1362 // handles STT_ARM_TFUNC.
1364 symbol_needs_plt_entry(const Symbol* sym)
1366 // An undefined symbol from an executable does not need a PLT entry.
1367 if (sym->is_undefined() && !parameters->options().shared())
1370 return (!parameters->doing_static_link()
1371 && (sym->type() == elfcpp::STT_FUNC
1372 || sym->type() == elfcpp::STT_ARM_TFUNC)
1373 && (sym->is_from_dynobj()
1374 || sym->is_undefined()
1375 || sym->is_preemptible()));
1378 // Whether we have issued an error about a non-PIC compilation.
1379 bool issued_non_pic_error_;
1382 // The class which implements relocation.
1392 // Return whether the static relocation needs to be applied.
1394 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1397 Output_section* output_section);
1399 // Do a relocation. Return false if the caller should not issue
1400 // any warnings about this relocation.
1402 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1403 Output_section*, size_t relnum,
1404 const elfcpp::Rel<32, big_endian>&,
1405 unsigned int r_type, const Sized_symbol<32>*,
1406 const Symbol_value<32>*,
1407 unsigned char*, Arm_address,
1410 // Return whether we want to pass flag NON_PIC_REF for this
1413 reloc_is_non_pic (unsigned int r_type)
1417 case elfcpp::R_ARM_REL32:
1418 case elfcpp::R_ARM_THM_CALL:
1419 case elfcpp::R_ARM_CALL:
1420 case elfcpp::R_ARM_JUMP24:
1421 case elfcpp::R_ARM_PREL31:
1422 case elfcpp::R_ARM_THM_ABS5:
1423 case elfcpp::R_ARM_ABS8:
1424 case elfcpp::R_ARM_ABS12:
1425 case elfcpp::R_ARM_ABS16:
1426 case elfcpp::R_ARM_BASE_ABS:
1434 // A class which returns the size required for a relocation type,
1435 // used while scanning relocs during a relocatable link.
1436 class Relocatable_size_for_reloc
1440 get_size_for_reloc(unsigned int, Relobj*);
1443 // Get the GOT section, creating it if necessary.
1444 Output_data_got<32, big_endian>*
1445 got_section(Symbol_table*, Layout*);
1447 // Get the GOT PLT section.
1449 got_plt_section() const
1451 gold_assert(this->got_plt_ != NULL);
1452 return this->got_plt_;
1455 // Create a PLT entry for a global symbol.
1457 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1459 // Get the PLT section.
1460 const Output_data_plt_arm<big_endian>*
1463 gold_assert(this->plt_ != NULL);
1467 // Get the dynamic reloc section, creating it if necessary.
1469 rel_dyn_section(Layout*);
1471 // Return true if the symbol may need a COPY relocation.
1472 // References from an executable object to non-function symbols
1473 // defined in a dynamic object may need a COPY relocation.
1475 may_need_copy_reloc(Symbol* gsym)
1477 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1478 && gsym->may_need_copy_reloc());
1481 // Add a potential copy relocation.
1483 copy_reloc(Symbol_table* symtab, Layout* layout,
1484 Sized_relobj<32, big_endian>* object,
1485 unsigned int shndx, Output_section* output_section,
1486 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1488 this->copy_relocs_.copy_reloc(symtab, layout,
1489 symtab->get_sized_symbol<32>(sym),
1490 object, shndx, output_section, reloc,
1491 this->rel_dyn_section(layout));
1494 // Whether two EABI versions are compatible.
1496 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1498 // Merge processor-specific flags from input object and those in the ELF
1499 // header of the output.
1501 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1504 do_make_elf_object(const std::string&, Input_file*, off_t,
1505 const elfcpp::Ehdr<32, big_endian>& ehdr);
1508 do_make_elf_object(const std::string&, Input_file*, off_t,
1509 const elfcpp::Ehdr<32, !big_endian>&)
1510 { gold_unreachable(); }
1513 do_make_elf_object(const std::string&, Input_file*, off_t,
1514 const elfcpp::Ehdr<64, false>&)
1515 { gold_unreachable(); }
1518 do_make_elf_object(const std::string&, Input_file*, off_t,
1519 const elfcpp::Ehdr<64, true>&)
1520 { gold_unreachable(); }
1522 // Information about this specific target which we pass to the
1523 // general Target structure.
1524 static const Target::Target_info arm_info;
1526 // The types of GOT entries needed for this platform.
1529 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1532 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
1534 // Map input section to Arm_input_section.
1535 typedef Unordered_map<Input_section_specifier,
1536 Arm_input_section<big_endian>*,
1537 Input_section_specifier::hash,
1538 Input_section_specifier::equal_to>
1539 Arm_input_section_map;
1542 Output_data_got<32, big_endian>* got_;
1544 Output_data_plt_arm<big_endian>* plt_;
1545 // The GOT PLT section.
1546 Output_data_space* got_plt_;
1547 // The dynamic reloc section.
1548 Reloc_section* rel_dyn_;
1549 // Relocs saved to avoid a COPY reloc.
1550 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1551 // Space for variables copied with a COPY reloc.
1552 Output_data_space* dynbss_;
1553 // Vector of Stub_tables created.
1554 Stub_table_list stub_tables_;
1556 const Stub_factory &stub_factory_;
1557 // Whether we can use BLX.
1559 // Whether we force PIC branch veneers.
1560 bool should_force_pic_veneer_;
1563 template<bool big_endian>
1564 const Target::Target_info Target_arm<big_endian>::arm_info =
1567 big_endian, // is_big_endian
1568 elfcpp::EM_ARM, // machine_code
1569 false, // has_make_symbol
1570 false, // has_resolve
1571 false, // has_code_fill
1572 true, // is_default_stack_executable
1574 "/usr/lib/libc.so.1", // dynamic_linker
1575 0x8000, // default_text_segment_address
1576 0x1000, // abi_pagesize (overridable by -z max-page-size)
1577 0x1000, // common_pagesize (overridable by -z common-page-size)
1578 elfcpp::SHN_UNDEF, // small_common_shndx
1579 elfcpp::SHN_UNDEF, // large_common_shndx
1580 0, // small_common_section_flags
1581 0 // large_common_section_flags
1584 // Arm relocate functions class
1587 template<bool big_endian>
1588 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1593 STATUS_OKAY, // No error during relocation.
1594 STATUS_OVERFLOW, // Relocation oveflow.
1595 STATUS_BAD_RELOC // Relocation cannot be applied.
1599 typedef Relocate_functions<32, big_endian> Base;
1600 typedef Arm_relocate_functions<big_endian> This;
1602 // Get an symbol value of *PSYMVAL with an ADDEND. This is a wrapper
1603 // to Symbol_value::value(). If HAS_THUMB_BIT is true, that LSB is used
1604 // to distinguish ARM and THUMB functions and it is treated specially.
1605 static inline Symbol_value<32>::Value
1606 arm_symbol_value (const Sized_relobj<32, big_endian> *object,
1607 const Symbol_value<32>* psymval,
1608 Symbol_value<32>::Value addend,
1611 typedef Symbol_value<32>::Value Valtype;
1615 Valtype raw = psymval->value(object, 0);
1616 Valtype thumb_bit = raw & 1;
1617 return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
1620 return psymval->value(object, addend);
1623 // Encoding of imm16 argument for movt and movw ARM instructions
1626 // imm16 := imm4 | imm12
1628 // 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
1629 // +-------+---------------+-------+-------+-----------------------+
1630 // | | |imm4 | |imm12 |
1631 // +-------+---------------+-------+-------+-----------------------+
1633 // Extract the relocation addend from VAL based on the ARM
1634 // instruction encoding described above.
1635 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1636 extract_arm_movw_movt_addend(
1637 typename elfcpp::Swap<32, big_endian>::Valtype val)
1639 // According to the Elf ABI for ARM Architecture the immediate
1640 // field is sign-extended to form the addend.
1641 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1644 // Insert X into VAL based on the ARM instruction encoding described
1646 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1647 insert_val_arm_movw_movt(
1648 typename elfcpp::Swap<32, big_endian>::Valtype val,
1649 typename elfcpp::Swap<32, big_endian>::Valtype x)
1653 val |= (x & 0xf000) << 4;
1657 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1660 // imm16 := imm4 | i | imm3 | imm8
1662 // 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
1663 // +---------+-+-----------+-------++-+-----+-------+---------------+
1664 // | |i| |imm4 || |imm3 | |imm8 |
1665 // +---------+-+-----------+-------++-+-----+-------+---------------+
1667 // Extract the relocation addend from VAL based on the Thumb2
1668 // instruction encoding described above.
1669 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1670 extract_thumb_movw_movt_addend(
1671 typename elfcpp::Swap<32, big_endian>::Valtype val)
1673 // According to the Elf ABI for ARM Architecture the immediate
1674 // field is sign-extended to form the addend.
1675 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1676 | ((val >> 15) & 0x0800)
1677 | ((val >> 4) & 0x0700)
1681 // Insert X into VAL based on the Thumb2 instruction encoding
1683 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1684 insert_val_thumb_movw_movt(
1685 typename elfcpp::Swap<32, big_endian>::Valtype val,
1686 typename elfcpp::Swap<32, big_endian>::Valtype x)
1689 val |= (x & 0xf000) << 4;
1690 val |= (x & 0x0800) << 15;
1691 val |= (x & 0x0700) << 4;
1692 val |= (x & 0x00ff);
1696 // FIXME: This probably only works for Android on ARM v5te. We should
1697 // following GNU ld for the general case.
1698 template<unsigned r_type>
1699 static inline typename This::Status
1700 arm_branch_common(unsigned char *view,
1701 const Sized_relobj<32, big_endian>* object,
1702 const Symbol_value<32>* psymval,
1703 Arm_address address,
1706 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1707 Valtype* wv = reinterpret_cast<Valtype*>(view);
1708 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1710 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
1711 && ((val & 0x0f000000UL) == 0x0a000000UL);
1712 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
1713 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
1714 && ((val & 0x0f000000UL) == 0x0b000000UL);
1715 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
1716 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
1718 if (r_type == elfcpp::R_ARM_CALL)
1720 if (!insn_is_uncond_bl && !insn_is_blx)
1721 return This::STATUS_BAD_RELOC;
1723 else if (r_type == elfcpp::R_ARM_JUMP24)
1725 if (!insn_is_b && !insn_is_cond_bl)
1726 return This::STATUS_BAD_RELOC;
1728 else if (r_type == elfcpp::R_ARM_PLT32)
1730 if (!insn_is_any_branch)
1731 return This::STATUS_BAD_RELOC;
1736 Valtype addend = utils::sign_extend<26>(val << 2);
1737 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1740 // If target has thumb bit set, we need to either turn the BL
1741 // into a BLX (for ARMv5 or above) or generate a stub.
1745 if (insn_is_uncond_bl)
1746 val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
1748 return This::STATUS_BAD_RELOC;
1751 gold_assert(!insn_is_blx);
1753 val = utils::bit_select(val, (x >> 2), 0xffffffUL);
1754 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1755 return (utils::has_overflow<26>(x)
1756 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
1761 // R_ARM_ABS8: S + A
1762 static inline typename This::Status
1763 abs8(unsigned char *view,
1764 const Sized_relobj<32, big_endian>* object,
1765 const Symbol_value<32>* psymval)
1767 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
1768 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1769 Valtype* wv = reinterpret_cast<Valtype*>(view);
1770 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
1771 Reltype addend = utils::sign_extend<8>(val);
1772 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1773 val = utils::bit_select(val, x, 0xffU);
1774 elfcpp::Swap<8, big_endian>::writeval(wv, val);
1775 return (utils::has_signed_unsigned_overflow<8>(x)
1776 ? This::STATUS_OVERFLOW
1777 : This::STATUS_OKAY);
1780 // R_ARM_THM_ABS5: S + A
1781 static inline typename This::Status
1782 thm_abs5(unsigned char *view,
1783 const Sized_relobj<32, big_endian>* object,
1784 const Symbol_value<32>* psymval)
1786 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1787 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1788 Valtype* wv = reinterpret_cast<Valtype*>(view);
1789 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1790 Reltype addend = (val & 0x7e0U) >> 6;
1791 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1792 val = utils::bit_select(val, x << 6, 0x7e0U);
1793 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1794 return (utils::has_overflow<5>(x)
1795 ? This::STATUS_OVERFLOW
1796 : This::STATUS_OKAY);
1799 // R_ARM_ABS12: S + A
1800 static inline typename This::Status
1801 abs12(unsigned char *view,
1802 const Sized_relobj<32, big_endian>* object,
1803 const Symbol_value<32>* psymval)
1805 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1806 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1807 Valtype* wv = reinterpret_cast<Valtype*>(view);
1808 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1809 Reltype addend = val & 0x0fffU;
1810 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1811 val = utils::bit_select(val, x, 0x0fffU);
1812 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1813 return (utils::has_overflow<12>(x)
1814 ? This::STATUS_OVERFLOW
1815 : This::STATUS_OKAY);
1818 // R_ARM_ABS16: S + A
1819 static inline typename This::Status
1820 abs16(unsigned char *view,
1821 const Sized_relobj<32, big_endian>* object,
1822 const Symbol_value<32>* psymval)
1824 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1825 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1826 Valtype* wv = reinterpret_cast<Valtype*>(view);
1827 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1828 Reltype addend = utils::sign_extend<16>(val);
1829 Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1830 val = utils::bit_select(val, x, 0xffffU);
1831 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1832 return (utils::has_signed_unsigned_overflow<16>(x)
1833 ? This::STATUS_OVERFLOW
1834 : This::STATUS_OKAY);
1837 // R_ARM_ABS32: (S + A) | T
1838 static inline typename This::Status
1839 abs32(unsigned char *view,
1840 const Sized_relobj<32, big_endian>* object,
1841 const Symbol_value<32>* psymval,
1844 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1845 Valtype* wv = reinterpret_cast<Valtype*>(view);
1846 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1847 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1848 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1849 return This::STATUS_OKAY;
1852 // R_ARM_REL32: (S + A) | T - P
1853 static inline typename This::Status
1854 rel32(unsigned char *view,
1855 const Sized_relobj<32, big_endian>* object,
1856 const Symbol_value<32>* psymval,
1857 Arm_address address,
1860 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1861 Valtype* wv = reinterpret_cast<Valtype*>(view);
1862 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1863 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1865 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1866 return This::STATUS_OKAY;
1869 // R_ARM_THM_CALL: (S + A) | T - P
1870 static inline typename This::Status
1871 thm_call(unsigned char *view,
1872 const Sized_relobj<32, big_endian>* object,
1873 const Symbol_value<32>* psymval,
1874 Arm_address address,
1877 // A thumb call consists of two instructions.
1878 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1879 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1880 Valtype* wv = reinterpret_cast<Valtype*>(view);
1881 Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
1882 Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
1883 // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1884 gold_assert((lo & 0xf800) == 0xf800);
1885 Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
1886 | ((lo & 0x7ff) << 1));
1887 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1890 // If target has no thumb bit set, we need to either turn the BL
1891 // into a BLX (for ARMv5 or above) or generate a stub.
1894 // This only works for ARMv5 and above with interworking enabled.
1897 hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
1898 lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
1899 elfcpp::Swap<16, big_endian>::writeval(wv, hi);
1900 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
1901 return (utils::has_overflow<23>(x)
1902 ? This::STATUS_OVERFLOW
1903 : This::STATUS_OKAY);
1906 // R_ARM_BASE_PREL: B(S) + A - P
1907 static inline typename This::Status
1908 base_prel(unsigned char* view,
1910 Arm_address address)
1912 Base::rel32(view, origin - address);
1916 // R_ARM_BASE_ABS: B(S) + A
1917 static inline typename This::Status
1918 base_abs(unsigned char* view,
1921 Base::rel32(view, origin);
1925 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1926 static inline typename This::Status
1927 got_brel(unsigned char* view,
1928 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1930 Base::rel32(view, got_offset);
1931 return This::STATUS_OKAY;
1934 // R_ARM_GOT_PREL: GOT(S) + A – P
1935 static inline typename This::Status
1936 got_prel(unsigned char* view,
1937 typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
1938 Arm_address address)
1940 Base::rel32(view, got_offset - address);
1941 return This::STATUS_OKAY;
1944 // R_ARM_PLT32: (S + A) | T - P
1945 static inline typename This::Status
1946 plt32(unsigned char *view,
1947 const Sized_relobj<32, big_endian>* object,
1948 const Symbol_value<32>* psymval,
1949 Arm_address address,
1952 return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
1953 address, has_thumb_bit);
1956 // R_ARM_CALL: (S + A) | T - P
1957 static inline typename This::Status
1958 call(unsigned char *view,
1959 const Sized_relobj<32, big_endian>* object,
1960 const Symbol_value<32>* psymval,
1961 Arm_address address,
1964 return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
1965 address, has_thumb_bit);
1968 // R_ARM_JUMP24: (S + A) | T - P
1969 static inline typename This::Status
1970 jump24(unsigned char *view,
1971 const Sized_relobj<32, big_endian>* object,
1972 const Symbol_value<32>* psymval,
1973 Arm_address address,
1976 return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
1977 address, has_thumb_bit);
1980 // R_ARM_PREL: (S + A) | T - P
1981 static inline typename This::Status
1982 prel31(unsigned char *view,
1983 const Sized_relobj<32, big_endian>* object,
1984 const Symbol_value<32>* psymval,
1985 Arm_address address,
1988 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1989 Valtype* wv = reinterpret_cast<Valtype*>(view);
1990 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1991 Valtype addend = utils::sign_extend<31>(val);
1992 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1994 val = utils::bit_select(val, x, 0x7fffffffU);
1995 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1996 return (utils::has_overflow<31>(x) ?
1997 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2000 // R_ARM_MOVW_ABS_NC: (S + A) | T
2001 static inline typename This::Status
2002 movw_abs_nc(unsigned char *view,
2003 const Sized_relobj<32, big_endian>* object,
2004 const Symbol_value<32>* psymval,
2007 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2008 Valtype* wv = reinterpret_cast<Valtype*>(view);
2009 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2010 Valtype addend = This::extract_arm_movw_movt_addend(val);
2011 Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
2012 val = This::insert_val_arm_movw_movt(val, x);
2013 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2014 return This::STATUS_OKAY;
2017 // R_ARM_MOVT_ABS: S + A
2018 static inline typename This::Status
2019 movt_abs(unsigned char *view,
2020 const Sized_relobj<32, big_endian>* object,
2021 const Symbol_value<32>* psymval)
2023 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2024 Valtype* wv = reinterpret_cast<Valtype*>(view);
2025 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2026 Valtype addend = This::extract_arm_movw_movt_addend(val);
2027 Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
2028 val = This::insert_val_arm_movw_movt(val, x);
2029 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2030 return This::STATUS_OKAY;
2033 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2034 static inline typename This::Status
2035 thm_movw_abs_nc(unsigned char *view,
2036 const Sized_relobj<32, big_endian>* object,
2037 const Symbol_value<32>* psymval,
2040 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2041 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2042 Valtype* wv = reinterpret_cast<Valtype*>(view);
2043 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2044 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2045 Reltype addend = extract_thumb_movw_movt_addend(val);
2046 Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
2047 val = This::insert_val_thumb_movw_movt(val, x);
2048 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2049 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2050 return This::STATUS_OKAY;
2053 // R_ARM_THM_MOVT_ABS: S + A
2054 static inline typename This::Status
2055 thm_movt_abs(unsigned char *view,
2056 const Sized_relobj<32, big_endian>* object,
2057 const Symbol_value<32>* psymval)
2059 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2060 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2061 Valtype* wv = reinterpret_cast<Valtype*>(view);
2062 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2063 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2064 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2065 Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
2066 val = This::insert_val_thumb_movw_movt(val, x);
2067 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2068 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2069 return This::STATUS_OKAY;
2072 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2073 static inline typename This::Status
2074 movw_prel_nc(unsigned char *view,
2075 const Sized_relobj<32, big_endian>* object,
2076 const Symbol_value<32>* psymval,
2077 Arm_address address,
2080 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2081 Valtype* wv = reinterpret_cast<Valtype*>(view);
2082 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2083 Valtype addend = This::extract_arm_movw_movt_addend(val);
2084 Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
2086 val = This::insert_val_arm_movw_movt(val, x);
2087 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2088 return This::STATUS_OKAY;
2091 // R_ARM_MOVT_PREL: S + A - P
2092 static inline typename This::Status
2093 movt_prel(unsigned char *view,
2094 const Sized_relobj<32, big_endian>* object,
2095 const Symbol_value<32>* psymval,
2096 Arm_address address)
2098 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2099 Valtype* wv = reinterpret_cast<Valtype*>(view);
2100 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2101 Valtype addend = This::extract_arm_movw_movt_addend(val);
2102 Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
2104 val = This::insert_val_arm_movw_movt(val, x);
2105 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2106 return This::STATUS_OKAY;
2109 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2110 static inline typename This::Status
2111 thm_movw_prel_nc(unsigned char *view,
2112 const Sized_relobj<32, big_endian>* object,
2113 const Symbol_value<32>* psymval,
2114 Arm_address address,
2117 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2118 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2119 Valtype* wv = reinterpret_cast<Valtype*>(view);
2120 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2121 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2122 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2123 Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
2125 val = This::insert_val_thumb_movw_movt(val, x);
2126 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2127 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2128 return This::STATUS_OKAY;
2131 // R_ARM_THM_MOVT_PREL: S + A - P
2132 static inline typename This::Status
2133 thm_movt_prel(unsigned char *view,
2134 const Sized_relobj<32, big_endian>* object,
2135 const Symbol_value<32>* psymval,
2136 Arm_address address)
2138 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2139 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2140 Valtype* wv = reinterpret_cast<Valtype*>(view);
2141 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2142 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2143 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2144 Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
2146 val = This::insert_val_thumb_movw_movt(val, x);
2147 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2148 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2149 return This::STATUS_OKAY;
2153 // Get the GOT section, creating it if necessary.
2155 template<bool big_endian>
2156 Output_data_got<32, big_endian>*
2157 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2159 if (this->got_ == NULL)
2161 gold_assert(symtab != NULL && layout != NULL);
2163 this->got_ = new Output_data_got<32, big_endian>();
2166 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2168 | elfcpp::SHF_WRITE),
2172 // The old GNU linker creates a .got.plt section. We just
2173 // create another set of data in the .got section. Note that we
2174 // always create a PLT if we create a GOT, although the PLT
2176 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2177 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2179 | elfcpp::SHF_WRITE),
2180 this->got_plt_, false);
2183 // The first three entries are reserved.
2184 this->got_plt_->set_current_data_size(3 * 4);
2186 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2187 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2189 0, 0, elfcpp::STT_OBJECT,
2191 elfcpp::STV_HIDDEN, 0,
2197 // Get the dynamic reloc section, creating it if necessary.
2199 template<bool big_endian>
2200 typename Target_arm<big_endian>::Reloc_section*
2201 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2203 if (this->rel_dyn_ == NULL)
2205 gold_assert(layout != NULL);
2206 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2207 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
2208 elfcpp::SHF_ALLOC, this->rel_dyn_, true);
2210 return this->rel_dyn_;
2213 // Insn_template methods.
2215 // Return byte size of an instruction template.
2218 Insn_template::size() const
2220 switch (this->type())
2233 // Return alignment of an instruction template.
2236 Insn_template::alignment() const
2238 switch (this->type())
2251 // Stub_template methods.
2253 Stub_template::Stub_template(
2254 Stub_type type, const Insn_template* insns,
2256 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2257 entry_in_thumb_mode_(false), relocs_()
2261 // Compute byte size and alignment of stub template.
2262 for (size_t i = 0; i < insn_count; i++)
2264 unsigned insn_alignment = insns[i].alignment();
2265 size_t insn_size = insns[i].size();
2266 gold_assert((offset & (insn_alignment - 1)) == 0);
2267 this->alignment_ = std::max(this->alignment_, insn_alignment);
2268 switch (insns[i].type())
2270 case Insn_template::THUMB16_TYPE:
2272 this->entry_in_thumb_mode_ = true;
2275 case Insn_template::THUMB32_TYPE:
2276 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2277 this->relocs_.push_back(Reloc(i, offset));
2279 this->entry_in_thumb_mode_ = true;
2282 case Insn_template::ARM_TYPE:
2283 // Handle cases where the target is encoded within the
2285 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2286 this->relocs_.push_back(Reloc(i, offset));
2289 case Insn_template::DATA_TYPE:
2290 // Entry point cannot be data.
2291 gold_assert(i != 0);
2292 this->relocs_.push_back(Reloc(i, offset));
2298 offset += insn_size;
2300 this->size_ = offset;
2303 // Reloc_stub::Key methods.
2305 // Dump a Key as a string for debugging.
2308 Reloc_stub::Key::name() const
2310 if (this->r_sym_ == invalid_index)
2312 // Global symbol key name
2313 // <stub-type>:<symbol name>:<addend>.
2314 const std::string sym_name = this->u_.symbol->name();
2315 // We need to print two hex number and two colons. So just add 100 bytes
2316 // to the symbol name size.
2317 size_t len = sym_name.size() + 100;
2318 char* buffer = new char[len];
2319 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2320 sym_name.c_str(), this->addend_);
2321 gold_assert(c > 0 && c < static_cast<int>(len));
2323 return std::string(buffer);
2327 // local symbol key name
2328 // <stub-type>:<object>:<r_sym>:<addend>.
2329 const size_t len = 200;
2331 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2332 this->u_.relobj, this->r_sym_, this->addend_);
2333 gold_assert(c > 0 && c < static_cast<int>(len));
2334 return std::string(buffer);
2338 // Reloc_stub methods.
2340 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2341 // LOCATION to DESTINATION.
2342 // This code is based on the arm_type_of_stub function in
2343 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2347 Reloc_stub::stub_type_for_reloc(
2348 unsigned int r_type,
2349 Arm_address location,
2350 Arm_address destination,
2351 bool target_is_thumb)
2353 Stub_type stub_type = arm_stub_none;
2355 // This is a bit ugly but we want to avoid using a templated class for
2356 // big and little endianities.
2358 bool should_force_pic_veneer;
2361 if (parameters->target().is_big_endian())
2363 const Target_arm<true>& big_endian_target =
2364 Target_arm<true>::default_target();
2365 may_use_blx = big_endian_target.may_use_blx();
2366 should_force_pic_veneer = big_endian_target.should_force_pic_veneer();
2367 thumb2 = big_endian_target.using_thumb2();
2368 thumb_only = big_endian_target.using_thumb_only();
2372 const Target_arm<false>& little_endian_target =
2373 Target_arm<false>::default_target();
2374 may_use_blx = little_endian_target.may_use_blx();
2375 should_force_pic_veneer = little_endian_target.should_force_pic_veneer();
2376 thumb2 = little_endian_target.using_thumb2();
2377 thumb_only = little_endian_target.using_thumb_only();
2380 int64_t branch_offset = (int64_t)destination - location;
2382 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2384 // Handle cases where:
2385 // - this call goes too far (different Thumb/Thumb2 max
2387 // - it's a Thumb->Arm call and blx is not available, or it's a
2388 // Thumb->Arm branch (not bl). A stub is needed in this case.
2390 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2391 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2393 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2394 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2395 || ((!target_is_thumb)
2396 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2397 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
2399 if (target_is_thumb)
2404 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2407 && (r_type == elfcpp::R_ARM_THM_CALL))
2408 // V5T and above. Stub starts with ARM code, so
2409 // we must be able to switch mode before
2410 // reaching it, which is only possible for 'bl'
2411 // (ie R_ARM_THM_CALL relocation).
2412 ? arm_stub_long_branch_any_thumb_pic
2413 // On V4T, use Thumb code only.
2414 : arm_stub_long_branch_v4t_thumb_thumb_pic)
2418 && (r_type == elfcpp::R_ARM_THM_CALL))
2419 ? arm_stub_long_branch_any_any // V5T and above.
2420 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
2424 stub_type = (parameters->options().shared() | should_force_pic_veneer)
2425 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
2426 : arm_stub_long_branch_thumb_only; // non-PIC stub.
2433 // FIXME: We should check that the input section is from an
2434 // object that has interwork enabled.
2436 stub_type = (parameters->options().shared()
2437 || should_force_pic_veneer)
2440 && (r_type == elfcpp::R_ARM_THM_CALL))
2441 ? arm_stub_long_branch_any_arm_pic // V5T and above.
2442 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
2446 && (r_type == elfcpp::R_ARM_THM_CALL))
2447 ? arm_stub_long_branch_any_any // V5T and above.
2448 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
2450 // Handle v4t short branches.
2451 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
2452 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
2453 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
2454 stub_type = arm_stub_short_branch_v4t_thumb_arm;
2458 else if (r_type == elfcpp::R_ARM_CALL
2459 || r_type == elfcpp::R_ARM_JUMP24
2460 || r_type == elfcpp::R_ARM_PLT32)
2462 if (target_is_thumb)
2466 // FIXME: We should check that the input section is from an
2467 // object that has interwork enabled.
2469 // We have an extra 2-bytes reach because of
2470 // the mode change (bit 24 (H) of BLX encoding).
2471 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
2472 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2473 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
2474 || (r_type == elfcpp::R_ARM_JUMP24)
2475 || (r_type == elfcpp::R_ARM_PLT32))
2477 stub_type = (parameters->options().shared()
2478 || should_force_pic_veneer)
2481 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
2482 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
2486 ? arm_stub_long_branch_any_any // V5T and above.
2487 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
2493 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
2494 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
2496 stub_type = (parameters->options().shared()
2497 || should_force_pic_veneer)
2498 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
2499 : arm_stub_long_branch_any_any; /// non-PIC.
2507 // Template to implement do_write for a specific target endianity.
2509 template<bool big_endian>
2511 Reloc_stub::do_fixed_endian_write(unsigned char* view,
2512 section_size_type view_size)
2514 const Stub_template* stub_template = this->stub_template();
2515 const Insn_template* insns = stub_template->insns();
2517 // FIXME: We do not handle BE8 encoding yet.
2518 unsigned char* pov = view;
2519 for (size_t i = 0; i < stub_template->insn_count(); i++)
2521 switch (insns[i].type())
2523 case Insn_template::THUMB16_TYPE:
2524 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2525 gold_assert(insns[i].reloc_addend() == 0);
2526 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2528 case Insn_template::THUMB32_TYPE:
2530 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2531 uint32_t lo = insns[i].data() & 0xffff;
2532 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2533 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2536 case Insn_template::ARM_TYPE:
2537 case Insn_template::DATA_TYPE:
2538 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2543 pov += insns[i].size();
2545 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2548 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2551 Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2555 this->do_fixed_endian_write<true>(view, view_size);
2557 this->do_fixed_endian_write<false>(view, view_size);
2560 // Stub_factory methods.
2562 Stub_factory::Stub_factory()
2564 // The instruction template sequences are declared as static
2565 // objects and initialized first time the constructor runs.
2567 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2568 // to reach the stub if necessary.
2569 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2571 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2572 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2573 // dcd R_ARM_ABS32(X)
2576 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2578 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2580 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2581 Insn_template::arm_insn(0xe12fff1c), // bx ip
2582 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2583 // dcd R_ARM_ABS32(X)
2586 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2587 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2589 Insn_template::thumb16_insn(0xb401), // push {r0}
2590 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2591 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2592 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2593 Insn_template::thumb16_insn(0x4760), // bx ip
2594 Insn_template::thumb16_insn(0xbf00), // nop
2595 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2596 // dcd R_ARM_ABS32(X)
2599 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2601 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2603 Insn_template::thumb16_insn(0x4778), // bx pc
2604 Insn_template::thumb16_insn(0x46c0), // nop
2605 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2606 Insn_template::arm_insn(0xe12fff1c), // bx ip
2607 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2608 // dcd R_ARM_ABS32(X)
2611 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2613 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2615 Insn_template::thumb16_insn(0x4778), // bx pc
2616 Insn_template::thumb16_insn(0x46c0), // nop
2617 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2618 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2619 // dcd R_ARM_ABS32(X)
2622 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2623 // one, when the destination is close enough.
2624 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2626 Insn_template::thumb16_insn(0x4778), // bx pc
2627 Insn_template::thumb16_insn(0x46c0), // nop
2628 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2631 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2632 // blx to reach the stub if necessary.
2633 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2635 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2636 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2637 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2638 // dcd R_ARM_REL32(X-4)
2641 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2642 // blx to reach the stub if necessary. We can not add into pc;
2643 // it is not guaranteed to mode switch (different in ARMv6 and
2645 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2647 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2648 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2649 Insn_template::arm_insn(0xe12fff1c), // bx ip
2650 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2651 // dcd R_ARM_REL32(X)
2654 // V4T ARM -> ARM long branch stub, PIC.
2655 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2657 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2658 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2659 Insn_template::arm_insn(0xe12fff1c), // bx ip
2660 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2661 // dcd R_ARM_REL32(X)
2664 // V4T Thumb -> ARM long branch stub, PIC.
2665 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2667 Insn_template::thumb16_insn(0x4778), // bx pc
2668 Insn_template::thumb16_insn(0x46c0), // nop
2669 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2670 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
2671 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2672 // dcd R_ARM_REL32(X)
2675 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2677 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
2679 Insn_template::thumb16_insn(0xb401), // push {r0}
2680 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2681 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
2682 Insn_template::thumb16_insn(0x4484), // add ip, r0
2683 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2684 Insn_template::thumb16_insn(0x4760), // bx ip
2685 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
2686 // dcd R_ARM_REL32(X)
2689 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2691 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2693 Insn_template::thumb16_insn(0x4778), // bx pc
2694 Insn_template::thumb16_insn(0x46c0), // nop
2695 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
2696 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2697 Insn_template::arm_insn(0xe12fff1c), // bx ip
2698 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2699 // dcd R_ARM_REL32(X)
2702 // Cortex-A8 erratum-workaround stubs.
2704 // Stub used for conditional branches (which may be beyond +/-1MB away,
2705 // so we can't use a conditional branch to reach this stub).
2712 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
2714 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
2715 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
2716 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
2720 // Stub used for b.w and bl.w instructions.
2722 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
2724 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2727 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
2729 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
2732 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
2733 // instruction (which switches to ARM mode) to point to this stub. Jump to
2734 // the real destination using an ARM-mode branch.
2735 const Insn_template elf32_arm_stub_a8_veneer_blx[] =
2737 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
2740 // Fill in the stub template look-up table. Stub templates are constructed
2741 // per instance of Stub_factory for fast look-up without locking
2742 // in a thread-enabled environment.
2744 this->stub_templates_[arm_stub_none] =
2745 new Stub_template(arm_stub_none, NULL, 0);
2747 #define DEF_STUB(x) \
2751 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2752 Stub_type type = arm_stub_##x; \
2753 this->stub_templates_[type] = \
2754 new Stub_template(type, elf32_arm_stub_##x, array_size); \
2762 // Stub_table methods.
2764 // Add a STUB with using KEY. Caller is reponsible for avoid adding
2765 // if already a STUB with the same key has been added.
2767 template<bool big_endian>
2769 Stub_table<big_endian>::add_reloc_stub(
2771 const Reloc_stub::Key& key)
2773 const Stub_template* stub_template = stub->stub_template();
2774 gold_assert(stub_template->type() == key.stub_type());
2775 this->reloc_stubs_[key] = stub;
2776 if (this->addralign_ < stub_template->alignment())
2777 this->addralign_ = stub_template->alignment();
2778 this->has_been_changed_ = true;
2781 template<bool big_endian>
2783 Stub_table<big_endian>::relocate_stubs(
2784 const Relocate_info<32, big_endian>* relinfo,
2785 Target_arm<big_endian>* arm_target,
2786 Output_section* output_section,
2787 unsigned char* view,
2788 Arm_address address,
2789 section_size_type view_size)
2791 // If we are passed a view bigger than the stub table's. we need to
2793 gold_assert(address == this->address()
2795 == static_cast<section_size_type>(this->data_size())));
2797 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2798 p != this->reloc_stubs_.end();
2801 Reloc_stub* stub = p->second;
2802 const Stub_template* stub_template = stub->stub_template();
2803 if (stub_template->reloc_count() != 0)
2805 // Adjust view to cover the stub only.
2806 section_size_type offset = stub->offset();
2807 section_size_type stub_size = stub_template->size();
2808 gold_assert(offset + stub_size <= view_size);
2810 arm_target->relocate_stub(stub, relinfo, output_section,
2811 view + offset, address + offset,
2817 // Reset address and file offset.
2819 template<bool big_endian>
2821 Stub_table<big_endian>::do_reset_address_and_file_offset()
2824 uint64_t max_addralign = 1;
2825 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2826 p != this->reloc_stubs_.end();
2829 Reloc_stub* stub = p->second;
2830 const Stub_template* stub_template = stub->stub_template();
2831 uint64_t stub_addralign = stub_template->alignment();
2832 max_addralign = std::max(max_addralign, stub_addralign);
2833 off = align_address(off, stub_addralign);
2834 stub->set_offset(off);
2835 stub->reset_destination_address();
2836 off += stub_template->size();
2839 this->addralign_ = max_addralign;
2840 this->set_current_data_size_for_child(off);
2843 // Write out the stubs to file.
2845 template<bool big_endian>
2847 Stub_table<big_endian>::do_write(Output_file* of)
2849 off_t offset = this->offset();
2850 const section_size_type oview_size =
2851 convert_to_section_size_type(this->data_size());
2852 unsigned char* const oview = of->get_output_view(offset, oview_size);
2854 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2855 p != this->reloc_stubs_.end();
2858 Reloc_stub* stub = p->second;
2859 Arm_address address = this->address() + stub->offset();
2861 == align_address(address,
2862 stub->stub_template()->alignment()));
2863 stub->write(oview + stub->offset(), stub->stub_template()->size(),
2866 of->write_output_view(this->offset(), oview_size, oview);
2869 // Arm_input_section methods.
2871 // Initialize an Arm_input_section.
2873 template<bool big_endian>
2875 Arm_input_section<big_endian>::init()
2877 Relobj* relobj = this->relobj();
2878 unsigned int shndx = this->shndx();
2880 // Cache these to speed up size and alignment queries. It is too slow
2881 // to call section_addraglin and section_size every time.
2882 this->original_addralign_ = relobj->section_addralign(shndx);
2883 this->original_size_ = relobj->section_size(shndx);
2885 // We want to make this look like the original input section after
2886 // output sections are finalized.
2887 Output_section* os = relobj->output_section(shndx);
2888 off_t offset = relobj->output_section_offset(shndx);
2889 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2890 this->set_address(os->address() + offset);
2891 this->set_file_offset(os->offset() + offset);
2893 this->set_current_data_size(this->original_size_);
2894 this->finalize_data_size();
2897 template<bool big_endian>
2899 Arm_input_section<big_endian>::do_write(Output_file* of)
2901 // We have to write out the original section content.
2902 section_size_type section_size;
2903 const unsigned char* section_contents =
2904 this->relobj()->section_contents(this->shndx(), §ion_size, false);
2905 of->write(this->offset(), section_contents, section_size);
2907 // If this owns a stub table and it is not empty, write it.
2908 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2909 this->stub_table_->write(of);
2912 // Finalize data size.
2914 template<bool big_endian>
2916 Arm_input_section<big_endian>::set_final_data_size()
2918 // If this owns a stub table, finalize its data size as well.
2919 if (this->is_stub_table_owner())
2921 uint64_t address = this->address();
2923 // The stub table comes after the original section contents.
2924 address += this->original_size_;
2925 address = align_address(address, this->stub_table_->addralign());
2926 off_t offset = this->offset() + (address - this->address());
2927 this->stub_table_->set_address_and_file_offset(address, offset);
2928 address += this->stub_table_->data_size();
2929 gold_assert(address == this->address() + this->current_data_size());
2932 this->set_data_size(this->current_data_size());
2935 // Reset address and file offset.
2937 template<bool big_endian>
2939 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
2941 // Size of the original input section contents.
2942 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2944 // If this is a stub table owner, account for the stub table size.
2945 if (this->is_stub_table_owner())
2947 Stub_table<big_endian>* stub_table = this->stub_table_;
2949 // Reset the stub table's address and file offset. The
2950 // current data size for child will be updated after that.
2951 stub_table_->reset_address_and_file_offset();
2952 off = align_address(off, stub_table_->addralign());
2953 off += stub_table->current_data_size();
2956 this->set_current_data_size(off);
2959 // Arm_output_section methods.
2961 // Create a stub group for input sections from BEGIN to END. OWNER
2962 // points to the input section to be the owner a new stub table.
2964 template<bool big_endian>
2966 Arm_output_section<big_endian>::create_stub_group(
2967 Input_section_list::const_iterator begin,
2968 Input_section_list::const_iterator end,
2969 Input_section_list::const_iterator owner,
2970 Target_arm<big_endian>* target,
2971 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
2973 // Currently we convert ordinary input sections into relaxed sections only
2974 // at this point but we may want to support creating relaxed input section
2975 // very early. So we check here to see if owner is already a relaxed
2978 Arm_input_section<big_endian>* arm_input_section;
2979 if (owner->is_relaxed_input_section())
2982 Arm_input_section<big_endian>::as_arm_input_section(
2983 owner->relaxed_input_section());
2987 gold_assert(owner->is_input_section());
2988 // Create a new relaxed input section.
2990 target->new_arm_input_section(owner->relobj(), owner->shndx());
2991 new_relaxed_sections->push_back(arm_input_section);
2994 // Create a stub table.
2995 Stub_table<big_endian>* stub_table =
2996 target->new_stub_table(arm_input_section);
2998 arm_input_section->set_stub_table(stub_table);
3000 Input_section_list::const_iterator p = begin;
3001 Input_section_list::const_iterator prev_p;
3003 // Look for input sections or relaxed input sections in [begin ... end].
3006 if (p->is_input_section() || p->is_relaxed_input_section())
3008 // The stub table information for input sections live
3009 // in their objects.
3010 Arm_relobj<big_endian>* arm_relobj =
3011 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
3012 arm_relobj->set_stub_table(p->shndx(), stub_table);
3016 while (prev_p != end);
3019 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3020 // of stub groups. We grow a stub group by adding input section until the
3021 // size is just below GROUP_SIZE. The last input section will be converted
3022 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3023 // input section after the stub table, effectively double the group size.
3025 // This is similar to the group_sections() function in elf32-arm.c but is
3026 // implemented differently.
3028 template<bool big_endian>
3030 Arm_output_section<big_endian>::group_sections(
3031 section_size_type group_size,
3032 bool stubs_always_after_branch,
3033 Target_arm<big_endian>* target)
3035 // We only care about sections containing code.
3036 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
3039 // States for grouping.
3042 // No group is being built.
3044 // A group is being built but the stub table is not found yet.
3045 // We keep group a stub group until the size is just under GROUP_SIZE.
3046 // The last input section in the group will be used as the stub table.
3047 FINDING_STUB_SECTION,
3048 // A group is being built and we have already found a stub table.
3049 // We enter this state to grow a stub group by adding input section
3050 // after the stub table. This effectively doubles the group size.
3054 // Any newly created relaxed sections are stored here.
3055 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
3057 State state = NO_GROUP;
3058 section_size_type off = 0;
3059 section_size_type group_begin_offset = 0;
3060 section_size_type group_end_offset = 0;
3061 section_size_type stub_table_end_offset = 0;
3062 Input_section_list::const_iterator group_begin =
3063 this->input_sections().end();
3064 Input_section_list::const_iterator stub_table =
3065 this->input_sections().end();
3066 Input_section_list::const_iterator group_end = this->input_sections().end();
3067 for (Input_section_list::const_iterator p = this->input_sections().begin();
3068 p != this->input_sections().end();
3071 section_size_type section_begin_offset =
3072 align_address(off, p->addralign());
3073 section_size_type section_end_offset =
3074 section_begin_offset + p->data_size();
3076 // Check to see if we should group the previously seens sections.
3082 case FINDING_STUB_SECTION:
3083 // Adding this section makes the group larger than GROUP_SIZE.
3084 if (section_end_offset - group_begin_offset >= group_size)
3086 if (stubs_always_after_branch)
3088 gold_assert(group_end != this->input_sections().end());
3089 this->create_stub_group(group_begin, group_end, group_end,
3090 target, &new_relaxed_sections);
3095 // But wait, there's more! Input sections up to
3096 // stub_group_size bytes after the stub table can be
3097 // handled by it too.
3098 state = HAS_STUB_SECTION;
3099 stub_table = group_end;
3100 stub_table_end_offset = group_end_offset;
3105 case HAS_STUB_SECTION:
3106 // Adding this section makes the post stub-section group larger
3108 if (section_end_offset - stub_table_end_offset >= group_size)
3110 gold_assert(group_end != this->input_sections().end());
3111 this->create_stub_group(group_begin, group_end, stub_table,
3112 target, &new_relaxed_sections);
3121 // If we see an input section and currently there is no group, start
3122 // a new one. Skip any empty sections.
3123 if ((p->is_input_section() || p->is_relaxed_input_section())
3124 && (p->relobj()->section_size(p->shndx()) != 0))
3126 if (state == NO_GROUP)
3128 state = FINDING_STUB_SECTION;
3130 group_begin_offset = section_begin_offset;
3133 // Keep track of the last input section seen.
3135 group_end_offset = section_end_offset;
3138 off = section_end_offset;
3141 // Create a stub group for any ungrouped sections.
3142 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
3144 gold_assert(group_end != this->input_sections().end());
3145 this->create_stub_group(group_begin, group_end,
3146 (state == FINDING_STUB_SECTION
3149 target, &new_relaxed_sections);
3152 // Convert input section into relaxed input section in a batch.
3153 if (!new_relaxed_sections.empty())
3154 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
3156 // Update the section offsets
3157 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
3159 Arm_relobj<big_endian>* arm_relobj =
3160 Arm_relobj<big_endian>::as_arm_relobj(
3161 new_relaxed_sections[i]->relobj());
3162 unsigned int shndx = new_relaxed_sections[i]->shndx();
3163 // Tell Arm_relobj that this input section is converted.
3164 arm_relobj->convert_input_section_to_relaxed_section(shndx);
3168 // Arm_relobj methods.
3170 // Scan relocations for stub generation.
3172 template<bool big_endian>
3174 Arm_relobj<big_endian>::scan_sections_for_stubs(
3175 Target_arm<big_endian>* arm_target,
3176 const Symbol_table* symtab,
3177 const Layout* layout)
3179 unsigned int shnum = this->shnum();
3180 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
3182 // Read the section headers.
3183 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
3187 // To speed up processing, we set up hash tables for fast lookup of
3188 // input offsets to output addresses.
3189 this->initialize_input_to_output_maps();
3191 const Relobj::Output_sections& out_sections(this->output_sections());
3193 Relocate_info<32, big_endian> relinfo;
3194 relinfo.symtab = symtab;
3195 relinfo.layout = layout;
3196 relinfo.object = this;
3198 const unsigned char* p = pshdrs + shdr_size;
3199 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
3201 typename elfcpp::Shdr<32, big_endian> shdr(p);
3203 unsigned int sh_type = shdr.get_sh_type();
3204 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
3207 off_t sh_size = shdr.get_sh_size();
3211 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
3212 if (index >= this->shnum())
3214 // Ignore reloc section with bad info. This error will be
3215 // reported in the final link.
3219 Output_section* os = out_sections[index];
3222 // This relocation section is against a section which we
3226 Arm_address output_offset = this->get_output_section_offset(index);
3228 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
3230 // Ignore reloc section with unexpected symbol table. The
3231 // error will be reported in the final link.
3235 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
3236 sh_size, true, false);
3238 unsigned int reloc_size;
3239 if (sh_type == elfcpp::SHT_REL)
3240 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
3242 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
3244 if (reloc_size != shdr.get_sh_entsize())
3246 // Ignore reloc section with unexpected entsize. The error
3247 // will be reported in the final link.
3251 size_t reloc_count = sh_size / reloc_size;
3252 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
3254 // Ignore reloc section with uneven size. The error will be
3255 // reported in the final link.
3259 gold_assert(output_offset != invalid_address
3260 || this->relocs_must_follow_section_writes());
3262 // Get the section contents. This does work for the case in which
3263 // we modify the contents of an input section. We need to pass the
3264 // output view under such circumstances.
3265 section_size_type input_view_size = 0;
3266 const unsigned char* input_view =
3267 this->section_contents(index, &input_view_size, false);
3269 relinfo.reloc_shndx = i;
3270 relinfo.data_shndx = index;
3271 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
3273 output_offset == invalid_address,
3279 // After we've done the relocations, we release the hash tables,
3280 // since we no longer need them.
3281 this->free_input_to_output_maps();
3284 // Count the local symbols. The ARM backend needs to know if a symbol
3285 // is a THUMB function or not. For global symbols, it is easy because
3286 // the Symbol object keeps the ELF symbol type. For local symbol it is
3287 // harder because we cannot access this information. So we override the
3288 // do_count_local_symbol in parent and scan local symbols to mark
3289 // THUMB functions. This is not the most efficient way but I do not want to
3290 // slow down other ports by calling a per symbol targer hook inside
3291 // Sized_relobj<size, big_endian>::do_count_local_symbols.
3293 template<bool big_endian>
3295 Arm_relobj<big_endian>::do_count_local_symbols(
3296 Stringpool_template<char>* pool,
3297 Stringpool_template<char>* dynpool)
3299 // We need to fix-up the values of any local symbols whose type are
3302 // Ask parent to count the local symbols.
3303 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
3304 const unsigned int loccount = this->local_symbol_count();
3308 // Intialize the thumb function bit-vector.
3309 std::vector<bool> empty_vector(loccount, false);
3310 this->local_symbol_is_thumb_function_.swap(empty_vector);
3312 // Read the symbol table section header.
3313 const unsigned int symtab_shndx = this->symtab_shndx();
3314 elfcpp::Shdr<32, big_endian>
3315 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
3316 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
3318 // Read the local symbols.
3319 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
3320 gold_assert(loccount == symtabshdr.get_sh_info());
3321 off_t locsize = loccount * sym_size;
3322 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
3323 locsize, true, true);
3325 // Loop over the local symbols and mark any local symbols pointing
3326 // to THUMB functions.
3328 // Skip the first dummy symbol.
3330 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
3331 this->local_values();
3332 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
3334 elfcpp::Sym<32, big_endian> sym(psyms);
3335 elfcpp::STT st_type = sym.get_st_type();
3336 Symbol_value<32>& lv((*plocal_values)[i]);
3337 Arm_address input_value = lv.input_value();
3339 if (st_type == elfcpp::STT_ARM_TFUNC
3340 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
3342 // This is a THUMB function. Mark this and canonicalize the
3343 // symbol value by setting LSB.
3344 this->local_symbol_is_thumb_function_[i] = true;
3345 if ((input_value & 1) == 0)
3346 lv.set_input_value(input_value | 1);
3351 // Relocate sections.
3352 template<bool big_endian>
3354 Arm_relobj<big_endian>::do_relocate_sections(
3355 const General_options& options,
3356 const Symbol_table* symtab,
3357 const Layout* layout,
3358 const unsigned char* pshdrs,
3359 typename Sized_relobj<32, big_endian>::Views* pviews)
3361 // Call parent to relocate sections.
3362 Sized_relobj<32, big_endian>::do_relocate_sections(options, symtab, layout,
3365 // We do not generate stubs if doing a relocatable link.
3366 if (parameters->options().relocatable())
3369 // Relocate stub tables.
3370 unsigned int shnum = this->shnum();
3372 Target_arm<big_endian>* arm_target =
3373 Target_arm<big_endian>::default_target();
3375 Relocate_info<32, big_endian> relinfo;
3376 relinfo.options = &options;
3377 relinfo.symtab = symtab;
3378 relinfo.layout = layout;
3379 relinfo.object = this;
3381 for (unsigned int i = 1; i < shnum; ++i)
3383 Arm_input_section<big_endian>* arm_input_section =
3384 arm_target->find_arm_input_section(this, i);
3386 if (arm_input_section == NULL
3387 || !arm_input_section->is_stub_table_owner()
3388 || arm_input_section->stub_table()->empty())
3391 // We cannot discard a section if it owns a stub table.
3392 Output_section* os = this->output_section(i);
3393 gold_assert(os != NULL);
3395 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
3396 relinfo.reloc_shdr = NULL;
3397 relinfo.data_shndx = i;
3398 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
3400 gold_assert((*pviews)[i].view != NULL);
3402 // We are passed the output section view. Adjust it to cover the
3404 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
3405 gold_assert((stub_table->address() >= (*pviews)[i].address)
3406 && ((stub_table->address() + stub_table->data_size())
3407 <= (*pviews)[i].address + (*pviews)[i].view_size));
3409 off_t offset = stub_table->address() - (*pviews)[i].address;
3410 unsigned char* view = (*pviews)[i].view + offset;
3411 Arm_address address = stub_table->address();
3412 section_size_type view_size = stub_table->data_size();
3414 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
3419 // Read the symbol information.
3421 template<bool big_endian>
3423 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3425 // Call parent class to read symbol information.
3426 Sized_relobj<32, big_endian>::do_read_symbols(sd);
3428 // Read processor-specific flags in ELF file header.
3429 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3430 elfcpp::Elf_sizes<32>::ehdr_size,
3432 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3433 this->processor_specific_flags_ = ehdr.get_e_flags();
3436 // Arm_dynobj methods.
3438 // Read the symbol information.
3440 template<bool big_endian>
3442 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3444 // Call parent class to read symbol information.
3445 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
3447 // Read processor-specific flags in ELF file header.
3448 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3449 elfcpp::Elf_sizes<32>::ehdr_size,
3451 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3452 this->processor_specific_flags_ = ehdr.get_e_flags();
3455 // Stub_addend_reader methods.
3457 // Read the addend of a REL relocation of type R_TYPE at VIEW.
3459 template<bool big_endian>
3460 elfcpp::Elf_types<32>::Elf_Swxword
3461 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
3462 unsigned int r_type,
3463 const unsigned char* view,
3464 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
3468 case elfcpp::R_ARM_CALL:
3469 case elfcpp::R_ARM_JUMP24:
3470 case elfcpp::R_ARM_PLT32:
3472 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3473 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3474 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3475 return utils::sign_extend<26>(val << 2);
3478 case elfcpp::R_ARM_THM_CALL:
3479 case elfcpp::R_ARM_THM_JUMP24:
3480 case elfcpp::R_ARM_THM_XPC22:
3482 // Fetch the addend. We use the Thumb-2 encoding (backwards
3483 // compatible with Thumb-1) involving the J1 and J2 bits.
3484 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3485 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3486 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3487 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3489 uint32_t s = (upper_insn & (1 << 10)) >> 10;
3490 uint32_t upper = upper_insn & 0x3ff;
3491 uint32_t lower = lower_insn & 0x7ff;
3492 uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
3493 uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
3494 uint32_t i1 = j1 ^ s ? 0 : 1;
3495 uint32_t i2 = j2 ^ s ? 0 : 1;
3497 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
3498 | (upper << 12) | (lower << 1));
3501 case elfcpp::R_ARM_THM_JUMP19:
3503 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3504 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3505 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3506 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3508 // Reconstruct the top three bits and squish the two 11 bit pieces
3510 uint32_t S = (upper_insn & 0x0400) >> 10;
3511 uint32_t J1 = (lower_insn & 0x2000) >> 13;
3512 uint32_t J2 = (lower_insn & 0x0800) >> 11;
3514 (S << 8) | (J2 << 7) | (J1 << 6) | (upper_insn & 0x003f);
3515 uint32_t lower = (lower_insn & 0x07ff);
3516 return utils::sign_extend<23>((upper << 12) | (lower << 1));
3524 // A class to handle the PLT data.
3526 template<bool big_endian>
3527 class Output_data_plt_arm : public Output_section_data
3530 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
3533 Output_data_plt_arm(Layout*, Output_data_space*);
3535 // Add an entry to the PLT.
3537 add_entry(Symbol* gsym);
3539 // Return the .rel.plt section data.
3540 const Reloc_section*
3542 { return this->rel_; }
3546 do_adjust_output_section(Output_section* os);
3548 // Write to a map file.
3550 do_print_to_mapfile(Mapfile* mapfile) const
3551 { mapfile->print_output_data(this, _("** PLT")); }
3554 // Template for the first PLT entry.
3555 static const uint32_t first_plt_entry[5];
3557 // Template for subsequent PLT entries.
3558 static const uint32_t plt_entry[3];
3560 // Set the final size.
3562 set_final_data_size()
3564 this->set_data_size(sizeof(first_plt_entry)
3565 + this->count_ * sizeof(plt_entry));
3568 // Write out the PLT data.
3570 do_write(Output_file*);
3572 // The reloc section.
3573 Reloc_section* rel_;
3574 // The .got.plt section.
3575 Output_data_space* got_plt_;
3576 // The number of PLT entries.
3577 unsigned int count_;
3580 // Create the PLT section. The ordinary .got section is an argument,
3581 // since we need to refer to the start. We also create our own .got
3582 // section just for PLT entries.
3584 template<bool big_endian>
3585 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
3586 Output_data_space* got_plt)
3587 : Output_section_data(4), got_plt_(got_plt), count_(0)
3589 this->rel_ = new Reloc_section(false);
3590 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
3591 elfcpp::SHF_ALLOC, this->rel_, true);
3594 template<bool big_endian>
3596 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
3601 // Add an entry to the PLT.
3603 template<bool big_endian>
3605 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
3607 gold_assert(!gsym->has_plt_offset());
3609 // Note that when setting the PLT offset we skip the initial
3610 // reserved PLT entry.
3611 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
3612 + sizeof(first_plt_entry));
3616 section_offset_type got_offset = this->got_plt_->current_data_size();
3618 // Every PLT entry needs a GOT entry which points back to the PLT
3619 // entry (this will be changed by the dynamic linker, normally
3620 // lazily when the function is called).
3621 this->got_plt_->set_current_data_size(got_offset + 4);
3623 // Every PLT entry needs a reloc.
3624 gsym->set_needs_dynsym_entry();
3625 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
3628 // Note that we don't need to save the symbol. The contents of the
3629 // PLT are independent of which symbols are used. The symbols only
3630 // appear in the relocations.
3634 // FIXME: This is not very flexible. Right now this has only been tested
3635 // on armv5te. If we are to support additional architecture features like
3636 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3638 // The first entry in the PLT.
3639 template<bool big_endian>
3640 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
3642 0xe52de004, // str lr, [sp, #-4]!
3643 0xe59fe004, // ldr lr, [pc, #4]
3644 0xe08fe00e, // add lr, pc, lr
3645 0xe5bef008, // ldr pc, [lr, #8]!
3646 0x00000000, // &GOT[0] - .
3649 // Subsequent entries in the PLT.
3651 template<bool big_endian>
3652 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
3654 0xe28fc600, // add ip, pc, #0xNN00000
3655 0xe28cca00, // add ip, ip, #0xNN000
3656 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
3659 // Write out the PLT. This uses the hand-coded instructions above,
3660 // and adjusts them as needed. This is all specified by the arm ELF
3661 // Processor Supplement.
3663 template<bool big_endian>
3665 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
3667 const off_t offset = this->offset();
3668 const section_size_type oview_size =
3669 convert_to_section_size_type(this->data_size());
3670 unsigned char* const oview = of->get_output_view(offset, oview_size);
3672 const off_t got_file_offset = this->got_plt_->offset();
3673 const section_size_type got_size =
3674 convert_to_section_size_type(this->got_plt_->data_size());
3675 unsigned char* const got_view = of->get_output_view(got_file_offset,
3677 unsigned char* pov = oview;
3679 Arm_address plt_address = this->address();
3680 Arm_address got_address = this->got_plt_->address();
3682 // Write first PLT entry. All but the last word are constants.
3683 const size_t num_first_plt_words = (sizeof(first_plt_entry)
3684 / sizeof(plt_entry[0]));
3685 for (size_t i = 0; i < num_first_plt_words - 1; i++)
3686 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
3687 // Last word in first PLT entry is &GOT[0] - .
3688 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
3689 got_address - (plt_address + 16));
3690 pov += sizeof(first_plt_entry);
3692 unsigned char* got_pov = got_view;
3694 memset(got_pov, 0, 12);
3697 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
3698 unsigned int plt_offset = sizeof(first_plt_entry);
3699 unsigned int plt_rel_offset = 0;
3700 unsigned int got_offset = 12;
3701 const unsigned int count = this->count_;
3702 for (unsigned int i = 0;
3705 pov += sizeof(plt_entry),
3707 plt_offset += sizeof(plt_entry),
3708 plt_rel_offset += rel_size,
3711 // Set and adjust the PLT entry itself.
3712 int32_t offset = ((got_address + got_offset)
3713 - (plt_address + plt_offset + 8));
3715 gold_assert(offset >= 0 && offset < 0x0fffffff);
3716 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
3717 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
3718 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
3719 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
3720 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
3721 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
3723 // Set the entry in the GOT.
3724 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
3727 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
3728 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
3730 of->write_output_view(offset, oview_size, oview);
3731 of->write_output_view(got_file_offset, got_size, got_view);
3734 // Create a PLT entry for a global symbol.
3736 template<bool big_endian>
3738 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
3741 if (gsym->has_plt_offset())
3744 if (this->plt_ == NULL)
3746 // Create the GOT sections first.
3747 this->got_section(symtab, layout);
3749 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
3750 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
3752 | elfcpp::SHF_EXECINSTR),
3755 this->plt_->add_entry(gsym);
3758 // Report an unsupported relocation against a local symbol.
3760 template<bool big_endian>
3762 Target_arm<big_endian>::Scan::unsupported_reloc_local(
3763 Sized_relobj<32, big_endian>* object,
3764 unsigned int r_type)
3766 gold_error(_("%s: unsupported reloc %u against local symbol"),
3767 object->name().c_str(), r_type);
3770 // We are about to emit a dynamic relocation of type R_TYPE. If the
3771 // dynamic linker does not support it, issue an error. The GNU linker
3772 // only issues a non-PIC error for an allocated read-only section.
3773 // Here we know the section is allocated, but we don't know that it is
3774 // read-only. But we check for all the relocation types which the
3775 // glibc dynamic linker supports, so it seems appropriate to issue an
3776 // error even if the section is not read-only.
3778 template<bool big_endian>
3780 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
3781 unsigned int r_type)
3785 // These are the relocation types supported by glibc for ARM.
3786 case elfcpp::R_ARM_RELATIVE:
3787 case elfcpp::R_ARM_COPY:
3788 case elfcpp::R_ARM_GLOB_DAT:
3789 case elfcpp::R_ARM_JUMP_SLOT:
3790 case elfcpp::R_ARM_ABS32:
3791 case elfcpp::R_ARM_ABS32_NOI:
3792 case elfcpp::R_ARM_PC24:
3793 // FIXME: The following 3 types are not supported by Android's dynamic
3795 case elfcpp::R_ARM_TLS_DTPMOD32:
3796 case elfcpp::R_ARM_TLS_DTPOFF32:
3797 case elfcpp::R_ARM_TLS_TPOFF32:
3801 // This prevents us from issuing more than one error per reloc
3802 // section. But we can still wind up issuing more than one
3803 // error per object file.
3804 if (this->issued_non_pic_error_)
3806 object->error(_("requires unsupported dynamic reloc; "
3807 "recompile with -fPIC"));
3808 this->issued_non_pic_error_ = true;
3811 case elfcpp::R_ARM_NONE:
3816 // Scan a relocation for a local symbol.
3817 // FIXME: This only handles a subset of relocation types used by Android
3818 // on ARM v5te devices.
3820 template<bool big_endian>
3822 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
3825 Sized_relobj<32, big_endian>* object,
3826 unsigned int data_shndx,
3827 Output_section* output_section,
3828 const elfcpp::Rel<32, big_endian>& reloc,
3829 unsigned int r_type,
3830 const elfcpp::Sym<32, big_endian>&)
3832 r_type = get_real_reloc_type(r_type);
3835 case elfcpp::R_ARM_NONE:
3838 case elfcpp::R_ARM_ABS32:
3839 case elfcpp::R_ARM_ABS32_NOI:
3840 // If building a shared library (or a position-independent
3841 // executable), we need to create a dynamic relocation for
3842 // this location. The relocation applied at link time will
3843 // apply the link-time value, so we flag the location with
3844 // an R_ARM_RELATIVE relocation so the dynamic loader can
3845 // relocate it easily.
3846 if (parameters->options().output_is_position_independent())
3848 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3849 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3850 // If we are to add more other reloc types than R_ARM_ABS32,
3851 // we need to add check_non_pic(object, r_type) here.
3852 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
3853 output_section, data_shndx,
3854 reloc.get_r_offset());
3858 case elfcpp::R_ARM_REL32:
3859 case elfcpp::R_ARM_THM_CALL:
3860 case elfcpp::R_ARM_CALL:
3861 case elfcpp::R_ARM_PREL31:
3862 case elfcpp::R_ARM_JUMP24:
3863 case elfcpp::R_ARM_PLT32:
3864 case elfcpp::R_ARM_THM_ABS5:
3865 case elfcpp::R_ARM_ABS8:
3866 case elfcpp::R_ARM_ABS12:
3867 case elfcpp::R_ARM_ABS16:
3868 case elfcpp::R_ARM_BASE_ABS:
3869 case elfcpp::R_ARM_MOVW_ABS_NC:
3870 case elfcpp::R_ARM_MOVT_ABS:
3871 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3872 case elfcpp::R_ARM_THM_MOVT_ABS:
3873 case elfcpp::R_ARM_MOVW_PREL_NC:
3874 case elfcpp::R_ARM_MOVT_PREL:
3875 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3876 case elfcpp::R_ARM_THM_MOVT_PREL:
3879 case elfcpp::R_ARM_GOTOFF32:
3880 // We need a GOT section:
3881 target->got_section(symtab, layout);
3884 case elfcpp::R_ARM_BASE_PREL:
3885 // FIXME: What about this?
3888 case elfcpp::R_ARM_GOT_BREL:
3889 case elfcpp::R_ARM_GOT_PREL:
3891 // The symbol requires a GOT entry.
3892 Output_data_got<32, big_endian>* got =
3893 target->got_section(symtab, layout);
3894 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3895 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
3897 // If we are generating a shared object, we need to add a
3898 // dynamic RELATIVE relocation for this symbol's GOT entry.
3899 if (parameters->options().output_is_position_independent())
3901 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3902 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3903 rel_dyn->add_local_relative(
3904 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
3905 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
3911 case elfcpp::R_ARM_TARGET1:
3912 // This should have been mapped to another type already.
3914 case elfcpp::R_ARM_COPY:
3915 case elfcpp::R_ARM_GLOB_DAT:
3916 case elfcpp::R_ARM_JUMP_SLOT:
3917 case elfcpp::R_ARM_RELATIVE:
3918 // These are relocations which should only be seen by the
3919 // dynamic linker, and should never be seen here.
3920 gold_error(_("%s: unexpected reloc %u in object file"),
3921 object->name().c_str(), r_type);
3925 unsupported_reloc_local(object, r_type);
3930 // Report an unsupported relocation against a global symbol.
3932 template<bool big_endian>
3934 Target_arm<big_endian>::Scan::unsupported_reloc_global(
3935 Sized_relobj<32, big_endian>* object,
3936 unsigned int r_type,
3939 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3940 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3943 // Scan a relocation for a global symbol.
3944 // FIXME: This only handles a subset of relocation types used by Android
3945 // on ARM v5te devices.
3947 template<bool big_endian>
3949 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
3952 Sized_relobj<32, big_endian>* object,
3953 unsigned int data_shndx,
3954 Output_section* output_section,
3955 const elfcpp::Rel<32, big_endian>& reloc,
3956 unsigned int r_type,
3959 r_type = get_real_reloc_type(r_type);
3962 case elfcpp::R_ARM_NONE:
3965 case elfcpp::R_ARM_ABS32:
3966 case elfcpp::R_ARM_ABS32_NOI:
3968 // Make a dynamic relocation if necessary.
3969 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
3971 if (target->may_need_copy_reloc(gsym))
3973 target->copy_reloc(symtab, layout, object,
3974 data_shndx, output_section, gsym, reloc);
3976 else if (gsym->can_use_relative_reloc(false))
3978 // If we are to add more other reloc types than R_ARM_ABS32,
3979 // we need to add check_non_pic(object, r_type) here.
3980 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3981 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
3982 output_section, object,
3983 data_shndx, reloc.get_r_offset());
3987 // If we are to add more other reloc types than R_ARM_ABS32,
3988 // we need to add check_non_pic(object, r_type) here.
3989 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3990 rel_dyn->add_global(gsym, r_type, output_section, object,
3991 data_shndx, reloc.get_r_offset());
3997 case elfcpp::R_ARM_MOVW_ABS_NC:
3998 case elfcpp::R_ARM_MOVT_ABS:
3999 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4000 case elfcpp::R_ARM_THM_MOVT_ABS:
4001 case elfcpp::R_ARM_MOVW_PREL_NC:
4002 case elfcpp::R_ARM_MOVT_PREL:
4003 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4004 case elfcpp::R_ARM_THM_MOVT_PREL:
4007 case elfcpp::R_ARM_THM_ABS5:
4008 case elfcpp::R_ARM_ABS8:
4009 case elfcpp::R_ARM_ABS12:
4010 case elfcpp::R_ARM_ABS16:
4011 case elfcpp::R_ARM_BASE_ABS:
4013 // No dynamic relocs of this kinds.
4014 // Report the error in case of PIC.
4015 int flags = Symbol::NON_PIC_REF;
4016 if (gsym->type() == elfcpp::STT_FUNC
4017 || gsym->type() == elfcpp::STT_ARM_TFUNC)
4018 flags |= Symbol::FUNCTION_CALL;
4019 if (gsym->needs_dynamic_reloc(flags))
4020 check_non_pic(object, r_type);
4024 case elfcpp::R_ARM_REL32:
4025 case elfcpp::R_ARM_PREL31:
4027 // Make a dynamic relocation if necessary.
4028 int flags = Symbol::NON_PIC_REF;
4029 if (gsym->needs_dynamic_reloc(flags))
4031 if (target->may_need_copy_reloc(gsym))
4033 target->copy_reloc(symtab, layout, object,
4034 data_shndx, output_section, gsym, reloc);
4038 check_non_pic(object, r_type);
4039 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4040 rel_dyn->add_global(gsym, r_type, output_section, object,
4041 data_shndx, reloc.get_r_offset());
4047 case elfcpp::R_ARM_JUMP24:
4048 case elfcpp::R_ARM_THM_CALL:
4049 case elfcpp::R_ARM_CALL:
4051 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
4052 target->make_plt_entry(symtab, layout, gsym);
4053 // Make a dynamic relocation if necessary.
4054 int flags = Symbol::NON_PIC_REF;
4055 if (gsym->type() == elfcpp::STT_FUNC
4056 || gsym->type() == elfcpp::STT_ARM_TFUNC)
4057 flags |= Symbol::FUNCTION_CALL;
4058 if (gsym->needs_dynamic_reloc(flags))
4060 if (target->may_need_copy_reloc(gsym))
4062 target->copy_reloc(symtab, layout, object,
4063 data_shndx, output_section, gsym,
4068 check_non_pic(object, r_type);
4069 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4070 rel_dyn->add_global(gsym, r_type, output_section, object,
4071 data_shndx, reloc.get_r_offset());
4077 case elfcpp::R_ARM_PLT32:
4078 // If the symbol is fully resolved, this is just a relative
4079 // local reloc. Otherwise we need a PLT entry.
4080 if (gsym->final_value_is_known())
4082 // If building a shared library, we can also skip the PLT entry
4083 // if the symbol is defined in the output file and is protected
4085 if (gsym->is_defined()
4086 && !gsym->is_from_dynobj()
4087 && !gsym->is_preemptible())
4089 target->make_plt_entry(symtab, layout, gsym);
4092 case elfcpp::R_ARM_GOTOFF32:
4093 // We need a GOT section.
4094 target->got_section(symtab, layout);
4097 case elfcpp::R_ARM_BASE_PREL:
4098 // FIXME: What about this?
4101 case elfcpp::R_ARM_GOT_BREL:
4102 case elfcpp::R_ARM_GOT_PREL:
4104 // The symbol requires a GOT entry.
4105 Output_data_got<32, big_endian>* got =
4106 target->got_section(symtab, layout);
4107 if (gsym->final_value_is_known())
4108 got->add_global(gsym, GOT_TYPE_STANDARD);
4111 // If this symbol is not fully resolved, we need to add a
4112 // GOT entry with a dynamic relocation.
4113 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4114 if (gsym->is_from_dynobj()
4115 || gsym->is_undefined()
4116 || gsym->is_preemptible())
4117 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
4118 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
4121 if (got->add_global(gsym, GOT_TYPE_STANDARD))
4122 rel_dyn->add_global_relative(
4123 gsym, elfcpp::R_ARM_RELATIVE, got,
4124 gsym->got_offset(GOT_TYPE_STANDARD));
4130 case elfcpp::R_ARM_TARGET1:
4131 // This should have been mapped to another type already.
4133 case elfcpp::R_ARM_COPY:
4134 case elfcpp::R_ARM_GLOB_DAT:
4135 case elfcpp::R_ARM_JUMP_SLOT:
4136 case elfcpp::R_ARM_RELATIVE:
4137 // These are relocations which should only be seen by the
4138 // dynamic linker, and should never be seen here.
4139 gold_error(_("%s: unexpected reloc %u in object file"),
4140 object->name().c_str(), r_type);
4144 unsupported_reloc_global(object, r_type, gsym);
4149 // Process relocations for gc.
4151 template<bool big_endian>
4153 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
4155 Sized_relobj<32, big_endian>* object,
4156 unsigned int data_shndx,
4158 const unsigned char* prelocs,
4160 Output_section* output_section,
4161 bool needs_special_offset_handling,
4162 size_t local_symbol_count,
4163 const unsigned char* plocal_symbols)
4165 typedef Target_arm<big_endian> Arm;
4166 typedef typename Target_arm<big_endian>::Scan Scan;
4168 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4177 needs_special_offset_handling,
4182 // Scan relocations for a section.
4184 template<bool big_endian>
4186 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
4188 Sized_relobj<32, big_endian>* object,
4189 unsigned int data_shndx,
4190 unsigned int sh_type,
4191 const unsigned char* prelocs,
4193 Output_section* output_section,
4194 bool needs_special_offset_handling,
4195 size_t local_symbol_count,
4196 const unsigned char* plocal_symbols)
4198 typedef typename Target_arm<big_endian>::Scan Scan;
4199 if (sh_type == elfcpp::SHT_RELA)
4201 gold_error(_("%s: unsupported RELA reloc section"),
4202 object->name().c_str());
4206 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4215 needs_special_offset_handling,
4220 // Finalize the sections.
4222 template<bool big_endian>
4224 Target_arm<big_endian>::do_finalize_sections(
4226 const Input_objects* input_objects)
4228 // Merge processor-specific flags.
4229 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4230 p != input_objects->relobj_end();
4233 Arm_relobj<big_endian>* arm_relobj =
4234 Arm_relobj<big_endian>::as_arm_relobj(*p);
4235 this->merge_processor_specific_flags(
4237 arm_relobj->processor_specific_flags());
4240 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
4241 p != input_objects->dynobj_end();
4244 Arm_dynobj<big_endian>* arm_dynobj =
4245 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
4246 this->merge_processor_specific_flags(
4248 arm_dynobj->processor_specific_flags());
4251 // Fill in some more dynamic tags.
4252 Output_data_dynamic* const odyn = layout->dynamic_data();
4255 if (this->got_plt_ != NULL
4256 && this->got_plt_->output_section() != NULL)
4257 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
4259 if (this->plt_ != NULL
4260 && this->plt_->output_section() != NULL)
4262 const Output_data* od = this->plt_->rel_plt();
4263 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
4264 odyn->add_section_address(elfcpp::DT_JMPREL, od);
4265 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
4268 if (this->rel_dyn_ != NULL
4269 && this->rel_dyn_->output_section() != NULL)
4271 const Output_data* od = this->rel_dyn_;
4272 odyn->add_section_address(elfcpp::DT_REL, od);
4273 odyn->add_section_size(elfcpp::DT_RELSZ, od);
4274 odyn->add_constant(elfcpp::DT_RELENT,
4275 elfcpp::Elf_sizes<32>::rel_size);
4278 if (!parameters->options().shared())
4280 // The value of the DT_DEBUG tag is filled in by the dynamic
4281 // linker at run time, and used by the debugger.
4282 odyn->add_constant(elfcpp::DT_DEBUG, 0);
4286 // Emit any relocs we saved in an attempt to avoid generating COPY
4288 if (this->copy_relocs_.any_saved_relocs())
4289 this->copy_relocs_.emit(this->rel_dyn_section(layout));
4291 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4292 // the .ARM.exidx section.
4293 if (!layout->script_options()->saw_phdrs_clause()
4294 && !parameters->options().relocatable())
4296 Output_section* exidx_section =
4297 layout->find_output_section(".ARM.exidx");
4299 if (exidx_section != NULL
4300 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
4302 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
4304 Output_segment* exidx_segment =
4305 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
4306 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
4312 // Return whether a direct absolute static relocation needs to be applied.
4313 // In cases where Scan::local() or Scan::global() has created
4314 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4315 // of the relocation is carried in the data, and we must not
4316 // apply the static relocation.
4318 template<bool big_endian>
4320 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
4321 const Sized_symbol<32>* gsym,
4324 Output_section* output_section)
4326 // If the output section is not allocated, then we didn't call
4327 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4329 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
4332 // For local symbols, we will have created a non-RELATIVE dynamic
4333 // relocation only if (a) the output is position independent,
4334 // (b) the relocation is absolute (not pc- or segment-relative), and
4335 // (c) the relocation is not 32 bits wide.
4337 return !(parameters->options().output_is_position_independent()
4338 && (ref_flags & Symbol::ABSOLUTE_REF)
4341 // For global symbols, we use the same helper routines used in the
4342 // scan pass. If we did not create a dynamic relocation, or if we
4343 // created a RELATIVE dynamic relocation, we should apply the static
4345 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
4346 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
4347 && gsym->can_use_relative_reloc(ref_flags
4348 & Symbol::FUNCTION_CALL);
4349 return !has_dyn || is_rel;
4352 // Perform a relocation.
4354 template<bool big_endian>
4356 Target_arm<big_endian>::Relocate::relocate(
4357 const Relocate_info<32, big_endian>* relinfo,
4359 Output_section *output_section,
4361 const elfcpp::Rel<32, big_endian>& rel,
4362 unsigned int r_type,
4363 const Sized_symbol<32>* gsym,
4364 const Symbol_value<32>* psymval,
4365 unsigned char* view,
4366 Arm_address address,
4367 section_size_type /* view_size */ )
4369 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
4371 r_type = get_real_reloc_type(r_type);
4373 // If this the symbol may be a Thumb function, set thumb bit to 1.
4374 bool has_thumb_bit = ((gsym != NULL)
4375 && (gsym->type() == elfcpp::STT_FUNC
4376 || gsym->type() == elfcpp::STT_ARM_TFUNC));
4378 // Pick the value to use for symbols defined in shared objects.
4379 Symbol_value<32> symval;
4381 && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
4383 symval.set_output_value(target->plt_section()->address()
4384 + gsym->plt_offset());
4389 const Sized_relobj<32, big_endian>* object = relinfo->object;
4391 // Get the GOT offset if needed.
4392 // The GOT pointer points to the end of the GOT section.
4393 // We need to subtract the size of the GOT section to get
4394 // the actual offset to use in the relocation.
4395 bool have_got_offset = false;
4396 unsigned int got_offset = 0;
4399 case elfcpp::R_ARM_GOT_BREL:
4400 case elfcpp::R_ARM_GOT_PREL:
4403 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4404 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4405 - target->got_size());
4409 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
4410 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4411 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4412 - target->got_size());
4414 have_got_offset = true;
4421 typename Arm_relocate_functions::Status reloc_status =
4422 Arm_relocate_functions::STATUS_OKAY;
4425 case elfcpp::R_ARM_NONE:
4428 case elfcpp::R_ARM_ABS8:
4429 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4431 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
4434 case elfcpp::R_ARM_ABS12:
4435 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4437 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
4440 case elfcpp::R_ARM_ABS16:
4441 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4443 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
4446 case elfcpp::R_ARM_ABS32:
4447 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4449 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4453 case elfcpp::R_ARM_ABS32_NOI:
4454 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4456 // No thumb bit for this relocation: (S + A)
4457 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4461 case elfcpp::R_ARM_MOVW_ABS_NC:
4462 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4464 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
4468 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4469 "a shared object; recompile with -fPIC"));
4472 case elfcpp::R_ARM_MOVT_ABS:
4473 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4475 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
4477 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4478 "a shared object; recompile with -fPIC"));
4481 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4482 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4484 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
4488 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4489 "making a shared object; recompile with -fPIC"));
4492 case elfcpp::R_ARM_THM_MOVT_ABS:
4493 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4495 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
4498 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4499 "making a shared object; recompile with -fPIC"));
4502 case elfcpp::R_ARM_MOVW_PREL_NC:
4503 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
4508 case elfcpp::R_ARM_MOVT_PREL:
4509 reloc_status = Arm_relocate_functions::movt_prel(view, object,
4513 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4514 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
4519 case elfcpp::R_ARM_THM_MOVT_PREL:
4520 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
4524 case elfcpp::R_ARM_REL32:
4525 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4526 address, has_thumb_bit);
4529 case elfcpp::R_ARM_THM_ABS5:
4530 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4532 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
4535 case elfcpp::R_ARM_THM_CALL:
4536 reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
4537 address, has_thumb_bit);
4540 case elfcpp::R_ARM_GOTOFF32:
4542 Arm_address got_origin;
4543 got_origin = target->got_plt_section()->address();
4544 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4545 got_origin, has_thumb_bit);
4549 case elfcpp::R_ARM_BASE_PREL:
4552 // Get the addressing origin of the output segment defining the
4553 // symbol gsym (AAELF 4.6.1.2 Relocation types)
4554 gold_assert(gsym != NULL);
4555 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4556 origin = gsym->output_segment()->vaddr();
4557 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4558 origin = gsym->output_data()->address();
4561 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4562 _("cannot find origin of R_ARM_BASE_PREL"));
4565 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
4569 case elfcpp::R_ARM_BASE_ABS:
4571 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4576 // Get the addressing origin of the output segment defining
4577 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
4579 // R_ARM_BASE_ABS with the NULL symbol will give the
4580 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
4581 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
4582 origin = target->got_plt_section()->address();
4583 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4584 origin = gsym->output_segment()->vaddr();
4585 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4586 origin = gsym->output_data()->address();
4589 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4590 _("cannot find origin of R_ARM_BASE_ABS"));
4594 reloc_status = Arm_relocate_functions::base_abs(view, origin);
4598 case elfcpp::R_ARM_GOT_BREL:
4599 gold_assert(have_got_offset);
4600 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
4603 case elfcpp::R_ARM_GOT_PREL:
4604 gold_assert(have_got_offset);
4605 // Get the address origin for GOT PLT, which is allocated right
4606 // after the GOT section, to calculate an absolute address of
4607 // the symbol GOT entry (got_origin + got_offset).
4608 Arm_address got_origin;
4609 got_origin = target->got_plt_section()->address();
4610 reloc_status = Arm_relocate_functions::got_prel(view,
4611 got_origin + got_offset,
4615 case elfcpp::R_ARM_PLT32:
4616 gold_assert(gsym == NULL
4617 || gsym->has_plt_offset()
4618 || gsym->final_value_is_known()
4619 || (gsym->is_defined()
4620 && !gsym->is_from_dynobj()
4621 && !gsym->is_preemptible()));
4622 reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
4623 address, has_thumb_bit);
4626 case elfcpp::R_ARM_CALL:
4627 reloc_status = Arm_relocate_functions::call(view, object, psymval,
4628 address, has_thumb_bit);
4631 case elfcpp::R_ARM_JUMP24:
4632 reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
4633 address, has_thumb_bit);
4636 case elfcpp::R_ARM_PREL31:
4637 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
4638 address, has_thumb_bit);
4641 case elfcpp::R_ARM_TARGET1:
4642 // This should have been mapped to another type already.
4644 case elfcpp::R_ARM_COPY:
4645 case elfcpp::R_ARM_GLOB_DAT:
4646 case elfcpp::R_ARM_JUMP_SLOT:
4647 case elfcpp::R_ARM_RELATIVE:
4648 // These are relocations which should only be seen by the
4649 // dynamic linker, and should never be seen here.
4650 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4651 _("unexpected reloc %u in object file"),
4656 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4657 _("unsupported reloc %u"),
4662 // Report any errors.
4663 switch (reloc_status)
4665 case Arm_relocate_functions::STATUS_OKAY:
4667 case Arm_relocate_functions::STATUS_OVERFLOW:
4668 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4669 _("relocation overflow in relocation %u"),
4672 case Arm_relocate_functions::STATUS_BAD_RELOC:
4673 gold_error_at_location(
4677 _("unexpected opcode while processing relocation %u"),
4687 // Relocate section data.
4689 template<bool big_endian>
4691 Target_arm<big_endian>::relocate_section(
4692 const Relocate_info<32, big_endian>* relinfo,
4693 unsigned int sh_type,
4694 const unsigned char* prelocs,
4696 Output_section* output_section,
4697 bool needs_special_offset_handling,
4698 unsigned char* view,
4699 Arm_address address,
4700 section_size_type view_size,
4701 const Reloc_symbol_changes* reloc_symbol_changes)
4703 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
4704 gold_assert(sh_type == elfcpp::SHT_REL);
4706 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
4713 needs_special_offset_handling,
4717 reloc_symbol_changes);
4720 // Return the size of a relocation while scanning during a relocatable
4723 template<bool big_endian>
4725 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
4726 unsigned int r_type,
4729 r_type = get_real_reloc_type(r_type);
4732 case elfcpp::R_ARM_NONE:
4735 case elfcpp::R_ARM_ABS8:
4738 case elfcpp::R_ARM_ABS16:
4739 case elfcpp::R_ARM_THM_ABS5:
4742 case elfcpp::R_ARM_ABS32:
4743 case elfcpp::R_ARM_ABS32_NOI:
4744 case elfcpp::R_ARM_ABS12:
4745 case elfcpp::R_ARM_BASE_ABS:
4746 case elfcpp::R_ARM_REL32:
4747 case elfcpp::R_ARM_THM_CALL:
4748 case elfcpp::R_ARM_GOTOFF32:
4749 case elfcpp::R_ARM_BASE_PREL:
4750 case elfcpp::R_ARM_GOT_BREL:
4751 case elfcpp::R_ARM_GOT_PREL:
4752 case elfcpp::R_ARM_PLT32:
4753 case elfcpp::R_ARM_CALL:
4754 case elfcpp::R_ARM_JUMP24:
4755 case elfcpp::R_ARM_PREL31:
4756 case elfcpp::R_ARM_MOVW_ABS_NC:
4757 case elfcpp::R_ARM_MOVT_ABS:
4758 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4759 case elfcpp::R_ARM_THM_MOVT_ABS:
4760 case elfcpp::R_ARM_MOVW_PREL_NC:
4761 case elfcpp::R_ARM_MOVT_PREL:
4762 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4763 case elfcpp::R_ARM_THM_MOVT_PREL:
4766 case elfcpp::R_ARM_TARGET1:
4767 // This should have been mapped to another type already.
4769 case elfcpp::R_ARM_COPY:
4770 case elfcpp::R_ARM_GLOB_DAT:
4771 case elfcpp::R_ARM_JUMP_SLOT:
4772 case elfcpp::R_ARM_RELATIVE:
4773 // These are relocations which should only be seen by the
4774 // dynamic linker, and should never be seen here.
4775 gold_error(_("%s: unexpected reloc %u in object file"),
4776 object->name().c_str(), r_type);
4780 object->error(_("unsupported reloc %u in object file"), r_type);
4785 // Scan the relocs during a relocatable link.
4787 template<bool big_endian>
4789 Target_arm<big_endian>::scan_relocatable_relocs(
4790 Symbol_table* symtab,
4792 Sized_relobj<32, big_endian>* object,
4793 unsigned int data_shndx,
4794 unsigned int sh_type,
4795 const unsigned char* prelocs,
4797 Output_section* output_section,
4798 bool needs_special_offset_handling,
4799 size_t local_symbol_count,
4800 const unsigned char* plocal_symbols,
4801 Relocatable_relocs* rr)
4803 gold_assert(sh_type == elfcpp::SHT_REL);
4805 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
4806 Relocatable_size_for_reloc> Scan_relocatable_relocs;
4808 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
4809 Scan_relocatable_relocs>(
4817 needs_special_offset_handling,
4823 // Relocate a section during a relocatable link.
4825 template<bool big_endian>
4827 Target_arm<big_endian>::relocate_for_relocatable(
4828 const Relocate_info<32, big_endian>* relinfo,
4829 unsigned int sh_type,
4830 const unsigned char* prelocs,
4832 Output_section* output_section,
4833 off_t offset_in_output_section,
4834 const Relocatable_relocs* rr,
4835 unsigned char* view,
4836 Arm_address view_address,
4837 section_size_type view_size,
4838 unsigned char* reloc_view,
4839 section_size_type reloc_view_size)
4841 gold_assert(sh_type == elfcpp::SHT_REL);
4843 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
4848 offset_in_output_section,
4857 // Return the value to use for a dynamic symbol which requires special
4858 // treatment. This is how we support equality comparisons of function
4859 // pointers across shared library boundaries, as described in the
4860 // processor specific ABI supplement.
4862 template<bool big_endian>
4864 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4866 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4867 return this->plt_section()->address() + gsym->plt_offset();
4870 // Map platform-specific relocs to real relocs
4872 template<bool big_endian>
4874 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4878 case elfcpp::R_ARM_TARGET1:
4879 // This is either R_ARM_ABS32 or R_ARM_REL32;
4880 return elfcpp::R_ARM_ABS32;
4882 case elfcpp::R_ARM_TARGET2:
4883 // This can be any reloc type but ususally is R_ARM_GOT_PREL
4884 return elfcpp::R_ARM_GOT_PREL;
4891 // Whether if two EABI versions V1 and V2 are compatible.
4893 template<bool big_endian>
4895 Target_arm<big_endian>::are_eabi_versions_compatible(
4896 elfcpp::Elf_Word v1,
4897 elfcpp::Elf_Word v2)
4899 // v4 and v5 are the same spec before and after it was released,
4900 // so allow mixing them.
4901 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
4902 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
4908 // Combine FLAGS from an input object called NAME and the processor-specific
4909 // flags in the ELF header of the output. Much of this is adapted from the
4910 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
4911 // in bfd/elf32-arm.c.
4913 template<bool big_endian>
4915 Target_arm<big_endian>::merge_processor_specific_flags(
4916 const std::string& name,
4917 elfcpp::Elf_Word flags)
4919 if (this->are_processor_specific_flags_set())
4921 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
4923 // Nothing to merge if flags equal to those in output.
4924 if (flags == out_flags)
4927 // Complain about various flag mismatches.
4928 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
4929 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
4930 if (!this->are_eabi_versions_compatible(version1, version2))
4931 gold_error(_("Source object %s has EABI version %d but output has "
4932 "EABI version %d."),
4934 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
4935 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
4939 // If the input is the default architecture and had the default
4940 // flags then do not bother setting the flags for the output
4941 // architecture, instead allow future merges to do this. If no
4942 // future merges ever set these flags then they will retain their
4943 // uninitialised values, which surprise surprise, correspond
4944 // to the default values.
4948 // This is the first time, just copy the flags.
4949 // We only copy the EABI version for now.
4950 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
4954 // Adjust ELF file header.
4955 template<bool big_endian>
4957 Target_arm<big_endian>::do_adjust_elf_header(
4958 unsigned char* view,
4961 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
4963 elfcpp::Ehdr<32, big_endian> ehdr(view);
4964 unsigned char e_ident[elfcpp::EI_NIDENT];
4965 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
4967 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
4968 == elfcpp::EF_ARM_EABI_UNKNOWN)
4969 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
4971 e_ident[elfcpp::EI_OSABI] = 0;
4972 e_ident[elfcpp::EI_ABIVERSION] = 0;
4974 // FIXME: Do EF_ARM_BE8 adjustment.
4976 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
4977 oehdr.put_e_ident(e_ident);
4980 // do_make_elf_object to override the same function in the base class.
4981 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
4982 // to store ARM specific information. Hence we need to have our own
4983 // ELF object creation.
4985 template<bool big_endian>
4987 Target_arm<big_endian>::do_make_elf_object(
4988 const std::string& name,
4989 Input_file* input_file,
4990 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
4992 int et = ehdr.get_e_type();
4993 if (et == elfcpp::ET_REL)
4995 Arm_relobj<big_endian>* obj =
4996 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
5000 else if (et == elfcpp::ET_DYN)
5002 Sized_dynobj<32, big_endian>* obj =
5003 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
5009 gold_error(_("%s: unsupported ELF file type %d"),
5015 // Return whether a relocation type used the LSB to distinguish THUMB
5017 template<bool big_endian>
5019 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
5023 case elfcpp::R_ARM_PC24:
5024 case elfcpp::R_ARM_ABS32:
5025 case elfcpp::R_ARM_REL32:
5026 case elfcpp::R_ARM_SBREL32:
5027 case elfcpp::R_ARM_THM_CALL:
5028 case elfcpp::R_ARM_GLOB_DAT:
5029 case elfcpp::R_ARM_JUMP_SLOT:
5030 case elfcpp::R_ARM_GOTOFF32:
5031 case elfcpp::R_ARM_PLT32:
5032 case elfcpp::R_ARM_CALL:
5033 case elfcpp::R_ARM_JUMP24:
5034 case elfcpp::R_ARM_THM_JUMP24:
5035 case elfcpp::R_ARM_SBREL31:
5036 case elfcpp::R_ARM_PREL31:
5037 case elfcpp::R_ARM_MOVW_ABS_NC:
5038 case elfcpp::R_ARM_MOVW_PREL_NC:
5039 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5040 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5041 case elfcpp::R_ARM_THM_JUMP19:
5042 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
5043 case elfcpp::R_ARM_ALU_PC_G0_NC:
5044 case elfcpp::R_ARM_ALU_PC_G0:
5045 case elfcpp::R_ARM_ALU_PC_G1_NC:
5046 case elfcpp::R_ARM_ALU_PC_G1:
5047 case elfcpp::R_ARM_ALU_PC_G2:
5048 case elfcpp::R_ARM_ALU_SB_G0_NC:
5049 case elfcpp::R_ARM_ALU_SB_G0:
5050 case elfcpp::R_ARM_ALU_SB_G1_NC:
5051 case elfcpp::R_ARM_ALU_SB_G1:
5052 case elfcpp::R_ARM_ALU_SB_G2:
5053 case elfcpp::R_ARM_MOVW_BREL_NC:
5054 case elfcpp::R_ARM_MOVW_BREL:
5055 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
5056 case elfcpp::R_ARM_THM_MOVW_BREL:
5063 // Stub-generation methods for Target_arm.
5065 // Make a new Arm_input_section object.
5067 template<bool big_endian>
5068 Arm_input_section<big_endian>*
5069 Target_arm<big_endian>::new_arm_input_section(
5073 Input_section_specifier iss(relobj, shndx);
5075 Arm_input_section<big_endian>* arm_input_section =
5076 new Arm_input_section<big_endian>(relobj, shndx);
5077 arm_input_section->init();
5079 // Register new Arm_input_section in map for look-up.
5080 std::pair<typename Arm_input_section_map::iterator, bool> ins =
5081 this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
5083 // Make sure that it we have not created another Arm_input_section
5084 // for this input section already.
5085 gold_assert(ins.second);
5087 return arm_input_section;
5090 // Find the Arm_input_section object corresponding to the SHNDX-th input
5091 // section of RELOBJ.
5093 template<bool big_endian>
5094 Arm_input_section<big_endian>*
5095 Target_arm<big_endian>::find_arm_input_section(
5097 unsigned int shndx) const
5099 Input_section_specifier iss(relobj, shndx);
5100 typename Arm_input_section_map::const_iterator p =
5101 this->arm_input_section_map_.find(iss);
5102 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
5105 // Make a new stub table.
5107 template<bool big_endian>
5108 Stub_table<big_endian>*
5109 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
5111 Stub_table<big_endian>* stub_table =
5112 new Stub_table<big_endian>(owner);
5113 this->stub_tables_.push_back(stub_table);
5115 stub_table->set_address(owner->address() + owner->data_size());
5116 stub_table->set_file_offset(owner->offset() + owner->data_size());
5117 stub_table->finalize_data_size();
5122 // The selector for arm object files.
5124 template<bool big_endian>
5125 class Target_selector_arm : public Target_selector
5128 Target_selector_arm()
5129 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
5130 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
5134 do_instantiate_target()
5135 { return new Target_arm<big_endian>(); }
5138 Target_selector_arm<false> target_selector_arm;
5139 Target_selector_arm<true> target_selector_armbe;
5141 } // End anonymous namespace.