* arm.cc (Arm_relocate_functions::thm_jump6): New function.
[binutils.git] / gold / arm.cc
blob1ab909e5e318fb9315740da169ed29a378ce14be
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>
33 #include <map>
34 #include <utility>
36 #include "elfcpp.h"
37 #include "parameters.h"
38 #include "reloc.h"
39 #include "arm.h"
40 #include "object.h"
41 #include "symtab.h"
42 #include "layout.h"
43 #include "output.h"
44 #include "copy-relocs.h"
45 #include "target.h"
46 #include "target-reloc.h"
47 #include "target-select.h"
48 #include "tls.h"
49 #include "defstd.h"
50 #include "gc.h"
51 #include "attributes.h"
53 namespace
56 using namespace gold;
58 template<bool big_endian>
59 class Output_data_plt_arm;
61 template<bool big_endian>
62 class Stub_table;
64 template<bool big_endian>
65 class Arm_input_section;
67 template<bool big_endian>
68 class Arm_output_section;
70 template<bool big_endian>
71 class Arm_relobj;
73 template<bool big_endian>
74 class Target_arm;
76 // For convenience.
77 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
79 // Maximum branch offsets for ARM, THUMB and THUMB2.
80 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
81 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
82 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
83 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
84 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
85 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
87 // The arm target class.
89 // This is a very simple port of gold for ARM-EABI. It is intended for
90 // supporting Android only for the time being. Only these relocation types
91 // are supported.
93 // R_ARM_NONE
94 // R_ARM_ABS32
95 // R_ARM_ABS32_NOI
96 // R_ARM_ABS16
97 // R_ARM_ABS12
98 // R_ARM_ABS8
99 // R_ARM_THM_ABS5
100 // R_ARM_BASE_ABS
101 // R_ARM_REL32
102 // R_ARM_THM_CALL
103 // R_ARM_COPY
104 // R_ARM_GLOB_DAT
105 // R_ARM_BASE_PREL
106 // R_ARM_JUMP_SLOT
107 // R_ARM_RELATIVE
108 // R_ARM_GOTOFF32
109 // R_ARM_GOT_BREL
110 // R_ARM_GOT_PREL
111 // R_ARM_PLT32
112 // R_ARM_CALL
113 // R_ARM_JUMP24
114 // R_ARM_TARGET1
115 // R_ARM_PREL31
116 // R_ARM_ABS8
117 // R_ARM_MOVW_ABS_NC
118 // R_ARM_MOVT_ABS
119 // R_ARM_THM_MOVW_ABS_NC
120 // R_ARM_THM_MOVT_ABS
121 // R_ARM_MOVW_PREL_NC
122 // R_ARM_MOVT_PREL
123 // R_ARM_THM_MOVW_PREL_NC
124 // R_ARM_THM_MOVT_PREL
125 // R_ARM_THM_JUMP6
126 // R_ARM_THM_JUMP8
127 // R_ARM_THM_JUMP11
129 // TODOs:
130 // - Support more relocation types as needed.
131 // - Make PLTs more flexible for different architecture features like
132 // Thumb-2 and BE8.
133 // There are probably a lot more.
135 // Instruction template class. This class is similar to the insn_sequence
136 // struct in bfd/elf32-arm.c.
138 class Insn_template
140 public:
141 // Types of instruction templates.
142 enum Type
144 THUMB16_TYPE = 1,
145 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
146 // templates with class-specific semantics. Currently this is used
147 // only by the Cortex_a8_stub class for handling condition codes in
148 // conditional branches.
149 THUMB16_SPECIAL_TYPE,
150 THUMB32_TYPE,
151 ARM_TYPE,
152 DATA_TYPE
155 // Factory methods to create instruction templates in different formats.
157 static const Insn_template
158 thumb16_insn(uint32_t data)
159 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
161 // A Thumb conditional branch, in which the proper condition is inserted
162 // when we build the stub.
163 static const Insn_template
164 thumb16_bcond_insn(uint32_t data)
165 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
167 static const Insn_template
168 thumb32_insn(uint32_t data)
169 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
171 static const Insn_template
172 thumb32_b_insn(uint32_t data, int reloc_addend)
174 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
175 reloc_addend);
178 static const Insn_template
179 arm_insn(uint32_t data)
180 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
182 static const Insn_template
183 arm_rel_insn(unsigned data, int reloc_addend)
184 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
186 static const Insn_template
187 data_word(unsigned data, unsigned int r_type, int reloc_addend)
188 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
190 // Accessors. This class is used for read-only objects so no modifiers
191 // are provided.
193 uint32_t
194 data() const
195 { return this->data_; }
197 // Return the instruction sequence type of this.
198 Type
199 type() const
200 { return this->type_; }
202 // Return the ARM relocation type of this.
203 unsigned int
204 r_type() const
205 { return this->r_type_; }
207 int32_t
208 reloc_addend() const
209 { return this->reloc_addend_; }
211 // Return size of instruction template in bytes.
212 size_t
213 size() const;
215 // Return byte-alignment of instruction template.
216 unsigned
217 alignment() const;
219 private:
220 // We make the constructor private to ensure that only the factory
221 // methods are used.
222 inline
223 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
224 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
227 // Instruction specific data. This is used to store information like
228 // some of the instruction bits.
229 uint32_t data_;
230 // Instruction template type.
231 Type type_;
232 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
233 unsigned int r_type_;
234 // Relocation addend.
235 int32_t reloc_addend_;
238 // Macro for generating code to stub types. One entry per long/short
239 // branch stub
241 #define DEF_STUBS \
242 DEF_STUB(long_branch_any_any) \
243 DEF_STUB(long_branch_v4t_arm_thumb) \
244 DEF_STUB(long_branch_thumb_only) \
245 DEF_STUB(long_branch_v4t_thumb_thumb) \
246 DEF_STUB(long_branch_v4t_thumb_arm) \
247 DEF_STUB(short_branch_v4t_thumb_arm) \
248 DEF_STUB(long_branch_any_arm_pic) \
249 DEF_STUB(long_branch_any_thumb_pic) \
250 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
251 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
252 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
253 DEF_STUB(long_branch_thumb_only_pic) \
254 DEF_STUB(a8_veneer_b_cond) \
255 DEF_STUB(a8_veneer_b) \
256 DEF_STUB(a8_veneer_bl) \
257 DEF_STUB(a8_veneer_blx)
259 // Stub types.
261 #define DEF_STUB(x) arm_stub_##x,
262 typedef enum
264 arm_stub_none,
265 DEF_STUBS
267 // First reloc stub type.
268 arm_stub_reloc_first = arm_stub_long_branch_any_any,
269 // Last reloc stub type.
270 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
272 // First Cortex-A8 stub type.
273 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
274 // Last Cortex-A8 stub type.
275 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
277 // Last stub type.
278 arm_stub_type_last = arm_stub_a8_veneer_blx
279 } Stub_type;
280 #undef DEF_STUB
282 // Stub template class. Templates are meant to be read-only objects.
283 // A stub template for a stub type contains all read-only attributes
284 // common to all stubs of the same type.
286 class Stub_template
288 public:
289 Stub_template(Stub_type, const Insn_template*, size_t);
291 ~Stub_template()
294 // Return stub type.
295 Stub_type
296 type() const
297 { return this->type_; }
299 // Return an array of instruction templates.
300 const Insn_template*
301 insns() const
302 { return this->insns_; }
304 // Return size of template in number of instructions.
305 size_t
306 insn_count() const
307 { return this->insn_count_; }
309 // Return size of template in bytes.
310 size_t
311 size() const
312 { return this->size_; }
314 // Return alignment of the stub template.
315 unsigned
316 alignment() const
317 { return this->alignment_; }
319 // Return whether entry point is in thumb mode.
320 bool
321 entry_in_thumb_mode() const
322 { return this->entry_in_thumb_mode_; }
324 // Return number of relocations in this template.
325 size_t
326 reloc_count() const
327 { return this->relocs_.size(); }
329 // Return index of the I-th instruction with relocation.
330 size_t
331 reloc_insn_index(size_t i) const
333 gold_assert(i < this->relocs_.size());
334 return this->relocs_[i].first;
337 // Return the offset of the I-th instruction with relocation from the
338 // beginning of the stub.
339 section_size_type
340 reloc_offset(size_t i) const
342 gold_assert(i < this->relocs_.size());
343 return this->relocs_[i].second;
346 private:
347 // This contains information about an instruction template with a relocation
348 // and its offset from start of stub.
349 typedef std::pair<size_t, section_size_type> Reloc;
351 // A Stub_template may not be copied. We want to share templates as much
352 // as possible.
353 Stub_template(const Stub_template&);
354 Stub_template& operator=(const Stub_template&);
356 // Stub type.
357 Stub_type type_;
358 // Points to an array of Insn_templates.
359 const Insn_template* insns_;
360 // Number of Insn_templates in insns_[].
361 size_t insn_count_;
362 // Size of templated instructions in bytes.
363 size_t size_;
364 // Alignment of templated instructions.
365 unsigned alignment_;
366 // Flag to indicate if entry is in thumb mode.
367 bool entry_in_thumb_mode_;
368 // A table of reloc instruction indices and offsets. We can find these by
369 // looking at the instruction templates but we pre-compute and then stash
370 // them here for speed.
371 std::vector<Reloc> relocs_;
375 // A class for code stubs. This is a base class for different type of
376 // stubs used in the ARM target.
379 class Stub
381 private:
382 static const section_offset_type invalid_offset =
383 static_cast<section_offset_type>(-1);
385 public:
386 Stub(const Stub_template* stub_template)
387 : stub_template_(stub_template), offset_(invalid_offset)
390 virtual
391 ~Stub()
394 // Return the stub template.
395 const Stub_template*
396 stub_template() const
397 { return this->stub_template_; }
399 // Return offset of code stub from beginning of its containing stub table.
400 section_offset_type
401 offset() const
403 gold_assert(this->offset_ != invalid_offset);
404 return this->offset_;
407 // Set offset of code stub from beginning of its containing stub table.
408 void
409 set_offset(section_offset_type offset)
410 { this->offset_ = offset; }
412 // Return the relocation target address of the i-th relocation in the
413 // stub. This must be defined in a child class.
414 Arm_address
415 reloc_target(size_t i)
416 { return this->do_reloc_target(i); }
418 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
419 void
420 write(unsigned char* view, section_size_type view_size, bool big_endian)
421 { this->do_write(view, view_size, big_endian); }
423 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
424 // for the i-th instruction.
425 uint16_t
426 thumb16_special(size_t i)
427 { return this->do_thumb16_special(i); }
429 protected:
430 // This must be defined in the child class.
431 virtual Arm_address
432 do_reloc_target(size_t) = 0;
434 // This may be overridden in the child class.
435 virtual void
436 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
438 if (big_endian)
439 this->do_fixed_endian_write<true>(view, view_size);
440 else
441 this->do_fixed_endian_write<false>(view, view_size);
444 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
445 // instruction template.
446 virtual uint16_t
447 do_thumb16_special(size_t)
448 { gold_unreachable(); }
450 private:
451 // A template to implement do_write.
452 template<bool big_endian>
453 void inline
454 do_fixed_endian_write(unsigned char*, section_size_type);
456 // Its template.
457 const Stub_template* stub_template_;
458 // Offset within the section of containing this stub.
459 section_offset_type offset_;
462 // Reloc stub class. These are stubs we use to fix up relocation because
463 // of limited branch ranges.
465 class Reloc_stub : public Stub
467 public:
468 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
469 // We assume we never jump to this address.
470 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
472 // Return destination address.
473 Arm_address
474 destination_address() const
476 gold_assert(this->destination_address_ != this->invalid_address);
477 return this->destination_address_;
480 // Set destination address.
481 void
482 set_destination_address(Arm_address address)
484 gold_assert(address != this->invalid_address);
485 this->destination_address_ = address;
488 // Reset destination address.
489 void
490 reset_destination_address()
491 { this->destination_address_ = this->invalid_address; }
493 // Determine stub type for a branch of a relocation of R_TYPE going
494 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
495 // the branch target is a thumb instruction. TARGET is used for look
496 // up ARM-specific linker settings.
497 static Stub_type
498 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
499 Arm_address branch_target, bool target_is_thumb);
501 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
502 // and an addend. Since we treat global and local symbol differently, we
503 // use a Symbol object for a global symbol and a object-index pair for
504 // a local symbol.
505 class Key
507 public:
508 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
509 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
510 // and R_SYM must not be invalid_index.
511 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
512 unsigned int r_sym, int32_t addend)
513 : stub_type_(stub_type), addend_(addend)
515 if (symbol != NULL)
517 this->r_sym_ = Reloc_stub::invalid_index;
518 this->u_.symbol = symbol;
520 else
522 gold_assert(relobj != NULL && r_sym != invalid_index);
523 this->r_sym_ = r_sym;
524 this->u_.relobj = relobj;
528 ~Key()
531 // Accessors: Keys are meant to be read-only object so no modifiers are
532 // provided.
534 // Return stub type.
535 Stub_type
536 stub_type() const
537 { return this->stub_type_; }
539 // Return the local symbol index or invalid_index.
540 unsigned int
541 r_sym() const
542 { return this->r_sym_; }
544 // Return the symbol if there is one.
545 const Symbol*
546 symbol() const
547 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
549 // Return the relobj if there is one.
550 const Relobj*
551 relobj() const
552 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
554 // Whether this equals to another key k.
555 bool
556 eq(const Key& k) const
558 return ((this->stub_type_ == k.stub_type_)
559 && (this->r_sym_ == k.r_sym_)
560 && ((this->r_sym_ != Reloc_stub::invalid_index)
561 ? (this->u_.relobj == k.u_.relobj)
562 : (this->u_.symbol == k.u_.symbol))
563 && (this->addend_ == k.addend_));
566 // Return a hash value.
567 size_t
568 hash_value() const
570 return (this->stub_type_
571 ^ this->r_sym_
572 ^ gold::string_hash<char>(
573 (this->r_sym_ != Reloc_stub::invalid_index)
574 ? this->u_.relobj->name().c_str()
575 : this->u_.symbol->name())
576 ^ this->addend_);
579 // Functors for STL associative containers.
580 struct hash
582 size_t
583 operator()(const Key& k) const
584 { return k.hash_value(); }
587 struct equal_to
589 bool
590 operator()(const Key& k1, const Key& k2) const
591 { return k1.eq(k2); }
594 // Name of key. This is mainly for debugging.
595 std::string
596 name() const;
598 private:
599 // Stub type.
600 Stub_type stub_type_;
601 // If this is a local symbol, this is the index in the defining object.
602 // Otherwise, it is invalid_index for a global symbol.
603 unsigned int r_sym_;
604 // If r_sym_ is invalid index. This points to a global symbol.
605 // Otherwise, this points a relobj. We used the unsized and target
606 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
607 // Arm_relobj. This is done to avoid making the stub class a template
608 // as most of the stub machinery is endianity-neutral. However, it
609 // may require a bit of casting done by users of this class.
610 union
612 const Symbol* symbol;
613 const Relobj* relobj;
614 } u_;
615 // Addend associated with a reloc.
616 int32_t addend_;
619 protected:
620 // Reloc_stubs are created via a stub factory. So these are protected.
621 Reloc_stub(const Stub_template* stub_template)
622 : Stub(stub_template), destination_address_(invalid_address)
625 ~Reloc_stub()
628 friend class Stub_factory;
630 // Return the relocation target address of the i-th relocation in the
631 // stub.
632 Arm_address
633 do_reloc_target(size_t i)
635 // All reloc stub have only one relocation.
636 gold_assert(i == 0);
637 return this->destination_address_;
640 private:
641 // Address of destination.
642 Arm_address destination_address_;
645 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
646 // THUMB branch that meets the following conditions:
648 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
649 // branch address is 0xffe.
650 // 2. The branch target address is in the same page as the first word of the
651 // branch.
652 // 3. The branch follows a 32-bit instruction which is not a branch.
654 // To do the fix up, we need to store the address of the branch instruction
655 // and its target at least. We also need to store the original branch
656 // instruction bits for the condition code in a conditional branch. The
657 // condition code is used in a special instruction template. We also want
658 // to identify input sections needing Cortex-A8 workaround quickly. We store
659 // extra information about object and section index of the code section
660 // containing a branch being fixed up. The information is used to mark
661 // the code section when we finalize the Cortex-A8 stubs.
664 class Cortex_a8_stub : public Stub
666 public:
667 ~Cortex_a8_stub()
670 // Return the object of the code section containing the branch being fixed
671 // up.
672 Relobj*
673 relobj() const
674 { return this->relobj_; }
676 // Return the section index of the code section containing the branch being
677 // fixed up.
678 unsigned int
679 shndx() const
680 { return this->shndx_; }
682 // Return the source address of stub. This is the address of the original
683 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
684 // instruction.
685 Arm_address
686 source_address() const
687 { return this->source_address_; }
689 // Return the destination address of the stub. This is the branch taken
690 // address of the original branch instruction. LSB is 1 if it is a THUMB
691 // instruction address.
692 Arm_address
693 destination_address() const
694 { return this->destination_address_; }
696 // Return the instruction being fixed up.
697 uint32_t
698 original_insn() const
699 { return this->original_insn_; }
701 protected:
702 // Cortex_a8_stubs are created via a stub factory. So these are protected.
703 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
704 unsigned int shndx, Arm_address source_address,
705 Arm_address destination_address, uint32_t original_insn)
706 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
707 source_address_(source_address | 1U),
708 destination_address_(destination_address),
709 original_insn_(original_insn)
712 friend class Stub_factory;
714 // Return the relocation target address of the i-th relocation in the
715 // stub.
716 Arm_address
717 do_reloc_target(size_t i)
719 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
721 // The conditional branch veneer has two relocations.
722 gold_assert(i < 2);
723 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
725 else
727 // All other Cortex-A8 stubs have only one relocation.
728 gold_assert(i == 0);
729 return this->destination_address_;
733 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
734 uint16_t
735 do_thumb16_special(size_t);
737 private:
738 // Object of the code section containing the branch being fixed up.
739 Relobj* relobj_;
740 // Section index of the code section containing the branch begin fixed up.
741 unsigned int shndx_;
742 // Source address of original branch.
743 Arm_address source_address_;
744 // Destination address of the original branch.
745 Arm_address destination_address_;
746 // Original branch instruction. This is needed for copying the condition
747 // code from a condition branch to its stub.
748 uint32_t original_insn_;
751 // Stub factory class.
753 class Stub_factory
755 public:
756 // Return the unique instance of this class.
757 static const Stub_factory&
758 get_instance()
760 static Stub_factory singleton;
761 return singleton;
764 // Make a relocation stub.
765 Reloc_stub*
766 make_reloc_stub(Stub_type stub_type) const
768 gold_assert(stub_type >= arm_stub_reloc_first
769 && stub_type <= arm_stub_reloc_last);
770 return new Reloc_stub(this->stub_templates_[stub_type]);
773 // Make a Cortex-A8 stub.
774 Cortex_a8_stub*
775 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
776 Arm_address source, Arm_address destination,
777 uint32_t original_insn) const
779 gold_assert(stub_type >= arm_stub_cortex_a8_first
780 && stub_type <= arm_stub_cortex_a8_last);
781 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
782 source, destination, original_insn);
785 private:
786 // Constructor and destructor are protected since we only return a single
787 // instance created in Stub_factory::get_instance().
789 Stub_factory();
791 // A Stub_factory may not be copied since it is a singleton.
792 Stub_factory(const Stub_factory&);
793 Stub_factory& operator=(Stub_factory&);
795 // Stub templates. These are initialized in the constructor.
796 const Stub_template* stub_templates_[arm_stub_type_last+1];
799 // A class to hold stubs for the ARM target.
801 template<bool big_endian>
802 class Stub_table : public Output_data
804 public:
805 Stub_table(Arm_input_section<big_endian>* owner)
806 : Output_data(), owner_(owner), reloc_stubs_(), cortex_a8_stubs_(),
807 prev_data_size_(0), prev_addralign_(1)
810 ~Stub_table()
813 // Owner of this stub table.
814 Arm_input_section<big_endian>*
815 owner() const
816 { return this->owner_; }
818 // Whether this stub table is empty.
819 bool
820 empty() const
821 { return this->reloc_stubs_.empty() && this->cortex_a8_stubs_.empty(); }
823 // Return the current data size.
824 off_t
825 current_data_size() const
826 { return this->current_data_size_for_child(); }
828 // Add a STUB with using KEY. Caller is reponsible for avoid adding
829 // if already a STUB with the same key has been added.
830 void
831 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
833 const Stub_template* stub_template = stub->stub_template();
834 gold_assert(stub_template->type() == key.stub_type());
835 this->reloc_stubs_[key] = stub;
838 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
839 // Caller is reponsible for avoid adding if already a STUB with the same
840 // address has been added.
841 void
842 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
844 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
845 this->cortex_a8_stubs_.insert(value);
848 // Remove all Cortex-A8 stubs.
849 void
850 remove_all_cortex_a8_stubs();
852 // Look up a relocation stub using KEY. Return NULL if there is none.
853 Reloc_stub*
854 find_reloc_stub(const Reloc_stub::Key& key) const
856 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
857 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
860 // Relocate stubs in this stub table.
861 void
862 relocate_stubs(const Relocate_info<32, big_endian>*,
863 Target_arm<big_endian>*, Output_section*,
864 unsigned char*, Arm_address, section_size_type);
866 // Update data size and alignment at the end of a relaxation pass. Return
867 // true if either data size or alignment is different from that of the
868 // previous relaxation pass.
869 bool
870 update_data_size_and_addralign();
872 // Finalize stubs. Set the offsets of all stubs and mark input sections
873 // needing the Cortex-A8 workaround.
874 void
875 finalize_stubs();
877 // Apply Cortex-A8 workaround to an address range.
878 void
879 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
880 unsigned char*, Arm_address,
881 section_size_type);
883 protected:
884 // Write out section contents.
885 void
886 do_write(Output_file*);
888 // Return the required alignment.
889 uint64_t
890 do_addralign() const
891 { return this->prev_addralign_; }
893 // Reset address and file offset.
894 void
895 do_reset_address_and_file_offset()
896 { this->set_current_data_size_for_child(this->prev_data_size_); }
898 // Set final data size.
899 void
900 set_final_data_size()
901 { this->set_data_size(this->current_data_size()); }
903 private:
904 // Relocate one stub.
905 void
906 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
907 Target_arm<big_endian>*, Output_section*,
908 unsigned char*, Arm_address, section_size_type);
910 // Unordered map of relocation stubs.
911 typedef
912 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
913 Reloc_stub::Key::equal_to>
914 Reloc_stub_map;
916 // List of Cortex-A8 stubs ordered by addresses of branches being
917 // fixed up in output.
918 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
920 // Owner of this stub table.
921 Arm_input_section<big_endian>* owner_;
922 // The relocation stubs.
923 Reloc_stub_map reloc_stubs_;
924 // The cortex_a8_stubs.
925 Cortex_a8_stub_list cortex_a8_stubs_;
926 // data size of this in the previous pass.
927 off_t prev_data_size_;
928 // address alignment of this in the previous pass.
929 uint64_t prev_addralign_;
932 // A class to wrap an ordinary input section containing executable code.
934 template<bool big_endian>
935 class Arm_input_section : public Output_relaxed_input_section
937 public:
938 Arm_input_section(Relobj* relobj, unsigned int shndx)
939 : Output_relaxed_input_section(relobj, shndx, 1),
940 original_addralign_(1), original_size_(0), stub_table_(NULL)
943 ~Arm_input_section()
946 // Initialize.
947 void
948 init();
950 // Whether this is a stub table owner.
951 bool
952 is_stub_table_owner() const
953 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
955 // Return the stub table.
956 Stub_table<big_endian>*
957 stub_table() const
958 { return this->stub_table_; }
960 // Set the stub_table.
961 void
962 set_stub_table(Stub_table<big_endian>* stub_table)
963 { this->stub_table_ = stub_table; }
965 // Downcast a base pointer to an Arm_input_section pointer. This is
966 // not type-safe but we only use Arm_input_section not the base class.
967 static Arm_input_section<big_endian>*
968 as_arm_input_section(Output_relaxed_input_section* poris)
969 { return static_cast<Arm_input_section<big_endian>*>(poris); }
971 protected:
972 // Write data to output file.
973 void
974 do_write(Output_file*);
976 // Return required alignment of this.
977 uint64_t
978 do_addralign() const
980 if (this->is_stub_table_owner())
981 return std::max(this->stub_table_->addralign(),
982 this->original_addralign_);
983 else
984 return this->original_addralign_;
987 // Finalize data size.
988 void
989 set_final_data_size();
991 // Reset address and file offset.
992 void
993 do_reset_address_and_file_offset();
995 // Output offset.
996 bool
997 do_output_offset(const Relobj* object, unsigned int shndx,
998 section_offset_type offset,
999 section_offset_type* poutput) const
1001 if ((object == this->relobj())
1002 && (shndx == this->shndx())
1003 && (offset >= 0)
1004 && (convert_types<uint64_t, section_offset_type>(offset)
1005 <= this->original_size_))
1007 *poutput = offset;
1008 return true;
1010 else
1011 return false;
1014 private:
1015 // Copying is not allowed.
1016 Arm_input_section(const Arm_input_section&);
1017 Arm_input_section& operator=(const Arm_input_section&);
1019 // Address alignment of the original input section.
1020 uint64_t original_addralign_;
1021 // Section size of the original input section.
1022 uint64_t original_size_;
1023 // Stub table.
1024 Stub_table<big_endian>* stub_table_;
1027 // Arm output section class. This is defined mainly to add a number of
1028 // stub generation methods.
1030 template<bool big_endian>
1031 class Arm_output_section : public Output_section
1033 public:
1034 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1035 elfcpp::Elf_Xword flags)
1036 : Output_section(name, type, flags)
1039 ~Arm_output_section()
1042 // Group input sections for stub generation.
1043 void
1044 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1046 // Downcast a base pointer to an Arm_output_section pointer. This is
1047 // not type-safe but we only use Arm_output_section not the base class.
1048 static Arm_output_section<big_endian>*
1049 as_arm_output_section(Output_section* os)
1050 { return static_cast<Arm_output_section<big_endian>*>(os); }
1052 private:
1053 // For convenience.
1054 typedef Output_section::Input_section Input_section;
1055 typedef Output_section::Input_section_list Input_section_list;
1057 // Create a stub group.
1058 void create_stub_group(Input_section_list::const_iterator,
1059 Input_section_list::const_iterator,
1060 Input_section_list::const_iterator,
1061 Target_arm<big_endian>*,
1062 std::vector<Output_relaxed_input_section*>*);
1065 // Arm_relobj class.
1067 template<bool big_endian>
1068 class Arm_relobj : public Sized_relobj<32, big_endian>
1070 public:
1071 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1073 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1074 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1075 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1076 stub_tables_(), local_symbol_is_thumb_function_(),
1077 attributes_section_data_(NULL), mapping_symbols_info_(),
1078 section_has_cortex_a8_workaround_(NULL)
1081 ~Arm_relobj()
1082 { delete this->attributes_section_data_; }
1084 // Return the stub table of the SHNDX-th section if there is one.
1085 Stub_table<big_endian>*
1086 stub_table(unsigned int shndx) const
1088 gold_assert(shndx < this->stub_tables_.size());
1089 return this->stub_tables_[shndx];
1092 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1093 void
1094 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1096 gold_assert(shndx < this->stub_tables_.size());
1097 this->stub_tables_[shndx] = stub_table;
1100 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1101 // index. This is only valid after do_count_local_symbol is called.
1102 bool
1103 local_symbol_is_thumb_function(unsigned int r_sym) const
1105 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1106 return this->local_symbol_is_thumb_function_[r_sym];
1109 // Scan all relocation sections for stub generation.
1110 void
1111 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1112 const Layout*);
1114 // Convert regular input section with index SHNDX to a relaxed section.
1115 void
1116 convert_input_section_to_relaxed_section(unsigned shndx)
1118 // The stubs have relocations and we need to process them after writing
1119 // out the stubs. So relocation now must follow section write.
1120 this->invalidate_section_offset(shndx);
1121 this->set_relocs_must_follow_section_writes();
1124 // Downcast a base pointer to an Arm_relobj pointer. This is
1125 // not type-safe but we only use Arm_relobj not the base class.
1126 static Arm_relobj<big_endian>*
1127 as_arm_relobj(Relobj* relobj)
1128 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1130 // Processor-specific flags in ELF file header. This is valid only after
1131 // reading symbols.
1132 elfcpp::Elf_Word
1133 processor_specific_flags() const
1134 { return this->processor_specific_flags_; }
1136 // Attribute section data This is the contents of the .ARM.attribute section
1137 // if there is one.
1138 const Attributes_section_data*
1139 attributes_section_data() const
1140 { return this->attributes_section_data_; }
1142 // Mapping symbol location.
1143 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1145 // Functor for STL container.
1146 struct Mapping_symbol_position_less
1148 bool
1149 operator()(const Mapping_symbol_position& p1,
1150 const Mapping_symbol_position& p2) const
1152 return (p1.first < p2.first
1153 || (p1.first == p2.first && p1.second < p2.second));
1157 // We only care about the first character of a mapping symbol, so
1158 // we only store that instead of the whole symbol name.
1159 typedef std::map<Mapping_symbol_position, char,
1160 Mapping_symbol_position_less> Mapping_symbols_info;
1162 // Whether a section contains any Cortex-A8 workaround.
1163 bool
1164 section_has_cortex_a8_workaround(unsigned int shndx) const
1166 return (this->section_has_cortex_a8_workaround_ != NULL
1167 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1170 // Mark a section that has Cortex-A8 workaround.
1171 void
1172 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1174 if (this->section_has_cortex_a8_workaround_ == NULL)
1175 this->section_has_cortex_a8_workaround_ =
1176 new std::vector<bool>(this->shnum(), false);
1177 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1180 protected:
1181 // Post constructor setup.
1182 void
1183 do_setup()
1185 // Call parent's setup method.
1186 Sized_relobj<32, big_endian>::do_setup();
1188 // Initialize look-up tables.
1189 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1190 this->stub_tables_.swap(empty_stub_table_list);
1193 // Count the local symbols.
1194 void
1195 do_count_local_symbols(Stringpool_template<char>*,
1196 Stringpool_template<char>*);
1198 void
1199 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1200 const unsigned char* pshdrs,
1201 typename Sized_relobj<32, big_endian>::Views* pivews);
1203 // Read the symbol information.
1204 void
1205 do_read_symbols(Read_symbols_data* sd);
1207 // Process relocs for garbage collection.
1208 void
1209 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1211 private:
1213 // Whether a section needs to be scanned for relocation stubs.
1214 bool
1215 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1216 const Relobj::Output_sections&,
1217 const Symbol_table *);
1219 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1220 bool
1221 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1222 unsigned int, Output_section*,
1223 const Symbol_table *);
1225 // Scan a section for the Cortex-A8 erratum.
1226 void
1227 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1228 unsigned int, Output_section*,
1229 Target_arm<big_endian>*);
1231 // List of stub tables.
1232 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1233 Stub_table_list stub_tables_;
1234 // Bit vector to tell if a local symbol is a thumb function or not.
1235 // This is only valid after do_count_local_symbol is called.
1236 std::vector<bool> local_symbol_is_thumb_function_;
1237 // processor-specific flags in ELF file header.
1238 elfcpp::Elf_Word processor_specific_flags_;
1239 // Object attributes if there is an .ARM.attributes section or NULL.
1240 Attributes_section_data* attributes_section_data_;
1241 // Mapping symbols information.
1242 Mapping_symbols_info mapping_symbols_info_;
1243 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1244 std::vector<bool>* section_has_cortex_a8_workaround_;
1247 // Arm_dynobj class.
1249 template<bool big_endian>
1250 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1252 public:
1253 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1254 const elfcpp::Ehdr<32, big_endian>& ehdr)
1255 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1256 processor_specific_flags_(0), attributes_section_data_(NULL)
1259 ~Arm_dynobj()
1260 { delete this->attributes_section_data_; }
1262 // Downcast a base pointer to an Arm_relobj pointer. This is
1263 // not type-safe but we only use Arm_relobj not the base class.
1264 static Arm_dynobj<big_endian>*
1265 as_arm_dynobj(Dynobj* dynobj)
1266 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1268 // Processor-specific flags in ELF file header. This is valid only after
1269 // reading symbols.
1270 elfcpp::Elf_Word
1271 processor_specific_flags() const
1272 { return this->processor_specific_flags_; }
1274 // Attributes section data.
1275 const Attributes_section_data*
1276 attributes_section_data() const
1277 { return this->attributes_section_data_; }
1279 protected:
1280 // Read the symbol information.
1281 void
1282 do_read_symbols(Read_symbols_data* sd);
1284 private:
1285 // processor-specific flags in ELF file header.
1286 elfcpp::Elf_Word processor_specific_flags_;
1287 // Object attributes if there is an .ARM.attributes section or NULL.
1288 Attributes_section_data* attributes_section_data_;
1291 // Functor to read reloc addends during stub generation.
1293 template<int sh_type, bool big_endian>
1294 struct Stub_addend_reader
1296 // Return the addend for a relocation of a particular type. Depending
1297 // on whether this is a REL or RELA relocation, read the addend from a
1298 // view or from a Reloc object.
1299 elfcpp::Elf_types<32>::Elf_Swxword
1300 operator()(
1301 unsigned int /* r_type */,
1302 const unsigned char* /* view */,
1303 const typename Reloc_types<sh_type,
1304 32, big_endian>::Reloc& /* reloc */) const;
1307 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1309 template<bool big_endian>
1310 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1312 elfcpp::Elf_types<32>::Elf_Swxword
1313 operator()(
1314 unsigned int,
1315 const unsigned char*,
1316 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1319 // Specialized Stub_addend_reader for RELA type relocation sections.
1320 // We currently do not handle RELA type relocation sections but it is trivial
1321 // to implement the addend reader. This is provided for completeness and to
1322 // make it easier to add support for RELA relocation sections in the future.
1324 template<bool big_endian>
1325 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1327 elfcpp::Elf_types<32>::Elf_Swxword
1328 operator()(
1329 unsigned int,
1330 const unsigned char*,
1331 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1332 big_endian>::Reloc& reloc) const
1333 { return reloc.get_r_addend(); }
1336 // Cortex_a8_reloc class. We keep record of relocation that may need
1337 // the Cortex-A8 erratum workaround.
1339 class Cortex_a8_reloc
1341 public:
1342 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1343 Arm_address destination)
1344 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1347 ~Cortex_a8_reloc()
1350 // Accessors: This is a read-only class.
1352 // Return the relocation stub associated with this relocation if there is
1353 // one.
1354 const Reloc_stub*
1355 reloc_stub() const
1356 { return this->reloc_stub_; }
1358 // Return the relocation type.
1359 unsigned int
1360 r_type() const
1361 { return this->r_type_; }
1363 // Return the destination address of the relocation. LSB stores the THUMB
1364 // bit.
1365 Arm_address
1366 destination() const
1367 { return this->destination_; }
1369 private:
1370 // Associated relocation stub if there is one, or NULL.
1371 const Reloc_stub* reloc_stub_;
1372 // Relocation type.
1373 unsigned int r_type_;
1374 // Destination address of this relocation. LSB is used to distinguish
1375 // ARM/THUMB mode.
1376 Arm_address destination_;
1379 // Utilities for manipulating integers of up to 32-bits
1381 namespace utils
1383 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1384 // an int32_t. NO_BITS must be between 1 to 32.
1385 template<int no_bits>
1386 static inline int32_t
1387 sign_extend(uint32_t bits)
1389 gold_assert(no_bits >= 0 && no_bits <= 32);
1390 if (no_bits == 32)
1391 return static_cast<int32_t>(bits);
1392 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1393 bits &= mask;
1394 uint32_t top_bit = 1U << (no_bits - 1);
1395 int32_t as_signed = static_cast<int32_t>(bits);
1396 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1399 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1400 template<int no_bits>
1401 static inline bool
1402 has_overflow(uint32_t bits)
1404 gold_assert(no_bits >= 0 && no_bits <= 32);
1405 if (no_bits == 32)
1406 return false;
1407 int32_t max = (1 << (no_bits - 1)) - 1;
1408 int32_t min = -(1 << (no_bits - 1));
1409 int32_t as_signed = static_cast<int32_t>(bits);
1410 return as_signed > max || as_signed < min;
1413 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1414 // fits in the given number of bits as either a signed or unsigned value.
1415 // For example, has_signed_unsigned_overflow<8> would check
1416 // -128 <= bits <= 255
1417 template<int no_bits>
1418 static inline bool
1419 has_signed_unsigned_overflow(uint32_t bits)
1421 gold_assert(no_bits >= 2 && no_bits <= 32);
1422 if (no_bits == 32)
1423 return false;
1424 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1425 int32_t min = -(1 << (no_bits - 1));
1426 int32_t as_signed = static_cast<int32_t>(bits);
1427 return as_signed > max || as_signed < min;
1430 // Select bits from A and B using bits in MASK. For each n in [0..31],
1431 // the n-th bit in the result is chosen from the n-th bits of A and B.
1432 // A zero selects A and a one selects B.
1433 static inline uint32_t
1434 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1435 { return (a & ~mask) | (b & mask); }
1438 template<bool big_endian>
1439 class Target_arm : public Sized_target<32, big_endian>
1441 public:
1442 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1443 Reloc_section;
1445 // When were are relocating a stub, we pass this as the relocation number.
1446 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1448 Target_arm()
1449 : Sized_target<32, big_endian>(&arm_info),
1450 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1451 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1452 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1453 should_force_pic_veneer_(false), arm_input_section_map_(),
1454 attributes_section_data_(NULL), fix_cortex_a8_(false),
1455 cortex_a8_relocs_info_()
1458 // Whether we can use BLX.
1459 bool
1460 may_use_blx() const
1461 { return this->may_use_blx_; }
1463 // Set use-BLX flag.
1464 void
1465 set_may_use_blx(bool value)
1466 { this->may_use_blx_ = value; }
1468 // Whether we force PCI branch veneers.
1469 bool
1470 should_force_pic_veneer() const
1471 { return this->should_force_pic_veneer_; }
1473 // Set PIC veneer flag.
1474 void
1475 set_should_force_pic_veneer(bool value)
1476 { this->should_force_pic_veneer_ = value; }
1478 // Whether we use THUMB-2 instructions.
1479 bool
1480 using_thumb2() const
1482 Object_attribute* attr =
1483 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1484 int arch = attr->int_value();
1485 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
1488 // Whether we use THUMB/THUMB-2 instructions only.
1489 bool
1490 using_thumb_only() const
1492 Object_attribute* attr =
1493 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1494 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1495 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1496 return false;
1497 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1498 return attr->int_value() == 'M';
1501 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1502 bool
1503 may_use_arm_nop() const
1505 Object_attribute* attr =
1506 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1507 int arch = attr->int_value();
1508 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1509 || arch == elfcpp::TAG_CPU_ARCH_V6K
1510 || arch == elfcpp::TAG_CPU_ARCH_V7
1511 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1514 // Whether we have THUMB-2 NOP.W instruction.
1515 bool
1516 may_use_thumb2_nop() const
1518 Object_attribute* attr =
1519 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1520 int arch = attr->int_value();
1521 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1522 || arch == elfcpp::TAG_CPU_ARCH_V7
1523 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1526 // Process the relocations to determine unreferenced sections for
1527 // garbage collection.
1528 void
1529 gc_process_relocs(Symbol_table* symtab,
1530 Layout* layout,
1531 Sized_relobj<32, big_endian>* object,
1532 unsigned int data_shndx,
1533 unsigned int sh_type,
1534 const unsigned char* prelocs,
1535 size_t reloc_count,
1536 Output_section* output_section,
1537 bool needs_special_offset_handling,
1538 size_t local_symbol_count,
1539 const unsigned char* plocal_symbols);
1541 // Scan the relocations to look for symbol adjustments.
1542 void
1543 scan_relocs(Symbol_table* symtab,
1544 Layout* layout,
1545 Sized_relobj<32, big_endian>* object,
1546 unsigned int data_shndx,
1547 unsigned int sh_type,
1548 const unsigned char* prelocs,
1549 size_t reloc_count,
1550 Output_section* output_section,
1551 bool needs_special_offset_handling,
1552 size_t local_symbol_count,
1553 const unsigned char* plocal_symbols);
1555 // Finalize the sections.
1556 void
1557 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1559 // Return the value to use for a dynamic symbol which requires special
1560 // treatment.
1561 uint64_t
1562 do_dynsym_value(const Symbol*) const;
1564 // Relocate a section.
1565 void
1566 relocate_section(const Relocate_info<32, big_endian>*,
1567 unsigned int sh_type,
1568 const unsigned char* prelocs,
1569 size_t reloc_count,
1570 Output_section* output_section,
1571 bool needs_special_offset_handling,
1572 unsigned char* view,
1573 Arm_address view_address,
1574 section_size_type view_size,
1575 const Reloc_symbol_changes*);
1577 // Scan the relocs during a relocatable link.
1578 void
1579 scan_relocatable_relocs(Symbol_table* symtab,
1580 Layout* layout,
1581 Sized_relobj<32, big_endian>* object,
1582 unsigned int data_shndx,
1583 unsigned int sh_type,
1584 const unsigned char* prelocs,
1585 size_t reloc_count,
1586 Output_section* output_section,
1587 bool needs_special_offset_handling,
1588 size_t local_symbol_count,
1589 const unsigned char* plocal_symbols,
1590 Relocatable_relocs*);
1592 // Relocate a section during a relocatable link.
1593 void
1594 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1595 unsigned int sh_type,
1596 const unsigned char* prelocs,
1597 size_t reloc_count,
1598 Output_section* output_section,
1599 off_t offset_in_output_section,
1600 const Relocatable_relocs*,
1601 unsigned char* view,
1602 Arm_address view_address,
1603 section_size_type view_size,
1604 unsigned char* reloc_view,
1605 section_size_type reloc_view_size);
1607 // Return whether SYM is defined by the ABI.
1608 bool
1609 do_is_defined_by_abi(Symbol* sym) const
1610 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1612 // Return the size of the GOT section.
1613 section_size_type
1614 got_size()
1616 gold_assert(this->got_ != NULL);
1617 return this->got_->data_size();
1620 // Map platform-specific reloc types
1621 static unsigned int
1622 get_real_reloc_type (unsigned int r_type);
1625 // Methods to support stub-generations.
1628 // Return the stub factory
1629 const Stub_factory&
1630 stub_factory() const
1631 { return this->stub_factory_; }
1633 // Make a new Arm_input_section object.
1634 Arm_input_section<big_endian>*
1635 new_arm_input_section(Relobj*, unsigned int);
1637 // Find the Arm_input_section object corresponding to the SHNDX-th input
1638 // section of RELOBJ.
1639 Arm_input_section<big_endian>*
1640 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
1642 // Make a new Stub_table
1643 Stub_table<big_endian>*
1644 new_stub_table(Arm_input_section<big_endian>*);
1646 // Scan a section for stub generation.
1647 void
1648 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
1649 const unsigned char*, size_t, Output_section*,
1650 bool, const unsigned char*, Arm_address,
1651 section_size_type);
1653 // Relocate a stub.
1654 void
1655 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1656 Output_section*, unsigned char*, Arm_address,
1657 section_size_type);
1659 // Get the default ARM target.
1660 static Target_arm<big_endian>*
1661 default_target()
1663 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1664 && parameters->target().is_big_endian() == big_endian);
1665 return static_cast<Target_arm<big_endian>*>(
1666 parameters->sized_target<32, big_endian>());
1669 // Whether relocation type uses LSB to distinguish THUMB addresses.
1670 static bool
1671 reloc_uses_thumb_bit(unsigned int r_type);
1673 // Whether NAME belongs to a mapping symbol.
1674 static bool
1675 is_mapping_symbol_name(const char* name)
1677 return (name
1678 && name[0] == '$'
1679 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
1680 && (name[2] == '\0' || name[2] == '.'));
1683 // Whether we work around the Cortex-A8 erratum.
1684 bool
1685 fix_cortex_a8() const
1686 { return this->fix_cortex_a8_; }
1688 // Scan a span of THUMB code section for Cortex-A8 erratum.
1689 void
1690 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
1691 section_size_type, section_size_type,
1692 const unsigned char*, Arm_address);
1694 // Apply Cortex-A8 workaround to a branch.
1695 void
1696 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
1697 unsigned char*, Arm_address);
1699 protected:
1700 // Make an ELF object.
1701 Object*
1702 do_make_elf_object(const std::string&, Input_file*, off_t,
1703 const elfcpp::Ehdr<32, big_endian>& ehdr);
1705 Object*
1706 do_make_elf_object(const std::string&, Input_file*, off_t,
1707 const elfcpp::Ehdr<32, !big_endian>&)
1708 { gold_unreachable(); }
1710 Object*
1711 do_make_elf_object(const std::string&, Input_file*, off_t,
1712 const elfcpp::Ehdr<64, false>&)
1713 { gold_unreachable(); }
1715 Object*
1716 do_make_elf_object(const std::string&, Input_file*, off_t,
1717 const elfcpp::Ehdr<64, true>&)
1718 { gold_unreachable(); }
1720 // Make an output section.
1721 Output_section*
1722 do_make_output_section(const char* name, elfcpp::Elf_Word type,
1723 elfcpp::Elf_Xword flags)
1724 { return new Arm_output_section<big_endian>(name, type, flags); }
1726 void
1727 do_adjust_elf_header(unsigned char* view, int len) const;
1729 // We only need to generate stubs, and hence perform relaxation if we are
1730 // not doing relocatable linking.
1731 bool
1732 do_may_relax() const
1733 { return !parameters->options().relocatable(); }
1735 bool
1736 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
1738 // Determine whether an object attribute tag takes an integer, a
1739 // string or both.
1741 do_attribute_arg_type(int tag) const;
1743 // Reorder tags during output.
1745 do_attributes_order(int num) const;
1747 private:
1748 // The class which scans relocations.
1749 class Scan
1751 public:
1752 Scan()
1753 : issued_non_pic_error_(false)
1756 inline void
1757 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1758 Sized_relobj<32, big_endian>* object,
1759 unsigned int data_shndx,
1760 Output_section* output_section,
1761 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1762 const elfcpp::Sym<32, big_endian>& lsym);
1764 inline void
1765 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1766 Sized_relobj<32, big_endian>* object,
1767 unsigned int data_shndx,
1768 Output_section* output_section,
1769 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1770 Symbol* gsym);
1772 private:
1773 static void
1774 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1775 unsigned int r_type);
1777 static void
1778 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1779 unsigned int r_type, Symbol*);
1781 void
1782 check_non_pic(Relobj*, unsigned int r_type);
1784 // Almost identical to Symbol::needs_plt_entry except that it also
1785 // handles STT_ARM_TFUNC.
1786 static bool
1787 symbol_needs_plt_entry(const Symbol* sym)
1789 // An undefined symbol from an executable does not need a PLT entry.
1790 if (sym->is_undefined() && !parameters->options().shared())
1791 return false;
1793 return (!parameters->doing_static_link()
1794 && (sym->type() == elfcpp::STT_FUNC
1795 || sym->type() == elfcpp::STT_ARM_TFUNC)
1796 && (sym->is_from_dynobj()
1797 || sym->is_undefined()
1798 || sym->is_preemptible()));
1801 // Whether we have issued an error about a non-PIC compilation.
1802 bool issued_non_pic_error_;
1805 // The class which implements relocation.
1806 class Relocate
1808 public:
1809 Relocate()
1812 ~Relocate()
1815 // Return whether the static relocation needs to be applied.
1816 inline bool
1817 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1818 int ref_flags,
1819 bool is_32bit,
1820 Output_section* output_section);
1822 // Do a relocation. Return false if the caller should not issue
1823 // any warnings about this relocation.
1824 inline bool
1825 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1826 Output_section*, size_t relnum,
1827 const elfcpp::Rel<32, big_endian>&,
1828 unsigned int r_type, const Sized_symbol<32>*,
1829 const Symbol_value<32>*,
1830 unsigned char*, Arm_address,
1831 section_size_type);
1833 // Return whether we want to pass flag NON_PIC_REF for this
1834 // reloc. This means the relocation type accesses a symbol not via
1835 // GOT or PLT.
1836 static inline bool
1837 reloc_is_non_pic (unsigned int r_type)
1839 switch (r_type)
1841 // These relocation types reference GOT or PLT entries explicitly.
1842 case elfcpp::R_ARM_GOT_BREL:
1843 case elfcpp::R_ARM_GOT_ABS:
1844 case elfcpp::R_ARM_GOT_PREL:
1845 case elfcpp::R_ARM_GOT_BREL12:
1846 case elfcpp::R_ARM_PLT32_ABS:
1847 case elfcpp::R_ARM_TLS_GD32:
1848 case elfcpp::R_ARM_TLS_LDM32:
1849 case elfcpp::R_ARM_TLS_IE32:
1850 case elfcpp::R_ARM_TLS_IE12GP:
1852 // These relocate types may use PLT entries.
1853 case elfcpp::R_ARM_CALL:
1854 case elfcpp::R_ARM_THM_CALL:
1855 case elfcpp::R_ARM_JUMP24:
1856 case elfcpp::R_ARM_THM_JUMP24:
1857 case elfcpp::R_ARM_THM_JUMP19:
1858 case elfcpp::R_ARM_PLT32:
1859 case elfcpp::R_ARM_THM_XPC22:
1860 return false;
1862 default:
1863 return true;
1868 // A class which returns the size required for a relocation type,
1869 // used while scanning relocs during a relocatable link.
1870 class Relocatable_size_for_reloc
1872 public:
1873 unsigned int
1874 get_size_for_reloc(unsigned int, Relobj*);
1877 // Get the GOT section, creating it if necessary.
1878 Output_data_got<32, big_endian>*
1879 got_section(Symbol_table*, Layout*);
1881 // Get the GOT PLT section.
1882 Output_data_space*
1883 got_plt_section() const
1885 gold_assert(this->got_plt_ != NULL);
1886 return this->got_plt_;
1889 // Create a PLT entry for a global symbol.
1890 void
1891 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1893 // Get the PLT section.
1894 const Output_data_plt_arm<big_endian>*
1895 plt_section() const
1897 gold_assert(this->plt_ != NULL);
1898 return this->plt_;
1901 // Get the dynamic reloc section, creating it if necessary.
1902 Reloc_section*
1903 rel_dyn_section(Layout*);
1905 // Return true if the symbol may need a COPY relocation.
1906 // References from an executable object to non-function symbols
1907 // defined in a dynamic object may need a COPY relocation.
1908 bool
1909 may_need_copy_reloc(Symbol* gsym)
1911 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1912 && gsym->may_need_copy_reloc());
1915 // Add a potential copy relocation.
1916 void
1917 copy_reloc(Symbol_table* symtab, Layout* layout,
1918 Sized_relobj<32, big_endian>* object,
1919 unsigned int shndx, Output_section* output_section,
1920 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1922 this->copy_relocs_.copy_reloc(symtab, layout,
1923 symtab->get_sized_symbol<32>(sym),
1924 object, shndx, output_section, reloc,
1925 this->rel_dyn_section(layout));
1928 // Whether two EABI versions are compatible.
1929 static bool
1930 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1932 // Merge processor-specific flags from input object and those in the ELF
1933 // header of the output.
1934 void
1935 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1937 // Get the secondary compatible architecture.
1938 static int
1939 get_secondary_compatible_arch(const Attributes_section_data*);
1941 // Set the secondary compatible architecture.
1942 static void
1943 set_secondary_compatible_arch(Attributes_section_data*, int);
1945 static int
1946 tag_cpu_arch_combine(const char*, int, int*, int, int);
1948 // Helper to print AEABI enum tag value.
1949 static std::string
1950 aeabi_enum_name(unsigned int);
1952 // Return string value for TAG_CPU_name.
1953 static std::string
1954 tag_cpu_name_value(unsigned int);
1956 // Merge object attributes from input object and those in the output.
1957 void
1958 merge_object_attributes(const char*, const Attributes_section_data*);
1960 // Helper to get an AEABI object attribute
1961 Object_attribute*
1962 get_aeabi_object_attribute(int tag) const
1964 Attributes_section_data* pasd = this->attributes_section_data_;
1965 gold_assert(pasd != NULL);
1966 Object_attribute* attr =
1967 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
1968 gold_assert(attr != NULL);
1969 return attr;
1973 // Methods to support stub-generations.
1976 // Group input sections for stub generation.
1977 void
1978 group_sections(Layout*, section_size_type, bool);
1980 // Scan a relocation for stub generation.
1981 void
1982 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
1983 const Sized_symbol<32>*, unsigned int,
1984 const Symbol_value<32>*,
1985 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
1987 // Scan a relocation section for stub.
1988 template<int sh_type>
1989 void
1990 scan_reloc_section_for_stubs(
1991 const Relocate_info<32, big_endian>* relinfo,
1992 const unsigned char* prelocs,
1993 size_t reloc_count,
1994 Output_section* output_section,
1995 bool needs_special_offset_handling,
1996 const unsigned char* view,
1997 elfcpp::Elf_types<32>::Elf_Addr view_address,
1998 section_size_type);
2000 // Information about this specific target which we pass to the
2001 // general Target structure.
2002 static const Target::Target_info arm_info;
2004 // The types of GOT entries needed for this platform.
2005 enum Got_type
2007 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
2010 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2012 // Map input section to Arm_input_section.
2013 typedef Unordered_map<Input_section_specifier,
2014 Arm_input_section<big_endian>*,
2015 Input_section_specifier::hash,
2016 Input_section_specifier::equal_to>
2017 Arm_input_section_map;
2019 // Map output addresses to relocs for Cortex-A8 erratum.
2020 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2021 Cortex_a8_relocs_info;
2023 // The GOT section.
2024 Output_data_got<32, big_endian>* got_;
2025 // The PLT section.
2026 Output_data_plt_arm<big_endian>* plt_;
2027 // The GOT PLT section.
2028 Output_data_space* got_plt_;
2029 // The dynamic reloc section.
2030 Reloc_section* rel_dyn_;
2031 // Relocs saved to avoid a COPY reloc.
2032 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2033 // Space for variables copied with a COPY reloc.
2034 Output_data_space* dynbss_;
2035 // Vector of Stub_tables created.
2036 Stub_table_list stub_tables_;
2037 // Stub factory.
2038 const Stub_factory &stub_factory_;
2039 // Whether we can use BLX.
2040 bool may_use_blx_;
2041 // Whether we force PIC branch veneers.
2042 bool should_force_pic_veneer_;
2043 // Map for locating Arm_input_sections.
2044 Arm_input_section_map arm_input_section_map_;
2045 // Attributes section data in output.
2046 Attributes_section_data* attributes_section_data_;
2047 // Whether we want to fix code for Cortex-A8 erratum.
2048 bool fix_cortex_a8_;
2049 // Map addresses to relocs for Cortex-A8 erratum.
2050 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2053 template<bool big_endian>
2054 const Target::Target_info Target_arm<big_endian>::arm_info =
2056 32, // size
2057 big_endian, // is_big_endian
2058 elfcpp::EM_ARM, // machine_code
2059 false, // has_make_symbol
2060 false, // has_resolve
2061 false, // has_code_fill
2062 true, // is_default_stack_executable
2063 '\0', // wrap_char
2064 "/usr/lib/libc.so.1", // dynamic_linker
2065 0x8000, // default_text_segment_address
2066 0x1000, // abi_pagesize (overridable by -z max-page-size)
2067 0x1000, // common_pagesize (overridable by -z common-page-size)
2068 elfcpp::SHN_UNDEF, // small_common_shndx
2069 elfcpp::SHN_UNDEF, // large_common_shndx
2070 0, // small_common_section_flags
2071 0, // large_common_section_flags
2072 ".ARM.attributes", // attributes_section
2073 "aeabi" // attributes_vendor
2076 // Arm relocate functions class
2079 template<bool big_endian>
2080 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2082 public:
2083 typedef enum
2085 STATUS_OKAY, // No error during relocation.
2086 STATUS_OVERFLOW, // Relocation oveflow.
2087 STATUS_BAD_RELOC // Relocation cannot be applied.
2088 } Status;
2090 private:
2091 typedef Relocate_functions<32, big_endian> Base;
2092 typedef Arm_relocate_functions<big_endian> This;
2094 // Encoding of imm16 argument for movt and movw ARM instructions
2095 // from ARM ARM:
2097 // imm16 := imm4 | imm12
2099 // 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
2100 // +-------+---------------+-------+-------+-----------------------+
2101 // | | |imm4 | |imm12 |
2102 // +-------+---------------+-------+-------+-----------------------+
2104 // Extract the relocation addend from VAL based on the ARM
2105 // instruction encoding described above.
2106 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2107 extract_arm_movw_movt_addend(
2108 typename elfcpp::Swap<32, big_endian>::Valtype val)
2110 // According to the Elf ABI for ARM Architecture the immediate
2111 // field is sign-extended to form the addend.
2112 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2115 // Insert X into VAL based on the ARM instruction encoding described
2116 // above.
2117 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2118 insert_val_arm_movw_movt(
2119 typename elfcpp::Swap<32, big_endian>::Valtype val,
2120 typename elfcpp::Swap<32, big_endian>::Valtype x)
2122 val &= 0xfff0f000;
2123 val |= x & 0x0fff;
2124 val |= (x & 0xf000) << 4;
2125 return val;
2128 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2129 // from ARM ARM:
2131 // imm16 := imm4 | i | imm3 | imm8
2133 // 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
2134 // +---------+-+-----------+-------++-+-----+-------+---------------+
2135 // | |i| |imm4 || |imm3 | |imm8 |
2136 // +---------+-+-----------+-------++-+-----+-------+---------------+
2138 // Extract the relocation addend from VAL based on the Thumb2
2139 // instruction encoding described above.
2140 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2141 extract_thumb_movw_movt_addend(
2142 typename elfcpp::Swap<32, big_endian>::Valtype val)
2144 // According to the Elf ABI for ARM Architecture the immediate
2145 // field is sign-extended to form the addend.
2146 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2147 | ((val >> 15) & 0x0800)
2148 | ((val >> 4) & 0x0700)
2149 | (val & 0x00ff));
2152 // Insert X into VAL based on the Thumb2 instruction encoding
2153 // described above.
2154 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2155 insert_val_thumb_movw_movt(
2156 typename elfcpp::Swap<32, big_endian>::Valtype val,
2157 typename elfcpp::Swap<32, big_endian>::Valtype x)
2159 val &= 0xfbf08f00;
2160 val |= (x & 0xf000) << 4;
2161 val |= (x & 0x0800) << 15;
2162 val |= (x & 0x0700) << 4;
2163 val |= (x & 0x00ff);
2164 return val;
2167 // Handle ARM long branches.
2168 static typename This::Status
2169 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2170 unsigned char *, const Sized_symbol<32>*,
2171 const Arm_relobj<big_endian>*, unsigned int,
2172 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2174 // Handle THUMB long branches.
2175 static typename This::Status
2176 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2177 unsigned char *, const Sized_symbol<32>*,
2178 const Arm_relobj<big_endian>*, unsigned int,
2179 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2181 public:
2183 // Return the branch offset of a 32-bit THUMB branch.
2184 static inline int32_t
2185 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2187 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2188 // involving the J1 and J2 bits.
2189 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2190 uint32_t upper = upper_insn & 0x3ffU;
2191 uint32_t lower = lower_insn & 0x7ffU;
2192 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2193 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2194 uint32_t i1 = j1 ^ s ? 0 : 1;
2195 uint32_t i2 = j2 ^ s ? 0 : 1;
2197 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2198 | (upper << 12) | (lower << 1));
2201 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2202 // UPPER_INSN is the original upper instruction of the branch. Caller is
2203 // responsible for overflow checking and BLX offset adjustment.
2204 static inline uint16_t
2205 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2207 uint32_t s = offset < 0 ? 1 : 0;
2208 uint32_t bits = static_cast<uint32_t>(offset);
2209 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2212 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2213 // LOWER_INSN is the original lower instruction of the branch. Caller is
2214 // responsible for overflow checking and BLX offset adjustment.
2215 static inline uint16_t
2216 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2218 uint32_t s = offset < 0 ? 1 : 0;
2219 uint32_t bits = static_cast<uint32_t>(offset);
2220 return ((lower_insn & ~0x2fffU)
2221 | ((((bits >> 23) & 1) ^ !s) << 13)
2222 | ((((bits >> 22) & 1) ^ !s) << 11)
2223 | ((bits >> 1) & 0x7ffU));
2226 // Return the branch offset of a 32-bit THUMB conditional branch.
2227 static inline int32_t
2228 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2230 uint32_t s = (upper_insn & 0x0400U) >> 10;
2231 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2232 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2233 uint32_t lower = (lower_insn & 0x07ffU);
2234 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2236 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2239 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2240 // instruction. UPPER_INSN is the original upper instruction of the branch.
2241 // Caller is responsible for overflow checking.
2242 static inline uint16_t
2243 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2245 uint32_t s = offset < 0 ? 1 : 0;
2246 uint32_t bits = static_cast<uint32_t>(offset);
2247 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2250 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2251 // instruction. LOWER_INSN is the original lower instruction of the branch.
2252 // Caller is reponsible for overflow checking.
2253 static inline uint16_t
2254 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2256 uint32_t bits = static_cast<uint32_t>(offset);
2257 uint32_t j2 = (bits & 0x00080000U) >> 19;
2258 uint32_t j1 = (bits & 0x00040000U) >> 18;
2259 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2261 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2264 // R_ARM_ABS8: S + A
2265 static inline typename This::Status
2266 abs8(unsigned char *view,
2267 const Sized_relobj<32, big_endian>* object,
2268 const Symbol_value<32>* psymval)
2270 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2271 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2272 Valtype* wv = reinterpret_cast<Valtype*>(view);
2273 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2274 Reltype addend = utils::sign_extend<8>(val);
2275 Reltype x = psymval->value(object, addend);
2276 val = utils::bit_select(val, x, 0xffU);
2277 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2278 return (utils::has_signed_unsigned_overflow<8>(x)
2279 ? This::STATUS_OVERFLOW
2280 : This::STATUS_OKAY);
2283 // R_ARM_THM_ABS5: S + A
2284 static inline typename This::Status
2285 thm_abs5(unsigned char *view,
2286 const Sized_relobj<32, big_endian>* object,
2287 const Symbol_value<32>* psymval)
2289 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2290 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2291 Valtype* wv = reinterpret_cast<Valtype*>(view);
2292 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2293 Reltype addend = (val & 0x7e0U) >> 6;
2294 Reltype x = psymval->value(object, addend);
2295 val = utils::bit_select(val, x << 6, 0x7e0U);
2296 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2297 return (utils::has_overflow<5>(x)
2298 ? This::STATUS_OVERFLOW
2299 : This::STATUS_OKAY);
2302 // R_ARM_ABS12: S + A
2303 static inline typename This::Status
2304 abs12(unsigned char *view,
2305 const Sized_relobj<32, big_endian>* object,
2306 const Symbol_value<32>* psymval)
2308 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2309 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2310 Valtype* wv = reinterpret_cast<Valtype*>(view);
2311 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2312 Reltype addend = val & 0x0fffU;
2313 Reltype x = psymval->value(object, addend);
2314 val = utils::bit_select(val, x, 0x0fffU);
2315 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2316 return (utils::has_overflow<12>(x)
2317 ? This::STATUS_OVERFLOW
2318 : This::STATUS_OKAY);
2321 // R_ARM_ABS16: S + A
2322 static inline typename This::Status
2323 abs16(unsigned char *view,
2324 const Sized_relobj<32, big_endian>* object,
2325 const Symbol_value<32>* psymval)
2327 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2328 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2329 Valtype* wv = reinterpret_cast<Valtype*>(view);
2330 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2331 Reltype addend = utils::sign_extend<16>(val);
2332 Reltype x = psymval->value(object, addend);
2333 val = utils::bit_select(val, x, 0xffffU);
2334 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2335 return (utils::has_signed_unsigned_overflow<16>(x)
2336 ? This::STATUS_OVERFLOW
2337 : This::STATUS_OKAY);
2340 // R_ARM_ABS32: (S + A) | T
2341 static inline typename This::Status
2342 abs32(unsigned char *view,
2343 const Sized_relobj<32, big_endian>* object,
2344 const Symbol_value<32>* psymval,
2345 Arm_address thumb_bit)
2347 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2348 Valtype* wv = reinterpret_cast<Valtype*>(view);
2349 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2350 Valtype x = psymval->value(object, addend) | thumb_bit;
2351 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2352 return This::STATUS_OKAY;
2355 // R_ARM_REL32: (S + A) | T - P
2356 static inline typename This::Status
2357 rel32(unsigned char *view,
2358 const Sized_relobj<32, big_endian>* object,
2359 const Symbol_value<32>* psymval,
2360 Arm_address address,
2361 Arm_address thumb_bit)
2363 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2364 Valtype* wv = reinterpret_cast<Valtype*>(view);
2365 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2366 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2367 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2368 return This::STATUS_OKAY;
2371 // R_ARM_THM_CALL: (S + A) | T - P
2372 static inline typename This::Status
2373 thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2374 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2375 unsigned int r_sym, const Symbol_value<32>* psymval,
2376 Arm_address address, Arm_address thumb_bit,
2377 bool is_weakly_undefined_without_plt)
2379 return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
2380 object, r_sym, psymval, address, thumb_bit,
2381 is_weakly_undefined_without_plt);
2384 // R_ARM_THM_JUMP24: (S + A) | T - P
2385 static inline typename This::Status
2386 thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2387 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2388 unsigned int r_sym, const Symbol_value<32>* psymval,
2389 Arm_address address, Arm_address thumb_bit,
2390 bool is_weakly_undefined_without_plt)
2392 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
2393 object, r_sym, psymval, address, thumb_bit,
2394 is_weakly_undefined_without_plt);
2397 // R_ARM_THM_JUMP24: (S + A) | T - P
2398 static typename This::Status
2399 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
2400 const Symbol_value<32>* psymval, Arm_address address,
2401 Arm_address thumb_bit);
2403 // R_ARM_THM_XPC22: (S + A) | T - P
2404 static inline typename This::Status
2405 thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2406 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2407 unsigned int r_sym, const Symbol_value<32>* psymval,
2408 Arm_address address, Arm_address thumb_bit,
2409 bool is_weakly_undefined_without_plt)
2411 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
2412 object, r_sym, psymval, address, thumb_bit,
2413 is_weakly_undefined_without_plt);
2416 // R_ARM_THM_JUMP6: S + A – P
2417 static inline typename This::Status
2418 thm_jump6(unsigned char *view,
2419 const Sized_relobj<32, big_endian>* object,
2420 const Symbol_value<32>* psymval,
2421 Arm_address address)
2423 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2424 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2425 Valtype* wv = reinterpret_cast<Valtype*>(view);
2426 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2427 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
2428 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
2429 Reltype x = (psymval->value(object, addend) - address);
2430 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
2431 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2432 // CZB does only forward jumps.
2433 return ((x > 0x007e)
2434 ? This::STATUS_OVERFLOW
2435 : This::STATUS_OKAY);
2438 // R_ARM_THM_JUMP8: S + A – P
2439 static inline typename This::Status
2440 thm_jump8(unsigned char *view,
2441 const Sized_relobj<32, big_endian>* object,
2442 const Symbol_value<32>* psymval,
2443 Arm_address address)
2445 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2446 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2447 Valtype* wv = reinterpret_cast<Valtype*>(view);
2448 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2449 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
2450 Reltype x = (psymval->value(object, addend) - address);
2451 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
2452 return (utils::has_overflow<8>(x)
2453 ? This::STATUS_OVERFLOW
2454 : This::STATUS_OKAY);
2457 // R_ARM_THM_JUMP11: S + A – P
2458 static inline typename This::Status
2459 thm_jump11(unsigned char *view,
2460 const Sized_relobj<32, big_endian>* object,
2461 const Symbol_value<32>* psymval,
2462 Arm_address address)
2464 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2465 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2466 Valtype* wv = reinterpret_cast<Valtype*>(view);
2467 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2468 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
2469 Reltype x = (psymval->value(object, addend) - address);
2470 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
2471 return (utils::has_overflow<11>(x)
2472 ? This::STATUS_OVERFLOW
2473 : This::STATUS_OKAY);
2476 // R_ARM_BASE_PREL: B(S) + A - P
2477 static inline typename This::Status
2478 base_prel(unsigned char* view,
2479 Arm_address origin,
2480 Arm_address address)
2482 Base::rel32(view, origin - address);
2483 return STATUS_OKAY;
2486 // R_ARM_BASE_ABS: B(S) + A
2487 static inline typename This::Status
2488 base_abs(unsigned char* view,
2489 Arm_address origin)
2491 Base::rel32(view, origin);
2492 return STATUS_OKAY;
2495 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2496 static inline typename This::Status
2497 got_brel(unsigned char* view,
2498 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
2500 Base::rel32(view, got_offset);
2501 return This::STATUS_OKAY;
2504 // R_ARM_GOT_PREL: GOT(S) + A - P
2505 static inline typename This::Status
2506 got_prel(unsigned char *view,
2507 Arm_address got_entry,
2508 Arm_address address)
2510 Base::rel32(view, got_entry - address);
2511 return This::STATUS_OKAY;
2514 // R_ARM_PLT32: (S + A) | T - P
2515 static inline typename This::Status
2516 plt32(const Relocate_info<32, big_endian>* relinfo,
2517 unsigned char *view,
2518 const Sized_symbol<32>* gsym,
2519 const Arm_relobj<big_endian>* object,
2520 unsigned int r_sym,
2521 const Symbol_value<32>* psymval,
2522 Arm_address address,
2523 Arm_address thumb_bit,
2524 bool is_weakly_undefined_without_plt)
2526 return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
2527 object, r_sym, psymval, address, thumb_bit,
2528 is_weakly_undefined_without_plt);
2531 // R_ARM_XPC25: (S + A) | T - P
2532 static inline typename This::Status
2533 xpc25(const Relocate_info<32, big_endian>* relinfo,
2534 unsigned char *view,
2535 const Sized_symbol<32>* gsym,
2536 const Arm_relobj<big_endian>* object,
2537 unsigned int r_sym,
2538 const Symbol_value<32>* psymval,
2539 Arm_address address,
2540 Arm_address thumb_bit,
2541 bool is_weakly_undefined_without_plt)
2543 return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
2544 object, r_sym, psymval, address, thumb_bit,
2545 is_weakly_undefined_without_plt);
2548 // R_ARM_CALL: (S + A) | T - P
2549 static inline typename This::Status
2550 call(const Relocate_info<32, big_endian>* relinfo,
2551 unsigned char *view,
2552 const Sized_symbol<32>* gsym,
2553 const Arm_relobj<big_endian>* object,
2554 unsigned int r_sym,
2555 const Symbol_value<32>* psymval,
2556 Arm_address address,
2557 Arm_address thumb_bit,
2558 bool is_weakly_undefined_without_plt)
2560 return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2561 object, r_sym, psymval, address, thumb_bit,
2562 is_weakly_undefined_without_plt);
2565 // R_ARM_JUMP24: (S + A) | T - P
2566 static inline typename This::Status
2567 jump24(const Relocate_info<32, big_endian>* relinfo,
2568 unsigned char *view,
2569 const Sized_symbol<32>* gsym,
2570 const Arm_relobj<big_endian>* object,
2571 unsigned int r_sym,
2572 const Symbol_value<32>* psymval,
2573 Arm_address address,
2574 Arm_address thumb_bit,
2575 bool is_weakly_undefined_without_plt)
2577 return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2578 object, r_sym, psymval, address, thumb_bit,
2579 is_weakly_undefined_without_plt);
2582 // R_ARM_PREL: (S + A) | T - P
2583 static inline typename This::Status
2584 prel31(unsigned char *view,
2585 const Sized_relobj<32, big_endian>* object,
2586 const Symbol_value<32>* psymval,
2587 Arm_address address,
2588 Arm_address thumb_bit)
2590 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2591 Valtype* wv = reinterpret_cast<Valtype*>(view);
2592 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2593 Valtype addend = utils::sign_extend<31>(val);
2594 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2595 val = utils::bit_select(val, x, 0x7fffffffU);
2596 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2597 return (utils::has_overflow<31>(x) ?
2598 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2601 // R_ARM_MOVW_ABS_NC: (S + A) | T
2602 static inline typename This::Status
2603 movw_abs_nc(unsigned char *view,
2604 const Sized_relobj<32, big_endian>* object,
2605 const Symbol_value<32>* psymval,
2606 Arm_address thumb_bit)
2608 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2609 Valtype* wv = reinterpret_cast<Valtype*>(view);
2610 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2611 Valtype addend = This::extract_arm_movw_movt_addend(val);
2612 Valtype x = psymval->value(object, addend) | thumb_bit;
2613 val = This::insert_val_arm_movw_movt(val, x);
2614 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2615 return This::STATUS_OKAY;
2618 // R_ARM_MOVT_ABS: S + A
2619 static inline typename This::Status
2620 movt_abs(unsigned char *view,
2621 const Sized_relobj<32, big_endian>* object,
2622 const Symbol_value<32>* psymval)
2624 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2625 Valtype* wv = reinterpret_cast<Valtype*>(view);
2626 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2627 Valtype addend = This::extract_arm_movw_movt_addend(val);
2628 Valtype x = psymval->value(object, addend) >> 16;
2629 val = This::insert_val_arm_movw_movt(val, x);
2630 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2631 return This::STATUS_OKAY;
2634 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2635 static inline typename This::Status
2636 thm_movw_abs_nc(unsigned char *view,
2637 const Sized_relobj<32, big_endian>* object,
2638 const Symbol_value<32>* psymval,
2639 Arm_address thumb_bit)
2641 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2642 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2643 Valtype* wv = reinterpret_cast<Valtype*>(view);
2644 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2645 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2646 Reltype addend = extract_thumb_movw_movt_addend(val);
2647 Reltype x = psymval->value(object, addend) | thumb_bit;
2648 val = This::insert_val_thumb_movw_movt(val, x);
2649 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2650 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2651 return This::STATUS_OKAY;
2654 // R_ARM_THM_MOVT_ABS: S + A
2655 static inline typename This::Status
2656 thm_movt_abs(unsigned char *view,
2657 const Sized_relobj<32, big_endian>* object,
2658 const Symbol_value<32>* psymval)
2660 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2661 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2662 Valtype* wv = reinterpret_cast<Valtype*>(view);
2663 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2664 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2665 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2666 Reltype x = psymval->value(object, addend) >> 16;
2667 val = This::insert_val_thumb_movw_movt(val, x);
2668 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2669 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2670 return This::STATUS_OKAY;
2673 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2674 static inline typename This::Status
2675 movw_prel_nc(unsigned char *view,
2676 const Sized_relobj<32, big_endian>* object,
2677 const Symbol_value<32>* psymval,
2678 Arm_address address,
2679 Arm_address thumb_bit)
2681 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2682 Valtype* wv = reinterpret_cast<Valtype*>(view);
2683 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2684 Valtype addend = This::extract_arm_movw_movt_addend(val);
2685 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2686 val = This::insert_val_arm_movw_movt(val, x);
2687 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2688 return This::STATUS_OKAY;
2691 // R_ARM_MOVT_PREL: S + A - P
2692 static inline typename This::Status
2693 movt_prel(unsigned char *view,
2694 const Sized_relobj<32, big_endian>* object,
2695 const Symbol_value<32>* psymval,
2696 Arm_address address)
2698 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2699 Valtype* wv = reinterpret_cast<Valtype*>(view);
2700 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2701 Valtype addend = This::extract_arm_movw_movt_addend(val);
2702 Valtype x = (psymval->value(object, addend) - address) >> 16;
2703 val = This::insert_val_arm_movw_movt(val, x);
2704 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2705 return This::STATUS_OKAY;
2708 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2709 static inline typename This::Status
2710 thm_movw_prel_nc(unsigned char *view,
2711 const Sized_relobj<32, big_endian>* object,
2712 const Symbol_value<32>* psymval,
2713 Arm_address address,
2714 Arm_address thumb_bit)
2716 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2717 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2718 Valtype* wv = reinterpret_cast<Valtype*>(view);
2719 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2720 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2721 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2722 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
2723 val = This::insert_val_thumb_movw_movt(val, x);
2724 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2725 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2726 return This::STATUS_OKAY;
2729 // R_ARM_THM_MOVT_PREL: S + A - P
2730 static inline typename This::Status
2731 thm_movt_prel(unsigned char *view,
2732 const Sized_relobj<32, big_endian>* object,
2733 const Symbol_value<32>* psymval,
2734 Arm_address address)
2736 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2737 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2738 Valtype* wv = reinterpret_cast<Valtype*>(view);
2739 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2740 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2741 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2742 Reltype x = (psymval->value(object, addend) - address) >> 16;
2743 val = This::insert_val_thumb_movw_movt(val, x);
2744 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2745 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2746 return This::STATUS_OKAY;
2750 // Relocate ARM long branches. This handles relocation types
2751 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2752 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2753 // undefined and we do not use PLT in this relocation. In such a case,
2754 // the branch is converted into an NOP.
2756 template<bool big_endian>
2757 typename Arm_relocate_functions<big_endian>::Status
2758 Arm_relocate_functions<big_endian>::arm_branch_common(
2759 unsigned int r_type,
2760 const Relocate_info<32, big_endian>* relinfo,
2761 unsigned char *view,
2762 const Sized_symbol<32>* gsym,
2763 const Arm_relobj<big_endian>* object,
2764 unsigned int r_sym,
2765 const Symbol_value<32>* psymval,
2766 Arm_address address,
2767 Arm_address thumb_bit,
2768 bool is_weakly_undefined_without_plt)
2770 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2771 Valtype* wv = reinterpret_cast<Valtype*>(view);
2772 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2774 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
2775 && ((val & 0x0f000000UL) == 0x0a000000UL);
2776 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
2777 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
2778 && ((val & 0x0f000000UL) == 0x0b000000UL);
2779 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
2780 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
2782 // Check that the instruction is valid.
2783 if (r_type == elfcpp::R_ARM_CALL)
2785 if (!insn_is_uncond_bl && !insn_is_blx)
2786 return This::STATUS_BAD_RELOC;
2788 else if (r_type == elfcpp::R_ARM_JUMP24)
2790 if (!insn_is_b && !insn_is_cond_bl)
2791 return This::STATUS_BAD_RELOC;
2793 else if (r_type == elfcpp::R_ARM_PLT32)
2795 if (!insn_is_any_branch)
2796 return This::STATUS_BAD_RELOC;
2798 else if (r_type == elfcpp::R_ARM_XPC25)
2800 // FIXME: AAELF document IH0044C does not say much about it other
2801 // than it being obsolete.
2802 if (!insn_is_any_branch)
2803 return This::STATUS_BAD_RELOC;
2805 else
2806 gold_unreachable();
2808 // A branch to an undefined weak symbol is turned into a jump to
2809 // the next instruction unless a PLT entry will be created.
2810 // Do the same for local undefined symbols.
2811 // The jump to the next instruction is optimized as a NOP depending
2812 // on the architecture.
2813 const Target_arm<big_endian>* arm_target =
2814 Target_arm<big_endian>::default_target();
2815 if (is_weakly_undefined_without_plt)
2817 Valtype cond = val & 0xf0000000U;
2818 if (arm_target->may_use_arm_nop())
2819 val = cond | 0x0320f000;
2820 else
2821 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2822 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2823 return This::STATUS_OKAY;
2826 Valtype addend = utils::sign_extend<26>(val << 2);
2827 Valtype branch_target = psymval->value(object, addend);
2828 int32_t branch_offset = branch_target - address;
2830 // We need a stub if the branch offset is too large or if we need
2831 // to switch mode.
2832 bool may_use_blx = arm_target->may_use_blx();
2833 Reloc_stub* stub = NULL;
2834 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
2835 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2836 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
2838 Stub_type stub_type =
2839 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2840 (thumb_bit != 0));
2841 if (stub_type != arm_stub_none)
2843 Stub_table<big_endian>* stub_table =
2844 object->stub_table(relinfo->data_shndx);
2845 gold_assert(stub_table != NULL);
2847 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2848 stub = stub_table->find_reloc_stub(stub_key);
2849 gold_assert(stub != NULL);
2850 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2851 branch_target = stub_table->address() + stub->offset() + addend;
2852 branch_offset = branch_target - address;
2853 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
2854 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
2858 // At this point, if we still need to switch mode, the instruction
2859 // must either be a BLX or a BL that can be converted to a BLX.
2860 if (thumb_bit != 0)
2862 // Turn BL to BLX.
2863 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
2864 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
2867 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
2868 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2869 return (utils::has_overflow<26>(branch_offset)
2870 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
2873 // Relocate THUMB long branches. This handles relocation types
2874 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2875 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2876 // undefined and we do not use PLT in this relocation. In such a case,
2877 // the branch is converted into an NOP.
2879 template<bool big_endian>
2880 typename Arm_relocate_functions<big_endian>::Status
2881 Arm_relocate_functions<big_endian>::thumb_branch_common(
2882 unsigned int r_type,
2883 const Relocate_info<32, big_endian>* relinfo,
2884 unsigned char *view,
2885 const Sized_symbol<32>* gsym,
2886 const Arm_relobj<big_endian>* object,
2887 unsigned int r_sym,
2888 const Symbol_value<32>* psymval,
2889 Arm_address address,
2890 Arm_address thumb_bit,
2891 bool is_weakly_undefined_without_plt)
2893 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2894 Valtype* wv = reinterpret_cast<Valtype*>(view);
2895 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2896 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2898 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2899 // into account.
2900 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
2901 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
2903 // Check that the instruction is valid.
2904 if (r_type == elfcpp::R_ARM_THM_CALL)
2906 if (!is_bl_insn && !is_blx_insn)
2907 return This::STATUS_BAD_RELOC;
2909 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
2911 // This cannot be a BLX.
2912 if (!is_bl_insn)
2913 return This::STATUS_BAD_RELOC;
2915 else if (r_type == elfcpp::R_ARM_THM_XPC22)
2917 // Check for Thumb to Thumb call.
2918 if (!is_blx_insn)
2919 return This::STATUS_BAD_RELOC;
2920 if (thumb_bit != 0)
2922 gold_warning(_("%s: Thumb BLX instruction targets "
2923 "thumb function '%s'."),
2924 object->name().c_str(),
2925 (gsym ? gsym->name() : "(local)"));
2926 // Convert BLX to BL.
2927 lower_insn |= 0x1000U;
2930 else
2931 gold_unreachable();
2933 // A branch to an undefined weak symbol is turned into a jump to
2934 // the next instruction unless a PLT entry will be created.
2935 // The jump to the next instruction is optimized as a NOP.W for
2936 // Thumb-2 enabled architectures.
2937 const Target_arm<big_endian>* arm_target =
2938 Target_arm<big_endian>::default_target();
2939 if (is_weakly_undefined_without_plt)
2941 if (arm_target->may_use_thumb2_nop())
2943 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
2944 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
2946 else
2948 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
2949 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
2951 return This::STATUS_OKAY;
2954 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
2955 Arm_address branch_target = psymval->value(object, addend);
2956 int32_t branch_offset = branch_target - address;
2958 // We need a stub if the branch offset is too large or if we need
2959 // to switch mode.
2960 bool may_use_blx = arm_target->may_use_blx();
2961 bool thumb2 = arm_target->using_thumb2();
2962 if ((!thumb2
2963 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2964 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2965 || (thumb2
2966 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2967 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2968 || ((thumb_bit == 0)
2969 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2970 || r_type == elfcpp::R_ARM_THM_JUMP24)))
2972 Stub_type stub_type =
2973 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2974 (thumb_bit != 0));
2975 if (stub_type != arm_stub_none)
2977 Stub_table<big_endian>* stub_table =
2978 object->stub_table(relinfo->data_shndx);
2979 gold_assert(stub_table != NULL);
2981 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2982 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
2983 gold_assert(stub != NULL);
2984 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2985 branch_target = stub_table->address() + stub->offset() + addend;
2986 branch_offset = branch_target - address;
2990 // At this point, if we still need to switch mode, the instruction
2991 // must either be a BLX or a BL that can be converted to a BLX.
2992 if (thumb_bit == 0)
2994 gold_assert(may_use_blx
2995 && (r_type == elfcpp::R_ARM_THM_CALL
2996 || r_type == elfcpp::R_ARM_THM_XPC22));
2997 // Make sure this is a BLX.
2998 lower_insn &= ~0x1000U;
3000 else
3002 // Make sure this is a BL.
3003 lower_insn |= 0x1000U;
3006 if ((lower_insn & 0x5000U) == 0x4000U)
3007 // For a BLX instruction, make sure that the relocation is rounded up
3008 // to a word boundary. This follows the semantics of the instruction
3009 // which specifies that bit 1 of the target address will come from bit
3010 // 1 of the base address.
3011 branch_offset = (branch_offset + 2) & ~3;
3013 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3014 // We use the Thumb-2 encoding, which is safe even if dealing with
3015 // a Thumb-1 instruction by virtue of our overflow check above. */
3016 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
3017 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
3019 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3020 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3022 return ((thumb2
3023 ? utils::has_overflow<25>(branch_offset)
3024 : utils::has_overflow<23>(branch_offset))
3025 ? This::STATUS_OVERFLOW
3026 : This::STATUS_OKAY);
3029 // Relocate THUMB-2 long conditional branches.
3030 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3031 // undefined and we do not use PLT in this relocation. In such a case,
3032 // the branch is converted into an NOP.
3034 template<bool big_endian>
3035 typename Arm_relocate_functions<big_endian>::Status
3036 Arm_relocate_functions<big_endian>::thm_jump19(
3037 unsigned char *view,
3038 const Arm_relobj<big_endian>* object,
3039 const Symbol_value<32>* psymval,
3040 Arm_address address,
3041 Arm_address thumb_bit)
3043 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3044 Valtype* wv = reinterpret_cast<Valtype*>(view);
3045 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3046 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3047 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
3049 Arm_address branch_target = psymval->value(object, addend);
3050 int32_t branch_offset = branch_target - address;
3052 // ??? Should handle interworking? GCC might someday try to
3053 // use this for tail calls.
3054 // FIXME: We do support thumb entry to PLT yet.
3055 if (thumb_bit == 0)
3057 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3058 return This::STATUS_BAD_RELOC;
3061 // Put RELOCATION back into the insn.
3062 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
3063 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
3065 // Put the relocated value back in the object file:
3066 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3067 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3069 return (utils::has_overflow<21>(branch_offset)
3070 ? This::STATUS_OVERFLOW
3071 : This::STATUS_OKAY);
3074 // Get the GOT section, creating it if necessary.
3076 template<bool big_endian>
3077 Output_data_got<32, big_endian>*
3078 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
3080 if (this->got_ == NULL)
3082 gold_assert(symtab != NULL && layout != NULL);
3084 this->got_ = new Output_data_got<32, big_endian>();
3086 Output_section* os;
3087 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3088 (elfcpp::SHF_ALLOC
3089 | elfcpp::SHF_WRITE),
3090 this->got_, false, true, true,
3091 false);
3093 // The old GNU linker creates a .got.plt section. We just
3094 // create another set of data in the .got section. Note that we
3095 // always create a PLT if we create a GOT, although the PLT
3096 // might be empty.
3097 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
3098 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3099 (elfcpp::SHF_ALLOC
3100 | elfcpp::SHF_WRITE),
3101 this->got_plt_, false, false,
3102 false, true);
3104 // The first three entries are reserved.
3105 this->got_plt_->set_current_data_size(3 * 4);
3107 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3108 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3109 Symbol_table::PREDEFINED,
3110 this->got_plt_,
3111 0, 0, elfcpp::STT_OBJECT,
3112 elfcpp::STB_LOCAL,
3113 elfcpp::STV_HIDDEN, 0,
3114 false, false);
3116 return this->got_;
3119 // Get the dynamic reloc section, creating it if necessary.
3121 template<bool big_endian>
3122 typename Target_arm<big_endian>::Reloc_section*
3123 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
3125 if (this->rel_dyn_ == NULL)
3127 gold_assert(layout != NULL);
3128 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
3129 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
3130 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
3131 false, false, false);
3133 return this->rel_dyn_;
3136 // Insn_template methods.
3138 // Return byte size of an instruction template.
3140 size_t
3141 Insn_template::size() const
3143 switch (this->type())
3145 case THUMB16_TYPE:
3146 case THUMB16_SPECIAL_TYPE:
3147 return 2;
3148 case ARM_TYPE:
3149 case THUMB32_TYPE:
3150 case DATA_TYPE:
3151 return 4;
3152 default:
3153 gold_unreachable();
3157 // Return alignment of an instruction template.
3159 unsigned
3160 Insn_template::alignment() const
3162 switch (this->type())
3164 case THUMB16_TYPE:
3165 case THUMB16_SPECIAL_TYPE:
3166 case THUMB32_TYPE:
3167 return 2;
3168 case ARM_TYPE:
3169 case DATA_TYPE:
3170 return 4;
3171 default:
3172 gold_unreachable();
3176 // Stub_template methods.
3178 Stub_template::Stub_template(
3179 Stub_type type, const Insn_template* insns,
3180 size_t insn_count)
3181 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
3182 entry_in_thumb_mode_(false), relocs_()
3184 off_t offset = 0;
3186 // Compute byte size and alignment of stub template.
3187 for (size_t i = 0; i < insn_count; i++)
3189 unsigned insn_alignment = insns[i].alignment();
3190 size_t insn_size = insns[i].size();
3191 gold_assert((offset & (insn_alignment - 1)) == 0);
3192 this->alignment_ = std::max(this->alignment_, insn_alignment);
3193 switch (insns[i].type())
3195 case Insn_template::THUMB16_TYPE:
3196 case Insn_template::THUMB16_SPECIAL_TYPE:
3197 if (i == 0)
3198 this->entry_in_thumb_mode_ = true;
3199 break;
3201 case Insn_template::THUMB32_TYPE:
3202 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
3203 this->relocs_.push_back(Reloc(i, offset));
3204 if (i == 0)
3205 this->entry_in_thumb_mode_ = true;
3206 break;
3208 case Insn_template::ARM_TYPE:
3209 // Handle cases where the target is encoded within the
3210 // instruction.
3211 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
3212 this->relocs_.push_back(Reloc(i, offset));
3213 break;
3215 case Insn_template::DATA_TYPE:
3216 // Entry point cannot be data.
3217 gold_assert(i != 0);
3218 this->relocs_.push_back(Reloc(i, offset));
3219 break;
3221 default:
3222 gold_unreachable();
3224 offset += insn_size;
3226 this->size_ = offset;
3229 // Stub methods.
3231 // Template to implement do_write for a specific target endianity.
3233 template<bool big_endian>
3234 void inline
3235 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
3237 const Stub_template* stub_template = this->stub_template();
3238 const Insn_template* insns = stub_template->insns();
3240 // FIXME: We do not handle BE8 encoding yet.
3241 unsigned char* pov = view;
3242 for (size_t i = 0; i < stub_template->insn_count(); i++)
3244 switch (insns[i].type())
3246 case Insn_template::THUMB16_TYPE:
3247 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
3248 break;
3249 case Insn_template::THUMB16_SPECIAL_TYPE:
3250 elfcpp::Swap<16, big_endian>::writeval(
3251 pov,
3252 this->thumb16_special(i));
3253 break;
3254 case Insn_template::THUMB32_TYPE:
3256 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
3257 uint32_t lo = insns[i].data() & 0xffff;
3258 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
3259 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
3261 break;
3262 case Insn_template::ARM_TYPE:
3263 case Insn_template::DATA_TYPE:
3264 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
3265 break;
3266 default:
3267 gold_unreachable();
3269 pov += insns[i].size();
3271 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
3274 // Reloc_stub::Key methods.
3276 // Dump a Key as a string for debugging.
3278 std::string
3279 Reloc_stub::Key::name() const
3281 if (this->r_sym_ == invalid_index)
3283 // Global symbol key name
3284 // <stub-type>:<symbol name>:<addend>.
3285 const std::string sym_name = this->u_.symbol->name();
3286 // We need to print two hex number and two colons. So just add 100 bytes
3287 // to the symbol name size.
3288 size_t len = sym_name.size() + 100;
3289 char* buffer = new char[len];
3290 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
3291 sym_name.c_str(), this->addend_);
3292 gold_assert(c > 0 && c < static_cast<int>(len));
3293 delete[] buffer;
3294 return std::string(buffer);
3296 else
3298 // local symbol key name
3299 // <stub-type>:<object>:<r_sym>:<addend>.
3300 const size_t len = 200;
3301 char buffer[len];
3302 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
3303 this->u_.relobj, this->r_sym_, this->addend_);
3304 gold_assert(c > 0 && c < static_cast<int>(len));
3305 return std::string(buffer);
3309 // Reloc_stub methods.
3311 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3312 // LOCATION to DESTINATION.
3313 // This code is based on the arm_type_of_stub function in
3314 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3315 // class simple.
3317 Stub_type
3318 Reloc_stub::stub_type_for_reloc(
3319 unsigned int r_type,
3320 Arm_address location,
3321 Arm_address destination,
3322 bool target_is_thumb)
3324 Stub_type stub_type = arm_stub_none;
3326 // This is a bit ugly but we want to avoid using a templated class for
3327 // big and little endianities.
3328 bool may_use_blx;
3329 bool should_force_pic_veneer;
3330 bool thumb2;
3331 bool thumb_only;
3332 if (parameters->target().is_big_endian())
3334 const Target_arm<true>* big_endian_target =
3335 Target_arm<true>::default_target();
3336 may_use_blx = big_endian_target->may_use_blx();
3337 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
3338 thumb2 = big_endian_target->using_thumb2();
3339 thumb_only = big_endian_target->using_thumb_only();
3341 else
3343 const Target_arm<false>* little_endian_target =
3344 Target_arm<false>::default_target();
3345 may_use_blx = little_endian_target->may_use_blx();
3346 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
3347 thumb2 = little_endian_target->using_thumb2();
3348 thumb_only = little_endian_target->using_thumb_only();
3351 int64_t branch_offset = (int64_t)destination - location;
3353 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
3355 // Handle cases where:
3356 // - this call goes too far (different Thumb/Thumb2 max
3357 // distance)
3358 // - it's a Thumb->Arm call and blx is not available, or it's a
3359 // Thumb->Arm branch (not bl). A stub is needed in this case.
3360 if ((!thumb2
3361 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3362 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3363 || (thumb2
3364 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3365 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3366 || ((!target_is_thumb)
3367 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3368 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
3370 if (target_is_thumb)
3372 // Thumb to thumb.
3373 if (!thumb_only)
3375 stub_type = (parameters->options().shared()
3376 || should_force_pic_veneer)
3377 // PIC stubs.
3378 ? ((may_use_blx
3379 && (r_type == elfcpp::R_ARM_THM_CALL))
3380 // V5T and above. Stub starts with ARM code, so
3381 // we must be able to switch mode before
3382 // reaching it, which is only possible for 'bl'
3383 // (ie R_ARM_THM_CALL relocation).
3384 ? arm_stub_long_branch_any_thumb_pic
3385 // On V4T, use Thumb code only.
3386 : arm_stub_long_branch_v4t_thumb_thumb_pic)
3388 // non-PIC stubs.
3389 : ((may_use_blx
3390 && (r_type == elfcpp::R_ARM_THM_CALL))
3391 ? arm_stub_long_branch_any_any // V5T and above.
3392 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
3394 else
3396 stub_type = (parameters->options().shared()
3397 || should_force_pic_veneer)
3398 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
3399 : arm_stub_long_branch_thumb_only; // non-PIC stub.
3402 else
3404 // Thumb to arm.
3406 // FIXME: We should check that the input section is from an
3407 // object that has interwork enabled.
3409 stub_type = (parameters->options().shared()
3410 || should_force_pic_veneer)
3411 // PIC stubs.
3412 ? ((may_use_blx
3413 && (r_type == elfcpp::R_ARM_THM_CALL))
3414 ? arm_stub_long_branch_any_arm_pic // V5T and above.
3415 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
3417 // non-PIC stubs.
3418 : ((may_use_blx
3419 && (r_type == elfcpp::R_ARM_THM_CALL))
3420 ? arm_stub_long_branch_any_any // V5T and above.
3421 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
3423 // Handle v4t short branches.
3424 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3425 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3426 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3427 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3431 else if (r_type == elfcpp::R_ARM_CALL
3432 || r_type == elfcpp::R_ARM_JUMP24
3433 || r_type == elfcpp::R_ARM_PLT32)
3435 if (target_is_thumb)
3437 // Arm to thumb.
3439 // FIXME: We should check that the input section is from an
3440 // object that has interwork enabled.
3442 // We have an extra 2-bytes reach because of
3443 // the mode change (bit 24 (H) of BLX encoding).
3444 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3445 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3446 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
3447 || (r_type == elfcpp::R_ARM_JUMP24)
3448 || (r_type == elfcpp::R_ARM_PLT32))
3450 stub_type = (parameters->options().shared()
3451 || should_force_pic_veneer)
3452 // PIC stubs.
3453 ? (may_use_blx
3454 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
3455 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
3457 // non-PIC stubs.
3458 : (may_use_blx
3459 ? arm_stub_long_branch_any_any // V5T and above.
3460 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
3463 else
3465 // Arm to arm.
3466 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3467 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3469 stub_type = (parameters->options().shared()
3470 || should_force_pic_veneer)
3471 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
3472 : arm_stub_long_branch_any_any; /// non-PIC.
3477 return stub_type;
3480 // Cortex_a8_stub methods.
3482 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3483 // I is the position of the instruction template in the stub template.
3485 uint16_t
3486 Cortex_a8_stub::do_thumb16_special(size_t i)
3488 // The only use of this is to copy condition code from a conditional
3489 // branch being worked around to the corresponding conditional branch in
3490 // to the stub.
3491 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3492 && i == 0);
3493 uint16_t data = this->stub_template()->insns()[i].data();
3494 gold_assert((data & 0xff00U) == 0xd000U);
3495 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
3496 return data;
3499 // Stub_factory methods.
3501 Stub_factory::Stub_factory()
3503 // The instruction template sequences are declared as static
3504 // objects and initialized first time the constructor runs.
3506 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3507 // to reach the stub if necessary.
3508 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
3510 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3511 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3512 // dcd R_ARM_ABS32(X)
3515 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3516 // available.
3517 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
3519 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3520 Insn_template::arm_insn(0xe12fff1c), // bx ip
3521 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3522 // dcd R_ARM_ABS32(X)
3525 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3526 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
3528 Insn_template::thumb16_insn(0xb401), // push {r0}
3529 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3530 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3531 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3532 Insn_template::thumb16_insn(0x4760), // bx ip
3533 Insn_template::thumb16_insn(0xbf00), // nop
3534 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3535 // dcd R_ARM_ABS32(X)
3538 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3539 // allowed.
3540 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
3542 Insn_template::thumb16_insn(0x4778), // bx pc
3543 Insn_template::thumb16_insn(0x46c0), // nop
3544 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3545 Insn_template::arm_insn(0xe12fff1c), // bx ip
3546 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3547 // dcd R_ARM_ABS32(X)
3550 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3551 // available.
3552 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
3554 Insn_template::thumb16_insn(0x4778), // bx pc
3555 Insn_template::thumb16_insn(0x46c0), // nop
3556 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3557 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3558 // dcd R_ARM_ABS32(X)
3561 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3562 // one, when the destination is close enough.
3563 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
3565 Insn_template::thumb16_insn(0x4778), // bx pc
3566 Insn_template::thumb16_insn(0x46c0), // nop
3567 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3570 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3571 // blx to reach the stub if necessary.
3572 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
3574 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3575 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3576 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3577 // dcd R_ARM_REL32(X-4)
3580 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3581 // blx to reach the stub if necessary. We can not add into pc;
3582 // it is not guaranteed to mode switch (different in ARMv6 and
3583 // ARMv7).
3584 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
3586 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3587 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3588 Insn_template::arm_insn(0xe12fff1c), // bx ip
3589 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3590 // dcd R_ARM_REL32(X)
3593 // V4T ARM -> ARM long branch stub, PIC.
3594 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
3596 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3597 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3598 Insn_template::arm_insn(0xe12fff1c), // bx ip
3599 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3600 // dcd R_ARM_REL32(X)
3603 // V4T Thumb -> ARM long branch stub, PIC.
3604 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
3606 Insn_template::thumb16_insn(0x4778), // bx pc
3607 Insn_template::thumb16_insn(0x46c0), // nop
3608 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3609 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3610 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3611 // dcd R_ARM_REL32(X)
3614 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3615 // architectures.
3616 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
3618 Insn_template::thumb16_insn(0xb401), // push {r0}
3619 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3620 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3621 Insn_template::thumb16_insn(0x4484), // add ip, r0
3622 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3623 Insn_template::thumb16_insn(0x4760), // bx ip
3624 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
3625 // dcd R_ARM_REL32(X)
3628 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3629 // allowed.
3630 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
3632 Insn_template::thumb16_insn(0x4778), // bx pc
3633 Insn_template::thumb16_insn(0x46c0), // nop
3634 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3635 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3636 Insn_template::arm_insn(0xe12fff1c), // bx ip
3637 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3638 // dcd R_ARM_REL32(X)
3641 // Cortex-A8 erratum-workaround stubs.
3643 // Stub used for conditional branches (which may be beyond +/-1MB away,
3644 // so we can't use a conditional branch to reach this stub).
3646 // original code:
3648 // b<cond> X
3649 // after:
3651 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
3653 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3654 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3655 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3656 // b.w X
3659 // Stub used for b.w and bl.w instructions.
3661 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
3663 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3666 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
3668 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3671 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3672 // instruction (which switches to ARM mode) to point to this stub. Jump to
3673 // the real destination using an ARM-mode branch.
3674 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
3676 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3679 // Fill in the stub template look-up table. Stub templates are constructed
3680 // per instance of Stub_factory for fast look-up without locking
3681 // in a thread-enabled environment.
3683 this->stub_templates_[arm_stub_none] =
3684 new Stub_template(arm_stub_none, NULL, 0);
3686 #define DEF_STUB(x) \
3687 do \
3689 size_t array_size \
3690 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3691 Stub_type type = arm_stub_##x; \
3692 this->stub_templates_[type] = \
3693 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3695 while (0);
3697 DEF_STUBS
3698 #undef DEF_STUB
3701 // Stub_table methods.
3703 // Removel all Cortex-A8 stub.
3705 template<bool big_endian>
3706 void
3707 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
3709 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
3710 p != this->cortex_a8_stubs_.end();
3711 ++p)
3712 delete p->second;
3713 this->cortex_a8_stubs_.clear();
3716 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
3718 template<bool big_endian>
3719 void
3720 Stub_table<big_endian>::relocate_stub(
3721 Stub* stub,
3722 const Relocate_info<32, big_endian>* relinfo,
3723 Target_arm<big_endian>* arm_target,
3724 Output_section* output_section,
3725 unsigned char* view,
3726 Arm_address address,
3727 section_size_type view_size)
3729 const Stub_template* stub_template = stub->stub_template();
3730 if (stub_template->reloc_count() != 0)
3732 // Adjust view to cover the stub only.
3733 section_size_type offset = stub->offset();
3734 section_size_type stub_size = stub_template->size();
3735 gold_assert(offset + stub_size <= view_size);
3737 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
3738 address + offset, stub_size);
3742 // Relocate all stubs in this stub table.
3744 template<bool big_endian>
3745 void
3746 Stub_table<big_endian>::relocate_stubs(
3747 const Relocate_info<32, big_endian>* relinfo,
3748 Target_arm<big_endian>* arm_target,
3749 Output_section* output_section,
3750 unsigned char* view,
3751 Arm_address address,
3752 section_size_type view_size)
3754 // If we are passed a view bigger than the stub table's. we need to
3755 // adjust the view.
3756 gold_assert(address == this->address()
3757 && (view_size
3758 == static_cast<section_size_type>(this->data_size())));
3760 // Relocate all relocation stubs.
3761 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3762 p != this->reloc_stubs_.end();
3763 ++p)
3764 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
3765 address, view_size);
3767 // Relocate all Cortex-A8 stubs.
3768 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
3769 p != this->cortex_a8_stubs_.end();
3770 ++p)
3771 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
3772 address, view_size);
3775 // Write out the stubs to file.
3777 template<bool big_endian>
3778 void
3779 Stub_table<big_endian>::do_write(Output_file* of)
3781 off_t offset = this->offset();
3782 const section_size_type oview_size =
3783 convert_to_section_size_type(this->data_size());
3784 unsigned char* const oview = of->get_output_view(offset, oview_size);
3786 // Write relocation stubs.
3787 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3788 p != this->reloc_stubs_.end();
3789 ++p)
3791 Reloc_stub* stub = p->second;
3792 Arm_address address = this->address() + stub->offset();
3793 gold_assert(address
3794 == align_address(address,
3795 stub->stub_template()->alignment()));
3796 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3797 big_endian);
3800 // Write Cortex-A8 stubs.
3801 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3802 p != this->cortex_a8_stubs_.end();
3803 ++p)
3805 Cortex_a8_stub* stub = p->second;
3806 Arm_address address = this->address() + stub->offset();
3807 gold_assert(address
3808 == align_address(address,
3809 stub->stub_template()->alignment()));
3810 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3811 big_endian);
3814 of->write_output_view(this->offset(), oview_size, oview);
3817 // Update the data size and address alignment of the stub table at the end
3818 // of a relaxation pass. Return true if either the data size or the
3819 // alignment changed in this relaxation pass.
3821 template<bool big_endian>
3822 bool
3823 Stub_table<big_endian>::update_data_size_and_addralign()
3825 off_t size = 0;
3826 unsigned addralign = 1;
3828 // Go over all stubs in table to compute data size and address alignment.
3830 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3831 p != this->reloc_stubs_.end();
3832 ++p)
3834 const Stub_template* stub_template = p->second->stub_template();
3835 addralign = std::max(addralign, stub_template->alignment());
3836 size = (align_address(size, stub_template->alignment())
3837 + stub_template->size());
3840 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3841 p != this->cortex_a8_stubs_.end();
3842 ++p)
3844 const Stub_template* stub_template = p->second->stub_template();
3845 addralign = std::max(addralign, stub_template->alignment());
3846 size = (align_address(size, stub_template->alignment())
3847 + stub_template->size());
3850 // Check if either data size or alignment changed in this pass.
3851 // Update prev_data_size_ and prev_addralign_. These will be used
3852 // as the current data size and address alignment for the next pass.
3853 bool changed = size != this->prev_data_size_;
3854 this->prev_data_size_ = size;
3856 if (addralign != this->prev_addralign_)
3857 changed = true;
3858 this->prev_addralign_ = addralign;
3860 return changed;
3863 // Finalize the stubs. This sets the offsets of the stubs within the stub
3864 // table. It also marks all input sections needing Cortex-A8 workaround.
3866 template<bool big_endian>
3867 void
3868 Stub_table<big_endian>::finalize_stubs()
3870 off_t off = 0;
3871 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3872 p != this->reloc_stubs_.end();
3873 ++p)
3875 Reloc_stub* stub = p->second;
3876 const Stub_template* stub_template = stub->stub_template();
3877 uint64_t stub_addralign = stub_template->alignment();
3878 off = align_address(off, stub_addralign);
3879 stub->set_offset(off);
3880 off += stub_template->size();
3883 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3884 p != this->cortex_a8_stubs_.end();
3885 ++p)
3887 Cortex_a8_stub* stub = p->second;
3888 const Stub_template* stub_template = stub->stub_template();
3889 uint64_t stub_addralign = stub_template->alignment();
3890 off = align_address(off, stub_addralign);
3891 stub->set_offset(off);
3892 off += stub_template->size();
3894 // Mark input section so that we can determine later if a code section
3895 // needs the Cortex-A8 workaround quickly.
3896 Arm_relobj<big_endian>* arm_relobj =
3897 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
3898 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
3901 gold_assert(off <= this->prev_data_size_);
3904 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
3905 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
3906 // of the address range seen by the linker.
3908 template<bool big_endian>
3909 void
3910 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
3911 Target_arm<big_endian>* arm_target,
3912 unsigned char* view,
3913 Arm_address view_address,
3914 section_size_type view_size)
3916 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
3917 for (Cortex_a8_stub_list::const_iterator p =
3918 this->cortex_a8_stubs_.lower_bound(view_address);
3919 ((p != this->cortex_a8_stubs_.end())
3920 && (p->first < (view_address + view_size)));
3921 ++p)
3923 // We do not store the THUMB bit in the LSB of either the branch address
3924 // or the stub offset. There is no need to strip the LSB.
3925 Arm_address branch_address = p->first;
3926 const Cortex_a8_stub* stub = p->second;
3927 Arm_address stub_address = this->address() + stub->offset();
3929 // Offset of the branch instruction relative to this view.
3930 section_size_type offset =
3931 convert_to_section_size_type(branch_address - view_address);
3932 gold_assert((offset + 4) <= view_size);
3934 arm_target->apply_cortex_a8_workaround(stub, stub_address,
3935 view + offset, branch_address);
3939 // Arm_input_section methods.
3941 // Initialize an Arm_input_section.
3943 template<bool big_endian>
3944 void
3945 Arm_input_section<big_endian>::init()
3947 Relobj* relobj = this->relobj();
3948 unsigned int shndx = this->shndx();
3950 // Cache these to speed up size and alignment queries. It is too slow
3951 // to call section_addraglin and section_size every time.
3952 this->original_addralign_ = relobj->section_addralign(shndx);
3953 this->original_size_ = relobj->section_size(shndx);
3955 // We want to make this look like the original input section after
3956 // output sections are finalized.
3957 Output_section* os = relobj->output_section(shndx);
3958 off_t offset = relobj->output_section_offset(shndx);
3959 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
3960 this->set_address(os->address() + offset);
3961 this->set_file_offset(os->offset() + offset);
3963 this->set_current_data_size(this->original_size_);
3964 this->finalize_data_size();
3967 template<bool big_endian>
3968 void
3969 Arm_input_section<big_endian>::do_write(Output_file* of)
3971 // We have to write out the original section content.
3972 section_size_type section_size;
3973 const unsigned char* section_contents =
3974 this->relobj()->section_contents(this->shndx(), &section_size, false);
3975 of->write(this->offset(), section_contents, section_size);
3977 // If this owns a stub table and it is not empty, write it.
3978 if (this->is_stub_table_owner() && !this->stub_table_->empty())
3979 this->stub_table_->write(of);
3982 // Finalize data size.
3984 template<bool big_endian>
3985 void
3986 Arm_input_section<big_endian>::set_final_data_size()
3988 // If this owns a stub table, finalize its data size as well.
3989 if (this->is_stub_table_owner())
3991 uint64_t address = this->address();
3993 // The stub table comes after the original section contents.
3994 address += this->original_size_;
3995 address = align_address(address, this->stub_table_->addralign());
3996 off_t offset = this->offset() + (address - this->address());
3997 this->stub_table_->set_address_and_file_offset(address, offset);
3998 address += this->stub_table_->data_size();
3999 gold_assert(address == this->address() + this->current_data_size());
4002 this->set_data_size(this->current_data_size());
4005 // Reset address and file offset.
4007 template<bool big_endian>
4008 void
4009 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
4011 // Size of the original input section contents.
4012 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4014 // If this is a stub table owner, account for the stub table size.
4015 if (this->is_stub_table_owner())
4017 Stub_table<big_endian>* stub_table = this->stub_table_;
4019 // Reset the stub table's address and file offset. The
4020 // current data size for child will be updated after that.
4021 stub_table_->reset_address_and_file_offset();
4022 off = align_address(off, stub_table_->addralign());
4023 off += stub_table->current_data_size();
4026 this->set_current_data_size(off);
4029 // Arm_output_section methods.
4031 // Create a stub group for input sections from BEGIN to END. OWNER
4032 // points to the input section to be the owner a new stub table.
4034 template<bool big_endian>
4035 void
4036 Arm_output_section<big_endian>::create_stub_group(
4037 Input_section_list::const_iterator begin,
4038 Input_section_list::const_iterator end,
4039 Input_section_list::const_iterator owner,
4040 Target_arm<big_endian>* target,
4041 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
4043 // Currently we convert ordinary input sections into relaxed sections only
4044 // at this point but we may want to support creating relaxed input section
4045 // very early. So we check here to see if owner is already a relaxed
4046 // section.
4048 Arm_input_section<big_endian>* arm_input_section;
4049 if (owner->is_relaxed_input_section())
4051 arm_input_section =
4052 Arm_input_section<big_endian>::as_arm_input_section(
4053 owner->relaxed_input_section());
4055 else
4057 gold_assert(owner->is_input_section());
4058 // Create a new relaxed input section.
4059 arm_input_section =
4060 target->new_arm_input_section(owner->relobj(), owner->shndx());
4061 new_relaxed_sections->push_back(arm_input_section);
4064 // Create a stub table.
4065 Stub_table<big_endian>* stub_table =
4066 target->new_stub_table(arm_input_section);
4068 arm_input_section->set_stub_table(stub_table);
4070 Input_section_list::const_iterator p = begin;
4071 Input_section_list::const_iterator prev_p;
4073 // Look for input sections or relaxed input sections in [begin ... end].
4076 if (p->is_input_section() || p->is_relaxed_input_section())
4078 // The stub table information for input sections live
4079 // in their objects.
4080 Arm_relobj<big_endian>* arm_relobj =
4081 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
4082 arm_relobj->set_stub_table(p->shndx(), stub_table);
4084 prev_p = p++;
4086 while (prev_p != end);
4089 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
4090 // of stub groups. We grow a stub group by adding input section until the
4091 // size is just below GROUP_SIZE. The last input section will be converted
4092 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
4093 // input section after the stub table, effectively double the group size.
4095 // This is similar to the group_sections() function in elf32-arm.c but is
4096 // implemented differently.
4098 template<bool big_endian>
4099 void
4100 Arm_output_section<big_endian>::group_sections(
4101 section_size_type group_size,
4102 bool stubs_always_after_branch,
4103 Target_arm<big_endian>* target)
4105 // We only care about sections containing code.
4106 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
4107 return;
4109 // States for grouping.
4110 typedef enum
4112 // No group is being built.
4113 NO_GROUP,
4114 // A group is being built but the stub table is not found yet.
4115 // We keep group a stub group until the size is just under GROUP_SIZE.
4116 // The last input section in the group will be used as the stub table.
4117 FINDING_STUB_SECTION,
4118 // A group is being built and we have already found a stub table.
4119 // We enter this state to grow a stub group by adding input section
4120 // after the stub table. This effectively doubles the group size.
4121 HAS_STUB_SECTION
4122 } State;
4124 // Any newly created relaxed sections are stored here.
4125 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
4127 State state = NO_GROUP;
4128 section_size_type off = 0;
4129 section_size_type group_begin_offset = 0;
4130 section_size_type group_end_offset = 0;
4131 section_size_type stub_table_end_offset = 0;
4132 Input_section_list::const_iterator group_begin =
4133 this->input_sections().end();
4134 Input_section_list::const_iterator stub_table =
4135 this->input_sections().end();
4136 Input_section_list::const_iterator group_end = this->input_sections().end();
4137 for (Input_section_list::const_iterator p = this->input_sections().begin();
4138 p != this->input_sections().end();
4139 ++p)
4141 section_size_type section_begin_offset =
4142 align_address(off, p->addralign());
4143 section_size_type section_end_offset =
4144 section_begin_offset + p->data_size();
4146 // Check to see if we should group the previously seens sections.
4147 switch (state)
4149 case NO_GROUP:
4150 break;
4152 case FINDING_STUB_SECTION:
4153 // Adding this section makes the group larger than GROUP_SIZE.
4154 if (section_end_offset - group_begin_offset >= group_size)
4156 if (stubs_always_after_branch)
4158 gold_assert(group_end != this->input_sections().end());
4159 this->create_stub_group(group_begin, group_end, group_end,
4160 target, &new_relaxed_sections);
4161 state = NO_GROUP;
4163 else
4165 // But wait, there's more! Input sections up to
4166 // stub_group_size bytes after the stub table can be
4167 // handled by it too.
4168 state = HAS_STUB_SECTION;
4169 stub_table = group_end;
4170 stub_table_end_offset = group_end_offset;
4173 break;
4175 case HAS_STUB_SECTION:
4176 // Adding this section makes the post stub-section group larger
4177 // than GROUP_SIZE.
4178 if (section_end_offset - stub_table_end_offset >= group_size)
4180 gold_assert(group_end != this->input_sections().end());
4181 this->create_stub_group(group_begin, group_end, stub_table,
4182 target, &new_relaxed_sections);
4183 state = NO_GROUP;
4185 break;
4187 default:
4188 gold_unreachable();
4191 // If we see an input section and currently there is no group, start
4192 // a new one. Skip any empty sections.
4193 if ((p->is_input_section() || p->is_relaxed_input_section())
4194 && (p->relobj()->section_size(p->shndx()) != 0))
4196 if (state == NO_GROUP)
4198 state = FINDING_STUB_SECTION;
4199 group_begin = p;
4200 group_begin_offset = section_begin_offset;
4203 // Keep track of the last input section seen.
4204 group_end = p;
4205 group_end_offset = section_end_offset;
4208 off = section_end_offset;
4211 // Create a stub group for any ungrouped sections.
4212 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
4214 gold_assert(group_end != this->input_sections().end());
4215 this->create_stub_group(group_begin, group_end,
4216 (state == FINDING_STUB_SECTION
4217 ? group_end
4218 : stub_table),
4219 target, &new_relaxed_sections);
4222 // Convert input section into relaxed input section in a batch.
4223 if (!new_relaxed_sections.empty())
4224 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
4226 // Update the section offsets
4227 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
4229 Arm_relobj<big_endian>* arm_relobj =
4230 Arm_relobj<big_endian>::as_arm_relobj(
4231 new_relaxed_sections[i]->relobj());
4232 unsigned int shndx = new_relaxed_sections[i]->shndx();
4233 // Tell Arm_relobj that this input section is converted.
4234 arm_relobj->convert_input_section_to_relaxed_section(shndx);
4238 // Arm_relobj methods.
4240 // Determine if we want to scan the SHNDX-th section for relocation stubs.
4241 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4243 template<bool big_endian>
4244 bool
4245 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
4246 const elfcpp::Shdr<32, big_endian>& shdr,
4247 const Relobj::Output_sections& out_sections,
4248 const Symbol_table *symtab)
4250 unsigned int sh_type = shdr.get_sh_type();
4251 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
4252 return false;
4254 // Ignore empty section.
4255 off_t sh_size = shdr.get_sh_size();
4256 if (sh_size == 0)
4257 return false;
4259 // Ignore reloc section with bad info. This error will be
4260 // reported in the final link.
4261 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
4262 if (index >= this->shnum())
4263 return false;
4265 // This relocation section is against a section which we
4266 // discarded or if the section is folded into another
4267 // section due to ICF.
4268 if (out_sections[index] == NULL || symtab->is_section_folded(this, index))
4269 return false;
4271 // Ignore reloc section with unexpected symbol table. The
4272 // error will be reported in the final link.
4273 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
4274 return false;
4276 unsigned int reloc_size;
4277 if (sh_type == elfcpp::SHT_REL)
4278 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
4279 else
4280 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
4282 // Ignore reloc section with unexpected entsize or uneven size.
4283 // The error will be reported in the final link.
4284 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
4285 return false;
4287 return true;
4290 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
4291 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
4293 template<bool big_endian>
4294 bool
4295 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
4296 const elfcpp::Shdr<32, big_endian>& shdr,
4297 unsigned int shndx,
4298 Output_section* os,
4299 const Symbol_table* symtab)
4301 // We only scan non-empty code sections.
4302 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) == 0
4303 || shdr.get_sh_size() == 0)
4304 return false;
4306 // Ignore discarded or ICF'ed sections.
4307 if (os == NULL || symtab->is_section_folded(this, shndx))
4308 return false;
4310 // Find output address of section.
4311 Arm_address address = os->output_address(this, shndx, 0);
4313 // If the section does not cross any 4K-boundaries, it does not need to
4314 // be scanned.
4315 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
4316 return false;
4318 return true;
4321 // Scan a section for Cortex-A8 workaround.
4323 template<bool big_endian>
4324 void
4325 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
4326 const elfcpp::Shdr<32, big_endian>& shdr,
4327 unsigned int shndx,
4328 Output_section* os,
4329 Target_arm<big_endian>* arm_target)
4331 Arm_address output_address = os->output_address(this, shndx, 0);
4333 // Get the section contents.
4334 section_size_type input_view_size = 0;
4335 const unsigned char* input_view =
4336 this->section_contents(shndx, &input_view_size, false);
4338 // We need to go through the mapping symbols to determine what to
4339 // scan. There are two reasons. First, we should look at THUMB code and
4340 // THUMB code only. Second, we only want to look at the 4K-page boundary
4341 // to speed up the scanning.
4343 // Look for the first mapping symbol in this section. It should be
4344 // at (shndx, 0).
4345 Mapping_symbol_position section_start(shndx, 0);
4346 typename Mapping_symbols_info::const_iterator p =
4347 this->mapping_symbols_info_.lower_bound(section_start);
4349 if (p == this->mapping_symbols_info_.end()
4350 || p->first != section_start)
4352 gold_warning(_("Cortex-A8 erratum scanning failed because there "
4353 "is no mapping symbols for section %u of %s"),
4354 shndx, this->name().c_str());
4355 return;
4358 while (p != this->mapping_symbols_info_.end()
4359 && p->first.first == shndx)
4361 typename Mapping_symbols_info::const_iterator next =
4362 this->mapping_symbols_info_.upper_bound(p->first);
4364 // Only scan part of a section with THUMB code.
4365 if (p->second == 't')
4367 // Determine the end of this range.
4368 section_size_type span_start =
4369 convert_to_section_size_type(p->first.second);
4370 section_size_type span_end;
4371 if (next != this->mapping_symbols_info_.end()
4372 && next->first.first == shndx)
4373 span_end = convert_to_section_size_type(next->first.second);
4374 else
4375 span_end = convert_to_section_size_type(shdr.get_sh_size());
4377 if (((span_start + output_address) & ~0xfffUL)
4378 != ((span_end + output_address - 1) & ~0xfffUL))
4380 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
4381 span_start, span_end,
4382 input_view,
4383 output_address);
4387 p = next;
4391 // Scan relocations for stub generation.
4393 template<bool big_endian>
4394 void
4395 Arm_relobj<big_endian>::scan_sections_for_stubs(
4396 Target_arm<big_endian>* arm_target,
4397 const Symbol_table* symtab,
4398 const Layout* layout)
4400 unsigned int shnum = this->shnum();
4401 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4403 // Read the section headers.
4404 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
4405 shnum * shdr_size,
4406 true, true);
4408 // To speed up processing, we set up hash tables for fast lookup of
4409 // input offsets to output addresses.
4410 this->initialize_input_to_output_maps();
4412 const Relobj::Output_sections& out_sections(this->output_sections());
4414 Relocate_info<32, big_endian> relinfo;
4415 relinfo.symtab = symtab;
4416 relinfo.layout = layout;
4417 relinfo.object = this;
4419 // Do relocation stubs scanning.
4420 const unsigned char* p = pshdrs + shdr_size;
4421 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
4423 const elfcpp::Shdr<32, big_endian> shdr(p);
4424 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab))
4426 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
4427 Arm_address output_offset = this->get_output_section_offset(index);
4428 Arm_address output_address;
4429 if(output_offset != invalid_address)
4430 output_address = out_sections[index]->address() + output_offset;
4431 else
4433 // Currently this only happens for a relaxed section.
4434 const Output_relaxed_input_section* poris =
4435 out_sections[index]->find_relaxed_input_section(this, index);
4436 gold_assert(poris != NULL);
4437 output_address = poris->address();
4440 // Get the relocations.
4441 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
4442 shdr.get_sh_size(),
4443 true, false);
4445 // Get the section contents. This does work for the case in which
4446 // we modify the contents of an input section. We need to pass the
4447 // output view under such circumstances.
4448 section_size_type input_view_size = 0;
4449 const unsigned char* input_view =
4450 this->section_contents(index, &input_view_size, false);
4452 relinfo.reloc_shndx = i;
4453 relinfo.data_shndx = index;
4454 unsigned int sh_type = shdr.get_sh_type();
4455 unsigned int reloc_size;
4456 if (sh_type == elfcpp::SHT_REL)
4457 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
4458 else
4459 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
4461 Output_section* os = out_sections[index];
4462 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
4463 shdr.get_sh_size() / reloc_size,
4465 output_offset == invalid_address,
4466 input_view, output_address,
4467 input_view_size);
4471 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
4472 // after its relocation section, if there is one, is processed for
4473 // relocation stubs. Merging this loop with the one above would have been
4474 // complicated since we would have had to make sure that relocation stub
4475 // scanning is done first.
4476 if (arm_target->fix_cortex_a8())
4478 const unsigned char* p = pshdrs + shdr_size;
4479 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
4481 const elfcpp::Shdr<32, big_endian> shdr(p);
4482 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
4483 out_sections[i],
4484 symtab))
4485 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
4486 arm_target);
4490 // After we've done the relocations, we release the hash tables,
4491 // since we no longer need them.
4492 this->free_input_to_output_maps();
4495 // Count the local symbols. The ARM backend needs to know if a symbol
4496 // is a THUMB function or not. For global symbols, it is easy because
4497 // the Symbol object keeps the ELF symbol type. For local symbol it is
4498 // harder because we cannot access this information. So we override the
4499 // do_count_local_symbol in parent and scan local symbols to mark
4500 // THUMB functions. This is not the most efficient way but I do not want to
4501 // slow down other ports by calling a per symbol targer hook inside
4502 // Sized_relobj<size, big_endian>::do_count_local_symbols.
4504 template<bool big_endian>
4505 void
4506 Arm_relobj<big_endian>::do_count_local_symbols(
4507 Stringpool_template<char>* pool,
4508 Stringpool_template<char>* dynpool)
4510 // We need to fix-up the values of any local symbols whose type are
4511 // STT_ARM_TFUNC.
4513 // Ask parent to count the local symbols.
4514 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
4515 const unsigned int loccount = this->local_symbol_count();
4516 if (loccount == 0)
4517 return;
4519 // Intialize the thumb function bit-vector.
4520 std::vector<bool> empty_vector(loccount, false);
4521 this->local_symbol_is_thumb_function_.swap(empty_vector);
4523 // Read the symbol table section header.
4524 const unsigned int symtab_shndx = this->symtab_shndx();
4525 elfcpp::Shdr<32, big_endian>
4526 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
4527 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
4529 // Read the local symbols.
4530 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
4531 gold_assert(loccount == symtabshdr.get_sh_info());
4532 off_t locsize = loccount * sym_size;
4533 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
4534 locsize, true, true);
4536 // For mapping symbol processing, we need to read the symbol names.
4537 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
4538 if (strtab_shndx >= this->shnum())
4540 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
4541 return;
4544 elfcpp::Shdr<32, big_endian>
4545 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
4546 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
4548 this->error(_("symbol table name section has wrong type: %u"),
4549 static_cast<unsigned int>(strtabshdr.get_sh_type()));
4550 return;
4552 const char* pnames =
4553 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
4554 strtabshdr.get_sh_size(),
4555 false, false));
4557 // Loop over the local symbols and mark any local symbols pointing
4558 // to THUMB functions.
4560 // Skip the first dummy symbol.
4561 psyms += sym_size;
4562 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
4563 this->local_values();
4564 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
4566 elfcpp::Sym<32, big_endian> sym(psyms);
4567 elfcpp::STT st_type = sym.get_st_type();
4568 Symbol_value<32>& lv((*plocal_values)[i]);
4569 Arm_address input_value = lv.input_value();
4571 // Check to see if this is a mapping symbol.
4572 const char* sym_name = pnames + sym.get_st_name();
4573 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
4575 unsigned int input_shndx = sym.get_st_shndx();
4577 // Strip of LSB in case this is a THUMB symbol.
4578 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
4579 this->mapping_symbols_info_[msp] = sym_name[1];
4582 if (st_type == elfcpp::STT_ARM_TFUNC
4583 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
4585 // This is a THUMB function. Mark this and canonicalize the
4586 // symbol value by setting LSB.
4587 this->local_symbol_is_thumb_function_[i] = true;
4588 if ((input_value & 1) == 0)
4589 lv.set_input_value(input_value | 1);
4594 // Relocate sections.
4595 template<bool big_endian>
4596 void
4597 Arm_relobj<big_endian>::do_relocate_sections(
4598 const Symbol_table* symtab,
4599 const Layout* layout,
4600 const unsigned char* pshdrs,
4601 typename Sized_relobj<32, big_endian>::Views* pviews)
4603 // Call parent to relocate sections.
4604 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
4605 pviews);
4607 // We do not generate stubs if doing a relocatable link.
4608 if (parameters->options().relocatable())
4609 return;
4611 // Relocate stub tables.
4612 unsigned int shnum = this->shnum();
4614 Target_arm<big_endian>* arm_target =
4615 Target_arm<big_endian>::default_target();
4617 Relocate_info<32, big_endian> relinfo;
4618 relinfo.symtab = symtab;
4619 relinfo.layout = layout;
4620 relinfo.object = this;
4622 for (unsigned int i = 1; i < shnum; ++i)
4624 Arm_input_section<big_endian>* arm_input_section =
4625 arm_target->find_arm_input_section(this, i);
4627 if (arm_input_section != NULL
4628 && arm_input_section->is_stub_table_owner()
4629 && !arm_input_section->stub_table()->empty())
4631 // We cannot discard a section if it owns a stub table.
4632 Output_section* os = this->output_section(i);
4633 gold_assert(os != NULL);
4635 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
4636 relinfo.reloc_shdr = NULL;
4637 relinfo.data_shndx = i;
4638 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
4640 gold_assert((*pviews)[i].view != NULL);
4642 // We are passed the output section view. Adjust it to cover the
4643 // stub table only.
4644 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
4645 gold_assert((stub_table->address() >= (*pviews)[i].address)
4646 && ((stub_table->address() + stub_table->data_size())
4647 <= (*pviews)[i].address + (*pviews)[i].view_size));
4649 off_t offset = stub_table->address() - (*pviews)[i].address;
4650 unsigned char* view = (*pviews)[i].view + offset;
4651 Arm_address address = stub_table->address();
4652 section_size_type view_size = stub_table->data_size();
4654 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
4655 view_size);
4658 // Apply Cortex A8 workaround if applicable.
4659 if (this->section_has_cortex_a8_workaround(i))
4661 unsigned char* view = (*pviews)[i].view;
4662 Arm_address view_address = (*pviews)[i].address;
4663 section_size_type view_size = (*pviews)[i].view_size;
4664 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
4666 // Adjust view to cover section.
4667 Output_section* os = this->output_section(i);
4668 gold_assert(os != NULL);
4669 Arm_address section_address = os->output_address(this, i, 0);
4670 uint64_t section_size = this->section_size(i);
4672 gold_assert(section_address >= view_address
4673 && ((section_address + section_size)
4674 <= (view_address + view_size)));
4676 unsigned char* section_view = view + (section_address - view_address);
4678 // Apply the Cortex-A8 workaround to the output address range
4679 // corresponding to this input section.
4680 stub_table->apply_cortex_a8_workaround_to_address_range(
4681 arm_target,
4682 section_view,
4683 section_address,
4684 section_size);
4689 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4690 // ABI information.
4692 template<bool big_endian>
4693 Attributes_section_data*
4694 read_arm_attributes_section(
4695 Object* object,
4696 Read_symbols_data *sd)
4698 // Read the attributes section if there is one.
4699 // We read from the end because gas seems to put it near the end of
4700 // the section headers.
4701 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4702 const unsigned char *ps =
4703 sd->section_headers->data() + shdr_size * (object->shnum() - 1);
4704 for (unsigned int i = object->shnum(); i > 0; --i, ps -= shdr_size)
4706 elfcpp::Shdr<32, big_endian> shdr(ps);
4707 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
4709 section_offset_type section_offset = shdr.get_sh_offset();
4710 section_size_type section_size =
4711 convert_to_section_size_type(shdr.get_sh_size());
4712 File_view* view = object->get_lasting_view(section_offset,
4713 section_size, true, false);
4714 return new Attributes_section_data(view->data(), section_size);
4717 return NULL;
4720 // Read the symbol information.
4722 template<bool big_endian>
4723 void
4724 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4726 // Call parent class to read symbol information.
4727 Sized_relobj<32, big_endian>::do_read_symbols(sd);
4729 // Read processor-specific flags in ELF file header.
4730 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4731 elfcpp::Elf_sizes<32>::ehdr_size,
4732 true, false);
4733 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4734 this->processor_specific_flags_ = ehdr.get_e_flags();
4735 this->attributes_section_data_ =
4736 read_arm_attributes_section<big_endian>(this, sd);
4739 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
4740 // sections for unwinding. These sections are referenced implicitly by
4741 // text sections linked in the section headers. If we ignore these implict
4742 // references, the .ARM.exidx sections and any .ARM.extab sections they use
4743 // will be garbage-collected incorrectly. Hence we override the same function
4744 // in the base class to handle these implicit references.
4746 template<bool big_endian>
4747 void
4748 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
4749 Layout* layout,
4750 Read_relocs_data* rd)
4752 // First, call base class method to process relocations in this object.
4753 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
4755 unsigned int shnum = this->shnum();
4756 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4757 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
4758 shnum * shdr_size,
4759 true, true);
4761 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
4762 // to these from the linked text sections.
4763 const unsigned char* ps = pshdrs + shdr_size;
4764 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
4766 elfcpp::Shdr<32, big_endian> shdr(ps);
4767 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
4769 // Found an .ARM.exidx section, add it to the set of reachable
4770 // sections from its linked text section.
4771 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
4772 symtab->gc()->add_reference(this, text_shndx, this, i);
4777 // Arm_dynobj methods.
4779 // Read the symbol information.
4781 template<bool big_endian>
4782 void
4783 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4785 // Call parent class to read symbol information.
4786 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
4788 // Read processor-specific flags in ELF file header.
4789 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4790 elfcpp::Elf_sizes<32>::ehdr_size,
4791 true, false);
4792 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4793 this->processor_specific_flags_ = ehdr.get_e_flags();
4794 this->attributes_section_data_ =
4795 read_arm_attributes_section<big_endian>(this, sd);
4798 // Stub_addend_reader methods.
4800 // Read the addend of a REL relocation of type R_TYPE at VIEW.
4802 template<bool big_endian>
4803 elfcpp::Elf_types<32>::Elf_Swxword
4804 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
4805 unsigned int r_type,
4806 const unsigned char* view,
4807 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
4809 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
4811 switch (r_type)
4813 case elfcpp::R_ARM_CALL:
4814 case elfcpp::R_ARM_JUMP24:
4815 case elfcpp::R_ARM_PLT32:
4817 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4818 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4819 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4820 return utils::sign_extend<26>(val << 2);
4823 case elfcpp::R_ARM_THM_CALL:
4824 case elfcpp::R_ARM_THM_JUMP24:
4825 case elfcpp::R_ARM_THM_XPC22:
4827 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4828 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4829 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4830 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4831 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
4834 case elfcpp::R_ARM_THM_JUMP19:
4836 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4837 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4838 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4839 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4840 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
4843 default:
4844 gold_unreachable();
4848 // A class to handle the PLT data.
4850 template<bool big_endian>
4851 class Output_data_plt_arm : public Output_section_data
4853 public:
4854 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
4855 Reloc_section;
4857 Output_data_plt_arm(Layout*, Output_data_space*);
4859 // Add an entry to the PLT.
4860 void
4861 add_entry(Symbol* gsym);
4863 // Return the .rel.plt section data.
4864 const Reloc_section*
4865 rel_plt() const
4866 { return this->rel_; }
4868 protected:
4869 void
4870 do_adjust_output_section(Output_section* os);
4872 // Write to a map file.
4873 void
4874 do_print_to_mapfile(Mapfile* mapfile) const
4875 { mapfile->print_output_data(this, _("** PLT")); }
4877 private:
4878 // Template for the first PLT entry.
4879 static const uint32_t first_plt_entry[5];
4881 // Template for subsequent PLT entries.
4882 static const uint32_t plt_entry[3];
4884 // Set the final size.
4885 void
4886 set_final_data_size()
4888 this->set_data_size(sizeof(first_plt_entry)
4889 + this->count_ * sizeof(plt_entry));
4892 // Write out the PLT data.
4893 void
4894 do_write(Output_file*);
4896 // The reloc section.
4897 Reloc_section* rel_;
4898 // The .got.plt section.
4899 Output_data_space* got_plt_;
4900 // The number of PLT entries.
4901 unsigned int count_;
4904 // Create the PLT section. The ordinary .got section is an argument,
4905 // since we need to refer to the start. We also create our own .got
4906 // section just for PLT entries.
4908 template<bool big_endian>
4909 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
4910 Output_data_space* got_plt)
4911 : Output_section_data(4), got_plt_(got_plt), count_(0)
4913 this->rel_ = new Reloc_section(false);
4914 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
4915 elfcpp::SHF_ALLOC, this->rel_, true, false,
4916 false, false);
4919 template<bool big_endian>
4920 void
4921 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
4923 os->set_entsize(0);
4926 // Add an entry to the PLT.
4928 template<bool big_endian>
4929 void
4930 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
4932 gold_assert(!gsym->has_plt_offset());
4934 // Note that when setting the PLT offset we skip the initial
4935 // reserved PLT entry.
4936 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
4937 + sizeof(first_plt_entry));
4939 ++this->count_;
4941 section_offset_type got_offset = this->got_plt_->current_data_size();
4943 // Every PLT entry needs a GOT entry which points back to the PLT
4944 // entry (this will be changed by the dynamic linker, normally
4945 // lazily when the function is called).
4946 this->got_plt_->set_current_data_size(got_offset + 4);
4948 // Every PLT entry needs a reloc.
4949 gsym->set_needs_dynsym_entry();
4950 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
4951 got_offset);
4953 // Note that we don't need to save the symbol. The contents of the
4954 // PLT are independent of which symbols are used. The symbols only
4955 // appear in the relocations.
4958 // ARM PLTs.
4959 // FIXME: This is not very flexible. Right now this has only been tested
4960 // on armv5te. If we are to support additional architecture features like
4961 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4963 // The first entry in the PLT.
4964 template<bool big_endian>
4965 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
4967 0xe52de004, // str lr, [sp, #-4]!
4968 0xe59fe004, // ldr lr, [pc, #4]
4969 0xe08fe00e, // add lr, pc, lr
4970 0xe5bef008, // ldr pc, [lr, #8]!
4971 0x00000000, // &GOT[0] - .
4974 // Subsequent entries in the PLT.
4976 template<bool big_endian>
4977 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
4979 0xe28fc600, // add ip, pc, #0xNN00000
4980 0xe28cca00, // add ip, ip, #0xNN000
4981 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4984 // Write out the PLT. This uses the hand-coded instructions above,
4985 // and adjusts them as needed. This is all specified by the arm ELF
4986 // Processor Supplement.
4988 template<bool big_endian>
4989 void
4990 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
4992 const off_t offset = this->offset();
4993 const section_size_type oview_size =
4994 convert_to_section_size_type(this->data_size());
4995 unsigned char* const oview = of->get_output_view(offset, oview_size);
4997 const off_t got_file_offset = this->got_plt_->offset();
4998 const section_size_type got_size =
4999 convert_to_section_size_type(this->got_plt_->data_size());
5000 unsigned char* const got_view = of->get_output_view(got_file_offset,
5001 got_size);
5002 unsigned char* pov = oview;
5004 Arm_address plt_address = this->address();
5005 Arm_address got_address = this->got_plt_->address();
5007 // Write first PLT entry. All but the last word are constants.
5008 const size_t num_first_plt_words = (sizeof(first_plt_entry)
5009 / sizeof(plt_entry[0]));
5010 for (size_t i = 0; i < num_first_plt_words - 1; i++)
5011 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
5012 // Last word in first PLT entry is &GOT[0] - .
5013 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
5014 got_address - (plt_address + 16));
5015 pov += sizeof(first_plt_entry);
5017 unsigned char* got_pov = got_view;
5019 memset(got_pov, 0, 12);
5020 got_pov += 12;
5022 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
5023 unsigned int plt_offset = sizeof(first_plt_entry);
5024 unsigned int plt_rel_offset = 0;
5025 unsigned int got_offset = 12;
5026 const unsigned int count = this->count_;
5027 for (unsigned int i = 0;
5028 i < count;
5029 ++i,
5030 pov += sizeof(plt_entry),
5031 got_pov += 4,
5032 plt_offset += sizeof(plt_entry),
5033 plt_rel_offset += rel_size,
5034 got_offset += 4)
5036 // Set and adjust the PLT entry itself.
5037 int32_t offset = ((got_address + got_offset)
5038 - (plt_address + plt_offset + 8));
5040 gold_assert(offset >= 0 && offset < 0x0fffffff);
5041 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
5042 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
5043 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
5044 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
5045 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
5046 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
5048 // Set the entry in the GOT.
5049 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
5052 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
5053 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
5055 of->write_output_view(offset, oview_size, oview);
5056 of->write_output_view(got_file_offset, got_size, got_view);
5059 // Create a PLT entry for a global symbol.
5061 template<bool big_endian>
5062 void
5063 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
5064 Symbol* gsym)
5066 if (gsym->has_plt_offset())
5067 return;
5069 if (this->plt_ == NULL)
5071 // Create the GOT sections first.
5072 this->got_section(symtab, layout);
5074 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
5075 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
5076 (elfcpp::SHF_ALLOC
5077 | elfcpp::SHF_EXECINSTR),
5078 this->plt_, false, false, false, false);
5080 this->plt_->add_entry(gsym);
5083 // Report an unsupported relocation against a local symbol.
5085 template<bool big_endian>
5086 void
5087 Target_arm<big_endian>::Scan::unsupported_reloc_local(
5088 Sized_relobj<32, big_endian>* object,
5089 unsigned int r_type)
5091 gold_error(_("%s: unsupported reloc %u against local symbol"),
5092 object->name().c_str(), r_type);
5095 // We are about to emit a dynamic relocation of type R_TYPE. If the
5096 // dynamic linker does not support it, issue an error. The GNU linker
5097 // only issues a non-PIC error for an allocated read-only section.
5098 // Here we know the section is allocated, but we don't know that it is
5099 // read-only. But we check for all the relocation types which the
5100 // glibc dynamic linker supports, so it seems appropriate to issue an
5101 // error even if the section is not read-only.
5103 template<bool big_endian>
5104 void
5105 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
5106 unsigned int r_type)
5108 switch (r_type)
5110 // These are the relocation types supported by glibc for ARM.
5111 case elfcpp::R_ARM_RELATIVE:
5112 case elfcpp::R_ARM_COPY:
5113 case elfcpp::R_ARM_GLOB_DAT:
5114 case elfcpp::R_ARM_JUMP_SLOT:
5115 case elfcpp::R_ARM_ABS32:
5116 case elfcpp::R_ARM_ABS32_NOI:
5117 case elfcpp::R_ARM_PC24:
5118 // FIXME: The following 3 types are not supported by Android's dynamic
5119 // linker.
5120 case elfcpp::R_ARM_TLS_DTPMOD32:
5121 case elfcpp::R_ARM_TLS_DTPOFF32:
5122 case elfcpp::R_ARM_TLS_TPOFF32:
5123 return;
5125 default:
5126 // This prevents us from issuing more than one error per reloc
5127 // section. But we can still wind up issuing more than one
5128 // error per object file.
5129 if (this->issued_non_pic_error_)
5130 return;
5131 object->error(_("requires unsupported dynamic reloc; "
5132 "recompile with -fPIC"));
5133 this->issued_non_pic_error_ = true;
5134 return;
5136 case elfcpp::R_ARM_NONE:
5137 gold_unreachable();
5141 // Scan a relocation for a local symbol.
5142 // FIXME: This only handles a subset of relocation types used by Android
5143 // on ARM v5te devices.
5145 template<bool big_endian>
5146 inline void
5147 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
5148 Layout* layout,
5149 Target_arm* target,
5150 Sized_relobj<32, big_endian>* object,
5151 unsigned int data_shndx,
5152 Output_section* output_section,
5153 const elfcpp::Rel<32, big_endian>& reloc,
5154 unsigned int r_type,
5155 const elfcpp::Sym<32, big_endian>&)
5157 r_type = get_real_reloc_type(r_type);
5158 switch (r_type)
5160 case elfcpp::R_ARM_NONE:
5161 break;
5163 case elfcpp::R_ARM_ABS32:
5164 case elfcpp::R_ARM_ABS32_NOI:
5165 // If building a shared library (or a position-independent
5166 // executable), we need to create a dynamic relocation for
5167 // this location. The relocation applied at link time will
5168 // apply the link-time value, so we flag the location with
5169 // an R_ARM_RELATIVE relocation so the dynamic loader can
5170 // relocate it easily.
5171 if (parameters->options().output_is_position_independent())
5173 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5174 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
5175 // If we are to add more other reloc types than R_ARM_ABS32,
5176 // we need to add check_non_pic(object, r_type) here.
5177 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
5178 output_section, data_shndx,
5179 reloc.get_r_offset());
5181 break;
5183 case elfcpp::R_ARM_REL32:
5184 case elfcpp::R_ARM_THM_CALL:
5185 case elfcpp::R_ARM_CALL:
5186 case elfcpp::R_ARM_PREL31:
5187 case elfcpp::R_ARM_JUMP24:
5188 case elfcpp::R_ARM_THM_JUMP24:
5189 case elfcpp::R_ARM_THM_JUMP19:
5190 case elfcpp::R_ARM_PLT32:
5191 case elfcpp::R_ARM_THM_ABS5:
5192 case elfcpp::R_ARM_ABS8:
5193 case elfcpp::R_ARM_ABS12:
5194 case elfcpp::R_ARM_ABS16:
5195 case elfcpp::R_ARM_BASE_ABS:
5196 case elfcpp::R_ARM_MOVW_ABS_NC:
5197 case elfcpp::R_ARM_MOVT_ABS:
5198 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5199 case elfcpp::R_ARM_THM_MOVT_ABS:
5200 case elfcpp::R_ARM_MOVW_PREL_NC:
5201 case elfcpp::R_ARM_MOVT_PREL:
5202 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5203 case elfcpp::R_ARM_THM_MOVT_PREL:
5204 case elfcpp::R_ARM_THM_JUMP6:
5205 case elfcpp::R_ARM_THM_JUMP8:
5206 case elfcpp::R_ARM_THM_JUMP11:
5207 break;
5209 case elfcpp::R_ARM_GOTOFF32:
5210 // We need a GOT section:
5211 target->got_section(symtab, layout);
5212 break;
5214 case elfcpp::R_ARM_BASE_PREL:
5215 // FIXME: What about this?
5216 break;
5218 case elfcpp::R_ARM_GOT_BREL:
5219 case elfcpp::R_ARM_GOT_PREL:
5221 // The symbol requires a GOT entry.
5222 Output_data_got<32, big_endian>* got =
5223 target->got_section(symtab, layout);
5224 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
5225 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
5227 // If we are generating a shared object, we need to add a
5228 // dynamic RELATIVE relocation for this symbol's GOT entry.
5229 if (parameters->options().output_is_position_independent())
5231 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5232 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
5233 rel_dyn->add_local_relative(
5234 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
5235 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
5239 break;
5241 case elfcpp::R_ARM_TARGET1:
5242 // This should have been mapped to another type already.
5243 // Fall through.
5244 case elfcpp::R_ARM_COPY:
5245 case elfcpp::R_ARM_GLOB_DAT:
5246 case elfcpp::R_ARM_JUMP_SLOT:
5247 case elfcpp::R_ARM_RELATIVE:
5248 // These are relocations which should only be seen by the
5249 // dynamic linker, and should never be seen here.
5250 gold_error(_("%s: unexpected reloc %u in object file"),
5251 object->name().c_str(), r_type);
5252 break;
5254 default:
5255 unsupported_reloc_local(object, r_type);
5256 break;
5260 // Report an unsupported relocation against a global symbol.
5262 template<bool big_endian>
5263 void
5264 Target_arm<big_endian>::Scan::unsupported_reloc_global(
5265 Sized_relobj<32, big_endian>* object,
5266 unsigned int r_type,
5267 Symbol* gsym)
5269 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5270 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5273 // Scan a relocation for a global symbol.
5274 // FIXME: This only handles a subset of relocation types used by Android
5275 // on ARM v5te devices.
5277 template<bool big_endian>
5278 inline void
5279 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
5280 Layout* layout,
5281 Target_arm* target,
5282 Sized_relobj<32, big_endian>* object,
5283 unsigned int data_shndx,
5284 Output_section* output_section,
5285 const elfcpp::Rel<32, big_endian>& reloc,
5286 unsigned int r_type,
5287 Symbol* gsym)
5289 r_type = get_real_reloc_type(r_type);
5290 switch (r_type)
5292 case elfcpp::R_ARM_NONE:
5293 break;
5295 case elfcpp::R_ARM_ABS32:
5296 case elfcpp::R_ARM_ABS32_NOI:
5298 // Make a dynamic relocation if necessary.
5299 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
5301 if (target->may_need_copy_reloc(gsym))
5303 target->copy_reloc(symtab, layout, object,
5304 data_shndx, output_section, gsym, reloc);
5306 else if (gsym->can_use_relative_reloc(false))
5308 // If we are to add more other reloc types than R_ARM_ABS32,
5309 // we need to add check_non_pic(object, r_type) here.
5310 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5311 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
5312 output_section, object,
5313 data_shndx, reloc.get_r_offset());
5315 else
5317 // If we are to add more other reloc types than R_ARM_ABS32,
5318 // we need to add check_non_pic(object, r_type) here.
5319 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5320 rel_dyn->add_global(gsym, r_type, output_section, object,
5321 data_shndx, reloc.get_r_offset());
5325 break;
5327 case elfcpp::R_ARM_MOVW_ABS_NC:
5328 case elfcpp::R_ARM_MOVT_ABS:
5329 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5330 case elfcpp::R_ARM_THM_MOVT_ABS:
5331 case elfcpp::R_ARM_MOVW_PREL_NC:
5332 case elfcpp::R_ARM_MOVT_PREL:
5333 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5334 case elfcpp::R_ARM_THM_MOVT_PREL:
5335 case elfcpp::R_ARM_THM_JUMP6:
5336 case elfcpp::R_ARM_THM_JUMP8:
5337 case elfcpp::R_ARM_THM_JUMP11:
5338 break;
5340 case elfcpp::R_ARM_THM_ABS5:
5341 case elfcpp::R_ARM_ABS8:
5342 case elfcpp::R_ARM_ABS12:
5343 case elfcpp::R_ARM_ABS16:
5344 case elfcpp::R_ARM_BASE_ABS:
5346 // No dynamic relocs of this kinds.
5347 // Report the error in case of PIC.
5348 int flags = Symbol::NON_PIC_REF;
5349 if (gsym->type() == elfcpp::STT_FUNC
5350 || gsym->type() == elfcpp::STT_ARM_TFUNC)
5351 flags |= Symbol::FUNCTION_CALL;
5352 if (gsym->needs_dynamic_reloc(flags))
5353 check_non_pic(object, r_type);
5355 break;
5357 case elfcpp::R_ARM_REL32:
5358 case elfcpp::R_ARM_PREL31:
5360 // Make a dynamic relocation if necessary.
5361 int flags = Symbol::NON_PIC_REF;
5362 if (gsym->needs_dynamic_reloc(flags))
5364 if (target->may_need_copy_reloc(gsym))
5366 target->copy_reloc(symtab, layout, object,
5367 data_shndx, output_section, gsym, reloc);
5369 else
5371 check_non_pic(object, r_type);
5372 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5373 rel_dyn->add_global(gsym, r_type, output_section, object,
5374 data_shndx, reloc.get_r_offset());
5378 break;
5380 case elfcpp::R_ARM_JUMP24:
5381 case elfcpp::R_ARM_THM_JUMP24:
5382 case elfcpp::R_ARM_THM_JUMP19:
5383 case elfcpp::R_ARM_CALL:
5384 case elfcpp::R_ARM_THM_CALL:
5386 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
5387 target->make_plt_entry(symtab, layout, gsym);
5388 else
5390 // Check to see if this is a function that would need a PLT
5391 // but does not get one because the function symbol is untyped.
5392 // This happens in assembly code missing a proper .type directive.
5393 if ((!gsym->is_undefined() || parameters->options().shared())
5394 && !parameters->doing_static_link()
5395 && gsym->type() == elfcpp::STT_NOTYPE
5396 && (gsym->is_from_dynobj()
5397 || gsym->is_undefined()
5398 || gsym->is_preemptible()))
5399 gold_error(_("%s is not a function."),
5400 gsym->demangled_name().c_str());
5402 break;
5404 case elfcpp::R_ARM_PLT32:
5405 // If the symbol is fully resolved, this is just a relative
5406 // local reloc. Otherwise we need a PLT entry.
5407 if (gsym->final_value_is_known())
5408 break;
5409 // If building a shared library, we can also skip the PLT entry
5410 // if the symbol is defined in the output file and is protected
5411 // or hidden.
5412 if (gsym->is_defined()
5413 && !gsym->is_from_dynobj()
5414 && !gsym->is_preemptible())
5415 break;
5416 target->make_plt_entry(symtab, layout, gsym);
5417 break;
5419 case elfcpp::R_ARM_GOTOFF32:
5420 // We need a GOT section.
5421 target->got_section(symtab, layout);
5422 break;
5424 case elfcpp::R_ARM_BASE_PREL:
5425 // FIXME: What about this?
5426 break;
5428 case elfcpp::R_ARM_GOT_BREL:
5429 case elfcpp::R_ARM_GOT_PREL:
5431 // The symbol requires a GOT entry.
5432 Output_data_got<32, big_endian>* got =
5433 target->got_section(symtab, layout);
5434 if (gsym->final_value_is_known())
5435 got->add_global(gsym, GOT_TYPE_STANDARD);
5436 else
5438 // If this symbol is not fully resolved, we need to add a
5439 // GOT entry with a dynamic relocation.
5440 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5441 if (gsym->is_from_dynobj()
5442 || gsym->is_undefined()
5443 || gsym->is_preemptible())
5444 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
5445 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
5446 else
5448 if (got->add_global(gsym, GOT_TYPE_STANDARD))
5449 rel_dyn->add_global_relative(
5450 gsym, elfcpp::R_ARM_RELATIVE, got,
5451 gsym->got_offset(GOT_TYPE_STANDARD));
5455 break;
5457 case elfcpp::R_ARM_TARGET1:
5458 // This should have been mapped to another type already.
5459 // Fall through.
5460 case elfcpp::R_ARM_COPY:
5461 case elfcpp::R_ARM_GLOB_DAT:
5462 case elfcpp::R_ARM_JUMP_SLOT:
5463 case elfcpp::R_ARM_RELATIVE:
5464 // These are relocations which should only be seen by the
5465 // dynamic linker, and should never be seen here.
5466 gold_error(_("%s: unexpected reloc %u in object file"),
5467 object->name().c_str(), r_type);
5468 break;
5470 default:
5471 unsupported_reloc_global(object, r_type, gsym);
5472 break;
5476 // Process relocations for gc.
5478 template<bool big_endian>
5479 void
5480 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
5481 Layout* layout,
5482 Sized_relobj<32, big_endian>* object,
5483 unsigned int data_shndx,
5484 unsigned int,
5485 const unsigned char* prelocs,
5486 size_t reloc_count,
5487 Output_section* output_section,
5488 bool needs_special_offset_handling,
5489 size_t local_symbol_count,
5490 const unsigned char* plocal_symbols)
5492 typedef Target_arm<big_endian> Arm;
5493 typedef typename Target_arm<big_endian>::Scan Scan;
5495 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
5496 symtab,
5497 layout,
5498 this,
5499 object,
5500 data_shndx,
5501 prelocs,
5502 reloc_count,
5503 output_section,
5504 needs_special_offset_handling,
5505 local_symbol_count,
5506 plocal_symbols);
5509 // Scan relocations for a section.
5511 template<bool big_endian>
5512 void
5513 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
5514 Layout* layout,
5515 Sized_relobj<32, big_endian>* object,
5516 unsigned int data_shndx,
5517 unsigned int sh_type,
5518 const unsigned char* prelocs,
5519 size_t reloc_count,
5520 Output_section* output_section,
5521 bool needs_special_offset_handling,
5522 size_t local_symbol_count,
5523 const unsigned char* plocal_symbols)
5525 typedef typename Target_arm<big_endian>::Scan Scan;
5526 if (sh_type == elfcpp::SHT_RELA)
5528 gold_error(_("%s: unsupported RELA reloc section"),
5529 object->name().c_str());
5530 return;
5533 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
5534 symtab,
5535 layout,
5536 this,
5537 object,
5538 data_shndx,
5539 prelocs,
5540 reloc_count,
5541 output_section,
5542 needs_special_offset_handling,
5543 local_symbol_count,
5544 plocal_symbols);
5547 // Finalize the sections.
5549 template<bool big_endian>
5550 void
5551 Target_arm<big_endian>::do_finalize_sections(
5552 Layout* layout,
5553 const Input_objects* input_objects,
5554 Symbol_table* symtab)
5556 // Merge processor-specific flags.
5557 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
5558 p != input_objects->relobj_end();
5559 ++p)
5561 Arm_relobj<big_endian>* arm_relobj =
5562 Arm_relobj<big_endian>::as_arm_relobj(*p);
5563 this->merge_processor_specific_flags(
5564 arm_relobj->name(),
5565 arm_relobj->processor_specific_flags());
5566 this->merge_object_attributes(arm_relobj->name().c_str(),
5567 arm_relobj->attributes_section_data());
5571 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
5572 p != input_objects->dynobj_end();
5573 ++p)
5575 Arm_dynobj<big_endian>* arm_dynobj =
5576 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
5577 this->merge_processor_specific_flags(
5578 arm_dynobj->name(),
5579 arm_dynobj->processor_specific_flags());
5580 this->merge_object_attributes(arm_dynobj->name().c_str(),
5581 arm_dynobj->attributes_section_data());
5584 // Check BLX use.
5585 const Object_attribute* cpu_arch_attr =
5586 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
5587 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
5588 this->set_may_use_blx(true);
5590 // Check if we need to use Cortex-A8 workaround.
5591 if (parameters->options().user_set_fix_cortex_a8())
5592 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
5593 else
5595 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
5596 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
5597 // profile.
5598 const Object_attribute* cpu_arch_profile_attr =
5599 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
5600 this->fix_cortex_a8_ =
5601 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
5602 && (cpu_arch_profile_attr->int_value() == 'A'
5603 || cpu_arch_profile_attr->int_value() == 0));
5606 // Fill in some more dynamic tags.
5607 const Reloc_section* rel_plt = (this->plt_ == NULL
5608 ? NULL
5609 : this->plt_->rel_plt());
5610 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
5611 this->rel_dyn_, true);
5613 // Emit any relocs we saved in an attempt to avoid generating COPY
5614 // relocs.
5615 if (this->copy_relocs_.any_saved_relocs())
5616 this->copy_relocs_.emit(this->rel_dyn_section(layout));
5618 // Handle the .ARM.exidx section.
5619 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
5620 if (exidx_section != NULL
5621 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
5622 && !parameters->options().relocatable())
5624 // Create __exidx_start and __exdix_end symbols.
5625 symtab->define_in_output_data("__exidx_start", NULL,
5626 Symbol_table::PREDEFINED,
5627 exidx_section, 0, 0, elfcpp::STT_OBJECT,
5628 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
5629 false, true);
5630 symtab->define_in_output_data("__exidx_end", NULL,
5631 Symbol_table::PREDEFINED,
5632 exidx_section, 0, 0, elfcpp::STT_OBJECT,
5633 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
5634 true, true);
5636 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
5637 // the .ARM.exidx section.
5638 if (!layout->script_options()->saw_phdrs_clause())
5640 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
5641 == NULL);
5642 Output_segment* exidx_segment =
5643 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
5644 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
5645 false);
5649 // Create an .ARM.attributes section if there is not one already.
5650 Output_attributes_section_data* attributes_section =
5651 new Output_attributes_section_data(*this->attributes_section_data_);
5652 layout->add_output_section_data(".ARM.attributes",
5653 elfcpp::SHT_ARM_ATTRIBUTES, 0,
5654 attributes_section, false, false, false,
5655 false);
5658 // Return whether a direct absolute static relocation needs to be applied.
5659 // In cases where Scan::local() or Scan::global() has created
5660 // a dynamic relocation other than R_ARM_RELATIVE, the addend
5661 // of the relocation is carried in the data, and we must not
5662 // apply the static relocation.
5664 template<bool big_endian>
5665 inline bool
5666 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
5667 const Sized_symbol<32>* gsym,
5668 int ref_flags,
5669 bool is_32bit,
5670 Output_section* output_section)
5672 // If the output section is not allocated, then we didn't call
5673 // scan_relocs, we didn't create a dynamic reloc, and we must apply
5674 // the reloc here.
5675 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
5676 return true;
5678 // For local symbols, we will have created a non-RELATIVE dynamic
5679 // relocation only if (a) the output is position independent,
5680 // (b) the relocation is absolute (not pc- or segment-relative), and
5681 // (c) the relocation is not 32 bits wide.
5682 if (gsym == NULL)
5683 return !(parameters->options().output_is_position_independent()
5684 && (ref_flags & Symbol::ABSOLUTE_REF)
5685 && !is_32bit);
5687 // For global symbols, we use the same helper routines used in the
5688 // scan pass. If we did not create a dynamic relocation, or if we
5689 // created a RELATIVE dynamic relocation, we should apply the static
5690 // relocation.
5691 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
5692 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
5693 && gsym->can_use_relative_reloc(ref_flags
5694 & Symbol::FUNCTION_CALL);
5695 return !has_dyn || is_rel;
5698 // Perform a relocation.
5700 template<bool big_endian>
5701 inline bool
5702 Target_arm<big_endian>::Relocate::relocate(
5703 const Relocate_info<32, big_endian>* relinfo,
5704 Target_arm* target,
5705 Output_section *output_section,
5706 size_t relnum,
5707 const elfcpp::Rel<32, big_endian>& rel,
5708 unsigned int r_type,
5709 const Sized_symbol<32>* gsym,
5710 const Symbol_value<32>* psymval,
5711 unsigned char* view,
5712 Arm_address address,
5713 section_size_type /* view_size */ )
5715 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
5717 r_type = get_real_reloc_type(r_type);
5719 const Arm_relobj<big_endian>* object =
5720 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
5722 // If the final branch target of a relocation is THUMB instruction, this
5723 // is 1. Otherwise it is 0.
5724 Arm_address thumb_bit = 0;
5725 Symbol_value<32> symval;
5726 bool is_weakly_undefined_without_plt = false;
5727 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
5729 if (gsym != NULL)
5731 // This is a global symbol. Determine if we use PLT and if the
5732 // final target is THUMB.
5733 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
5735 // This uses a PLT, change the symbol value.
5736 symval.set_output_value(target->plt_section()->address()
5737 + gsym->plt_offset());
5738 psymval = &symval;
5740 else if (gsym->is_weak_undefined())
5742 // This is a weakly undefined symbol and we do not use PLT
5743 // for this relocation. A branch targeting this symbol will
5744 // be converted into an NOP.
5745 is_weakly_undefined_without_plt = true;
5747 else
5749 // Set thumb bit if symbol:
5750 // -Has type STT_ARM_TFUNC or
5751 // -Has type STT_FUNC, is defined and with LSB in value set.
5752 thumb_bit =
5753 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
5754 || (gsym->type() == elfcpp::STT_FUNC
5755 && !gsym->is_undefined()
5756 && ((psymval->value(object, 0) & 1) != 0)))
5758 : 0);
5761 else
5763 // This is a local symbol. Determine if the final target is THUMB.
5764 // We saved this information when all the local symbols were read.
5765 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
5766 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
5767 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
5770 else
5772 // This is a fake relocation synthesized for a stub. It does not have
5773 // a real symbol. We just look at the LSB of the symbol value to
5774 // determine if the target is THUMB or not.
5775 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
5778 // Strip LSB if this points to a THUMB target.
5779 if (thumb_bit != 0
5780 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
5781 && ((psymval->value(object, 0) & 1) != 0))
5783 Arm_address stripped_value =
5784 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
5785 symval.set_output_value(stripped_value);
5786 psymval = &symval;
5789 // Get the GOT offset if needed.
5790 // The GOT pointer points to the end of the GOT section.
5791 // We need to subtract the size of the GOT section to get
5792 // the actual offset to use in the relocation.
5793 bool have_got_offset = false;
5794 unsigned int got_offset = 0;
5795 switch (r_type)
5797 case elfcpp::R_ARM_GOT_BREL:
5798 case elfcpp::R_ARM_GOT_PREL:
5799 if (gsym != NULL)
5801 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5802 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
5803 - target->got_size());
5805 else
5807 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5808 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5809 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
5810 - target->got_size());
5812 have_got_offset = true;
5813 break;
5815 default:
5816 break;
5819 // To look up relocation stubs, we need to pass the symbol table index of
5820 // a local symbol.
5821 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5823 typename Arm_relocate_functions::Status reloc_status =
5824 Arm_relocate_functions::STATUS_OKAY;
5825 switch (r_type)
5827 case elfcpp::R_ARM_NONE:
5828 break;
5830 case elfcpp::R_ARM_ABS8:
5831 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5832 output_section))
5833 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
5834 break;
5836 case elfcpp::R_ARM_ABS12:
5837 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5838 output_section))
5839 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
5840 break;
5842 case elfcpp::R_ARM_ABS16:
5843 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5844 output_section))
5845 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5846 break;
5848 case elfcpp::R_ARM_ABS32:
5849 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5850 output_section))
5851 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5852 thumb_bit);
5853 break;
5855 case elfcpp::R_ARM_ABS32_NOI:
5856 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5857 output_section))
5858 // No thumb bit for this relocation: (S + A)
5859 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5861 break;
5863 case elfcpp::R_ARM_MOVW_ABS_NC:
5864 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5865 output_section))
5866 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
5867 psymval,
5868 thumb_bit);
5869 else
5870 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5871 "a shared object; recompile with -fPIC"));
5872 break;
5874 case elfcpp::R_ARM_MOVT_ABS:
5875 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5876 output_section))
5877 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
5878 else
5879 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5880 "a shared object; recompile with -fPIC"));
5881 break;
5883 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5884 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5885 output_section))
5886 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
5887 psymval,
5888 thumb_bit);
5889 else
5890 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5891 "making a shared object; recompile with -fPIC"));
5892 break;
5894 case elfcpp::R_ARM_THM_MOVT_ABS:
5895 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5896 output_section))
5897 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
5898 psymval);
5899 else
5900 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5901 "making a shared object; recompile with -fPIC"));
5902 break;
5904 case elfcpp::R_ARM_MOVW_PREL_NC:
5905 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
5906 psymval, address,
5907 thumb_bit);
5908 break;
5910 case elfcpp::R_ARM_MOVT_PREL:
5911 reloc_status = Arm_relocate_functions::movt_prel(view, object,
5912 psymval, address);
5913 break;
5915 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5916 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
5917 psymval, address,
5918 thumb_bit);
5919 break;
5921 case elfcpp::R_ARM_THM_MOVT_PREL:
5922 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
5923 psymval, address);
5924 break;
5926 case elfcpp::R_ARM_REL32:
5927 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5928 address, thumb_bit);
5929 break;
5931 case elfcpp::R_ARM_THM_ABS5:
5932 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5933 output_section))
5934 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
5935 break;
5937 case elfcpp::R_ARM_THM_CALL:
5938 reloc_status =
5939 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
5940 psymval, address, thumb_bit,
5941 is_weakly_undefined_without_plt);
5942 break;
5944 case elfcpp::R_ARM_XPC25:
5945 reloc_status =
5946 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
5947 psymval, address, thumb_bit,
5948 is_weakly_undefined_without_plt);
5949 break;
5951 case elfcpp::R_ARM_THM_XPC22:
5952 reloc_status =
5953 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
5954 psymval, address, thumb_bit,
5955 is_weakly_undefined_without_plt);
5956 break;
5958 case elfcpp::R_ARM_GOTOFF32:
5960 Arm_address got_origin;
5961 got_origin = target->got_plt_section()->address();
5962 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5963 got_origin, thumb_bit);
5965 break;
5967 case elfcpp::R_ARM_BASE_PREL:
5969 uint32_t origin;
5970 // Get the addressing origin of the output segment defining the
5971 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5972 gold_assert(gsym != NULL);
5973 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5974 origin = gsym->output_segment()->vaddr();
5975 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5976 origin = gsym->output_data()->address();
5977 else
5979 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5980 _("cannot find origin of R_ARM_BASE_PREL"));
5981 return true;
5983 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
5985 break;
5987 case elfcpp::R_ARM_BASE_ABS:
5989 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5990 output_section))
5991 break;
5993 uint32_t origin;
5994 // Get the addressing origin of the output segment defining
5995 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5996 if (gsym == NULL)
5997 // R_ARM_BASE_ABS with the NULL symbol will give the
5998 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5999 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
6000 origin = target->got_plt_section()->address();
6001 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
6002 origin = gsym->output_segment()->vaddr();
6003 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
6004 origin = gsym->output_data()->address();
6005 else
6007 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6008 _("cannot find origin of R_ARM_BASE_ABS"));
6009 return true;
6012 reloc_status = Arm_relocate_functions::base_abs(view, origin);
6014 break;
6016 case elfcpp::R_ARM_GOT_BREL:
6017 gold_assert(have_got_offset);
6018 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
6019 break;
6021 case elfcpp::R_ARM_GOT_PREL:
6022 gold_assert(have_got_offset);
6023 // Get the address origin for GOT PLT, which is allocated right
6024 // after the GOT section, to calculate an absolute address of
6025 // the symbol GOT entry (got_origin + got_offset).
6026 Arm_address got_origin;
6027 got_origin = target->got_plt_section()->address();
6028 reloc_status = Arm_relocate_functions::got_prel(view,
6029 got_origin + got_offset,
6030 address);
6031 break;
6033 case elfcpp::R_ARM_PLT32:
6034 gold_assert(gsym == NULL
6035 || gsym->has_plt_offset()
6036 || gsym->final_value_is_known()
6037 || (gsym->is_defined()
6038 && !gsym->is_from_dynobj()
6039 && !gsym->is_preemptible()));
6040 reloc_status =
6041 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
6042 psymval, address, thumb_bit,
6043 is_weakly_undefined_without_plt);
6044 break;
6046 case elfcpp::R_ARM_CALL:
6047 reloc_status =
6048 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
6049 psymval, address, thumb_bit,
6050 is_weakly_undefined_without_plt);
6051 break;
6053 case elfcpp::R_ARM_JUMP24:
6054 reloc_status =
6055 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
6056 psymval, address, thumb_bit,
6057 is_weakly_undefined_without_plt);
6058 break;
6060 case elfcpp::R_ARM_THM_JUMP24:
6061 reloc_status =
6062 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
6063 psymval, address, thumb_bit,
6064 is_weakly_undefined_without_plt);
6065 break;
6067 case elfcpp::R_ARM_THM_JUMP19:
6068 reloc_status =
6069 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
6070 thumb_bit);
6071 break;
6073 case elfcpp::R_ARM_THM_JUMP6:
6074 reloc_status =
6075 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
6076 break;
6078 case elfcpp::R_ARM_THM_JUMP8:
6079 reloc_status =
6080 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
6081 break;
6083 case elfcpp::R_ARM_THM_JUMP11:
6084 reloc_status =
6085 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
6086 break;
6088 case elfcpp::R_ARM_PREL31:
6089 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
6090 address, thumb_bit);
6091 break;
6093 case elfcpp::R_ARM_TARGET1:
6094 // This should have been mapped to another type already.
6095 // Fall through.
6096 case elfcpp::R_ARM_COPY:
6097 case elfcpp::R_ARM_GLOB_DAT:
6098 case elfcpp::R_ARM_JUMP_SLOT:
6099 case elfcpp::R_ARM_RELATIVE:
6100 // These are relocations which should only be seen by the
6101 // dynamic linker, and should never be seen here.
6102 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6103 _("unexpected reloc %u in object file"),
6104 r_type);
6105 break;
6107 default:
6108 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6109 _("unsupported reloc %u"),
6110 r_type);
6111 break;
6114 // Report any errors.
6115 switch (reloc_status)
6117 case Arm_relocate_functions::STATUS_OKAY:
6118 break;
6119 case Arm_relocate_functions::STATUS_OVERFLOW:
6120 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6121 _("relocation overflow in relocation %u"),
6122 r_type);
6123 break;
6124 case Arm_relocate_functions::STATUS_BAD_RELOC:
6125 gold_error_at_location(
6126 relinfo,
6127 relnum,
6128 rel.get_r_offset(),
6129 _("unexpected opcode while processing relocation %u"),
6130 r_type);
6131 break;
6132 default:
6133 gold_unreachable();
6136 return true;
6139 // Relocate section data.
6141 template<bool big_endian>
6142 void
6143 Target_arm<big_endian>::relocate_section(
6144 const Relocate_info<32, big_endian>* relinfo,
6145 unsigned int sh_type,
6146 const unsigned char* prelocs,
6147 size_t reloc_count,
6148 Output_section* output_section,
6149 bool needs_special_offset_handling,
6150 unsigned char* view,
6151 Arm_address address,
6152 section_size_type view_size,
6153 const Reloc_symbol_changes* reloc_symbol_changes)
6155 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
6156 gold_assert(sh_type == elfcpp::SHT_REL);
6158 Arm_input_section<big_endian>* arm_input_section =
6159 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
6161 // This is an ARM input section and the view covers the whole output
6162 // section.
6163 if (arm_input_section != NULL)
6165 gold_assert(needs_special_offset_handling);
6166 Arm_address section_address = arm_input_section->address();
6167 section_size_type section_size = arm_input_section->data_size();
6169 gold_assert((arm_input_section->address() >= address)
6170 && ((arm_input_section->address()
6171 + arm_input_section->data_size())
6172 <= (address + view_size)));
6174 off_t offset = section_address - address;
6175 view += offset;
6176 address += offset;
6177 view_size = section_size;
6180 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
6181 Arm_relocate>(
6182 relinfo,
6183 this,
6184 prelocs,
6185 reloc_count,
6186 output_section,
6187 needs_special_offset_handling,
6188 view,
6189 address,
6190 view_size,
6191 reloc_symbol_changes);
6194 // Return the size of a relocation while scanning during a relocatable
6195 // link.
6197 template<bool big_endian>
6198 unsigned int
6199 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
6200 unsigned int r_type,
6201 Relobj* object)
6203 r_type = get_real_reloc_type(r_type);
6204 switch (r_type)
6206 case elfcpp::R_ARM_NONE:
6207 return 0;
6209 case elfcpp::R_ARM_ABS8:
6210 return 1;
6212 case elfcpp::R_ARM_ABS16:
6213 case elfcpp::R_ARM_THM_ABS5:
6214 case elfcpp::R_ARM_THM_JUMP6:
6215 case elfcpp::R_ARM_THM_JUMP8:
6216 case elfcpp::R_ARM_THM_JUMP11:
6217 return 2;
6219 case elfcpp::R_ARM_ABS32:
6220 case elfcpp::R_ARM_ABS32_NOI:
6221 case elfcpp::R_ARM_ABS12:
6222 case elfcpp::R_ARM_BASE_ABS:
6223 case elfcpp::R_ARM_REL32:
6224 case elfcpp::R_ARM_THM_CALL:
6225 case elfcpp::R_ARM_GOTOFF32:
6226 case elfcpp::R_ARM_BASE_PREL:
6227 case elfcpp::R_ARM_GOT_BREL:
6228 case elfcpp::R_ARM_GOT_PREL:
6229 case elfcpp::R_ARM_PLT32:
6230 case elfcpp::R_ARM_CALL:
6231 case elfcpp::R_ARM_JUMP24:
6232 case elfcpp::R_ARM_PREL31:
6233 case elfcpp::R_ARM_MOVW_ABS_NC:
6234 case elfcpp::R_ARM_MOVT_ABS:
6235 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6236 case elfcpp::R_ARM_THM_MOVT_ABS:
6237 case elfcpp::R_ARM_MOVW_PREL_NC:
6238 case elfcpp::R_ARM_MOVT_PREL:
6239 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6240 case elfcpp::R_ARM_THM_MOVT_PREL:
6241 return 4;
6243 case elfcpp::R_ARM_TARGET1:
6244 // This should have been mapped to another type already.
6245 // Fall through.
6246 case elfcpp::R_ARM_COPY:
6247 case elfcpp::R_ARM_GLOB_DAT:
6248 case elfcpp::R_ARM_JUMP_SLOT:
6249 case elfcpp::R_ARM_RELATIVE:
6250 // These are relocations which should only be seen by the
6251 // dynamic linker, and should never be seen here.
6252 gold_error(_("%s: unexpected reloc %u in object file"),
6253 object->name().c_str(), r_type);
6254 return 0;
6256 default:
6257 object->error(_("unsupported reloc %u in object file"), r_type);
6258 return 0;
6262 // Scan the relocs during a relocatable link.
6264 template<bool big_endian>
6265 void
6266 Target_arm<big_endian>::scan_relocatable_relocs(
6267 Symbol_table* symtab,
6268 Layout* layout,
6269 Sized_relobj<32, big_endian>* object,
6270 unsigned int data_shndx,
6271 unsigned int sh_type,
6272 const unsigned char* prelocs,
6273 size_t reloc_count,
6274 Output_section* output_section,
6275 bool needs_special_offset_handling,
6276 size_t local_symbol_count,
6277 const unsigned char* plocal_symbols,
6278 Relocatable_relocs* rr)
6280 gold_assert(sh_type == elfcpp::SHT_REL);
6282 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
6283 Relocatable_size_for_reloc> Scan_relocatable_relocs;
6285 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
6286 Scan_relocatable_relocs>(
6287 symtab,
6288 layout,
6289 object,
6290 data_shndx,
6291 prelocs,
6292 reloc_count,
6293 output_section,
6294 needs_special_offset_handling,
6295 local_symbol_count,
6296 plocal_symbols,
6297 rr);
6300 // Relocate a section during a relocatable link.
6302 template<bool big_endian>
6303 void
6304 Target_arm<big_endian>::relocate_for_relocatable(
6305 const Relocate_info<32, big_endian>* relinfo,
6306 unsigned int sh_type,
6307 const unsigned char* prelocs,
6308 size_t reloc_count,
6309 Output_section* output_section,
6310 off_t offset_in_output_section,
6311 const Relocatable_relocs* rr,
6312 unsigned char* view,
6313 Arm_address view_address,
6314 section_size_type view_size,
6315 unsigned char* reloc_view,
6316 section_size_type reloc_view_size)
6318 gold_assert(sh_type == elfcpp::SHT_REL);
6320 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
6321 relinfo,
6322 prelocs,
6323 reloc_count,
6324 output_section,
6325 offset_in_output_section,
6327 view,
6328 view_address,
6329 view_size,
6330 reloc_view,
6331 reloc_view_size);
6334 // Return the value to use for a dynamic symbol which requires special
6335 // treatment. This is how we support equality comparisons of function
6336 // pointers across shared library boundaries, as described in the
6337 // processor specific ABI supplement.
6339 template<bool big_endian>
6340 uint64_t
6341 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
6343 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6344 return this->plt_section()->address() + gsym->plt_offset();
6347 // Map platform-specific relocs to real relocs
6349 template<bool big_endian>
6350 unsigned int
6351 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
6353 switch (r_type)
6355 case elfcpp::R_ARM_TARGET1:
6356 // This is either R_ARM_ABS32 or R_ARM_REL32;
6357 return elfcpp::R_ARM_ABS32;
6359 case elfcpp::R_ARM_TARGET2:
6360 // This can be any reloc type but ususally is R_ARM_GOT_PREL
6361 return elfcpp::R_ARM_GOT_PREL;
6363 default:
6364 return r_type;
6368 // Whether if two EABI versions V1 and V2 are compatible.
6370 template<bool big_endian>
6371 bool
6372 Target_arm<big_endian>::are_eabi_versions_compatible(
6373 elfcpp::Elf_Word v1,
6374 elfcpp::Elf_Word v2)
6376 // v4 and v5 are the same spec before and after it was released,
6377 // so allow mixing them.
6378 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
6379 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
6380 return true;
6382 return v1 == v2;
6385 // Combine FLAGS from an input object called NAME and the processor-specific
6386 // flags in the ELF header of the output. Much of this is adapted from the
6387 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
6388 // in bfd/elf32-arm.c.
6390 template<bool big_endian>
6391 void
6392 Target_arm<big_endian>::merge_processor_specific_flags(
6393 const std::string& name,
6394 elfcpp::Elf_Word flags)
6396 if (this->are_processor_specific_flags_set())
6398 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
6400 // Nothing to merge if flags equal to those in output.
6401 if (flags == out_flags)
6402 return;
6404 // Complain about various flag mismatches.
6405 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
6406 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
6407 if (!this->are_eabi_versions_compatible(version1, version2))
6408 gold_error(_("Source object %s has EABI version %d but output has "
6409 "EABI version %d."),
6410 name.c_str(),
6411 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
6412 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
6414 else
6416 // If the input is the default architecture and had the default
6417 // flags then do not bother setting the flags for the output
6418 // architecture, instead allow future merges to do this. If no
6419 // future merges ever set these flags then they will retain their
6420 // uninitialised values, which surprise surprise, correspond
6421 // to the default values.
6422 if (flags == 0)
6423 return;
6425 // This is the first time, just copy the flags.
6426 // We only copy the EABI version for now.
6427 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
6431 // Adjust ELF file header.
6432 template<bool big_endian>
6433 void
6434 Target_arm<big_endian>::do_adjust_elf_header(
6435 unsigned char* view,
6436 int len) const
6438 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
6440 elfcpp::Ehdr<32, big_endian> ehdr(view);
6441 unsigned char e_ident[elfcpp::EI_NIDENT];
6442 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
6444 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
6445 == elfcpp::EF_ARM_EABI_UNKNOWN)
6446 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
6447 else
6448 e_ident[elfcpp::EI_OSABI] = 0;
6449 e_ident[elfcpp::EI_ABIVERSION] = 0;
6451 // FIXME: Do EF_ARM_BE8 adjustment.
6453 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
6454 oehdr.put_e_ident(e_ident);
6457 // do_make_elf_object to override the same function in the base class.
6458 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
6459 // to store ARM specific information. Hence we need to have our own
6460 // ELF object creation.
6462 template<bool big_endian>
6463 Object*
6464 Target_arm<big_endian>::do_make_elf_object(
6465 const std::string& name,
6466 Input_file* input_file,
6467 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
6469 int et = ehdr.get_e_type();
6470 if (et == elfcpp::ET_REL)
6472 Arm_relobj<big_endian>* obj =
6473 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
6474 obj->setup();
6475 return obj;
6477 else if (et == elfcpp::ET_DYN)
6479 Sized_dynobj<32, big_endian>* obj =
6480 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
6481 obj->setup();
6482 return obj;
6484 else
6486 gold_error(_("%s: unsupported ELF file type %d"),
6487 name.c_str(), et);
6488 return NULL;
6492 // Read the architecture from the Tag_also_compatible_with attribute, if any.
6493 // Returns -1 if no architecture could be read.
6494 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
6496 template<bool big_endian>
6498 Target_arm<big_endian>::get_secondary_compatible_arch(
6499 const Attributes_section_data* pasd)
6501 const Object_attribute *known_attributes =
6502 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
6504 // Note: the tag and its argument below are uleb128 values, though
6505 // currently-defined values fit in one byte for each.
6506 const std::string& sv =
6507 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
6508 if (sv.size() == 2
6509 && sv.data()[0] == elfcpp::Tag_CPU_arch
6510 && (sv.data()[1] & 128) != 128)
6511 return sv.data()[1];
6513 // This tag is "safely ignorable", so don't complain if it looks funny.
6514 return -1;
6517 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
6518 // The tag is removed if ARCH is -1.
6519 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
6521 template<bool big_endian>
6522 void
6523 Target_arm<big_endian>::set_secondary_compatible_arch(
6524 Attributes_section_data* pasd,
6525 int arch)
6527 Object_attribute *known_attributes =
6528 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
6530 if (arch == -1)
6532 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
6533 return;
6536 // Note: the tag and its argument below are uleb128 values, though
6537 // currently-defined values fit in one byte for each.
6538 char sv[3];
6539 sv[0] = elfcpp::Tag_CPU_arch;
6540 gold_assert(arch != 0);
6541 sv[1] = arch;
6542 sv[2] = '\0';
6544 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
6547 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
6548 // into account.
6549 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
6551 template<bool big_endian>
6553 Target_arm<big_endian>::tag_cpu_arch_combine(
6554 const char* name,
6555 int oldtag,
6556 int* secondary_compat_out,
6557 int newtag,
6558 int secondary_compat)
6560 #define T(X) elfcpp::TAG_CPU_ARCH_##X
6561 static const int v6t2[] =
6563 T(V6T2), // PRE_V4.
6564 T(V6T2), // V4.
6565 T(V6T2), // V4T.
6566 T(V6T2), // V5T.
6567 T(V6T2), // V5TE.
6568 T(V6T2), // V5TEJ.
6569 T(V6T2), // V6.
6570 T(V7), // V6KZ.
6571 T(V6T2) // V6T2.
6573 static const int v6k[] =
6575 T(V6K), // PRE_V4.
6576 T(V6K), // V4.
6577 T(V6K), // V4T.
6578 T(V6K), // V5T.
6579 T(V6K), // V5TE.
6580 T(V6K), // V5TEJ.
6581 T(V6K), // V6.
6582 T(V6KZ), // V6KZ.
6583 T(V7), // V6T2.
6584 T(V6K) // V6K.
6586 static const int v7[] =
6588 T(V7), // PRE_V4.
6589 T(V7), // V4.
6590 T(V7), // V4T.
6591 T(V7), // V5T.
6592 T(V7), // V5TE.
6593 T(V7), // V5TEJ.
6594 T(V7), // V6.
6595 T(V7), // V6KZ.
6596 T(V7), // V6T2.
6597 T(V7), // V6K.
6598 T(V7) // V7.
6600 static const int v6_m[] =
6602 -1, // PRE_V4.
6603 -1, // V4.
6604 T(V6K), // V4T.
6605 T(V6K), // V5T.
6606 T(V6K), // V5TE.
6607 T(V6K), // V5TEJ.
6608 T(V6K), // V6.
6609 T(V6KZ), // V6KZ.
6610 T(V7), // V6T2.
6611 T(V6K), // V6K.
6612 T(V7), // V7.
6613 T(V6_M) // V6_M.
6615 static const int v6s_m[] =
6617 -1, // PRE_V4.
6618 -1, // V4.
6619 T(V6K), // V4T.
6620 T(V6K), // V5T.
6621 T(V6K), // V5TE.
6622 T(V6K), // V5TEJ.
6623 T(V6K), // V6.
6624 T(V6KZ), // V6KZ.
6625 T(V7), // V6T2.
6626 T(V6K), // V6K.
6627 T(V7), // V7.
6628 T(V6S_M), // V6_M.
6629 T(V6S_M) // V6S_M.
6631 static const int v7e_m[] =
6633 -1, // PRE_V4.
6634 -1, // V4.
6635 T(V7E_M), // V4T.
6636 T(V7E_M), // V5T.
6637 T(V7E_M), // V5TE.
6638 T(V7E_M), // V5TEJ.
6639 T(V7E_M), // V6.
6640 T(V7E_M), // V6KZ.
6641 T(V7E_M), // V6T2.
6642 T(V7E_M), // V6K.
6643 T(V7E_M), // V7.
6644 T(V7E_M), // V6_M.
6645 T(V7E_M), // V6S_M.
6646 T(V7E_M) // V7E_M.
6648 static const int v4t_plus_v6_m[] =
6650 -1, // PRE_V4.
6651 -1, // V4.
6652 T(V4T), // V4T.
6653 T(V5T), // V5T.
6654 T(V5TE), // V5TE.
6655 T(V5TEJ), // V5TEJ.
6656 T(V6), // V6.
6657 T(V6KZ), // V6KZ.
6658 T(V6T2), // V6T2.
6659 T(V6K), // V6K.
6660 T(V7), // V7.
6661 T(V6_M), // V6_M.
6662 T(V6S_M), // V6S_M.
6663 T(V7E_M), // V7E_M.
6664 T(V4T_PLUS_V6_M) // V4T plus V6_M.
6666 static const int *comb[] =
6668 v6t2,
6669 v6k,
6671 v6_m,
6672 v6s_m,
6673 v7e_m,
6674 // Pseudo-architecture.
6675 v4t_plus_v6_m
6678 // Check we've not got a higher architecture than we know about.
6680 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
6682 gold_error(_("%s: unknown CPU architecture"), name);
6683 return -1;
6686 // Override old tag if we have a Tag_also_compatible_with on the output.
6688 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
6689 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
6690 oldtag = T(V4T_PLUS_V6_M);
6692 // And override the new tag if we have a Tag_also_compatible_with on the
6693 // input.
6695 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
6696 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
6697 newtag = T(V4T_PLUS_V6_M);
6699 // Architectures before V6KZ add features monotonically.
6700 int tagh = std::max(oldtag, newtag);
6701 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
6702 return tagh;
6704 int tagl = std::min(oldtag, newtag);
6705 int result = comb[tagh - T(V6T2)][tagl];
6707 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
6708 // as the canonical version.
6709 if (result == T(V4T_PLUS_V6_M))
6711 result = T(V4T);
6712 *secondary_compat_out = T(V6_M);
6714 else
6715 *secondary_compat_out = -1;
6717 if (result == -1)
6719 gold_error(_("%s: conflicting CPU architectures %d/%d"),
6720 name, oldtag, newtag);
6721 return -1;
6724 return result;
6725 #undef T
6728 // Helper to print AEABI enum tag value.
6730 template<bool big_endian>
6731 std::string
6732 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
6734 static const char *aeabi_enum_names[] =
6735 { "", "variable-size", "32-bit", "" };
6736 const size_t aeabi_enum_names_size =
6737 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
6739 if (value < aeabi_enum_names_size)
6740 return std::string(aeabi_enum_names[value]);
6741 else
6743 char buffer[100];
6744 sprintf(buffer, "<unknown value %u>", value);
6745 return std::string(buffer);
6749 // Return the string value to store in TAG_CPU_name.
6751 template<bool big_endian>
6752 std::string
6753 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
6755 static const char *name_table[] = {
6756 // These aren't real CPU names, but we can't guess
6757 // that from the architecture version alone.
6758 "Pre v4",
6759 "ARM v4",
6760 "ARM v4T",
6761 "ARM v5T",
6762 "ARM v5TE",
6763 "ARM v5TEJ",
6764 "ARM v6",
6765 "ARM v6KZ",
6766 "ARM v6T2",
6767 "ARM v6K",
6768 "ARM v7",
6769 "ARM v6-M",
6770 "ARM v6S-M",
6771 "ARM v7E-M"
6773 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
6775 if (value < name_table_size)
6776 return std::string(name_table[value]);
6777 else
6779 char buffer[100];
6780 sprintf(buffer, "<unknown CPU value %u>", value);
6781 return std::string(buffer);
6785 // Merge object attributes from input file called NAME with those of the
6786 // output. The input object attributes are in the object pointed by PASD.
6788 template<bool big_endian>
6789 void
6790 Target_arm<big_endian>::merge_object_attributes(
6791 const char* name,
6792 const Attributes_section_data* pasd)
6794 // Return if there is no attributes section data.
6795 if (pasd == NULL)
6796 return;
6798 // If output has no object attributes, just copy.
6799 if (this->attributes_section_data_ == NULL)
6801 this->attributes_section_data_ = new Attributes_section_data(*pasd);
6802 return;
6805 const int vendor = Object_attribute::OBJ_ATTR_PROC;
6806 const Object_attribute* in_attr = pasd->known_attributes(vendor);
6807 Object_attribute* out_attr =
6808 this->attributes_section_data_->known_attributes(vendor);
6810 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6811 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
6812 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
6814 // Ignore mismatches if the object doesn't use floating point. */
6815 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
6816 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
6817 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
6818 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
6819 gold_error(_("%s uses VFP register arguments, output does not"),
6820 name);
6823 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
6825 // Merge this attribute with existing attributes.
6826 switch (i)
6828 case elfcpp::Tag_CPU_raw_name:
6829 case elfcpp::Tag_CPU_name:
6830 // These are merged after Tag_CPU_arch.
6831 break;
6833 case elfcpp::Tag_ABI_optimization_goals:
6834 case elfcpp::Tag_ABI_FP_optimization_goals:
6835 // Use the first value seen.
6836 break;
6838 case elfcpp::Tag_CPU_arch:
6840 unsigned int saved_out_attr = out_attr->int_value();
6841 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6842 int secondary_compat =
6843 this->get_secondary_compatible_arch(pasd);
6844 int secondary_compat_out =
6845 this->get_secondary_compatible_arch(
6846 this->attributes_section_data_);
6847 out_attr[i].set_int_value(
6848 tag_cpu_arch_combine(name, out_attr[i].int_value(),
6849 &secondary_compat_out,
6850 in_attr[i].int_value(),
6851 secondary_compat));
6852 this->set_secondary_compatible_arch(this->attributes_section_data_,
6853 secondary_compat_out);
6855 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6856 if (out_attr[i].int_value() == saved_out_attr)
6857 ; // Leave the names alone.
6858 else if (out_attr[i].int_value() == in_attr[i].int_value())
6860 // The output architecture has been changed to match the
6861 // input architecture. Use the input names.
6862 out_attr[elfcpp::Tag_CPU_name].set_string_value(
6863 in_attr[elfcpp::Tag_CPU_name].string_value());
6864 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
6865 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
6867 else
6869 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
6870 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
6873 // If we still don't have a value for Tag_CPU_name,
6874 // make one up now. Tag_CPU_raw_name remains blank.
6875 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
6877 const std::string cpu_name =
6878 this->tag_cpu_name_value(out_attr[i].int_value());
6879 // FIXME: If we see an unknown CPU, this will be set
6880 // to "<unknown CPU n>", where n is the attribute value.
6881 // This is different from BFD, which leaves the name alone.
6882 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
6885 break;
6887 case elfcpp::Tag_ARM_ISA_use:
6888 case elfcpp::Tag_THUMB_ISA_use:
6889 case elfcpp::Tag_WMMX_arch:
6890 case elfcpp::Tag_Advanced_SIMD_arch:
6891 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6892 case elfcpp::Tag_ABI_FP_rounding:
6893 case elfcpp::Tag_ABI_FP_exceptions:
6894 case elfcpp::Tag_ABI_FP_user_exceptions:
6895 case elfcpp::Tag_ABI_FP_number_model:
6896 case elfcpp::Tag_VFP_HP_extension:
6897 case elfcpp::Tag_CPU_unaligned_access:
6898 case elfcpp::Tag_T2EE_use:
6899 case elfcpp::Tag_Virtualization_use:
6900 case elfcpp::Tag_MPextension_use:
6901 // Use the largest value specified.
6902 if (in_attr[i].int_value() > out_attr[i].int_value())
6903 out_attr[i].set_int_value(in_attr[i].int_value());
6904 break;
6906 case elfcpp::Tag_ABI_align8_preserved:
6907 case elfcpp::Tag_ABI_PCS_RO_data:
6908 // Use the smallest value specified.
6909 if (in_attr[i].int_value() < out_attr[i].int_value())
6910 out_attr[i].set_int_value(in_attr[i].int_value());
6911 break;
6913 case elfcpp::Tag_ABI_align8_needed:
6914 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
6915 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
6916 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
6917 == 0)))
6919 // This error message should be enabled once all non-conformant
6920 // binaries in the toolchain have had the attributes set
6921 // properly.
6922 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6923 // name);
6925 // Fall through.
6926 case elfcpp::Tag_ABI_FP_denormal:
6927 case elfcpp::Tag_ABI_PCS_GOT_use:
6929 // These tags have 0 = don't care, 1 = strong requirement,
6930 // 2 = weak requirement.
6931 static const int order_021[3] = {0, 2, 1};
6933 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6934 // value if greater than 2 (for future-proofing).
6935 if ((in_attr[i].int_value() > 2
6936 && in_attr[i].int_value() > out_attr[i].int_value())
6937 || (in_attr[i].int_value() <= 2
6938 && out_attr[i].int_value() <= 2
6939 && (order_021[in_attr[i].int_value()]
6940 > order_021[out_attr[i].int_value()])))
6941 out_attr[i].set_int_value(in_attr[i].int_value());
6943 break;
6945 case elfcpp::Tag_CPU_arch_profile:
6946 if (out_attr[i].int_value() != in_attr[i].int_value())
6948 // 0 will merge with anything.
6949 // 'A' and 'S' merge to 'A'.
6950 // 'R' and 'S' merge to 'R'.
6951 // 'M' and 'A|R|S' is an error.
6952 if (out_attr[i].int_value() == 0
6953 || (out_attr[i].int_value() == 'S'
6954 && (in_attr[i].int_value() == 'A'
6955 || in_attr[i].int_value() == 'R')))
6956 out_attr[i].set_int_value(in_attr[i].int_value());
6957 else if (in_attr[i].int_value() == 0
6958 || (in_attr[i].int_value() == 'S'
6959 && (out_attr[i].int_value() == 'A'
6960 || out_attr[i].int_value() == 'R')))
6961 ; // Do nothing.
6962 else
6964 gold_error
6965 (_("conflicting architecture profiles %c/%c"),
6966 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
6967 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
6970 break;
6971 case elfcpp::Tag_VFP_arch:
6973 static const struct
6975 int ver;
6976 int regs;
6977 } vfp_versions[7] =
6979 {0, 0},
6980 {1, 16},
6981 {2, 16},
6982 {3, 32},
6983 {3, 16},
6984 {4, 32},
6985 {4, 16}
6988 // Values greater than 6 aren't defined, so just pick the
6989 // biggest.
6990 if (in_attr[i].int_value() > 6
6991 && in_attr[i].int_value() > out_attr[i].int_value())
6993 *out_attr = *in_attr;
6994 break;
6996 // The output uses the superset of input features
6997 // (ISA version) and registers.
6998 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
6999 vfp_versions[out_attr[i].int_value()].ver);
7000 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
7001 vfp_versions[out_attr[i].int_value()].regs);
7002 // This assumes all possible supersets are also a valid
7003 // options.
7004 int newval;
7005 for (newval = 6; newval > 0; newval--)
7007 if (regs == vfp_versions[newval].regs
7008 && ver == vfp_versions[newval].ver)
7009 break;
7011 out_attr[i].set_int_value(newval);
7013 break;
7014 case elfcpp::Tag_PCS_config:
7015 if (out_attr[i].int_value() == 0)
7016 out_attr[i].set_int_value(in_attr[i].int_value());
7017 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
7019 // It's sometimes ok to mix different configs, so this is only
7020 // a warning.
7021 gold_warning(_("%s: conflicting platform configuration"), name);
7023 break;
7024 case elfcpp::Tag_ABI_PCS_R9_use:
7025 if (in_attr[i].int_value() != out_attr[i].int_value()
7026 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
7027 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
7029 gold_error(_("%s: conflicting use of R9"), name);
7031 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
7032 out_attr[i].set_int_value(in_attr[i].int_value());
7033 break;
7034 case elfcpp::Tag_ABI_PCS_RW_data:
7035 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
7036 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
7037 != elfcpp::AEABI_R9_SB)
7038 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
7039 != elfcpp::AEABI_R9_unused))
7041 gold_error(_("%s: SB relative addressing conflicts with use "
7042 "of R9"),
7043 name);
7045 // Use the smallest value specified.
7046 if (in_attr[i].int_value() < out_attr[i].int_value())
7047 out_attr[i].set_int_value(in_attr[i].int_value());
7048 break;
7049 case elfcpp::Tag_ABI_PCS_wchar_t:
7050 // FIXME: Make it possible to turn off this warning.
7051 if (out_attr[i].int_value()
7052 && in_attr[i].int_value()
7053 && out_attr[i].int_value() != in_attr[i].int_value())
7055 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
7056 "use %u-byte wchar_t; use of wchar_t values "
7057 "across objects may fail"),
7058 name, in_attr[i].int_value(),
7059 out_attr[i].int_value());
7061 else if (in_attr[i].int_value() && !out_attr[i].int_value())
7062 out_attr[i].set_int_value(in_attr[i].int_value());
7063 break;
7064 case elfcpp::Tag_ABI_enum_size:
7065 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
7067 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
7068 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
7070 // The existing object is compatible with anything.
7071 // Use whatever requirements the new object has.
7072 out_attr[i].set_int_value(in_attr[i].int_value());
7074 // FIXME: Make it possible to turn off this warning.
7075 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
7076 && out_attr[i].int_value() != in_attr[i].int_value())
7078 unsigned int in_value = in_attr[i].int_value();
7079 unsigned int out_value = out_attr[i].int_value();
7080 gold_warning(_("%s uses %s enums yet the output is to use "
7081 "%s enums; use of enum values across objects "
7082 "may fail"),
7083 name,
7084 this->aeabi_enum_name(in_value).c_str(),
7085 this->aeabi_enum_name(out_value).c_str());
7088 break;
7089 case elfcpp::Tag_ABI_VFP_args:
7090 // Aready done.
7091 break;
7092 case elfcpp::Tag_ABI_WMMX_args:
7093 if (in_attr[i].int_value() != out_attr[i].int_value())
7095 gold_error(_("%s uses iWMMXt register arguments, output does "
7096 "not"),
7097 name);
7099 break;
7100 case Object_attribute::Tag_compatibility:
7101 // Merged in target-independent code.
7102 break;
7103 case elfcpp::Tag_ABI_HardFP_use:
7104 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
7105 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
7106 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
7107 out_attr[i].set_int_value(3);
7108 else if (in_attr[i].int_value() > out_attr[i].int_value())
7109 out_attr[i].set_int_value(in_attr[i].int_value());
7110 break;
7111 case elfcpp::Tag_ABI_FP_16bit_format:
7112 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
7114 if (in_attr[i].int_value() != out_attr[i].int_value())
7115 gold_error(_("fp16 format mismatch between %s and output"),
7116 name);
7118 if (in_attr[i].int_value() != 0)
7119 out_attr[i].set_int_value(in_attr[i].int_value());
7120 break;
7122 case elfcpp::Tag_nodefaults:
7123 // This tag is set if it exists, but the value is unused (and is
7124 // typically zero). We don't actually need to do anything here -
7125 // the merge happens automatically when the type flags are merged
7126 // below.
7127 break;
7128 case elfcpp::Tag_also_compatible_with:
7129 // Already done in Tag_CPU_arch.
7130 break;
7131 case elfcpp::Tag_conformance:
7132 // Keep the attribute if it matches. Throw it away otherwise.
7133 // No attribute means no claim to conform.
7134 if (in_attr[i].string_value() != out_attr[i].string_value())
7135 out_attr[i].set_string_value("");
7136 break;
7138 default:
7140 const char* err_object = NULL;
7142 // The "known_obj_attributes" table does contain some undefined
7143 // attributes. Ensure that there are unused.
7144 if (out_attr[i].int_value() != 0
7145 || out_attr[i].string_value() != "")
7146 err_object = "output";
7147 else if (in_attr[i].int_value() != 0
7148 || in_attr[i].string_value() != "")
7149 err_object = name;
7151 if (err_object != NULL)
7153 // Attribute numbers >=64 (mod 128) can be safely ignored.
7154 if ((i & 127) < 64)
7155 gold_error(_("%s: unknown mandatory EABI object attribute "
7156 "%d"),
7157 err_object, i);
7158 else
7159 gold_warning(_("%s: unknown EABI object attribute %d"),
7160 err_object, i);
7163 // Only pass on attributes that match in both inputs.
7164 if (!in_attr[i].matches(out_attr[i]))
7166 out_attr[i].set_int_value(0);
7167 out_attr[i].set_string_value("");
7172 // If out_attr was copied from in_attr then it won't have a type yet.
7173 if (in_attr[i].type() && !out_attr[i].type())
7174 out_attr[i].set_type(in_attr[i].type());
7177 // Merge Tag_compatibility attributes and any common GNU ones.
7178 this->attributes_section_data_->merge(name, pasd);
7180 // Check for any attributes not known on ARM.
7181 typedef Vendor_object_attributes::Other_attributes Other_attributes;
7182 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
7183 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
7184 Other_attributes* out_other_attributes =
7185 this->attributes_section_data_->other_attributes(vendor);
7186 Other_attributes::iterator out_iter = out_other_attributes->begin();
7188 while (in_iter != in_other_attributes->end()
7189 || out_iter != out_other_attributes->end())
7191 const char* err_object = NULL;
7192 int err_tag = 0;
7194 // The tags for each list are in numerical order.
7195 // If the tags are equal, then merge.
7196 if (out_iter != out_other_attributes->end()
7197 && (in_iter == in_other_attributes->end()
7198 || in_iter->first > out_iter->first))
7200 // This attribute only exists in output. We can't merge, and we
7201 // don't know what the tag means, so delete it.
7202 err_object = "output";
7203 err_tag = out_iter->first;
7204 int saved_tag = out_iter->first;
7205 delete out_iter->second;
7206 out_other_attributes->erase(out_iter);
7207 out_iter = out_other_attributes->upper_bound(saved_tag);
7209 else if (in_iter != in_other_attributes->end()
7210 && (out_iter != out_other_attributes->end()
7211 || in_iter->first < out_iter->first))
7213 // This attribute only exists in input. We can't merge, and we
7214 // don't know what the tag means, so ignore it.
7215 err_object = name;
7216 err_tag = in_iter->first;
7217 ++in_iter;
7219 else // The tags are equal.
7221 // As present, all attributes in the list are unknown, and
7222 // therefore can't be merged meaningfully.
7223 err_object = "output";
7224 err_tag = out_iter->first;
7226 // Only pass on attributes that match in both inputs.
7227 if (!in_iter->second->matches(*(out_iter->second)))
7229 // No match. Delete the attribute.
7230 int saved_tag = out_iter->first;
7231 delete out_iter->second;
7232 out_other_attributes->erase(out_iter);
7233 out_iter = out_other_attributes->upper_bound(saved_tag);
7235 else
7237 // Matched. Keep the attribute and move to the next.
7238 ++out_iter;
7239 ++in_iter;
7243 if (err_object)
7245 // Attribute numbers >=64 (mod 128) can be safely ignored. */
7246 if ((err_tag & 127) < 64)
7248 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
7249 err_object, err_tag);
7251 else
7253 gold_warning(_("%s: unknown EABI object attribute %d"),
7254 err_object, err_tag);
7260 // Return whether a relocation type used the LSB to distinguish THUMB
7261 // addresses.
7262 template<bool big_endian>
7263 bool
7264 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
7266 switch (r_type)
7268 case elfcpp::R_ARM_PC24:
7269 case elfcpp::R_ARM_ABS32:
7270 case elfcpp::R_ARM_REL32:
7271 case elfcpp::R_ARM_SBREL32:
7272 case elfcpp::R_ARM_THM_CALL:
7273 case elfcpp::R_ARM_GLOB_DAT:
7274 case elfcpp::R_ARM_JUMP_SLOT:
7275 case elfcpp::R_ARM_GOTOFF32:
7276 case elfcpp::R_ARM_PLT32:
7277 case elfcpp::R_ARM_CALL:
7278 case elfcpp::R_ARM_JUMP24:
7279 case elfcpp::R_ARM_THM_JUMP24:
7280 case elfcpp::R_ARM_SBREL31:
7281 case elfcpp::R_ARM_PREL31:
7282 case elfcpp::R_ARM_MOVW_ABS_NC:
7283 case elfcpp::R_ARM_MOVW_PREL_NC:
7284 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7285 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7286 case elfcpp::R_ARM_THM_JUMP19:
7287 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7288 case elfcpp::R_ARM_ALU_PC_G0_NC:
7289 case elfcpp::R_ARM_ALU_PC_G0:
7290 case elfcpp::R_ARM_ALU_PC_G1_NC:
7291 case elfcpp::R_ARM_ALU_PC_G1:
7292 case elfcpp::R_ARM_ALU_PC_G2:
7293 case elfcpp::R_ARM_ALU_SB_G0_NC:
7294 case elfcpp::R_ARM_ALU_SB_G0:
7295 case elfcpp::R_ARM_ALU_SB_G1_NC:
7296 case elfcpp::R_ARM_ALU_SB_G1:
7297 case elfcpp::R_ARM_ALU_SB_G2:
7298 case elfcpp::R_ARM_MOVW_BREL_NC:
7299 case elfcpp::R_ARM_MOVW_BREL:
7300 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7301 case elfcpp::R_ARM_THM_MOVW_BREL:
7302 return true;
7303 default:
7304 return false;
7308 // Stub-generation methods for Target_arm.
7310 // Make a new Arm_input_section object.
7312 template<bool big_endian>
7313 Arm_input_section<big_endian>*
7314 Target_arm<big_endian>::new_arm_input_section(
7315 Relobj* relobj,
7316 unsigned int shndx)
7318 Input_section_specifier iss(relobj, shndx);
7320 Arm_input_section<big_endian>* arm_input_section =
7321 new Arm_input_section<big_endian>(relobj, shndx);
7322 arm_input_section->init();
7324 // Register new Arm_input_section in map for look-up.
7325 std::pair<typename Arm_input_section_map::iterator, bool> ins =
7326 this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
7328 // Make sure that it we have not created another Arm_input_section
7329 // for this input section already.
7330 gold_assert(ins.second);
7332 return arm_input_section;
7335 // Find the Arm_input_section object corresponding to the SHNDX-th input
7336 // section of RELOBJ.
7338 template<bool big_endian>
7339 Arm_input_section<big_endian>*
7340 Target_arm<big_endian>::find_arm_input_section(
7341 Relobj* relobj,
7342 unsigned int shndx) const
7344 Input_section_specifier iss(relobj, shndx);
7345 typename Arm_input_section_map::const_iterator p =
7346 this->arm_input_section_map_.find(iss);
7347 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
7350 // Make a new stub table.
7352 template<bool big_endian>
7353 Stub_table<big_endian>*
7354 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
7356 Stub_table<big_endian>* stub_table =
7357 new Stub_table<big_endian>(owner);
7358 this->stub_tables_.push_back(stub_table);
7360 stub_table->set_address(owner->address() + owner->data_size());
7361 stub_table->set_file_offset(owner->offset() + owner->data_size());
7362 stub_table->finalize_data_size();
7364 return stub_table;
7367 // Scan a relocation for stub generation.
7369 template<bool big_endian>
7370 void
7371 Target_arm<big_endian>::scan_reloc_for_stub(
7372 const Relocate_info<32, big_endian>* relinfo,
7373 unsigned int r_type,
7374 const Sized_symbol<32>* gsym,
7375 unsigned int r_sym,
7376 const Symbol_value<32>* psymval,
7377 elfcpp::Elf_types<32>::Elf_Swxword addend,
7378 Arm_address address)
7380 typedef typename Target_arm<big_endian>::Relocate Relocate;
7382 const Arm_relobj<big_endian>* arm_relobj =
7383 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
7385 bool target_is_thumb;
7386 Symbol_value<32> symval;
7387 if (gsym != NULL)
7389 // This is a global symbol. Determine if we use PLT and if the
7390 // final target is THUMB.
7391 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
7393 // This uses a PLT, change the symbol value.
7394 symval.set_output_value(this->plt_section()->address()
7395 + gsym->plt_offset());
7396 psymval = &symval;
7397 target_is_thumb = false;
7399 else if (gsym->is_undefined())
7400 // There is no need to generate a stub symbol is undefined.
7401 return;
7402 else
7404 target_is_thumb =
7405 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
7406 || (gsym->type() == elfcpp::STT_FUNC
7407 && !gsym->is_undefined()
7408 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
7411 else
7413 // This is a local symbol. Determine if the final target is THUMB.
7414 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
7417 // Strip LSB if this points to a THUMB target.
7418 if (target_is_thumb
7419 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
7420 && ((psymval->value(arm_relobj, 0) & 1) != 0))
7422 Arm_address stripped_value =
7423 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
7424 symval.set_output_value(stripped_value);
7425 psymval = &symval;
7428 // Get the symbol value.
7429 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
7431 // Owing to pipelining, the PC relative branches below actually skip
7432 // two instructions when the branch offset is 0.
7433 Arm_address destination;
7434 switch (r_type)
7436 case elfcpp::R_ARM_CALL:
7437 case elfcpp::R_ARM_JUMP24:
7438 case elfcpp::R_ARM_PLT32:
7439 // ARM branches.
7440 destination = value + addend + 8;
7441 break;
7442 case elfcpp::R_ARM_THM_CALL:
7443 case elfcpp::R_ARM_THM_XPC22:
7444 case elfcpp::R_ARM_THM_JUMP24:
7445 case elfcpp::R_ARM_THM_JUMP19:
7446 // THUMB branches.
7447 destination = value + addend + 4;
7448 break;
7449 default:
7450 gold_unreachable();
7453 Reloc_stub* stub = NULL;
7454 Stub_type stub_type =
7455 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
7456 target_is_thumb);
7457 if (stub_type != arm_stub_none)
7459 // Try looking up an existing stub from a stub table.
7460 Stub_table<big_endian>* stub_table =
7461 arm_relobj->stub_table(relinfo->data_shndx);
7462 gold_assert(stub_table != NULL);
7464 // Locate stub by destination.
7465 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
7467 // Create a stub if there is not one already
7468 stub = stub_table->find_reloc_stub(stub_key);
7469 if (stub == NULL)
7471 // create a new stub and add it to stub table.
7472 stub = this->stub_factory().make_reloc_stub(stub_type);
7473 stub_table->add_reloc_stub(stub, stub_key);
7476 // Record the destination address.
7477 stub->set_destination_address(destination
7478 | (target_is_thumb ? 1 : 0));
7481 // For Cortex-A8, we need to record a relocation at 4K page boundary.
7482 if (this->fix_cortex_a8_
7483 && (r_type == elfcpp::R_ARM_THM_JUMP24
7484 || r_type == elfcpp::R_ARM_THM_JUMP19
7485 || r_type == elfcpp::R_ARM_THM_CALL
7486 || r_type == elfcpp::R_ARM_THM_XPC22)
7487 && (address & 0xfffU) == 0xffeU)
7489 // Found a candidate. Note we haven't checked the destination is
7490 // within 4K here: if we do so (and don't create a record) we can't
7491 // tell that a branch should have been relocated when scanning later.
7492 this->cortex_a8_relocs_info_[address] =
7493 new Cortex_a8_reloc(stub, r_type,
7494 destination | (target_is_thumb ? 1 : 0));
7498 // This function scans a relocation sections for stub generation.
7499 // The template parameter Relocate must be a class type which provides
7500 // a single function, relocate(), which implements the machine
7501 // specific part of a relocation.
7503 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
7504 // SHT_REL or SHT_RELA.
7506 // PRELOCS points to the relocation data. RELOC_COUNT is the number
7507 // of relocs. OUTPUT_SECTION is the output section.
7508 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
7509 // mapped to output offsets.
7511 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
7512 // VIEW_SIZE is the size. These refer to the input section, unless
7513 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
7514 // the output section.
7516 template<bool big_endian>
7517 template<int sh_type>
7518 void inline
7519 Target_arm<big_endian>::scan_reloc_section_for_stubs(
7520 const Relocate_info<32, big_endian>* relinfo,
7521 const unsigned char* prelocs,
7522 size_t reloc_count,
7523 Output_section* output_section,
7524 bool needs_special_offset_handling,
7525 const unsigned char* view,
7526 elfcpp::Elf_types<32>::Elf_Addr view_address,
7527 section_size_type)
7529 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
7530 const int reloc_size =
7531 Reloc_types<sh_type, 32, big_endian>::reloc_size;
7533 Arm_relobj<big_endian>* arm_object =
7534 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
7535 unsigned int local_count = arm_object->local_symbol_count();
7537 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
7539 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7541 Reltype reloc(prelocs);
7543 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
7544 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
7545 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
7547 r_type = this->get_real_reloc_type(r_type);
7549 // Only a few relocation types need stubs.
7550 if ((r_type != elfcpp::R_ARM_CALL)
7551 && (r_type != elfcpp::R_ARM_JUMP24)
7552 && (r_type != elfcpp::R_ARM_PLT32)
7553 && (r_type != elfcpp::R_ARM_THM_CALL)
7554 && (r_type != elfcpp::R_ARM_THM_XPC22)
7555 && (r_type != elfcpp::R_ARM_THM_JUMP24)
7556 && (r_type != elfcpp::R_ARM_THM_JUMP19))
7557 continue;
7559 section_offset_type offset =
7560 convert_to_section_size_type(reloc.get_r_offset());
7562 if (needs_special_offset_handling)
7564 offset = output_section->output_offset(relinfo->object,
7565 relinfo->data_shndx,
7566 offset);
7567 if (offset == -1)
7568 continue;
7571 // Get the addend.
7572 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
7573 elfcpp::Elf_types<32>::Elf_Swxword addend =
7574 stub_addend_reader(r_type, view + offset, reloc);
7576 const Sized_symbol<32>* sym;
7578 Symbol_value<32> symval;
7579 const Symbol_value<32> *psymval;
7580 if (r_sym < local_count)
7582 sym = NULL;
7583 psymval = arm_object->local_symbol(r_sym);
7585 // If the local symbol belongs to a section we are discarding,
7586 // and that section is a debug section, try to find the
7587 // corresponding kept section and map this symbol to its
7588 // counterpart in the kept section. The symbol must not
7589 // correspond to a section we are folding.
7590 bool is_ordinary;
7591 unsigned int shndx = psymval->input_shndx(&is_ordinary);
7592 if (is_ordinary
7593 && shndx != elfcpp::SHN_UNDEF
7594 && !arm_object->is_section_included(shndx)
7595 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
7597 if (comdat_behavior == CB_UNDETERMINED)
7599 std::string name =
7600 arm_object->section_name(relinfo->data_shndx);
7601 comdat_behavior = get_comdat_behavior(name.c_str());
7603 if (comdat_behavior == CB_PRETEND)
7605 bool found;
7606 typename elfcpp::Elf_types<32>::Elf_Addr value =
7607 arm_object->map_to_kept_section(shndx, &found);
7608 if (found)
7609 symval.set_output_value(value + psymval->input_value());
7610 else
7611 symval.set_output_value(0);
7613 else
7615 symval.set_output_value(0);
7617 symval.set_no_output_symtab_entry();
7618 psymval = &symval;
7621 else
7623 const Symbol* gsym = arm_object->global_symbol(r_sym);
7624 gold_assert(gsym != NULL);
7625 if (gsym->is_forwarder())
7626 gsym = relinfo->symtab->resolve_forwards(gsym);
7628 sym = static_cast<const Sized_symbol<32>*>(gsym);
7629 if (sym->has_symtab_index())
7630 symval.set_output_symtab_index(sym->symtab_index());
7631 else
7632 symval.set_no_output_symtab_entry();
7634 // We need to compute the would-be final value of this global
7635 // symbol.
7636 const Symbol_table* symtab = relinfo->symtab;
7637 const Sized_symbol<32>* sized_symbol =
7638 symtab->get_sized_symbol<32>(gsym);
7639 Symbol_table::Compute_final_value_status status;
7640 Arm_address value =
7641 symtab->compute_final_value<32>(sized_symbol, &status);
7643 // Skip this if the symbol has not output section.
7644 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
7645 continue;
7647 symval.set_output_value(value);
7648 psymval = &symval;
7651 // If symbol is a section symbol, we don't know the actual type of
7652 // destination. Give up.
7653 if (psymval->is_section_symbol())
7654 continue;
7656 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
7657 addend, view_address + offset);
7661 // Scan an input section for stub generation.
7663 template<bool big_endian>
7664 void
7665 Target_arm<big_endian>::scan_section_for_stubs(
7666 const Relocate_info<32, big_endian>* relinfo,
7667 unsigned int sh_type,
7668 const unsigned char* prelocs,
7669 size_t reloc_count,
7670 Output_section* output_section,
7671 bool needs_special_offset_handling,
7672 const unsigned char* view,
7673 Arm_address view_address,
7674 section_size_type view_size)
7676 if (sh_type == elfcpp::SHT_REL)
7677 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
7678 relinfo,
7679 prelocs,
7680 reloc_count,
7681 output_section,
7682 needs_special_offset_handling,
7683 view,
7684 view_address,
7685 view_size);
7686 else if (sh_type == elfcpp::SHT_RELA)
7687 // We do not support RELA type relocations yet. This is provided for
7688 // completeness.
7689 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
7690 relinfo,
7691 prelocs,
7692 reloc_count,
7693 output_section,
7694 needs_special_offset_handling,
7695 view,
7696 view_address,
7697 view_size);
7698 else
7699 gold_unreachable();
7702 // Group input sections for stub generation.
7704 // We goup input sections in an output sections so that the total size,
7705 // including any padding space due to alignment is smaller than GROUP_SIZE
7706 // unless the only input section in group is bigger than GROUP_SIZE already.
7707 // Then an ARM stub table is created to follow the last input section
7708 // in group. For each group an ARM stub table is created an is placed
7709 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
7710 // extend the group after the stub table.
7712 template<bool big_endian>
7713 void
7714 Target_arm<big_endian>::group_sections(
7715 Layout* layout,
7716 section_size_type group_size,
7717 bool stubs_always_after_branch)
7719 // Group input sections and insert stub table
7720 Layout::Section_list section_list;
7721 layout->get_allocated_sections(&section_list);
7722 for (Layout::Section_list::const_iterator p = section_list.begin();
7723 p != section_list.end();
7724 ++p)
7726 Arm_output_section<big_endian>* output_section =
7727 Arm_output_section<big_endian>::as_arm_output_section(*p);
7728 output_section->group_sections(group_size, stubs_always_after_branch,
7729 this);
7733 // Relaxation hook. This is where we do stub generation.
7735 template<bool big_endian>
7736 bool
7737 Target_arm<big_endian>::do_relax(
7738 int pass,
7739 const Input_objects* input_objects,
7740 Symbol_table* symtab,
7741 Layout* layout)
7743 // No need to generate stubs if this is a relocatable link.
7744 gold_assert(!parameters->options().relocatable());
7746 // If this is the first pass, we need to group input sections into
7747 // stub groups.
7748 if (pass == 1)
7750 // Determine the stub group size. The group size is the absolute
7751 // value of the parameter --stub-group-size. If --stub-group-size
7752 // is passed a negative value, we restict stubs to be always after
7753 // the stubbed branches.
7754 int32_t stub_group_size_param =
7755 parameters->options().stub_group_size();
7756 bool stubs_always_after_branch = stub_group_size_param < 0;
7757 section_size_type stub_group_size = abs(stub_group_size_param);
7759 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
7760 // page as the first half of a 32-bit branch straddling two 4K pages.
7761 // This is a crude way of enforcing that.
7762 if (this->fix_cortex_a8_)
7763 stubs_always_after_branch = true;
7765 if (stub_group_size == 1)
7767 // Default value.
7768 // Thumb branch range is +-4MB has to be used as the default
7769 // maximum size (a given section can contain both ARM and Thumb
7770 // code, so the worst case has to be taken into account).
7772 // This value is 24K less than that, which allows for 2025
7773 // 12-byte stubs. If we exceed that, then we will fail to link.
7774 // The user will have to relink with an explicit group size
7775 // option.
7776 stub_group_size = 4170000;
7779 group_sections(layout, stub_group_size, stubs_always_after_branch);
7782 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
7783 // beginning of each relaxation pass, just blow away all the stubs.
7784 // Alternatively, we could selectively remove only the stubs and reloc
7785 // information for code sections that have moved since the last pass.
7786 // That would require more book-keeping.
7787 typedef typename Stub_table_list::iterator Stub_table_iterator;
7788 if (this->fix_cortex_a8_)
7790 // Clear all Cortex-A8 reloc information.
7791 for (typename Cortex_a8_relocs_info::const_iterator p =
7792 this->cortex_a8_relocs_info_.begin();
7793 p != this->cortex_a8_relocs_info_.end();
7794 ++p)
7795 delete p->second;
7796 this->cortex_a8_relocs_info_.clear();
7798 // Remove all Cortex-A8 stubs.
7799 for (Stub_table_iterator sp = this->stub_tables_.begin();
7800 sp != this->stub_tables_.end();
7801 ++sp)
7802 (*sp)->remove_all_cortex_a8_stubs();
7805 // Scan relocs for relocation stubs
7806 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
7807 op != input_objects->relobj_end();
7808 ++op)
7810 Arm_relobj<big_endian>* arm_relobj =
7811 Arm_relobj<big_endian>::as_arm_relobj(*op);
7812 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
7815 // Check all stub tables to see if any of them have their data sizes
7816 // or addresses alignments changed. These are the only things that
7817 // matter.
7818 bool any_stub_table_changed = false;
7819 for (Stub_table_iterator sp = this->stub_tables_.begin();
7820 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7821 ++sp)
7823 if ((*sp)->update_data_size_and_addralign())
7824 any_stub_table_changed = true;
7827 // Finalize the stubs in the last relaxation pass.
7828 if (!any_stub_table_changed)
7829 for (Stub_table_iterator sp = this->stub_tables_.begin();
7830 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7831 ++sp)
7832 (*sp)->finalize_stubs();
7834 return any_stub_table_changed;
7837 // Relocate a stub.
7839 template<bool big_endian>
7840 void
7841 Target_arm<big_endian>::relocate_stub(
7842 Stub* stub,
7843 const Relocate_info<32, big_endian>* relinfo,
7844 Output_section* output_section,
7845 unsigned char* view,
7846 Arm_address address,
7847 section_size_type view_size)
7849 Relocate relocate;
7850 const Stub_template* stub_template = stub->stub_template();
7851 for (size_t i = 0; i < stub_template->reloc_count(); i++)
7853 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
7854 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
7856 unsigned int r_type = insn->r_type();
7857 section_size_type reloc_offset = stub_template->reloc_offset(i);
7858 section_size_type reloc_size = insn->size();
7859 gold_assert(reloc_offset + reloc_size <= view_size);
7861 // This is the address of the stub destination.
7862 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
7863 Symbol_value<32> symval;
7864 symval.set_output_value(target);
7866 // Synthesize a fake reloc just in case. We don't have a symbol so
7867 // we use 0.
7868 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
7869 memset(reloc_buffer, 0, sizeof(reloc_buffer));
7870 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
7871 reloc_write.put_r_offset(reloc_offset);
7872 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
7873 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
7875 relocate.relocate(relinfo, this, output_section,
7876 this->fake_relnum_for_stubs, rel, r_type,
7877 NULL, &symval, view + reloc_offset,
7878 address + reloc_offset, reloc_size);
7882 // Determine whether an object attribute tag takes an integer, a
7883 // string or both.
7885 template<bool big_endian>
7887 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
7889 if (tag == Object_attribute::Tag_compatibility)
7890 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7891 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
7892 else if (tag == elfcpp::Tag_nodefaults)
7893 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7894 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
7895 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
7896 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
7897 else if (tag < 32)
7898 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
7899 else
7900 return ((tag & 1) != 0
7901 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7902 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
7905 // Reorder attributes.
7907 // The ABI defines that Tag_conformance should be emitted first, and that
7908 // Tag_nodefaults should be second (if either is defined). This sets those
7909 // two positions, and bumps up the position of all the remaining tags to
7910 // compensate.
7912 template<bool big_endian>
7914 Target_arm<big_endian>::do_attributes_order(int num) const
7916 // Reorder the known object attributes in output. We want to move
7917 // Tag_conformance to position 4 and Tag_conformance to position 5
7918 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7919 if (num == 4)
7920 return elfcpp::Tag_conformance;
7921 if (num == 5)
7922 return elfcpp::Tag_nodefaults;
7923 if ((num - 2) < elfcpp::Tag_nodefaults)
7924 return num - 2;
7925 if ((num - 1) < elfcpp::Tag_conformance)
7926 return num - 1;
7927 return num;
7930 // Scan a span of THUMB code for Cortex-A8 erratum.
7932 template<bool big_endian>
7933 void
7934 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
7935 Arm_relobj<big_endian>* arm_relobj,
7936 unsigned int shndx,
7937 section_size_type span_start,
7938 section_size_type span_end,
7939 const unsigned char* view,
7940 Arm_address address)
7942 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
7944 // The opcode is BLX.W, BL.W, B.W, Bcc.W
7945 // The branch target is in the same 4KB region as the
7946 // first half of the branch.
7947 // The instruction before the branch is a 32-bit
7948 // length non-branch instruction.
7949 section_size_type i = span_start;
7950 bool last_was_32bit = false;
7951 bool last_was_branch = false;
7952 while (i < span_end)
7954 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
7955 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
7956 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
7957 bool is_blx = false, is_b = false;
7958 bool is_bl = false, is_bcc = false;
7960 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
7961 if (insn_32bit)
7963 // Load the rest of the insn (in manual-friendly order).
7964 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
7966 // Encoding T4: B<c>.W.
7967 is_b = (insn & 0xf800d000U) == 0xf0009000U;
7968 // Encoding T1: BL<c>.W.
7969 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
7970 // Encoding T2: BLX<c>.W.
7971 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
7972 // Encoding T3: B<c>.W (not permitted in IT block).
7973 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
7974 && (insn & 0x07f00000U) != 0x03800000U);
7977 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
7979 // If this instruction is a 32-bit THUMB branch that crosses a 4K
7980 // page boundary and it follows 32-bit non-branch instruction,
7981 // we need to work around.
7982 if (is_32bit_branch
7983 && ((address + i) & 0xfffU) == 0xffeU
7984 && last_was_32bit
7985 && !last_was_branch)
7987 // Check to see if there is a relocation stub for this branch.
7988 bool force_target_arm = false;
7989 bool force_target_thumb = false;
7990 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
7991 Cortex_a8_relocs_info::const_iterator p =
7992 this->cortex_a8_relocs_info_.find(address + i);
7994 if (p != this->cortex_a8_relocs_info_.end())
7996 cortex_a8_reloc = p->second;
7997 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
7999 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
8000 && !target_is_thumb)
8001 force_target_arm = true;
8002 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
8003 && target_is_thumb)
8004 force_target_thumb = true;
8007 off_t offset;
8008 Stub_type stub_type = arm_stub_none;
8010 // Check if we have an offending branch instruction.
8011 uint16_t upper_insn = (insn >> 16) & 0xffffU;
8012 uint16_t lower_insn = insn & 0xffffU;
8013 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
8015 if (cortex_a8_reloc != NULL
8016 && cortex_a8_reloc->reloc_stub() != NULL)
8017 // We've already made a stub for this instruction, e.g.
8018 // it's a long branch or a Thumb->ARM stub. Assume that
8019 // stub will suffice to work around the A8 erratum (see
8020 // setting of always_after_branch above).
8022 else if (is_bcc)
8024 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
8025 lower_insn);
8026 stub_type = arm_stub_a8_veneer_b_cond;
8028 else if (is_b || is_bl || is_blx)
8030 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
8031 lower_insn);
8032 if (is_blx)
8033 offset &= ~3;
8035 stub_type = (is_blx
8036 ? arm_stub_a8_veneer_blx
8037 : (is_bl
8038 ? arm_stub_a8_veneer_bl
8039 : arm_stub_a8_veneer_b));
8042 if (stub_type != arm_stub_none)
8044 Arm_address pc_for_insn = address + i + 4;
8046 // The original instruction is a BL, but the target is
8047 // an ARM instruction. If we were not making a stub,
8048 // the BL would have been converted to a BLX. Use the
8049 // BLX stub instead in that case.
8050 if (this->may_use_blx() && force_target_arm
8051 && stub_type == arm_stub_a8_veneer_bl)
8053 stub_type = arm_stub_a8_veneer_blx;
8054 is_blx = true;
8055 is_bl = false;
8057 // Conversely, if the original instruction was
8058 // BLX but the target is Thumb mode, use the BL stub.
8059 else if (force_target_thumb
8060 && stub_type == arm_stub_a8_veneer_blx)
8062 stub_type = arm_stub_a8_veneer_bl;
8063 is_blx = false;
8064 is_bl = true;
8067 if (is_blx)
8068 pc_for_insn &= ~3;
8070 // If we found a relocation, use the proper destination,
8071 // not the offset in the (unrelocated) instruction.
8072 // Note this is always done if we switched the stub type above.
8073 if (cortex_a8_reloc != NULL)
8074 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
8076 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
8078 // Add a new stub if destination address in in the same page.
8079 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
8081 Cortex_a8_stub* stub =
8082 this->stub_factory_.make_cortex_a8_stub(stub_type,
8083 arm_relobj, shndx,
8084 address + i,
8085 target, insn);
8086 Stub_table<big_endian>* stub_table =
8087 arm_relobj->stub_table(shndx);
8088 gold_assert(stub_table != NULL);
8089 stub_table->add_cortex_a8_stub(address + i, stub);
8094 i += insn_32bit ? 4 : 2;
8095 last_was_32bit = insn_32bit;
8096 last_was_branch = is_32bit_branch;
8100 // Apply the Cortex-A8 workaround.
8102 template<bool big_endian>
8103 void
8104 Target_arm<big_endian>::apply_cortex_a8_workaround(
8105 const Cortex_a8_stub* stub,
8106 Arm_address stub_address,
8107 unsigned char* insn_view,
8108 Arm_address insn_address)
8110 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
8111 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
8112 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
8113 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
8114 off_t branch_offset = stub_address - (insn_address + 4);
8116 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
8117 switch (stub->stub_template()->type())
8119 case arm_stub_a8_veneer_b_cond:
8120 gold_assert(!utils::has_overflow<21>(branch_offset));
8121 upper_insn = RelocFuncs::thumb32_cond_branch_upper(upper_insn,
8122 branch_offset);
8123 lower_insn = RelocFuncs::thumb32_cond_branch_lower(lower_insn,
8124 branch_offset);
8125 break;
8127 case arm_stub_a8_veneer_b:
8128 case arm_stub_a8_veneer_bl:
8129 case arm_stub_a8_veneer_blx:
8130 if ((lower_insn & 0x5000U) == 0x4000U)
8131 // For a BLX instruction, make sure that the relocation is
8132 // rounded up to a word boundary. This follows the semantics of
8133 // the instruction which specifies that bit 1 of the target
8134 // address will come from bit 1 of the base address.
8135 branch_offset = (branch_offset + 2) & ~3;
8137 // Put BRANCH_OFFSET back into the insn.
8138 gold_assert(!utils::has_overflow<25>(branch_offset));
8139 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
8140 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
8141 break;
8143 default:
8144 gold_unreachable();
8147 // Put the relocated value back in the object file:
8148 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
8149 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
8152 template<bool big_endian>
8153 class Target_selector_arm : public Target_selector
8155 public:
8156 Target_selector_arm()
8157 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
8158 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
8161 Target*
8162 do_instantiate_target()
8163 { return new Target_arm<big_endian>(); }
8166 Target_selector_arm<false> target_selector_arm;
8167 Target_selector_arm<true> target_selector_armbe;
8169 } // End anonymous namespace.