daily update
[binutils.git] / gold / arm.cc
blob6c81d3071b94b93982cf3a0afbcb744fc4a98cf0
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 be 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 an invalid index, this points to a global symbol.
602 // Otherwise, it points to a relobj. We used the unsized and target
603 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
604 // Arm_relobj, in order 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 using KEY. The caller is responsible for avoiding addition
899 // if a STUB with the same key has already 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 // The caller is responsible for avoiding addition if a STUB with the same
919 // address has already 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 // Write to a map file.
1068 void
1069 do_print_to_mapfile(Mapfile* mapfile) const
1070 { mapfile->print_output_data(this, _("** ARM cantunwind")); }
1072 private:
1073 // Implement do_write for a given endianness.
1074 template<bool big_endian>
1075 void inline
1076 do_fixed_endian_write(Output_file*);
1078 // The object containing the section pointed by this.
1079 Relobj* relobj_;
1080 // The section index of the section pointed by this.
1081 unsigned int shndx_;
1084 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1085 // Offset map is used to map input section offset within the EXIDX section
1086 // to the output offset from the start of this EXIDX section.
1088 typedef std::map<section_offset_type, section_offset_type>
1089 Arm_exidx_section_offset_map;
1091 // Arm_exidx_merged_section class. This represents an EXIDX input section
1092 // with some of its entries merged.
1094 class Arm_exidx_merged_section : public Output_relaxed_input_section
1096 public:
1097 // Constructor for Arm_exidx_merged_section.
1098 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1099 // SECTION_OFFSET_MAP points to a section offset map describing how
1100 // parts of the input section are mapped to output. DELETED_BYTES is
1101 // the number of bytes deleted from the EXIDX input section.
1102 Arm_exidx_merged_section(
1103 const Arm_exidx_input_section& exidx_input_section,
1104 const Arm_exidx_section_offset_map& section_offset_map,
1105 uint32_t deleted_bytes);
1107 // Build output contents.
1108 void
1109 build_contents(const unsigned char*, section_size_type);
1111 // Return the original EXIDX input section.
1112 const Arm_exidx_input_section&
1113 exidx_input_section() const
1114 { return this->exidx_input_section_; }
1116 // Return the section offset map.
1117 const Arm_exidx_section_offset_map&
1118 section_offset_map() const
1119 { return this->section_offset_map_; }
1121 protected:
1122 // Write merged section into file OF.
1123 void
1124 do_write(Output_file* of);
1126 bool
1127 do_output_offset(const Relobj*, unsigned int, section_offset_type,
1128 section_offset_type*) const;
1130 private:
1131 // Original EXIDX input section.
1132 const Arm_exidx_input_section& exidx_input_section_;
1133 // Section offset map.
1134 const Arm_exidx_section_offset_map& section_offset_map_;
1135 // Merged section contents. We need to keep build the merged section
1136 // and save it here to avoid accessing the original EXIDX section when
1137 // we cannot lock the sections' object.
1138 unsigned char* section_contents_;
1141 // A class to wrap an ordinary input section containing executable code.
1143 template<bool big_endian>
1144 class Arm_input_section : public Output_relaxed_input_section
1146 public:
1147 Arm_input_section(Relobj* relobj, unsigned int shndx)
1148 : Output_relaxed_input_section(relobj, shndx, 1),
1149 original_addralign_(1), original_size_(0), stub_table_(NULL),
1150 original_contents_(NULL)
1153 ~Arm_input_section()
1154 { delete[] this->original_contents_; }
1156 // Initialize.
1157 void
1158 init();
1160 // Whether this is a stub table owner.
1161 bool
1162 is_stub_table_owner() const
1163 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1165 // Return the stub table.
1166 Stub_table<big_endian>*
1167 stub_table() const
1168 { return this->stub_table_; }
1170 // Set the stub_table.
1171 void
1172 set_stub_table(Stub_table<big_endian>* stub_table)
1173 { this->stub_table_ = stub_table; }
1175 // Downcast a base pointer to an Arm_input_section pointer. This is
1176 // not type-safe but we only use Arm_input_section not the base class.
1177 static Arm_input_section<big_endian>*
1178 as_arm_input_section(Output_relaxed_input_section* poris)
1179 { return static_cast<Arm_input_section<big_endian>*>(poris); }
1181 // Return the original size of the section.
1182 uint32_t
1183 original_size() const
1184 { return this->original_size_; }
1186 protected:
1187 // Write data to output file.
1188 void
1189 do_write(Output_file*);
1191 // Return required alignment of this.
1192 uint64_t
1193 do_addralign() const
1195 if (this->is_stub_table_owner())
1196 return std::max(this->stub_table_->addralign(),
1197 static_cast<uint64_t>(this->original_addralign_));
1198 else
1199 return this->original_addralign_;
1202 // Finalize data size.
1203 void
1204 set_final_data_size();
1206 // Reset address and file offset.
1207 void
1208 do_reset_address_and_file_offset();
1210 // Output offset.
1211 bool
1212 do_output_offset(const Relobj* object, unsigned int shndx,
1213 section_offset_type offset,
1214 section_offset_type* poutput) const
1216 if ((object == this->relobj())
1217 && (shndx == this->shndx())
1218 && (offset >= 0)
1219 && (offset <=
1220 convert_types<section_offset_type, uint32_t>(this->original_size_)))
1222 *poutput = offset;
1223 return true;
1225 else
1226 return false;
1229 private:
1230 // Copying is not allowed.
1231 Arm_input_section(const Arm_input_section&);
1232 Arm_input_section& operator=(const Arm_input_section&);
1234 // Address alignment of the original input section.
1235 uint32_t original_addralign_;
1236 // Section size of the original input section.
1237 uint32_t original_size_;
1238 // Stub table.
1239 Stub_table<big_endian>* stub_table_;
1240 // Original section contents. We have to make a copy here since the file
1241 // containing the original section may not be locked when we need to access
1242 // the contents.
1243 unsigned char* original_contents_;
1246 // Arm_exidx_fixup class. This is used to define a number of methods
1247 // and keep states for fixing up EXIDX coverage.
1249 class Arm_exidx_fixup
1251 public:
1252 Arm_exidx_fixup(Output_section* exidx_output_section,
1253 bool merge_exidx_entries = true)
1254 : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1255 last_inlined_entry_(0), last_input_section_(NULL),
1256 section_offset_map_(NULL), first_output_text_section_(NULL),
1257 merge_exidx_entries_(merge_exidx_entries)
1260 ~Arm_exidx_fixup()
1261 { delete this->section_offset_map_; }
1263 // Process an EXIDX section for entry merging. SECTION_CONTENTS points
1264 // to the EXIDX contents and SECTION_SIZE is the size of the contents. Return
1265 // number of bytes to be deleted in output. If parts of the input EXIDX
1266 // section are merged a heap allocated Arm_exidx_section_offset_map is store
1267 // in the located PSECTION_OFFSET_MAP. The caller owns the map and is
1268 // responsible for releasing it.
1269 template<bool big_endian>
1270 uint32_t
1271 process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1272 const unsigned char* section_contents,
1273 section_size_type section_size,
1274 Arm_exidx_section_offset_map** psection_offset_map);
1276 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1277 // input section, if there is not one already.
1278 void
1279 add_exidx_cantunwind_as_needed();
1281 // Return the output section for the text section which is linked to the
1282 // first exidx input in output.
1283 Output_section*
1284 first_output_text_section() const
1285 { return this->first_output_text_section_; }
1287 private:
1288 // Copying is not allowed.
1289 Arm_exidx_fixup(const Arm_exidx_fixup&);
1290 Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1292 // Type of EXIDX unwind entry.
1293 enum Unwind_type
1295 // No type.
1296 UT_NONE,
1297 // EXIDX_CANTUNWIND.
1298 UT_EXIDX_CANTUNWIND,
1299 // Inlined entry.
1300 UT_INLINED_ENTRY,
1301 // Normal entry.
1302 UT_NORMAL_ENTRY,
1305 // Process an EXIDX entry. We only care about the second word of the
1306 // entry. Return true if the entry can be deleted.
1307 bool
1308 process_exidx_entry(uint32_t second_word);
1310 // Update the current section offset map during EXIDX section fix-up.
1311 // If there is no map, create one. INPUT_OFFSET is the offset of a
1312 // reference point, DELETED_BYTES is the number of deleted by in the
1313 // section so far. If DELETE_ENTRY is true, the reference point and
1314 // all offsets after the previous reference point are discarded.
1315 void
1316 update_offset_map(section_offset_type input_offset,
1317 section_size_type deleted_bytes, bool delete_entry);
1319 // EXIDX output section.
1320 Output_section* exidx_output_section_;
1321 // Unwind type of the last EXIDX entry processed.
1322 Unwind_type last_unwind_type_;
1323 // Last seen inlined EXIDX entry.
1324 uint32_t last_inlined_entry_;
1325 // Last processed EXIDX input section.
1326 const Arm_exidx_input_section* last_input_section_;
1327 // Section offset map created in process_exidx_section.
1328 Arm_exidx_section_offset_map* section_offset_map_;
1329 // Output section for the text section which is linked to the first exidx
1330 // input in output.
1331 Output_section* first_output_text_section_;
1333 bool merge_exidx_entries_;
1336 // Arm output section class. This is defined mainly to add a number of
1337 // stub generation methods.
1339 template<bool big_endian>
1340 class Arm_output_section : public Output_section
1342 public:
1343 typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
1345 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1346 elfcpp::Elf_Xword flags)
1347 : Output_section(name, type, flags)
1349 if (type == elfcpp::SHT_ARM_EXIDX)
1350 this->set_always_keeps_input_sections();
1353 ~Arm_output_section()
1356 // Group input sections for stub generation.
1357 void
1358 group_sections(section_size_type, bool, Target_arm<big_endian>*, const Task*);
1360 // Downcast a base pointer to an Arm_output_section pointer. This is
1361 // not type-safe but we only use Arm_output_section not the base class.
1362 static Arm_output_section<big_endian>*
1363 as_arm_output_section(Output_section* os)
1364 { return static_cast<Arm_output_section<big_endian>*>(os); }
1366 // Append all input text sections in this into LIST.
1367 void
1368 append_text_sections_to_list(Text_section_list* list);
1370 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1371 // is a list of text input sections sorted in ascending order of their
1372 // output addresses.
1373 void
1374 fix_exidx_coverage(Layout* layout,
1375 const Text_section_list& sorted_text_section,
1376 Symbol_table* symtab,
1377 bool merge_exidx_entries,
1378 const Task* task);
1380 // Link an EXIDX section into its corresponding text section.
1381 void
1382 set_exidx_section_link();
1384 private:
1385 // For convenience.
1386 typedef Output_section::Input_section Input_section;
1387 typedef Output_section::Input_section_list Input_section_list;
1389 // Create a stub group.
1390 void create_stub_group(Input_section_list::const_iterator,
1391 Input_section_list::const_iterator,
1392 Input_section_list::const_iterator,
1393 Target_arm<big_endian>*,
1394 std::vector<Output_relaxed_input_section*>*,
1395 const Task* task);
1398 // Arm_exidx_input_section class. This represents an EXIDX input section.
1400 class Arm_exidx_input_section
1402 public:
1403 static const section_offset_type invalid_offset =
1404 static_cast<section_offset_type>(-1);
1406 Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1407 unsigned int link, uint32_t size,
1408 uint32_t addralign, uint32_t text_size)
1409 : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1410 addralign_(addralign), text_size_(text_size), has_errors_(false)
1413 ~Arm_exidx_input_section()
1416 // Accessors: This is a read-only class.
1418 // Return the object containing this EXIDX input section.
1419 Relobj*
1420 relobj() const
1421 { return this->relobj_; }
1423 // Return the section index of this EXIDX input section.
1424 unsigned int
1425 shndx() const
1426 { return this->shndx_; }
1428 // Return the section index of linked text section in the same object.
1429 unsigned int
1430 link() const
1431 { return this->link_; }
1433 // Return size of the EXIDX input section.
1434 uint32_t
1435 size() const
1436 { return this->size_; }
1438 // Return address alignment of EXIDX input section.
1439 uint32_t
1440 addralign() const
1441 { return this->addralign_; }
1443 // Return size of the associated text input section.
1444 uint32_t
1445 text_size() const
1446 { return this->text_size_; }
1448 // Whether there are any errors in the EXIDX input section.
1449 bool
1450 has_errors() const
1451 { return this->has_errors_; }
1453 // Set has-errors flag.
1454 void
1455 set_has_errors()
1456 { this->has_errors_ = true; }
1458 private:
1459 // Object containing this.
1460 Relobj* relobj_;
1461 // Section index of this.
1462 unsigned int shndx_;
1463 // text section linked to this in the same object.
1464 unsigned int link_;
1465 // Size of this. For ARM 32-bit is sufficient.
1466 uint32_t size_;
1467 // Address alignment of this. For ARM 32-bit is sufficient.
1468 uint32_t addralign_;
1469 // Size of associated text section.
1470 uint32_t text_size_;
1471 // Whether this has any errors.
1472 bool has_errors_;
1475 // Arm_relobj class.
1477 template<bool big_endian>
1478 class Arm_relobj : public Sized_relobj<32, big_endian>
1480 public:
1481 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1483 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1484 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1485 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1486 stub_tables_(), local_symbol_is_thumb_function_(),
1487 attributes_section_data_(NULL), mapping_symbols_info_(),
1488 section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
1489 output_local_symbol_count_needs_update_(false),
1490 merge_flags_and_attributes_(true)
1493 ~Arm_relobj()
1494 { delete this->attributes_section_data_; }
1496 // Return the stub table of the SHNDX-th section if there is one.
1497 Stub_table<big_endian>*
1498 stub_table(unsigned int shndx) const
1500 gold_assert(shndx < this->stub_tables_.size());
1501 return this->stub_tables_[shndx];
1504 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1505 void
1506 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1508 gold_assert(shndx < this->stub_tables_.size());
1509 this->stub_tables_[shndx] = stub_table;
1512 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1513 // index. This is only valid after do_count_local_symbol is called.
1514 bool
1515 local_symbol_is_thumb_function(unsigned int r_sym) const
1517 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1518 return this->local_symbol_is_thumb_function_[r_sym];
1521 // Scan all relocation sections for stub generation.
1522 void
1523 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1524 const Layout*);
1526 // Convert regular input section with index SHNDX to a relaxed section.
1527 void
1528 convert_input_section_to_relaxed_section(unsigned shndx)
1530 // The stubs have relocations and we need to process them after writing
1531 // out the stubs. So relocation now must follow section write.
1532 this->set_section_offset(shndx, -1ULL);
1533 this->set_relocs_must_follow_section_writes();
1536 // Downcast a base pointer to an Arm_relobj pointer. This is
1537 // not type-safe but we only use Arm_relobj not the base class.
1538 static Arm_relobj<big_endian>*
1539 as_arm_relobj(Relobj* relobj)
1540 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1542 // Processor-specific flags in ELF file header. This is valid only after
1543 // reading symbols.
1544 elfcpp::Elf_Word
1545 processor_specific_flags() const
1546 { return this->processor_specific_flags_; }
1548 // Attribute section data This is the contents of the .ARM.attribute section
1549 // if there is one.
1550 const Attributes_section_data*
1551 attributes_section_data() const
1552 { return this->attributes_section_data_; }
1554 // Mapping symbol location.
1555 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1557 // Functor for STL container.
1558 struct Mapping_symbol_position_less
1560 bool
1561 operator()(const Mapping_symbol_position& p1,
1562 const Mapping_symbol_position& p2) const
1564 return (p1.first < p2.first
1565 || (p1.first == p2.first && p1.second < p2.second));
1569 // We only care about the first character of a mapping symbol, so
1570 // we only store that instead of the whole symbol name.
1571 typedef std::map<Mapping_symbol_position, char,
1572 Mapping_symbol_position_less> Mapping_symbols_info;
1574 // Whether a section contains any Cortex-A8 workaround.
1575 bool
1576 section_has_cortex_a8_workaround(unsigned int shndx) const
1578 return (this->section_has_cortex_a8_workaround_ != NULL
1579 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1582 // Mark a section that has Cortex-A8 workaround.
1583 void
1584 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1586 if (this->section_has_cortex_a8_workaround_ == NULL)
1587 this->section_has_cortex_a8_workaround_ =
1588 new std::vector<bool>(this->shnum(), false);
1589 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1592 // Return the EXIDX section of an text section with index SHNDX or NULL
1593 // if the text section has no associated EXIDX section.
1594 const Arm_exidx_input_section*
1595 exidx_input_section_by_link(unsigned int shndx) const
1597 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1598 return ((p != this->exidx_section_map_.end()
1599 && p->second->link() == shndx)
1600 ? p->second
1601 : NULL);
1604 // Return the EXIDX section with index SHNDX or NULL if there is none.
1605 const Arm_exidx_input_section*
1606 exidx_input_section_by_shndx(unsigned shndx) const
1608 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1609 return ((p != this->exidx_section_map_.end()
1610 && p->second->shndx() == shndx)
1611 ? p->second
1612 : NULL);
1615 // Whether output local symbol count needs updating.
1616 bool
1617 output_local_symbol_count_needs_update() const
1618 { return this->output_local_symbol_count_needs_update_; }
1620 // Set output_local_symbol_count_needs_update flag to be true.
1621 void
1622 set_output_local_symbol_count_needs_update()
1623 { this->output_local_symbol_count_needs_update_ = true; }
1625 // Update output local symbol count at the end of relaxation.
1626 void
1627 update_output_local_symbol_count();
1629 // Whether we want to merge processor-specific flags and attributes.
1630 bool
1631 merge_flags_and_attributes() const
1632 { return this->merge_flags_and_attributes_; }
1634 // Export list of EXIDX section indices.
1635 void
1636 get_exidx_shndx_list(std::vector<unsigned int>* list) const
1638 list->clear();
1639 for (Exidx_section_map::const_iterator p = this->exidx_section_map_.begin();
1640 p != this->exidx_section_map_.end();
1641 ++p)
1643 if (p->second->shndx() == p->first)
1644 list->push_back(p->first);
1646 // Sort list to make result independent of implementation of map.
1647 std::sort(list->begin(), list->end());
1650 protected:
1651 // Post constructor setup.
1652 void
1653 do_setup()
1655 // Call parent's setup method.
1656 Sized_relobj<32, big_endian>::do_setup();
1658 // Initialize look-up tables.
1659 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1660 this->stub_tables_.swap(empty_stub_table_list);
1663 // Count the local symbols.
1664 void
1665 do_count_local_symbols(Stringpool_template<char>*,
1666 Stringpool_template<char>*);
1668 void
1669 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1670 const unsigned char* pshdrs, Output_file* of,
1671 typename Sized_relobj<32, big_endian>::Views* pivews);
1673 // Read the symbol information.
1674 void
1675 do_read_symbols(Read_symbols_data* sd);
1677 // Process relocs for garbage collection.
1678 void
1679 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1681 private:
1683 // Whether a section needs to be scanned for relocation stubs.
1684 bool
1685 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1686 const Relobj::Output_sections&,
1687 const Symbol_table*, const unsigned char*);
1689 // Whether a section is a scannable text section.
1690 bool
1691 section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
1692 const Output_section*, const Symbol_table*);
1694 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1695 bool
1696 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1697 unsigned int, Output_section*,
1698 const Symbol_table*);
1700 // Scan a section for the Cortex-A8 erratum.
1701 void
1702 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1703 unsigned int, Output_section*,
1704 Target_arm<big_endian>*);
1706 // Find the linked text section of an EXIDX section by looking at the
1707 // first relocation of the EXIDX section. PSHDR points to the section
1708 // headers of a relocation section and PSYMS points to the local symbols.
1709 // PSHNDX points to a location storing the text section index if found.
1710 // Return whether we can find the linked section.
1711 bool
1712 find_linked_text_section(const unsigned char* pshdr,
1713 const unsigned char* psyms, unsigned int* pshndx);
1716 // Make a new Arm_exidx_input_section object for EXIDX section with
1717 // index SHNDX and section header SHDR. TEXT_SHNDX is the section
1718 // index of the linked text section.
1719 void
1720 make_exidx_input_section(unsigned int shndx,
1721 const elfcpp::Shdr<32, big_endian>& shdr,
1722 unsigned int text_shndx,
1723 const elfcpp::Shdr<32, big_endian>& text_shdr);
1725 // Return the output address of either a plain input section or a
1726 // relaxed input section. SHNDX is the section index.
1727 Arm_address
1728 simple_input_section_output_address(unsigned int, Output_section*);
1730 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1731 typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1732 Exidx_section_map;
1734 // List of stub tables.
1735 Stub_table_list stub_tables_;
1736 // Bit vector to tell if a local symbol is a thumb function or not.
1737 // This is only valid after do_count_local_symbol is called.
1738 std::vector<bool> local_symbol_is_thumb_function_;
1739 // processor-specific flags in ELF file header.
1740 elfcpp::Elf_Word processor_specific_flags_;
1741 // Object attributes if there is an .ARM.attributes section or NULL.
1742 Attributes_section_data* attributes_section_data_;
1743 // Mapping symbols information.
1744 Mapping_symbols_info mapping_symbols_info_;
1745 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1746 std::vector<bool>* section_has_cortex_a8_workaround_;
1747 // Map a text section to its associated .ARM.exidx section, if there is one.
1748 Exidx_section_map exidx_section_map_;
1749 // Whether output local symbol count needs updating.
1750 bool output_local_symbol_count_needs_update_;
1751 // Whether we merge processor flags and attributes of this object to
1752 // output.
1753 bool merge_flags_and_attributes_;
1756 // Arm_dynobj class.
1758 template<bool big_endian>
1759 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1761 public:
1762 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1763 const elfcpp::Ehdr<32, big_endian>& ehdr)
1764 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1765 processor_specific_flags_(0), attributes_section_data_(NULL)
1768 ~Arm_dynobj()
1769 { delete this->attributes_section_data_; }
1771 // Downcast a base pointer to an Arm_relobj pointer. This is
1772 // not type-safe but we only use Arm_relobj not the base class.
1773 static Arm_dynobj<big_endian>*
1774 as_arm_dynobj(Dynobj* dynobj)
1775 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1777 // Processor-specific flags in ELF file header. This is valid only after
1778 // reading symbols.
1779 elfcpp::Elf_Word
1780 processor_specific_flags() const
1781 { return this->processor_specific_flags_; }
1783 // Attributes section data.
1784 const Attributes_section_data*
1785 attributes_section_data() const
1786 { return this->attributes_section_data_; }
1788 protected:
1789 // Read the symbol information.
1790 void
1791 do_read_symbols(Read_symbols_data* sd);
1793 private:
1794 // processor-specific flags in ELF file header.
1795 elfcpp::Elf_Word processor_specific_flags_;
1796 // Object attributes if there is an .ARM.attributes section or NULL.
1797 Attributes_section_data* attributes_section_data_;
1800 // Functor to read reloc addends during stub generation.
1802 template<int sh_type, bool big_endian>
1803 struct Stub_addend_reader
1805 // Return the addend for a relocation of a particular type. Depending
1806 // on whether this is a REL or RELA relocation, read the addend from a
1807 // view or from a Reloc object.
1808 elfcpp::Elf_types<32>::Elf_Swxword
1809 operator()(
1810 unsigned int /* r_type */,
1811 const unsigned char* /* view */,
1812 const typename Reloc_types<sh_type,
1813 32, big_endian>::Reloc& /* reloc */) const;
1816 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1818 template<bool big_endian>
1819 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1821 elfcpp::Elf_types<32>::Elf_Swxword
1822 operator()(
1823 unsigned int,
1824 const unsigned char*,
1825 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1828 // Specialized Stub_addend_reader for RELA type relocation sections.
1829 // We currently do not handle RELA type relocation sections but it is trivial
1830 // to implement the addend reader. This is provided for completeness and to
1831 // make it easier to add support for RELA relocation sections in the future.
1833 template<bool big_endian>
1834 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1836 elfcpp::Elf_types<32>::Elf_Swxword
1837 operator()(
1838 unsigned int,
1839 const unsigned char*,
1840 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1841 big_endian>::Reloc& reloc) const
1842 { return reloc.get_r_addend(); }
1845 // Cortex_a8_reloc class. We keep record of relocation that may need
1846 // the Cortex-A8 erratum workaround.
1848 class Cortex_a8_reloc
1850 public:
1851 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1852 Arm_address destination)
1853 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1856 ~Cortex_a8_reloc()
1859 // Accessors: This is a read-only class.
1861 // Return the relocation stub associated with this relocation if there is
1862 // one.
1863 const Reloc_stub*
1864 reloc_stub() const
1865 { return this->reloc_stub_; }
1867 // Return the relocation type.
1868 unsigned int
1869 r_type() const
1870 { return this->r_type_; }
1872 // Return the destination address of the relocation. LSB stores the THUMB
1873 // bit.
1874 Arm_address
1875 destination() const
1876 { return this->destination_; }
1878 private:
1879 // Associated relocation stub if there is one, or NULL.
1880 const Reloc_stub* reloc_stub_;
1881 // Relocation type.
1882 unsigned int r_type_;
1883 // Destination address of this relocation. LSB is used to distinguish
1884 // ARM/THUMB mode.
1885 Arm_address destination_;
1888 // Arm_output_data_got class. We derive this from Output_data_got to add
1889 // extra methods to handle TLS relocations in a static link.
1891 template<bool big_endian>
1892 class Arm_output_data_got : public Output_data_got<32, big_endian>
1894 public:
1895 Arm_output_data_got(Symbol_table* symtab, Layout* layout)
1896 : Output_data_got<32, big_endian>(), symbol_table_(symtab), layout_(layout)
1899 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1900 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1901 // applied in a static link.
1902 void
1903 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1904 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
1906 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1907 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1908 // relocation that needs to be applied in a static link.
1909 void
1910 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1911 Sized_relobj<32, big_endian>* relobj, unsigned int index)
1913 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
1914 index));
1917 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
1918 // The first one is initialized to be 1, which is the module index for
1919 // the main executable and the second one 0. A reloc of the type
1920 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
1921 // be applied by gold. GSYM is a global symbol.
1922 void
1923 add_tls_gd32_with_static_reloc(unsigned int got_type, Symbol* gsym);
1925 // Same as the above but for a local symbol in OBJECT with INDEX.
1926 void
1927 add_tls_gd32_with_static_reloc(unsigned int got_type,
1928 Sized_relobj<32, big_endian>* object,
1929 unsigned int index);
1931 protected:
1932 // Write out the GOT table.
1933 void
1934 do_write(Output_file*);
1936 private:
1937 // This class represent dynamic relocations that need to be applied by
1938 // gold because we are using TLS relocations in a static link.
1939 class Static_reloc
1941 public:
1942 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1943 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
1944 { this->u_.global.symbol = gsym; }
1946 Static_reloc(unsigned int got_offset, unsigned int r_type,
1947 Sized_relobj<32, big_endian>* relobj, unsigned int index)
1948 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
1950 this->u_.local.relobj = relobj;
1951 this->u_.local.index = index;
1954 // Return the GOT offset.
1955 unsigned int
1956 got_offset() const
1957 { return this->got_offset_; }
1959 // Relocation type.
1960 unsigned int
1961 r_type() const
1962 { return this->r_type_; }
1964 // Whether the symbol is global or not.
1965 bool
1966 symbol_is_global() const
1967 { return this->symbol_is_global_; }
1969 // For a relocation against a global symbol, the global symbol.
1970 Symbol*
1971 symbol() const
1973 gold_assert(this->symbol_is_global_);
1974 return this->u_.global.symbol;
1977 // For a relocation against a local symbol, the defining object.
1978 Sized_relobj<32, big_endian>*
1979 relobj() const
1981 gold_assert(!this->symbol_is_global_);
1982 return this->u_.local.relobj;
1985 // For a relocation against a local symbol, the local symbol index.
1986 unsigned int
1987 index() const
1989 gold_assert(!this->symbol_is_global_);
1990 return this->u_.local.index;
1993 private:
1994 // GOT offset of the entry to which this relocation is applied.
1995 unsigned int got_offset_;
1996 // Type of relocation.
1997 unsigned int r_type_;
1998 // Whether this relocation is against a global symbol.
1999 bool symbol_is_global_;
2000 // A global or local symbol.
2001 union
2003 struct
2005 // For a global symbol, the symbol itself.
2006 Symbol* symbol;
2007 } global;
2008 struct
2010 // For a local symbol, the object defining object.
2011 Sized_relobj<32, big_endian>* relobj;
2012 // For a local symbol, the symbol index.
2013 unsigned int index;
2014 } local;
2015 } u_;
2018 // Symbol table of the output object.
2019 Symbol_table* symbol_table_;
2020 // Layout of the output object.
2021 Layout* layout_;
2022 // Static relocs to be applied to the GOT.
2023 std::vector<Static_reloc> static_relocs_;
2026 // The ARM target has many relocation types with odd-sizes or noncontiguous
2027 // bits. The default handling of relocatable relocation cannot process these
2028 // relocations. So we have to extend the default code.
2030 template<bool big_endian, int sh_type, typename Classify_reloc>
2031 class Arm_scan_relocatable_relocs :
2032 public Default_scan_relocatable_relocs<sh_type, Classify_reloc>
2034 public:
2035 // Return the strategy to use for a local symbol which is a section
2036 // symbol, given the relocation type.
2037 inline Relocatable_relocs::Reloc_strategy
2038 local_section_strategy(unsigned int r_type, Relobj*)
2040 if (sh_type == elfcpp::SHT_RELA)
2041 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2042 else
2044 if (r_type == elfcpp::R_ARM_TARGET1
2045 || r_type == elfcpp::R_ARM_TARGET2)
2047 const Target_arm<big_endian>* arm_target =
2048 Target_arm<big_endian>::default_target();
2049 r_type = arm_target->get_real_reloc_type(r_type);
2052 switch(r_type)
2054 // Relocations that write nothing. These exclude R_ARM_TARGET1
2055 // and R_ARM_TARGET2.
2056 case elfcpp::R_ARM_NONE:
2057 case elfcpp::R_ARM_V4BX:
2058 case elfcpp::R_ARM_TLS_GOTDESC:
2059 case elfcpp::R_ARM_TLS_CALL:
2060 case elfcpp::R_ARM_TLS_DESCSEQ:
2061 case elfcpp::R_ARM_THM_TLS_CALL:
2062 case elfcpp::R_ARM_GOTRELAX:
2063 case elfcpp::R_ARM_GNU_VTENTRY:
2064 case elfcpp::R_ARM_GNU_VTINHERIT:
2065 case elfcpp::R_ARM_THM_TLS_DESCSEQ16:
2066 case elfcpp::R_ARM_THM_TLS_DESCSEQ32:
2067 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
2068 // These should have been converted to something else above.
2069 case elfcpp::R_ARM_TARGET1:
2070 case elfcpp::R_ARM_TARGET2:
2071 gold_unreachable();
2072 // Relocations that write full 32 bits.
2073 case elfcpp::R_ARM_ABS32:
2074 case elfcpp::R_ARM_REL32:
2075 case elfcpp::R_ARM_SBREL32:
2076 case elfcpp::R_ARM_GOTOFF32:
2077 case elfcpp::R_ARM_BASE_PREL:
2078 case elfcpp::R_ARM_GOT_BREL:
2079 case elfcpp::R_ARM_BASE_ABS:
2080 case elfcpp::R_ARM_ABS32_NOI:
2081 case elfcpp::R_ARM_REL32_NOI:
2082 case elfcpp::R_ARM_PLT32_ABS:
2083 case elfcpp::R_ARM_GOT_ABS:
2084 case elfcpp::R_ARM_GOT_PREL:
2085 case elfcpp::R_ARM_TLS_GD32:
2086 case elfcpp::R_ARM_TLS_LDM32:
2087 case elfcpp::R_ARM_TLS_LDO32:
2088 case elfcpp::R_ARM_TLS_IE32:
2089 case elfcpp::R_ARM_TLS_LE32:
2090 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
2091 default:
2092 // For all other static relocations, return RELOC_SPECIAL.
2093 return Relocatable_relocs::RELOC_SPECIAL;
2099 // Utilities for manipulating integers of up to 32-bits
2101 namespace utils
2103 // Sign extend an n-bit unsigned integer stored in an uint32_t into
2104 // an int32_t. NO_BITS must be between 1 to 32.
2105 template<int no_bits>
2106 static inline int32_t
2107 sign_extend(uint32_t bits)
2109 gold_assert(no_bits >= 0 && no_bits <= 32);
2110 if (no_bits == 32)
2111 return static_cast<int32_t>(bits);
2112 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
2113 bits &= mask;
2114 uint32_t top_bit = 1U << (no_bits - 1);
2115 int32_t as_signed = static_cast<int32_t>(bits);
2116 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
2119 // Detects overflow of an NO_BITS integer stored in a uint32_t.
2120 template<int no_bits>
2121 static inline bool
2122 has_overflow(uint32_t bits)
2124 gold_assert(no_bits >= 0 && no_bits <= 32);
2125 if (no_bits == 32)
2126 return false;
2127 int32_t max = (1 << (no_bits - 1)) - 1;
2128 int32_t min = -(1 << (no_bits - 1));
2129 int32_t as_signed = static_cast<int32_t>(bits);
2130 return as_signed > max || as_signed < min;
2133 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
2134 // fits in the given number of bits as either a signed or unsigned value.
2135 // For example, has_signed_unsigned_overflow<8> would check
2136 // -128 <= bits <= 255
2137 template<int no_bits>
2138 static inline bool
2139 has_signed_unsigned_overflow(uint32_t bits)
2141 gold_assert(no_bits >= 2 && no_bits <= 32);
2142 if (no_bits == 32)
2143 return false;
2144 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
2145 int32_t min = -(1 << (no_bits - 1));
2146 int32_t as_signed = static_cast<int32_t>(bits);
2147 return as_signed > max || as_signed < min;
2150 // Select bits from A and B using bits in MASK. For each n in [0..31],
2151 // the n-th bit in the result is chosen from the n-th bits of A and B.
2152 // A zero selects A and a one selects B.
2153 static inline uint32_t
2154 bit_select(uint32_t a, uint32_t b, uint32_t mask)
2155 { return (a & ~mask) | (b & mask); }
2158 template<bool big_endian>
2159 class Target_arm : public Sized_target<32, big_endian>
2161 public:
2162 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
2163 Reloc_section;
2165 // When were are relocating a stub, we pass this as the relocation number.
2166 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
2168 Target_arm()
2169 : Sized_target<32, big_endian>(&arm_info),
2170 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
2171 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
2172 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
2173 stub_tables_(), stub_factory_(Stub_factory::get_instance()),
2174 may_use_blx_(false), should_force_pic_veneer_(false),
2175 arm_input_section_map_(), attributes_section_data_(NULL),
2176 fix_cortex_a8_(false), cortex_a8_relocs_info_()
2179 // Virtual function which is set to return true by a target if
2180 // it can use relocation types to determine if a function's
2181 // pointer is taken.
2182 virtual bool
2183 can_check_for_function_pointers() const
2184 { return true; }
2186 // Whether a section called SECTION_NAME may have function pointers to
2187 // sections not eligible for safe ICF folding.
2188 virtual bool
2189 section_may_have_icf_unsafe_pointers(const char* section_name) const
2191 return (!is_prefix_of(".ARM.exidx", section_name)
2192 && !is_prefix_of(".ARM.extab", section_name)
2193 && Target::section_may_have_icf_unsafe_pointers(section_name));
2196 // Whether we can use BLX.
2197 bool
2198 may_use_blx() const
2199 { return this->may_use_blx_; }
2201 // Set use-BLX flag.
2202 void
2203 set_may_use_blx(bool value)
2204 { this->may_use_blx_ = value; }
2206 // Whether we force PCI branch veneers.
2207 bool
2208 should_force_pic_veneer() const
2209 { return this->should_force_pic_veneer_; }
2211 // Set PIC veneer flag.
2212 void
2213 set_should_force_pic_veneer(bool value)
2214 { this->should_force_pic_veneer_ = value; }
2216 // Whether we use THUMB-2 instructions.
2217 bool
2218 using_thumb2() const
2220 Object_attribute* attr =
2221 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2222 int arch = attr->int_value();
2223 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
2226 // Whether we use THUMB/THUMB-2 instructions only.
2227 bool
2228 using_thumb_only() const
2230 Object_attribute* attr =
2231 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2233 if (attr->int_value() == elfcpp::TAG_CPU_ARCH_V6_M
2234 || attr->int_value() == elfcpp::TAG_CPU_ARCH_V6S_M)
2235 return true;
2236 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
2237 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
2238 return false;
2239 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
2240 return attr->int_value() == 'M';
2243 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
2244 bool
2245 may_use_arm_nop() const
2247 Object_attribute* attr =
2248 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2249 int arch = attr->int_value();
2250 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2251 || arch == elfcpp::TAG_CPU_ARCH_V6K
2252 || arch == elfcpp::TAG_CPU_ARCH_V7
2253 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2256 // Whether we have THUMB-2 NOP.W instruction.
2257 bool
2258 may_use_thumb2_nop() const
2260 Object_attribute* attr =
2261 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2262 int arch = attr->int_value();
2263 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2264 || arch == elfcpp::TAG_CPU_ARCH_V7
2265 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2268 // Process the relocations to determine unreferenced sections for
2269 // garbage collection.
2270 void
2271 gc_process_relocs(Symbol_table* symtab,
2272 Layout* layout,
2273 Sized_relobj<32, big_endian>* object,
2274 unsigned int data_shndx,
2275 unsigned int sh_type,
2276 const unsigned char* prelocs,
2277 size_t reloc_count,
2278 Output_section* output_section,
2279 bool needs_special_offset_handling,
2280 size_t local_symbol_count,
2281 const unsigned char* plocal_symbols);
2283 // Scan the relocations to look for symbol adjustments.
2284 void
2285 scan_relocs(Symbol_table* symtab,
2286 Layout* layout,
2287 Sized_relobj<32, big_endian>* object,
2288 unsigned int data_shndx,
2289 unsigned int sh_type,
2290 const unsigned char* prelocs,
2291 size_t reloc_count,
2292 Output_section* output_section,
2293 bool needs_special_offset_handling,
2294 size_t local_symbol_count,
2295 const unsigned char* plocal_symbols);
2297 // Finalize the sections.
2298 void
2299 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2301 // Return the value to use for a dynamic symbol which requires special
2302 // treatment.
2303 uint64_t
2304 do_dynsym_value(const Symbol*) const;
2306 // Relocate a section.
2307 void
2308 relocate_section(const Relocate_info<32, big_endian>*,
2309 unsigned int sh_type,
2310 const unsigned char* prelocs,
2311 size_t reloc_count,
2312 Output_section* output_section,
2313 bool needs_special_offset_handling,
2314 unsigned char* view,
2315 Arm_address view_address,
2316 section_size_type view_size,
2317 const Reloc_symbol_changes*);
2319 // Scan the relocs during a relocatable link.
2320 void
2321 scan_relocatable_relocs(Symbol_table* symtab,
2322 Layout* layout,
2323 Sized_relobj<32, big_endian>* object,
2324 unsigned int data_shndx,
2325 unsigned int sh_type,
2326 const unsigned char* prelocs,
2327 size_t reloc_count,
2328 Output_section* output_section,
2329 bool needs_special_offset_handling,
2330 size_t local_symbol_count,
2331 const unsigned char* plocal_symbols,
2332 Relocatable_relocs*);
2334 // Relocate a section during a relocatable link.
2335 void
2336 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
2337 unsigned int sh_type,
2338 const unsigned char* prelocs,
2339 size_t reloc_count,
2340 Output_section* output_section,
2341 off_t offset_in_output_section,
2342 const Relocatable_relocs*,
2343 unsigned char* view,
2344 Arm_address view_address,
2345 section_size_type view_size,
2346 unsigned char* reloc_view,
2347 section_size_type reloc_view_size);
2349 // Perform target-specific processing in a relocatable link. This is
2350 // only used if we use the relocation strategy RELOC_SPECIAL.
2351 void
2352 relocate_special_relocatable(const Relocate_info<32, big_endian>* relinfo,
2353 unsigned int sh_type,
2354 const unsigned char* preloc_in,
2355 size_t relnum,
2356 Output_section* output_section,
2357 off_t offset_in_output_section,
2358 unsigned char* view,
2359 typename elfcpp::Elf_types<32>::Elf_Addr
2360 view_address,
2361 section_size_type view_size,
2362 unsigned char* preloc_out);
2364 // Return whether SYM is defined by the ABI.
2365 bool
2366 do_is_defined_by_abi(Symbol* sym) const
2367 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
2369 // Return whether there is a GOT section.
2370 bool
2371 has_got_section() const
2372 { return this->got_ != NULL; }
2374 // Return the size of the GOT section.
2375 section_size_type
2376 got_size() const
2378 gold_assert(this->got_ != NULL);
2379 return this->got_->data_size();
2382 // Return the number of entries in the GOT.
2383 unsigned int
2384 got_entry_count() const
2386 if (!this->has_got_section())
2387 return 0;
2388 return this->got_size() / 4;
2391 // Return the number of entries in the PLT.
2392 unsigned int
2393 plt_entry_count() const;
2395 // Return the offset of the first non-reserved PLT entry.
2396 unsigned int
2397 first_plt_entry_offset() const;
2399 // Return the size of each PLT entry.
2400 unsigned int
2401 plt_entry_size() const;
2403 // Map platform-specific reloc types
2404 static unsigned int
2405 get_real_reloc_type(unsigned int r_type);
2408 // Methods to support stub-generations.
2411 // Return the stub factory
2412 const Stub_factory&
2413 stub_factory() const
2414 { return this->stub_factory_; }
2416 // Make a new Arm_input_section object.
2417 Arm_input_section<big_endian>*
2418 new_arm_input_section(Relobj*, unsigned int);
2420 // Find the Arm_input_section object corresponding to the SHNDX-th input
2421 // section of RELOBJ.
2422 Arm_input_section<big_endian>*
2423 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
2425 // Make a new Stub_table
2426 Stub_table<big_endian>*
2427 new_stub_table(Arm_input_section<big_endian>*);
2429 // Scan a section for stub generation.
2430 void
2431 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2432 const unsigned char*, size_t, Output_section*,
2433 bool, const unsigned char*, Arm_address,
2434 section_size_type);
2436 // Relocate a stub.
2437 void
2438 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
2439 Output_section*, unsigned char*, Arm_address,
2440 section_size_type);
2442 // Get the default ARM target.
2443 static Target_arm<big_endian>*
2444 default_target()
2446 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2447 && parameters->target().is_big_endian() == big_endian);
2448 return static_cast<Target_arm<big_endian>*>(
2449 parameters->sized_target<32, big_endian>());
2452 // Whether NAME belongs to a mapping symbol.
2453 static bool
2454 is_mapping_symbol_name(const char* name)
2456 return (name
2457 && name[0] == '$'
2458 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2459 && (name[2] == '\0' || name[2] == '.'));
2462 // Whether we work around the Cortex-A8 erratum.
2463 bool
2464 fix_cortex_a8() const
2465 { return this->fix_cortex_a8_; }
2467 // Whether we merge exidx entries in debuginfo.
2468 bool
2469 merge_exidx_entries() const
2470 { return parameters->options().merge_exidx_entries(); }
2472 // Whether we fix R_ARM_V4BX relocation.
2473 // 0 - do not fix
2474 // 1 - replace with MOV instruction (armv4 target)
2475 // 2 - make interworking veneer (>= armv4t targets only)
2476 General_options::Fix_v4bx
2477 fix_v4bx() const
2478 { return parameters->options().fix_v4bx(); }
2480 // Scan a span of THUMB code section for Cortex-A8 erratum.
2481 void
2482 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2483 section_size_type, section_size_type,
2484 const unsigned char*, Arm_address);
2486 // Apply Cortex-A8 workaround to a branch.
2487 void
2488 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2489 unsigned char*, Arm_address);
2491 protected:
2492 // Make an ELF object.
2493 Object*
2494 do_make_elf_object(const std::string&, Input_file*, off_t,
2495 const elfcpp::Ehdr<32, big_endian>& ehdr);
2497 Object*
2498 do_make_elf_object(const std::string&, Input_file*, off_t,
2499 const elfcpp::Ehdr<32, !big_endian>&)
2500 { gold_unreachable(); }
2502 Object*
2503 do_make_elf_object(const std::string&, Input_file*, off_t,
2504 const elfcpp::Ehdr<64, false>&)
2505 { gold_unreachable(); }
2507 Object*
2508 do_make_elf_object(const std::string&, Input_file*, off_t,
2509 const elfcpp::Ehdr<64, true>&)
2510 { gold_unreachable(); }
2512 // Make an output section.
2513 Output_section*
2514 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2515 elfcpp::Elf_Xword flags)
2516 { return new Arm_output_section<big_endian>(name, type, flags); }
2518 void
2519 do_adjust_elf_header(unsigned char* view, int len) const;
2521 // We only need to generate stubs, and hence perform relaxation if we are
2522 // not doing relocatable linking.
2523 bool
2524 do_may_relax() const
2525 { return !parameters->options().relocatable(); }
2527 bool
2528 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
2530 // Determine whether an object attribute tag takes an integer, a
2531 // string or both.
2533 do_attribute_arg_type(int tag) const;
2535 // Reorder tags during output.
2537 do_attributes_order(int num) const;
2539 // This is called when the target is selected as the default.
2540 void
2541 do_select_as_default_target()
2543 // No locking is required since there should only be one default target.
2544 // We cannot have both the big-endian and little-endian ARM targets
2545 // as the default.
2546 gold_assert(arm_reloc_property_table == NULL);
2547 arm_reloc_property_table = new Arm_reloc_property_table();
2550 private:
2551 // The class which scans relocations.
2552 class Scan
2554 public:
2555 Scan()
2556 : issued_non_pic_error_(false)
2559 static inline int
2560 get_reference_flags(unsigned int r_type);
2562 inline void
2563 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
2564 Sized_relobj<32, big_endian>* object,
2565 unsigned int data_shndx,
2566 Output_section* output_section,
2567 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2568 const elfcpp::Sym<32, big_endian>& lsym);
2570 inline void
2571 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
2572 Sized_relobj<32, big_endian>* object,
2573 unsigned int data_shndx,
2574 Output_section* output_section,
2575 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2576 Symbol* gsym);
2578 inline bool
2579 local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2580 Sized_relobj<32, big_endian>* ,
2581 unsigned int ,
2582 Output_section* ,
2583 const elfcpp::Rel<32, big_endian>& ,
2584 unsigned int ,
2585 const elfcpp::Sym<32, big_endian>&);
2587 inline bool
2588 global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2589 Sized_relobj<32, big_endian>* ,
2590 unsigned int ,
2591 Output_section* ,
2592 const elfcpp::Rel<32, big_endian>& ,
2593 unsigned int , Symbol*);
2595 private:
2596 static void
2597 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
2598 unsigned int r_type);
2600 static void
2601 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
2602 unsigned int r_type, Symbol*);
2604 void
2605 check_non_pic(Relobj*, unsigned int r_type);
2607 // Almost identical to Symbol::needs_plt_entry except that it also
2608 // handles STT_ARM_TFUNC.
2609 static bool
2610 symbol_needs_plt_entry(const Symbol* sym)
2612 // An undefined symbol from an executable does not need a PLT entry.
2613 if (sym->is_undefined() && !parameters->options().shared())
2614 return false;
2616 return (!parameters->doing_static_link()
2617 && (sym->type() == elfcpp::STT_FUNC
2618 || sym->type() == elfcpp::STT_ARM_TFUNC)
2619 && (sym->is_from_dynobj()
2620 || sym->is_undefined()
2621 || sym->is_preemptible()));
2624 inline bool
2625 possible_function_pointer_reloc(unsigned int r_type);
2627 // Whether we have issued an error about a non-PIC compilation.
2628 bool issued_non_pic_error_;
2631 // The class which implements relocation.
2632 class Relocate
2634 public:
2635 Relocate()
2638 ~Relocate()
2641 // Return whether the static relocation needs to be applied.
2642 inline bool
2643 should_apply_static_reloc(const Sized_symbol<32>* gsym,
2644 unsigned int r_type,
2645 bool is_32bit,
2646 Output_section* output_section);
2648 // Do a relocation. Return false if the caller should not issue
2649 // any warnings about this relocation.
2650 inline bool
2651 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2652 Output_section*, size_t relnum,
2653 const elfcpp::Rel<32, big_endian>&,
2654 unsigned int r_type, const Sized_symbol<32>*,
2655 const Symbol_value<32>*,
2656 unsigned char*, Arm_address,
2657 section_size_type);
2659 // Return whether we want to pass flag NON_PIC_REF for this
2660 // reloc. This means the relocation type accesses a symbol not via
2661 // GOT or PLT.
2662 static inline bool
2663 reloc_is_non_pic(unsigned int r_type)
2665 switch (r_type)
2667 // These relocation types reference GOT or PLT entries explicitly.
2668 case elfcpp::R_ARM_GOT_BREL:
2669 case elfcpp::R_ARM_GOT_ABS:
2670 case elfcpp::R_ARM_GOT_PREL:
2671 case elfcpp::R_ARM_GOT_BREL12:
2672 case elfcpp::R_ARM_PLT32_ABS:
2673 case elfcpp::R_ARM_TLS_GD32:
2674 case elfcpp::R_ARM_TLS_LDM32:
2675 case elfcpp::R_ARM_TLS_IE32:
2676 case elfcpp::R_ARM_TLS_IE12GP:
2678 // These relocate types may use PLT entries.
2679 case elfcpp::R_ARM_CALL:
2680 case elfcpp::R_ARM_THM_CALL:
2681 case elfcpp::R_ARM_JUMP24:
2682 case elfcpp::R_ARM_THM_JUMP24:
2683 case elfcpp::R_ARM_THM_JUMP19:
2684 case elfcpp::R_ARM_PLT32:
2685 case elfcpp::R_ARM_THM_XPC22:
2686 case elfcpp::R_ARM_PREL31:
2687 case elfcpp::R_ARM_SBREL31:
2688 return false;
2690 default:
2691 return true;
2695 private:
2696 // Do a TLS relocation.
2697 inline typename Arm_relocate_functions<big_endian>::Status
2698 relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
2699 size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
2700 const Sized_symbol<32>*, const Symbol_value<32>*,
2701 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
2702 section_size_type);
2706 // A class which returns the size required for a relocation type,
2707 // used while scanning relocs during a relocatable link.
2708 class Relocatable_size_for_reloc
2710 public:
2711 unsigned int
2712 get_size_for_reloc(unsigned int, Relobj*);
2715 // Adjust TLS relocation type based on the options and whether this
2716 // is a local symbol.
2717 static tls::Tls_optimization
2718 optimize_tls_reloc(bool is_final, int r_type);
2720 // Get the GOT section, creating it if necessary.
2721 Arm_output_data_got<big_endian>*
2722 got_section(Symbol_table*, Layout*);
2724 // Get the GOT PLT section.
2725 Output_data_space*
2726 got_plt_section() const
2728 gold_assert(this->got_plt_ != NULL);
2729 return this->got_plt_;
2732 // Create a PLT entry for a global symbol.
2733 void
2734 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2736 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2737 void
2738 define_tls_base_symbol(Symbol_table*, Layout*);
2740 // Create a GOT entry for the TLS module index.
2741 unsigned int
2742 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2743 Sized_relobj<32, big_endian>* object);
2745 // Get the PLT section.
2746 const Output_data_plt_arm<big_endian>*
2747 plt_section() const
2749 gold_assert(this->plt_ != NULL);
2750 return this->plt_;
2753 // Get the dynamic reloc section, creating it if necessary.
2754 Reloc_section*
2755 rel_dyn_section(Layout*);
2757 // Get the section to use for TLS_DESC relocations.
2758 Reloc_section*
2759 rel_tls_desc_section(Layout*) const;
2761 // Return true if the symbol may need a COPY relocation.
2762 // References from an executable object to non-function symbols
2763 // defined in a dynamic object may need a COPY relocation.
2764 bool
2765 may_need_copy_reloc(Symbol* gsym)
2767 return (gsym->type() != elfcpp::STT_ARM_TFUNC
2768 && gsym->may_need_copy_reloc());
2771 // Add a potential copy relocation.
2772 void
2773 copy_reloc(Symbol_table* symtab, Layout* layout,
2774 Sized_relobj<32, big_endian>* object,
2775 unsigned int shndx, Output_section* output_section,
2776 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2778 this->copy_relocs_.copy_reloc(symtab, layout,
2779 symtab->get_sized_symbol<32>(sym),
2780 object, shndx, output_section, reloc,
2781 this->rel_dyn_section(layout));
2784 // Whether two EABI versions are compatible.
2785 static bool
2786 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2788 // Merge processor-specific flags from input object and those in the ELF
2789 // header of the output.
2790 void
2791 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2793 // Get the secondary compatible architecture.
2794 static int
2795 get_secondary_compatible_arch(const Attributes_section_data*);
2797 // Set the secondary compatible architecture.
2798 static void
2799 set_secondary_compatible_arch(Attributes_section_data*, int);
2801 static int
2802 tag_cpu_arch_combine(const char*, int, int*, int, int);
2804 // Helper to print AEABI enum tag value.
2805 static std::string
2806 aeabi_enum_name(unsigned int);
2808 // Return string value for TAG_CPU_name.
2809 static std::string
2810 tag_cpu_name_value(unsigned int);
2812 // Merge object attributes from input object and those in the output.
2813 void
2814 merge_object_attributes(const char*, const Attributes_section_data*);
2816 // Helper to get an AEABI object attribute
2817 Object_attribute*
2818 get_aeabi_object_attribute(int tag) const
2820 Attributes_section_data* pasd = this->attributes_section_data_;
2821 gold_assert(pasd != NULL);
2822 Object_attribute* attr =
2823 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2824 gold_assert(attr != NULL);
2825 return attr;
2829 // Methods to support stub-generations.
2832 // Group input sections for stub generation.
2833 void
2834 group_sections(Layout*, section_size_type, bool, const Task*);
2836 // Scan a relocation for stub generation.
2837 void
2838 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2839 const Sized_symbol<32>*, unsigned int,
2840 const Symbol_value<32>*,
2841 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
2843 // Scan a relocation section for stub.
2844 template<int sh_type>
2845 void
2846 scan_reloc_section_for_stubs(
2847 const Relocate_info<32, big_endian>* relinfo,
2848 const unsigned char* prelocs,
2849 size_t reloc_count,
2850 Output_section* output_section,
2851 bool needs_special_offset_handling,
2852 const unsigned char* view,
2853 elfcpp::Elf_types<32>::Elf_Addr view_address,
2854 section_size_type);
2856 // Fix .ARM.exidx section coverage.
2857 void
2858 fix_exidx_coverage(Layout*, const Input_objects*,
2859 Arm_output_section<big_endian>*, Symbol_table*,
2860 const Task*);
2862 // Functors for STL set.
2863 struct output_section_address_less_than
2865 bool
2866 operator()(const Output_section* s1, const Output_section* s2) const
2867 { return s1->address() < s2->address(); }
2870 // Information about this specific target which we pass to the
2871 // general Target structure.
2872 static const Target::Target_info arm_info;
2874 // The types of GOT entries needed for this platform.
2875 // These values are exposed to the ABI in an incremental link.
2876 // Do not renumber existing values without changing the version
2877 // number of the .gnu_incremental_inputs section.
2878 enum Got_type
2880 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2881 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
2882 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
2883 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
2884 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
2887 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2889 // Map input section to Arm_input_section.
2890 typedef Unordered_map<Section_id,
2891 Arm_input_section<big_endian>*,
2892 Section_id_hash>
2893 Arm_input_section_map;
2895 // Map output addresses to relocs for Cortex-A8 erratum.
2896 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2897 Cortex_a8_relocs_info;
2899 // The GOT section.
2900 Arm_output_data_got<big_endian>* got_;
2901 // The PLT section.
2902 Output_data_plt_arm<big_endian>* plt_;
2903 // The GOT PLT section.
2904 Output_data_space* got_plt_;
2905 // The dynamic reloc section.
2906 Reloc_section* rel_dyn_;
2907 // Relocs saved to avoid a COPY reloc.
2908 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2909 // Space for variables copied with a COPY reloc.
2910 Output_data_space* dynbss_;
2911 // Offset of the GOT entry for the TLS module index.
2912 unsigned int got_mod_index_offset_;
2913 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2914 bool tls_base_symbol_defined_;
2915 // Vector of Stub_tables created.
2916 Stub_table_list stub_tables_;
2917 // Stub factory.
2918 const Stub_factory &stub_factory_;
2919 // Whether we can use BLX.
2920 bool may_use_blx_;
2921 // Whether we force PIC branch veneers.
2922 bool should_force_pic_veneer_;
2923 // Map for locating Arm_input_sections.
2924 Arm_input_section_map arm_input_section_map_;
2925 // Attributes section data in output.
2926 Attributes_section_data* attributes_section_data_;
2927 // Whether we want to fix code for Cortex-A8 erratum.
2928 bool fix_cortex_a8_;
2929 // Map addresses to relocs for Cortex-A8 erratum.
2930 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2933 template<bool big_endian>
2934 const Target::Target_info Target_arm<big_endian>::arm_info =
2936 32, // size
2937 big_endian, // is_big_endian
2938 elfcpp::EM_ARM, // machine_code
2939 false, // has_make_symbol
2940 false, // has_resolve
2941 false, // has_code_fill
2942 true, // is_default_stack_executable
2943 '\0', // wrap_char
2944 "/usr/lib/libc.so.1", // dynamic_linker
2945 0x8000, // default_text_segment_address
2946 0x1000, // abi_pagesize (overridable by -z max-page-size)
2947 0x1000, // common_pagesize (overridable by -z common-page-size)
2948 elfcpp::SHN_UNDEF, // small_common_shndx
2949 elfcpp::SHN_UNDEF, // large_common_shndx
2950 0, // small_common_section_flags
2951 0, // large_common_section_flags
2952 ".ARM.attributes", // attributes_section
2953 "aeabi" // attributes_vendor
2956 // Arm relocate functions class
2959 template<bool big_endian>
2960 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2962 public:
2963 typedef enum
2965 STATUS_OKAY, // No error during relocation.
2966 STATUS_OVERFLOW, // Relocation overflow.
2967 STATUS_BAD_RELOC // Relocation cannot be applied.
2968 } Status;
2970 private:
2971 typedef Relocate_functions<32, big_endian> Base;
2972 typedef Arm_relocate_functions<big_endian> This;
2974 // Encoding of imm16 argument for movt and movw ARM instructions
2975 // from ARM ARM:
2977 // imm16 := imm4 | imm12
2979 // 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
2980 // +-------+---------------+-------+-------+-----------------------+
2981 // | | |imm4 | |imm12 |
2982 // +-------+---------------+-------+-------+-----------------------+
2984 // Extract the relocation addend from VAL based on the ARM
2985 // instruction encoding described above.
2986 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2987 extract_arm_movw_movt_addend(
2988 typename elfcpp::Swap<32, big_endian>::Valtype val)
2990 // According to the Elf ABI for ARM Architecture the immediate
2991 // field is sign-extended to form the addend.
2992 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2995 // Insert X into VAL based on the ARM instruction encoding described
2996 // above.
2997 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2998 insert_val_arm_movw_movt(
2999 typename elfcpp::Swap<32, big_endian>::Valtype val,
3000 typename elfcpp::Swap<32, big_endian>::Valtype x)
3002 val &= 0xfff0f000;
3003 val |= x & 0x0fff;
3004 val |= (x & 0xf000) << 4;
3005 return val;
3008 // Encoding of imm16 argument for movt and movw Thumb2 instructions
3009 // from ARM ARM:
3011 // imm16 := imm4 | i | imm3 | imm8
3013 // 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
3014 // +---------+-+-----------+-------++-+-----+-------+---------------+
3015 // | |i| |imm4 || |imm3 | |imm8 |
3016 // +---------+-+-----------+-------++-+-----+-------+---------------+
3018 // Extract the relocation addend from VAL based on the Thumb2
3019 // instruction encoding described above.
3020 static inline typename elfcpp::Swap<32, big_endian>::Valtype
3021 extract_thumb_movw_movt_addend(
3022 typename elfcpp::Swap<32, big_endian>::Valtype val)
3024 // According to the Elf ABI for ARM Architecture the immediate
3025 // field is sign-extended to form the addend.
3026 return utils::sign_extend<16>(((val >> 4) & 0xf000)
3027 | ((val >> 15) & 0x0800)
3028 | ((val >> 4) & 0x0700)
3029 | (val & 0x00ff));
3032 // Insert X into VAL based on the Thumb2 instruction encoding
3033 // described above.
3034 static inline typename elfcpp::Swap<32, big_endian>::Valtype
3035 insert_val_thumb_movw_movt(
3036 typename elfcpp::Swap<32, big_endian>::Valtype val,
3037 typename elfcpp::Swap<32, big_endian>::Valtype x)
3039 val &= 0xfbf08f00;
3040 val |= (x & 0xf000) << 4;
3041 val |= (x & 0x0800) << 15;
3042 val |= (x & 0x0700) << 4;
3043 val |= (x & 0x00ff);
3044 return val;
3047 // Calculate the smallest constant Kn for the specified residual.
3048 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3049 static uint32_t
3050 calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
3052 int32_t msb;
3054 if (residual == 0)
3055 return 0;
3056 // Determine the most significant bit in the residual and
3057 // align the resulting value to a 2-bit boundary.
3058 for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
3060 // The desired shift is now (msb - 6), or zero, whichever
3061 // is the greater.
3062 return (((msb - 6) < 0) ? 0 : (msb - 6));
3065 // Calculate the final residual for the specified group index.
3066 // If the passed group index is less than zero, the method will return
3067 // the value of the specified residual without any change.
3068 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3069 static typename elfcpp::Swap<32, big_endian>::Valtype
3070 calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
3071 const int group)
3073 for (int n = 0; n <= group; n++)
3075 // Calculate which part of the value to mask.
3076 uint32_t shift = calc_grp_kn(residual);
3077 // Calculate the residual for the next time around.
3078 residual &= ~(residual & (0xff << shift));
3081 return residual;
3084 // Calculate the value of Gn for the specified group index.
3085 // We return it in the form of an encoded constant-and-rotation.
3086 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3087 static typename elfcpp::Swap<32, big_endian>::Valtype
3088 calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
3089 const int group)
3091 typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
3092 uint32_t shift = 0;
3094 for (int n = 0; n <= group; n++)
3096 // Calculate which part of the value to mask.
3097 shift = calc_grp_kn(residual);
3098 // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
3099 gn = residual & (0xff << shift);
3100 // Calculate the residual for the next time around.
3101 residual &= ~gn;
3103 // Return Gn in the form of an encoded constant-and-rotation.
3104 return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
3107 public:
3108 // Handle ARM long branches.
3109 static typename This::Status
3110 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
3111 unsigned char*, const Sized_symbol<32>*,
3112 const Arm_relobj<big_endian>*, unsigned int,
3113 const Symbol_value<32>*, Arm_address, Arm_address, bool);
3115 // Handle THUMB long branches.
3116 static typename This::Status
3117 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
3118 unsigned char*, const Sized_symbol<32>*,
3119 const Arm_relobj<big_endian>*, unsigned int,
3120 const Symbol_value<32>*, Arm_address, Arm_address, bool);
3123 // Return the branch offset of a 32-bit THUMB branch.
3124 static inline int32_t
3125 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
3127 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
3128 // involving the J1 and J2 bits.
3129 uint32_t s = (upper_insn & (1U << 10)) >> 10;
3130 uint32_t upper = upper_insn & 0x3ffU;
3131 uint32_t lower = lower_insn & 0x7ffU;
3132 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
3133 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
3134 uint32_t i1 = j1 ^ s ? 0 : 1;
3135 uint32_t i2 = j2 ^ s ? 0 : 1;
3137 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
3138 | (upper << 12) | (lower << 1));
3141 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
3142 // UPPER_INSN is the original upper instruction of the branch. Caller is
3143 // responsible for overflow checking and BLX offset adjustment.
3144 static inline uint16_t
3145 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
3147 uint32_t s = offset < 0 ? 1 : 0;
3148 uint32_t bits = static_cast<uint32_t>(offset);
3149 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
3152 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
3153 // LOWER_INSN is the original lower instruction of the branch. Caller is
3154 // responsible for overflow checking and BLX offset adjustment.
3155 static inline uint16_t
3156 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
3158 uint32_t s = offset < 0 ? 1 : 0;
3159 uint32_t bits = static_cast<uint32_t>(offset);
3160 return ((lower_insn & ~0x2fffU)
3161 | ((((bits >> 23) & 1) ^ !s) << 13)
3162 | ((((bits >> 22) & 1) ^ !s) << 11)
3163 | ((bits >> 1) & 0x7ffU));
3166 // Return the branch offset of a 32-bit THUMB conditional branch.
3167 static inline int32_t
3168 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
3170 uint32_t s = (upper_insn & 0x0400U) >> 10;
3171 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
3172 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
3173 uint32_t lower = (lower_insn & 0x07ffU);
3174 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
3176 return utils::sign_extend<21>((upper << 12) | (lower << 1));
3179 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
3180 // instruction. UPPER_INSN is the original upper instruction of the branch.
3181 // Caller is responsible for overflow checking.
3182 static inline uint16_t
3183 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
3185 uint32_t s = offset < 0 ? 1 : 0;
3186 uint32_t bits = static_cast<uint32_t>(offset);
3187 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
3190 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
3191 // instruction. LOWER_INSN is the original lower instruction of the branch.
3192 // The caller is responsible for overflow checking.
3193 static inline uint16_t
3194 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
3196 uint32_t bits = static_cast<uint32_t>(offset);
3197 uint32_t j2 = (bits & 0x00080000U) >> 19;
3198 uint32_t j1 = (bits & 0x00040000U) >> 18;
3199 uint32_t lo = (bits & 0x00000ffeU) >> 1;
3201 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
3204 // R_ARM_ABS8: S + A
3205 static inline typename This::Status
3206 abs8(unsigned char* view,
3207 const Sized_relobj<32, big_endian>* object,
3208 const Symbol_value<32>* psymval)
3210 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
3211 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3212 Valtype* wv = reinterpret_cast<Valtype*>(view);
3213 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
3214 Reltype addend = utils::sign_extend<8>(val);
3215 Reltype x = psymval->value(object, addend);
3216 val = utils::bit_select(val, x, 0xffU);
3217 elfcpp::Swap<8, big_endian>::writeval(wv, val);
3219 // R_ARM_ABS8 permits signed or unsigned results.
3220 int signed_x = static_cast<int32_t>(x);
3221 return ((signed_x < -128 || signed_x > 255)
3222 ? This::STATUS_OVERFLOW
3223 : This::STATUS_OKAY);
3226 // R_ARM_THM_ABS5: S + A
3227 static inline typename This::Status
3228 thm_abs5(unsigned char* view,
3229 const Sized_relobj<32, big_endian>* object,
3230 const Symbol_value<32>* psymval)
3232 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3233 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3234 Valtype* wv = reinterpret_cast<Valtype*>(view);
3235 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3236 Reltype addend = (val & 0x7e0U) >> 6;
3237 Reltype x = psymval->value(object, addend);
3238 val = utils::bit_select(val, x << 6, 0x7e0U);
3239 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3241 // R_ARM_ABS16 permits signed or unsigned results.
3242 int signed_x = static_cast<int32_t>(x);
3243 return ((signed_x < -32768 || signed_x > 65535)
3244 ? This::STATUS_OVERFLOW
3245 : This::STATUS_OKAY);
3248 // R_ARM_ABS12: S + A
3249 static inline typename This::Status
3250 abs12(unsigned char* view,
3251 const Sized_relobj<32, big_endian>* object,
3252 const Symbol_value<32>* psymval)
3254 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3255 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3256 Valtype* wv = reinterpret_cast<Valtype*>(view);
3257 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3258 Reltype addend = val & 0x0fffU;
3259 Reltype x = psymval->value(object, addend);
3260 val = utils::bit_select(val, x, 0x0fffU);
3261 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3262 return (utils::has_overflow<12>(x)
3263 ? This::STATUS_OVERFLOW
3264 : This::STATUS_OKAY);
3267 // R_ARM_ABS16: S + A
3268 static inline typename This::Status
3269 abs16(unsigned char* view,
3270 const Sized_relobj<32, big_endian>* object,
3271 const Symbol_value<32>* psymval)
3273 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3274 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3275 Valtype* wv = reinterpret_cast<Valtype*>(view);
3276 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3277 Reltype addend = utils::sign_extend<16>(val);
3278 Reltype x = psymval->value(object, addend);
3279 val = utils::bit_select(val, x, 0xffffU);
3280 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3281 return (utils::has_signed_unsigned_overflow<16>(x)
3282 ? This::STATUS_OVERFLOW
3283 : This::STATUS_OKAY);
3286 // R_ARM_ABS32: (S + A) | T
3287 static inline typename This::Status
3288 abs32(unsigned char* view,
3289 const Sized_relobj<32, big_endian>* object,
3290 const Symbol_value<32>* psymval,
3291 Arm_address thumb_bit)
3293 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3294 Valtype* wv = reinterpret_cast<Valtype*>(view);
3295 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3296 Valtype x = psymval->value(object, addend) | thumb_bit;
3297 elfcpp::Swap<32, big_endian>::writeval(wv, x);
3298 return This::STATUS_OKAY;
3301 // R_ARM_REL32: (S + A) | T - P
3302 static inline typename This::Status
3303 rel32(unsigned char* view,
3304 const Sized_relobj<32, big_endian>* object,
3305 const Symbol_value<32>* psymval,
3306 Arm_address address,
3307 Arm_address thumb_bit)
3309 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3310 Valtype* wv = reinterpret_cast<Valtype*>(view);
3311 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3312 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3313 elfcpp::Swap<32, big_endian>::writeval(wv, x);
3314 return This::STATUS_OKAY;
3317 // R_ARM_THM_JUMP24: (S + A) | T - P
3318 static typename This::Status
3319 thm_jump19(unsigned char* view, const Arm_relobj<big_endian>* object,
3320 const Symbol_value<32>* psymval, Arm_address address,
3321 Arm_address thumb_bit);
3323 // R_ARM_THM_JUMP6: S + A – P
3324 static inline typename This::Status
3325 thm_jump6(unsigned char* view,
3326 const Sized_relobj<32, big_endian>* object,
3327 const Symbol_value<32>* psymval,
3328 Arm_address address)
3330 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3331 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3332 Valtype* wv = reinterpret_cast<Valtype*>(view);
3333 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3334 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
3335 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
3336 Reltype x = (psymval->value(object, addend) - address);
3337 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
3338 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3339 // CZB does only forward jumps.
3340 return ((x > 0x007e)
3341 ? This::STATUS_OVERFLOW
3342 : This::STATUS_OKAY);
3345 // R_ARM_THM_JUMP8: S + A – P
3346 static inline typename This::Status
3347 thm_jump8(unsigned char* view,
3348 const Sized_relobj<32, big_endian>* object,
3349 const Symbol_value<32>* psymval,
3350 Arm_address address)
3352 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3353 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3354 Valtype* wv = reinterpret_cast<Valtype*>(view);
3355 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3356 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
3357 Reltype x = (psymval->value(object, addend) - address);
3358 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
3359 return (utils::has_overflow<8>(x)
3360 ? This::STATUS_OVERFLOW
3361 : This::STATUS_OKAY);
3364 // R_ARM_THM_JUMP11: S + A – P
3365 static inline typename This::Status
3366 thm_jump11(unsigned char* view,
3367 const Sized_relobj<32, big_endian>* object,
3368 const Symbol_value<32>* psymval,
3369 Arm_address address)
3371 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3372 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3373 Valtype* wv = reinterpret_cast<Valtype*>(view);
3374 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3375 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
3376 Reltype x = (psymval->value(object, addend) - address);
3377 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
3378 return (utils::has_overflow<11>(x)
3379 ? This::STATUS_OVERFLOW
3380 : This::STATUS_OKAY);
3383 // R_ARM_BASE_PREL: B(S) + A - P
3384 static inline typename This::Status
3385 base_prel(unsigned char* view,
3386 Arm_address origin,
3387 Arm_address address)
3389 Base::rel32(view, origin - address);
3390 return STATUS_OKAY;
3393 // R_ARM_BASE_ABS: B(S) + A
3394 static inline typename This::Status
3395 base_abs(unsigned char* view,
3396 Arm_address origin)
3398 Base::rel32(view, origin);
3399 return STATUS_OKAY;
3402 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3403 static inline typename This::Status
3404 got_brel(unsigned char* view,
3405 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
3407 Base::rel32(view, got_offset);
3408 return This::STATUS_OKAY;
3411 // R_ARM_GOT_PREL: GOT(S) + A - P
3412 static inline typename This::Status
3413 got_prel(unsigned char* view,
3414 Arm_address got_entry,
3415 Arm_address address)
3417 Base::rel32(view, got_entry - address);
3418 return This::STATUS_OKAY;
3421 // R_ARM_PREL: (S + A) | T - P
3422 static inline typename This::Status
3423 prel31(unsigned char* view,
3424 const Sized_relobj<32, big_endian>* object,
3425 const Symbol_value<32>* psymval,
3426 Arm_address address,
3427 Arm_address thumb_bit)
3429 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3430 Valtype* wv = reinterpret_cast<Valtype*>(view);
3431 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3432 Valtype addend = utils::sign_extend<31>(val);
3433 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3434 val = utils::bit_select(val, x, 0x7fffffffU);
3435 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3436 return (utils::has_overflow<31>(x) ?
3437 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3440 // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
3441 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3442 // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3443 // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
3444 static inline typename This::Status
3445 movw(unsigned char* view,
3446 const Sized_relobj<32, big_endian>* object,
3447 const Symbol_value<32>* psymval,
3448 Arm_address relative_address_base,
3449 Arm_address thumb_bit,
3450 bool check_overflow)
3452 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3453 Valtype* wv = reinterpret_cast<Valtype*>(view);
3454 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3455 Valtype addend = This::extract_arm_movw_movt_addend(val);
3456 Valtype x = ((psymval->value(object, addend) | thumb_bit)
3457 - relative_address_base);
3458 val = This::insert_val_arm_movw_movt(val, x);
3459 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3460 return ((check_overflow && utils::has_overflow<16>(x))
3461 ? This::STATUS_OVERFLOW
3462 : This::STATUS_OKAY);
3465 // R_ARM_MOVT_ABS: S + A (relative address base is 0)
3466 // R_ARM_MOVT_PREL: S + A - P
3467 // R_ARM_MOVT_BREL: S + A - B(S)
3468 static inline typename This::Status
3469 movt(unsigned char* view,
3470 const Sized_relobj<32, big_endian>* object,
3471 const Symbol_value<32>* psymval,
3472 Arm_address relative_address_base)
3474 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3475 Valtype* wv = reinterpret_cast<Valtype*>(view);
3476 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3477 Valtype addend = This::extract_arm_movw_movt_addend(val);
3478 Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3479 val = This::insert_val_arm_movw_movt(val, x);
3480 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3481 // FIXME: IHI0044D says that we should check for overflow.
3482 return This::STATUS_OKAY;
3485 // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
3486 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3487 // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3488 // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
3489 static inline typename This::Status
3490 thm_movw(unsigned char* view,
3491 const Sized_relobj<32, big_endian>* object,
3492 const Symbol_value<32>* psymval,
3493 Arm_address relative_address_base,
3494 Arm_address thumb_bit,
3495 bool check_overflow)
3497 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3498 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3499 Valtype* wv = reinterpret_cast<Valtype*>(view);
3500 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3501 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3502 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3503 Reltype x =
3504 (psymval->value(object, addend) | thumb_bit) - relative_address_base;
3505 val = This::insert_val_thumb_movw_movt(val, x);
3506 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3507 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3508 return ((check_overflow && utils::has_overflow<16>(x))
3509 ? This::STATUS_OVERFLOW
3510 : This::STATUS_OKAY);
3513 // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
3514 // R_ARM_THM_MOVT_PREL: S + A - P
3515 // R_ARM_THM_MOVT_BREL: S + A - B(S)
3516 static inline typename This::Status
3517 thm_movt(unsigned char* view,
3518 const Sized_relobj<32, big_endian>* object,
3519 const Symbol_value<32>* psymval,
3520 Arm_address relative_address_base)
3522 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3523 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3524 Valtype* wv = reinterpret_cast<Valtype*>(view);
3525 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3526 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3527 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3528 Reltype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3529 val = This::insert_val_thumb_movw_movt(val, x);
3530 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3531 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3532 return This::STATUS_OKAY;
3535 // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3536 static inline typename This::Status
3537 thm_alu11(unsigned char* view,
3538 const Sized_relobj<32, big_endian>* object,
3539 const Symbol_value<32>* psymval,
3540 Arm_address address,
3541 Arm_address thumb_bit)
3543 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3544 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3545 Valtype* wv = reinterpret_cast<Valtype*>(view);
3546 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3547 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3549 // 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
3550 // -----------------------------------------------------------------------
3551 // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
3552 // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
3553 // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
3554 // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
3555 // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
3556 // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
3558 // Determine a sign for the addend.
3559 const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
3560 || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3561 // Thumb2 addend encoding:
3562 // imm12 := i | imm3 | imm8
3563 int32_t addend = (insn & 0xff)
3564 | ((insn & 0x00007000) >> 4)
3565 | ((insn & 0x04000000) >> 15);
3566 // Apply a sign to the added.
3567 addend *= sign;
3569 int32_t x = (psymval->value(object, addend) | thumb_bit)
3570 - (address & 0xfffffffc);
3571 Reltype val = abs(x);
3572 // Mask out the value and a distinct part of the ADD/SUB opcode
3573 // (bits 7:5 of opword).
3574 insn = (insn & 0xfb0f8f00)
3575 | (val & 0xff)
3576 | ((val & 0x700) << 4)
3577 | ((val & 0x800) << 15);
3578 // Set the opcode according to whether the value to go in the
3579 // place is negative.
3580 if (x < 0)
3581 insn |= 0x00a00000;
3583 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3584 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3585 return ((val > 0xfff) ?
3586 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3589 // R_ARM_THM_PC8: S + A - Pa (Thumb)
3590 static inline typename This::Status
3591 thm_pc8(unsigned char* view,
3592 const Sized_relobj<32, big_endian>* object,
3593 const Symbol_value<32>* psymval,
3594 Arm_address address)
3596 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3597 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3598 Valtype* wv = reinterpret_cast<Valtype*>(view);
3599 Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
3600 Reltype addend = ((insn & 0x00ff) << 2);
3601 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3602 Reltype val = abs(x);
3603 insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
3605 elfcpp::Swap<16, big_endian>::writeval(wv, insn);
3606 return ((val > 0x03fc)
3607 ? This::STATUS_OVERFLOW
3608 : This::STATUS_OKAY);
3611 // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3612 static inline typename This::Status
3613 thm_pc12(unsigned char* view,
3614 const Sized_relobj<32, big_endian>* object,
3615 const Symbol_value<32>* psymval,
3616 Arm_address address)
3618 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3619 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3620 Valtype* wv = reinterpret_cast<Valtype*>(view);
3621 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3622 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3623 // Determine a sign for the addend (positive if the U bit is 1).
3624 const int sign = (insn & 0x00800000) ? 1 : -1;
3625 int32_t addend = (insn & 0xfff);
3626 // Apply a sign to the added.
3627 addend *= sign;
3629 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3630 Reltype val = abs(x);
3631 // Mask out and apply the value and the U bit.
3632 insn = (insn & 0xff7ff000) | (val & 0xfff);
3633 // Set the U bit according to whether the value to go in the
3634 // place is positive.
3635 if (x >= 0)
3636 insn |= 0x00800000;
3638 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3639 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3640 return ((val > 0xfff) ?
3641 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3644 // R_ARM_V4BX
3645 static inline typename This::Status
3646 v4bx(const Relocate_info<32, big_endian>* relinfo,
3647 unsigned char* view,
3648 const Arm_relobj<big_endian>* object,
3649 const Arm_address address,
3650 const bool is_interworking)
3653 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3654 Valtype* wv = reinterpret_cast<Valtype*>(view);
3655 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3657 // Ensure that we have a BX instruction.
3658 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3659 const uint32_t reg = (val & 0xf);
3660 if (is_interworking && reg != 0xf)
3662 Stub_table<big_endian>* stub_table =
3663 object->stub_table(relinfo->data_shndx);
3664 gold_assert(stub_table != NULL);
3666 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3667 gold_assert(stub != NULL);
3669 int32_t veneer_address =
3670 stub_table->address() + stub->offset() - 8 - address;
3671 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3672 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3673 // Replace with a branch to veneer (B <addr>)
3674 val = (val & 0xf0000000) | 0x0a000000
3675 | ((veneer_address >> 2) & 0x00ffffff);
3677 else
3679 // Preserve Rm (lowest four bits) and the condition code
3680 // (highest four bits). Other bits encode MOV PC,Rm.
3681 val = (val & 0xf000000f) | 0x01a0f000;
3683 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3684 return This::STATUS_OKAY;
3687 // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3688 // R_ARM_ALU_PC_G0: ((S + A) | T) - P
3689 // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3690 // R_ARM_ALU_PC_G1: ((S + A) | T) - P
3691 // R_ARM_ALU_PC_G2: ((S + A) | T) - P
3692 // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3693 // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
3694 // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3695 // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
3696 // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
3697 static inline typename This::Status
3698 arm_grp_alu(unsigned char* view,
3699 const Sized_relobj<32, big_endian>* object,
3700 const Symbol_value<32>* psymval,
3701 const int group,
3702 Arm_address address,
3703 Arm_address thumb_bit,
3704 bool check_overflow)
3706 gold_assert(group >= 0 && group < 3);
3707 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3708 Valtype* wv = reinterpret_cast<Valtype*>(view);
3709 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3711 // ALU group relocations are allowed only for the ADD/SUB instructions.
3712 // (0x00800000 - ADD, 0x00400000 - SUB)
3713 const Valtype opcode = insn & 0x01e00000;
3714 if (opcode != 0x00800000 && opcode != 0x00400000)
3715 return This::STATUS_BAD_RELOC;
3717 // Determine a sign for the addend.
3718 const int sign = (opcode == 0x00800000) ? 1 : -1;
3719 // shifter = rotate_imm * 2
3720 const uint32_t shifter = (insn & 0xf00) >> 7;
3721 // Initial addend value.
3722 int32_t addend = insn & 0xff;
3723 // Rotate addend right by shifter.
3724 addend = (addend >> shifter) | (addend << (32 - shifter));
3725 // Apply a sign to the added.
3726 addend *= sign;
3728 int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
3729 Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
3730 // Check for overflow if required
3731 if (check_overflow
3732 && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
3733 return This::STATUS_OVERFLOW;
3735 // Mask out the value and the ADD/SUB part of the opcode; take care
3736 // not to destroy the S bit.
3737 insn &= 0xff1ff000;
3738 // Set the opcode according to whether the value to go in the
3739 // place is negative.
3740 insn |= ((x < 0) ? 0x00400000 : 0x00800000);
3741 // Encode the offset (encoded Gn).
3742 insn |= gn;
3744 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3745 return This::STATUS_OKAY;
3748 // R_ARM_LDR_PC_G0: S + A - P
3749 // R_ARM_LDR_PC_G1: S + A - P
3750 // R_ARM_LDR_PC_G2: S + A - P
3751 // R_ARM_LDR_SB_G0: S + A - B(S)
3752 // R_ARM_LDR_SB_G1: S + A - B(S)
3753 // R_ARM_LDR_SB_G2: S + A - B(S)
3754 static inline typename This::Status
3755 arm_grp_ldr(unsigned char* view,
3756 const Sized_relobj<32, big_endian>* object,
3757 const Symbol_value<32>* psymval,
3758 const int group,
3759 Arm_address address)
3761 gold_assert(group >= 0 && group < 3);
3762 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3763 Valtype* wv = reinterpret_cast<Valtype*>(view);
3764 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3766 const int sign = (insn & 0x00800000) ? 1 : -1;
3767 int32_t addend = (insn & 0xfff) * sign;
3768 int32_t x = (psymval->value(object, addend) - address);
3769 // Calculate the relevant G(n-1) value to obtain this stage residual.
3770 Valtype residual =
3771 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3772 if (residual >= 0x1000)
3773 return This::STATUS_OVERFLOW;
3775 // Mask out the value and U bit.
3776 insn &= 0xff7ff000;
3777 // Set the U bit for non-negative values.
3778 if (x >= 0)
3779 insn |= 0x00800000;
3780 insn |= residual;
3782 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3783 return This::STATUS_OKAY;
3786 // R_ARM_LDRS_PC_G0: S + A - P
3787 // R_ARM_LDRS_PC_G1: S + A - P
3788 // R_ARM_LDRS_PC_G2: S + A - P
3789 // R_ARM_LDRS_SB_G0: S + A - B(S)
3790 // R_ARM_LDRS_SB_G1: S + A - B(S)
3791 // R_ARM_LDRS_SB_G2: S + A - B(S)
3792 static inline typename This::Status
3793 arm_grp_ldrs(unsigned char* view,
3794 const Sized_relobj<32, big_endian>* object,
3795 const Symbol_value<32>* psymval,
3796 const int group,
3797 Arm_address address)
3799 gold_assert(group >= 0 && group < 3);
3800 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3801 Valtype* wv = reinterpret_cast<Valtype*>(view);
3802 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3804 const int sign = (insn & 0x00800000) ? 1 : -1;
3805 int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
3806 int32_t x = (psymval->value(object, addend) - address);
3807 // Calculate the relevant G(n-1) value to obtain this stage residual.
3808 Valtype residual =
3809 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3810 if (residual >= 0x100)
3811 return This::STATUS_OVERFLOW;
3813 // Mask out the value and U bit.
3814 insn &= 0xff7ff0f0;
3815 // Set the U bit for non-negative values.
3816 if (x >= 0)
3817 insn |= 0x00800000;
3818 insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
3820 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3821 return This::STATUS_OKAY;
3824 // R_ARM_LDC_PC_G0: S + A - P
3825 // R_ARM_LDC_PC_G1: S + A - P
3826 // R_ARM_LDC_PC_G2: S + A - P
3827 // R_ARM_LDC_SB_G0: S + A - B(S)
3828 // R_ARM_LDC_SB_G1: S + A - B(S)
3829 // R_ARM_LDC_SB_G2: S + A - B(S)
3830 static inline typename This::Status
3831 arm_grp_ldc(unsigned char* view,
3832 const Sized_relobj<32, big_endian>* object,
3833 const Symbol_value<32>* psymval,
3834 const int group,
3835 Arm_address address)
3837 gold_assert(group >= 0 && group < 3);
3838 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3839 Valtype* wv = reinterpret_cast<Valtype*>(view);
3840 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3842 const int sign = (insn & 0x00800000) ? 1 : -1;
3843 int32_t addend = ((insn & 0xff) << 2) * sign;
3844 int32_t x = (psymval->value(object, addend) - address);
3845 // Calculate the relevant G(n-1) value to obtain this stage residual.
3846 Valtype residual =
3847 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3848 if ((residual & 0x3) != 0 || residual >= 0x400)
3849 return This::STATUS_OVERFLOW;
3851 // Mask out the value and U bit.
3852 insn &= 0xff7fff00;
3853 // Set the U bit for non-negative values.
3854 if (x >= 0)
3855 insn |= 0x00800000;
3856 insn |= (residual >> 2);
3858 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3859 return This::STATUS_OKAY;
3863 // Relocate ARM long branches. This handles relocation types
3864 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3865 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3866 // undefined and we do not use PLT in this relocation. In such a case,
3867 // the branch is converted into an NOP.
3869 template<bool big_endian>
3870 typename Arm_relocate_functions<big_endian>::Status
3871 Arm_relocate_functions<big_endian>::arm_branch_common(
3872 unsigned int r_type,
3873 const Relocate_info<32, big_endian>* relinfo,
3874 unsigned char* view,
3875 const Sized_symbol<32>* gsym,
3876 const Arm_relobj<big_endian>* object,
3877 unsigned int r_sym,
3878 const Symbol_value<32>* psymval,
3879 Arm_address address,
3880 Arm_address thumb_bit,
3881 bool is_weakly_undefined_without_plt)
3883 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3884 Valtype* wv = reinterpret_cast<Valtype*>(view);
3885 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3887 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3888 && ((val & 0x0f000000UL) == 0x0a000000UL);
3889 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3890 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3891 && ((val & 0x0f000000UL) == 0x0b000000UL);
3892 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3893 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3895 // Check that the instruction is valid.
3896 if (r_type == elfcpp::R_ARM_CALL)
3898 if (!insn_is_uncond_bl && !insn_is_blx)
3899 return This::STATUS_BAD_RELOC;
3901 else if (r_type == elfcpp::R_ARM_JUMP24)
3903 if (!insn_is_b && !insn_is_cond_bl)
3904 return This::STATUS_BAD_RELOC;
3906 else if (r_type == elfcpp::R_ARM_PLT32)
3908 if (!insn_is_any_branch)
3909 return This::STATUS_BAD_RELOC;
3911 else if (r_type == elfcpp::R_ARM_XPC25)
3913 // FIXME: AAELF document IH0044C does not say much about it other
3914 // than it being obsolete.
3915 if (!insn_is_any_branch)
3916 return This::STATUS_BAD_RELOC;
3918 else
3919 gold_unreachable();
3921 // A branch to an undefined weak symbol is turned into a jump to
3922 // the next instruction unless a PLT entry will be created.
3923 // Do the same for local undefined symbols.
3924 // The jump to the next instruction is optimized as a NOP depending
3925 // on the architecture.
3926 const Target_arm<big_endian>* arm_target =
3927 Target_arm<big_endian>::default_target();
3928 if (is_weakly_undefined_without_plt)
3930 gold_assert(!parameters->options().relocatable());
3931 Valtype cond = val & 0xf0000000U;
3932 if (arm_target->may_use_arm_nop())
3933 val = cond | 0x0320f000;
3934 else
3935 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3936 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3937 return This::STATUS_OKAY;
3940 Valtype addend = utils::sign_extend<26>(val << 2);
3941 Valtype branch_target = psymval->value(object, addend);
3942 int32_t branch_offset = branch_target - address;
3944 // We need a stub if the branch offset is too large or if we need
3945 // to switch mode.
3946 bool may_use_blx = arm_target->may_use_blx();
3947 Reloc_stub* stub = NULL;
3949 if (!parameters->options().relocatable()
3950 && (utils::has_overflow<26>(branch_offset)
3951 || ((thumb_bit != 0)
3952 && !(may_use_blx && r_type == elfcpp::R_ARM_CALL))))
3954 Valtype unadjusted_branch_target = psymval->value(object, 0);
3956 Stub_type stub_type =
3957 Reloc_stub::stub_type_for_reloc(r_type, address,
3958 unadjusted_branch_target,
3959 (thumb_bit != 0));
3960 if (stub_type != arm_stub_none)
3962 Stub_table<big_endian>* stub_table =
3963 object->stub_table(relinfo->data_shndx);
3964 gold_assert(stub_table != NULL);
3966 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3967 stub = stub_table->find_reloc_stub(stub_key);
3968 gold_assert(stub != NULL);
3969 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3970 branch_target = stub_table->address() + stub->offset() + addend;
3971 branch_offset = branch_target - address;
3972 gold_assert(!utils::has_overflow<26>(branch_offset));
3976 // At this point, if we still need to switch mode, the instruction
3977 // must either be a BLX or a BL that can be converted to a BLX.
3978 if (thumb_bit != 0)
3980 // Turn BL to BLX.
3981 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3982 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3985 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3986 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3987 return (utils::has_overflow<26>(branch_offset)
3988 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3991 // Relocate THUMB long branches. This handles relocation types
3992 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3993 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3994 // undefined and we do not use PLT in this relocation. In such a case,
3995 // the branch is converted into an NOP.
3997 template<bool big_endian>
3998 typename Arm_relocate_functions<big_endian>::Status
3999 Arm_relocate_functions<big_endian>::thumb_branch_common(
4000 unsigned int r_type,
4001 const Relocate_info<32, big_endian>* relinfo,
4002 unsigned char* view,
4003 const Sized_symbol<32>* gsym,
4004 const Arm_relobj<big_endian>* object,
4005 unsigned int r_sym,
4006 const Symbol_value<32>* psymval,
4007 Arm_address address,
4008 Arm_address thumb_bit,
4009 bool is_weakly_undefined_without_plt)
4011 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4012 Valtype* wv = reinterpret_cast<Valtype*>(view);
4013 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4014 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4016 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
4017 // into account.
4018 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
4019 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
4021 // Check that the instruction is valid.
4022 if (r_type == elfcpp::R_ARM_THM_CALL)
4024 if (!is_bl_insn && !is_blx_insn)
4025 return This::STATUS_BAD_RELOC;
4027 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
4029 // This cannot be a BLX.
4030 if (!is_bl_insn)
4031 return This::STATUS_BAD_RELOC;
4033 else if (r_type == elfcpp::R_ARM_THM_XPC22)
4035 // Check for Thumb to Thumb call.
4036 if (!is_blx_insn)
4037 return This::STATUS_BAD_RELOC;
4038 if (thumb_bit != 0)
4040 gold_warning(_("%s: Thumb BLX instruction targets "
4041 "thumb function '%s'."),
4042 object->name().c_str(),
4043 (gsym ? gsym->name() : "(local)"));
4044 // Convert BLX to BL.
4045 lower_insn |= 0x1000U;
4048 else
4049 gold_unreachable();
4051 // A branch to an undefined weak symbol is turned into a jump to
4052 // the next instruction unless a PLT entry will be created.
4053 // The jump to the next instruction is optimized as a NOP.W for
4054 // Thumb-2 enabled architectures.
4055 const Target_arm<big_endian>* arm_target =
4056 Target_arm<big_endian>::default_target();
4057 if (is_weakly_undefined_without_plt)
4059 gold_assert(!parameters->options().relocatable());
4060 if (arm_target->may_use_thumb2_nop())
4062 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
4063 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
4065 else
4067 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
4068 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
4070 return This::STATUS_OKAY;
4073 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
4074 Arm_address branch_target = psymval->value(object, addend);
4076 // For BLX, bit 1 of target address comes from bit 1 of base address.
4077 bool may_use_blx = arm_target->may_use_blx();
4078 if (thumb_bit == 0 && may_use_blx)
4079 branch_target = utils::bit_select(branch_target, address, 0x2);
4081 int32_t branch_offset = branch_target - address;
4083 // We need a stub if the branch offset is too large or if we need
4084 // to switch mode.
4085 bool thumb2 = arm_target->using_thumb2();
4086 if (!parameters->options().relocatable()
4087 && ((!thumb2 && utils::has_overflow<23>(branch_offset))
4088 || (thumb2 && utils::has_overflow<25>(branch_offset))
4089 || ((thumb_bit == 0)
4090 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4091 || r_type == elfcpp::R_ARM_THM_JUMP24))))
4093 Arm_address unadjusted_branch_target = psymval->value(object, 0);
4095 Stub_type stub_type =
4096 Reloc_stub::stub_type_for_reloc(r_type, address,
4097 unadjusted_branch_target,
4098 (thumb_bit != 0));
4100 if (stub_type != arm_stub_none)
4102 Stub_table<big_endian>* stub_table =
4103 object->stub_table(relinfo->data_shndx);
4104 gold_assert(stub_table != NULL);
4106 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
4107 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
4108 gold_assert(stub != NULL);
4109 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
4110 branch_target = stub_table->address() + stub->offset() + addend;
4111 if (thumb_bit == 0 && may_use_blx)
4112 branch_target = utils::bit_select(branch_target, address, 0x2);
4113 branch_offset = branch_target - address;
4117 // At this point, if we still need to switch mode, the instruction
4118 // must either be a BLX or a BL that can be converted to a BLX.
4119 if (thumb_bit == 0)
4121 gold_assert(may_use_blx
4122 && (r_type == elfcpp::R_ARM_THM_CALL
4123 || r_type == elfcpp::R_ARM_THM_XPC22));
4124 // Make sure this is a BLX.
4125 lower_insn &= ~0x1000U;
4127 else
4129 // Make sure this is a BL.
4130 lower_insn |= 0x1000U;
4133 // For a BLX instruction, make sure that the relocation is rounded up
4134 // to a word boundary. This follows the semantics of the instruction
4135 // which specifies that bit 1 of the target address will come from bit
4136 // 1 of the base address.
4137 if ((lower_insn & 0x5000U) == 0x4000U)
4138 gold_assert((branch_offset & 3) == 0);
4140 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
4141 // We use the Thumb-2 encoding, which is safe even if dealing with
4142 // a Thumb-1 instruction by virtue of our overflow check above. */
4143 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
4144 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
4146 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
4147 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
4149 gold_assert(!utils::has_overflow<25>(branch_offset));
4151 return ((thumb2
4152 ? utils::has_overflow<25>(branch_offset)
4153 : utils::has_overflow<23>(branch_offset))
4154 ? This::STATUS_OVERFLOW
4155 : This::STATUS_OKAY);
4158 // Relocate THUMB-2 long conditional branches.
4159 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
4160 // undefined and we do not use PLT in this relocation. In such a case,
4161 // the branch is converted into an NOP.
4163 template<bool big_endian>
4164 typename Arm_relocate_functions<big_endian>::Status
4165 Arm_relocate_functions<big_endian>::thm_jump19(
4166 unsigned char* view,
4167 const Arm_relobj<big_endian>* object,
4168 const Symbol_value<32>* psymval,
4169 Arm_address address,
4170 Arm_address thumb_bit)
4172 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4173 Valtype* wv = reinterpret_cast<Valtype*>(view);
4174 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4175 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4176 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
4178 Arm_address branch_target = psymval->value(object, addend);
4179 int32_t branch_offset = branch_target - address;
4181 // ??? Should handle interworking? GCC might someday try to
4182 // use this for tail calls.
4183 // FIXME: We do support thumb entry to PLT yet.
4184 if (thumb_bit == 0)
4186 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
4187 return This::STATUS_BAD_RELOC;
4190 // Put RELOCATION back into the insn.
4191 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
4192 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
4194 // Put the relocated value back in the object file:
4195 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
4196 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
4198 return (utils::has_overflow<21>(branch_offset)
4199 ? This::STATUS_OVERFLOW
4200 : This::STATUS_OKAY);
4203 // Get the GOT section, creating it if necessary.
4205 template<bool big_endian>
4206 Arm_output_data_got<big_endian>*
4207 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
4209 if (this->got_ == NULL)
4211 gold_assert(symtab != NULL && layout != NULL);
4213 this->got_ = new Arm_output_data_got<big_endian>(symtab, layout);
4215 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4216 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
4217 this->got_, ORDER_DATA, false);
4219 // The old GNU linker creates a .got.plt section. We just
4220 // create another set of data in the .got section. Note that we
4221 // always create a PLT if we create a GOT, although the PLT
4222 // might be empty.
4223 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
4224 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4225 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
4226 this->got_plt_, ORDER_DATA, false);
4228 // The first three entries are reserved.
4229 this->got_plt_->set_current_data_size(3 * 4);
4231 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
4232 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
4233 Symbol_table::PREDEFINED,
4234 this->got_plt_,
4235 0, 0, elfcpp::STT_OBJECT,
4236 elfcpp::STB_LOCAL,
4237 elfcpp::STV_HIDDEN, 0,
4238 false, false);
4240 return this->got_;
4243 // Get the dynamic reloc section, creating it if necessary.
4245 template<bool big_endian>
4246 typename Target_arm<big_endian>::Reloc_section*
4247 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
4249 if (this->rel_dyn_ == NULL)
4251 gold_assert(layout != NULL);
4252 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
4253 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
4254 elfcpp::SHF_ALLOC, this->rel_dyn_,
4255 ORDER_DYNAMIC_RELOCS, false);
4257 return this->rel_dyn_;
4260 // Insn_template methods.
4262 // Return byte size of an instruction template.
4264 size_t
4265 Insn_template::size() const
4267 switch (this->type())
4269 case THUMB16_TYPE:
4270 case THUMB16_SPECIAL_TYPE:
4271 return 2;
4272 case ARM_TYPE:
4273 case THUMB32_TYPE:
4274 case DATA_TYPE:
4275 return 4;
4276 default:
4277 gold_unreachable();
4281 // Return alignment of an instruction template.
4283 unsigned
4284 Insn_template::alignment() const
4286 switch (this->type())
4288 case THUMB16_TYPE:
4289 case THUMB16_SPECIAL_TYPE:
4290 case THUMB32_TYPE:
4291 return 2;
4292 case ARM_TYPE:
4293 case DATA_TYPE:
4294 return 4;
4295 default:
4296 gold_unreachable();
4300 // Stub_template methods.
4302 Stub_template::Stub_template(
4303 Stub_type type, const Insn_template* insns,
4304 size_t insn_count)
4305 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
4306 entry_in_thumb_mode_(false), relocs_()
4308 off_t offset = 0;
4310 // Compute byte size and alignment of stub template.
4311 for (size_t i = 0; i < insn_count; i++)
4313 unsigned insn_alignment = insns[i].alignment();
4314 size_t insn_size = insns[i].size();
4315 gold_assert((offset & (insn_alignment - 1)) == 0);
4316 this->alignment_ = std::max(this->alignment_, insn_alignment);
4317 switch (insns[i].type())
4319 case Insn_template::THUMB16_TYPE:
4320 case Insn_template::THUMB16_SPECIAL_TYPE:
4321 if (i == 0)
4322 this->entry_in_thumb_mode_ = true;
4323 break;
4325 case Insn_template::THUMB32_TYPE:
4326 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
4327 this->relocs_.push_back(Reloc(i, offset));
4328 if (i == 0)
4329 this->entry_in_thumb_mode_ = true;
4330 break;
4332 case Insn_template::ARM_TYPE:
4333 // Handle cases where the target is encoded within the
4334 // instruction.
4335 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
4336 this->relocs_.push_back(Reloc(i, offset));
4337 break;
4339 case Insn_template::DATA_TYPE:
4340 // Entry point cannot be data.
4341 gold_assert(i != 0);
4342 this->relocs_.push_back(Reloc(i, offset));
4343 break;
4345 default:
4346 gold_unreachable();
4348 offset += insn_size;
4350 this->size_ = offset;
4353 // Stub methods.
4355 // Template to implement do_write for a specific target endianness.
4357 template<bool big_endian>
4358 void inline
4359 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
4361 const Stub_template* stub_template = this->stub_template();
4362 const Insn_template* insns = stub_template->insns();
4364 // FIXME: We do not handle BE8 encoding yet.
4365 unsigned char* pov = view;
4366 for (size_t i = 0; i < stub_template->insn_count(); i++)
4368 switch (insns[i].type())
4370 case Insn_template::THUMB16_TYPE:
4371 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
4372 break;
4373 case Insn_template::THUMB16_SPECIAL_TYPE:
4374 elfcpp::Swap<16, big_endian>::writeval(
4375 pov,
4376 this->thumb16_special(i));
4377 break;
4378 case Insn_template::THUMB32_TYPE:
4380 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
4381 uint32_t lo = insns[i].data() & 0xffff;
4382 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
4383 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
4385 break;
4386 case Insn_template::ARM_TYPE:
4387 case Insn_template::DATA_TYPE:
4388 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
4389 break;
4390 default:
4391 gold_unreachable();
4393 pov += insns[i].size();
4395 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
4398 // Reloc_stub::Key methods.
4400 // Dump a Key as a string for debugging.
4402 std::string
4403 Reloc_stub::Key::name() const
4405 if (this->r_sym_ == invalid_index)
4407 // Global symbol key name
4408 // <stub-type>:<symbol name>:<addend>.
4409 const std::string sym_name = this->u_.symbol->name();
4410 // We need to print two hex number and two colons. So just add 100 bytes
4411 // to the symbol name size.
4412 size_t len = sym_name.size() + 100;
4413 char* buffer = new char[len];
4414 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
4415 sym_name.c_str(), this->addend_);
4416 gold_assert(c > 0 && c < static_cast<int>(len));
4417 delete[] buffer;
4418 return std::string(buffer);
4420 else
4422 // local symbol key name
4423 // <stub-type>:<object>:<r_sym>:<addend>.
4424 const size_t len = 200;
4425 char buffer[len];
4426 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
4427 this->u_.relobj, this->r_sym_, this->addend_);
4428 gold_assert(c > 0 && c < static_cast<int>(len));
4429 return std::string(buffer);
4433 // Reloc_stub methods.
4435 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
4436 // LOCATION to DESTINATION.
4437 // This code is based on the arm_type_of_stub function in
4438 // bfd/elf32-arm.c. We have changed the interface a little to keep the Stub
4439 // class simple.
4441 Stub_type
4442 Reloc_stub::stub_type_for_reloc(
4443 unsigned int r_type,
4444 Arm_address location,
4445 Arm_address destination,
4446 bool target_is_thumb)
4448 Stub_type stub_type = arm_stub_none;
4450 // This is a bit ugly but we want to avoid using a templated class for
4451 // big and little endianities.
4452 bool may_use_blx;
4453 bool should_force_pic_veneer;
4454 bool thumb2;
4455 bool thumb_only;
4456 if (parameters->target().is_big_endian())
4458 const Target_arm<true>* big_endian_target =
4459 Target_arm<true>::default_target();
4460 may_use_blx = big_endian_target->may_use_blx();
4461 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
4462 thumb2 = big_endian_target->using_thumb2();
4463 thumb_only = big_endian_target->using_thumb_only();
4465 else
4467 const Target_arm<false>* little_endian_target =
4468 Target_arm<false>::default_target();
4469 may_use_blx = little_endian_target->may_use_blx();
4470 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
4471 thumb2 = little_endian_target->using_thumb2();
4472 thumb_only = little_endian_target->using_thumb_only();
4475 int64_t branch_offset;
4476 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
4478 // For THUMB BLX instruction, bit 1 of target comes from bit 1 of the
4479 // base address (instruction address + 4).
4480 if ((r_type == elfcpp::R_ARM_THM_CALL) && may_use_blx && !target_is_thumb)
4481 destination = utils::bit_select(destination, location, 0x2);
4482 branch_offset = static_cast<int64_t>(destination) - location;
4484 // Handle cases where:
4485 // - this call goes too far (different Thumb/Thumb2 max
4486 // distance)
4487 // - it's a Thumb->Arm call and blx is not available, or it's a
4488 // Thumb->Arm branch (not bl). A stub is needed in this case.
4489 if ((!thumb2
4490 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4491 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4492 || (thumb2
4493 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4494 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4495 || ((!target_is_thumb)
4496 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4497 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
4499 if (target_is_thumb)
4501 // Thumb to thumb.
4502 if (!thumb_only)
4504 stub_type = (parameters->options().shared()
4505 || should_force_pic_veneer)
4506 // PIC stubs.
4507 ? ((may_use_blx
4508 && (r_type == elfcpp::R_ARM_THM_CALL))
4509 // V5T and above. Stub starts with ARM code, so
4510 // we must be able to switch mode before
4511 // reaching it, which is only possible for 'bl'
4512 // (ie R_ARM_THM_CALL relocation).
4513 ? arm_stub_long_branch_any_thumb_pic
4514 // On V4T, use Thumb code only.
4515 : arm_stub_long_branch_v4t_thumb_thumb_pic)
4517 // non-PIC stubs.
4518 : ((may_use_blx
4519 && (r_type == elfcpp::R_ARM_THM_CALL))
4520 ? arm_stub_long_branch_any_any // V5T and above.
4521 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
4523 else
4525 stub_type = (parameters->options().shared()
4526 || should_force_pic_veneer)
4527 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
4528 : arm_stub_long_branch_thumb_only; // non-PIC stub.
4531 else
4533 // Thumb to arm.
4535 // FIXME: We should check that the input section is from an
4536 // object that has interwork enabled.
4538 stub_type = (parameters->options().shared()
4539 || should_force_pic_veneer)
4540 // PIC stubs.
4541 ? ((may_use_blx
4542 && (r_type == elfcpp::R_ARM_THM_CALL))
4543 ? arm_stub_long_branch_any_arm_pic // V5T and above.
4544 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
4546 // non-PIC stubs.
4547 : ((may_use_blx
4548 && (r_type == elfcpp::R_ARM_THM_CALL))
4549 ? arm_stub_long_branch_any_any // V5T and above.
4550 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
4552 // Handle v4t short branches.
4553 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4554 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4555 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4556 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4560 else if (r_type == elfcpp::R_ARM_CALL
4561 || r_type == elfcpp::R_ARM_JUMP24
4562 || r_type == elfcpp::R_ARM_PLT32)
4564 branch_offset = static_cast<int64_t>(destination) - location;
4565 if (target_is_thumb)
4567 // Arm to thumb.
4569 // FIXME: We should check that the input section is from an
4570 // object that has interwork enabled.
4572 // We have an extra 2-bytes reach because of
4573 // the mode change (bit 24 (H) of BLX encoding).
4574 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4575 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4576 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
4577 || (r_type == elfcpp::R_ARM_JUMP24)
4578 || (r_type == elfcpp::R_ARM_PLT32))
4580 stub_type = (parameters->options().shared()
4581 || should_force_pic_veneer)
4582 // PIC stubs.
4583 ? (may_use_blx
4584 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
4585 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
4587 // non-PIC stubs.
4588 : (may_use_blx
4589 ? arm_stub_long_branch_any_any // V5T and above.
4590 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
4593 else
4595 // Arm to arm.
4596 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4597 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4599 stub_type = (parameters->options().shared()
4600 || should_force_pic_veneer)
4601 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
4602 : arm_stub_long_branch_any_any; /// non-PIC.
4607 return stub_type;
4610 // Cortex_a8_stub methods.
4612 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4613 // I is the position of the instruction template in the stub template.
4615 uint16_t
4616 Cortex_a8_stub::do_thumb16_special(size_t i)
4618 // The only use of this is to copy condition code from a conditional
4619 // branch being worked around to the corresponding conditional branch in
4620 // to the stub.
4621 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4622 && i == 0);
4623 uint16_t data = this->stub_template()->insns()[i].data();
4624 gold_assert((data & 0xff00U) == 0xd000U);
4625 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
4626 return data;
4629 // Stub_factory methods.
4631 Stub_factory::Stub_factory()
4633 // The instruction template sequences are declared as static
4634 // objects and initialized first time the constructor runs.
4636 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4637 // to reach the stub if necessary.
4638 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
4640 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4641 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4642 // dcd R_ARM_ABS32(X)
4645 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4646 // available.
4647 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
4649 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4650 Insn_template::arm_insn(0xe12fff1c), // bx ip
4651 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4652 // dcd R_ARM_ABS32(X)
4655 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4656 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
4658 Insn_template::thumb16_insn(0xb401), // push {r0}
4659 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4660 Insn_template::thumb16_insn(0x4684), // mov ip, r0
4661 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4662 Insn_template::thumb16_insn(0x4760), // bx ip
4663 Insn_template::thumb16_insn(0xbf00), // nop
4664 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4665 // dcd R_ARM_ABS32(X)
4668 // V4T Thumb -> Thumb long branch stub. Using the stack is not
4669 // allowed.
4670 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
4672 Insn_template::thumb16_insn(0x4778), // bx pc
4673 Insn_template::thumb16_insn(0x46c0), // nop
4674 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4675 Insn_template::arm_insn(0xe12fff1c), // bx ip
4676 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4677 // dcd R_ARM_ABS32(X)
4680 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4681 // available.
4682 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
4684 Insn_template::thumb16_insn(0x4778), // bx pc
4685 Insn_template::thumb16_insn(0x46c0), // nop
4686 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4687 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4688 // dcd R_ARM_ABS32(X)
4691 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4692 // one, when the destination is close enough.
4693 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
4695 Insn_template::thumb16_insn(0x4778), // bx pc
4696 Insn_template::thumb16_insn(0x46c0), // nop
4697 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4700 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4701 // blx to reach the stub if necessary.
4702 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
4704 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4705 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4706 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4707 // dcd R_ARM_REL32(X-4)
4710 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4711 // blx to reach the stub if necessary. We can not add into pc;
4712 // it is not guaranteed to mode switch (different in ARMv6 and
4713 // ARMv7).
4714 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
4716 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4717 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4718 Insn_template::arm_insn(0xe12fff1c), // bx ip
4719 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4720 // dcd R_ARM_REL32(X)
4723 // V4T ARM -> ARM long branch stub, PIC.
4724 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4726 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4727 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4728 Insn_template::arm_insn(0xe12fff1c), // bx ip
4729 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4730 // dcd R_ARM_REL32(X)
4733 // V4T Thumb -> ARM long branch stub, PIC.
4734 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4736 Insn_template::thumb16_insn(0x4778), // bx pc
4737 Insn_template::thumb16_insn(0x46c0), // nop
4738 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4739 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4740 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4741 // dcd R_ARM_REL32(X)
4744 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4745 // architectures.
4746 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4748 Insn_template::thumb16_insn(0xb401), // push {r0}
4749 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4750 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4751 Insn_template::thumb16_insn(0x4484), // add ip, r0
4752 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4753 Insn_template::thumb16_insn(0x4760), // bx ip
4754 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4755 // dcd R_ARM_REL32(X)
4758 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4759 // allowed.
4760 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4762 Insn_template::thumb16_insn(0x4778), // bx pc
4763 Insn_template::thumb16_insn(0x46c0), // nop
4764 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4765 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4766 Insn_template::arm_insn(0xe12fff1c), // bx ip
4767 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4768 // dcd R_ARM_REL32(X)
4771 // Cortex-A8 erratum-workaround stubs.
4773 // Stub used for conditional branches (which may be beyond +/-1MB away,
4774 // so we can't use a conditional branch to reach this stub).
4776 // original code:
4778 // b<cond> X
4779 // after:
4781 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4783 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4784 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4785 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4786 // b.w X
4789 // Stub used for b.w and bl.w instructions.
4791 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4793 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4796 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4798 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4801 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4802 // instruction (which switches to ARM mode) to point to this stub. Jump to
4803 // the real destination using an ARM-mode branch.
4804 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
4806 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4809 // Stub used to provide an interworking for R_ARM_V4BX relocation
4810 // (bx r[n] instruction).
4811 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4813 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4814 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4815 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4818 // Fill in the stub template look-up table. Stub templates are constructed
4819 // per instance of Stub_factory for fast look-up without locking
4820 // in a thread-enabled environment.
4822 this->stub_templates_[arm_stub_none] =
4823 new Stub_template(arm_stub_none, NULL, 0);
4825 #define DEF_STUB(x) \
4826 do \
4828 size_t array_size \
4829 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4830 Stub_type type = arm_stub_##x; \
4831 this->stub_templates_[type] = \
4832 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4834 while (0);
4836 DEF_STUBS
4837 #undef DEF_STUB
4840 // Stub_table methods.
4842 // Remove all Cortex-A8 stub.
4844 template<bool big_endian>
4845 void
4846 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4848 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4849 p != this->cortex_a8_stubs_.end();
4850 ++p)
4851 delete p->second;
4852 this->cortex_a8_stubs_.clear();
4855 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4857 template<bool big_endian>
4858 void
4859 Stub_table<big_endian>::relocate_stub(
4860 Stub* stub,
4861 const Relocate_info<32, big_endian>* relinfo,
4862 Target_arm<big_endian>* arm_target,
4863 Output_section* output_section,
4864 unsigned char* view,
4865 Arm_address address,
4866 section_size_type view_size)
4868 const Stub_template* stub_template = stub->stub_template();
4869 if (stub_template->reloc_count() != 0)
4871 // Adjust view to cover the stub only.
4872 section_size_type offset = stub->offset();
4873 section_size_type stub_size = stub_template->size();
4874 gold_assert(offset + stub_size <= view_size);
4876 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4877 address + offset, stub_size);
4881 // Relocate all stubs in this stub table.
4883 template<bool big_endian>
4884 void
4885 Stub_table<big_endian>::relocate_stubs(
4886 const Relocate_info<32, big_endian>* relinfo,
4887 Target_arm<big_endian>* arm_target,
4888 Output_section* output_section,
4889 unsigned char* view,
4890 Arm_address address,
4891 section_size_type view_size)
4893 // If we are passed a view bigger than the stub table's. we need to
4894 // adjust the view.
4895 gold_assert(address == this->address()
4896 && (view_size
4897 == static_cast<section_size_type>(this->data_size())));
4899 // Relocate all relocation stubs.
4900 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4901 p != this->reloc_stubs_.end();
4902 ++p)
4903 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4904 address, view_size);
4906 // Relocate all Cortex-A8 stubs.
4907 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4908 p != this->cortex_a8_stubs_.end();
4909 ++p)
4910 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4911 address, view_size);
4913 // Relocate all ARM V4BX stubs.
4914 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4915 p != this->arm_v4bx_stubs_.end();
4916 ++p)
4918 if (*p != NULL)
4919 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4920 address, view_size);
4924 // Write out the stubs to file.
4926 template<bool big_endian>
4927 void
4928 Stub_table<big_endian>::do_write(Output_file* of)
4930 off_t offset = this->offset();
4931 const section_size_type oview_size =
4932 convert_to_section_size_type(this->data_size());
4933 unsigned char* const oview = of->get_output_view(offset, oview_size);
4935 // Write relocation stubs.
4936 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4937 p != this->reloc_stubs_.end();
4938 ++p)
4940 Reloc_stub* stub = p->second;
4941 Arm_address address = this->address() + stub->offset();
4942 gold_assert(address
4943 == align_address(address,
4944 stub->stub_template()->alignment()));
4945 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4946 big_endian);
4949 // Write Cortex-A8 stubs.
4950 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4951 p != this->cortex_a8_stubs_.end();
4952 ++p)
4954 Cortex_a8_stub* stub = p->second;
4955 Arm_address address = this->address() + stub->offset();
4956 gold_assert(address
4957 == align_address(address,
4958 stub->stub_template()->alignment()));
4959 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4960 big_endian);
4963 // Write ARM V4BX relocation stubs.
4964 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4965 p != this->arm_v4bx_stubs_.end();
4966 ++p)
4968 if (*p == NULL)
4969 continue;
4971 Arm_address address = this->address() + (*p)->offset();
4972 gold_assert(address
4973 == align_address(address,
4974 (*p)->stub_template()->alignment()));
4975 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4976 big_endian);
4979 of->write_output_view(this->offset(), oview_size, oview);
4982 // Update the data size and address alignment of the stub table at the end
4983 // of a relaxation pass. Return true if either the data size or the
4984 // alignment changed in this relaxation pass.
4986 template<bool big_endian>
4987 bool
4988 Stub_table<big_endian>::update_data_size_and_addralign()
4990 // Go over all stubs in table to compute data size and address alignment.
4991 off_t size = this->reloc_stubs_size_;
4992 unsigned addralign = this->reloc_stubs_addralign_;
4994 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4995 p != this->cortex_a8_stubs_.end();
4996 ++p)
4998 const Stub_template* stub_template = p->second->stub_template();
4999 addralign = std::max(addralign, stub_template->alignment());
5000 size = (align_address(size, stub_template->alignment())
5001 + stub_template->size());
5004 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
5005 p != this->arm_v4bx_stubs_.end();
5006 ++p)
5008 if (*p == NULL)
5009 continue;
5011 const Stub_template* stub_template = (*p)->stub_template();
5012 addralign = std::max(addralign, stub_template->alignment());
5013 size = (align_address(size, stub_template->alignment())
5014 + stub_template->size());
5017 // Check if either data size or alignment changed in this pass.
5018 // Update prev_data_size_ and prev_addralign_. These will be used
5019 // as the current data size and address alignment for the next pass.
5020 bool changed = size != this->prev_data_size_;
5021 this->prev_data_size_ = size;
5023 if (addralign != this->prev_addralign_)
5024 changed = true;
5025 this->prev_addralign_ = addralign;
5027 return changed;
5030 // Finalize the stubs. This sets the offsets of the stubs within the stub
5031 // table. It also marks all input sections needing Cortex-A8 workaround.
5033 template<bool big_endian>
5034 void
5035 Stub_table<big_endian>::finalize_stubs()
5037 off_t off = this->reloc_stubs_size_;
5038 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
5039 p != this->cortex_a8_stubs_.end();
5040 ++p)
5042 Cortex_a8_stub* stub = p->second;
5043 const Stub_template* stub_template = stub->stub_template();
5044 uint64_t stub_addralign = stub_template->alignment();
5045 off = align_address(off, stub_addralign);
5046 stub->set_offset(off);
5047 off += stub_template->size();
5049 // Mark input section so that we can determine later if a code section
5050 // needs the Cortex-A8 workaround quickly.
5051 Arm_relobj<big_endian>* arm_relobj =
5052 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
5053 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
5056 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
5057 p != this->arm_v4bx_stubs_.end();
5058 ++p)
5060 if (*p == NULL)
5061 continue;
5063 const Stub_template* stub_template = (*p)->stub_template();
5064 uint64_t stub_addralign = stub_template->alignment();
5065 off = align_address(off, stub_addralign);
5066 (*p)->set_offset(off);
5067 off += stub_template->size();
5070 gold_assert(off <= this->prev_data_size_);
5073 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
5074 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
5075 // of the address range seen by the linker.
5077 template<bool big_endian>
5078 void
5079 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
5080 Target_arm<big_endian>* arm_target,
5081 unsigned char* view,
5082 Arm_address view_address,
5083 section_size_type view_size)
5085 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
5086 for (Cortex_a8_stub_list::const_iterator p =
5087 this->cortex_a8_stubs_.lower_bound(view_address);
5088 ((p != this->cortex_a8_stubs_.end())
5089 && (p->first < (view_address + view_size)));
5090 ++p)
5092 // We do not store the THUMB bit in the LSB of either the branch address
5093 // or the stub offset. There is no need to strip the LSB.
5094 Arm_address branch_address = p->first;
5095 const Cortex_a8_stub* stub = p->second;
5096 Arm_address stub_address = this->address() + stub->offset();
5098 // Offset of the branch instruction relative to this view.
5099 section_size_type offset =
5100 convert_to_section_size_type(branch_address - view_address);
5101 gold_assert((offset + 4) <= view_size);
5103 arm_target->apply_cortex_a8_workaround(stub, stub_address,
5104 view + offset, branch_address);
5108 // Arm_input_section methods.
5110 // Initialize an Arm_input_section.
5112 template<bool big_endian>
5113 void
5114 Arm_input_section<big_endian>::init()
5116 Relobj* relobj = this->relobj();
5117 unsigned int shndx = this->shndx();
5119 // We have to cache original size, alignment and contents to avoid locking
5120 // the original file.
5121 this->original_addralign_ =
5122 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
5124 // This is not efficient but we expect only a small number of relaxed
5125 // input sections for stubs.
5126 section_size_type section_size;
5127 const unsigned char* section_contents =
5128 relobj->section_contents(shndx, &section_size, false);
5129 this->original_size_ =
5130 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
5132 gold_assert(this->original_contents_ == NULL);
5133 this->original_contents_ = new unsigned char[section_size];
5134 memcpy(this->original_contents_, section_contents, section_size);
5136 // We want to make this look like the original input section after
5137 // output sections are finalized.
5138 Output_section* os = relobj->output_section(shndx);
5139 off_t offset = relobj->output_section_offset(shndx);
5140 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
5141 this->set_address(os->address() + offset);
5142 this->set_file_offset(os->offset() + offset);
5144 this->set_current_data_size(this->original_size_);
5145 this->finalize_data_size();
5148 template<bool big_endian>
5149 void
5150 Arm_input_section<big_endian>::do_write(Output_file* of)
5152 // We have to write out the original section content.
5153 gold_assert(this->original_contents_ != NULL);
5154 of->write(this->offset(), this->original_contents_,
5155 this->original_size_);
5157 // If this owns a stub table and it is not empty, write it.
5158 if (this->is_stub_table_owner() && !this->stub_table_->empty())
5159 this->stub_table_->write(of);
5162 // Finalize data size.
5164 template<bool big_endian>
5165 void
5166 Arm_input_section<big_endian>::set_final_data_size()
5168 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
5170 if (this->is_stub_table_owner())
5172 this->stub_table_->finalize_data_size();
5173 off = align_address(off, this->stub_table_->addralign());
5174 off += this->stub_table_->data_size();
5176 this->set_data_size(off);
5179 // Reset address and file offset.
5181 template<bool big_endian>
5182 void
5183 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
5185 // Size of the original input section contents.
5186 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
5188 // If this is a stub table owner, account for the stub table size.
5189 if (this->is_stub_table_owner())
5191 Stub_table<big_endian>* stub_table = this->stub_table_;
5193 // Reset the stub table's address and file offset. The
5194 // current data size for child will be updated after that.
5195 stub_table_->reset_address_and_file_offset();
5196 off = align_address(off, stub_table_->addralign());
5197 off += stub_table->current_data_size();
5200 this->set_current_data_size(off);
5203 // Arm_exidx_cantunwind methods.
5205 // Write this to Output file OF for a fixed endianness.
5207 template<bool big_endian>
5208 void
5209 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
5211 off_t offset = this->offset();
5212 const section_size_type oview_size = 8;
5213 unsigned char* const oview = of->get_output_view(offset, oview_size);
5215 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5216 Valtype* wv = reinterpret_cast<Valtype*>(oview);
5218 Output_section* os = this->relobj_->output_section(this->shndx_);
5219 gold_assert(os != NULL);
5221 Arm_relobj<big_endian>* arm_relobj =
5222 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
5223 Arm_address output_offset =
5224 arm_relobj->get_output_section_offset(this->shndx_);
5225 Arm_address section_start;
5226 section_size_type section_size;
5228 // Find out the end of the text section referred by this.
5229 if (output_offset != Arm_relobj<big_endian>::invalid_address)
5231 section_start = os->address() + output_offset;
5232 const Arm_exidx_input_section* exidx_input_section =
5233 arm_relobj->exidx_input_section_by_link(this->shndx_);
5234 gold_assert(exidx_input_section != NULL);
5235 section_size =
5236 convert_to_section_size_type(exidx_input_section->text_size());
5238 else
5240 // Currently this only happens for a relaxed section.
5241 const Output_relaxed_input_section* poris =
5242 os->find_relaxed_input_section(this->relobj_, this->shndx_);
5243 gold_assert(poris != NULL);
5244 section_start = poris->address();
5245 section_size = convert_to_section_size_type(poris->data_size());
5248 // We always append this to the end of an EXIDX section.
5249 Arm_address output_address = section_start + section_size;
5251 // Write out the entry. The first word either points to the beginning
5252 // or after the end of a text section. The second word is the special
5253 // EXIDX_CANTUNWIND value.
5254 uint32_t prel31_offset = output_address - this->address();
5255 if (utils::has_overflow<31>(offset))
5256 gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
5257 elfcpp::Swap<32, big_endian>::writeval(wv, prel31_offset & 0x7fffffffU);
5258 elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
5260 of->write_output_view(this->offset(), oview_size, oview);
5263 // Arm_exidx_merged_section methods.
5265 // Constructor for Arm_exidx_merged_section.
5266 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
5267 // SECTION_OFFSET_MAP points to a section offset map describing how
5268 // parts of the input section are mapped to output. DELETED_BYTES is
5269 // the number of bytes deleted from the EXIDX input section.
5271 Arm_exidx_merged_section::Arm_exidx_merged_section(
5272 const Arm_exidx_input_section& exidx_input_section,
5273 const Arm_exidx_section_offset_map& section_offset_map,
5274 uint32_t deleted_bytes)
5275 : Output_relaxed_input_section(exidx_input_section.relobj(),
5276 exidx_input_section.shndx(),
5277 exidx_input_section.addralign()),
5278 exidx_input_section_(exidx_input_section),
5279 section_offset_map_(section_offset_map)
5281 // If we retain or discard the whole EXIDX input section, we would
5282 // not be here.
5283 gold_assert(deleted_bytes != 0
5284 && deleted_bytes != this->exidx_input_section_.size());
5286 // Fix size here so that we do not need to implement set_final_data_size.
5287 uint32_t size = exidx_input_section.size() - deleted_bytes;
5288 this->set_data_size(size);
5289 this->fix_data_size();
5291 // Allocate buffer for section contents and build contents.
5292 this->section_contents_ = new unsigned char[size];
5295 // Build the contents of a merged EXIDX output section.
5297 void
5298 Arm_exidx_merged_section::build_contents(
5299 const unsigned char* original_contents,
5300 section_size_type original_size)
5302 // Go over spans of input offsets and write only those that are not
5303 // discarded.
5304 section_offset_type in_start = 0;
5305 section_offset_type out_start = 0;
5306 section_offset_type in_max =
5307 convert_types<section_offset_type>(original_size);
5308 section_offset_type out_max =
5309 convert_types<section_offset_type>(this->data_size());
5310 for (Arm_exidx_section_offset_map::const_iterator p =
5311 this->section_offset_map_.begin();
5312 p != this->section_offset_map_.end();
5313 ++p)
5315 section_offset_type in_end = p->first;
5316 gold_assert(in_end >= in_start);
5317 section_offset_type out_end = p->second;
5318 size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
5319 if (out_end != -1)
5321 size_t out_chunk_size =
5322 convert_types<size_t>(out_end - out_start + 1);
5324 gold_assert(out_chunk_size == in_chunk_size
5325 && in_end < in_max && out_end < out_max);
5327 memcpy(this->section_contents_ + out_start,
5328 original_contents + in_start,
5329 out_chunk_size);
5330 out_start += out_chunk_size;
5332 in_start += in_chunk_size;
5336 // Given an input OBJECT, an input section index SHNDX within that
5337 // object, and an OFFSET relative to the start of that input
5338 // section, return whether or not the corresponding offset within
5339 // the output section is known. If this function returns true, it
5340 // sets *POUTPUT to the output offset. The value -1 indicates that
5341 // this input offset is being discarded.
5343 bool
5344 Arm_exidx_merged_section::do_output_offset(
5345 const Relobj* relobj,
5346 unsigned int shndx,
5347 section_offset_type offset,
5348 section_offset_type* poutput) const
5350 // We only handle offsets for the original EXIDX input section.
5351 if (relobj != this->exidx_input_section_.relobj()
5352 || shndx != this->exidx_input_section_.shndx())
5353 return false;
5355 section_offset_type section_size =
5356 convert_types<section_offset_type>(this->exidx_input_section_.size());
5357 if (offset < 0 || offset >= section_size)
5358 // Input offset is out of valid range.
5359 *poutput = -1;
5360 else
5362 // We need to look up the section offset map to determine the output
5363 // offset. Find the reference point in map that is first offset
5364 // bigger than or equal to this offset.
5365 Arm_exidx_section_offset_map::const_iterator p =
5366 this->section_offset_map_.lower_bound(offset);
5368 // The section offset maps are build such that this should not happen if
5369 // input offset is in the valid range.
5370 gold_assert(p != this->section_offset_map_.end());
5372 // We need to check if this is dropped.
5373 section_offset_type ref = p->first;
5374 section_offset_type mapped_ref = p->second;
5376 if (mapped_ref != Arm_exidx_input_section::invalid_offset)
5377 // Offset is present in output.
5378 *poutput = mapped_ref + (offset - ref);
5379 else
5380 // Offset is discarded owing to EXIDX entry merging.
5381 *poutput = -1;
5384 return true;
5387 // Write this to output file OF.
5389 void
5390 Arm_exidx_merged_section::do_write(Output_file* of)
5392 off_t offset = this->offset();
5393 const section_size_type oview_size = this->data_size();
5394 unsigned char* const oview = of->get_output_view(offset, oview_size);
5396 Output_section* os = this->relobj()->output_section(this->shndx());
5397 gold_assert(os != NULL);
5399 memcpy(oview, this->section_contents_, oview_size);
5400 of->write_output_view(this->offset(), oview_size, oview);
5403 // Arm_exidx_fixup methods.
5405 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
5406 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
5407 // points to the end of the last seen EXIDX section.
5409 void
5410 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
5412 if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
5413 && this->last_input_section_ != NULL)
5415 Relobj* relobj = this->last_input_section_->relobj();
5416 unsigned int text_shndx = this->last_input_section_->link();
5417 Arm_exidx_cantunwind* cantunwind =
5418 new Arm_exidx_cantunwind(relobj, text_shndx);
5419 this->exidx_output_section_->add_output_section_data(cantunwind);
5420 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5424 // Process an EXIDX section entry in input. Return whether this entry
5425 // can be deleted in the output. SECOND_WORD in the second word of the
5426 // EXIDX entry.
5428 bool
5429 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
5431 bool delete_entry;
5432 if (second_word == elfcpp::EXIDX_CANTUNWIND)
5434 // Merge if previous entry is also an EXIDX_CANTUNWIND.
5435 delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
5436 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5438 else if ((second_word & 0x80000000) != 0)
5440 // Inlined unwinding data. Merge if equal to previous.
5441 delete_entry = (merge_exidx_entries_
5442 && this->last_unwind_type_ == UT_INLINED_ENTRY
5443 && this->last_inlined_entry_ == second_word);
5444 this->last_unwind_type_ = UT_INLINED_ENTRY;
5445 this->last_inlined_entry_ = second_word;
5447 else
5449 // Normal table entry. In theory we could merge these too,
5450 // but duplicate entries are likely to be much less common.
5451 delete_entry = false;
5452 this->last_unwind_type_ = UT_NORMAL_ENTRY;
5454 return delete_entry;
5457 // Update the current section offset map during EXIDX section fix-up.
5458 // If there is no map, create one. INPUT_OFFSET is the offset of a
5459 // reference point, DELETED_BYTES is the number of deleted by in the
5460 // section so far. If DELETE_ENTRY is true, the reference point and
5461 // all offsets after the previous reference point are discarded.
5463 void
5464 Arm_exidx_fixup::update_offset_map(
5465 section_offset_type input_offset,
5466 section_size_type deleted_bytes,
5467 bool delete_entry)
5469 if (this->section_offset_map_ == NULL)
5470 this->section_offset_map_ = new Arm_exidx_section_offset_map();
5471 section_offset_type output_offset;
5472 if (delete_entry)
5473 output_offset = Arm_exidx_input_section::invalid_offset;
5474 else
5475 output_offset = input_offset - deleted_bytes;
5476 (*this->section_offset_map_)[input_offset] = output_offset;
5479 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
5480 // bytes deleted. SECTION_CONTENTS points to the contents of the EXIDX
5481 // section and SECTION_SIZE is the number of bytes pointed by SECTION_CONTENTS.
5482 // If some entries are merged, also store a pointer to a newly created
5483 // Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The caller
5484 // owns the map and is responsible for releasing it after use.
5486 template<bool big_endian>
5487 uint32_t
5488 Arm_exidx_fixup::process_exidx_section(
5489 const Arm_exidx_input_section* exidx_input_section,
5490 const unsigned char* section_contents,
5491 section_size_type section_size,
5492 Arm_exidx_section_offset_map** psection_offset_map)
5494 Relobj* relobj = exidx_input_section->relobj();
5495 unsigned shndx = exidx_input_section->shndx();
5497 if ((section_size % 8) != 0)
5499 // Something is wrong with this section. Better not touch it.
5500 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5501 relobj->name().c_str(), shndx);
5502 this->last_input_section_ = exidx_input_section;
5503 this->last_unwind_type_ = UT_NONE;
5504 return 0;
5507 uint32_t deleted_bytes = 0;
5508 bool prev_delete_entry = false;
5509 gold_assert(this->section_offset_map_ == NULL);
5511 for (section_size_type i = 0; i < section_size; i += 8)
5513 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5514 const Valtype* wv =
5515 reinterpret_cast<const Valtype*>(section_contents + i + 4);
5516 uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
5518 bool delete_entry = this->process_exidx_entry(second_word);
5520 // Entry deletion causes changes in output offsets. We use a std::map
5521 // to record these. And entry (x, y) means input offset x
5522 // is mapped to output offset y. If y is invalid_offset, then x is
5523 // dropped in the output. Because of the way std::map::lower_bound
5524 // works, we record the last offset in a region w.r.t to keeping or
5525 // dropping. If there is no entry (x0, y0) for an input offset x0,
5526 // the output offset y0 of it is determined by the output offset y1 of
5527 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5528 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Otherwise, y1
5529 // y0 is also -1.
5530 if (delete_entry != prev_delete_entry && i != 0)
5531 this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
5533 // Update total deleted bytes for this entry.
5534 if (delete_entry)
5535 deleted_bytes += 8;
5537 prev_delete_entry = delete_entry;
5540 // If section offset map is not NULL, make an entry for the end of
5541 // section.
5542 if (this->section_offset_map_ != NULL)
5543 update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
5545 *psection_offset_map = this->section_offset_map_;
5546 this->section_offset_map_ = NULL;
5547 this->last_input_section_ = exidx_input_section;
5549 // Set the first output text section so that we can link the EXIDX output
5550 // section to it. Ignore any EXIDX input section that is completely merged.
5551 if (this->first_output_text_section_ == NULL
5552 && deleted_bytes != section_size)
5554 unsigned int link = exidx_input_section->link();
5555 Output_section* os = relobj->output_section(link);
5556 gold_assert(os != NULL);
5557 this->first_output_text_section_ = os;
5560 return deleted_bytes;
5563 // Arm_output_section methods.
5565 // Create a stub group for input sections from BEGIN to END. OWNER
5566 // points to the input section to be the owner a new stub table.
5568 template<bool big_endian>
5569 void
5570 Arm_output_section<big_endian>::create_stub_group(
5571 Input_section_list::const_iterator begin,
5572 Input_section_list::const_iterator end,
5573 Input_section_list::const_iterator owner,
5574 Target_arm<big_endian>* target,
5575 std::vector<Output_relaxed_input_section*>* new_relaxed_sections,
5576 const Task* task)
5578 // We use a different kind of relaxed section in an EXIDX section.
5579 // The static casting from Output_relaxed_input_section to
5580 // Arm_input_section is invalid in an EXIDX section. We are okay
5581 // because we should not be calling this for an EXIDX section.
5582 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
5584 // Currently we convert ordinary input sections into relaxed sections only
5585 // at this point but we may want to support creating relaxed input section
5586 // very early. So we check here to see if owner is already a relaxed
5587 // section.
5589 Arm_input_section<big_endian>* arm_input_section;
5590 if (owner->is_relaxed_input_section())
5592 arm_input_section =
5593 Arm_input_section<big_endian>::as_arm_input_section(
5594 owner->relaxed_input_section());
5596 else
5598 gold_assert(owner->is_input_section());
5599 // Create a new relaxed input section. We need to lock the original
5600 // file.
5601 Task_lock_obj<Object> tl(task, owner->relobj());
5602 arm_input_section =
5603 target->new_arm_input_section(owner->relobj(), owner->shndx());
5604 new_relaxed_sections->push_back(arm_input_section);
5607 // Create a stub table.
5608 Stub_table<big_endian>* stub_table =
5609 target->new_stub_table(arm_input_section);
5611 arm_input_section->set_stub_table(stub_table);
5613 Input_section_list::const_iterator p = begin;
5614 Input_section_list::const_iterator prev_p;
5616 // Look for input sections or relaxed input sections in [begin ... end].
5619 if (p->is_input_section() || p->is_relaxed_input_section())
5621 // The stub table information for input sections live
5622 // in their objects.
5623 Arm_relobj<big_endian>* arm_relobj =
5624 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5625 arm_relobj->set_stub_table(p->shndx(), stub_table);
5627 prev_p = p++;
5629 while (prev_p != end);
5632 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
5633 // of stub groups. We grow a stub group by adding input section until the
5634 // size is just below GROUP_SIZE. The last input section will be converted
5635 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5636 // input section after the stub table, effectively double the group size.
5638 // This is similar to the group_sections() function in elf32-arm.c but is
5639 // implemented differently.
5641 template<bool big_endian>
5642 void
5643 Arm_output_section<big_endian>::group_sections(
5644 section_size_type group_size,
5645 bool stubs_always_after_branch,
5646 Target_arm<big_endian>* target,
5647 const Task* task)
5649 // We only care about sections containing code.
5650 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5651 return;
5653 // States for grouping.
5654 typedef enum
5656 // No group is being built.
5657 NO_GROUP,
5658 // A group is being built but the stub table is not found yet.
5659 // We keep group a stub group until the size is just under GROUP_SIZE.
5660 // The last input section in the group will be used as the stub table.
5661 FINDING_STUB_SECTION,
5662 // A group is being built and we have already found a stub table.
5663 // We enter this state to grow a stub group by adding input section
5664 // after the stub table. This effectively doubles the group size.
5665 HAS_STUB_SECTION
5666 } State;
5668 // Any newly created relaxed sections are stored here.
5669 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
5671 State state = NO_GROUP;
5672 section_size_type off = 0;
5673 section_size_type group_begin_offset = 0;
5674 section_size_type group_end_offset = 0;
5675 section_size_type stub_table_end_offset = 0;
5676 Input_section_list::const_iterator group_begin =
5677 this->input_sections().end();
5678 Input_section_list::const_iterator stub_table =
5679 this->input_sections().end();
5680 Input_section_list::const_iterator group_end = this->input_sections().end();
5681 for (Input_section_list::const_iterator p = this->input_sections().begin();
5682 p != this->input_sections().end();
5683 ++p)
5685 section_size_type section_begin_offset =
5686 align_address(off, p->addralign());
5687 section_size_type section_end_offset =
5688 section_begin_offset + p->data_size();
5690 // Check to see if we should group the previously seen sections.
5691 switch (state)
5693 case NO_GROUP:
5694 break;
5696 case FINDING_STUB_SECTION:
5697 // Adding this section makes the group larger than GROUP_SIZE.
5698 if (section_end_offset - group_begin_offset >= group_size)
5700 if (stubs_always_after_branch)
5702 gold_assert(group_end != this->input_sections().end());
5703 this->create_stub_group(group_begin, group_end, group_end,
5704 target, &new_relaxed_sections,
5705 task);
5706 state = NO_GROUP;
5708 else
5710 // But wait, there's more! Input sections up to
5711 // stub_group_size bytes after the stub table can be
5712 // handled by it too.
5713 state = HAS_STUB_SECTION;
5714 stub_table = group_end;
5715 stub_table_end_offset = group_end_offset;
5718 break;
5720 case HAS_STUB_SECTION:
5721 // Adding this section makes the post stub-section group larger
5722 // than GROUP_SIZE.
5723 if (section_end_offset - stub_table_end_offset >= group_size)
5725 gold_assert(group_end != this->input_sections().end());
5726 this->create_stub_group(group_begin, group_end, stub_table,
5727 target, &new_relaxed_sections, task);
5728 state = NO_GROUP;
5730 break;
5732 default:
5733 gold_unreachable();
5736 // If we see an input section and currently there is no group, start
5737 // a new one. Skip any empty sections. We look at the data size
5738 // instead of calling p->relobj()->section_size() to avoid locking.
5739 if ((p->is_input_section() || p->is_relaxed_input_section())
5740 && (p->data_size() != 0))
5742 if (state == NO_GROUP)
5744 state = FINDING_STUB_SECTION;
5745 group_begin = p;
5746 group_begin_offset = section_begin_offset;
5749 // Keep track of the last input section seen.
5750 group_end = p;
5751 group_end_offset = section_end_offset;
5754 off = section_end_offset;
5757 // Create a stub group for any ungrouped sections.
5758 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5760 gold_assert(group_end != this->input_sections().end());
5761 this->create_stub_group(group_begin, group_end,
5762 (state == FINDING_STUB_SECTION
5763 ? group_end
5764 : stub_table),
5765 target, &new_relaxed_sections, task);
5768 // Convert input section into relaxed input section in a batch.
5769 if (!new_relaxed_sections.empty())
5770 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5772 // Update the section offsets
5773 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5775 Arm_relobj<big_endian>* arm_relobj =
5776 Arm_relobj<big_endian>::as_arm_relobj(
5777 new_relaxed_sections[i]->relobj());
5778 unsigned int shndx = new_relaxed_sections[i]->shndx();
5779 // Tell Arm_relobj that this input section is converted.
5780 arm_relobj->convert_input_section_to_relaxed_section(shndx);
5784 // Append non empty text sections in this to LIST in ascending
5785 // order of their position in this.
5787 template<bool big_endian>
5788 void
5789 Arm_output_section<big_endian>::append_text_sections_to_list(
5790 Text_section_list* list)
5792 gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
5794 for (Input_section_list::const_iterator p = this->input_sections().begin();
5795 p != this->input_sections().end();
5796 ++p)
5798 // We only care about plain or relaxed input sections. We also
5799 // ignore any merged sections.
5800 if ((p->is_input_section() || p->is_relaxed_input_section())
5801 && p->data_size() != 0)
5802 list->push_back(Text_section_list::value_type(p->relobj(),
5803 p->shndx()));
5807 template<bool big_endian>
5808 void
5809 Arm_output_section<big_endian>::fix_exidx_coverage(
5810 Layout* layout,
5811 const Text_section_list& sorted_text_sections,
5812 Symbol_table* symtab,
5813 bool merge_exidx_entries,
5814 const Task* task)
5816 // We should only do this for the EXIDX output section.
5817 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5819 // We don't want the relaxation loop to undo these changes, so we discard
5820 // the current saved states and take another one after the fix-up.
5821 this->discard_states();
5823 // Remove all input sections.
5824 uint64_t address = this->address();
5825 typedef std::list<Output_section::Input_section> Input_section_list;
5826 Input_section_list input_sections;
5827 this->reset_address_and_file_offset();
5828 this->get_input_sections(address, std::string(""), &input_sections);
5830 if (!this->input_sections().empty())
5831 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5833 // Go through all the known input sections and record them.
5834 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5835 typedef Unordered_map<Section_id, const Output_section::Input_section*,
5836 Section_id_hash> Text_to_exidx_map;
5837 Text_to_exidx_map text_to_exidx_map;
5838 for (Input_section_list::const_iterator p = input_sections.begin();
5839 p != input_sections.end();
5840 ++p)
5842 // This should never happen. At this point, we should only see
5843 // plain EXIDX input sections.
5844 gold_assert(!p->is_relaxed_input_section());
5845 text_to_exidx_map[Section_id(p->relobj(), p->shndx())] = &(*p);
5848 Arm_exidx_fixup exidx_fixup(this, merge_exidx_entries);
5850 // Go over the sorted text sections.
5851 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5852 Section_id_set processed_input_sections;
5853 for (Text_section_list::const_iterator p = sorted_text_sections.begin();
5854 p != sorted_text_sections.end();
5855 ++p)
5857 Relobj* relobj = p->first;
5858 unsigned int shndx = p->second;
5860 Arm_relobj<big_endian>* arm_relobj =
5861 Arm_relobj<big_endian>::as_arm_relobj(relobj);
5862 const Arm_exidx_input_section* exidx_input_section =
5863 arm_relobj->exidx_input_section_by_link(shndx);
5865 // If this text section has no EXIDX section or if the EXIDX section
5866 // has errors, force an EXIDX_CANTUNWIND entry pointing to the end
5867 // of the last seen EXIDX section.
5868 if (exidx_input_section == NULL || exidx_input_section->has_errors())
5870 exidx_fixup.add_exidx_cantunwind_as_needed();
5871 continue;
5874 Relobj* exidx_relobj = exidx_input_section->relobj();
5875 unsigned int exidx_shndx = exidx_input_section->shndx();
5876 Section_id sid(exidx_relobj, exidx_shndx);
5877 Text_to_exidx_map::const_iterator iter = text_to_exidx_map.find(sid);
5878 if (iter == text_to_exidx_map.end())
5880 // This is odd. We have not seen this EXIDX input section before.
5881 // We cannot do fix-up. If we saw a SECTIONS clause in a script,
5882 // issue a warning instead. We assume the user knows what he
5883 // or she is doing. Otherwise, this is an error.
5884 if (layout->script_options()->saw_sections_clause())
5885 gold_warning(_("unwinding may not work because EXIDX input section"
5886 " %u of %s is not in EXIDX output section"),
5887 exidx_shndx, exidx_relobj->name().c_str());
5888 else
5889 gold_error(_("unwinding may not work because EXIDX input section"
5890 " %u of %s is not in EXIDX output section"),
5891 exidx_shndx, exidx_relobj->name().c_str());
5893 exidx_fixup.add_exidx_cantunwind_as_needed();
5894 continue;
5897 // We need to access the contents of the EXIDX section, lock the
5898 // object here.
5899 Task_lock_obj<Object> tl(task, exidx_relobj);
5900 section_size_type exidx_size;
5901 const unsigned char* exidx_contents =
5902 exidx_relobj->section_contents(exidx_shndx, &exidx_size, false);
5904 // Fix up coverage and append input section to output data list.
5905 Arm_exidx_section_offset_map* section_offset_map = NULL;
5906 uint32_t deleted_bytes =
5907 exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
5908 exidx_contents,
5909 exidx_size,
5910 &section_offset_map);
5912 if (deleted_bytes == exidx_input_section->size())
5914 // The whole EXIDX section got merged. Remove it from output.
5915 gold_assert(section_offset_map == NULL);
5916 exidx_relobj->set_output_section(exidx_shndx, NULL);
5918 // All local symbols defined in this input section will be dropped.
5919 // We need to adjust output local symbol count.
5920 arm_relobj->set_output_local_symbol_count_needs_update();
5922 else if (deleted_bytes > 0)
5924 // Some entries are merged. We need to convert this EXIDX input
5925 // section into a relaxed section.
5926 gold_assert(section_offset_map != NULL);
5928 Arm_exidx_merged_section* merged_section =
5929 new Arm_exidx_merged_section(*exidx_input_section,
5930 *section_offset_map, deleted_bytes);
5931 merged_section->build_contents(exidx_contents, exidx_size);
5933 const std::string secname = exidx_relobj->section_name(exidx_shndx);
5934 this->add_relaxed_input_section(layout, merged_section, secname);
5935 arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
5937 // All local symbols defined in discarded portions of this input
5938 // section will be dropped. We need to adjust output local symbol
5939 // count.
5940 arm_relobj->set_output_local_symbol_count_needs_update();
5942 else
5944 // Just add back the EXIDX input section.
5945 gold_assert(section_offset_map == NULL);
5946 const Output_section::Input_section* pis = iter->second;
5947 gold_assert(pis->is_input_section());
5948 this->add_script_input_section(*pis);
5951 processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx));
5954 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5955 exidx_fixup.add_exidx_cantunwind_as_needed();
5957 // Remove any known EXIDX input sections that are not processed.
5958 for (Input_section_list::const_iterator p = input_sections.begin();
5959 p != input_sections.end();
5960 ++p)
5962 if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
5963 == processed_input_sections.end())
5965 // We discard a known EXIDX section because its linked
5966 // text section has been folded by ICF. We also discard an
5967 // EXIDX section with error, the output does not matter in this
5968 // case. We do this to avoid triggering asserts.
5969 Arm_relobj<big_endian>* arm_relobj =
5970 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5971 const Arm_exidx_input_section* exidx_input_section =
5972 arm_relobj->exidx_input_section_by_shndx(p->shndx());
5973 gold_assert(exidx_input_section != NULL);
5974 if (!exidx_input_section->has_errors())
5976 unsigned int text_shndx = exidx_input_section->link();
5977 gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
5980 // Remove this from link. We also need to recount the
5981 // local symbols.
5982 p->relobj()->set_output_section(p->shndx(), NULL);
5983 arm_relobj->set_output_local_symbol_count_needs_update();
5987 // Link exidx output section to the first seen output section and
5988 // set correct entry size.
5989 this->set_link_section(exidx_fixup.first_output_text_section());
5990 this->set_entsize(8);
5992 // Make changes permanent.
5993 this->save_states();
5994 this->set_section_offsets_need_adjustment();
5997 // Link EXIDX output sections to text output sections.
5999 template<bool big_endian>
6000 void
6001 Arm_output_section<big_endian>::set_exidx_section_link()
6003 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
6004 if (!this->input_sections().empty())
6006 Input_section_list::const_iterator p = this->input_sections().begin();
6007 Arm_relobj<big_endian>* arm_relobj =
6008 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
6009 unsigned exidx_shndx = p->shndx();
6010 const Arm_exidx_input_section* exidx_input_section =
6011 arm_relobj->exidx_input_section_by_shndx(exidx_shndx);
6012 gold_assert(exidx_input_section != NULL);
6013 unsigned int text_shndx = exidx_input_section->link();
6014 Output_section* os = arm_relobj->output_section(text_shndx);
6015 this->set_link_section(os);
6019 // Arm_relobj methods.
6021 // Determine if an input section is scannable for stub processing. SHDR is
6022 // the header of the section and SHNDX is the section index. OS is the output
6023 // section for the input section and SYMTAB is the global symbol table used to
6024 // look up ICF information.
6026 template<bool big_endian>
6027 bool
6028 Arm_relobj<big_endian>::section_is_scannable(
6029 const elfcpp::Shdr<32, big_endian>& shdr,
6030 unsigned int shndx,
6031 const Output_section* os,
6032 const Symbol_table* symtab)
6034 // Skip any empty sections, unallocated sections or sections whose
6035 // type are not SHT_PROGBITS.
6036 if (shdr.get_sh_size() == 0
6037 || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
6038 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
6039 return false;
6041 // Skip any discarded or ICF'ed sections.
6042 if (os == NULL || symtab->is_section_folded(this, shndx))
6043 return false;
6045 // If this requires special offset handling, check to see if it is
6046 // a relaxed section. If this is not, then it is a merged section that
6047 // we cannot handle.
6048 if (this->is_output_section_offset_invalid(shndx))
6050 const Output_relaxed_input_section* poris =
6051 os->find_relaxed_input_section(this, shndx);
6052 if (poris == NULL)
6053 return false;
6056 return true;
6059 // Determine if we want to scan the SHNDX-th section for relocation stubs.
6060 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
6062 template<bool big_endian>
6063 bool
6064 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
6065 const elfcpp::Shdr<32, big_endian>& shdr,
6066 const Relobj::Output_sections& out_sections,
6067 const Symbol_table* symtab,
6068 const unsigned char* pshdrs)
6070 unsigned int sh_type = shdr.get_sh_type();
6071 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
6072 return false;
6074 // Ignore empty section.
6075 off_t sh_size = shdr.get_sh_size();
6076 if (sh_size == 0)
6077 return false;
6079 // Ignore reloc section with unexpected symbol table. The
6080 // error will be reported in the final link.
6081 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
6082 return false;
6084 unsigned int reloc_size;
6085 if (sh_type == elfcpp::SHT_REL)
6086 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6087 else
6088 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6090 // Ignore reloc section with unexpected entsize or uneven size.
6091 // The error will be reported in the final link.
6092 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
6093 return false;
6095 // Ignore reloc section with bad info. This error will be
6096 // reported in the final link.
6097 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
6098 if (index >= this->shnum())
6099 return false;
6101 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6102 const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
6103 return this->section_is_scannable(text_shdr, index,
6104 out_sections[index], symtab);
6107 // Return the output address of either a plain input section or a relaxed
6108 // input section. SHNDX is the section index. We define and use this
6109 // instead of calling Output_section::output_address because that is slow
6110 // for large output.
6112 template<bool big_endian>
6113 Arm_address
6114 Arm_relobj<big_endian>::simple_input_section_output_address(
6115 unsigned int shndx,
6116 Output_section* os)
6118 if (this->is_output_section_offset_invalid(shndx))
6120 const Output_relaxed_input_section* poris =
6121 os->find_relaxed_input_section(this, shndx);
6122 // We do not handle merged sections here.
6123 gold_assert(poris != NULL);
6124 return poris->address();
6126 else
6127 return os->address() + this->get_output_section_offset(shndx);
6130 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
6131 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
6133 template<bool big_endian>
6134 bool
6135 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
6136 const elfcpp::Shdr<32, big_endian>& shdr,
6137 unsigned int shndx,
6138 Output_section* os,
6139 const Symbol_table* symtab)
6141 if (!this->section_is_scannable(shdr, shndx, os, symtab))
6142 return false;
6144 // If the section does not cross any 4K-boundaries, it does not need to
6145 // be scanned.
6146 Arm_address address = this->simple_input_section_output_address(shndx, os);
6147 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
6148 return false;
6150 return true;
6153 // Scan a section for Cortex-A8 workaround.
6155 template<bool big_endian>
6156 void
6157 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
6158 const elfcpp::Shdr<32, big_endian>& shdr,
6159 unsigned int shndx,
6160 Output_section* os,
6161 Target_arm<big_endian>* arm_target)
6163 // Look for the first mapping symbol in this section. It should be
6164 // at (shndx, 0).
6165 Mapping_symbol_position section_start(shndx, 0);
6166 typename Mapping_symbols_info::const_iterator p =
6167 this->mapping_symbols_info_.lower_bound(section_start);
6169 // There are no mapping symbols for this section. Treat it as a data-only
6170 // section. Issue a warning if section is marked as containing
6171 // instructions.
6172 if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
6174 if ((this->section_flags(shndx) & elfcpp::SHF_EXECINSTR) != 0)
6175 gold_warning(_("cannot scan executable section %u of %s for Cortex-A8 "
6176 "erratum because it has no mapping symbols."),
6177 shndx, this->name().c_str());
6178 return;
6181 Arm_address output_address =
6182 this->simple_input_section_output_address(shndx, os);
6184 // Get the section contents.
6185 section_size_type input_view_size = 0;
6186 const unsigned char* input_view =
6187 this->section_contents(shndx, &input_view_size, false);
6189 // We need to go through the mapping symbols to determine what to
6190 // scan. There are two reasons. First, we should look at THUMB code and
6191 // THUMB code only. Second, we only want to look at the 4K-page boundary
6192 // to speed up the scanning.
6194 while (p != this->mapping_symbols_info_.end()
6195 && p->first.first == shndx)
6197 typename Mapping_symbols_info::const_iterator next =
6198 this->mapping_symbols_info_.upper_bound(p->first);
6200 // Only scan part of a section with THUMB code.
6201 if (p->second == 't')
6203 // Determine the end of this range.
6204 section_size_type span_start =
6205 convert_to_section_size_type(p->first.second);
6206 section_size_type span_end;
6207 if (next != this->mapping_symbols_info_.end()
6208 && next->first.first == shndx)
6209 span_end = convert_to_section_size_type(next->first.second);
6210 else
6211 span_end = convert_to_section_size_type(shdr.get_sh_size());
6213 if (((span_start + output_address) & ~0xfffUL)
6214 != ((span_end + output_address - 1) & ~0xfffUL))
6216 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
6217 span_start, span_end,
6218 input_view,
6219 output_address);
6223 p = next;
6227 // Scan relocations for stub generation.
6229 template<bool big_endian>
6230 void
6231 Arm_relobj<big_endian>::scan_sections_for_stubs(
6232 Target_arm<big_endian>* arm_target,
6233 const Symbol_table* symtab,
6234 const Layout* layout)
6236 unsigned int shnum = this->shnum();
6237 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6239 // Read the section headers.
6240 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6241 shnum * shdr_size,
6242 true, true);
6244 // To speed up processing, we set up hash tables for fast lookup of
6245 // input offsets to output addresses.
6246 this->initialize_input_to_output_maps();
6248 const Relobj::Output_sections& out_sections(this->output_sections());
6250 Relocate_info<32, big_endian> relinfo;
6251 relinfo.symtab = symtab;
6252 relinfo.layout = layout;
6253 relinfo.object = this;
6255 // Do relocation stubs scanning.
6256 const unsigned char* p = pshdrs + shdr_size;
6257 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
6259 const elfcpp::Shdr<32, big_endian> shdr(p);
6260 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
6261 pshdrs))
6263 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
6264 Arm_address output_offset = this->get_output_section_offset(index);
6265 Arm_address output_address;
6266 if (output_offset != invalid_address)
6267 output_address = out_sections[index]->address() + output_offset;
6268 else
6270 // Currently this only happens for a relaxed section.
6271 const Output_relaxed_input_section* poris =
6272 out_sections[index]->find_relaxed_input_section(this, index);
6273 gold_assert(poris != NULL);
6274 output_address = poris->address();
6277 // Get the relocations.
6278 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
6279 shdr.get_sh_size(),
6280 true, false);
6282 // Get the section contents. This does work for the case in which
6283 // we modify the contents of an input section. We need to pass the
6284 // output view under such circumstances.
6285 section_size_type input_view_size = 0;
6286 const unsigned char* input_view =
6287 this->section_contents(index, &input_view_size, false);
6289 relinfo.reloc_shndx = i;
6290 relinfo.data_shndx = index;
6291 unsigned int sh_type = shdr.get_sh_type();
6292 unsigned int reloc_size;
6293 if (sh_type == elfcpp::SHT_REL)
6294 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6295 else
6296 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6298 Output_section* os = out_sections[index];
6299 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
6300 shdr.get_sh_size() / reloc_size,
6302 output_offset == invalid_address,
6303 input_view, output_address,
6304 input_view_size);
6308 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
6309 // after its relocation section, if there is one, is processed for
6310 // relocation stubs. Merging this loop with the one above would have been
6311 // complicated since we would have had to make sure that relocation stub
6312 // scanning is done first.
6313 if (arm_target->fix_cortex_a8())
6315 const unsigned char* p = pshdrs + shdr_size;
6316 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
6318 const elfcpp::Shdr<32, big_endian> shdr(p);
6319 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
6320 out_sections[i],
6321 symtab))
6322 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
6323 arm_target);
6327 // After we've done the relocations, we release the hash tables,
6328 // since we no longer need them.
6329 this->free_input_to_output_maps();
6332 // Count the local symbols. The ARM backend needs to know if a symbol
6333 // is a THUMB function or not. For global symbols, it is easy because
6334 // the Symbol object keeps the ELF symbol type. For local symbol it is
6335 // harder because we cannot access this information. So we override the
6336 // do_count_local_symbol in parent and scan local symbols to mark
6337 // THUMB functions. This is not the most efficient way but I do not want to
6338 // slow down other ports by calling a per symbol target hook inside
6339 // Sized_relobj<size, big_endian>::do_count_local_symbols.
6341 template<bool big_endian>
6342 void
6343 Arm_relobj<big_endian>::do_count_local_symbols(
6344 Stringpool_template<char>* pool,
6345 Stringpool_template<char>* dynpool)
6347 // We need to fix-up the values of any local symbols whose type are
6348 // STT_ARM_TFUNC.
6350 // Ask parent to count the local symbols.
6351 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
6352 const unsigned int loccount = this->local_symbol_count();
6353 if (loccount == 0)
6354 return;
6356 // Initialize the thumb function bit-vector.
6357 std::vector<bool> empty_vector(loccount, false);
6358 this->local_symbol_is_thumb_function_.swap(empty_vector);
6360 // Read the symbol table section header.
6361 const unsigned int symtab_shndx = this->symtab_shndx();
6362 elfcpp::Shdr<32, big_endian>
6363 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6364 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6366 // Read the local symbols.
6367 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6368 gold_assert(loccount == symtabshdr.get_sh_info());
6369 off_t locsize = loccount * sym_size;
6370 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6371 locsize, true, true);
6373 // For mapping symbol processing, we need to read the symbol names.
6374 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
6375 if (strtab_shndx >= this->shnum())
6377 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
6378 return;
6381 elfcpp::Shdr<32, big_endian>
6382 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
6383 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
6385 this->error(_("symbol table name section has wrong type: %u"),
6386 static_cast<unsigned int>(strtabshdr.get_sh_type()));
6387 return;
6389 const char* pnames =
6390 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
6391 strtabshdr.get_sh_size(),
6392 false, false));
6394 // Loop over the local symbols and mark any local symbols pointing
6395 // to THUMB functions.
6397 // Skip the first dummy symbol.
6398 psyms += sym_size;
6399 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
6400 this->local_values();
6401 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6403 elfcpp::Sym<32, big_endian> sym(psyms);
6404 elfcpp::STT st_type = sym.get_st_type();
6405 Symbol_value<32>& lv((*plocal_values)[i]);
6406 Arm_address input_value = lv.input_value();
6408 // Check to see if this is a mapping symbol.
6409 const char* sym_name = pnames + sym.get_st_name();
6410 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
6412 bool is_ordinary;
6413 unsigned int input_shndx =
6414 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
6415 gold_assert(is_ordinary);
6417 // Strip of LSB in case this is a THUMB symbol.
6418 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
6419 this->mapping_symbols_info_[msp] = sym_name[1];
6422 if (st_type == elfcpp::STT_ARM_TFUNC
6423 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
6425 // This is a THUMB function. Mark this and canonicalize the
6426 // symbol value by setting LSB.
6427 this->local_symbol_is_thumb_function_[i] = true;
6428 if ((input_value & 1) == 0)
6429 lv.set_input_value(input_value | 1);
6434 // Relocate sections.
6435 template<bool big_endian>
6436 void
6437 Arm_relobj<big_endian>::do_relocate_sections(
6438 const Symbol_table* symtab,
6439 const Layout* layout,
6440 const unsigned char* pshdrs,
6441 Output_file* of,
6442 typename Sized_relobj<32, big_endian>::Views* pviews)
6444 // Call parent to relocate sections.
6445 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
6446 of, pviews);
6448 // We do not generate stubs if doing a relocatable link.
6449 if (parameters->options().relocatable())
6450 return;
6452 // Relocate stub tables.
6453 unsigned int shnum = this->shnum();
6455 Target_arm<big_endian>* arm_target =
6456 Target_arm<big_endian>::default_target();
6458 Relocate_info<32, big_endian> relinfo;
6459 relinfo.symtab = symtab;
6460 relinfo.layout = layout;
6461 relinfo.object = this;
6463 for (unsigned int i = 1; i < shnum; ++i)
6465 Arm_input_section<big_endian>* arm_input_section =
6466 arm_target->find_arm_input_section(this, i);
6468 if (arm_input_section != NULL
6469 && arm_input_section->is_stub_table_owner()
6470 && !arm_input_section->stub_table()->empty())
6472 // We cannot discard a section if it owns a stub table.
6473 Output_section* os = this->output_section(i);
6474 gold_assert(os != NULL);
6476 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
6477 relinfo.reloc_shdr = NULL;
6478 relinfo.data_shndx = i;
6479 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
6481 gold_assert((*pviews)[i].view != NULL);
6483 // We are passed the output section view. Adjust it to cover the
6484 // stub table only.
6485 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
6486 gold_assert((stub_table->address() >= (*pviews)[i].address)
6487 && ((stub_table->address() + stub_table->data_size())
6488 <= (*pviews)[i].address + (*pviews)[i].view_size));
6490 off_t offset = stub_table->address() - (*pviews)[i].address;
6491 unsigned char* view = (*pviews)[i].view + offset;
6492 Arm_address address = stub_table->address();
6493 section_size_type view_size = stub_table->data_size();
6495 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
6496 view_size);
6499 // Apply Cortex A8 workaround if applicable.
6500 if (this->section_has_cortex_a8_workaround(i))
6502 unsigned char* view = (*pviews)[i].view;
6503 Arm_address view_address = (*pviews)[i].address;
6504 section_size_type view_size = (*pviews)[i].view_size;
6505 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
6507 // Adjust view to cover section.
6508 Output_section* os = this->output_section(i);
6509 gold_assert(os != NULL);
6510 Arm_address section_address =
6511 this->simple_input_section_output_address(i, os);
6512 uint64_t section_size = this->section_size(i);
6514 gold_assert(section_address >= view_address
6515 && ((section_address + section_size)
6516 <= (view_address + view_size)));
6518 unsigned char* section_view = view + (section_address - view_address);
6520 // Apply the Cortex-A8 workaround to the output address range
6521 // corresponding to this input section.
6522 stub_table->apply_cortex_a8_workaround_to_address_range(
6523 arm_target,
6524 section_view,
6525 section_address,
6526 section_size);
6531 // Find the linked text section of an EXIDX section by looking at the first
6532 // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6533 // must be linked to its associated code section via the sh_link field of
6534 // its section header. However, some tools are broken and the link is not
6535 // always set. LD just drops such an EXIDX section silently, causing the
6536 // associated code not unwindabled. Here we try a little bit harder to
6537 // discover the linked code section.
6539 // PSHDR points to the section header of a relocation section of an EXIDX
6540 // section. If we can find a linked text section, return true and
6541 // store the text section index in the location PSHNDX. Otherwise
6542 // return false.
6544 template<bool big_endian>
6545 bool
6546 Arm_relobj<big_endian>::find_linked_text_section(
6547 const unsigned char* pshdr,
6548 const unsigned char* psyms,
6549 unsigned int* pshndx)
6551 elfcpp::Shdr<32, big_endian> shdr(pshdr);
6553 // If there is no relocation, we cannot find the linked text section.
6554 size_t reloc_size;
6555 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6556 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6557 else
6558 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6559 size_t reloc_count = shdr.get_sh_size() / reloc_size;
6561 // Get the relocations.
6562 const unsigned char* prelocs =
6563 this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false);
6565 // Find the REL31 relocation for the first word of the first EXIDX entry.
6566 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6568 Arm_address r_offset;
6569 typename elfcpp::Elf_types<32>::Elf_WXword r_info;
6570 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6572 typename elfcpp::Rel<32, big_endian> reloc(prelocs);
6573 r_info = reloc.get_r_info();
6574 r_offset = reloc.get_r_offset();
6576 else
6578 typename elfcpp::Rela<32, big_endian> reloc(prelocs);
6579 r_info = reloc.get_r_info();
6580 r_offset = reloc.get_r_offset();
6583 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6584 if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
6585 continue;
6587 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6588 if (r_sym == 0
6589 || r_sym >= this->local_symbol_count()
6590 || r_offset != 0)
6591 continue;
6593 // This is the relocation for the first word of the first EXIDX entry.
6594 // We expect to see a local section symbol.
6595 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6596 elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
6597 if (sym.get_st_type() == elfcpp::STT_SECTION)
6599 bool is_ordinary;
6600 *pshndx =
6601 this->adjust_sym_shndx(r_sym, sym.get_st_shndx(), &is_ordinary);
6602 gold_assert(is_ordinary);
6603 return true;
6605 else
6606 return false;
6609 return false;
6612 // Make an EXIDX input section object for an EXIDX section whose index is
6613 // SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
6614 // is the section index of the linked text section.
6616 template<bool big_endian>
6617 void
6618 Arm_relobj<big_endian>::make_exidx_input_section(
6619 unsigned int shndx,
6620 const elfcpp::Shdr<32, big_endian>& shdr,
6621 unsigned int text_shndx,
6622 const elfcpp::Shdr<32, big_endian>& text_shdr)
6624 // Create an Arm_exidx_input_section object for this EXIDX section.
6625 Arm_exidx_input_section* exidx_input_section =
6626 new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
6627 shdr.get_sh_addralign(),
6628 text_shdr.get_sh_size());
6630 gold_assert(this->exidx_section_map_[shndx] == NULL);
6631 this->exidx_section_map_[shndx] = exidx_input_section;
6633 if (text_shndx == elfcpp::SHN_UNDEF || text_shndx >= this->shnum())
6635 gold_error(_("EXIDX section %s(%u) links to invalid section %u in %s"),
6636 this->section_name(shndx).c_str(), shndx, text_shndx,
6637 this->name().c_str());
6638 exidx_input_section->set_has_errors();
6640 else if (this->exidx_section_map_[text_shndx] != NULL)
6642 unsigned other_exidx_shndx =
6643 this->exidx_section_map_[text_shndx]->shndx();
6644 gold_error(_("EXIDX sections %s(%u) and %s(%u) both link to text section"
6645 "%s(%u) in %s"),
6646 this->section_name(shndx).c_str(), shndx,
6647 this->section_name(other_exidx_shndx).c_str(),
6648 other_exidx_shndx, this->section_name(text_shndx).c_str(),
6649 text_shndx, this->name().c_str());
6650 exidx_input_section->set_has_errors();
6652 else
6653 this->exidx_section_map_[text_shndx] = exidx_input_section;
6655 // Check section flags of text section.
6656 if ((text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
6658 gold_error(_("EXIDX section %s(%u) links to non-allocated section %s(%u) "
6659 " in %s"),
6660 this->section_name(shndx).c_str(), shndx,
6661 this->section_name(text_shndx).c_str(), text_shndx,
6662 this->name().c_str());
6663 exidx_input_section->set_has_errors();
6665 else if ((text_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) == 0)
6666 // I would like to make this an error but currently ld just ignores
6667 // this.
6668 gold_warning(_("EXIDX section %s(%u) links to non-executable section "
6669 "%s(%u) in %s"),
6670 this->section_name(shndx).c_str(), shndx,
6671 this->section_name(text_shndx).c_str(), text_shndx,
6672 this->name().c_str());
6675 // Read the symbol information.
6677 template<bool big_endian>
6678 void
6679 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6681 // Call parent class to read symbol information.
6682 Sized_relobj<32, big_endian>::do_read_symbols(sd);
6684 // If this input file is a binary file, it has no processor
6685 // specific flags and attributes section.
6686 Input_file::Format format = this->input_file()->format();
6687 if (format != Input_file::FORMAT_ELF)
6689 gold_assert(format == Input_file::FORMAT_BINARY);
6690 this->merge_flags_and_attributes_ = false;
6691 return;
6694 // Read processor-specific flags in ELF file header.
6695 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6696 elfcpp::Elf_sizes<32>::ehdr_size,
6697 true, false);
6698 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6699 this->processor_specific_flags_ = ehdr.get_e_flags();
6701 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6702 // sections.
6703 std::vector<unsigned int> deferred_exidx_sections;
6704 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6705 const unsigned char* pshdrs = sd->section_headers->data();
6706 const unsigned char* ps = pshdrs + shdr_size;
6707 bool must_merge_flags_and_attributes = false;
6708 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6710 elfcpp::Shdr<32, big_endian> shdr(ps);
6712 // Sometimes an object has no contents except the section name string
6713 // table and an empty symbol table with the undefined symbol. We
6714 // don't want to merge processor-specific flags from such an object.
6715 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6717 // Symbol table is not empty.
6718 const elfcpp::Elf_types<32>::Elf_WXword sym_size =
6719 elfcpp::Elf_sizes<32>::sym_size;
6720 if (shdr.get_sh_size() > sym_size)
6721 must_merge_flags_and_attributes = true;
6723 else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6724 // If this is neither an empty symbol table nor a string table,
6725 // be conservative.
6726 must_merge_flags_and_attributes = true;
6728 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6730 gold_assert(this->attributes_section_data_ == NULL);
6731 section_offset_type section_offset = shdr.get_sh_offset();
6732 section_size_type section_size =
6733 convert_to_section_size_type(shdr.get_sh_size());
6734 const unsigned char* view =
6735 this->get_view(section_offset, section_size, true, false);
6736 this->attributes_section_data_ =
6737 new Attributes_section_data(view, section_size);
6739 else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6741 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6742 if (text_shndx == elfcpp::SHN_UNDEF)
6743 deferred_exidx_sections.push_back(i);
6744 else
6746 elfcpp::Shdr<32, big_endian> text_shdr(pshdrs
6747 + text_shndx * shdr_size);
6748 this->make_exidx_input_section(i, shdr, text_shndx, text_shdr);
6750 // EHABI 4.4.1 requires that SHF_LINK_ORDER flag to be set.
6751 if ((shdr.get_sh_flags() & elfcpp::SHF_LINK_ORDER) == 0)
6752 gold_warning(_("SHF_LINK_ORDER not set in EXIDX section %s of %s"),
6753 this->section_name(i).c_str(), this->name().c_str());
6757 // This is rare.
6758 if (!must_merge_flags_and_attributes)
6760 gold_assert(deferred_exidx_sections.empty());
6761 this->merge_flags_and_attributes_ = false;
6762 return;
6765 // Some tools are broken and they do not set the link of EXIDX sections.
6766 // We look at the first relocation to figure out the linked sections.
6767 if (!deferred_exidx_sections.empty())
6769 // We need to go over the section headers again to find the mapping
6770 // from sections being relocated to their relocation sections. This is
6771 // a bit inefficient as we could do that in the loop above. However,
6772 // we do not expect any deferred EXIDX sections normally. So we do not
6773 // want to slow down the most common path.
6774 typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
6775 Reloc_map reloc_map;
6776 ps = pshdrs + shdr_size;
6777 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6779 elfcpp::Shdr<32, big_endian> shdr(ps);
6780 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
6781 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
6783 unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
6784 if (info_shndx >= this->shnum())
6785 gold_error(_("relocation section %u has invalid info %u"),
6786 i, info_shndx);
6787 Reloc_map::value_type value(info_shndx, i);
6788 std::pair<Reloc_map::iterator, bool> result =
6789 reloc_map.insert(value);
6790 if (!result.second)
6791 gold_error(_("section %u has multiple relocation sections "
6792 "%u and %u"),
6793 info_shndx, i, reloc_map[info_shndx]);
6797 // Read the symbol table section header.
6798 const unsigned int symtab_shndx = this->symtab_shndx();
6799 elfcpp::Shdr<32, big_endian>
6800 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6801 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6803 // Read the local symbols.
6804 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6805 const unsigned int loccount = this->local_symbol_count();
6806 gold_assert(loccount == symtabshdr.get_sh_info());
6807 off_t locsize = loccount * sym_size;
6808 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6809 locsize, true, true);
6811 // Process the deferred EXIDX sections.
6812 for (unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
6814 unsigned int shndx = deferred_exidx_sections[i];
6815 elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
6816 unsigned int text_shndx = elfcpp::SHN_UNDEF;
6817 Reloc_map::const_iterator it = reloc_map.find(shndx);
6818 if (it != reloc_map.end())
6819 find_linked_text_section(pshdrs + it->second * shdr_size,
6820 psyms, &text_shndx);
6821 elfcpp::Shdr<32, big_endian> text_shdr(pshdrs
6822 + text_shndx * shdr_size);
6823 this->make_exidx_input_section(shndx, shdr, text_shndx, text_shdr);
6828 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
6829 // sections for unwinding. These sections are referenced implicitly by
6830 // text sections linked in the section headers. If we ignore these implicit
6831 // references, the .ARM.exidx sections and any .ARM.extab sections they use
6832 // will be garbage-collected incorrectly. Hence we override the same function
6833 // in the base class to handle these implicit references.
6835 template<bool big_endian>
6836 void
6837 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
6838 Layout* layout,
6839 Read_relocs_data* rd)
6841 // First, call base class method to process relocations in this object.
6842 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
6844 // If --gc-sections is not specified, there is nothing more to do.
6845 // This happens when --icf is used but --gc-sections is not.
6846 if (!parameters->options().gc_sections())
6847 return;
6849 unsigned int shnum = this->shnum();
6850 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6851 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6852 shnum * shdr_size,
6853 true, true);
6855 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
6856 // to these from the linked text sections.
6857 const unsigned char* ps = pshdrs + shdr_size;
6858 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
6860 elfcpp::Shdr<32, big_endian> shdr(ps);
6861 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6863 // Found an .ARM.exidx section, add it to the set of reachable
6864 // sections from its linked text section.
6865 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6866 symtab->gc()->add_reference(this, text_shndx, this, i);
6871 // Update output local symbol count. Owing to EXIDX entry merging, some local
6872 // symbols will be removed in output. Adjust output local symbol count
6873 // accordingly. We can only changed the static output local symbol count. It
6874 // is too late to change the dynamic symbols.
6876 template<bool big_endian>
6877 void
6878 Arm_relobj<big_endian>::update_output_local_symbol_count()
6880 // Caller should check that this needs updating. We want caller checking
6881 // because output_local_symbol_count_needs_update() is most likely inlined.
6882 gold_assert(this->output_local_symbol_count_needs_update_);
6884 gold_assert(this->symtab_shndx() != -1U);
6885 if (this->symtab_shndx() == 0)
6887 // This object has no symbols. Weird but legal.
6888 return;
6891 // Read the symbol table section header.
6892 const unsigned int symtab_shndx = this->symtab_shndx();
6893 elfcpp::Shdr<32, big_endian>
6894 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6895 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6897 // Read the local symbols.
6898 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6899 const unsigned int loccount = this->local_symbol_count();
6900 gold_assert(loccount == symtabshdr.get_sh_info());
6901 off_t locsize = loccount * sym_size;
6902 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6903 locsize, true, true);
6905 // Loop over the local symbols.
6907 typedef typename Sized_relobj<32, big_endian>::Output_sections
6908 Output_sections;
6909 const Output_sections& out_sections(this->output_sections());
6910 unsigned int shnum = this->shnum();
6911 unsigned int count = 0;
6912 // Skip the first, dummy, symbol.
6913 psyms += sym_size;
6914 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6916 elfcpp::Sym<32, big_endian> sym(psyms);
6918 Symbol_value<32>& lv((*this->local_values())[i]);
6920 // This local symbol was already discarded by do_count_local_symbols.
6921 if (lv.is_output_symtab_index_set() && !lv.has_output_symtab_entry())
6922 continue;
6924 bool is_ordinary;
6925 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
6926 &is_ordinary);
6928 if (shndx < shnum)
6930 Output_section* os = out_sections[shndx];
6932 // This local symbol no longer has an output section. Discard it.
6933 if (os == NULL)
6935 lv.set_no_output_symtab_entry();
6936 continue;
6939 // Currently we only discard parts of EXIDX input sections.
6940 // We explicitly check for a merged EXIDX input section to avoid
6941 // calling Output_section_data::output_offset unless necessary.
6942 if ((this->get_output_section_offset(shndx) == invalid_address)
6943 && (this->exidx_input_section_by_shndx(shndx) != NULL))
6945 section_offset_type output_offset =
6946 os->output_offset(this, shndx, lv.input_value());
6947 if (output_offset == -1)
6949 // This symbol is defined in a part of an EXIDX input section
6950 // that is discarded due to entry merging.
6951 lv.set_no_output_symtab_entry();
6952 continue;
6957 ++count;
6960 this->set_output_local_symbol_count(count);
6961 this->output_local_symbol_count_needs_update_ = false;
6964 // Arm_dynobj methods.
6966 // Read the symbol information.
6968 template<bool big_endian>
6969 void
6970 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6972 // Call parent class to read symbol information.
6973 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
6975 // Read processor-specific flags in ELF file header.
6976 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6977 elfcpp::Elf_sizes<32>::ehdr_size,
6978 true, false);
6979 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6980 this->processor_specific_flags_ = ehdr.get_e_flags();
6982 // Read the attributes section if there is one.
6983 // We read from the end because gas seems to put it near the end of
6984 // the section headers.
6985 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6986 const unsigned char* ps =
6987 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
6988 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
6990 elfcpp::Shdr<32, big_endian> shdr(ps);
6991 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6993 section_offset_type section_offset = shdr.get_sh_offset();
6994 section_size_type section_size =
6995 convert_to_section_size_type(shdr.get_sh_size());
6996 const unsigned char* view =
6997 this->get_view(section_offset, section_size, true, false);
6998 this->attributes_section_data_ =
6999 new Attributes_section_data(view, section_size);
7000 break;
7005 // Stub_addend_reader methods.
7007 // Read the addend of a REL relocation of type R_TYPE at VIEW.
7009 template<bool big_endian>
7010 elfcpp::Elf_types<32>::Elf_Swxword
7011 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
7012 unsigned int r_type,
7013 const unsigned char* view,
7014 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
7016 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
7018 switch (r_type)
7020 case elfcpp::R_ARM_CALL:
7021 case elfcpp::R_ARM_JUMP24:
7022 case elfcpp::R_ARM_PLT32:
7024 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
7025 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
7026 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
7027 return utils::sign_extend<26>(val << 2);
7030 case elfcpp::R_ARM_THM_CALL:
7031 case elfcpp::R_ARM_THM_JUMP24:
7032 case elfcpp::R_ARM_THM_XPC22:
7034 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
7035 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
7036 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
7037 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
7038 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
7041 case elfcpp::R_ARM_THM_JUMP19:
7043 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
7044 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
7045 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
7046 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
7047 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
7050 default:
7051 gold_unreachable();
7055 // Arm_output_data_got methods.
7057 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
7058 // The first one is initialized to be 1, which is the module index for
7059 // the main executable and the second one 0. A reloc of the type
7060 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
7061 // be applied by gold. GSYM is a global symbol.
7063 template<bool big_endian>
7064 void
7065 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
7066 unsigned int got_type,
7067 Symbol* gsym)
7069 if (gsym->has_got_offset(got_type))
7070 return;
7072 // We are doing a static link. Just mark it as belong to module 1,
7073 // the executable.
7074 unsigned int got_offset = this->add_constant(1);
7075 gsym->set_got_offset(got_type, got_offset);
7076 got_offset = this->add_constant(0);
7077 this->static_relocs_.push_back(Static_reloc(got_offset,
7078 elfcpp::R_ARM_TLS_DTPOFF32,
7079 gsym));
7082 // Same as the above but for a local symbol.
7084 template<bool big_endian>
7085 void
7086 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
7087 unsigned int got_type,
7088 Sized_relobj<32, big_endian>* object,
7089 unsigned int index)
7091 if (object->local_has_got_offset(index, got_type))
7092 return;
7094 // We are doing a static link. Just mark it as belong to module 1,
7095 // the executable.
7096 unsigned int got_offset = this->add_constant(1);
7097 object->set_local_got_offset(index, got_type, got_offset);
7098 got_offset = this->add_constant(0);
7099 this->static_relocs_.push_back(Static_reloc(got_offset,
7100 elfcpp::R_ARM_TLS_DTPOFF32,
7101 object, index));
7104 template<bool big_endian>
7105 void
7106 Arm_output_data_got<big_endian>::do_write(Output_file* of)
7108 // Call parent to write out GOT.
7109 Output_data_got<32, big_endian>::do_write(of);
7111 // We are done if there is no fix up.
7112 if (this->static_relocs_.empty())
7113 return;
7115 gold_assert(parameters->doing_static_link());
7117 const off_t offset = this->offset();
7118 const section_size_type oview_size =
7119 convert_to_section_size_type(this->data_size());
7120 unsigned char* const oview = of->get_output_view(offset, oview_size);
7122 Output_segment* tls_segment = this->layout_->tls_segment();
7123 gold_assert(tls_segment != NULL);
7125 // The thread pointer $tp points to the TCB, which is followed by the
7126 // TLS. So we need to adjust $tp relative addressing by this amount.
7127 Arm_address aligned_tcb_size =
7128 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
7130 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
7132 Static_reloc& reloc(this->static_relocs_[i]);
7134 Arm_address value;
7135 if (!reloc.symbol_is_global())
7137 Sized_relobj<32, big_endian>* object = reloc.relobj();
7138 const Symbol_value<32>* psymval =
7139 reloc.relobj()->local_symbol(reloc.index());
7141 // We are doing static linking. Issue an error and skip this
7142 // relocation if the symbol is undefined or in a discarded_section.
7143 bool is_ordinary;
7144 unsigned int shndx = psymval->input_shndx(&is_ordinary);
7145 if ((shndx == elfcpp::SHN_UNDEF)
7146 || (is_ordinary
7147 && shndx != elfcpp::SHN_UNDEF
7148 && !object->is_section_included(shndx)
7149 && !this->symbol_table_->is_section_folded(object, shndx)))
7151 gold_error(_("undefined or discarded local symbol %u from "
7152 " object %s in GOT"),
7153 reloc.index(), reloc.relobj()->name().c_str());
7154 continue;
7157 value = psymval->value(object, 0);
7159 else
7161 const Symbol* gsym = reloc.symbol();
7162 gold_assert(gsym != NULL);
7163 if (gsym->is_forwarder())
7164 gsym = this->symbol_table_->resolve_forwards(gsym);
7166 // We are doing static linking. Issue an error and skip this
7167 // relocation if the symbol is undefined or in a discarded_section
7168 // unless it is a weakly_undefined symbol.
7169 if ((gsym->is_defined_in_discarded_section()
7170 || gsym->is_undefined())
7171 && !gsym->is_weak_undefined())
7173 gold_error(_("undefined or discarded symbol %s in GOT"),
7174 gsym->name());
7175 continue;
7178 if (!gsym->is_weak_undefined())
7180 const Sized_symbol<32>* sym =
7181 static_cast<const Sized_symbol<32>*>(gsym);
7182 value = sym->value();
7184 else
7185 value = 0;
7188 unsigned got_offset = reloc.got_offset();
7189 gold_assert(got_offset < oview_size);
7191 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
7192 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
7193 Valtype x;
7194 switch (reloc.r_type())
7196 case elfcpp::R_ARM_TLS_DTPOFF32:
7197 x = value;
7198 break;
7199 case elfcpp::R_ARM_TLS_TPOFF32:
7200 x = value + aligned_tcb_size;
7201 break;
7202 default:
7203 gold_unreachable();
7205 elfcpp::Swap<32, big_endian>::writeval(wv, x);
7208 of->write_output_view(offset, oview_size, oview);
7211 // A class to handle the PLT data.
7213 template<bool big_endian>
7214 class Output_data_plt_arm : public Output_section_data
7216 public:
7217 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
7218 Reloc_section;
7220 Output_data_plt_arm(Layout*, Output_data_space*);
7222 // Add an entry to the PLT.
7223 void
7224 add_entry(Symbol* gsym);
7226 // Return the .rel.plt section data.
7227 const Reloc_section*
7228 rel_plt() const
7229 { return this->rel_; }
7231 // Return the number of PLT entries.
7232 unsigned int
7233 entry_count() const
7234 { return this->count_; }
7236 // Return the offset of the first non-reserved PLT entry.
7237 static unsigned int
7238 first_plt_entry_offset()
7239 { return sizeof(first_plt_entry); }
7241 // Return the size of a PLT entry.
7242 static unsigned int
7243 get_plt_entry_size()
7244 { return sizeof(plt_entry); }
7246 protected:
7247 void
7248 do_adjust_output_section(Output_section* os);
7250 // Write to a map file.
7251 void
7252 do_print_to_mapfile(Mapfile* mapfile) const
7253 { mapfile->print_output_data(this, _("** PLT")); }
7255 private:
7256 // Template for the first PLT entry.
7257 static const uint32_t first_plt_entry[5];
7259 // Template for subsequent PLT entries.
7260 static const uint32_t plt_entry[3];
7262 // Set the final size.
7263 void
7264 set_final_data_size()
7266 this->set_data_size(sizeof(first_plt_entry)
7267 + this->count_ * sizeof(plt_entry));
7270 // Write out the PLT data.
7271 void
7272 do_write(Output_file*);
7274 // The reloc section.
7275 Reloc_section* rel_;
7276 // The .got.plt section.
7277 Output_data_space* got_plt_;
7278 // The number of PLT entries.
7279 unsigned int count_;
7282 // Create the PLT section. The ordinary .got section is an argument,
7283 // since we need to refer to the start. We also create our own .got
7284 // section just for PLT entries.
7286 template<bool big_endian>
7287 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
7288 Output_data_space* got_plt)
7289 : Output_section_data(4), got_plt_(got_plt), count_(0)
7291 this->rel_ = new Reloc_section(false);
7292 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
7293 elfcpp::SHF_ALLOC, this->rel_,
7294 ORDER_DYNAMIC_PLT_RELOCS, false);
7297 template<bool big_endian>
7298 void
7299 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
7301 os->set_entsize(0);
7304 // Add an entry to the PLT.
7306 template<bool big_endian>
7307 void
7308 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
7310 gold_assert(!gsym->has_plt_offset());
7312 // Note that when setting the PLT offset we skip the initial
7313 // reserved PLT entry.
7314 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
7315 + sizeof(first_plt_entry));
7317 ++this->count_;
7319 section_offset_type got_offset = this->got_plt_->current_data_size();
7321 // Every PLT entry needs a GOT entry which points back to the PLT
7322 // entry (this will be changed by the dynamic linker, normally
7323 // lazily when the function is called).
7324 this->got_plt_->set_current_data_size(got_offset + 4);
7326 // Every PLT entry needs a reloc.
7327 gsym->set_needs_dynsym_entry();
7328 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
7329 got_offset);
7331 // Note that we don't need to save the symbol. The contents of the
7332 // PLT are independent of which symbols are used. The symbols only
7333 // appear in the relocations.
7336 // ARM PLTs.
7337 // FIXME: This is not very flexible. Right now this has only been tested
7338 // on armv5te. If we are to support additional architecture features like
7339 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
7341 // The first entry in the PLT.
7342 template<bool big_endian>
7343 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
7345 0xe52de004, // str lr, [sp, #-4]!
7346 0xe59fe004, // ldr lr, [pc, #4]
7347 0xe08fe00e, // add lr, pc, lr
7348 0xe5bef008, // ldr pc, [lr, #8]!
7349 0x00000000, // &GOT[0] - .
7352 // Subsequent entries in the PLT.
7354 template<bool big_endian>
7355 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
7357 0xe28fc600, // add ip, pc, #0xNN00000
7358 0xe28cca00, // add ip, ip, #0xNN000
7359 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
7362 // Write out the PLT. This uses the hand-coded instructions above,
7363 // and adjusts them as needed. This is all specified by the arm ELF
7364 // Processor Supplement.
7366 template<bool big_endian>
7367 void
7368 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
7370 const off_t offset = this->offset();
7371 const section_size_type oview_size =
7372 convert_to_section_size_type(this->data_size());
7373 unsigned char* const oview = of->get_output_view(offset, oview_size);
7375 const off_t got_file_offset = this->got_plt_->offset();
7376 const section_size_type got_size =
7377 convert_to_section_size_type(this->got_plt_->data_size());
7378 unsigned char* const got_view = of->get_output_view(got_file_offset,
7379 got_size);
7380 unsigned char* pov = oview;
7382 Arm_address plt_address = this->address();
7383 Arm_address got_address = this->got_plt_->address();
7385 // Write first PLT entry. All but the last word are constants.
7386 const size_t num_first_plt_words = (sizeof(first_plt_entry)
7387 / sizeof(plt_entry[0]));
7388 for (size_t i = 0; i < num_first_plt_words - 1; i++)
7389 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
7390 // Last word in first PLT entry is &GOT[0] - .
7391 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
7392 got_address - (plt_address + 16));
7393 pov += sizeof(first_plt_entry);
7395 unsigned char* got_pov = got_view;
7397 memset(got_pov, 0, 12);
7398 got_pov += 12;
7400 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
7401 unsigned int plt_offset = sizeof(first_plt_entry);
7402 unsigned int plt_rel_offset = 0;
7403 unsigned int got_offset = 12;
7404 const unsigned int count = this->count_;
7405 for (unsigned int i = 0;
7406 i < count;
7407 ++i,
7408 pov += sizeof(plt_entry),
7409 got_pov += 4,
7410 plt_offset += sizeof(plt_entry),
7411 plt_rel_offset += rel_size,
7412 got_offset += 4)
7414 // Set and adjust the PLT entry itself.
7415 int32_t offset = ((got_address + got_offset)
7416 - (plt_address + plt_offset + 8));
7418 gold_assert(offset >= 0 && offset < 0x0fffffff);
7419 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
7420 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
7421 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
7422 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
7423 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
7424 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
7426 // Set the entry in the GOT.
7427 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
7430 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
7431 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
7433 of->write_output_view(offset, oview_size, oview);
7434 of->write_output_view(got_file_offset, got_size, got_view);
7437 // Create a PLT entry for a global symbol.
7439 template<bool big_endian>
7440 void
7441 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
7442 Symbol* gsym)
7444 if (gsym->has_plt_offset())
7445 return;
7447 if (this->plt_ == NULL)
7449 // Create the GOT sections first.
7450 this->got_section(symtab, layout);
7452 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
7453 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
7454 (elfcpp::SHF_ALLOC
7455 | elfcpp::SHF_EXECINSTR),
7456 this->plt_, ORDER_PLT, false);
7458 this->plt_->add_entry(gsym);
7461 // Return the number of entries in the PLT.
7463 template<bool big_endian>
7464 unsigned int
7465 Target_arm<big_endian>::plt_entry_count() const
7467 if (this->plt_ == NULL)
7468 return 0;
7469 return this->plt_->entry_count();
7472 // Return the offset of the first non-reserved PLT entry.
7474 template<bool big_endian>
7475 unsigned int
7476 Target_arm<big_endian>::first_plt_entry_offset() const
7478 return Output_data_plt_arm<big_endian>::first_plt_entry_offset();
7481 // Return the size of each PLT entry.
7483 template<bool big_endian>
7484 unsigned int
7485 Target_arm<big_endian>::plt_entry_size() const
7487 return Output_data_plt_arm<big_endian>::get_plt_entry_size();
7490 // Get the section to use for TLS_DESC relocations.
7492 template<bool big_endian>
7493 typename Target_arm<big_endian>::Reloc_section*
7494 Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
7496 return this->plt_section()->rel_tls_desc(layout);
7499 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
7501 template<bool big_endian>
7502 void
7503 Target_arm<big_endian>::define_tls_base_symbol(
7504 Symbol_table* symtab,
7505 Layout* layout)
7507 if (this->tls_base_symbol_defined_)
7508 return;
7510 Output_segment* tls_segment = layout->tls_segment();
7511 if (tls_segment != NULL)
7513 bool is_exec = parameters->options().output_is_executable();
7514 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
7515 Symbol_table::PREDEFINED,
7516 tls_segment, 0, 0,
7517 elfcpp::STT_TLS,
7518 elfcpp::STB_LOCAL,
7519 elfcpp::STV_HIDDEN, 0,
7520 (is_exec
7521 ? Symbol::SEGMENT_END
7522 : Symbol::SEGMENT_START),
7523 true);
7525 this->tls_base_symbol_defined_ = true;
7528 // Create a GOT entry for the TLS module index.
7530 template<bool big_endian>
7531 unsigned int
7532 Target_arm<big_endian>::got_mod_index_entry(
7533 Symbol_table* symtab,
7534 Layout* layout,
7535 Sized_relobj<32, big_endian>* object)
7537 if (this->got_mod_index_offset_ == -1U)
7539 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7540 Arm_output_data_got<big_endian>* got = this->got_section(symtab, layout);
7541 unsigned int got_offset;
7542 if (!parameters->doing_static_link())
7544 got_offset = got->add_constant(0);
7545 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
7546 rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
7547 got_offset);
7549 else
7551 // We are doing a static link. Just mark it as belong to module 1,
7552 // the executable.
7553 got_offset = got->add_constant(1);
7556 got->add_constant(0);
7557 this->got_mod_index_offset_ = got_offset;
7559 return this->got_mod_index_offset_;
7562 // Optimize the TLS relocation type based on what we know about the
7563 // symbol. IS_FINAL is true if the final address of this symbol is
7564 // known at link time.
7566 template<bool big_endian>
7567 tls::Tls_optimization
7568 Target_arm<big_endian>::optimize_tls_reloc(bool, int)
7570 // FIXME: Currently we do not do any TLS optimization.
7571 return tls::TLSOPT_NONE;
7574 // Get the Reference_flags for a particular relocation.
7576 template<bool big_endian>
7578 Target_arm<big_endian>::Scan::get_reference_flags(unsigned int r_type)
7580 switch (r_type)
7582 case elfcpp::R_ARM_NONE:
7583 case elfcpp::R_ARM_V4BX:
7584 case elfcpp::R_ARM_GNU_VTENTRY:
7585 case elfcpp::R_ARM_GNU_VTINHERIT:
7586 // No symbol reference.
7587 return 0;
7589 case elfcpp::R_ARM_ABS32:
7590 case elfcpp::R_ARM_ABS16:
7591 case elfcpp::R_ARM_ABS12:
7592 case elfcpp::R_ARM_THM_ABS5:
7593 case elfcpp::R_ARM_ABS8:
7594 case elfcpp::R_ARM_BASE_ABS:
7595 case elfcpp::R_ARM_MOVW_ABS_NC:
7596 case elfcpp::R_ARM_MOVT_ABS:
7597 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7598 case elfcpp::R_ARM_THM_MOVT_ABS:
7599 case elfcpp::R_ARM_ABS32_NOI:
7600 return Symbol::ABSOLUTE_REF;
7602 case elfcpp::R_ARM_REL32:
7603 case elfcpp::R_ARM_LDR_PC_G0:
7604 case elfcpp::R_ARM_SBREL32:
7605 case elfcpp::R_ARM_THM_PC8:
7606 case elfcpp::R_ARM_BASE_PREL:
7607 case elfcpp::R_ARM_MOVW_PREL_NC:
7608 case elfcpp::R_ARM_MOVT_PREL:
7609 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7610 case elfcpp::R_ARM_THM_MOVT_PREL:
7611 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7612 case elfcpp::R_ARM_THM_PC12:
7613 case elfcpp::R_ARM_REL32_NOI:
7614 case elfcpp::R_ARM_ALU_PC_G0_NC:
7615 case elfcpp::R_ARM_ALU_PC_G0:
7616 case elfcpp::R_ARM_ALU_PC_G1_NC:
7617 case elfcpp::R_ARM_ALU_PC_G1:
7618 case elfcpp::R_ARM_ALU_PC_G2:
7619 case elfcpp::R_ARM_LDR_PC_G1:
7620 case elfcpp::R_ARM_LDR_PC_G2:
7621 case elfcpp::R_ARM_LDRS_PC_G0:
7622 case elfcpp::R_ARM_LDRS_PC_G1:
7623 case elfcpp::R_ARM_LDRS_PC_G2:
7624 case elfcpp::R_ARM_LDC_PC_G0:
7625 case elfcpp::R_ARM_LDC_PC_G1:
7626 case elfcpp::R_ARM_LDC_PC_G2:
7627 case elfcpp::R_ARM_ALU_SB_G0_NC:
7628 case elfcpp::R_ARM_ALU_SB_G0:
7629 case elfcpp::R_ARM_ALU_SB_G1_NC:
7630 case elfcpp::R_ARM_ALU_SB_G1:
7631 case elfcpp::R_ARM_ALU_SB_G2:
7632 case elfcpp::R_ARM_LDR_SB_G0:
7633 case elfcpp::R_ARM_LDR_SB_G1:
7634 case elfcpp::R_ARM_LDR_SB_G2:
7635 case elfcpp::R_ARM_LDRS_SB_G0:
7636 case elfcpp::R_ARM_LDRS_SB_G1:
7637 case elfcpp::R_ARM_LDRS_SB_G2:
7638 case elfcpp::R_ARM_LDC_SB_G0:
7639 case elfcpp::R_ARM_LDC_SB_G1:
7640 case elfcpp::R_ARM_LDC_SB_G2:
7641 case elfcpp::R_ARM_MOVW_BREL_NC:
7642 case elfcpp::R_ARM_MOVT_BREL:
7643 case elfcpp::R_ARM_MOVW_BREL:
7644 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7645 case elfcpp::R_ARM_THM_MOVT_BREL:
7646 case elfcpp::R_ARM_THM_MOVW_BREL:
7647 case elfcpp::R_ARM_GOTOFF32:
7648 case elfcpp::R_ARM_GOTOFF12:
7649 case elfcpp::R_ARM_SBREL31:
7650 return Symbol::RELATIVE_REF;
7652 case elfcpp::R_ARM_PLT32:
7653 case elfcpp::R_ARM_CALL:
7654 case elfcpp::R_ARM_JUMP24:
7655 case elfcpp::R_ARM_THM_CALL:
7656 case elfcpp::R_ARM_THM_JUMP24:
7657 case elfcpp::R_ARM_THM_JUMP19:
7658 case elfcpp::R_ARM_THM_JUMP6:
7659 case elfcpp::R_ARM_THM_JUMP11:
7660 case elfcpp::R_ARM_THM_JUMP8:
7661 // R_ARM_PREL31 is not used to relocate call/jump instructions but
7662 // in unwind tables. It may point to functions via PLTs.
7663 // So we treat it like call/jump relocations above.
7664 case elfcpp::R_ARM_PREL31:
7665 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7667 case elfcpp::R_ARM_GOT_BREL:
7668 case elfcpp::R_ARM_GOT_ABS:
7669 case elfcpp::R_ARM_GOT_PREL:
7670 // Absolute in GOT.
7671 return Symbol::ABSOLUTE_REF;
7673 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7674 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7675 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7676 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7677 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7678 return Symbol::TLS_REF;
7680 case elfcpp::R_ARM_TARGET1:
7681 case elfcpp::R_ARM_TARGET2:
7682 case elfcpp::R_ARM_COPY:
7683 case elfcpp::R_ARM_GLOB_DAT:
7684 case elfcpp::R_ARM_JUMP_SLOT:
7685 case elfcpp::R_ARM_RELATIVE:
7686 case elfcpp::R_ARM_PC24:
7687 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7688 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7689 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7690 default:
7691 // Not expected. We will give an error later.
7692 return 0;
7696 // Report an unsupported relocation against a local symbol.
7698 template<bool big_endian>
7699 void
7700 Target_arm<big_endian>::Scan::unsupported_reloc_local(
7701 Sized_relobj<32, big_endian>* object,
7702 unsigned int r_type)
7704 gold_error(_("%s: unsupported reloc %u against local symbol"),
7705 object->name().c_str(), r_type);
7708 // We are about to emit a dynamic relocation of type R_TYPE. If the
7709 // dynamic linker does not support it, issue an error. The GNU linker
7710 // only issues a non-PIC error for an allocated read-only section.
7711 // Here we know the section is allocated, but we don't know that it is
7712 // read-only. But we check for all the relocation types which the
7713 // glibc dynamic linker supports, so it seems appropriate to issue an
7714 // error even if the section is not read-only.
7716 template<bool big_endian>
7717 void
7718 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
7719 unsigned int r_type)
7721 switch (r_type)
7723 // These are the relocation types supported by glibc for ARM.
7724 case elfcpp::R_ARM_RELATIVE:
7725 case elfcpp::R_ARM_COPY:
7726 case elfcpp::R_ARM_GLOB_DAT:
7727 case elfcpp::R_ARM_JUMP_SLOT:
7728 case elfcpp::R_ARM_ABS32:
7729 case elfcpp::R_ARM_ABS32_NOI:
7730 case elfcpp::R_ARM_PC24:
7731 // FIXME: The following 3 types are not supported by Android's dynamic
7732 // linker.
7733 case elfcpp::R_ARM_TLS_DTPMOD32:
7734 case elfcpp::R_ARM_TLS_DTPOFF32:
7735 case elfcpp::R_ARM_TLS_TPOFF32:
7736 return;
7738 default:
7740 // This prevents us from issuing more than one error per reloc
7741 // section. But we can still wind up issuing more than one
7742 // error per object file.
7743 if (this->issued_non_pic_error_)
7744 return;
7745 const Arm_reloc_property* reloc_property =
7746 arm_reloc_property_table->get_reloc_property(r_type);
7747 gold_assert(reloc_property != NULL);
7748 object->error(_("requires unsupported dynamic reloc %s; "
7749 "recompile with -fPIC"),
7750 reloc_property->name().c_str());
7751 this->issued_non_pic_error_ = true;
7752 return;
7755 case elfcpp::R_ARM_NONE:
7756 gold_unreachable();
7760 // Scan a relocation for a local symbol.
7761 // FIXME: This only handles a subset of relocation types used by Android
7762 // on ARM v5te devices.
7764 template<bool big_endian>
7765 inline void
7766 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
7767 Layout* layout,
7768 Target_arm* target,
7769 Sized_relobj<32, big_endian>* object,
7770 unsigned int data_shndx,
7771 Output_section* output_section,
7772 const elfcpp::Rel<32, big_endian>& reloc,
7773 unsigned int r_type,
7774 const elfcpp::Sym<32, big_endian>& lsym)
7776 r_type = get_real_reloc_type(r_type);
7777 switch (r_type)
7779 case elfcpp::R_ARM_NONE:
7780 case elfcpp::R_ARM_V4BX:
7781 case elfcpp::R_ARM_GNU_VTENTRY:
7782 case elfcpp::R_ARM_GNU_VTINHERIT:
7783 break;
7785 case elfcpp::R_ARM_ABS32:
7786 case elfcpp::R_ARM_ABS32_NOI:
7787 // If building a shared library (or a position-independent
7788 // executable), we need to create a dynamic relocation for
7789 // this location. The relocation applied at link time will
7790 // apply the link-time value, so we flag the location with
7791 // an R_ARM_RELATIVE relocation so the dynamic loader can
7792 // relocate it easily.
7793 if (parameters->options().output_is_position_independent())
7795 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7796 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7797 // If we are to add more other reloc types than R_ARM_ABS32,
7798 // we need to add check_non_pic(object, r_type) here.
7799 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
7800 output_section, data_shndx,
7801 reloc.get_r_offset());
7803 break;
7805 case elfcpp::R_ARM_ABS16:
7806 case elfcpp::R_ARM_ABS12:
7807 case elfcpp::R_ARM_THM_ABS5:
7808 case elfcpp::R_ARM_ABS8:
7809 case elfcpp::R_ARM_BASE_ABS:
7810 case elfcpp::R_ARM_MOVW_ABS_NC:
7811 case elfcpp::R_ARM_MOVT_ABS:
7812 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7813 case elfcpp::R_ARM_THM_MOVT_ABS:
7814 // If building a shared library (or a position-independent
7815 // executable), we need to create a dynamic relocation for
7816 // this location. Because the addend needs to remain in the
7817 // data section, we need to be careful not to apply this
7818 // relocation statically.
7819 if (parameters->options().output_is_position_independent())
7821 check_non_pic(object, r_type);
7822 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7823 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7824 if (lsym.get_st_type() != elfcpp::STT_SECTION)
7825 rel_dyn->add_local(object, r_sym, r_type, output_section,
7826 data_shndx, reloc.get_r_offset());
7827 else
7829 gold_assert(lsym.get_st_value() == 0);
7830 unsigned int shndx = lsym.get_st_shndx();
7831 bool is_ordinary;
7832 shndx = object->adjust_sym_shndx(r_sym, shndx,
7833 &is_ordinary);
7834 if (!is_ordinary)
7835 object->error(_("section symbol %u has bad shndx %u"),
7836 r_sym, shndx);
7837 else
7838 rel_dyn->add_local_section(object, shndx,
7839 r_type, output_section,
7840 data_shndx, reloc.get_r_offset());
7843 break;
7845 case elfcpp::R_ARM_REL32:
7846 case elfcpp::R_ARM_LDR_PC_G0:
7847 case elfcpp::R_ARM_SBREL32:
7848 case elfcpp::R_ARM_THM_CALL:
7849 case elfcpp::R_ARM_THM_PC8:
7850 case elfcpp::R_ARM_BASE_PREL:
7851 case elfcpp::R_ARM_PLT32:
7852 case elfcpp::R_ARM_CALL:
7853 case elfcpp::R_ARM_JUMP24:
7854 case elfcpp::R_ARM_THM_JUMP24:
7855 case elfcpp::R_ARM_SBREL31:
7856 case elfcpp::R_ARM_PREL31:
7857 case elfcpp::R_ARM_MOVW_PREL_NC:
7858 case elfcpp::R_ARM_MOVT_PREL:
7859 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7860 case elfcpp::R_ARM_THM_MOVT_PREL:
7861 case elfcpp::R_ARM_THM_JUMP19:
7862 case elfcpp::R_ARM_THM_JUMP6:
7863 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7864 case elfcpp::R_ARM_THM_PC12:
7865 case elfcpp::R_ARM_REL32_NOI:
7866 case elfcpp::R_ARM_ALU_PC_G0_NC:
7867 case elfcpp::R_ARM_ALU_PC_G0:
7868 case elfcpp::R_ARM_ALU_PC_G1_NC:
7869 case elfcpp::R_ARM_ALU_PC_G1:
7870 case elfcpp::R_ARM_ALU_PC_G2:
7871 case elfcpp::R_ARM_LDR_PC_G1:
7872 case elfcpp::R_ARM_LDR_PC_G2:
7873 case elfcpp::R_ARM_LDRS_PC_G0:
7874 case elfcpp::R_ARM_LDRS_PC_G1:
7875 case elfcpp::R_ARM_LDRS_PC_G2:
7876 case elfcpp::R_ARM_LDC_PC_G0:
7877 case elfcpp::R_ARM_LDC_PC_G1:
7878 case elfcpp::R_ARM_LDC_PC_G2:
7879 case elfcpp::R_ARM_ALU_SB_G0_NC:
7880 case elfcpp::R_ARM_ALU_SB_G0:
7881 case elfcpp::R_ARM_ALU_SB_G1_NC:
7882 case elfcpp::R_ARM_ALU_SB_G1:
7883 case elfcpp::R_ARM_ALU_SB_G2:
7884 case elfcpp::R_ARM_LDR_SB_G0:
7885 case elfcpp::R_ARM_LDR_SB_G1:
7886 case elfcpp::R_ARM_LDR_SB_G2:
7887 case elfcpp::R_ARM_LDRS_SB_G0:
7888 case elfcpp::R_ARM_LDRS_SB_G1:
7889 case elfcpp::R_ARM_LDRS_SB_G2:
7890 case elfcpp::R_ARM_LDC_SB_G0:
7891 case elfcpp::R_ARM_LDC_SB_G1:
7892 case elfcpp::R_ARM_LDC_SB_G2:
7893 case elfcpp::R_ARM_MOVW_BREL_NC:
7894 case elfcpp::R_ARM_MOVT_BREL:
7895 case elfcpp::R_ARM_MOVW_BREL:
7896 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7897 case elfcpp::R_ARM_THM_MOVT_BREL:
7898 case elfcpp::R_ARM_THM_MOVW_BREL:
7899 case elfcpp::R_ARM_THM_JUMP11:
7900 case elfcpp::R_ARM_THM_JUMP8:
7901 // We don't need to do anything for a relative addressing relocation
7902 // against a local symbol if it does not reference the GOT.
7903 break;
7905 case elfcpp::R_ARM_GOTOFF32:
7906 case elfcpp::R_ARM_GOTOFF12:
7907 // We need a GOT section:
7908 target->got_section(symtab, layout);
7909 break;
7911 case elfcpp::R_ARM_GOT_BREL:
7912 case elfcpp::R_ARM_GOT_PREL:
7914 // The symbol requires a GOT entry.
7915 Arm_output_data_got<big_endian>* got =
7916 target->got_section(symtab, layout);
7917 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7918 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
7920 // If we are generating a shared object, we need to add a
7921 // dynamic RELATIVE relocation for this symbol's GOT entry.
7922 if (parameters->options().output_is_position_independent())
7924 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7925 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7926 rel_dyn->add_local_relative(
7927 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
7928 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
7932 break;
7934 case elfcpp::R_ARM_TARGET1:
7935 case elfcpp::R_ARM_TARGET2:
7936 // This should have been mapped to another type already.
7937 // Fall through.
7938 case elfcpp::R_ARM_COPY:
7939 case elfcpp::R_ARM_GLOB_DAT:
7940 case elfcpp::R_ARM_JUMP_SLOT:
7941 case elfcpp::R_ARM_RELATIVE:
7942 // These are relocations which should only be seen by the
7943 // dynamic linker, and should never be seen here.
7944 gold_error(_("%s: unexpected reloc %u in object file"),
7945 object->name().c_str(), r_type);
7946 break;
7949 // These are initial TLS relocs, which are expected when
7950 // linking.
7951 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7952 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7953 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7954 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7955 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7957 bool output_is_shared = parameters->options().shared();
7958 const tls::Tls_optimization optimized_type
7959 = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
7960 r_type);
7961 switch (r_type)
7963 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7964 if (optimized_type == tls::TLSOPT_NONE)
7966 // Create a pair of GOT entries for the module index and
7967 // dtv-relative offset.
7968 Arm_output_data_got<big_endian>* got
7969 = target->got_section(symtab, layout);
7970 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7971 unsigned int shndx = lsym.get_st_shndx();
7972 bool is_ordinary;
7973 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7974 if (!is_ordinary)
7976 object->error(_("local symbol %u has bad shndx %u"),
7977 r_sym, shndx);
7978 break;
7981 if (!parameters->doing_static_link())
7982 got->add_local_pair_with_rel(object, r_sym, shndx,
7983 GOT_TYPE_TLS_PAIR,
7984 target->rel_dyn_section(layout),
7985 elfcpp::R_ARM_TLS_DTPMOD32, 0);
7986 else
7987 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR,
7988 object, r_sym);
7990 else
7991 // FIXME: TLS optimization not supported yet.
7992 gold_unreachable();
7993 break;
7995 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7996 if (optimized_type == tls::TLSOPT_NONE)
7998 // Create a GOT entry for the module index.
7999 target->got_mod_index_entry(symtab, layout, object);
8001 else
8002 // FIXME: TLS optimization not supported yet.
8003 gold_unreachable();
8004 break;
8006 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8007 break;
8009 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8010 layout->set_has_static_tls();
8011 if (optimized_type == tls::TLSOPT_NONE)
8013 // Create a GOT entry for the tp-relative offset.
8014 Arm_output_data_got<big_endian>* got
8015 = target->got_section(symtab, layout);
8016 unsigned int r_sym =
8017 elfcpp::elf_r_sym<32>(reloc.get_r_info());
8018 if (!parameters->doing_static_link())
8019 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
8020 target->rel_dyn_section(layout),
8021 elfcpp::R_ARM_TLS_TPOFF32);
8022 else if (!object->local_has_got_offset(r_sym,
8023 GOT_TYPE_TLS_OFFSET))
8025 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
8026 unsigned int got_offset =
8027 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
8028 got->add_static_reloc(got_offset,
8029 elfcpp::R_ARM_TLS_TPOFF32, object,
8030 r_sym);
8033 else
8034 // FIXME: TLS optimization not supported yet.
8035 gold_unreachable();
8036 break;
8038 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8039 layout->set_has_static_tls();
8040 if (output_is_shared)
8042 // We need to create a dynamic relocation.
8043 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
8044 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
8045 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8046 rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
8047 output_section, data_shndx,
8048 reloc.get_r_offset());
8050 break;
8052 default:
8053 gold_unreachable();
8056 break;
8058 case elfcpp::R_ARM_PC24:
8059 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
8060 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
8061 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
8062 default:
8063 unsupported_reloc_local(object, r_type);
8064 break;
8068 // Report an unsupported relocation against a global symbol.
8070 template<bool big_endian>
8071 void
8072 Target_arm<big_endian>::Scan::unsupported_reloc_global(
8073 Sized_relobj<32, big_endian>* object,
8074 unsigned int r_type,
8075 Symbol* gsym)
8077 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8078 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8081 template<bool big_endian>
8082 inline bool
8083 Target_arm<big_endian>::Scan::possible_function_pointer_reloc(
8084 unsigned int r_type)
8086 switch (r_type)
8088 case elfcpp::R_ARM_PC24:
8089 case elfcpp::R_ARM_THM_CALL:
8090 case elfcpp::R_ARM_PLT32:
8091 case elfcpp::R_ARM_CALL:
8092 case elfcpp::R_ARM_JUMP24:
8093 case elfcpp::R_ARM_THM_JUMP24:
8094 case elfcpp::R_ARM_SBREL31:
8095 case elfcpp::R_ARM_PREL31:
8096 case elfcpp::R_ARM_THM_JUMP19:
8097 case elfcpp::R_ARM_THM_JUMP6:
8098 case elfcpp::R_ARM_THM_JUMP11:
8099 case elfcpp::R_ARM_THM_JUMP8:
8100 // All the relocations above are branches except SBREL31 and PREL31.
8101 return false;
8103 default:
8104 // Be conservative and assume this is a function pointer.
8105 return true;
8109 template<bool big_endian>
8110 inline bool
8111 Target_arm<big_endian>::Scan::local_reloc_may_be_function_pointer(
8112 Symbol_table*,
8113 Layout*,
8114 Target_arm<big_endian>* target,
8115 Sized_relobj<32, big_endian>*,
8116 unsigned int,
8117 Output_section*,
8118 const elfcpp::Rel<32, big_endian>&,
8119 unsigned int r_type,
8120 const elfcpp::Sym<32, big_endian>&)
8122 r_type = target->get_real_reloc_type(r_type);
8123 return possible_function_pointer_reloc(r_type);
8126 template<bool big_endian>
8127 inline bool
8128 Target_arm<big_endian>::Scan::global_reloc_may_be_function_pointer(
8129 Symbol_table*,
8130 Layout*,
8131 Target_arm<big_endian>* target,
8132 Sized_relobj<32, big_endian>*,
8133 unsigned int,
8134 Output_section*,
8135 const elfcpp::Rel<32, big_endian>&,
8136 unsigned int r_type,
8137 Symbol* gsym)
8139 // GOT is not a function.
8140 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8141 return false;
8143 r_type = target->get_real_reloc_type(r_type);
8144 return possible_function_pointer_reloc(r_type);
8147 // Scan a relocation for a global symbol.
8149 template<bool big_endian>
8150 inline void
8151 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
8152 Layout* layout,
8153 Target_arm* target,
8154 Sized_relobj<32, big_endian>* object,
8155 unsigned int data_shndx,
8156 Output_section* output_section,
8157 const elfcpp::Rel<32, big_endian>& reloc,
8158 unsigned int r_type,
8159 Symbol* gsym)
8161 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
8162 // section. We check here to avoid creating a dynamic reloc against
8163 // _GLOBAL_OFFSET_TABLE_.
8164 if (!target->has_got_section()
8165 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8166 target->got_section(symtab, layout);
8168 r_type = get_real_reloc_type(r_type);
8169 switch (r_type)
8171 case elfcpp::R_ARM_NONE:
8172 case elfcpp::R_ARM_V4BX:
8173 case elfcpp::R_ARM_GNU_VTENTRY:
8174 case elfcpp::R_ARM_GNU_VTINHERIT:
8175 break;
8177 case elfcpp::R_ARM_ABS32:
8178 case elfcpp::R_ARM_ABS16:
8179 case elfcpp::R_ARM_ABS12:
8180 case elfcpp::R_ARM_THM_ABS5:
8181 case elfcpp::R_ARM_ABS8:
8182 case elfcpp::R_ARM_BASE_ABS:
8183 case elfcpp::R_ARM_MOVW_ABS_NC:
8184 case elfcpp::R_ARM_MOVT_ABS:
8185 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8186 case elfcpp::R_ARM_THM_MOVT_ABS:
8187 case elfcpp::R_ARM_ABS32_NOI:
8188 // Absolute addressing relocations.
8190 // Make a PLT entry if necessary.
8191 if (this->symbol_needs_plt_entry(gsym))
8193 target->make_plt_entry(symtab, layout, gsym);
8194 // Since this is not a PC-relative relocation, we may be
8195 // taking the address of a function. In that case we need to
8196 // set the entry in the dynamic symbol table to the address of
8197 // the PLT entry.
8198 if (gsym->is_from_dynobj() && !parameters->options().shared())
8199 gsym->set_needs_dynsym_value();
8201 // Make a dynamic relocation if necessary.
8202 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
8204 if (gsym->may_need_copy_reloc())
8206 target->copy_reloc(symtab, layout, object,
8207 data_shndx, output_section, gsym, reloc);
8209 else if ((r_type == elfcpp::R_ARM_ABS32
8210 || r_type == elfcpp::R_ARM_ABS32_NOI)
8211 && gsym->can_use_relative_reloc(false))
8213 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8214 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
8215 output_section, object,
8216 data_shndx, reloc.get_r_offset());
8218 else
8220 check_non_pic(object, r_type);
8221 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8222 rel_dyn->add_global(gsym, r_type, output_section, object,
8223 data_shndx, reloc.get_r_offset());
8227 break;
8229 case elfcpp::R_ARM_GOTOFF32:
8230 case elfcpp::R_ARM_GOTOFF12:
8231 // We need a GOT section.
8232 target->got_section(symtab, layout);
8233 break;
8235 case elfcpp::R_ARM_REL32:
8236 case elfcpp::R_ARM_LDR_PC_G0:
8237 case elfcpp::R_ARM_SBREL32:
8238 case elfcpp::R_ARM_THM_PC8:
8239 case elfcpp::R_ARM_BASE_PREL:
8240 case elfcpp::R_ARM_MOVW_PREL_NC:
8241 case elfcpp::R_ARM_MOVT_PREL:
8242 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8243 case elfcpp::R_ARM_THM_MOVT_PREL:
8244 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8245 case elfcpp::R_ARM_THM_PC12:
8246 case elfcpp::R_ARM_REL32_NOI:
8247 case elfcpp::R_ARM_ALU_PC_G0_NC:
8248 case elfcpp::R_ARM_ALU_PC_G0:
8249 case elfcpp::R_ARM_ALU_PC_G1_NC:
8250 case elfcpp::R_ARM_ALU_PC_G1:
8251 case elfcpp::R_ARM_ALU_PC_G2:
8252 case elfcpp::R_ARM_LDR_PC_G1:
8253 case elfcpp::R_ARM_LDR_PC_G2:
8254 case elfcpp::R_ARM_LDRS_PC_G0:
8255 case elfcpp::R_ARM_LDRS_PC_G1:
8256 case elfcpp::R_ARM_LDRS_PC_G2:
8257 case elfcpp::R_ARM_LDC_PC_G0:
8258 case elfcpp::R_ARM_LDC_PC_G1:
8259 case elfcpp::R_ARM_LDC_PC_G2:
8260 case elfcpp::R_ARM_ALU_SB_G0_NC:
8261 case elfcpp::R_ARM_ALU_SB_G0:
8262 case elfcpp::R_ARM_ALU_SB_G1_NC:
8263 case elfcpp::R_ARM_ALU_SB_G1:
8264 case elfcpp::R_ARM_ALU_SB_G2:
8265 case elfcpp::R_ARM_LDR_SB_G0:
8266 case elfcpp::R_ARM_LDR_SB_G1:
8267 case elfcpp::R_ARM_LDR_SB_G2:
8268 case elfcpp::R_ARM_LDRS_SB_G0:
8269 case elfcpp::R_ARM_LDRS_SB_G1:
8270 case elfcpp::R_ARM_LDRS_SB_G2:
8271 case elfcpp::R_ARM_LDC_SB_G0:
8272 case elfcpp::R_ARM_LDC_SB_G1:
8273 case elfcpp::R_ARM_LDC_SB_G2:
8274 case elfcpp::R_ARM_MOVW_BREL_NC:
8275 case elfcpp::R_ARM_MOVT_BREL:
8276 case elfcpp::R_ARM_MOVW_BREL:
8277 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8278 case elfcpp::R_ARM_THM_MOVT_BREL:
8279 case elfcpp::R_ARM_THM_MOVW_BREL:
8280 // Relative addressing relocations.
8282 // Make a dynamic relocation if necessary.
8283 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
8285 if (target->may_need_copy_reloc(gsym))
8287 target->copy_reloc(symtab, layout, object,
8288 data_shndx, output_section, gsym, reloc);
8290 else
8292 check_non_pic(object, r_type);
8293 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8294 rel_dyn->add_global(gsym, r_type, output_section, object,
8295 data_shndx, reloc.get_r_offset());
8299 break;
8301 case elfcpp::R_ARM_THM_CALL:
8302 case elfcpp::R_ARM_PLT32:
8303 case elfcpp::R_ARM_CALL:
8304 case elfcpp::R_ARM_JUMP24:
8305 case elfcpp::R_ARM_THM_JUMP24:
8306 case elfcpp::R_ARM_SBREL31:
8307 case elfcpp::R_ARM_PREL31:
8308 case elfcpp::R_ARM_THM_JUMP19:
8309 case elfcpp::R_ARM_THM_JUMP6:
8310 case elfcpp::R_ARM_THM_JUMP11:
8311 case elfcpp::R_ARM_THM_JUMP8:
8312 // All the relocation above are branches except for the PREL31 ones.
8313 // A PREL31 relocation can point to a personality function in a shared
8314 // library. In that case we want to use a PLT because we want to
8315 // call the personality routine and the dynamic linkers we care about
8316 // do not support dynamic PREL31 relocations. An REL31 relocation may
8317 // point to a function whose unwinding behaviour is being described but
8318 // we will not mistakenly generate a PLT for that because we should use
8319 // a local section symbol.
8321 // If the symbol is fully resolved, this is just a relative
8322 // local reloc. Otherwise we need a PLT entry.
8323 if (gsym->final_value_is_known())
8324 break;
8325 // If building a shared library, we can also skip the PLT entry
8326 // if the symbol is defined in the output file and is protected
8327 // or hidden.
8328 if (gsym->is_defined()
8329 && !gsym->is_from_dynobj()
8330 && !gsym->is_preemptible())
8331 break;
8332 target->make_plt_entry(symtab, layout, gsym);
8333 break;
8335 case elfcpp::R_ARM_GOT_BREL:
8336 case elfcpp::R_ARM_GOT_ABS:
8337 case elfcpp::R_ARM_GOT_PREL:
8339 // The symbol requires a GOT entry.
8340 Arm_output_data_got<big_endian>* got =
8341 target->got_section(symtab, layout);
8342 if (gsym->final_value_is_known())
8343 got->add_global(gsym, GOT_TYPE_STANDARD);
8344 else
8346 // If this symbol is not fully resolved, we need to add a
8347 // GOT entry with a dynamic relocation.
8348 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8349 if (gsym->is_from_dynobj()
8350 || gsym->is_undefined()
8351 || gsym->is_preemptible())
8352 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
8353 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
8354 else
8356 if (got->add_global(gsym, GOT_TYPE_STANDARD))
8357 rel_dyn->add_global_relative(
8358 gsym, elfcpp::R_ARM_RELATIVE, got,
8359 gsym->got_offset(GOT_TYPE_STANDARD));
8363 break;
8365 case elfcpp::R_ARM_TARGET1:
8366 case elfcpp::R_ARM_TARGET2:
8367 // These should have been mapped to other types already.
8368 // Fall through.
8369 case elfcpp::R_ARM_COPY:
8370 case elfcpp::R_ARM_GLOB_DAT:
8371 case elfcpp::R_ARM_JUMP_SLOT:
8372 case elfcpp::R_ARM_RELATIVE:
8373 // These are relocations which should only be seen by the
8374 // dynamic linker, and should never be seen here.
8375 gold_error(_("%s: unexpected reloc %u in object file"),
8376 object->name().c_str(), r_type);
8377 break;
8379 // These are initial tls relocs, which are expected when
8380 // linking.
8381 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8382 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8383 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8384 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8385 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8387 const bool is_final = gsym->final_value_is_known();
8388 const tls::Tls_optimization optimized_type
8389 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
8390 switch (r_type)
8392 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8393 if (optimized_type == tls::TLSOPT_NONE)
8395 // Create a pair of GOT entries for the module index and
8396 // dtv-relative offset.
8397 Arm_output_data_got<big_endian>* got
8398 = target->got_section(symtab, layout);
8399 if (!parameters->doing_static_link())
8400 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
8401 target->rel_dyn_section(layout),
8402 elfcpp::R_ARM_TLS_DTPMOD32,
8403 elfcpp::R_ARM_TLS_DTPOFF32);
8404 else
8405 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
8407 else
8408 // FIXME: TLS optimization not supported yet.
8409 gold_unreachable();
8410 break;
8412 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8413 if (optimized_type == tls::TLSOPT_NONE)
8415 // Create a GOT entry for the module index.
8416 target->got_mod_index_entry(symtab, layout, object);
8418 else
8419 // FIXME: TLS optimization not supported yet.
8420 gold_unreachable();
8421 break;
8423 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8424 break;
8426 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8427 layout->set_has_static_tls();
8428 if (optimized_type == tls::TLSOPT_NONE)
8430 // Create a GOT entry for the tp-relative offset.
8431 Arm_output_data_got<big_endian>* got
8432 = target->got_section(symtab, layout);
8433 if (!parameters->doing_static_link())
8434 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
8435 target->rel_dyn_section(layout),
8436 elfcpp::R_ARM_TLS_TPOFF32);
8437 else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
8439 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
8440 unsigned int got_offset =
8441 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
8442 got->add_static_reloc(got_offset,
8443 elfcpp::R_ARM_TLS_TPOFF32, gsym);
8446 else
8447 // FIXME: TLS optimization not supported yet.
8448 gold_unreachable();
8449 break;
8451 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8452 layout->set_has_static_tls();
8453 if (parameters->options().shared())
8455 // We need to create a dynamic relocation.
8456 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8457 rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
8458 output_section, object,
8459 data_shndx, reloc.get_r_offset());
8461 break;
8463 default:
8464 gold_unreachable();
8467 break;
8469 case elfcpp::R_ARM_PC24:
8470 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
8471 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
8472 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
8473 default:
8474 unsupported_reloc_global(object, r_type, gsym);
8475 break;
8479 // Process relocations for gc.
8481 template<bool big_endian>
8482 void
8483 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
8484 Layout* layout,
8485 Sized_relobj<32, big_endian>* object,
8486 unsigned int data_shndx,
8487 unsigned int,
8488 const unsigned char* prelocs,
8489 size_t reloc_count,
8490 Output_section* output_section,
8491 bool needs_special_offset_handling,
8492 size_t local_symbol_count,
8493 const unsigned char* plocal_symbols)
8495 typedef Target_arm<big_endian> Arm;
8496 typedef typename Target_arm<big_endian>::Scan Scan;
8498 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan,
8499 typename Target_arm::Relocatable_size_for_reloc>(
8500 symtab,
8501 layout,
8502 this,
8503 object,
8504 data_shndx,
8505 prelocs,
8506 reloc_count,
8507 output_section,
8508 needs_special_offset_handling,
8509 local_symbol_count,
8510 plocal_symbols);
8513 // Scan relocations for a section.
8515 template<bool big_endian>
8516 void
8517 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
8518 Layout* layout,
8519 Sized_relobj<32, big_endian>* object,
8520 unsigned int data_shndx,
8521 unsigned int sh_type,
8522 const unsigned char* prelocs,
8523 size_t reloc_count,
8524 Output_section* output_section,
8525 bool needs_special_offset_handling,
8526 size_t local_symbol_count,
8527 const unsigned char* plocal_symbols)
8529 typedef typename Target_arm<big_endian>::Scan Scan;
8530 if (sh_type == elfcpp::SHT_RELA)
8532 gold_error(_("%s: unsupported RELA reloc section"),
8533 object->name().c_str());
8534 return;
8537 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
8538 symtab,
8539 layout,
8540 this,
8541 object,
8542 data_shndx,
8543 prelocs,
8544 reloc_count,
8545 output_section,
8546 needs_special_offset_handling,
8547 local_symbol_count,
8548 plocal_symbols);
8551 // Finalize the sections.
8553 template<bool big_endian>
8554 void
8555 Target_arm<big_endian>::do_finalize_sections(
8556 Layout* layout,
8557 const Input_objects* input_objects,
8558 Symbol_table* symtab)
8560 bool merged_any_attributes = false;
8561 // Merge processor-specific flags.
8562 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8563 p != input_objects->relobj_end();
8564 ++p)
8566 Arm_relobj<big_endian>* arm_relobj =
8567 Arm_relobj<big_endian>::as_arm_relobj(*p);
8568 if (arm_relobj->merge_flags_and_attributes())
8570 this->merge_processor_specific_flags(
8571 arm_relobj->name(),
8572 arm_relobj->processor_specific_flags());
8573 this->merge_object_attributes(arm_relobj->name().c_str(),
8574 arm_relobj->attributes_section_data());
8575 merged_any_attributes = true;
8579 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
8580 p != input_objects->dynobj_end();
8581 ++p)
8583 Arm_dynobj<big_endian>* arm_dynobj =
8584 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
8585 this->merge_processor_specific_flags(
8586 arm_dynobj->name(),
8587 arm_dynobj->processor_specific_flags());
8588 this->merge_object_attributes(arm_dynobj->name().c_str(),
8589 arm_dynobj->attributes_section_data());
8590 merged_any_attributes = true;
8593 // Create an empty uninitialized attribute section if we still don't have it
8594 // at this moment. This happens if there is no attributes sections in all
8595 // inputs.
8596 if (this->attributes_section_data_ == NULL)
8597 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
8599 // Check BLX use.
8600 const Object_attribute* cpu_arch_attr =
8601 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
8602 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
8603 this->set_may_use_blx(true);
8605 // Check if we need to use Cortex-A8 workaround.
8606 if (parameters->options().user_set_fix_cortex_a8())
8607 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
8608 else
8610 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
8611 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
8612 // profile.
8613 const Object_attribute* cpu_arch_profile_attr =
8614 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
8615 this->fix_cortex_a8_ =
8616 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
8617 && (cpu_arch_profile_attr->int_value() == 'A'
8618 || cpu_arch_profile_attr->int_value() == 0));
8621 // Check if we can use V4BX interworking.
8622 // The V4BX interworking stub contains BX instruction,
8623 // which is not specified for some profiles.
8624 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8625 && !this->may_use_blx())
8626 gold_error(_("unable to provide V4BX reloc interworking fix up; "
8627 "the target profile does not support BX instruction"));
8629 // Fill in some more dynamic tags.
8630 const Reloc_section* rel_plt = (this->plt_ == NULL
8631 ? NULL
8632 : this->plt_->rel_plt());
8633 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
8634 this->rel_dyn_, true, false);
8636 // Emit any relocs we saved in an attempt to avoid generating COPY
8637 // relocs.
8638 if (this->copy_relocs_.any_saved_relocs())
8639 this->copy_relocs_.emit(this->rel_dyn_section(layout));
8641 // Handle the .ARM.exidx section.
8642 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
8644 if (!parameters->options().relocatable())
8646 if (exidx_section != NULL
8647 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
8649 // Create __exidx_start and __exidx_end symbols.
8650 symtab->define_in_output_data("__exidx_start", NULL,
8651 Symbol_table::PREDEFINED,
8652 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8653 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
8654 0, false, true);
8655 symtab->define_in_output_data("__exidx_end", NULL,
8656 Symbol_table::PREDEFINED,
8657 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8658 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
8659 0, true, true);
8661 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
8662 // the .ARM.exidx section.
8663 if (!layout->script_options()->saw_phdrs_clause())
8665 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0,
8667 == NULL);
8668 Output_segment* exidx_segment =
8669 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
8670 exidx_segment->add_output_section_to_nonload(exidx_section,
8671 elfcpp::PF_R);
8674 else
8676 symtab->define_as_constant("__exidx_start", NULL,
8677 Symbol_table::PREDEFINED,
8678 0, 0, elfcpp::STT_OBJECT,
8679 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8680 true, false);
8681 symtab->define_as_constant("__exidx_end", NULL,
8682 Symbol_table::PREDEFINED,
8683 0, 0, elfcpp::STT_OBJECT,
8684 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8685 true, false);
8689 // Create an .ARM.attributes section if we have merged any attributes
8690 // from inputs.
8691 if (merged_any_attributes)
8693 Output_attributes_section_data* attributes_section =
8694 new Output_attributes_section_data(*this->attributes_section_data_);
8695 layout->add_output_section_data(".ARM.attributes",
8696 elfcpp::SHT_ARM_ATTRIBUTES, 0,
8697 attributes_section, ORDER_INVALID,
8698 false);
8701 // Fix up links in section EXIDX headers.
8702 for (Layout::Section_list::const_iterator p = layout->section_list().begin();
8703 p != layout->section_list().end();
8704 ++p)
8705 if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
8707 Arm_output_section<big_endian>* os =
8708 Arm_output_section<big_endian>::as_arm_output_section(*p);
8709 os->set_exidx_section_link();
8713 // Return whether a direct absolute static relocation needs to be applied.
8714 // In cases where Scan::local() or Scan::global() has created
8715 // a dynamic relocation other than R_ARM_RELATIVE, the addend
8716 // of the relocation is carried in the data, and we must not
8717 // apply the static relocation.
8719 template<bool big_endian>
8720 inline bool
8721 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
8722 const Sized_symbol<32>* gsym,
8723 unsigned int r_type,
8724 bool is_32bit,
8725 Output_section* output_section)
8727 // If the output section is not allocated, then we didn't call
8728 // scan_relocs, we didn't create a dynamic reloc, and we must apply
8729 // the reloc here.
8730 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
8731 return true;
8733 int ref_flags = Scan::get_reference_flags(r_type);
8735 // For local symbols, we will have created a non-RELATIVE dynamic
8736 // relocation only if (a) the output is position independent,
8737 // (b) the relocation is absolute (not pc- or segment-relative), and
8738 // (c) the relocation is not 32 bits wide.
8739 if (gsym == NULL)
8740 return !(parameters->options().output_is_position_independent()
8741 && (ref_flags & Symbol::ABSOLUTE_REF)
8742 && !is_32bit);
8744 // For global symbols, we use the same helper routines used in the
8745 // scan pass. If we did not create a dynamic relocation, or if we
8746 // created a RELATIVE dynamic relocation, we should apply the static
8747 // relocation.
8748 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
8749 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
8750 && gsym->can_use_relative_reloc(ref_flags
8751 & Symbol::FUNCTION_CALL);
8752 return !has_dyn || is_rel;
8755 // Perform a relocation.
8757 template<bool big_endian>
8758 inline bool
8759 Target_arm<big_endian>::Relocate::relocate(
8760 const Relocate_info<32, big_endian>* relinfo,
8761 Target_arm* target,
8762 Output_section* output_section,
8763 size_t relnum,
8764 const elfcpp::Rel<32, big_endian>& rel,
8765 unsigned int r_type,
8766 const Sized_symbol<32>* gsym,
8767 const Symbol_value<32>* psymval,
8768 unsigned char* view,
8769 Arm_address address,
8770 section_size_type view_size)
8772 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
8774 r_type = get_real_reloc_type(r_type);
8775 const Arm_reloc_property* reloc_property =
8776 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8777 if (reloc_property == NULL)
8779 std::string reloc_name =
8780 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8781 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8782 _("cannot relocate %s in object file"),
8783 reloc_name.c_str());
8784 return true;
8787 const Arm_relobj<big_endian>* object =
8788 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8790 // If the final branch target of a relocation is THUMB instruction, this
8791 // is 1. Otherwise it is 0.
8792 Arm_address thumb_bit = 0;
8793 Symbol_value<32> symval;
8794 bool is_weakly_undefined_without_plt = false;
8795 bool have_got_offset = false;
8796 unsigned int got_offset = 0;
8798 // If the relocation uses the GOT entry of a symbol instead of the symbol
8799 // itself, we don't care about whether the symbol is defined or what kind
8800 // of symbol it is.
8801 if (reloc_property->uses_got_entry())
8803 // Get the GOT offset.
8804 // The GOT pointer points to the end of the GOT section.
8805 // We need to subtract the size of the GOT section to get
8806 // the actual offset to use in the relocation.
8807 // TODO: We should move GOT offset computing code in TLS relocations
8808 // to here.
8809 switch (r_type)
8811 case elfcpp::R_ARM_GOT_BREL:
8812 case elfcpp::R_ARM_GOT_PREL:
8813 if (gsym != NULL)
8815 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8816 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
8817 - target->got_size());
8819 else
8821 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8822 gold_assert(object->local_has_got_offset(r_sym,
8823 GOT_TYPE_STANDARD));
8824 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
8825 - target->got_size());
8827 have_got_offset = true;
8828 break;
8830 default:
8831 break;
8834 else if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
8836 if (gsym != NULL)
8838 // This is a global symbol. Determine if we use PLT and if the
8839 // final target is THUMB.
8840 if (gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
8842 // This uses a PLT, change the symbol value.
8843 symval.set_output_value(target->plt_section()->address()
8844 + gsym->plt_offset());
8845 psymval = &symval;
8847 else if (gsym->is_weak_undefined())
8849 // This is a weakly undefined symbol and we do not use PLT
8850 // for this relocation. A branch targeting this symbol will
8851 // be converted into an NOP.
8852 is_weakly_undefined_without_plt = true;
8854 else if (gsym->is_undefined() && reloc_property->uses_symbol())
8856 // This relocation uses the symbol value but the symbol is
8857 // undefined. Exit early and have the caller reporting an
8858 // error.
8859 return true;
8861 else
8863 // Set thumb bit if symbol:
8864 // -Has type STT_ARM_TFUNC or
8865 // -Has type STT_FUNC, is defined and with LSB in value set.
8866 thumb_bit =
8867 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
8868 || (gsym->type() == elfcpp::STT_FUNC
8869 && !gsym->is_undefined()
8870 && ((psymval->value(object, 0) & 1) != 0)))
8872 : 0);
8875 else
8877 // This is a local symbol. Determine if the final target is THUMB.
8878 // We saved this information when all the local symbols were read.
8879 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
8880 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
8881 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
8884 else
8886 // This is a fake relocation synthesized for a stub. It does not have
8887 // a real symbol. We just look at the LSB of the symbol value to
8888 // determine if the target is THUMB or not.
8889 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
8892 // Strip LSB if this points to a THUMB target.
8893 if (thumb_bit != 0
8894 && reloc_property->uses_thumb_bit()
8895 && ((psymval->value(object, 0) & 1) != 0))
8897 Arm_address stripped_value =
8898 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
8899 symval.set_output_value(stripped_value);
8900 psymval = &symval;
8903 // To look up relocation stubs, we need to pass the symbol table index of
8904 // a local symbol.
8905 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8907 // Get the addressing origin of the output segment defining the
8908 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
8909 Arm_address sym_origin = 0;
8910 if (reloc_property->uses_symbol_base())
8912 if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
8913 // R_ARM_BASE_ABS with the NULL symbol will give the
8914 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
8915 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
8916 sym_origin = target->got_plt_section()->address();
8917 else if (gsym == NULL)
8918 sym_origin = 0;
8919 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
8920 sym_origin = gsym->output_segment()->vaddr();
8921 else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
8922 sym_origin = gsym->output_data()->address();
8924 // TODO: Assumes the segment base to be zero for the global symbols
8925 // till the proper support for the segment-base-relative addressing
8926 // will be implemented. This is consistent with GNU ld.
8929 // For relative addressing relocation, find out the relative address base.
8930 Arm_address relative_address_base = 0;
8931 switch(reloc_property->relative_address_base())
8933 case Arm_reloc_property::RAB_NONE:
8934 // Relocations with relative address bases RAB_TLS and RAB_tp are
8935 // handled by relocate_tls. So we do not need to do anything here.
8936 case Arm_reloc_property::RAB_TLS:
8937 case Arm_reloc_property::RAB_tp:
8938 break;
8939 case Arm_reloc_property::RAB_B_S:
8940 relative_address_base = sym_origin;
8941 break;
8942 case Arm_reloc_property::RAB_GOT_ORG:
8943 relative_address_base = target->got_plt_section()->address();
8944 break;
8945 case Arm_reloc_property::RAB_P:
8946 relative_address_base = address;
8947 break;
8948 case Arm_reloc_property::RAB_Pa:
8949 relative_address_base = address & 0xfffffffcU;
8950 break;
8951 default:
8952 gold_unreachable();
8955 typename Arm_relocate_functions::Status reloc_status =
8956 Arm_relocate_functions::STATUS_OKAY;
8957 bool check_overflow = reloc_property->checks_overflow();
8958 switch (r_type)
8960 case elfcpp::R_ARM_NONE:
8961 break;
8963 case elfcpp::R_ARM_ABS8:
8964 if (should_apply_static_reloc(gsym, r_type, false, output_section))
8965 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
8966 break;
8968 case elfcpp::R_ARM_ABS12:
8969 if (should_apply_static_reloc(gsym, r_type, false, output_section))
8970 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
8971 break;
8973 case elfcpp::R_ARM_ABS16:
8974 if (should_apply_static_reloc(gsym, r_type, false, output_section))
8975 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
8976 break;
8978 case elfcpp::R_ARM_ABS32:
8979 if (should_apply_static_reloc(gsym, r_type, true, output_section))
8980 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8981 thumb_bit);
8982 break;
8984 case elfcpp::R_ARM_ABS32_NOI:
8985 if (should_apply_static_reloc(gsym, r_type, true, output_section))
8986 // No thumb bit for this relocation: (S + A)
8987 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8989 break;
8991 case elfcpp::R_ARM_MOVW_ABS_NC:
8992 if (should_apply_static_reloc(gsym, r_type, false, output_section))
8993 reloc_status = Arm_relocate_functions::movw(view, object, psymval,
8994 0, thumb_bit,
8995 check_overflow);
8996 break;
8998 case elfcpp::R_ARM_MOVT_ABS:
8999 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9000 reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
9001 break;
9003 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
9004 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9005 reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
9006 0, thumb_bit, false);
9007 break;
9009 case elfcpp::R_ARM_THM_MOVT_ABS:
9010 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9011 reloc_status = Arm_relocate_functions::thm_movt(view, object,
9012 psymval, 0);
9013 break;
9015 case elfcpp::R_ARM_MOVW_PREL_NC:
9016 case elfcpp::R_ARM_MOVW_BREL_NC:
9017 case elfcpp::R_ARM_MOVW_BREL:
9018 reloc_status =
9019 Arm_relocate_functions::movw(view, object, psymval,
9020 relative_address_base, thumb_bit,
9021 check_overflow);
9022 break;
9024 case elfcpp::R_ARM_MOVT_PREL:
9025 case elfcpp::R_ARM_MOVT_BREL:
9026 reloc_status =
9027 Arm_relocate_functions::movt(view, object, psymval,
9028 relative_address_base);
9029 break;
9031 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
9032 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
9033 case elfcpp::R_ARM_THM_MOVW_BREL:
9034 reloc_status =
9035 Arm_relocate_functions::thm_movw(view, object, psymval,
9036 relative_address_base,
9037 thumb_bit, check_overflow);
9038 break;
9040 case elfcpp::R_ARM_THM_MOVT_PREL:
9041 case elfcpp::R_ARM_THM_MOVT_BREL:
9042 reloc_status =
9043 Arm_relocate_functions::thm_movt(view, object, psymval,
9044 relative_address_base);
9045 break;
9047 case elfcpp::R_ARM_REL32:
9048 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
9049 address, thumb_bit);
9050 break;
9052 case elfcpp::R_ARM_THM_ABS5:
9053 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9054 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
9055 break;
9057 // Thumb long branches.
9058 case elfcpp::R_ARM_THM_CALL:
9059 case elfcpp::R_ARM_THM_XPC22:
9060 case elfcpp::R_ARM_THM_JUMP24:
9061 reloc_status =
9062 Arm_relocate_functions::thumb_branch_common(
9063 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
9064 thumb_bit, is_weakly_undefined_without_plt);
9065 break;
9067 case elfcpp::R_ARM_GOTOFF32:
9069 Arm_address got_origin;
9070 got_origin = target->got_plt_section()->address();
9071 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
9072 got_origin, thumb_bit);
9074 break;
9076 case elfcpp::R_ARM_BASE_PREL:
9077 gold_assert(gsym != NULL);
9078 reloc_status =
9079 Arm_relocate_functions::base_prel(view, sym_origin, address);
9080 break;
9082 case elfcpp::R_ARM_BASE_ABS:
9083 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9084 reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
9085 break;
9087 case elfcpp::R_ARM_GOT_BREL:
9088 gold_assert(have_got_offset);
9089 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
9090 break;
9092 case elfcpp::R_ARM_GOT_PREL:
9093 gold_assert(have_got_offset);
9094 // Get the address origin for GOT PLT, which is allocated right
9095 // after the GOT section, to calculate an absolute address of
9096 // the symbol GOT entry (got_origin + got_offset).
9097 Arm_address got_origin;
9098 got_origin = target->got_plt_section()->address();
9099 reloc_status = Arm_relocate_functions::got_prel(view,
9100 got_origin + got_offset,
9101 address);
9102 break;
9104 case elfcpp::R_ARM_PLT32:
9105 case elfcpp::R_ARM_CALL:
9106 case elfcpp::R_ARM_JUMP24:
9107 case elfcpp::R_ARM_XPC25:
9108 gold_assert(gsym == NULL
9109 || gsym->has_plt_offset()
9110 || gsym->final_value_is_known()
9111 || (gsym->is_defined()
9112 && !gsym->is_from_dynobj()
9113 && !gsym->is_preemptible()));
9114 reloc_status =
9115 Arm_relocate_functions::arm_branch_common(
9116 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
9117 thumb_bit, is_weakly_undefined_without_plt);
9118 break;
9120 case elfcpp::R_ARM_THM_JUMP19:
9121 reloc_status =
9122 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
9123 thumb_bit);
9124 break;
9126 case elfcpp::R_ARM_THM_JUMP6:
9127 reloc_status =
9128 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
9129 break;
9131 case elfcpp::R_ARM_THM_JUMP8:
9132 reloc_status =
9133 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
9134 break;
9136 case elfcpp::R_ARM_THM_JUMP11:
9137 reloc_status =
9138 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
9139 break;
9141 case elfcpp::R_ARM_PREL31:
9142 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
9143 address, thumb_bit);
9144 break;
9146 case elfcpp::R_ARM_V4BX:
9147 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
9149 const bool is_v4bx_interworking =
9150 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
9151 reloc_status =
9152 Arm_relocate_functions::v4bx(relinfo, view, object, address,
9153 is_v4bx_interworking);
9155 break;
9157 case elfcpp::R_ARM_THM_PC8:
9158 reloc_status =
9159 Arm_relocate_functions::thm_pc8(view, object, psymval, address);
9160 break;
9162 case elfcpp::R_ARM_THM_PC12:
9163 reloc_status =
9164 Arm_relocate_functions::thm_pc12(view, object, psymval, address);
9165 break;
9167 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
9168 reloc_status =
9169 Arm_relocate_functions::thm_alu11(view, object, psymval, address,
9170 thumb_bit);
9171 break;
9173 case elfcpp::R_ARM_ALU_PC_G0_NC:
9174 case elfcpp::R_ARM_ALU_PC_G0:
9175 case elfcpp::R_ARM_ALU_PC_G1_NC:
9176 case elfcpp::R_ARM_ALU_PC_G1:
9177 case elfcpp::R_ARM_ALU_PC_G2:
9178 case elfcpp::R_ARM_ALU_SB_G0_NC:
9179 case elfcpp::R_ARM_ALU_SB_G0:
9180 case elfcpp::R_ARM_ALU_SB_G1_NC:
9181 case elfcpp::R_ARM_ALU_SB_G1:
9182 case elfcpp::R_ARM_ALU_SB_G2:
9183 reloc_status =
9184 Arm_relocate_functions::arm_grp_alu(view, object, psymval,
9185 reloc_property->group_index(),
9186 relative_address_base,
9187 thumb_bit, check_overflow);
9188 break;
9190 case elfcpp::R_ARM_LDR_PC_G0:
9191 case elfcpp::R_ARM_LDR_PC_G1:
9192 case elfcpp::R_ARM_LDR_PC_G2:
9193 case elfcpp::R_ARM_LDR_SB_G0:
9194 case elfcpp::R_ARM_LDR_SB_G1:
9195 case elfcpp::R_ARM_LDR_SB_G2:
9196 reloc_status =
9197 Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
9198 reloc_property->group_index(),
9199 relative_address_base);
9200 break;
9202 case elfcpp::R_ARM_LDRS_PC_G0:
9203 case elfcpp::R_ARM_LDRS_PC_G1:
9204 case elfcpp::R_ARM_LDRS_PC_G2:
9205 case elfcpp::R_ARM_LDRS_SB_G0:
9206 case elfcpp::R_ARM_LDRS_SB_G1:
9207 case elfcpp::R_ARM_LDRS_SB_G2:
9208 reloc_status =
9209 Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
9210 reloc_property->group_index(),
9211 relative_address_base);
9212 break;
9214 case elfcpp::R_ARM_LDC_PC_G0:
9215 case elfcpp::R_ARM_LDC_PC_G1:
9216 case elfcpp::R_ARM_LDC_PC_G2:
9217 case elfcpp::R_ARM_LDC_SB_G0:
9218 case elfcpp::R_ARM_LDC_SB_G1:
9219 case elfcpp::R_ARM_LDC_SB_G2:
9220 reloc_status =
9221 Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
9222 reloc_property->group_index(),
9223 relative_address_base);
9224 break;
9226 // These are initial tls relocs, which are expected when
9227 // linking.
9228 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
9229 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
9230 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
9231 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
9232 case elfcpp::R_ARM_TLS_LE32: // Local-exec
9233 reloc_status =
9234 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
9235 view, address, view_size);
9236 break;
9238 // The known and unknown unsupported and/or deprecated relocations.
9239 case elfcpp::R_ARM_PC24:
9240 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
9241 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
9242 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
9243 default:
9244 // Just silently leave the method. We should get an appropriate error
9245 // message in the scan methods.
9246 break;
9249 // Report any errors.
9250 switch (reloc_status)
9252 case Arm_relocate_functions::STATUS_OKAY:
9253 break;
9254 case Arm_relocate_functions::STATUS_OVERFLOW:
9255 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9256 _("relocation overflow in %s"),
9257 reloc_property->name().c_str());
9258 break;
9259 case Arm_relocate_functions::STATUS_BAD_RELOC:
9260 gold_error_at_location(
9261 relinfo,
9262 relnum,
9263 rel.get_r_offset(),
9264 _("unexpected opcode while processing relocation %s"),
9265 reloc_property->name().c_str());
9266 break;
9267 default:
9268 gold_unreachable();
9271 return true;
9274 // Perform a TLS relocation.
9276 template<bool big_endian>
9277 inline typename Arm_relocate_functions<big_endian>::Status
9278 Target_arm<big_endian>::Relocate::relocate_tls(
9279 const Relocate_info<32, big_endian>* relinfo,
9280 Target_arm<big_endian>* target,
9281 size_t relnum,
9282 const elfcpp::Rel<32, big_endian>& rel,
9283 unsigned int r_type,
9284 const Sized_symbol<32>* gsym,
9285 const Symbol_value<32>* psymval,
9286 unsigned char* view,
9287 elfcpp::Elf_types<32>::Elf_Addr address,
9288 section_size_type /*view_size*/ )
9290 typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
9291 typedef Relocate_functions<32, big_endian> RelocFuncs;
9292 Output_segment* tls_segment = relinfo->layout->tls_segment();
9294 const Sized_relobj<32, big_endian>* object = relinfo->object;
9296 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
9298 const bool is_final = (gsym == NULL
9299 ? !parameters->options().shared()
9300 : gsym->final_value_is_known());
9301 const tls::Tls_optimization optimized_type
9302 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
9303 switch (r_type)
9305 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
9307 unsigned int got_type = GOT_TYPE_TLS_PAIR;
9308 unsigned int got_offset;
9309 if (gsym != NULL)
9311 gold_assert(gsym->has_got_offset(got_type));
9312 got_offset = gsym->got_offset(got_type) - target->got_size();
9314 else
9316 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9317 gold_assert(object->local_has_got_offset(r_sym, got_type));
9318 got_offset = (object->local_got_offset(r_sym, got_type)
9319 - target->got_size());
9321 if (optimized_type == tls::TLSOPT_NONE)
9323 Arm_address got_entry =
9324 target->got_plt_section()->address() + got_offset;
9326 // Relocate the field with the PC relative offset of the pair of
9327 // GOT entries.
9328 RelocFuncs::pcrel32(view, got_entry, address);
9329 return ArmRelocFuncs::STATUS_OKAY;
9332 break;
9334 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
9335 if (optimized_type == tls::TLSOPT_NONE)
9337 // Relocate the field with the offset of the GOT entry for
9338 // the module index.
9339 unsigned int got_offset;
9340 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
9341 - target->got_size());
9342 Arm_address got_entry =
9343 target->got_plt_section()->address() + got_offset;
9345 // Relocate the field with the PC relative offset of the pair of
9346 // GOT entries.
9347 RelocFuncs::pcrel32(view, got_entry, address);
9348 return ArmRelocFuncs::STATUS_OKAY;
9350 break;
9352 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
9353 RelocFuncs::rel32(view, value);
9354 return ArmRelocFuncs::STATUS_OKAY;
9356 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
9357 if (optimized_type == tls::TLSOPT_NONE)
9359 // Relocate the field with the offset of the GOT entry for
9360 // the tp-relative offset of the symbol.
9361 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
9362 unsigned int got_offset;
9363 if (gsym != NULL)
9365 gold_assert(gsym->has_got_offset(got_type));
9366 got_offset = gsym->got_offset(got_type);
9368 else
9370 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9371 gold_assert(object->local_has_got_offset(r_sym, got_type));
9372 got_offset = object->local_got_offset(r_sym, got_type);
9375 // All GOT offsets are relative to the end of the GOT.
9376 got_offset -= target->got_size();
9378 Arm_address got_entry =
9379 target->got_plt_section()->address() + got_offset;
9381 // Relocate the field with the PC relative offset of the GOT entry.
9382 RelocFuncs::pcrel32(view, got_entry, address);
9383 return ArmRelocFuncs::STATUS_OKAY;
9385 break;
9387 case elfcpp::R_ARM_TLS_LE32: // Local-exec
9388 // If we're creating a shared library, a dynamic relocation will
9389 // have been created for this location, so do not apply it now.
9390 if (!parameters->options().shared())
9392 gold_assert(tls_segment != NULL);
9394 // $tp points to the TCB, which is followed by the TLS, so we
9395 // need to add TCB size to the offset.
9396 Arm_address aligned_tcb_size =
9397 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
9398 RelocFuncs::rel32(view, value + aligned_tcb_size);
9401 return ArmRelocFuncs::STATUS_OKAY;
9403 default:
9404 gold_unreachable();
9407 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9408 _("unsupported reloc %u"),
9409 r_type);
9410 return ArmRelocFuncs::STATUS_BAD_RELOC;
9413 // Relocate section data.
9415 template<bool big_endian>
9416 void
9417 Target_arm<big_endian>::relocate_section(
9418 const Relocate_info<32, big_endian>* relinfo,
9419 unsigned int sh_type,
9420 const unsigned char* prelocs,
9421 size_t reloc_count,
9422 Output_section* output_section,
9423 bool needs_special_offset_handling,
9424 unsigned char* view,
9425 Arm_address address,
9426 section_size_type view_size,
9427 const Reloc_symbol_changes* reloc_symbol_changes)
9429 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
9430 gold_assert(sh_type == elfcpp::SHT_REL);
9432 // See if we are relocating a relaxed input section. If so, the view
9433 // covers the whole output section and we need to adjust accordingly.
9434 if (needs_special_offset_handling)
9436 const Output_relaxed_input_section* poris =
9437 output_section->find_relaxed_input_section(relinfo->object,
9438 relinfo->data_shndx);
9439 if (poris != NULL)
9441 Arm_address section_address = poris->address();
9442 section_size_type section_size = poris->data_size();
9444 gold_assert((section_address >= address)
9445 && ((section_address + section_size)
9446 <= (address + view_size)));
9448 off_t offset = section_address - address;
9449 view += offset;
9450 address += offset;
9451 view_size = section_size;
9455 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
9456 Arm_relocate>(
9457 relinfo,
9458 this,
9459 prelocs,
9460 reloc_count,
9461 output_section,
9462 needs_special_offset_handling,
9463 view,
9464 address,
9465 view_size,
9466 reloc_symbol_changes);
9469 // Return the size of a relocation while scanning during a relocatable
9470 // link.
9472 template<bool big_endian>
9473 unsigned int
9474 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
9475 unsigned int r_type,
9476 Relobj* object)
9478 r_type = get_real_reloc_type(r_type);
9479 const Arm_reloc_property* arp =
9480 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
9481 if (arp != NULL)
9482 return arp->size();
9483 else
9485 std::string reloc_name =
9486 arm_reloc_property_table->reloc_name_in_error_message(r_type);
9487 gold_error(_("%s: unexpected %s in object file"),
9488 object->name().c_str(), reloc_name.c_str());
9489 return 0;
9493 // Scan the relocs during a relocatable link.
9495 template<bool big_endian>
9496 void
9497 Target_arm<big_endian>::scan_relocatable_relocs(
9498 Symbol_table* symtab,
9499 Layout* layout,
9500 Sized_relobj<32, big_endian>* object,
9501 unsigned int data_shndx,
9502 unsigned int sh_type,
9503 const unsigned char* prelocs,
9504 size_t reloc_count,
9505 Output_section* output_section,
9506 bool needs_special_offset_handling,
9507 size_t local_symbol_count,
9508 const unsigned char* plocal_symbols,
9509 Relocatable_relocs* rr)
9511 gold_assert(sh_type == elfcpp::SHT_REL);
9513 typedef Arm_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL,
9514 Relocatable_size_for_reloc> Scan_relocatable_relocs;
9516 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
9517 Scan_relocatable_relocs>(
9518 symtab,
9519 layout,
9520 object,
9521 data_shndx,
9522 prelocs,
9523 reloc_count,
9524 output_section,
9525 needs_special_offset_handling,
9526 local_symbol_count,
9527 plocal_symbols,
9528 rr);
9531 // Relocate a section during a relocatable link.
9533 template<bool big_endian>
9534 void
9535 Target_arm<big_endian>::relocate_for_relocatable(
9536 const Relocate_info<32, big_endian>* relinfo,
9537 unsigned int sh_type,
9538 const unsigned char* prelocs,
9539 size_t reloc_count,
9540 Output_section* output_section,
9541 off_t offset_in_output_section,
9542 const Relocatable_relocs* rr,
9543 unsigned char* view,
9544 Arm_address view_address,
9545 section_size_type view_size,
9546 unsigned char* reloc_view,
9547 section_size_type reloc_view_size)
9549 gold_assert(sh_type == elfcpp::SHT_REL);
9551 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
9552 relinfo,
9553 prelocs,
9554 reloc_count,
9555 output_section,
9556 offset_in_output_section,
9558 view,
9559 view_address,
9560 view_size,
9561 reloc_view,
9562 reloc_view_size);
9565 // Perform target-specific processing in a relocatable link. This is
9566 // only used if we use the relocation strategy RELOC_SPECIAL.
9568 template<bool big_endian>
9569 void
9570 Target_arm<big_endian>::relocate_special_relocatable(
9571 const Relocate_info<32, big_endian>* relinfo,
9572 unsigned int sh_type,
9573 const unsigned char* preloc_in,
9574 size_t relnum,
9575 Output_section* output_section,
9576 off_t offset_in_output_section,
9577 unsigned char* view,
9578 elfcpp::Elf_types<32>::Elf_Addr view_address,
9579 section_size_type,
9580 unsigned char* preloc_out)
9582 // We can only handle REL type relocation sections.
9583 gold_assert(sh_type == elfcpp::SHT_REL);
9585 typedef typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc Reltype;
9586 typedef typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc_write
9587 Reltype_write;
9588 const Arm_address invalid_address = static_cast<Arm_address>(0) - 1;
9590 const Arm_relobj<big_endian>* object =
9591 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
9592 const unsigned int local_count = object->local_symbol_count();
9594 Reltype reloc(preloc_in);
9595 Reltype_write reloc_write(preloc_out);
9597 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
9598 const unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
9599 const unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
9601 const Arm_reloc_property* arp =
9602 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
9603 gold_assert(arp != NULL);
9605 // Get the new symbol index.
9606 // We only use RELOC_SPECIAL strategy in local relocations.
9607 gold_assert(r_sym < local_count);
9609 // We are adjusting a section symbol. We need to find
9610 // the symbol table index of the section symbol for
9611 // the output section corresponding to input section
9612 // in which this symbol is defined.
9613 bool is_ordinary;
9614 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
9615 gold_assert(is_ordinary);
9616 Output_section* os = object->output_section(shndx);
9617 gold_assert(os != NULL);
9618 gold_assert(os->needs_symtab_index());
9619 unsigned int new_symndx = os->symtab_index();
9621 // Get the new offset--the location in the output section where
9622 // this relocation should be applied.
9624 Arm_address offset = reloc.get_r_offset();
9625 Arm_address new_offset;
9626 if (offset_in_output_section != invalid_address)
9627 new_offset = offset + offset_in_output_section;
9628 else
9630 section_offset_type sot_offset =
9631 convert_types<section_offset_type, Arm_address>(offset);
9632 section_offset_type new_sot_offset =
9633 output_section->output_offset(object, relinfo->data_shndx,
9634 sot_offset);
9635 gold_assert(new_sot_offset != -1);
9636 new_offset = new_sot_offset;
9639 // In an object file, r_offset is an offset within the section.
9640 // In an executable or dynamic object, generated by
9641 // --emit-relocs, r_offset is an absolute address.
9642 if (!parameters->options().relocatable())
9644 new_offset += view_address;
9645 if (offset_in_output_section != invalid_address)
9646 new_offset -= offset_in_output_section;
9649 reloc_write.put_r_offset(new_offset);
9650 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
9652 // Handle the reloc addend.
9653 // The relocation uses a section symbol in the input file.
9654 // We are adjusting it to use a section symbol in the output
9655 // file. The input section symbol refers to some address in
9656 // the input section. We need the relocation in the output
9657 // file to refer to that same address. This adjustment to
9658 // the addend is the same calculation we use for a simple
9659 // absolute relocation for the input section symbol.
9661 const Symbol_value<32>* psymval = object->local_symbol(r_sym);
9663 // Handle THUMB bit.
9664 Symbol_value<32> symval;
9665 Arm_address thumb_bit =
9666 object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
9667 if (thumb_bit != 0
9668 && arp->uses_thumb_bit()
9669 && ((psymval->value(object, 0) & 1) != 0))
9671 Arm_address stripped_value =
9672 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
9673 symval.set_output_value(stripped_value);
9674 psymval = &symval;
9677 unsigned char* paddend = view + offset;
9678 typename Arm_relocate_functions<big_endian>::Status reloc_status =
9679 Arm_relocate_functions<big_endian>::STATUS_OKAY;
9680 switch (r_type)
9682 case elfcpp::R_ARM_ABS8:
9683 reloc_status = Arm_relocate_functions<big_endian>::abs8(paddend, object,
9684 psymval);
9685 break;
9687 case elfcpp::R_ARM_ABS12:
9688 reloc_status = Arm_relocate_functions<big_endian>::abs12(paddend, object,
9689 psymval);
9690 break;
9692 case elfcpp::R_ARM_ABS16:
9693 reloc_status = Arm_relocate_functions<big_endian>::abs16(paddend, object,
9694 psymval);
9695 break;
9697 case elfcpp::R_ARM_THM_ABS5:
9698 reloc_status = Arm_relocate_functions<big_endian>::thm_abs5(paddend,
9699 object,
9700 psymval);
9701 break;
9703 case elfcpp::R_ARM_MOVW_ABS_NC:
9704 case elfcpp::R_ARM_MOVW_PREL_NC:
9705 case elfcpp::R_ARM_MOVW_BREL_NC:
9706 case elfcpp::R_ARM_MOVW_BREL:
9707 reloc_status = Arm_relocate_functions<big_endian>::movw(
9708 paddend, object, psymval, 0, thumb_bit, arp->checks_overflow());
9709 break;
9711 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
9712 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
9713 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
9714 case elfcpp::R_ARM_THM_MOVW_BREL:
9715 reloc_status = Arm_relocate_functions<big_endian>::thm_movw(
9716 paddend, object, psymval, 0, thumb_bit, arp->checks_overflow());
9717 break;
9719 case elfcpp::R_ARM_THM_CALL:
9720 case elfcpp::R_ARM_THM_XPC22:
9721 case elfcpp::R_ARM_THM_JUMP24:
9722 reloc_status =
9723 Arm_relocate_functions<big_endian>::thumb_branch_common(
9724 r_type, relinfo, paddend, NULL, object, 0, psymval, 0, thumb_bit,
9725 false);
9726 break;
9728 case elfcpp::R_ARM_PLT32:
9729 case elfcpp::R_ARM_CALL:
9730 case elfcpp::R_ARM_JUMP24:
9731 case elfcpp::R_ARM_XPC25:
9732 reloc_status =
9733 Arm_relocate_functions<big_endian>::arm_branch_common(
9734 r_type, relinfo, paddend, NULL, object, 0, psymval, 0, thumb_bit,
9735 false);
9736 break;
9738 case elfcpp::R_ARM_THM_JUMP19:
9739 reloc_status =
9740 Arm_relocate_functions<big_endian>::thm_jump19(paddend, object,
9741 psymval, 0, thumb_bit);
9742 break;
9744 case elfcpp::R_ARM_THM_JUMP6:
9745 reloc_status =
9746 Arm_relocate_functions<big_endian>::thm_jump6(paddend, object, psymval,
9748 break;
9750 case elfcpp::R_ARM_THM_JUMP8:
9751 reloc_status =
9752 Arm_relocate_functions<big_endian>::thm_jump8(paddend, object, psymval,
9754 break;
9756 case elfcpp::R_ARM_THM_JUMP11:
9757 reloc_status =
9758 Arm_relocate_functions<big_endian>::thm_jump11(paddend, object, psymval,
9760 break;
9762 case elfcpp::R_ARM_PREL31:
9763 reloc_status =
9764 Arm_relocate_functions<big_endian>::prel31(paddend, object, psymval, 0,
9765 thumb_bit);
9766 break;
9768 case elfcpp::R_ARM_THM_PC8:
9769 reloc_status =
9770 Arm_relocate_functions<big_endian>::thm_pc8(paddend, object, psymval,
9772 break;
9774 case elfcpp::R_ARM_THM_PC12:
9775 reloc_status =
9776 Arm_relocate_functions<big_endian>::thm_pc12(paddend, object, psymval,
9778 break;
9780 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
9781 reloc_status =
9782 Arm_relocate_functions<big_endian>::thm_alu11(paddend, object, psymval,
9783 0, thumb_bit);
9784 break;
9786 // These relocation truncate relocation results so we cannot handle them
9787 // in a relocatable link.
9788 case elfcpp::R_ARM_MOVT_ABS:
9789 case elfcpp::R_ARM_THM_MOVT_ABS:
9790 case elfcpp::R_ARM_MOVT_PREL:
9791 case elfcpp::R_ARM_MOVT_BREL:
9792 case elfcpp::R_ARM_THM_MOVT_PREL:
9793 case elfcpp::R_ARM_THM_MOVT_BREL:
9794 case elfcpp::R_ARM_ALU_PC_G0_NC:
9795 case elfcpp::R_ARM_ALU_PC_G0:
9796 case elfcpp::R_ARM_ALU_PC_G1_NC:
9797 case elfcpp::R_ARM_ALU_PC_G1:
9798 case elfcpp::R_ARM_ALU_PC_G2:
9799 case elfcpp::R_ARM_ALU_SB_G0_NC:
9800 case elfcpp::R_ARM_ALU_SB_G0:
9801 case elfcpp::R_ARM_ALU_SB_G1_NC:
9802 case elfcpp::R_ARM_ALU_SB_G1:
9803 case elfcpp::R_ARM_ALU_SB_G2:
9804 case elfcpp::R_ARM_LDR_PC_G0:
9805 case elfcpp::R_ARM_LDR_PC_G1:
9806 case elfcpp::R_ARM_LDR_PC_G2:
9807 case elfcpp::R_ARM_LDR_SB_G0:
9808 case elfcpp::R_ARM_LDR_SB_G1:
9809 case elfcpp::R_ARM_LDR_SB_G2:
9810 case elfcpp::R_ARM_LDRS_PC_G0:
9811 case elfcpp::R_ARM_LDRS_PC_G1:
9812 case elfcpp::R_ARM_LDRS_PC_G2:
9813 case elfcpp::R_ARM_LDRS_SB_G0:
9814 case elfcpp::R_ARM_LDRS_SB_G1:
9815 case elfcpp::R_ARM_LDRS_SB_G2:
9816 case elfcpp::R_ARM_LDC_PC_G0:
9817 case elfcpp::R_ARM_LDC_PC_G1:
9818 case elfcpp::R_ARM_LDC_PC_G2:
9819 case elfcpp::R_ARM_LDC_SB_G0:
9820 case elfcpp::R_ARM_LDC_SB_G1:
9821 case elfcpp::R_ARM_LDC_SB_G2:
9822 gold_error(_("cannot handle %s in a relocatable link"),
9823 arp->name().c_str());
9824 break;
9826 default:
9827 gold_unreachable();
9830 // Report any errors.
9831 switch (reloc_status)
9833 case Arm_relocate_functions<big_endian>::STATUS_OKAY:
9834 break;
9835 case Arm_relocate_functions<big_endian>::STATUS_OVERFLOW:
9836 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
9837 _("relocation overflow in %s"),
9838 arp->name().c_str());
9839 break;
9840 case Arm_relocate_functions<big_endian>::STATUS_BAD_RELOC:
9841 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
9842 _("unexpected opcode while processing relocation %s"),
9843 arp->name().c_str());
9844 break;
9845 default:
9846 gold_unreachable();
9850 // Return the value to use for a dynamic symbol which requires special
9851 // treatment. This is how we support equality comparisons of function
9852 // pointers across shared library boundaries, as described in the
9853 // processor specific ABI supplement.
9855 template<bool big_endian>
9856 uint64_t
9857 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
9859 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
9860 return this->plt_section()->address() + gsym->plt_offset();
9863 // Map platform-specific relocs to real relocs
9865 template<bool big_endian>
9866 unsigned int
9867 Target_arm<big_endian>::get_real_reloc_type(unsigned int r_type)
9869 switch (r_type)
9871 case elfcpp::R_ARM_TARGET1:
9872 // This is either R_ARM_ABS32 or R_ARM_REL32;
9873 return elfcpp::R_ARM_ABS32;
9875 case elfcpp::R_ARM_TARGET2:
9876 // This can be any reloc type but usually is R_ARM_GOT_PREL
9877 return elfcpp::R_ARM_GOT_PREL;
9879 default:
9880 return r_type;
9884 // Whether if two EABI versions V1 and V2 are compatible.
9886 template<bool big_endian>
9887 bool
9888 Target_arm<big_endian>::are_eabi_versions_compatible(
9889 elfcpp::Elf_Word v1,
9890 elfcpp::Elf_Word v2)
9892 // v4 and v5 are the same spec before and after it was released,
9893 // so allow mixing them.
9894 if ((v1 == elfcpp::EF_ARM_EABI_UNKNOWN || v2 == elfcpp::EF_ARM_EABI_UNKNOWN)
9895 || (v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
9896 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
9897 return true;
9899 return v1 == v2;
9902 // Combine FLAGS from an input object called NAME and the processor-specific
9903 // flags in the ELF header of the output. Much of this is adapted from the
9904 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
9905 // in bfd/elf32-arm.c.
9907 template<bool big_endian>
9908 void
9909 Target_arm<big_endian>::merge_processor_specific_flags(
9910 const std::string& name,
9911 elfcpp::Elf_Word flags)
9913 if (this->are_processor_specific_flags_set())
9915 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
9917 // Nothing to merge if flags equal to those in output.
9918 if (flags == out_flags)
9919 return;
9921 // Complain about various flag mismatches.
9922 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
9923 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
9924 if (!this->are_eabi_versions_compatible(version1, version2)
9925 && parameters->options().warn_mismatch())
9926 gold_error(_("Source object %s has EABI version %d but output has "
9927 "EABI version %d."),
9928 name.c_str(),
9929 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
9930 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
9932 else
9934 // If the input is the default architecture and had the default
9935 // flags then do not bother setting the flags for the output
9936 // architecture, instead allow future merges to do this. If no
9937 // future merges ever set these flags then they will retain their
9938 // uninitialised values, which surprise surprise, correspond
9939 // to the default values.
9940 if (flags == 0)
9941 return;
9943 // This is the first time, just copy the flags.
9944 // We only copy the EABI version for now.
9945 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
9949 // Adjust ELF file header.
9950 template<bool big_endian>
9951 void
9952 Target_arm<big_endian>::do_adjust_elf_header(
9953 unsigned char* view,
9954 int len) const
9956 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
9958 elfcpp::Ehdr<32, big_endian> ehdr(view);
9959 unsigned char e_ident[elfcpp::EI_NIDENT];
9960 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9962 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
9963 == elfcpp::EF_ARM_EABI_UNKNOWN)
9964 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
9965 else
9966 e_ident[elfcpp::EI_OSABI] = 0;
9967 e_ident[elfcpp::EI_ABIVERSION] = 0;
9969 // FIXME: Do EF_ARM_BE8 adjustment.
9971 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
9972 oehdr.put_e_ident(e_ident);
9975 // do_make_elf_object to override the same function in the base class.
9976 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
9977 // to store ARM specific information. Hence we need to have our own
9978 // ELF object creation.
9980 template<bool big_endian>
9981 Object*
9982 Target_arm<big_endian>::do_make_elf_object(
9983 const std::string& name,
9984 Input_file* input_file,
9985 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
9987 int et = ehdr.get_e_type();
9988 if (et == elfcpp::ET_REL)
9990 Arm_relobj<big_endian>* obj =
9991 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
9992 obj->setup();
9993 return obj;
9995 else if (et == elfcpp::ET_DYN)
9997 Sized_dynobj<32, big_endian>* obj =
9998 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
9999 obj->setup();
10000 return obj;
10002 else
10004 gold_error(_("%s: unsupported ELF file type %d"),
10005 name.c_str(), et);
10006 return NULL;
10010 // Read the architecture from the Tag_also_compatible_with attribute, if any.
10011 // Returns -1 if no architecture could be read.
10012 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
10014 template<bool big_endian>
10016 Target_arm<big_endian>::get_secondary_compatible_arch(
10017 const Attributes_section_data* pasd)
10019 const Object_attribute* known_attributes =
10020 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
10022 // Note: the tag and its argument below are uleb128 values, though
10023 // currently-defined values fit in one byte for each.
10024 const std::string& sv =
10025 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
10026 if (sv.size() == 2
10027 && sv.data()[0] == elfcpp::Tag_CPU_arch
10028 && (sv.data()[1] & 128) != 128)
10029 return sv.data()[1];
10031 // This tag is "safely ignorable", so don't complain if it looks funny.
10032 return -1;
10035 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
10036 // The tag is removed if ARCH is -1.
10037 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
10039 template<bool big_endian>
10040 void
10041 Target_arm<big_endian>::set_secondary_compatible_arch(
10042 Attributes_section_data* pasd,
10043 int arch)
10045 Object_attribute* known_attributes =
10046 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
10048 if (arch == -1)
10050 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
10051 return;
10054 // Note: the tag and its argument below are uleb128 values, though
10055 // currently-defined values fit in one byte for each.
10056 char sv[3];
10057 sv[0] = elfcpp::Tag_CPU_arch;
10058 gold_assert(arch != 0);
10059 sv[1] = arch;
10060 sv[2] = '\0';
10062 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
10065 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
10066 // into account.
10067 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
10069 template<bool big_endian>
10071 Target_arm<big_endian>::tag_cpu_arch_combine(
10072 const char* name,
10073 int oldtag,
10074 int* secondary_compat_out,
10075 int newtag,
10076 int secondary_compat)
10078 #define T(X) elfcpp::TAG_CPU_ARCH_##X
10079 static const int v6t2[] =
10081 T(V6T2), // PRE_V4.
10082 T(V6T2), // V4.
10083 T(V6T2), // V4T.
10084 T(V6T2), // V5T.
10085 T(V6T2), // V5TE.
10086 T(V6T2), // V5TEJ.
10087 T(V6T2), // V6.
10088 T(V7), // V6KZ.
10089 T(V6T2) // V6T2.
10091 static const int v6k[] =
10093 T(V6K), // PRE_V4.
10094 T(V6K), // V4.
10095 T(V6K), // V4T.
10096 T(V6K), // V5T.
10097 T(V6K), // V5TE.
10098 T(V6K), // V5TEJ.
10099 T(V6K), // V6.
10100 T(V6KZ), // V6KZ.
10101 T(V7), // V6T2.
10102 T(V6K) // V6K.
10104 static const int v7[] =
10106 T(V7), // PRE_V4.
10107 T(V7), // V4.
10108 T(V7), // V4T.
10109 T(V7), // V5T.
10110 T(V7), // V5TE.
10111 T(V7), // V5TEJ.
10112 T(V7), // V6.
10113 T(V7), // V6KZ.
10114 T(V7), // V6T2.
10115 T(V7), // V6K.
10116 T(V7) // V7.
10118 static const int v6_m[] =
10120 -1, // PRE_V4.
10121 -1, // V4.
10122 T(V6K), // V4T.
10123 T(V6K), // V5T.
10124 T(V6K), // V5TE.
10125 T(V6K), // V5TEJ.
10126 T(V6K), // V6.
10127 T(V6KZ), // V6KZ.
10128 T(V7), // V6T2.
10129 T(V6K), // V6K.
10130 T(V7), // V7.
10131 T(V6_M) // V6_M.
10133 static const int v6s_m[] =
10135 -1, // PRE_V4.
10136 -1, // V4.
10137 T(V6K), // V4T.
10138 T(V6K), // V5T.
10139 T(V6K), // V5TE.
10140 T(V6K), // V5TEJ.
10141 T(V6K), // V6.
10142 T(V6KZ), // V6KZ.
10143 T(V7), // V6T2.
10144 T(V6K), // V6K.
10145 T(V7), // V7.
10146 T(V6S_M), // V6_M.
10147 T(V6S_M) // V6S_M.
10149 static const int v7e_m[] =
10151 -1, // PRE_V4.
10152 -1, // V4.
10153 T(V7E_M), // V4T.
10154 T(V7E_M), // V5T.
10155 T(V7E_M), // V5TE.
10156 T(V7E_M), // V5TEJ.
10157 T(V7E_M), // V6.
10158 T(V7E_M), // V6KZ.
10159 T(V7E_M), // V6T2.
10160 T(V7E_M), // V6K.
10161 T(V7E_M), // V7.
10162 T(V7E_M), // V6_M.
10163 T(V7E_M), // V6S_M.
10164 T(V7E_M) // V7E_M.
10166 static const int v4t_plus_v6_m[] =
10168 -1, // PRE_V4.
10169 -1, // V4.
10170 T(V4T), // V4T.
10171 T(V5T), // V5T.
10172 T(V5TE), // V5TE.
10173 T(V5TEJ), // V5TEJ.
10174 T(V6), // V6.
10175 T(V6KZ), // V6KZ.
10176 T(V6T2), // V6T2.
10177 T(V6K), // V6K.
10178 T(V7), // V7.
10179 T(V6_M), // V6_M.
10180 T(V6S_M), // V6S_M.
10181 T(V7E_M), // V7E_M.
10182 T(V4T_PLUS_V6_M) // V4T plus V6_M.
10184 static const int* comb[] =
10186 v6t2,
10187 v6k,
10189 v6_m,
10190 v6s_m,
10191 v7e_m,
10192 // Pseudo-architecture.
10193 v4t_plus_v6_m
10196 // Check we've not got a higher architecture than we know about.
10198 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
10200 gold_error(_("%s: unknown CPU architecture"), name);
10201 return -1;
10204 // Override old tag if we have a Tag_also_compatible_with on the output.
10206 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
10207 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
10208 oldtag = T(V4T_PLUS_V6_M);
10210 // And override the new tag if we have a Tag_also_compatible_with on the
10211 // input.
10213 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
10214 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
10215 newtag = T(V4T_PLUS_V6_M);
10217 // Architectures before V6KZ add features monotonically.
10218 int tagh = std::max(oldtag, newtag);
10219 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
10220 return tagh;
10222 int tagl = std::min(oldtag, newtag);
10223 int result = comb[tagh - T(V6T2)][tagl];
10225 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
10226 // as the canonical version.
10227 if (result == T(V4T_PLUS_V6_M))
10229 result = T(V4T);
10230 *secondary_compat_out = T(V6_M);
10232 else
10233 *secondary_compat_out = -1;
10235 if (result == -1)
10237 gold_error(_("%s: conflicting CPU architectures %d/%d"),
10238 name, oldtag, newtag);
10239 return -1;
10242 return result;
10243 #undef T
10246 // Helper to print AEABI enum tag value.
10248 template<bool big_endian>
10249 std::string
10250 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
10252 static const char* aeabi_enum_names[] =
10253 { "", "variable-size", "32-bit", "" };
10254 const size_t aeabi_enum_names_size =
10255 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
10257 if (value < aeabi_enum_names_size)
10258 return std::string(aeabi_enum_names[value]);
10259 else
10261 char buffer[100];
10262 sprintf(buffer, "<unknown value %u>", value);
10263 return std::string(buffer);
10267 // Return the string value to store in TAG_CPU_name.
10269 template<bool big_endian>
10270 std::string
10271 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
10273 static const char* name_table[] = {
10274 // These aren't real CPU names, but we can't guess
10275 // that from the architecture version alone.
10276 "Pre v4",
10277 "ARM v4",
10278 "ARM v4T",
10279 "ARM v5T",
10280 "ARM v5TE",
10281 "ARM v5TEJ",
10282 "ARM v6",
10283 "ARM v6KZ",
10284 "ARM v6T2",
10285 "ARM v6K",
10286 "ARM v7",
10287 "ARM v6-M",
10288 "ARM v6S-M",
10289 "ARM v7E-M"
10291 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
10293 if (value < name_table_size)
10294 return std::string(name_table[value]);
10295 else
10297 char buffer[100];
10298 sprintf(buffer, "<unknown CPU value %u>", value);
10299 return std::string(buffer);
10303 // Merge object attributes from input file called NAME with those of the
10304 // output. The input object attributes are in the object pointed by PASD.
10306 template<bool big_endian>
10307 void
10308 Target_arm<big_endian>::merge_object_attributes(
10309 const char* name,
10310 const Attributes_section_data* pasd)
10312 // Return if there is no attributes section data.
10313 if (pasd == NULL)
10314 return;
10316 // If output has no object attributes, just copy.
10317 const int vendor = Object_attribute::OBJ_ATTR_PROC;
10318 if (this->attributes_section_data_ == NULL)
10320 this->attributes_section_data_ = new Attributes_section_data(*pasd);
10321 Object_attribute* out_attr =
10322 this->attributes_section_data_->known_attributes(vendor);
10324 // We do not output objects with Tag_MPextension_use_legacy - we move
10325 // the attribute's value to Tag_MPextension_use. */
10326 if (out_attr[elfcpp::Tag_MPextension_use_legacy].int_value() != 0)
10328 if (out_attr[elfcpp::Tag_MPextension_use].int_value() != 0
10329 && out_attr[elfcpp::Tag_MPextension_use_legacy].int_value()
10330 != out_attr[elfcpp::Tag_MPextension_use].int_value())
10332 gold_error(_("%s has both the current and legacy "
10333 "Tag_MPextension_use attributes"),
10334 name);
10337 out_attr[elfcpp::Tag_MPextension_use] =
10338 out_attr[elfcpp::Tag_MPextension_use_legacy];
10339 out_attr[elfcpp::Tag_MPextension_use_legacy].set_type(0);
10340 out_attr[elfcpp::Tag_MPextension_use_legacy].set_int_value(0);
10343 return;
10346 const Object_attribute* in_attr = pasd->known_attributes(vendor);
10347 Object_attribute* out_attr =
10348 this->attributes_section_data_->known_attributes(vendor);
10350 // This needs to happen before Tag_ABI_FP_number_model is merged. */
10351 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
10352 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
10354 // Ignore mismatches if the object doesn't use floating point. */
10355 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
10356 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
10357 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
10358 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0
10359 && parameters->options().warn_mismatch())
10360 gold_error(_("%s uses VFP register arguments, output does not"),
10361 name);
10364 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
10366 // Merge this attribute with existing attributes.
10367 switch (i)
10369 case elfcpp::Tag_CPU_raw_name:
10370 case elfcpp::Tag_CPU_name:
10371 // These are merged after Tag_CPU_arch.
10372 break;
10374 case elfcpp::Tag_ABI_optimization_goals:
10375 case elfcpp::Tag_ABI_FP_optimization_goals:
10376 // Use the first value seen.
10377 break;
10379 case elfcpp::Tag_CPU_arch:
10381 unsigned int saved_out_attr = out_attr->int_value();
10382 // Merge Tag_CPU_arch and Tag_also_compatible_with.
10383 int secondary_compat =
10384 this->get_secondary_compatible_arch(pasd);
10385 int secondary_compat_out =
10386 this->get_secondary_compatible_arch(
10387 this->attributes_section_data_);
10388 out_attr[i].set_int_value(
10389 tag_cpu_arch_combine(name, out_attr[i].int_value(),
10390 &secondary_compat_out,
10391 in_attr[i].int_value(),
10392 secondary_compat));
10393 this->set_secondary_compatible_arch(this->attributes_section_data_,
10394 secondary_compat_out);
10396 // Merge Tag_CPU_name and Tag_CPU_raw_name.
10397 if (out_attr[i].int_value() == saved_out_attr)
10398 ; // Leave the names alone.
10399 else if (out_attr[i].int_value() == in_attr[i].int_value())
10401 // The output architecture has been changed to match the
10402 // input architecture. Use the input names.
10403 out_attr[elfcpp::Tag_CPU_name].set_string_value(
10404 in_attr[elfcpp::Tag_CPU_name].string_value());
10405 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
10406 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
10408 else
10410 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
10411 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
10414 // If we still don't have a value for Tag_CPU_name,
10415 // make one up now. Tag_CPU_raw_name remains blank.
10416 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
10418 const std::string cpu_name =
10419 this->tag_cpu_name_value(out_attr[i].int_value());
10420 // FIXME: If we see an unknown CPU, this will be set
10421 // to "<unknown CPU n>", where n is the attribute value.
10422 // This is different from BFD, which leaves the name alone.
10423 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
10426 break;
10428 case elfcpp::Tag_ARM_ISA_use:
10429 case elfcpp::Tag_THUMB_ISA_use:
10430 case elfcpp::Tag_WMMX_arch:
10431 case elfcpp::Tag_Advanced_SIMD_arch:
10432 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
10433 case elfcpp::Tag_ABI_FP_rounding:
10434 case elfcpp::Tag_ABI_FP_exceptions:
10435 case elfcpp::Tag_ABI_FP_user_exceptions:
10436 case elfcpp::Tag_ABI_FP_number_model:
10437 case elfcpp::Tag_VFP_HP_extension:
10438 case elfcpp::Tag_CPU_unaligned_access:
10439 case elfcpp::Tag_T2EE_use:
10440 case elfcpp::Tag_Virtualization_use:
10441 case elfcpp::Tag_MPextension_use:
10442 // Use the largest value specified.
10443 if (in_attr[i].int_value() > out_attr[i].int_value())
10444 out_attr[i].set_int_value(in_attr[i].int_value());
10445 break;
10447 case elfcpp::Tag_ABI_align8_preserved:
10448 case elfcpp::Tag_ABI_PCS_RO_data:
10449 // Use the smallest value specified.
10450 if (in_attr[i].int_value() < out_attr[i].int_value())
10451 out_attr[i].set_int_value(in_attr[i].int_value());
10452 break;
10454 case elfcpp::Tag_ABI_align8_needed:
10455 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
10456 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
10457 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
10458 == 0)))
10460 // This error message should be enabled once all non-conforming
10461 // binaries in the toolchain have had the attributes set
10462 // properly.
10463 // gold_error(_("output 8-byte data alignment conflicts with %s"),
10464 // name);
10466 // Fall through.
10467 case elfcpp::Tag_ABI_FP_denormal:
10468 case elfcpp::Tag_ABI_PCS_GOT_use:
10470 // These tags have 0 = don't care, 1 = strong requirement,
10471 // 2 = weak requirement.
10472 static const int order_021[3] = {0, 2, 1};
10474 // Use the "greatest" from the sequence 0, 2, 1, or the largest
10475 // value if greater than 2 (for future-proofing).
10476 if ((in_attr[i].int_value() > 2
10477 && in_attr[i].int_value() > out_attr[i].int_value())
10478 || (in_attr[i].int_value() <= 2
10479 && out_attr[i].int_value() <= 2
10480 && (order_021[in_attr[i].int_value()]
10481 > order_021[out_attr[i].int_value()])))
10482 out_attr[i].set_int_value(in_attr[i].int_value());
10484 break;
10486 case elfcpp::Tag_CPU_arch_profile:
10487 if (out_attr[i].int_value() != in_attr[i].int_value())
10489 // 0 will merge with anything.
10490 // 'A' and 'S' merge to 'A'.
10491 // 'R' and 'S' merge to 'R'.
10492 // 'M' and 'A|R|S' is an error.
10493 if (out_attr[i].int_value() == 0
10494 || (out_attr[i].int_value() == 'S'
10495 && (in_attr[i].int_value() == 'A'
10496 || in_attr[i].int_value() == 'R')))
10497 out_attr[i].set_int_value(in_attr[i].int_value());
10498 else if (in_attr[i].int_value() == 0
10499 || (in_attr[i].int_value() == 'S'
10500 && (out_attr[i].int_value() == 'A'
10501 || out_attr[i].int_value() == 'R')))
10502 ; // Do nothing.
10503 else if (parameters->options().warn_mismatch())
10505 gold_error
10506 (_("conflicting architecture profiles %c/%c"),
10507 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
10508 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
10511 break;
10512 case elfcpp::Tag_VFP_arch:
10514 static const struct
10516 int ver;
10517 int regs;
10518 } vfp_versions[7] =
10520 {0, 0},
10521 {1, 16},
10522 {2, 16},
10523 {3, 32},
10524 {3, 16},
10525 {4, 32},
10526 {4, 16}
10529 // Values greater than 6 aren't defined, so just pick the
10530 // biggest.
10531 if (in_attr[i].int_value() > 6
10532 && in_attr[i].int_value() > out_attr[i].int_value())
10534 *out_attr = *in_attr;
10535 break;
10537 // The output uses the superset of input features
10538 // (ISA version) and registers.
10539 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
10540 vfp_versions[out_attr[i].int_value()].ver);
10541 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
10542 vfp_versions[out_attr[i].int_value()].regs);
10543 // This assumes all possible supersets are also a valid
10544 // options.
10545 int newval;
10546 for (newval = 6; newval > 0; newval--)
10548 if (regs == vfp_versions[newval].regs
10549 && ver == vfp_versions[newval].ver)
10550 break;
10552 out_attr[i].set_int_value(newval);
10554 break;
10555 case elfcpp::Tag_PCS_config:
10556 if (out_attr[i].int_value() == 0)
10557 out_attr[i].set_int_value(in_attr[i].int_value());
10558 else if (in_attr[i].int_value() != 0
10559 && out_attr[i].int_value() != 0
10560 && parameters->options().warn_mismatch())
10562 // It's sometimes ok to mix different configs, so this is only
10563 // a warning.
10564 gold_warning(_("%s: conflicting platform configuration"), name);
10566 break;
10567 case elfcpp::Tag_ABI_PCS_R9_use:
10568 if (in_attr[i].int_value() != out_attr[i].int_value()
10569 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
10570 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused
10571 && parameters->options().warn_mismatch())
10573 gold_error(_("%s: conflicting use of R9"), name);
10575 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
10576 out_attr[i].set_int_value(in_attr[i].int_value());
10577 break;
10578 case elfcpp::Tag_ABI_PCS_RW_data:
10579 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
10580 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
10581 != elfcpp::AEABI_R9_SB)
10582 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
10583 != elfcpp::AEABI_R9_unused)
10584 && parameters->options().warn_mismatch())
10586 gold_error(_("%s: SB relative addressing conflicts with use "
10587 "of R9"),
10588 name);
10590 // Use the smallest value specified.
10591 if (in_attr[i].int_value() < out_attr[i].int_value())
10592 out_attr[i].set_int_value(in_attr[i].int_value());
10593 break;
10594 case elfcpp::Tag_ABI_PCS_wchar_t:
10595 if (out_attr[i].int_value()
10596 && in_attr[i].int_value()
10597 && out_attr[i].int_value() != in_attr[i].int_value()
10598 && parameters->options().warn_mismatch()
10599 && parameters->options().wchar_size_warning())
10601 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
10602 "use %u-byte wchar_t; use of wchar_t values "
10603 "across objects may fail"),
10604 name, in_attr[i].int_value(),
10605 out_attr[i].int_value());
10607 else if (in_attr[i].int_value() && !out_attr[i].int_value())
10608 out_attr[i].set_int_value(in_attr[i].int_value());
10609 break;
10610 case elfcpp::Tag_ABI_enum_size:
10611 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
10613 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
10614 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
10616 // The existing object is compatible with anything.
10617 // Use whatever requirements the new object has.
10618 out_attr[i].set_int_value(in_attr[i].int_value());
10620 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
10621 && out_attr[i].int_value() != in_attr[i].int_value()
10622 && parameters->options().warn_mismatch()
10623 && parameters->options().enum_size_warning())
10625 unsigned int in_value = in_attr[i].int_value();
10626 unsigned int out_value = out_attr[i].int_value();
10627 gold_warning(_("%s uses %s enums yet the output is to use "
10628 "%s enums; use of enum values across objects "
10629 "may fail"),
10630 name,
10631 this->aeabi_enum_name(in_value).c_str(),
10632 this->aeabi_enum_name(out_value).c_str());
10635 break;
10636 case elfcpp::Tag_ABI_VFP_args:
10637 // Already done.
10638 break;
10639 case elfcpp::Tag_ABI_WMMX_args:
10640 if (in_attr[i].int_value() != out_attr[i].int_value()
10641 && parameters->options().warn_mismatch())
10643 gold_error(_("%s uses iWMMXt register arguments, output does "
10644 "not"),
10645 name);
10647 break;
10648 case Object_attribute::Tag_compatibility:
10649 // Merged in target-independent code.
10650 break;
10651 case elfcpp::Tag_ABI_HardFP_use:
10652 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
10653 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
10654 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
10655 out_attr[i].set_int_value(3);
10656 else if (in_attr[i].int_value() > out_attr[i].int_value())
10657 out_attr[i].set_int_value(in_attr[i].int_value());
10658 break;
10659 case elfcpp::Tag_ABI_FP_16bit_format:
10660 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
10662 if (in_attr[i].int_value() != out_attr[i].int_value()
10663 && parameters->options().warn_mismatch())
10664 gold_error(_("fp16 format mismatch between %s and output"),
10665 name);
10667 if (in_attr[i].int_value() != 0)
10668 out_attr[i].set_int_value(in_attr[i].int_value());
10669 break;
10671 case elfcpp::Tag_DIV_use:
10672 // This tag is set to zero if we can use UDIV and SDIV in Thumb
10673 // mode on a v7-M or v7-R CPU; to one if we can not use UDIV or
10674 // SDIV at all; and to two if we can use UDIV or SDIV on a v7-A
10675 // CPU. We will merge as follows: If the input attribute's value
10676 // is one then the output attribute's value remains unchanged. If
10677 // the input attribute's value is zero or two then if the output
10678 // attribute's value is one the output value is set to the input
10679 // value, otherwise the output value must be the same as the
10680 // inputs. */
10681 if (in_attr[i].int_value() != 1 && out_attr[i].int_value() != 1)
10683 if (in_attr[i].int_value() != out_attr[i].int_value())
10685 gold_error(_("DIV usage mismatch between %s and output"),
10686 name);
10690 if (in_attr[i].int_value() != 1)
10691 out_attr[i].set_int_value(in_attr[i].int_value());
10693 break;
10695 case elfcpp::Tag_MPextension_use_legacy:
10696 // We don't output objects with Tag_MPextension_use_legacy - we
10697 // move the value to Tag_MPextension_use.
10698 if (in_attr[i].int_value() != 0
10699 && in_attr[elfcpp::Tag_MPextension_use].int_value() != 0)
10701 if (in_attr[elfcpp::Tag_MPextension_use].int_value()
10702 != in_attr[i].int_value())
10704 gold_error(_("%s has has both the current and legacy "
10705 "Tag_MPextension_use attributes"),
10706 name);
10710 if (in_attr[i].int_value()
10711 > out_attr[elfcpp::Tag_MPextension_use].int_value())
10712 out_attr[elfcpp::Tag_MPextension_use] = in_attr[i];
10714 break;
10716 case elfcpp::Tag_nodefaults:
10717 // This tag is set if it exists, but the value is unused (and is
10718 // typically zero). We don't actually need to do anything here -
10719 // the merge happens automatically when the type flags are merged
10720 // below.
10721 break;
10722 case elfcpp::Tag_also_compatible_with:
10723 // Already done in Tag_CPU_arch.
10724 break;
10725 case elfcpp::Tag_conformance:
10726 // Keep the attribute if it matches. Throw it away otherwise.
10727 // No attribute means no claim to conform.
10728 if (in_attr[i].string_value() != out_attr[i].string_value())
10729 out_attr[i].set_string_value("");
10730 break;
10732 default:
10734 const char* err_object = NULL;
10736 // The "known_obj_attributes" table does contain some undefined
10737 // attributes. Ensure that there are unused.
10738 if (out_attr[i].int_value() != 0
10739 || out_attr[i].string_value() != "")
10740 err_object = "output";
10741 else if (in_attr[i].int_value() != 0
10742 || in_attr[i].string_value() != "")
10743 err_object = name;
10745 if (err_object != NULL
10746 && parameters->options().warn_mismatch())
10748 // Attribute numbers >=64 (mod 128) can be safely ignored.
10749 if ((i & 127) < 64)
10750 gold_error(_("%s: unknown mandatory EABI object attribute "
10751 "%d"),
10752 err_object, i);
10753 else
10754 gold_warning(_("%s: unknown EABI object attribute %d"),
10755 err_object, i);
10758 // Only pass on attributes that match in both inputs.
10759 if (!in_attr[i].matches(out_attr[i]))
10761 out_attr[i].set_int_value(0);
10762 out_attr[i].set_string_value("");
10767 // If out_attr was copied from in_attr then it won't have a type yet.
10768 if (in_attr[i].type() && !out_attr[i].type())
10769 out_attr[i].set_type(in_attr[i].type());
10772 // Merge Tag_compatibility attributes and any common GNU ones.
10773 this->attributes_section_data_->merge(name, pasd);
10775 // Check for any attributes not known on ARM.
10776 typedef Vendor_object_attributes::Other_attributes Other_attributes;
10777 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
10778 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
10779 Other_attributes* out_other_attributes =
10780 this->attributes_section_data_->other_attributes(vendor);
10781 Other_attributes::iterator out_iter = out_other_attributes->begin();
10783 while (in_iter != in_other_attributes->end()
10784 || out_iter != out_other_attributes->end())
10786 const char* err_object = NULL;
10787 int err_tag = 0;
10789 // The tags for each list are in numerical order.
10790 // If the tags are equal, then merge.
10791 if (out_iter != out_other_attributes->end()
10792 && (in_iter == in_other_attributes->end()
10793 || in_iter->first > out_iter->first))
10795 // This attribute only exists in output. We can't merge, and we
10796 // don't know what the tag means, so delete it.
10797 err_object = "output";
10798 err_tag = out_iter->first;
10799 int saved_tag = out_iter->first;
10800 delete out_iter->second;
10801 out_other_attributes->erase(out_iter);
10802 out_iter = out_other_attributes->upper_bound(saved_tag);
10804 else if (in_iter != in_other_attributes->end()
10805 && (out_iter != out_other_attributes->end()
10806 || in_iter->first < out_iter->first))
10808 // This attribute only exists in input. We can't merge, and we
10809 // don't know what the tag means, so ignore it.
10810 err_object = name;
10811 err_tag = in_iter->first;
10812 ++in_iter;
10814 else // The tags are equal.
10816 // As present, all attributes in the list are unknown, and
10817 // therefore can't be merged meaningfully.
10818 err_object = "output";
10819 err_tag = out_iter->first;
10821 // Only pass on attributes that match in both inputs.
10822 if (!in_iter->second->matches(*(out_iter->second)))
10824 // No match. Delete the attribute.
10825 int saved_tag = out_iter->first;
10826 delete out_iter->second;
10827 out_other_attributes->erase(out_iter);
10828 out_iter = out_other_attributes->upper_bound(saved_tag);
10830 else
10832 // Matched. Keep the attribute and move to the next.
10833 ++out_iter;
10834 ++in_iter;
10838 if (err_object && parameters->options().warn_mismatch())
10840 // Attribute numbers >=64 (mod 128) can be safely ignored. */
10841 if ((err_tag & 127) < 64)
10843 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
10844 err_object, err_tag);
10846 else
10848 gold_warning(_("%s: unknown EABI object attribute %d"),
10849 err_object, err_tag);
10855 // Stub-generation methods for Target_arm.
10857 // Make a new Arm_input_section object.
10859 template<bool big_endian>
10860 Arm_input_section<big_endian>*
10861 Target_arm<big_endian>::new_arm_input_section(
10862 Relobj* relobj,
10863 unsigned int shndx)
10865 Section_id sid(relobj, shndx);
10867 Arm_input_section<big_endian>* arm_input_section =
10868 new Arm_input_section<big_endian>(relobj, shndx);
10869 arm_input_section->init();
10871 // Register new Arm_input_section in map for look-up.
10872 std::pair<typename Arm_input_section_map::iterator, bool> ins =
10873 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
10875 // Make sure that it we have not created another Arm_input_section
10876 // for this input section already.
10877 gold_assert(ins.second);
10879 return arm_input_section;
10882 // Find the Arm_input_section object corresponding to the SHNDX-th input
10883 // section of RELOBJ.
10885 template<bool big_endian>
10886 Arm_input_section<big_endian>*
10887 Target_arm<big_endian>::find_arm_input_section(
10888 Relobj* relobj,
10889 unsigned int shndx) const
10891 Section_id sid(relobj, shndx);
10892 typename Arm_input_section_map::const_iterator p =
10893 this->arm_input_section_map_.find(sid);
10894 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
10897 // Make a new stub table.
10899 template<bool big_endian>
10900 Stub_table<big_endian>*
10901 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
10903 Stub_table<big_endian>* stub_table =
10904 new Stub_table<big_endian>(owner);
10905 this->stub_tables_.push_back(stub_table);
10907 stub_table->set_address(owner->address() + owner->data_size());
10908 stub_table->set_file_offset(owner->offset() + owner->data_size());
10909 stub_table->finalize_data_size();
10911 return stub_table;
10914 // Scan a relocation for stub generation.
10916 template<bool big_endian>
10917 void
10918 Target_arm<big_endian>::scan_reloc_for_stub(
10919 const Relocate_info<32, big_endian>* relinfo,
10920 unsigned int r_type,
10921 const Sized_symbol<32>* gsym,
10922 unsigned int r_sym,
10923 const Symbol_value<32>* psymval,
10924 elfcpp::Elf_types<32>::Elf_Swxword addend,
10925 Arm_address address)
10927 typedef typename Target_arm<big_endian>::Relocate Relocate;
10929 const Arm_relobj<big_endian>* arm_relobj =
10930 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10932 bool target_is_thumb;
10933 Symbol_value<32> symval;
10934 if (gsym != NULL)
10936 // This is a global symbol. Determine if we use PLT and if the
10937 // final target is THUMB.
10938 if (gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
10940 // This uses a PLT, change the symbol value.
10941 symval.set_output_value(this->plt_section()->address()
10942 + gsym->plt_offset());
10943 psymval = &symval;
10944 target_is_thumb = false;
10946 else if (gsym->is_undefined())
10947 // There is no need to generate a stub symbol is undefined.
10948 return;
10949 else
10951 target_is_thumb =
10952 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
10953 || (gsym->type() == elfcpp::STT_FUNC
10954 && !gsym->is_undefined()
10955 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
10958 else
10960 // This is a local symbol. Determine if the final target is THUMB.
10961 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
10964 // Strip LSB if this points to a THUMB target.
10965 const Arm_reloc_property* reloc_property =
10966 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
10967 gold_assert(reloc_property != NULL);
10968 if (target_is_thumb
10969 && reloc_property->uses_thumb_bit()
10970 && ((psymval->value(arm_relobj, 0) & 1) != 0))
10972 Arm_address stripped_value =
10973 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
10974 symval.set_output_value(stripped_value);
10975 psymval = &symval;
10978 // Get the symbol value.
10979 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
10981 // Owing to pipelining, the PC relative branches below actually skip
10982 // two instructions when the branch offset is 0.
10983 Arm_address destination;
10984 switch (r_type)
10986 case elfcpp::R_ARM_CALL:
10987 case elfcpp::R_ARM_JUMP24:
10988 case elfcpp::R_ARM_PLT32:
10989 // ARM branches.
10990 destination = value + addend + 8;
10991 break;
10992 case elfcpp::R_ARM_THM_CALL:
10993 case elfcpp::R_ARM_THM_XPC22:
10994 case elfcpp::R_ARM_THM_JUMP24:
10995 case elfcpp::R_ARM_THM_JUMP19:
10996 // THUMB branches.
10997 destination = value + addend + 4;
10998 break;
10999 default:
11000 gold_unreachable();
11003 Reloc_stub* stub = NULL;
11004 Stub_type stub_type =
11005 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
11006 target_is_thumb);
11007 if (stub_type != arm_stub_none)
11009 // Try looking up an existing stub from a stub table.
11010 Stub_table<big_endian>* stub_table =
11011 arm_relobj->stub_table(relinfo->data_shndx);
11012 gold_assert(stub_table != NULL);
11014 // Locate stub by destination.
11015 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
11017 // Create a stub if there is not one already
11018 stub = stub_table->find_reloc_stub(stub_key);
11019 if (stub == NULL)
11021 // create a new stub and add it to stub table.
11022 stub = this->stub_factory().make_reloc_stub(stub_type);
11023 stub_table->add_reloc_stub(stub, stub_key);
11026 // Record the destination address.
11027 stub->set_destination_address(destination
11028 | (target_is_thumb ? 1 : 0));
11031 // For Cortex-A8, we need to record a relocation at 4K page boundary.
11032 if (this->fix_cortex_a8_
11033 && (r_type == elfcpp::R_ARM_THM_JUMP24
11034 || r_type == elfcpp::R_ARM_THM_JUMP19
11035 || r_type == elfcpp::R_ARM_THM_CALL
11036 || r_type == elfcpp::R_ARM_THM_XPC22)
11037 && (address & 0xfffU) == 0xffeU)
11039 // Found a candidate. Note we haven't checked the destination is
11040 // within 4K here: if we do so (and don't create a record) we can't
11041 // tell that a branch should have been relocated when scanning later.
11042 this->cortex_a8_relocs_info_[address] =
11043 new Cortex_a8_reloc(stub, r_type,
11044 destination | (target_is_thumb ? 1 : 0));
11048 // This function scans a relocation sections for stub generation.
11049 // The template parameter Relocate must be a class type which provides
11050 // a single function, relocate(), which implements the machine
11051 // specific part of a relocation.
11053 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
11054 // SHT_REL or SHT_RELA.
11056 // PRELOCS points to the relocation data. RELOC_COUNT is the number
11057 // of relocs. OUTPUT_SECTION is the output section.
11058 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
11059 // mapped to output offsets.
11061 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
11062 // VIEW_SIZE is the size. These refer to the input section, unless
11063 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
11064 // the output section.
11066 template<bool big_endian>
11067 template<int sh_type>
11068 void inline
11069 Target_arm<big_endian>::scan_reloc_section_for_stubs(
11070 const Relocate_info<32, big_endian>* relinfo,
11071 const unsigned char* prelocs,
11072 size_t reloc_count,
11073 Output_section* output_section,
11074 bool needs_special_offset_handling,
11075 const unsigned char* view,
11076 elfcpp::Elf_types<32>::Elf_Addr view_address,
11077 section_size_type)
11079 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
11080 const int reloc_size =
11081 Reloc_types<sh_type, 32, big_endian>::reloc_size;
11083 Arm_relobj<big_endian>* arm_object =
11084 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
11085 unsigned int local_count = arm_object->local_symbol_count();
11087 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
11089 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
11091 Reltype reloc(prelocs);
11093 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
11094 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
11095 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
11097 r_type = this->get_real_reloc_type(r_type);
11099 // Only a few relocation types need stubs.
11100 if ((r_type != elfcpp::R_ARM_CALL)
11101 && (r_type != elfcpp::R_ARM_JUMP24)
11102 && (r_type != elfcpp::R_ARM_PLT32)
11103 && (r_type != elfcpp::R_ARM_THM_CALL)
11104 && (r_type != elfcpp::R_ARM_THM_XPC22)
11105 && (r_type != elfcpp::R_ARM_THM_JUMP24)
11106 && (r_type != elfcpp::R_ARM_THM_JUMP19)
11107 && (r_type != elfcpp::R_ARM_V4BX))
11108 continue;
11110 section_offset_type offset =
11111 convert_to_section_size_type(reloc.get_r_offset());
11113 if (needs_special_offset_handling)
11115 offset = output_section->output_offset(relinfo->object,
11116 relinfo->data_shndx,
11117 offset);
11118 if (offset == -1)
11119 continue;
11122 // Create a v4bx stub if --fix-v4bx-interworking is used.
11123 if (r_type == elfcpp::R_ARM_V4BX)
11125 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING)
11127 // Get the BX instruction.
11128 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
11129 const Valtype* wv =
11130 reinterpret_cast<const Valtype*>(view + offset);
11131 elfcpp::Elf_types<32>::Elf_Swxword insn =
11132 elfcpp::Swap<32, big_endian>::readval(wv);
11133 const uint32_t reg = (insn & 0xf);
11135 if (reg < 0xf)
11137 // Try looking up an existing stub from a stub table.
11138 Stub_table<big_endian>* stub_table =
11139 arm_object->stub_table(relinfo->data_shndx);
11140 gold_assert(stub_table != NULL);
11142 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
11144 // create a new stub and add it to stub table.
11145 Arm_v4bx_stub* stub =
11146 this->stub_factory().make_arm_v4bx_stub(reg);
11147 gold_assert(stub != NULL);
11148 stub_table->add_arm_v4bx_stub(stub);
11152 continue;
11155 // Get the addend.
11156 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
11157 elfcpp::Elf_types<32>::Elf_Swxword addend =
11158 stub_addend_reader(r_type, view + offset, reloc);
11160 const Sized_symbol<32>* sym;
11162 Symbol_value<32> symval;
11163 const Symbol_value<32> *psymval;
11164 bool is_defined_in_discarded_section;
11165 unsigned int shndx;
11166 if (r_sym < local_count)
11168 sym = NULL;
11169 psymval = arm_object->local_symbol(r_sym);
11171 // If the local symbol belongs to a section we are discarding,
11172 // and that section is a debug section, try to find the
11173 // corresponding kept section and map this symbol to its
11174 // counterpart in the kept section. The symbol must not
11175 // correspond to a section we are folding.
11176 bool is_ordinary;
11177 shndx = psymval->input_shndx(&is_ordinary);
11178 is_defined_in_discarded_section =
11179 (is_ordinary
11180 && shndx != elfcpp::SHN_UNDEF
11181 && !arm_object->is_section_included(shndx)
11182 && !relinfo->symtab->is_section_folded(arm_object, shndx));
11184 // We need to compute the would-be final value of this local
11185 // symbol.
11186 if (!is_defined_in_discarded_section)
11188 typedef Sized_relobj<32, big_endian> ObjType;
11189 typename ObjType::Compute_final_local_value_status status =
11190 arm_object->compute_final_local_value(r_sym, psymval, &symval,
11191 relinfo->symtab);
11192 if (status == ObjType::CFLV_OK)
11194 // Currently we cannot handle a branch to a target in
11195 // a merged section. If this is the case, issue an error
11196 // and also free the merge symbol value.
11197 if (!symval.has_output_value())
11199 const std::string& section_name =
11200 arm_object->section_name(shndx);
11201 arm_object->error(_("cannot handle branch to local %u "
11202 "in a merged section %s"),
11203 r_sym, section_name.c_str());
11205 psymval = &symval;
11207 else
11209 // We cannot determine the final value.
11210 continue;
11214 else
11216 const Symbol* gsym;
11217 gsym = arm_object->global_symbol(r_sym);
11218 gold_assert(gsym != NULL);
11219 if (gsym->is_forwarder())
11220 gsym = relinfo->symtab->resolve_forwards(gsym);
11222 sym = static_cast<const Sized_symbol<32>*>(gsym);
11223 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
11224 symval.set_output_symtab_index(sym->symtab_index());
11225 else
11226 symval.set_no_output_symtab_entry();
11228 // We need to compute the would-be final value of this global
11229 // symbol.
11230 const Symbol_table* symtab = relinfo->symtab;
11231 const Sized_symbol<32>* sized_symbol =
11232 symtab->get_sized_symbol<32>(gsym);
11233 Symbol_table::Compute_final_value_status status;
11234 Arm_address value =
11235 symtab->compute_final_value<32>(sized_symbol, &status);
11237 // Skip this if the symbol has not output section.
11238 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
11239 continue;
11240 symval.set_output_value(value);
11242 if (gsym->type() == elfcpp::STT_TLS)
11243 symval.set_is_tls_symbol();
11244 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
11245 symval.set_is_ifunc_symbol();
11246 psymval = &symval;
11248 is_defined_in_discarded_section =
11249 (gsym->is_defined_in_discarded_section()
11250 && gsym->is_undefined());
11251 shndx = 0;
11254 Symbol_value<32> symval2;
11255 if (is_defined_in_discarded_section)
11257 if (comdat_behavior == CB_UNDETERMINED)
11259 std::string name = arm_object->section_name(relinfo->data_shndx);
11260 comdat_behavior = get_comdat_behavior(name.c_str());
11262 if (comdat_behavior == CB_PRETEND)
11264 // FIXME: This case does not work for global symbols.
11265 // We have no place to store the original section index.
11266 // Fortunately this does not matter for comdat sections,
11267 // only for sections explicitly discarded by a linker
11268 // script.
11269 bool found;
11270 typename elfcpp::Elf_types<32>::Elf_Addr value =
11271 arm_object->map_to_kept_section(shndx, &found);
11272 if (found)
11273 symval2.set_output_value(value + psymval->input_value());
11274 else
11275 symval2.set_output_value(0);
11277 else
11279 if (comdat_behavior == CB_WARNING)
11280 gold_warning_at_location(relinfo, i, offset,
11281 _("relocation refers to discarded "
11282 "section"));
11283 symval2.set_output_value(0);
11285 symval2.set_no_output_symtab_entry();
11286 psymval = &symval2;
11289 // If symbol is a section symbol, we don't know the actual type of
11290 // destination. Give up.
11291 if (psymval->is_section_symbol())
11292 continue;
11294 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
11295 addend, view_address + offset);
11299 // Scan an input section for stub generation.
11301 template<bool big_endian>
11302 void
11303 Target_arm<big_endian>::scan_section_for_stubs(
11304 const Relocate_info<32, big_endian>* relinfo,
11305 unsigned int sh_type,
11306 const unsigned char* prelocs,
11307 size_t reloc_count,
11308 Output_section* output_section,
11309 bool needs_special_offset_handling,
11310 const unsigned char* view,
11311 Arm_address view_address,
11312 section_size_type view_size)
11314 if (sh_type == elfcpp::SHT_REL)
11315 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
11316 relinfo,
11317 prelocs,
11318 reloc_count,
11319 output_section,
11320 needs_special_offset_handling,
11321 view,
11322 view_address,
11323 view_size);
11324 else if (sh_type == elfcpp::SHT_RELA)
11325 // We do not support RELA type relocations yet. This is provided for
11326 // completeness.
11327 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
11328 relinfo,
11329 prelocs,
11330 reloc_count,
11331 output_section,
11332 needs_special_offset_handling,
11333 view,
11334 view_address,
11335 view_size);
11336 else
11337 gold_unreachable();
11340 // Group input sections for stub generation.
11342 // We group input sections in an output section so that the total size,
11343 // including any padding space due to alignment is smaller than GROUP_SIZE
11344 // unless the only input section in group is bigger than GROUP_SIZE already.
11345 // Then an ARM stub table is created to follow the last input section
11346 // in group. For each group an ARM stub table is created an is placed
11347 // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
11348 // extend the group after the stub table.
11350 template<bool big_endian>
11351 void
11352 Target_arm<big_endian>::group_sections(
11353 Layout* layout,
11354 section_size_type group_size,
11355 bool stubs_always_after_branch,
11356 const Task* task)
11358 // Group input sections and insert stub table
11359 Layout::Section_list section_list;
11360 layout->get_allocated_sections(&section_list);
11361 for (Layout::Section_list::const_iterator p = section_list.begin();
11362 p != section_list.end();
11363 ++p)
11365 Arm_output_section<big_endian>* output_section =
11366 Arm_output_section<big_endian>::as_arm_output_section(*p);
11367 output_section->group_sections(group_size, stubs_always_after_branch,
11368 this, task);
11372 // Relaxation hook. This is where we do stub generation.
11374 template<bool big_endian>
11375 bool
11376 Target_arm<big_endian>::do_relax(
11377 int pass,
11378 const Input_objects* input_objects,
11379 Symbol_table* symtab,
11380 Layout* layout,
11381 const Task* task)
11383 // No need to generate stubs if this is a relocatable link.
11384 gold_assert(!parameters->options().relocatable());
11386 // If this is the first pass, we need to group input sections into
11387 // stub groups.
11388 bool done_exidx_fixup = false;
11389 typedef typename Stub_table_list::iterator Stub_table_iterator;
11390 if (pass == 1)
11392 // Determine the stub group size. The group size is the absolute
11393 // value of the parameter --stub-group-size. If --stub-group-size
11394 // is passed a negative value, we restrict stubs to be always after
11395 // the stubbed branches.
11396 int32_t stub_group_size_param =
11397 parameters->options().stub_group_size();
11398 bool stubs_always_after_branch = stub_group_size_param < 0;
11399 section_size_type stub_group_size = abs(stub_group_size_param);
11401 if (stub_group_size == 1)
11403 // Default value.
11404 // Thumb branch range is +-4MB has to be used as the default
11405 // maximum size (a given section can contain both ARM and Thumb
11406 // code, so the worst case has to be taken into account). If we are
11407 // fixing cortex-a8 errata, the branch range has to be even smaller,
11408 // since wide conditional branch has a range of +-1MB only.
11410 // This value is 48K less than that, which allows for 4096
11411 // 12-byte stubs. If we exceed that, then we will fail to link.
11412 // The user will have to relink with an explicit group size
11413 // option.
11414 stub_group_size = 4145152;
11417 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
11418 // page as the first half of a 32-bit branch straddling two 4K pages.
11419 // This is a crude way of enforcing that. In addition, long conditional
11420 // branches of THUMB-2 have a range of +-1M. If we are fixing cortex-A8
11421 // erratum, limit the group size to (1M - 12k) to avoid unreachable
11422 // cortex-A8 stubs from long conditional branches.
11423 if (this->fix_cortex_a8_)
11425 stubs_always_after_branch = true;
11426 const section_size_type cortex_a8_group_size = 1024 * (1024 - 12);
11427 stub_group_size = std::max(stub_group_size, cortex_a8_group_size);
11430 group_sections(layout, stub_group_size, stubs_always_after_branch, task);
11432 // Also fix .ARM.exidx section coverage.
11433 Arm_output_section<big_endian>* exidx_output_section = NULL;
11434 for (Layout::Section_list::const_iterator p =
11435 layout->section_list().begin();
11436 p != layout->section_list().end();
11437 ++p)
11438 if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
11440 if (exidx_output_section == NULL)
11441 exidx_output_section =
11442 Arm_output_section<big_endian>::as_arm_output_section(*p);
11443 else
11444 // We cannot handle this now.
11445 gold_error(_("multiple SHT_ARM_EXIDX sections %s and %s in a "
11446 "non-relocatable link"),
11447 exidx_output_section->name(),
11448 (*p)->name());
11451 if (exidx_output_section != NULL)
11453 this->fix_exidx_coverage(layout, input_objects, exidx_output_section,
11454 symtab, task);
11455 done_exidx_fixup = true;
11458 else
11460 // If this is not the first pass, addresses and file offsets have
11461 // been reset at this point, set them here.
11462 for (Stub_table_iterator sp = this->stub_tables_.begin();
11463 sp != this->stub_tables_.end();
11464 ++sp)
11466 Arm_input_section<big_endian>* owner = (*sp)->owner();
11467 off_t off = align_address(owner->original_size(),
11468 (*sp)->addralign());
11469 (*sp)->set_address_and_file_offset(owner->address() + off,
11470 owner->offset() + off);
11474 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
11475 // beginning of each relaxation pass, just blow away all the stubs.
11476 // Alternatively, we could selectively remove only the stubs and reloc
11477 // information for code sections that have moved since the last pass.
11478 // That would require more book-keeping.
11479 if (this->fix_cortex_a8_)
11481 // Clear all Cortex-A8 reloc information.
11482 for (typename Cortex_a8_relocs_info::const_iterator p =
11483 this->cortex_a8_relocs_info_.begin();
11484 p != this->cortex_a8_relocs_info_.end();
11485 ++p)
11486 delete p->second;
11487 this->cortex_a8_relocs_info_.clear();
11489 // Remove all Cortex-A8 stubs.
11490 for (Stub_table_iterator sp = this->stub_tables_.begin();
11491 sp != this->stub_tables_.end();
11492 ++sp)
11493 (*sp)->remove_all_cortex_a8_stubs();
11496 // Scan relocs for relocation stubs
11497 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
11498 op != input_objects->relobj_end();
11499 ++op)
11501 Arm_relobj<big_endian>* arm_relobj =
11502 Arm_relobj<big_endian>::as_arm_relobj(*op);
11503 // Lock the object so we can read from it. This is only called
11504 // single-threaded from Layout::finalize, so it is OK to lock.
11505 Task_lock_obj<Object> tl(task, arm_relobj);
11506 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
11509 // Check all stub tables to see if any of them have their data sizes
11510 // or addresses alignments changed. These are the only things that
11511 // matter.
11512 bool any_stub_table_changed = false;
11513 Unordered_set<const Output_section*> sections_needing_adjustment;
11514 for (Stub_table_iterator sp = this->stub_tables_.begin();
11515 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
11516 ++sp)
11518 if ((*sp)->update_data_size_and_addralign())
11520 // Update data size of stub table owner.
11521 Arm_input_section<big_endian>* owner = (*sp)->owner();
11522 uint64_t address = owner->address();
11523 off_t offset = owner->offset();
11524 owner->reset_address_and_file_offset();
11525 owner->set_address_and_file_offset(address, offset);
11527 sections_needing_adjustment.insert(owner->output_section());
11528 any_stub_table_changed = true;
11532 // Output_section_data::output_section() returns a const pointer but we
11533 // need to update output sections, so we record all output sections needing
11534 // update above and scan the sections here to find out what sections need
11535 // to be updated.
11536 for (Layout::Section_list::const_iterator p = layout->section_list().begin();
11537 p != layout->section_list().end();
11538 ++p)
11540 if (sections_needing_adjustment.find(*p)
11541 != sections_needing_adjustment.end())
11542 (*p)->set_section_offsets_need_adjustment();
11545 // Stop relaxation if no EXIDX fix-up and no stub table change.
11546 bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
11548 // Finalize the stubs in the last relaxation pass.
11549 if (!continue_relaxation)
11551 for (Stub_table_iterator sp = this->stub_tables_.begin();
11552 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
11553 ++sp)
11554 (*sp)->finalize_stubs();
11556 // Update output local symbol counts of objects if necessary.
11557 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
11558 op != input_objects->relobj_end();
11559 ++op)
11561 Arm_relobj<big_endian>* arm_relobj =
11562 Arm_relobj<big_endian>::as_arm_relobj(*op);
11564 // Update output local symbol counts. We need to discard local
11565 // symbols defined in parts of input sections that are discarded by
11566 // relaxation.
11567 if (arm_relobj->output_local_symbol_count_needs_update())
11569 // We need to lock the object's file to update it.
11570 Task_lock_obj<Object> tl(task, arm_relobj);
11571 arm_relobj->update_output_local_symbol_count();
11576 return continue_relaxation;
11579 // Relocate a stub.
11581 template<bool big_endian>
11582 void
11583 Target_arm<big_endian>::relocate_stub(
11584 Stub* stub,
11585 const Relocate_info<32, big_endian>* relinfo,
11586 Output_section* output_section,
11587 unsigned char* view,
11588 Arm_address address,
11589 section_size_type view_size)
11591 Relocate relocate;
11592 const Stub_template* stub_template = stub->stub_template();
11593 for (size_t i = 0; i < stub_template->reloc_count(); i++)
11595 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
11596 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
11598 unsigned int r_type = insn->r_type();
11599 section_size_type reloc_offset = stub_template->reloc_offset(i);
11600 section_size_type reloc_size = insn->size();
11601 gold_assert(reloc_offset + reloc_size <= view_size);
11603 // This is the address of the stub destination.
11604 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
11605 Symbol_value<32> symval;
11606 symval.set_output_value(target);
11608 // Synthesize a fake reloc just in case. We don't have a symbol so
11609 // we use 0.
11610 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
11611 memset(reloc_buffer, 0, sizeof(reloc_buffer));
11612 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
11613 reloc_write.put_r_offset(reloc_offset);
11614 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
11615 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
11617 relocate.relocate(relinfo, this, output_section,
11618 this->fake_relnum_for_stubs, rel, r_type,
11619 NULL, &symval, view + reloc_offset,
11620 address + reloc_offset, reloc_size);
11624 // Determine whether an object attribute tag takes an integer, a
11625 // string or both.
11627 template<bool big_endian>
11629 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
11631 if (tag == Object_attribute::Tag_compatibility)
11632 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
11633 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
11634 else if (tag == elfcpp::Tag_nodefaults)
11635 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
11636 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
11637 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
11638 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
11639 else if (tag < 32)
11640 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
11641 else
11642 return ((tag & 1) != 0
11643 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
11644 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
11647 // Reorder attributes.
11649 // The ABI defines that Tag_conformance should be emitted first, and that
11650 // Tag_nodefaults should be second (if either is defined). This sets those
11651 // two positions, and bumps up the position of all the remaining tags to
11652 // compensate.
11654 template<bool big_endian>
11656 Target_arm<big_endian>::do_attributes_order(int num) const
11658 // Reorder the known object attributes in output. We want to move
11659 // Tag_conformance to position 4 and Tag_conformance to position 5
11660 // and shift everything between 4 .. Tag_conformance - 1 to make room.
11661 if (num == 4)
11662 return elfcpp::Tag_conformance;
11663 if (num == 5)
11664 return elfcpp::Tag_nodefaults;
11665 if ((num - 2) < elfcpp::Tag_nodefaults)
11666 return num - 2;
11667 if ((num - 1) < elfcpp::Tag_conformance)
11668 return num - 1;
11669 return num;
11672 // Scan a span of THUMB code for Cortex-A8 erratum.
11674 template<bool big_endian>
11675 void
11676 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
11677 Arm_relobj<big_endian>* arm_relobj,
11678 unsigned int shndx,
11679 section_size_type span_start,
11680 section_size_type span_end,
11681 const unsigned char* view,
11682 Arm_address address)
11684 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
11686 // The opcode is BLX.W, BL.W, B.W, Bcc.W
11687 // The branch target is in the same 4KB region as the
11688 // first half of the branch.
11689 // The instruction before the branch is a 32-bit
11690 // length non-branch instruction.
11691 section_size_type i = span_start;
11692 bool last_was_32bit = false;
11693 bool last_was_branch = false;
11694 while (i < span_end)
11696 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
11697 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
11698 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
11699 bool is_blx = false, is_b = false;
11700 bool is_bl = false, is_bcc = false;
11702 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
11703 if (insn_32bit)
11705 // Load the rest of the insn (in manual-friendly order).
11706 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
11708 // Encoding T4: B<c>.W.
11709 is_b = (insn & 0xf800d000U) == 0xf0009000U;
11710 // Encoding T1: BL<c>.W.
11711 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
11712 // Encoding T2: BLX<c>.W.
11713 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
11714 // Encoding T3: B<c>.W (not permitted in IT block).
11715 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
11716 && (insn & 0x07f00000U) != 0x03800000U);
11719 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
11721 // If this instruction is a 32-bit THUMB branch that crosses a 4K
11722 // page boundary and it follows 32-bit non-branch instruction,
11723 // we need to work around.
11724 if (is_32bit_branch
11725 && ((address + i) & 0xfffU) == 0xffeU
11726 && last_was_32bit
11727 && !last_was_branch)
11729 // Check to see if there is a relocation stub for this branch.
11730 bool force_target_arm = false;
11731 bool force_target_thumb = false;
11732 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
11733 Cortex_a8_relocs_info::const_iterator p =
11734 this->cortex_a8_relocs_info_.find(address + i);
11736 if (p != this->cortex_a8_relocs_info_.end())
11738 cortex_a8_reloc = p->second;
11739 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
11741 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
11742 && !target_is_thumb)
11743 force_target_arm = true;
11744 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
11745 && target_is_thumb)
11746 force_target_thumb = true;
11749 off_t offset;
11750 Stub_type stub_type = arm_stub_none;
11752 // Check if we have an offending branch instruction.
11753 uint16_t upper_insn = (insn >> 16) & 0xffffU;
11754 uint16_t lower_insn = insn & 0xffffU;
11755 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
11757 if (cortex_a8_reloc != NULL
11758 && cortex_a8_reloc->reloc_stub() != NULL)
11759 // We've already made a stub for this instruction, e.g.
11760 // it's a long branch or a Thumb->ARM stub. Assume that
11761 // stub will suffice to work around the A8 erratum (see
11762 // setting of always_after_branch above).
11764 else if (is_bcc)
11766 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
11767 lower_insn);
11768 stub_type = arm_stub_a8_veneer_b_cond;
11770 else if (is_b || is_bl || is_blx)
11772 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
11773 lower_insn);
11774 if (is_blx)
11775 offset &= ~3;
11777 stub_type = (is_blx
11778 ? arm_stub_a8_veneer_blx
11779 : (is_bl
11780 ? arm_stub_a8_veneer_bl
11781 : arm_stub_a8_veneer_b));
11784 if (stub_type != arm_stub_none)
11786 Arm_address pc_for_insn = address + i + 4;
11788 // The original instruction is a BL, but the target is
11789 // an ARM instruction. If we were not making a stub,
11790 // the BL would have been converted to a BLX. Use the
11791 // BLX stub instead in that case.
11792 if (this->may_use_blx() && force_target_arm
11793 && stub_type == arm_stub_a8_veneer_bl)
11795 stub_type = arm_stub_a8_veneer_blx;
11796 is_blx = true;
11797 is_bl = false;
11799 // Conversely, if the original instruction was
11800 // BLX but the target is Thumb mode, use the BL stub.
11801 else if (force_target_thumb
11802 && stub_type == arm_stub_a8_veneer_blx)
11804 stub_type = arm_stub_a8_veneer_bl;
11805 is_blx = false;
11806 is_bl = true;
11809 if (is_blx)
11810 pc_for_insn &= ~3;
11812 // If we found a relocation, use the proper destination,
11813 // not the offset in the (unrelocated) instruction.
11814 // Note this is always done if we switched the stub type above.
11815 if (cortex_a8_reloc != NULL)
11816 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
11818 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
11820 // Add a new stub if destination address in in the same page.
11821 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
11823 Cortex_a8_stub* stub =
11824 this->stub_factory_.make_cortex_a8_stub(stub_type,
11825 arm_relobj, shndx,
11826 address + i,
11827 target, insn);
11828 Stub_table<big_endian>* stub_table =
11829 arm_relobj->stub_table(shndx);
11830 gold_assert(stub_table != NULL);
11831 stub_table->add_cortex_a8_stub(address + i, stub);
11836 i += insn_32bit ? 4 : 2;
11837 last_was_32bit = insn_32bit;
11838 last_was_branch = is_32bit_branch;
11842 // Apply the Cortex-A8 workaround.
11844 template<bool big_endian>
11845 void
11846 Target_arm<big_endian>::apply_cortex_a8_workaround(
11847 const Cortex_a8_stub* stub,
11848 Arm_address stub_address,
11849 unsigned char* insn_view,
11850 Arm_address insn_address)
11852 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
11853 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
11854 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
11855 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
11856 off_t branch_offset = stub_address - (insn_address + 4);
11858 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
11859 switch (stub->stub_template()->type())
11861 case arm_stub_a8_veneer_b_cond:
11862 // For a conditional branch, we re-write it to be an unconditional
11863 // branch to the stub. We use the THUMB-2 encoding here.
11864 upper_insn = 0xf000U;
11865 lower_insn = 0xb800U;
11866 // Fall through
11867 case arm_stub_a8_veneer_b:
11868 case arm_stub_a8_veneer_bl:
11869 case arm_stub_a8_veneer_blx:
11870 if ((lower_insn & 0x5000U) == 0x4000U)
11871 // For a BLX instruction, make sure that the relocation is
11872 // rounded up to a word boundary. This follows the semantics of
11873 // the instruction which specifies that bit 1 of the target
11874 // address will come from bit 1 of the base address.
11875 branch_offset = (branch_offset + 2) & ~3;
11877 // Put BRANCH_OFFSET back into the insn.
11878 gold_assert(!utils::has_overflow<25>(branch_offset));
11879 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
11880 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
11881 break;
11883 default:
11884 gold_unreachable();
11887 // Put the relocated value back in the object file:
11888 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
11889 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
11892 template<bool big_endian>
11893 class Target_selector_arm : public Target_selector
11895 public:
11896 Target_selector_arm()
11897 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
11898 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
11901 Target*
11902 do_instantiate_target()
11903 { return new Target_arm<big_endian>(); }
11906 // Fix .ARM.exidx section coverage.
11908 template<bool big_endian>
11909 void
11910 Target_arm<big_endian>::fix_exidx_coverage(
11911 Layout* layout,
11912 const Input_objects* input_objects,
11913 Arm_output_section<big_endian>* exidx_section,
11914 Symbol_table* symtab,
11915 const Task* task)
11917 // We need to look at all the input sections in output in ascending
11918 // order of of output address. We do that by building a sorted list
11919 // of output sections by addresses. Then we looks at the output sections
11920 // in order. The input sections in an output section are already sorted
11921 // by addresses within the output section.
11923 typedef std::set<Output_section*, output_section_address_less_than>
11924 Sorted_output_section_list;
11925 Sorted_output_section_list sorted_output_sections;
11927 // Find out all the output sections of input sections pointed by
11928 // EXIDX input sections.
11929 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
11930 p != input_objects->relobj_end();
11931 ++p)
11933 Arm_relobj<big_endian>* arm_relobj =
11934 Arm_relobj<big_endian>::as_arm_relobj(*p);
11935 std::vector<unsigned int> shndx_list;
11936 arm_relobj->get_exidx_shndx_list(&shndx_list);
11937 for (size_t i = 0; i < shndx_list.size(); ++i)
11939 const Arm_exidx_input_section* exidx_input_section =
11940 arm_relobj->exidx_input_section_by_shndx(shndx_list[i]);
11941 gold_assert(exidx_input_section != NULL);
11942 if (!exidx_input_section->has_errors())
11944 unsigned int text_shndx = exidx_input_section->link();
11945 Output_section* os = arm_relobj->output_section(text_shndx);
11946 if (os != NULL && (os->flags() & elfcpp::SHF_ALLOC) != 0)
11947 sorted_output_sections.insert(os);
11952 // Go over the output sections in ascending order of output addresses.
11953 typedef typename Arm_output_section<big_endian>::Text_section_list
11954 Text_section_list;
11955 Text_section_list sorted_text_sections;
11956 for (typename Sorted_output_section_list::iterator p =
11957 sorted_output_sections.begin();
11958 p != sorted_output_sections.end();
11959 ++p)
11961 Arm_output_section<big_endian>* arm_output_section =
11962 Arm_output_section<big_endian>::as_arm_output_section(*p);
11963 arm_output_section->append_text_sections_to_list(&sorted_text_sections);
11966 exidx_section->fix_exidx_coverage(layout, sorted_text_sections, symtab,
11967 merge_exidx_entries(), task);
11970 Target_selector_arm<false> target_selector_arm;
11971 Target_selector_arm<true> target_selector_armbe;
11973 } // End anonymous namespace.