Merge remote branch 'origin/master'
[binutils/dougsmingw.git] / gold / arm.cc
blob11a0734625e42aaaa8371b618aa6f377433fb990
1 // arm.cc -- arm target support for gold.
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
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>
35 #include <set>
37 #include "elfcpp.h"
38 #include "parameters.h"
39 #include "reloc.h"
40 #include "arm.h"
41 #include "object.h"
42 #include "symtab.h"
43 #include "layout.h"
44 #include "output.h"
45 #include "copy-relocs.h"
46 #include "target.h"
47 #include "target-reloc.h"
48 #include "target-select.h"
49 #include "tls.h"
50 #include "defstd.h"
51 #include "gc.h"
52 #include "attributes.h"
53 #include "arm-reloc-property.h"
55 namespace
58 using namespace gold;
60 template<bool big_endian>
61 class Output_data_plt_arm;
63 template<bool big_endian>
64 class Stub_table;
66 template<bool big_endian>
67 class Arm_input_section;
69 class Arm_exidx_cantunwind;
71 class Arm_exidx_merged_section;
73 class Arm_exidx_fixup;
75 template<bool big_endian>
76 class Arm_output_section;
78 class Arm_exidx_input_section;
80 template<bool big_endian>
81 class Arm_relobj;
83 template<bool big_endian>
84 class Arm_relocate_functions;
86 template<bool big_endian>
87 class Arm_output_data_got;
89 template<bool big_endian>
90 class Target_arm;
92 // For convenience.
93 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
95 // Maximum branch offsets for ARM, THUMB and THUMB2.
96 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
97 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
98 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
99 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
100 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
101 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
103 // Thread Control Block size.
104 const size_t ARM_TCB_SIZE = 8;
106 // The arm target class.
108 // This is a very simple port of gold for ARM-EABI. It is intended for
109 // supporting Android only for the time being.
111 // TODOs:
112 // - Implement all static relocation types documented in arm-reloc.def.
113 // - Make PLTs more flexible for different architecture features like
114 // Thumb-2 and BE8.
115 // There are probably a lot more.
117 // Ideally we would like to avoid using global variables but this is used
118 // very in many places and sometimes in loops. If we use a function
119 // returning a static instance of Arm_reloc_property_table, it will very
120 // slow in an threaded environment since the static instance needs to be
121 // locked. The pointer is below initialized in the
122 // Target::do_select_as_default_target() hook so that we do not spend time
123 // building the table if we are not linking ARM objects.
125 // An alternative is to to process the information in arm-reloc.def in
126 // compilation time and generate a representation of it in PODs only. That
127 // way we can avoid initialization when the linker starts.
129 Arm_reloc_property_table *arm_reloc_property_table = NULL;
131 // Instruction template class. This class is similar to the insn_sequence
132 // struct in bfd/elf32-arm.c.
134 class Insn_template
136 public:
137 // Types of instruction templates.
138 enum Type
140 THUMB16_TYPE = 1,
141 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
142 // templates with class-specific semantics. Currently this is used
143 // only by the Cortex_a8_stub class for handling condition codes in
144 // conditional branches.
145 THUMB16_SPECIAL_TYPE,
146 THUMB32_TYPE,
147 ARM_TYPE,
148 DATA_TYPE
151 // Factory methods to create instruction templates in different formats.
153 static const Insn_template
154 thumb16_insn(uint32_t data)
155 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
157 // A Thumb conditional branch, in which the proper condition is inserted
158 // when we build the stub.
159 static const Insn_template
160 thumb16_bcond_insn(uint32_t data)
161 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
163 static const Insn_template
164 thumb32_insn(uint32_t data)
165 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
167 static const Insn_template
168 thumb32_b_insn(uint32_t data, int reloc_addend)
170 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
171 reloc_addend);
174 static const Insn_template
175 arm_insn(uint32_t data)
176 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
178 static const Insn_template
179 arm_rel_insn(unsigned data, int reloc_addend)
180 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
182 static const Insn_template
183 data_word(unsigned data, unsigned int r_type, int reloc_addend)
184 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
186 // Accessors. This class is used for read-only objects so no modifiers
187 // are provided.
189 uint32_t
190 data() const
191 { return this->data_; }
193 // Return the instruction sequence type of this.
194 Type
195 type() const
196 { return this->type_; }
198 // Return the ARM relocation type of this.
199 unsigned int
200 r_type() const
201 { return this->r_type_; }
203 int32_t
204 reloc_addend() const
205 { return this->reloc_addend_; }
207 // Return size of instruction template in bytes.
208 size_t
209 size() const;
211 // Return byte-alignment of instruction template.
212 unsigned
213 alignment() const;
215 private:
216 // We make the constructor private to ensure that only the factory
217 // methods are used.
218 inline
219 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
220 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
223 // Instruction specific data. This is used to store information like
224 // some of the instruction bits.
225 uint32_t data_;
226 // Instruction template type.
227 Type type_;
228 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
229 unsigned int r_type_;
230 // Relocation addend.
231 int32_t reloc_addend_;
234 // Macro for generating code to stub types. One entry per long/short
235 // branch stub
237 #define DEF_STUBS \
238 DEF_STUB(long_branch_any_any) \
239 DEF_STUB(long_branch_v4t_arm_thumb) \
240 DEF_STUB(long_branch_thumb_only) \
241 DEF_STUB(long_branch_v4t_thumb_thumb) \
242 DEF_STUB(long_branch_v4t_thumb_arm) \
243 DEF_STUB(short_branch_v4t_thumb_arm) \
244 DEF_STUB(long_branch_any_arm_pic) \
245 DEF_STUB(long_branch_any_thumb_pic) \
246 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
247 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
248 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
249 DEF_STUB(long_branch_thumb_only_pic) \
250 DEF_STUB(a8_veneer_b_cond) \
251 DEF_STUB(a8_veneer_b) \
252 DEF_STUB(a8_veneer_bl) \
253 DEF_STUB(a8_veneer_blx) \
254 DEF_STUB(v4_veneer_bx)
256 // Stub types.
258 #define DEF_STUB(x) arm_stub_##x,
259 typedef enum
261 arm_stub_none,
262 DEF_STUBS
264 // First reloc stub type.
265 arm_stub_reloc_first = arm_stub_long_branch_any_any,
266 // Last reloc stub type.
267 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
269 // First Cortex-A8 stub type.
270 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
271 // Last Cortex-A8 stub type.
272 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
274 // Last stub type.
275 arm_stub_type_last = arm_stub_v4_veneer_bx
276 } Stub_type;
277 #undef DEF_STUB
279 // Stub template class. Templates are meant to be read-only objects.
280 // A stub template for a stub type contains all read-only attributes
281 // common to all stubs of the same type.
283 class Stub_template
285 public:
286 Stub_template(Stub_type, const Insn_template*, size_t);
288 ~Stub_template()
291 // Return stub type.
292 Stub_type
293 type() const
294 { return this->type_; }
296 // Return an array of instruction templates.
297 const Insn_template*
298 insns() const
299 { return this->insns_; }
301 // Return size of template in number of instructions.
302 size_t
303 insn_count() const
304 { return this->insn_count_; }
306 // Return size of template in bytes.
307 size_t
308 size() const
309 { return this->size_; }
311 // Return alignment of the stub template.
312 unsigned
313 alignment() const
314 { return this->alignment_; }
316 // Return whether entry point is in thumb mode.
317 bool
318 entry_in_thumb_mode() const
319 { return this->entry_in_thumb_mode_; }
321 // Return number of relocations in this template.
322 size_t
323 reloc_count() const
324 { return this->relocs_.size(); }
326 // Return index of the I-th instruction with relocation.
327 size_t
328 reloc_insn_index(size_t i) const
330 gold_assert(i < this->relocs_.size());
331 return this->relocs_[i].first;
334 // Return the offset of the I-th instruction with relocation from the
335 // beginning of the stub.
336 section_size_type
337 reloc_offset(size_t i) const
339 gold_assert(i < this->relocs_.size());
340 return this->relocs_[i].second;
343 private:
344 // This contains information about an instruction template with a relocation
345 // and its offset from start of stub.
346 typedef std::pair<size_t, section_size_type> Reloc;
348 // A Stub_template may not be copied. We want to share templates as much
349 // as possible.
350 Stub_template(const Stub_template&);
351 Stub_template& operator=(const Stub_template&);
353 // Stub type.
354 Stub_type type_;
355 // Points to an array of Insn_templates.
356 const Insn_template* insns_;
357 // Number of Insn_templates in insns_[].
358 size_t insn_count_;
359 // Size of templated instructions in bytes.
360 size_t size_;
361 // Alignment of templated instructions.
362 unsigned alignment_;
363 // Flag to indicate if entry is in thumb mode.
364 bool entry_in_thumb_mode_;
365 // A table of reloc instruction indices and offsets. We can find these by
366 // looking at the instruction templates but we pre-compute and then stash
367 // them here for speed.
368 std::vector<Reloc> relocs_;
372 // A class for code stubs. This is a base class for different type of
373 // stubs used in the ARM target.
376 class Stub
378 private:
379 static const section_offset_type invalid_offset =
380 static_cast<section_offset_type>(-1);
382 public:
383 Stub(const Stub_template* stub_template)
384 : stub_template_(stub_template), offset_(invalid_offset)
387 virtual
388 ~Stub()
391 // Return the stub template.
392 const Stub_template*
393 stub_template() const
394 { return this->stub_template_; }
396 // Return offset of code stub from beginning of its containing stub table.
397 section_offset_type
398 offset() const
400 gold_assert(this->offset_ != invalid_offset);
401 return this->offset_;
404 // Set offset of code stub from beginning of its containing stub table.
405 void
406 set_offset(section_offset_type offset)
407 { this->offset_ = offset; }
409 // Return the relocation target address of the i-th relocation in the
410 // stub. This must be defined in a child class.
411 Arm_address
412 reloc_target(size_t i)
413 { return this->do_reloc_target(i); }
415 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
416 void
417 write(unsigned char* view, section_size_type view_size, bool big_endian)
418 { this->do_write(view, view_size, big_endian); }
420 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
421 // for the i-th instruction.
422 uint16_t
423 thumb16_special(size_t i)
424 { return this->do_thumb16_special(i); }
426 protected:
427 // This must be defined in the child class.
428 virtual Arm_address
429 do_reloc_target(size_t) = 0;
431 // This may be overridden in the child class.
432 virtual void
433 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
435 if (big_endian)
436 this->do_fixed_endian_write<true>(view, view_size);
437 else
438 this->do_fixed_endian_write<false>(view, view_size);
441 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
442 // instruction template.
443 virtual uint16_t
444 do_thumb16_special(size_t)
445 { gold_unreachable(); }
447 private:
448 // A template to implement do_write.
449 template<bool big_endian>
450 void inline
451 do_fixed_endian_write(unsigned char*, section_size_type);
453 // Its template.
454 const Stub_template* stub_template_;
455 // Offset within the section of containing this stub.
456 section_offset_type offset_;
459 // Reloc stub class. These are stubs we use to fix up relocation because
460 // of limited branch ranges.
462 class Reloc_stub : public Stub
464 public:
465 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
466 // We assume we never jump to this address.
467 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
469 // Return destination address.
470 Arm_address
471 destination_address() const
473 gold_assert(this->destination_address_ != this->invalid_address);
474 return this->destination_address_;
477 // Set destination address.
478 void
479 set_destination_address(Arm_address address)
481 gold_assert(address != this->invalid_address);
482 this->destination_address_ = address;
485 // Reset destination address.
486 void
487 reset_destination_address()
488 { this->destination_address_ = this->invalid_address; }
490 // Determine stub type for a branch of a relocation of R_TYPE going
491 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
492 // the branch target is a thumb instruction. TARGET is used for look
493 // up ARM-specific linker settings.
494 static Stub_type
495 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
496 Arm_address branch_target, bool target_is_thumb);
498 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
499 // and an addend. Since we treat global and local symbol differently, we
500 // use a Symbol object for a global symbol and a object-index pair for
501 // a local symbol.
502 class Key
504 public:
505 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
506 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
507 // and R_SYM must not be invalid_index.
508 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
509 unsigned int r_sym, int32_t addend)
510 : stub_type_(stub_type), addend_(addend)
512 if (symbol != NULL)
514 this->r_sym_ = Reloc_stub::invalid_index;
515 this->u_.symbol = symbol;
517 else
519 gold_assert(relobj != NULL && r_sym != invalid_index);
520 this->r_sym_ = r_sym;
521 this->u_.relobj = relobj;
525 ~Key()
528 // Accessors: Keys are meant to be read-only object so no modifiers are
529 // provided.
531 // Return stub type.
532 Stub_type
533 stub_type() const
534 { return this->stub_type_; }
536 // Return the local symbol index or invalid_index.
537 unsigned int
538 r_sym() const
539 { return this->r_sym_; }
541 // Return the symbol if there is one.
542 const Symbol*
543 symbol() const
544 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
546 // Return the relobj if there is one.
547 const Relobj*
548 relobj() const
549 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
551 // Whether this equals to another key k.
552 bool
553 eq(const Key& k) const
555 return ((this->stub_type_ == k.stub_type_)
556 && (this->r_sym_ == k.r_sym_)
557 && ((this->r_sym_ != Reloc_stub::invalid_index)
558 ? (this->u_.relobj == k.u_.relobj)
559 : (this->u_.symbol == k.u_.symbol))
560 && (this->addend_ == k.addend_));
563 // Return a hash value.
564 size_t
565 hash_value() const
567 return (this->stub_type_
568 ^ this->r_sym_
569 ^ gold::string_hash<char>(
570 (this->r_sym_ != Reloc_stub::invalid_index)
571 ? this->u_.relobj->name().c_str()
572 : this->u_.symbol->name())
573 ^ this->addend_);
576 // Functors for STL associative containers.
577 struct hash
579 size_t
580 operator()(const Key& k) const
581 { return k.hash_value(); }
584 struct equal_to
586 bool
587 operator()(const Key& k1, const Key& k2) const
588 { return k1.eq(k2); }
591 // Name of key. This is mainly for debugging.
592 std::string
593 name() const;
595 private:
596 // Stub type.
597 Stub_type stub_type_;
598 // If this is a local symbol, this is the index in the defining object.
599 // Otherwise, it is invalid_index for a global symbol.
600 unsigned int r_sym_;
601 // If r_sym_ is invalid index. This points to a global symbol.
602 // Otherwise, this points a relobj. We used the unsized and target
603 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
604 // Arm_relobj. This is done to avoid making the stub class a template
605 // as most of the stub machinery is endianness-neutral. However, it
606 // may require a bit of casting done by users of this class.
607 union
609 const Symbol* symbol;
610 const Relobj* relobj;
611 } u_;
612 // Addend associated with a reloc.
613 int32_t addend_;
616 protected:
617 // Reloc_stubs are created via a stub factory. So these are protected.
618 Reloc_stub(const Stub_template* stub_template)
619 : Stub(stub_template), destination_address_(invalid_address)
622 ~Reloc_stub()
625 friend class Stub_factory;
627 // Return the relocation target address of the i-th relocation in the
628 // stub.
629 Arm_address
630 do_reloc_target(size_t i)
632 // All reloc stub have only one relocation.
633 gold_assert(i == 0);
634 return this->destination_address_;
637 private:
638 // Address of destination.
639 Arm_address destination_address_;
642 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
643 // THUMB branch that meets the following conditions:
645 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
646 // branch address is 0xffe.
647 // 2. The branch target address is in the same page as the first word of the
648 // branch.
649 // 3. The branch follows a 32-bit instruction which is not a branch.
651 // To do the fix up, we need to store the address of the branch instruction
652 // and its target at least. We also need to store the original branch
653 // instruction bits for the condition code in a conditional branch. The
654 // condition code is used in a special instruction template. We also want
655 // to identify input sections needing Cortex-A8 workaround quickly. We store
656 // extra information about object and section index of the code section
657 // containing a branch being fixed up. The information is used to mark
658 // the code section when we finalize the Cortex-A8 stubs.
661 class Cortex_a8_stub : public Stub
663 public:
664 ~Cortex_a8_stub()
667 // Return the object of the code section containing the branch being fixed
668 // up.
669 Relobj*
670 relobj() const
671 { return this->relobj_; }
673 // Return the section index of the code section containing the branch being
674 // fixed up.
675 unsigned int
676 shndx() const
677 { return this->shndx_; }
679 // Return the source address of stub. This is the address of the original
680 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
681 // instruction.
682 Arm_address
683 source_address() const
684 { return this->source_address_; }
686 // Return the destination address of the stub. This is the branch taken
687 // address of the original branch instruction. LSB is 1 if it is a THUMB
688 // instruction address.
689 Arm_address
690 destination_address() const
691 { return this->destination_address_; }
693 // Return the instruction being fixed up.
694 uint32_t
695 original_insn() const
696 { return this->original_insn_; }
698 protected:
699 // Cortex_a8_stubs are created via a stub factory. So these are protected.
700 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
701 unsigned int shndx, Arm_address source_address,
702 Arm_address destination_address, uint32_t original_insn)
703 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
704 source_address_(source_address | 1U),
705 destination_address_(destination_address),
706 original_insn_(original_insn)
709 friend class Stub_factory;
711 // Return the relocation target address of the i-th relocation in the
712 // stub.
713 Arm_address
714 do_reloc_target(size_t i)
716 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
718 // The conditional branch veneer has two relocations.
719 gold_assert(i < 2);
720 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
722 else
724 // All other Cortex-A8 stubs have only one relocation.
725 gold_assert(i == 0);
726 return this->destination_address_;
730 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
731 uint16_t
732 do_thumb16_special(size_t);
734 private:
735 // Object of the code section containing the branch being fixed up.
736 Relobj* relobj_;
737 // Section index of the code section containing the branch begin fixed up.
738 unsigned int shndx_;
739 // Source address of original branch.
740 Arm_address source_address_;
741 // Destination address of the original branch.
742 Arm_address destination_address_;
743 // Original branch instruction. This is needed for copying the condition
744 // code from a condition branch to its stub.
745 uint32_t original_insn_;
748 // ARMv4 BX Rx branch relocation stub class.
749 class Arm_v4bx_stub : public Stub
751 public:
752 ~Arm_v4bx_stub()
755 // Return the associated register.
756 uint32_t
757 reg() const
758 { return this->reg_; }
760 protected:
761 // Arm V4BX stubs are created via a stub factory. So these are protected.
762 Arm_v4bx_stub(const Stub_template* stub_template, const uint32_t reg)
763 : Stub(stub_template), reg_(reg)
766 friend class Stub_factory;
768 // Return the relocation target address of the i-th relocation in the
769 // stub.
770 Arm_address
771 do_reloc_target(size_t)
772 { gold_unreachable(); }
774 // This may be overridden in the child class.
775 virtual void
776 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
778 if (big_endian)
779 this->do_fixed_endian_v4bx_write<true>(view, view_size);
780 else
781 this->do_fixed_endian_v4bx_write<false>(view, view_size);
784 private:
785 // A template to implement do_write.
786 template<bool big_endian>
787 void inline
788 do_fixed_endian_v4bx_write(unsigned char* view, section_size_type)
790 const Insn_template* insns = this->stub_template()->insns();
791 elfcpp::Swap<32, big_endian>::writeval(view,
792 (insns[0].data()
793 + (this->reg_ << 16)));
794 view += insns[0].size();
795 elfcpp::Swap<32, big_endian>::writeval(view,
796 (insns[1].data() + this->reg_));
797 view += insns[1].size();
798 elfcpp::Swap<32, big_endian>::writeval(view,
799 (insns[2].data() + this->reg_));
802 // A register index (r0-r14), which is associated with the stub.
803 uint32_t reg_;
806 // Stub factory class.
808 class Stub_factory
810 public:
811 // Return the unique instance of this class.
812 static const Stub_factory&
813 get_instance()
815 static Stub_factory singleton;
816 return singleton;
819 // Make a relocation stub.
820 Reloc_stub*
821 make_reloc_stub(Stub_type stub_type) const
823 gold_assert(stub_type >= arm_stub_reloc_first
824 && stub_type <= arm_stub_reloc_last);
825 return new Reloc_stub(this->stub_templates_[stub_type]);
828 // Make a Cortex-A8 stub.
829 Cortex_a8_stub*
830 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
831 Arm_address source, Arm_address destination,
832 uint32_t original_insn) const
834 gold_assert(stub_type >= arm_stub_cortex_a8_first
835 && stub_type <= arm_stub_cortex_a8_last);
836 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
837 source, destination, original_insn);
840 // Make an ARM V4BX relocation stub.
841 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
842 Arm_v4bx_stub*
843 make_arm_v4bx_stub(uint32_t reg) const
845 gold_assert(reg < 0xf);
846 return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
847 reg);
850 private:
851 // Constructor and destructor are protected since we only return a single
852 // instance created in Stub_factory::get_instance().
854 Stub_factory();
856 // A Stub_factory may not be copied since it is a singleton.
857 Stub_factory(const Stub_factory&);
858 Stub_factory& operator=(Stub_factory&);
860 // Stub templates. These are initialized in the constructor.
861 const Stub_template* stub_templates_[arm_stub_type_last+1];
864 // A class to hold stubs for the ARM target.
866 template<bool big_endian>
867 class Stub_table : public Output_data
869 public:
870 Stub_table(Arm_input_section<big_endian>* owner)
871 : Output_data(), owner_(owner), reloc_stubs_(), reloc_stubs_size_(0),
872 reloc_stubs_addralign_(1), cortex_a8_stubs_(), arm_v4bx_stubs_(0xf),
873 prev_data_size_(0), prev_addralign_(1)
876 ~Stub_table()
879 // Owner of this stub table.
880 Arm_input_section<big_endian>*
881 owner() const
882 { return this->owner_; }
884 // Whether this stub table is empty.
885 bool
886 empty() const
888 return (this->reloc_stubs_.empty()
889 && this->cortex_a8_stubs_.empty()
890 && this->arm_v4bx_stubs_.empty());
893 // Return the current data size.
894 off_t
895 current_data_size() const
896 { return this->current_data_size_for_child(); }
898 // Add a STUB with using KEY. Caller is reponsible for avoid adding
899 // if already a STUB with the same key has been added.
900 void
901 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
903 const Stub_template* stub_template = stub->stub_template();
904 gold_assert(stub_template->type() == key.stub_type());
905 this->reloc_stubs_[key] = stub;
907 // Assign stub offset early. We can do this because we never remove
908 // reloc stubs and they are in the beginning of the stub table.
909 uint64_t align = stub_template->alignment();
910 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_, align);
911 stub->set_offset(this->reloc_stubs_size_);
912 this->reloc_stubs_size_ += stub_template->size();
913 this->reloc_stubs_addralign_ =
914 std::max(this->reloc_stubs_addralign_, align);
917 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
918 // Caller is reponsible for avoid adding if already a STUB with the same
919 // address has been added.
920 void
921 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
923 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
924 this->cortex_a8_stubs_.insert(value);
927 // Add an ARM V4BX relocation stub. A register index will be retrieved
928 // from the stub.
929 void
930 add_arm_v4bx_stub(Arm_v4bx_stub* stub)
932 gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
933 this->arm_v4bx_stubs_[stub->reg()] = stub;
936 // Remove all Cortex-A8 stubs.
937 void
938 remove_all_cortex_a8_stubs();
940 // Look up a relocation stub using KEY. Return NULL if there is none.
941 Reloc_stub*
942 find_reloc_stub(const Reloc_stub::Key& key) const
944 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
945 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
948 // Look up an arm v4bx relocation stub using the register index.
949 // Return NULL if there is none.
950 Arm_v4bx_stub*
951 find_arm_v4bx_stub(const uint32_t reg) const
953 gold_assert(reg < 0xf);
954 return this->arm_v4bx_stubs_[reg];
957 // Relocate stubs in this stub table.
958 void
959 relocate_stubs(const Relocate_info<32, big_endian>*,
960 Target_arm<big_endian>*, Output_section*,
961 unsigned char*, Arm_address, section_size_type);
963 // Update data size and alignment at the end of a relaxation pass. Return
964 // true if either data size or alignment is different from that of the
965 // previous relaxation pass.
966 bool
967 update_data_size_and_addralign();
969 // Finalize stubs. Set the offsets of all stubs and mark input sections
970 // needing the Cortex-A8 workaround.
971 void
972 finalize_stubs();
974 // Apply Cortex-A8 workaround to an address range.
975 void
976 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
977 unsigned char*, Arm_address,
978 section_size_type);
980 protected:
981 // Write out section contents.
982 void
983 do_write(Output_file*);
985 // Return the required alignment.
986 uint64_t
987 do_addralign() const
988 { return this->prev_addralign_; }
990 // Reset address and file offset.
991 void
992 do_reset_address_and_file_offset()
993 { this->set_current_data_size_for_child(this->prev_data_size_); }
995 // Set final data size.
996 void
997 set_final_data_size()
998 { this->set_data_size(this->current_data_size()); }
1000 private:
1001 // Relocate one stub.
1002 void
1003 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1004 Target_arm<big_endian>*, Output_section*,
1005 unsigned char*, Arm_address, section_size_type);
1007 // Unordered map of relocation stubs.
1008 typedef
1009 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
1010 Reloc_stub::Key::equal_to>
1011 Reloc_stub_map;
1013 // List of Cortex-A8 stubs ordered by addresses of branches being
1014 // fixed up in output.
1015 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
1016 // List of Arm V4BX relocation stubs ordered by associated registers.
1017 typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
1019 // Owner of this stub table.
1020 Arm_input_section<big_endian>* owner_;
1021 // The relocation stubs.
1022 Reloc_stub_map reloc_stubs_;
1023 // Size of reloc stubs.
1024 off_t reloc_stubs_size_;
1025 // Maximum address alignment of reloc stubs.
1026 uint64_t reloc_stubs_addralign_;
1027 // The cortex_a8_stubs.
1028 Cortex_a8_stub_list cortex_a8_stubs_;
1029 // The Arm V4BX relocation stubs.
1030 Arm_v4bx_stub_list arm_v4bx_stubs_;
1031 // data size of this in the previous pass.
1032 off_t prev_data_size_;
1033 // address alignment of this in the previous pass.
1034 uint64_t prev_addralign_;
1037 // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1038 // we add to the end of an EXIDX input section that goes into the output.
1040 class Arm_exidx_cantunwind : public Output_section_data
1042 public:
1043 Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
1044 : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
1047 // Return the object containing the section pointed by this.
1048 Relobj*
1049 relobj() const
1050 { return this->relobj_; }
1052 // Return the section index of the section pointed by this.
1053 unsigned int
1054 shndx() const
1055 { return this->shndx_; }
1057 protected:
1058 void
1059 do_write(Output_file* of)
1061 if (parameters->target().is_big_endian())
1062 this->do_fixed_endian_write<true>(of);
1063 else
1064 this->do_fixed_endian_write<false>(of);
1067 private:
1068 // Implement do_write for a given endianness.
1069 template<bool big_endian>
1070 void inline
1071 do_fixed_endian_write(Output_file*);
1073 // The object containing the section pointed by this.
1074 Relobj* relobj_;
1075 // The section index of the section pointed by this.
1076 unsigned int shndx_;
1079 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1080 // Offset map is used to map input section offset within the EXIDX section
1081 // to the output offset from the start of this EXIDX section.
1083 typedef std::map<section_offset_type, section_offset_type>
1084 Arm_exidx_section_offset_map;
1086 // Arm_exidx_merged_section class. This represents an EXIDX input section
1087 // with some of its entries merged.
1089 class Arm_exidx_merged_section : public Output_relaxed_input_section
1091 public:
1092 // Constructor for Arm_exidx_merged_section.
1093 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1094 // SECTION_OFFSET_MAP points to a section offset map describing how
1095 // parts of the input section are mapped to output. DELETED_BYTES is
1096 // the number of bytes deleted from the EXIDX input section.
1097 Arm_exidx_merged_section(
1098 const Arm_exidx_input_section& exidx_input_section,
1099 const Arm_exidx_section_offset_map& section_offset_map,
1100 uint32_t deleted_bytes);
1102 // Return the original EXIDX input section.
1103 const Arm_exidx_input_section&
1104 exidx_input_section() const
1105 { return this->exidx_input_section_; }
1107 // Return the section offset map.
1108 const Arm_exidx_section_offset_map&
1109 section_offset_map() const
1110 { return this->section_offset_map_; }
1112 protected:
1113 // Write merged section into file OF.
1114 void
1115 do_write(Output_file* of);
1117 bool
1118 do_output_offset(const Relobj*, unsigned int, section_offset_type,
1119 section_offset_type*) const;
1121 private:
1122 // Original EXIDX input section.
1123 const Arm_exidx_input_section& exidx_input_section_;
1124 // Section offset map.
1125 const Arm_exidx_section_offset_map& section_offset_map_;
1128 // A class to wrap an ordinary input section containing executable code.
1130 template<bool big_endian>
1131 class Arm_input_section : public Output_relaxed_input_section
1133 public:
1134 Arm_input_section(Relobj* relobj, unsigned int shndx)
1135 : Output_relaxed_input_section(relobj, shndx, 1),
1136 original_addralign_(1), original_size_(0), stub_table_(NULL)
1139 ~Arm_input_section()
1142 // Initialize.
1143 void
1144 init();
1146 // Whether this is a stub table owner.
1147 bool
1148 is_stub_table_owner() const
1149 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1151 // Return the stub table.
1152 Stub_table<big_endian>*
1153 stub_table() const
1154 { return this->stub_table_; }
1156 // Set the stub_table.
1157 void
1158 set_stub_table(Stub_table<big_endian>* stub_table)
1159 { this->stub_table_ = stub_table; }
1161 // Downcast a base pointer to an Arm_input_section pointer. This is
1162 // not type-safe but we only use Arm_input_section not the base class.
1163 static Arm_input_section<big_endian>*
1164 as_arm_input_section(Output_relaxed_input_section* poris)
1165 { return static_cast<Arm_input_section<big_endian>*>(poris); }
1167 protected:
1168 // Write data to output file.
1169 void
1170 do_write(Output_file*);
1172 // Return required alignment of this.
1173 uint64_t
1174 do_addralign() const
1176 if (this->is_stub_table_owner())
1177 return std::max(this->stub_table_->addralign(),
1178 this->original_addralign_);
1179 else
1180 return this->original_addralign_;
1183 // Finalize data size.
1184 void
1185 set_final_data_size();
1187 // Reset address and file offset.
1188 void
1189 do_reset_address_and_file_offset();
1191 // Output offset.
1192 bool
1193 do_output_offset(const Relobj* object, unsigned int shndx,
1194 section_offset_type offset,
1195 section_offset_type* poutput) const
1197 if ((object == this->relobj())
1198 && (shndx == this->shndx())
1199 && (offset >= 0)
1200 && (convert_types<uint64_t, section_offset_type>(offset)
1201 <= this->original_size_))
1203 *poutput = offset;
1204 return true;
1206 else
1207 return false;
1210 private:
1211 // Copying is not allowed.
1212 Arm_input_section(const Arm_input_section&);
1213 Arm_input_section& operator=(const Arm_input_section&);
1215 // Address alignment of the original input section.
1216 uint64_t original_addralign_;
1217 // Section size of the original input section.
1218 uint64_t original_size_;
1219 // Stub table.
1220 Stub_table<big_endian>* stub_table_;
1223 // Arm_exidx_fixup class. This is used to define a number of methods
1224 // and keep states for fixing up EXIDX coverage.
1226 class Arm_exidx_fixup
1228 public:
1229 Arm_exidx_fixup(Output_section* exidx_output_section,
1230 bool merge_exidx_entries = true)
1231 : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1232 last_inlined_entry_(0), last_input_section_(NULL),
1233 section_offset_map_(NULL), first_output_text_section_(NULL),
1234 merge_exidx_entries_(merge_exidx_entries)
1237 ~Arm_exidx_fixup()
1238 { delete this->section_offset_map_; }
1240 // Process an EXIDX section for entry merging. Return number of bytes to
1241 // be deleted in output. If parts of the input EXIDX section are merged
1242 // a heap allocated Arm_exidx_section_offset_map is store in the located
1243 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1244 // releasing it.
1245 template<bool big_endian>
1246 uint32_t
1247 process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1248 Arm_exidx_section_offset_map** psection_offset_map);
1250 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1251 // input section, if there is not one already.
1252 void
1253 add_exidx_cantunwind_as_needed();
1255 // Return the output section for the text section which is linked to the
1256 // first exidx input in output.
1257 Output_section*
1258 first_output_text_section() const
1259 { return this->first_output_text_section_; }
1261 private:
1262 // Copying is not allowed.
1263 Arm_exidx_fixup(const Arm_exidx_fixup&);
1264 Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1266 // Type of EXIDX unwind entry.
1267 enum Unwind_type
1269 // No type.
1270 UT_NONE,
1271 // EXIDX_CANTUNWIND.
1272 UT_EXIDX_CANTUNWIND,
1273 // Inlined entry.
1274 UT_INLINED_ENTRY,
1275 // Normal entry.
1276 UT_NORMAL_ENTRY,
1279 // Process an EXIDX entry. We only care about the second word of the
1280 // entry. Return true if the entry can be deleted.
1281 bool
1282 process_exidx_entry(uint32_t second_word);
1284 // Update the current section offset map during EXIDX section fix-up.
1285 // If there is no map, create one. INPUT_OFFSET is the offset of a
1286 // reference point, DELETED_BYTES is the number of deleted by in the
1287 // section so far. If DELETE_ENTRY is true, the reference point and
1288 // all offsets after the previous reference point are discarded.
1289 void
1290 update_offset_map(section_offset_type input_offset,
1291 section_size_type deleted_bytes, bool delete_entry);
1293 // EXIDX output section.
1294 Output_section* exidx_output_section_;
1295 // Unwind type of the last EXIDX entry processed.
1296 Unwind_type last_unwind_type_;
1297 // Last seen inlined EXIDX entry.
1298 uint32_t last_inlined_entry_;
1299 // Last processed EXIDX input section.
1300 const Arm_exidx_input_section* last_input_section_;
1301 // Section offset map created in process_exidx_section.
1302 Arm_exidx_section_offset_map* section_offset_map_;
1303 // Output section for the text section which is linked to the first exidx
1304 // input in output.
1305 Output_section* first_output_text_section_;
1307 bool merge_exidx_entries_;
1310 // Arm output section class. This is defined mainly to add a number of
1311 // stub generation methods.
1313 template<bool big_endian>
1314 class Arm_output_section : public Output_section
1316 public:
1317 typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
1319 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1320 elfcpp::Elf_Xword flags)
1321 : Output_section(name, type, flags)
1324 ~Arm_output_section()
1327 // Group input sections for stub generation.
1328 void
1329 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1331 // Downcast a base pointer to an Arm_output_section pointer. This is
1332 // not type-safe but we only use Arm_output_section not the base class.
1333 static Arm_output_section<big_endian>*
1334 as_arm_output_section(Output_section* os)
1335 { return static_cast<Arm_output_section<big_endian>*>(os); }
1337 // Append all input text sections in this into LIST.
1338 void
1339 append_text_sections_to_list(Text_section_list* list);
1341 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1342 // is a list of text input sections sorted in ascending order of their
1343 // output addresses.
1344 void
1345 fix_exidx_coverage(Layout* layout,
1346 const Text_section_list& sorted_text_section,
1347 Symbol_table* symtab,
1348 bool merge_exidx_entries);
1350 private:
1351 // For convenience.
1352 typedef Output_section::Input_section Input_section;
1353 typedef Output_section::Input_section_list Input_section_list;
1355 // Create a stub group.
1356 void create_stub_group(Input_section_list::const_iterator,
1357 Input_section_list::const_iterator,
1358 Input_section_list::const_iterator,
1359 Target_arm<big_endian>*,
1360 std::vector<Output_relaxed_input_section*>*);
1363 // Arm_exidx_input_section class. This represents an EXIDX input section.
1365 class Arm_exidx_input_section
1367 public:
1368 static const section_offset_type invalid_offset =
1369 static_cast<section_offset_type>(-1);
1371 Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1372 unsigned int link, uint32_t size, uint32_t addralign)
1373 : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1374 addralign_(addralign)
1377 ~Arm_exidx_input_section()
1380 // Accessors: This is a read-only class.
1382 // Return the object containing this EXIDX input section.
1383 Relobj*
1384 relobj() const
1385 { return this->relobj_; }
1387 // Return the section index of this EXIDX input section.
1388 unsigned int
1389 shndx() const
1390 { return this->shndx_; }
1392 // Return the section index of linked text section in the same object.
1393 unsigned int
1394 link() const
1395 { return this->link_; }
1397 // Return size of the EXIDX input section.
1398 uint32_t
1399 size() const
1400 { return this->size_; }
1402 // Reutnr address alignment of EXIDX input section.
1403 uint32_t
1404 addralign() const
1405 { return this->addralign_; }
1407 private:
1408 // Object containing this.
1409 Relobj* relobj_;
1410 // Section index of this.
1411 unsigned int shndx_;
1412 // text section linked to this in the same object.
1413 unsigned int link_;
1414 // Size of this. For ARM 32-bit is sufficient.
1415 uint32_t size_;
1416 // Address alignment of this. For ARM 32-bit is sufficient.
1417 uint32_t addralign_;
1420 // Arm_relobj class.
1422 template<bool big_endian>
1423 class Arm_relobj : public Sized_relobj<32, big_endian>
1425 public:
1426 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1428 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1429 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1430 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1431 stub_tables_(), local_symbol_is_thumb_function_(),
1432 attributes_section_data_(NULL), mapping_symbols_info_(),
1433 section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
1434 output_local_symbol_count_needs_update_(false),
1435 merge_flags_and_attributes_(true)
1438 ~Arm_relobj()
1439 { delete this->attributes_section_data_; }
1441 // Return the stub table of the SHNDX-th section if there is one.
1442 Stub_table<big_endian>*
1443 stub_table(unsigned int shndx) const
1445 gold_assert(shndx < this->stub_tables_.size());
1446 return this->stub_tables_[shndx];
1449 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1450 void
1451 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1453 gold_assert(shndx < this->stub_tables_.size());
1454 this->stub_tables_[shndx] = stub_table;
1457 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1458 // index. This is only valid after do_count_local_symbol is called.
1459 bool
1460 local_symbol_is_thumb_function(unsigned int r_sym) const
1462 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1463 return this->local_symbol_is_thumb_function_[r_sym];
1466 // Scan all relocation sections for stub generation.
1467 void
1468 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1469 const Layout*);
1471 // Convert regular input section with index SHNDX to a relaxed section.
1472 void
1473 convert_input_section_to_relaxed_section(unsigned shndx)
1475 // The stubs have relocations and we need to process them after writing
1476 // out the stubs. So relocation now must follow section write.
1477 this->set_section_offset(shndx, -1ULL);
1478 this->set_relocs_must_follow_section_writes();
1481 // Downcast a base pointer to an Arm_relobj pointer. This is
1482 // not type-safe but we only use Arm_relobj not the base class.
1483 static Arm_relobj<big_endian>*
1484 as_arm_relobj(Relobj* relobj)
1485 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1487 // Processor-specific flags in ELF file header. This is valid only after
1488 // reading symbols.
1489 elfcpp::Elf_Word
1490 processor_specific_flags() const
1491 { return this->processor_specific_flags_; }
1493 // Attribute section data This is the contents of the .ARM.attribute section
1494 // if there is one.
1495 const Attributes_section_data*
1496 attributes_section_data() const
1497 { return this->attributes_section_data_; }
1499 // Mapping symbol location.
1500 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1502 // Functor for STL container.
1503 struct Mapping_symbol_position_less
1505 bool
1506 operator()(const Mapping_symbol_position& p1,
1507 const Mapping_symbol_position& p2) const
1509 return (p1.first < p2.first
1510 || (p1.first == p2.first && p1.second < p2.second));
1514 // We only care about the first character of a mapping symbol, so
1515 // we only store that instead of the whole symbol name.
1516 typedef std::map<Mapping_symbol_position, char,
1517 Mapping_symbol_position_less> Mapping_symbols_info;
1519 // Whether a section contains any Cortex-A8 workaround.
1520 bool
1521 section_has_cortex_a8_workaround(unsigned int shndx) const
1523 return (this->section_has_cortex_a8_workaround_ != NULL
1524 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1527 // Mark a section that has Cortex-A8 workaround.
1528 void
1529 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1531 if (this->section_has_cortex_a8_workaround_ == NULL)
1532 this->section_has_cortex_a8_workaround_ =
1533 new std::vector<bool>(this->shnum(), false);
1534 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1537 // Return the EXIDX section of an text section with index SHNDX or NULL
1538 // if the text section has no associated EXIDX section.
1539 const Arm_exidx_input_section*
1540 exidx_input_section_by_link(unsigned int shndx) const
1542 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1543 return ((p != this->exidx_section_map_.end()
1544 && p->second->link() == shndx)
1545 ? p->second
1546 : NULL);
1549 // Return the EXIDX section with index SHNDX or NULL if there is none.
1550 const Arm_exidx_input_section*
1551 exidx_input_section_by_shndx(unsigned shndx) const
1553 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1554 return ((p != this->exidx_section_map_.end()
1555 && p->second->shndx() == shndx)
1556 ? p->second
1557 : NULL);
1560 // Whether output local symbol count needs updating.
1561 bool
1562 output_local_symbol_count_needs_update() const
1563 { return this->output_local_symbol_count_needs_update_; }
1565 // Set output_local_symbol_count_needs_update flag to be true.
1566 void
1567 set_output_local_symbol_count_needs_update()
1568 { this->output_local_symbol_count_needs_update_ = true; }
1570 // Update output local symbol count at the end of relaxation.
1571 void
1572 update_output_local_symbol_count();
1574 // Whether we want to merge processor-specific flags and attributes.
1575 bool
1576 merge_flags_and_attributes() const
1577 { return this->merge_flags_and_attributes_; }
1579 protected:
1580 // Post constructor setup.
1581 void
1582 do_setup()
1584 // Call parent's setup method.
1585 Sized_relobj<32, big_endian>::do_setup();
1587 // Initialize look-up tables.
1588 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1589 this->stub_tables_.swap(empty_stub_table_list);
1592 // Count the local symbols.
1593 void
1594 do_count_local_symbols(Stringpool_template<char>*,
1595 Stringpool_template<char>*);
1597 void
1598 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1599 const unsigned char* pshdrs,
1600 typename Sized_relobj<32, big_endian>::Views* pivews);
1602 // Read the symbol information.
1603 void
1604 do_read_symbols(Read_symbols_data* sd);
1606 // Process relocs for garbage collection.
1607 void
1608 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1610 private:
1612 // Whether a section needs to be scanned for relocation stubs.
1613 bool
1614 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1615 const Relobj::Output_sections&,
1616 const Symbol_table *, const unsigned char*);
1618 // Whether a section is a scannable text section.
1619 bool
1620 section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
1621 const Output_section*, const Symbol_table *);
1623 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1624 bool
1625 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1626 unsigned int, Output_section*,
1627 const Symbol_table *);
1629 // Scan a section for the Cortex-A8 erratum.
1630 void
1631 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1632 unsigned int, Output_section*,
1633 Target_arm<big_endian>*);
1635 // Find the linked text section of an EXIDX section by looking at the
1636 // first reloction of the EXIDX section. PSHDR points to the section
1637 // headers of a relocation section and PSYMS points to the local symbols.
1638 // PSHNDX points to a location storing the text section index if found.
1639 // Return whether we can find the linked section.
1640 bool
1641 find_linked_text_section(const unsigned char* pshdr,
1642 const unsigned char* psyms, unsigned int* pshndx);
1645 // Make a new Arm_exidx_input_section object for EXIDX section with
1646 // index SHNDX and section header SHDR. TEXT_SHNDX is the section
1647 // index of the linked text section.
1648 void
1649 make_exidx_input_section(unsigned int shndx,
1650 const elfcpp::Shdr<32, big_endian>& shdr,
1651 unsigned int text_shndx);
1653 // Return the output address of either a plain input section or a
1654 // relaxed input section. SHNDX is the section index.
1655 Arm_address
1656 simple_input_section_output_address(unsigned int, Output_section*);
1658 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1659 typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1660 Exidx_section_map;
1662 // List of stub tables.
1663 Stub_table_list stub_tables_;
1664 // Bit vector to tell if a local symbol is a thumb function or not.
1665 // This is only valid after do_count_local_symbol is called.
1666 std::vector<bool> local_symbol_is_thumb_function_;
1667 // processor-specific flags in ELF file header.
1668 elfcpp::Elf_Word processor_specific_flags_;
1669 // Object attributes if there is an .ARM.attributes section or NULL.
1670 Attributes_section_data* attributes_section_data_;
1671 // Mapping symbols information.
1672 Mapping_symbols_info mapping_symbols_info_;
1673 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1674 std::vector<bool>* section_has_cortex_a8_workaround_;
1675 // Map a text section to its associated .ARM.exidx section, if there is one.
1676 Exidx_section_map exidx_section_map_;
1677 // Whether output local symbol count needs updating.
1678 bool output_local_symbol_count_needs_update_;
1679 // Whether we merge processor flags and attributes of this object to
1680 // output.
1681 bool merge_flags_and_attributes_;
1684 // Arm_dynobj class.
1686 template<bool big_endian>
1687 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1689 public:
1690 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1691 const elfcpp::Ehdr<32, big_endian>& ehdr)
1692 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1693 processor_specific_flags_(0), attributes_section_data_(NULL)
1696 ~Arm_dynobj()
1697 { delete this->attributes_section_data_; }
1699 // Downcast a base pointer to an Arm_relobj pointer. This is
1700 // not type-safe but we only use Arm_relobj not the base class.
1701 static Arm_dynobj<big_endian>*
1702 as_arm_dynobj(Dynobj* dynobj)
1703 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1705 // Processor-specific flags in ELF file header. This is valid only after
1706 // reading symbols.
1707 elfcpp::Elf_Word
1708 processor_specific_flags() const
1709 { return this->processor_specific_flags_; }
1711 // Attributes section data.
1712 const Attributes_section_data*
1713 attributes_section_data() const
1714 { return this->attributes_section_data_; }
1716 protected:
1717 // Read the symbol information.
1718 void
1719 do_read_symbols(Read_symbols_data* sd);
1721 private:
1722 // processor-specific flags in ELF file header.
1723 elfcpp::Elf_Word processor_specific_flags_;
1724 // Object attributes if there is an .ARM.attributes section or NULL.
1725 Attributes_section_data* attributes_section_data_;
1728 // Functor to read reloc addends during stub generation.
1730 template<int sh_type, bool big_endian>
1731 struct Stub_addend_reader
1733 // Return the addend for a relocation of a particular type. Depending
1734 // on whether this is a REL or RELA relocation, read the addend from a
1735 // view or from a Reloc object.
1736 elfcpp::Elf_types<32>::Elf_Swxword
1737 operator()(
1738 unsigned int /* r_type */,
1739 const unsigned char* /* view */,
1740 const typename Reloc_types<sh_type,
1741 32, big_endian>::Reloc& /* reloc */) const;
1744 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1746 template<bool big_endian>
1747 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1749 elfcpp::Elf_types<32>::Elf_Swxword
1750 operator()(
1751 unsigned int,
1752 const unsigned char*,
1753 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1756 // Specialized Stub_addend_reader for RELA type relocation sections.
1757 // We currently do not handle RELA type relocation sections but it is trivial
1758 // to implement the addend reader. This is provided for completeness and to
1759 // make it easier to add support for RELA relocation sections in the future.
1761 template<bool big_endian>
1762 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1764 elfcpp::Elf_types<32>::Elf_Swxword
1765 operator()(
1766 unsigned int,
1767 const unsigned char*,
1768 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1769 big_endian>::Reloc& reloc) const
1770 { return reloc.get_r_addend(); }
1773 // Cortex_a8_reloc class. We keep record of relocation that may need
1774 // the Cortex-A8 erratum workaround.
1776 class Cortex_a8_reloc
1778 public:
1779 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1780 Arm_address destination)
1781 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1784 ~Cortex_a8_reloc()
1787 // Accessors: This is a read-only class.
1789 // Return the relocation stub associated with this relocation if there is
1790 // one.
1791 const Reloc_stub*
1792 reloc_stub() const
1793 { return this->reloc_stub_; }
1795 // Return the relocation type.
1796 unsigned int
1797 r_type() const
1798 { return this->r_type_; }
1800 // Return the destination address of the relocation. LSB stores the THUMB
1801 // bit.
1802 Arm_address
1803 destination() const
1804 { return this->destination_; }
1806 private:
1807 // Associated relocation stub if there is one, or NULL.
1808 const Reloc_stub* reloc_stub_;
1809 // Relocation type.
1810 unsigned int r_type_;
1811 // Destination address of this relocation. LSB is used to distinguish
1812 // ARM/THUMB mode.
1813 Arm_address destination_;
1816 // Arm_output_data_got class. We derive this from Output_data_got to add
1817 // extra methods to handle TLS relocations in a static link.
1819 template<bool big_endian>
1820 class Arm_output_data_got : public Output_data_got<32, big_endian>
1822 public:
1823 Arm_output_data_got(Symbol_table* symtab, Layout* layout)
1824 : Output_data_got<32, big_endian>(), symbol_table_(symtab), layout_(layout)
1827 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1828 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1829 // applied in a static link.
1830 void
1831 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1832 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
1834 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1835 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1836 // relocation that needs to be applied in a static link.
1837 void
1838 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1839 Sized_relobj<32, big_endian>* relobj, unsigned int index)
1841 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
1842 index));
1845 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
1846 // The first one is initialized to be 1, which is the module index for
1847 // the main executable and the second one 0. A reloc of the type
1848 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
1849 // be applied by gold. GSYM is a global symbol.
1850 void
1851 add_tls_gd32_with_static_reloc(unsigned int got_type, Symbol* gsym);
1853 // Same as the above but for a local symbol in OBJECT with INDEX.
1854 void
1855 add_tls_gd32_with_static_reloc(unsigned int got_type,
1856 Sized_relobj<32, big_endian>* object,
1857 unsigned int index);
1859 protected:
1860 // Write out the GOT table.
1861 void
1862 do_write(Output_file*);
1864 private:
1865 // This class represent dynamic relocations that need to be applied by
1866 // gold because we are using TLS relocations in a static link.
1867 class Static_reloc
1869 public:
1870 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1871 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
1872 { this->u_.global.symbol = gsym; }
1874 Static_reloc(unsigned int got_offset, unsigned int r_type,
1875 Sized_relobj<32, big_endian>* relobj, unsigned int index)
1876 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
1878 this->u_.local.relobj = relobj;
1879 this->u_.local.index = index;
1882 // Return the GOT offset.
1883 unsigned int
1884 got_offset() const
1885 { return this->got_offset_; }
1887 // Relocation type.
1888 unsigned int
1889 r_type() const
1890 { return this->r_type_; }
1892 // Whether the symbol is global or not.
1893 bool
1894 symbol_is_global() const
1895 { return this->symbol_is_global_; }
1897 // For a relocation against a global symbol, the global symbol.
1898 Symbol*
1899 symbol() const
1901 gold_assert(this->symbol_is_global_);
1902 return this->u_.global.symbol;
1905 // For a relocation against a local symbol, the defining object.
1906 Sized_relobj<32, big_endian>*
1907 relobj() const
1909 gold_assert(!this->symbol_is_global_);
1910 return this->u_.local.relobj;
1913 // For a relocation against a local symbol, the local symbol index.
1914 unsigned int
1915 index() const
1917 gold_assert(!this->symbol_is_global_);
1918 return this->u_.local.index;
1921 private:
1922 // GOT offset of the entry to which this relocation is applied.
1923 unsigned int got_offset_;
1924 // Type of relocation.
1925 unsigned int r_type_;
1926 // Whether this relocation is against a global symbol.
1927 bool symbol_is_global_;
1928 // A global or local symbol.
1929 union
1931 struct
1933 // For a global symbol, the symbol itself.
1934 Symbol* symbol;
1935 } global;
1936 struct
1938 // For a local symbol, the object defining object.
1939 Sized_relobj<32, big_endian>* relobj;
1940 // For a local symbol, the symbol index.
1941 unsigned int index;
1942 } local;
1943 } u_;
1946 // Symbol table of the output object.
1947 Symbol_table* symbol_table_;
1948 // Layout of the output object.
1949 Layout* layout_;
1950 // Static relocs to be applied to the GOT.
1951 std::vector<Static_reloc> static_relocs_;
1954 // Utilities for manipulating integers of up to 32-bits
1956 namespace utils
1958 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1959 // an int32_t. NO_BITS must be between 1 to 32.
1960 template<int no_bits>
1961 static inline int32_t
1962 sign_extend(uint32_t bits)
1964 gold_assert(no_bits >= 0 && no_bits <= 32);
1965 if (no_bits == 32)
1966 return static_cast<int32_t>(bits);
1967 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1968 bits &= mask;
1969 uint32_t top_bit = 1U << (no_bits - 1);
1970 int32_t as_signed = static_cast<int32_t>(bits);
1971 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1974 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1975 template<int no_bits>
1976 static inline bool
1977 has_overflow(uint32_t bits)
1979 gold_assert(no_bits >= 0 && no_bits <= 32);
1980 if (no_bits == 32)
1981 return false;
1982 int32_t max = (1 << (no_bits - 1)) - 1;
1983 int32_t min = -(1 << (no_bits - 1));
1984 int32_t as_signed = static_cast<int32_t>(bits);
1985 return as_signed > max || as_signed < min;
1988 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1989 // fits in the given number of bits as either a signed or unsigned value.
1990 // For example, has_signed_unsigned_overflow<8> would check
1991 // -128 <= bits <= 255
1992 template<int no_bits>
1993 static inline bool
1994 has_signed_unsigned_overflow(uint32_t bits)
1996 gold_assert(no_bits >= 2 && no_bits <= 32);
1997 if (no_bits == 32)
1998 return false;
1999 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
2000 int32_t min = -(1 << (no_bits - 1));
2001 int32_t as_signed = static_cast<int32_t>(bits);
2002 return as_signed > max || as_signed < min;
2005 // Select bits from A and B using bits in MASK. For each n in [0..31],
2006 // the n-th bit in the result is chosen from the n-th bits of A and B.
2007 // A zero selects A and a one selects B.
2008 static inline uint32_t
2009 bit_select(uint32_t a, uint32_t b, uint32_t mask)
2010 { return (a & ~mask) | (b & mask); }
2013 template<bool big_endian>
2014 class Target_arm : public Sized_target<32, big_endian>
2016 public:
2017 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
2018 Reloc_section;
2020 // When were are relocating a stub, we pass this as the relocation number.
2021 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
2023 Target_arm()
2024 : Sized_target<32, big_endian>(&arm_info),
2025 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
2026 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
2027 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
2028 stub_tables_(), stub_factory_(Stub_factory::get_instance()),
2029 may_use_blx_(false), should_force_pic_veneer_(false),
2030 arm_input_section_map_(), attributes_section_data_(NULL),
2031 fix_cortex_a8_(false), cortex_a8_relocs_info_()
2034 // Whether we can use BLX.
2035 bool
2036 may_use_blx() const
2037 { return this->may_use_blx_; }
2039 // Set use-BLX flag.
2040 void
2041 set_may_use_blx(bool value)
2042 { this->may_use_blx_ = value; }
2044 // Whether we force PCI branch veneers.
2045 bool
2046 should_force_pic_veneer() const
2047 { return this->should_force_pic_veneer_; }
2049 // Set PIC veneer flag.
2050 void
2051 set_should_force_pic_veneer(bool value)
2052 { this->should_force_pic_veneer_ = value; }
2054 // Whether we use THUMB-2 instructions.
2055 bool
2056 using_thumb2() const
2058 Object_attribute* attr =
2059 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2060 int arch = attr->int_value();
2061 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
2064 // Whether we use THUMB/THUMB-2 instructions only.
2065 bool
2066 using_thumb_only() const
2068 Object_attribute* attr =
2069 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2071 if (attr->int_value() == elfcpp::TAG_CPU_ARCH_V6_M
2072 || attr->int_value() == elfcpp::TAG_CPU_ARCH_V6S_M)
2073 return true;
2074 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
2075 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
2076 return false;
2077 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
2078 return attr->int_value() == 'M';
2081 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
2082 bool
2083 may_use_arm_nop() const
2085 Object_attribute* attr =
2086 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2087 int arch = attr->int_value();
2088 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2089 || arch == elfcpp::TAG_CPU_ARCH_V6K
2090 || arch == elfcpp::TAG_CPU_ARCH_V7
2091 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2094 // Whether we have THUMB-2 NOP.W instruction.
2095 bool
2096 may_use_thumb2_nop() const
2098 Object_attribute* attr =
2099 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2100 int arch = attr->int_value();
2101 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2102 || arch == elfcpp::TAG_CPU_ARCH_V7
2103 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2106 // Process the relocations to determine unreferenced sections for
2107 // garbage collection.
2108 void
2109 gc_process_relocs(Symbol_table* symtab,
2110 Layout* layout,
2111 Sized_relobj<32, big_endian>* object,
2112 unsigned int data_shndx,
2113 unsigned int sh_type,
2114 const unsigned char* prelocs,
2115 size_t reloc_count,
2116 Output_section* output_section,
2117 bool needs_special_offset_handling,
2118 size_t local_symbol_count,
2119 const unsigned char* plocal_symbols);
2121 // Scan the relocations to look for symbol adjustments.
2122 void
2123 scan_relocs(Symbol_table* symtab,
2124 Layout* layout,
2125 Sized_relobj<32, big_endian>* object,
2126 unsigned int data_shndx,
2127 unsigned int sh_type,
2128 const unsigned char* prelocs,
2129 size_t reloc_count,
2130 Output_section* output_section,
2131 bool needs_special_offset_handling,
2132 size_t local_symbol_count,
2133 const unsigned char* plocal_symbols);
2135 // Finalize the sections.
2136 void
2137 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2139 // Return the value to use for a dynamic symbol which requires special
2140 // treatment.
2141 uint64_t
2142 do_dynsym_value(const Symbol*) const;
2144 // Relocate a section.
2145 void
2146 relocate_section(const Relocate_info<32, big_endian>*,
2147 unsigned int sh_type,
2148 const unsigned char* prelocs,
2149 size_t reloc_count,
2150 Output_section* output_section,
2151 bool needs_special_offset_handling,
2152 unsigned char* view,
2153 Arm_address view_address,
2154 section_size_type view_size,
2155 const Reloc_symbol_changes*);
2157 // Scan the relocs during a relocatable link.
2158 void
2159 scan_relocatable_relocs(Symbol_table* symtab,
2160 Layout* layout,
2161 Sized_relobj<32, big_endian>* object,
2162 unsigned int data_shndx,
2163 unsigned int sh_type,
2164 const unsigned char* prelocs,
2165 size_t reloc_count,
2166 Output_section* output_section,
2167 bool needs_special_offset_handling,
2168 size_t local_symbol_count,
2169 const unsigned char* plocal_symbols,
2170 Relocatable_relocs*);
2172 // Relocate a section during a relocatable link.
2173 void
2174 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
2175 unsigned int sh_type,
2176 const unsigned char* prelocs,
2177 size_t reloc_count,
2178 Output_section* output_section,
2179 off_t offset_in_output_section,
2180 const Relocatable_relocs*,
2181 unsigned char* view,
2182 Arm_address view_address,
2183 section_size_type view_size,
2184 unsigned char* reloc_view,
2185 section_size_type reloc_view_size);
2187 // Return whether SYM is defined by the ABI.
2188 bool
2189 do_is_defined_by_abi(Symbol* sym) const
2190 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
2192 // Return whether there is a GOT section.
2193 bool
2194 has_got_section() const
2195 { return this->got_ != NULL; }
2197 // Return the size of the GOT section.
2198 section_size_type
2199 got_size()
2201 gold_assert(this->got_ != NULL);
2202 return this->got_->data_size();
2205 // Map platform-specific reloc types
2206 static unsigned int
2207 get_real_reloc_type (unsigned int r_type);
2210 // Methods to support stub-generations.
2213 // Return the stub factory
2214 const Stub_factory&
2215 stub_factory() const
2216 { return this->stub_factory_; }
2218 // Make a new Arm_input_section object.
2219 Arm_input_section<big_endian>*
2220 new_arm_input_section(Relobj*, unsigned int);
2222 // Find the Arm_input_section object corresponding to the SHNDX-th input
2223 // section of RELOBJ.
2224 Arm_input_section<big_endian>*
2225 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
2227 // Make a new Stub_table
2228 Stub_table<big_endian>*
2229 new_stub_table(Arm_input_section<big_endian>*);
2231 // Scan a section for stub generation.
2232 void
2233 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2234 const unsigned char*, size_t, Output_section*,
2235 bool, const unsigned char*, Arm_address,
2236 section_size_type);
2238 // Relocate a stub.
2239 void
2240 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
2241 Output_section*, unsigned char*, Arm_address,
2242 section_size_type);
2244 // Get the default ARM target.
2245 static Target_arm<big_endian>*
2246 default_target()
2248 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2249 && parameters->target().is_big_endian() == big_endian);
2250 return static_cast<Target_arm<big_endian>*>(
2251 parameters->sized_target<32, big_endian>());
2254 // Whether NAME belongs to a mapping symbol.
2255 static bool
2256 is_mapping_symbol_name(const char* name)
2258 return (name
2259 && name[0] == '$'
2260 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2261 && (name[2] == '\0' || name[2] == '.'));
2264 // Whether we work around the Cortex-A8 erratum.
2265 bool
2266 fix_cortex_a8() const
2267 { return this->fix_cortex_a8_; }
2269 // Whether we merge exidx entries in debuginfo.
2270 bool
2271 merge_exidx_entries() const
2272 { return parameters->options().merge_exidx_entries(); }
2274 // Whether we fix R_ARM_V4BX relocation.
2275 // 0 - do not fix
2276 // 1 - replace with MOV instruction (armv4 target)
2277 // 2 - make interworking veneer (>= armv4t targets only)
2278 General_options::Fix_v4bx
2279 fix_v4bx() const
2280 { return parameters->options().fix_v4bx(); }
2282 // Scan a span of THUMB code section for Cortex-A8 erratum.
2283 void
2284 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2285 section_size_type, section_size_type,
2286 const unsigned char*, Arm_address);
2288 // Apply Cortex-A8 workaround to a branch.
2289 void
2290 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2291 unsigned char*, Arm_address);
2293 protected:
2294 // Make an ELF object.
2295 Object*
2296 do_make_elf_object(const std::string&, Input_file*, off_t,
2297 const elfcpp::Ehdr<32, big_endian>& ehdr);
2299 Object*
2300 do_make_elf_object(const std::string&, Input_file*, off_t,
2301 const elfcpp::Ehdr<32, !big_endian>&)
2302 { gold_unreachable(); }
2304 Object*
2305 do_make_elf_object(const std::string&, Input_file*, off_t,
2306 const elfcpp::Ehdr<64, false>&)
2307 { gold_unreachable(); }
2309 Object*
2310 do_make_elf_object(const std::string&, Input_file*, off_t,
2311 const elfcpp::Ehdr<64, true>&)
2312 { gold_unreachable(); }
2314 // Make an output section.
2315 Output_section*
2316 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2317 elfcpp::Elf_Xword flags)
2318 { return new Arm_output_section<big_endian>(name, type, flags); }
2320 void
2321 do_adjust_elf_header(unsigned char* view, int len) const;
2323 // We only need to generate stubs, and hence perform relaxation if we are
2324 // not doing relocatable linking.
2325 bool
2326 do_may_relax() const
2327 { return !parameters->options().relocatable(); }
2329 bool
2330 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
2332 // Determine whether an object attribute tag takes an integer, a
2333 // string or both.
2335 do_attribute_arg_type(int tag) const;
2337 // Reorder tags during output.
2339 do_attributes_order(int num) const;
2341 // This is called when the target is selected as the default.
2342 void
2343 do_select_as_default_target()
2345 // No locking is required since there should only be one default target.
2346 // We cannot have both the big-endian and little-endian ARM targets
2347 // as the default.
2348 gold_assert(arm_reloc_property_table == NULL);
2349 arm_reloc_property_table = new Arm_reloc_property_table();
2352 private:
2353 // The class which scans relocations.
2354 class Scan
2356 public:
2357 Scan()
2358 : issued_non_pic_error_(false)
2361 inline void
2362 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
2363 Sized_relobj<32, big_endian>* object,
2364 unsigned int data_shndx,
2365 Output_section* output_section,
2366 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2367 const elfcpp::Sym<32, big_endian>& lsym);
2369 inline void
2370 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
2371 Sized_relobj<32, big_endian>* object,
2372 unsigned int data_shndx,
2373 Output_section* output_section,
2374 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2375 Symbol* gsym);
2377 inline bool
2378 local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2379 Sized_relobj<32, big_endian>* ,
2380 unsigned int ,
2381 Output_section* ,
2382 const elfcpp::Rel<32, big_endian>& ,
2383 unsigned int ,
2384 const elfcpp::Sym<32, big_endian>&)
2385 { return false; }
2387 inline bool
2388 global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2389 Sized_relobj<32, big_endian>* ,
2390 unsigned int ,
2391 Output_section* ,
2392 const elfcpp::Rel<32, big_endian>& ,
2393 unsigned int , Symbol*)
2394 { return false; }
2396 private:
2397 static void
2398 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
2399 unsigned int r_type);
2401 static void
2402 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
2403 unsigned int r_type, Symbol*);
2405 void
2406 check_non_pic(Relobj*, unsigned int r_type);
2408 // Almost identical to Symbol::needs_plt_entry except that it also
2409 // handles STT_ARM_TFUNC.
2410 static bool
2411 symbol_needs_plt_entry(const Symbol* sym)
2413 // An undefined symbol from an executable does not need a PLT entry.
2414 if (sym->is_undefined() && !parameters->options().shared())
2415 return false;
2417 return (!parameters->doing_static_link()
2418 && (sym->type() == elfcpp::STT_FUNC
2419 || sym->type() == elfcpp::STT_ARM_TFUNC)
2420 && (sym->is_from_dynobj()
2421 || sym->is_undefined()
2422 || sym->is_preemptible()));
2425 // Whether we have issued an error about a non-PIC compilation.
2426 bool issued_non_pic_error_;
2429 // The class which implements relocation.
2430 class Relocate
2432 public:
2433 Relocate()
2436 ~Relocate()
2439 // Return whether the static relocation needs to be applied.
2440 inline bool
2441 should_apply_static_reloc(const Sized_symbol<32>* gsym,
2442 int ref_flags,
2443 bool is_32bit,
2444 Output_section* output_section);
2446 // Do a relocation. Return false if the caller should not issue
2447 // any warnings about this relocation.
2448 inline bool
2449 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2450 Output_section*, size_t relnum,
2451 const elfcpp::Rel<32, big_endian>&,
2452 unsigned int r_type, const Sized_symbol<32>*,
2453 const Symbol_value<32>*,
2454 unsigned char*, Arm_address,
2455 section_size_type);
2457 // Return whether we want to pass flag NON_PIC_REF for this
2458 // reloc. This means the relocation type accesses a symbol not via
2459 // GOT or PLT.
2460 static inline bool
2461 reloc_is_non_pic (unsigned int r_type)
2463 switch (r_type)
2465 // These relocation types reference GOT or PLT entries explicitly.
2466 case elfcpp::R_ARM_GOT_BREL:
2467 case elfcpp::R_ARM_GOT_ABS:
2468 case elfcpp::R_ARM_GOT_PREL:
2469 case elfcpp::R_ARM_GOT_BREL12:
2470 case elfcpp::R_ARM_PLT32_ABS:
2471 case elfcpp::R_ARM_TLS_GD32:
2472 case elfcpp::R_ARM_TLS_LDM32:
2473 case elfcpp::R_ARM_TLS_IE32:
2474 case elfcpp::R_ARM_TLS_IE12GP:
2476 // These relocate types may use PLT entries.
2477 case elfcpp::R_ARM_CALL:
2478 case elfcpp::R_ARM_THM_CALL:
2479 case elfcpp::R_ARM_JUMP24:
2480 case elfcpp::R_ARM_THM_JUMP24:
2481 case elfcpp::R_ARM_THM_JUMP19:
2482 case elfcpp::R_ARM_PLT32:
2483 case elfcpp::R_ARM_THM_XPC22:
2484 case elfcpp::R_ARM_PREL31:
2485 case elfcpp::R_ARM_SBREL31:
2486 return false;
2488 default:
2489 return true;
2493 private:
2494 // Do a TLS relocation.
2495 inline typename Arm_relocate_functions<big_endian>::Status
2496 relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
2497 size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
2498 const Sized_symbol<32>*, const Symbol_value<32>*,
2499 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
2500 section_size_type);
2504 // A class which returns the size required for a relocation type,
2505 // used while scanning relocs during a relocatable link.
2506 class Relocatable_size_for_reloc
2508 public:
2509 unsigned int
2510 get_size_for_reloc(unsigned int, Relobj*);
2513 // Adjust TLS relocation type based on the options and whether this
2514 // is a local symbol.
2515 static tls::Tls_optimization
2516 optimize_tls_reloc(bool is_final, int r_type);
2518 // Get the GOT section, creating it if necessary.
2519 Arm_output_data_got<big_endian>*
2520 got_section(Symbol_table*, Layout*);
2522 // Get the GOT PLT section.
2523 Output_data_space*
2524 got_plt_section() const
2526 gold_assert(this->got_plt_ != NULL);
2527 return this->got_plt_;
2530 // Create a PLT entry for a global symbol.
2531 void
2532 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2534 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2535 void
2536 define_tls_base_symbol(Symbol_table*, Layout*);
2538 // Create a GOT entry for the TLS module index.
2539 unsigned int
2540 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2541 Sized_relobj<32, big_endian>* object);
2543 // Get the PLT section.
2544 const Output_data_plt_arm<big_endian>*
2545 plt_section() const
2547 gold_assert(this->plt_ != NULL);
2548 return this->plt_;
2551 // Get the dynamic reloc section, creating it if necessary.
2552 Reloc_section*
2553 rel_dyn_section(Layout*);
2555 // Get the section to use for TLS_DESC relocations.
2556 Reloc_section*
2557 rel_tls_desc_section(Layout*) const;
2559 // Return true if the symbol may need a COPY relocation.
2560 // References from an executable object to non-function symbols
2561 // defined in a dynamic object may need a COPY relocation.
2562 bool
2563 may_need_copy_reloc(Symbol* gsym)
2565 return (gsym->type() != elfcpp::STT_ARM_TFUNC
2566 && gsym->may_need_copy_reloc());
2569 // Add a potential copy relocation.
2570 void
2571 copy_reloc(Symbol_table* symtab, Layout* layout,
2572 Sized_relobj<32, big_endian>* object,
2573 unsigned int shndx, Output_section* output_section,
2574 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2576 this->copy_relocs_.copy_reloc(symtab, layout,
2577 symtab->get_sized_symbol<32>(sym),
2578 object, shndx, output_section, reloc,
2579 this->rel_dyn_section(layout));
2582 // Whether two EABI versions are compatible.
2583 static bool
2584 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2586 // Merge processor-specific flags from input object and those in the ELF
2587 // header of the output.
2588 void
2589 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2591 // Get the secondary compatible architecture.
2592 static int
2593 get_secondary_compatible_arch(const Attributes_section_data*);
2595 // Set the secondary compatible architecture.
2596 static void
2597 set_secondary_compatible_arch(Attributes_section_data*, int);
2599 static int
2600 tag_cpu_arch_combine(const char*, int, int*, int, int);
2602 // Helper to print AEABI enum tag value.
2603 static std::string
2604 aeabi_enum_name(unsigned int);
2606 // Return string value for TAG_CPU_name.
2607 static std::string
2608 tag_cpu_name_value(unsigned int);
2610 // Merge object attributes from input object and those in the output.
2611 void
2612 merge_object_attributes(const char*, const Attributes_section_data*);
2614 // Helper to get an AEABI object attribute
2615 Object_attribute*
2616 get_aeabi_object_attribute(int tag) const
2618 Attributes_section_data* pasd = this->attributes_section_data_;
2619 gold_assert(pasd != NULL);
2620 Object_attribute* attr =
2621 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2622 gold_assert(attr != NULL);
2623 return attr;
2627 // Methods to support stub-generations.
2630 // Group input sections for stub generation.
2631 void
2632 group_sections(Layout*, section_size_type, bool);
2634 // Scan a relocation for stub generation.
2635 void
2636 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2637 const Sized_symbol<32>*, unsigned int,
2638 const Symbol_value<32>*,
2639 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
2641 // Scan a relocation section for stub.
2642 template<int sh_type>
2643 void
2644 scan_reloc_section_for_stubs(
2645 const Relocate_info<32, big_endian>* relinfo,
2646 const unsigned char* prelocs,
2647 size_t reloc_count,
2648 Output_section* output_section,
2649 bool needs_special_offset_handling,
2650 const unsigned char* view,
2651 elfcpp::Elf_types<32>::Elf_Addr view_address,
2652 section_size_type);
2654 // Fix .ARM.exidx section coverage.
2655 void
2656 fix_exidx_coverage(Layout*, Arm_output_section<big_endian>*, Symbol_table*);
2658 // Functors for STL set.
2659 struct output_section_address_less_than
2661 bool
2662 operator()(const Output_section* s1, const Output_section* s2) const
2663 { return s1->address() < s2->address(); }
2666 // Information about this specific target which we pass to the
2667 // general Target structure.
2668 static const Target::Target_info arm_info;
2670 // The types of GOT entries needed for this platform.
2671 enum Got_type
2673 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2674 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
2675 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
2676 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
2677 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
2680 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2682 // Map input section to Arm_input_section.
2683 typedef Unordered_map<Section_id,
2684 Arm_input_section<big_endian>*,
2685 Section_id_hash>
2686 Arm_input_section_map;
2688 // Map output addresses to relocs for Cortex-A8 erratum.
2689 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2690 Cortex_a8_relocs_info;
2692 // The GOT section.
2693 Arm_output_data_got<big_endian>* got_;
2694 // The PLT section.
2695 Output_data_plt_arm<big_endian>* plt_;
2696 // The GOT PLT section.
2697 Output_data_space* got_plt_;
2698 // The dynamic reloc section.
2699 Reloc_section* rel_dyn_;
2700 // Relocs saved to avoid a COPY reloc.
2701 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2702 // Space for variables copied with a COPY reloc.
2703 Output_data_space* dynbss_;
2704 // Offset of the GOT entry for the TLS module index.
2705 unsigned int got_mod_index_offset_;
2706 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2707 bool tls_base_symbol_defined_;
2708 // Vector of Stub_tables created.
2709 Stub_table_list stub_tables_;
2710 // Stub factory.
2711 const Stub_factory &stub_factory_;
2712 // Whether we can use BLX.
2713 bool may_use_blx_;
2714 // Whether we force PIC branch veneers.
2715 bool should_force_pic_veneer_;
2716 // Map for locating Arm_input_sections.
2717 Arm_input_section_map arm_input_section_map_;
2718 // Attributes section data in output.
2719 Attributes_section_data* attributes_section_data_;
2720 // Whether we want to fix code for Cortex-A8 erratum.
2721 bool fix_cortex_a8_;
2722 // Map addresses to relocs for Cortex-A8 erratum.
2723 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2726 template<bool big_endian>
2727 const Target::Target_info Target_arm<big_endian>::arm_info =
2729 32, // size
2730 big_endian, // is_big_endian
2731 elfcpp::EM_ARM, // machine_code
2732 false, // has_make_symbol
2733 false, // has_resolve
2734 false, // has_code_fill
2735 true, // is_default_stack_executable
2736 '\0', // wrap_char
2737 "/usr/lib/libc.so.1", // dynamic_linker
2738 0x8000, // default_text_segment_address
2739 0x1000, // abi_pagesize (overridable by -z max-page-size)
2740 0x1000, // common_pagesize (overridable by -z common-page-size)
2741 elfcpp::SHN_UNDEF, // small_common_shndx
2742 elfcpp::SHN_UNDEF, // large_common_shndx
2743 0, // small_common_section_flags
2744 0, // large_common_section_flags
2745 ".ARM.attributes", // attributes_section
2746 "aeabi" // attributes_vendor
2749 // Arm relocate functions class
2752 template<bool big_endian>
2753 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2755 public:
2756 typedef enum
2758 STATUS_OKAY, // No error during relocation.
2759 STATUS_OVERFLOW, // Relocation oveflow.
2760 STATUS_BAD_RELOC // Relocation cannot be applied.
2761 } Status;
2763 private:
2764 typedef Relocate_functions<32, big_endian> Base;
2765 typedef Arm_relocate_functions<big_endian> This;
2767 // Encoding of imm16 argument for movt and movw ARM instructions
2768 // from ARM ARM:
2770 // imm16 := imm4 | imm12
2772 // 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
2773 // +-------+---------------+-------+-------+-----------------------+
2774 // | | |imm4 | |imm12 |
2775 // +-------+---------------+-------+-------+-----------------------+
2777 // Extract the relocation addend from VAL based on the ARM
2778 // instruction encoding described above.
2779 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2780 extract_arm_movw_movt_addend(
2781 typename elfcpp::Swap<32, big_endian>::Valtype val)
2783 // According to the Elf ABI for ARM Architecture the immediate
2784 // field is sign-extended to form the addend.
2785 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2788 // Insert X into VAL based on the ARM instruction encoding described
2789 // above.
2790 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2791 insert_val_arm_movw_movt(
2792 typename elfcpp::Swap<32, big_endian>::Valtype val,
2793 typename elfcpp::Swap<32, big_endian>::Valtype x)
2795 val &= 0xfff0f000;
2796 val |= x & 0x0fff;
2797 val |= (x & 0xf000) << 4;
2798 return val;
2801 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2802 // from ARM ARM:
2804 // imm16 := imm4 | i | imm3 | imm8
2806 // 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
2807 // +---------+-+-----------+-------++-+-----+-------+---------------+
2808 // | |i| |imm4 || |imm3 | |imm8 |
2809 // +---------+-+-----------+-------++-+-----+-------+---------------+
2811 // Extract the relocation addend from VAL based on the Thumb2
2812 // instruction encoding described above.
2813 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2814 extract_thumb_movw_movt_addend(
2815 typename elfcpp::Swap<32, big_endian>::Valtype val)
2817 // According to the Elf ABI for ARM Architecture the immediate
2818 // field is sign-extended to form the addend.
2819 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2820 | ((val >> 15) & 0x0800)
2821 | ((val >> 4) & 0x0700)
2822 | (val & 0x00ff));
2825 // Insert X into VAL based on the Thumb2 instruction encoding
2826 // described above.
2827 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2828 insert_val_thumb_movw_movt(
2829 typename elfcpp::Swap<32, big_endian>::Valtype val,
2830 typename elfcpp::Swap<32, big_endian>::Valtype x)
2832 val &= 0xfbf08f00;
2833 val |= (x & 0xf000) << 4;
2834 val |= (x & 0x0800) << 15;
2835 val |= (x & 0x0700) << 4;
2836 val |= (x & 0x00ff);
2837 return val;
2840 // Calculate the smallest constant Kn for the specified residual.
2841 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2842 static uint32_t
2843 calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
2845 int32_t msb;
2847 if (residual == 0)
2848 return 0;
2849 // Determine the most significant bit in the residual and
2850 // align the resulting value to a 2-bit boundary.
2851 for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
2853 // The desired shift is now (msb - 6), or zero, whichever
2854 // is the greater.
2855 return (((msb - 6) < 0) ? 0 : (msb - 6));
2858 // Calculate the final residual for the specified group index.
2859 // If the passed group index is less than zero, the method will return
2860 // the value of the specified residual without any change.
2861 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2862 static typename elfcpp::Swap<32, big_endian>::Valtype
2863 calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2864 const int group)
2866 for (int n = 0; n <= group; n++)
2868 // Calculate which part of the value to mask.
2869 uint32_t shift = calc_grp_kn(residual);
2870 // Calculate the residual for the next time around.
2871 residual &= ~(residual & (0xff << shift));
2874 return residual;
2877 // Calculate the value of Gn for the specified group index.
2878 // We return it in the form of an encoded constant-and-rotation.
2879 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2880 static typename elfcpp::Swap<32, big_endian>::Valtype
2881 calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2882 const int group)
2884 typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
2885 uint32_t shift = 0;
2887 for (int n = 0; n <= group; n++)
2889 // Calculate which part of the value to mask.
2890 shift = calc_grp_kn(residual);
2891 // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
2892 gn = residual & (0xff << shift);
2893 // Calculate the residual for the next time around.
2894 residual &= ~gn;
2896 // Return Gn in the form of an encoded constant-and-rotation.
2897 return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
2900 public:
2901 // Handle ARM long branches.
2902 static typename This::Status
2903 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2904 unsigned char *, const Sized_symbol<32>*,
2905 const Arm_relobj<big_endian>*, unsigned int,
2906 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2908 // Handle THUMB long branches.
2909 static typename This::Status
2910 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2911 unsigned char *, const Sized_symbol<32>*,
2912 const Arm_relobj<big_endian>*, unsigned int,
2913 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2916 // Return the branch offset of a 32-bit THUMB branch.
2917 static inline int32_t
2918 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2920 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2921 // involving the J1 and J2 bits.
2922 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2923 uint32_t upper = upper_insn & 0x3ffU;
2924 uint32_t lower = lower_insn & 0x7ffU;
2925 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2926 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2927 uint32_t i1 = j1 ^ s ? 0 : 1;
2928 uint32_t i2 = j2 ^ s ? 0 : 1;
2930 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2931 | (upper << 12) | (lower << 1));
2934 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2935 // UPPER_INSN is the original upper instruction of the branch. Caller is
2936 // responsible for overflow checking and BLX offset adjustment.
2937 static inline uint16_t
2938 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2940 uint32_t s = offset < 0 ? 1 : 0;
2941 uint32_t bits = static_cast<uint32_t>(offset);
2942 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2945 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2946 // LOWER_INSN is the original lower instruction of the branch. Caller is
2947 // responsible for overflow checking and BLX offset adjustment.
2948 static inline uint16_t
2949 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2951 uint32_t s = offset < 0 ? 1 : 0;
2952 uint32_t bits = static_cast<uint32_t>(offset);
2953 return ((lower_insn & ~0x2fffU)
2954 | ((((bits >> 23) & 1) ^ !s) << 13)
2955 | ((((bits >> 22) & 1) ^ !s) << 11)
2956 | ((bits >> 1) & 0x7ffU));
2959 // Return the branch offset of a 32-bit THUMB conditional branch.
2960 static inline int32_t
2961 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2963 uint32_t s = (upper_insn & 0x0400U) >> 10;
2964 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2965 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2966 uint32_t lower = (lower_insn & 0x07ffU);
2967 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2969 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2972 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2973 // instruction. UPPER_INSN is the original upper instruction of the branch.
2974 // Caller is responsible for overflow checking.
2975 static inline uint16_t
2976 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2978 uint32_t s = offset < 0 ? 1 : 0;
2979 uint32_t bits = static_cast<uint32_t>(offset);
2980 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2983 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2984 // instruction. LOWER_INSN is the original lower instruction of the branch.
2985 // Caller is reponsible for overflow checking.
2986 static inline uint16_t
2987 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2989 uint32_t bits = static_cast<uint32_t>(offset);
2990 uint32_t j2 = (bits & 0x00080000U) >> 19;
2991 uint32_t j1 = (bits & 0x00040000U) >> 18;
2992 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2994 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2997 // R_ARM_ABS8: S + A
2998 static inline typename This::Status
2999 abs8(unsigned char *view,
3000 const Sized_relobj<32, big_endian>* object,
3001 const Symbol_value<32>* psymval)
3003 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
3004 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3005 Valtype* wv = reinterpret_cast<Valtype*>(view);
3006 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
3007 Reltype addend = utils::sign_extend<8>(val);
3008 Reltype x = psymval->value(object, addend);
3009 val = utils::bit_select(val, x, 0xffU);
3010 elfcpp::Swap<8, big_endian>::writeval(wv, val);
3012 // R_ARM_ABS8 permits signed or unsigned results.
3013 int signed_x = static_cast<int32_t>(x);
3014 return ((signed_x < -128 || signed_x > 255)
3015 ? This::STATUS_OVERFLOW
3016 : This::STATUS_OKAY);
3019 // R_ARM_THM_ABS5: S + A
3020 static inline typename This::Status
3021 thm_abs5(unsigned char *view,
3022 const Sized_relobj<32, big_endian>* object,
3023 const Symbol_value<32>* psymval)
3025 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3026 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3027 Valtype* wv = reinterpret_cast<Valtype*>(view);
3028 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3029 Reltype addend = (val & 0x7e0U) >> 6;
3030 Reltype x = psymval->value(object, addend);
3031 val = utils::bit_select(val, x << 6, 0x7e0U);
3032 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3034 // R_ARM_ABS16 permits signed or unsigned results.
3035 int signed_x = static_cast<int32_t>(x);
3036 return ((signed_x < -32768 || signed_x > 65535)
3037 ? This::STATUS_OVERFLOW
3038 : This::STATUS_OKAY);
3041 // R_ARM_ABS12: S + A
3042 static inline typename This::Status
3043 abs12(unsigned char *view,
3044 const Sized_relobj<32, big_endian>* object,
3045 const Symbol_value<32>* psymval)
3047 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3048 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3049 Valtype* wv = reinterpret_cast<Valtype*>(view);
3050 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3051 Reltype addend = val & 0x0fffU;
3052 Reltype x = psymval->value(object, addend);
3053 val = utils::bit_select(val, x, 0x0fffU);
3054 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3055 return (utils::has_overflow<12>(x)
3056 ? This::STATUS_OVERFLOW
3057 : This::STATUS_OKAY);
3060 // R_ARM_ABS16: S + A
3061 static inline typename This::Status
3062 abs16(unsigned char *view,
3063 const Sized_relobj<32, big_endian>* object,
3064 const Symbol_value<32>* psymval)
3066 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3067 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3068 Valtype* wv = reinterpret_cast<Valtype*>(view);
3069 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3070 Reltype addend = utils::sign_extend<16>(val);
3071 Reltype x = psymval->value(object, addend);
3072 val = utils::bit_select(val, x, 0xffffU);
3073 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3074 return (utils::has_signed_unsigned_overflow<16>(x)
3075 ? This::STATUS_OVERFLOW
3076 : This::STATUS_OKAY);
3079 // R_ARM_ABS32: (S + A) | T
3080 static inline typename This::Status
3081 abs32(unsigned char *view,
3082 const Sized_relobj<32, big_endian>* object,
3083 const Symbol_value<32>* psymval,
3084 Arm_address thumb_bit)
3086 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3087 Valtype* wv = reinterpret_cast<Valtype*>(view);
3088 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3089 Valtype x = psymval->value(object, addend) | thumb_bit;
3090 elfcpp::Swap<32, big_endian>::writeval(wv, x);
3091 return This::STATUS_OKAY;
3094 // R_ARM_REL32: (S + A) | T - P
3095 static inline typename This::Status
3096 rel32(unsigned char *view,
3097 const Sized_relobj<32, big_endian>* object,
3098 const Symbol_value<32>* psymval,
3099 Arm_address address,
3100 Arm_address thumb_bit)
3102 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3103 Valtype* wv = reinterpret_cast<Valtype*>(view);
3104 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3105 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3106 elfcpp::Swap<32, big_endian>::writeval(wv, x);
3107 return This::STATUS_OKAY;
3110 // R_ARM_THM_JUMP24: (S + A) | T - P
3111 static typename This::Status
3112 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
3113 const Symbol_value<32>* psymval, Arm_address address,
3114 Arm_address thumb_bit);
3116 // R_ARM_THM_JUMP6: S + A – P
3117 static inline typename This::Status
3118 thm_jump6(unsigned char *view,
3119 const Sized_relobj<32, big_endian>* object,
3120 const Symbol_value<32>* psymval,
3121 Arm_address address)
3123 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3124 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3125 Valtype* wv = reinterpret_cast<Valtype*>(view);
3126 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3127 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
3128 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
3129 Reltype x = (psymval->value(object, addend) - address);
3130 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
3131 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3132 // CZB does only forward jumps.
3133 return ((x > 0x007e)
3134 ? This::STATUS_OVERFLOW
3135 : This::STATUS_OKAY);
3138 // R_ARM_THM_JUMP8: S + A – P
3139 static inline typename This::Status
3140 thm_jump8(unsigned char *view,
3141 const Sized_relobj<32, big_endian>* object,
3142 const Symbol_value<32>* psymval,
3143 Arm_address address)
3145 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3146 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3147 Valtype* wv = reinterpret_cast<Valtype*>(view);
3148 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3149 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
3150 Reltype x = (psymval->value(object, addend) - address);
3151 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
3152 return (utils::has_overflow<8>(x)
3153 ? This::STATUS_OVERFLOW
3154 : This::STATUS_OKAY);
3157 // R_ARM_THM_JUMP11: S + A – P
3158 static inline typename This::Status
3159 thm_jump11(unsigned char *view,
3160 const Sized_relobj<32, big_endian>* object,
3161 const Symbol_value<32>* psymval,
3162 Arm_address address)
3164 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3165 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3166 Valtype* wv = reinterpret_cast<Valtype*>(view);
3167 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3168 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
3169 Reltype x = (psymval->value(object, addend) - address);
3170 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
3171 return (utils::has_overflow<11>(x)
3172 ? This::STATUS_OVERFLOW
3173 : This::STATUS_OKAY);
3176 // R_ARM_BASE_PREL: B(S) + A - P
3177 static inline typename This::Status
3178 base_prel(unsigned char* view,
3179 Arm_address origin,
3180 Arm_address address)
3182 Base::rel32(view, origin - address);
3183 return STATUS_OKAY;
3186 // R_ARM_BASE_ABS: B(S) + A
3187 static inline typename This::Status
3188 base_abs(unsigned char* view,
3189 Arm_address origin)
3191 Base::rel32(view, origin);
3192 return STATUS_OKAY;
3195 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3196 static inline typename This::Status
3197 got_brel(unsigned char* view,
3198 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
3200 Base::rel32(view, got_offset);
3201 return This::STATUS_OKAY;
3204 // R_ARM_GOT_PREL: GOT(S) + A - P
3205 static inline typename This::Status
3206 got_prel(unsigned char *view,
3207 Arm_address got_entry,
3208 Arm_address address)
3210 Base::rel32(view, got_entry - address);
3211 return This::STATUS_OKAY;
3214 // R_ARM_PREL: (S + A) | T - P
3215 static inline typename This::Status
3216 prel31(unsigned char *view,
3217 const Sized_relobj<32, big_endian>* object,
3218 const Symbol_value<32>* psymval,
3219 Arm_address address,
3220 Arm_address thumb_bit)
3222 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3223 Valtype* wv = reinterpret_cast<Valtype*>(view);
3224 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3225 Valtype addend = utils::sign_extend<31>(val);
3226 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3227 val = utils::bit_select(val, x, 0x7fffffffU);
3228 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3229 return (utils::has_overflow<31>(x) ?
3230 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3233 // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
3234 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3235 // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3236 // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
3237 static inline typename This::Status
3238 movw(unsigned char* view,
3239 const Sized_relobj<32, big_endian>* object,
3240 const Symbol_value<32>* psymval,
3241 Arm_address relative_address_base,
3242 Arm_address thumb_bit,
3243 bool check_overflow)
3245 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3246 Valtype* wv = reinterpret_cast<Valtype*>(view);
3247 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3248 Valtype addend = This::extract_arm_movw_movt_addend(val);
3249 Valtype x = ((psymval->value(object, addend) | thumb_bit)
3250 - relative_address_base);
3251 val = This::insert_val_arm_movw_movt(val, x);
3252 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3253 return ((check_overflow && utils::has_overflow<16>(x))
3254 ? This::STATUS_OVERFLOW
3255 : This::STATUS_OKAY);
3258 // R_ARM_MOVT_ABS: S + A (relative address base is 0)
3259 // R_ARM_MOVT_PREL: S + A - P
3260 // R_ARM_MOVT_BREL: S + A - B(S)
3261 static inline typename This::Status
3262 movt(unsigned char* view,
3263 const Sized_relobj<32, big_endian>* object,
3264 const Symbol_value<32>* psymval,
3265 Arm_address relative_address_base)
3267 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3268 Valtype* wv = reinterpret_cast<Valtype*>(view);
3269 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3270 Valtype addend = This::extract_arm_movw_movt_addend(val);
3271 Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3272 val = This::insert_val_arm_movw_movt(val, x);
3273 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3274 // FIXME: IHI0044D says that we should check for overflow.
3275 return This::STATUS_OKAY;
3278 // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
3279 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3280 // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3281 // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
3282 static inline typename This::Status
3283 thm_movw(unsigned char *view,
3284 const Sized_relobj<32, big_endian>* object,
3285 const Symbol_value<32>* psymval,
3286 Arm_address relative_address_base,
3287 Arm_address thumb_bit,
3288 bool check_overflow)
3290 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3291 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3292 Valtype* wv = reinterpret_cast<Valtype*>(view);
3293 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3294 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3295 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3296 Reltype x =
3297 (psymval->value(object, addend) | thumb_bit) - relative_address_base;
3298 val = This::insert_val_thumb_movw_movt(val, x);
3299 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3300 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3301 return ((check_overflow && utils::has_overflow<16>(x))
3302 ? This::STATUS_OVERFLOW
3303 : This::STATUS_OKAY);
3306 // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
3307 // R_ARM_THM_MOVT_PREL: S + A - P
3308 // R_ARM_THM_MOVT_BREL: S + A - B(S)
3309 static inline typename This::Status
3310 thm_movt(unsigned char* view,
3311 const Sized_relobj<32, big_endian>* object,
3312 const Symbol_value<32>* psymval,
3313 Arm_address relative_address_base)
3315 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3316 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3317 Valtype* wv = reinterpret_cast<Valtype*>(view);
3318 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3319 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3320 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3321 Reltype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3322 val = This::insert_val_thumb_movw_movt(val, x);
3323 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3324 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3325 return This::STATUS_OKAY;
3328 // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3329 static inline typename This::Status
3330 thm_alu11(unsigned char* view,
3331 const Sized_relobj<32, big_endian>* object,
3332 const Symbol_value<32>* psymval,
3333 Arm_address address,
3334 Arm_address thumb_bit)
3336 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3337 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3338 Valtype* wv = reinterpret_cast<Valtype*>(view);
3339 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3340 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3342 // 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
3343 // -----------------------------------------------------------------------
3344 // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
3345 // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
3346 // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
3347 // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
3348 // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
3349 // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
3351 // Determine a sign for the addend.
3352 const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
3353 || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3354 // Thumb2 addend encoding:
3355 // imm12 := i | imm3 | imm8
3356 int32_t addend = (insn & 0xff)
3357 | ((insn & 0x00007000) >> 4)
3358 | ((insn & 0x04000000) >> 15);
3359 // Apply a sign to the added.
3360 addend *= sign;
3362 int32_t x = (psymval->value(object, addend) | thumb_bit)
3363 - (address & 0xfffffffc);
3364 Reltype val = abs(x);
3365 // Mask out the value and a distinct part of the ADD/SUB opcode
3366 // (bits 7:5 of opword).
3367 insn = (insn & 0xfb0f8f00)
3368 | (val & 0xff)
3369 | ((val & 0x700) << 4)
3370 | ((val & 0x800) << 15);
3371 // Set the opcode according to whether the value to go in the
3372 // place is negative.
3373 if (x < 0)
3374 insn |= 0x00a00000;
3376 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3377 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3378 return ((val > 0xfff) ?
3379 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3382 // R_ARM_THM_PC8: S + A - Pa (Thumb)
3383 static inline typename This::Status
3384 thm_pc8(unsigned char* view,
3385 const Sized_relobj<32, big_endian>* object,
3386 const Symbol_value<32>* psymval,
3387 Arm_address address)
3389 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3390 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3391 Valtype* wv = reinterpret_cast<Valtype*>(view);
3392 Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
3393 Reltype addend = ((insn & 0x00ff) << 2);
3394 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3395 Reltype val = abs(x);
3396 insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
3398 elfcpp::Swap<16, big_endian>::writeval(wv, insn);
3399 return ((val > 0x03fc)
3400 ? This::STATUS_OVERFLOW
3401 : This::STATUS_OKAY);
3404 // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3405 static inline typename This::Status
3406 thm_pc12(unsigned char* view,
3407 const Sized_relobj<32, big_endian>* object,
3408 const Symbol_value<32>* psymval,
3409 Arm_address address)
3411 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3412 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3413 Valtype* wv = reinterpret_cast<Valtype*>(view);
3414 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3415 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3416 // Determine a sign for the addend (positive if the U bit is 1).
3417 const int sign = (insn & 0x00800000) ? 1 : -1;
3418 int32_t addend = (insn & 0xfff);
3419 // Apply a sign to the added.
3420 addend *= sign;
3422 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3423 Reltype val = abs(x);
3424 // Mask out and apply the value and the U bit.
3425 insn = (insn & 0xff7ff000) | (val & 0xfff);
3426 // Set the U bit according to whether the value to go in the
3427 // place is positive.
3428 if (x >= 0)
3429 insn |= 0x00800000;
3431 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3432 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3433 return ((val > 0xfff) ?
3434 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3437 // R_ARM_V4BX
3438 static inline typename This::Status
3439 v4bx(const Relocate_info<32, big_endian>* relinfo,
3440 unsigned char *view,
3441 const Arm_relobj<big_endian>* object,
3442 const Arm_address address,
3443 const bool is_interworking)
3446 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3447 Valtype* wv = reinterpret_cast<Valtype*>(view);
3448 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3450 // Ensure that we have a BX instruction.
3451 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3452 const uint32_t reg = (val & 0xf);
3453 if (is_interworking && reg != 0xf)
3455 Stub_table<big_endian>* stub_table =
3456 object->stub_table(relinfo->data_shndx);
3457 gold_assert(stub_table != NULL);
3459 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3460 gold_assert(stub != NULL);
3462 int32_t veneer_address =
3463 stub_table->address() + stub->offset() - 8 - address;
3464 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3465 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3466 // Replace with a branch to veneer (B <addr>)
3467 val = (val & 0xf0000000) | 0x0a000000
3468 | ((veneer_address >> 2) & 0x00ffffff);
3470 else
3472 // Preserve Rm (lowest four bits) and the condition code
3473 // (highest four bits). Other bits encode MOV PC,Rm.
3474 val = (val & 0xf000000f) | 0x01a0f000;
3476 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3477 return This::STATUS_OKAY;
3480 // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3481 // R_ARM_ALU_PC_G0: ((S + A) | T) - P
3482 // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3483 // R_ARM_ALU_PC_G1: ((S + A) | T) - P
3484 // R_ARM_ALU_PC_G2: ((S + A) | T) - P
3485 // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3486 // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
3487 // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3488 // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
3489 // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
3490 static inline typename This::Status
3491 arm_grp_alu(unsigned char* view,
3492 const Sized_relobj<32, big_endian>* object,
3493 const Symbol_value<32>* psymval,
3494 const int group,
3495 Arm_address address,
3496 Arm_address thumb_bit,
3497 bool check_overflow)
3499 gold_assert(group >= 0 && group < 3);
3500 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3501 Valtype* wv = reinterpret_cast<Valtype*>(view);
3502 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3504 // ALU group relocations are allowed only for the ADD/SUB instructions.
3505 // (0x00800000 - ADD, 0x00400000 - SUB)
3506 const Valtype opcode = insn & 0x01e00000;
3507 if (opcode != 0x00800000 && opcode != 0x00400000)
3508 return This::STATUS_BAD_RELOC;
3510 // Determine a sign for the addend.
3511 const int sign = (opcode == 0x00800000) ? 1 : -1;
3512 // shifter = rotate_imm * 2
3513 const uint32_t shifter = (insn & 0xf00) >> 7;
3514 // Initial addend value.
3515 int32_t addend = insn & 0xff;
3516 // Rotate addend right by shifter.
3517 addend = (addend >> shifter) | (addend << (32 - shifter));
3518 // Apply a sign to the added.
3519 addend *= sign;
3521 int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
3522 Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
3523 // Check for overflow if required
3524 if (check_overflow
3525 && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
3526 return This::STATUS_OVERFLOW;
3528 // Mask out the value and the ADD/SUB part of the opcode; take care
3529 // not to destroy the S bit.
3530 insn &= 0xff1ff000;
3531 // Set the opcode according to whether the value to go in the
3532 // place is negative.
3533 insn |= ((x < 0) ? 0x00400000 : 0x00800000);
3534 // Encode the offset (encoded Gn).
3535 insn |= gn;
3537 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3538 return This::STATUS_OKAY;
3541 // R_ARM_LDR_PC_G0: S + A - P
3542 // R_ARM_LDR_PC_G1: S + A - P
3543 // R_ARM_LDR_PC_G2: S + A - P
3544 // R_ARM_LDR_SB_G0: S + A - B(S)
3545 // R_ARM_LDR_SB_G1: S + A - B(S)
3546 // R_ARM_LDR_SB_G2: S + A - B(S)
3547 static inline typename This::Status
3548 arm_grp_ldr(unsigned char* view,
3549 const Sized_relobj<32, big_endian>* object,
3550 const Symbol_value<32>* psymval,
3551 const int group,
3552 Arm_address address)
3554 gold_assert(group >= 0 && group < 3);
3555 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3556 Valtype* wv = reinterpret_cast<Valtype*>(view);
3557 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3559 const int sign = (insn & 0x00800000) ? 1 : -1;
3560 int32_t addend = (insn & 0xfff) * sign;
3561 int32_t x = (psymval->value(object, addend) - address);
3562 // Calculate the relevant G(n-1) value to obtain this stage residual.
3563 Valtype residual =
3564 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3565 if (residual >= 0x1000)
3566 return This::STATUS_OVERFLOW;
3568 // Mask out the value and U bit.
3569 insn &= 0xff7ff000;
3570 // Set the U bit for non-negative values.
3571 if (x >= 0)
3572 insn |= 0x00800000;
3573 insn |= residual;
3575 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3576 return This::STATUS_OKAY;
3579 // R_ARM_LDRS_PC_G0: S + A - P
3580 // R_ARM_LDRS_PC_G1: S + A - P
3581 // R_ARM_LDRS_PC_G2: S + A - P
3582 // R_ARM_LDRS_SB_G0: S + A - B(S)
3583 // R_ARM_LDRS_SB_G1: S + A - B(S)
3584 // R_ARM_LDRS_SB_G2: S + A - B(S)
3585 static inline typename This::Status
3586 arm_grp_ldrs(unsigned char* view,
3587 const Sized_relobj<32, big_endian>* object,
3588 const Symbol_value<32>* psymval,
3589 const int group,
3590 Arm_address address)
3592 gold_assert(group >= 0 && group < 3);
3593 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3594 Valtype* wv = reinterpret_cast<Valtype*>(view);
3595 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3597 const int sign = (insn & 0x00800000) ? 1 : -1;
3598 int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
3599 int32_t x = (psymval->value(object, addend) - address);
3600 // Calculate the relevant G(n-1) value to obtain this stage residual.
3601 Valtype residual =
3602 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3603 if (residual >= 0x100)
3604 return This::STATUS_OVERFLOW;
3606 // Mask out the value and U bit.
3607 insn &= 0xff7ff0f0;
3608 // Set the U bit for non-negative values.
3609 if (x >= 0)
3610 insn |= 0x00800000;
3611 insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
3613 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3614 return This::STATUS_OKAY;
3617 // R_ARM_LDC_PC_G0: S + A - P
3618 // R_ARM_LDC_PC_G1: S + A - P
3619 // R_ARM_LDC_PC_G2: S + A - P
3620 // R_ARM_LDC_SB_G0: S + A - B(S)
3621 // R_ARM_LDC_SB_G1: S + A - B(S)
3622 // R_ARM_LDC_SB_G2: S + A - B(S)
3623 static inline typename This::Status
3624 arm_grp_ldc(unsigned char* view,
3625 const Sized_relobj<32, big_endian>* object,
3626 const Symbol_value<32>* psymval,
3627 const int group,
3628 Arm_address address)
3630 gold_assert(group >= 0 && group < 3);
3631 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3632 Valtype* wv = reinterpret_cast<Valtype*>(view);
3633 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3635 const int sign = (insn & 0x00800000) ? 1 : -1;
3636 int32_t addend = ((insn & 0xff) << 2) * sign;
3637 int32_t x = (psymval->value(object, addend) - address);
3638 // Calculate the relevant G(n-1) value to obtain this stage residual.
3639 Valtype residual =
3640 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3641 if ((residual & 0x3) != 0 || residual >= 0x400)
3642 return This::STATUS_OVERFLOW;
3644 // Mask out the value and U bit.
3645 insn &= 0xff7fff00;
3646 // Set the U bit for non-negative values.
3647 if (x >= 0)
3648 insn |= 0x00800000;
3649 insn |= (residual >> 2);
3651 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3652 return This::STATUS_OKAY;
3656 // Relocate ARM long branches. This handles relocation types
3657 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3658 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3659 // undefined and we do not use PLT in this relocation. In such a case,
3660 // the branch is converted into an NOP.
3662 template<bool big_endian>
3663 typename Arm_relocate_functions<big_endian>::Status
3664 Arm_relocate_functions<big_endian>::arm_branch_common(
3665 unsigned int r_type,
3666 const Relocate_info<32, big_endian>* relinfo,
3667 unsigned char *view,
3668 const Sized_symbol<32>* gsym,
3669 const Arm_relobj<big_endian>* object,
3670 unsigned int r_sym,
3671 const Symbol_value<32>* psymval,
3672 Arm_address address,
3673 Arm_address thumb_bit,
3674 bool is_weakly_undefined_without_plt)
3676 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3677 Valtype* wv = reinterpret_cast<Valtype*>(view);
3678 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3680 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3681 && ((val & 0x0f000000UL) == 0x0a000000UL);
3682 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3683 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3684 && ((val & 0x0f000000UL) == 0x0b000000UL);
3685 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3686 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3688 // Check that the instruction is valid.
3689 if (r_type == elfcpp::R_ARM_CALL)
3691 if (!insn_is_uncond_bl && !insn_is_blx)
3692 return This::STATUS_BAD_RELOC;
3694 else if (r_type == elfcpp::R_ARM_JUMP24)
3696 if (!insn_is_b && !insn_is_cond_bl)
3697 return This::STATUS_BAD_RELOC;
3699 else if (r_type == elfcpp::R_ARM_PLT32)
3701 if (!insn_is_any_branch)
3702 return This::STATUS_BAD_RELOC;
3704 else if (r_type == elfcpp::R_ARM_XPC25)
3706 // FIXME: AAELF document IH0044C does not say much about it other
3707 // than it being obsolete.
3708 if (!insn_is_any_branch)
3709 return This::STATUS_BAD_RELOC;
3711 else
3712 gold_unreachable();
3714 // A branch to an undefined weak symbol is turned into a jump to
3715 // the next instruction unless a PLT entry will be created.
3716 // Do the same for local undefined symbols.
3717 // The jump to the next instruction is optimized as a NOP depending
3718 // on the architecture.
3719 const Target_arm<big_endian>* arm_target =
3720 Target_arm<big_endian>::default_target();
3721 if (is_weakly_undefined_without_plt)
3723 Valtype cond = val & 0xf0000000U;
3724 if (arm_target->may_use_arm_nop())
3725 val = cond | 0x0320f000;
3726 else
3727 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3728 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3729 return This::STATUS_OKAY;
3732 Valtype addend = utils::sign_extend<26>(val << 2);
3733 Valtype branch_target = psymval->value(object, addend);
3734 int32_t branch_offset = branch_target - address;
3736 // We need a stub if the branch offset is too large or if we need
3737 // to switch mode.
3738 bool may_use_blx = arm_target->may_use_blx();
3739 Reloc_stub* stub = NULL;
3740 if (utils::has_overflow<26>(branch_offset)
3741 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
3743 Valtype unadjusted_branch_target = psymval->value(object, 0);
3745 Stub_type stub_type =
3746 Reloc_stub::stub_type_for_reloc(r_type, address,
3747 unadjusted_branch_target,
3748 (thumb_bit != 0));
3749 if (stub_type != arm_stub_none)
3751 Stub_table<big_endian>* stub_table =
3752 object->stub_table(relinfo->data_shndx);
3753 gold_assert(stub_table != NULL);
3755 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3756 stub = stub_table->find_reloc_stub(stub_key);
3757 gold_assert(stub != NULL);
3758 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3759 branch_target = stub_table->address() + stub->offset() + addend;
3760 branch_offset = branch_target - address;
3761 gold_assert(!utils::has_overflow<26>(branch_offset));
3765 // At this point, if we still need to switch mode, the instruction
3766 // must either be a BLX or a BL that can be converted to a BLX.
3767 if (thumb_bit != 0)
3769 // Turn BL to BLX.
3770 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3771 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3774 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3775 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3776 return (utils::has_overflow<26>(branch_offset)
3777 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3780 // Relocate THUMB long branches. This handles relocation types
3781 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3782 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3783 // undefined and we do not use PLT in this relocation. In such a case,
3784 // the branch is converted into an NOP.
3786 template<bool big_endian>
3787 typename Arm_relocate_functions<big_endian>::Status
3788 Arm_relocate_functions<big_endian>::thumb_branch_common(
3789 unsigned int r_type,
3790 const Relocate_info<32, big_endian>* relinfo,
3791 unsigned char *view,
3792 const Sized_symbol<32>* gsym,
3793 const Arm_relobj<big_endian>* object,
3794 unsigned int r_sym,
3795 const Symbol_value<32>* psymval,
3796 Arm_address address,
3797 Arm_address thumb_bit,
3798 bool is_weakly_undefined_without_plt)
3800 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3801 Valtype* wv = reinterpret_cast<Valtype*>(view);
3802 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3803 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3805 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3806 // into account.
3807 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
3808 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
3810 // Check that the instruction is valid.
3811 if (r_type == elfcpp::R_ARM_THM_CALL)
3813 if (!is_bl_insn && !is_blx_insn)
3814 return This::STATUS_BAD_RELOC;
3816 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
3818 // This cannot be a BLX.
3819 if (!is_bl_insn)
3820 return This::STATUS_BAD_RELOC;
3822 else if (r_type == elfcpp::R_ARM_THM_XPC22)
3824 // Check for Thumb to Thumb call.
3825 if (!is_blx_insn)
3826 return This::STATUS_BAD_RELOC;
3827 if (thumb_bit != 0)
3829 gold_warning(_("%s: Thumb BLX instruction targets "
3830 "thumb function '%s'."),
3831 object->name().c_str(),
3832 (gsym ? gsym->name() : "(local)"));
3833 // Convert BLX to BL.
3834 lower_insn |= 0x1000U;
3837 else
3838 gold_unreachable();
3840 // A branch to an undefined weak symbol is turned into a jump to
3841 // the next instruction unless a PLT entry will be created.
3842 // The jump to the next instruction is optimized as a NOP.W for
3843 // Thumb-2 enabled architectures.
3844 const Target_arm<big_endian>* arm_target =
3845 Target_arm<big_endian>::default_target();
3846 if (is_weakly_undefined_without_plt)
3848 if (arm_target->may_use_thumb2_nop())
3850 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
3851 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
3853 else
3855 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
3856 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
3858 return This::STATUS_OKAY;
3861 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
3862 Arm_address branch_target = psymval->value(object, addend);
3864 // For BLX, bit 1 of target address comes from bit 1 of base address.
3865 bool may_use_blx = arm_target->may_use_blx();
3866 if (thumb_bit == 0 && may_use_blx)
3867 branch_target = utils::bit_select(branch_target, address, 0x2);
3869 int32_t branch_offset = branch_target - address;
3871 // We need a stub if the branch offset is too large or if we need
3872 // to switch mode.
3873 bool thumb2 = arm_target->using_thumb2();
3874 if ((!thumb2 && utils::has_overflow<23>(branch_offset))
3875 || (thumb2 && utils::has_overflow<25>(branch_offset))
3876 || ((thumb_bit == 0)
3877 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3878 || r_type == elfcpp::R_ARM_THM_JUMP24)))
3880 Arm_address unadjusted_branch_target = psymval->value(object, 0);
3882 Stub_type stub_type =
3883 Reloc_stub::stub_type_for_reloc(r_type, address,
3884 unadjusted_branch_target,
3885 (thumb_bit != 0));
3887 if (stub_type != arm_stub_none)
3889 Stub_table<big_endian>* stub_table =
3890 object->stub_table(relinfo->data_shndx);
3891 gold_assert(stub_table != NULL);
3893 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3894 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
3895 gold_assert(stub != NULL);
3896 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3897 branch_target = stub_table->address() + stub->offset() + addend;
3898 if (thumb_bit == 0 && may_use_blx)
3899 branch_target = utils::bit_select(branch_target, address, 0x2);
3900 branch_offset = branch_target - address;
3904 // At this point, if we still need to switch mode, the instruction
3905 // must either be a BLX or a BL that can be converted to a BLX.
3906 if (thumb_bit == 0)
3908 gold_assert(may_use_blx
3909 && (r_type == elfcpp::R_ARM_THM_CALL
3910 || r_type == elfcpp::R_ARM_THM_XPC22));
3911 // Make sure this is a BLX.
3912 lower_insn &= ~0x1000U;
3914 else
3916 // Make sure this is a BL.
3917 lower_insn |= 0x1000U;
3920 // For a BLX instruction, make sure that the relocation is rounded up
3921 // to a word boundary. This follows the semantics of the instruction
3922 // which specifies that bit 1 of the target address will come from bit
3923 // 1 of the base address.
3924 if ((lower_insn & 0x5000U) == 0x4000U)
3925 gold_assert((branch_offset & 3) == 0);
3927 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3928 // We use the Thumb-2 encoding, which is safe even if dealing with
3929 // a Thumb-1 instruction by virtue of our overflow check above. */
3930 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
3931 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
3933 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3934 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3936 gold_assert(!utils::has_overflow<25>(branch_offset));
3938 return ((thumb2
3939 ? utils::has_overflow<25>(branch_offset)
3940 : utils::has_overflow<23>(branch_offset))
3941 ? This::STATUS_OVERFLOW
3942 : This::STATUS_OKAY);
3945 // Relocate THUMB-2 long conditional branches.
3946 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3947 // undefined and we do not use PLT in this relocation. In such a case,
3948 // the branch is converted into an NOP.
3950 template<bool big_endian>
3951 typename Arm_relocate_functions<big_endian>::Status
3952 Arm_relocate_functions<big_endian>::thm_jump19(
3953 unsigned char *view,
3954 const Arm_relobj<big_endian>* object,
3955 const Symbol_value<32>* psymval,
3956 Arm_address address,
3957 Arm_address thumb_bit)
3959 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3960 Valtype* wv = reinterpret_cast<Valtype*>(view);
3961 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3962 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3963 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
3965 Arm_address branch_target = psymval->value(object, addend);
3966 int32_t branch_offset = branch_target - address;
3968 // ??? Should handle interworking? GCC might someday try to
3969 // use this for tail calls.
3970 // FIXME: We do support thumb entry to PLT yet.
3971 if (thumb_bit == 0)
3973 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3974 return This::STATUS_BAD_RELOC;
3977 // Put RELOCATION back into the insn.
3978 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
3979 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
3981 // Put the relocated value back in the object file:
3982 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3983 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3985 return (utils::has_overflow<21>(branch_offset)
3986 ? This::STATUS_OVERFLOW
3987 : This::STATUS_OKAY);
3990 // Get the GOT section, creating it if necessary.
3992 template<bool big_endian>
3993 Arm_output_data_got<big_endian>*
3994 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
3996 if (this->got_ == NULL)
3998 gold_assert(symtab != NULL && layout != NULL);
4000 this->got_ = new Arm_output_data_got<big_endian>(symtab, layout);
4002 Output_section* os;
4003 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4004 (elfcpp::SHF_ALLOC
4005 | elfcpp::SHF_WRITE),
4006 this->got_, false, false, false,
4007 true);
4008 // The old GNU linker creates a .got.plt section. We just
4009 // create another set of data in the .got section. Note that we
4010 // always create a PLT if we create a GOT, although the PLT
4011 // might be empty.
4012 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
4013 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4014 (elfcpp::SHF_ALLOC
4015 | elfcpp::SHF_WRITE),
4016 this->got_plt_, false, false,
4017 false, false);
4019 // The first three entries are reserved.
4020 this->got_plt_->set_current_data_size(3 * 4);
4022 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
4023 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
4024 Symbol_table::PREDEFINED,
4025 this->got_plt_,
4026 0, 0, elfcpp::STT_OBJECT,
4027 elfcpp::STB_LOCAL,
4028 elfcpp::STV_HIDDEN, 0,
4029 false, false);
4031 return this->got_;
4034 // Get the dynamic reloc section, creating it if necessary.
4036 template<bool big_endian>
4037 typename Target_arm<big_endian>::Reloc_section*
4038 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
4040 if (this->rel_dyn_ == NULL)
4042 gold_assert(layout != NULL);
4043 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
4044 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
4045 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
4046 false, false, false);
4048 return this->rel_dyn_;
4051 // Insn_template methods.
4053 // Return byte size of an instruction template.
4055 size_t
4056 Insn_template::size() const
4058 switch (this->type())
4060 case THUMB16_TYPE:
4061 case THUMB16_SPECIAL_TYPE:
4062 return 2;
4063 case ARM_TYPE:
4064 case THUMB32_TYPE:
4065 case DATA_TYPE:
4066 return 4;
4067 default:
4068 gold_unreachable();
4072 // Return alignment of an instruction template.
4074 unsigned
4075 Insn_template::alignment() const
4077 switch (this->type())
4079 case THUMB16_TYPE:
4080 case THUMB16_SPECIAL_TYPE:
4081 case THUMB32_TYPE:
4082 return 2;
4083 case ARM_TYPE:
4084 case DATA_TYPE:
4085 return 4;
4086 default:
4087 gold_unreachable();
4091 // Stub_template methods.
4093 Stub_template::Stub_template(
4094 Stub_type type, const Insn_template* insns,
4095 size_t insn_count)
4096 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
4097 entry_in_thumb_mode_(false), relocs_()
4099 off_t offset = 0;
4101 // Compute byte size and alignment of stub template.
4102 for (size_t i = 0; i < insn_count; i++)
4104 unsigned insn_alignment = insns[i].alignment();
4105 size_t insn_size = insns[i].size();
4106 gold_assert((offset & (insn_alignment - 1)) == 0);
4107 this->alignment_ = std::max(this->alignment_, insn_alignment);
4108 switch (insns[i].type())
4110 case Insn_template::THUMB16_TYPE:
4111 case Insn_template::THUMB16_SPECIAL_TYPE:
4112 if (i == 0)
4113 this->entry_in_thumb_mode_ = true;
4114 break;
4116 case Insn_template::THUMB32_TYPE:
4117 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
4118 this->relocs_.push_back(Reloc(i, offset));
4119 if (i == 0)
4120 this->entry_in_thumb_mode_ = true;
4121 break;
4123 case Insn_template::ARM_TYPE:
4124 // Handle cases where the target is encoded within the
4125 // instruction.
4126 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
4127 this->relocs_.push_back(Reloc(i, offset));
4128 break;
4130 case Insn_template::DATA_TYPE:
4131 // Entry point cannot be data.
4132 gold_assert(i != 0);
4133 this->relocs_.push_back(Reloc(i, offset));
4134 break;
4136 default:
4137 gold_unreachable();
4139 offset += insn_size;
4141 this->size_ = offset;
4144 // Stub methods.
4146 // Template to implement do_write for a specific target endianness.
4148 template<bool big_endian>
4149 void inline
4150 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
4152 const Stub_template* stub_template = this->stub_template();
4153 const Insn_template* insns = stub_template->insns();
4155 // FIXME: We do not handle BE8 encoding yet.
4156 unsigned char* pov = view;
4157 for (size_t i = 0; i < stub_template->insn_count(); i++)
4159 switch (insns[i].type())
4161 case Insn_template::THUMB16_TYPE:
4162 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
4163 break;
4164 case Insn_template::THUMB16_SPECIAL_TYPE:
4165 elfcpp::Swap<16, big_endian>::writeval(
4166 pov,
4167 this->thumb16_special(i));
4168 break;
4169 case Insn_template::THUMB32_TYPE:
4171 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
4172 uint32_t lo = insns[i].data() & 0xffff;
4173 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
4174 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
4176 break;
4177 case Insn_template::ARM_TYPE:
4178 case Insn_template::DATA_TYPE:
4179 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
4180 break;
4181 default:
4182 gold_unreachable();
4184 pov += insns[i].size();
4186 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
4189 // Reloc_stub::Key methods.
4191 // Dump a Key as a string for debugging.
4193 std::string
4194 Reloc_stub::Key::name() const
4196 if (this->r_sym_ == invalid_index)
4198 // Global symbol key name
4199 // <stub-type>:<symbol name>:<addend>.
4200 const std::string sym_name = this->u_.symbol->name();
4201 // We need to print two hex number and two colons. So just add 100 bytes
4202 // to the symbol name size.
4203 size_t len = sym_name.size() + 100;
4204 char* buffer = new char[len];
4205 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
4206 sym_name.c_str(), this->addend_);
4207 gold_assert(c > 0 && c < static_cast<int>(len));
4208 delete[] buffer;
4209 return std::string(buffer);
4211 else
4213 // local symbol key name
4214 // <stub-type>:<object>:<r_sym>:<addend>.
4215 const size_t len = 200;
4216 char buffer[len];
4217 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
4218 this->u_.relobj, this->r_sym_, this->addend_);
4219 gold_assert(c > 0 && c < static_cast<int>(len));
4220 return std::string(buffer);
4224 // Reloc_stub methods.
4226 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
4227 // LOCATION to DESTINATION.
4228 // This code is based on the arm_type_of_stub function in
4229 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
4230 // class simple.
4232 Stub_type
4233 Reloc_stub::stub_type_for_reloc(
4234 unsigned int r_type,
4235 Arm_address location,
4236 Arm_address destination,
4237 bool target_is_thumb)
4239 Stub_type stub_type = arm_stub_none;
4241 // This is a bit ugly but we want to avoid using a templated class for
4242 // big and little endianities.
4243 bool may_use_blx;
4244 bool should_force_pic_veneer;
4245 bool thumb2;
4246 bool thumb_only;
4247 if (parameters->target().is_big_endian())
4249 const Target_arm<true>* big_endian_target =
4250 Target_arm<true>::default_target();
4251 may_use_blx = big_endian_target->may_use_blx();
4252 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
4253 thumb2 = big_endian_target->using_thumb2();
4254 thumb_only = big_endian_target->using_thumb_only();
4256 else
4258 const Target_arm<false>* little_endian_target =
4259 Target_arm<false>::default_target();
4260 may_use_blx = little_endian_target->may_use_blx();
4261 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
4262 thumb2 = little_endian_target->using_thumb2();
4263 thumb_only = little_endian_target->using_thumb_only();
4266 int64_t branch_offset;
4267 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
4269 // For THUMB BLX instruction, bit 1 of target comes from bit 1 of the
4270 // base address (instruction address + 4).
4271 if ((r_type == elfcpp::R_ARM_THM_CALL) && may_use_blx && !target_is_thumb)
4272 destination = utils::bit_select(destination, location, 0x2);
4273 branch_offset = static_cast<int64_t>(destination) - location;
4275 // Handle cases where:
4276 // - this call goes too far (different Thumb/Thumb2 max
4277 // distance)
4278 // - it's a Thumb->Arm call and blx is not available, or it's a
4279 // Thumb->Arm branch (not bl). A stub is needed in this case.
4280 if ((!thumb2
4281 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4282 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4283 || (thumb2
4284 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4285 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4286 || ((!target_is_thumb)
4287 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4288 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
4290 if (target_is_thumb)
4292 // Thumb to thumb.
4293 if (!thumb_only)
4295 stub_type = (parameters->options().shared()
4296 || should_force_pic_veneer)
4297 // PIC stubs.
4298 ? ((may_use_blx
4299 && (r_type == elfcpp::R_ARM_THM_CALL))
4300 // V5T and above. Stub starts with ARM code, so
4301 // we must be able to switch mode before
4302 // reaching it, which is only possible for 'bl'
4303 // (ie R_ARM_THM_CALL relocation).
4304 ? arm_stub_long_branch_any_thumb_pic
4305 // On V4T, use Thumb code only.
4306 : arm_stub_long_branch_v4t_thumb_thumb_pic)
4308 // non-PIC stubs.
4309 : ((may_use_blx
4310 && (r_type == elfcpp::R_ARM_THM_CALL))
4311 ? arm_stub_long_branch_any_any // V5T and above.
4312 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
4314 else
4316 stub_type = (parameters->options().shared()
4317 || should_force_pic_veneer)
4318 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
4319 : arm_stub_long_branch_thumb_only; // non-PIC stub.
4322 else
4324 // Thumb to arm.
4326 // FIXME: We should check that the input section is from an
4327 // object that has interwork enabled.
4329 stub_type = (parameters->options().shared()
4330 || should_force_pic_veneer)
4331 // PIC stubs.
4332 ? ((may_use_blx
4333 && (r_type == elfcpp::R_ARM_THM_CALL))
4334 ? arm_stub_long_branch_any_arm_pic // V5T and above.
4335 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
4337 // non-PIC stubs.
4338 : ((may_use_blx
4339 && (r_type == elfcpp::R_ARM_THM_CALL))
4340 ? arm_stub_long_branch_any_any // V5T and above.
4341 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
4343 // Handle v4t short branches.
4344 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4345 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4346 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4347 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4351 else if (r_type == elfcpp::R_ARM_CALL
4352 || r_type == elfcpp::R_ARM_JUMP24
4353 || r_type == elfcpp::R_ARM_PLT32)
4355 branch_offset = static_cast<int64_t>(destination) - location;
4356 if (target_is_thumb)
4358 // Arm to thumb.
4360 // FIXME: We should check that the input section is from an
4361 // object that has interwork enabled.
4363 // We have an extra 2-bytes reach because of
4364 // the mode change (bit 24 (H) of BLX encoding).
4365 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4366 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4367 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
4368 || (r_type == elfcpp::R_ARM_JUMP24)
4369 || (r_type == elfcpp::R_ARM_PLT32))
4371 stub_type = (parameters->options().shared()
4372 || should_force_pic_veneer)
4373 // PIC stubs.
4374 ? (may_use_blx
4375 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
4376 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
4378 // non-PIC stubs.
4379 : (may_use_blx
4380 ? arm_stub_long_branch_any_any // V5T and above.
4381 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
4384 else
4386 // Arm to arm.
4387 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4388 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4390 stub_type = (parameters->options().shared()
4391 || should_force_pic_veneer)
4392 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
4393 : arm_stub_long_branch_any_any; /// non-PIC.
4398 return stub_type;
4401 // Cortex_a8_stub methods.
4403 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4404 // I is the position of the instruction template in the stub template.
4406 uint16_t
4407 Cortex_a8_stub::do_thumb16_special(size_t i)
4409 // The only use of this is to copy condition code from a conditional
4410 // branch being worked around to the corresponding conditional branch in
4411 // to the stub.
4412 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4413 && i == 0);
4414 uint16_t data = this->stub_template()->insns()[i].data();
4415 gold_assert((data & 0xff00U) == 0xd000U);
4416 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
4417 return data;
4420 // Stub_factory methods.
4422 Stub_factory::Stub_factory()
4424 // The instruction template sequences are declared as static
4425 // objects and initialized first time the constructor runs.
4427 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4428 // to reach the stub if necessary.
4429 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
4431 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4432 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4433 // dcd R_ARM_ABS32(X)
4436 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4437 // available.
4438 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
4440 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4441 Insn_template::arm_insn(0xe12fff1c), // bx ip
4442 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4443 // dcd R_ARM_ABS32(X)
4446 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4447 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
4449 Insn_template::thumb16_insn(0xb401), // push {r0}
4450 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4451 Insn_template::thumb16_insn(0x4684), // mov ip, r0
4452 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4453 Insn_template::thumb16_insn(0x4760), // bx ip
4454 Insn_template::thumb16_insn(0xbf00), // nop
4455 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4456 // dcd R_ARM_ABS32(X)
4459 // V4T Thumb -> Thumb long branch stub. Using the stack is not
4460 // allowed.
4461 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
4463 Insn_template::thumb16_insn(0x4778), // bx pc
4464 Insn_template::thumb16_insn(0x46c0), // nop
4465 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4466 Insn_template::arm_insn(0xe12fff1c), // bx ip
4467 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4468 // dcd R_ARM_ABS32(X)
4471 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4472 // available.
4473 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
4475 Insn_template::thumb16_insn(0x4778), // bx pc
4476 Insn_template::thumb16_insn(0x46c0), // nop
4477 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4478 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4479 // dcd R_ARM_ABS32(X)
4482 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4483 // one, when the destination is close enough.
4484 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
4486 Insn_template::thumb16_insn(0x4778), // bx pc
4487 Insn_template::thumb16_insn(0x46c0), // nop
4488 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4491 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4492 // blx to reach the stub if necessary.
4493 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
4495 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4496 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4497 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4498 // dcd R_ARM_REL32(X-4)
4501 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4502 // blx to reach the stub if necessary. We can not add into pc;
4503 // it is not guaranteed to mode switch (different in ARMv6 and
4504 // ARMv7).
4505 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
4507 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4508 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4509 Insn_template::arm_insn(0xe12fff1c), // bx ip
4510 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4511 // dcd R_ARM_REL32(X)
4514 // V4T ARM -> ARM long branch stub, PIC.
4515 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4517 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4518 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4519 Insn_template::arm_insn(0xe12fff1c), // bx ip
4520 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4521 // dcd R_ARM_REL32(X)
4524 // V4T Thumb -> ARM long branch stub, PIC.
4525 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4527 Insn_template::thumb16_insn(0x4778), // bx pc
4528 Insn_template::thumb16_insn(0x46c0), // nop
4529 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4530 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4531 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4532 // dcd R_ARM_REL32(X)
4535 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4536 // architectures.
4537 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4539 Insn_template::thumb16_insn(0xb401), // push {r0}
4540 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4541 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4542 Insn_template::thumb16_insn(0x4484), // add ip, r0
4543 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4544 Insn_template::thumb16_insn(0x4760), // bx ip
4545 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4546 // dcd R_ARM_REL32(X)
4549 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4550 // allowed.
4551 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4553 Insn_template::thumb16_insn(0x4778), // bx pc
4554 Insn_template::thumb16_insn(0x46c0), // nop
4555 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4556 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4557 Insn_template::arm_insn(0xe12fff1c), // bx ip
4558 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4559 // dcd R_ARM_REL32(X)
4562 // Cortex-A8 erratum-workaround stubs.
4564 // Stub used for conditional branches (which may be beyond +/-1MB away,
4565 // so we can't use a conditional branch to reach this stub).
4567 // original code:
4569 // b<cond> X
4570 // after:
4572 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4574 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4575 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4576 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4577 // b.w X
4580 // Stub used for b.w and bl.w instructions.
4582 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4584 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4587 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4589 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4592 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4593 // instruction (which switches to ARM mode) to point to this stub. Jump to
4594 // the real destination using an ARM-mode branch.
4595 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
4597 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4600 // Stub used to provide an interworking for R_ARM_V4BX relocation
4601 // (bx r[n] instruction).
4602 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4604 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4605 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4606 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4609 // Fill in the stub template look-up table. Stub templates are constructed
4610 // per instance of Stub_factory for fast look-up without locking
4611 // in a thread-enabled environment.
4613 this->stub_templates_[arm_stub_none] =
4614 new Stub_template(arm_stub_none, NULL, 0);
4616 #define DEF_STUB(x) \
4617 do \
4619 size_t array_size \
4620 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4621 Stub_type type = arm_stub_##x; \
4622 this->stub_templates_[type] = \
4623 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4625 while (0);
4627 DEF_STUBS
4628 #undef DEF_STUB
4631 // Stub_table methods.
4633 // Removel all Cortex-A8 stub.
4635 template<bool big_endian>
4636 void
4637 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4639 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4640 p != this->cortex_a8_stubs_.end();
4641 ++p)
4642 delete p->second;
4643 this->cortex_a8_stubs_.clear();
4646 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4648 template<bool big_endian>
4649 void
4650 Stub_table<big_endian>::relocate_stub(
4651 Stub* stub,
4652 const Relocate_info<32, big_endian>* relinfo,
4653 Target_arm<big_endian>* arm_target,
4654 Output_section* output_section,
4655 unsigned char* view,
4656 Arm_address address,
4657 section_size_type view_size)
4659 const Stub_template* stub_template = stub->stub_template();
4660 if (stub_template->reloc_count() != 0)
4662 // Adjust view to cover the stub only.
4663 section_size_type offset = stub->offset();
4664 section_size_type stub_size = stub_template->size();
4665 gold_assert(offset + stub_size <= view_size);
4667 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4668 address + offset, stub_size);
4672 // Relocate all stubs in this stub table.
4674 template<bool big_endian>
4675 void
4676 Stub_table<big_endian>::relocate_stubs(
4677 const Relocate_info<32, big_endian>* relinfo,
4678 Target_arm<big_endian>* arm_target,
4679 Output_section* output_section,
4680 unsigned char* view,
4681 Arm_address address,
4682 section_size_type view_size)
4684 // If we are passed a view bigger than the stub table's. we need to
4685 // adjust the view.
4686 gold_assert(address == this->address()
4687 && (view_size
4688 == static_cast<section_size_type>(this->data_size())));
4690 // Relocate all relocation stubs.
4691 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4692 p != this->reloc_stubs_.end();
4693 ++p)
4694 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4695 address, view_size);
4697 // Relocate all Cortex-A8 stubs.
4698 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4699 p != this->cortex_a8_stubs_.end();
4700 ++p)
4701 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4702 address, view_size);
4704 // Relocate all ARM V4BX stubs.
4705 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4706 p != this->arm_v4bx_stubs_.end();
4707 ++p)
4709 if (*p != NULL)
4710 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4711 address, view_size);
4715 // Write out the stubs to file.
4717 template<bool big_endian>
4718 void
4719 Stub_table<big_endian>::do_write(Output_file* of)
4721 off_t offset = this->offset();
4722 const section_size_type oview_size =
4723 convert_to_section_size_type(this->data_size());
4724 unsigned char* const oview = of->get_output_view(offset, oview_size);
4726 // Write relocation stubs.
4727 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4728 p != this->reloc_stubs_.end();
4729 ++p)
4731 Reloc_stub* stub = p->second;
4732 Arm_address address = this->address() + stub->offset();
4733 gold_assert(address
4734 == align_address(address,
4735 stub->stub_template()->alignment()));
4736 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4737 big_endian);
4740 // Write Cortex-A8 stubs.
4741 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4742 p != this->cortex_a8_stubs_.end();
4743 ++p)
4745 Cortex_a8_stub* stub = p->second;
4746 Arm_address address = this->address() + stub->offset();
4747 gold_assert(address
4748 == align_address(address,
4749 stub->stub_template()->alignment()));
4750 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4751 big_endian);
4754 // Write ARM V4BX relocation stubs.
4755 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4756 p != this->arm_v4bx_stubs_.end();
4757 ++p)
4759 if (*p == NULL)
4760 continue;
4762 Arm_address address = this->address() + (*p)->offset();
4763 gold_assert(address
4764 == align_address(address,
4765 (*p)->stub_template()->alignment()));
4766 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4767 big_endian);
4770 of->write_output_view(this->offset(), oview_size, oview);
4773 // Update the data size and address alignment of the stub table at the end
4774 // of a relaxation pass. Return true if either the data size or the
4775 // alignment changed in this relaxation pass.
4777 template<bool big_endian>
4778 bool
4779 Stub_table<big_endian>::update_data_size_and_addralign()
4781 // Go over all stubs in table to compute data size and address alignment.
4782 off_t size = this->reloc_stubs_size_;
4783 unsigned addralign = this->reloc_stubs_addralign_;
4785 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4786 p != this->cortex_a8_stubs_.end();
4787 ++p)
4789 const Stub_template* stub_template = p->second->stub_template();
4790 addralign = std::max(addralign, stub_template->alignment());
4791 size = (align_address(size, stub_template->alignment())
4792 + stub_template->size());
4795 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4796 p != this->arm_v4bx_stubs_.end();
4797 ++p)
4799 if (*p == NULL)
4800 continue;
4802 const Stub_template* stub_template = (*p)->stub_template();
4803 addralign = std::max(addralign, stub_template->alignment());
4804 size = (align_address(size, stub_template->alignment())
4805 + stub_template->size());
4808 // Check if either data size or alignment changed in this pass.
4809 // Update prev_data_size_ and prev_addralign_. These will be used
4810 // as the current data size and address alignment for the next pass.
4811 bool changed = size != this->prev_data_size_;
4812 this->prev_data_size_ = size;
4814 if (addralign != this->prev_addralign_)
4815 changed = true;
4816 this->prev_addralign_ = addralign;
4818 return changed;
4821 // Finalize the stubs. This sets the offsets of the stubs within the stub
4822 // table. It also marks all input sections needing Cortex-A8 workaround.
4824 template<bool big_endian>
4825 void
4826 Stub_table<big_endian>::finalize_stubs()
4828 off_t off = this->reloc_stubs_size_;
4829 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4830 p != this->cortex_a8_stubs_.end();
4831 ++p)
4833 Cortex_a8_stub* stub = p->second;
4834 const Stub_template* stub_template = stub->stub_template();
4835 uint64_t stub_addralign = stub_template->alignment();
4836 off = align_address(off, stub_addralign);
4837 stub->set_offset(off);
4838 off += stub_template->size();
4840 // Mark input section so that we can determine later if a code section
4841 // needs the Cortex-A8 workaround quickly.
4842 Arm_relobj<big_endian>* arm_relobj =
4843 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
4844 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
4847 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4848 p != this->arm_v4bx_stubs_.end();
4849 ++p)
4851 if (*p == NULL)
4852 continue;
4854 const Stub_template* stub_template = (*p)->stub_template();
4855 uint64_t stub_addralign = stub_template->alignment();
4856 off = align_address(off, stub_addralign);
4857 (*p)->set_offset(off);
4858 off += stub_template->size();
4861 gold_assert(off <= this->prev_data_size_);
4864 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4865 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4866 // of the address range seen by the linker.
4868 template<bool big_endian>
4869 void
4870 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
4871 Target_arm<big_endian>* arm_target,
4872 unsigned char* view,
4873 Arm_address view_address,
4874 section_size_type view_size)
4876 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4877 for (Cortex_a8_stub_list::const_iterator p =
4878 this->cortex_a8_stubs_.lower_bound(view_address);
4879 ((p != this->cortex_a8_stubs_.end())
4880 && (p->first < (view_address + view_size)));
4881 ++p)
4883 // We do not store the THUMB bit in the LSB of either the branch address
4884 // or the stub offset. There is no need to strip the LSB.
4885 Arm_address branch_address = p->first;
4886 const Cortex_a8_stub* stub = p->second;
4887 Arm_address stub_address = this->address() + stub->offset();
4889 // Offset of the branch instruction relative to this view.
4890 section_size_type offset =
4891 convert_to_section_size_type(branch_address - view_address);
4892 gold_assert((offset + 4) <= view_size);
4894 arm_target->apply_cortex_a8_workaround(stub, stub_address,
4895 view + offset, branch_address);
4899 // Arm_input_section methods.
4901 // Initialize an Arm_input_section.
4903 template<bool big_endian>
4904 void
4905 Arm_input_section<big_endian>::init()
4907 Relobj* relobj = this->relobj();
4908 unsigned int shndx = this->shndx();
4910 // Cache these to speed up size and alignment queries. It is too slow
4911 // to call section_addraglin and section_size every time.
4912 this->original_addralign_ = relobj->section_addralign(shndx);
4913 this->original_size_ = relobj->section_size(shndx);
4915 // We want to make this look like the original input section after
4916 // output sections are finalized.
4917 Output_section* os = relobj->output_section(shndx);
4918 off_t offset = relobj->output_section_offset(shndx);
4919 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
4920 this->set_address(os->address() + offset);
4921 this->set_file_offset(os->offset() + offset);
4923 this->set_current_data_size(this->original_size_);
4924 this->finalize_data_size();
4927 template<bool big_endian>
4928 void
4929 Arm_input_section<big_endian>::do_write(Output_file* of)
4931 // We have to write out the original section content.
4932 section_size_type section_size;
4933 const unsigned char* section_contents =
4934 this->relobj()->section_contents(this->shndx(), &section_size, false);
4935 of->write(this->offset(), section_contents, section_size);
4937 // If this owns a stub table and it is not empty, write it.
4938 if (this->is_stub_table_owner() && !this->stub_table_->empty())
4939 this->stub_table_->write(of);
4942 // Finalize data size.
4944 template<bool big_endian>
4945 void
4946 Arm_input_section<big_endian>::set_final_data_size()
4948 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4950 if (this->is_stub_table_owner())
4952 // The stub table comes after the original section contents.
4953 off = align_address(off, this->stub_table_->addralign());
4954 this->stub_table_->set_address_and_file_offset(this->address() + off,
4955 this->offset() + off);
4956 off += this->stub_table_->data_size();
4958 this->set_data_size(off);
4961 // Reset address and file offset.
4963 template<bool big_endian>
4964 void
4965 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
4967 // Size of the original input section contents.
4968 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4970 // If this is a stub table owner, account for the stub table size.
4971 if (this->is_stub_table_owner())
4973 Stub_table<big_endian>* stub_table = this->stub_table_;
4975 // Reset the stub table's address and file offset. The
4976 // current data size for child will be updated after that.
4977 stub_table_->reset_address_and_file_offset();
4978 off = align_address(off, stub_table_->addralign());
4979 off += stub_table->current_data_size();
4982 this->set_current_data_size(off);
4985 // Arm_exidx_cantunwind methods.
4987 // Write this to Output file OF for a fixed endianness.
4989 template<bool big_endian>
4990 void
4991 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
4993 off_t offset = this->offset();
4994 const section_size_type oview_size = 8;
4995 unsigned char* const oview = of->get_output_view(offset, oview_size);
4997 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4998 Valtype* wv = reinterpret_cast<Valtype*>(oview);
5000 Output_section* os = this->relobj_->output_section(this->shndx_);
5001 gold_assert(os != NULL);
5003 Arm_relobj<big_endian>* arm_relobj =
5004 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
5005 Arm_address output_offset =
5006 arm_relobj->get_output_section_offset(this->shndx_);
5007 Arm_address section_start;
5008 if (output_offset != Arm_relobj<big_endian>::invalid_address)
5009 section_start = os->address() + output_offset;
5010 else
5012 // Currently this only happens for a relaxed section.
5013 const Output_relaxed_input_section* poris =
5014 os->find_relaxed_input_section(this->relobj_, this->shndx_);
5015 gold_assert(poris != NULL);
5016 section_start = poris->address();
5019 // We always append this to the end of an EXIDX section.
5020 Arm_address output_address =
5021 section_start + this->relobj_->section_size(this->shndx_);
5023 // Write out the entry. The first word either points to the beginning
5024 // or after the end of a text section. The second word is the special
5025 // EXIDX_CANTUNWIND value.
5026 uint32_t prel31_offset = output_address - this->address();
5027 if (utils::has_overflow<31>(offset))
5028 gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
5029 elfcpp::Swap<32, big_endian>::writeval(wv, prel31_offset & 0x7fffffffU);
5030 elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
5032 of->write_output_view(this->offset(), oview_size, oview);
5035 // Arm_exidx_merged_section methods.
5037 // Constructor for Arm_exidx_merged_section.
5038 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
5039 // SECTION_OFFSET_MAP points to a section offset map describing how
5040 // parts of the input section are mapped to output. DELETED_BYTES is
5041 // the number of bytes deleted from the EXIDX input section.
5043 Arm_exidx_merged_section::Arm_exidx_merged_section(
5044 const Arm_exidx_input_section& exidx_input_section,
5045 const Arm_exidx_section_offset_map& section_offset_map,
5046 uint32_t deleted_bytes)
5047 : Output_relaxed_input_section(exidx_input_section.relobj(),
5048 exidx_input_section.shndx(),
5049 exidx_input_section.addralign()),
5050 exidx_input_section_(exidx_input_section),
5051 section_offset_map_(section_offset_map)
5053 // Fix size here so that we do not need to implement set_final_data_size.
5054 this->set_data_size(exidx_input_section.size() - deleted_bytes);
5055 this->fix_data_size();
5058 // Given an input OBJECT, an input section index SHNDX within that
5059 // object, and an OFFSET relative to the start of that input
5060 // section, return whether or not the corresponding offset within
5061 // the output section is known. If this function returns true, it
5062 // sets *POUTPUT to the output offset. The value -1 indicates that
5063 // this input offset is being discarded.
5065 bool
5066 Arm_exidx_merged_section::do_output_offset(
5067 const Relobj* relobj,
5068 unsigned int shndx,
5069 section_offset_type offset,
5070 section_offset_type* poutput) const
5072 // We only handle offsets for the original EXIDX input section.
5073 if (relobj != this->exidx_input_section_.relobj()
5074 || shndx != this->exidx_input_section_.shndx())
5075 return false;
5077 section_offset_type section_size =
5078 convert_types<section_offset_type>(this->exidx_input_section_.size());
5079 if (offset < 0 || offset >= section_size)
5080 // Input offset is out of valid range.
5081 *poutput = -1;
5082 else
5084 // We need to look up the section offset map to determine the output
5085 // offset. Find the reference point in map that is first offset
5086 // bigger than or equal to this offset.
5087 Arm_exidx_section_offset_map::const_iterator p =
5088 this->section_offset_map_.lower_bound(offset);
5090 // The section offset maps are build such that this should not happen if
5091 // input offset is in the valid range.
5092 gold_assert(p != this->section_offset_map_.end());
5094 // We need to check if this is dropped.
5095 section_offset_type ref = p->first;
5096 section_offset_type mapped_ref = p->second;
5098 if (mapped_ref != Arm_exidx_input_section::invalid_offset)
5099 // Offset is present in output.
5100 *poutput = mapped_ref + (offset - ref);
5101 else
5102 // Offset is discarded owing to EXIDX entry merging.
5103 *poutput = -1;
5106 return true;
5109 // Write this to output file OF.
5111 void
5112 Arm_exidx_merged_section::do_write(Output_file* of)
5114 // If we retain or discard the whole EXIDX input section, we would
5115 // not be here.
5116 gold_assert(this->data_size() != this->exidx_input_section_.size()
5117 && this->data_size() != 0);
5119 off_t offset = this->offset();
5120 const section_size_type oview_size = this->data_size();
5121 unsigned char* const oview = of->get_output_view(offset, oview_size);
5123 Output_section* os = this->relobj()->output_section(this->shndx());
5124 gold_assert(os != NULL);
5126 // Get contents of EXIDX input section.
5127 section_size_type section_size;
5128 const unsigned char* section_contents =
5129 this->relobj()->section_contents(this->shndx(), &section_size, false);
5130 gold_assert(section_size == this->exidx_input_section_.size());
5132 // Go over spans of input offsets and write only those that are not
5133 // discarded.
5134 section_offset_type in_start = 0;
5135 section_offset_type out_start = 0;
5136 for(Arm_exidx_section_offset_map::const_iterator p =
5137 this->section_offset_map_.begin();
5138 p != this->section_offset_map_.end();
5139 ++p)
5141 section_offset_type in_end = p->first;
5142 gold_assert(in_end >= in_start);
5143 section_offset_type out_end = p->second;
5144 size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
5145 if (out_end != -1)
5147 size_t out_chunk_size =
5148 convert_types<size_t>(out_end - out_start + 1);
5149 gold_assert(out_chunk_size == in_chunk_size);
5150 memcpy(oview + out_start, section_contents + in_start,
5151 out_chunk_size);
5152 out_start += out_chunk_size;
5154 in_start += in_chunk_size;
5157 gold_assert(convert_to_section_size_type(out_start) == oview_size);
5158 of->write_output_view(this->offset(), oview_size, oview);
5161 // Arm_exidx_fixup methods.
5163 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
5164 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
5165 // points to the end of the last seen EXIDX section.
5167 void
5168 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
5170 if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
5171 && this->last_input_section_ != NULL)
5173 Relobj* relobj = this->last_input_section_->relobj();
5174 unsigned int text_shndx = this->last_input_section_->link();
5175 Arm_exidx_cantunwind* cantunwind =
5176 new Arm_exidx_cantunwind(relobj, text_shndx);
5177 this->exidx_output_section_->add_output_section_data(cantunwind);
5178 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5182 // Process an EXIDX section entry in input. Return whether this entry
5183 // can be deleted in the output. SECOND_WORD in the second word of the
5184 // EXIDX entry.
5186 bool
5187 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
5189 bool delete_entry;
5190 if (second_word == elfcpp::EXIDX_CANTUNWIND)
5192 // Merge if previous entry is also an EXIDX_CANTUNWIND.
5193 delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
5194 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5196 else if ((second_word & 0x80000000) != 0)
5198 // Inlined unwinding data. Merge if equal to previous.
5199 delete_entry = (merge_exidx_entries_
5200 && this->last_unwind_type_ == UT_INLINED_ENTRY
5201 && this->last_inlined_entry_ == second_word);
5202 this->last_unwind_type_ = UT_INLINED_ENTRY;
5203 this->last_inlined_entry_ = second_word;
5205 else
5207 // Normal table entry. In theory we could merge these too,
5208 // but duplicate entries are likely to be much less common.
5209 delete_entry = false;
5210 this->last_unwind_type_ = UT_NORMAL_ENTRY;
5212 return delete_entry;
5215 // Update the current section offset map during EXIDX section fix-up.
5216 // If there is no map, create one. INPUT_OFFSET is the offset of a
5217 // reference point, DELETED_BYTES is the number of deleted by in the
5218 // section so far. If DELETE_ENTRY is true, the reference point and
5219 // all offsets after the previous reference point are discarded.
5221 void
5222 Arm_exidx_fixup::update_offset_map(
5223 section_offset_type input_offset,
5224 section_size_type deleted_bytes,
5225 bool delete_entry)
5227 if (this->section_offset_map_ == NULL)
5228 this->section_offset_map_ = new Arm_exidx_section_offset_map();
5229 section_offset_type output_offset;
5230 if (delete_entry)
5231 output_offset = Arm_exidx_input_section::invalid_offset;
5232 else
5233 output_offset = input_offset - deleted_bytes;
5234 (*this->section_offset_map_)[input_offset] = output_offset;
5237 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
5238 // bytes deleted. If some entries are merged, also store a pointer to a newly
5239 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
5240 // caller owns the map and is responsible for releasing it after use.
5242 template<bool big_endian>
5243 uint32_t
5244 Arm_exidx_fixup::process_exidx_section(
5245 const Arm_exidx_input_section* exidx_input_section,
5246 Arm_exidx_section_offset_map** psection_offset_map)
5248 Relobj* relobj = exidx_input_section->relobj();
5249 unsigned shndx = exidx_input_section->shndx();
5250 section_size_type section_size;
5251 const unsigned char* section_contents =
5252 relobj->section_contents(shndx, &section_size, false);
5254 if ((section_size % 8) != 0)
5256 // Something is wrong with this section. Better not touch it.
5257 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5258 relobj->name().c_str(), shndx);
5259 this->last_input_section_ = exidx_input_section;
5260 this->last_unwind_type_ = UT_NONE;
5261 return 0;
5264 uint32_t deleted_bytes = 0;
5265 bool prev_delete_entry = false;
5266 gold_assert(this->section_offset_map_ == NULL);
5268 for (section_size_type i = 0; i < section_size; i += 8)
5270 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5271 const Valtype* wv =
5272 reinterpret_cast<const Valtype*>(section_contents + i + 4);
5273 uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
5275 bool delete_entry = this->process_exidx_entry(second_word);
5277 // Entry deletion causes changes in output offsets. We use a std::map
5278 // to record these. And entry (x, y) means input offset x
5279 // is mapped to output offset y. If y is invalid_offset, then x is
5280 // dropped in the output. Because of the way std::map::lower_bound
5281 // works, we record the last offset in a region w.r.t to keeping or
5282 // dropping. If there is no entry (x0, y0) for an input offset x0,
5283 // the output offset y0 of it is determined by the output offset y1 of
5284 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5285 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
5286 // y0 is also -1.
5287 if (delete_entry != prev_delete_entry && i != 0)
5288 this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
5290 // Update total deleted bytes for this entry.
5291 if (delete_entry)
5292 deleted_bytes += 8;
5294 prev_delete_entry = delete_entry;
5297 // If section offset map is not NULL, make an entry for the end of
5298 // section.
5299 if (this->section_offset_map_ != NULL)
5300 update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
5302 *psection_offset_map = this->section_offset_map_;
5303 this->section_offset_map_ = NULL;
5304 this->last_input_section_ = exidx_input_section;
5306 // Set the first output text section so that we can link the EXIDX output
5307 // section to it. Ignore any EXIDX input section that is completely merged.
5308 if (this->first_output_text_section_ == NULL
5309 && deleted_bytes != section_size)
5311 unsigned int link = exidx_input_section->link();
5312 Output_section* os = relobj->output_section(link);
5313 gold_assert(os != NULL);
5314 this->first_output_text_section_ = os;
5317 return deleted_bytes;
5320 // Arm_output_section methods.
5322 // Create a stub group for input sections from BEGIN to END. OWNER
5323 // points to the input section to be the owner a new stub table.
5325 template<bool big_endian>
5326 void
5327 Arm_output_section<big_endian>::create_stub_group(
5328 Input_section_list::const_iterator begin,
5329 Input_section_list::const_iterator end,
5330 Input_section_list::const_iterator owner,
5331 Target_arm<big_endian>* target,
5332 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
5334 // We use a different kind of relaxed section in an EXIDX section.
5335 // The static casting from Output_relaxed_input_section to
5336 // Arm_input_section is invalid in an EXIDX section. We are okay
5337 // because we should not be calling this for an EXIDX section.
5338 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
5340 // Currently we convert ordinary input sections into relaxed sections only
5341 // at this point but we may want to support creating relaxed input section
5342 // very early. So we check here to see if owner is already a relaxed
5343 // section.
5345 Arm_input_section<big_endian>* arm_input_section;
5346 if (owner->is_relaxed_input_section())
5348 arm_input_section =
5349 Arm_input_section<big_endian>::as_arm_input_section(
5350 owner->relaxed_input_section());
5352 else
5354 gold_assert(owner->is_input_section());
5355 // Create a new relaxed input section.
5356 arm_input_section =
5357 target->new_arm_input_section(owner->relobj(), owner->shndx());
5358 new_relaxed_sections->push_back(arm_input_section);
5361 // Create a stub table.
5362 Stub_table<big_endian>* stub_table =
5363 target->new_stub_table(arm_input_section);
5365 arm_input_section->set_stub_table(stub_table);
5367 Input_section_list::const_iterator p = begin;
5368 Input_section_list::const_iterator prev_p;
5370 // Look for input sections or relaxed input sections in [begin ... end].
5373 if (p->is_input_section() || p->is_relaxed_input_section())
5375 // The stub table information for input sections live
5376 // in their objects.
5377 Arm_relobj<big_endian>* arm_relobj =
5378 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5379 arm_relobj->set_stub_table(p->shndx(), stub_table);
5381 prev_p = p++;
5383 while (prev_p != end);
5386 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
5387 // of stub groups. We grow a stub group by adding input section until the
5388 // size is just below GROUP_SIZE. The last input section will be converted
5389 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5390 // input section after the stub table, effectively double the group size.
5392 // This is similar to the group_sections() function in elf32-arm.c but is
5393 // implemented differently.
5395 template<bool big_endian>
5396 void
5397 Arm_output_section<big_endian>::group_sections(
5398 section_size_type group_size,
5399 bool stubs_always_after_branch,
5400 Target_arm<big_endian>* target)
5402 // We only care about sections containing code.
5403 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5404 return;
5406 // States for grouping.
5407 typedef enum
5409 // No group is being built.
5410 NO_GROUP,
5411 // A group is being built but the stub table is not found yet.
5412 // We keep group a stub group until the size is just under GROUP_SIZE.
5413 // The last input section in the group will be used as the stub table.
5414 FINDING_STUB_SECTION,
5415 // A group is being built and we have already found a stub table.
5416 // We enter this state to grow a stub group by adding input section
5417 // after the stub table. This effectively doubles the group size.
5418 HAS_STUB_SECTION
5419 } State;
5421 // Any newly created relaxed sections are stored here.
5422 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
5424 State state = NO_GROUP;
5425 section_size_type off = 0;
5426 section_size_type group_begin_offset = 0;
5427 section_size_type group_end_offset = 0;
5428 section_size_type stub_table_end_offset = 0;
5429 Input_section_list::const_iterator group_begin =
5430 this->input_sections().end();
5431 Input_section_list::const_iterator stub_table =
5432 this->input_sections().end();
5433 Input_section_list::const_iterator group_end = this->input_sections().end();
5434 for (Input_section_list::const_iterator p = this->input_sections().begin();
5435 p != this->input_sections().end();
5436 ++p)
5438 section_size_type section_begin_offset =
5439 align_address(off, p->addralign());
5440 section_size_type section_end_offset =
5441 section_begin_offset + p->data_size();
5443 // Check to see if we should group the previously seens sections.
5444 switch (state)
5446 case NO_GROUP:
5447 break;
5449 case FINDING_STUB_SECTION:
5450 // Adding this section makes the group larger than GROUP_SIZE.
5451 if (section_end_offset - group_begin_offset >= group_size)
5453 if (stubs_always_after_branch)
5455 gold_assert(group_end != this->input_sections().end());
5456 this->create_stub_group(group_begin, group_end, group_end,
5457 target, &new_relaxed_sections);
5458 state = NO_GROUP;
5460 else
5462 // But wait, there's more! Input sections up to
5463 // stub_group_size bytes after the stub table can be
5464 // handled by it too.
5465 state = HAS_STUB_SECTION;
5466 stub_table = group_end;
5467 stub_table_end_offset = group_end_offset;
5470 break;
5472 case HAS_STUB_SECTION:
5473 // Adding this section makes the post stub-section group larger
5474 // than GROUP_SIZE.
5475 if (section_end_offset - stub_table_end_offset >= group_size)
5477 gold_assert(group_end != this->input_sections().end());
5478 this->create_stub_group(group_begin, group_end, stub_table,
5479 target, &new_relaxed_sections);
5480 state = NO_GROUP;
5482 break;
5484 default:
5485 gold_unreachable();
5488 // If we see an input section and currently there is no group, start
5489 // a new one. Skip any empty sections.
5490 if ((p->is_input_section() || p->is_relaxed_input_section())
5491 && (p->relobj()->section_size(p->shndx()) != 0))
5493 if (state == NO_GROUP)
5495 state = FINDING_STUB_SECTION;
5496 group_begin = p;
5497 group_begin_offset = section_begin_offset;
5500 // Keep track of the last input section seen.
5501 group_end = p;
5502 group_end_offset = section_end_offset;
5505 off = section_end_offset;
5508 // Create a stub group for any ungrouped sections.
5509 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5511 gold_assert(group_end != this->input_sections().end());
5512 this->create_stub_group(group_begin, group_end,
5513 (state == FINDING_STUB_SECTION
5514 ? group_end
5515 : stub_table),
5516 target, &new_relaxed_sections);
5519 // Convert input section into relaxed input section in a batch.
5520 if (!new_relaxed_sections.empty())
5521 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5523 // Update the section offsets
5524 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5526 Arm_relobj<big_endian>* arm_relobj =
5527 Arm_relobj<big_endian>::as_arm_relobj(
5528 new_relaxed_sections[i]->relobj());
5529 unsigned int shndx = new_relaxed_sections[i]->shndx();
5530 // Tell Arm_relobj that this input section is converted.
5531 arm_relobj->convert_input_section_to_relaxed_section(shndx);
5535 // Append non empty text sections in this to LIST in ascending
5536 // order of their position in this.
5538 template<bool big_endian>
5539 void
5540 Arm_output_section<big_endian>::append_text_sections_to_list(
5541 Text_section_list* list)
5543 // We only care about text sections.
5544 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5545 return;
5547 gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
5549 for (Input_section_list::const_iterator p = this->input_sections().begin();
5550 p != this->input_sections().end();
5551 ++p)
5553 // We only care about plain or relaxed input sections. We also
5554 // ignore any merged sections.
5555 if ((p->is_input_section() || p->is_relaxed_input_section())
5556 && p->data_size() != 0)
5557 list->push_back(Text_section_list::value_type(p->relobj(),
5558 p->shndx()));
5562 template<bool big_endian>
5563 void
5564 Arm_output_section<big_endian>::fix_exidx_coverage(
5565 Layout* layout,
5566 const Text_section_list& sorted_text_sections,
5567 Symbol_table* symtab,
5568 bool merge_exidx_entries)
5570 // We should only do this for the EXIDX output section.
5571 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5573 // We don't want the relaxation loop to undo these changes, so we discard
5574 // the current saved states and take another one after the fix-up.
5575 this->discard_states();
5577 // Remove all input sections.
5578 uint64_t address = this->address();
5579 typedef std::list<Simple_input_section> Simple_input_section_list;
5580 Simple_input_section_list input_sections;
5581 this->reset_address_and_file_offset();
5582 this->get_input_sections(address, std::string(""), &input_sections);
5584 if (!this->input_sections().empty())
5585 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5587 // Go through all the known input sections and record them.
5588 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5589 Section_id_set known_input_sections;
5590 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5591 p != input_sections.end();
5592 ++p)
5594 // This should never happen. At this point, we should only see
5595 // plain EXIDX input sections.
5596 gold_assert(!p->is_relaxed_input_section());
5597 known_input_sections.insert(Section_id(p->relobj(), p->shndx()));
5600 Arm_exidx_fixup exidx_fixup(this, merge_exidx_entries);
5602 // Go over the sorted text sections.
5603 Section_id_set processed_input_sections;
5604 for (Text_section_list::const_iterator p = sorted_text_sections.begin();
5605 p != sorted_text_sections.end();
5606 ++p)
5608 Relobj* relobj = p->first;
5609 unsigned int shndx = p->second;
5611 Arm_relobj<big_endian>* arm_relobj =
5612 Arm_relobj<big_endian>::as_arm_relobj(relobj);
5613 const Arm_exidx_input_section* exidx_input_section =
5614 arm_relobj->exidx_input_section_by_link(shndx);
5616 // If this text section has no EXIDX section, force an EXIDX_CANTUNWIND
5617 // entry pointing to the end of the last seen EXIDX section.
5618 if (exidx_input_section == NULL)
5620 exidx_fixup.add_exidx_cantunwind_as_needed();
5621 continue;
5624 Relobj* exidx_relobj = exidx_input_section->relobj();
5625 unsigned int exidx_shndx = exidx_input_section->shndx();
5626 Section_id sid(exidx_relobj, exidx_shndx);
5627 if (known_input_sections.find(sid) == known_input_sections.end())
5629 // This is odd. We have not seen this EXIDX input section before.
5630 // We cannot do fix-up. If we saw a SECTIONS clause in a script,
5631 // issue a warning instead. We assume the user knows what he
5632 // or she is doing. Otherwise, this is an error.
5633 if (layout->script_options()->saw_sections_clause())
5634 gold_warning(_("unwinding may not work because EXIDX input section"
5635 " %u of %s is not in EXIDX output section"),
5636 exidx_shndx, exidx_relobj->name().c_str());
5637 else
5638 gold_error(_("unwinding may not work because EXIDX input section"
5639 " %u of %s is not in EXIDX output section"),
5640 exidx_shndx, exidx_relobj->name().c_str());
5642 exidx_fixup.add_exidx_cantunwind_as_needed();
5643 continue;
5646 // Fix up coverage and append input section to output data list.
5647 Arm_exidx_section_offset_map* section_offset_map = NULL;
5648 uint32_t deleted_bytes =
5649 exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
5650 &section_offset_map);
5652 if (deleted_bytes == exidx_input_section->size())
5654 // The whole EXIDX section got merged. Remove it from output.
5655 gold_assert(section_offset_map == NULL);
5656 exidx_relobj->set_output_section(exidx_shndx, NULL);
5658 // All local symbols defined in this input section will be dropped.
5659 // We need to adjust output local symbol count.
5660 arm_relobj->set_output_local_symbol_count_needs_update();
5662 else if (deleted_bytes > 0)
5664 // Some entries are merged. We need to convert this EXIDX input
5665 // section into a relaxed section.
5666 gold_assert(section_offset_map != NULL);
5667 Arm_exidx_merged_section* merged_section =
5668 new Arm_exidx_merged_section(*exidx_input_section,
5669 *section_offset_map, deleted_bytes);
5670 this->add_relaxed_input_section(merged_section);
5671 arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
5673 // All local symbols defined in discarded portions of this input
5674 // section will be dropped. We need to adjust output local symbol
5675 // count.
5676 arm_relobj->set_output_local_symbol_count_needs_update();
5678 else
5680 // Just add back the EXIDX input section.
5681 gold_assert(section_offset_map == NULL);
5682 Output_section::Simple_input_section sis(exidx_relobj, exidx_shndx);
5683 this->add_simple_input_section(sis, exidx_input_section->size(),
5684 exidx_input_section->addralign());
5687 processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx));
5690 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5691 exidx_fixup.add_exidx_cantunwind_as_needed();
5693 // Remove any known EXIDX input sections that are not processed.
5694 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5695 p != input_sections.end();
5696 ++p)
5698 if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
5699 == processed_input_sections.end())
5701 // We only discard a known EXIDX section because its linked
5702 // text section has been folded by ICF.
5703 Arm_relobj<big_endian>* arm_relobj =
5704 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5705 const Arm_exidx_input_section* exidx_input_section =
5706 arm_relobj->exidx_input_section_by_shndx(p->shndx());
5707 gold_assert(exidx_input_section != NULL);
5708 unsigned int text_shndx = exidx_input_section->link();
5709 gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
5711 // Remove this from link. We also need to recount the
5712 // local symbols.
5713 p->relobj()->set_output_section(p->shndx(), NULL);
5714 arm_relobj->set_output_local_symbol_count_needs_update();
5718 // Link exidx output section to the first seen output section and
5719 // set correct entry size.
5720 this->set_link_section(exidx_fixup.first_output_text_section());
5721 this->set_entsize(8);
5723 // Make changes permanent.
5724 this->save_states();
5725 this->set_section_offsets_need_adjustment();
5728 // Arm_relobj methods.
5730 // Determine if an input section is scannable for stub processing. SHDR is
5731 // the header of the section and SHNDX is the section index. OS is the output
5732 // section for the input section and SYMTAB is the global symbol table used to
5733 // look up ICF information.
5735 template<bool big_endian>
5736 bool
5737 Arm_relobj<big_endian>::section_is_scannable(
5738 const elfcpp::Shdr<32, big_endian>& shdr,
5739 unsigned int shndx,
5740 const Output_section* os,
5741 const Symbol_table *symtab)
5743 // Skip any empty sections, unallocated sections or sections whose
5744 // type are not SHT_PROGBITS.
5745 if (shdr.get_sh_size() == 0
5746 || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
5747 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
5748 return false;
5750 // Skip any discarded or ICF'ed sections.
5751 if (os == NULL || symtab->is_section_folded(this, shndx))
5752 return false;
5754 // If this requires special offset handling, check to see if it is
5755 // a relaxed section. If this is not, then it is a merged section that
5756 // we cannot handle.
5757 if (this->is_output_section_offset_invalid(shndx))
5759 const Output_relaxed_input_section* poris =
5760 os->find_relaxed_input_section(this, shndx);
5761 if (poris == NULL)
5762 return false;
5765 return true;
5768 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5769 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5771 template<bool big_endian>
5772 bool
5773 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
5774 const elfcpp::Shdr<32, big_endian>& shdr,
5775 const Relobj::Output_sections& out_sections,
5776 const Symbol_table *symtab,
5777 const unsigned char* pshdrs)
5779 unsigned int sh_type = shdr.get_sh_type();
5780 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
5781 return false;
5783 // Ignore empty section.
5784 off_t sh_size = shdr.get_sh_size();
5785 if (sh_size == 0)
5786 return false;
5788 // Ignore reloc section with unexpected symbol table. The
5789 // error will be reported in the final link.
5790 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
5791 return false;
5793 unsigned int reloc_size;
5794 if (sh_type == elfcpp::SHT_REL)
5795 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5796 else
5797 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5799 // Ignore reloc section with unexpected entsize or uneven size.
5800 // The error will be reported in the final link.
5801 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
5802 return false;
5804 // Ignore reloc section with bad info. This error will be
5805 // reported in the final link.
5806 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5807 if (index >= this->shnum())
5808 return false;
5810 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5811 const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
5812 return this->section_is_scannable(text_shdr, index,
5813 out_sections[index], symtab);
5816 // Return the output address of either a plain input section or a relaxed
5817 // input section. SHNDX is the section index. We define and use this
5818 // instead of calling Output_section::output_address because that is slow
5819 // for large output.
5821 template<bool big_endian>
5822 Arm_address
5823 Arm_relobj<big_endian>::simple_input_section_output_address(
5824 unsigned int shndx,
5825 Output_section* os)
5827 if (this->is_output_section_offset_invalid(shndx))
5829 const Output_relaxed_input_section* poris =
5830 os->find_relaxed_input_section(this, shndx);
5831 // We do not handle merged sections here.
5832 gold_assert(poris != NULL);
5833 return poris->address();
5835 else
5836 return os->address() + this->get_output_section_offset(shndx);
5839 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
5840 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5842 template<bool big_endian>
5843 bool
5844 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
5845 const elfcpp::Shdr<32, big_endian>& shdr,
5846 unsigned int shndx,
5847 Output_section* os,
5848 const Symbol_table* symtab)
5850 if (!this->section_is_scannable(shdr, shndx, os, symtab))
5851 return false;
5853 // If the section does not cross any 4K-boundaries, it does not need to
5854 // be scanned.
5855 Arm_address address = this->simple_input_section_output_address(shndx, os);
5856 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
5857 return false;
5859 return true;
5862 // Scan a section for Cortex-A8 workaround.
5864 template<bool big_endian>
5865 void
5866 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
5867 const elfcpp::Shdr<32, big_endian>& shdr,
5868 unsigned int shndx,
5869 Output_section* os,
5870 Target_arm<big_endian>* arm_target)
5872 // Look for the first mapping symbol in this section. It should be
5873 // at (shndx, 0).
5874 Mapping_symbol_position section_start(shndx, 0);
5875 typename Mapping_symbols_info::const_iterator p =
5876 this->mapping_symbols_info_.lower_bound(section_start);
5878 // There are no mapping symbols for this section. Treat it as a data-only
5879 // section. Issue a warning if section is marked as containing
5880 // instructions.
5881 if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
5883 if ((this->section_flags(shndx) & elfcpp::SHF_EXECINSTR) != 0)
5884 gold_warning(_("cannot scan executable section %u of %s for Cortex-A8 "
5885 "erratum because it has no mapping symbols."),
5886 shndx, this->name().c_str());
5887 return;
5890 Arm_address output_address =
5891 this->simple_input_section_output_address(shndx, os);
5893 // Get the section contents.
5894 section_size_type input_view_size = 0;
5895 const unsigned char* input_view =
5896 this->section_contents(shndx, &input_view_size, false);
5898 // We need to go through the mapping symbols to determine what to
5899 // scan. There are two reasons. First, we should look at THUMB code and
5900 // THUMB code only. Second, we only want to look at the 4K-page boundary
5901 // to speed up the scanning.
5903 while (p != this->mapping_symbols_info_.end()
5904 && p->first.first == shndx)
5906 typename Mapping_symbols_info::const_iterator next =
5907 this->mapping_symbols_info_.upper_bound(p->first);
5909 // Only scan part of a section with THUMB code.
5910 if (p->second == 't')
5912 // Determine the end of this range.
5913 section_size_type span_start =
5914 convert_to_section_size_type(p->first.second);
5915 section_size_type span_end;
5916 if (next != this->mapping_symbols_info_.end()
5917 && next->first.first == shndx)
5918 span_end = convert_to_section_size_type(next->first.second);
5919 else
5920 span_end = convert_to_section_size_type(shdr.get_sh_size());
5922 if (((span_start + output_address) & ~0xfffUL)
5923 != ((span_end + output_address - 1) & ~0xfffUL))
5925 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
5926 span_start, span_end,
5927 input_view,
5928 output_address);
5932 p = next;
5936 // Scan relocations for stub generation.
5938 template<bool big_endian>
5939 void
5940 Arm_relobj<big_endian>::scan_sections_for_stubs(
5941 Target_arm<big_endian>* arm_target,
5942 const Symbol_table* symtab,
5943 const Layout* layout)
5945 unsigned int shnum = this->shnum();
5946 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5948 // Read the section headers.
5949 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
5950 shnum * shdr_size,
5951 true, true);
5953 // To speed up processing, we set up hash tables for fast lookup of
5954 // input offsets to output addresses.
5955 this->initialize_input_to_output_maps();
5957 const Relobj::Output_sections& out_sections(this->output_sections());
5959 Relocate_info<32, big_endian> relinfo;
5960 relinfo.symtab = symtab;
5961 relinfo.layout = layout;
5962 relinfo.object = this;
5964 // Do relocation stubs scanning.
5965 const unsigned char* p = pshdrs + shdr_size;
5966 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5968 const elfcpp::Shdr<32, big_endian> shdr(p);
5969 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
5970 pshdrs))
5972 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5973 Arm_address output_offset = this->get_output_section_offset(index);
5974 Arm_address output_address;
5975 if (output_offset != invalid_address)
5976 output_address = out_sections[index]->address() + output_offset;
5977 else
5979 // Currently this only happens for a relaxed section.
5980 const Output_relaxed_input_section* poris =
5981 out_sections[index]->find_relaxed_input_section(this, index);
5982 gold_assert(poris != NULL);
5983 output_address = poris->address();
5986 // Get the relocations.
5987 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
5988 shdr.get_sh_size(),
5989 true, false);
5991 // Get the section contents. This does work for the case in which
5992 // we modify the contents of an input section. We need to pass the
5993 // output view under such circumstances.
5994 section_size_type input_view_size = 0;
5995 const unsigned char* input_view =
5996 this->section_contents(index, &input_view_size, false);
5998 relinfo.reloc_shndx = i;
5999 relinfo.data_shndx = index;
6000 unsigned int sh_type = shdr.get_sh_type();
6001 unsigned int reloc_size;
6002 if (sh_type == elfcpp::SHT_REL)
6003 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6004 else
6005 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6007 Output_section* os = out_sections[index];
6008 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
6009 shdr.get_sh_size() / reloc_size,
6011 output_offset == invalid_address,
6012 input_view, output_address,
6013 input_view_size);
6017 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
6018 // after its relocation section, if there is one, is processed for
6019 // relocation stubs. Merging this loop with the one above would have been
6020 // complicated since we would have had to make sure that relocation stub
6021 // scanning is done first.
6022 if (arm_target->fix_cortex_a8())
6024 const unsigned char* p = pshdrs + shdr_size;
6025 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
6027 const elfcpp::Shdr<32, big_endian> shdr(p);
6028 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
6029 out_sections[i],
6030 symtab))
6031 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
6032 arm_target);
6036 // After we've done the relocations, we release the hash tables,
6037 // since we no longer need them.
6038 this->free_input_to_output_maps();
6041 // Count the local symbols. The ARM backend needs to know if a symbol
6042 // is a THUMB function or not. For global symbols, it is easy because
6043 // the Symbol object keeps the ELF symbol type. For local symbol it is
6044 // harder because we cannot access this information. So we override the
6045 // do_count_local_symbol in parent and scan local symbols to mark
6046 // THUMB functions. This is not the most efficient way but I do not want to
6047 // slow down other ports by calling a per symbol targer hook inside
6048 // Sized_relobj<size, big_endian>::do_count_local_symbols.
6050 template<bool big_endian>
6051 void
6052 Arm_relobj<big_endian>::do_count_local_symbols(
6053 Stringpool_template<char>* pool,
6054 Stringpool_template<char>* dynpool)
6056 // We need to fix-up the values of any local symbols whose type are
6057 // STT_ARM_TFUNC.
6059 // Ask parent to count the local symbols.
6060 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
6061 const unsigned int loccount = this->local_symbol_count();
6062 if (loccount == 0)
6063 return;
6065 // Intialize the thumb function bit-vector.
6066 std::vector<bool> empty_vector(loccount, false);
6067 this->local_symbol_is_thumb_function_.swap(empty_vector);
6069 // Read the symbol table section header.
6070 const unsigned int symtab_shndx = this->symtab_shndx();
6071 elfcpp::Shdr<32, big_endian>
6072 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6073 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6075 // Read the local symbols.
6076 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6077 gold_assert(loccount == symtabshdr.get_sh_info());
6078 off_t locsize = loccount * sym_size;
6079 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6080 locsize, true, true);
6082 // For mapping symbol processing, we need to read the symbol names.
6083 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
6084 if (strtab_shndx >= this->shnum())
6086 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
6087 return;
6090 elfcpp::Shdr<32, big_endian>
6091 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
6092 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
6094 this->error(_("symbol table name section has wrong type: %u"),
6095 static_cast<unsigned int>(strtabshdr.get_sh_type()));
6096 return;
6098 const char* pnames =
6099 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
6100 strtabshdr.get_sh_size(),
6101 false, false));
6103 // Loop over the local symbols and mark any local symbols pointing
6104 // to THUMB functions.
6106 // Skip the first dummy symbol.
6107 psyms += sym_size;
6108 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
6109 this->local_values();
6110 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6112 elfcpp::Sym<32, big_endian> sym(psyms);
6113 elfcpp::STT st_type = sym.get_st_type();
6114 Symbol_value<32>& lv((*plocal_values)[i]);
6115 Arm_address input_value = lv.input_value();
6117 // Check to see if this is a mapping symbol.
6118 const char* sym_name = pnames + sym.get_st_name();
6119 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
6121 bool is_ordinary;
6122 unsigned int input_shndx =
6123 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
6124 gold_assert(is_ordinary);
6126 // Strip of LSB in case this is a THUMB symbol.
6127 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
6128 this->mapping_symbols_info_[msp] = sym_name[1];
6131 if (st_type == elfcpp::STT_ARM_TFUNC
6132 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
6134 // This is a THUMB function. Mark this and canonicalize the
6135 // symbol value by setting LSB.
6136 this->local_symbol_is_thumb_function_[i] = true;
6137 if ((input_value & 1) == 0)
6138 lv.set_input_value(input_value | 1);
6143 // Relocate sections.
6144 template<bool big_endian>
6145 void
6146 Arm_relobj<big_endian>::do_relocate_sections(
6147 const Symbol_table* symtab,
6148 const Layout* layout,
6149 const unsigned char* pshdrs,
6150 typename Sized_relobj<32, big_endian>::Views* pviews)
6152 // Call parent to relocate sections.
6153 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
6154 pviews);
6156 // We do not generate stubs if doing a relocatable link.
6157 if (parameters->options().relocatable())
6158 return;
6160 // Relocate stub tables.
6161 unsigned int shnum = this->shnum();
6163 Target_arm<big_endian>* arm_target =
6164 Target_arm<big_endian>::default_target();
6166 Relocate_info<32, big_endian> relinfo;
6167 relinfo.symtab = symtab;
6168 relinfo.layout = layout;
6169 relinfo.object = this;
6171 for (unsigned int i = 1; i < shnum; ++i)
6173 Arm_input_section<big_endian>* arm_input_section =
6174 arm_target->find_arm_input_section(this, i);
6176 if (arm_input_section != NULL
6177 && arm_input_section->is_stub_table_owner()
6178 && !arm_input_section->stub_table()->empty())
6180 // We cannot discard a section if it owns a stub table.
6181 Output_section* os = this->output_section(i);
6182 gold_assert(os != NULL);
6184 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
6185 relinfo.reloc_shdr = NULL;
6186 relinfo.data_shndx = i;
6187 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
6189 gold_assert((*pviews)[i].view != NULL);
6191 // We are passed the output section view. Adjust it to cover the
6192 // stub table only.
6193 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
6194 gold_assert((stub_table->address() >= (*pviews)[i].address)
6195 && ((stub_table->address() + stub_table->data_size())
6196 <= (*pviews)[i].address + (*pviews)[i].view_size));
6198 off_t offset = stub_table->address() - (*pviews)[i].address;
6199 unsigned char* view = (*pviews)[i].view + offset;
6200 Arm_address address = stub_table->address();
6201 section_size_type view_size = stub_table->data_size();
6203 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
6204 view_size);
6207 // Apply Cortex A8 workaround if applicable.
6208 if (this->section_has_cortex_a8_workaround(i))
6210 unsigned char* view = (*pviews)[i].view;
6211 Arm_address view_address = (*pviews)[i].address;
6212 section_size_type view_size = (*pviews)[i].view_size;
6213 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
6215 // Adjust view to cover section.
6216 Output_section* os = this->output_section(i);
6217 gold_assert(os != NULL);
6218 Arm_address section_address =
6219 this->simple_input_section_output_address(i, os);
6220 uint64_t section_size = this->section_size(i);
6222 gold_assert(section_address >= view_address
6223 && ((section_address + section_size)
6224 <= (view_address + view_size)));
6226 unsigned char* section_view = view + (section_address - view_address);
6228 // Apply the Cortex-A8 workaround to the output address range
6229 // corresponding to this input section.
6230 stub_table->apply_cortex_a8_workaround_to_address_range(
6231 arm_target,
6232 section_view,
6233 section_address,
6234 section_size);
6239 // Find the linked text section of an EXIDX section by looking the the first
6240 // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6241 // must be linked to to its associated code section via the sh_link field of
6242 // its section header. However, some tools are broken and the link is not
6243 // always set. LD just drops such an EXIDX section silently, causing the
6244 // associated code not unwindabled. Here we try a little bit harder to
6245 // discover the linked code section.
6247 // PSHDR points to the section header of a relocation section of an EXIDX
6248 // section. If we can find a linked text section, return true and
6249 // store the text section index in the location PSHNDX. Otherwise
6250 // return false.
6252 template<bool big_endian>
6253 bool
6254 Arm_relobj<big_endian>::find_linked_text_section(
6255 const unsigned char* pshdr,
6256 const unsigned char* psyms,
6257 unsigned int* pshndx)
6259 elfcpp::Shdr<32, big_endian> shdr(pshdr);
6261 // If there is no relocation, we cannot find the linked text section.
6262 size_t reloc_size;
6263 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6264 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6265 else
6266 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6267 size_t reloc_count = shdr.get_sh_size() / reloc_size;
6269 // Get the relocations.
6270 const unsigned char* prelocs =
6271 this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false);
6273 // Find the REL31 relocation for the first word of the first EXIDX entry.
6274 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6276 Arm_address r_offset;
6277 typename elfcpp::Elf_types<32>::Elf_WXword r_info;
6278 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6280 typename elfcpp::Rel<32, big_endian> reloc(prelocs);
6281 r_info = reloc.get_r_info();
6282 r_offset = reloc.get_r_offset();
6284 else
6286 typename elfcpp::Rela<32, big_endian> reloc(prelocs);
6287 r_info = reloc.get_r_info();
6288 r_offset = reloc.get_r_offset();
6291 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6292 if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
6293 continue;
6295 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6296 if (r_sym == 0
6297 || r_sym >= this->local_symbol_count()
6298 || r_offset != 0)
6299 continue;
6301 // This is the relocation for the first word of the first EXIDX entry.
6302 // We expect to see a local section symbol.
6303 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6304 elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
6305 if (sym.get_st_type() == elfcpp::STT_SECTION)
6307 bool is_ordinary;
6308 *pshndx =
6309 this->adjust_sym_shndx(r_sym, sym.get_st_shndx(), &is_ordinary);
6310 gold_assert(is_ordinary);
6311 return true;
6313 else
6314 return false;
6317 return false;
6320 // Make an EXIDX input section object for an EXIDX section whose index is
6321 // SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
6322 // is the section index of the linked text section.
6324 template<bool big_endian>
6325 void
6326 Arm_relobj<big_endian>::make_exidx_input_section(
6327 unsigned int shndx,
6328 const elfcpp::Shdr<32, big_endian>& shdr,
6329 unsigned int text_shndx)
6331 // Issue an error and ignore this EXIDX section if it points to a text
6332 // section already has an EXIDX section.
6333 if (this->exidx_section_map_[text_shndx] != NULL)
6335 gold_error(_("EXIDX sections %u and %u both link to text section %u "
6336 "in %s"),
6337 shndx, this->exidx_section_map_[text_shndx]->shndx(),
6338 text_shndx, this->name().c_str());
6339 return;
6342 // Create an Arm_exidx_input_section object for this EXIDX section.
6343 Arm_exidx_input_section* exidx_input_section =
6344 new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
6345 shdr.get_sh_addralign());
6346 this->exidx_section_map_[text_shndx] = exidx_input_section;
6348 // Also map the EXIDX section index to this.
6349 gold_assert(this->exidx_section_map_[shndx] == NULL);
6350 this->exidx_section_map_[shndx] = exidx_input_section;
6353 // Read the symbol information.
6355 template<bool big_endian>
6356 void
6357 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6359 // Call parent class to read symbol information.
6360 Sized_relobj<32, big_endian>::do_read_symbols(sd);
6362 // If this input file is a binary file, it has no processor
6363 // specific flags and attributes section.
6364 Input_file::Format format = this->input_file()->format();
6365 if (format != Input_file::FORMAT_ELF)
6367 gold_assert(format == Input_file::FORMAT_BINARY);
6368 this->merge_flags_and_attributes_ = false;
6369 return;
6372 // Read processor-specific flags in ELF file header.
6373 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6374 elfcpp::Elf_sizes<32>::ehdr_size,
6375 true, false);
6376 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6377 this->processor_specific_flags_ = ehdr.get_e_flags();
6379 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6380 // sections.
6381 std::vector<unsigned int> deferred_exidx_sections;
6382 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6383 const unsigned char* pshdrs = sd->section_headers->data();
6384 const unsigned char *ps = pshdrs + shdr_size;
6385 bool must_merge_flags_and_attributes = false;
6386 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6388 elfcpp::Shdr<32, big_endian> shdr(ps);
6390 // Sometimes an object has no contents except the section name string
6391 // table and an empty symbol table with the undefined symbol. We
6392 // don't want to merge processor-specific flags from such an object.
6393 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6395 // Symbol table is not empty.
6396 const elfcpp::Elf_types<32>::Elf_WXword sym_size =
6397 elfcpp::Elf_sizes<32>::sym_size;
6398 if (shdr.get_sh_size() > sym_size)
6399 must_merge_flags_and_attributes = true;
6401 else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6402 // If this is neither an empty symbol table nor a string table,
6403 // be conservative.
6404 must_merge_flags_and_attributes = true;
6406 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6408 gold_assert(this->attributes_section_data_ == NULL);
6409 section_offset_type section_offset = shdr.get_sh_offset();
6410 section_size_type section_size =
6411 convert_to_section_size_type(shdr.get_sh_size());
6412 File_view* view = this->get_lasting_view(section_offset,
6413 section_size, true, false);
6414 this->attributes_section_data_ =
6415 new Attributes_section_data(view->data(), section_size);
6417 else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6419 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6420 if (text_shndx >= this->shnum())
6421 gold_error(_("EXIDX section %u linked to invalid section %u"),
6422 i, text_shndx);
6423 else if (text_shndx == elfcpp::SHN_UNDEF)
6424 deferred_exidx_sections.push_back(i);
6425 else
6426 this->make_exidx_input_section(i, shdr, text_shndx);
6430 // This is rare.
6431 if (!must_merge_flags_and_attributes)
6433 this->merge_flags_and_attributes_ = false;
6434 return;
6437 // Some tools are broken and they do not set the link of EXIDX sections.
6438 // We look at the first relocation to figure out the linked sections.
6439 if (!deferred_exidx_sections.empty())
6441 // We need to go over the section headers again to find the mapping
6442 // from sections being relocated to their relocation sections. This is
6443 // a bit inefficient as we could do that in the loop above. However,
6444 // we do not expect any deferred EXIDX sections normally. So we do not
6445 // want to slow down the most common path.
6446 typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
6447 Reloc_map reloc_map;
6448 ps = pshdrs + shdr_size;
6449 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6451 elfcpp::Shdr<32, big_endian> shdr(ps);
6452 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
6453 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
6455 unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
6456 if (info_shndx >= this->shnum())
6457 gold_error(_("relocation section %u has invalid info %u"),
6458 i, info_shndx);
6459 Reloc_map::value_type value(info_shndx, i);
6460 std::pair<Reloc_map::iterator, bool> result =
6461 reloc_map.insert(value);
6462 if (!result.second)
6463 gold_error(_("section %u has multiple relocation sections "
6464 "%u and %u"),
6465 info_shndx, i, reloc_map[info_shndx]);
6469 // Read the symbol table section header.
6470 const unsigned int symtab_shndx = this->symtab_shndx();
6471 elfcpp::Shdr<32, big_endian>
6472 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6473 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6475 // Read the local symbols.
6476 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6477 const unsigned int loccount = this->local_symbol_count();
6478 gold_assert(loccount == symtabshdr.get_sh_info());
6479 off_t locsize = loccount * sym_size;
6480 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6481 locsize, true, true);
6483 // Process the deferred EXIDX sections.
6484 for(unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
6486 unsigned int shndx = deferred_exidx_sections[i];
6487 elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
6488 unsigned int text_shndx;
6489 Reloc_map::const_iterator it = reloc_map.find(shndx);
6490 if (it != reloc_map.end()
6491 && find_linked_text_section(pshdrs + it->second * shdr_size,
6492 psyms, &text_shndx))
6493 this->make_exidx_input_section(shndx, shdr, text_shndx);
6494 else
6495 gold_error(_("EXIDX section %u has no linked text section."),
6496 shndx);
6501 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
6502 // sections for unwinding. These sections are referenced implicitly by
6503 // text sections linked in the section headers. If we ignore these implict
6504 // references, the .ARM.exidx sections and any .ARM.extab sections they use
6505 // will be garbage-collected incorrectly. Hence we override the same function
6506 // in the base class to handle these implicit references.
6508 template<bool big_endian>
6509 void
6510 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
6511 Layout* layout,
6512 Read_relocs_data* rd)
6514 // First, call base class method to process relocations in this object.
6515 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
6517 // If --gc-sections is not specified, there is nothing more to do.
6518 // This happens when --icf is used but --gc-sections is not.
6519 if (!parameters->options().gc_sections())
6520 return;
6522 unsigned int shnum = this->shnum();
6523 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6524 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6525 shnum * shdr_size,
6526 true, true);
6528 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
6529 // to these from the linked text sections.
6530 const unsigned char* ps = pshdrs + shdr_size;
6531 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
6533 elfcpp::Shdr<32, big_endian> shdr(ps);
6534 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6536 // Found an .ARM.exidx section, add it to the set of reachable
6537 // sections from its linked text section.
6538 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6539 symtab->gc()->add_reference(this, text_shndx, this, i);
6544 // Update output local symbol count. Owing to EXIDX entry merging, some local
6545 // symbols will be removed in output. Adjust output local symbol count
6546 // accordingly. We can only changed the static output local symbol count. It
6547 // is too late to change the dynamic symbols.
6549 template<bool big_endian>
6550 void
6551 Arm_relobj<big_endian>::update_output_local_symbol_count()
6553 // Caller should check that this needs updating. We want caller checking
6554 // because output_local_symbol_count_needs_update() is most likely inlined.
6555 gold_assert(this->output_local_symbol_count_needs_update_);
6557 gold_assert(this->symtab_shndx() != -1U);
6558 if (this->symtab_shndx() == 0)
6560 // This object has no symbols. Weird but legal.
6561 return;
6564 // Read the symbol table section header.
6565 const unsigned int symtab_shndx = this->symtab_shndx();
6566 elfcpp::Shdr<32, big_endian>
6567 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6568 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6570 // Read the local symbols.
6571 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6572 const unsigned int loccount = this->local_symbol_count();
6573 gold_assert(loccount == symtabshdr.get_sh_info());
6574 off_t locsize = loccount * sym_size;
6575 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6576 locsize, true, true);
6578 // Loop over the local symbols.
6580 typedef typename Sized_relobj<32, big_endian>::Output_sections
6581 Output_sections;
6582 const Output_sections& out_sections(this->output_sections());
6583 unsigned int shnum = this->shnum();
6584 unsigned int count = 0;
6585 // Skip the first, dummy, symbol.
6586 psyms += sym_size;
6587 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6589 elfcpp::Sym<32, big_endian> sym(psyms);
6591 Symbol_value<32>& lv((*this->local_values())[i]);
6593 // This local symbol was already discarded by do_count_local_symbols.
6594 if (lv.is_output_symtab_index_set() && !lv.has_output_symtab_entry())
6595 continue;
6597 bool is_ordinary;
6598 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
6599 &is_ordinary);
6601 if (shndx < shnum)
6603 Output_section* os = out_sections[shndx];
6605 // This local symbol no longer has an output section. Discard it.
6606 if (os == NULL)
6608 lv.set_no_output_symtab_entry();
6609 continue;
6612 // Currently we only discard parts of EXIDX input sections.
6613 // We explicitly check for a merged EXIDX input section to avoid
6614 // calling Output_section_data::output_offset unless necessary.
6615 if ((this->get_output_section_offset(shndx) == invalid_address)
6616 && (this->exidx_input_section_by_shndx(shndx) != NULL))
6618 section_offset_type output_offset =
6619 os->output_offset(this, shndx, lv.input_value());
6620 if (output_offset == -1)
6622 // This symbol is defined in a part of an EXIDX input section
6623 // that is discarded due to entry merging.
6624 lv.set_no_output_symtab_entry();
6625 continue;
6630 ++count;
6633 this->set_output_local_symbol_count(count);
6634 this->output_local_symbol_count_needs_update_ = false;
6637 // Arm_dynobj methods.
6639 // Read the symbol information.
6641 template<bool big_endian>
6642 void
6643 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6645 // Call parent class to read symbol information.
6646 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
6648 // Read processor-specific flags in ELF file header.
6649 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6650 elfcpp::Elf_sizes<32>::ehdr_size,
6651 true, false);
6652 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6653 this->processor_specific_flags_ = ehdr.get_e_flags();
6655 // Read the attributes section if there is one.
6656 // We read from the end because gas seems to put it near the end of
6657 // the section headers.
6658 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6659 const unsigned char *ps =
6660 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
6661 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
6663 elfcpp::Shdr<32, big_endian> shdr(ps);
6664 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6666 section_offset_type section_offset = shdr.get_sh_offset();
6667 section_size_type section_size =
6668 convert_to_section_size_type(shdr.get_sh_size());
6669 File_view* view = this->get_lasting_view(section_offset,
6670 section_size, true, false);
6671 this->attributes_section_data_ =
6672 new Attributes_section_data(view->data(), section_size);
6673 break;
6678 // Stub_addend_reader methods.
6680 // Read the addend of a REL relocation of type R_TYPE at VIEW.
6682 template<bool big_endian>
6683 elfcpp::Elf_types<32>::Elf_Swxword
6684 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
6685 unsigned int r_type,
6686 const unsigned char* view,
6687 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
6689 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
6691 switch (r_type)
6693 case elfcpp::R_ARM_CALL:
6694 case elfcpp::R_ARM_JUMP24:
6695 case elfcpp::R_ARM_PLT32:
6697 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6698 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6699 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
6700 return utils::sign_extend<26>(val << 2);
6703 case elfcpp::R_ARM_THM_CALL:
6704 case elfcpp::R_ARM_THM_JUMP24:
6705 case elfcpp::R_ARM_THM_XPC22:
6707 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6708 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6709 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6710 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6711 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
6714 case elfcpp::R_ARM_THM_JUMP19:
6716 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6717 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6718 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6719 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6720 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
6723 default:
6724 gold_unreachable();
6728 // Arm_output_data_got methods.
6730 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
6731 // The first one is initialized to be 1, which is the module index for
6732 // the main executable and the second one 0. A reloc of the type
6733 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
6734 // be applied by gold. GSYM is a global symbol.
6736 template<bool big_endian>
6737 void
6738 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
6739 unsigned int got_type,
6740 Symbol* gsym)
6742 if (gsym->has_got_offset(got_type))
6743 return;
6745 // We are doing a static link. Just mark it as belong to module 1,
6746 // the executable.
6747 unsigned int got_offset = this->add_constant(1);
6748 gsym->set_got_offset(got_type, got_offset);
6749 got_offset = this->add_constant(0);
6750 this->static_relocs_.push_back(Static_reloc(got_offset,
6751 elfcpp::R_ARM_TLS_DTPOFF32,
6752 gsym));
6755 // Same as the above but for a local symbol.
6757 template<bool big_endian>
6758 void
6759 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
6760 unsigned int got_type,
6761 Sized_relobj<32, big_endian>* object,
6762 unsigned int index)
6764 if (object->local_has_got_offset(index, got_type))
6765 return;
6767 // We are doing a static link. Just mark it as belong to module 1,
6768 // the executable.
6769 unsigned int got_offset = this->add_constant(1);
6770 object->set_local_got_offset(index, got_type, got_offset);
6771 got_offset = this->add_constant(0);
6772 this->static_relocs_.push_back(Static_reloc(got_offset,
6773 elfcpp::R_ARM_TLS_DTPOFF32,
6774 object, index));
6777 template<bool big_endian>
6778 void
6779 Arm_output_data_got<big_endian>::do_write(Output_file* of)
6781 // Call parent to write out GOT.
6782 Output_data_got<32, big_endian>::do_write(of);
6784 // We are done if there is no fix up.
6785 if (this->static_relocs_.empty())
6786 return;
6788 gold_assert(parameters->doing_static_link());
6790 const off_t offset = this->offset();
6791 const section_size_type oview_size =
6792 convert_to_section_size_type(this->data_size());
6793 unsigned char* const oview = of->get_output_view(offset, oview_size);
6795 Output_segment* tls_segment = this->layout_->tls_segment();
6796 gold_assert(tls_segment != NULL);
6798 // The thread pointer $tp points to the TCB, which is followed by the
6799 // TLS. So we need to adjust $tp relative addressing by this amount.
6800 Arm_address aligned_tcb_size =
6801 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
6803 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6805 Static_reloc& reloc(this->static_relocs_[i]);
6807 Arm_address value;
6808 if (!reloc.symbol_is_global())
6810 Sized_relobj<32, big_endian>* object = reloc.relobj();
6811 const Symbol_value<32>* psymval =
6812 reloc.relobj()->local_symbol(reloc.index());
6814 // We are doing static linking. Issue an error and skip this
6815 // relocation if the symbol is undefined or in a discarded_section.
6816 bool is_ordinary;
6817 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6818 if ((shndx == elfcpp::SHN_UNDEF)
6819 || (is_ordinary
6820 && shndx != elfcpp::SHN_UNDEF
6821 && !object->is_section_included(shndx)
6822 && !this->symbol_table_->is_section_folded(object, shndx)))
6824 gold_error(_("undefined or discarded local symbol %u from "
6825 " object %s in GOT"),
6826 reloc.index(), reloc.relobj()->name().c_str());
6827 continue;
6830 value = psymval->value(object, 0);
6832 else
6834 const Symbol* gsym = reloc.symbol();
6835 gold_assert(gsym != NULL);
6836 if (gsym->is_forwarder())
6837 gsym = this->symbol_table_->resolve_forwards(gsym);
6839 // We are doing static linking. Issue an error and skip this
6840 // relocation if the symbol is undefined or in a discarded_section
6841 // unless it is a weakly_undefined symbol.
6842 if ((gsym->is_defined_in_discarded_section()
6843 || gsym->is_undefined())
6844 && !gsym->is_weak_undefined())
6846 gold_error(_("undefined or discarded symbol %s in GOT"),
6847 gsym->name());
6848 continue;
6851 if (!gsym->is_weak_undefined())
6853 const Sized_symbol<32>* sym =
6854 static_cast<const Sized_symbol<32>*>(gsym);
6855 value = sym->value();
6857 else
6858 value = 0;
6861 unsigned got_offset = reloc.got_offset();
6862 gold_assert(got_offset < oview_size);
6864 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6865 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6866 Valtype x;
6867 switch (reloc.r_type())
6869 case elfcpp::R_ARM_TLS_DTPOFF32:
6870 x = value;
6871 break;
6872 case elfcpp::R_ARM_TLS_TPOFF32:
6873 x = value + aligned_tcb_size;
6874 break;
6875 default:
6876 gold_unreachable();
6878 elfcpp::Swap<32, big_endian>::writeval(wv, x);
6881 of->write_output_view(offset, oview_size, oview);
6884 // A class to handle the PLT data.
6886 template<bool big_endian>
6887 class Output_data_plt_arm : public Output_section_data
6889 public:
6890 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
6891 Reloc_section;
6893 Output_data_plt_arm(Layout*, Output_data_space*);
6895 // Add an entry to the PLT.
6896 void
6897 add_entry(Symbol* gsym);
6899 // Return the .rel.plt section data.
6900 const Reloc_section*
6901 rel_plt() const
6902 { return this->rel_; }
6904 protected:
6905 void
6906 do_adjust_output_section(Output_section* os);
6908 // Write to a map file.
6909 void
6910 do_print_to_mapfile(Mapfile* mapfile) const
6911 { mapfile->print_output_data(this, _("** PLT")); }
6913 private:
6914 // Template for the first PLT entry.
6915 static const uint32_t first_plt_entry[5];
6917 // Template for subsequent PLT entries.
6918 static const uint32_t plt_entry[3];
6920 // Set the final size.
6921 void
6922 set_final_data_size()
6924 this->set_data_size(sizeof(first_plt_entry)
6925 + this->count_ * sizeof(plt_entry));
6928 // Write out the PLT data.
6929 void
6930 do_write(Output_file*);
6932 // The reloc section.
6933 Reloc_section* rel_;
6934 // The .got.plt section.
6935 Output_data_space* got_plt_;
6936 // The number of PLT entries.
6937 unsigned int count_;
6940 // Create the PLT section. The ordinary .got section is an argument,
6941 // since we need to refer to the start. We also create our own .got
6942 // section just for PLT entries.
6944 template<bool big_endian>
6945 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
6946 Output_data_space* got_plt)
6947 : Output_section_data(4), got_plt_(got_plt), count_(0)
6949 this->rel_ = new Reloc_section(false);
6950 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
6951 elfcpp::SHF_ALLOC, this->rel_, true, false,
6952 false, false);
6955 template<bool big_endian>
6956 void
6957 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
6959 os->set_entsize(0);
6962 // Add an entry to the PLT.
6964 template<bool big_endian>
6965 void
6966 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
6968 gold_assert(!gsym->has_plt_offset());
6970 // Note that when setting the PLT offset we skip the initial
6971 // reserved PLT entry.
6972 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
6973 + sizeof(first_plt_entry));
6975 ++this->count_;
6977 section_offset_type got_offset = this->got_plt_->current_data_size();
6979 // Every PLT entry needs a GOT entry which points back to the PLT
6980 // entry (this will be changed by the dynamic linker, normally
6981 // lazily when the function is called).
6982 this->got_plt_->set_current_data_size(got_offset + 4);
6984 // Every PLT entry needs a reloc.
6985 gsym->set_needs_dynsym_entry();
6986 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
6987 got_offset);
6989 // Note that we don't need to save the symbol. The contents of the
6990 // PLT are independent of which symbols are used. The symbols only
6991 // appear in the relocations.
6994 // ARM PLTs.
6995 // FIXME: This is not very flexible. Right now this has only been tested
6996 // on armv5te. If we are to support additional architecture features like
6997 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
6999 // The first entry in the PLT.
7000 template<bool big_endian>
7001 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
7003 0xe52de004, // str lr, [sp, #-4]!
7004 0xe59fe004, // ldr lr, [pc, #4]
7005 0xe08fe00e, // add lr, pc, lr
7006 0xe5bef008, // ldr pc, [lr, #8]!
7007 0x00000000, // &GOT[0] - .
7010 // Subsequent entries in the PLT.
7012 template<bool big_endian>
7013 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
7015 0xe28fc600, // add ip, pc, #0xNN00000
7016 0xe28cca00, // add ip, ip, #0xNN000
7017 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
7020 // Write out the PLT. This uses the hand-coded instructions above,
7021 // and adjusts them as needed. This is all specified by the arm ELF
7022 // Processor Supplement.
7024 template<bool big_endian>
7025 void
7026 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
7028 const off_t offset = this->offset();
7029 const section_size_type oview_size =
7030 convert_to_section_size_type(this->data_size());
7031 unsigned char* const oview = of->get_output_view(offset, oview_size);
7033 const off_t got_file_offset = this->got_plt_->offset();
7034 const section_size_type got_size =
7035 convert_to_section_size_type(this->got_plt_->data_size());
7036 unsigned char* const got_view = of->get_output_view(got_file_offset,
7037 got_size);
7038 unsigned char* pov = oview;
7040 Arm_address plt_address = this->address();
7041 Arm_address got_address = this->got_plt_->address();
7043 // Write first PLT entry. All but the last word are constants.
7044 const size_t num_first_plt_words = (sizeof(first_plt_entry)
7045 / sizeof(plt_entry[0]));
7046 for (size_t i = 0; i < num_first_plt_words - 1; i++)
7047 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
7048 // Last word in first PLT entry is &GOT[0] - .
7049 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
7050 got_address - (plt_address + 16));
7051 pov += sizeof(first_plt_entry);
7053 unsigned char* got_pov = got_view;
7055 memset(got_pov, 0, 12);
7056 got_pov += 12;
7058 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
7059 unsigned int plt_offset = sizeof(first_plt_entry);
7060 unsigned int plt_rel_offset = 0;
7061 unsigned int got_offset = 12;
7062 const unsigned int count = this->count_;
7063 for (unsigned int i = 0;
7064 i < count;
7065 ++i,
7066 pov += sizeof(plt_entry),
7067 got_pov += 4,
7068 plt_offset += sizeof(plt_entry),
7069 plt_rel_offset += rel_size,
7070 got_offset += 4)
7072 // Set and adjust the PLT entry itself.
7073 int32_t offset = ((got_address + got_offset)
7074 - (plt_address + plt_offset + 8));
7076 gold_assert(offset >= 0 && offset < 0x0fffffff);
7077 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
7078 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
7079 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
7080 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
7081 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
7082 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
7084 // Set the entry in the GOT.
7085 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
7088 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
7089 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
7091 of->write_output_view(offset, oview_size, oview);
7092 of->write_output_view(got_file_offset, got_size, got_view);
7095 // Create a PLT entry for a global symbol.
7097 template<bool big_endian>
7098 void
7099 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
7100 Symbol* gsym)
7102 if (gsym->has_plt_offset())
7103 return;
7105 if (this->plt_ == NULL)
7107 // Create the GOT sections first.
7108 this->got_section(symtab, layout);
7110 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
7111 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
7112 (elfcpp::SHF_ALLOC
7113 | elfcpp::SHF_EXECINSTR),
7114 this->plt_, false, false, false, false);
7116 this->plt_->add_entry(gsym);
7119 // Get the section to use for TLS_DESC relocations.
7121 template<bool big_endian>
7122 typename Target_arm<big_endian>::Reloc_section*
7123 Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
7125 return this->plt_section()->rel_tls_desc(layout);
7128 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
7130 template<bool big_endian>
7131 void
7132 Target_arm<big_endian>::define_tls_base_symbol(
7133 Symbol_table* symtab,
7134 Layout* layout)
7136 if (this->tls_base_symbol_defined_)
7137 return;
7139 Output_segment* tls_segment = layout->tls_segment();
7140 if (tls_segment != NULL)
7142 bool is_exec = parameters->options().output_is_executable();
7143 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
7144 Symbol_table::PREDEFINED,
7145 tls_segment, 0, 0,
7146 elfcpp::STT_TLS,
7147 elfcpp::STB_LOCAL,
7148 elfcpp::STV_HIDDEN, 0,
7149 (is_exec
7150 ? Symbol::SEGMENT_END
7151 : Symbol::SEGMENT_START),
7152 true);
7154 this->tls_base_symbol_defined_ = true;
7157 // Create a GOT entry for the TLS module index.
7159 template<bool big_endian>
7160 unsigned int
7161 Target_arm<big_endian>::got_mod_index_entry(
7162 Symbol_table* symtab,
7163 Layout* layout,
7164 Sized_relobj<32, big_endian>* object)
7166 if (this->got_mod_index_offset_ == -1U)
7168 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7169 Arm_output_data_got<big_endian>* got = this->got_section(symtab, layout);
7170 unsigned int got_offset;
7171 if (!parameters->doing_static_link())
7173 got_offset = got->add_constant(0);
7174 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
7175 rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
7176 got_offset);
7178 else
7180 // We are doing a static link. Just mark it as belong to module 1,
7181 // the executable.
7182 got_offset = got->add_constant(1);
7185 got->add_constant(0);
7186 this->got_mod_index_offset_ = got_offset;
7188 return this->got_mod_index_offset_;
7191 // Optimize the TLS relocation type based on what we know about the
7192 // symbol. IS_FINAL is true if the final address of this symbol is
7193 // known at link time.
7195 template<bool big_endian>
7196 tls::Tls_optimization
7197 Target_arm<big_endian>::optimize_tls_reloc(bool, int)
7199 // FIXME: Currently we do not do any TLS optimization.
7200 return tls::TLSOPT_NONE;
7203 // Report an unsupported relocation against a local symbol.
7205 template<bool big_endian>
7206 void
7207 Target_arm<big_endian>::Scan::unsupported_reloc_local(
7208 Sized_relobj<32, big_endian>* object,
7209 unsigned int r_type)
7211 gold_error(_("%s: unsupported reloc %u against local symbol"),
7212 object->name().c_str(), r_type);
7215 // We are about to emit a dynamic relocation of type R_TYPE. If the
7216 // dynamic linker does not support it, issue an error. The GNU linker
7217 // only issues a non-PIC error for an allocated read-only section.
7218 // Here we know the section is allocated, but we don't know that it is
7219 // read-only. But we check for all the relocation types which the
7220 // glibc dynamic linker supports, so it seems appropriate to issue an
7221 // error even if the section is not read-only.
7223 template<bool big_endian>
7224 void
7225 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
7226 unsigned int r_type)
7228 switch (r_type)
7230 // These are the relocation types supported by glibc for ARM.
7231 case elfcpp::R_ARM_RELATIVE:
7232 case elfcpp::R_ARM_COPY:
7233 case elfcpp::R_ARM_GLOB_DAT:
7234 case elfcpp::R_ARM_JUMP_SLOT:
7235 case elfcpp::R_ARM_ABS32:
7236 case elfcpp::R_ARM_ABS32_NOI:
7237 case elfcpp::R_ARM_PC24:
7238 // FIXME: The following 3 types are not supported by Android's dynamic
7239 // linker.
7240 case elfcpp::R_ARM_TLS_DTPMOD32:
7241 case elfcpp::R_ARM_TLS_DTPOFF32:
7242 case elfcpp::R_ARM_TLS_TPOFF32:
7243 return;
7245 default:
7247 // This prevents us from issuing more than one error per reloc
7248 // section. But we can still wind up issuing more than one
7249 // error per object file.
7250 if (this->issued_non_pic_error_)
7251 return;
7252 const Arm_reloc_property* reloc_property =
7253 arm_reloc_property_table->get_reloc_property(r_type);
7254 gold_assert(reloc_property != NULL);
7255 object->error(_("requires unsupported dynamic reloc %s; "
7256 "recompile with -fPIC"),
7257 reloc_property->name().c_str());
7258 this->issued_non_pic_error_ = true;
7259 return;
7262 case elfcpp::R_ARM_NONE:
7263 gold_unreachable();
7267 // Scan a relocation for a local symbol.
7268 // FIXME: This only handles a subset of relocation types used by Android
7269 // on ARM v5te devices.
7271 template<bool big_endian>
7272 inline void
7273 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
7274 Layout* layout,
7275 Target_arm* target,
7276 Sized_relobj<32, big_endian>* object,
7277 unsigned int data_shndx,
7278 Output_section* output_section,
7279 const elfcpp::Rel<32, big_endian>& reloc,
7280 unsigned int r_type,
7281 const elfcpp::Sym<32, big_endian>& lsym)
7283 r_type = get_real_reloc_type(r_type);
7284 switch (r_type)
7286 case elfcpp::R_ARM_NONE:
7287 case elfcpp::R_ARM_V4BX:
7288 case elfcpp::R_ARM_GNU_VTENTRY:
7289 case elfcpp::R_ARM_GNU_VTINHERIT:
7290 break;
7292 case elfcpp::R_ARM_ABS32:
7293 case elfcpp::R_ARM_ABS32_NOI:
7294 // If building a shared library (or a position-independent
7295 // executable), we need to create a dynamic relocation for
7296 // this location. The relocation applied at link time will
7297 // apply the link-time value, so we flag the location with
7298 // an R_ARM_RELATIVE relocation so the dynamic loader can
7299 // relocate it easily.
7300 if (parameters->options().output_is_position_independent())
7302 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7303 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7304 // If we are to add more other reloc types than R_ARM_ABS32,
7305 // we need to add check_non_pic(object, r_type) here.
7306 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
7307 output_section, data_shndx,
7308 reloc.get_r_offset());
7310 break;
7312 case elfcpp::R_ARM_ABS16:
7313 case elfcpp::R_ARM_ABS12:
7314 case elfcpp::R_ARM_THM_ABS5:
7315 case elfcpp::R_ARM_ABS8:
7316 case elfcpp::R_ARM_BASE_ABS:
7317 case elfcpp::R_ARM_MOVW_ABS_NC:
7318 case elfcpp::R_ARM_MOVT_ABS:
7319 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7320 case elfcpp::R_ARM_THM_MOVT_ABS:
7321 // If building a shared library (or a position-independent
7322 // executable), we need to create a dynamic relocation for
7323 // this location. Because the addend needs to remain in the
7324 // data section, we need to be careful not to apply this
7325 // relocation statically.
7326 if (parameters->options().output_is_position_independent())
7328 check_non_pic(object, r_type);
7329 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7330 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7331 if (lsym.get_st_type() != elfcpp::STT_SECTION)
7332 rel_dyn->add_local(object, r_sym, r_type, output_section,
7333 data_shndx, reloc.get_r_offset());
7334 else
7336 gold_assert(lsym.get_st_value() == 0);
7337 unsigned int shndx = lsym.get_st_shndx();
7338 bool is_ordinary;
7339 shndx = object->adjust_sym_shndx(r_sym, shndx,
7340 &is_ordinary);
7341 if (!is_ordinary)
7342 object->error(_("section symbol %u has bad shndx %u"),
7343 r_sym, shndx);
7344 else
7345 rel_dyn->add_local_section(object, shndx,
7346 r_type, output_section,
7347 data_shndx, reloc.get_r_offset());
7350 break;
7352 case elfcpp::R_ARM_PC24:
7353 case elfcpp::R_ARM_REL32:
7354 case elfcpp::R_ARM_LDR_PC_G0:
7355 case elfcpp::R_ARM_SBREL32:
7356 case elfcpp::R_ARM_THM_CALL:
7357 case elfcpp::R_ARM_THM_PC8:
7358 case elfcpp::R_ARM_BASE_PREL:
7359 case elfcpp::R_ARM_PLT32:
7360 case elfcpp::R_ARM_CALL:
7361 case elfcpp::R_ARM_JUMP24:
7362 case elfcpp::R_ARM_THM_JUMP24:
7363 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7364 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7365 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7366 case elfcpp::R_ARM_SBREL31:
7367 case elfcpp::R_ARM_PREL31:
7368 case elfcpp::R_ARM_MOVW_PREL_NC:
7369 case elfcpp::R_ARM_MOVT_PREL:
7370 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7371 case elfcpp::R_ARM_THM_MOVT_PREL:
7372 case elfcpp::R_ARM_THM_JUMP19:
7373 case elfcpp::R_ARM_THM_JUMP6:
7374 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7375 case elfcpp::R_ARM_THM_PC12:
7376 case elfcpp::R_ARM_REL32_NOI:
7377 case elfcpp::R_ARM_ALU_PC_G0_NC:
7378 case elfcpp::R_ARM_ALU_PC_G0:
7379 case elfcpp::R_ARM_ALU_PC_G1_NC:
7380 case elfcpp::R_ARM_ALU_PC_G1:
7381 case elfcpp::R_ARM_ALU_PC_G2:
7382 case elfcpp::R_ARM_LDR_PC_G1:
7383 case elfcpp::R_ARM_LDR_PC_G2:
7384 case elfcpp::R_ARM_LDRS_PC_G0:
7385 case elfcpp::R_ARM_LDRS_PC_G1:
7386 case elfcpp::R_ARM_LDRS_PC_G2:
7387 case elfcpp::R_ARM_LDC_PC_G0:
7388 case elfcpp::R_ARM_LDC_PC_G1:
7389 case elfcpp::R_ARM_LDC_PC_G2:
7390 case elfcpp::R_ARM_ALU_SB_G0_NC:
7391 case elfcpp::R_ARM_ALU_SB_G0:
7392 case elfcpp::R_ARM_ALU_SB_G1_NC:
7393 case elfcpp::R_ARM_ALU_SB_G1:
7394 case elfcpp::R_ARM_ALU_SB_G2:
7395 case elfcpp::R_ARM_LDR_SB_G0:
7396 case elfcpp::R_ARM_LDR_SB_G1:
7397 case elfcpp::R_ARM_LDR_SB_G2:
7398 case elfcpp::R_ARM_LDRS_SB_G0:
7399 case elfcpp::R_ARM_LDRS_SB_G1:
7400 case elfcpp::R_ARM_LDRS_SB_G2:
7401 case elfcpp::R_ARM_LDC_SB_G0:
7402 case elfcpp::R_ARM_LDC_SB_G1:
7403 case elfcpp::R_ARM_LDC_SB_G2:
7404 case elfcpp::R_ARM_MOVW_BREL_NC:
7405 case elfcpp::R_ARM_MOVT_BREL:
7406 case elfcpp::R_ARM_MOVW_BREL:
7407 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7408 case elfcpp::R_ARM_THM_MOVT_BREL:
7409 case elfcpp::R_ARM_THM_MOVW_BREL:
7410 case elfcpp::R_ARM_THM_JUMP11:
7411 case elfcpp::R_ARM_THM_JUMP8:
7412 // We don't need to do anything for a relative addressing relocation
7413 // against a local symbol if it does not reference the GOT.
7414 break;
7416 case elfcpp::R_ARM_GOTOFF32:
7417 case elfcpp::R_ARM_GOTOFF12:
7418 // We need a GOT section:
7419 target->got_section(symtab, layout);
7420 break;
7422 case elfcpp::R_ARM_GOT_BREL:
7423 case elfcpp::R_ARM_GOT_PREL:
7425 // The symbol requires a GOT entry.
7426 Arm_output_data_got<big_endian>* got =
7427 target->got_section(symtab, layout);
7428 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7429 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
7431 // If we are generating a shared object, we need to add a
7432 // dynamic RELATIVE relocation for this symbol's GOT entry.
7433 if (parameters->options().output_is_position_independent())
7435 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7436 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7437 rel_dyn->add_local_relative(
7438 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
7439 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
7443 break;
7445 case elfcpp::R_ARM_TARGET1:
7446 case elfcpp::R_ARM_TARGET2:
7447 // This should have been mapped to another type already.
7448 // Fall through.
7449 case elfcpp::R_ARM_COPY:
7450 case elfcpp::R_ARM_GLOB_DAT:
7451 case elfcpp::R_ARM_JUMP_SLOT:
7452 case elfcpp::R_ARM_RELATIVE:
7453 // These are relocations which should only be seen by the
7454 // dynamic linker, and should never be seen here.
7455 gold_error(_("%s: unexpected reloc %u in object file"),
7456 object->name().c_str(), r_type);
7457 break;
7460 // These are initial TLS relocs, which are expected when
7461 // linking.
7462 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7463 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7464 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7465 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7466 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7468 bool output_is_shared = parameters->options().shared();
7469 const tls::Tls_optimization optimized_type
7470 = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
7471 r_type);
7472 switch (r_type)
7474 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7475 if (optimized_type == tls::TLSOPT_NONE)
7477 // Create a pair of GOT entries for the module index and
7478 // dtv-relative offset.
7479 Arm_output_data_got<big_endian>* got
7480 = target->got_section(symtab, layout);
7481 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7482 unsigned int shndx = lsym.get_st_shndx();
7483 bool is_ordinary;
7484 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7485 if (!is_ordinary)
7487 object->error(_("local symbol %u has bad shndx %u"),
7488 r_sym, shndx);
7489 break;
7492 if (!parameters->doing_static_link())
7493 got->add_local_pair_with_rel(object, r_sym, shndx,
7494 GOT_TYPE_TLS_PAIR,
7495 target->rel_dyn_section(layout),
7496 elfcpp::R_ARM_TLS_DTPMOD32, 0);
7497 else
7498 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR,
7499 object, r_sym);
7501 else
7502 // FIXME: TLS optimization not supported yet.
7503 gold_unreachable();
7504 break;
7506 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7507 if (optimized_type == tls::TLSOPT_NONE)
7509 // Create a GOT entry for the module index.
7510 target->got_mod_index_entry(symtab, layout, object);
7512 else
7513 // FIXME: TLS optimization not supported yet.
7514 gold_unreachable();
7515 break;
7517 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7518 break;
7520 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7521 layout->set_has_static_tls();
7522 if (optimized_type == tls::TLSOPT_NONE)
7524 // Create a GOT entry for the tp-relative offset.
7525 Arm_output_data_got<big_endian>* got
7526 = target->got_section(symtab, layout);
7527 unsigned int r_sym =
7528 elfcpp::elf_r_sym<32>(reloc.get_r_info());
7529 if (!parameters->doing_static_link())
7530 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
7531 target->rel_dyn_section(layout),
7532 elfcpp::R_ARM_TLS_TPOFF32);
7533 else if (!object->local_has_got_offset(r_sym,
7534 GOT_TYPE_TLS_OFFSET))
7536 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
7537 unsigned int got_offset =
7538 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
7539 got->add_static_reloc(got_offset,
7540 elfcpp::R_ARM_TLS_TPOFF32, object,
7541 r_sym);
7544 else
7545 // FIXME: TLS optimization not supported yet.
7546 gold_unreachable();
7547 break;
7549 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7550 layout->set_has_static_tls();
7551 if (output_is_shared)
7553 // We need to create a dynamic relocation.
7554 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
7555 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7556 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7557 rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
7558 output_section, data_shndx,
7559 reloc.get_r_offset());
7561 break;
7563 default:
7564 gold_unreachable();
7567 break;
7569 default:
7570 unsupported_reloc_local(object, r_type);
7571 break;
7575 // Report an unsupported relocation against a global symbol.
7577 template<bool big_endian>
7578 void
7579 Target_arm<big_endian>::Scan::unsupported_reloc_global(
7580 Sized_relobj<32, big_endian>* object,
7581 unsigned int r_type,
7582 Symbol* gsym)
7584 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7585 object->name().c_str(), r_type, gsym->demangled_name().c_str());
7588 // Scan a relocation for a global symbol.
7590 template<bool big_endian>
7591 inline void
7592 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
7593 Layout* layout,
7594 Target_arm* target,
7595 Sized_relobj<32, big_endian>* object,
7596 unsigned int data_shndx,
7597 Output_section* output_section,
7598 const elfcpp::Rel<32, big_endian>& reloc,
7599 unsigned int r_type,
7600 Symbol* gsym)
7602 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
7603 // section. We check here to avoid creating a dynamic reloc against
7604 // _GLOBAL_OFFSET_TABLE_.
7605 if (!target->has_got_section()
7606 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7607 target->got_section(symtab, layout);
7609 r_type = get_real_reloc_type(r_type);
7610 switch (r_type)
7612 case elfcpp::R_ARM_NONE:
7613 case elfcpp::R_ARM_V4BX:
7614 case elfcpp::R_ARM_GNU_VTENTRY:
7615 case elfcpp::R_ARM_GNU_VTINHERIT:
7616 break;
7618 case elfcpp::R_ARM_ABS32:
7619 case elfcpp::R_ARM_ABS16:
7620 case elfcpp::R_ARM_ABS12:
7621 case elfcpp::R_ARM_THM_ABS5:
7622 case elfcpp::R_ARM_ABS8:
7623 case elfcpp::R_ARM_BASE_ABS:
7624 case elfcpp::R_ARM_MOVW_ABS_NC:
7625 case elfcpp::R_ARM_MOVT_ABS:
7626 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7627 case elfcpp::R_ARM_THM_MOVT_ABS:
7628 case elfcpp::R_ARM_ABS32_NOI:
7629 // Absolute addressing relocations.
7631 // Make a PLT entry if necessary.
7632 if (this->symbol_needs_plt_entry(gsym))
7634 target->make_plt_entry(symtab, layout, gsym);
7635 // Since this is not a PC-relative relocation, we may be
7636 // taking the address of a function. In that case we need to
7637 // set the entry in the dynamic symbol table to the address of
7638 // the PLT entry.
7639 if (gsym->is_from_dynobj() && !parameters->options().shared())
7640 gsym->set_needs_dynsym_value();
7642 // Make a dynamic relocation if necessary.
7643 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
7645 if (gsym->may_need_copy_reloc())
7647 target->copy_reloc(symtab, layout, object,
7648 data_shndx, output_section, gsym, reloc);
7650 else if ((r_type == elfcpp::R_ARM_ABS32
7651 || r_type == elfcpp::R_ARM_ABS32_NOI)
7652 && gsym->can_use_relative_reloc(false))
7654 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7655 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
7656 output_section, object,
7657 data_shndx, reloc.get_r_offset());
7659 else
7661 check_non_pic(object, r_type);
7662 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7663 rel_dyn->add_global(gsym, r_type, output_section, object,
7664 data_shndx, reloc.get_r_offset());
7668 break;
7670 case elfcpp::R_ARM_GOTOFF32:
7671 case elfcpp::R_ARM_GOTOFF12:
7672 // We need a GOT section.
7673 target->got_section(symtab, layout);
7674 break;
7676 case elfcpp::R_ARM_REL32:
7677 case elfcpp::R_ARM_LDR_PC_G0:
7678 case elfcpp::R_ARM_SBREL32:
7679 case elfcpp::R_ARM_THM_PC8:
7680 case elfcpp::R_ARM_BASE_PREL:
7681 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7682 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7683 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7684 case elfcpp::R_ARM_MOVW_PREL_NC:
7685 case elfcpp::R_ARM_MOVT_PREL:
7686 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7687 case elfcpp::R_ARM_THM_MOVT_PREL:
7688 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7689 case elfcpp::R_ARM_THM_PC12:
7690 case elfcpp::R_ARM_REL32_NOI:
7691 case elfcpp::R_ARM_ALU_PC_G0_NC:
7692 case elfcpp::R_ARM_ALU_PC_G0:
7693 case elfcpp::R_ARM_ALU_PC_G1_NC:
7694 case elfcpp::R_ARM_ALU_PC_G1:
7695 case elfcpp::R_ARM_ALU_PC_G2:
7696 case elfcpp::R_ARM_LDR_PC_G1:
7697 case elfcpp::R_ARM_LDR_PC_G2:
7698 case elfcpp::R_ARM_LDRS_PC_G0:
7699 case elfcpp::R_ARM_LDRS_PC_G1:
7700 case elfcpp::R_ARM_LDRS_PC_G2:
7701 case elfcpp::R_ARM_LDC_PC_G0:
7702 case elfcpp::R_ARM_LDC_PC_G1:
7703 case elfcpp::R_ARM_LDC_PC_G2:
7704 case elfcpp::R_ARM_ALU_SB_G0_NC:
7705 case elfcpp::R_ARM_ALU_SB_G0:
7706 case elfcpp::R_ARM_ALU_SB_G1_NC:
7707 case elfcpp::R_ARM_ALU_SB_G1:
7708 case elfcpp::R_ARM_ALU_SB_G2:
7709 case elfcpp::R_ARM_LDR_SB_G0:
7710 case elfcpp::R_ARM_LDR_SB_G1:
7711 case elfcpp::R_ARM_LDR_SB_G2:
7712 case elfcpp::R_ARM_LDRS_SB_G0:
7713 case elfcpp::R_ARM_LDRS_SB_G1:
7714 case elfcpp::R_ARM_LDRS_SB_G2:
7715 case elfcpp::R_ARM_LDC_SB_G0:
7716 case elfcpp::R_ARM_LDC_SB_G1:
7717 case elfcpp::R_ARM_LDC_SB_G2:
7718 case elfcpp::R_ARM_MOVW_BREL_NC:
7719 case elfcpp::R_ARM_MOVT_BREL:
7720 case elfcpp::R_ARM_MOVW_BREL:
7721 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7722 case elfcpp::R_ARM_THM_MOVT_BREL:
7723 case elfcpp::R_ARM_THM_MOVW_BREL:
7724 // Relative addressing relocations.
7726 // Make a dynamic relocation if necessary.
7727 int flags = Symbol::NON_PIC_REF;
7728 if (gsym->needs_dynamic_reloc(flags))
7730 if (target->may_need_copy_reloc(gsym))
7732 target->copy_reloc(symtab, layout, object,
7733 data_shndx, output_section, gsym, reloc);
7735 else
7737 check_non_pic(object, r_type);
7738 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7739 rel_dyn->add_global(gsym, r_type, output_section, object,
7740 data_shndx, reloc.get_r_offset());
7744 break;
7746 case elfcpp::R_ARM_PC24:
7747 case elfcpp::R_ARM_THM_CALL:
7748 case elfcpp::R_ARM_PLT32:
7749 case elfcpp::R_ARM_CALL:
7750 case elfcpp::R_ARM_JUMP24:
7751 case elfcpp::R_ARM_THM_JUMP24:
7752 case elfcpp::R_ARM_SBREL31:
7753 case elfcpp::R_ARM_PREL31:
7754 case elfcpp::R_ARM_THM_JUMP19:
7755 case elfcpp::R_ARM_THM_JUMP6:
7756 case elfcpp::R_ARM_THM_JUMP11:
7757 case elfcpp::R_ARM_THM_JUMP8:
7758 // All the relocation above are branches except for the PREL31 ones.
7759 // A PREL31 relocation can point to a personality function in a shared
7760 // library. In that case we want to use a PLT because we want to
7761 // call the personality routine and the dyanmic linkers we care about
7762 // do not support dynamic PREL31 relocations. An REL31 relocation may
7763 // point to a function whose unwinding behaviour is being described but
7764 // we will not mistakenly generate a PLT for that because we should use
7765 // a local section symbol.
7767 // If the symbol is fully resolved, this is just a relative
7768 // local reloc. Otherwise we need a PLT entry.
7769 if (gsym->final_value_is_known())
7770 break;
7771 // If building a shared library, we can also skip the PLT entry
7772 // if the symbol is defined in the output file and is protected
7773 // or hidden.
7774 if (gsym->is_defined()
7775 && !gsym->is_from_dynobj()
7776 && !gsym->is_preemptible())
7777 break;
7778 target->make_plt_entry(symtab, layout, gsym);
7779 break;
7781 case elfcpp::R_ARM_GOT_BREL:
7782 case elfcpp::R_ARM_GOT_ABS:
7783 case elfcpp::R_ARM_GOT_PREL:
7785 // The symbol requires a GOT entry.
7786 Arm_output_data_got<big_endian>* got =
7787 target->got_section(symtab, layout);
7788 if (gsym->final_value_is_known())
7789 got->add_global(gsym, GOT_TYPE_STANDARD);
7790 else
7792 // If this symbol is not fully resolved, we need to add a
7793 // GOT entry with a dynamic relocation.
7794 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7795 if (gsym->is_from_dynobj()
7796 || gsym->is_undefined()
7797 || gsym->is_preemptible())
7798 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
7799 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
7800 else
7802 if (got->add_global(gsym, GOT_TYPE_STANDARD))
7803 rel_dyn->add_global_relative(
7804 gsym, elfcpp::R_ARM_RELATIVE, got,
7805 gsym->got_offset(GOT_TYPE_STANDARD));
7809 break;
7811 case elfcpp::R_ARM_TARGET1:
7812 case elfcpp::R_ARM_TARGET2:
7813 // These should have been mapped to other types already.
7814 // Fall through.
7815 case elfcpp::R_ARM_COPY:
7816 case elfcpp::R_ARM_GLOB_DAT:
7817 case elfcpp::R_ARM_JUMP_SLOT:
7818 case elfcpp::R_ARM_RELATIVE:
7819 // These are relocations which should only be seen by the
7820 // dynamic linker, and should never be seen here.
7821 gold_error(_("%s: unexpected reloc %u in object file"),
7822 object->name().c_str(), r_type);
7823 break;
7825 // These are initial tls relocs, which are expected when
7826 // linking.
7827 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7828 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7829 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7830 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7831 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7833 const bool is_final = gsym->final_value_is_known();
7834 const tls::Tls_optimization optimized_type
7835 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
7836 switch (r_type)
7838 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7839 if (optimized_type == tls::TLSOPT_NONE)
7841 // Create a pair of GOT entries for the module index and
7842 // dtv-relative offset.
7843 Arm_output_data_got<big_endian>* got
7844 = target->got_section(symtab, layout);
7845 if (!parameters->doing_static_link())
7846 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
7847 target->rel_dyn_section(layout),
7848 elfcpp::R_ARM_TLS_DTPMOD32,
7849 elfcpp::R_ARM_TLS_DTPOFF32);
7850 else
7851 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
7853 else
7854 // FIXME: TLS optimization not supported yet.
7855 gold_unreachable();
7856 break;
7858 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7859 if (optimized_type == tls::TLSOPT_NONE)
7861 // Create a GOT entry for the module index.
7862 target->got_mod_index_entry(symtab, layout, object);
7864 else
7865 // FIXME: TLS optimization not supported yet.
7866 gold_unreachable();
7867 break;
7869 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7870 break;
7872 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7873 layout->set_has_static_tls();
7874 if (optimized_type == tls::TLSOPT_NONE)
7876 // Create a GOT entry for the tp-relative offset.
7877 Arm_output_data_got<big_endian>* got
7878 = target->got_section(symtab, layout);
7879 if (!parameters->doing_static_link())
7880 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
7881 target->rel_dyn_section(layout),
7882 elfcpp::R_ARM_TLS_TPOFF32);
7883 else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
7885 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
7886 unsigned int got_offset =
7887 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
7888 got->add_static_reloc(got_offset,
7889 elfcpp::R_ARM_TLS_TPOFF32, gsym);
7892 else
7893 // FIXME: TLS optimization not supported yet.
7894 gold_unreachable();
7895 break;
7897 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7898 layout->set_has_static_tls();
7899 if (parameters->options().shared())
7901 // We need to create a dynamic relocation.
7902 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7903 rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
7904 output_section, object,
7905 data_shndx, reloc.get_r_offset());
7907 break;
7909 default:
7910 gold_unreachable();
7913 break;
7915 default:
7916 unsupported_reloc_global(object, r_type, gsym);
7917 break;
7921 // Process relocations for gc.
7923 template<bool big_endian>
7924 void
7925 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
7926 Layout* layout,
7927 Sized_relobj<32, big_endian>* object,
7928 unsigned int data_shndx,
7929 unsigned int,
7930 const unsigned char* prelocs,
7931 size_t reloc_count,
7932 Output_section* output_section,
7933 bool needs_special_offset_handling,
7934 size_t local_symbol_count,
7935 const unsigned char* plocal_symbols)
7937 typedef Target_arm<big_endian> Arm;
7938 typedef typename Target_arm<big_endian>::Scan Scan;
7940 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
7941 symtab,
7942 layout,
7943 this,
7944 object,
7945 data_shndx,
7946 prelocs,
7947 reloc_count,
7948 output_section,
7949 needs_special_offset_handling,
7950 local_symbol_count,
7951 plocal_symbols);
7954 // Scan relocations for a section.
7956 template<bool big_endian>
7957 void
7958 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
7959 Layout* layout,
7960 Sized_relobj<32, big_endian>* object,
7961 unsigned int data_shndx,
7962 unsigned int sh_type,
7963 const unsigned char* prelocs,
7964 size_t reloc_count,
7965 Output_section* output_section,
7966 bool needs_special_offset_handling,
7967 size_t local_symbol_count,
7968 const unsigned char* plocal_symbols)
7970 typedef typename Target_arm<big_endian>::Scan Scan;
7971 if (sh_type == elfcpp::SHT_RELA)
7973 gold_error(_("%s: unsupported RELA reloc section"),
7974 object->name().c_str());
7975 return;
7978 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
7979 symtab,
7980 layout,
7981 this,
7982 object,
7983 data_shndx,
7984 prelocs,
7985 reloc_count,
7986 output_section,
7987 needs_special_offset_handling,
7988 local_symbol_count,
7989 plocal_symbols);
7992 // Finalize the sections.
7994 template<bool big_endian>
7995 void
7996 Target_arm<big_endian>::do_finalize_sections(
7997 Layout* layout,
7998 const Input_objects* input_objects,
7999 Symbol_table* symtab)
8001 // Create an empty uninitialized attribute section if we still don't have it
8002 // at this moment.
8003 if (this->attributes_section_data_ == NULL)
8004 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
8006 // Merge processor-specific flags.
8007 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8008 p != input_objects->relobj_end();
8009 ++p)
8011 Arm_relobj<big_endian>* arm_relobj =
8012 Arm_relobj<big_endian>::as_arm_relobj(*p);
8013 if (arm_relobj->merge_flags_and_attributes())
8015 this->merge_processor_specific_flags(
8016 arm_relobj->name(),
8017 arm_relobj->processor_specific_flags());
8018 this->merge_object_attributes(arm_relobj->name().c_str(),
8019 arm_relobj->attributes_section_data());
8023 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
8024 p != input_objects->dynobj_end();
8025 ++p)
8027 Arm_dynobj<big_endian>* arm_dynobj =
8028 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
8029 this->merge_processor_specific_flags(
8030 arm_dynobj->name(),
8031 arm_dynobj->processor_specific_flags());
8032 this->merge_object_attributes(arm_dynobj->name().c_str(),
8033 arm_dynobj->attributes_section_data());
8036 // Check BLX use.
8037 const Object_attribute* cpu_arch_attr =
8038 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
8039 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
8040 this->set_may_use_blx(true);
8042 // Check if we need to use Cortex-A8 workaround.
8043 if (parameters->options().user_set_fix_cortex_a8())
8044 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
8045 else
8047 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
8048 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
8049 // profile.
8050 const Object_attribute* cpu_arch_profile_attr =
8051 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
8052 this->fix_cortex_a8_ =
8053 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
8054 && (cpu_arch_profile_attr->int_value() == 'A'
8055 || cpu_arch_profile_attr->int_value() == 0));
8058 // Check if we can use V4BX interworking.
8059 // The V4BX interworking stub contains BX instruction,
8060 // which is not specified for some profiles.
8061 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8062 && !this->may_use_blx())
8063 gold_error(_("unable to provide V4BX reloc interworking fix up; "
8064 "the target profile does not support BX instruction"));
8066 // Fill in some more dynamic tags.
8067 const Reloc_section* rel_plt = (this->plt_ == NULL
8068 ? NULL
8069 : this->plt_->rel_plt());
8070 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
8071 this->rel_dyn_, true, false);
8073 // Emit any relocs we saved in an attempt to avoid generating COPY
8074 // relocs.
8075 if (this->copy_relocs_.any_saved_relocs())
8076 this->copy_relocs_.emit(this->rel_dyn_section(layout));
8078 // Handle the .ARM.exidx section.
8079 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
8080 if (exidx_section != NULL
8081 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
8082 && !parameters->options().relocatable())
8084 // Create __exidx_start and __exdix_end symbols.
8085 symtab->define_in_output_data("__exidx_start", NULL,
8086 Symbol_table::PREDEFINED,
8087 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8088 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8089 false, true);
8090 symtab->define_in_output_data("__exidx_end", NULL,
8091 Symbol_table::PREDEFINED,
8092 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8093 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8094 true, true);
8096 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
8097 // the .ARM.exidx section.
8098 if (!layout->script_options()->saw_phdrs_clause())
8100 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
8101 == NULL);
8102 Output_segment* exidx_segment =
8103 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
8104 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
8105 false);
8109 // Create an .ARM.attributes section unless we have no regular input
8110 // object. In that case the output will be empty.
8111 if (input_objects->number_of_relobjs() != 0)
8113 Output_attributes_section_data* attributes_section =
8114 new Output_attributes_section_data(*this->attributes_section_data_);
8115 layout->add_output_section_data(".ARM.attributes",
8116 elfcpp::SHT_ARM_ATTRIBUTES, 0,
8117 attributes_section, false, false, false,
8118 false);
8122 // Return whether a direct absolute static relocation needs to be applied.
8123 // In cases where Scan::local() or Scan::global() has created
8124 // a dynamic relocation other than R_ARM_RELATIVE, the addend
8125 // of the relocation is carried in the data, and we must not
8126 // apply the static relocation.
8128 template<bool big_endian>
8129 inline bool
8130 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
8131 const Sized_symbol<32>* gsym,
8132 int ref_flags,
8133 bool is_32bit,
8134 Output_section* output_section)
8136 // If the output section is not allocated, then we didn't call
8137 // scan_relocs, we didn't create a dynamic reloc, and we must apply
8138 // the reloc here.
8139 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
8140 return true;
8142 // For local symbols, we will have created a non-RELATIVE dynamic
8143 // relocation only if (a) the output is position independent,
8144 // (b) the relocation is absolute (not pc- or segment-relative), and
8145 // (c) the relocation is not 32 bits wide.
8146 if (gsym == NULL)
8147 return !(parameters->options().output_is_position_independent()
8148 && (ref_flags & Symbol::ABSOLUTE_REF)
8149 && !is_32bit);
8151 // For global symbols, we use the same helper routines used in the
8152 // scan pass. If we did not create a dynamic relocation, or if we
8153 // created a RELATIVE dynamic relocation, we should apply the static
8154 // relocation.
8155 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
8156 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
8157 && gsym->can_use_relative_reloc(ref_flags
8158 & Symbol::FUNCTION_CALL);
8159 return !has_dyn || is_rel;
8162 // Perform a relocation.
8164 template<bool big_endian>
8165 inline bool
8166 Target_arm<big_endian>::Relocate::relocate(
8167 const Relocate_info<32, big_endian>* relinfo,
8168 Target_arm* target,
8169 Output_section *output_section,
8170 size_t relnum,
8171 const elfcpp::Rel<32, big_endian>& rel,
8172 unsigned int r_type,
8173 const Sized_symbol<32>* gsym,
8174 const Symbol_value<32>* psymval,
8175 unsigned char* view,
8176 Arm_address address,
8177 section_size_type view_size)
8179 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
8181 r_type = get_real_reloc_type(r_type);
8182 const Arm_reloc_property* reloc_property =
8183 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8184 if (reloc_property == NULL)
8186 std::string reloc_name =
8187 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8188 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8189 _("cannot relocate %s in object file"),
8190 reloc_name.c_str());
8191 return true;
8194 const Arm_relobj<big_endian>* object =
8195 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8197 // If the final branch target of a relocation is THUMB instruction, this
8198 // is 1. Otherwise it is 0.
8199 Arm_address thumb_bit = 0;
8200 Symbol_value<32> symval;
8201 bool is_weakly_undefined_without_plt = false;
8202 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
8204 if (gsym != NULL)
8206 // This is a global symbol. Determine if we use PLT and if the
8207 // final target is THUMB.
8208 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
8210 // This uses a PLT, change the symbol value.
8211 symval.set_output_value(target->plt_section()->address()
8212 + gsym->plt_offset());
8213 psymval = &symval;
8215 else if (gsym->is_weak_undefined())
8217 // This is a weakly undefined symbol and we do not use PLT
8218 // for this relocation. A branch targeting this symbol will
8219 // be converted into an NOP.
8220 is_weakly_undefined_without_plt = true;
8222 else
8224 // Set thumb bit if symbol:
8225 // -Has type STT_ARM_TFUNC or
8226 // -Has type STT_FUNC, is defined and with LSB in value set.
8227 thumb_bit =
8228 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
8229 || (gsym->type() == elfcpp::STT_FUNC
8230 && !gsym->is_undefined()
8231 && ((psymval->value(object, 0) & 1) != 0)))
8233 : 0);
8236 else
8238 // This is a local symbol. Determine if the final target is THUMB.
8239 // We saved this information when all the local symbols were read.
8240 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
8241 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
8242 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
8245 else
8247 // This is a fake relocation synthesized for a stub. It does not have
8248 // a real symbol. We just look at the LSB of the symbol value to
8249 // determine if the target is THUMB or not.
8250 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
8253 // Strip LSB if this points to a THUMB target.
8254 if (thumb_bit != 0
8255 && reloc_property->uses_thumb_bit()
8256 && ((psymval->value(object, 0) & 1) != 0))
8258 Arm_address stripped_value =
8259 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
8260 symval.set_output_value(stripped_value);
8261 psymval = &symval;
8264 // Get the GOT offset if needed.
8265 // The GOT pointer points to the end of the GOT section.
8266 // We need to subtract the size of the GOT section to get
8267 // the actual offset to use in the relocation.
8268 bool have_got_offset = false;
8269 unsigned int got_offset = 0;
8270 switch (r_type)
8272 case elfcpp::R_ARM_GOT_BREL:
8273 case elfcpp::R_ARM_GOT_PREL:
8274 if (gsym != NULL)
8276 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8277 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
8278 - target->got_size());
8280 else
8282 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8283 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8284 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
8285 - target->got_size());
8287 have_got_offset = true;
8288 break;
8290 default:
8291 break;
8294 // To look up relocation stubs, we need to pass the symbol table index of
8295 // a local symbol.
8296 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8298 // Get the addressing origin of the output segment defining the
8299 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
8300 Arm_address sym_origin = 0;
8301 if (reloc_property->uses_symbol_base())
8303 if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
8304 // R_ARM_BASE_ABS with the NULL symbol will give the
8305 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
8306 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
8307 sym_origin = target->got_plt_section()->address();
8308 else if (gsym == NULL)
8309 sym_origin = 0;
8310 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
8311 sym_origin = gsym->output_segment()->vaddr();
8312 else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
8313 sym_origin = gsym->output_data()->address();
8315 // TODO: Assumes the segment base to be zero for the global symbols
8316 // till the proper support for the segment-base-relative addressing
8317 // will be implemented. This is consistent with GNU ld.
8320 // For relative addressing relocation, find out the relative address base.
8321 Arm_address relative_address_base = 0;
8322 switch(reloc_property->relative_address_base())
8324 case Arm_reloc_property::RAB_NONE:
8325 // Relocations with relative address bases RAB_TLS and RAB_tp are
8326 // handled by relocate_tls. So we do not need to do anything here.
8327 case Arm_reloc_property::RAB_TLS:
8328 case Arm_reloc_property::RAB_tp:
8329 break;
8330 case Arm_reloc_property::RAB_B_S:
8331 relative_address_base = sym_origin;
8332 break;
8333 case Arm_reloc_property::RAB_GOT_ORG:
8334 relative_address_base = target->got_plt_section()->address();
8335 break;
8336 case Arm_reloc_property::RAB_P:
8337 relative_address_base = address;
8338 break;
8339 case Arm_reloc_property::RAB_Pa:
8340 relative_address_base = address & 0xfffffffcU;
8341 break;
8342 default:
8343 gold_unreachable();
8346 typename Arm_relocate_functions::Status reloc_status =
8347 Arm_relocate_functions::STATUS_OKAY;
8348 bool check_overflow = reloc_property->checks_overflow();
8349 switch (r_type)
8351 case elfcpp::R_ARM_NONE:
8352 break;
8354 case elfcpp::R_ARM_ABS8:
8355 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8356 output_section))
8357 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
8358 break;
8360 case elfcpp::R_ARM_ABS12:
8361 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8362 output_section))
8363 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
8364 break;
8366 case elfcpp::R_ARM_ABS16:
8367 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8368 output_section))
8369 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
8370 break;
8372 case elfcpp::R_ARM_ABS32:
8373 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
8374 output_section))
8375 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8376 thumb_bit);
8377 break;
8379 case elfcpp::R_ARM_ABS32_NOI:
8380 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
8381 output_section))
8382 // No thumb bit for this relocation: (S + A)
8383 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8385 break;
8387 case elfcpp::R_ARM_MOVW_ABS_NC:
8388 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8389 output_section))
8390 reloc_status = Arm_relocate_functions::movw(view, object, psymval,
8391 0, thumb_bit,
8392 check_overflow);
8393 break;
8395 case elfcpp::R_ARM_MOVT_ABS:
8396 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8397 output_section))
8398 reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
8399 break;
8401 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8402 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8403 output_section))
8404 reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
8405 0, thumb_bit, false);
8406 break;
8408 case elfcpp::R_ARM_THM_MOVT_ABS:
8409 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8410 output_section))
8411 reloc_status = Arm_relocate_functions::thm_movt(view, object,
8412 psymval, 0);
8413 break;
8415 case elfcpp::R_ARM_MOVW_PREL_NC:
8416 case elfcpp::R_ARM_MOVW_BREL_NC:
8417 case elfcpp::R_ARM_MOVW_BREL:
8418 reloc_status =
8419 Arm_relocate_functions::movw(view, object, psymval,
8420 relative_address_base, thumb_bit,
8421 check_overflow);
8422 break;
8424 case elfcpp::R_ARM_MOVT_PREL:
8425 case elfcpp::R_ARM_MOVT_BREL:
8426 reloc_status =
8427 Arm_relocate_functions::movt(view, object, psymval,
8428 relative_address_base);
8429 break;
8431 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8432 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8433 case elfcpp::R_ARM_THM_MOVW_BREL:
8434 reloc_status =
8435 Arm_relocate_functions::thm_movw(view, object, psymval,
8436 relative_address_base,
8437 thumb_bit, check_overflow);
8438 break;
8440 case elfcpp::R_ARM_THM_MOVT_PREL:
8441 case elfcpp::R_ARM_THM_MOVT_BREL:
8442 reloc_status =
8443 Arm_relocate_functions::thm_movt(view, object, psymval,
8444 relative_address_base);
8445 break;
8447 case elfcpp::R_ARM_REL32:
8448 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8449 address, thumb_bit);
8450 break;
8452 case elfcpp::R_ARM_THM_ABS5:
8453 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8454 output_section))
8455 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
8456 break;
8458 // Thumb long branches.
8459 case elfcpp::R_ARM_THM_CALL:
8460 case elfcpp::R_ARM_THM_XPC22:
8461 case elfcpp::R_ARM_THM_JUMP24:
8462 reloc_status =
8463 Arm_relocate_functions::thumb_branch_common(
8464 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8465 thumb_bit, is_weakly_undefined_without_plt);
8466 break;
8468 case elfcpp::R_ARM_GOTOFF32:
8470 Arm_address got_origin;
8471 got_origin = target->got_plt_section()->address();
8472 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8473 got_origin, thumb_bit);
8475 break;
8477 case elfcpp::R_ARM_BASE_PREL:
8478 gold_assert(gsym != NULL);
8479 reloc_status =
8480 Arm_relocate_functions::base_prel(view, sym_origin, address);
8481 break;
8483 case elfcpp::R_ARM_BASE_ABS:
8485 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8486 output_section))
8487 break;
8489 reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
8491 break;
8493 case elfcpp::R_ARM_GOT_BREL:
8494 gold_assert(have_got_offset);
8495 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
8496 break;
8498 case elfcpp::R_ARM_GOT_PREL:
8499 gold_assert(have_got_offset);
8500 // Get the address origin for GOT PLT, which is allocated right
8501 // after the GOT section, to calculate an absolute address of
8502 // the symbol GOT entry (got_origin + got_offset).
8503 Arm_address got_origin;
8504 got_origin = target->got_plt_section()->address();
8505 reloc_status = Arm_relocate_functions::got_prel(view,
8506 got_origin + got_offset,
8507 address);
8508 break;
8510 case elfcpp::R_ARM_PLT32:
8511 case elfcpp::R_ARM_CALL:
8512 case elfcpp::R_ARM_JUMP24:
8513 case elfcpp::R_ARM_XPC25:
8514 gold_assert(gsym == NULL
8515 || gsym->has_plt_offset()
8516 || gsym->final_value_is_known()
8517 || (gsym->is_defined()
8518 && !gsym->is_from_dynobj()
8519 && !gsym->is_preemptible()));
8520 reloc_status =
8521 Arm_relocate_functions::arm_branch_common(
8522 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8523 thumb_bit, is_weakly_undefined_without_plt);
8524 break;
8526 case elfcpp::R_ARM_THM_JUMP19:
8527 reloc_status =
8528 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
8529 thumb_bit);
8530 break;
8532 case elfcpp::R_ARM_THM_JUMP6:
8533 reloc_status =
8534 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
8535 break;
8537 case elfcpp::R_ARM_THM_JUMP8:
8538 reloc_status =
8539 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
8540 break;
8542 case elfcpp::R_ARM_THM_JUMP11:
8543 reloc_status =
8544 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
8545 break;
8547 case elfcpp::R_ARM_PREL31:
8548 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
8549 address, thumb_bit);
8550 break;
8552 case elfcpp::R_ARM_V4BX:
8553 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
8555 const bool is_v4bx_interworking =
8556 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
8557 reloc_status =
8558 Arm_relocate_functions::v4bx(relinfo, view, object, address,
8559 is_v4bx_interworking);
8561 break;
8563 case elfcpp::R_ARM_THM_PC8:
8564 reloc_status =
8565 Arm_relocate_functions::thm_pc8(view, object, psymval, address);
8566 break;
8568 case elfcpp::R_ARM_THM_PC12:
8569 reloc_status =
8570 Arm_relocate_functions::thm_pc12(view, object, psymval, address);
8571 break;
8573 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8574 reloc_status =
8575 Arm_relocate_functions::thm_alu11(view, object, psymval, address,
8576 thumb_bit);
8577 break;
8579 case elfcpp::R_ARM_ALU_PC_G0_NC:
8580 case elfcpp::R_ARM_ALU_PC_G0:
8581 case elfcpp::R_ARM_ALU_PC_G1_NC:
8582 case elfcpp::R_ARM_ALU_PC_G1:
8583 case elfcpp::R_ARM_ALU_PC_G2:
8584 case elfcpp::R_ARM_ALU_SB_G0_NC:
8585 case elfcpp::R_ARM_ALU_SB_G0:
8586 case elfcpp::R_ARM_ALU_SB_G1_NC:
8587 case elfcpp::R_ARM_ALU_SB_G1:
8588 case elfcpp::R_ARM_ALU_SB_G2:
8589 reloc_status =
8590 Arm_relocate_functions::arm_grp_alu(view, object, psymval,
8591 reloc_property->group_index(),
8592 relative_address_base,
8593 thumb_bit, check_overflow);
8594 break;
8596 case elfcpp::R_ARM_LDR_PC_G0:
8597 case elfcpp::R_ARM_LDR_PC_G1:
8598 case elfcpp::R_ARM_LDR_PC_G2:
8599 case elfcpp::R_ARM_LDR_SB_G0:
8600 case elfcpp::R_ARM_LDR_SB_G1:
8601 case elfcpp::R_ARM_LDR_SB_G2:
8602 reloc_status =
8603 Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
8604 reloc_property->group_index(),
8605 relative_address_base);
8606 break;
8608 case elfcpp::R_ARM_LDRS_PC_G0:
8609 case elfcpp::R_ARM_LDRS_PC_G1:
8610 case elfcpp::R_ARM_LDRS_PC_G2:
8611 case elfcpp::R_ARM_LDRS_SB_G0:
8612 case elfcpp::R_ARM_LDRS_SB_G1:
8613 case elfcpp::R_ARM_LDRS_SB_G2:
8614 reloc_status =
8615 Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
8616 reloc_property->group_index(),
8617 relative_address_base);
8618 break;
8620 case elfcpp::R_ARM_LDC_PC_G0:
8621 case elfcpp::R_ARM_LDC_PC_G1:
8622 case elfcpp::R_ARM_LDC_PC_G2:
8623 case elfcpp::R_ARM_LDC_SB_G0:
8624 case elfcpp::R_ARM_LDC_SB_G1:
8625 case elfcpp::R_ARM_LDC_SB_G2:
8626 reloc_status =
8627 Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
8628 reloc_property->group_index(),
8629 relative_address_base);
8630 break;
8632 // These are initial tls relocs, which are expected when
8633 // linking.
8634 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8635 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8636 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8637 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8638 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8639 reloc_status =
8640 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
8641 view, address, view_size);
8642 break;
8644 default:
8645 gold_unreachable();
8648 // Report any errors.
8649 switch (reloc_status)
8651 case Arm_relocate_functions::STATUS_OKAY:
8652 break;
8653 case Arm_relocate_functions::STATUS_OVERFLOW:
8654 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8655 _("relocation overflow in %s"),
8656 reloc_property->name().c_str());
8657 break;
8658 case Arm_relocate_functions::STATUS_BAD_RELOC:
8659 gold_error_at_location(
8660 relinfo,
8661 relnum,
8662 rel.get_r_offset(),
8663 _("unexpected opcode while processing relocation %s"),
8664 reloc_property->name().c_str());
8665 break;
8666 default:
8667 gold_unreachable();
8670 return true;
8673 // Perform a TLS relocation.
8675 template<bool big_endian>
8676 inline typename Arm_relocate_functions<big_endian>::Status
8677 Target_arm<big_endian>::Relocate::relocate_tls(
8678 const Relocate_info<32, big_endian>* relinfo,
8679 Target_arm<big_endian>* target,
8680 size_t relnum,
8681 const elfcpp::Rel<32, big_endian>& rel,
8682 unsigned int r_type,
8683 const Sized_symbol<32>* gsym,
8684 const Symbol_value<32>* psymval,
8685 unsigned char* view,
8686 elfcpp::Elf_types<32>::Elf_Addr address,
8687 section_size_type /*view_size*/ )
8689 typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
8690 typedef Relocate_functions<32, big_endian> RelocFuncs;
8691 Output_segment* tls_segment = relinfo->layout->tls_segment();
8693 const Sized_relobj<32, big_endian>* object = relinfo->object;
8695 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
8697 const bool is_final = (gsym == NULL
8698 ? !parameters->options().shared()
8699 : gsym->final_value_is_known());
8700 const tls::Tls_optimization optimized_type
8701 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
8702 switch (r_type)
8704 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8706 unsigned int got_type = GOT_TYPE_TLS_PAIR;
8707 unsigned int got_offset;
8708 if (gsym != NULL)
8710 gold_assert(gsym->has_got_offset(got_type));
8711 got_offset = gsym->got_offset(got_type) - target->got_size();
8713 else
8715 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8716 gold_assert(object->local_has_got_offset(r_sym, got_type));
8717 got_offset = (object->local_got_offset(r_sym, got_type)
8718 - target->got_size());
8720 if (optimized_type == tls::TLSOPT_NONE)
8722 Arm_address got_entry =
8723 target->got_plt_section()->address() + got_offset;
8725 // Relocate the field with the PC relative offset of the pair of
8726 // GOT entries.
8727 RelocFuncs::pcrel32(view, got_entry, address);
8728 return ArmRelocFuncs::STATUS_OKAY;
8731 break;
8733 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8734 if (optimized_type == tls::TLSOPT_NONE)
8736 // Relocate the field with the offset of the GOT entry for
8737 // the module index.
8738 unsigned int got_offset;
8739 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
8740 - target->got_size());
8741 Arm_address got_entry =
8742 target->got_plt_section()->address() + got_offset;
8744 // Relocate the field with the PC relative offset of the pair of
8745 // GOT entries.
8746 RelocFuncs::pcrel32(view, got_entry, address);
8747 return ArmRelocFuncs::STATUS_OKAY;
8749 break;
8751 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8752 RelocFuncs::rel32(view, value);
8753 return ArmRelocFuncs::STATUS_OKAY;
8755 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8756 if (optimized_type == tls::TLSOPT_NONE)
8758 // Relocate the field with the offset of the GOT entry for
8759 // the tp-relative offset of the symbol.
8760 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
8761 unsigned int got_offset;
8762 if (gsym != NULL)
8764 gold_assert(gsym->has_got_offset(got_type));
8765 got_offset = gsym->got_offset(got_type);
8767 else
8769 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8770 gold_assert(object->local_has_got_offset(r_sym, got_type));
8771 got_offset = object->local_got_offset(r_sym, got_type);
8774 // All GOT offsets are relative to the end of the GOT.
8775 got_offset -= target->got_size();
8777 Arm_address got_entry =
8778 target->got_plt_section()->address() + got_offset;
8780 // Relocate the field with the PC relative offset of the GOT entry.
8781 RelocFuncs::pcrel32(view, got_entry, address);
8782 return ArmRelocFuncs::STATUS_OKAY;
8784 break;
8786 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8787 // If we're creating a shared library, a dynamic relocation will
8788 // have been created for this location, so do not apply it now.
8789 if (!parameters->options().shared())
8791 gold_assert(tls_segment != NULL);
8793 // $tp points to the TCB, which is followed by the TLS, so we
8794 // need to add TCB size to the offset.
8795 Arm_address aligned_tcb_size =
8796 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
8797 RelocFuncs::rel32(view, value + aligned_tcb_size);
8800 return ArmRelocFuncs::STATUS_OKAY;
8802 default:
8803 gold_unreachable();
8806 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8807 _("unsupported reloc %u"),
8808 r_type);
8809 return ArmRelocFuncs::STATUS_BAD_RELOC;
8812 // Relocate section data.
8814 template<bool big_endian>
8815 void
8816 Target_arm<big_endian>::relocate_section(
8817 const Relocate_info<32, big_endian>* relinfo,
8818 unsigned int sh_type,
8819 const unsigned char* prelocs,
8820 size_t reloc_count,
8821 Output_section* output_section,
8822 bool needs_special_offset_handling,
8823 unsigned char* view,
8824 Arm_address address,
8825 section_size_type view_size,
8826 const Reloc_symbol_changes* reloc_symbol_changes)
8828 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
8829 gold_assert(sh_type == elfcpp::SHT_REL);
8831 // See if we are relocating a relaxed input section. If so, the view
8832 // covers the whole output section and we need to adjust accordingly.
8833 if (needs_special_offset_handling)
8835 const Output_relaxed_input_section* poris =
8836 output_section->find_relaxed_input_section(relinfo->object,
8837 relinfo->data_shndx);
8838 if (poris != NULL)
8840 Arm_address section_address = poris->address();
8841 section_size_type section_size = poris->data_size();
8843 gold_assert((section_address >= address)
8844 && ((section_address + section_size)
8845 <= (address + view_size)));
8847 off_t offset = section_address - address;
8848 view += offset;
8849 address += offset;
8850 view_size = section_size;
8854 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
8855 Arm_relocate>(
8856 relinfo,
8857 this,
8858 prelocs,
8859 reloc_count,
8860 output_section,
8861 needs_special_offset_handling,
8862 view,
8863 address,
8864 view_size,
8865 reloc_symbol_changes);
8868 // Return the size of a relocation while scanning during a relocatable
8869 // link.
8871 template<bool big_endian>
8872 unsigned int
8873 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
8874 unsigned int r_type,
8875 Relobj* object)
8877 r_type = get_real_reloc_type(r_type);
8878 const Arm_reloc_property* arp =
8879 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8880 if (arp != NULL)
8881 return arp->size();
8882 else
8884 std::string reloc_name =
8885 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8886 gold_error(_("%s: unexpected %s in object file"),
8887 object->name().c_str(), reloc_name.c_str());
8888 return 0;
8892 // Scan the relocs during a relocatable link.
8894 template<bool big_endian>
8895 void
8896 Target_arm<big_endian>::scan_relocatable_relocs(
8897 Symbol_table* symtab,
8898 Layout* layout,
8899 Sized_relobj<32, big_endian>* object,
8900 unsigned int data_shndx,
8901 unsigned int sh_type,
8902 const unsigned char* prelocs,
8903 size_t reloc_count,
8904 Output_section* output_section,
8905 bool needs_special_offset_handling,
8906 size_t local_symbol_count,
8907 const unsigned char* plocal_symbols,
8908 Relocatable_relocs* rr)
8910 gold_assert(sh_type == elfcpp::SHT_REL);
8912 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
8913 Relocatable_size_for_reloc> Scan_relocatable_relocs;
8915 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
8916 Scan_relocatable_relocs>(
8917 symtab,
8918 layout,
8919 object,
8920 data_shndx,
8921 prelocs,
8922 reloc_count,
8923 output_section,
8924 needs_special_offset_handling,
8925 local_symbol_count,
8926 plocal_symbols,
8927 rr);
8930 // Relocate a section during a relocatable link.
8932 template<bool big_endian>
8933 void
8934 Target_arm<big_endian>::relocate_for_relocatable(
8935 const Relocate_info<32, big_endian>* relinfo,
8936 unsigned int sh_type,
8937 const unsigned char* prelocs,
8938 size_t reloc_count,
8939 Output_section* output_section,
8940 off_t offset_in_output_section,
8941 const Relocatable_relocs* rr,
8942 unsigned char* view,
8943 Arm_address view_address,
8944 section_size_type view_size,
8945 unsigned char* reloc_view,
8946 section_size_type reloc_view_size)
8948 gold_assert(sh_type == elfcpp::SHT_REL);
8950 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
8951 relinfo,
8952 prelocs,
8953 reloc_count,
8954 output_section,
8955 offset_in_output_section,
8957 view,
8958 view_address,
8959 view_size,
8960 reloc_view,
8961 reloc_view_size);
8964 // Return the value to use for a dynamic symbol which requires special
8965 // treatment. This is how we support equality comparisons of function
8966 // pointers across shared library boundaries, as described in the
8967 // processor specific ABI supplement.
8969 template<bool big_endian>
8970 uint64_t
8971 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
8973 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
8974 return this->plt_section()->address() + gsym->plt_offset();
8977 // Map platform-specific relocs to real relocs
8979 template<bool big_endian>
8980 unsigned int
8981 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
8983 switch (r_type)
8985 case elfcpp::R_ARM_TARGET1:
8986 // This is either R_ARM_ABS32 or R_ARM_REL32;
8987 return elfcpp::R_ARM_ABS32;
8989 case elfcpp::R_ARM_TARGET2:
8990 // This can be any reloc type but ususally is R_ARM_GOT_PREL
8991 return elfcpp::R_ARM_GOT_PREL;
8993 default:
8994 return r_type;
8998 // Whether if two EABI versions V1 and V2 are compatible.
9000 template<bool big_endian>
9001 bool
9002 Target_arm<big_endian>::are_eabi_versions_compatible(
9003 elfcpp::Elf_Word v1,
9004 elfcpp::Elf_Word v2)
9006 // v4 and v5 are the same spec before and after it was released,
9007 // so allow mixing them.
9008 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
9009 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
9010 return true;
9012 return v1 == v2;
9015 // Combine FLAGS from an input object called NAME and the processor-specific
9016 // flags in the ELF header of the output. Much of this is adapted from the
9017 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
9018 // in bfd/elf32-arm.c.
9020 template<bool big_endian>
9021 void
9022 Target_arm<big_endian>::merge_processor_specific_flags(
9023 const std::string& name,
9024 elfcpp::Elf_Word flags)
9026 if (this->are_processor_specific_flags_set())
9028 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
9030 // Nothing to merge if flags equal to those in output.
9031 if (flags == out_flags)
9032 return;
9034 // Complain about various flag mismatches.
9035 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
9036 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
9037 if (!this->are_eabi_versions_compatible(version1, version2)
9038 && parameters->options().warn_mismatch())
9039 gold_error(_("Source object %s has EABI version %d but output has "
9040 "EABI version %d."),
9041 name.c_str(),
9042 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
9043 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
9045 else
9047 // If the input is the default architecture and had the default
9048 // flags then do not bother setting the flags for the output
9049 // architecture, instead allow future merges to do this. If no
9050 // future merges ever set these flags then they will retain their
9051 // uninitialised values, which surprise surprise, correspond
9052 // to the default values.
9053 if (flags == 0)
9054 return;
9056 // This is the first time, just copy the flags.
9057 // We only copy the EABI version for now.
9058 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
9062 // Adjust ELF file header.
9063 template<bool big_endian>
9064 void
9065 Target_arm<big_endian>::do_adjust_elf_header(
9066 unsigned char* view,
9067 int len) const
9069 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
9071 elfcpp::Ehdr<32, big_endian> ehdr(view);
9072 unsigned char e_ident[elfcpp::EI_NIDENT];
9073 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9075 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
9076 == elfcpp::EF_ARM_EABI_UNKNOWN)
9077 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
9078 else
9079 e_ident[elfcpp::EI_OSABI] = 0;
9080 e_ident[elfcpp::EI_ABIVERSION] = 0;
9082 // FIXME: Do EF_ARM_BE8 adjustment.
9084 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
9085 oehdr.put_e_ident(e_ident);
9088 // do_make_elf_object to override the same function in the base class.
9089 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
9090 // to store ARM specific information. Hence we need to have our own
9091 // ELF object creation.
9093 template<bool big_endian>
9094 Object*
9095 Target_arm<big_endian>::do_make_elf_object(
9096 const std::string& name,
9097 Input_file* input_file,
9098 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
9100 int et = ehdr.get_e_type();
9101 if (et == elfcpp::ET_REL)
9103 Arm_relobj<big_endian>* obj =
9104 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
9105 obj->setup();
9106 return obj;
9108 else if (et == elfcpp::ET_DYN)
9110 Sized_dynobj<32, big_endian>* obj =
9111 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
9112 obj->setup();
9113 return obj;
9115 else
9117 gold_error(_("%s: unsupported ELF file type %d"),
9118 name.c_str(), et);
9119 return NULL;
9123 // Read the architecture from the Tag_also_compatible_with attribute, if any.
9124 // Returns -1 if no architecture could be read.
9125 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
9127 template<bool big_endian>
9129 Target_arm<big_endian>::get_secondary_compatible_arch(
9130 const Attributes_section_data* pasd)
9132 const Object_attribute *known_attributes =
9133 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
9135 // Note: the tag and its argument below are uleb128 values, though
9136 // currently-defined values fit in one byte for each.
9137 const std::string& sv =
9138 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
9139 if (sv.size() == 2
9140 && sv.data()[0] == elfcpp::Tag_CPU_arch
9141 && (sv.data()[1] & 128) != 128)
9142 return sv.data()[1];
9144 // This tag is "safely ignorable", so don't complain if it looks funny.
9145 return -1;
9148 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
9149 // The tag is removed if ARCH is -1.
9150 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
9152 template<bool big_endian>
9153 void
9154 Target_arm<big_endian>::set_secondary_compatible_arch(
9155 Attributes_section_data* pasd,
9156 int arch)
9158 Object_attribute *known_attributes =
9159 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
9161 if (arch == -1)
9163 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
9164 return;
9167 // Note: the tag and its argument below are uleb128 values, though
9168 // currently-defined values fit in one byte for each.
9169 char sv[3];
9170 sv[0] = elfcpp::Tag_CPU_arch;
9171 gold_assert(arch != 0);
9172 sv[1] = arch;
9173 sv[2] = '\0';
9175 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
9178 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
9179 // into account.
9180 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
9182 template<bool big_endian>
9184 Target_arm<big_endian>::tag_cpu_arch_combine(
9185 const char* name,
9186 int oldtag,
9187 int* secondary_compat_out,
9188 int newtag,
9189 int secondary_compat)
9191 #define T(X) elfcpp::TAG_CPU_ARCH_##X
9192 static const int v6t2[] =
9194 T(V6T2), // PRE_V4.
9195 T(V6T2), // V4.
9196 T(V6T2), // V4T.
9197 T(V6T2), // V5T.
9198 T(V6T2), // V5TE.
9199 T(V6T2), // V5TEJ.
9200 T(V6T2), // V6.
9201 T(V7), // V6KZ.
9202 T(V6T2) // V6T2.
9204 static const int v6k[] =
9206 T(V6K), // PRE_V4.
9207 T(V6K), // V4.
9208 T(V6K), // V4T.
9209 T(V6K), // V5T.
9210 T(V6K), // V5TE.
9211 T(V6K), // V5TEJ.
9212 T(V6K), // V6.
9213 T(V6KZ), // V6KZ.
9214 T(V7), // V6T2.
9215 T(V6K) // V6K.
9217 static const int v7[] =
9219 T(V7), // PRE_V4.
9220 T(V7), // V4.
9221 T(V7), // V4T.
9222 T(V7), // V5T.
9223 T(V7), // V5TE.
9224 T(V7), // V5TEJ.
9225 T(V7), // V6.
9226 T(V7), // V6KZ.
9227 T(V7), // V6T2.
9228 T(V7), // V6K.
9229 T(V7) // V7.
9231 static const int v6_m[] =
9233 -1, // PRE_V4.
9234 -1, // V4.
9235 T(V6K), // V4T.
9236 T(V6K), // V5T.
9237 T(V6K), // V5TE.
9238 T(V6K), // V5TEJ.
9239 T(V6K), // V6.
9240 T(V6KZ), // V6KZ.
9241 T(V7), // V6T2.
9242 T(V6K), // V6K.
9243 T(V7), // V7.
9244 T(V6_M) // V6_M.
9246 static const int v6s_m[] =
9248 -1, // PRE_V4.
9249 -1, // V4.
9250 T(V6K), // V4T.
9251 T(V6K), // V5T.
9252 T(V6K), // V5TE.
9253 T(V6K), // V5TEJ.
9254 T(V6K), // V6.
9255 T(V6KZ), // V6KZ.
9256 T(V7), // V6T2.
9257 T(V6K), // V6K.
9258 T(V7), // V7.
9259 T(V6S_M), // V6_M.
9260 T(V6S_M) // V6S_M.
9262 static const int v7e_m[] =
9264 -1, // PRE_V4.
9265 -1, // V4.
9266 T(V7E_M), // V4T.
9267 T(V7E_M), // V5T.
9268 T(V7E_M), // V5TE.
9269 T(V7E_M), // V5TEJ.
9270 T(V7E_M), // V6.
9271 T(V7E_M), // V6KZ.
9272 T(V7E_M), // V6T2.
9273 T(V7E_M), // V6K.
9274 T(V7E_M), // V7.
9275 T(V7E_M), // V6_M.
9276 T(V7E_M), // V6S_M.
9277 T(V7E_M) // V7E_M.
9279 static const int v4t_plus_v6_m[] =
9281 -1, // PRE_V4.
9282 -1, // V4.
9283 T(V4T), // V4T.
9284 T(V5T), // V5T.
9285 T(V5TE), // V5TE.
9286 T(V5TEJ), // V5TEJ.
9287 T(V6), // V6.
9288 T(V6KZ), // V6KZ.
9289 T(V6T2), // V6T2.
9290 T(V6K), // V6K.
9291 T(V7), // V7.
9292 T(V6_M), // V6_M.
9293 T(V6S_M), // V6S_M.
9294 T(V7E_M), // V7E_M.
9295 T(V4T_PLUS_V6_M) // V4T plus V6_M.
9297 static const int *comb[] =
9299 v6t2,
9300 v6k,
9302 v6_m,
9303 v6s_m,
9304 v7e_m,
9305 // Pseudo-architecture.
9306 v4t_plus_v6_m
9309 // Check we've not got a higher architecture than we know about.
9311 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
9313 gold_error(_("%s: unknown CPU architecture"), name);
9314 return -1;
9317 // Override old tag if we have a Tag_also_compatible_with on the output.
9319 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
9320 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
9321 oldtag = T(V4T_PLUS_V6_M);
9323 // And override the new tag if we have a Tag_also_compatible_with on the
9324 // input.
9326 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
9327 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
9328 newtag = T(V4T_PLUS_V6_M);
9330 // Architectures before V6KZ add features monotonically.
9331 int tagh = std::max(oldtag, newtag);
9332 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
9333 return tagh;
9335 int tagl = std::min(oldtag, newtag);
9336 int result = comb[tagh - T(V6T2)][tagl];
9338 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
9339 // as the canonical version.
9340 if (result == T(V4T_PLUS_V6_M))
9342 result = T(V4T);
9343 *secondary_compat_out = T(V6_M);
9345 else
9346 *secondary_compat_out = -1;
9348 if (result == -1)
9350 gold_error(_("%s: conflicting CPU architectures %d/%d"),
9351 name, oldtag, newtag);
9352 return -1;
9355 return result;
9356 #undef T
9359 // Helper to print AEABI enum tag value.
9361 template<bool big_endian>
9362 std::string
9363 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
9365 static const char *aeabi_enum_names[] =
9366 { "", "variable-size", "32-bit", "" };
9367 const size_t aeabi_enum_names_size =
9368 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
9370 if (value < aeabi_enum_names_size)
9371 return std::string(aeabi_enum_names[value]);
9372 else
9374 char buffer[100];
9375 sprintf(buffer, "<unknown value %u>", value);
9376 return std::string(buffer);
9380 // Return the string value to store in TAG_CPU_name.
9382 template<bool big_endian>
9383 std::string
9384 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
9386 static const char *name_table[] = {
9387 // These aren't real CPU names, but we can't guess
9388 // that from the architecture version alone.
9389 "Pre v4",
9390 "ARM v4",
9391 "ARM v4T",
9392 "ARM v5T",
9393 "ARM v5TE",
9394 "ARM v5TEJ",
9395 "ARM v6",
9396 "ARM v6KZ",
9397 "ARM v6T2",
9398 "ARM v6K",
9399 "ARM v7",
9400 "ARM v6-M",
9401 "ARM v6S-M",
9402 "ARM v7E-M"
9404 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
9406 if (value < name_table_size)
9407 return std::string(name_table[value]);
9408 else
9410 char buffer[100];
9411 sprintf(buffer, "<unknown CPU value %u>", value);
9412 return std::string(buffer);
9416 // Merge object attributes from input file called NAME with those of the
9417 // output. The input object attributes are in the object pointed by PASD.
9419 template<bool big_endian>
9420 void
9421 Target_arm<big_endian>::merge_object_attributes(
9422 const char* name,
9423 const Attributes_section_data* pasd)
9425 // Return if there is no attributes section data.
9426 if (pasd == NULL)
9427 return;
9429 // If output has no object attributes, just copy.
9430 if (this->attributes_section_data_ == NULL)
9432 this->attributes_section_data_ = new Attributes_section_data(*pasd);
9433 return;
9436 const int vendor = Object_attribute::OBJ_ATTR_PROC;
9437 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9438 Object_attribute* out_attr =
9439 this->attributes_section_data_->known_attributes(vendor);
9441 // This needs to happen before Tag_ABI_FP_number_model is merged. */
9442 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
9443 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
9445 // Ignore mismatches if the object doesn't use floating point. */
9446 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
9447 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
9448 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
9449 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0
9450 && parameters->options().warn_mismatch())
9451 gold_error(_("%s uses VFP register arguments, output does not"),
9452 name);
9455 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
9457 // Merge this attribute with existing attributes.
9458 switch (i)
9460 case elfcpp::Tag_CPU_raw_name:
9461 case elfcpp::Tag_CPU_name:
9462 // These are merged after Tag_CPU_arch.
9463 break;
9465 case elfcpp::Tag_ABI_optimization_goals:
9466 case elfcpp::Tag_ABI_FP_optimization_goals:
9467 // Use the first value seen.
9468 break;
9470 case elfcpp::Tag_CPU_arch:
9472 unsigned int saved_out_attr = out_attr->int_value();
9473 // Merge Tag_CPU_arch and Tag_also_compatible_with.
9474 int secondary_compat =
9475 this->get_secondary_compatible_arch(pasd);
9476 int secondary_compat_out =
9477 this->get_secondary_compatible_arch(
9478 this->attributes_section_data_);
9479 out_attr[i].set_int_value(
9480 tag_cpu_arch_combine(name, out_attr[i].int_value(),
9481 &secondary_compat_out,
9482 in_attr[i].int_value(),
9483 secondary_compat));
9484 this->set_secondary_compatible_arch(this->attributes_section_data_,
9485 secondary_compat_out);
9487 // Merge Tag_CPU_name and Tag_CPU_raw_name.
9488 if (out_attr[i].int_value() == saved_out_attr)
9489 ; // Leave the names alone.
9490 else if (out_attr[i].int_value() == in_attr[i].int_value())
9492 // The output architecture has been changed to match the
9493 // input architecture. Use the input names.
9494 out_attr[elfcpp::Tag_CPU_name].set_string_value(
9495 in_attr[elfcpp::Tag_CPU_name].string_value());
9496 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
9497 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
9499 else
9501 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
9502 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
9505 // If we still don't have a value for Tag_CPU_name,
9506 // make one up now. Tag_CPU_raw_name remains blank.
9507 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
9509 const std::string cpu_name =
9510 this->tag_cpu_name_value(out_attr[i].int_value());
9511 // FIXME: If we see an unknown CPU, this will be set
9512 // to "<unknown CPU n>", where n is the attribute value.
9513 // This is different from BFD, which leaves the name alone.
9514 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
9517 break;
9519 case elfcpp::Tag_ARM_ISA_use:
9520 case elfcpp::Tag_THUMB_ISA_use:
9521 case elfcpp::Tag_WMMX_arch:
9522 case elfcpp::Tag_Advanced_SIMD_arch:
9523 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
9524 case elfcpp::Tag_ABI_FP_rounding:
9525 case elfcpp::Tag_ABI_FP_exceptions:
9526 case elfcpp::Tag_ABI_FP_user_exceptions:
9527 case elfcpp::Tag_ABI_FP_number_model:
9528 case elfcpp::Tag_VFP_HP_extension:
9529 case elfcpp::Tag_CPU_unaligned_access:
9530 case elfcpp::Tag_T2EE_use:
9531 case elfcpp::Tag_Virtualization_use:
9532 case elfcpp::Tag_MPextension_use:
9533 // Use the largest value specified.
9534 if (in_attr[i].int_value() > out_attr[i].int_value())
9535 out_attr[i].set_int_value(in_attr[i].int_value());
9536 break;
9538 case elfcpp::Tag_ABI_align8_preserved:
9539 case elfcpp::Tag_ABI_PCS_RO_data:
9540 // Use the smallest value specified.
9541 if (in_attr[i].int_value() < out_attr[i].int_value())
9542 out_attr[i].set_int_value(in_attr[i].int_value());
9543 break;
9545 case elfcpp::Tag_ABI_align8_needed:
9546 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
9547 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
9548 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
9549 == 0)))
9551 // This error message should be enabled once all non-conformant
9552 // binaries in the toolchain have had the attributes set
9553 // properly.
9554 // gold_error(_("output 8-byte data alignment conflicts with %s"),
9555 // name);
9557 // Fall through.
9558 case elfcpp::Tag_ABI_FP_denormal:
9559 case elfcpp::Tag_ABI_PCS_GOT_use:
9561 // These tags have 0 = don't care, 1 = strong requirement,
9562 // 2 = weak requirement.
9563 static const int order_021[3] = {0, 2, 1};
9565 // Use the "greatest" from the sequence 0, 2, 1, or the largest
9566 // value if greater than 2 (for future-proofing).
9567 if ((in_attr[i].int_value() > 2
9568 && in_attr[i].int_value() > out_attr[i].int_value())
9569 || (in_attr[i].int_value() <= 2
9570 && out_attr[i].int_value() <= 2
9571 && (order_021[in_attr[i].int_value()]
9572 > order_021[out_attr[i].int_value()])))
9573 out_attr[i].set_int_value(in_attr[i].int_value());
9575 break;
9577 case elfcpp::Tag_CPU_arch_profile:
9578 if (out_attr[i].int_value() != in_attr[i].int_value())
9580 // 0 will merge with anything.
9581 // 'A' and 'S' merge to 'A'.
9582 // 'R' and 'S' merge to 'R'.
9583 // 'M' and 'A|R|S' is an error.
9584 if (out_attr[i].int_value() == 0
9585 || (out_attr[i].int_value() == 'S'
9586 && (in_attr[i].int_value() == 'A'
9587 || in_attr[i].int_value() == 'R')))
9588 out_attr[i].set_int_value(in_attr[i].int_value());
9589 else if (in_attr[i].int_value() == 0
9590 || (in_attr[i].int_value() == 'S'
9591 && (out_attr[i].int_value() == 'A'
9592 || out_attr[i].int_value() == 'R')))
9593 ; // Do nothing.
9594 else if (parameters->options().warn_mismatch())
9596 gold_error
9597 (_("conflicting architecture profiles %c/%c"),
9598 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
9599 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
9602 break;
9603 case elfcpp::Tag_VFP_arch:
9605 static const struct
9607 int ver;
9608 int regs;
9609 } vfp_versions[7] =
9611 {0, 0},
9612 {1, 16},
9613 {2, 16},
9614 {3, 32},
9615 {3, 16},
9616 {4, 32},
9617 {4, 16}
9620 // Values greater than 6 aren't defined, so just pick the
9621 // biggest.
9622 if (in_attr[i].int_value() > 6
9623 && in_attr[i].int_value() > out_attr[i].int_value())
9625 *out_attr = *in_attr;
9626 break;
9628 // The output uses the superset of input features
9629 // (ISA version) and registers.
9630 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
9631 vfp_versions[out_attr[i].int_value()].ver);
9632 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
9633 vfp_versions[out_attr[i].int_value()].regs);
9634 // This assumes all possible supersets are also a valid
9635 // options.
9636 int newval;
9637 for (newval = 6; newval > 0; newval--)
9639 if (regs == vfp_versions[newval].regs
9640 && ver == vfp_versions[newval].ver)
9641 break;
9643 out_attr[i].set_int_value(newval);
9645 break;
9646 case elfcpp::Tag_PCS_config:
9647 if (out_attr[i].int_value() == 0)
9648 out_attr[i].set_int_value(in_attr[i].int_value());
9649 else if (in_attr[i].int_value() != 0
9650 && out_attr[i].int_value() != 0
9651 && parameters->options().warn_mismatch())
9653 // It's sometimes ok to mix different configs, so this is only
9654 // a warning.
9655 gold_warning(_("%s: conflicting platform configuration"), name);
9657 break;
9658 case elfcpp::Tag_ABI_PCS_R9_use:
9659 if (in_attr[i].int_value() != out_attr[i].int_value()
9660 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
9661 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused
9662 && parameters->options().warn_mismatch())
9664 gold_error(_("%s: conflicting use of R9"), name);
9666 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
9667 out_attr[i].set_int_value(in_attr[i].int_value());
9668 break;
9669 case elfcpp::Tag_ABI_PCS_RW_data:
9670 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
9671 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9672 != elfcpp::AEABI_R9_SB)
9673 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9674 != elfcpp::AEABI_R9_unused)
9675 && parameters->options().warn_mismatch())
9677 gold_error(_("%s: SB relative addressing conflicts with use "
9678 "of R9"),
9679 name);
9681 // Use the smallest value specified.
9682 if (in_attr[i].int_value() < out_attr[i].int_value())
9683 out_attr[i].set_int_value(in_attr[i].int_value());
9684 break;
9685 case elfcpp::Tag_ABI_PCS_wchar_t:
9686 // FIXME: Make it possible to turn off this warning.
9687 if (out_attr[i].int_value()
9688 && in_attr[i].int_value()
9689 && out_attr[i].int_value() != in_attr[i].int_value()
9690 && parameters->options().warn_mismatch())
9692 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
9693 "use %u-byte wchar_t; use of wchar_t values "
9694 "across objects may fail"),
9695 name, in_attr[i].int_value(),
9696 out_attr[i].int_value());
9698 else if (in_attr[i].int_value() && !out_attr[i].int_value())
9699 out_attr[i].set_int_value(in_attr[i].int_value());
9700 break;
9701 case elfcpp::Tag_ABI_enum_size:
9702 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
9704 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
9705 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
9707 // The existing object is compatible with anything.
9708 // Use whatever requirements the new object has.
9709 out_attr[i].set_int_value(in_attr[i].int_value());
9711 // FIXME: Make it possible to turn off this warning.
9712 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
9713 && out_attr[i].int_value() != in_attr[i].int_value()
9714 && parameters->options().warn_mismatch())
9716 unsigned int in_value = in_attr[i].int_value();
9717 unsigned int out_value = out_attr[i].int_value();
9718 gold_warning(_("%s uses %s enums yet the output is to use "
9719 "%s enums; use of enum values across objects "
9720 "may fail"),
9721 name,
9722 this->aeabi_enum_name(in_value).c_str(),
9723 this->aeabi_enum_name(out_value).c_str());
9726 break;
9727 case elfcpp::Tag_ABI_VFP_args:
9728 // Aready done.
9729 break;
9730 case elfcpp::Tag_ABI_WMMX_args:
9731 if (in_attr[i].int_value() != out_attr[i].int_value()
9732 && parameters->options().warn_mismatch())
9734 gold_error(_("%s uses iWMMXt register arguments, output does "
9735 "not"),
9736 name);
9738 break;
9739 case Object_attribute::Tag_compatibility:
9740 // Merged in target-independent code.
9741 break;
9742 case elfcpp::Tag_ABI_HardFP_use:
9743 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
9744 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
9745 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
9746 out_attr[i].set_int_value(3);
9747 else if (in_attr[i].int_value() > out_attr[i].int_value())
9748 out_attr[i].set_int_value(in_attr[i].int_value());
9749 break;
9750 case elfcpp::Tag_ABI_FP_16bit_format:
9751 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
9753 if (in_attr[i].int_value() != out_attr[i].int_value()
9754 && parameters->options().warn_mismatch())
9755 gold_error(_("fp16 format mismatch between %s and output"),
9756 name);
9758 if (in_attr[i].int_value() != 0)
9759 out_attr[i].set_int_value(in_attr[i].int_value());
9760 break;
9762 case elfcpp::Tag_nodefaults:
9763 // This tag is set if it exists, but the value is unused (and is
9764 // typically zero). We don't actually need to do anything here -
9765 // the merge happens automatically when the type flags are merged
9766 // below.
9767 break;
9768 case elfcpp::Tag_also_compatible_with:
9769 // Already done in Tag_CPU_arch.
9770 break;
9771 case elfcpp::Tag_conformance:
9772 // Keep the attribute if it matches. Throw it away otherwise.
9773 // No attribute means no claim to conform.
9774 if (in_attr[i].string_value() != out_attr[i].string_value())
9775 out_attr[i].set_string_value("");
9776 break;
9778 default:
9780 const char* err_object = NULL;
9782 // The "known_obj_attributes" table does contain some undefined
9783 // attributes. Ensure that there are unused.
9784 if (out_attr[i].int_value() != 0
9785 || out_attr[i].string_value() != "")
9786 err_object = "output";
9787 else if (in_attr[i].int_value() != 0
9788 || in_attr[i].string_value() != "")
9789 err_object = name;
9791 if (err_object != NULL
9792 && parameters->options().warn_mismatch())
9794 // Attribute numbers >=64 (mod 128) can be safely ignored.
9795 if ((i & 127) < 64)
9796 gold_error(_("%s: unknown mandatory EABI object attribute "
9797 "%d"),
9798 err_object, i);
9799 else
9800 gold_warning(_("%s: unknown EABI object attribute %d"),
9801 err_object, i);
9804 // Only pass on attributes that match in both inputs.
9805 if (!in_attr[i].matches(out_attr[i]))
9807 out_attr[i].set_int_value(0);
9808 out_attr[i].set_string_value("");
9813 // If out_attr was copied from in_attr then it won't have a type yet.
9814 if (in_attr[i].type() && !out_attr[i].type())
9815 out_attr[i].set_type(in_attr[i].type());
9818 // Merge Tag_compatibility attributes and any common GNU ones.
9819 this->attributes_section_data_->merge(name, pasd);
9821 // Check for any attributes not known on ARM.
9822 typedef Vendor_object_attributes::Other_attributes Other_attributes;
9823 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
9824 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
9825 Other_attributes* out_other_attributes =
9826 this->attributes_section_data_->other_attributes(vendor);
9827 Other_attributes::iterator out_iter = out_other_attributes->begin();
9829 while (in_iter != in_other_attributes->end()
9830 || out_iter != out_other_attributes->end())
9832 const char* err_object = NULL;
9833 int err_tag = 0;
9835 // The tags for each list are in numerical order.
9836 // If the tags are equal, then merge.
9837 if (out_iter != out_other_attributes->end()
9838 && (in_iter == in_other_attributes->end()
9839 || in_iter->first > out_iter->first))
9841 // This attribute only exists in output. We can't merge, and we
9842 // don't know what the tag means, so delete it.
9843 err_object = "output";
9844 err_tag = out_iter->first;
9845 int saved_tag = out_iter->first;
9846 delete out_iter->second;
9847 out_other_attributes->erase(out_iter);
9848 out_iter = out_other_attributes->upper_bound(saved_tag);
9850 else if (in_iter != in_other_attributes->end()
9851 && (out_iter != out_other_attributes->end()
9852 || in_iter->first < out_iter->first))
9854 // This attribute only exists in input. We can't merge, and we
9855 // don't know what the tag means, so ignore it.
9856 err_object = name;
9857 err_tag = in_iter->first;
9858 ++in_iter;
9860 else // The tags are equal.
9862 // As present, all attributes in the list are unknown, and
9863 // therefore can't be merged meaningfully.
9864 err_object = "output";
9865 err_tag = out_iter->first;
9867 // Only pass on attributes that match in both inputs.
9868 if (!in_iter->second->matches(*(out_iter->second)))
9870 // No match. Delete the attribute.
9871 int saved_tag = out_iter->first;
9872 delete out_iter->second;
9873 out_other_attributes->erase(out_iter);
9874 out_iter = out_other_attributes->upper_bound(saved_tag);
9876 else
9878 // Matched. Keep the attribute and move to the next.
9879 ++out_iter;
9880 ++in_iter;
9884 if (err_object && parameters->options().warn_mismatch())
9886 // Attribute numbers >=64 (mod 128) can be safely ignored. */
9887 if ((err_tag & 127) < 64)
9889 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
9890 err_object, err_tag);
9892 else
9894 gold_warning(_("%s: unknown EABI object attribute %d"),
9895 err_object, err_tag);
9901 // Stub-generation methods for Target_arm.
9903 // Make a new Arm_input_section object.
9905 template<bool big_endian>
9906 Arm_input_section<big_endian>*
9907 Target_arm<big_endian>::new_arm_input_section(
9908 Relobj* relobj,
9909 unsigned int shndx)
9911 Section_id sid(relobj, shndx);
9913 Arm_input_section<big_endian>* arm_input_section =
9914 new Arm_input_section<big_endian>(relobj, shndx);
9915 arm_input_section->init();
9917 // Register new Arm_input_section in map for look-up.
9918 std::pair<typename Arm_input_section_map::iterator, bool> ins =
9919 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
9921 // Make sure that it we have not created another Arm_input_section
9922 // for this input section already.
9923 gold_assert(ins.second);
9925 return arm_input_section;
9928 // Find the Arm_input_section object corresponding to the SHNDX-th input
9929 // section of RELOBJ.
9931 template<bool big_endian>
9932 Arm_input_section<big_endian>*
9933 Target_arm<big_endian>::find_arm_input_section(
9934 Relobj* relobj,
9935 unsigned int shndx) const
9937 Section_id sid(relobj, shndx);
9938 typename Arm_input_section_map::const_iterator p =
9939 this->arm_input_section_map_.find(sid);
9940 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
9943 // Make a new stub table.
9945 template<bool big_endian>
9946 Stub_table<big_endian>*
9947 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
9949 Stub_table<big_endian>* stub_table =
9950 new Stub_table<big_endian>(owner);
9951 this->stub_tables_.push_back(stub_table);
9953 stub_table->set_address(owner->address() + owner->data_size());
9954 stub_table->set_file_offset(owner->offset() + owner->data_size());
9955 stub_table->finalize_data_size();
9957 return stub_table;
9960 // Scan a relocation for stub generation.
9962 template<bool big_endian>
9963 void
9964 Target_arm<big_endian>::scan_reloc_for_stub(
9965 const Relocate_info<32, big_endian>* relinfo,
9966 unsigned int r_type,
9967 const Sized_symbol<32>* gsym,
9968 unsigned int r_sym,
9969 const Symbol_value<32>* psymval,
9970 elfcpp::Elf_types<32>::Elf_Swxword addend,
9971 Arm_address address)
9973 typedef typename Target_arm<big_endian>::Relocate Relocate;
9975 const Arm_relobj<big_endian>* arm_relobj =
9976 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
9978 bool target_is_thumb;
9979 Symbol_value<32> symval;
9980 if (gsym != NULL)
9982 // This is a global symbol. Determine if we use PLT and if the
9983 // final target is THUMB.
9984 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
9986 // This uses a PLT, change the symbol value.
9987 symval.set_output_value(this->plt_section()->address()
9988 + gsym->plt_offset());
9989 psymval = &symval;
9990 target_is_thumb = false;
9992 else if (gsym->is_undefined())
9993 // There is no need to generate a stub symbol is undefined.
9994 return;
9995 else
9997 target_is_thumb =
9998 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
9999 || (gsym->type() == elfcpp::STT_FUNC
10000 && !gsym->is_undefined()
10001 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
10004 else
10006 // This is a local symbol. Determine if the final target is THUMB.
10007 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
10010 // Strip LSB if this points to a THUMB target.
10011 const Arm_reloc_property* reloc_property =
10012 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
10013 gold_assert(reloc_property != NULL);
10014 if (target_is_thumb
10015 && reloc_property->uses_thumb_bit()
10016 && ((psymval->value(arm_relobj, 0) & 1) != 0))
10018 Arm_address stripped_value =
10019 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
10020 symval.set_output_value(stripped_value);
10021 psymval = &symval;
10024 // Get the symbol value.
10025 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
10027 // Owing to pipelining, the PC relative branches below actually skip
10028 // two instructions when the branch offset is 0.
10029 Arm_address destination;
10030 switch (r_type)
10032 case elfcpp::R_ARM_CALL:
10033 case elfcpp::R_ARM_JUMP24:
10034 case elfcpp::R_ARM_PLT32:
10035 // ARM branches.
10036 destination = value + addend + 8;
10037 break;
10038 case elfcpp::R_ARM_THM_CALL:
10039 case elfcpp::R_ARM_THM_XPC22:
10040 case elfcpp::R_ARM_THM_JUMP24:
10041 case elfcpp::R_ARM_THM_JUMP19:
10042 // THUMB branches.
10043 destination = value + addend + 4;
10044 break;
10045 default:
10046 gold_unreachable();
10049 Reloc_stub* stub = NULL;
10050 Stub_type stub_type =
10051 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
10052 target_is_thumb);
10053 if (stub_type != arm_stub_none)
10055 // Try looking up an existing stub from a stub table.
10056 Stub_table<big_endian>* stub_table =
10057 arm_relobj->stub_table(relinfo->data_shndx);
10058 gold_assert(stub_table != NULL);
10060 // Locate stub by destination.
10061 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
10063 // Create a stub if there is not one already
10064 stub = stub_table->find_reloc_stub(stub_key);
10065 if (stub == NULL)
10067 // create a new stub and add it to stub table.
10068 stub = this->stub_factory().make_reloc_stub(stub_type);
10069 stub_table->add_reloc_stub(stub, stub_key);
10072 // Record the destination address.
10073 stub->set_destination_address(destination
10074 | (target_is_thumb ? 1 : 0));
10077 // For Cortex-A8, we need to record a relocation at 4K page boundary.
10078 if (this->fix_cortex_a8_
10079 && (r_type == elfcpp::R_ARM_THM_JUMP24
10080 || r_type == elfcpp::R_ARM_THM_JUMP19
10081 || r_type == elfcpp::R_ARM_THM_CALL
10082 || r_type == elfcpp::R_ARM_THM_XPC22)
10083 && (address & 0xfffU) == 0xffeU)
10085 // Found a candidate. Note we haven't checked the destination is
10086 // within 4K here: if we do so (and don't create a record) we can't
10087 // tell that a branch should have been relocated when scanning later.
10088 this->cortex_a8_relocs_info_[address] =
10089 new Cortex_a8_reloc(stub, r_type,
10090 destination | (target_is_thumb ? 1 : 0));
10094 // This function scans a relocation sections for stub generation.
10095 // The template parameter Relocate must be a class type which provides
10096 // a single function, relocate(), which implements the machine
10097 // specific part of a relocation.
10099 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
10100 // SHT_REL or SHT_RELA.
10102 // PRELOCS points to the relocation data. RELOC_COUNT is the number
10103 // of relocs. OUTPUT_SECTION is the output section.
10104 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
10105 // mapped to output offsets.
10107 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
10108 // VIEW_SIZE is the size. These refer to the input section, unless
10109 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
10110 // the output section.
10112 template<bool big_endian>
10113 template<int sh_type>
10114 void inline
10115 Target_arm<big_endian>::scan_reloc_section_for_stubs(
10116 const Relocate_info<32, big_endian>* relinfo,
10117 const unsigned char* prelocs,
10118 size_t reloc_count,
10119 Output_section* output_section,
10120 bool needs_special_offset_handling,
10121 const unsigned char* view,
10122 elfcpp::Elf_types<32>::Elf_Addr view_address,
10123 section_size_type)
10125 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
10126 const int reloc_size =
10127 Reloc_types<sh_type, 32, big_endian>::reloc_size;
10129 Arm_relobj<big_endian>* arm_object =
10130 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10131 unsigned int local_count = arm_object->local_symbol_count();
10133 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
10135 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
10137 Reltype reloc(prelocs);
10139 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10140 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
10141 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
10143 r_type = this->get_real_reloc_type(r_type);
10145 // Only a few relocation types need stubs.
10146 if ((r_type != elfcpp::R_ARM_CALL)
10147 && (r_type != elfcpp::R_ARM_JUMP24)
10148 && (r_type != elfcpp::R_ARM_PLT32)
10149 && (r_type != elfcpp::R_ARM_THM_CALL)
10150 && (r_type != elfcpp::R_ARM_THM_XPC22)
10151 && (r_type != elfcpp::R_ARM_THM_JUMP24)
10152 && (r_type != elfcpp::R_ARM_THM_JUMP19)
10153 && (r_type != elfcpp::R_ARM_V4BX))
10154 continue;
10156 section_offset_type offset =
10157 convert_to_section_size_type(reloc.get_r_offset());
10159 if (needs_special_offset_handling)
10161 offset = output_section->output_offset(relinfo->object,
10162 relinfo->data_shndx,
10163 offset);
10164 if (offset == -1)
10165 continue;
10168 // Create a v4bx stub if --fix-v4bx-interworking is used.
10169 if (r_type == elfcpp::R_ARM_V4BX)
10171 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING)
10173 // Get the BX instruction.
10174 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10175 const Valtype* wv =
10176 reinterpret_cast<const Valtype*>(view + offset);
10177 elfcpp::Elf_types<32>::Elf_Swxword insn =
10178 elfcpp::Swap<32, big_endian>::readval(wv);
10179 const uint32_t reg = (insn & 0xf);
10181 if (reg < 0xf)
10183 // Try looking up an existing stub from a stub table.
10184 Stub_table<big_endian>* stub_table =
10185 arm_object->stub_table(relinfo->data_shndx);
10186 gold_assert(stub_table != NULL);
10188 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
10190 // create a new stub and add it to stub table.
10191 Arm_v4bx_stub* stub =
10192 this->stub_factory().make_arm_v4bx_stub(reg);
10193 gold_assert(stub != NULL);
10194 stub_table->add_arm_v4bx_stub(stub);
10198 continue;
10201 // Get the addend.
10202 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
10203 elfcpp::Elf_types<32>::Elf_Swxword addend =
10204 stub_addend_reader(r_type, view + offset, reloc);
10206 const Sized_symbol<32>* sym;
10208 Symbol_value<32> symval;
10209 const Symbol_value<32> *psymval;
10210 if (r_sym < local_count)
10212 sym = NULL;
10213 psymval = arm_object->local_symbol(r_sym);
10215 // If the local symbol belongs to a section we are discarding,
10216 // and that section is a debug section, try to find the
10217 // corresponding kept section and map this symbol to its
10218 // counterpart in the kept section. The symbol must not
10219 // correspond to a section we are folding.
10220 bool is_ordinary;
10221 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10222 if (is_ordinary
10223 && shndx != elfcpp::SHN_UNDEF
10224 && !arm_object->is_section_included(shndx)
10225 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
10227 if (comdat_behavior == CB_UNDETERMINED)
10229 std::string name =
10230 arm_object->section_name(relinfo->data_shndx);
10231 comdat_behavior = get_comdat_behavior(name.c_str());
10233 if (comdat_behavior == CB_PRETEND)
10235 bool found;
10236 typename elfcpp::Elf_types<32>::Elf_Addr value =
10237 arm_object->map_to_kept_section(shndx, &found);
10238 if (found)
10239 symval.set_output_value(value + psymval->input_value());
10240 else
10241 symval.set_output_value(0);
10243 else
10245 symval.set_output_value(0);
10247 symval.set_no_output_symtab_entry();
10248 psymval = &symval;
10251 else
10253 const Symbol* gsym = arm_object->global_symbol(r_sym);
10254 gold_assert(gsym != NULL);
10255 if (gsym->is_forwarder())
10256 gsym = relinfo->symtab->resolve_forwards(gsym);
10258 sym = static_cast<const Sized_symbol<32>*>(gsym);
10259 if (sym->has_symtab_index())
10260 symval.set_output_symtab_index(sym->symtab_index());
10261 else
10262 symval.set_no_output_symtab_entry();
10264 // We need to compute the would-be final value of this global
10265 // symbol.
10266 const Symbol_table* symtab = relinfo->symtab;
10267 const Sized_symbol<32>* sized_symbol =
10268 symtab->get_sized_symbol<32>(gsym);
10269 Symbol_table::Compute_final_value_status status;
10270 Arm_address value =
10271 symtab->compute_final_value<32>(sized_symbol, &status);
10273 // Skip this if the symbol has not output section.
10274 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
10275 continue;
10277 symval.set_output_value(value);
10278 psymval = &symval;
10281 // If symbol is a section symbol, we don't know the actual type of
10282 // destination. Give up.
10283 if (psymval->is_section_symbol())
10284 continue;
10286 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
10287 addend, view_address + offset);
10291 // Scan an input section for stub generation.
10293 template<bool big_endian>
10294 void
10295 Target_arm<big_endian>::scan_section_for_stubs(
10296 const Relocate_info<32, big_endian>* relinfo,
10297 unsigned int sh_type,
10298 const unsigned char* prelocs,
10299 size_t reloc_count,
10300 Output_section* output_section,
10301 bool needs_special_offset_handling,
10302 const unsigned char* view,
10303 Arm_address view_address,
10304 section_size_type view_size)
10306 if (sh_type == elfcpp::SHT_REL)
10307 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
10308 relinfo,
10309 prelocs,
10310 reloc_count,
10311 output_section,
10312 needs_special_offset_handling,
10313 view,
10314 view_address,
10315 view_size);
10316 else if (sh_type == elfcpp::SHT_RELA)
10317 // We do not support RELA type relocations yet. This is provided for
10318 // completeness.
10319 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
10320 relinfo,
10321 prelocs,
10322 reloc_count,
10323 output_section,
10324 needs_special_offset_handling,
10325 view,
10326 view_address,
10327 view_size);
10328 else
10329 gold_unreachable();
10332 // Group input sections for stub generation.
10334 // We goup input sections in an output sections so that the total size,
10335 // including any padding space due to alignment is smaller than GROUP_SIZE
10336 // unless the only input section in group is bigger than GROUP_SIZE already.
10337 // Then an ARM stub table is created to follow the last input section
10338 // in group. For each group an ARM stub table is created an is placed
10339 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
10340 // extend the group after the stub table.
10342 template<bool big_endian>
10343 void
10344 Target_arm<big_endian>::group_sections(
10345 Layout* layout,
10346 section_size_type group_size,
10347 bool stubs_always_after_branch)
10349 // Group input sections and insert stub table
10350 Layout::Section_list section_list;
10351 layout->get_allocated_sections(&section_list);
10352 for (Layout::Section_list::const_iterator p = section_list.begin();
10353 p != section_list.end();
10354 ++p)
10356 Arm_output_section<big_endian>* output_section =
10357 Arm_output_section<big_endian>::as_arm_output_section(*p);
10358 output_section->group_sections(group_size, stubs_always_after_branch,
10359 this);
10363 // Relaxation hook. This is where we do stub generation.
10365 template<bool big_endian>
10366 bool
10367 Target_arm<big_endian>::do_relax(
10368 int pass,
10369 const Input_objects* input_objects,
10370 Symbol_table* symtab,
10371 Layout* layout)
10373 // No need to generate stubs if this is a relocatable link.
10374 gold_assert(!parameters->options().relocatable());
10376 // If this is the first pass, we need to group input sections into
10377 // stub groups.
10378 bool done_exidx_fixup = false;
10379 if (pass == 1)
10381 // Determine the stub group size. The group size is the absolute
10382 // value of the parameter --stub-group-size. If --stub-group-size
10383 // is passed a negative value, we restict stubs to be always after
10384 // the stubbed branches.
10385 int32_t stub_group_size_param =
10386 parameters->options().stub_group_size();
10387 bool stubs_always_after_branch = stub_group_size_param < 0;
10388 section_size_type stub_group_size = abs(stub_group_size_param);
10390 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
10391 // page as the first half of a 32-bit branch straddling two 4K pages.
10392 // This is a crude way of enforcing that.
10393 if (this->fix_cortex_a8_)
10394 stubs_always_after_branch = true;
10396 if (stub_group_size == 1)
10398 // Default value.
10399 // Thumb branch range is +-4MB has to be used as the default
10400 // maximum size (a given section can contain both ARM and Thumb
10401 // code, so the worst case has to be taken into account). If we are
10402 // fixing cortex-a8 errata, the branch range has to be even smaller,
10403 // since wide conditional branch has a range of +-1MB only.
10405 // This value is 24K less than that, which allows for 2025
10406 // 12-byte stubs. If we exceed that, then we will fail to link.
10407 // The user will have to relink with an explicit group size
10408 // option.
10409 if (this->fix_cortex_a8_)
10410 stub_group_size = 1024276;
10411 else
10412 stub_group_size = 4170000;
10415 group_sections(layout, stub_group_size, stubs_always_after_branch);
10417 // Also fix .ARM.exidx section coverage.
10418 Output_section* os = layout->find_output_section(".ARM.exidx");
10419 if (os != NULL && os->type() == elfcpp::SHT_ARM_EXIDX)
10421 Arm_output_section<big_endian>* exidx_output_section =
10422 Arm_output_section<big_endian>::as_arm_output_section(os);
10423 this->fix_exidx_coverage(layout, exidx_output_section, symtab);
10424 done_exidx_fixup = true;
10428 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
10429 // beginning of each relaxation pass, just blow away all the stubs.
10430 // Alternatively, we could selectively remove only the stubs and reloc
10431 // information for code sections that have moved since the last pass.
10432 // That would require more book-keeping.
10433 typedef typename Stub_table_list::iterator Stub_table_iterator;
10434 if (this->fix_cortex_a8_)
10436 // Clear all Cortex-A8 reloc information.
10437 for (typename Cortex_a8_relocs_info::const_iterator p =
10438 this->cortex_a8_relocs_info_.begin();
10439 p != this->cortex_a8_relocs_info_.end();
10440 ++p)
10441 delete p->second;
10442 this->cortex_a8_relocs_info_.clear();
10444 // Remove all Cortex-A8 stubs.
10445 for (Stub_table_iterator sp = this->stub_tables_.begin();
10446 sp != this->stub_tables_.end();
10447 ++sp)
10448 (*sp)->remove_all_cortex_a8_stubs();
10451 // Scan relocs for relocation stubs
10452 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
10453 op != input_objects->relobj_end();
10454 ++op)
10456 Arm_relobj<big_endian>* arm_relobj =
10457 Arm_relobj<big_endian>::as_arm_relobj(*op);
10458 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
10461 // Check all stub tables to see if any of them have their data sizes
10462 // or addresses alignments changed. These are the only things that
10463 // matter.
10464 bool any_stub_table_changed = false;
10465 Unordered_set<const Output_section*> sections_needing_adjustment;
10466 for (Stub_table_iterator sp = this->stub_tables_.begin();
10467 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
10468 ++sp)
10470 if ((*sp)->update_data_size_and_addralign())
10472 // Update data size of stub table owner.
10473 Arm_input_section<big_endian>* owner = (*sp)->owner();
10474 uint64_t address = owner->address();
10475 off_t offset = owner->offset();
10476 owner->reset_address_and_file_offset();
10477 owner->set_address_and_file_offset(address, offset);
10479 sections_needing_adjustment.insert(owner->output_section());
10480 any_stub_table_changed = true;
10484 // Output_section_data::output_section() returns a const pointer but we
10485 // need to update output sections, so we record all output sections needing
10486 // update above and scan the sections here to find out what sections need
10487 // to be updated.
10488 for(Layout::Section_list::const_iterator p = layout->section_list().begin();
10489 p != layout->section_list().end();
10490 ++p)
10492 if (sections_needing_adjustment.find(*p)
10493 != sections_needing_adjustment.end())
10494 (*p)->set_section_offsets_need_adjustment();
10497 // Stop relaxation if no EXIDX fix-up and no stub table change.
10498 bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
10500 // Finalize the stubs in the last relaxation pass.
10501 if (!continue_relaxation)
10503 for (Stub_table_iterator sp = this->stub_tables_.begin();
10504 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
10505 ++sp)
10506 (*sp)->finalize_stubs();
10508 // Update output local symbol counts of objects if necessary.
10509 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
10510 op != input_objects->relobj_end();
10511 ++op)
10513 Arm_relobj<big_endian>* arm_relobj =
10514 Arm_relobj<big_endian>::as_arm_relobj(*op);
10516 // Update output local symbol counts. We need to discard local
10517 // symbols defined in parts of input sections that are discarded by
10518 // relaxation.
10519 if (arm_relobj->output_local_symbol_count_needs_update())
10520 arm_relobj->update_output_local_symbol_count();
10524 return continue_relaxation;
10527 // Relocate a stub.
10529 template<bool big_endian>
10530 void
10531 Target_arm<big_endian>::relocate_stub(
10532 Stub* stub,
10533 const Relocate_info<32, big_endian>* relinfo,
10534 Output_section* output_section,
10535 unsigned char* view,
10536 Arm_address address,
10537 section_size_type view_size)
10539 Relocate relocate;
10540 const Stub_template* stub_template = stub->stub_template();
10541 for (size_t i = 0; i < stub_template->reloc_count(); i++)
10543 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
10544 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
10546 unsigned int r_type = insn->r_type();
10547 section_size_type reloc_offset = stub_template->reloc_offset(i);
10548 section_size_type reloc_size = insn->size();
10549 gold_assert(reloc_offset + reloc_size <= view_size);
10551 // This is the address of the stub destination.
10552 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
10553 Symbol_value<32> symval;
10554 symval.set_output_value(target);
10556 // Synthesize a fake reloc just in case. We don't have a symbol so
10557 // we use 0.
10558 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
10559 memset(reloc_buffer, 0, sizeof(reloc_buffer));
10560 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
10561 reloc_write.put_r_offset(reloc_offset);
10562 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
10563 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
10565 relocate.relocate(relinfo, this, output_section,
10566 this->fake_relnum_for_stubs, rel, r_type,
10567 NULL, &symval, view + reloc_offset,
10568 address + reloc_offset, reloc_size);
10572 // Determine whether an object attribute tag takes an integer, a
10573 // string or both.
10575 template<bool big_endian>
10577 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
10579 if (tag == Object_attribute::Tag_compatibility)
10580 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10581 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
10582 else if (tag == elfcpp::Tag_nodefaults)
10583 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10584 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
10585 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
10586 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
10587 else if (tag < 32)
10588 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
10589 else
10590 return ((tag & 1) != 0
10591 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
10592 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10595 // Reorder attributes.
10597 // The ABI defines that Tag_conformance should be emitted first, and that
10598 // Tag_nodefaults should be second (if either is defined). This sets those
10599 // two positions, and bumps up the position of all the remaining tags to
10600 // compensate.
10602 template<bool big_endian>
10604 Target_arm<big_endian>::do_attributes_order(int num) const
10606 // Reorder the known object attributes in output. We want to move
10607 // Tag_conformance to position 4 and Tag_conformance to position 5
10608 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
10609 if (num == 4)
10610 return elfcpp::Tag_conformance;
10611 if (num == 5)
10612 return elfcpp::Tag_nodefaults;
10613 if ((num - 2) < elfcpp::Tag_nodefaults)
10614 return num - 2;
10615 if ((num - 1) < elfcpp::Tag_conformance)
10616 return num - 1;
10617 return num;
10620 // Scan a span of THUMB code for Cortex-A8 erratum.
10622 template<bool big_endian>
10623 void
10624 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
10625 Arm_relobj<big_endian>* arm_relobj,
10626 unsigned int shndx,
10627 section_size_type span_start,
10628 section_size_type span_end,
10629 const unsigned char* view,
10630 Arm_address address)
10632 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
10634 // The opcode is BLX.W, BL.W, B.W, Bcc.W
10635 // The branch target is in the same 4KB region as the
10636 // first half of the branch.
10637 // The instruction before the branch is a 32-bit
10638 // length non-branch instruction.
10639 section_size_type i = span_start;
10640 bool last_was_32bit = false;
10641 bool last_was_branch = false;
10642 while (i < span_end)
10644 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10645 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
10646 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
10647 bool is_blx = false, is_b = false;
10648 bool is_bl = false, is_bcc = false;
10650 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
10651 if (insn_32bit)
10653 // Load the rest of the insn (in manual-friendly order).
10654 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
10656 // Encoding T4: B<c>.W.
10657 is_b = (insn & 0xf800d000U) == 0xf0009000U;
10658 // Encoding T1: BL<c>.W.
10659 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
10660 // Encoding T2: BLX<c>.W.
10661 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
10662 // Encoding T3: B<c>.W (not permitted in IT block).
10663 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
10664 && (insn & 0x07f00000U) != 0x03800000U);
10667 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
10669 // If this instruction is a 32-bit THUMB branch that crosses a 4K
10670 // page boundary and it follows 32-bit non-branch instruction,
10671 // we need to work around.
10672 if (is_32bit_branch
10673 && ((address + i) & 0xfffU) == 0xffeU
10674 && last_was_32bit
10675 && !last_was_branch)
10677 // Check to see if there is a relocation stub for this branch.
10678 bool force_target_arm = false;
10679 bool force_target_thumb = false;
10680 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
10681 Cortex_a8_relocs_info::const_iterator p =
10682 this->cortex_a8_relocs_info_.find(address + i);
10684 if (p != this->cortex_a8_relocs_info_.end())
10686 cortex_a8_reloc = p->second;
10687 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
10689 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10690 && !target_is_thumb)
10691 force_target_arm = true;
10692 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10693 && target_is_thumb)
10694 force_target_thumb = true;
10697 off_t offset;
10698 Stub_type stub_type = arm_stub_none;
10700 // Check if we have an offending branch instruction.
10701 uint16_t upper_insn = (insn >> 16) & 0xffffU;
10702 uint16_t lower_insn = insn & 0xffffU;
10703 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10705 if (cortex_a8_reloc != NULL
10706 && cortex_a8_reloc->reloc_stub() != NULL)
10707 // We've already made a stub for this instruction, e.g.
10708 // it's a long branch or a Thumb->ARM stub. Assume that
10709 // stub will suffice to work around the A8 erratum (see
10710 // setting of always_after_branch above).
10712 else if (is_bcc)
10714 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
10715 lower_insn);
10716 stub_type = arm_stub_a8_veneer_b_cond;
10718 else if (is_b || is_bl || is_blx)
10720 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
10721 lower_insn);
10722 if (is_blx)
10723 offset &= ~3;
10725 stub_type = (is_blx
10726 ? arm_stub_a8_veneer_blx
10727 : (is_bl
10728 ? arm_stub_a8_veneer_bl
10729 : arm_stub_a8_veneer_b));
10732 if (stub_type != arm_stub_none)
10734 Arm_address pc_for_insn = address + i + 4;
10736 // The original instruction is a BL, but the target is
10737 // an ARM instruction. If we were not making a stub,
10738 // the BL would have been converted to a BLX. Use the
10739 // BLX stub instead in that case.
10740 if (this->may_use_blx() && force_target_arm
10741 && stub_type == arm_stub_a8_veneer_bl)
10743 stub_type = arm_stub_a8_veneer_blx;
10744 is_blx = true;
10745 is_bl = false;
10747 // Conversely, if the original instruction was
10748 // BLX but the target is Thumb mode, use the BL stub.
10749 else if (force_target_thumb
10750 && stub_type == arm_stub_a8_veneer_blx)
10752 stub_type = arm_stub_a8_veneer_bl;
10753 is_blx = false;
10754 is_bl = true;
10757 if (is_blx)
10758 pc_for_insn &= ~3;
10760 // If we found a relocation, use the proper destination,
10761 // not the offset in the (unrelocated) instruction.
10762 // Note this is always done if we switched the stub type above.
10763 if (cortex_a8_reloc != NULL)
10764 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
10766 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
10768 // Add a new stub if destination address in in the same page.
10769 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
10771 Cortex_a8_stub* stub =
10772 this->stub_factory_.make_cortex_a8_stub(stub_type,
10773 arm_relobj, shndx,
10774 address + i,
10775 target, insn);
10776 Stub_table<big_endian>* stub_table =
10777 arm_relobj->stub_table(shndx);
10778 gold_assert(stub_table != NULL);
10779 stub_table->add_cortex_a8_stub(address + i, stub);
10784 i += insn_32bit ? 4 : 2;
10785 last_was_32bit = insn_32bit;
10786 last_was_branch = is_32bit_branch;
10790 // Apply the Cortex-A8 workaround.
10792 template<bool big_endian>
10793 void
10794 Target_arm<big_endian>::apply_cortex_a8_workaround(
10795 const Cortex_a8_stub* stub,
10796 Arm_address stub_address,
10797 unsigned char* insn_view,
10798 Arm_address insn_address)
10800 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10801 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
10802 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
10803 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
10804 off_t branch_offset = stub_address - (insn_address + 4);
10806 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10807 switch (stub->stub_template()->type())
10809 case arm_stub_a8_veneer_b_cond:
10810 gold_assert(!utils::has_overflow<21>(branch_offset));
10811 upper_insn = RelocFuncs::thumb32_cond_branch_upper(upper_insn,
10812 branch_offset);
10813 lower_insn = RelocFuncs::thumb32_cond_branch_lower(lower_insn,
10814 branch_offset);
10815 break;
10817 case arm_stub_a8_veneer_b:
10818 case arm_stub_a8_veneer_bl:
10819 case arm_stub_a8_veneer_blx:
10820 if ((lower_insn & 0x5000U) == 0x4000U)
10821 // For a BLX instruction, make sure that the relocation is
10822 // rounded up to a word boundary. This follows the semantics of
10823 // the instruction which specifies that bit 1 of the target
10824 // address will come from bit 1 of the base address.
10825 branch_offset = (branch_offset + 2) & ~3;
10827 // Put BRANCH_OFFSET back into the insn.
10828 gold_assert(!utils::has_overflow<25>(branch_offset));
10829 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
10830 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
10831 break;
10833 default:
10834 gold_unreachable();
10837 // Put the relocated value back in the object file:
10838 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
10839 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
10842 template<bool big_endian>
10843 class Target_selector_arm : public Target_selector
10845 public:
10846 Target_selector_arm()
10847 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
10848 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
10851 Target*
10852 do_instantiate_target()
10853 { return new Target_arm<big_endian>(); }
10856 // Fix .ARM.exidx section coverage.
10858 template<bool big_endian>
10859 void
10860 Target_arm<big_endian>::fix_exidx_coverage(
10861 Layout* layout,
10862 Arm_output_section<big_endian>* exidx_section,
10863 Symbol_table* symtab)
10865 // We need to look at all the input sections in output in ascending
10866 // order of of output address. We do that by building a sorted list
10867 // of output sections by addresses. Then we looks at the output sections
10868 // in order. The input sections in an output section are already sorted
10869 // by addresses within the output section.
10871 typedef std::set<Output_section*, output_section_address_less_than>
10872 Sorted_output_section_list;
10873 Sorted_output_section_list sorted_output_sections;
10874 Layout::Section_list section_list;
10875 layout->get_allocated_sections(&section_list);
10876 for (Layout::Section_list::const_iterator p = section_list.begin();
10877 p != section_list.end();
10878 ++p)
10880 // We only care about output sections that contain executable code.
10881 if (((*p)->flags() & elfcpp::SHF_EXECINSTR) != 0)
10882 sorted_output_sections.insert(*p);
10885 // Go over the output sections in ascending order of output addresses.
10886 typedef typename Arm_output_section<big_endian>::Text_section_list
10887 Text_section_list;
10888 Text_section_list sorted_text_sections;
10889 for(typename Sorted_output_section_list::iterator p =
10890 sorted_output_sections.begin();
10891 p != sorted_output_sections.end();
10892 ++p)
10894 Arm_output_section<big_endian>* arm_output_section =
10895 Arm_output_section<big_endian>::as_arm_output_section(*p);
10896 arm_output_section->append_text_sections_to_list(&sorted_text_sections);
10899 exidx_section->fix_exidx_coverage(layout, sorted_text_sections, symtab,
10900 merge_exidx_entries());
10903 Target_selector_arm<false> target_selector_arm;
10904 Target_selector_arm<true> target_selector_armbe;
10906 } // End anonymous namespace.