Properly demangle a global constructor symbol.
[binutils.git] / gold / arm.cc
blob183bc30248101d35c073c985373ca5501ec34766
1 // arm.cc -- arm target support for gold.
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
7 // bfd/elf32-arm.c.
9 // This file is part of gold.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
26 #include "gold.h"
28 #include <cstring>
29 #include <limits>
30 #include <cstdio>
31 #include <string>
32 #include <algorithm>
33 #include <map>
34 #include <utility>
35 #include <set>
37 #include "elfcpp.h"
38 #include "parameters.h"
39 #include "reloc.h"
40 #include "arm.h"
41 #include "object.h"
42 #include "symtab.h"
43 #include "layout.h"
44 #include "output.h"
45 #include "copy-relocs.h"
46 #include "target.h"
47 #include "target-reloc.h"
48 #include "target-select.h"
49 #include "tls.h"
50 #include "defstd.h"
51 #include "gc.h"
52 #include "attributes.h"
53 #include "arm-reloc-property.h"
55 namespace
58 using namespace gold;
60 template<bool big_endian>
61 class Output_data_plt_arm;
63 template<bool big_endian>
64 class Stub_table;
66 template<bool big_endian>
67 class Arm_input_section;
69 class Arm_exidx_cantunwind;
71 class Arm_exidx_merged_section;
73 class Arm_exidx_fixup;
75 template<bool big_endian>
76 class Arm_output_section;
78 class Arm_exidx_input_section;
80 template<bool big_endian>
81 class Arm_relobj;
83 template<bool big_endian>
84 class Arm_relocate_functions;
86 template<bool big_endian>
87 class Arm_output_data_got;
89 template<bool big_endian>
90 class Target_arm;
92 // For convenience.
93 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
95 // Maximum branch offsets for ARM, THUMB and THUMB2.
96 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
97 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
98 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
99 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
100 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
101 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
103 // Thread Control Block size.
104 const size_t ARM_TCB_SIZE = 8;
106 // The arm target class.
108 // This is a very simple port of gold for ARM-EABI. It is intended for
109 // supporting Android only for the time being.
111 // TODOs:
112 // - Implement all static relocation types documented in arm-reloc.def.
113 // - Make PLTs more flexible for different architecture features like
114 // Thumb-2 and BE8.
115 // There are probably a lot more.
117 // Ideally we would like to avoid using global variables but this is used
118 // very in many places and sometimes in loops. If we use a function
119 // returning a static instance of Arm_reloc_property_table, it will very
120 // slow in an threaded environment since the static instance needs to be
121 // locked. The pointer is below initialized in the
122 // Target::do_select_as_default_target() hook so that we do not spend time
123 // building the table if we are not linking ARM objects.
125 // An alternative is to to process the information in arm-reloc.def in
126 // compilation time and generate a representation of it in PODs only. That
127 // way we can avoid initialization when the linker starts.
129 Arm_reloc_property_table* arm_reloc_property_table = NULL;
131 // Instruction template class. This class is similar to the insn_sequence
132 // struct in bfd/elf32-arm.c.
134 class Insn_template
136 public:
137 // Types of instruction templates.
138 enum Type
140 THUMB16_TYPE = 1,
141 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
142 // templates with class-specific semantics. Currently this is used
143 // only by the Cortex_a8_stub class for handling condition codes in
144 // conditional branches.
145 THUMB16_SPECIAL_TYPE,
146 THUMB32_TYPE,
147 ARM_TYPE,
148 DATA_TYPE
151 // Factory methods to create instruction templates in different formats.
153 static const Insn_template
154 thumb16_insn(uint32_t data)
155 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
157 // A Thumb conditional branch, in which the proper condition is inserted
158 // when we build the stub.
159 static const Insn_template
160 thumb16_bcond_insn(uint32_t data)
161 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
163 static const Insn_template
164 thumb32_insn(uint32_t data)
165 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
167 static const Insn_template
168 thumb32_b_insn(uint32_t data, int reloc_addend)
170 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
171 reloc_addend);
174 static const Insn_template
175 arm_insn(uint32_t data)
176 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
178 static const Insn_template
179 arm_rel_insn(unsigned data, int reloc_addend)
180 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
182 static const Insn_template
183 data_word(unsigned data, unsigned int r_type, int reloc_addend)
184 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
186 // Accessors. This class is used for read-only objects so no modifiers
187 // are provided.
189 uint32_t
190 data() const
191 { return this->data_; }
193 // Return the instruction sequence type of this.
194 Type
195 type() const
196 { return this->type_; }
198 // Return the ARM relocation type of this.
199 unsigned int
200 r_type() const
201 { return this->r_type_; }
203 int32_t
204 reloc_addend() const
205 { return this->reloc_addend_; }
207 // Return size of instruction template in bytes.
208 size_t
209 size() const;
211 // Return byte-alignment of instruction template.
212 unsigned
213 alignment() const;
215 private:
216 // We make the constructor private to ensure that only the factory
217 // methods are used.
218 inline
219 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
220 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
223 // Instruction specific data. This is used to store information like
224 // some of the instruction bits.
225 uint32_t data_;
226 // Instruction template type.
227 Type type_;
228 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
229 unsigned int r_type_;
230 // Relocation addend.
231 int32_t reloc_addend_;
234 // Macro for generating code to stub types. One entry per long/short
235 // branch stub
237 #define DEF_STUBS \
238 DEF_STUB(long_branch_any_any) \
239 DEF_STUB(long_branch_v4t_arm_thumb) \
240 DEF_STUB(long_branch_thumb_only) \
241 DEF_STUB(long_branch_v4t_thumb_thumb) \
242 DEF_STUB(long_branch_v4t_thumb_arm) \
243 DEF_STUB(short_branch_v4t_thumb_arm) \
244 DEF_STUB(long_branch_any_arm_pic) \
245 DEF_STUB(long_branch_any_thumb_pic) \
246 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
247 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
248 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
249 DEF_STUB(long_branch_thumb_only_pic) \
250 DEF_STUB(a8_veneer_b_cond) \
251 DEF_STUB(a8_veneer_b) \
252 DEF_STUB(a8_veneer_bl) \
253 DEF_STUB(a8_veneer_blx) \
254 DEF_STUB(v4_veneer_bx)
256 // Stub types.
258 #define DEF_STUB(x) arm_stub_##x,
259 typedef enum
261 arm_stub_none,
262 DEF_STUBS
264 // First reloc stub type.
265 arm_stub_reloc_first = arm_stub_long_branch_any_any,
266 // Last reloc stub type.
267 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
269 // First Cortex-A8 stub type.
270 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
271 // Last Cortex-A8 stub type.
272 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
274 // Last stub type.
275 arm_stub_type_last = arm_stub_v4_veneer_bx
276 } Stub_type;
277 #undef DEF_STUB
279 // Stub template class. Templates are meant to be read-only objects.
280 // A stub template for a stub type contains all read-only attributes
281 // common to all stubs of the same type.
283 class Stub_template
285 public:
286 Stub_template(Stub_type, const Insn_template*, size_t);
288 ~Stub_template()
291 // Return stub type.
292 Stub_type
293 type() const
294 { return this->type_; }
296 // Return an array of instruction templates.
297 const Insn_template*
298 insns() const
299 { return this->insns_; }
301 // Return size of template in number of instructions.
302 size_t
303 insn_count() const
304 { return this->insn_count_; }
306 // Return size of template in bytes.
307 size_t
308 size() const
309 { return this->size_; }
311 // Return alignment of the stub template.
312 unsigned
313 alignment() const
314 { return this->alignment_; }
316 // Return whether entry point is in thumb mode.
317 bool
318 entry_in_thumb_mode() const
319 { return this->entry_in_thumb_mode_; }
321 // Return number of relocations in this template.
322 size_t
323 reloc_count() const
324 { return this->relocs_.size(); }
326 // Return index of the I-th instruction with relocation.
327 size_t
328 reloc_insn_index(size_t i) const
330 gold_assert(i < this->relocs_.size());
331 return this->relocs_[i].first;
334 // Return the offset of the I-th instruction with relocation from the
335 // beginning of the stub.
336 section_size_type
337 reloc_offset(size_t i) const
339 gold_assert(i < this->relocs_.size());
340 return this->relocs_[i].second;
343 private:
344 // This contains information about an instruction template with a relocation
345 // and its offset from start of stub.
346 typedef std::pair<size_t, section_size_type> Reloc;
348 // A Stub_template may not be copied. We want to share templates as much
349 // as possible.
350 Stub_template(const Stub_template&);
351 Stub_template& operator=(const Stub_template&);
353 // Stub type.
354 Stub_type type_;
355 // Points to an array of Insn_templates.
356 const Insn_template* insns_;
357 // Number of Insn_templates in insns_[].
358 size_t insn_count_;
359 // Size of templated instructions in bytes.
360 size_t size_;
361 // Alignment of templated instructions.
362 unsigned alignment_;
363 // Flag to indicate if entry is in thumb mode.
364 bool entry_in_thumb_mode_;
365 // A table of reloc instruction indices and offsets. We can find these by
366 // looking at the instruction templates but we pre-compute and then stash
367 // them here for speed.
368 std::vector<Reloc> relocs_;
372 // A class for code stubs. This is a base class for different type of
373 // stubs used in the ARM target.
376 class Stub
378 private:
379 static const section_offset_type invalid_offset =
380 static_cast<section_offset_type>(-1);
382 public:
383 Stub(const Stub_template* stub_template)
384 : stub_template_(stub_template), offset_(invalid_offset)
387 virtual
388 ~Stub()
391 // Return the stub template.
392 const Stub_template*
393 stub_template() const
394 { return this->stub_template_; }
396 // Return offset of code stub from beginning of its containing stub table.
397 section_offset_type
398 offset() const
400 gold_assert(this->offset_ != invalid_offset);
401 return this->offset_;
404 // Set offset of code stub from beginning of its containing stub table.
405 void
406 set_offset(section_offset_type offset)
407 { this->offset_ = offset; }
409 // Return the relocation target address of the i-th relocation in the
410 // stub. This must be defined in a child class.
411 Arm_address
412 reloc_target(size_t i)
413 { return this->do_reloc_target(i); }
415 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
416 void
417 write(unsigned char* view, section_size_type view_size, bool big_endian)
418 { this->do_write(view, view_size, big_endian); }
420 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
421 // for the i-th instruction.
422 uint16_t
423 thumb16_special(size_t i)
424 { return this->do_thumb16_special(i); }
426 protected:
427 // This must be defined in the child class.
428 virtual Arm_address
429 do_reloc_target(size_t) = 0;
431 // This may be overridden in the child class.
432 virtual void
433 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
435 if (big_endian)
436 this->do_fixed_endian_write<true>(view, view_size);
437 else
438 this->do_fixed_endian_write<false>(view, view_size);
441 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
442 // instruction template.
443 virtual uint16_t
444 do_thumb16_special(size_t)
445 { gold_unreachable(); }
447 private:
448 // A template to implement do_write.
449 template<bool big_endian>
450 void inline
451 do_fixed_endian_write(unsigned char*, section_size_type);
453 // Its template.
454 const Stub_template* stub_template_;
455 // Offset within the section of containing this stub.
456 section_offset_type offset_;
459 // Reloc stub class. These are stubs we use to fix up relocation because
460 // of limited branch ranges.
462 class Reloc_stub : public Stub
464 public:
465 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
466 // We assume we never jump to this address.
467 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
469 // Return destination address.
470 Arm_address
471 destination_address() const
473 gold_assert(this->destination_address_ != this->invalid_address);
474 return this->destination_address_;
477 // Set destination address.
478 void
479 set_destination_address(Arm_address address)
481 gold_assert(address != this->invalid_address);
482 this->destination_address_ = address;
485 // Reset destination address.
486 void
487 reset_destination_address()
488 { this->destination_address_ = this->invalid_address; }
490 // Determine stub type for a branch of a relocation of R_TYPE going
491 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
492 // the branch target is a thumb instruction. TARGET is used for look
493 // up ARM-specific linker settings.
494 static Stub_type
495 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
496 Arm_address branch_target, bool target_is_thumb);
498 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
499 // and an addend. Since we treat global and local symbol differently, we
500 // use a Symbol object for a global symbol and a object-index pair for
501 // a local symbol.
502 class Key
504 public:
505 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
506 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
507 // and R_SYM must not be invalid_index.
508 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
509 unsigned int r_sym, int32_t addend)
510 : stub_type_(stub_type), addend_(addend)
512 if (symbol != NULL)
514 this->r_sym_ = Reloc_stub::invalid_index;
515 this->u_.symbol = symbol;
517 else
519 gold_assert(relobj != NULL && r_sym != invalid_index);
520 this->r_sym_ = r_sym;
521 this->u_.relobj = relobj;
525 ~Key()
528 // Accessors: Keys are meant to be read-only object so no modifiers are
529 // provided.
531 // Return stub type.
532 Stub_type
533 stub_type() const
534 { return this->stub_type_; }
536 // Return the local symbol index or invalid_index.
537 unsigned int
538 r_sym() const
539 { return this->r_sym_; }
541 // Return the symbol if there is one.
542 const Symbol*
543 symbol() const
544 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
546 // Return the relobj if there is one.
547 const Relobj*
548 relobj() const
549 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
551 // Whether this equals to another key k.
552 bool
553 eq(const Key& k) const
555 return ((this->stub_type_ == k.stub_type_)
556 && (this->r_sym_ == k.r_sym_)
557 && ((this->r_sym_ != Reloc_stub::invalid_index)
558 ? (this->u_.relobj == k.u_.relobj)
559 : (this->u_.symbol == k.u_.symbol))
560 && (this->addend_ == k.addend_));
563 // Return a hash value.
564 size_t
565 hash_value() const
567 return (this->stub_type_
568 ^ this->r_sym_
569 ^ gold::string_hash<char>(
570 (this->r_sym_ != Reloc_stub::invalid_index)
571 ? this->u_.relobj->name().c_str()
572 : this->u_.symbol->name())
573 ^ this->addend_);
576 // Functors for STL associative containers.
577 struct hash
579 size_t
580 operator()(const Key& k) const
581 { return k.hash_value(); }
584 struct equal_to
586 bool
587 operator()(const Key& k1, const Key& k2) const
588 { return k1.eq(k2); }
591 // Name of key. This is mainly for debugging.
592 std::string
593 name() const;
595 private:
596 // Stub type.
597 Stub_type stub_type_;
598 // If this is a local symbol, this is the index in the defining object.
599 // Otherwise, it is invalid_index for a global symbol.
600 unsigned int r_sym_;
601 // If r_sym_ is invalid index. This points to a global symbol.
602 // Otherwise, this points a relobj. We used the unsized and target
603 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
604 // Arm_relobj. This is done to avoid making the stub class a template
605 // as most of the stub machinery is endianness-neutral. However, it
606 // may require a bit of casting done by users of this class.
607 union
609 const Symbol* symbol;
610 const Relobj* relobj;
611 } u_;
612 // Addend associated with a reloc.
613 int32_t addend_;
616 protected:
617 // Reloc_stubs are created via a stub factory. So these are protected.
618 Reloc_stub(const Stub_template* stub_template)
619 : Stub(stub_template), destination_address_(invalid_address)
622 ~Reloc_stub()
625 friend class Stub_factory;
627 // Return the relocation target address of the i-th relocation in the
628 // stub.
629 Arm_address
630 do_reloc_target(size_t i)
632 // All reloc stub have only one relocation.
633 gold_assert(i == 0);
634 return this->destination_address_;
637 private:
638 // Address of destination.
639 Arm_address destination_address_;
642 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
643 // THUMB branch that meets the following conditions:
645 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
646 // branch address is 0xffe.
647 // 2. The branch target address is in the same page as the first word of the
648 // branch.
649 // 3. The branch follows a 32-bit instruction which is not a branch.
651 // To do the fix up, we need to store the address of the branch instruction
652 // and its target at least. We also need to store the original branch
653 // instruction bits for the condition code in a conditional branch. The
654 // condition code is used in a special instruction template. We also want
655 // to identify input sections needing Cortex-A8 workaround quickly. We store
656 // extra information about object and section index of the code section
657 // containing a branch being fixed up. The information is used to mark
658 // the code section when we finalize the Cortex-A8 stubs.
661 class Cortex_a8_stub : public Stub
663 public:
664 ~Cortex_a8_stub()
667 // Return the object of the code section containing the branch being fixed
668 // up.
669 Relobj*
670 relobj() const
671 { return this->relobj_; }
673 // Return the section index of the code section containing the branch being
674 // fixed up.
675 unsigned int
676 shndx() const
677 { return this->shndx_; }
679 // Return the source address of stub. This is the address of the original
680 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
681 // instruction.
682 Arm_address
683 source_address() const
684 { return this->source_address_; }
686 // Return the destination address of the stub. This is the branch taken
687 // address of the original branch instruction. LSB is 1 if it is a THUMB
688 // instruction address.
689 Arm_address
690 destination_address() const
691 { return this->destination_address_; }
693 // Return the instruction being fixed up.
694 uint32_t
695 original_insn() const
696 { return this->original_insn_; }
698 protected:
699 // Cortex_a8_stubs are created via a stub factory. So these are protected.
700 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
701 unsigned int shndx, Arm_address source_address,
702 Arm_address destination_address, uint32_t original_insn)
703 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
704 source_address_(source_address | 1U),
705 destination_address_(destination_address),
706 original_insn_(original_insn)
709 friend class Stub_factory;
711 // Return the relocation target address of the i-th relocation in the
712 // stub.
713 Arm_address
714 do_reloc_target(size_t i)
716 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
718 // The conditional branch veneer has two relocations.
719 gold_assert(i < 2);
720 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
722 else
724 // All other Cortex-A8 stubs have only one relocation.
725 gold_assert(i == 0);
726 return this->destination_address_;
730 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
731 uint16_t
732 do_thumb16_special(size_t);
734 private:
735 // Object of the code section containing the branch being fixed up.
736 Relobj* relobj_;
737 // Section index of the code section containing the branch begin fixed up.
738 unsigned int shndx_;
739 // Source address of original branch.
740 Arm_address source_address_;
741 // Destination address of the original branch.
742 Arm_address destination_address_;
743 // Original branch instruction. This is needed for copying the condition
744 // code from a condition branch to its stub.
745 uint32_t original_insn_;
748 // ARMv4 BX Rx branch relocation stub class.
749 class Arm_v4bx_stub : public Stub
751 public:
752 ~Arm_v4bx_stub()
755 // Return the associated register.
756 uint32_t
757 reg() const
758 { return this->reg_; }
760 protected:
761 // Arm V4BX stubs are created via a stub factory. So these are protected.
762 Arm_v4bx_stub(const Stub_template* stub_template, const uint32_t reg)
763 : Stub(stub_template), reg_(reg)
766 friend class Stub_factory;
768 // Return the relocation target address of the i-th relocation in the
769 // stub.
770 Arm_address
771 do_reloc_target(size_t)
772 { gold_unreachable(); }
774 // This may be overridden in the child class.
775 virtual void
776 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
778 if (big_endian)
779 this->do_fixed_endian_v4bx_write<true>(view, view_size);
780 else
781 this->do_fixed_endian_v4bx_write<false>(view, view_size);
784 private:
785 // A template to implement do_write.
786 template<bool big_endian>
787 void inline
788 do_fixed_endian_v4bx_write(unsigned char* view, section_size_type)
790 const Insn_template* insns = this->stub_template()->insns();
791 elfcpp::Swap<32, big_endian>::writeval(view,
792 (insns[0].data()
793 + (this->reg_ << 16)));
794 view += insns[0].size();
795 elfcpp::Swap<32, big_endian>::writeval(view,
796 (insns[1].data() + this->reg_));
797 view += insns[1].size();
798 elfcpp::Swap<32, big_endian>::writeval(view,
799 (insns[2].data() + this->reg_));
802 // A register index (r0-r14), which is associated with the stub.
803 uint32_t reg_;
806 // Stub factory class.
808 class Stub_factory
810 public:
811 // Return the unique instance of this class.
812 static const Stub_factory&
813 get_instance()
815 static Stub_factory singleton;
816 return singleton;
819 // Make a relocation stub.
820 Reloc_stub*
821 make_reloc_stub(Stub_type stub_type) const
823 gold_assert(stub_type >= arm_stub_reloc_first
824 && stub_type <= arm_stub_reloc_last);
825 return new Reloc_stub(this->stub_templates_[stub_type]);
828 // Make a Cortex-A8 stub.
829 Cortex_a8_stub*
830 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
831 Arm_address source, Arm_address destination,
832 uint32_t original_insn) const
834 gold_assert(stub_type >= arm_stub_cortex_a8_first
835 && stub_type <= arm_stub_cortex_a8_last);
836 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
837 source, destination, original_insn);
840 // Make an ARM V4BX relocation stub.
841 // This method creates a stub from the arm_stub_v4_veneer_bx template only.
842 Arm_v4bx_stub*
843 make_arm_v4bx_stub(uint32_t reg) const
845 gold_assert(reg < 0xf);
846 return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
847 reg);
850 private:
851 // Constructor and destructor are protected since we only return a single
852 // instance created in Stub_factory::get_instance().
854 Stub_factory();
856 // A Stub_factory may not be copied since it is a singleton.
857 Stub_factory(const Stub_factory&);
858 Stub_factory& operator=(Stub_factory&);
860 // Stub templates. These are initialized in the constructor.
861 const Stub_template* stub_templates_[arm_stub_type_last+1];
864 // A class to hold stubs for the ARM target.
866 template<bool big_endian>
867 class Stub_table : public Output_data
869 public:
870 Stub_table(Arm_input_section<big_endian>* owner)
871 : Output_data(), owner_(owner), reloc_stubs_(), reloc_stubs_size_(0),
872 reloc_stubs_addralign_(1), cortex_a8_stubs_(), arm_v4bx_stubs_(0xf),
873 prev_data_size_(0), prev_addralign_(1)
876 ~Stub_table()
879 // Owner of this stub table.
880 Arm_input_section<big_endian>*
881 owner() const
882 { return this->owner_; }
884 // Whether this stub table is empty.
885 bool
886 empty() const
888 return (this->reloc_stubs_.empty()
889 && this->cortex_a8_stubs_.empty()
890 && this->arm_v4bx_stubs_.empty());
893 // Return the current data size.
894 off_t
895 current_data_size() const
896 { return this->current_data_size_for_child(); }
898 // Add a STUB with using KEY. Caller is reponsible for avoid adding
899 // if already a STUB with the same key has been added.
900 void
901 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
903 const Stub_template* stub_template = stub->stub_template();
904 gold_assert(stub_template->type() == key.stub_type());
905 this->reloc_stubs_[key] = stub;
907 // Assign stub offset early. We can do this because we never remove
908 // reloc stubs and they are in the beginning of the stub table.
909 uint64_t align = stub_template->alignment();
910 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_, align);
911 stub->set_offset(this->reloc_stubs_size_);
912 this->reloc_stubs_size_ += stub_template->size();
913 this->reloc_stubs_addralign_ =
914 std::max(this->reloc_stubs_addralign_, align);
917 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
918 // Caller is reponsible for avoid adding if already a STUB with the same
919 // address has been added.
920 void
921 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
923 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
924 this->cortex_a8_stubs_.insert(value);
927 // Add an ARM V4BX relocation stub. A register index will be retrieved
928 // from the stub.
929 void
930 add_arm_v4bx_stub(Arm_v4bx_stub* stub)
932 gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
933 this->arm_v4bx_stubs_[stub->reg()] = stub;
936 // Remove all Cortex-A8 stubs.
937 void
938 remove_all_cortex_a8_stubs();
940 // Look up a relocation stub using KEY. Return NULL if there is none.
941 Reloc_stub*
942 find_reloc_stub(const Reloc_stub::Key& key) const
944 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
945 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
948 // Look up an arm v4bx relocation stub using the register index.
949 // Return NULL if there is none.
950 Arm_v4bx_stub*
951 find_arm_v4bx_stub(const uint32_t reg) const
953 gold_assert(reg < 0xf);
954 return this->arm_v4bx_stubs_[reg];
957 // Relocate stubs in this stub table.
958 void
959 relocate_stubs(const Relocate_info<32, big_endian>*,
960 Target_arm<big_endian>*, Output_section*,
961 unsigned char*, Arm_address, section_size_type);
963 // Update data size and alignment at the end of a relaxation pass. Return
964 // true if either data size or alignment is different from that of the
965 // previous relaxation pass.
966 bool
967 update_data_size_and_addralign();
969 // Finalize stubs. Set the offsets of all stubs and mark input sections
970 // needing the Cortex-A8 workaround.
971 void
972 finalize_stubs();
974 // Apply Cortex-A8 workaround to an address range.
975 void
976 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
977 unsigned char*, Arm_address,
978 section_size_type);
980 protected:
981 // Write out section contents.
982 void
983 do_write(Output_file*);
985 // Return the required alignment.
986 uint64_t
987 do_addralign() const
988 { return this->prev_addralign_; }
990 // Reset address and file offset.
991 void
992 do_reset_address_and_file_offset()
993 { this->set_current_data_size_for_child(this->prev_data_size_); }
995 // Set final data size.
996 void
997 set_final_data_size()
998 { this->set_data_size(this->current_data_size()); }
1000 private:
1001 // Relocate one stub.
1002 void
1003 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1004 Target_arm<big_endian>*, Output_section*,
1005 unsigned char*, Arm_address, section_size_type);
1007 // Unordered map of relocation stubs.
1008 typedef
1009 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
1010 Reloc_stub::Key::equal_to>
1011 Reloc_stub_map;
1013 // List of Cortex-A8 stubs ordered by addresses of branches being
1014 // fixed up in output.
1015 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
1016 // List of Arm V4BX relocation stubs ordered by associated registers.
1017 typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
1019 // Owner of this stub table.
1020 Arm_input_section<big_endian>* owner_;
1021 // The relocation stubs.
1022 Reloc_stub_map reloc_stubs_;
1023 // Size of reloc stubs.
1024 off_t reloc_stubs_size_;
1025 // Maximum address alignment of reloc stubs.
1026 uint64_t reloc_stubs_addralign_;
1027 // The cortex_a8_stubs.
1028 Cortex_a8_stub_list cortex_a8_stubs_;
1029 // The Arm V4BX relocation stubs.
1030 Arm_v4bx_stub_list arm_v4bx_stubs_;
1031 // data size of this in the previous pass.
1032 off_t prev_data_size_;
1033 // address alignment of this in the previous pass.
1034 uint64_t prev_addralign_;
1037 // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
1038 // we add to the end of an EXIDX input section that goes into the output.
1040 class Arm_exidx_cantunwind : public Output_section_data
1042 public:
1043 Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
1044 : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
1047 // Return the object containing the section pointed by this.
1048 Relobj*
1049 relobj() const
1050 { return this->relobj_; }
1052 // Return the section index of the section pointed by this.
1053 unsigned int
1054 shndx() const
1055 { return this->shndx_; }
1057 protected:
1058 void
1059 do_write(Output_file* of)
1061 if (parameters->target().is_big_endian())
1062 this->do_fixed_endian_write<true>(of);
1063 else
1064 this->do_fixed_endian_write<false>(of);
1067 // 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 // reponsible 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 reloction 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 incontigious
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 oveflow.
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 // Caller is reponsible 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 liitle 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 // Removel 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. Othewise, 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 seens 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 targer 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 // Intialize 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 the the first
6532 // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6533 // must be linked to 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 currenlty 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 implict
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_PREL31:
7650 case elfcpp::R_ARM_SBREL31:
7651 return Symbol::RELATIVE_REF;
7653 case elfcpp::R_ARM_PLT32:
7654 case elfcpp::R_ARM_CALL:
7655 case elfcpp::R_ARM_JUMP24:
7656 case elfcpp::R_ARM_THM_CALL:
7657 case elfcpp::R_ARM_THM_JUMP24:
7658 case elfcpp::R_ARM_THM_JUMP19:
7659 case elfcpp::R_ARM_THM_JUMP6:
7660 case elfcpp::R_ARM_THM_JUMP11:
7661 case elfcpp::R_ARM_THM_JUMP8:
7662 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7664 case elfcpp::R_ARM_GOT_BREL:
7665 case elfcpp::R_ARM_GOT_ABS:
7666 case elfcpp::R_ARM_GOT_PREL:
7667 // Absolute in GOT.
7668 return Symbol::ABSOLUTE_REF;
7670 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7671 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7672 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7673 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7674 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7675 return Symbol::TLS_REF;
7677 case elfcpp::R_ARM_TARGET1:
7678 case elfcpp::R_ARM_TARGET2:
7679 case elfcpp::R_ARM_COPY:
7680 case elfcpp::R_ARM_GLOB_DAT:
7681 case elfcpp::R_ARM_JUMP_SLOT:
7682 case elfcpp::R_ARM_RELATIVE:
7683 case elfcpp::R_ARM_PC24:
7684 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7685 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7686 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7687 default:
7688 // Not expected. We will give an error later.
7689 return 0;
7693 // Report an unsupported relocation against a local symbol.
7695 template<bool big_endian>
7696 void
7697 Target_arm<big_endian>::Scan::unsupported_reloc_local(
7698 Sized_relobj<32, big_endian>* object,
7699 unsigned int r_type)
7701 gold_error(_("%s: unsupported reloc %u against local symbol"),
7702 object->name().c_str(), r_type);
7705 // We are about to emit a dynamic relocation of type R_TYPE. If the
7706 // dynamic linker does not support it, issue an error. The GNU linker
7707 // only issues a non-PIC error for an allocated read-only section.
7708 // Here we know the section is allocated, but we don't know that it is
7709 // read-only. But we check for all the relocation types which the
7710 // glibc dynamic linker supports, so it seems appropriate to issue an
7711 // error even if the section is not read-only.
7713 template<bool big_endian>
7714 void
7715 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
7716 unsigned int r_type)
7718 switch (r_type)
7720 // These are the relocation types supported by glibc for ARM.
7721 case elfcpp::R_ARM_RELATIVE:
7722 case elfcpp::R_ARM_COPY:
7723 case elfcpp::R_ARM_GLOB_DAT:
7724 case elfcpp::R_ARM_JUMP_SLOT:
7725 case elfcpp::R_ARM_ABS32:
7726 case elfcpp::R_ARM_ABS32_NOI:
7727 case elfcpp::R_ARM_PC24:
7728 // FIXME: The following 3 types are not supported by Android's dynamic
7729 // linker.
7730 case elfcpp::R_ARM_TLS_DTPMOD32:
7731 case elfcpp::R_ARM_TLS_DTPOFF32:
7732 case elfcpp::R_ARM_TLS_TPOFF32:
7733 return;
7735 default:
7737 // This prevents us from issuing more than one error per reloc
7738 // section. But we can still wind up issuing more than one
7739 // error per object file.
7740 if (this->issued_non_pic_error_)
7741 return;
7742 const Arm_reloc_property* reloc_property =
7743 arm_reloc_property_table->get_reloc_property(r_type);
7744 gold_assert(reloc_property != NULL);
7745 object->error(_("requires unsupported dynamic reloc %s; "
7746 "recompile with -fPIC"),
7747 reloc_property->name().c_str());
7748 this->issued_non_pic_error_ = true;
7749 return;
7752 case elfcpp::R_ARM_NONE:
7753 gold_unreachable();
7757 // Scan a relocation for a local symbol.
7758 // FIXME: This only handles a subset of relocation types used by Android
7759 // on ARM v5te devices.
7761 template<bool big_endian>
7762 inline void
7763 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
7764 Layout* layout,
7765 Target_arm* target,
7766 Sized_relobj<32, big_endian>* object,
7767 unsigned int data_shndx,
7768 Output_section* output_section,
7769 const elfcpp::Rel<32, big_endian>& reloc,
7770 unsigned int r_type,
7771 const elfcpp::Sym<32, big_endian>& lsym)
7773 r_type = get_real_reloc_type(r_type);
7774 switch (r_type)
7776 case elfcpp::R_ARM_NONE:
7777 case elfcpp::R_ARM_V4BX:
7778 case elfcpp::R_ARM_GNU_VTENTRY:
7779 case elfcpp::R_ARM_GNU_VTINHERIT:
7780 break;
7782 case elfcpp::R_ARM_ABS32:
7783 case elfcpp::R_ARM_ABS32_NOI:
7784 // If building a shared library (or a position-independent
7785 // executable), we need to create a dynamic relocation for
7786 // this location. The relocation applied at link time will
7787 // apply the link-time value, so we flag the location with
7788 // an R_ARM_RELATIVE relocation so the dynamic loader can
7789 // relocate it easily.
7790 if (parameters->options().output_is_position_independent())
7792 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7793 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7794 // If we are to add more other reloc types than R_ARM_ABS32,
7795 // we need to add check_non_pic(object, r_type) here.
7796 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
7797 output_section, data_shndx,
7798 reloc.get_r_offset());
7800 break;
7802 case elfcpp::R_ARM_ABS16:
7803 case elfcpp::R_ARM_ABS12:
7804 case elfcpp::R_ARM_THM_ABS5:
7805 case elfcpp::R_ARM_ABS8:
7806 case elfcpp::R_ARM_BASE_ABS:
7807 case elfcpp::R_ARM_MOVW_ABS_NC:
7808 case elfcpp::R_ARM_MOVT_ABS:
7809 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7810 case elfcpp::R_ARM_THM_MOVT_ABS:
7811 // If building a shared library (or a position-independent
7812 // executable), we need to create a dynamic relocation for
7813 // this location. Because the addend needs to remain in the
7814 // data section, we need to be careful not to apply this
7815 // relocation statically.
7816 if (parameters->options().output_is_position_independent())
7818 check_non_pic(object, r_type);
7819 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7820 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7821 if (lsym.get_st_type() != elfcpp::STT_SECTION)
7822 rel_dyn->add_local(object, r_sym, r_type, output_section,
7823 data_shndx, reloc.get_r_offset());
7824 else
7826 gold_assert(lsym.get_st_value() == 0);
7827 unsigned int shndx = lsym.get_st_shndx();
7828 bool is_ordinary;
7829 shndx = object->adjust_sym_shndx(r_sym, shndx,
7830 &is_ordinary);
7831 if (!is_ordinary)
7832 object->error(_("section symbol %u has bad shndx %u"),
7833 r_sym, shndx);
7834 else
7835 rel_dyn->add_local_section(object, shndx,
7836 r_type, output_section,
7837 data_shndx, reloc.get_r_offset());
7840 break;
7842 case elfcpp::R_ARM_REL32:
7843 case elfcpp::R_ARM_LDR_PC_G0:
7844 case elfcpp::R_ARM_SBREL32:
7845 case elfcpp::R_ARM_THM_CALL:
7846 case elfcpp::R_ARM_THM_PC8:
7847 case elfcpp::R_ARM_BASE_PREL:
7848 case elfcpp::R_ARM_PLT32:
7849 case elfcpp::R_ARM_CALL:
7850 case elfcpp::R_ARM_JUMP24:
7851 case elfcpp::R_ARM_THM_JUMP24:
7852 case elfcpp::R_ARM_SBREL31:
7853 case elfcpp::R_ARM_PREL31:
7854 case elfcpp::R_ARM_MOVW_PREL_NC:
7855 case elfcpp::R_ARM_MOVT_PREL:
7856 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7857 case elfcpp::R_ARM_THM_MOVT_PREL:
7858 case elfcpp::R_ARM_THM_JUMP19:
7859 case elfcpp::R_ARM_THM_JUMP6:
7860 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7861 case elfcpp::R_ARM_THM_PC12:
7862 case elfcpp::R_ARM_REL32_NOI:
7863 case elfcpp::R_ARM_ALU_PC_G0_NC:
7864 case elfcpp::R_ARM_ALU_PC_G0:
7865 case elfcpp::R_ARM_ALU_PC_G1_NC:
7866 case elfcpp::R_ARM_ALU_PC_G1:
7867 case elfcpp::R_ARM_ALU_PC_G2:
7868 case elfcpp::R_ARM_LDR_PC_G1:
7869 case elfcpp::R_ARM_LDR_PC_G2:
7870 case elfcpp::R_ARM_LDRS_PC_G0:
7871 case elfcpp::R_ARM_LDRS_PC_G1:
7872 case elfcpp::R_ARM_LDRS_PC_G2:
7873 case elfcpp::R_ARM_LDC_PC_G0:
7874 case elfcpp::R_ARM_LDC_PC_G1:
7875 case elfcpp::R_ARM_LDC_PC_G2:
7876 case elfcpp::R_ARM_ALU_SB_G0_NC:
7877 case elfcpp::R_ARM_ALU_SB_G0:
7878 case elfcpp::R_ARM_ALU_SB_G1_NC:
7879 case elfcpp::R_ARM_ALU_SB_G1:
7880 case elfcpp::R_ARM_ALU_SB_G2:
7881 case elfcpp::R_ARM_LDR_SB_G0:
7882 case elfcpp::R_ARM_LDR_SB_G1:
7883 case elfcpp::R_ARM_LDR_SB_G2:
7884 case elfcpp::R_ARM_LDRS_SB_G0:
7885 case elfcpp::R_ARM_LDRS_SB_G1:
7886 case elfcpp::R_ARM_LDRS_SB_G2:
7887 case elfcpp::R_ARM_LDC_SB_G0:
7888 case elfcpp::R_ARM_LDC_SB_G1:
7889 case elfcpp::R_ARM_LDC_SB_G2:
7890 case elfcpp::R_ARM_MOVW_BREL_NC:
7891 case elfcpp::R_ARM_MOVT_BREL:
7892 case elfcpp::R_ARM_MOVW_BREL:
7893 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7894 case elfcpp::R_ARM_THM_MOVT_BREL:
7895 case elfcpp::R_ARM_THM_MOVW_BREL:
7896 case elfcpp::R_ARM_THM_JUMP11:
7897 case elfcpp::R_ARM_THM_JUMP8:
7898 // We don't need to do anything for a relative addressing relocation
7899 // against a local symbol if it does not reference the GOT.
7900 break;
7902 case elfcpp::R_ARM_GOTOFF32:
7903 case elfcpp::R_ARM_GOTOFF12:
7904 // We need a GOT section:
7905 target->got_section(symtab, layout);
7906 break;
7908 case elfcpp::R_ARM_GOT_BREL:
7909 case elfcpp::R_ARM_GOT_PREL:
7911 // The symbol requires a GOT entry.
7912 Arm_output_data_got<big_endian>* got =
7913 target->got_section(symtab, layout);
7914 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7915 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
7917 // If we are generating a shared object, we need to add a
7918 // dynamic RELATIVE relocation for this symbol's GOT entry.
7919 if (parameters->options().output_is_position_independent())
7921 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7922 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7923 rel_dyn->add_local_relative(
7924 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
7925 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
7929 break;
7931 case elfcpp::R_ARM_TARGET1:
7932 case elfcpp::R_ARM_TARGET2:
7933 // This should have been mapped to another type already.
7934 // Fall through.
7935 case elfcpp::R_ARM_COPY:
7936 case elfcpp::R_ARM_GLOB_DAT:
7937 case elfcpp::R_ARM_JUMP_SLOT:
7938 case elfcpp::R_ARM_RELATIVE:
7939 // These are relocations which should only be seen by the
7940 // dynamic linker, and should never be seen here.
7941 gold_error(_("%s: unexpected reloc %u in object file"),
7942 object->name().c_str(), r_type);
7943 break;
7946 // These are initial TLS relocs, which are expected when
7947 // linking.
7948 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7949 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7950 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7951 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7952 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7954 bool output_is_shared = parameters->options().shared();
7955 const tls::Tls_optimization optimized_type
7956 = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
7957 r_type);
7958 switch (r_type)
7960 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7961 if (optimized_type == tls::TLSOPT_NONE)
7963 // Create a pair of GOT entries for the module index and
7964 // dtv-relative offset.
7965 Arm_output_data_got<big_endian>* got
7966 = target->got_section(symtab, layout);
7967 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7968 unsigned int shndx = lsym.get_st_shndx();
7969 bool is_ordinary;
7970 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7971 if (!is_ordinary)
7973 object->error(_("local symbol %u has bad shndx %u"),
7974 r_sym, shndx);
7975 break;
7978 if (!parameters->doing_static_link())
7979 got->add_local_pair_with_rel(object, r_sym, shndx,
7980 GOT_TYPE_TLS_PAIR,
7981 target->rel_dyn_section(layout),
7982 elfcpp::R_ARM_TLS_DTPMOD32, 0);
7983 else
7984 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR,
7985 object, r_sym);
7987 else
7988 // FIXME: TLS optimization not supported yet.
7989 gold_unreachable();
7990 break;
7992 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7993 if (optimized_type == tls::TLSOPT_NONE)
7995 // Create a GOT entry for the module index.
7996 target->got_mod_index_entry(symtab, layout, object);
7998 else
7999 // FIXME: TLS optimization not supported yet.
8000 gold_unreachable();
8001 break;
8003 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8004 break;
8006 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8007 layout->set_has_static_tls();
8008 if (optimized_type == tls::TLSOPT_NONE)
8010 // Create a GOT entry for the tp-relative offset.
8011 Arm_output_data_got<big_endian>* got
8012 = target->got_section(symtab, layout);
8013 unsigned int r_sym =
8014 elfcpp::elf_r_sym<32>(reloc.get_r_info());
8015 if (!parameters->doing_static_link())
8016 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
8017 target->rel_dyn_section(layout),
8018 elfcpp::R_ARM_TLS_TPOFF32);
8019 else if (!object->local_has_got_offset(r_sym,
8020 GOT_TYPE_TLS_OFFSET))
8022 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
8023 unsigned int got_offset =
8024 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
8025 got->add_static_reloc(got_offset,
8026 elfcpp::R_ARM_TLS_TPOFF32, object,
8027 r_sym);
8030 else
8031 // FIXME: TLS optimization not supported yet.
8032 gold_unreachable();
8033 break;
8035 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8036 layout->set_has_static_tls();
8037 if (output_is_shared)
8039 // We need to create a dynamic relocation.
8040 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
8041 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
8042 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8043 rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
8044 output_section, data_shndx,
8045 reloc.get_r_offset());
8047 break;
8049 default:
8050 gold_unreachable();
8053 break;
8055 case elfcpp::R_ARM_PC24:
8056 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
8057 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
8058 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
8059 default:
8060 unsupported_reloc_local(object, r_type);
8061 break;
8065 // Report an unsupported relocation against a global symbol.
8067 template<bool big_endian>
8068 void
8069 Target_arm<big_endian>::Scan::unsupported_reloc_global(
8070 Sized_relobj<32, big_endian>* object,
8071 unsigned int r_type,
8072 Symbol* gsym)
8074 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8075 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8078 template<bool big_endian>
8079 inline bool
8080 Target_arm<big_endian>::Scan::possible_function_pointer_reloc(
8081 unsigned int r_type)
8083 switch (r_type)
8085 case elfcpp::R_ARM_PC24:
8086 case elfcpp::R_ARM_THM_CALL:
8087 case elfcpp::R_ARM_PLT32:
8088 case elfcpp::R_ARM_CALL:
8089 case elfcpp::R_ARM_JUMP24:
8090 case elfcpp::R_ARM_THM_JUMP24:
8091 case elfcpp::R_ARM_SBREL31:
8092 case elfcpp::R_ARM_PREL31:
8093 case elfcpp::R_ARM_THM_JUMP19:
8094 case elfcpp::R_ARM_THM_JUMP6:
8095 case elfcpp::R_ARM_THM_JUMP11:
8096 case elfcpp::R_ARM_THM_JUMP8:
8097 // All the relocations above are branches except SBREL31 and PREL31.
8098 return false;
8100 default:
8101 // Be conservative and assume this is a function pointer.
8102 return true;
8106 template<bool big_endian>
8107 inline bool
8108 Target_arm<big_endian>::Scan::local_reloc_may_be_function_pointer(
8109 Symbol_table*,
8110 Layout*,
8111 Target_arm<big_endian>* target,
8112 Sized_relobj<32, big_endian>*,
8113 unsigned int,
8114 Output_section*,
8115 const elfcpp::Rel<32, big_endian>&,
8116 unsigned int r_type,
8117 const elfcpp::Sym<32, big_endian>&)
8119 r_type = target->get_real_reloc_type(r_type);
8120 return possible_function_pointer_reloc(r_type);
8123 template<bool big_endian>
8124 inline bool
8125 Target_arm<big_endian>::Scan::global_reloc_may_be_function_pointer(
8126 Symbol_table*,
8127 Layout*,
8128 Target_arm<big_endian>* target,
8129 Sized_relobj<32, big_endian>*,
8130 unsigned int,
8131 Output_section*,
8132 const elfcpp::Rel<32, big_endian>&,
8133 unsigned int r_type,
8134 Symbol* gsym)
8136 // GOT is not a function.
8137 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8138 return false;
8140 r_type = target->get_real_reloc_type(r_type);
8141 return possible_function_pointer_reloc(r_type);
8144 // Scan a relocation for a global symbol.
8146 template<bool big_endian>
8147 inline void
8148 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
8149 Layout* layout,
8150 Target_arm* target,
8151 Sized_relobj<32, big_endian>* object,
8152 unsigned int data_shndx,
8153 Output_section* output_section,
8154 const elfcpp::Rel<32, big_endian>& reloc,
8155 unsigned int r_type,
8156 Symbol* gsym)
8158 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
8159 // section. We check here to avoid creating a dynamic reloc against
8160 // _GLOBAL_OFFSET_TABLE_.
8161 if (!target->has_got_section()
8162 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8163 target->got_section(symtab, layout);
8165 r_type = get_real_reloc_type(r_type);
8166 switch (r_type)
8168 case elfcpp::R_ARM_NONE:
8169 case elfcpp::R_ARM_V4BX:
8170 case elfcpp::R_ARM_GNU_VTENTRY:
8171 case elfcpp::R_ARM_GNU_VTINHERIT:
8172 break;
8174 case elfcpp::R_ARM_ABS32:
8175 case elfcpp::R_ARM_ABS16:
8176 case elfcpp::R_ARM_ABS12:
8177 case elfcpp::R_ARM_THM_ABS5:
8178 case elfcpp::R_ARM_ABS8:
8179 case elfcpp::R_ARM_BASE_ABS:
8180 case elfcpp::R_ARM_MOVW_ABS_NC:
8181 case elfcpp::R_ARM_MOVT_ABS:
8182 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8183 case elfcpp::R_ARM_THM_MOVT_ABS:
8184 case elfcpp::R_ARM_ABS32_NOI:
8185 // Absolute addressing relocations.
8187 // Make a PLT entry if necessary.
8188 if (this->symbol_needs_plt_entry(gsym))
8190 target->make_plt_entry(symtab, layout, gsym);
8191 // Since this is not a PC-relative relocation, we may be
8192 // taking the address of a function. In that case we need to
8193 // set the entry in the dynamic symbol table to the address of
8194 // the PLT entry.
8195 if (gsym->is_from_dynobj() && !parameters->options().shared())
8196 gsym->set_needs_dynsym_value();
8198 // Make a dynamic relocation if necessary.
8199 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
8201 if (gsym->may_need_copy_reloc())
8203 target->copy_reloc(symtab, layout, object,
8204 data_shndx, output_section, gsym, reloc);
8206 else if ((r_type == elfcpp::R_ARM_ABS32
8207 || r_type == elfcpp::R_ARM_ABS32_NOI)
8208 && gsym->can_use_relative_reloc(false))
8210 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8211 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
8212 output_section, object,
8213 data_shndx, reloc.get_r_offset());
8215 else
8217 check_non_pic(object, r_type);
8218 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8219 rel_dyn->add_global(gsym, r_type, output_section, object,
8220 data_shndx, reloc.get_r_offset());
8224 break;
8226 case elfcpp::R_ARM_GOTOFF32:
8227 case elfcpp::R_ARM_GOTOFF12:
8228 // We need a GOT section.
8229 target->got_section(symtab, layout);
8230 break;
8232 case elfcpp::R_ARM_REL32:
8233 case elfcpp::R_ARM_LDR_PC_G0:
8234 case elfcpp::R_ARM_SBREL32:
8235 case elfcpp::R_ARM_THM_PC8:
8236 case elfcpp::R_ARM_BASE_PREL:
8237 case elfcpp::R_ARM_MOVW_PREL_NC:
8238 case elfcpp::R_ARM_MOVT_PREL:
8239 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8240 case elfcpp::R_ARM_THM_MOVT_PREL:
8241 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8242 case elfcpp::R_ARM_THM_PC12:
8243 case elfcpp::R_ARM_REL32_NOI:
8244 case elfcpp::R_ARM_ALU_PC_G0_NC:
8245 case elfcpp::R_ARM_ALU_PC_G0:
8246 case elfcpp::R_ARM_ALU_PC_G1_NC:
8247 case elfcpp::R_ARM_ALU_PC_G1:
8248 case elfcpp::R_ARM_ALU_PC_G2:
8249 case elfcpp::R_ARM_LDR_PC_G1:
8250 case elfcpp::R_ARM_LDR_PC_G2:
8251 case elfcpp::R_ARM_LDRS_PC_G0:
8252 case elfcpp::R_ARM_LDRS_PC_G1:
8253 case elfcpp::R_ARM_LDRS_PC_G2:
8254 case elfcpp::R_ARM_LDC_PC_G0:
8255 case elfcpp::R_ARM_LDC_PC_G1:
8256 case elfcpp::R_ARM_LDC_PC_G2:
8257 case elfcpp::R_ARM_ALU_SB_G0_NC:
8258 case elfcpp::R_ARM_ALU_SB_G0:
8259 case elfcpp::R_ARM_ALU_SB_G1_NC:
8260 case elfcpp::R_ARM_ALU_SB_G1:
8261 case elfcpp::R_ARM_ALU_SB_G2:
8262 case elfcpp::R_ARM_LDR_SB_G0:
8263 case elfcpp::R_ARM_LDR_SB_G1:
8264 case elfcpp::R_ARM_LDR_SB_G2:
8265 case elfcpp::R_ARM_LDRS_SB_G0:
8266 case elfcpp::R_ARM_LDRS_SB_G1:
8267 case elfcpp::R_ARM_LDRS_SB_G2:
8268 case elfcpp::R_ARM_LDC_SB_G0:
8269 case elfcpp::R_ARM_LDC_SB_G1:
8270 case elfcpp::R_ARM_LDC_SB_G2:
8271 case elfcpp::R_ARM_MOVW_BREL_NC:
8272 case elfcpp::R_ARM_MOVT_BREL:
8273 case elfcpp::R_ARM_MOVW_BREL:
8274 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8275 case elfcpp::R_ARM_THM_MOVT_BREL:
8276 case elfcpp::R_ARM_THM_MOVW_BREL:
8277 // Relative addressing relocations.
8279 // Make a dynamic relocation if necessary.
8280 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
8282 if (target->may_need_copy_reloc(gsym))
8284 target->copy_reloc(symtab, layout, object,
8285 data_shndx, output_section, gsym, reloc);
8287 else
8289 check_non_pic(object, r_type);
8290 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8291 rel_dyn->add_global(gsym, r_type, output_section, object,
8292 data_shndx, reloc.get_r_offset());
8296 break;
8298 case elfcpp::R_ARM_THM_CALL:
8299 case elfcpp::R_ARM_PLT32:
8300 case elfcpp::R_ARM_CALL:
8301 case elfcpp::R_ARM_JUMP24:
8302 case elfcpp::R_ARM_THM_JUMP24:
8303 case elfcpp::R_ARM_SBREL31:
8304 case elfcpp::R_ARM_PREL31:
8305 case elfcpp::R_ARM_THM_JUMP19:
8306 case elfcpp::R_ARM_THM_JUMP6:
8307 case elfcpp::R_ARM_THM_JUMP11:
8308 case elfcpp::R_ARM_THM_JUMP8:
8309 // All the relocation above are branches except for the PREL31 ones.
8310 // A PREL31 relocation can point to a personality function in a shared
8311 // library. In that case we want to use a PLT because we want to
8312 // call the personality routine and the dyanmic linkers we care about
8313 // do not support dynamic PREL31 relocations. An REL31 relocation may
8314 // point to a function whose unwinding behaviour is being described but
8315 // we will not mistakenly generate a PLT for that because we should use
8316 // a local section symbol.
8318 // If the symbol is fully resolved, this is just a relative
8319 // local reloc. Otherwise we need a PLT entry.
8320 if (gsym->final_value_is_known())
8321 break;
8322 // If building a shared library, we can also skip the PLT entry
8323 // if the symbol is defined in the output file and is protected
8324 // or hidden.
8325 if (gsym->is_defined()
8326 && !gsym->is_from_dynobj()
8327 && !gsym->is_preemptible())
8328 break;
8329 target->make_plt_entry(symtab, layout, gsym);
8330 break;
8332 case elfcpp::R_ARM_GOT_BREL:
8333 case elfcpp::R_ARM_GOT_ABS:
8334 case elfcpp::R_ARM_GOT_PREL:
8336 // The symbol requires a GOT entry.
8337 Arm_output_data_got<big_endian>* got =
8338 target->got_section(symtab, layout);
8339 if (gsym->final_value_is_known())
8340 got->add_global(gsym, GOT_TYPE_STANDARD);
8341 else
8343 // If this symbol is not fully resolved, we need to add a
8344 // GOT entry with a dynamic relocation.
8345 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8346 if (gsym->is_from_dynobj()
8347 || gsym->is_undefined()
8348 || gsym->is_preemptible())
8349 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
8350 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
8351 else
8353 if (got->add_global(gsym, GOT_TYPE_STANDARD))
8354 rel_dyn->add_global_relative(
8355 gsym, elfcpp::R_ARM_RELATIVE, got,
8356 gsym->got_offset(GOT_TYPE_STANDARD));
8360 break;
8362 case elfcpp::R_ARM_TARGET1:
8363 case elfcpp::R_ARM_TARGET2:
8364 // These should have been mapped to other types already.
8365 // Fall through.
8366 case elfcpp::R_ARM_COPY:
8367 case elfcpp::R_ARM_GLOB_DAT:
8368 case elfcpp::R_ARM_JUMP_SLOT:
8369 case elfcpp::R_ARM_RELATIVE:
8370 // These are relocations which should only be seen by the
8371 // dynamic linker, and should never be seen here.
8372 gold_error(_("%s: unexpected reloc %u in object file"),
8373 object->name().c_str(), r_type);
8374 break;
8376 // These are initial tls relocs, which are expected when
8377 // linking.
8378 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8379 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8380 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8381 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8382 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8384 const bool is_final = gsym->final_value_is_known();
8385 const tls::Tls_optimization optimized_type
8386 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
8387 switch (r_type)
8389 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8390 if (optimized_type == tls::TLSOPT_NONE)
8392 // Create a pair of GOT entries for the module index and
8393 // dtv-relative offset.
8394 Arm_output_data_got<big_endian>* got
8395 = target->got_section(symtab, layout);
8396 if (!parameters->doing_static_link())
8397 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
8398 target->rel_dyn_section(layout),
8399 elfcpp::R_ARM_TLS_DTPMOD32,
8400 elfcpp::R_ARM_TLS_DTPOFF32);
8401 else
8402 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
8404 else
8405 // FIXME: TLS optimization not supported yet.
8406 gold_unreachable();
8407 break;
8409 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8410 if (optimized_type == tls::TLSOPT_NONE)
8412 // Create a GOT entry for the module index.
8413 target->got_mod_index_entry(symtab, layout, object);
8415 else
8416 // FIXME: TLS optimization not supported yet.
8417 gold_unreachable();
8418 break;
8420 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8421 break;
8423 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8424 layout->set_has_static_tls();
8425 if (optimized_type == tls::TLSOPT_NONE)
8427 // Create a GOT entry for the tp-relative offset.
8428 Arm_output_data_got<big_endian>* got
8429 = target->got_section(symtab, layout);
8430 if (!parameters->doing_static_link())
8431 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
8432 target->rel_dyn_section(layout),
8433 elfcpp::R_ARM_TLS_TPOFF32);
8434 else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
8436 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
8437 unsigned int got_offset =
8438 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
8439 got->add_static_reloc(got_offset,
8440 elfcpp::R_ARM_TLS_TPOFF32, gsym);
8443 else
8444 // FIXME: TLS optimization not supported yet.
8445 gold_unreachable();
8446 break;
8448 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8449 layout->set_has_static_tls();
8450 if (parameters->options().shared())
8452 // We need to create a dynamic relocation.
8453 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8454 rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
8455 output_section, object,
8456 data_shndx, reloc.get_r_offset());
8458 break;
8460 default:
8461 gold_unreachable();
8464 break;
8466 case elfcpp::R_ARM_PC24:
8467 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
8468 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
8469 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
8470 default:
8471 unsupported_reloc_global(object, r_type, gsym);
8472 break;
8476 // Process relocations for gc.
8478 template<bool big_endian>
8479 void
8480 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
8481 Layout* layout,
8482 Sized_relobj<32, big_endian>* object,
8483 unsigned int data_shndx,
8484 unsigned int,
8485 const unsigned char* prelocs,
8486 size_t reloc_count,
8487 Output_section* output_section,
8488 bool needs_special_offset_handling,
8489 size_t local_symbol_count,
8490 const unsigned char* plocal_symbols)
8492 typedef Target_arm<big_endian> Arm;
8493 typedef typename Target_arm<big_endian>::Scan Scan;
8495 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan,
8496 typename Target_arm::Relocatable_size_for_reloc>(
8497 symtab,
8498 layout,
8499 this,
8500 object,
8501 data_shndx,
8502 prelocs,
8503 reloc_count,
8504 output_section,
8505 needs_special_offset_handling,
8506 local_symbol_count,
8507 plocal_symbols);
8510 // Scan relocations for a section.
8512 template<bool big_endian>
8513 void
8514 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
8515 Layout* layout,
8516 Sized_relobj<32, big_endian>* object,
8517 unsigned int data_shndx,
8518 unsigned int sh_type,
8519 const unsigned char* prelocs,
8520 size_t reloc_count,
8521 Output_section* output_section,
8522 bool needs_special_offset_handling,
8523 size_t local_symbol_count,
8524 const unsigned char* plocal_symbols)
8526 typedef typename Target_arm<big_endian>::Scan Scan;
8527 if (sh_type == elfcpp::SHT_RELA)
8529 gold_error(_("%s: unsupported RELA reloc section"),
8530 object->name().c_str());
8531 return;
8534 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
8535 symtab,
8536 layout,
8537 this,
8538 object,
8539 data_shndx,
8540 prelocs,
8541 reloc_count,
8542 output_section,
8543 needs_special_offset_handling,
8544 local_symbol_count,
8545 plocal_symbols);
8548 // Finalize the sections.
8550 template<bool big_endian>
8551 void
8552 Target_arm<big_endian>::do_finalize_sections(
8553 Layout* layout,
8554 const Input_objects* input_objects,
8555 Symbol_table* symtab)
8557 bool merged_any_attributes = false;
8558 // Merge processor-specific flags.
8559 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8560 p != input_objects->relobj_end();
8561 ++p)
8563 Arm_relobj<big_endian>* arm_relobj =
8564 Arm_relobj<big_endian>::as_arm_relobj(*p);
8565 if (arm_relobj->merge_flags_and_attributes())
8567 this->merge_processor_specific_flags(
8568 arm_relobj->name(),
8569 arm_relobj->processor_specific_flags());
8570 this->merge_object_attributes(arm_relobj->name().c_str(),
8571 arm_relobj->attributes_section_data());
8572 merged_any_attributes = true;
8576 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
8577 p != input_objects->dynobj_end();
8578 ++p)
8580 Arm_dynobj<big_endian>* arm_dynobj =
8581 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
8582 this->merge_processor_specific_flags(
8583 arm_dynobj->name(),
8584 arm_dynobj->processor_specific_flags());
8585 this->merge_object_attributes(arm_dynobj->name().c_str(),
8586 arm_dynobj->attributes_section_data());
8587 merged_any_attributes = true;
8590 // Create an empty uninitialized attribute section if we still don't have it
8591 // at this moment. This happens if there is no attributes sections in all
8592 // inputs.
8593 if (this->attributes_section_data_ == NULL)
8594 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
8596 // Check BLX use.
8597 const Object_attribute* cpu_arch_attr =
8598 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
8599 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
8600 this->set_may_use_blx(true);
8602 // Check if we need to use Cortex-A8 workaround.
8603 if (parameters->options().user_set_fix_cortex_a8())
8604 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
8605 else
8607 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
8608 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
8609 // profile.
8610 const Object_attribute* cpu_arch_profile_attr =
8611 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
8612 this->fix_cortex_a8_ =
8613 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
8614 && (cpu_arch_profile_attr->int_value() == 'A'
8615 || cpu_arch_profile_attr->int_value() == 0));
8618 // Check if we can use V4BX interworking.
8619 // The V4BX interworking stub contains BX instruction,
8620 // which is not specified for some profiles.
8621 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8622 && !this->may_use_blx())
8623 gold_error(_("unable to provide V4BX reloc interworking fix up; "
8624 "the target profile does not support BX instruction"));
8626 // Fill in some more dynamic tags.
8627 const Reloc_section* rel_plt = (this->plt_ == NULL
8628 ? NULL
8629 : this->plt_->rel_plt());
8630 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
8631 this->rel_dyn_, true, false);
8633 // Emit any relocs we saved in an attempt to avoid generating COPY
8634 // relocs.
8635 if (this->copy_relocs_.any_saved_relocs())
8636 this->copy_relocs_.emit(this->rel_dyn_section(layout));
8638 // Handle the .ARM.exidx section.
8639 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
8641 if (!parameters->options().relocatable())
8643 if (exidx_section != NULL
8644 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
8646 // Create __exidx_start and __exdix_end symbols.
8647 symtab->define_in_output_data("__exidx_start", NULL,
8648 Symbol_table::PREDEFINED,
8649 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8650 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
8651 0, false, true);
8652 symtab->define_in_output_data("__exidx_end", NULL,
8653 Symbol_table::PREDEFINED,
8654 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8655 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
8656 0, true, true);
8658 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
8659 // the .ARM.exidx section.
8660 if (!layout->script_options()->saw_phdrs_clause())
8662 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0,
8664 == NULL);
8665 Output_segment* exidx_segment =
8666 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
8667 exidx_segment->add_output_section_to_nonload(exidx_section,
8668 elfcpp::PF_R);
8671 else
8673 symtab->define_as_constant("__exidx_start", NULL,
8674 Symbol_table::PREDEFINED,
8675 0, 0, elfcpp::STT_OBJECT,
8676 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8677 true, false);
8678 symtab->define_as_constant("__exidx_end", NULL,
8679 Symbol_table::PREDEFINED,
8680 0, 0, elfcpp::STT_OBJECT,
8681 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8682 true, false);
8686 // Create an .ARM.attributes section if we have merged any attributes
8687 // from inputs.
8688 if (merged_any_attributes)
8690 Output_attributes_section_data* attributes_section =
8691 new Output_attributes_section_data(*this->attributes_section_data_);
8692 layout->add_output_section_data(".ARM.attributes",
8693 elfcpp::SHT_ARM_ATTRIBUTES, 0,
8694 attributes_section, ORDER_INVALID,
8695 false);
8698 // Fix up links in section EXIDX headers.
8699 for (Layout::Section_list::const_iterator p = layout->section_list().begin();
8700 p != layout->section_list().end();
8701 ++p)
8702 if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
8704 Arm_output_section<big_endian>* os =
8705 Arm_output_section<big_endian>::as_arm_output_section(*p);
8706 os->set_exidx_section_link();
8710 // Return whether a direct absolute static relocation needs to be applied.
8711 // In cases where Scan::local() or Scan::global() has created
8712 // a dynamic relocation other than R_ARM_RELATIVE, the addend
8713 // of the relocation is carried in the data, and we must not
8714 // apply the static relocation.
8716 template<bool big_endian>
8717 inline bool
8718 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
8719 const Sized_symbol<32>* gsym,
8720 unsigned int r_type,
8721 bool is_32bit,
8722 Output_section* output_section)
8724 // If the output section is not allocated, then we didn't call
8725 // scan_relocs, we didn't create a dynamic reloc, and we must apply
8726 // the reloc here.
8727 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
8728 return true;
8730 int ref_flags = Scan::get_reference_flags(r_type);
8732 // For local symbols, we will have created a non-RELATIVE dynamic
8733 // relocation only if (a) the output is position independent,
8734 // (b) the relocation is absolute (not pc- or segment-relative), and
8735 // (c) the relocation is not 32 bits wide.
8736 if (gsym == NULL)
8737 return !(parameters->options().output_is_position_independent()
8738 && (ref_flags & Symbol::ABSOLUTE_REF)
8739 && !is_32bit);
8741 // For global symbols, we use the same helper routines used in the
8742 // scan pass. If we did not create a dynamic relocation, or if we
8743 // created a RELATIVE dynamic relocation, we should apply the static
8744 // relocation.
8745 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
8746 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
8747 && gsym->can_use_relative_reloc(ref_flags
8748 & Symbol::FUNCTION_CALL);
8749 return !has_dyn || is_rel;
8752 // Perform a relocation.
8754 template<bool big_endian>
8755 inline bool
8756 Target_arm<big_endian>::Relocate::relocate(
8757 const Relocate_info<32, big_endian>* relinfo,
8758 Target_arm* target,
8759 Output_section* output_section,
8760 size_t relnum,
8761 const elfcpp::Rel<32, big_endian>& rel,
8762 unsigned int r_type,
8763 const Sized_symbol<32>* gsym,
8764 const Symbol_value<32>* psymval,
8765 unsigned char* view,
8766 Arm_address address,
8767 section_size_type view_size)
8769 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
8771 r_type = get_real_reloc_type(r_type);
8772 const Arm_reloc_property* reloc_property =
8773 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8774 if (reloc_property == NULL)
8776 std::string reloc_name =
8777 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8778 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8779 _("cannot relocate %s in object file"),
8780 reloc_name.c_str());
8781 return true;
8784 const Arm_relobj<big_endian>* object =
8785 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8787 // If the final branch target of a relocation is THUMB instruction, this
8788 // is 1. Otherwise it is 0.
8789 Arm_address thumb_bit = 0;
8790 Symbol_value<32> symval;
8791 bool is_weakly_undefined_without_plt = false;
8792 bool have_got_offset = false;
8793 unsigned int got_offset = 0;
8795 // If the relocation uses the GOT entry of a symbol instead of the symbol
8796 // itself, we don't care about whether the symbol is defined or what kind
8797 // of symbol it is.
8798 if (reloc_property->uses_got_entry())
8800 // Get the GOT offset.
8801 // The GOT pointer points to the end of the GOT section.
8802 // We need to subtract the size of the GOT section to get
8803 // the actual offset to use in the relocation.
8804 // TODO: We should move GOT offset computing code in TLS relocations
8805 // to here.
8806 switch (r_type)
8808 case elfcpp::R_ARM_GOT_BREL:
8809 case elfcpp::R_ARM_GOT_PREL:
8810 if (gsym != NULL)
8812 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8813 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
8814 - target->got_size());
8816 else
8818 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8819 gold_assert(object->local_has_got_offset(r_sym,
8820 GOT_TYPE_STANDARD));
8821 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
8822 - target->got_size());
8824 have_got_offset = true;
8825 break;
8827 default:
8828 break;
8831 else if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
8833 if (gsym != NULL)
8835 // This is a global symbol. Determine if we use PLT and if the
8836 // final target is THUMB.
8837 if (gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
8839 // This uses a PLT, change the symbol value.
8840 symval.set_output_value(target->plt_section()->address()
8841 + gsym->plt_offset());
8842 psymval = &symval;
8844 else if (gsym->is_weak_undefined())
8846 // This is a weakly undefined symbol and we do not use PLT
8847 // for this relocation. A branch targeting this symbol will
8848 // be converted into an NOP.
8849 is_weakly_undefined_without_plt = true;
8851 else if (gsym->is_undefined() && reloc_property->uses_symbol())
8853 // This relocation uses the symbol value but the symbol is
8854 // undefined. Exit early and have the caller reporting an
8855 // error.
8856 return true;
8858 else
8860 // Set thumb bit if symbol:
8861 // -Has type STT_ARM_TFUNC or
8862 // -Has type STT_FUNC, is defined and with LSB in value set.
8863 thumb_bit =
8864 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
8865 || (gsym->type() == elfcpp::STT_FUNC
8866 && !gsym->is_undefined()
8867 && ((psymval->value(object, 0) & 1) != 0)))
8869 : 0);
8872 else
8874 // This is a local symbol. Determine if the final target is THUMB.
8875 // We saved this information when all the local symbols were read.
8876 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
8877 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
8878 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
8881 else
8883 // This is a fake relocation synthesized for a stub. It does not have
8884 // a real symbol. We just look at the LSB of the symbol value to
8885 // determine if the target is THUMB or not.
8886 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
8889 // Strip LSB if this points to a THUMB target.
8890 if (thumb_bit != 0
8891 && reloc_property->uses_thumb_bit()
8892 && ((psymval->value(object, 0) & 1) != 0))
8894 Arm_address stripped_value =
8895 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
8896 symval.set_output_value(stripped_value);
8897 psymval = &symval;
8900 // To look up relocation stubs, we need to pass the symbol table index of
8901 // a local symbol.
8902 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8904 // Get the addressing origin of the output segment defining the
8905 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
8906 Arm_address sym_origin = 0;
8907 if (reloc_property->uses_symbol_base())
8909 if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
8910 // R_ARM_BASE_ABS with the NULL symbol will give the
8911 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
8912 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
8913 sym_origin = target->got_plt_section()->address();
8914 else if (gsym == NULL)
8915 sym_origin = 0;
8916 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
8917 sym_origin = gsym->output_segment()->vaddr();
8918 else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
8919 sym_origin = gsym->output_data()->address();
8921 // TODO: Assumes the segment base to be zero for the global symbols
8922 // till the proper support for the segment-base-relative addressing
8923 // will be implemented. This is consistent with GNU ld.
8926 // For relative addressing relocation, find out the relative address base.
8927 Arm_address relative_address_base = 0;
8928 switch(reloc_property->relative_address_base())
8930 case Arm_reloc_property::RAB_NONE:
8931 // Relocations with relative address bases RAB_TLS and RAB_tp are
8932 // handled by relocate_tls. So we do not need to do anything here.
8933 case Arm_reloc_property::RAB_TLS:
8934 case Arm_reloc_property::RAB_tp:
8935 break;
8936 case Arm_reloc_property::RAB_B_S:
8937 relative_address_base = sym_origin;
8938 break;
8939 case Arm_reloc_property::RAB_GOT_ORG:
8940 relative_address_base = target->got_plt_section()->address();
8941 break;
8942 case Arm_reloc_property::RAB_P:
8943 relative_address_base = address;
8944 break;
8945 case Arm_reloc_property::RAB_Pa:
8946 relative_address_base = address & 0xfffffffcU;
8947 break;
8948 default:
8949 gold_unreachable();
8952 typename Arm_relocate_functions::Status reloc_status =
8953 Arm_relocate_functions::STATUS_OKAY;
8954 bool check_overflow = reloc_property->checks_overflow();
8955 switch (r_type)
8957 case elfcpp::R_ARM_NONE:
8958 break;
8960 case elfcpp::R_ARM_ABS8:
8961 if (should_apply_static_reloc(gsym, r_type, false, output_section))
8962 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
8963 break;
8965 case elfcpp::R_ARM_ABS12:
8966 if (should_apply_static_reloc(gsym, r_type, false, output_section))
8967 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
8968 break;
8970 case elfcpp::R_ARM_ABS16:
8971 if (should_apply_static_reloc(gsym, r_type, false, output_section))
8972 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
8973 break;
8975 case elfcpp::R_ARM_ABS32:
8976 if (should_apply_static_reloc(gsym, r_type, true, output_section))
8977 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8978 thumb_bit);
8979 break;
8981 case elfcpp::R_ARM_ABS32_NOI:
8982 if (should_apply_static_reloc(gsym, r_type, true, output_section))
8983 // No thumb bit for this relocation: (S + A)
8984 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8986 break;
8988 case elfcpp::R_ARM_MOVW_ABS_NC:
8989 if (should_apply_static_reloc(gsym, r_type, false, output_section))
8990 reloc_status = Arm_relocate_functions::movw(view, object, psymval,
8991 0, thumb_bit,
8992 check_overflow);
8993 break;
8995 case elfcpp::R_ARM_MOVT_ABS:
8996 if (should_apply_static_reloc(gsym, r_type, false, output_section))
8997 reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
8998 break;
9000 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
9001 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9002 reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
9003 0, thumb_bit, false);
9004 break;
9006 case elfcpp::R_ARM_THM_MOVT_ABS:
9007 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9008 reloc_status = Arm_relocate_functions::thm_movt(view, object,
9009 psymval, 0);
9010 break;
9012 case elfcpp::R_ARM_MOVW_PREL_NC:
9013 case elfcpp::R_ARM_MOVW_BREL_NC:
9014 case elfcpp::R_ARM_MOVW_BREL:
9015 reloc_status =
9016 Arm_relocate_functions::movw(view, object, psymval,
9017 relative_address_base, thumb_bit,
9018 check_overflow);
9019 break;
9021 case elfcpp::R_ARM_MOVT_PREL:
9022 case elfcpp::R_ARM_MOVT_BREL:
9023 reloc_status =
9024 Arm_relocate_functions::movt(view, object, psymval,
9025 relative_address_base);
9026 break;
9028 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
9029 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
9030 case elfcpp::R_ARM_THM_MOVW_BREL:
9031 reloc_status =
9032 Arm_relocate_functions::thm_movw(view, object, psymval,
9033 relative_address_base,
9034 thumb_bit, check_overflow);
9035 break;
9037 case elfcpp::R_ARM_THM_MOVT_PREL:
9038 case elfcpp::R_ARM_THM_MOVT_BREL:
9039 reloc_status =
9040 Arm_relocate_functions::thm_movt(view, object, psymval,
9041 relative_address_base);
9042 break;
9044 case elfcpp::R_ARM_REL32:
9045 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
9046 address, thumb_bit);
9047 break;
9049 case elfcpp::R_ARM_THM_ABS5:
9050 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9051 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
9052 break;
9054 // Thumb long branches.
9055 case elfcpp::R_ARM_THM_CALL:
9056 case elfcpp::R_ARM_THM_XPC22:
9057 case elfcpp::R_ARM_THM_JUMP24:
9058 reloc_status =
9059 Arm_relocate_functions::thumb_branch_common(
9060 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
9061 thumb_bit, is_weakly_undefined_without_plt);
9062 break;
9064 case elfcpp::R_ARM_GOTOFF32:
9066 Arm_address got_origin;
9067 got_origin = target->got_plt_section()->address();
9068 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
9069 got_origin, thumb_bit);
9071 break;
9073 case elfcpp::R_ARM_BASE_PREL:
9074 gold_assert(gsym != NULL);
9075 reloc_status =
9076 Arm_relocate_functions::base_prel(view, sym_origin, address);
9077 break;
9079 case elfcpp::R_ARM_BASE_ABS:
9080 if (should_apply_static_reloc(gsym, r_type, false, output_section))
9081 reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
9082 break;
9084 case elfcpp::R_ARM_GOT_BREL:
9085 gold_assert(have_got_offset);
9086 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
9087 break;
9089 case elfcpp::R_ARM_GOT_PREL:
9090 gold_assert(have_got_offset);
9091 // Get the address origin for GOT PLT, which is allocated right
9092 // after the GOT section, to calculate an absolute address of
9093 // the symbol GOT entry (got_origin + got_offset).
9094 Arm_address got_origin;
9095 got_origin = target->got_plt_section()->address();
9096 reloc_status = Arm_relocate_functions::got_prel(view,
9097 got_origin + got_offset,
9098 address);
9099 break;
9101 case elfcpp::R_ARM_PLT32:
9102 case elfcpp::R_ARM_CALL:
9103 case elfcpp::R_ARM_JUMP24:
9104 case elfcpp::R_ARM_XPC25:
9105 gold_assert(gsym == NULL
9106 || gsym->has_plt_offset()
9107 || gsym->final_value_is_known()
9108 || (gsym->is_defined()
9109 && !gsym->is_from_dynobj()
9110 && !gsym->is_preemptible()));
9111 reloc_status =
9112 Arm_relocate_functions::arm_branch_common(
9113 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
9114 thumb_bit, is_weakly_undefined_without_plt);
9115 break;
9117 case elfcpp::R_ARM_THM_JUMP19:
9118 reloc_status =
9119 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
9120 thumb_bit);
9121 break;
9123 case elfcpp::R_ARM_THM_JUMP6:
9124 reloc_status =
9125 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
9126 break;
9128 case elfcpp::R_ARM_THM_JUMP8:
9129 reloc_status =
9130 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
9131 break;
9133 case elfcpp::R_ARM_THM_JUMP11:
9134 reloc_status =
9135 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
9136 break;
9138 case elfcpp::R_ARM_PREL31:
9139 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
9140 address, thumb_bit);
9141 break;
9143 case elfcpp::R_ARM_V4BX:
9144 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
9146 const bool is_v4bx_interworking =
9147 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
9148 reloc_status =
9149 Arm_relocate_functions::v4bx(relinfo, view, object, address,
9150 is_v4bx_interworking);
9152 break;
9154 case elfcpp::R_ARM_THM_PC8:
9155 reloc_status =
9156 Arm_relocate_functions::thm_pc8(view, object, psymval, address);
9157 break;
9159 case elfcpp::R_ARM_THM_PC12:
9160 reloc_status =
9161 Arm_relocate_functions::thm_pc12(view, object, psymval, address);
9162 break;
9164 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
9165 reloc_status =
9166 Arm_relocate_functions::thm_alu11(view, object, psymval, address,
9167 thumb_bit);
9168 break;
9170 case elfcpp::R_ARM_ALU_PC_G0_NC:
9171 case elfcpp::R_ARM_ALU_PC_G0:
9172 case elfcpp::R_ARM_ALU_PC_G1_NC:
9173 case elfcpp::R_ARM_ALU_PC_G1:
9174 case elfcpp::R_ARM_ALU_PC_G2:
9175 case elfcpp::R_ARM_ALU_SB_G0_NC:
9176 case elfcpp::R_ARM_ALU_SB_G0:
9177 case elfcpp::R_ARM_ALU_SB_G1_NC:
9178 case elfcpp::R_ARM_ALU_SB_G1:
9179 case elfcpp::R_ARM_ALU_SB_G2:
9180 reloc_status =
9181 Arm_relocate_functions::arm_grp_alu(view, object, psymval,
9182 reloc_property->group_index(),
9183 relative_address_base,
9184 thumb_bit, check_overflow);
9185 break;
9187 case elfcpp::R_ARM_LDR_PC_G0:
9188 case elfcpp::R_ARM_LDR_PC_G1:
9189 case elfcpp::R_ARM_LDR_PC_G2:
9190 case elfcpp::R_ARM_LDR_SB_G0:
9191 case elfcpp::R_ARM_LDR_SB_G1:
9192 case elfcpp::R_ARM_LDR_SB_G2:
9193 reloc_status =
9194 Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
9195 reloc_property->group_index(),
9196 relative_address_base);
9197 break;
9199 case elfcpp::R_ARM_LDRS_PC_G0:
9200 case elfcpp::R_ARM_LDRS_PC_G1:
9201 case elfcpp::R_ARM_LDRS_PC_G2:
9202 case elfcpp::R_ARM_LDRS_SB_G0:
9203 case elfcpp::R_ARM_LDRS_SB_G1:
9204 case elfcpp::R_ARM_LDRS_SB_G2:
9205 reloc_status =
9206 Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
9207 reloc_property->group_index(),
9208 relative_address_base);
9209 break;
9211 case elfcpp::R_ARM_LDC_PC_G0:
9212 case elfcpp::R_ARM_LDC_PC_G1:
9213 case elfcpp::R_ARM_LDC_PC_G2:
9214 case elfcpp::R_ARM_LDC_SB_G0:
9215 case elfcpp::R_ARM_LDC_SB_G1:
9216 case elfcpp::R_ARM_LDC_SB_G2:
9217 reloc_status =
9218 Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
9219 reloc_property->group_index(),
9220 relative_address_base);
9221 break;
9223 // These are initial tls relocs, which are expected when
9224 // linking.
9225 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
9226 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
9227 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
9228 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
9229 case elfcpp::R_ARM_TLS_LE32: // Local-exec
9230 reloc_status =
9231 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
9232 view, address, view_size);
9233 break;
9235 // The known and unknown unsupported and/or deprecated relocations.
9236 case elfcpp::R_ARM_PC24:
9237 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
9238 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
9239 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
9240 default:
9241 // Just silently leave the method. We should get an appropriate error
9242 // message in the scan methods.
9243 break;
9246 // Report any errors.
9247 switch (reloc_status)
9249 case Arm_relocate_functions::STATUS_OKAY:
9250 break;
9251 case Arm_relocate_functions::STATUS_OVERFLOW:
9252 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9253 _("relocation overflow in %s"),
9254 reloc_property->name().c_str());
9255 break;
9256 case Arm_relocate_functions::STATUS_BAD_RELOC:
9257 gold_error_at_location(
9258 relinfo,
9259 relnum,
9260 rel.get_r_offset(),
9261 _("unexpected opcode while processing relocation %s"),
9262 reloc_property->name().c_str());
9263 break;
9264 default:
9265 gold_unreachable();
9268 return true;
9271 // Perform a TLS relocation.
9273 template<bool big_endian>
9274 inline typename Arm_relocate_functions<big_endian>::Status
9275 Target_arm<big_endian>::Relocate::relocate_tls(
9276 const Relocate_info<32, big_endian>* relinfo,
9277 Target_arm<big_endian>* target,
9278 size_t relnum,
9279 const elfcpp::Rel<32, big_endian>& rel,
9280 unsigned int r_type,
9281 const Sized_symbol<32>* gsym,
9282 const Symbol_value<32>* psymval,
9283 unsigned char* view,
9284 elfcpp::Elf_types<32>::Elf_Addr address,
9285 section_size_type /*view_size*/ )
9287 typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
9288 typedef Relocate_functions<32, big_endian> RelocFuncs;
9289 Output_segment* tls_segment = relinfo->layout->tls_segment();
9291 const Sized_relobj<32, big_endian>* object = relinfo->object;
9293 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
9295 const bool is_final = (gsym == NULL
9296 ? !parameters->options().shared()
9297 : gsym->final_value_is_known());
9298 const tls::Tls_optimization optimized_type
9299 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
9300 switch (r_type)
9302 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
9304 unsigned int got_type = GOT_TYPE_TLS_PAIR;
9305 unsigned int got_offset;
9306 if (gsym != NULL)
9308 gold_assert(gsym->has_got_offset(got_type));
9309 got_offset = gsym->got_offset(got_type) - target->got_size();
9311 else
9313 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9314 gold_assert(object->local_has_got_offset(r_sym, got_type));
9315 got_offset = (object->local_got_offset(r_sym, got_type)
9316 - target->got_size());
9318 if (optimized_type == tls::TLSOPT_NONE)
9320 Arm_address got_entry =
9321 target->got_plt_section()->address() + got_offset;
9323 // Relocate the field with the PC relative offset of the pair of
9324 // GOT entries.
9325 RelocFuncs::pcrel32(view, got_entry, address);
9326 return ArmRelocFuncs::STATUS_OKAY;
9329 break;
9331 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
9332 if (optimized_type == tls::TLSOPT_NONE)
9334 // Relocate the field with the offset of the GOT entry for
9335 // the module index.
9336 unsigned int got_offset;
9337 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
9338 - target->got_size());
9339 Arm_address got_entry =
9340 target->got_plt_section()->address() + got_offset;
9342 // Relocate the field with the PC relative offset of the pair of
9343 // GOT entries.
9344 RelocFuncs::pcrel32(view, got_entry, address);
9345 return ArmRelocFuncs::STATUS_OKAY;
9347 break;
9349 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
9350 RelocFuncs::rel32(view, value);
9351 return ArmRelocFuncs::STATUS_OKAY;
9353 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
9354 if (optimized_type == tls::TLSOPT_NONE)
9356 // Relocate the field with the offset of the GOT entry for
9357 // the tp-relative offset of the symbol.
9358 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
9359 unsigned int got_offset;
9360 if (gsym != NULL)
9362 gold_assert(gsym->has_got_offset(got_type));
9363 got_offset = gsym->got_offset(got_type);
9365 else
9367 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9368 gold_assert(object->local_has_got_offset(r_sym, got_type));
9369 got_offset = object->local_got_offset(r_sym, got_type);
9372 // All GOT offsets are relative to the end of the GOT.
9373 got_offset -= target->got_size();
9375 Arm_address got_entry =
9376 target->got_plt_section()->address() + got_offset;
9378 // Relocate the field with the PC relative offset of the GOT entry.
9379 RelocFuncs::pcrel32(view, got_entry, address);
9380 return ArmRelocFuncs::STATUS_OKAY;
9382 break;
9384 case elfcpp::R_ARM_TLS_LE32: // Local-exec
9385 // If we're creating a shared library, a dynamic relocation will
9386 // have been created for this location, so do not apply it now.
9387 if (!parameters->options().shared())
9389 gold_assert(tls_segment != NULL);
9391 // $tp points to the TCB, which is followed by the TLS, so we
9392 // need to add TCB size to the offset.
9393 Arm_address aligned_tcb_size =
9394 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
9395 RelocFuncs::rel32(view, value + aligned_tcb_size);
9398 return ArmRelocFuncs::STATUS_OKAY;
9400 default:
9401 gold_unreachable();
9404 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9405 _("unsupported reloc %u"),
9406 r_type);
9407 return ArmRelocFuncs::STATUS_BAD_RELOC;
9410 // Relocate section data.
9412 template<bool big_endian>
9413 void
9414 Target_arm<big_endian>::relocate_section(
9415 const Relocate_info<32, big_endian>* relinfo,
9416 unsigned int sh_type,
9417 const unsigned char* prelocs,
9418 size_t reloc_count,
9419 Output_section* output_section,
9420 bool needs_special_offset_handling,
9421 unsigned char* view,
9422 Arm_address address,
9423 section_size_type view_size,
9424 const Reloc_symbol_changes* reloc_symbol_changes)
9426 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
9427 gold_assert(sh_type == elfcpp::SHT_REL);
9429 // See if we are relocating a relaxed input section. If so, the view
9430 // covers the whole output section and we need to adjust accordingly.
9431 if (needs_special_offset_handling)
9433 const Output_relaxed_input_section* poris =
9434 output_section->find_relaxed_input_section(relinfo->object,
9435 relinfo->data_shndx);
9436 if (poris != NULL)
9438 Arm_address section_address = poris->address();
9439 section_size_type section_size = poris->data_size();
9441 gold_assert((section_address >= address)
9442 && ((section_address + section_size)
9443 <= (address + view_size)));
9445 off_t offset = section_address - address;
9446 view += offset;
9447 address += offset;
9448 view_size = section_size;
9452 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
9453 Arm_relocate>(
9454 relinfo,
9455 this,
9456 prelocs,
9457 reloc_count,
9458 output_section,
9459 needs_special_offset_handling,
9460 view,
9461 address,
9462 view_size,
9463 reloc_symbol_changes);
9466 // Return the size of a relocation while scanning during a relocatable
9467 // link.
9469 template<bool big_endian>
9470 unsigned int
9471 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
9472 unsigned int r_type,
9473 Relobj* object)
9475 r_type = get_real_reloc_type(r_type);
9476 const Arm_reloc_property* arp =
9477 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
9478 if (arp != NULL)
9479 return arp->size();
9480 else
9482 std::string reloc_name =
9483 arm_reloc_property_table->reloc_name_in_error_message(r_type);
9484 gold_error(_("%s: unexpected %s in object file"),
9485 object->name().c_str(), reloc_name.c_str());
9486 return 0;
9490 // Scan the relocs during a relocatable link.
9492 template<bool big_endian>
9493 void
9494 Target_arm<big_endian>::scan_relocatable_relocs(
9495 Symbol_table* symtab,
9496 Layout* layout,
9497 Sized_relobj<32, big_endian>* object,
9498 unsigned int data_shndx,
9499 unsigned int sh_type,
9500 const unsigned char* prelocs,
9501 size_t reloc_count,
9502 Output_section* output_section,
9503 bool needs_special_offset_handling,
9504 size_t local_symbol_count,
9505 const unsigned char* plocal_symbols,
9506 Relocatable_relocs* rr)
9508 gold_assert(sh_type == elfcpp::SHT_REL);
9510 typedef Arm_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL,
9511 Relocatable_size_for_reloc> Scan_relocatable_relocs;
9513 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
9514 Scan_relocatable_relocs>(
9515 symtab,
9516 layout,
9517 object,
9518 data_shndx,
9519 prelocs,
9520 reloc_count,
9521 output_section,
9522 needs_special_offset_handling,
9523 local_symbol_count,
9524 plocal_symbols,
9525 rr);
9528 // Relocate a section during a relocatable link.
9530 template<bool big_endian>
9531 void
9532 Target_arm<big_endian>::relocate_for_relocatable(
9533 const Relocate_info<32, big_endian>* relinfo,
9534 unsigned int sh_type,
9535 const unsigned char* prelocs,
9536 size_t reloc_count,
9537 Output_section* output_section,
9538 off_t offset_in_output_section,
9539 const Relocatable_relocs* rr,
9540 unsigned char* view,
9541 Arm_address view_address,
9542 section_size_type view_size,
9543 unsigned char* reloc_view,
9544 section_size_type reloc_view_size)
9546 gold_assert(sh_type == elfcpp::SHT_REL);
9548 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
9549 relinfo,
9550 prelocs,
9551 reloc_count,
9552 output_section,
9553 offset_in_output_section,
9555 view,
9556 view_address,
9557 view_size,
9558 reloc_view,
9559 reloc_view_size);
9562 // Perform target-specific processing in a relocatable link. This is
9563 // only used if we use the relocation strategy RELOC_SPECIAL.
9565 template<bool big_endian>
9566 void
9567 Target_arm<big_endian>::relocate_special_relocatable(
9568 const Relocate_info<32, big_endian>* relinfo,
9569 unsigned int sh_type,
9570 const unsigned char* preloc_in,
9571 size_t relnum,
9572 Output_section* output_section,
9573 off_t offset_in_output_section,
9574 unsigned char* view,
9575 elfcpp::Elf_types<32>::Elf_Addr view_address,
9576 section_size_type,
9577 unsigned char* preloc_out)
9579 // We can only handle REL type relocation sections.
9580 gold_assert(sh_type == elfcpp::SHT_REL);
9582 typedef typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc Reltype;
9583 typedef typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc_write
9584 Reltype_write;
9585 const Arm_address invalid_address = static_cast<Arm_address>(0) - 1;
9587 const Arm_relobj<big_endian>* object =
9588 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
9589 const unsigned int local_count = object->local_symbol_count();
9591 Reltype reloc(preloc_in);
9592 Reltype_write reloc_write(preloc_out);
9594 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
9595 const unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
9596 const unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
9598 const Arm_reloc_property* arp =
9599 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
9600 gold_assert(arp != NULL);
9602 // Get the new symbol index.
9603 // We only use RELOC_SPECIAL strategy in local relocations.
9604 gold_assert(r_sym < local_count);
9606 // We are adjusting a section symbol. We need to find
9607 // the symbol table index of the section symbol for
9608 // the output section corresponding to input section
9609 // in which this symbol is defined.
9610 bool is_ordinary;
9611 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
9612 gold_assert(is_ordinary);
9613 Output_section* os = object->output_section(shndx);
9614 gold_assert(os != NULL);
9615 gold_assert(os->needs_symtab_index());
9616 unsigned int new_symndx = os->symtab_index();
9618 // Get the new offset--the location in the output section where
9619 // this relocation should be applied.
9621 Arm_address offset = reloc.get_r_offset();
9622 Arm_address new_offset;
9623 if (offset_in_output_section != invalid_address)
9624 new_offset = offset + offset_in_output_section;
9625 else
9627 section_offset_type sot_offset =
9628 convert_types<section_offset_type, Arm_address>(offset);
9629 section_offset_type new_sot_offset =
9630 output_section->output_offset(object, relinfo->data_shndx,
9631 sot_offset);
9632 gold_assert(new_sot_offset != -1);
9633 new_offset = new_sot_offset;
9636 // In an object file, r_offset is an offset within the section.
9637 // In an executable or dynamic object, generated by
9638 // --emit-relocs, r_offset is an absolute address.
9639 if (!parameters->options().relocatable())
9641 new_offset += view_address;
9642 if (offset_in_output_section != invalid_address)
9643 new_offset -= offset_in_output_section;
9646 reloc_write.put_r_offset(new_offset);
9647 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
9649 // Handle the reloc addend.
9650 // The relocation uses a section symbol in the input file.
9651 // We are adjusting it to use a section symbol in the output
9652 // file. The input section symbol refers to some address in
9653 // the input section. We need the relocation in the output
9654 // file to refer to that same address. This adjustment to
9655 // the addend is the same calculation we use for a simple
9656 // absolute relocation for the input section symbol.
9658 const Symbol_value<32>* psymval = object->local_symbol(r_sym);
9660 // Handle THUMB bit.
9661 Symbol_value<32> symval;
9662 Arm_address thumb_bit =
9663 object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
9664 if (thumb_bit != 0
9665 && arp->uses_thumb_bit()
9666 && ((psymval->value(object, 0) & 1) != 0))
9668 Arm_address stripped_value =
9669 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
9670 symval.set_output_value(stripped_value);
9671 psymval = &symval;
9674 unsigned char* paddend = view + offset;
9675 typename Arm_relocate_functions<big_endian>::Status reloc_status =
9676 Arm_relocate_functions<big_endian>::STATUS_OKAY;
9677 switch (r_type)
9679 case elfcpp::R_ARM_ABS8:
9680 reloc_status = Arm_relocate_functions<big_endian>::abs8(paddend, object,
9681 psymval);
9682 break;
9684 case elfcpp::R_ARM_ABS12:
9685 reloc_status = Arm_relocate_functions<big_endian>::abs12(paddend, object,
9686 psymval);
9687 break;
9689 case elfcpp::R_ARM_ABS16:
9690 reloc_status = Arm_relocate_functions<big_endian>::abs16(paddend, object,
9691 psymval);
9692 break;
9694 case elfcpp::R_ARM_THM_ABS5:
9695 reloc_status = Arm_relocate_functions<big_endian>::thm_abs5(paddend,
9696 object,
9697 psymval);
9698 break;
9700 case elfcpp::R_ARM_MOVW_ABS_NC:
9701 case elfcpp::R_ARM_MOVW_PREL_NC:
9702 case elfcpp::R_ARM_MOVW_BREL_NC:
9703 case elfcpp::R_ARM_MOVW_BREL:
9704 reloc_status = Arm_relocate_functions<big_endian>::movw(
9705 paddend, object, psymval, 0, thumb_bit, arp->checks_overflow());
9706 break;
9708 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
9709 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
9710 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
9711 case elfcpp::R_ARM_THM_MOVW_BREL:
9712 reloc_status = Arm_relocate_functions<big_endian>::thm_movw(
9713 paddend, object, psymval, 0, thumb_bit, arp->checks_overflow());
9714 break;
9716 case elfcpp::R_ARM_THM_CALL:
9717 case elfcpp::R_ARM_THM_XPC22:
9718 case elfcpp::R_ARM_THM_JUMP24:
9719 reloc_status =
9720 Arm_relocate_functions<big_endian>::thumb_branch_common(
9721 r_type, relinfo, paddend, NULL, object, 0, psymval, 0, thumb_bit,
9722 false);
9723 break;
9725 case elfcpp::R_ARM_PLT32:
9726 case elfcpp::R_ARM_CALL:
9727 case elfcpp::R_ARM_JUMP24:
9728 case elfcpp::R_ARM_XPC25:
9729 reloc_status =
9730 Arm_relocate_functions<big_endian>::arm_branch_common(
9731 r_type, relinfo, paddend, NULL, object, 0, psymval, 0, thumb_bit,
9732 false);
9733 break;
9735 case elfcpp::R_ARM_THM_JUMP19:
9736 reloc_status =
9737 Arm_relocate_functions<big_endian>::thm_jump19(paddend, object,
9738 psymval, 0, thumb_bit);
9739 break;
9741 case elfcpp::R_ARM_THM_JUMP6:
9742 reloc_status =
9743 Arm_relocate_functions<big_endian>::thm_jump6(paddend, object, psymval,
9745 break;
9747 case elfcpp::R_ARM_THM_JUMP8:
9748 reloc_status =
9749 Arm_relocate_functions<big_endian>::thm_jump8(paddend, object, psymval,
9751 break;
9753 case elfcpp::R_ARM_THM_JUMP11:
9754 reloc_status =
9755 Arm_relocate_functions<big_endian>::thm_jump11(paddend, object, psymval,
9757 break;
9759 case elfcpp::R_ARM_PREL31:
9760 reloc_status =
9761 Arm_relocate_functions<big_endian>::prel31(paddend, object, psymval, 0,
9762 thumb_bit);
9763 break;
9765 case elfcpp::R_ARM_THM_PC8:
9766 reloc_status =
9767 Arm_relocate_functions<big_endian>::thm_pc8(paddend, object, psymval,
9769 break;
9771 case elfcpp::R_ARM_THM_PC12:
9772 reloc_status =
9773 Arm_relocate_functions<big_endian>::thm_pc12(paddend, object, psymval,
9775 break;
9777 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
9778 reloc_status =
9779 Arm_relocate_functions<big_endian>::thm_alu11(paddend, object, psymval,
9780 0, thumb_bit);
9781 break;
9783 // These relocation truncate relocation results so we cannot handle them
9784 // in a relocatable link.
9785 case elfcpp::R_ARM_MOVT_ABS:
9786 case elfcpp::R_ARM_THM_MOVT_ABS:
9787 case elfcpp::R_ARM_MOVT_PREL:
9788 case elfcpp::R_ARM_MOVT_BREL:
9789 case elfcpp::R_ARM_THM_MOVT_PREL:
9790 case elfcpp::R_ARM_THM_MOVT_BREL:
9791 case elfcpp::R_ARM_ALU_PC_G0_NC:
9792 case elfcpp::R_ARM_ALU_PC_G0:
9793 case elfcpp::R_ARM_ALU_PC_G1_NC:
9794 case elfcpp::R_ARM_ALU_PC_G1:
9795 case elfcpp::R_ARM_ALU_PC_G2:
9796 case elfcpp::R_ARM_ALU_SB_G0_NC:
9797 case elfcpp::R_ARM_ALU_SB_G0:
9798 case elfcpp::R_ARM_ALU_SB_G1_NC:
9799 case elfcpp::R_ARM_ALU_SB_G1:
9800 case elfcpp::R_ARM_ALU_SB_G2:
9801 case elfcpp::R_ARM_LDR_PC_G0:
9802 case elfcpp::R_ARM_LDR_PC_G1:
9803 case elfcpp::R_ARM_LDR_PC_G2:
9804 case elfcpp::R_ARM_LDR_SB_G0:
9805 case elfcpp::R_ARM_LDR_SB_G1:
9806 case elfcpp::R_ARM_LDR_SB_G2:
9807 case elfcpp::R_ARM_LDRS_PC_G0:
9808 case elfcpp::R_ARM_LDRS_PC_G1:
9809 case elfcpp::R_ARM_LDRS_PC_G2:
9810 case elfcpp::R_ARM_LDRS_SB_G0:
9811 case elfcpp::R_ARM_LDRS_SB_G1:
9812 case elfcpp::R_ARM_LDRS_SB_G2:
9813 case elfcpp::R_ARM_LDC_PC_G0:
9814 case elfcpp::R_ARM_LDC_PC_G1:
9815 case elfcpp::R_ARM_LDC_PC_G2:
9816 case elfcpp::R_ARM_LDC_SB_G0:
9817 case elfcpp::R_ARM_LDC_SB_G1:
9818 case elfcpp::R_ARM_LDC_SB_G2:
9819 gold_error(_("cannot handle %s in a relocatable link"),
9820 arp->name().c_str());
9821 break;
9823 default:
9824 gold_unreachable();
9827 // Report any errors.
9828 switch (reloc_status)
9830 case Arm_relocate_functions<big_endian>::STATUS_OKAY:
9831 break;
9832 case Arm_relocate_functions<big_endian>::STATUS_OVERFLOW:
9833 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
9834 _("relocation overflow in %s"),
9835 arp->name().c_str());
9836 break;
9837 case Arm_relocate_functions<big_endian>::STATUS_BAD_RELOC:
9838 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
9839 _("unexpected opcode while processing relocation %s"),
9840 arp->name().c_str());
9841 break;
9842 default:
9843 gold_unreachable();
9847 // Return the value to use for a dynamic symbol which requires special
9848 // treatment. This is how we support equality comparisons of function
9849 // pointers across shared library boundaries, as described in the
9850 // processor specific ABI supplement.
9852 template<bool big_endian>
9853 uint64_t
9854 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
9856 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
9857 return this->plt_section()->address() + gsym->plt_offset();
9860 // Map platform-specific relocs to real relocs
9862 template<bool big_endian>
9863 unsigned int
9864 Target_arm<big_endian>::get_real_reloc_type(unsigned int r_type)
9866 switch (r_type)
9868 case elfcpp::R_ARM_TARGET1:
9869 // This is either R_ARM_ABS32 or R_ARM_REL32;
9870 return elfcpp::R_ARM_ABS32;
9872 case elfcpp::R_ARM_TARGET2:
9873 // This can be any reloc type but ususally is R_ARM_GOT_PREL
9874 return elfcpp::R_ARM_GOT_PREL;
9876 default:
9877 return r_type;
9881 // Whether if two EABI versions V1 and V2 are compatible.
9883 template<bool big_endian>
9884 bool
9885 Target_arm<big_endian>::are_eabi_versions_compatible(
9886 elfcpp::Elf_Word v1,
9887 elfcpp::Elf_Word v2)
9889 // v4 and v5 are the same spec before and after it was released,
9890 // so allow mixing them.
9891 if ((v1 == elfcpp::EF_ARM_EABI_UNKNOWN || v2 == elfcpp::EF_ARM_EABI_UNKNOWN)
9892 || (v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
9893 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
9894 return true;
9896 return v1 == v2;
9899 // Combine FLAGS from an input object called NAME and the processor-specific
9900 // flags in the ELF header of the output. Much of this is adapted from the
9901 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
9902 // in bfd/elf32-arm.c.
9904 template<bool big_endian>
9905 void
9906 Target_arm<big_endian>::merge_processor_specific_flags(
9907 const std::string& name,
9908 elfcpp::Elf_Word flags)
9910 if (this->are_processor_specific_flags_set())
9912 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
9914 // Nothing to merge if flags equal to those in output.
9915 if (flags == out_flags)
9916 return;
9918 // Complain about various flag mismatches.
9919 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
9920 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
9921 if (!this->are_eabi_versions_compatible(version1, version2)
9922 && parameters->options().warn_mismatch())
9923 gold_error(_("Source object %s has EABI version %d but output has "
9924 "EABI version %d."),
9925 name.c_str(),
9926 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
9927 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
9929 else
9931 // If the input is the default architecture and had the default
9932 // flags then do not bother setting the flags for the output
9933 // architecture, instead allow future merges to do this. If no
9934 // future merges ever set these flags then they will retain their
9935 // uninitialised values, which surprise surprise, correspond
9936 // to the default values.
9937 if (flags == 0)
9938 return;
9940 // This is the first time, just copy the flags.
9941 // We only copy the EABI version for now.
9942 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
9946 // Adjust ELF file header.
9947 template<bool big_endian>
9948 void
9949 Target_arm<big_endian>::do_adjust_elf_header(
9950 unsigned char* view,
9951 int len) const
9953 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
9955 elfcpp::Ehdr<32, big_endian> ehdr(view);
9956 unsigned char e_ident[elfcpp::EI_NIDENT];
9957 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9959 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
9960 == elfcpp::EF_ARM_EABI_UNKNOWN)
9961 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
9962 else
9963 e_ident[elfcpp::EI_OSABI] = 0;
9964 e_ident[elfcpp::EI_ABIVERSION] = 0;
9966 // FIXME: Do EF_ARM_BE8 adjustment.
9968 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
9969 oehdr.put_e_ident(e_ident);
9972 // do_make_elf_object to override the same function in the base class.
9973 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
9974 // to store ARM specific information. Hence we need to have our own
9975 // ELF object creation.
9977 template<bool big_endian>
9978 Object*
9979 Target_arm<big_endian>::do_make_elf_object(
9980 const std::string& name,
9981 Input_file* input_file,
9982 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
9984 int et = ehdr.get_e_type();
9985 if (et == elfcpp::ET_REL)
9987 Arm_relobj<big_endian>* obj =
9988 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
9989 obj->setup();
9990 return obj;
9992 else if (et == elfcpp::ET_DYN)
9994 Sized_dynobj<32, big_endian>* obj =
9995 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
9996 obj->setup();
9997 return obj;
9999 else
10001 gold_error(_("%s: unsupported ELF file type %d"),
10002 name.c_str(), et);
10003 return NULL;
10007 // Read the architecture from the Tag_also_compatible_with attribute, if any.
10008 // Returns -1 if no architecture could be read.
10009 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
10011 template<bool big_endian>
10013 Target_arm<big_endian>::get_secondary_compatible_arch(
10014 const Attributes_section_data* pasd)
10016 const Object_attribute* known_attributes =
10017 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
10019 // Note: the tag and its argument below are uleb128 values, though
10020 // currently-defined values fit in one byte for each.
10021 const std::string& sv =
10022 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
10023 if (sv.size() == 2
10024 && sv.data()[0] == elfcpp::Tag_CPU_arch
10025 && (sv.data()[1] & 128) != 128)
10026 return sv.data()[1];
10028 // This tag is "safely ignorable", so don't complain if it looks funny.
10029 return -1;
10032 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
10033 // The tag is removed if ARCH is -1.
10034 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
10036 template<bool big_endian>
10037 void
10038 Target_arm<big_endian>::set_secondary_compatible_arch(
10039 Attributes_section_data* pasd,
10040 int arch)
10042 Object_attribute* known_attributes =
10043 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
10045 if (arch == -1)
10047 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
10048 return;
10051 // Note: the tag and its argument below are uleb128 values, though
10052 // currently-defined values fit in one byte for each.
10053 char sv[3];
10054 sv[0] = elfcpp::Tag_CPU_arch;
10055 gold_assert(arch != 0);
10056 sv[1] = arch;
10057 sv[2] = '\0';
10059 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
10062 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
10063 // into account.
10064 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
10066 template<bool big_endian>
10068 Target_arm<big_endian>::tag_cpu_arch_combine(
10069 const char* name,
10070 int oldtag,
10071 int* secondary_compat_out,
10072 int newtag,
10073 int secondary_compat)
10075 #define T(X) elfcpp::TAG_CPU_ARCH_##X
10076 static const int v6t2[] =
10078 T(V6T2), // PRE_V4.
10079 T(V6T2), // V4.
10080 T(V6T2), // V4T.
10081 T(V6T2), // V5T.
10082 T(V6T2), // V5TE.
10083 T(V6T2), // V5TEJ.
10084 T(V6T2), // V6.
10085 T(V7), // V6KZ.
10086 T(V6T2) // V6T2.
10088 static const int v6k[] =
10090 T(V6K), // PRE_V4.
10091 T(V6K), // V4.
10092 T(V6K), // V4T.
10093 T(V6K), // V5T.
10094 T(V6K), // V5TE.
10095 T(V6K), // V5TEJ.
10096 T(V6K), // V6.
10097 T(V6KZ), // V6KZ.
10098 T(V7), // V6T2.
10099 T(V6K) // V6K.
10101 static const int v7[] =
10103 T(V7), // PRE_V4.
10104 T(V7), // V4.
10105 T(V7), // V4T.
10106 T(V7), // V5T.
10107 T(V7), // V5TE.
10108 T(V7), // V5TEJ.
10109 T(V7), // V6.
10110 T(V7), // V6KZ.
10111 T(V7), // V6T2.
10112 T(V7), // V6K.
10113 T(V7) // V7.
10115 static const int v6_m[] =
10117 -1, // PRE_V4.
10118 -1, // V4.
10119 T(V6K), // V4T.
10120 T(V6K), // V5T.
10121 T(V6K), // V5TE.
10122 T(V6K), // V5TEJ.
10123 T(V6K), // V6.
10124 T(V6KZ), // V6KZ.
10125 T(V7), // V6T2.
10126 T(V6K), // V6K.
10127 T(V7), // V7.
10128 T(V6_M) // V6_M.
10130 static const int v6s_m[] =
10132 -1, // PRE_V4.
10133 -1, // V4.
10134 T(V6K), // V4T.
10135 T(V6K), // V5T.
10136 T(V6K), // V5TE.
10137 T(V6K), // V5TEJ.
10138 T(V6K), // V6.
10139 T(V6KZ), // V6KZ.
10140 T(V7), // V6T2.
10141 T(V6K), // V6K.
10142 T(V7), // V7.
10143 T(V6S_M), // V6_M.
10144 T(V6S_M) // V6S_M.
10146 static const int v7e_m[] =
10148 -1, // PRE_V4.
10149 -1, // V4.
10150 T(V7E_M), // V4T.
10151 T(V7E_M), // V5T.
10152 T(V7E_M), // V5TE.
10153 T(V7E_M), // V5TEJ.
10154 T(V7E_M), // V6.
10155 T(V7E_M), // V6KZ.
10156 T(V7E_M), // V6T2.
10157 T(V7E_M), // V6K.
10158 T(V7E_M), // V7.
10159 T(V7E_M), // V6_M.
10160 T(V7E_M), // V6S_M.
10161 T(V7E_M) // V7E_M.
10163 static const int v4t_plus_v6_m[] =
10165 -1, // PRE_V4.
10166 -1, // V4.
10167 T(V4T), // V4T.
10168 T(V5T), // V5T.
10169 T(V5TE), // V5TE.
10170 T(V5TEJ), // V5TEJ.
10171 T(V6), // V6.
10172 T(V6KZ), // V6KZ.
10173 T(V6T2), // V6T2.
10174 T(V6K), // V6K.
10175 T(V7), // V7.
10176 T(V6_M), // V6_M.
10177 T(V6S_M), // V6S_M.
10178 T(V7E_M), // V7E_M.
10179 T(V4T_PLUS_V6_M) // V4T plus V6_M.
10181 static const int* comb[] =
10183 v6t2,
10184 v6k,
10186 v6_m,
10187 v6s_m,
10188 v7e_m,
10189 // Pseudo-architecture.
10190 v4t_plus_v6_m
10193 // Check we've not got a higher architecture than we know about.
10195 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
10197 gold_error(_("%s: unknown CPU architecture"), name);
10198 return -1;
10201 // Override old tag if we have a Tag_also_compatible_with on the output.
10203 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
10204 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
10205 oldtag = T(V4T_PLUS_V6_M);
10207 // And override the new tag if we have a Tag_also_compatible_with on the
10208 // input.
10210 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
10211 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
10212 newtag = T(V4T_PLUS_V6_M);
10214 // Architectures before V6KZ add features monotonically.
10215 int tagh = std::max(oldtag, newtag);
10216 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
10217 return tagh;
10219 int tagl = std::min(oldtag, newtag);
10220 int result = comb[tagh - T(V6T2)][tagl];
10222 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
10223 // as the canonical version.
10224 if (result == T(V4T_PLUS_V6_M))
10226 result = T(V4T);
10227 *secondary_compat_out = T(V6_M);
10229 else
10230 *secondary_compat_out = -1;
10232 if (result == -1)
10234 gold_error(_("%s: conflicting CPU architectures %d/%d"),
10235 name, oldtag, newtag);
10236 return -1;
10239 return result;
10240 #undef T
10243 // Helper to print AEABI enum tag value.
10245 template<bool big_endian>
10246 std::string
10247 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
10249 static const char* aeabi_enum_names[] =
10250 { "", "variable-size", "32-bit", "" };
10251 const size_t aeabi_enum_names_size =
10252 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
10254 if (value < aeabi_enum_names_size)
10255 return std::string(aeabi_enum_names[value]);
10256 else
10258 char buffer[100];
10259 sprintf(buffer, "<unknown value %u>", value);
10260 return std::string(buffer);
10264 // Return the string value to store in TAG_CPU_name.
10266 template<bool big_endian>
10267 std::string
10268 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
10270 static const char* name_table[] = {
10271 // These aren't real CPU names, but we can't guess
10272 // that from the architecture version alone.
10273 "Pre v4",
10274 "ARM v4",
10275 "ARM v4T",
10276 "ARM v5T",
10277 "ARM v5TE",
10278 "ARM v5TEJ",
10279 "ARM v6",
10280 "ARM v6KZ",
10281 "ARM v6T2",
10282 "ARM v6K",
10283 "ARM v7",
10284 "ARM v6-M",
10285 "ARM v6S-M",
10286 "ARM v7E-M"
10288 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
10290 if (value < name_table_size)
10291 return std::string(name_table[value]);
10292 else
10294 char buffer[100];
10295 sprintf(buffer, "<unknown CPU value %u>", value);
10296 return std::string(buffer);
10300 // Merge object attributes from input file called NAME with those of the
10301 // output. The input object attributes are in the object pointed by PASD.
10303 template<bool big_endian>
10304 void
10305 Target_arm<big_endian>::merge_object_attributes(
10306 const char* name,
10307 const Attributes_section_data* pasd)
10309 // Return if there is no attributes section data.
10310 if (pasd == NULL)
10311 return;
10313 // If output has no object attributes, just copy.
10314 const int vendor = Object_attribute::OBJ_ATTR_PROC;
10315 if (this->attributes_section_data_ == NULL)
10317 this->attributes_section_data_ = new Attributes_section_data(*pasd);
10318 Object_attribute* out_attr =
10319 this->attributes_section_data_->known_attributes(vendor);
10321 // We do not output objects with Tag_MPextension_use_legacy - we move
10322 // the attribute's value to Tag_MPextension_use. */
10323 if (out_attr[elfcpp::Tag_MPextension_use_legacy].int_value() != 0)
10325 if (out_attr[elfcpp::Tag_MPextension_use].int_value() != 0
10326 && out_attr[elfcpp::Tag_MPextension_use_legacy].int_value()
10327 != out_attr[elfcpp::Tag_MPextension_use].int_value())
10329 gold_error(_("%s has both the current and legacy "
10330 "Tag_MPextension_use attributes"),
10331 name);
10334 out_attr[elfcpp::Tag_MPextension_use] =
10335 out_attr[elfcpp::Tag_MPextension_use_legacy];
10336 out_attr[elfcpp::Tag_MPextension_use_legacy].set_type(0);
10337 out_attr[elfcpp::Tag_MPextension_use_legacy].set_int_value(0);
10340 return;
10343 const Object_attribute* in_attr = pasd->known_attributes(vendor);
10344 Object_attribute* out_attr =
10345 this->attributes_section_data_->known_attributes(vendor);
10347 // This needs to happen before Tag_ABI_FP_number_model is merged. */
10348 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
10349 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
10351 // Ignore mismatches if the object doesn't use floating point. */
10352 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
10353 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
10354 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
10355 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0
10356 && parameters->options().warn_mismatch())
10357 gold_error(_("%s uses VFP register arguments, output does not"),
10358 name);
10361 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
10363 // Merge this attribute with existing attributes.
10364 switch (i)
10366 case elfcpp::Tag_CPU_raw_name:
10367 case elfcpp::Tag_CPU_name:
10368 // These are merged after Tag_CPU_arch.
10369 break;
10371 case elfcpp::Tag_ABI_optimization_goals:
10372 case elfcpp::Tag_ABI_FP_optimization_goals:
10373 // Use the first value seen.
10374 break;
10376 case elfcpp::Tag_CPU_arch:
10378 unsigned int saved_out_attr = out_attr->int_value();
10379 // Merge Tag_CPU_arch and Tag_also_compatible_with.
10380 int secondary_compat =
10381 this->get_secondary_compatible_arch(pasd);
10382 int secondary_compat_out =
10383 this->get_secondary_compatible_arch(
10384 this->attributes_section_data_);
10385 out_attr[i].set_int_value(
10386 tag_cpu_arch_combine(name, out_attr[i].int_value(),
10387 &secondary_compat_out,
10388 in_attr[i].int_value(),
10389 secondary_compat));
10390 this->set_secondary_compatible_arch(this->attributes_section_data_,
10391 secondary_compat_out);
10393 // Merge Tag_CPU_name and Tag_CPU_raw_name.
10394 if (out_attr[i].int_value() == saved_out_attr)
10395 ; // Leave the names alone.
10396 else if (out_attr[i].int_value() == in_attr[i].int_value())
10398 // The output architecture has been changed to match the
10399 // input architecture. Use the input names.
10400 out_attr[elfcpp::Tag_CPU_name].set_string_value(
10401 in_attr[elfcpp::Tag_CPU_name].string_value());
10402 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
10403 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
10405 else
10407 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
10408 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
10411 // If we still don't have a value for Tag_CPU_name,
10412 // make one up now. Tag_CPU_raw_name remains blank.
10413 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
10415 const std::string cpu_name =
10416 this->tag_cpu_name_value(out_attr[i].int_value());
10417 // FIXME: If we see an unknown CPU, this will be set
10418 // to "<unknown CPU n>", where n is the attribute value.
10419 // This is different from BFD, which leaves the name alone.
10420 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
10423 break;
10425 case elfcpp::Tag_ARM_ISA_use:
10426 case elfcpp::Tag_THUMB_ISA_use:
10427 case elfcpp::Tag_WMMX_arch:
10428 case elfcpp::Tag_Advanced_SIMD_arch:
10429 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
10430 case elfcpp::Tag_ABI_FP_rounding:
10431 case elfcpp::Tag_ABI_FP_exceptions:
10432 case elfcpp::Tag_ABI_FP_user_exceptions:
10433 case elfcpp::Tag_ABI_FP_number_model:
10434 case elfcpp::Tag_VFP_HP_extension:
10435 case elfcpp::Tag_CPU_unaligned_access:
10436 case elfcpp::Tag_T2EE_use:
10437 case elfcpp::Tag_Virtualization_use:
10438 case elfcpp::Tag_MPextension_use:
10439 // Use the largest value specified.
10440 if (in_attr[i].int_value() > out_attr[i].int_value())
10441 out_attr[i].set_int_value(in_attr[i].int_value());
10442 break;
10444 case elfcpp::Tag_ABI_align8_preserved:
10445 case elfcpp::Tag_ABI_PCS_RO_data:
10446 // Use the smallest value specified.
10447 if (in_attr[i].int_value() < out_attr[i].int_value())
10448 out_attr[i].set_int_value(in_attr[i].int_value());
10449 break;
10451 case elfcpp::Tag_ABI_align8_needed:
10452 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
10453 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
10454 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
10455 == 0)))
10457 // This error message should be enabled once all non-conformant
10458 // binaries in the toolchain have had the attributes set
10459 // properly.
10460 // gold_error(_("output 8-byte data alignment conflicts with %s"),
10461 // name);
10463 // Fall through.
10464 case elfcpp::Tag_ABI_FP_denormal:
10465 case elfcpp::Tag_ABI_PCS_GOT_use:
10467 // These tags have 0 = don't care, 1 = strong requirement,
10468 // 2 = weak requirement.
10469 static const int order_021[3] = {0, 2, 1};
10471 // Use the "greatest" from the sequence 0, 2, 1, or the largest
10472 // value if greater than 2 (for future-proofing).
10473 if ((in_attr[i].int_value() > 2
10474 && in_attr[i].int_value() > out_attr[i].int_value())
10475 || (in_attr[i].int_value() <= 2
10476 && out_attr[i].int_value() <= 2
10477 && (order_021[in_attr[i].int_value()]
10478 > order_021[out_attr[i].int_value()])))
10479 out_attr[i].set_int_value(in_attr[i].int_value());
10481 break;
10483 case elfcpp::Tag_CPU_arch_profile:
10484 if (out_attr[i].int_value() != in_attr[i].int_value())
10486 // 0 will merge with anything.
10487 // 'A' and 'S' merge to 'A'.
10488 // 'R' and 'S' merge to 'R'.
10489 // 'M' and 'A|R|S' is an error.
10490 if (out_attr[i].int_value() == 0
10491 || (out_attr[i].int_value() == 'S'
10492 && (in_attr[i].int_value() == 'A'
10493 || in_attr[i].int_value() == 'R')))
10494 out_attr[i].set_int_value(in_attr[i].int_value());
10495 else if (in_attr[i].int_value() == 0
10496 || (in_attr[i].int_value() == 'S'
10497 && (out_attr[i].int_value() == 'A'
10498 || out_attr[i].int_value() == 'R')))
10499 ; // Do nothing.
10500 else if (parameters->options().warn_mismatch())
10502 gold_error
10503 (_("conflicting architecture profiles %c/%c"),
10504 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
10505 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
10508 break;
10509 case elfcpp::Tag_VFP_arch:
10511 static const struct
10513 int ver;
10514 int regs;
10515 } vfp_versions[7] =
10517 {0, 0},
10518 {1, 16},
10519 {2, 16},
10520 {3, 32},
10521 {3, 16},
10522 {4, 32},
10523 {4, 16}
10526 // Values greater than 6 aren't defined, so just pick the
10527 // biggest.
10528 if (in_attr[i].int_value() > 6
10529 && in_attr[i].int_value() > out_attr[i].int_value())
10531 *out_attr = *in_attr;
10532 break;
10534 // The output uses the superset of input features
10535 // (ISA version) and registers.
10536 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
10537 vfp_versions[out_attr[i].int_value()].ver);
10538 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
10539 vfp_versions[out_attr[i].int_value()].regs);
10540 // This assumes all possible supersets are also a valid
10541 // options.
10542 int newval;
10543 for (newval = 6; newval > 0; newval--)
10545 if (regs == vfp_versions[newval].regs
10546 && ver == vfp_versions[newval].ver)
10547 break;
10549 out_attr[i].set_int_value(newval);
10551 break;
10552 case elfcpp::Tag_PCS_config:
10553 if (out_attr[i].int_value() == 0)
10554 out_attr[i].set_int_value(in_attr[i].int_value());
10555 else if (in_attr[i].int_value() != 0
10556 && out_attr[i].int_value() != 0
10557 && parameters->options().warn_mismatch())
10559 // It's sometimes ok to mix different configs, so this is only
10560 // a warning.
10561 gold_warning(_("%s: conflicting platform configuration"), name);
10563 break;
10564 case elfcpp::Tag_ABI_PCS_R9_use:
10565 if (in_attr[i].int_value() != out_attr[i].int_value()
10566 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
10567 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused
10568 && parameters->options().warn_mismatch())
10570 gold_error(_("%s: conflicting use of R9"), name);
10572 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
10573 out_attr[i].set_int_value(in_attr[i].int_value());
10574 break;
10575 case elfcpp::Tag_ABI_PCS_RW_data:
10576 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
10577 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
10578 != elfcpp::AEABI_R9_SB)
10579 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
10580 != elfcpp::AEABI_R9_unused)
10581 && parameters->options().warn_mismatch())
10583 gold_error(_("%s: SB relative addressing conflicts with use "
10584 "of R9"),
10585 name);
10587 // Use the smallest value specified.
10588 if (in_attr[i].int_value() < out_attr[i].int_value())
10589 out_attr[i].set_int_value(in_attr[i].int_value());
10590 break;
10591 case elfcpp::Tag_ABI_PCS_wchar_t:
10592 if (out_attr[i].int_value()
10593 && in_attr[i].int_value()
10594 && out_attr[i].int_value() != in_attr[i].int_value()
10595 && parameters->options().warn_mismatch()
10596 && parameters->options().wchar_size_warning())
10598 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
10599 "use %u-byte wchar_t; use of wchar_t values "
10600 "across objects may fail"),
10601 name, in_attr[i].int_value(),
10602 out_attr[i].int_value());
10604 else if (in_attr[i].int_value() && !out_attr[i].int_value())
10605 out_attr[i].set_int_value(in_attr[i].int_value());
10606 break;
10607 case elfcpp::Tag_ABI_enum_size:
10608 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
10610 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
10611 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
10613 // The existing object is compatible with anything.
10614 // Use whatever requirements the new object has.
10615 out_attr[i].set_int_value(in_attr[i].int_value());
10617 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
10618 && out_attr[i].int_value() != in_attr[i].int_value()
10619 && parameters->options().warn_mismatch()
10620 && parameters->options().enum_size_warning())
10622 unsigned int in_value = in_attr[i].int_value();
10623 unsigned int out_value = out_attr[i].int_value();
10624 gold_warning(_("%s uses %s enums yet the output is to use "
10625 "%s enums; use of enum values across objects "
10626 "may fail"),
10627 name,
10628 this->aeabi_enum_name(in_value).c_str(),
10629 this->aeabi_enum_name(out_value).c_str());
10632 break;
10633 case elfcpp::Tag_ABI_VFP_args:
10634 // Aready done.
10635 break;
10636 case elfcpp::Tag_ABI_WMMX_args:
10637 if (in_attr[i].int_value() != out_attr[i].int_value()
10638 && parameters->options().warn_mismatch())
10640 gold_error(_("%s uses iWMMXt register arguments, output does "
10641 "not"),
10642 name);
10644 break;
10645 case Object_attribute::Tag_compatibility:
10646 // Merged in target-independent code.
10647 break;
10648 case elfcpp::Tag_ABI_HardFP_use:
10649 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
10650 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
10651 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
10652 out_attr[i].set_int_value(3);
10653 else if (in_attr[i].int_value() > out_attr[i].int_value())
10654 out_attr[i].set_int_value(in_attr[i].int_value());
10655 break;
10656 case elfcpp::Tag_ABI_FP_16bit_format:
10657 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
10659 if (in_attr[i].int_value() != out_attr[i].int_value()
10660 && parameters->options().warn_mismatch())
10661 gold_error(_("fp16 format mismatch between %s and output"),
10662 name);
10664 if (in_attr[i].int_value() != 0)
10665 out_attr[i].set_int_value(in_attr[i].int_value());
10666 break;
10668 case elfcpp::Tag_DIV_use:
10669 // This tag is set to zero if we can use UDIV and SDIV in Thumb
10670 // mode on a v7-M or v7-R CPU; to one if we can not use UDIV or
10671 // SDIV at all; and to two if we can use UDIV or SDIV on a v7-A
10672 // CPU. We will merge as follows: If the input attribute's value
10673 // is one then the output attribute's value remains unchanged. If
10674 // the input attribute's value is zero or two then if the output
10675 // attribute's value is one the output value is set to the input
10676 // value, otherwise the output value must be the same as the
10677 // inputs. */
10678 if (in_attr[i].int_value() != 1 && out_attr[i].int_value() != 1)
10680 if (in_attr[i].int_value() != out_attr[i].int_value())
10682 gold_error(_("DIV usage mismatch between %s and output"),
10683 name);
10687 if (in_attr[i].int_value() != 1)
10688 out_attr[i].set_int_value(in_attr[i].int_value());
10690 break;
10692 case elfcpp::Tag_MPextension_use_legacy:
10693 // We don't output objects with Tag_MPextension_use_legacy - we
10694 // move the value to Tag_MPextension_use.
10695 if (in_attr[i].int_value() != 0
10696 && in_attr[elfcpp::Tag_MPextension_use].int_value() != 0)
10698 if (in_attr[elfcpp::Tag_MPextension_use].int_value()
10699 != in_attr[i].int_value())
10701 gold_error(_("%s has has both the current and legacy "
10702 "Tag_MPextension_use attributes"),
10703 name);
10707 if (in_attr[i].int_value()
10708 > out_attr[elfcpp::Tag_MPextension_use].int_value())
10709 out_attr[elfcpp::Tag_MPextension_use] = in_attr[i];
10711 break;
10713 case elfcpp::Tag_nodefaults:
10714 // This tag is set if it exists, but the value is unused (and is
10715 // typically zero). We don't actually need to do anything here -
10716 // the merge happens automatically when the type flags are merged
10717 // below.
10718 break;
10719 case elfcpp::Tag_also_compatible_with:
10720 // Already done in Tag_CPU_arch.
10721 break;
10722 case elfcpp::Tag_conformance:
10723 // Keep the attribute if it matches. Throw it away otherwise.
10724 // No attribute means no claim to conform.
10725 if (in_attr[i].string_value() != out_attr[i].string_value())
10726 out_attr[i].set_string_value("");
10727 break;
10729 default:
10731 const char* err_object = NULL;
10733 // The "known_obj_attributes" table does contain some undefined
10734 // attributes. Ensure that there are unused.
10735 if (out_attr[i].int_value() != 0
10736 || out_attr[i].string_value() != "")
10737 err_object = "output";
10738 else if (in_attr[i].int_value() != 0
10739 || in_attr[i].string_value() != "")
10740 err_object = name;
10742 if (err_object != NULL
10743 && parameters->options().warn_mismatch())
10745 // Attribute numbers >=64 (mod 128) can be safely ignored.
10746 if ((i & 127) < 64)
10747 gold_error(_("%s: unknown mandatory EABI object attribute "
10748 "%d"),
10749 err_object, i);
10750 else
10751 gold_warning(_("%s: unknown EABI object attribute %d"),
10752 err_object, i);
10755 // Only pass on attributes that match in both inputs.
10756 if (!in_attr[i].matches(out_attr[i]))
10758 out_attr[i].set_int_value(0);
10759 out_attr[i].set_string_value("");
10764 // If out_attr was copied from in_attr then it won't have a type yet.
10765 if (in_attr[i].type() && !out_attr[i].type())
10766 out_attr[i].set_type(in_attr[i].type());
10769 // Merge Tag_compatibility attributes and any common GNU ones.
10770 this->attributes_section_data_->merge(name, pasd);
10772 // Check for any attributes not known on ARM.
10773 typedef Vendor_object_attributes::Other_attributes Other_attributes;
10774 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
10775 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
10776 Other_attributes* out_other_attributes =
10777 this->attributes_section_data_->other_attributes(vendor);
10778 Other_attributes::iterator out_iter = out_other_attributes->begin();
10780 while (in_iter != in_other_attributes->end()
10781 || out_iter != out_other_attributes->end())
10783 const char* err_object = NULL;
10784 int err_tag = 0;
10786 // The tags for each list are in numerical order.
10787 // If the tags are equal, then merge.
10788 if (out_iter != out_other_attributes->end()
10789 && (in_iter == in_other_attributes->end()
10790 || in_iter->first > out_iter->first))
10792 // This attribute only exists in output. We can't merge, and we
10793 // don't know what the tag means, so delete it.
10794 err_object = "output";
10795 err_tag = out_iter->first;
10796 int saved_tag = out_iter->first;
10797 delete out_iter->second;
10798 out_other_attributes->erase(out_iter);
10799 out_iter = out_other_attributes->upper_bound(saved_tag);
10801 else if (in_iter != in_other_attributes->end()
10802 && (out_iter != out_other_attributes->end()
10803 || in_iter->first < out_iter->first))
10805 // This attribute only exists in input. We can't merge, and we
10806 // don't know what the tag means, so ignore it.
10807 err_object = name;
10808 err_tag = in_iter->first;
10809 ++in_iter;
10811 else // The tags are equal.
10813 // As present, all attributes in the list are unknown, and
10814 // therefore can't be merged meaningfully.
10815 err_object = "output";
10816 err_tag = out_iter->first;
10818 // Only pass on attributes that match in both inputs.
10819 if (!in_iter->second->matches(*(out_iter->second)))
10821 // No match. Delete the attribute.
10822 int saved_tag = out_iter->first;
10823 delete out_iter->second;
10824 out_other_attributes->erase(out_iter);
10825 out_iter = out_other_attributes->upper_bound(saved_tag);
10827 else
10829 // Matched. Keep the attribute and move to the next.
10830 ++out_iter;
10831 ++in_iter;
10835 if (err_object && parameters->options().warn_mismatch())
10837 // Attribute numbers >=64 (mod 128) can be safely ignored. */
10838 if ((err_tag & 127) < 64)
10840 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
10841 err_object, err_tag);
10843 else
10845 gold_warning(_("%s: unknown EABI object attribute %d"),
10846 err_object, err_tag);
10852 // Stub-generation methods for Target_arm.
10854 // Make a new Arm_input_section object.
10856 template<bool big_endian>
10857 Arm_input_section<big_endian>*
10858 Target_arm<big_endian>::new_arm_input_section(
10859 Relobj* relobj,
10860 unsigned int shndx)
10862 Section_id sid(relobj, shndx);
10864 Arm_input_section<big_endian>* arm_input_section =
10865 new Arm_input_section<big_endian>(relobj, shndx);
10866 arm_input_section->init();
10868 // Register new Arm_input_section in map for look-up.
10869 std::pair<typename Arm_input_section_map::iterator, bool> ins =
10870 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
10872 // Make sure that it we have not created another Arm_input_section
10873 // for this input section already.
10874 gold_assert(ins.second);
10876 return arm_input_section;
10879 // Find the Arm_input_section object corresponding to the SHNDX-th input
10880 // section of RELOBJ.
10882 template<bool big_endian>
10883 Arm_input_section<big_endian>*
10884 Target_arm<big_endian>::find_arm_input_section(
10885 Relobj* relobj,
10886 unsigned int shndx) const
10888 Section_id sid(relobj, shndx);
10889 typename Arm_input_section_map::const_iterator p =
10890 this->arm_input_section_map_.find(sid);
10891 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
10894 // Make a new stub table.
10896 template<bool big_endian>
10897 Stub_table<big_endian>*
10898 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
10900 Stub_table<big_endian>* stub_table =
10901 new Stub_table<big_endian>(owner);
10902 this->stub_tables_.push_back(stub_table);
10904 stub_table->set_address(owner->address() + owner->data_size());
10905 stub_table->set_file_offset(owner->offset() + owner->data_size());
10906 stub_table->finalize_data_size();
10908 return stub_table;
10911 // Scan a relocation for stub generation.
10913 template<bool big_endian>
10914 void
10915 Target_arm<big_endian>::scan_reloc_for_stub(
10916 const Relocate_info<32, big_endian>* relinfo,
10917 unsigned int r_type,
10918 const Sized_symbol<32>* gsym,
10919 unsigned int r_sym,
10920 const Symbol_value<32>* psymval,
10921 elfcpp::Elf_types<32>::Elf_Swxword addend,
10922 Arm_address address)
10924 typedef typename Target_arm<big_endian>::Relocate Relocate;
10926 const Arm_relobj<big_endian>* arm_relobj =
10927 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10929 bool target_is_thumb;
10930 Symbol_value<32> symval;
10931 if (gsym != NULL)
10933 // This is a global symbol. Determine if we use PLT and if the
10934 // final target is THUMB.
10935 if (gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
10937 // This uses a PLT, change the symbol value.
10938 symval.set_output_value(this->plt_section()->address()
10939 + gsym->plt_offset());
10940 psymval = &symval;
10941 target_is_thumb = false;
10943 else if (gsym->is_undefined())
10944 // There is no need to generate a stub symbol is undefined.
10945 return;
10946 else
10948 target_is_thumb =
10949 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
10950 || (gsym->type() == elfcpp::STT_FUNC
10951 && !gsym->is_undefined()
10952 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
10955 else
10957 // This is a local symbol. Determine if the final target is THUMB.
10958 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
10961 // Strip LSB if this points to a THUMB target.
10962 const Arm_reloc_property* reloc_property =
10963 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
10964 gold_assert(reloc_property != NULL);
10965 if (target_is_thumb
10966 && reloc_property->uses_thumb_bit()
10967 && ((psymval->value(arm_relobj, 0) & 1) != 0))
10969 Arm_address stripped_value =
10970 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
10971 symval.set_output_value(stripped_value);
10972 psymval = &symval;
10975 // Get the symbol value.
10976 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
10978 // Owing to pipelining, the PC relative branches below actually skip
10979 // two instructions when the branch offset is 0.
10980 Arm_address destination;
10981 switch (r_type)
10983 case elfcpp::R_ARM_CALL:
10984 case elfcpp::R_ARM_JUMP24:
10985 case elfcpp::R_ARM_PLT32:
10986 // ARM branches.
10987 destination = value + addend + 8;
10988 break;
10989 case elfcpp::R_ARM_THM_CALL:
10990 case elfcpp::R_ARM_THM_XPC22:
10991 case elfcpp::R_ARM_THM_JUMP24:
10992 case elfcpp::R_ARM_THM_JUMP19:
10993 // THUMB branches.
10994 destination = value + addend + 4;
10995 break;
10996 default:
10997 gold_unreachable();
11000 Reloc_stub* stub = NULL;
11001 Stub_type stub_type =
11002 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
11003 target_is_thumb);
11004 if (stub_type != arm_stub_none)
11006 // Try looking up an existing stub from a stub table.
11007 Stub_table<big_endian>* stub_table =
11008 arm_relobj->stub_table(relinfo->data_shndx);
11009 gold_assert(stub_table != NULL);
11011 // Locate stub by destination.
11012 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
11014 // Create a stub if there is not one already
11015 stub = stub_table->find_reloc_stub(stub_key);
11016 if (stub == NULL)
11018 // create a new stub and add it to stub table.
11019 stub = this->stub_factory().make_reloc_stub(stub_type);
11020 stub_table->add_reloc_stub(stub, stub_key);
11023 // Record the destination address.
11024 stub->set_destination_address(destination
11025 | (target_is_thumb ? 1 : 0));
11028 // For Cortex-A8, we need to record a relocation at 4K page boundary.
11029 if (this->fix_cortex_a8_
11030 && (r_type == elfcpp::R_ARM_THM_JUMP24
11031 || r_type == elfcpp::R_ARM_THM_JUMP19
11032 || r_type == elfcpp::R_ARM_THM_CALL
11033 || r_type == elfcpp::R_ARM_THM_XPC22)
11034 && (address & 0xfffU) == 0xffeU)
11036 // Found a candidate. Note we haven't checked the destination is
11037 // within 4K here: if we do so (and don't create a record) we can't
11038 // tell that a branch should have been relocated when scanning later.
11039 this->cortex_a8_relocs_info_[address] =
11040 new Cortex_a8_reloc(stub, r_type,
11041 destination | (target_is_thumb ? 1 : 0));
11045 // This function scans a relocation sections for stub generation.
11046 // The template parameter Relocate must be a class type which provides
11047 // a single function, relocate(), which implements the machine
11048 // specific part of a relocation.
11050 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
11051 // SHT_REL or SHT_RELA.
11053 // PRELOCS points to the relocation data. RELOC_COUNT is the number
11054 // of relocs. OUTPUT_SECTION is the output section.
11055 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
11056 // mapped to output offsets.
11058 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
11059 // VIEW_SIZE is the size. These refer to the input section, unless
11060 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
11061 // the output section.
11063 template<bool big_endian>
11064 template<int sh_type>
11065 void inline
11066 Target_arm<big_endian>::scan_reloc_section_for_stubs(
11067 const Relocate_info<32, big_endian>* relinfo,
11068 const unsigned char* prelocs,
11069 size_t reloc_count,
11070 Output_section* output_section,
11071 bool needs_special_offset_handling,
11072 const unsigned char* view,
11073 elfcpp::Elf_types<32>::Elf_Addr view_address,
11074 section_size_type)
11076 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
11077 const int reloc_size =
11078 Reloc_types<sh_type, 32, big_endian>::reloc_size;
11080 Arm_relobj<big_endian>* arm_object =
11081 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
11082 unsigned int local_count = arm_object->local_symbol_count();
11084 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
11086 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
11088 Reltype reloc(prelocs);
11090 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
11091 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
11092 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
11094 r_type = this->get_real_reloc_type(r_type);
11096 // Only a few relocation types need stubs.
11097 if ((r_type != elfcpp::R_ARM_CALL)
11098 && (r_type != elfcpp::R_ARM_JUMP24)
11099 && (r_type != elfcpp::R_ARM_PLT32)
11100 && (r_type != elfcpp::R_ARM_THM_CALL)
11101 && (r_type != elfcpp::R_ARM_THM_XPC22)
11102 && (r_type != elfcpp::R_ARM_THM_JUMP24)
11103 && (r_type != elfcpp::R_ARM_THM_JUMP19)
11104 && (r_type != elfcpp::R_ARM_V4BX))
11105 continue;
11107 section_offset_type offset =
11108 convert_to_section_size_type(reloc.get_r_offset());
11110 if (needs_special_offset_handling)
11112 offset = output_section->output_offset(relinfo->object,
11113 relinfo->data_shndx,
11114 offset);
11115 if (offset == -1)
11116 continue;
11119 // Create a v4bx stub if --fix-v4bx-interworking is used.
11120 if (r_type == elfcpp::R_ARM_V4BX)
11122 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING)
11124 // Get the BX instruction.
11125 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
11126 const Valtype* wv =
11127 reinterpret_cast<const Valtype*>(view + offset);
11128 elfcpp::Elf_types<32>::Elf_Swxword insn =
11129 elfcpp::Swap<32, big_endian>::readval(wv);
11130 const uint32_t reg = (insn & 0xf);
11132 if (reg < 0xf)
11134 // Try looking up an existing stub from a stub table.
11135 Stub_table<big_endian>* stub_table =
11136 arm_object->stub_table(relinfo->data_shndx);
11137 gold_assert(stub_table != NULL);
11139 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
11141 // create a new stub and add it to stub table.
11142 Arm_v4bx_stub* stub =
11143 this->stub_factory().make_arm_v4bx_stub(reg);
11144 gold_assert(stub != NULL);
11145 stub_table->add_arm_v4bx_stub(stub);
11149 continue;
11152 // Get the addend.
11153 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
11154 elfcpp::Elf_types<32>::Elf_Swxword addend =
11155 stub_addend_reader(r_type, view + offset, reloc);
11157 const Sized_symbol<32>* sym;
11159 Symbol_value<32> symval;
11160 const Symbol_value<32> *psymval;
11161 bool is_defined_in_discarded_section;
11162 unsigned int shndx;
11163 if (r_sym < local_count)
11165 sym = NULL;
11166 psymval = arm_object->local_symbol(r_sym);
11168 // If the local symbol belongs to a section we are discarding,
11169 // and that section is a debug section, try to find the
11170 // corresponding kept section and map this symbol to its
11171 // counterpart in the kept section. The symbol must not
11172 // correspond to a section we are folding.
11173 bool is_ordinary;
11174 shndx = psymval->input_shndx(&is_ordinary);
11175 is_defined_in_discarded_section =
11176 (is_ordinary
11177 && shndx != elfcpp::SHN_UNDEF
11178 && !arm_object->is_section_included(shndx)
11179 && !relinfo->symtab->is_section_folded(arm_object, shndx));
11181 // We need to compute the would-be final value of this local
11182 // symbol.
11183 if (!is_defined_in_discarded_section)
11185 typedef Sized_relobj<32, big_endian> ObjType;
11186 typename ObjType::Compute_final_local_value_status status =
11187 arm_object->compute_final_local_value(r_sym, psymval, &symval,
11188 relinfo->symtab);
11189 if (status == ObjType::CFLV_OK)
11191 // Currently we cannot handle a branch to a target in
11192 // a merged section. If this is the case, issue an error
11193 // and also free the merge symbol value.
11194 if (!symval.has_output_value())
11196 const std::string& section_name =
11197 arm_object->section_name(shndx);
11198 arm_object->error(_("cannot handle branch to local %u "
11199 "in a merged section %s"),
11200 r_sym, section_name.c_str());
11202 psymval = &symval;
11204 else
11206 // We cannot determine the final value.
11207 continue;
11211 else
11213 const Symbol* gsym;
11214 gsym = arm_object->global_symbol(r_sym);
11215 gold_assert(gsym != NULL);
11216 if (gsym->is_forwarder())
11217 gsym = relinfo->symtab->resolve_forwards(gsym);
11219 sym = static_cast<const Sized_symbol<32>*>(gsym);
11220 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
11221 symval.set_output_symtab_index(sym->symtab_index());
11222 else
11223 symval.set_no_output_symtab_entry();
11225 // We need to compute the would-be final value of this global
11226 // symbol.
11227 const Symbol_table* symtab = relinfo->symtab;
11228 const Sized_symbol<32>* sized_symbol =
11229 symtab->get_sized_symbol<32>(gsym);
11230 Symbol_table::Compute_final_value_status status;
11231 Arm_address value =
11232 symtab->compute_final_value<32>(sized_symbol, &status);
11234 // Skip this if the symbol has not output section.
11235 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
11236 continue;
11237 symval.set_output_value(value);
11239 if (gsym->type() == elfcpp::STT_TLS)
11240 symval.set_is_tls_symbol();
11241 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
11242 symval.set_is_ifunc_symbol();
11243 psymval = &symval;
11245 is_defined_in_discarded_section =
11246 (gsym->is_defined_in_discarded_section()
11247 && gsym->is_undefined());
11248 shndx = 0;
11251 Symbol_value<32> symval2;
11252 if (is_defined_in_discarded_section)
11254 if (comdat_behavior == CB_UNDETERMINED)
11256 std::string name = arm_object->section_name(relinfo->data_shndx);
11257 comdat_behavior = get_comdat_behavior(name.c_str());
11259 if (comdat_behavior == CB_PRETEND)
11261 // FIXME: This case does not work for global symbols.
11262 // We have no place to store the original section index.
11263 // Fortunately this does not matter for comdat sections,
11264 // only for sections explicitly discarded by a linker
11265 // script.
11266 bool found;
11267 typename elfcpp::Elf_types<32>::Elf_Addr value =
11268 arm_object->map_to_kept_section(shndx, &found);
11269 if (found)
11270 symval2.set_output_value(value + psymval->input_value());
11271 else
11272 symval2.set_output_value(0);
11274 else
11276 if (comdat_behavior == CB_WARNING)
11277 gold_warning_at_location(relinfo, i, offset,
11278 _("relocation refers to discarded "
11279 "section"));
11280 symval2.set_output_value(0);
11282 symval2.set_no_output_symtab_entry();
11283 psymval = &symval2;
11286 // If symbol is a section symbol, we don't know the actual type of
11287 // destination. Give up.
11288 if (psymval->is_section_symbol())
11289 continue;
11291 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
11292 addend, view_address + offset);
11296 // Scan an input section for stub generation.
11298 template<bool big_endian>
11299 void
11300 Target_arm<big_endian>::scan_section_for_stubs(
11301 const Relocate_info<32, big_endian>* relinfo,
11302 unsigned int sh_type,
11303 const unsigned char* prelocs,
11304 size_t reloc_count,
11305 Output_section* output_section,
11306 bool needs_special_offset_handling,
11307 const unsigned char* view,
11308 Arm_address view_address,
11309 section_size_type view_size)
11311 if (sh_type == elfcpp::SHT_REL)
11312 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
11313 relinfo,
11314 prelocs,
11315 reloc_count,
11316 output_section,
11317 needs_special_offset_handling,
11318 view,
11319 view_address,
11320 view_size);
11321 else if (sh_type == elfcpp::SHT_RELA)
11322 // We do not support RELA type relocations yet. This is provided for
11323 // completeness.
11324 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
11325 relinfo,
11326 prelocs,
11327 reloc_count,
11328 output_section,
11329 needs_special_offset_handling,
11330 view,
11331 view_address,
11332 view_size);
11333 else
11334 gold_unreachable();
11337 // Group input sections for stub generation.
11339 // We goup input sections in an output sections so that the total size,
11340 // including any padding space due to alignment is smaller than GROUP_SIZE
11341 // unless the only input section in group is bigger than GROUP_SIZE already.
11342 // Then an ARM stub table is created to follow the last input section
11343 // in group. For each group an ARM stub table is created an is placed
11344 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
11345 // extend the group after the stub table.
11347 template<bool big_endian>
11348 void
11349 Target_arm<big_endian>::group_sections(
11350 Layout* layout,
11351 section_size_type group_size,
11352 bool stubs_always_after_branch,
11353 const Task* task)
11355 // Group input sections and insert stub table
11356 Layout::Section_list section_list;
11357 layout->get_allocated_sections(&section_list);
11358 for (Layout::Section_list::const_iterator p = section_list.begin();
11359 p != section_list.end();
11360 ++p)
11362 Arm_output_section<big_endian>* output_section =
11363 Arm_output_section<big_endian>::as_arm_output_section(*p);
11364 output_section->group_sections(group_size, stubs_always_after_branch,
11365 this, task);
11369 // Relaxation hook. This is where we do stub generation.
11371 template<bool big_endian>
11372 bool
11373 Target_arm<big_endian>::do_relax(
11374 int pass,
11375 const Input_objects* input_objects,
11376 Symbol_table* symtab,
11377 Layout* layout,
11378 const Task* task)
11380 // No need to generate stubs if this is a relocatable link.
11381 gold_assert(!parameters->options().relocatable());
11383 // If this is the first pass, we need to group input sections into
11384 // stub groups.
11385 bool done_exidx_fixup = false;
11386 typedef typename Stub_table_list::iterator Stub_table_iterator;
11387 if (pass == 1)
11389 // Determine the stub group size. The group size is the absolute
11390 // value of the parameter --stub-group-size. If --stub-group-size
11391 // is passed a negative value, we restict stubs to be always after
11392 // the stubbed branches.
11393 int32_t stub_group_size_param =
11394 parameters->options().stub_group_size();
11395 bool stubs_always_after_branch = stub_group_size_param < 0;
11396 section_size_type stub_group_size = abs(stub_group_size_param);
11398 if (stub_group_size == 1)
11400 // Default value.
11401 // Thumb branch range is +-4MB has to be used as the default
11402 // maximum size (a given section can contain both ARM and Thumb
11403 // code, so the worst case has to be taken into account). If we are
11404 // fixing cortex-a8 errata, the branch range has to be even smaller,
11405 // since wide conditional branch has a range of +-1MB only.
11407 // This value is 48K less than that, which allows for 4096
11408 // 12-byte stubs. If we exceed that, then we will fail to link.
11409 // The user will have to relink with an explicit group size
11410 // option.
11411 stub_group_size = 4145152;
11414 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
11415 // page as the first half of a 32-bit branch straddling two 4K pages.
11416 // This is a crude way of enforcing that. In addition, long conditional
11417 // branches of THUMB-2 have a range of +-1M. If we are fixing cortex-A8
11418 // erratum, limit the group size to (1M - 12k) to avoid unreachable
11419 // cortex-A8 stubs from long conditional branches.
11420 if (this->fix_cortex_a8_)
11422 stubs_always_after_branch = true;
11423 const section_size_type cortex_a8_group_size = 1024 * (1024 - 12);
11424 stub_group_size = std::max(stub_group_size, cortex_a8_group_size);
11427 group_sections(layout, stub_group_size, stubs_always_after_branch, task);
11429 // Also fix .ARM.exidx section coverage.
11430 Arm_output_section<big_endian>* exidx_output_section = NULL;
11431 for (Layout::Section_list::const_iterator p =
11432 layout->section_list().begin();
11433 p != layout->section_list().end();
11434 ++p)
11435 if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
11437 if (exidx_output_section == NULL)
11438 exidx_output_section =
11439 Arm_output_section<big_endian>::as_arm_output_section(*p);
11440 else
11441 // We cannot handle this now.
11442 gold_error(_("multiple SHT_ARM_EXIDX sections %s and %s in a "
11443 "non-relocatable link"),
11444 exidx_output_section->name(),
11445 (*p)->name());
11448 if (exidx_output_section != NULL)
11450 this->fix_exidx_coverage(layout, input_objects, exidx_output_section,
11451 symtab, task);
11452 done_exidx_fixup = true;
11455 else
11457 // If this is not the first pass, addresses and file offsets have
11458 // been reset at this point, set them here.
11459 for (Stub_table_iterator sp = this->stub_tables_.begin();
11460 sp != this->stub_tables_.end();
11461 ++sp)
11463 Arm_input_section<big_endian>* owner = (*sp)->owner();
11464 off_t off = align_address(owner->original_size(),
11465 (*sp)->addralign());
11466 (*sp)->set_address_and_file_offset(owner->address() + off,
11467 owner->offset() + off);
11471 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
11472 // beginning of each relaxation pass, just blow away all the stubs.
11473 // Alternatively, we could selectively remove only the stubs and reloc
11474 // information for code sections that have moved since the last pass.
11475 // That would require more book-keeping.
11476 if (this->fix_cortex_a8_)
11478 // Clear all Cortex-A8 reloc information.
11479 for (typename Cortex_a8_relocs_info::const_iterator p =
11480 this->cortex_a8_relocs_info_.begin();
11481 p != this->cortex_a8_relocs_info_.end();
11482 ++p)
11483 delete p->second;
11484 this->cortex_a8_relocs_info_.clear();
11486 // Remove all Cortex-A8 stubs.
11487 for (Stub_table_iterator sp = this->stub_tables_.begin();
11488 sp != this->stub_tables_.end();
11489 ++sp)
11490 (*sp)->remove_all_cortex_a8_stubs();
11493 // Scan relocs for relocation stubs
11494 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
11495 op != input_objects->relobj_end();
11496 ++op)
11498 Arm_relobj<big_endian>* arm_relobj =
11499 Arm_relobj<big_endian>::as_arm_relobj(*op);
11500 // Lock the object so we can read from it. This is only called
11501 // single-threaded from Layout::finalize, so it is OK to lock.
11502 Task_lock_obj<Object> tl(task, arm_relobj);
11503 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
11506 // Check all stub tables to see if any of them have their data sizes
11507 // or addresses alignments changed. These are the only things that
11508 // matter.
11509 bool any_stub_table_changed = false;
11510 Unordered_set<const Output_section*> sections_needing_adjustment;
11511 for (Stub_table_iterator sp = this->stub_tables_.begin();
11512 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
11513 ++sp)
11515 if ((*sp)->update_data_size_and_addralign())
11517 // Update data size of stub table owner.
11518 Arm_input_section<big_endian>* owner = (*sp)->owner();
11519 uint64_t address = owner->address();
11520 off_t offset = owner->offset();
11521 owner->reset_address_and_file_offset();
11522 owner->set_address_and_file_offset(address, offset);
11524 sections_needing_adjustment.insert(owner->output_section());
11525 any_stub_table_changed = true;
11529 // Output_section_data::output_section() returns a const pointer but we
11530 // need to update output sections, so we record all output sections needing
11531 // update above and scan the sections here to find out what sections need
11532 // to be updated.
11533 for (Layout::Section_list::const_iterator p = layout->section_list().begin();
11534 p != layout->section_list().end();
11535 ++p)
11537 if (sections_needing_adjustment.find(*p)
11538 != sections_needing_adjustment.end())
11539 (*p)->set_section_offsets_need_adjustment();
11542 // Stop relaxation if no EXIDX fix-up and no stub table change.
11543 bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
11545 // Finalize the stubs in the last relaxation pass.
11546 if (!continue_relaxation)
11548 for (Stub_table_iterator sp = this->stub_tables_.begin();
11549 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
11550 ++sp)
11551 (*sp)->finalize_stubs();
11553 // Update output local symbol counts of objects if necessary.
11554 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
11555 op != input_objects->relobj_end();
11556 ++op)
11558 Arm_relobj<big_endian>* arm_relobj =
11559 Arm_relobj<big_endian>::as_arm_relobj(*op);
11561 // Update output local symbol counts. We need to discard local
11562 // symbols defined in parts of input sections that are discarded by
11563 // relaxation.
11564 if (arm_relobj->output_local_symbol_count_needs_update())
11566 // We need to lock the object's file to update it.
11567 Task_lock_obj<Object> tl(task, arm_relobj);
11568 arm_relobj->update_output_local_symbol_count();
11573 return continue_relaxation;
11576 // Relocate a stub.
11578 template<bool big_endian>
11579 void
11580 Target_arm<big_endian>::relocate_stub(
11581 Stub* stub,
11582 const Relocate_info<32, big_endian>* relinfo,
11583 Output_section* output_section,
11584 unsigned char* view,
11585 Arm_address address,
11586 section_size_type view_size)
11588 Relocate relocate;
11589 const Stub_template* stub_template = stub->stub_template();
11590 for (size_t i = 0; i < stub_template->reloc_count(); i++)
11592 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
11593 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
11595 unsigned int r_type = insn->r_type();
11596 section_size_type reloc_offset = stub_template->reloc_offset(i);
11597 section_size_type reloc_size = insn->size();
11598 gold_assert(reloc_offset + reloc_size <= view_size);
11600 // This is the address of the stub destination.
11601 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
11602 Symbol_value<32> symval;
11603 symval.set_output_value(target);
11605 // Synthesize a fake reloc just in case. We don't have a symbol so
11606 // we use 0.
11607 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
11608 memset(reloc_buffer, 0, sizeof(reloc_buffer));
11609 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
11610 reloc_write.put_r_offset(reloc_offset);
11611 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
11612 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
11614 relocate.relocate(relinfo, this, output_section,
11615 this->fake_relnum_for_stubs, rel, r_type,
11616 NULL, &symval, view + reloc_offset,
11617 address + reloc_offset, reloc_size);
11621 // Determine whether an object attribute tag takes an integer, a
11622 // string or both.
11624 template<bool big_endian>
11626 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
11628 if (tag == Object_attribute::Tag_compatibility)
11629 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
11630 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
11631 else if (tag == elfcpp::Tag_nodefaults)
11632 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
11633 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
11634 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
11635 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
11636 else if (tag < 32)
11637 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
11638 else
11639 return ((tag & 1) != 0
11640 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
11641 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
11644 // Reorder attributes.
11646 // The ABI defines that Tag_conformance should be emitted first, and that
11647 // Tag_nodefaults should be second (if either is defined). This sets those
11648 // two positions, and bumps up the position of all the remaining tags to
11649 // compensate.
11651 template<bool big_endian>
11653 Target_arm<big_endian>::do_attributes_order(int num) const
11655 // Reorder the known object attributes in output. We want to move
11656 // Tag_conformance to position 4 and Tag_conformance to position 5
11657 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
11658 if (num == 4)
11659 return elfcpp::Tag_conformance;
11660 if (num == 5)
11661 return elfcpp::Tag_nodefaults;
11662 if ((num - 2) < elfcpp::Tag_nodefaults)
11663 return num - 2;
11664 if ((num - 1) < elfcpp::Tag_conformance)
11665 return num - 1;
11666 return num;
11669 // Scan a span of THUMB code for Cortex-A8 erratum.
11671 template<bool big_endian>
11672 void
11673 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
11674 Arm_relobj<big_endian>* arm_relobj,
11675 unsigned int shndx,
11676 section_size_type span_start,
11677 section_size_type span_end,
11678 const unsigned char* view,
11679 Arm_address address)
11681 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
11683 // The opcode is BLX.W, BL.W, B.W, Bcc.W
11684 // The branch target is in the same 4KB region as the
11685 // first half of the branch.
11686 // The instruction before the branch is a 32-bit
11687 // length non-branch instruction.
11688 section_size_type i = span_start;
11689 bool last_was_32bit = false;
11690 bool last_was_branch = false;
11691 while (i < span_end)
11693 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
11694 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
11695 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
11696 bool is_blx = false, is_b = false;
11697 bool is_bl = false, is_bcc = false;
11699 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
11700 if (insn_32bit)
11702 // Load the rest of the insn (in manual-friendly order).
11703 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
11705 // Encoding T4: B<c>.W.
11706 is_b = (insn & 0xf800d000U) == 0xf0009000U;
11707 // Encoding T1: BL<c>.W.
11708 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
11709 // Encoding T2: BLX<c>.W.
11710 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
11711 // Encoding T3: B<c>.W (not permitted in IT block).
11712 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
11713 && (insn & 0x07f00000U) != 0x03800000U);
11716 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
11718 // If this instruction is a 32-bit THUMB branch that crosses a 4K
11719 // page boundary and it follows 32-bit non-branch instruction,
11720 // we need to work around.
11721 if (is_32bit_branch
11722 && ((address + i) & 0xfffU) == 0xffeU
11723 && last_was_32bit
11724 && !last_was_branch)
11726 // Check to see if there is a relocation stub for this branch.
11727 bool force_target_arm = false;
11728 bool force_target_thumb = false;
11729 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
11730 Cortex_a8_relocs_info::const_iterator p =
11731 this->cortex_a8_relocs_info_.find(address + i);
11733 if (p != this->cortex_a8_relocs_info_.end())
11735 cortex_a8_reloc = p->second;
11736 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
11738 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
11739 && !target_is_thumb)
11740 force_target_arm = true;
11741 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
11742 && target_is_thumb)
11743 force_target_thumb = true;
11746 off_t offset;
11747 Stub_type stub_type = arm_stub_none;
11749 // Check if we have an offending branch instruction.
11750 uint16_t upper_insn = (insn >> 16) & 0xffffU;
11751 uint16_t lower_insn = insn & 0xffffU;
11752 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
11754 if (cortex_a8_reloc != NULL
11755 && cortex_a8_reloc->reloc_stub() != NULL)
11756 // We've already made a stub for this instruction, e.g.
11757 // it's a long branch or a Thumb->ARM stub. Assume that
11758 // stub will suffice to work around the A8 erratum (see
11759 // setting of always_after_branch above).
11761 else if (is_bcc)
11763 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
11764 lower_insn);
11765 stub_type = arm_stub_a8_veneer_b_cond;
11767 else if (is_b || is_bl || is_blx)
11769 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
11770 lower_insn);
11771 if (is_blx)
11772 offset &= ~3;
11774 stub_type = (is_blx
11775 ? arm_stub_a8_veneer_blx
11776 : (is_bl
11777 ? arm_stub_a8_veneer_bl
11778 : arm_stub_a8_veneer_b));
11781 if (stub_type != arm_stub_none)
11783 Arm_address pc_for_insn = address + i + 4;
11785 // The original instruction is a BL, but the target is
11786 // an ARM instruction. If we were not making a stub,
11787 // the BL would have been converted to a BLX. Use the
11788 // BLX stub instead in that case.
11789 if (this->may_use_blx() && force_target_arm
11790 && stub_type == arm_stub_a8_veneer_bl)
11792 stub_type = arm_stub_a8_veneer_blx;
11793 is_blx = true;
11794 is_bl = false;
11796 // Conversely, if the original instruction was
11797 // BLX but the target is Thumb mode, use the BL stub.
11798 else if (force_target_thumb
11799 && stub_type == arm_stub_a8_veneer_blx)
11801 stub_type = arm_stub_a8_veneer_bl;
11802 is_blx = false;
11803 is_bl = true;
11806 if (is_blx)
11807 pc_for_insn &= ~3;
11809 // If we found a relocation, use the proper destination,
11810 // not the offset in the (unrelocated) instruction.
11811 // Note this is always done if we switched the stub type above.
11812 if (cortex_a8_reloc != NULL)
11813 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
11815 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
11817 // Add a new stub if destination address in in the same page.
11818 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
11820 Cortex_a8_stub* stub =
11821 this->stub_factory_.make_cortex_a8_stub(stub_type,
11822 arm_relobj, shndx,
11823 address + i,
11824 target, insn);
11825 Stub_table<big_endian>* stub_table =
11826 arm_relobj->stub_table(shndx);
11827 gold_assert(stub_table != NULL);
11828 stub_table->add_cortex_a8_stub(address + i, stub);
11833 i += insn_32bit ? 4 : 2;
11834 last_was_32bit = insn_32bit;
11835 last_was_branch = is_32bit_branch;
11839 // Apply the Cortex-A8 workaround.
11841 template<bool big_endian>
11842 void
11843 Target_arm<big_endian>::apply_cortex_a8_workaround(
11844 const Cortex_a8_stub* stub,
11845 Arm_address stub_address,
11846 unsigned char* insn_view,
11847 Arm_address insn_address)
11849 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
11850 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
11851 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
11852 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
11853 off_t branch_offset = stub_address - (insn_address + 4);
11855 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
11856 switch (stub->stub_template()->type())
11858 case arm_stub_a8_veneer_b_cond:
11859 // For a conditional branch, we re-write it to be a uncondition
11860 // branch to the stub. We use the THUMB-2 encoding here.
11861 upper_insn = 0xf000U;
11862 lower_insn = 0xb800U;
11863 // Fall through
11864 case arm_stub_a8_veneer_b:
11865 case arm_stub_a8_veneer_bl:
11866 case arm_stub_a8_veneer_blx:
11867 if ((lower_insn & 0x5000U) == 0x4000U)
11868 // For a BLX instruction, make sure that the relocation is
11869 // rounded up to a word boundary. This follows the semantics of
11870 // the instruction which specifies that bit 1 of the target
11871 // address will come from bit 1 of the base address.
11872 branch_offset = (branch_offset + 2) & ~3;
11874 // Put BRANCH_OFFSET back into the insn.
11875 gold_assert(!utils::has_overflow<25>(branch_offset));
11876 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
11877 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
11878 break;
11880 default:
11881 gold_unreachable();
11884 // Put the relocated value back in the object file:
11885 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
11886 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
11889 template<bool big_endian>
11890 class Target_selector_arm : public Target_selector
11892 public:
11893 Target_selector_arm()
11894 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
11895 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
11898 Target*
11899 do_instantiate_target()
11900 { return new Target_arm<big_endian>(); }
11903 // Fix .ARM.exidx section coverage.
11905 template<bool big_endian>
11906 void
11907 Target_arm<big_endian>::fix_exidx_coverage(
11908 Layout* layout,
11909 const Input_objects* input_objects,
11910 Arm_output_section<big_endian>* exidx_section,
11911 Symbol_table* symtab,
11912 const Task* task)
11914 // We need to look at all the input sections in output in ascending
11915 // order of of output address. We do that by building a sorted list
11916 // of output sections by addresses. Then we looks at the output sections
11917 // in order. The input sections in an output section are already sorted
11918 // by addresses within the output section.
11920 typedef std::set<Output_section*, output_section_address_less_than>
11921 Sorted_output_section_list;
11922 Sorted_output_section_list sorted_output_sections;
11924 // Find out all the output sections of input sections pointed by
11925 // EXIDX input sections.
11926 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
11927 p != input_objects->relobj_end();
11928 ++p)
11930 Arm_relobj<big_endian>* arm_relobj =
11931 Arm_relobj<big_endian>::as_arm_relobj(*p);
11932 std::vector<unsigned int> shndx_list;
11933 arm_relobj->get_exidx_shndx_list(&shndx_list);
11934 for (size_t i = 0; i < shndx_list.size(); ++i)
11936 const Arm_exidx_input_section* exidx_input_section =
11937 arm_relobj->exidx_input_section_by_shndx(shndx_list[i]);
11938 gold_assert(exidx_input_section != NULL);
11939 if (!exidx_input_section->has_errors())
11941 unsigned int text_shndx = exidx_input_section->link();
11942 Output_section* os = arm_relobj->output_section(text_shndx);
11943 if (os != NULL && (os->flags() & elfcpp::SHF_ALLOC) != 0)
11944 sorted_output_sections.insert(os);
11949 // Go over the output sections in ascending order of output addresses.
11950 typedef typename Arm_output_section<big_endian>::Text_section_list
11951 Text_section_list;
11952 Text_section_list sorted_text_sections;
11953 for (typename Sorted_output_section_list::iterator p =
11954 sorted_output_sections.begin();
11955 p != sorted_output_sections.end();
11956 ++p)
11958 Arm_output_section<big_endian>* arm_output_section =
11959 Arm_output_section<big_endian>::as_arm_output_section(*p);
11960 arm_output_section->append_text_sections_to_list(&sorted_text_sections);
11963 exidx_section->fix_exidx_coverage(layout, sorted_text_sections, symtab,
11964 merge_exidx_entries(), task);
11967 Target_selector_arm<false> target_selector_arm;
11968 Target_selector_arm<true> target_selector_armbe;
11970 } // End anonymous namespace.