2009-12-07 Rafael Avila de Espindola <espindola@google.com>
[binutils.git] / gold / arm.cc
blob85c4cafa0e71f23a7bf53fa2aed96e3bc74c050f
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
7 // bfd/elf32-arm.c.
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.
26 #include "gold.h"
28 #include <cstring>
29 #include <limits>
30 #include <cstdio>
31 #include <string>
32 #include <algorithm>
34 #include "elfcpp.h"
35 #include "parameters.h"
36 #include "reloc.h"
37 #include "arm.h"
38 #include "object.h"
39 #include "symtab.h"
40 #include "layout.h"
41 #include "output.h"
42 #include "copy-relocs.h"
43 #include "target.h"
44 #include "target-reloc.h"
45 #include "target-select.h"
46 #include "tls.h"
47 #include "defstd.h"
48 #include "gc.h"
50 namespace
53 using namespace gold;
55 template<bool big_endian>
56 class Output_data_plt_arm;
58 template<bool big_endian>
59 class Stub_table;
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>
68 class Arm_relobj;
70 template<bool big_endian>
71 class Target_arm;
73 // For convenience.
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
88 // are supported.
90 // R_ARM_NONE
91 // R_ARM_ABS32
92 // R_ARM_ABS32_NOI
93 // R_ARM_ABS16
94 // R_ARM_ABS12
95 // R_ARM_ABS8
96 // R_ARM_THM_ABS5
97 // R_ARM_BASE_ABS
98 // R_ARM_REL32
99 // R_ARM_THM_CALL
100 // R_ARM_COPY
101 // R_ARM_GLOB_DAT
102 // R_ARM_BASE_PREL
103 // R_ARM_JUMP_SLOT
104 // R_ARM_RELATIVE
105 // R_ARM_GOTOFF32
106 // R_ARM_GOT_BREL
107 // R_ARM_GOT_PREL
108 // R_ARM_PLT32
109 // R_ARM_CALL
110 // R_ARM_JUMP24
111 // R_ARM_TARGET1
112 // R_ARM_PREL31
113 // R_ARM_ABS8
114 // R_ARM_MOVW_ABS_NC
115 // R_ARM_MOVT_ABS
116 // R_ARM_THM_MOVW_ABS_NC
117 // R_ARM_THM_MOVT_ABS
118 // R_ARM_MOVW_PREL_NC
119 // R_ARM_MOVT_PREL
120 // R_ARM_THM_MOVW_PREL_NC
121 // R_ARM_THM_MOVT_PREL
123 // TODOs:
124 // - Support more relocation types as needed.
125 // - Make PLTs more flexible for different architecture features like
126 // Thumb-2 and BE8.
127 // There are probably a lot more.
129 // Instruction template class. This class is similar to the insn_sequence
130 // struct in bfd/elf32-arm.c.
132 class Insn_template
134 public:
135 // Types of instruction templates.
136 enum Type
138 THUMB16_TYPE = 1,
139 THUMB32_TYPE,
140 ARM_TYPE,
141 DATA_TYPE
144 // Factory methods to create instrunction templates in different formats.
146 static const Insn_template
147 thumb16_insn(uint32_t data)
148 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
150 // A bit of a hack. A Thumb conditional branch, in which the proper
151 // condition is inserted when we build the stub.
152 static const Insn_template
153 thumb16_bcond_insn(uint32_t data)
154 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 1); }
156 static const Insn_template
157 thumb32_insn(uint32_t data)
158 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
160 static const Insn_template
161 thumb32_b_insn(uint32_t data, int reloc_addend)
163 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
164 reloc_addend);
167 static const Insn_template
168 arm_insn(uint32_t data)
169 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
171 static const Insn_template
172 arm_rel_insn(unsigned data, int reloc_addend)
173 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
175 static const Insn_template
176 data_word(unsigned data, unsigned int r_type, int reloc_addend)
177 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
179 // Accessors. This class is used for read-only objects so no modifiers
180 // are provided.
182 uint32_t
183 data() const
184 { return this->data_; }
186 // Return the instruction sequence type of this.
187 Type
188 type() const
189 { return this->type_; }
191 // Return the ARM relocation type of this.
192 unsigned int
193 r_type() const
194 { return this->r_type_; }
196 int32_t
197 reloc_addend() const
198 { return this->reloc_addend_; }
200 // Return size of instrunction template in bytes.
201 size_t
202 size() const;
204 // Return byte-alignment of instrunction template.
205 unsigned
206 alignment() const;
208 private:
209 // We make the constructor private to ensure that only the factory
210 // methods are used.
211 inline
212 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
213 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
216 // Instruction specific data. This is used to store information like
217 // some of the instruction bits.
218 uint32_t data_;
219 // Instruction template type.
220 Type type_;
221 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
222 unsigned int r_type_;
223 // Relocation addend.
224 int32_t reloc_addend_;
227 // Macro for generating code to stub types. One entry per long/short
228 // branch stub
230 #define DEF_STUBS \
231 DEF_STUB(long_branch_any_any) \
232 DEF_STUB(long_branch_v4t_arm_thumb) \
233 DEF_STUB(long_branch_thumb_only) \
234 DEF_STUB(long_branch_v4t_thumb_thumb) \
235 DEF_STUB(long_branch_v4t_thumb_arm) \
236 DEF_STUB(short_branch_v4t_thumb_arm) \
237 DEF_STUB(long_branch_any_arm_pic) \
238 DEF_STUB(long_branch_any_thumb_pic) \
239 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
240 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
241 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
242 DEF_STUB(long_branch_thumb_only_pic) \
243 DEF_STUB(a8_veneer_b_cond) \
244 DEF_STUB(a8_veneer_b) \
245 DEF_STUB(a8_veneer_bl) \
246 DEF_STUB(a8_veneer_blx)
248 // Stub types.
250 #define DEF_STUB(x) arm_stub_##x,
251 typedef enum
253 arm_stub_none,
254 DEF_STUBS
256 // First reloc stub type.
257 arm_stub_reloc_first = arm_stub_long_branch_any_any,
258 // Last reloc stub type.
259 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
261 // First Cortex-A8 stub type.
262 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
263 // Last Cortex-A8 stub type.
264 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
266 // Last stub type.
267 arm_stub_type_last = arm_stub_a8_veneer_blx
268 } Stub_type;
269 #undef DEF_STUB
271 // Stub template class. Templates are meant to be read-only objects.
272 // A stub template for a stub type contains all read-only attributes
273 // common to all stubs of the same type.
275 class Stub_template
277 public:
278 Stub_template(Stub_type, const Insn_template*, size_t);
280 ~Stub_template()
283 // Return stub type.
284 Stub_type
285 type() const
286 { return this->type_; }
288 // Return an array of instruction templates.
289 const Insn_template*
290 insns() const
291 { return this->insns_; }
293 // Return size of template in number of instructions.
294 size_t
295 insn_count() const
296 { return this->insn_count_; }
298 // Return size of template in bytes.
299 size_t
300 size() const
301 { return this->size_; }
303 // Return alignment of the stub template.
304 unsigned
305 alignment() const
306 { return this->alignment_; }
308 // Return whether entry point is in thumb mode.
309 bool
310 entry_in_thumb_mode() const
311 { return this->entry_in_thumb_mode_; }
313 // Return number of relocations in this template.
314 size_t
315 reloc_count() const
316 { return this->relocs_.size(); }
318 // Return index of the I-th instruction with relocation.
319 size_t
320 reloc_insn_index(size_t i) const
322 gold_assert(i < this->relocs_.size());
323 return this->relocs_[i].first;
326 // Return the offset of the I-th instruction with relocation from the
327 // beginning of the stub.
328 section_size_type
329 reloc_offset(size_t i) const
331 gold_assert(i < this->relocs_.size());
332 return this->relocs_[i].second;
335 private:
336 // This contains information about an instruction template with a relocation
337 // and its offset from start of stub.
338 typedef std::pair<size_t, section_size_type> Reloc;
340 // A Stub_template may not be copied. We want to share templates as much
341 // as possible.
342 Stub_template(const Stub_template&);
343 Stub_template& operator=(const Stub_template&);
345 // Stub type.
346 Stub_type type_;
347 // Points to an array of Insn_templates.
348 const Insn_template* insns_;
349 // Number of Insn_templates in insns_[].
350 size_t insn_count_;
351 // Size of templated instructions in bytes.
352 size_t size_;
353 // Alignment of templated instructions.
354 unsigned alignment_;
355 // Flag to indicate if entry is in thumb mode.
356 bool entry_in_thumb_mode_;
357 // A table of reloc instruction indices and offsets. We can find these by
358 // looking at the instruction templates but we pre-compute and then stash
359 // them here for speed.
360 std::vector<Reloc> relocs_;
364 // A class for code stubs. This is a base class for different type of
365 // stubs used in the ARM target.
368 class Stub
370 private:
371 static const section_offset_type invalid_offset =
372 static_cast<section_offset_type>(-1);
374 public:
375 Stub(const Stub_template* stub_template)
376 : stub_template_(stub_template), offset_(invalid_offset)
379 virtual
380 ~Stub()
383 // Return the stub template.
384 const Stub_template*
385 stub_template() const
386 { return this->stub_template_; }
388 // Return offset of code stub from beginning of its containing stub table.
389 section_offset_type
390 offset() const
392 gold_assert(this->offset_ != invalid_offset);
393 return this->offset_;
396 // Set offset of code stub from beginning of its containing stub table.
397 void
398 set_offset(section_offset_type offset)
399 { this->offset_ = offset; }
401 // Return the relocation target address of the i-th relocation in the
402 // stub. This must be defined in a child class.
403 Arm_address
404 reloc_target(size_t i)
405 { return this->do_reloc_target(i); }
407 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
408 void
409 write(unsigned char* view, section_size_type view_size, bool big_endian)
410 { this->do_write(view, view_size, big_endian); }
412 protected:
413 // This must be defined in the child class.
414 virtual Arm_address
415 do_reloc_target(size_t) = 0;
417 // This must be defined in the child class.
418 virtual void
419 do_write(unsigned char*, section_size_type, bool) = 0;
421 private:
422 // Its template.
423 const Stub_template* stub_template_;
424 // Offset within the section of containing this stub.
425 section_offset_type offset_;
428 // Reloc stub class. These are stubs we use to fix up relocation because
429 // of limited branch ranges.
431 class Reloc_stub : public Stub
433 public:
434 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
435 // We assume we never jump to this address.
436 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
438 // Return destination address.
439 Arm_address
440 destination_address() const
442 gold_assert(this->destination_address_ != this->invalid_address);
443 return this->destination_address_;
446 // Set destination address.
447 void
448 set_destination_address(Arm_address address)
450 gold_assert(address != this->invalid_address);
451 this->destination_address_ = address;
454 // Reset destination address.
455 void
456 reset_destination_address()
457 { this->destination_address_ = this->invalid_address; }
459 // Determine stub type for a branch of a relocation of R_TYPE going
460 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
461 // the branch target is a thumb instruction. TARGET is used for look
462 // up ARM-specific linker settings.
463 static Stub_type
464 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
465 Arm_address branch_target, bool target_is_thumb);
467 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
468 // and an addend. Since we treat global and local symbol differently, we
469 // use a Symbol object for a global symbol and a object-index pair for
470 // a local symbol.
471 class Key
473 public:
474 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
475 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
476 // and R_SYM must not be invalid_index.
477 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
478 unsigned int r_sym, int32_t addend)
479 : stub_type_(stub_type), addend_(addend)
481 if (symbol != NULL)
483 this->r_sym_ = Reloc_stub::invalid_index;
484 this->u_.symbol = symbol;
486 else
488 gold_assert(relobj != NULL && r_sym != invalid_index);
489 this->r_sym_ = r_sym;
490 this->u_.relobj = relobj;
494 ~Key()
497 // Accessors: Keys are meant to be read-only object so no modifiers are
498 // provided.
500 // Return stub type.
501 Stub_type
502 stub_type() const
503 { return this->stub_type_; }
505 // Return the local symbol index or invalid_index.
506 unsigned int
507 r_sym() const
508 { return this->r_sym_; }
510 // Return the symbol if there is one.
511 const Symbol*
512 symbol() const
513 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
515 // Return the relobj if there is one.
516 const Relobj*
517 relobj() const
518 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
520 // Whether this equals to another key k.
521 bool
522 eq(const Key& k) const
524 return ((this->stub_type_ == k.stub_type_)
525 && (this->r_sym_ == k.r_sym_)
526 && ((this->r_sym_ != Reloc_stub::invalid_index)
527 ? (this->u_.relobj == k.u_.relobj)
528 : (this->u_.symbol == k.u_.symbol))
529 && (this->addend_ == k.addend_));
532 // Return a hash value.
533 size_t
534 hash_value() const
536 return (this->stub_type_
537 ^ this->r_sym_
538 ^ gold::string_hash<char>(
539 (this->r_sym_ != Reloc_stub::invalid_index)
540 ? this->u_.relobj->name().c_str()
541 : this->u_.symbol->name())
542 ^ this->addend_);
545 // Functors for STL associative containers.
546 struct hash
548 size_t
549 operator()(const Key& k) const
550 { return k.hash_value(); }
553 struct equal_to
555 bool
556 operator()(const Key& k1, const Key& k2) const
557 { return k1.eq(k2); }
560 // Name of key. This is mainly for debugging.
561 std::string
562 name() const;
564 private:
565 // Stub type.
566 Stub_type stub_type_;
567 // If this is a local symbol, this is the index in the defining object.
568 // Otherwise, it is invalid_index for a global symbol.
569 unsigned int r_sym_;
570 // If r_sym_ is invalid index. This points to a global symbol.
571 // Otherwise, this points a relobj. We used the unsized and target
572 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
573 // Arm_relobj. This is done to avoid making the stub class a template
574 // as most of the stub machinery is endianity-neutral. However, it
575 // may require a bit of casting done by users of this class.
576 union
578 const Symbol* symbol;
579 const Relobj* relobj;
580 } u_;
581 // Addend associated with a reloc.
582 int32_t addend_;
585 protected:
586 // Reloc_stubs are created via a stub factory. So these are protected.
587 Reloc_stub(const Stub_template* stub_template)
588 : Stub(stub_template), destination_address_(invalid_address)
591 ~Reloc_stub()
594 friend class Stub_factory;
596 private:
597 // Return the relocation target address of the i-th relocation in the
598 // stub.
599 Arm_address
600 do_reloc_target(size_t i)
602 // All reloc stub have only one relocation.
603 gold_assert(i == 0);
604 return this->destination_address_;
607 // A template to implement do_write below.
608 template<bool big_endian>
609 void inline
610 do_fixed_endian_write(unsigned char*, section_size_type);
612 // Write a stub.
613 void
614 do_write(unsigned char* view, section_size_type view_size, bool big_endian);
616 // Address of destination.
617 Arm_address destination_address_;
620 // Stub factory class.
622 class Stub_factory
624 public:
625 // Return the unique instance of this class.
626 static const Stub_factory&
627 get_instance()
629 static Stub_factory singleton;
630 return singleton;
633 // Make a relocation stub.
634 Reloc_stub*
635 make_reloc_stub(Stub_type stub_type) const
637 gold_assert(stub_type >= arm_stub_reloc_first
638 && stub_type <= arm_stub_reloc_last);
639 return new Reloc_stub(this->stub_templates_[stub_type]);
642 private:
643 // Constructor and destructor are protected since we only return a single
644 // instance created in Stub_factory::get_instance().
646 Stub_factory();
648 // A Stub_factory may not be copied since it is a singleton.
649 Stub_factory(const Stub_factory&);
650 Stub_factory& operator=(Stub_factory&);
652 // Stub templates. These are initialized in the constructor.
653 const Stub_template* stub_templates_[arm_stub_type_last+1];
656 // A class to hold stubs for the ARM target.
658 template<bool big_endian>
659 class Stub_table : public Output_data
661 public:
662 Stub_table(Arm_input_section<big_endian>* owner)
663 : Output_data(), addralign_(1), owner_(owner), has_been_changed_(false),
664 reloc_stubs_()
667 ~Stub_table()
670 // Owner of this stub table.
671 Arm_input_section<big_endian>*
672 owner() const
673 { return this->owner_; }
675 // Whether this stub table is empty.
676 bool
677 empty() const
678 { return this->reloc_stubs_.empty(); }
680 // Whether this has been changed.
681 bool
682 has_been_changed() const
683 { return this->has_been_changed_; }
685 // Set the has-been-changed flag.
686 void
687 set_has_been_changed(bool value)
688 { this->has_been_changed_ = value; }
690 // Return the current data size.
691 off_t
692 current_data_size() const
693 { return this->current_data_size_for_child(); }
695 // Add a STUB with using KEY. Caller is reponsible for avoid adding
696 // if already a STUB with the same key has been added.
697 void
698 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key);
700 // Look up a relocation stub using KEY. Return NULL if there is none.
701 Reloc_stub*
702 find_reloc_stub(const Reloc_stub::Key& key) const
704 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
705 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
708 // Relocate stubs in this stub table.
709 void
710 relocate_stubs(const Relocate_info<32, big_endian>*,
711 Target_arm<big_endian>*, Output_section*,
712 unsigned char*, Arm_address, section_size_type);
714 protected:
715 // Write out section contents.
716 void
717 do_write(Output_file*);
719 // Return the required alignment.
720 uint64_t
721 do_addralign() const
722 { return this->addralign_; }
724 // Finalize data size.
725 void
726 set_final_data_size()
727 { this->set_data_size(this->current_data_size_for_child()); }
729 // Reset address and file offset.
730 void
731 do_reset_address_and_file_offset();
733 private:
734 // Unordered map of stubs.
735 typedef
736 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
737 Reloc_stub::Key::equal_to>
738 Reloc_stub_map;
740 // Address alignment
741 uint64_t addralign_;
742 // Owner of this stub table.
743 Arm_input_section<big_endian>* owner_;
744 // This is set to true during relaxiong if the size of the stub table
745 // has been changed.
746 bool has_been_changed_;
747 // The relocation stubs.
748 Reloc_stub_map reloc_stubs_;
751 // A class to wrap an ordinary input section containing executable code.
753 template<bool big_endian>
754 class Arm_input_section : public Output_relaxed_input_section
756 public:
757 Arm_input_section(Relobj* relobj, unsigned int shndx)
758 : Output_relaxed_input_section(relobj, shndx, 1),
759 original_addralign_(1), original_size_(0), stub_table_(NULL)
762 ~Arm_input_section()
765 // Initialize.
766 void
767 init();
769 // Whether this is a stub table owner.
770 bool
771 is_stub_table_owner() const
772 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
774 // Return the stub table.
775 Stub_table<big_endian>*
776 stub_table() const
777 { return this->stub_table_; }
779 // Set the stub_table.
780 void
781 set_stub_table(Stub_table<big_endian>* stub_table)
782 { this->stub_table_ = stub_table; }
784 // Downcast a base pointer to an Arm_input_section pointer. This is
785 // not type-safe but we only use Arm_input_section not the base class.
786 static Arm_input_section<big_endian>*
787 as_arm_input_section(Output_relaxed_input_section* poris)
788 { return static_cast<Arm_input_section<big_endian>*>(poris); }
790 protected:
791 // Write data to output file.
792 void
793 do_write(Output_file*);
795 // Return required alignment of this.
796 uint64_t
797 do_addralign() const
799 if (this->is_stub_table_owner())
800 return std::max(this->stub_table_->addralign(),
801 this->original_addralign_);
802 else
803 return this->original_addralign_;
806 // Finalize data size.
807 void
808 set_final_data_size();
810 // Reset address and file offset.
811 void
812 do_reset_address_and_file_offset();
814 // Output offset.
815 bool
816 do_output_offset(const Relobj* object, unsigned int shndx,
817 section_offset_type offset,
818 section_offset_type* poutput) const
820 if ((object == this->relobj())
821 && (shndx == this->shndx())
822 && (offset >= 0)
823 && (convert_types<uint64_t, section_offset_type>(offset)
824 <= this->original_size_))
826 *poutput = offset;
827 return true;
829 else
830 return false;
833 private:
834 // Copying is not allowed.
835 Arm_input_section(const Arm_input_section&);
836 Arm_input_section& operator=(const Arm_input_section&);
838 // Address alignment of the original input section.
839 uint64_t original_addralign_;
840 // Section size of the original input section.
841 uint64_t original_size_;
842 // Stub table.
843 Stub_table<big_endian>* stub_table_;
846 // Arm output section class. This is defined mainly to add a number of
847 // stub generation methods.
849 template<bool big_endian>
850 class Arm_output_section : public Output_section
852 public:
853 Arm_output_section(const char* name, elfcpp::Elf_Word type,
854 elfcpp::Elf_Xword flags)
855 : Output_section(name, type, flags)
858 ~Arm_output_section()
861 // Group input sections for stub generation.
862 void
863 group_sections(section_size_type, bool, Target_arm<big_endian>*);
865 // Downcast a base pointer to an Arm_output_section pointer. This is
866 // not type-safe but we only use Arm_output_section not the base class.
867 static Arm_output_section<big_endian>*
868 as_arm_output_section(Output_section* os)
869 { return static_cast<Arm_output_section<big_endian>*>(os); }
871 private:
872 // For convenience.
873 typedef Output_section::Input_section Input_section;
874 typedef Output_section::Input_section_list Input_section_list;
876 // Create a stub group.
877 void create_stub_group(Input_section_list::const_iterator,
878 Input_section_list::const_iterator,
879 Input_section_list::const_iterator,
880 Target_arm<big_endian>*,
881 std::vector<Output_relaxed_input_section*>*);
884 // Arm_relobj class.
886 template<bool big_endian>
887 class Arm_relobj : public Sized_relobj<32, big_endian>
889 public:
890 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
892 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
893 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
894 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
895 stub_tables_(), local_symbol_is_thumb_function_()
898 ~Arm_relobj()
901 // Return the stub table of the SHNDX-th section if there is one.
902 Stub_table<big_endian>*
903 stub_table(unsigned int shndx) const
905 gold_assert(shndx < this->stub_tables_.size());
906 return this->stub_tables_[shndx];
909 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
910 void
911 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
913 gold_assert(shndx < this->stub_tables_.size());
914 this->stub_tables_[shndx] = stub_table;
917 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
918 // index. This is only valid after do_count_local_symbol is called.
919 bool
920 local_symbol_is_thumb_function(unsigned int r_sym) const
922 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
923 return this->local_symbol_is_thumb_function_[r_sym];
926 // Scan all relocation sections for stub generation.
927 void
928 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
929 const Layout*);
931 // Convert regular input section with index SHNDX to a relaxed section.
932 void
933 convert_input_section_to_relaxed_section(unsigned shndx)
935 // The stubs have relocations and we need to process them after writing
936 // out the stubs. So relocation now must follow section write.
937 this->invalidate_section_offset(shndx);
938 this->set_relocs_must_follow_section_writes();
941 // Downcast a base pointer to an Arm_relobj pointer. This is
942 // not type-safe but we only use Arm_relobj not the base class.
943 static Arm_relobj<big_endian>*
944 as_arm_relobj(Relobj* relobj)
945 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
947 // Processor-specific flags in ELF file header. This is valid only after
948 // reading symbols.
949 elfcpp::Elf_Word
950 processor_specific_flags() const
951 { return this->processor_specific_flags_; }
953 protected:
954 // Post constructor setup.
955 void
956 do_setup()
958 // Call parent's setup method.
959 Sized_relobj<32, big_endian>::do_setup();
961 // Initialize look-up tables.
962 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
963 this->stub_tables_.swap(empty_stub_table_list);
966 // Count the local symbols.
967 void
968 do_count_local_symbols(Stringpool_template<char>*,
969 Stringpool_template<char>*);
971 void
972 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
973 const unsigned char* pshdrs,
974 typename Sized_relobj<32, big_endian>::Views* pivews);
976 // Read the symbol information.
977 void
978 do_read_symbols(Read_symbols_data* sd);
980 private:
981 // List of stub tables.
982 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
983 Stub_table_list stub_tables_;
984 // Bit vector to tell if a local symbol is a thumb function or not.
985 // This is only valid after do_count_local_symbol is called.
986 std::vector<bool> local_symbol_is_thumb_function_;
987 // processor-specific flags in ELF file header.
988 elfcpp::Elf_Word processor_specific_flags_;
991 // Arm_dynobj class.
993 template<bool big_endian>
994 class Arm_dynobj : public Sized_dynobj<32, big_endian>
996 public:
997 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
998 const elfcpp::Ehdr<32, big_endian>& ehdr)
999 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1000 processor_specific_flags_(0)
1003 ~Arm_dynobj()
1006 // Downcast a base pointer to an Arm_relobj pointer. This is
1007 // not type-safe but we only use Arm_relobj not the base class.
1008 static Arm_dynobj<big_endian>*
1009 as_arm_dynobj(Dynobj* dynobj)
1010 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1012 // Processor-specific flags in ELF file header. This is valid only after
1013 // reading symbols.
1014 elfcpp::Elf_Word
1015 processor_specific_flags() const
1016 { return this->processor_specific_flags_; }
1018 protected:
1019 // Read the symbol information.
1020 void
1021 do_read_symbols(Read_symbols_data* sd);
1023 private:
1024 // processor-specific flags in ELF file header.
1025 elfcpp::Elf_Word processor_specific_flags_;
1028 // Functor to read reloc addends during stub generation.
1030 template<int sh_type, bool big_endian>
1031 struct Stub_addend_reader
1033 // Return the addend for a relocation of a particular type. Depending
1034 // on whether this is a REL or RELA relocation, read the addend from a
1035 // view or from a Reloc object.
1036 elfcpp::Elf_types<32>::Elf_Swxword
1037 operator()(
1038 unsigned int /* r_type */,
1039 const unsigned char* /* view */,
1040 const typename Reloc_types<sh_type,
1041 32, big_endian>::Reloc& /* reloc */) const;
1044 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1046 template<bool big_endian>
1047 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1049 elfcpp::Elf_types<32>::Elf_Swxword
1050 operator()(
1051 unsigned int,
1052 const unsigned char*,
1053 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1056 // Specialized Stub_addend_reader for RELA type relocation sections.
1057 // We currently do not handle RELA type relocation sections but it is trivial
1058 // to implement the addend reader. This is provided for completeness and to
1059 // make it easier to add support for RELA relocation sections in the future.
1061 template<bool big_endian>
1062 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1064 elfcpp::Elf_types<32>::Elf_Swxword
1065 operator()(
1066 unsigned int,
1067 const unsigned char*,
1068 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1069 big_endian>::Reloc& reloc) const
1070 { return reloc.get_r_addend(); }
1073 // Utilities for manipulating integers of up to 32-bits
1075 namespace utils
1077 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1078 // an int32_t. NO_BITS must be between 1 to 32.
1079 template<int no_bits>
1080 static inline int32_t
1081 sign_extend(uint32_t bits)
1083 gold_assert(no_bits >= 0 && no_bits <= 32);
1084 if (no_bits == 32)
1085 return static_cast<int32_t>(bits);
1086 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1087 bits &= mask;
1088 uint32_t top_bit = 1U << (no_bits - 1);
1089 int32_t as_signed = static_cast<int32_t>(bits);
1090 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1093 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1094 template<int no_bits>
1095 static inline bool
1096 has_overflow(uint32_t bits)
1098 gold_assert(no_bits >= 0 && no_bits <= 32);
1099 if (no_bits == 32)
1100 return false;
1101 int32_t max = (1 << (no_bits - 1)) - 1;
1102 int32_t min = -(1 << (no_bits - 1));
1103 int32_t as_signed = static_cast<int32_t>(bits);
1104 return as_signed > max || as_signed < min;
1107 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1108 // fits in the given number of bits as either a signed or unsigned value.
1109 // For example, has_signed_unsigned_overflow<8> would check
1110 // -128 <= bits <= 255
1111 template<int no_bits>
1112 static inline bool
1113 has_signed_unsigned_overflow(uint32_t bits)
1115 gold_assert(no_bits >= 2 && no_bits <= 32);
1116 if (no_bits == 32)
1117 return false;
1118 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1119 int32_t min = -(1 << (no_bits - 1));
1120 int32_t as_signed = static_cast<int32_t>(bits);
1121 return as_signed > max || as_signed < min;
1124 // Select bits from A and B using bits in MASK. For each n in [0..31],
1125 // the n-th bit in the result is chosen from the n-th bits of A and B.
1126 // A zero selects A and a one selects B.
1127 static inline uint32_t
1128 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1129 { return (a & ~mask) | (b & mask); }
1132 template<bool big_endian>
1133 class Target_arm : public Sized_target<32, big_endian>
1135 public:
1136 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1137 Reloc_section;
1139 // When were are relocating a stub, we pass this as the relocation number.
1140 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1142 Target_arm()
1143 : Sized_target<32, big_endian>(&arm_info),
1144 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1145 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1146 stub_factory_(Stub_factory::get_instance()),
1147 may_use_blx_(true), should_force_pic_veneer_(false),
1148 arm_input_section_map_()
1151 // Whether we can use BLX.
1152 bool
1153 may_use_blx() const
1154 { return this->may_use_blx_; }
1156 // Set use-BLX flag.
1157 void
1158 set_may_use_blx(bool value)
1159 { this->may_use_blx_ = value; }
1161 // Whether we force PCI branch veneers.
1162 bool
1163 should_force_pic_veneer() const
1164 { return this->should_force_pic_veneer_; }
1166 // Set PIC veneer flag.
1167 void
1168 set_should_force_pic_veneer(bool value)
1169 { this->should_force_pic_veneer_ = value; }
1171 // Whether we use THUMB-2 instructions.
1172 bool
1173 using_thumb2() const
1175 // FIXME: This should not hard-coded.
1176 return false;
1179 // Whether we use THUMB/THUMB-2 instructions only.
1180 bool
1181 using_thumb_only() const
1183 // FIXME: This should not hard-coded.
1184 return false;
1187 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1188 bool
1189 may_use_arm_nop() const
1191 // FIXME: This should not hard-coded.
1192 return false;
1195 // Whether we have THUMB-2 NOP.W instruction.
1196 bool
1197 may_use_thumb2_nop() const
1199 // FIXME: This should not hard-coded.
1200 return false;
1203 // Process the relocations to determine unreferenced sections for
1204 // garbage collection.
1205 void
1206 gc_process_relocs(Symbol_table* symtab,
1207 Layout* layout,
1208 Sized_relobj<32, big_endian>* object,
1209 unsigned int data_shndx,
1210 unsigned int sh_type,
1211 const unsigned char* prelocs,
1212 size_t reloc_count,
1213 Output_section* output_section,
1214 bool needs_special_offset_handling,
1215 size_t local_symbol_count,
1216 const unsigned char* plocal_symbols);
1218 // Scan the relocations to look for symbol adjustments.
1219 void
1220 scan_relocs(Symbol_table* symtab,
1221 Layout* layout,
1222 Sized_relobj<32, big_endian>* object,
1223 unsigned int data_shndx,
1224 unsigned int sh_type,
1225 const unsigned char* prelocs,
1226 size_t reloc_count,
1227 Output_section* output_section,
1228 bool needs_special_offset_handling,
1229 size_t local_symbol_count,
1230 const unsigned char* plocal_symbols);
1232 // Finalize the sections.
1233 void
1234 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1236 // Return the value to use for a dynamic symbol which requires special
1237 // treatment.
1238 uint64_t
1239 do_dynsym_value(const Symbol*) const;
1241 // Relocate a section.
1242 void
1243 relocate_section(const Relocate_info<32, big_endian>*,
1244 unsigned int sh_type,
1245 const unsigned char* prelocs,
1246 size_t reloc_count,
1247 Output_section* output_section,
1248 bool needs_special_offset_handling,
1249 unsigned char* view,
1250 Arm_address view_address,
1251 section_size_type view_size,
1252 const Reloc_symbol_changes*);
1254 // Scan the relocs during a relocatable link.
1255 void
1256 scan_relocatable_relocs(Symbol_table* symtab,
1257 Layout* layout,
1258 Sized_relobj<32, big_endian>* object,
1259 unsigned int data_shndx,
1260 unsigned int sh_type,
1261 const unsigned char* prelocs,
1262 size_t reloc_count,
1263 Output_section* output_section,
1264 bool needs_special_offset_handling,
1265 size_t local_symbol_count,
1266 const unsigned char* plocal_symbols,
1267 Relocatable_relocs*);
1269 // Relocate a section during a relocatable link.
1270 void
1271 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1272 unsigned int sh_type,
1273 const unsigned char* prelocs,
1274 size_t reloc_count,
1275 Output_section* output_section,
1276 off_t offset_in_output_section,
1277 const Relocatable_relocs*,
1278 unsigned char* view,
1279 Arm_address view_address,
1280 section_size_type view_size,
1281 unsigned char* reloc_view,
1282 section_size_type reloc_view_size);
1284 // Return whether SYM is defined by the ABI.
1285 bool
1286 do_is_defined_by_abi(Symbol* sym) const
1287 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1289 // Return the size of the GOT section.
1290 section_size_type
1291 got_size()
1293 gold_assert(this->got_ != NULL);
1294 return this->got_->data_size();
1297 // Map platform-specific reloc types
1298 static unsigned int
1299 get_real_reloc_type (unsigned int r_type);
1302 // Methods to support stub-generations.
1305 // Return the stub factory
1306 const Stub_factory&
1307 stub_factory() const
1308 { return this->stub_factory_; }
1310 // Make a new Arm_input_section object.
1311 Arm_input_section<big_endian>*
1312 new_arm_input_section(Relobj*, unsigned int);
1314 // Find the Arm_input_section object corresponding to the SHNDX-th input
1315 // section of RELOBJ.
1316 Arm_input_section<big_endian>*
1317 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
1319 // Make a new Stub_table
1320 Stub_table<big_endian>*
1321 new_stub_table(Arm_input_section<big_endian>*);
1323 // Scan a section for stub generation.
1324 void
1325 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
1326 const unsigned char*, size_t, Output_section*,
1327 bool, const unsigned char*, Arm_address,
1328 section_size_type);
1330 // Relocate a stub.
1331 void
1332 relocate_stub(Reloc_stub*, const Relocate_info<32, big_endian>*,
1333 Output_section*, unsigned char*, Arm_address,
1334 section_size_type);
1336 // Get the default ARM target.
1337 static Target_arm<big_endian>*
1338 default_target()
1340 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1341 && parameters->target().is_big_endian() == big_endian);
1342 return static_cast<Target_arm<big_endian>*>(
1343 parameters->sized_target<32, big_endian>());
1346 // Whether relocation type uses LSB to distinguish THUMB addresses.
1347 static bool
1348 reloc_uses_thumb_bit(unsigned int r_type);
1350 protected:
1351 // Make an ELF object.
1352 Object*
1353 do_make_elf_object(const std::string&, Input_file*, off_t,
1354 const elfcpp::Ehdr<32, big_endian>& ehdr);
1356 Object*
1357 do_make_elf_object(const std::string&, Input_file*, off_t,
1358 const elfcpp::Ehdr<32, !big_endian>&)
1359 { gold_unreachable(); }
1361 Object*
1362 do_make_elf_object(const std::string&, Input_file*, off_t,
1363 const elfcpp::Ehdr<64, false>&)
1364 { gold_unreachable(); }
1366 Object*
1367 do_make_elf_object(const std::string&, Input_file*, off_t,
1368 const elfcpp::Ehdr<64, true>&)
1369 { gold_unreachable(); }
1371 // Make an output section.
1372 Output_section*
1373 do_make_output_section(const char* name, elfcpp::Elf_Word type,
1374 elfcpp::Elf_Xword flags)
1375 { return new Arm_output_section<big_endian>(name, type, flags); }
1377 void
1378 do_adjust_elf_header(unsigned char* view, int len) const;
1380 // We only need to generate stubs, and hence perform relaxation if we are
1381 // not doing relocatable linking.
1382 bool
1383 do_may_relax() const
1384 { return !parameters->options().relocatable(); }
1386 bool
1387 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
1389 private:
1390 // The class which scans relocations.
1391 class Scan
1393 public:
1394 Scan()
1395 : issued_non_pic_error_(false)
1398 inline void
1399 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1400 Sized_relobj<32, big_endian>* object,
1401 unsigned int data_shndx,
1402 Output_section* output_section,
1403 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1404 const elfcpp::Sym<32, big_endian>& lsym);
1406 inline void
1407 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1408 Sized_relobj<32, big_endian>* object,
1409 unsigned int data_shndx,
1410 Output_section* output_section,
1411 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1412 Symbol* gsym);
1414 private:
1415 static void
1416 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1417 unsigned int r_type);
1419 static void
1420 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1421 unsigned int r_type, Symbol*);
1423 void
1424 check_non_pic(Relobj*, unsigned int r_type);
1426 // Almost identical to Symbol::needs_plt_entry except that it also
1427 // handles STT_ARM_TFUNC.
1428 static bool
1429 symbol_needs_plt_entry(const Symbol* sym)
1431 // An undefined symbol from an executable does not need a PLT entry.
1432 if (sym->is_undefined() && !parameters->options().shared())
1433 return false;
1435 return (!parameters->doing_static_link()
1436 && (sym->type() == elfcpp::STT_FUNC
1437 || sym->type() == elfcpp::STT_ARM_TFUNC)
1438 && (sym->is_from_dynobj()
1439 || sym->is_undefined()
1440 || sym->is_preemptible()));
1443 // Whether we have issued an error about a non-PIC compilation.
1444 bool issued_non_pic_error_;
1447 // The class which implements relocation.
1448 class Relocate
1450 public:
1451 Relocate()
1454 ~Relocate()
1457 // Return whether the static relocation needs to be applied.
1458 inline bool
1459 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1460 int ref_flags,
1461 bool is_32bit,
1462 Output_section* output_section);
1464 // Do a relocation. Return false if the caller should not issue
1465 // any warnings about this relocation.
1466 inline bool
1467 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1468 Output_section*, size_t relnum,
1469 const elfcpp::Rel<32, big_endian>&,
1470 unsigned int r_type, const Sized_symbol<32>*,
1471 const Symbol_value<32>*,
1472 unsigned char*, Arm_address,
1473 section_size_type);
1475 // Return whether we want to pass flag NON_PIC_REF for this
1476 // reloc. This means the relocation type accesses a symbol not via
1477 // GOT or PLT.
1478 static inline bool
1479 reloc_is_non_pic (unsigned int r_type)
1481 switch (r_type)
1483 // These relocation types reference GOT or PLT entries explicitly.
1484 case elfcpp::R_ARM_GOT_BREL:
1485 case elfcpp::R_ARM_GOT_ABS:
1486 case elfcpp::R_ARM_GOT_PREL:
1487 case elfcpp::R_ARM_GOT_BREL12:
1488 case elfcpp::R_ARM_PLT32_ABS:
1489 case elfcpp::R_ARM_TLS_GD32:
1490 case elfcpp::R_ARM_TLS_LDM32:
1491 case elfcpp::R_ARM_TLS_IE32:
1492 case elfcpp::R_ARM_TLS_IE12GP:
1494 // These relocate types may use PLT entries.
1495 case elfcpp::R_ARM_CALL:
1496 case elfcpp::R_ARM_THM_CALL:
1497 case elfcpp::R_ARM_JUMP24:
1498 case elfcpp::R_ARM_THM_JUMP24:
1499 case elfcpp::R_ARM_THM_JUMP19:
1500 case elfcpp::R_ARM_PLT32:
1501 case elfcpp::R_ARM_THM_XPC22:
1502 return false;
1504 default:
1505 return true;
1510 // A class which returns the size required for a relocation type,
1511 // used while scanning relocs during a relocatable link.
1512 class Relocatable_size_for_reloc
1514 public:
1515 unsigned int
1516 get_size_for_reloc(unsigned int, Relobj*);
1519 // Get the GOT section, creating it if necessary.
1520 Output_data_got<32, big_endian>*
1521 got_section(Symbol_table*, Layout*);
1523 // Get the GOT PLT section.
1524 Output_data_space*
1525 got_plt_section() const
1527 gold_assert(this->got_plt_ != NULL);
1528 return this->got_plt_;
1531 // Create a PLT entry for a global symbol.
1532 void
1533 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1535 // Get the PLT section.
1536 const Output_data_plt_arm<big_endian>*
1537 plt_section() const
1539 gold_assert(this->plt_ != NULL);
1540 return this->plt_;
1543 // Get the dynamic reloc section, creating it if necessary.
1544 Reloc_section*
1545 rel_dyn_section(Layout*);
1547 // Return true if the symbol may need a COPY relocation.
1548 // References from an executable object to non-function symbols
1549 // defined in a dynamic object may need a COPY relocation.
1550 bool
1551 may_need_copy_reloc(Symbol* gsym)
1553 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1554 && gsym->may_need_copy_reloc());
1557 // Add a potential copy relocation.
1558 void
1559 copy_reloc(Symbol_table* symtab, Layout* layout,
1560 Sized_relobj<32, big_endian>* object,
1561 unsigned int shndx, Output_section* output_section,
1562 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1564 this->copy_relocs_.copy_reloc(symtab, layout,
1565 symtab->get_sized_symbol<32>(sym),
1566 object, shndx, output_section, reloc,
1567 this->rel_dyn_section(layout));
1570 // Whether two EABI versions are compatible.
1571 static bool
1572 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1574 // Merge processor-specific flags from input object and those in the ELF
1575 // header of the output.
1576 void
1577 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1580 // Methods to support stub-generations.
1583 // Group input sections for stub generation.
1584 void
1585 group_sections(Layout*, section_size_type, bool);
1587 // Scan a relocation for stub generation.
1588 void
1589 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
1590 const Sized_symbol<32>*, unsigned int,
1591 const Symbol_value<32>*,
1592 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
1594 // Scan a relocation section for stub.
1595 template<int sh_type>
1596 void
1597 scan_reloc_section_for_stubs(
1598 const Relocate_info<32, big_endian>* relinfo,
1599 const unsigned char* prelocs,
1600 size_t reloc_count,
1601 Output_section* output_section,
1602 bool needs_special_offset_handling,
1603 const unsigned char* view,
1604 elfcpp::Elf_types<32>::Elf_Addr view_address,
1605 section_size_type);
1607 // Information about this specific target which we pass to the
1608 // general Target structure.
1609 static const Target::Target_info arm_info;
1611 // The types of GOT entries needed for this platform.
1612 enum Got_type
1614 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1617 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
1619 // Map input section to Arm_input_section.
1620 typedef Unordered_map<Input_section_specifier,
1621 Arm_input_section<big_endian>*,
1622 Input_section_specifier::hash,
1623 Input_section_specifier::equal_to>
1624 Arm_input_section_map;
1626 // The GOT section.
1627 Output_data_got<32, big_endian>* got_;
1628 // The PLT section.
1629 Output_data_plt_arm<big_endian>* plt_;
1630 // The GOT PLT section.
1631 Output_data_space* got_plt_;
1632 // The dynamic reloc section.
1633 Reloc_section* rel_dyn_;
1634 // Relocs saved to avoid a COPY reloc.
1635 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1636 // Space for variables copied with a COPY reloc.
1637 Output_data_space* dynbss_;
1638 // Vector of Stub_tables created.
1639 Stub_table_list stub_tables_;
1640 // Stub factory.
1641 const Stub_factory &stub_factory_;
1642 // Whether we can use BLX.
1643 bool may_use_blx_;
1644 // Whether we force PIC branch veneers.
1645 bool should_force_pic_veneer_;
1646 // Map for locating Arm_input_sections.
1647 Arm_input_section_map arm_input_section_map_;
1650 template<bool big_endian>
1651 const Target::Target_info Target_arm<big_endian>::arm_info =
1653 32, // size
1654 big_endian, // is_big_endian
1655 elfcpp::EM_ARM, // machine_code
1656 false, // has_make_symbol
1657 false, // has_resolve
1658 false, // has_code_fill
1659 true, // is_default_stack_executable
1660 '\0', // wrap_char
1661 "/usr/lib/libc.so.1", // dynamic_linker
1662 0x8000, // default_text_segment_address
1663 0x1000, // abi_pagesize (overridable by -z max-page-size)
1664 0x1000, // common_pagesize (overridable by -z common-page-size)
1665 elfcpp::SHN_UNDEF, // small_common_shndx
1666 elfcpp::SHN_UNDEF, // large_common_shndx
1667 0, // small_common_section_flags
1668 0, // large_common_section_flags
1669 ".ARM.attributes", // attributes_section
1670 "aeabi" // attributes_vendor
1673 // Arm relocate functions class
1676 template<bool big_endian>
1677 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1679 public:
1680 typedef enum
1682 STATUS_OKAY, // No error during relocation.
1683 STATUS_OVERFLOW, // Relocation oveflow.
1684 STATUS_BAD_RELOC // Relocation cannot be applied.
1685 } Status;
1687 private:
1688 typedef Relocate_functions<32, big_endian> Base;
1689 typedef Arm_relocate_functions<big_endian> This;
1691 // Encoding of imm16 argument for movt and movw ARM instructions
1692 // from ARM ARM:
1694 // imm16 := imm4 | imm12
1696 // 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
1697 // +-------+---------------+-------+-------+-----------------------+
1698 // | | |imm4 | |imm12 |
1699 // +-------+---------------+-------+-------+-----------------------+
1701 // Extract the relocation addend from VAL based on the ARM
1702 // instruction encoding described above.
1703 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1704 extract_arm_movw_movt_addend(
1705 typename elfcpp::Swap<32, big_endian>::Valtype val)
1707 // According to the Elf ABI for ARM Architecture the immediate
1708 // field is sign-extended to form the addend.
1709 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1712 // Insert X into VAL based on the ARM instruction encoding described
1713 // above.
1714 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1715 insert_val_arm_movw_movt(
1716 typename elfcpp::Swap<32, big_endian>::Valtype val,
1717 typename elfcpp::Swap<32, big_endian>::Valtype x)
1719 val &= 0xfff0f000;
1720 val |= x & 0x0fff;
1721 val |= (x & 0xf000) << 4;
1722 return val;
1725 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1726 // from ARM ARM:
1728 // imm16 := imm4 | i | imm3 | imm8
1730 // 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
1731 // +---------+-+-----------+-------++-+-----+-------+---------------+
1732 // | |i| |imm4 || |imm3 | |imm8 |
1733 // +---------+-+-----------+-------++-+-----+-------+---------------+
1735 // Extract the relocation addend from VAL based on the Thumb2
1736 // instruction encoding described above.
1737 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1738 extract_thumb_movw_movt_addend(
1739 typename elfcpp::Swap<32, big_endian>::Valtype val)
1741 // According to the Elf ABI for ARM Architecture the immediate
1742 // field is sign-extended to form the addend.
1743 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1744 | ((val >> 15) & 0x0800)
1745 | ((val >> 4) & 0x0700)
1746 | (val & 0x00ff));
1749 // Insert X into VAL based on the Thumb2 instruction encoding
1750 // described above.
1751 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1752 insert_val_thumb_movw_movt(
1753 typename elfcpp::Swap<32, big_endian>::Valtype val,
1754 typename elfcpp::Swap<32, big_endian>::Valtype x)
1756 val &= 0xfbf08f00;
1757 val |= (x & 0xf000) << 4;
1758 val |= (x & 0x0800) << 15;
1759 val |= (x & 0x0700) << 4;
1760 val |= (x & 0x00ff);
1761 return val;
1764 // Handle ARM long branches.
1765 static typename This::Status
1766 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
1767 unsigned char *, const Sized_symbol<32>*,
1768 const Arm_relobj<big_endian>*, unsigned int,
1769 const Symbol_value<32>*, Arm_address, Arm_address, bool);
1771 // Handle THUMB long branches.
1772 static typename This::Status
1773 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
1774 unsigned char *, const Sized_symbol<32>*,
1775 const Arm_relobj<big_endian>*, unsigned int,
1776 const Symbol_value<32>*, Arm_address, Arm_address, bool);
1778 public:
1780 // R_ARM_ABS8: S + A
1781 static inline typename This::Status
1782 abs8(unsigned char *view,
1783 const Sized_relobj<32, big_endian>* object,
1784 const Symbol_value<32>* psymval)
1786 typedef typename elfcpp::Swap<8, 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<8, big_endian>::readval(wv);
1790 Reltype addend = utils::sign_extend<8>(val);
1791 Reltype x = psymval->value(object, addend);
1792 val = utils::bit_select(val, x, 0xffU);
1793 elfcpp::Swap<8, big_endian>::writeval(wv, val);
1794 return (utils::has_signed_unsigned_overflow<8>(x)
1795 ? This::STATUS_OVERFLOW
1796 : This::STATUS_OKAY);
1799 // R_ARM_THM_ABS5: S + A
1800 static inline typename This::Status
1801 thm_abs5(unsigned char *view,
1802 const Sized_relobj<32, big_endian>* object,
1803 const Symbol_value<32>* psymval)
1805 typedef typename elfcpp::Swap<16, 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<16, big_endian>::readval(wv);
1809 Reltype addend = (val & 0x7e0U) >> 6;
1810 Reltype x = psymval->value(object, addend);
1811 val = utils::bit_select(val, x << 6, 0x7e0U);
1812 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1813 return (utils::has_overflow<5>(x)
1814 ? This::STATUS_OVERFLOW
1815 : This::STATUS_OKAY);
1818 // R_ARM_ABS12: S + A
1819 static inline typename This::Status
1820 abs12(unsigned char *view,
1821 const Sized_relobj<32, big_endian>* object,
1822 const Symbol_value<32>* psymval)
1824 typedef typename elfcpp::Swap<32, 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<32, big_endian>::readval(wv);
1828 Reltype addend = val & 0x0fffU;
1829 Reltype x = psymval->value(object, addend);
1830 val = utils::bit_select(val, x, 0x0fffU);
1831 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1832 return (utils::has_overflow<12>(x)
1833 ? This::STATUS_OVERFLOW
1834 : This::STATUS_OKAY);
1837 // R_ARM_ABS16: S + A
1838 static inline typename This::Status
1839 abs16(unsigned char *view,
1840 const Sized_relobj<32, big_endian>* object,
1841 const Symbol_value<32>* psymval)
1843 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1844 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1845 Valtype* wv = reinterpret_cast<Valtype*>(view);
1846 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1847 Reltype addend = utils::sign_extend<16>(val);
1848 Reltype x = psymval->value(object, addend);
1849 val = utils::bit_select(val, x, 0xffffU);
1850 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1851 return (utils::has_signed_unsigned_overflow<16>(x)
1852 ? This::STATUS_OVERFLOW
1853 : This::STATUS_OKAY);
1856 // R_ARM_ABS32: (S + A) | T
1857 static inline typename This::Status
1858 abs32(unsigned char *view,
1859 const Sized_relobj<32, big_endian>* object,
1860 const Symbol_value<32>* psymval,
1861 Arm_address thumb_bit)
1863 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1864 Valtype* wv = reinterpret_cast<Valtype*>(view);
1865 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1866 Valtype x = psymval->value(object, addend) | thumb_bit;
1867 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1868 return This::STATUS_OKAY;
1871 // R_ARM_REL32: (S + A) | T - P
1872 static inline typename This::Status
1873 rel32(unsigned char *view,
1874 const Sized_relobj<32, big_endian>* object,
1875 const Symbol_value<32>* psymval,
1876 Arm_address address,
1877 Arm_address thumb_bit)
1879 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1880 Valtype* wv = reinterpret_cast<Valtype*>(view);
1881 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1882 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
1883 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1884 return This::STATUS_OKAY;
1887 // R_ARM_THM_CALL: (S + A) | T - P
1888 static inline typename This::Status
1889 thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
1890 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
1891 unsigned int r_sym, const Symbol_value<32>* psymval,
1892 Arm_address address, Arm_address thumb_bit,
1893 bool is_weakly_undefined_without_plt)
1895 return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
1896 object, r_sym, psymval, address, thumb_bit,
1897 is_weakly_undefined_without_plt);
1900 // R_ARM_THM_JUMP24: (S + A) | T - P
1901 static inline typename This::Status
1902 thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
1903 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
1904 unsigned int r_sym, const Symbol_value<32>* psymval,
1905 Arm_address address, Arm_address thumb_bit,
1906 bool is_weakly_undefined_without_plt)
1908 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
1909 object, r_sym, psymval, address, thumb_bit,
1910 is_weakly_undefined_without_plt);
1913 // R_ARM_THM_XPC22: (S + A) | T - P
1914 static inline typename This::Status
1915 thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
1916 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
1917 unsigned int r_sym, const Symbol_value<32>* psymval,
1918 Arm_address address, Arm_address thumb_bit,
1919 bool is_weakly_undefined_without_plt)
1921 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
1922 object, r_sym, psymval, address, thumb_bit,
1923 is_weakly_undefined_without_plt);
1926 // R_ARM_BASE_PREL: B(S) + A - P
1927 static inline typename This::Status
1928 base_prel(unsigned char* view,
1929 Arm_address origin,
1930 Arm_address address)
1932 Base::rel32(view, origin - address);
1933 return STATUS_OKAY;
1936 // R_ARM_BASE_ABS: B(S) + A
1937 static inline typename This::Status
1938 base_abs(unsigned char* view,
1939 Arm_address origin)
1941 Base::rel32(view, origin);
1942 return STATUS_OKAY;
1945 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1946 static inline typename This::Status
1947 got_brel(unsigned char* view,
1948 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1950 Base::rel32(view, got_offset);
1951 return This::STATUS_OKAY;
1954 // R_ARM_GOT_PREL: GOT(S) + A - P
1955 static inline typename This::Status
1956 got_prel(unsigned char *view,
1957 Arm_address got_entry,
1958 Arm_address address)
1960 Base::rel32(view, got_entry - address);
1961 return This::STATUS_OKAY;
1964 // R_ARM_PLT32: (S + A) | T - P
1965 static inline typename This::Status
1966 plt32(const Relocate_info<32, big_endian>* relinfo,
1967 unsigned char *view,
1968 const Sized_symbol<32>* gsym,
1969 const Arm_relobj<big_endian>* object,
1970 unsigned int r_sym,
1971 const Symbol_value<32>* psymval,
1972 Arm_address address,
1973 Arm_address thumb_bit,
1974 bool is_weakly_undefined_without_plt)
1976 return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
1977 object, r_sym, psymval, address, thumb_bit,
1978 is_weakly_undefined_without_plt);
1981 // R_ARM_XPC25: (S + A) | T - P
1982 static inline typename This::Status
1983 xpc25(const Relocate_info<32, big_endian>* relinfo,
1984 unsigned char *view,
1985 const Sized_symbol<32>* gsym,
1986 const Arm_relobj<big_endian>* object,
1987 unsigned int r_sym,
1988 const Symbol_value<32>* psymval,
1989 Arm_address address,
1990 Arm_address thumb_bit,
1991 bool is_weakly_undefined_without_plt)
1993 return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
1994 object, r_sym, psymval, address, thumb_bit,
1995 is_weakly_undefined_without_plt);
1998 // R_ARM_CALL: (S + A) | T - P
1999 static inline typename This::Status
2000 call(const Relocate_info<32, big_endian>* relinfo,
2001 unsigned char *view,
2002 const Sized_symbol<32>* gsym,
2003 const Arm_relobj<big_endian>* object,
2004 unsigned int r_sym,
2005 const Symbol_value<32>* psymval,
2006 Arm_address address,
2007 Arm_address thumb_bit,
2008 bool is_weakly_undefined_without_plt)
2010 return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2011 object, r_sym, psymval, address, thumb_bit,
2012 is_weakly_undefined_without_plt);
2015 // R_ARM_JUMP24: (S + A) | T - P
2016 static inline typename This::Status
2017 jump24(const Relocate_info<32, big_endian>* relinfo,
2018 unsigned char *view,
2019 const Sized_symbol<32>* gsym,
2020 const Arm_relobj<big_endian>* object,
2021 unsigned int r_sym,
2022 const Symbol_value<32>* psymval,
2023 Arm_address address,
2024 Arm_address thumb_bit,
2025 bool is_weakly_undefined_without_plt)
2027 return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2028 object, r_sym, psymval, address, thumb_bit,
2029 is_weakly_undefined_without_plt);
2032 // R_ARM_PREL: (S + A) | T - P
2033 static inline typename This::Status
2034 prel31(unsigned char *view,
2035 const Sized_relobj<32, big_endian>* object,
2036 const Symbol_value<32>* psymval,
2037 Arm_address address,
2038 Arm_address thumb_bit)
2040 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2041 Valtype* wv = reinterpret_cast<Valtype*>(view);
2042 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2043 Valtype addend = utils::sign_extend<31>(val);
2044 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2045 val = utils::bit_select(val, x, 0x7fffffffU);
2046 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2047 return (utils::has_overflow<31>(x) ?
2048 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2051 // R_ARM_MOVW_ABS_NC: (S + A) | T
2052 static inline typename This::Status
2053 movw_abs_nc(unsigned char *view,
2054 const Sized_relobj<32, big_endian>* object,
2055 const Symbol_value<32>* psymval,
2056 Arm_address thumb_bit)
2058 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2059 Valtype* wv = reinterpret_cast<Valtype*>(view);
2060 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2061 Valtype addend = This::extract_arm_movw_movt_addend(val);
2062 Valtype x = psymval->value(object, addend) | thumb_bit;
2063 val = This::insert_val_arm_movw_movt(val, x);
2064 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2065 return This::STATUS_OKAY;
2068 // R_ARM_MOVT_ABS: S + A
2069 static inline typename This::Status
2070 movt_abs(unsigned char *view,
2071 const Sized_relobj<32, big_endian>* object,
2072 const Symbol_value<32>* psymval)
2074 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2075 Valtype* wv = reinterpret_cast<Valtype*>(view);
2076 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2077 Valtype addend = This::extract_arm_movw_movt_addend(val);
2078 Valtype x = psymval->value(object, addend) >> 16;
2079 val = This::insert_val_arm_movw_movt(val, x);
2080 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2081 return This::STATUS_OKAY;
2084 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2085 static inline typename This::Status
2086 thm_movw_abs_nc(unsigned char *view,
2087 const Sized_relobj<32, big_endian>* object,
2088 const Symbol_value<32>* psymval,
2089 Arm_address thumb_bit)
2091 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2092 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2093 Valtype* wv = reinterpret_cast<Valtype*>(view);
2094 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2095 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2096 Reltype addend = extract_thumb_movw_movt_addend(val);
2097 Reltype x = psymval->value(object, addend) | thumb_bit;
2098 val = This::insert_val_thumb_movw_movt(val, x);
2099 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2100 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2101 return This::STATUS_OKAY;
2104 // R_ARM_THM_MOVT_ABS: S + A
2105 static inline typename This::Status
2106 thm_movt_abs(unsigned char *view,
2107 const Sized_relobj<32, big_endian>* object,
2108 const Symbol_value<32>* psymval)
2110 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2111 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2112 Valtype* wv = reinterpret_cast<Valtype*>(view);
2113 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2114 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2115 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2116 Reltype x = psymval->value(object, addend) >> 16;
2117 val = This::insert_val_thumb_movw_movt(val, x);
2118 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2119 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2120 return This::STATUS_OKAY;
2123 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2124 static inline typename This::Status
2125 movw_prel_nc(unsigned char *view,
2126 const Sized_relobj<32, big_endian>* object,
2127 const Symbol_value<32>* psymval,
2128 Arm_address address,
2129 Arm_address thumb_bit)
2131 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2132 Valtype* wv = reinterpret_cast<Valtype*>(view);
2133 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2134 Valtype addend = This::extract_arm_movw_movt_addend(val);
2135 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2136 val = This::insert_val_arm_movw_movt(val, x);
2137 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2138 return This::STATUS_OKAY;
2141 // R_ARM_MOVT_PREL: S + A - P
2142 static inline typename This::Status
2143 movt_prel(unsigned char *view,
2144 const Sized_relobj<32, big_endian>* object,
2145 const Symbol_value<32>* psymval,
2146 Arm_address address)
2148 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2149 Valtype* wv = reinterpret_cast<Valtype*>(view);
2150 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2151 Valtype addend = This::extract_arm_movw_movt_addend(val);
2152 Valtype x = (psymval->value(object, addend) - address) >> 16;
2153 val = This::insert_val_arm_movw_movt(val, x);
2154 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2155 return This::STATUS_OKAY;
2158 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2159 static inline typename This::Status
2160 thm_movw_prel_nc(unsigned char *view,
2161 const Sized_relobj<32, big_endian>* object,
2162 const Symbol_value<32>* psymval,
2163 Arm_address address,
2164 Arm_address thumb_bit)
2166 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2167 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2168 Valtype* wv = reinterpret_cast<Valtype*>(view);
2169 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2170 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2171 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2172 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
2173 val = This::insert_val_thumb_movw_movt(val, x);
2174 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2175 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2176 return This::STATUS_OKAY;
2179 // R_ARM_THM_MOVT_PREL: S + A - P
2180 static inline typename This::Status
2181 thm_movt_prel(unsigned char *view,
2182 const Sized_relobj<32, big_endian>* object,
2183 const Symbol_value<32>* psymval,
2184 Arm_address address)
2186 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2187 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2188 Valtype* wv = reinterpret_cast<Valtype*>(view);
2189 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2190 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2191 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2192 Reltype x = (psymval->value(object, addend) - address) >> 16;
2193 val = This::insert_val_thumb_movw_movt(val, x);
2194 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2195 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2196 return This::STATUS_OKAY;
2200 // Relocate ARM long branches. This handles relocation types
2201 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2202 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2203 // undefined and we do not use PLT in this relocation. In such a case,
2204 // the branch is converted into an NOP.
2206 template<bool big_endian>
2207 typename Arm_relocate_functions<big_endian>::Status
2208 Arm_relocate_functions<big_endian>::arm_branch_common(
2209 unsigned int r_type,
2210 const Relocate_info<32, big_endian>* relinfo,
2211 unsigned char *view,
2212 const Sized_symbol<32>* gsym,
2213 const Arm_relobj<big_endian>* object,
2214 unsigned int r_sym,
2215 const Symbol_value<32>* psymval,
2216 Arm_address address,
2217 Arm_address thumb_bit,
2218 bool is_weakly_undefined_without_plt)
2220 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2221 Valtype* wv = reinterpret_cast<Valtype*>(view);
2222 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2224 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
2225 && ((val & 0x0f000000UL) == 0x0a000000UL);
2226 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
2227 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
2228 && ((val & 0x0f000000UL) == 0x0b000000UL);
2229 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
2230 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
2232 // Check that the instruction is valid.
2233 if (r_type == elfcpp::R_ARM_CALL)
2235 if (!insn_is_uncond_bl && !insn_is_blx)
2236 return This::STATUS_BAD_RELOC;
2238 else if (r_type == elfcpp::R_ARM_JUMP24)
2240 if (!insn_is_b && !insn_is_cond_bl)
2241 return This::STATUS_BAD_RELOC;
2243 else if (r_type == elfcpp::R_ARM_PLT32)
2245 if (!insn_is_any_branch)
2246 return This::STATUS_BAD_RELOC;
2248 else if (r_type == elfcpp::R_ARM_XPC25)
2250 // FIXME: AAELF document IH0044C does not say much about it other
2251 // than it being obsolete.
2252 if (!insn_is_any_branch)
2253 return This::STATUS_BAD_RELOC;
2255 else
2256 gold_unreachable();
2258 // A branch to an undefined weak symbol is turned into a jump to
2259 // the next instruction unless a PLT entry will be created.
2260 // Do the same for local undefined symbols.
2261 // The jump to the next instruction is optimized as a NOP depending
2262 // on the architecture.
2263 const Target_arm<big_endian>* arm_target =
2264 Target_arm<big_endian>::default_target();
2265 if (is_weakly_undefined_without_plt)
2267 Valtype cond = val & 0xf0000000U;
2268 if (arm_target->may_use_arm_nop())
2269 val = cond | 0x0320f000;
2270 else
2271 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2272 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2273 return This::STATUS_OKAY;
2276 Valtype addend = utils::sign_extend<26>(val << 2);
2277 Valtype branch_target = psymval->value(object, addend);
2278 int32_t branch_offset = branch_target - address;
2280 // We need a stub if the branch offset is too large or if we need
2281 // to switch mode.
2282 bool may_use_blx = arm_target->may_use_blx();
2283 Reloc_stub* stub = NULL;
2284 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
2285 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2286 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
2288 Stub_type stub_type =
2289 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2290 (thumb_bit != 0));
2291 if (stub_type != arm_stub_none)
2293 Stub_table<big_endian>* stub_table =
2294 object->stub_table(relinfo->data_shndx);
2295 gold_assert(stub_table != NULL);
2297 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2298 stub = stub_table->find_reloc_stub(stub_key);
2299 gold_assert(stub != NULL);
2300 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2301 branch_target = stub_table->address() + stub->offset() + addend;
2302 branch_offset = branch_target - address;
2303 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
2304 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
2308 // At this point, if we still need to switch mode, the instruction
2309 // must either be a BLX or a BL that can be converted to a BLX.
2310 if (thumb_bit != 0)
2312 // Turn BL to BLX.
2313 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
2314 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
2317 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
2318 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2319 return (utils::has_overflow<26>(branch_offset)
2320 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
2323 // Relocate THUMB long branches. This handles relocation types
2324 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2325 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2326 // undefined and we do not use PLT in this relocation. In such a case,
2327 // the branch is converted into an NOP.
2329 template<bool big_endian>
2330 typename Arm_relocate_functions<big_endian>::Status
2331 Arm_relocate_functions<big_endian>::thumb_branch_common(
2332 unsigned int r_type,
2333 const Relocate_info<32, big_endian>* relinfo,
2334 unsigned char *view,
2335 const Sized_symbol<32>* gsym,
2336 const Arm_relobj<big_endian>* object,
2337 unsigned int r_sym,
2338 const Symbol_value<32>* psymval,
2339 Arm_address address,
2340 Arm_address thumb_bit,
2341 bool is_weakly_undefined_without_plt)
2343 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2344 Valtype* wv = reinterpret_cast<Valtype*>(view);
2345 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2346 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2348 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2349 // into account.
2350 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
2351 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
2353 // Check that the instruction is valid.
2354 if (r_type == elfcpp::R_ARM_THM_CALL)
2356 if (!is_bl_insn && !is_blx_insn)
2357 return This::STATUS_BAD_RELOC;
2359 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
2361 // This cannot be a BLX.
2362 if (!is_bl_insn)
2363 return This::STATUS_BAD_RELOC;
2365 else if (r_type == elfcpp::R_ARM_THM_XPC22)
2367 // Check for Thumb to Thumb call.
2368 if (!is_blx_insn)
2369 return This::STATUS_BAD_RELOC;
2370 if (thumb_bit != 0)
2372 gold_warning(_("%s: Thumb BLX instruction targets "
2373 "thumb function '%s'."),
2374 object->name().c_str(),
2375 (gsym ? gsym->name() : "(local)"));
2376 // Convert BLX to BL.
2377 lower_insn |= 0x1000U;
2380 else
2381 gold_unreachable();
2383 // A branch to an undefined weak symbol is turned into a jump to
2384 // the next instruction unless a PLT entry will be created.
2385 // The jump to the next instruction is optimized as a NOP.W for
2386 // Thumb-2 enabled architectures.
2387 const Target_arm<big_endian>* arm_target =
2388 Target_arm<big_endian>::default_target();
2389 if (is_weakly_undefined_without_plt)
2391 if (arm_target->may_use_thumb2_nop())
2393 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
2394 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
2396 else
2398 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
2399 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
2401 return This::STATUS_OKAY;
2404 // Fetch the addend. We use the Thumb-2 encoding (backwards compatible
2405 // with Thumb-1) involving the J1 and J2 bits.
2406 uint32_t s = (upper_insn & (1 << 10)) >> 10;
2407 uint32_t upper = upper_insn & 0x3ff;
2408 uint32_t lower = lower_insn & 0x7ff;
2409 uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
2410 uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
2411 uint32_t i1 = j1 ^ s ? 0 : 1;
2412 uint32_t i2 = j2 ^ s ? 0 : 1;
2414 int32_t addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
2415 // Sign extend.
2416 addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
2418 Arm_address branch_target = psymval->value(object, addend);
2419 int32_t branch_offset = branch_target - address;
2421 // We need a stub if the branch offset is too large or if we need
2422 // to switch mode.
2423 bool may_use_blx = arm_target->may_use_blx();
2424 bool thumb2 = arm_target->using_thumb2();
2425 if ((!thumb2
2426 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2427 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2428 || (thumb2
2429 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2430 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2431 || ((thumb_bit == 0)
2432 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2433 || r_type == elfcpp::R_ARM_THM_JUMP24)))
2435 Stub_type stub_type =
2436 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2437 (thumb_bit != 0));
2438 if (stub_type != arm_stub_none)
2440 Stub_table<big_endian>* stub_table =
2441 object->stub_table(relinfo->data_shndx);
2442 gold_assert(stub_table != NULL);
2444 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2445 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
2446 gold_assert(stub != NULL);
2447 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2448 branch_target = stub_table->address() + stub->offset() + addend;
2449 branch_offset = branch_target - address;
2453 // At this point, if we still need to switch mode, the instruction
2454 // must either be a BLX or a BL that can be converted to a BLX.
2455 if (thumb_bit == 0)
2457 gold_assert(may_use_blx
2458 && (r_type == elfcpp::R_ARM_THM_CALL
2459 || r_type == elfcpp::R_ARM_THM_XPC22));
2460 // Make sure this is a BLX.
2461 lower_insn &= ~0x1000U;
2463 else
2465 // Make sure this is a BL.
2466 lower_insn |= 0x1000U;
2469 uint32_t reloc_sign = (branch_offset < 0) ? 1 : 0;
2470 uint32_t relocation = static_cast<uint32_t>(branch_offset);
2472 if ((lower_insn & 0x5000U) == 0x4000U)
2473 // For a BLX instruction, make sure that the relocation is rounded up
2474 // to a word boundary. This follows the semantics of the instruction
2475 // which specifies that bit 1 of the target address will come from bit
2476 // 1 of the base address.
2477 relocation = (relocation + 2U) & ~3U;
2479 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2480 // We use the Thumb-2 encoding, which is safe even if dealing with
2481 // a Thumb-1 instruction by virtue of our overflow check above. */
2482 upper_insn = (upper_insn & ~0x7ffU)
2483 | ((relocation >> 12) & 0x3ffU)
2484 | (reloc_sign << 10);
2485 lower_insn = (lower_insn & ~0x2fffU)
2486 | (((!((relocation >> 23) & 1U)) ^ reloc_sign) << 13)
2487 | (((!((relocation >> 22) & 1U)) ^ reloc_sign) << 11)
2488 | ((relocation >> 1) & 0x7ffU);
2490 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
2491 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
2493 return ((thumb2
2494 ? utils::has_overflow<25>(relocation)
2495 : utils::has_overflow<23>(relocation))
2496 ? This::STATUS_OVERFLOW
2497 : This::STATUS_OKAY);
2500 // Get the GOT section, creating it if necessary.
2502 template<bool big_endian>
2503 Output_data_got<32, big_endian>*
2504 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2506 if (this->got_ == NULL)
2508 gold_assert(symtab != NULL && layout != NULL);
2510 this->got_ = new Output_data_got<32, big_endian>();
2512 Output_section* os;
2513 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2514 (elfcpp::SHF_ALLOC
2515 | elfcpp::SHF_WRITE),
2516 this->got_, false);
2517 os->set_is_relro();
2519 // The old GNU linker creates a .got.plt section. We just
2520 // create another set of data in the .got section. Note that we
2521 // always create a PLT if we create a GOT, although the PLT
2522 // might be empty.
2523 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2524 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2525 (elfcpp::SHF_ALLOC
2526 | elfcpp::SHF_WRITE),
2527 this->got_plt_, false);
2528 os->set_is_relro();
2530 // The first three entries are reserved.
2531 this->got_plt_->set_current_data_size(3 * 4);
2533 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2534 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2535 this->got_plt_,
2536 0, 0, elfcpp::STT_OBJECT,
2537 elfcpp::STB_LOCAL,
2538 elfcpp::STV_HIDDEN, 0,
2539 false, false);
2541 return this->got_;
2544 // Get the dynamic reloc section, creating it if necessary.
2546 template<bool big_endian>
2547 typename Target_arm<big_endian>::Reloc_section*
2548 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2550 if (this->rel_dyn_ == NULL)
2552 gold_assert(layout != NULL);
2553 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2554 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
2555 elfcpp::SHF_ALLOC, this->rel_dyn_, true);
2557 return this->rel_dyn_;
2560 // Insn_template methods.
2562 // Return byte size of an instruction template.
2564 size_t
2565 Insn_template::size() const
2567 switch (this->type())
2569 case THUMB16_TYPE:
2570 return 2;
2571 case ARM_TYPE:
2572 case THUMB32_TYPE:
2573 case DATA_TYPE:
2574 return 4;
2575 default:
2576 gold_unreachable();
2580 // Return alignment of an instruction template.
2582 unsigned
2583 Insn_template::alignment() const
2585 switch (this->type())
2587 case THUMB16_TYPE:
2588 case THUMB32_TYPE:
2589 return 2;
2590 case ARM_TYPE:
2591 case DATA_TYPE:
2592 return 4;
2593 default:
2594 gold_unreachable();
2598 // Stub_template methods.
2600 Stub_template::Stub_template(
2601 Stub_type type, const Insn_template* insns,
2602 size_t insn_count)
2603 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2604 entry_in_thumb_mode_(false), relocs_()
2606 off_t offset = 0;
2608 // Compute byte size and alignment of stub template.
2609 for (size_t i = 0; i < insn_count; i++)
2611 unsigned insn_alignment = insns[i].alignment();
2612 size_t insn_size = insns[i].size();
2613 gold_assert((offset & (insn_alignment - 1)) == 0);
2614 this->alignment_ = std::max(this->alignment_, insn_alignment);
2615 switch (insns[i].type())
2617 case Insn_template::THUMB16_TYPE:
2618 if (i == 0)
2619 this->entry_in_thumb_mode_ = true;
2620 break;
2622 case Insn_template::THUMB32_TYPE:
2623 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2624 this->relocs_.push_back(Reloc(i, offset));
2625 if (i == 0)
2626 this->entry_in_thumb_mode_ = true;
2627 break;
2629 case Insn_template::ARM_TYPE:
2630 // Handle cases where the target is encoded within the
2631 // instruction.
2632 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2633 this->relocs_.push_back(Reloc(i, offset));
2634 break;
2636 case Insn_template::DATA_TYPE:
2637 // Entry point cannot be data.
2638 gold_assert(i != 0);
2639 this->relocs_.push_back(Reloc(i, offset));
2640 break;
2642 default:
2643 gold_unreachable();
2645 offset += insn_size;
2647 this->size_ = offset;
2650 // Reloc_stub::Key methods.
2652 // Dump a Key as a string for debugging.
2654 std::string
2655 Reloc_stub::Key::name() const
2657 if (this->r_sym_ == invalid_index)
2659 // Global symbol key name
2660 // <stub-type>:<symbol name>:<addend>.
2661 const std::string sym_name = this->u_.symbol->name();
2662 // We need to print two hex number and two colons. So just add 100 bytes
2663 // to the symbol name size.
2664 size_t len = sym_name.size() + 100;
2665 char* buffer = new char[len];
2666 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2667 sym_name.c_str(), this->addend_);
2668 gold_assert(c > 0 && c < static_cast<int>(len));
2669 delete[] buffer;
2670 return std::string(buffer);
2672 else
2674 // local symbol key name
2675 // <stub-type>:<object>:<r_sym>:<addend>.
2676 const size_t len = 200;
2677 char buffer[len];
2678 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2679 this->u_.relobj, this->r_sym_, this->addend_);
2680 gold_assert(c > 0 && c < static_cast<int>(len));
2681 return std::string(buffer);
2685 // Reloc_stub methods.
2687 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2688 // LOCATION to DESTINATION.
2689 // This code is based on the arm_type_of_stub function in
2690 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2691 // class simple.
2693 Stub_type
2694 Reloc_stub::stub_type_for_reloc(
2695 unsigned int r_type,
2696 Arm_address location,
2697 Arm_address destination,
2698 bool target_is_thumb)
2700 Stub_type stub_type = arm_stub_none;
2702 // This is a bit ugly but we want to avoid using a templated class for
2703 // big and little endianities.
2704 bool may_use_blx;
2705 bool should_force_pic_veneer;
2706 bool thumb2;
2707 bool thumb_only;
2708 if (parameters->target().is_big_endian())
2710 const Target_arm<true>* big_endian_target =
2711 Target_arm<true>::default_target();
2712 may_use_blx = big_endian_target->may_use_blx();
2713 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
2714 thumb2 = big_endian_target->using_thumb2();
2715 thumb_only = big_endian_target->using_thumb_only();
2717 else
2719 const Target_arm<false>* little_endian_target =
2720 Target_arm<false>::default_target();
2721 may_use_blx = little_endian_target->may_use_blx();
2722 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
2723 thumb2 = little_endian_target->using_thumb2();
2724 thumb_only = little_endian_target->using_thumb_only();
2727 int64_t branch_offset = (int64_t)destination - location;
2729 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2731 // Handle cases where:
2732 // - this call goes too far (different Thumb/Thumb2 max
2733 // distance)
2734 // - it's a Thumb->Arm call and blx is not available, or it's a
2735 // Thumb->Arm branch (not bl). A stub is needed in this case.
2736 if ((!thumb2
2737 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2738 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2739 || (thumb2
2740 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2741 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2742 || ((!target_is_thumb)
2743 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2744 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
2746 if (target_is_thumb)
2748 // Thumb to thumb.
2749 if (!thumb_only)
2751 stub_type = (parameters->options().shared()
2752 || should_force_pic_veneer)
2753 // PIC stubs.
2754 ? ((may_use_blx
2755 && (r_type == elfcpp::R_ARM_THM_CALL))
2756 // V5T and above. Stub starts with ARM code, so
2757 // we must be able to switch mode before
2758 // reaching it, which is only possible for 'bl'
2759 // (ie R_ARM_THM_CALL relocation).
2760 ? arm_stub_long_branch_any_thumb_pic
2761 // On V4T, use Thumb code only.
2762 : arm_stub_long_branch_v4t_thumb_thumb_pic)
2764 // non-PIC stubs.
2765 : ((may_use_blx
2766 && (r_type == elfcpp::R_ARM_THM_CALL))
2767 ? arm_stub_long_branch_any_any // V5T and above.
2768 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
2770 else
2772 stub_type = (parameters->options().shared()
2773 || should_force_pic_veneer)
2774 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
2775 : arm_stub_long_branch_thumb_only; // non-PIC stub.
2778 else
2780 // Thumb to arm.
2782 // FIXME: We should check that the input section is from an
2783 // object that has interwork enabled.
2785 stub_type = (parameters->options().shared()
2786 || should_force_pic_veneer)
2787 // PIC stubs.
2788 ? ((may_use_blx
2789 && (r_type == elfcpp::R_ARM_THM_CALL))
2790 ? arm_stub_long_branch_any_arm_pic // V5T and above.
2791 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
2793 // non-PIC stubs.
2794 : ((may_use_blx
2795 && (r_type == elfcpp::R_ARM_THM_CALL))
2796 ? arm_stub_long_branch_any_any // V5T and above.
2797 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
2799 // Handle v4t short branches.
2800 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
2801 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
2802 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
2803 stub_type = arm_stub_short_branch_v4t_thumb_arm;
2807 else if (r_type == elfcpp::R_ARM_CALL
2808 || r_type == elfcpp::R_ARM_JUMP24
2809 || r_type == elfcpp::R_ARM_PLT32)
2811 if (target_is_thumb)
2813 // Arm to thumb.
2815 // FIXME: We should check that the input section is from an
2816 // object that has interwork enabled.
2818 // We have an extra 2-bytes reach because of
2819 // the mode change (bit 24 (H) of BLX encoding).
2820 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
2821 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2822 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
2823 || (r_type == elfcpp::R_ARM_JUMP24)
2824 || (r_type == elfcpp::R_ARM_PLT32))
2826 stub_type = (parameters->options().shared()
2827 || should_force_pic_veneer)
2828 // PIC stubs.
2829 ? (may_use_blx
2830 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
2831 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
2833 // non-PIC stubs.
2834 : (may_use_blx
2835 ? arm_stub_long_branch_any_any // V5T and above.
2836 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
2839 else
2841 // Arm to arm.
2842 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
2843 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
2845 stub_type = (parameters->options().shared()
2846 || should_force_pic_veneer)
2847 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
2848 : arm_stub_long_branch_any_any; /// non-PIC.
2853 return stub_type;
2856 // Template to implement do_write for a specific target endianity.
2858 template<bool big_endian>
2859 void inline
2860 Reloc_stub::do_fixed_endian_write(unsigned char* view,
2861 section_size_type view_size)
2863 const Stub_template* stub_template = this->stub_template();
2864 const Insn_template* insns = stub_template->insns();
2866 // FIXME: We do not handle BE8 encoding yet.
2867 unsigned char* pov = view;
2868 for (size_t i = 0; i < stub_template->insn_count(); i++)
2870 switch (insns[i].type())
2872 case Insn_template::THUMB16_TYPE:
2873 // Non-zero reloc addends are only used in Cortex-A8 stubs.
2874 gold_assert(insns[i].reloc_addend() == 0);
2875 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2876 break;
2877 case Insn_template::THUMB32_TYPE:
2879 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2880 uint32_t lo = insns[i].data() & 0xffff;
2881 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2882 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2884 break;
2885 case Insn_template::ARM_TYPE:
2886 case Insn_template::DATA_TYPE:
2887 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2888 break;
2889 default:
2890 gold_unreachable();
2892 pov += insns[i].size();
2894 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2897 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2899 void
2900 Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2901 bool big_endian)
2903 if (big_endian)
2904 this->do_fixed_endian_write<true>(view, view_size);
2905 else
2906 this->do_fixed_endian_write<false>(view, view_size);
2909 // Stub_factory methods.
2911 Stub_factory::Stub_factory()
2913 // The instruction template sequences are declared as static
2914 // objects and initialized first time the constructor runs.
2916 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2917 // to reach the stub if necessary.
2918 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2920 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2921 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2922 // dcd R_ARM_ABS32(X)
2925 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2926 // available.
2927 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2929 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2930 Insn_template::arm_insn(0xe12fff1c), // bx ip
2931 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2932 // dcd R_ARM_ABS32(X)
2935 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2936 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2938 Insn_template::thumb16_insn(0xb401), // push {r0}
2939 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
2940 Insn_template::thumb16_insn(0x4684), // mov ip, r0
2941 Insn_template::thumb16_insn(0xbc01), // pop {r0}
2942 Insn_template::thumb16_insn(0x4760), // bx ip
2943 Insn_template::thumb16_insn(0xbf00), // nop
2944 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2945 // dcd R_ARM_ABS32(X)
2948 // V4T Thumb -> Thumb long branch stub. Using the stack is not
2949 // allowed.
2950 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2952 Insn_template::thumb16_insn(0x4778), // bx pc
2953 Insn_template::thumb16_insn(0x46c0), // nop
2954 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
2955 Insn_template::arm_insn(0xe12fff1c), // bx ip
2956 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2957 // dcd R_ARM_ABS32(X)
2960 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2961 // available.
2962 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2964 Insn_template::thumb16_insn(0x4778), // bx pc
2965 Insn_template::thumb16_insn(0x46c0), // nop
2966 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
2967 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2968 // dcd R_ARM_ABS32(X)
2971 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2972 // one, when the destination is close enough.
2973 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2975 Insn_template::thumb16_insn(0x4778), // bx pc
2976 Insn_template::thumb16_insn(0x46c0), // nop
2977 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
2980 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
2981 // blx to reach the stub if necessary.
2982 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2984 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
2985 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
2986 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2987 // dcd R_ARM_REL32(X-4)
2990 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
2991 // blx to reach the stub if necessary. We can not add into pc;
2992 // it is not guaranteed to mode switch (different in ARMv6 and
2993 // ARMv7).
2994 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2996 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
2997 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
2998 Insn_template::arm_insn(0xe12fff1c), // bx ip
2999 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3000 // dcd R_ARM_REL32(X)
3003 // V4T ARM -> ARM long branch stub, PIC.
3004 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
3006 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3007 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3008 Insn_template::arm_insn(0xe12fff1c), // bx ip
3009 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3010 // dcd R_ARM_REL32(X)
3013 // V4T Thumb -> ARM long branch stub, PIC.
3014 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
3016 Insn_template::thumb16_insn(0x4778), // bx pc
3017 Insn_template::thumb16_insn(0x46c0), // nop
3018 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3019 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3020 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3021 // dcd R_ARM_REL32(X)
3024 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3025 // architectures.
3026 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
3028 Insn_template::thumb16_insn(0xb401), // push {r0}
3029 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3030 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3031 Insn_template::thumb16_insn(0x4484), // add ip, r0
3032 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3033 Insn_template::thumb16_insn(0x4760), // bx ip
3034 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
3035 // dcd R_ARM_REL32(X)
3038 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3039 // allowed.
3040 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
3042 Insn_template::thumb16_insn(0x4778), // bx pc
3043 Insn_template::thumb16_insn(0x46c0), // nop
3044 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3045 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3046 Insn_template::arm_insn(0xe12fff1c), // bx ip
3047 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3048 // dcd R_ARM_REL32(X)
3051 // Cortex-A8 erratum-workaround stubs.
3053 // Stub used for conditional branches (which may be beyond +/-1MB away,
3054 // so we can't use a conditional branch to reach this stub).
3056 // original code:
3058 // b<cond> X
3059 // after:
3061 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
3063 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3064 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3065 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3066 // b.w X
3069 // Stub used for b.w and bl.w instructions.
3071 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
3073 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3076 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
3078 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3081 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3082 // instruction (which switches to ARM mode) to point to this stub. Jump to
3083 // the real destination using an ARM-mode branch.
3084 const Insn_template elf32_arm_stub_a8_veneer_blx[] =
3086 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3089 // Fill in the stub template look-up table. Stub templates are constructed
3090 // per instance of Stub_factory for fast look-up without locking
3091 // in a thread-enabled environment.
3093 this->stub_templates_[arm_stub_none] =
3094 new Stub_template(arm_stub_none, NULL, 0);
3096 #define DEF_STUB(x) \
3097 do \
3099 size_t array_size \
3100 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3101 Stub_type type = arm_stub_##x; \
3102 this->stub_templates_[type] = \
3103 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3105 while (0);
3107 DEF_STUBS
3108 #undef DEF_STUB
3111 // Stub_table methods.
3113 // Add a STUB with using KEY. Caller is reponsible for avoid adding
3114 // if already a STUB with the same key has been added.
3116 template<bool big_endian>
3117 void
3118 Stub_table<big_endian>::add_reloc_stub(
3119 Reloc_stub* stub,
3120 const Reloc_stub::Key& key)
3122 const Stub_template* stub_template = stub->stub_template();
3123 gold_assert(stub_template->type() == key.stub_type());
3124 this->reloc_stubs_[key] = stub;
3125 if (this->addralign_ < stub_template->alignment())
3126 this->addralign_ = stub_template->alignment();
3127 this->has_been_changed_ = true;
3130 template<bool big_endian>
3131 void
3132 Stub_table<big_endian>::relocate_stubs(
3133 const Relocate_info<32, big_endian>* relinfo,
3134 Target_arm<big_endian>* arm_target,
3135 Output_section* output_section,
3136 unsigned char* view,
3137 Arm_address address,
3138 section_size_type view_size)
3140 // If we are passed a view bigger than the stub table's. we need to
3141 // adjust the view.
3142 gold_assert(address == this->address()
3143 && (view_size
3144 == static_cast<section_size_type>(this->data_size())));
3146 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3147 p != this->reloc_stubs_.end();
3148 ++p)
3150 Reloc_stub* stub = p->second;
3151 const Stub_template* stub_template = stub->stub_template();
3152 if (stub_template->reloc_count() != 0)
3154 // Adjust view to cover the stub only.
3155 section_size_type offset = stub->offset();
3156 section_size_type stub_size = stub_template->size();
3157 gold_assert(offset + stub_size <= view_size);
3159 arm_target->relocate_stub(stub, relinfo, output_section,
3160 view + offset, address + offset,
3161 stub_size);
3166 // Reset address and file offset.
3168 template<bool big_endian>
3169 void
3170 Stub_table<big_endian>::do_reset_address_and_file_offset()
3172 off_t off = 0;
3173 uint64_t max_addralign = 1;
3174 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3175 p != this->reloc_stubs_.end();
3176 ++p)
3178 Reloc_stub* stub = p->second;
3179 const Stub_template* stub_template = stub->stub_template();
3180 uint64_t stub_addralign = stub_template->alignment();
3181 max_addralign = std::max(max_addralign, stub_addralign);
3182 off = align_address(off, stub_addralign);
3183 stub->set_offset(off);
3184 stub->reset_destination_address();
3185 off += stub_template->size();
3188 this->addralign_ = max_addralign;
3189 this->set_current_data_size_for_child(off);
3192 // Write out the stubs to file.
3194 template<bool big_endian>
3195 void
3196 Stub_table<big_endian>::do_write(Output_file* of)
3198 off_t offset = this->offset();
3199 const section_size_type oview_size =
3200 convert_to_section_size_type(this->data_size());
3201 unsigned char* const oview = of->get_output_view(offset, oview_size);
3203 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3204 p != this->reloc_stubs_.end();
3205 ++p)
3207 Reloc_stub* stub = p->second;
3208 Arm_address address = this->address() + stub->offset();
3209 gold_assert(address
3210 == align_address(address,
3211 stub->stub_template()->alignment()));
3212 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3213 big_endian);
3215 of->write_output_view(this->offset(), oview_size, oview);
3218 // Arm_input_section methods.
3220 // Initialize an Arm_input_section.
3222 template<bool big_endian>
3223 void
3224 Arm_input_section<big_endian>::init()
3226 Relobj* relobj = this->relobj();
3227 unsigned int shndx = this->shndx();
3229 // Cache these to speed up size and alignment queries. It is too slow
3230 // to call section_addraglin and section_size every time.
3231 this->original_addralign_ = relobj->section_addralign(shndx);
3232 this->original_size_ = relobj->section_size(shndx);
3234 // We want to make this look like the original input section after
3235 // output sections are finalized.
3236 Output_section* os = relobj->output_section(shndx);
3237 off_t offset = relobj->output_section_offset(shndx);
3238 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
3239 this->set_address(os->address() + offset);
3240 this->set_file_offset(os->offset() + offset);
3242 this->set_current_data_size(this->original_size_);
3243 this->finalize_data_size();
3246 template<bool big_endian>
3247 void
3248 Arm_input_section<big_endian>::do_write(Output_file* of)
3250 // We have to write out the original section content.
3251 section_size_type section_size;
3252 const unsigned char* section_contents =
3253 this->relobj()->section_contents(this->shndx(), &section_size, false);
3254 of->write(this->offset(), section_contents, section_size);
3256 // If this owns a stub table and it is not empty, write it.
3257 if (this->is_stub_table_owner() && !this->stub_table_->empty())
3258 this->stub_table_->write(of);
3261 // Finalize data size.
3263 template<bool big_endian>
3264 void
3265 Arm_input_section<big_endian>::set_final_data_size()
3267 // If this owns a stub table, finalize its data size as well.
3268 if (this->is_stub_table_owner())
3270 uint64_t address = this->address();
3272 // The stub table comes after the original section contents.
3273 address += this->original_size_;
3274 address = align_address(address, this->stub_table_->addralign());
3275 off_t offset = this->offset() + (address - this->address());
3276 this->stub_table_->set_address_and_file_offset(address, offset);
3277 address += this->stub_table_->data_size();
3278 gold_assert(address == this->address() + this->current_data_size());
3281 this->set_data_size(this->current_data_size());
3284 // Reset address and file offset.
3286 template<bool big_endian>
3287 void
3288 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
3290 // Size of the original input section contents.
3291 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
3293 // If this is a stub table owner, account for the stub table size.
3294 if (this->is_stub_table_owner())
3296 Stub_table<big_endian>* stub_table = this->stub_table_;
3298 // Reset the stub table's address and file offset. The
3299 // current data size for child will be updated after that.
3300 stub_table_->reset_address_and_file_offset();
3301 off = align_address(off, stub_table_->addralign());
3302 off += stub_table->current_data_size();
3305 this->set_current_data_size(off);
3308 // Arm_output_section methods.
3310 // Create a stub group for input sections from BEGIN to END. OWNER
3311 // points to the input section to be the owner a new stub table.
3313 template<bool big_endian>
3314 void
3315 Arm_output_section<big_endian>::create_stub_group(
3316 Input_section_list::const_iterator begin,
3317 Input_section_list::const_iterator end,
3318 Input_section_list::const_iterator owner,
3319 Target_arm<big_endian>* target,
3320 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
3322 // Currently we convert ordinary input sections into relaxed sections only
3323 // at this point but we may want to support creating relaxed input section
3324 // very early. So we check here to see if owner is already a relaxed
3325 // section.
3327 Arm_input_section<big_endian>* arm_input_section;
3328 if (owner->is_relaxed_input_section())
3330 arm_input_section =
3331 Arm_input_section<big_endian>::as_arm_input_section(
3332 owner->relaxed_input_section());
3334 else
3336 gold_assert(owner->is_input_section());
3337 // Create a new relaxed input section.
3338 arm_input_section =
3339 target->new_arm_input_section(owner->relobj(), owner->shndx());
3340 new_relaxed_sections->push_back(arm_input_section);
3343 // Create a stub table.
3344 Stub_table<big_endian>* stub_table =
3345 target->new_stub_table(arm_input_section);
3347 arm_input_section->set_stub_table(stub_table);
3349 Input_section_list::const_iterator p = begin;
3350 Input_section_list::const_iterator prev_p;
3352 // Look for input sections or relaxed input sections in [begin ... end].
3355 if (p->is_input_section() || p->is_relaxed_input_section())
3357 // The stub table information for input sections live
3358 // in their objects.
3359 Arm_relobj<big_endian>* arm_relobj =
3360 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
3361 arm_relobj->set_stub_table(p->shndx(), stub_table);
3363 prev_p = p++;
3365 while (prev_p != end);
3368 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3369 // of stub groups. We grow a stub group by adding input section until the
3370 // size is just below GROUP_SIZE. The last input section will be converted
3371 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3372 // input section after the stub table, effectively double the group size.
3374 // This is similar to the group_sections() function in elf32-arm.c but is
3375 // implemented differently.
3377 template<bool big_endian>
3378 void
3379 Arm_output_section<big_endian>::group_sections(
3380 section_size_type group_size,
3381 bool stubs_always_after_branch,
3382 Target_arm<big_endian>* target)
3384 // We only care about sections containing code.
3385 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
3386 return;
3388 // States for grouping.
3389 typedef enum
3391 // No group is being built.
3392 NO_GROUP,
3393 // A group is being built but the stub table is not found yet.
3394 // We keep group a stub group until the size is just under GROUP_SIZE.
3395 // The last input section in the group will be used as the stub table.
3396 FINDING_STUB_SECTION,
3397 // A group is being built and we have already found a stub table.
3398 // We enter this state to grow a stub group by adding input section
3399 // after the stub table. This effectively doubles the group size.
3400 HAS_STUB_SECTION
3401 } State;
3403 // Any newly created relaxed sections are stored here.
3404 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
3406 State state = NO_GROUP;
3407 section_size_type off = 0;
3408 section_size_type group_begin_offset = 0;
3409 section_size_type group_end_offset = 0;
3410 section_size_type stub_table_end_offset = 0;
3411 Input_section_list::const_iterator group_begin =
3412 this->input_sections().end();
3413 Input_section_list::const_iterator stub_table =
3414 this->input_sections().end();
3415 Input_section_list::const_iterator group_end = this->input_sections().end();
3416 for (Input_section_list::const_iterator p = this->input_sections().begin();
3417 p != this->input_sections().end();
3418 ++p)
3420 section_size_type section_begin_offset =
3421 align_address(off, p->addralign());
3422 section_size_type section_end_offset =
3423 section_begin_offset + p->data_size();
3425 // Check to see if we should group the previously seens sections.
3426 switch (state)
3428 case NO_GROUP:
3429 break;
3431 case FINDING_STUB_SECTION:
3432 // Adding this section makes the group larger than GROUP_SIZE.
3433 if (section_end_offset - group_begin_offset >= group_size)
3435 if (stubs_always_after_branch)
3437 gold_assert(group_end != this->input_sections().end());
3438 this->create_stub_group(group_begin, group_end, group_end,
3439 target, &new_relaxed_sections);
3440 state = NO_GROUP;
3442 else
3444 // But wait, there's more! Input sections up to
3445 // stub_group_size bytes after the stub table can be
3446 // handled by it too.
3447 state = HAS_STUB_SECTION;
3448 stub_table = group_end;
3449 stub_table_end_offset = group_end_offset;
3452 break;
3454 case HAS_STUB_SECTION:
3455 // Adding this section makes the post stub-section group larger
3456 // than GROUP_SIZE.
3457 if (section_end_offset - stub_table_end_offset >= group_size)
3459 gold_assert(group_end != this->input_sections().end());
3460 this->create_stub_group(group_begin, group_end, stub_table,
3461 target, &new_relaxed_sections);
3462 state = NO_GROUP;
3464 break;
3466 default:
3467 gold_unreachable();
3470 // If we see an input section and currently there is no group, start
3471 // a new one. Skip any empty sections.
3472 if ((p->is_input_section() || p->is_relaxed_input_section())
3473 && (p->relobj()->section_size(p->shndx()) != 0))
3475 if (state == NO_GROUP)
3477 state = FINDING_STUB_SECTION;
3478 group_begin = p;
3479 group_begin_offset = section_begin_offset;
3482 // Keep track of the last input section seen.
3483 group_end = p;
3484 group_end_offset = section_end_offset;
3487 off = section_end_offset;
3490 // Create a stub group for any ungrouped sections.
3491 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
3493 gold_assert(group_end != this->input_sections().end());
3494 this->create_stub_group(group_begin, group_end,
3495 (state == FINDING_STUB_SECTION
3496 ? group_end
3497 : stub_table),
3498 target, &new_relaxed_sections);
3501 // Convert input section into relaxed input section in a batch.
3502 if (!new_relaxed_sections.empty())
3503 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
3505 // Update the section offsets
3506 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
3508 Arm_relobj<big_endian>* arm_relobj =
3509 Arm_relobj<big_endian>::as_arm_relobj(
3510 new_relaxed_sections[i]->relobj());
3511 unsigned int shndx = new_relaxed_sections[i]->shndx();
3512 // Tell Arm_relobj that this input section is converted.
3513 arm_relobj->convert_input_section_to_relaxed_section(shndx);
3517 // Arm_relobj methods.
3519 // Scan relocations for stub generation.
3521 template<bool big_endian>
3522 void
3523 Arm_relobj<big_endian>::scan_sections_for_stubs(
3524 Target_arm<big_endian>* arm_target,
3525 const Symbol_table* symtab,
3526 const Layout* layout)
3528 unsigned int shnum = this->shnum();
3529 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
3531 // Read the section headers.
3532 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
3533 shnum * shdr_size,
3534 true, true);
3536 // To speed up processing, we set up hash tables for fast lookup of
3537 // input offsets to output addresses.
3538 this->initialize_input_to_output_maps();
3540 const Relobj::Output_sections& out_sections(this->output_sections());
3542 Relocate_info<32, big_endian> relinfo;
3543 relinfo.symtab = symtab;
3544 relinfo.layout = layout;
3545 relinfo.object = this;
3547 const unsigned char* p = pshdrs + shdr_size;
3548 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
3550 typename elfcpp::Shdr<32, big_endian> shdr(p);
3552 unsigned int sh_type = shdr.get_sh_type();
3553 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
3554 continue;
3556 off_t sh_size = shdr.get_sh_size();
3557 if (sh_size == 0)
3558 continue;
3560 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
3561 if (index >= this->shnum())
3563 // Ignore reloc section with bad info. This error will be
3564 // reported in the final link.
3565 continue;
3568 Output_section* os = out_sections[index];
3569 if (os == NULL)
3571 // This relocation section is against a section which we
3572 // discarded.
3573 continue;
3575 Arm_address output_offset = this->get_output_section_offset(index);
3577 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
3579 // Ignore reloc section with unexpected symbol table. The
3580 // error will be reported in the final link.
3581 continue;
3584 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
3585 sh_size, true, false);
3587 unsigned int reloc_size;
3588 if (sh_type == elfcpp::SHT_REL)
3589 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
3590 else
3591 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
3593 if (reloc_size != shdr.get_sh_entsize())
3595 // Ignore reloc section with unexpected entsize. The error
3596 // will be reported in the final link.
3597 continue;
3600 size_t reloc_count = sh_size / reloc_size;
3601 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
3603 // Ignore reloc section with uneven size. The error will be
3604 // reported in the final link.
3605 continue;
3608 gold_assert(output_offset != invalid_address
3609 || this->relocs_must_follow_section_writes());
3611 // Get the section contents. This does work for the case in which
3612 // we modify the contents of an input section. We need to pass the
3613 // output view under such circumstances.
3614 section_size_type input_view_size = 0;
3615 const unsigned char* input_view =
3616 this->section_contents(index, &input_view_size, false);
3618 relinfo.reloc_shndx = i;
3619 relinfo.data_shndx = index;
3620 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
3621 reloc_count, os,
3622 output_offset == invalid_address,
3623 input_view,
3624 os->address(),
3625 input_view_size);
3628 // After we've done the relocations, we release the hash tables,
3629 // since we no longer need them.
3630 this->free_input_to_output_maps();
3633 // Count the local symbols. The ARM backend needs to know if a symbol
3634 // is a THUMB function or not. For global symbols, it is easy because
3635 // the Symbol object keeps the ELF symbol type. For local symbol it is
3636 // harder because we cannot access this information. So we override the
3637 // do_count_local_symbol in parent and scan local symbols to mark
3638 // THUMB functions. This is not the most efficient way but I do not want to
3639 // slow down other ports by calling a per symbol targer hook inside
3640 // Sized_relobj<size, big_endian>::do_count_local_symbols.
3642 template<bool big_endian>
3643 void
3644 Arm_relobj<big_endian>::do_count_local_symbols(
3645 Stringpool_template<char>* pool,
3646 Stringpool_template<char>* dynpool)
3648 // We need to fix-up the values of any local symbols whose type are
3649 // STT_ARM_TFUNC.
3651 // Ask parent to count the local symbols.
3652 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
3653 const unsigned int loccount = this->local_symbol_count();
3654 if (loccount == 0)
3655 return;
3657 // Intialize the thumb function bit-vector.
3658 std::vector<bool> empty_vector(loccount, false);
3659 this->local_symbol_is_thumb_function_.swap(empty_vector);
3661 // Read the symbol table section header.
3662 const unsigned int symtab_shndx = this->symtab_shndx();
3663 elfcpp::Shdr<32, big_endian>
3664 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
3665 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
3667 // Read the local symbols.
3668 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
3669 gold_assert(loccount == symtabshdr.get_sh_info());
3670 off_t locsize = loccount * sym_size;
3671 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
3672 locsize, true, true);
3674 // Loop over the local symbols and mark any local symbols pointing
3675 // to THUMB functions.
3677 // Skip the first dummy symbol.
3678 psyms += sym_size;
3679 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
3680 this->local_values();
3681 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
3683 elfcpp::Sym<32, big_endian> sym(psyms);
3684 elfcpp::STT st_type = sym.get_st_type();
3685 Symbol_value<32>& lv((*plocal_values)[i]);
3686 Arm_address input_value = lv.input_value();
3688 if (st_type == elfcpp::STT_ARM_TFUNC
3689 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
3691 // This is a THUMB function. Mark this and canonicalize the
3692 // symbol value by setting LSB.
3693 this->local_symbol_is_thumb_function_[i] = true;
3694 if ((input_value & 1) == 0)
3695 lv.set_input_value(input_value | 1);
3700 // Relocate sections.
3701 template<bool big_endian>
3702 void
3703 Arm_relobj<big_endian>::do_relocate_sections(
3704 const Symbol_table* symtab,
3705 const Layout* layout,
3706 const unsigned char* pshdrs,
3707 typename Sized_relobj<32, big_endian>::Views* pviews)
3709 // Call parent to relocate sections.
3710 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
3711 pviews);
3713 // We do not generate stubs if doing a relocatable link.
3714 if (parameters->options().relocatable())
3715 return;
3717 // Relocate stub tables.
3718 unsigned int shnum = this->shnum();
3720 Target_arm<big_endian>* arm_target =
3721 Target_arm<big_endian>::default_target();
3723 Relocate_info<32, big_endian> relinfo;
3724 relinfo.symtab = symtab;
3725 relinfo.layout = layout;
3726 relinfo.object = this;
3728 for (unsigned int i = 1; i < shnum; ++i)
3730 Arm_input_section<big_endian>* arm_input_section =
3731 arm_target->find_arm_input_section(this, i);
3733 if (arm_input_section == NULL
3734 || !arm_input_section->is_stub_table_owner()
3735 || arm_input_section->stub_table()->empty())
3736 continue;
3738 // We cannot discard a section if it owns a stub table.
3739 Output_section* os = this->output_section(i);
3740 gold_assert(os != NULL);
3742 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
3743 relinfo.reloc_shdr = NULL;
3744 relinfo.data_shndx = i;
3745 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
3747 gold_assert((*pviews)[i].view != NULL);
3749 // We are passed the output section view. Adjust it to cover the
3750 // stub table only.
3751 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
3752 gold_assert((stub_table->address() >= (*pviews)[i].address)
3753 && ((stub_table->address() + stub_table->data_size())
3754 <= (*pviews)[i].address + (*pviews)[i].view_size));
3756 off_t offset = stub_table->address() - (*pviews)[i].address;
3757 unsigned char* view = (*pviews)[i].view + offset;
3758 Arm_address address = stub_table->address();
3759 section_size_type view_size = stub_table->data_size();
3761 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
3762 view_size);
3766 // Read the symbol information.
3768 template<bool big_endian>
3769 void
3770 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3772 // Call parent class to read symbol information.
3773 Sized_relobj<32, big_endian>::do_read_symbols(sd);
3775 // Read processor-specific flags in ELF file header.
3776 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3777 elfcpp::Elf_sizes<32>::ehdr_size,
3778 true, false);
3779 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3780 this->processor_specific_flags_ = ehdr.get_e_flags();
3783 // Arm_dynobj methods.
3785 // Read the symbol information.
3787 template<bool big_endian>
3788 void
3789 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3791 // Call parent class to read symbol information.
3792 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
3794 // Read processor-specific flags in ELF file header.
3795 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3796 elfcpp::Elf_sizes<32>::ehdr_size,
3797 true, false);
3798 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3799 this->processor_specific_flags_ = ehdr.get_e_flags();
3802 // Stub_addend_reader methods.
3804 // Read the addend of a REL relocation of type R_TYPE at VIEW.
3806 template<bool big_endian>
3807 elfcpp::Elf_types<32>::Elf_Swxword
3808 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
3809 unsigned int r_type,
3810 const unsigned char* view,
3811 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
3813 switch (r_type)
3815 case elfcpp::R_ARM_CALL:
3816 case elfcpp::R_ARM_JUMP24:
3817 case elfcpp::R_ARM_PLT32:
3819 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3820 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3821 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3822 return utils::sign_extend<26>(val << 2);
3825 case elfcpp::R_ARM_THM_CALL:
3826 case elfcpp::R_ARM_THM_JUMP24:
3827 case elfcpp::R_ARM_THM_XPC22:
3829 // Fetch the addend. We use the Thumb-2 encoding (backwards
3830 // compatible with Thumb-1) involving the J1 and J2 bits.
3831 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3832 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3833 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3834 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3836 uint32_t s = (upper_insn & (1 << 10)) >> 10;
3837 uint32_t upper = upper_insn & 0x3ff;
3838 uint32_t lower = lower_insn & 0x7ff;
3839 uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
3840 uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
3841 uint32_t i1 = j1 ^ s ? 0 : 1;
3842 uint32_t i2 = j2 ^ s ? 0 : 1;
3844 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
3845 | (upper << 12) | (lower << 1));
3848 case elfcpp::R_ARM_THM_JUMP19:
3850 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3851 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3852 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3853 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3855 // Reconstruct the top three bits and squish the two 11 bit pieces
3856 // together.
3857 uint32_t S = (upper_insn & 0x0400) >> 10;
3858 uint32_t J1 = (lower_insn & 0x2000) >> 13;
3859 uint32_t J2 = (lower_insn & 0x0800) >> 11;
3860 uint32_t upper =
3861 (S << 8) | (J2 << 7) | (J1 << 6) | (upper_insn & 0x003f);
3862 uint32_t lower = (lower_insn & 0x07ff);
3863 return utils::sign_extend<23>((upper << 12) | (lower << 1));
3866 default:
3867 gold_unreachable();
3871 // A class to handle the PLT data.
3873 template<bool big_endian>
3874 class Output_data_plt_arm : public Output_section_data
3876 public:
3877 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
3878 Reloc_section;
3880 Output_data_plt_arm(Layout*, Output_data_space*);
3882 // Add an entry to the PLT.
3883 void
3884 add_entry(Symbol* gsym);
3886 // Return the .rel.plt section data.
3887 const Reloc_section*
3888 rel_plt() const
3889 { return this->rel_; }
3891 protected:
3892 void
3893 do_adjust_output_section(Output_section* os);
3895 // Write to a map file.
3896 void
3897 do_print_to_mapfile(Mapfile* mapfile) const
3898 { mapfile->print_output_data(this, _("** PLT")); }
3900 private:
3901 // Template for the first PLT entry.
3902 static const uint32_t first_plt_entry[5];
3904 // Template for subsequent PLT entries.
3905 static const uint32_t plt_entry[3];
3907 // Set the final size.
3908 void
3909 set_final_data_size()
3911 this->set_data_size(sizeof(first_plt_entry)
3912 + this->count_ * sizeof(plt_entry));
3915 // Write out the PLT data.
3916 void
3917 do_write(Output_file*);
3919 // The reloc section.
3920 Reloc_section* rel_;
3921 // The .got.plt section.
3922 Output_data_space* got_plt_;
3923 // The number of PLT entries.
3924 unsigned int count_;
3927 // Create the PLT section. The ordinary .got section is an argument,
3928 // since we need to refer to the start. We also create our own .got
3929 // section just for PLT entries.
3931 template<bool big_endian>
3932 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
3933 Output_data_space* got_plt)
3934 : Output_section_data(4), got_plt_(got_plt), count_(0)
3936 this->rel_ = new Reloc_section(false);
3937 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
3938 elfcpp::SHF_ALLOC, this->rel_, true);
3941 template<bool big_endian>
3942 void
3943 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
3945 os->set_entsize(0);
3948 // Add an entry to the PLT.
3950 template<bool big_endian>
3951 void
3952 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
3954 gold_assert(!gsym->has_plt_offset());
3956 // Note that when setting the PLT offset we skip the initial
3957 // reserved PLT entry.
3958 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
3959 + sizeof(first_plt_entry));
3961 ++this->count_;
3963 section_offset_type got_offset = this->got_plt_->current_data_size();
3965 // Every PLT entry needs a GOT entry which points back to the PLT
3966 // entry (this will be changed by the dynamic linker, normally
3967 // lazily when the function is called).
3968 this->got_plt_->set_current_data_size(got_offset + 4);
3970 // Every PLT entry needs a reloc.
3971 gsym->set_needs_dynsym_entry();
3972 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
3973 got_offset);
3975 // Note that we don't need to save the symbol. The contents of the
3976 // PLT are independent of which symbols are used. The symbols only
3977 // appear in the relocations.
3980 // ARM PLTs.
3981 // FIXME: This is not very flexible. Right now this has only been tested
3982 // on armv5te. If we are to support additional architecture features like
3983 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3985 // The first entry in the PLT.
3986 template<bool big_endian>
3987 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
3989 0xe52de004, // str lr, [sp, #-4]!
3990 0xe59fe004, // ldr lr, [pc, #4]
3991 0xe08fe00e, // add lr, pc, lr
3992 0xe5bef008, // ldr pc, [lr, #8]!
3993 0x00000000, // &GOT[0] - .
3996 // Subsequent entries in the PLT.
3998 template<bool big_endian>
3999 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
4001 0xe28fc600, // add ip, pc, #0xNN00000
4002 0xe28cca00, // add ip, ip, #0xNN000
4003 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4006 // Write out the PLT. This uses the hand-coded instructions above,
4007 // and adjusts them as needed. This is all specified by the arm ELF
4008 // Processor Supplement.
4010 template<bool big_endian>
4011 void
4012 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
4014 const off_t offset = this->offset();
4015 const section_size_type oview_size =
4016 convert_to_section_size_type(this->data_size());
4017 unsigned char* const oview = of->get_output_view(offset, oview_size);
4019 const off_t got_file_offset = this->got_plt_->offset();
4020 const section_size_type got_size =
4021 convert_to_section_size_type(this->got_plt_->data_size());
4022 unsigned char* const got_view = of->get_output_view(got_file_offset,
4023 got_size);
4024 unsigned char* pov = oview;
4026 Arm_address plt_address = this->address();
4027 Arm_address got_address = this->got_plt_->address();
4029 // Write first PLT entry. All but the last word are constants.
4030 const size_t num_first_plt_words = (sizeof(first_plt_entry)
4031 / sizeof(plt_entry[0]));
4032 for (size_t i = 0; i < num_first_plt_words - 1; i++)
4033 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
4034 // Last word in first PLT entry is &GOT[0] - .
4035 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
4036 got_address - (plt_address + 16));
4037 pov += sizeof(first_plt_entry);
4039 unsigned char* got_pov = got_view;
4041 memset(got_pov, 0, 12);
4042 got_pov += 12;
4044 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
4045 unsigned int plt_offset = sizeof(first_plt_entry);
4046 unsigned int plt_rel_offset = 0;
4047 unsigned int got_offset = 12;
4048 const unsigned int count = this->count_;
4049 for (unsigned int i = 0;
4050 i < count;
4051 ++i,
4052 pov += sizeof(plt_entry),
4053 got_pov += 4,
4054 plt_offset += sizeof(plt_entry),
4055 plt_rel_offset += rel_size,
4056 got_offset += 4)
4058 // Set and adjust the PLT entry itself.
4059 int32_t offset = ((got_address + got_offset)
4060 - (plt_address + plt_offset + 8));
4062 gold_assert(offset >= 0 && offset < 0x0fffffff);
4063 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
4064 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
4065 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
4066 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
4067 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
4068 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
4070 // Set the entry in the GOT.
4071 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
4074 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4075 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4077 of->write_output_view(offset, oview_size, oview);
4078 of->write_output_view(got_file_offset, got_size, got_view);
4081 // Create a PLT entry for a global symbol.
4083 template<bool big_endian>
4084 void
4085 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
4086 Symbol* gsym)
4088 if (gsym->has_plt_offset())
4089 return;
4091 if (this->plt_ == NULL)
4093 // Create the GOT sections first.
4094 this->got_section(symtab, layout);
4096 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
4097 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
4098 (elfcpp::SHF_ALLOC
4099 | elfcpp::SHF_EXECINSTR),
4100 this->plt_, false);
4102 this->plt_->add_entry(gsym);
4105 // Report an unsupported relocation against a local symbol.
4107 template<bool big_endian>
4108 void
4109 Target_arm<big_endian>::Scan::unsupported_reloc_local(
4110 Sized_relobj<32, big_endian>* object,
4111 unsigned int r_type)
4113 gold_error(_("%s: unsupported reloc %u against local symbol"),
4114 object->name().c_str(), r_type);
4117 // We are about to emit a dynamic relocation of type R_TYPE. If the
4118 // dynamic linker does not support it, issue an error. The GNU linker
4119 // only issues a non-PIC error for an allocated read-only section.
4120 // Here we know the section is allocated, but we don't know that it is
4121 // read-only. But we check for all the relocation types which the
4122 // glibc dynamic linker supports, so it seems appropriate to issue an
4123 // error even if the section is not read-only.
4125 template<bool big_endian>
4126 void
4127 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
4128 unsigned int r_type)
4130 switch (r_type)
4132 // These are the relocation types supported by glibc for ARM.
4133 case elfcpp::R_ARM_RELATIVE:
4134 case elfcpp::R_ARM_COPY:
4135 case elfcpp::R_ARM_GLOB_DAT:
4136 case elfcpp::R_ARM_JUMP_SLOT:
4137 case elfcpp::R_ARM_ABS32:
4138 case elfcpp::R_ARM_ABS32_NOI:
4139 case elfcpp::R_ARM_PC24:
4140 // FIXME: The following 3 types are not supported by Android's dynamic
4141 // linker.
4142 case elfcpp::R_ARM_TLS_DTPMOD32:
4143 case elfcpp::R_ARM_TLS_DTPOFF32:
4144 case elfcpp::R_ARM_TLS_TPOFF32:
4145 return;
4147 default:
4148 // This prevents us from issuing more than one error per reloc
4149 // section. But we can still wind up issuing more than one
4150 // error per object file.
4151 if (this->issued_non_pic_error_)
4152 return;
4153 object->error(_("requires unsupported dynamic reloc; "
4154 "recompile with -fPIC"));
4155 this->issued_non_pic_error_ = true;
4156 return;
4158 case elfcpp::R_ARM_NONE:
4159 gold_unreachable();
4163 // Scan a relocation for a local symbol.
4164 // FIXME: This only handles a subset of relocation types used by Android
4165 // on ARM v5te devices.
4167 template<bool big_endian>
4168 inline void
4169 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
4170 Layout* layout,
4171 Target_arm* target,
4172 Sized_relobj<32, big_endian>* object,
4173 unsigned int data_shndx,
4174 Output_section* output_section,
4175 const elfcpp::Rel<32, big_endian>& reloc,
4176 unsigned int r_type,
4177 const elfcpp::Sym<32, big_endian>&)
4179 r_type = get_real_reloc_type(r_type);
4180 switch (r_type)
4182 case elfcpp::R_ARM_NONE:
4183 break;
4185 case elfcpp::R_ARM_ABS32:
4186 case elfcpp::R_ARM_ABS32_NOI:
4187 // If building a shared library (or a position-independent
4188 // executable), we need to create a dynamic relocation for
4189 // this location. The relocation applied at link time will
4190 // apply the link-time value, so we flag the location with
4191 // an R_ARM_RELATIVE relocation so the dynamic loader can
4192 // relocate it easily.
4193 if (parameters->options().output_is_position_independent())
4195 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4196 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4197 // If we are to add more other reloc types than R_ARM_ABS32,
4198 // we need to add check_non_pic(object, r_type) here.
4199 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
4200 output_section, data_shndx,
4201 reloc.get_r_offset());
4203 break;
4205 case elfcpp::R_ARM_REL32:
4206 case elfcpp::R_ARM_THM_CALL:
4207 case elfcpp::R_ARM_CALL:
4208 case elfcpp::R_ARM_PREL31:
4209 case elfcpp::R_ARM_JUMP24:
4210 case elfcpp::R_ARM_PLT32:
4211 case elfcpp::R_ARM_THM_ABS5:
4212 case elfcpp::R_ARM_ABS8:
4213 case elfcpp::R_ARM_ABS12:
4214 case elfcpp::R_ARM_ABS16:
4215 case elfcpp::R_ARM_BASE_ABS:
4216 case elfcpp::R_ARM_MOVW_ABS_NC:
4217 case elfcpp::R_ARM_MOVT_ABS:
4218 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4219 case elfcpp::R_ARM_THM_MOVT_ABS:
4220 case elfcpp::R_ARM_MOVW_PREL_NC:
4221 case elfcpp::R_ARM_MOVT_PREL:
4222 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4223 case elfcpp::R_ARM_THM_MOVT_PREL:
4224 break;
4226 case elfcpp::R_ARM_GOTOFF32:
4227 // We need a GOT section:
4228 target->got_section(symtab, layout);
4229 break;
4231 case elfcpp::R_ARM_BASE_PREL:
4232 // FIXME: What about this?
4233 break;
4235 case elfcpp::R_ARM_GOT_BREL:
4236 case elfcpp::R_ARM_GOT_PREL:
4238 // The symbol requires a GOT entry.
4239 Output_data_got<32, big_endian>* got =
4240 target->got_section(symtab, layout);
4241 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4242 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
4244 // If we are generating a shared object, we need to add a
4245 // dynamic RELATIVE relocation for this symbol's GOT entry.
4246 if (parameters->options().output_is_position_independent())
4248 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4249 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4250 rel_dyn->add_local_relative(
4251 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
4252 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
4256 break;
4258 case elfcpp::R_ARM_TARGET1:
4259 // This should have been mapped to another type already.
4260 // Fall through.
4261 case elfcpp::R_ARM_COPY:
4262 case elfcpp::R_ARM_GLOB_DAT:
4263 case elfcpp::R_ARM_JUMP_SLOT:
4264 case elfcpp::R_ARM_RELATIVE:
4265 // These are relocations which should only be seen by the
4266 // dynamic linker, and should never be seen here.
4267 gold_error(_("%s: unexpected reloc %u in object file"),
4268 object->name().c_str(), r_type);
4269 break;
4271 default:
4272 unsupported_reloc_local(object, r_type);
4273 break;
4277 // Report an unsupported relocation against a global symbol.
4279 template<bool big_endian>
4280 void
4281 Target_arm<big_endian>::Scan::unsupported_reloc_global(
4282 Sized_relobj<32, big_endian>* object,
4283 unsigned int r_type,
4284 Symbol* gsym)
4286 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4287 object->name().c_str(), r_type, gsym->demangled_name().c_str());
4290 // Scan a relocation for a global symbol.
4291 // FIXME: This only handles a subset of relocation types used by Android
4292 // on ARM v5te devices.
4294 template<bool big_endian>
4295 inline void
4296 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
4297 Layout* layout,
4298 Target_arm* target,
4299 Sized_relobj<32, big_endian>* object,
4300 unsigned int data_shndx,
4301 Output_section* output_section,
4302 const elfcpp::Rel<32, big_endian>& reloc,
4303 unsigned int r_type,
4304 Symbol* gsym)
4306 r_type = get_real_reloc_type(r_type);
4307 switch (r_type)
4309 case elfcpp::R_ARM_NONE:
4310 break;
4312 case elfcpp::R_ARM_ABS32:
4313 case elfcpp::R_ARM_ABS32_NOI:
4315 // Make a dynamic relocation if necessary.
4316 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
4318 if (target->may_need_copy_reloc(gsym))
4320 target->copy_reloc(symtab, layout, object,
4321 data_shndx, output_section, gsym, reloc);
4323 else if (gsym->can_use_relative_reloc(false))
4325 // If we are to add more other reloc types than R_ARM_ABS32,
4326 // we need to add check_non_pic(object, r_type) here.
4327 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4328 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
4329 output_section, object,
4330 data_shndx, reloc.get_r_offset());
4332 else
4334 // If we are to add more other reloc types than R_ARM_ABS32,
4335 // we need to add check_non_pic(object, r_type) here.
4336 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4337 rel_dyn->add_global(gsym, r_type, output_section, object,
4338 data_shndx, reloc.get_r_offset());
4342 break;
4344 case elfcpp::R_ARM_MOVW_ABS_NC:
4345 case elfcpp::R_ARM_MOVT_ABS:
4346 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4347 case elfcpp::R_ARM_THM_MOVT_ABS:
4348 case elfcpp::R_ARM_MOVW_PREL_NC:
4349 case elfcpp::R_ARM_MOVT_PREL:
4350 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4351 case elfcpp::R_ARM_THM_MOVT_PREL:
4352 break;
4354 case elfcpp::R_ARM_THM_ABS5:
4355 case elfcpp::R_ARM_ABS8:
4356 case elfcpp::R_ARM_ABS12:
4357 case elfcpp::R_ARM_ABS16:
4358 case elfcpp::R_ARM_BASE_ABS:
4360 // No dynamic relocs of this kinds.
4361 // Report the error in case of PIC.
4362 int flags = Symbol::NON_PIC_REF;
4363 if (gsym->type() == elfcpp::STT_FUNC
4364 || gsym->type() == elfcpp::STT_ARM_TFUNC)
4365 flags |= Symbol::FUNCTION_CALL;
4366 if (gsym->needs_dynamic_reloc(flags))
4367 check_non_pic(object, r_type);
4369 break;
4371 case elfcpp::R_ARM_REL32:
4372 case elfcpp::R_ARM_PREL31:
4374 // Make a dynamic relocation if necessary.
4375 int flags = Symbol::NON_PIC_REF;
4376 if (gsym->needs_dynamic_reloc(flags))
4378 if (target->may_need_copy_reloc(gsym))
4380 target->copy_reloc(symtab, layout, object,
4381 data_shndx, output_section, gsym, reloc);
4383 else
4385 check_non_pic(object, r_type);
4386 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4387 rel_dyn->add_global(gsym, r_type, output_section, object,
4388 data_shndx, reloc.get_r_offset());
4392 break;
4394 case elfcpp::R_ARM_JUMP24:
4395 case elfcpp::R_ARM_THM_JUMP24:
4396 case elfcpp::R_ARM_CALL:
4397 case elfcpp::R_ARM_THM_CALL:
4399 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
4400 target->make_plt_entry(symtab, layout, gsym);
4401 else
4403 // Check to see if this is a function that would need a PLT
4404 // but does not get one because the function symbol is untyped.
4405 // This happens in assembly code missing a proper .type directive.
4406 if ((!gsym->is_undefined() || parameters->options().shared())
4407 && !parameters->doing_static_link()
4408 && gsym->type() == elfcpp::STT_NOTYPE
4409 && (gsym->is_from_dynobj()
4410 || gsym->is_undefined()
4411 || gsym->is_preemptible()))
4412 gold_error(_("%s is not a function."),
4413 gsym->demangled_name().c_str());
4415 break;
4417 case elfcpp::R_ARM_PLT32:
4418 // If the symbol is fully resolved, this is just a relative
4419 // local reloc. Otherwise we need a PLT entry.
4420 if (gsym->final_value_is_known())
4421 break;
4422 // If building a shared library, we can also skip the PLT entry
4423 // if the symbol is defined in the output file and is protected
4424 // or hidden.
4425 if (gsym->is_defined()
4426 && !gsym->is_from_dynobj()
4427 && !gsym->is_preemptible())
4428 break;
4429 target->make_plt_entry(symtab, layout, gsym);
4430 break;
4432 case elfcpp::R_ARM_GOTOFF32:
4433 // We need a GOT section.
4434 target->got_section(symtab, layout);
4435 break;
4437 case elfcpp::R_ARM_BASE_PREL:
4438 // FIXME: What about this?
4439 break;
4441 case elfcpp::R_ARM_GOT_BREL:
4442 case elfcpp::R_ARM_GOT_PREL:
4444 // The symbol requires a GOT entry.
4445 Output_data_got<32, big_endian>* got =
4446 target->got_section(symtab, layout);
4447 if (gsym->final_value_is_known())
4448 got->add_global(gsym, GOT_TYPE_STANDARD);
4449 else
4451 // If this symbol is not fully resolved, we need to add a
4452 // GOT entry with a dynamic relocation.
4453 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4454 if (gsym->is_from_dynobj()
4455 || gsym->is_undefined()
4456 || gsym->is_preemptible())
4457 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
4458 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
4459 else
4461 if (got->add_global(gsym, GOT_TYPE_STANDARD))
4462 rel_dyn->add_global_relative(
4463 gsym, elfcpp::R_ARM_RELATIVE, got,
4464 gsym->got_offset(GOT_TYPE_STANDARD));
4468 break;
4470 case elfcpp::R_ARM_TARGET1:
4471 // This should have been mapped to another type already.
4472 // Fall through.
4473 case elfcpp::R_ARM_COPY:
4474 case elfcpp::R_ARM_GLOB_DAT:
4475 case elfcpp::R_ARM_JUMP_SLOT:
4476 case elfcpp::R_ARM_RELATIVE:
4477 // These are relocations which should only be seen by the
4478 // dynamic linker, and should never be seen here.
4479 gold_error(_("%s: unexpected reloc %u in object file"),
4480 object->name().c_str(), r_type);
4481 break;
4483 default:
4484 unsupported_reloc_global(object, r_type, gsym);
4485 break;
4489 // Process relocations for gc.
4491 template<bool big_endian>
4492 void
4493 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
4494 Layout* layout,
4495 Sized_relobj<32, big_endian>* object,
4496 unsigned int data_shndx,
4497 unsigned int,
4498 const unsigned char* prelocs,
4499 size_t reloc_count,
4500 Output_section* output_section,
4501 bool needs_special_offset_handling,
4502 size_t local_symbol_count,
4503 const unsigned char* plocal_symbols)
4505 typedef Target_arm<big_endian> Arm;
4506 typedef typename Target_arm<big_endian>::Scan Scan;
4508 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4509 symtab,
4510 layout,
4511 this,
4512 object,
4513 data_shndx,
4514 prelocs,
4515 reloc_count,
4516 output_section,
4517 needs_special_offset_handling,
4518 local_symbol_count,
4519 plocal_symbols);
4522 // Scan relocations for a section.
4524 template<bool big_endian>
4525 void
4526 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
4527 Layout* layout,
4528 Sized_relobj<32, big_endian>* object,
4529 unsigned int data_shndx,
4530 unsigned int sh_type,
4531 const unsigned char* prelocs,
4532 size_t reloc_count,
4533 Output_section* output_section,
4534 bool needs_special_offset_handling,
4535 size_t local_symbol_count,
4536 const unsigned char* plocal_symbols)
4538 typedef typename Target_arm<big_endian>::Scan Scan;
4539 if (sh_type == elfcpp::SHT_RELA)
4541 gold_error(_("%s: unsupported RELA reloc section"),
4542 object->name().c_str());
4543 return;
4546 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4547 symtab,
4548 layout,
4549 this,
4550 object,
4551 data_shndx,
4552 prelocs,
4553 reloc_count,
4554 output_section,
4555 needs_special_offset_handling,
4556 local_symbol_count,
4557 plocal_symbols);
4560 // Finalize the sections.
4562 template<bool big_endian>
4563 void
4564 Target_arm<big_endian>::do_finalize_sections(
4565 Layout* layout,
4566 const Input_objects* input_objects,
4567 Symbol_table* symtab)
4569 // Merge processor-specific flags.
4570 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4571 p != input_objects->relobj_end();
4572 ++p)
4574 Arm_relobj<big_endian>* arm_relobj =
4575 Arm_relobj<big_endian>::as_arm_relobj(*p);
4576 this->merge_processor_specific_flags(
4577 arm_relobj->name(),
4578 arm_relobj->processor_specific_flags());
4581 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
4582 p != input_objects->dynobj_end();
4583 ++p)
4585 Arm_dynobj<big_endian>* arm_dynobj =
4586 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
4587 this->merge_processor_specific_flags(
4588 arm_dynobj->name(),
4589 arm_dynobj->processor_specific_flags());
4592 // Fill in some more dynamic tags.
4593 Output_data_dynamic* const odyn = layout->dynamic_data();
4594 if (odyn != NULL)
4596 if (this->got_plt_ != NULL
4597 && this->got_plt_->output_section() != NULL)
4598 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
4600 if (this->plt_ != NULL
4601 && this->plt_->output_section() != NULL)
4603 const Output_data* od = this->plt_->rel_plt();
4604 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
4605 odyn->add_section_address(elfcpp::DT_JMPREL, od);
4606 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
4609 if (this->rel_dyn_ != NULL
4610 && this->rel_dyn_->output_section() != NULL)
4612 const Output_data* od = this->rel_dyn_;
4613 odyn->add_section_address(elfcpp::DT_REL, od);
4614 odyn->add_section_size(elfcpp::DT_RELSZ, od);
4615 odyn->add_constant(elfcpp::DT_RELENT,
4616 elfcpp::Elf_sizes<32>::rel_size);
4619 if (!parameters->options().shared())
4621 // The value of the DT_DEBUG tag is filled in by the dynamic
4622 // linker at run time, and used by the debugger.
4623 odyn->add_constant(elfcpp::DT_DEBUG, 0);
4627 // Emit any relocs we saved in an attempt to avoid generating COPY
4628 // relocs.
4629 if (this->copy_relocs_.any_saved_relocs())
4630 this->copy_relocs_.emit(this->rel_dyn_section(layout));
4632 // Handle the .ARM.exidx section.
4633 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
4634 if (exidx_section != NULL
4635 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
4636 && !parameters->options().relocatable())
4638 // Create __exidx_start and __exdix_end symbols.
4639 symtab->define_in_output_data("__exidx_start", NULL, exidx_section,
4640 0, 0, elfcpp::STT_OBJECT,
4641 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
4642 false, false);
4643 symtab->define_in_output_data("__exidx_end", NULL, exidx_section,
4644 0, 0, elfcpp::STT_OBJECT,
4645 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
4646 true, false);
4648 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4649 // the .ARM.exidx section.
4650 if (!layout->script_options()->saw_phdrs_clause())
4652 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
4653 == NULL);
4654 Output_segment* exidx_segment =
4655 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
4656 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
4657 false);
4662 // Return whether a direct absolute static relocation needs to be applied.
4663 // In cases where Scan::local() or Scan::global() has created
4664 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4665 // of the relocation is carried in the data, and we must not
4666 // apply the static relocation.
4668 template<bool big_endian>
4669 inline bool
4670 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
4671 const Sized_symbol<32>* gsym,
4672 int ref_flags,
4673 bool is_32bit,
4674 Output_section* output_section)
4676 // If the output section is not allocated, then we didn't call
4677 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4678 // the reloc here.
4679 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
4680 return true;
4682 // For local symbols, we will have created a non-RELATIVE dynamic
4683 // relocation only if (a) the output is position independent,
4684 // (b) the relocation is absolute (not pc- or segment-relative), and
4685 // (c) the relocation is not 32 bits wide.
4686 if (gsym == NULL)
4687 return !(parameters->options().output_is_position_independent()
4688 && (ref_flags & Symbol::ABSOLUTE_REF)
4689 && !is_32bit);
4691 // For global symbols, we use the same helper routines used in the
4692 // scan pass. If we did not create a dynamic relocation, or if we
4693 // created a RELATIVE dynamic relocation, we should apply the static
4694 // relocation.
4695 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
4696 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
4697 && gsym->can_use_relative_reloc(ref_flags
4698 & Symbol::FUNCTION_CALL);
4699 return !has_dyn || is_rel;
4702 // Perform a relocation.
4704 template<bool big_endian>
4705 inline bool
4706 Target_arm<big_endian>::Relocate::relocate(
4707 const Relocate_info<32, big_endian>* relinfo,
4708 Target_arm* target,
4709 Output_section *output_section,
4710 size_t relnum,
4711 const elfcpp::Rel<32, big_endian>& rel,
4712 unsigned int r_type,
4713 const Sized_symbol<32>* gsym,
4714 const Symbol_value<32>* psymval,
4715 unsigned char* view,
4716 Arm_address address,
4717 section_size_type /* view_size */ )
4719 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
4721 r_type = get_real_reloc_type(r_type);
4723 const Arm_relobj<big_endian>* object =
4724 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
4726 // If the final branch target of a relocation is THUMB instruction, this
4727 // is 1. Otherwise it is 0.
4728 Arm_address thumb_bit = 0;
4729 Symbol_value<32> symval;
4730 bool is_weakly_undefined_without_plt = false;
4731 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
4733 if (gsym != NULL)
4735 // This is a global symbol. Determine if we use PLT and if the
4736 // final target is THUMB.
4737 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
4739 // This uses a PLT, change the symbol value.
4740 symval.set_output_value(target->plt_section()->address()
4741 + gsym->plt_offset());
4742 psymval = &symval;
4744 else if (gsym->is_weak_undefined())
4746 // This is a weakly undefined symbol and we do not use PLT
4747 // for this relocation. A branch targeting this symbol will
4748 // be converted into an NOP.
4749 is_weakly_undefined_without_plt = true;
4751 else
4753 // Set thumb bit if symbol:
4754 // -Has type STT_ARM_TFUNC or
4755 // -Has type STT_FUNC, is defined and with LSB in value set.
4756 thumb_bit =
4757 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
4758 || (gsym->type() == elfcpp::STT_FUNC
4759 && !gsym->is_undefined()
4760 && ((psymval->value(object, 0) & 1) != 0)))
4762 : 0);
4765 else
4767 // This is a local symbol. Determine if the final target is THUMB.
4768 // We saved this information when all the local symbols were read.
4769 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
4770 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
4771 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
4774 else
4776 // This is a fake relocation synthesized for a stub. It does not have
4777 // a real symbol. We just look at the LSB of the symbol value to
4778 // determine if the target is THUMB or not.
4779 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
4782 // Strip LSB if this points to a THUMB target.
4783 if (thumb_bit != 0
4784 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
4785 && ((psymval->value(object, 0) & 1) != 0))
4787 Arm_address stripped_value =
4788 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
4789 symval.set_output_value(stripped_value);
4790 psymval = &symval;
4793 // Get the GOT offset if needed.
4794 // The GOT pointer points to the end of the GOT section.
4795 // We need to subtract the size of the GOT section to get
4796 // the actual offset to use in the relocation.
4797 bool have_got_offset = false;
4798 unsigned int got_offset = 0;
4799 switch (r_type)
4801 case elfcpp::R_ARM_GOT_BREL:
4802 case elfcpp::R_ARM_GOT_PREL:
4803 if (gsym != NULL)
4805 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4806 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4807 - target->got_size());
4809 else
4811 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
4812 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4813 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4814 - target->got_size());
4816 have_got_offset = true;
4817 break;
4819 default:
4820 break;
4823 // To look up relocation stubs, we need to pass the symbol table index of
4824 // a local symbol.
4825 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
4827 typename Arm_relocate_functions::Status reloc_status =
4828 Arm_relocate_functions::STATUS_OKAY;
4829 switch (r_type)
4831 case elfcpp::R_ARM_NONE:
4832 break;
4834 case elfcpp::R_ARM_ABS8:
4835 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4836 output_section))
4837 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
4838 break;
4840 case elfcpp::R_ARM_ABS12:
4841 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4842 output_section))
4843 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
4844 break;
4846 case elfcpp::R_ARM_ABS16:
4847 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4848 output_section))
4849 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
4850 break;
4852 case elfcpp::R_ARM_ABS32:
4853 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4854 output_section))
4855 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4856 thumb_bit);
4857 break;
4859 case elfcpp::R_ARM_ABS32_NOI:
4860 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4861 output_section))
4862 // No thumb bit for this relocation: (S + A)
4863 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4865 break;
4867 case elfcpp::R_ARM_MOVW_ABS_NC:
4868 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4869 output_section))
4870 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
4871 psymval,
4872 thumb_bit);
4873 else
4874 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4875 "a shared object; recompile with -fPIC"));
4876 break;
4878 case elfcpp::R_ARM_MOVT_ABS:
4879 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4880 output_section))
4881 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
4882 else
4883 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4884 "a shared object; recompile with -fPIC"));
4885 break;
4887 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4888 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4889 output_section))
4890 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
4891 psymval,
4892 thumb_bit);
4893 else
4894 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4895 "making a shared object; recompile with -fPIC"));
4896 break;
4898 case elfcpp::R_ARM_THM_MOVT_ABS:
4899 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4900 output_section))
4901 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
4902 psymval);
4903 else
4904 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4905 "making a shared object; recompile with -fPIC"));
4906 break;
4908 case elfcpp::R_ARM_MOVW_PREL_NC:
4909 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
4910 psymval, address,
4911 thumb_bit);
4912 break;
4914 case elfcpp::R_ARM_MOVT_PREL:
4915 reloc_status = Arm_relocate_functions::movt_prel(view, object,
4916 psymval, address);
4917 break;
4919 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4920 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
4921 psymval, address,
4922 thumb_bit);
4923 break;
4925 case elfcpp::R_ARM_THM_MOVT_PREL:
4926 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
4927 psymval, address);
4928 break;
4930 case elfcpp::R_ARM_REL32:
4931 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4932 address, thumb_bit);
4933 break;
4935 case elfcpp::R_ARM_THM_ABS5:
4936 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4937 output_section))
4938 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
4939 break;
4941 case elfcpp::R_ARM_THM_CALL:
4942 reloc_status =
4943 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
4944 psymval, address, thumb_bit,
4945 is_weakly_undefined_without_plt);
4946 break;
4948 case elfcpp::R_ARM_XPC25:
4949 reloc_status =
4950 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
4951 psymval, address, thumb_bit,
4952 is_weakly_undefined_without_plt);
4953 break;
4955 case elfcpp::R_ARM_THM_XPC22:
4956 reloc_status =
4957 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
4958 psymval, address, thumb_bit,
4959 is_weakly_undefined_without_plt);
4960 break;
4962 case elfcpp::R_ARM_GOTOFF32:
4964 Arm_address got_origin;
4965 got_origin = target->got_plt_section()->address();
4966 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4967 got_origin, thumb_bit);
4969 break;
4971 case elfcpp::R_ARM_BASE_PREL:
4973 uint32_t origin;
4974 // Get the addressing origin of the output segment defining the
4975 // symbol gsym (AAELF 4.6.1.2 Relocation types)
4976 gold_assert(gsym != NULL);
4977 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4978 origin = gsym->output_segment()->vaddr();
4979 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4980 origin = gsym->output_data()->address();
4981 else
4983 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4984 _("cannot find origin of R_ARM_BASE_PREL"));
4985 return true;
4987 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
4989 break;
4991 case elfcpp::R_ARM_BASE_ABS:
4993 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4994 output_section))
4995 break;
4997 uint32_t origin;
4998 // Get the addressing origin of the output segment defining
4999 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5000 if (gsym == NULL)
5001 // R_ARM_BASE_ABS with the NULL symbol will give the
5002 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5003 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5004 origin = target->got_plt_section()->address();
5005 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5006 origin = gsym->output_segment()->vaddr();
5007 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5008 origin = gsym->output_data()->address();
5009 else
5011 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5012 _("cannot find origin of R_ARM_BASE_ABS"));
5013 return true;
5016 reloc_status = Arm_relocate_functions::base_abs(view, origin);
5018 break;
5020 case elfcpp::R_ARM_GOT_BREL:
5021 gold_assert(have_got_offset);
5022 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
5023 break;
5025 case elfcpp::R_ARM_GOT_PREL:
5026 gold_assert(have_got_offset);
5027 // Get the address origin for GOT PLT, which is allocated right
5028 // after the GOT section, to calculate an absolute address of
5029 // the symbol GOT entry (got_origin + got_offset).
5030 Arm_address got_origin;
5031 got_origin = target->got_plt_section()->address();
5032 reloc_status = Arm_relocate_functions::got_prel(view,
5033 got_origin + got_offset,
5034 address);
5035 break;
5037 case elfcpp::R_ARM_PLT32:
5038 gold_assert(gsym == NULL
5039 || gsym->has_plt_offset()
5040 || gsym->final_value_is_known()
5041 || (gsym->is_defined()
5042 && !gsym->is_from_dynobj()
5043 && !gsym->is_preemptible()));
5044 reloc_status =
5045 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
5046 psymval, address, thumb_bit,
5047 is_weakly_undefined_without_plt);
5048 break;
5050 case elfcpp::R_ARM_CALL:
5051 reloc_status =
5052 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
5053 psymval, address, thumb_bit,
5054 is_weakly_undefined_without_plt);
5055 break;
5057 case elfcpp::R_ARM_JUMP24:
5058 reloc_status =
5059 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
5060 psymval, address, thumb_bit,
5061 is_weakly_undefined_without_plt);
5062 break;
5064 case elfcpp::R_ARM_THM_JUMP24:
5065 reloc_status =
5066 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
5067 psymval, address, thumb_bit,
5068 is_weakly_undefined_without_plt);
5069 break;
5071 case elfcpp::R_ARM_PREL31:
5072 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
5073 address, thumb_bit);
5074 break;
5076 case elfcpp::R_ARM_TARGET1:
5077 // This should have been mapped to another type already.
5078 // Fall through.
5079 case elfcpp::R_ARM_COPY:
5080 case elfcpp::R_ARM_GLOB_DAT:
5081 case elfcpp::R_ARM_JUMP_SLOT:
5082 case elfcpp::R_ARM_RELATIVE:
5083 // These are relocations which should only be seen by the
5084 // dynamic linker, and should never be seen here.
5085 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5086 _("unexpected reloc %u in object file"),
5087 r_type);
5088 break;
5090 default:
5091 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5092 _("unsupported reloc %u"),
5093 r_type);
5094 break;
5097 // Report any errors.
5098 switch (reloc_status)
5100 case Arm_relocate_functions::STATUS_OKAY:
5101 break;
5102 case Arm_relocate_functions::STATUS_OVERFLOW:
5103 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5104 _("relocation overflow in relocation %u"),
5105 r_type);
5106 break;
5107 case Arm_relocate_functions::STATUS_BAD_RELOC:
5108 gold_error_at_location(
5109 relinfo,
5110 relnum,
5111 rel.get_r_offset(),
5112 _("unexpected opcode while processing relocation %u"),
5113 r_type);
5114 break;
5115 default:
5116 gold_unreachable();
5119 return true;
5122 // Relocate section data.
5124 template<bool big_endian>
5125 void
5126 Target_arm<big_endian>::relocate_section(
5127 const Relocate_info<32, big_endian>* relinfo,
5128 unsigned int sh_type,
5129 const unsigned char* prelocs,
5130 size_t reloc_count,
5131 Output_section* output_section,
5132 bool needs_special_offset_handling,
5133 unsigned char* view,
5134 Arm_address address,
5135 section_size_type view_size,
5136 const Reloc_symbol_changes* reloc_symbol_changes)
5138 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
5139 gold_assert(sh_type == elfcpp::SHT_REL);
5141 Arm_input_section<big_endian>* arm_input_section =
5142 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
5144 // This is an ARM input section and the view covers the whole output
5145 // section.
5146 if (arm_input_section != NULL)
5148 gold_assert(needs_special_offset_handling);
5149 Arm_address section_address = arm_input_section->address();
5150 section_size_type section_size = arm_input_section->data_size();
5152 gold_assert((arm_input_section->address() >= address)
5153 && ((arm_input_section->address()
5154 + arm_input_section->data_size())
5155 <= (address + view_size)));
5157 off_t offset = section_address - address;
5158 view += offset;
5159 address += offset;
5160 view_size = section_size;
5163 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
5164 Arm_relocate>(
5165 relinfo,
5166 this,
5167 prelocs,
5168 reloc_count,
5169 output_section,
5170 needs_special_offset_handling,
5171 view,
5172 address,
5173 view_size,
5174 reloc_symbol_changes);
5177 // Return the size of a relocation while scanning during a relocatable
5178 // link.
5180 template<bool big_endian>
5181 unsigned int
5182 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
5183 unsigned int r_type,
5184 Relobj* object)
5186 r_type = get_real_reloc_type(r_type);
5187 switch (r_type)
5189 case elfcpp::R_ARM_NONE:
5190 return 0;
5192 case elfcpp::R_ARM_ABS8:
5193 return 1;
5195 case elfcpp::R_ARM_ABS16:
5196 case elfcpp::R_ARM_THM_ABS5:
5197 return 2;
5199 case elfcpp::R_ARM_ABS32:
5200 case elfcpp::R_ARM_ABS32_NOI:
5201 case elfcpp::R_ARM_ABS12:
5202 case elfcpp::R_ARM_BASE_ABS:
5203 case elfcpp::R_ARM_REL32:
5204 case elfcpp::R_ARM_THM_CALL:
5205 case elfcpp::R_ARM_GOTOFF32:
5206 case elfcpp::R_ARM_BASE_PREL:
5207 case elfcpp::R_ARM_GOT_BREL:
5208 case elfcpp::R_ARM_GOT_PREL:
5209 case elfcpp::R_ARM_PLT32:
5210 case elfcpp::R_ARM_CALL:
5211 case elfcpp::R_ARM_JUMP24:
5212 case elfcpp::R_ARM_PREL31:
5213 case elfcpp::R_ARM_MOVW_ABS_NC:
5214 case elfcpp::R_ARM_MOVT_ABS:
5215 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5216 case elfcpp::R_ARM_THM_MOVT_ABS:
5217 case elfcpp::R_ARM_MOVW_PREL_NC:
5218 case elfcpp::R_ARM_MOVT_PREL:
5219 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5220 case elfcpp::R_ARM_THM_MOVT_PREL:
5221 return 4;
5223 case elfcpp::R_ARM_TARGET1:
5224 // This should have been mapped to another type already.
5225 // Fall through.
5226 case elfcpp::R_ARM_COPY:
5227 case elfcpp::R_ARM_GLOB_DAT:
5228 case elfcpp::R_ARM_JUMP_SLOT:
5229 case elfcpp::R_ARM_RELATIVE:
5230 // These are relocations which should only be seen by the
5231 // dynamic linker, and should never be seen here.
5232 gold_error(_("%s: unexpected reloc %u in object file"),
5233 object->name().c_str(), r_type);
5234 return 0;
5236 default:
5237 object->error(_("unsupported reloc %u in object file"), r_type);
5238 return 0;
5242 // Scan the relocs during a relocatable link.
5244 template<bool big_endian>
5245 void
5246 Target_arm<big_endian>::scan_relocatable_relocs(
5247 Symbol_table* symtab,
5248 Layout* layout,
5249 Sized_relobj<32, big_endian>* object,
5250 unsigned int data_shndx,
5251 unsigned int sh_type,
5252 const unsigned char* prelocs,
5253 size_t reloc_count,
5254 Output_section* output_section,
5255 bool needs_special_offset_handling,
5256 size_t local_symbol_count,
5257 const unsigned char* plocal_symbols,
5258 Relocatable_relocs* rr)
5260 gold_assert(sh_type == elfcpp::SHT_REL);
5262 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
5263 Relocatable_size_for_reloc> Scan_relocatable_relocs;
5265 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
5266 Scan_relocatable_relocs>(
5267 symtab,
5268 layout,
5269 object,
5270 data_shndx,
5271 prelocs,
5272 reloc_count,
5273 output_section,
5274 needs_special_offset_handling,
5275 local_symbol_count,
5276 plocal_symbols,
5277 rr);
5280 // Relocate a section during a relocatable link.
5282 template<bool big_endian>
5283 void
5284 Target_arm<big_endian>::relocate_for_relocatable(
5285 const Relocate_info<32, big_endian>* relinfo,
5286 unsigned int sh_type,
5287 const unsigned char* prelocs,
5288 size_t reloc_count,
5289 Output_section* output_section,
5290 off_t offset_in_output_section,
5291 const Relocatable_relocs* rr,
5292 unsigned char* view,
5293 Arm_address view_address,
5294 section_size_type view_size,
5295 unsigned char* reloc_view,
5296 section_size_type reloc_view_size)
5298 gold_assert(sh_type == elfcpp::SHT_REL);
5300 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
5301 relinfo,
5302 prelocs,
5303 reloc_count,
5304 output_section,
5305 offset_in_output_section,
5307 view,
5308 view_address,
5309 view_size,
5310 reloc_view,
5311 reloc_view_size);
5314 // Return the value to use for a dynamic symbol which requires special
5315 // treatment. This is how we support equality comparisons of function
5316 // pointers across shared library boundaries, as described in the
5317 // processor specific ABI supplement.
5319 template<bool big_endian>
5320 uint64_t
5321 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
5323 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5324 return this->plt_section()->address() + gsym->plt_offset();
5327 // Map platform-specific relocs to real relocs
5329 template<bool big_endian>
5330 unsigned int
5331 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
5333 switch (r_type)
5335 case elfcpp::R_ARM_TARGET1:
5336 // This is either R_ARM_ABS32 or R_ARM_REL32;
5337 return elfcpp::R_ARM_ABS32;
5339 case elfcpp::R_ARM_TARGET2:
5340 // This can be any reloc type but ususally is R_ARM_GOT_PREL
5341 return elfcpp::R_ARM_GOT_PREL;
5343 default:
5344 return r_type;
5348 // Whether if two EABI versions V1 and V2 are compatible.
5350 template<bool big_endian>
5351 bool
5352 Target_arm<big_endian>::are_eabi_versions_compatible(
5353 elfcpp::Elf_Word v1,
5354 elfcpp::Elf_Word v2)
5356 // v4 and v5 are the same spec before and after it was released,
5357 // so allow mixing them.
5358 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
5359 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
5360 return true;
5362 return v1 == v2;
5365 // Combine FLAGS from an input object called NAME and the processor-specific
5366 // flags in the ELF header of the output. Much of this is adapted from the
5367 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5368 // in bfd/elf32-arm.c.
5370 template<bool big_endian>
5371 void
5372 Target_arm<big_endian>::merge_processor_specific_flags(
5373 const std::string& name,
5374 elfcpp::Elf_Word flags)
5376 if (this->are_processor_specific_flags_set())
5378 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
5380 // Nothing to merge if flags equal to those in output.
5381 if (flags == out_flags)
5382 return;
5384 // Complain about various flag mismatches.
5385 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
5386 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
5387 if (!this->are_eabi_versions_compatible(version1, version2))
5388 gold_error(_("Source object %s has EABI version %d but output has "
5389 "EABI version %d."),
5390 name.c_str(),
5391 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
5392 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
5394 else
5396 // If the input is the default architecture and had the default
5397 // flags then do not bother setting the flags for the output
5398 // architecture, instead allow future merges to do this. If no
5399 // future merges ever set these flags then they will retain their
5400 // uninitialised values, which surprise surprise, correspond
5401 // to the default values.
5402 if (flags == 0)
5403 return;
5405 // This is the first time, just copy the flags.
5406 // We only copy the EABI version for now.
5407 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
5411 // Adjust ELF file header.
5412 template<bool big_endian>
5413 void
5414 Target_arm<big_endian>::do_adjust_elf_header(
5415 unsigned char* view,
5416 int len) const
5418 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
5420 elfcpp::Ehdr<32, big_endian> ehdr(view);
5421 unsigned char e_ident[elfcpp::EI_NIDENT];
5422 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
5424 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5425 == elfcpp::EF_ARM_EABI_UNKNOWN)
5426 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
5427 else
5428 e_ident[elfcpp::EI_OSABI] = 0;
5429 e_ident[elfcpp::EI_ABIVERSION] = 0;
5431 // FIXME: Do EF_ARM_BE8 adjustment.
5433 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
5434 oehdr.put_e_ident(e_ident);
5437 // do_make_elf_object to override the same function in the base class.
5438 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5439 // to store ARM specific information. Hence we need to have our own
5440 // ELF object creation.
5442 template<bool big_endian>
5443 Object*
5444 Target_arm<big_endian>::do_make_elf_object(
5445 const std::string& name,
5446 Input_file* input_file,
5447 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
5449 int et = ehdr.get_e_type();
5450 if (et == elfcpp::ET_REL)
5452 Arm_relobj<big_endian>* obj =
5453 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
5454 obj->setup();
5455 return obj;
5457 else if (et == elfcpp::ET_DYN)
5459 Sized_dynobj<32, big_endian>* obj =
5460 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
5461 obj->setup();
5462 return obj;
5464 else
5466 gold_error(_("%s: unsupported ELF file type %d"),
5467 name.c_str(), et);
5468 return NULL;
5472 // Return whether a relocation type used the LSB to distinguish THUMB
5473 // addresses.
5474 template<bool big_endian>
5475 bool
5476 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
5478 switch (r_type)
5480 case elfcpp::R_ARM_PC24:
5481 case elfcpp::R_ARM_ABS32:
5482 case elfcpp::R_ARM_REL32:
5483 case elfcpp::R_ARM_SBREL32:
5484 case elfcpp::R_ARM_THM_CALL:
5485 case elfcpp::R_ARM_GLOB_DAT:
5486 case elfcpp::R_ARM_JUMP_SLOT:
5487 case elfcpp::R_ARM_GOTOFF32:
5488 case elfcpp::R_ARM_PLT32:
5489 case elfcpp::R_ARM_CALL:
5490 case elfcpp::R_ARM_JUMP24:
5491 case elfcpp::R_ARM_THM_JUMP24:
5492 case elfcpp::R_ARM_SBREL31:
5493 case elfcpp::R_ARM_PREL31:
5494 case elfcpp::R_ARM_MOVW_ABS_NC:
5495 case elfcpp::R_ARM_MOVW_PREL_NC:
5496 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5497 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5498 case elfcpp::R_ARM_THM_JUMP19:
5499 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
5500 case elfcpp::R_ARM_ALU_PC_G0_NC:
5501 case elfcpp::R_ARM_ALU_PC_G0:
5502 case elfcpp::R_ARM_ALU_PC_G1_NC:
5503 case elfcpp::R_ARM_ALU_PC_G1:
5504 case elfcpp::R_ARM_ALU_PC_G2:
5505 case elfcpp::R_ARM_ALU_SB_G0_NC:
5506 case elfcpp::R_ARM_ALU_SB_G0:
5507 case elfcpp::R_ARM_ALU_SB_G1_NC:
5508 case elfcpp::R_ARM_ALU_SB_G1:
5509 case elfcpp::R_ARM_ALU_SB_G2:
5510 case elfcpp::R_ARM_MOVW_BREL_NC:
5511 case elfcpp::R_ARM_MOVW_BREL:
5512 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
5513 case elfcpp::R_ARM_THM_MOVW_BREL:
5514 return true;
5515 default:
5516 return false;
5520 // Stub-generation methods for Target_arm.
5522 // Make a new Arm_input_section object.
5524 template<bool big_endian>
5525 Arm_input_section<big_endian>*
5526 Target_arm<big_endian>::new_arm_input_section(
5527 Relobj* relobj,
5528 unsigned int shndx)
5530 Input_section_specifier iss(relobj, shndx);
5532 Arm_input_section<big_endian>* arm_input_section =
5533 new Arm_input_section<big_endian>(relobj, shndx);
5534 arm_input_section->init();
5536 // Register new Arm_input_section in map for look-up.
5537 std::pair<typename Arm_input_section_map::iterator, bool> ins =
5538 this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
5540 // Make sure that it we have not created another Arm_input_section
5541 // for this input section already.
5542 gold_assert(ins.second);
5544 return arm_input_section;
5547 // Find the Arm_input_section object corresponding to the SHNDX-th input
5548 // section of RELOBJ.
5550 template<bool big_endian>
5551 Arm_input_section<big_endian>*
5552 Target_arm<big_endian>::find_arm_input_section(
5553 Relobj* relobj,
5554 unsigned int shndx) const
5556 Input_section_specifier iss(relobj, shndx);
5557 typename Arm_input_section_map::const_iterator p =
5558 this->arm_input_section_map_.find(iss);
5559 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
5562 // Make a new stub table.
5564 template<bool big_endian>
5565 Stub_table<big_endian>*
5566 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
5568 Stub_table<big_endian>* stub_table =
5569 new Stub_table<big_endian>(owner);
5570 this->stub_tables_.push_back(stub_table);
5572 stub_table->set_address(owner->address() + owner->data_size());
5573 stub_table->set_file_offset(owner->offset() + owner->data_size());
5574 stub_table->finalize_data_size();
5576 return stub_table;
5579 // Scan a relocation for stub generation.
5581 template<bool big_endian>
5582 void
5583 Target_arm<big_endian>::scan_reloc_for_stub(
5584 const Relocate_info<32, big_endian>* relinfo,
5585 unsigned int r_type,
5586 const Sized_symbol<32>* gsym,
5587 unsigned int r_sym,
5588 const Symbol_value<32>* psymval,
5589 elfcpp::Elf_types<32>::Elf_Swxword addend,
5590 Arm_address address)
5592 typedef typename Target_arm<big_endian>::Relocate Relocate;
5594 const Arm_relobj<big_endian>* arm_relobj =
5595 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
5597 bool target_is_thumb;
5598 Symbol_value<32> symval;
5599 if (gsym != NULL)
5601 // This is a global symbol. Determine if we use PLT and if the
5602 // final target is THUMB.
5603 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
5605 // This uses a PLT, change the symbol value.
5606 symval.set_output_value(this->plt_section()->address()
5607 + gsym->plt_offset());
5608 psymval = &symval;
5609 target_is_thumb = false;
5611 else if (gsym->is_undefined())
5612 // There is no need to generate a stub symbol is undefined.
5613 return;
5614 else
5616 target_is_thumb =
5617 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
5618 || (gsym->type() == elfcpp::STT_FUNC
5619 && !gsym->is_undefined()
5620 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
5623 else
5625 // This is a local symbol. Determine if the final target is THUMB.
5626 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
5629 // Strip LSB if this points to a THUMB target.
5630 if (target_is_thumb
5631 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
5632 && ((psymval->value(arm_relobj, 0) & 1) != 0))
5634 Arm_address stripped_value =
5635 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
5636 symval.set_output_value(stripped_value);
5637 psymval = &symval;
5640 // Get the symbol value.
5641 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
5643 // Owing to pipelining, the PC relative branches below actually skip
5644 // two instructions when the branch offset is 0.
5645 Arm_address destination;
5646 switch (r_type)
5648 case elfcpp::R_ARM_CALL:
5649 case elfcpp::R_ARM_JUMP24:
5650 case elfcpp::R_ARM_PLT32:
5651 // ARM branches.
5652 destination = value + addend + 8;
5653 break;
5654 case elfcpp::R_ARM_THM_CALL:
5655 case elfcpp::R_ARM_THM_XPC22:
5656 case elfcpp::R_ARM_THM_JUMP24:
5657 case elfcpp::R_ARM_THM_JUMP19:
5658 // THUMB branches.
5659 destination = value + addend + 4;
5660 break;
5661 default:
5662 gold_unreachable();
5665 Stub_type stub_type =
5666 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
5667 target_is_thumb);
5669 // This reloc does not need a stub.
5670 if (stub_type == arm_stub_none)
5671 return;
5673 // Try looking up an existing stub from a stub table.
5674 Stub_table<big_endian>* stub_table =
5675 arm_relobj->stub_table(relinfo->data_shndx);
5676 gold_assert(stub_table != NULL);
5678 // Locate stub by destination.
5679 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
5681 // Create a stub if there is not one already
5682 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
5683 if (stub == NULL)
5685 // create a new stub and add it to stub table.
5686 stub = this->stub_factory().make_reloc_stub(stub_type);
5687 stub_table->add_reloc_stub(stub, stub_key);
5690 // Record the destination address.
5691 stub->set_destination_address(destination
5692 | (target_is_thumb ? 1 : 0));
5695 // This function scans a relocation sections for stub generation.
5696 // The template parameter Relocate must be a class type which provides
5697 // a single function, relocate(), which implements the machine
5698 // specific part of a relocation.
5700 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
5701 // SHT_REL or SHT_RELA.
5703 // PRELOCS points to the relocation data. RELOC_COUNT is the number
5704 // of relocs. OUTPUT_SECTION is the output section.
5705 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
5706 // mapped to output offsets.
5708 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
5709 // VIEW_SIZE is the size. These refer to the input section, unless
5710 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
5711 // the output section.
5713 template<bool big_endian>
5714 template<int sh_type>
5715 void inline
5716 Target_arm<big_endian>::scan_reloc_section_for_stubs(
5717 const Relocate_info<32, big_endian>* relinfo,
5718 const unsigned char* prelocs,
5719 size_t reloc_count,
5720 Output_section* output_section,
5721 bool needs_special_offset_handling,
5722 const unsigned char* view,
5723 elfcpp::Elf_types<32>::Elf_Addr view_address,
5724 section_size_type)
5726 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
5727 const int reloc_size =
5728 Reloc_types<sh_type, 32, big_endian>::reloc_size;
5730 Arm_relobj<big_endian>* arm_object =
5731 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
5732 unsigned int local_count = arm_object->local_symbol_count();
5734 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
5736 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
5738 Reltype reloc(prelocs);
5740 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
5741 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
5742 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
5744 r_type = this->get_real_reloc_type(r_type);
5746 // Only a few relocation types need stubs.
5747 if ((r_type != elfcpp::R_ARM_CALL)
5748 && (r_type != elfcpp::R_ARM_JUMP24)
5749 && (r_type != elfcpp::R_ARM_PLT32)
5750 && (r_type != elfcpp::R_ARM_THM_CALL)
5751 && (r_type != elfcpp::R_ARM_THM_XPC22)
5752 && (r_type != elfcpp::R_ARM_THM_JUMP24)
5753 && (r_type != elfcpp::R_ARM_THM_JUMP19))
5754 continue;
5756 section_offset_type offset =
5757 convert_to_section_size_type(reloc.get_r_offset());
5759 if (needs_special_offset_handling)
5761 offset = output_section->output_offset(relinfo->object,
5762 relinfo->data_shndx,
5763 offset);
5764 if (offset == -1)
5765 continue;
5768 // Get the addend.
5769 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
5770 elfcpp::Elf_types<32>::Elf_Swxword addend =
5771 stub_addend_reader(r_type, view + offset, reloc);
5773 const Sized_symbol<32>* sym;
5775 Symbol_value<32> symval;
5776 const Symbol_value<32> *psymval;
5777 if (r_sym < local_count)
5779 sym = NULL;
5780 psymval = arm_object->local_symbol(r_sym);
5782 // If the local symbol belongs to a section we are discarding,
5783 // and that section is a debug section, try to find the
5784 // corresponding kept section and map this symbol to its
5785 // counterpart in the kept section. The symbol must not
5786 // correspond to a section we are folding.
5787 bool is_ordinary;
5788 unsigned int shndx = psymval->input_shndx(&is_ordinary);
5789 if (is_ordinary
5790 && shndx != elfcpp::SHN_UNDEF
5791 && !arm_object->is_section_included(shndx)
5792 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
5794 if (comdat_behavior == CB_UNDETERMINED)
5796 std::string name =
5797 arm_object->section_name(relinfo->data_shndx);
5798 comdat_behavior = get_comdat_behavior(name.c_str());
5800 if (comdat_behavior == CB_PRETEND)
5802 bool found;
5803 typename elfcpp::Elf_types<32>::Elf_Addr value =
5804 arm_object->map_to_kept_section(shndx, &found);
5805 if (found)
5806 symval.set_output_value(value + psymval->input_value());
5807 else
5808 symval.set_output_value(0);
5810 else
5812 symval.set_output_value(0);
5814 symval.set_no_output_symtab_entry();
5815 psymval = &symval;
5818 else
5820 const Symbol* gsym = arm_object->global_symbol(r_sym);
5821 gold_assert(gsym != NULL);
5822 if (gsym->is_forwarder())
5823 gsym = relinfo->symtab->resolve_forwards(gsym);
5825 sym = static_cast<const Sized_symbol<32>*>(gsym);
5826 if (sym->has_symtab_index())
5827 symval.set_output_symtab_index(sym->symtab_index());
5828 else
5829 symval.set_no_output_symtab_entry();
5831 // We need to compute the would-be final value of this global
5832 // symbol.
5833 const Symbol_table* symtab = relinfo->symtab;
5834 const Sized_symbol<32>* sized_symbol =
5835 symtab->get_sized_symbol<32>(gsym);
5836 Symbol_table::Compute_final_value_status status;
5837 Arm_address value =
5838 symtab->compute_final_value<32>(sized_symbol, &status);
5840 // Skip this if the symbol has not output section.
5841 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
5842 continue;
5844 symval.set_output_value(value);
5845 psymval = &symval;
5848 // If symbol is a section symbol, we don't know the actual type of
5849 // destination. Give up.
5850 if (psymval->is_section_symbol())
5851 continue;
5853 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
5854 addend, view_address + offset);
5858 // Scan an input section for stub generation.
5860 template<bool big_endian>
5861 void
5862 Target_arm<big_endian>::scan_section_for_stubs(
5863 const Relocate_info<32, big_endian>* relinfo,
5864 unsigned int sh_type,
5865 const unsigned char* prelocs,
5866 size_t reloc_count,
5867 Output_section* output_section,
5868 bool needs_special_offset_handling,
5869 const unsigned char* view,
5870 Arm_address view_address,
5871 section_size_type view_size)
5873 if (sh_type == elfcpp::SHT_REL)
5874 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
5875 relinfo,
5876 prelocs,
5877 reloc_count,
5878 output_section,
5879 needs_special_offset_handling,
5880 view,
5881 view_address,
5882 view_size);
5883 else if (sh_type == elfcpp::SHT_RELA)
5884 // We do not support RELA type relocations yet. This is provided for
5885 // completeness.
5886 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
5887 relinfo,
5888 prelocs,
5889 reloc_count,
5890 output_section,
5891 needs_special_offset_handling,
5892 view,
5893 view_address,
5894 view_size);
5895 else
5896 gold_unreachable();
5899 // Group input sections for stub generation.
5901 // We goup input sections in an output sections so that the total size,
5902 // including any padding space due to alignment is smaller than GROUP_SIZE
5903 // unless the only input section in group is bigger than GROUP_SIZE already.
5904 // Then an ARM stub table is created to follow the last input section
5905 // in group. For each group an ARM stub table is created an is placed
5906 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
5907 // extend the group after the stub table.
5909 template<bool big_endian>
5910 void
5911 Target_arm<big_endian>::group_sections(
5912 Layout* layout,
5913 section_size_type group_size,
5914 bool stubs_always_after_branch)
5916 // Group input sections and insert stub table
5917 Layout::Section_list section_list;
5918 layout->get_allocated_sections(&section_list);
5919 for (Layout::Section_list::const_iterator p = section_list.begin();
5920 p != section_list.end();
5921 ++p)
5923 Arm_output_section<big_endian>* output_section =
5924 Arm_output_section<big_endian>::as_arm_output_section(*p);
5925 output_section->group_sections(group_size, stubs_always_after_branch,
5926 this);
5930 // Relaxation hook. This is where we do stub generation.
5932 template<bool big_endian>
5933 bool
5934 Target_arm<big_endian>::do_relax(
5935 int pass,
5936 const Input_objects* input_objects,
5937 Symbol_table* symtab,
5938 Layout* layout)
5940 // No need to generate stubs if this is a relocatable link.
5941 gold_assert(!parameters->options().relocatable());
5943 // If this is the first pass, we need to group input sections into
5944 // stub groups.
5945 if (pass == 1)
5947 // Determine the stub group size. The group size is the absolute
5948 // value of the parameter --stub-group-size. If --stub-group-size
5949 // is passed a negative value, we restict stubs to be always after
5950 // the stubbed branches.
5951 int32_t stub_group_size_param =
5952 parameters->options().stub_group_size();
5953 bool stubs_always_after_branch = stub_group_size_param < 0;
5954 section_size_type stub_group_size = abs(stub_group_size_param);
5956 if (stub_group_size == 1)
5958 // Default value.
5959 // Thumb branch range is +-4MB has to be used as the default
5960 // maximum size (a given section can contain both ARM and Thumb
5961 // code, so the worst case has to be taken into account).
5963 // This value is 24K less than that, which allows for 2025
5964 // 12-byte stubs. If we exceed that, then we will fail to link.
5965 // The user will have to relink with an explicit group size
5966 // option.
5967 stub_group_size = 4170000;
5970 group_sections(layout, stub_group_size, stubs_always_after_branch);
5973 // clear changed flags for all stub_tables
5974 typedef typename Stub_table_list::iterator Stub_table_iterator;
5975 for (Stub_table_iterator sp = this->stub_tables_.begin();
5976 sp != this->stub_tables_.end();
5977 ++sp)
5978 (*sp)->set_has_been_changed(false);
5980 // scan relocs for stubs
5981 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
5982 op != input_objects->relobj_end();
5983 ++op)
5985 Arm_relobj<big_endian>* arm_relobj =
5986 Arm_relobj<big_endian>::as_arm_relobj(*op);
5987 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
5990 bool any_stub_table_changed = false;
5991 for (Stub_table_iterator sp = this->stub_tables_.begin();
5992 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
5993 ++sp)
5995 if ((*sp)->has_been_changed())
5996 any_stub_table_changed = true;
5999 return any_stub_table_changed;
6002 // Relocate a stub.
6004 template<bool big_endian>
6005 void
6006 Target_arm<big_endian>::relocate_stub(
6007 Reloc_stub* stub,
6008 const Relocate_info<32, big_endian>* relinfo,
6009 Output_section* output_section,
6010 unsigned char* view,
6011 Arm_address address,
6012 section_size_type view_size)
6014 Relocate relocate;
6015 const Stub_template* stub_template = stub->stub_template();
6016 for (size_t i = 0; i < stub_template->reloc_count(); i++)
6018 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
6019 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
6021 unsigned int r_type = insn->r_type();
6022 section_size_type reloc_offset = stub_template->reloc_offset(i);
6023 section_size_type reloc_size = insn->size();
6024 gold_assert(reloc_offset + reloc_size <= view_size);
6026 // This is the address of the stub destination.
6027 Arm_address target = stub->reloc_target(i);
6028 Symbol_value<32> symval;
6029 symval.set_output_value(target);
6031 // Synthesize a fake reloc just in case. We don't have a symbol so
6032 // we use 0.
6033 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
6034 memset(reloc_buffer, 0, sizeof(reloc_buffer));
6035 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
6036 reloc_write.put_r_offset(reloc_offset);
6037 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
6038 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
6040 relocate.relocate(relinfo, this, output_section,
6041 this->fake_relnum_for_stubs, rel, r_type,
6042 NULL, &symval, view + reloc_offset,
6043 address + reloc_offset, reloc_size);
6047 // The selector for arm object files.
6049 template<bool big_endian>
6050 class Target_selector_arm : public Target_selector
6052 public:
6053 Target_selector_arm()
6054 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
6055 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
6058 Target*
6059 do_instantiate_target()
6060 { return new Target_arm<big_endian>(); }
6063 Target_selector_arm<false> target_selector_arm;
6064 Target_selector_arm<true> target_selector_armbe;
6066 } // End anonymous namespace.