2010-05-12 Doug Kwan <dougkwan@google.com>
[deliverable/binutils-gdb.git] / gold / arm.cc
1 // arm.cc -- arm target support for gold.
2
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.
8
9 // This file is part of gold.
10
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.
15
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.
20
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.
25
26 #include "gold.h"
27
28 #include <cstring>
29 #include <limits>
30 #include <cstdio>
31 #include <string>
32 #include <algorithm>
33 #include <map>
34 #include <utility>
35 #include <set>
36
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"
54
55 namespace
56 {
57
58 using namespace gold;
59
60 template<bool big_endian>
61 class Output_data_plt_arm;
62
63 template<bool big_endian>
64 class Stub_table;
65
66 template<bool big_endian>
67 class Arm_input_section;
68
69 class Arm_exidx_cantunwind;
70
71 class Arm_exidx_merged_section;
72
73 class Arm_exidx_fixup;
74
75 template<bool big_endian>
76 class Arm_output_section;
77
78 class Arm_exidx_input_section;
79
80 template<bool big_endian>
81 class Arm_relobj;
82
83 template<bool big_endian>
84 class Arm_relocate_functions;
85
86 template<bool big_endian>
87 class Arm_output_data_got;
88
89 template<bool big_endian>
90 class Target_arm;
91
92 // For convenience.
93 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
94
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);
102
103 // Thread Control Block size.
104 const size_t ARM_TCB_SIZE = 8;
105
106 // The arm target class.
107 //
108 // This is a very simple port of gold for ARM-EABI. It is intended for
109 // supporting Android only for the time being.
110 //
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.
116
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.
124 //
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.
128
129 Arm_reloc_property_table *arm_reloc_property_table = NULL;
130
131 // Instruction template class. This class is similar to the insn_sequence
132 // struct in bfd/elf32-arm.c.
133
134 class Insn_template
135 {
136 public:
137 // Types of instruction templates.
138 enum Type
139 {
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
149 };
150
151 // Factory methods to create instruction templates in different formats.
152
153 static const Insn_template
154 thumb16_insn(uint32_t data)
155 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
156
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); }
162
163 static const Insn_template
164 thumb32_insn(uint32_t data)
165 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
166
167 static const Insn_template
168 thumb32_b_insn(uint32_t data, int reloc_addend)
169 {
170 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
171 reloc_addend);
172 }
173
174 static const Insn_template
175 arm_insn(uint32_t data)
176 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
177
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); }
181
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); }
185
186 // Accessors. This class is used for read-only objects so no modifiers
187 // are provided.
188
189 uint32_t
190 data() const
191 { return this->data_; }
192
193 // Return the instruction sequence type of this.
194 Type
195 type() const
196 { return this->type_; }
197
198 // Return the ARM relocation type of this.
199 unsigned int
200 r_type() const
201 { return this->r_type_; }
202
203 int32_t
204 reloc_addend() const
205 { return this->reloc_addend_; }
206
207 // Return size of instruction template in bytes.
208 size_t
209 size() const;
210
211 // Return byte-alignment of instruction template.
212 unsigned
213 alignment() const;
214
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)
221 { }
222
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_;
232 };
233
234 // Macro for generating code to stub types. One entry per long/short
235 // branch stub
236
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)
255
256 // Stub types.
257
258 #define DEF_STUB(x) arm_stub_##x,
259 typedef enum
260 {
261 arm_stub_none,
262 DEF_STUBS
263
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,
268
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,
273
274 // Last stub type.
275 arm_stub_type_last = arm_stub_v4_veneer_bx
276 } Stub_type;
277 #undef DEF_STUB
278
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.
282
283 class Stub_template
284 {
285 public:
286 Stub_template(Stub_type, const Insn_template*, size_t);
287
288 ~Stub_template()
289 { }
290
291 // Return stub type.
292 Stub_type
293 type() const
294 { return this->type_; }
295
296 // Return an array of instruction templates.
297 const Insn_template*
298 insns() const
299 { return this->insns_; }
300
301 // Return size of template in number of instructions.
302 size_t
303 insn_count() const
304 { return this->insn_count_; }
305
306 // Return size of template in bytes.
307 size_t
308 size() const
309 { return this->size_; }
310
311 // Return alignment of the stub template.
312 unsigned
313 alignment() const
314 { return this->alignment_; }
315
316 // Return whether entry point is in thumb mode.
317 bool
318 entry_in_thumb_mode() const
319 { return this->entry_in_thumb_mode_; }
320
321 // Return number of relocations in this template.
322 size_t
323 reloc_count() const
324 { return this->relocs_.size(); }
325
326 // Return index of the I-th instruction with relocation.
327 size_t
328 reloc_insn_index(size_t i) const
329 {
330 gold_assert(i < this->relocs_.size());
331 return this->relocs_[i].first;
332 }
333
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
338 {
339 gold_assert(i < this->relocs_.size());
340 return this->relocs_[i].second;
341 }
342
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;
347
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&);
352
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_;
369 };
370
371 //
372 // A class for code stubs. This is a base class for different type of
373 // stubs used in the ARM target.
374 //
375
376 class Stub
377 {
378 private:
379 static const section_offset_type invalid_offset =
380 static_cast<section_offset_type>(-1);
381
382 public:
383 Stub(const Stub_template* stub_template)
384 : stub_template_(stub_template), offset_(invalid_offset)
385 { }
386
387 virtual
388 ~Stub()
389 { }
390
391 // Return the stub template.
392 const Stub_template*
393 stub_template() const
394 { return this->stub_template_; }
395
396 // Return offset of code stub from beginning of its containing stub table.
397 section_offset_type
398 offset() const
399 {
400 gold_assert(this->offset_ != invalid_offset);
401 return this->offset_;
402 }
403
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; }
408
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); }
414
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); }
419
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); }
425
426 protected:
427 // This must be defined in the child class.
428 virtual Arm_address
429 do_reloc_target(size_t) = 0;
430
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)
434 {
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);
439 }
440
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(); }
446
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);
452
453 // Its template.
454 const Stub_template* stub_template_;
455 // Offset within the section of containing this stub.
456 section_offset_type offset_;
457 };
458
459 // Reloc stub class. These are stubs we use to fix up relocation because
460 // of limited branch ranges.
461
462 class Reloc_stub : public Stub
463 {
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);
468
469 // Return destination address.
470 Arm_address
471 destination_address() const
472 {
473 gold_assert(this->destination_address_ != this->invalid_address);
474 return this->destination_address_;
475 }
476
477 // Set destination address.
478 void
479 set_destination_address(Arm_address address)
480 {
481 gold_assert(address != this->invalid_address);
482 this->destination_address_ = address;
483 }
484
485 // Reset destination address.
486 void
487 reset_destination_address()
488 { this->destination_address_ = this->invalid_address; }
489
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);
497
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
503 {
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)
511 {
512 if (symbol != NULL)
513 {
514 this->r_sym_ = Reloc_stub::invalid_index;
515 this->u_.symbol = symbol;
516 }
517 else
518 {
519 gold_assert(relobj != NULL && r_sym != invalid_index);
520 this->r_sym_ = r_sym;
521 this->u_.relobj = relobj;
522 }
523 }
524
525 ~Key()
526 { }
527
528 // Accessors: Keys are meant to be read-only object so no modifiers are
529 // provided.
530
531 // Return stub type.
532 Stub_type
533 stub_type() const
534 { return this->stub_type_; }
535
536 // Return the local symbol index or invalid_index.
537 unsigned int
538 r_sym() const
539 { return this->r_sym_; }
540
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; }
545
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; }
550
551 // Whether this equals to another key k.
552 bool
553 eq(const Key& k) const
554 {
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_));
561 }
562
563 // Return a hash value.
564 size_t
565 hash_value() const
566 {
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_);
574 }
575
576 // Functors for STL associative containers.
577 struct hash
578 {
579 size_t
580 operator()(const Key& k) const
581 { return k.hash_value(); }
582 };
583
584 struct equal_to
585 {
586 bool
587 operator()(const Key& k1, const Key& k2) const
588 { return k1.eq(k2); }
589 };
590
591 // Name of key. This is mainly for debugging.
592 std::string
593 name() const;
594
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
608 {
609 const Symbol* symbol;
610 const Relobj* relobj;
611 } u_;
612 // Addend associated with a reloc.
613 int32_t addend_;
614 };
615
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)
620 { }
621
622 ~Reloc_stub()
623 { }
624
625 friend class Stub_factory;
626
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)
631 {
632 // All reloc stub have only one relocation.
633 gold_assert(i == 0);
634 return this->destination_address_;
635 }
636
637 private:
638 // Address of destination.
639 Arm_address destination_address_;
640 };
641
642 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
643 // THUMB branch that meets the following conditions:
644 //
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.
650 //
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.
659 //
660
661 class Cortex_a8_stub : public Stub
662 {
663 public:
664 ~Cortex_a8_stub()
665 { }
666
667 // Return the object of the code section containing the branch being fixed
668 // up.
669 Relobj*
670 relobj() const
671 { return this->relobj_; }
672
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_; }
678
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_; }
685
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_; }
692
693 // Return the instruction being fixed up.
694 uint32_t
695 original_insn() const
696 { return this->original_insn_; }
697
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)
707 { }
708
709 friend class Stub_factory;
710
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)
715 {
716 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
717 {
718 // The conditional branch veneer has two relocations.
719 gold_assert(i < 2);
720 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
721 }
722 else
723 {
724 // All other Cortex-A8 stubs have only one relocation.
725 gold_assert(i == 0);
726 return this->destination_address_;
727 }
728 }
729
730 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
731 uint16_t
732 do_thumb16_special(size_t);
733
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_;
746 };
747
748 // ARMv4 BX Rx branch relocation stub class.
749 class Arm_v4bx_stub : public Stub
750 {
751 public:
752 ~Arm_v4bx_stub()
753 { }
754
755 // Return the associated register.
756 uint32_t
757 reg() const
758 { return this->reg_; }
759
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)
764 { }
765
766 friend class Stub_factory;
767
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(); }
773
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)
777 {
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);
782 }
783
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)
789 {
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_));
800 }
801
802 // A register index (r0-r14), which is associated with the stub.
803 uint32_t reg_;
804 };
805
806 // Stub factory class.
807
808 class Stub_factory
809 {
810 public:
811 // Return the unique instance of this class.
812 static const Stub_factory&
813 get_instance()
814 {
815 static Stub_factory singleton;
816 return singleton;
817 }
818
819 // Make a relocation stub.
820 Reloc_stub*
821 make_reloc_stub(Stub_type stub_type) const
822 {
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]);
826 }
827
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
833 {
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);
838 }
839
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
844 {
845 gold_assert(reg < 0xf);
846 return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
847 reg);
848 }
849
850 private:
851 // Constructor and destructor are protected since we only return a single
852 // instance created in Stub_factory::get_instance().
853
854 Stub_factory();
855
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&);
859
860 // Stub templates. These are initialized in the constructor.
861 const Stub_template* stub_templates_[arm_stub_type_last+1];
862 };
863
864 // A class to hold stubs for the ARM target.
865
866 template<bool big_endian>
867 class Stub_table : public Output_data
868 {
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)
874 { }
875
876 ~Stub_table()
877 { }
878
879 // Owner of this stub table.
880 Arm_input_section<big_endian>*
881 owner() const
882 { return this->owner_; }
883
884 // Whether this stub table is empty.
885 bool
886 empty() const
887 {
888 return (this->reloc_stubs_.empty()
889 && this->cortex_a8_stubs_.empty()
890 && this->arm_v4bx_stubs_.empty());
891 }
892
893 // Return the current data size.
894 off_t
895 current_data_size() const
896 { return this->current_data_size_for_child(); }
897
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)
902 {
903 const Stub_template* stub_template = stub->stub_template();
904 gold_assert(stub_template->type() == key.stub_type());
905 this->reloc_stubs_[key] = stub;
906
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);
915 }
916
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)
922 {
923 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
924 this->cortex_a8_stubs_.insert(value);
925 }
926
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)
931 {
932 gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
933 this->arm_v4bx_stubs_[stub->reg()] = stub;
934 }
935
936 // Remove all Cortex-A8 stubs.
937 void
938 remove_all_cortex_a8_stubs();
939
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
943 {
944 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
945 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
946 }
947
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
952 {
953 gold_assert(reg < 0xf);
954 return this->arm_v4bx_stubs_[reg];
955 }
956
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);
962
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();
968
969 // Finalize stubs. Set the offsets of all stubs and mark input sections
970 // needing the Cortex-A8 workaround.
971 void
972 finalize_stubs();
973
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);
979
980 protected:
981 // Write out section contents.
982 void
983 do_write(Output_file*);
984
985 // Return the required alignment.
986 uint64_t
987 do_addralign() const
988 { return this->prev_addralign_; }
989
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_); }
994
995 // Set final data size.
996 void
997 set_final_data_size()
998 { this->set_data_size(this->current_data_size()); }
999
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);
1006
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;
1012
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;
1018
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_;
1035 };
1036
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.
1039
1040 class Arm_exidx_cantunwind : public Output_section_data
1041 {
1042 public:
1043 Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
1044 : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
1045 { }
1046
1047 // Return the object containing the section pointed by this.
1048 Relobj*
1049 relobj() const
1050 { return this->relobj_; }
1051
1052 // Return the section index of the section pointed by this.
1053 unsigned int
1054 shndx() const
1055 { return this->shndx_; }
1056
1057 protected:
1058 void
1059 do_write(Output_file* of)
1060 {
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);
1065 }
1066
1067 private:
1068 // Implement do_write for a given endianness.
1069 template<bool big_endian>
1070 void inline
1071 do_fixed_endian_write(Output_file*);
1072
1073 // The object containing the section pointed by this.
1074 Relobj* relobj_;
1075 // The section index of the section pointed by this.
1076 unsigned int shndx_;
1077 };
1078
1079 // During EXIDX coverage fix-up, we compact an EXIDX section. The
1080 // Offset map is used to map input section offset within the EXIDX section
1081 // to the output offset from the start of this EXIDX section.
1082
1083 typedef std::map<section_offset_type, section_offset_type>
1084 Arm_exidx_section_offset_map;
1085
1086 // Arm_exidx_merged_section class. This represents an EXIDX input section
1087 // with some of its entries merged.
1088
1089 class Arm_exidx_merged_section : public Output_relaxed_input_section
1090 {
1091 public:
1092 // Constructor for Arm_exidx_merged_section.
1093 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1094 // SECTION_OFFSET_MAP points to a section offset map describing how
1095 // parts of the input section are mapped to output. DELETED_BYTES is
1096 // the number of bytes deleted from the EXIDX input section.
1097 Arm_exidx_merged_section(
1098 const Arm_exidx_input_section& exidx_input_section,
1099 const Arm_exidx_section_offset_map& section_offset_map,
1100 uint32_t deleted_bytes);
1101
1102 // Return the original EXIDX input section.
1103 const Arm_exidx_input_section&
1104 exidx_input_section() const
1105 { return this->exidx_input_section_; }
1106
1107 // Return the section offset map.
1108 const Arm_exidx_section_offset_map&
1109 section_offset_map() const
1110 { return this->section_offset_map_; }
1111
1112 protected:
1113 // Write merged section into file OF.
1114 void
1115 do_write(Output_file* of);
1116
1117 bool
1118 do_output_offset(const Relobj*, unsigned int, section_offset_type,
1119 section_offset_type*) const;
1120
1121 private:
1122 // Original EXIDX input section.
1123 const Arm_exidx_input_section& exidx_input_section_;
1124 // Section offset map.
1125 const Arm_exidx_section_offset_map& section_offset_map_;
1126 };
1127
1128 // A class to wrap an ordinary input section containing executable code.
1129
1130 template<bool big_endian>
1131 class Arm_input_section : public Output_relaxed_input_section
1132 {
1133 public:
1134 Arm_input_section(Relobj* relobj, unsigned int shndx)
1135 : Output_relaxed_input_section(relobj, shndx, 1),
1136 original_addralign_(1), original_size_(0), stub_table_(NULL)
1137 { }
1138
1139 ~Arm_input_section()
1140 { }
1141
1142 // Initialize.
1143 void
1144 init();
1145
1146 // Whether this is a stub table owner.
1147 bool
1148 is_stub_table_owner() const
1149 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1150
1151 // Return the stub table.
1152 Stub_table<big_endian>*
1153 stub_table() const
1154 { return this->stub_table_; }
1155
1156 // Set the stub_table.
1157 void
1158 set_stub_table(Stub_table<big_endian>* stub_table)
1159 { this->stub_table_ = stub_table; }
1160
1161 // Downcast a base pointer to an Arm_input_section pointer. This is
1162 // not type-safe but we only use Arm_input_section not the base class.
1163 static Arm_input_section<big_endian>*
1164 as_arm_input_section(Output_relaxed_input_section* poris)
1165 { return static_cast<Arm_input_section<big_endian>*>(poris); }
1166
1167 protected:
1168 // Write data to output file.
1169 void
1170 do_write(Output_file*);
1171
1172 // Return required alignment of this.
1173 uint64_t
1174 do_addralign() const
1175 {
1176 if (this->is_stub_table_owner())
1177 return std::max(this->stub_table_->addralign(),
1178 this->original_addralign_);
1179 else
1180 return this->original_addralign_;
1181 }
1182
1183 // Finalize data size.
1184 void
1185 set_final_data_size();
1186
1187 // Reset address and file offset.
1188 void
1189 do_reset_address_and_file_offset();
1190
1191 // Output offset.
1192 bool
1193 do_output_offset(const Relobj* object, unsigned int shndx,
1194 section_offset_type offset,
1195 section_offset_type* poutput) const
1196 {
1197 if ((object == this->relobj())
1198 && (shndx == this->shndx())
1199 && (offset >= 0)
1200 && (convert_types<uint64_t, section_offset_type>(offset)
1201 <= this->original_size_))
1202 {
1203 *poutput = offset;
1204 return true;
1205 }
1206 else
1207 return false;
1208 }
1209
1210 private:
1211 // Copying is not allowed.
1212 Arm_input_section(const Arm_input_section&);
1213 Arm_input_section& operator=(const Arm_input_section&);
1214
1215 // Address alignment of the original input section.
1216 uint64_t original_addralign_;
1217 // Section size of the original input section.
1218 uint64_t original_size_;
1219 // Stub table.
1220 Stub_table<big_endian>* stub_table_;
1221 };
1222
1223 // Arm_exidx_fixup class. This is used to define a number of methods
1224 // and keep states for fixing up EXIDX coverage.
1225
1226 class Arm_exidx_fixup
1227 {
1228 public:
1229 Arm_exidx_fixup(Output_section* exidx_output_section,
1230 bool merge_exidx_entries = true)
1231 : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1232 last_inlined_entry_(0), last_input_section_(NULL),
1233 section_offset_map_(NULL), first_output_text_section_(NULL),
1234 merge_exidx_entries_(merge_exidx_entries)
1235 { }
1236
1237 ~Arm_exidx_fixup()
1238 { delete this->section_offset_map_; }
1239
1240 // Process an EXIDX section for entry merging. Return number of bytes to
1241 // be deleted in output. If parts of the input EXIDX section are merged
1242 // a heap allocated Arm_exidx_section_offset_map is store in the located
1243 // PSECTION_OFFSET_MAP. The caller owns the map and is reponsible for
1244 // releasing it.
1245 template<bool big_endian>
1246 uint32_t
1247 process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1248 Arm_exidx_section_offset_map** psection_offset_map);
1249
1250 // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1251 // input section, if there is not one already.
1252 void
1253 add_exidx_cantunwind_as_needed();
1254
1255 // Return the output section for the text section which is linked to the
1256 // first exidx input in output.
1257 Output_section*
1258 first_output_text_section() const
1259 { return this->first_output_text_section_; }
1260
1261 private:
1262 // Copying is not allowed.
1263 Arm_exidx_fixup(const Arm_exidx_fixup&);
1264 Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1265
1266 // Type of EXIDX unwind entry.
1267 enum Unwind_type
1268 {
1269 // No type.
1270 UT_NONE,
1271 // EXIDX_CANTUNWIND.
1272 UT_EXIDX_CANTUNWIND,
1273 // Inlined entry.
1274 UT_INLINED_ENTRY,
1275 // Normal entry.
1276 UT_NORMAL_ENTRY,
1277 };
1278
1279 // Process an EXIDX entry. We only care about the second word of the
1280 // entry. Return true if the entry can be deleted.
1281 bool
1282 process_exidx_entry(uint32_t second_word);
1283
1284 // Update the current section offset map during EXIDX section fix-up.
1285 // If there is no map, create one. INPUT_OFFSET is the offset of a
1286 // reference point, DELETED_BYTES is the number of deleted by in the
1287 // section so far. If DELETE_ENTRY is true, the reference point and
1288 // all offsets after the previous reference point are discarded.
1289 void
1290 update_offset_map(section_offset_type input_offset,
1291 section_size_type deleted_bytes, bool delete_entry);
1292
1293 // EXIDX output section.
1294 Output_section* exidx_output_section_;
1295 // Unwind type of the last EXIDX entry processed.
1296 Unwind_type last_unwind_type_;
1297 // Last seen inlined EXIDX entry.
1298 uint32_t last_inlined_entry_;
1299 // Last processed EXIDX input section.
1300 const Arm_exidx_input_section* last_input_section_;
1301 // Section offset map created in process_exidx_section.
1302 Arm_exidx_section_offset_map* section_offset_map_;
1303 // Output section for the text section which is linked to the first exidx
1304 // input in output.
1305 Output_section* first_output_text_section_;
1306
1307 bool merge_exidx_entries_;
1308 };
1309
1310 // Arm output section class. This is defined mainly to add a number of
1311 // stub generation methods.
1312
1313 template<bool big_endian>
1314 class Arm_output_section : public Output_section
1315 {
1316 public:
1317 typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
1318
1319 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1320 elfcpp::Elf_Xword flags)
1321 : Output_section(name, type, flags)
1322 { }
1323
1324 ~Arm_output_section()
1325 { }
1326
1327 // Group input sections for stub generation.
1328 void
1329 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1330
1331 // Downcast a base pointer to an Arm_output_section pointer. This is
1332 // not type-safe but we only use Arm_output_section not the base class.
1333 static Arm_output_section<big_endian>*
1334 as_arm_output_section(Output_section* os)
1335 { return static_cast<Arm_output_section<big_endian>*>(os); }
1336
1337 // Append all input text sections in this into LIST.
1338 void
1339 append_text_sections_to_list(Text_section_list* list);
1340
1341 // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
1342 // is a list of text input sections sorted in ascending order of their
1343 // output addresses.
1344 void
1345 fix_exidx_coverage(Layout* layout,
1346 const Text_section_list& sorted_text_section,
1347 Symbol_table* symtab,
1348 bool merge_exidx_entries);
1349
1350 private:
1351 // For convenience.
1352 typedef Output_section::Input_section Input_section;
1353 typedef Output_section::Input_section_list Input_section_list;
1354
1355 // Create a stub group.
1356 void create_stub_group(Input_section_list::const_iterator,
1357 Input_section_list::const_iterator,
1358 Input_section_list::const_iterator,
1359 Target_arm<big_endian>*,
1360 std::vector<Output_relaxed_input_section*>*);
1361 };
1362
1363 // Arm_exidx_input_section class. This represents an EXIDX input section.
1364
1365 class Arm_exidx_input_section
1366 {
1367 public:
1368 static const section_offset_type invalid_offset =
1369 static_cast<section_offset_type>(-1);
1370
1371 Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1372 unsigned int link, uint32_t size, uint32_t addralign)
1373 : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1374 addralign_(addralign)
1375 { }
1376
1377 ~Arm_exidx_input_section()
1378 { }
1379
1380 // Accessors: This is a read-only class.
1381
1382 // Return the object containing this EXIDX input section.
1383 Relobj*
1384 relobj() const
1385 { return this->relobj_; }
1386
1387 // Return the section index of this EXIDX input section.
1388 unsigned int
1389 shndx() const
1390 { return this->shndx_; }
1391
1392 // Return the section index of linked text section in the same object.
1393 unsigned int
1394 link() const
1395 { return this->link_; }
1396
1397 // Return size of the EXIDX input section.
1398 uint32_t
1399 size() const
1400 { return this->size_; }
1401
1402 // Reutnr address alignment of EXIDX input section.
1403 uint32_t
1404 addralign() const
1405 { return this->addralign_; }
1406
1407 private:
1408 // Object containing this.
1409 Relobj* relobj_;
1410 // Section index of this.
1411 unsigned int shndx_;
1412 // text section linked to this in the same object.
1413 unsigned int link_;
1414 // Size of this. For ARM 32-bit is sufficient.
1415 uint32_t size_;
1416 // Address alignment of this. For ARM 32-bit is sufficient.
1417 uint32_t addralign_;
1418 };
1419
1420 // Arm_relobj class.
1421
1422 template<bool big_endian>
1423 class Arm_relobj : public Sized_relobj<32, big_endian>
1424 {
1425 public:
1426 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1427
1428 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1429 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1430 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1431 stub_tables_(), local_symbol_is_thumb_function_(),
1432 attributes_section_data_(NULL), mapping_symbols_info_(),
1433 section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
1434 output_local_symbol_count_needs_update_(false),
1435 merge_flags_and_attributes_(true)
1436 { }
1437
1438 ~Arm_relobj()
1439 { delete this->attributes_section_data_; }
1440
1441 // Return the stub table of the SHNDX-th section if there is one.
1442 Stub_table<big_endian>*
1443 stub_table(unsigned int shndx) const
1444 {
1445 gold_assert(shndx < this->stub_tables_.size());
1446 return this->stub_tables_[shndx];
1447 }
1448
1449 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1450 void
1451 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1452 {
1453 gold_assert(shndx < this->stub_tables_.size());
1454 this->stub_tables_[shndx] = stub_table;
1455 }
1456
1457 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1458 // index. This is only valid after do_count_local_symbol is called.
1459 bool
1460 local_symbol_is_thumb_function(unsigned int r_sym) const
1461 {
1462 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1463 return this->local_symbol_is_thumb_function_[r_sym];
1464 }
1465
1466 // Scan all relocation sections for stub generation.
1467 void
1468 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1469 const Layout*);
1470
1471 // Convert regular input section with index SHNDX to a relaxed section.
1472 void
1473 convert_input_section_to_relaxed_section(unsigned shndx)
1474 {
1475 // The stubs have relocations and we need to process them after writing
1476 // out the stubs. So relocation now must follow section write.
1477 this->set_section_offset(shndx, -1ULL);
1478 this->set_relocs_must_follow_section_writes();
1479 }
1480
1481 // Downcast a base pointer to an Arm_relobj pointer. This is
1482 // not type-safe but we only use Arm_relobj not the base class.
1483 static Arm_relobj<big_endian>*
1484 as_arm_relobj(Relobj* relobj)
1485 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1486
1487 // Processor-specific flags in ELF file header. This is valid only after
1488 // reading symbols.
1489 elfcpp::Elf_Word
1490 processor_specific_flags() const
1491 { return this->processor_specific_flags_; }
1492
1493 // Attribute section data This is the contents of the .ARM.attribute section
1494 // if there is one.
1495 const Attributes_section_data*
1496 attributes_section_data() const
1497 { return this->attributes_section_data_; }
1498
1499 // Mapping symbol location.
1500 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1501
1502 // Functor for STL container.
1503 struct Mapping_symbol_position_less
1504 {
1505 bool
1506 operator()(const Mapping_symbol_position& p1,
1507 const Mapping_symbol_position& p2) const
1508 {
1509 return (p1.first < p2.first
1510 || (p1.first == p2.first && p1.second < p2.second));
1511 }
1512 };
1513
1514 // We only care about the first character of a mapping symbol, so
1515 // we only store that instead of the whole symbol name.
1516 typedef std::map<Mapping_symbol_position, char,
1517 Mapping_symbol_position_less> Mapping_symbols_info;
1518
1519 // Whether a section contains any Cortex-A8 workaround.
1520 bool
1521 section_has_cortex_a8_workaround(unsigned int shndx) const
1522 {
1523 return (this->section_has_cortex_a8_workaround_ != NULL
1524 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1525 }
1526
1527 // Mark a section that has Cortex-A8 workaround.
1528 void
1529 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1530 {
1531 if (this->section_has_cortex_a8_workaround_ == NULL)
1532 this->section_has_cortex_a8_workaround_ =
1533 new std::vector<bool>(this->shnum(), false);
1534 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1535 }
1536
1537 // Return the EXIDX section of an text section with index SHNDX or NULL
1538 // if the text section has no associated EXIDX section.
1539 const Arm_exidx_input_section*
1540 exidx_input_section_by_link(unsigned int shndx) const
1541 {
1542 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1543 return ((p != this->exidx_section_map_.end()
1544 && p->second->link() == shndx)
1545 ? p->second
1546 : NULL);
1547 }
1548
1549 // Return the EXIDX section with index SHNDX or NULL if there is none.
1550 const Arm_exidx_input_section*
1551 exidx_input_section_by_shndx(unsigned shndx) const
1552 {
1553 Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1554 return ((p != this->exidx_section_map_.end()
1555 && p->second->shndx() == shndx)
1556 ? p->second
1557 : NULL);
1558 }
1559
1560 // Whether output local symbol count needs updating.
1561 bool
1562 output_local_symbol_count_needs_update() const
1563 { return this->output_local_symbol_count_needs_update_; }
1564
1565 // Set output_local_symbol_count_needs_update flag to be true.
1566 void
1567 set_output_local_symbol_count_needs_update()
1568 { this->output_local_symbol_count_needs_update_ = true; }
1569
1570 // Update output local symbol count at the end of relaxation.
1571 void
1572 update_output_local_symbol_count();
1573
1574 // Whether we want to merge processor-specific flags and attributes.
1575 bool
1576 merge_flags_and_attributes() const
1577 { return this->merge_flags_and_attributes_; }
1578
1579 protected:
1580 // Post constructor setup.
1581 void
1582 do_setup()
1583 {
1584 // Call parent's setup method.
1585 Sized_relobj<32, big_endian>::do_setup();
1586
1587 // Initialize look-up tables.
1588 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1589 this->stub_tables_.swap(empty_stub_table_list);
1590 }
1591
1592 // Count the local symbols.
1593 void
1594 do_count_local_symbols(Stringpool_template<char>*,
1595 Stringpool_template<char>*);
1596
1597 void
1598 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1599 const unsigned char* pshdrs,
1600 typename Sized_relobj<32, big_endian>::Views* pivews);
1601
1602 // Read the symbol information.
1603 void
1604 do_read_symbols(Read_symbols_data* sd);
1605
1606 // Process relocs for garbage collection.
1607 void
1608 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1609
1610 private:
1611
1612 // Whether a section needs to be scanned for relocation stubs.
1613 bool
1614 section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1615 const Relobj::Output_sections&,
1616 const Symbol_table *, const unsigned char*);
1617
1618 // Whether a section is a scannable text section.
1619 bool
1620 section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
1621 const Output_section*, const Symbol_table *);
1622
1623 // Whether a section needs to be scanned for the Cortex-A8 erratum.
1624 bool
1625 section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1626 unsigned int, Output_section*,
1627 const Symbol_table *);
1628
1629 // Scan a section for the Cortex-A8 erratum.
1630 void
1631 scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1632 unsigned int, Output_section*,
1633 Target_arm<big_endian>*);
1634
1635 // Find the linked text section of an EXIDX section by looking at the
1636 // first reloction of the EXIDX section. PSHDR points to the section
1637 // headers of a relocation section and PSYMS points to the local symbols.
1638 // PSHNDX points to a location storing the text section index if found.
1639 // Return whether we can find the linked section.
1640 bool
1641 find_linked_text_section(const unsigned char* pshdr,
1642 const unsigned char* psyms, unsigned int* pshndx);
1643
1644 //
1645 // Make a new Arm_exidx_input_section object for EXIDX section with
1646 // index SHNDX and section header SHDR. TEXT_SHNDX is the section
1647 // index of the linked text section.
1648 void
1649 make_exidx_input_section(unsigned int shndx,
1650 const elfcpp::Shdr<32, big_endian>& shdr,
1651 unsigned int text_shndx);
1652
1653 // Return the output address of either a plain input section or a
1654 // relaxed input section. SHNDX is the section index.
1655 Arm_address
1656 simple_input_section_output_address(unsigned int, Output_section*);
1657
1658 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1659 typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1660 Exidx_section_map;
1661
1662 // List of stub tables.
1663 Stub_table_list stub_tables_;
1664 // Bit vector to tell if a local symbol is a thumb function or not.
1665 // This is only valid after do_count_local_symbol is called.
1666 std::vector<bool> local_symbol_is_thumb_function_;
1667 // processor-specific flags in ELF file header.
1668 elfcpp::Elf_Word processor_specific_flags_;
1669 // Object attributes if there is an .ARM.attributes section or NULL.
1670 Attributes_section_data* attributes_section_data_;
1671 // Mapping symbols information.
1672 Mapping_symbols_info mapping_symbols_info_;
1673 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1674 std::vector<bool>* section_has_cortex_a8_workaround_;
1675 // Map a text section to its associated .ARM.exidx section, if there is one.
1676 Exidx_section_map exidx_section_map_;
1677 // Whether output local symbol count needs updating.
1678 bool output_local_symbol_count_needs_update_;
1679 // Whether we merge processor flags and attributes of this object to
1680 // output.
1681 bool merge_flags_and_attributes_;
1682 };
1683
1684 // Arm_dynobj class.
1685
1686 template<bool big_endian>
1687 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1688 {
1689 public:
1690 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1691 const elfcpp::Ehdr<32, big_endian>& ehdr)
1692 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1693 processor_specific_flags_(0), attributes_section_data_(NULL)
1694 { }
1695
1696 ~Arm_dynobj()
1697 { delete this->attributes_section_data_; }
1698
1699 // Downcast a base pointer to an Arm_relobj pointer. This is
1700 // not type-safe but we only use Arm_relobj not the base class.
1701 static Arm_dynobj<big_endian>*
1702 as_arm_dynobj(Dynobj* dynobj)
1703 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1704
1705 // Processor-specific flags in ELF file header. This is valid only after
1706 // reading symbols.
1707 elfcpp::Elf_Word
1708 processor_specific_flags() const
1709 { return this->processor_specific_flags_; }
1710
1711 // Attributes section data.
1712 const Attributes_section_data*
1713 attributes_section_data() const
1714 { return this->attributes_section_data_; }
1715
1716 protected:
1717 // Read the symbol information.
1718 void
1719 do_read_symbols(Read_symbols_data* sd);
1720
1721 private:
1722 // processor-specific flags in ELF file header.
1723 elfcpp::Elf_Word processor_specific_flags_;
1724 // Object attributes if there is an .ARM.attributes section or NULL.
1725 Attributes_section_data* attributes_section_data_;
1726 };
1727
1728 // Functor to read reloc addends during stub generation.
1729
1730 template<int sh_type, bool big_endian>
1731 struct Stub_addend_reader
1732 {
1733 // Return the addend for a relocation of a particular type. Depending
1734 // on whether this is a REL or RELA relocation, read the addend from a
1735 // view or from a Reloc object.
1736 elfcpp::Elf_types<32>::Elf_Swxword
1737 operator()(
1738 unsigned int /* r_type */,
1739 const unsigned char* /* view */,
1740 const typename Reloc_types<sh_type,
1741 32, big_endian>::Reloc& /* reloc */) const;
1742 };
1743
1744 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1745
1746 template<bool big_endian>
1747 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1748 {
1749 elfcpp::Elf_types<32>::Elf_Swxword
1750 operator()(
1751 unsigned int,
1752 const unsigned char*,
1753 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1754 };
1755
1756 // Specialized Stub_addend_reader for RELA type relocation sections.
1757 // We currently do not handle RELA type relocation sections but it is trivial
1758 // to implement the addend reader. This is provided for completeness and to
1759 // make it easier to add support for RELA relocation sections in the future.
1760
1761 template<bool big_endian>
1762 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1763 {
1764 elfcpp::Elf_types<32>::Elf_Swxword
1765 operator()(
1766 unsigned int,
1767 const unsigned char*,
1768 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1769 big_endian>::Reloc& reloc) const
1770 { return reloc.get_r_addend(); }
1771 };
1772
1773 // Cortex_a8_reloc class. We keep record of relocation that may need
1774 // the Cortex-A8 erratum workaround.
1775
1776 class Cortex_a8_reloc
1777 {
1778 public:
1779 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1780 Arm_address destination)
1781 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1782 { }
1783
1784 ~Cortex_a8_reloc()
1785 { }
1786
1787 // Accessors: This is a read-only class.
1788
1789 // Return the relocation stub associated with this relocation if there is
1790 // one.
1791 const Reloc_stub*
1792 reloc_stub() const
1793 { return this->reloc_stub_; }
1794
1795 // Return the relocation type.
1796 unsigned int
1797 r_type() const
1798 { return this->r_type_; }
1799
1800 // Return the destination address of the relocation. LSB stores the THUMB
1801 // bit.
1802 Arm_address
1803 destination() const
1804 { return this->destination_; }
1805
1806 private:
1807 // Associated relocation stub if there is one, or NULL.
1808 const Reloc_stub* reloc_stub_;
1809 // Relocation type.
1810 unsigned int r_type_;
1811 // Destination address of this relocation. LSB is used to distinguish
1812 // ARM/THUMB mode.
1813 Arm_address destination_;
1814 };
1815
1816 // Arm_output_data_got class. We derive this from Output_data_got to add
1817 // extra methods to handle TLS relocations in a static link.
1818
1819 template<bool big_endian>
1820 class Arm_output_data_got : public Output_data_got<32, big_endian>
1821 {
1822 public:
1823 Arm_output_data_got(Symbol_table* symtab, Layout* layout)
1824 : Output_data_got<32, big_endian>(), symbol_table_(symtab), layout_(layout)
1825 { }
1826
1827 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1828 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1829 // applied in a static link.
1830 void
1831 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1832 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
1833
1834 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1835 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1836 // relocation that needs to be applied in a static link.
1837 void
1838 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1839 Sized_relobj<32, big_endian>* relobj, unsigned int index)
1840 {
1841 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
1842 index));
1843 }
1844
1845 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
1846 // The first one is initialized to be 1, which is the module index for
1847 // the main executable and the second one 0. A reloc of the type
1848 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
1849 // be applied by gold. GSYM is a global symbol.
1850 void
1851 add_tls_gd32_with_static_reloc(unsigned int got_type, Symbol* gsym);
1852
1853 // Same as the above but for a local symbol in OBJECT with INDEX.
1854 void
1855 add_tls_gd32_with_static_reloc(unsigned int got_type,
1856 Sized_relobj<32, big_endian>* object,
1857 unsigned int index);
1858
1859 protected:
1860 // Write out the GOT table.
1861 void
1862 do_write(Output_file*);
1863
1864 private:
1865 // This class represent dynamic relocations that need to be applied by
1866 // gold because we are using TLS relocations in a static link.
1867 class Static_reloc
1868 {
1869 public:
1870 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1871 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
1872 { this->u_.global.symbol = gsym; }
1873
1874 Static_reloc(unsigned int got_offset, unsigned int r_type,
1875 Sized_relobj<32, big_endian>* relobj, unsigned int index)
1876 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
1877 {
1878 this->u_.local.relobj = relobj;
1879 this->u_.local.index = index;
1880 }
1881
1882 // Return the GOT offset.
1883 unsigned int
1884 got_offset() const
1885 { return this->got_offset_; }
1886
1887 // Relocation type.
1888 unsigned int
1889 r_type() const
1890 { return this->r_type_; }
1891
1892 // Whether the symbol is global or not.
1893 bool
1894 symbol_is_global() const
1895 { return this->symbol_is_global_; }
1896
1897 // For a relocation against a global symbol, the global symbol.
1898 Symbol*
1899 symbol() const
1900 {
1901 gold_assert(this->symbol_is_global_);
1902 return this->u_.global.symbol;
1903 }
1904
1905 // For a relocation against a local symbol, the defining object.
1906 Sized_relobj<32, big_endian>*
1907 relobj() const
1908 {
1909 gold_assert(!this->symbol_is_global_);
1910 return this->u_.local.relobj;
1911 }
1912
1913 // For a relocation against a local symbol, the local symbol index.
1914 unsigned int
1915 index() const
1916 {
1917 gold_assert(!this->symbol_is_global_);
1918 return this->u_.local.index;
1919 }
1920
1921 private:
1922 // GOT offset of the entry to which this relocation is applied.
1923 unsigned int got_offset_;
1924 // Type of relocation.
1925 unsigned int r_type_;
1926 // Whether this relocation is against a global symbol.
1927 bool symbol_is_global_;
1928 // A global or local symbol.
1929 union
1930 {
1931 struct
1932 {
1933 // For a global symbol, the symbol itself.
1934 Symbol* symbol;
1935 } global;
1936 struct
1937 {
1938 // For a local symbol, the object defining object.
1939 Sized_relobj<32, big_endian>* relobj;
1940 // For a local symbol, the symbol index.
1941 unsigned int index;
1942 } local;
1943 } u_;
1944 };
1945
1946 // Symbol table of the output object.
1947 Symbol_table* symbol_table_;
1948 // Layout of the output object.
1949 Layout* layout_;
1950 // Static relocs to be applied to the GOT.
1951 std::vector<Static_reloc> static_relocs_;
1952 };
1953
1954 // Utilities for manipulating integers of up to 32-bits
1955
1956 namespace utils
1957 {
1958 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1959 // an int32_t. NO_BITS must be between 1 to 32.
1960 template<int no_bits>
1961 static inline int32_t
1962 sign_extend(uint32_t bits)
1963 {
1964 gold_assert(no_bits >= 0 && no_bits <= 32);
1965 if (no_bits == 32)
1966 return static_cast<int32_t>(bits);
1967 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1968 bits &= mask;
1969 uint32_t top_bit = 1U << (no_bits - 1);
1970 int32_t as_signed = static_cast<int32_t>(bits);
1971 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1972 }
1973
1974 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1975 template<int no_bits>
1976 static inline bool
1977 has_overflow(uint32_t bits)
1978 {
1979 gold_assert(no_bits >= 0 && no_bits <= 32);
1980 if (no_bits == 32)
1981 return false;
1982 int32_t max = (1 << (no_bits - 1)) - 1;
1983 int32_t min = -(1 << (no_bits - 1));
1984 int32_t as_signed = static_cast<int32_t>(bits);
1985 return as_signed > max || as_signed < min;
1986 }
1987
1988 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1989 // fits in the given number of bits as either a signed or unsigned value.
1990 // For example, has_signed_unsigned_overflow<8> would check
1991 // -128 <= bits <= 255
1992 template<int no_bits>
1993 static inline bool
1994 has_signed_unsigned_overflow(uint32_t bits)
1995 {
1996 gold_assert(no_bits >= 2 && no_bits <= 32);
1997 if (no_bits == 32)
1998 return false;
1999 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
2000 int32_t min = -(1 << (no_bits - 1));
2001 int32_t as_signed = static_cast<int32_t>(bits);
2002 return as_signed > max || as_signed < min;
2003 }
2004
2005 // Select bits from A and B using bits in MASK. For each n in [0..31],
2006 // the n-th bit in the result is chosen from the n-th bits of A and B.
2007 // A zero selects A and a one selects B.
2008 static inline uint32_t
2009 bit_select(uint32_t a, uint32_t b, uint32_t mask)
2010 { return (a & ~mask) | (b & mask); }
2011 };
2012
2013 template<bool big_endian>
2014 class Target_arm : public Sized_target<32, big_endian>
2015 {
2016 public:
2017 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
2018 Reloc_section;
2019
2020 // When were are relocating a stub, we pass this as the relocation number.
2021 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
2022
2023 Target_arm()
2024 : Sized_target<32, big_endian>(&arm_info),
2025 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
2026 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
2027 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
2028 stub_tables_(), stub_factory_(Stub_factory::get_instance()),
2029 may_use_blx_(false), should_force_pic_veneer_(false),
2030 arm_input_section_map_(), attributes_section_data_(NULL),
2031 fix_cortex_a8_(false), cortex_a8_relocs_info_()
2032 { }
2033
2034 // Whether we can use BLX.
2035 bool
2036 may_use_blx() const
2037 { return this->may_use_blx_; }
2038
2039 // Set use-BLX flag.
2040 void
2041 set_may_use_blx(bool value)
2042 { this->may_use_blx_ = value; }
2043
2044 // Whether we force PCI branch veneers.
2045 bool
2046 should_force_pic_veneer() const
2047 { return this->should_force_pic_veneer_; }
2048
2049 // Set PIC veneer flag.
2050 void
2051 set_should_force_pic_veneer(bool value)
2052 { this->should_force_pic_veneer_ = value; }
2053
2054 // Whether we use THUMB-2 instructions.
2055 bool
2056 using_thumb2() const
2057 {
2058 Object_attribute* attr =
2059 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2060 int arch = attr->int_value();
2061 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
2062 }
2063
2064 // Whether we use THUMB/THUMB-2 instructions only.
2065 bool
2066 using_thumb_only() const
2067 {
2068 Object_attribute* attr =
2069 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2070
2071 if (attr->int_value() == elfcpp::TAG_CPU_ARCH_V6_M
2072 || attr->int_value() == elfcpp::TAG_CPU_ARCH_V6S_M)
2073 return true;
2074 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
2075 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
2076 return false;
2077 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
2078 return attr->int_value() == 'M';
2079 }
2080
2081 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
2082 bool
2083 may_use_arm_nop() const
2084 {
2085 Object_attribute* attr =
2086 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2087 int arch = attr->int_value();
2088 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2089 || arch == elfcpp::TAG_CPU_ARCH_V6K
2090 || arch == elfcpp::TAG_CPU_ARCH_V7
2091 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2092 }
2093
2094 // Whether we have THUMB-2 NOP.W instruction.
2095 bool
2096 may_use_thumb2_nop() const
2097 {
2098 Object_attribute* attr =
2099 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2100 int arch = attr->int_value();
2101 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2102 || arch == elfcpp::TAG_CPU_ARCH_V7
2103 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2104 }
2105
2106 // Process the relocations to determine unreferenced sections for
2107 // garbage collection.
2108 void
2109 gc_process_relocs(Symbol_table* symtab,
2110 Layout* layout,
2111 Sized_relobj<32, big_endian>* object,
2112 unsigned int data_shndx,
2113 unsigned int sh_type,
2114 const unsigned char* prelocs,
2115 size_t reloc_count,
2116 Output_section* output_section,
2117 bool needs_special_offset_handling,
2118 size_t local_symbol_count,
2119 const unsigned char* plocal_symbols);
2120
2121 // Scan the relocations to look for symbol adjustments.
2122 void
2123 scan_relocs(Symbol_table* symtab,
2124 Layout* layout,
2125 Sized_relobj<32, big_endian>* object,
2126 unsigned int data_shndx,
2127 unsigned int sh_type,
2128 const unsigned char* prelocs,
2129 size_t reloc_count,
2130 Output_section* output_section,
2131 bool needs_special_offset_handling,
2132 size_t local_symbol_count,
2133 const unsigned char* plocal_symbols);
2134
2135 // Finalize the sections.
2136 void
2137 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2138
2139 // Return the value to use for a dynamic symbol which requires special
2140 // treatment.
2141 uint64_t
2142 do_dynsym_value(const Symbol*) const;
2143
2144 // Relocate a section.
2145 void
2146 relocate_section(const Relocate_info<32, big_endian>*,
2147 unsigned int sh_type,
2148 const unsigned char* prelocs,
2149 size_t reloc_count,
2150 Output_section* output_section,
2151 bool needs_special_offset_handling,
2152 unsigned char* view,
2153 Arm_address view_address,
2154 section_size_type view_size,
2155 const Reloc_symbol_changes*);
2156
2157 // Scan the relocs during a relocatable link.
2158 void
2159 scan_relocatable_relocs(Symbol_table* symtab,
2160 Layout* layout,
2161 Sized_relobj<32, big_endian>* object,
2162 unsigned int data_shndx,
2163 unsigned int sh_type,
2164 const unsigned char* prelocs,
2165 size_t reloc_count,
2166 Output_section* output_section,
2167 bool needs_special_offset_handling,
2168 size_t local_symbol_count,
2169 const unsigned char* plocal_symbols,
2170 Relocatable_relocs*);
2171
2172 // Relocate a section during a relocatable link.
2173 void
2174 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
2175 unsigned int sh_type,
2176 const unsigned char* prelocs,
2177 size_t reloc_count,
2178 Output_section* output_section,
2179 off_t offset_in_output_section,
2180 const Relocatable_relocs*,
2181 unsigned char* view,
2182 Arm_address view_address,
2183 section_size_type view_size,
2184 unsigned char* reloc_view,
2185 section_size_type reloc_view_size);
2186
2187 // Return whether SYM is defined by the ABI.
2188 bool
2189 do_is_defined_by_abi(Symbol* sym) const
2190 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
2191
2192 // Return whether there is a GOT section.
2193 bool
2194 has_got_section() const
2195 { return this->got_ != NULL; }
2196
2197 // Return the size of the GOT section.
2198 section_size_type
2199 got_size()
2200 {
2201 gold_assert(this->got_ != NULL);
2202 return this->got_->data_size();
2203 }
2204
2205 // Map platform-specific reloc types
2206 static unsigned int
2207 get_real_reloc_type (unsigned int r_type);
2208
2209 //
2210 // Methods to support stub-generations.
2211 //
2212
2213 // Return the stub factory
2214 const Stub_factory&
2215 stub_factory() const
2216 { return this->stub_factory_; }
2217
2218 // Make a new Arm_input_section object.
2219 Arm_input_section<big_endian>*
2220 new_arm_input_section(Relobj*, unsigned int);
2221
2222 // Find the Arm_input_section object corresponding to the SHNDX-th input
2223 // section of RELOBJ.
2224 Arm_input_section<big_endian>*
2225 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
2226
2227 // Make a new Stub_table
2228 Stub_table<big_endian>*
2229 new_stub_table(Arm_input_section<big_endian>*);
2230
2231 // Scan a section for stub generation.
2232 void
2233 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2234 const unsigned char*, size_t, Output_section*,
2235 bool, const unsigned char*, Arm_address,
2236 section_size_type);
2237
2238 // Relocate a stub.
2239 void
2240 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
2241 Output_section*, unsigned char*, Arm_address,
2242 section_size_type);
2243
2244 // Get the default ARM target.
2245 static Target_arm<big_endian>*
2246 default_target()
2247 {
2248 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2249 && parameters->target().is_big_endian() == big_endian);
2250 return static_cast<Target_arm<big_endian>*>(
2251 parameters->sized_target<32, big_endian>());
2252 }
2253
2254 // Whether NAME belongs to a mapping symbol.
2255 static bool
2256 is_mapping_symbol_name(const char* name)
2257 {
2258 return (name
2259 && name[0] == '$'
2260 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2261 && (name[2] == '\0' || name[2] == '.'));
2262 }
2263
2264 // Whether we work around the Cortex-A8 erratum.
2265 bool
2266 fix_cortex_a8() const
2267 { return this->fix_cortex_a8_; }
2268
2269 // Whether we merge exidx entries in debuginfo.
2270 bool
2271 merge_exidx_entries() const
2272 { return parameters->options().merge_exidx_entries(); }
2273
2274 // Whether we fix R_ARM_V4BX relocation.
2275 // 0 - do not fix
2276 // 1 - replace with MOV instruction (armv4 target)
2277 // 2 - make interworking veneer (>= armv4t targets only)
2278 General_options::Fix_v4bx
2279 fix_v4bx() const
2280 { return parameters->options().fix_v4bx(); }
2281
2282 // Scan a span of THUMB code section for Cortex-A8 erratum.
2283 void
2284 scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2285 section_size_type, section_size_type,
2286 const unsigned char*, Arm_address);
2287
2288 // Apply Cortex-A8 workaround to a branch.
2289 void
2290 apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2291 unsigned char*, Arm_address);
2292
2293 protected:
2294 // Make an ELF object.
2295 Object*
2296 do_make_elf_object(const std::string&, Input_file*, off_t,
2297 const elfcpp::Ehdr<32, big_endian>& ehdr);
2298
2299 Object*
2300 do_make_elf_object(const std::string&, Input_file*, off_t,
2301 const elfcpp::Ehdr<32, !big_endian>&)
2302 { gold_unreachable(); }
2303
2304 Object*
2305 do_make_elf_object(const std::string&, Input_file*, off_t,
2306 const elfcpp::Ehdr<64, false>&)
2307 { gold_unreachable(); }
2308
2309 Object*
2310 do_make_elf_object(const std::string&, Input_file*, off_t,
2311 const elfcpp::Ehdr<64, true>&)
2312 { gold_unreachable(); }
2313
2314 // Make an output section.
2315 Output_section*
2316 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2317 elfcpp::Elf_Xword flags)
2318 { return new Arm_output_section<big_endian>(name, type, flags); }
2319
2320 void
2321 do_adjust_elf_header(unsigned char* view, int len) const;
2322
2323 // We only need to generate stubs, and hence perform relaxation if we are
2324 // not doing relocatable linking.
2325 bool
2326 do_may_relax() const
2327 { return !parameters->options().relocatable(); }
2328
2329 bool
2330 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
2331
2332 // Determine whether an object attribute tag takes an integer, a
2333 // string or both.
2334 int
2335 do_attribute_arg_type(int tag) const;
2336
2337 // Reorder tags during output.
2338 int
2339 do_attributes_order(int num) const;
2340
2341 // This is called when the target is selected as the default.
2342 void
2343 do_select_as_default_target()
2344 {
2345 // No locking is required since there should only be one default target.
2346 // We cannot have both the big-endian and little-endian ARM targets
2347 // as the default.
2348 gold_assert(arm_reloc_property_table == NULL);
2349 arm_reloc_property_table = new Arm_reloc_property_table();
2350 }
2351
2352 private:
2353 // The class which scans relocations.
2354 class Scan
2355 {
2356 public:
2357 Scan()
2358 : issued_non_pic_error_(false)
2359 { }
2360
2361 inline void
2362 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
2363 Sized_relobj<32, big_endian>* object,
2364 unsigned int data_shndx,
2365 Output_section* output_section,
2366 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2367 const elfcpp::Sym<32, big_endian>& lsym);
2368
2369 inline void
2370 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
2371 Sized_relobj<32, big_endian>* object,
2372 unsigned int data_shndx,
2373 Output_section* output_section,
2374 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2375 Symbol* gsym);
2376
2377 inline bool
2378 local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2379 Sized_relobj<32, big_endian>* ,
2380 unsigned int ,
2381 Output_section* ,
2382 const elfcpp::Rel<32, big_endian>& ,
2383 unsigned int ,
2384 const elfcpp::Sym<32, big_endian>&)
2385 { return false; }
2386
2387 inline bool
2388 global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2389 Sized_relobj<32, big_endian>* ,
2390 unsigned int ,
2391 Output_section* ,
2392 const elfcpp::Rel<32, big_endian>& ,
2393 unsigned int , Symbol*)
2394 { return false; }
2395
2396 private:
2397 static void
2398 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
2399 unsigned int r_type);
2400
2401 static void
2402 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
2403 unsigned int r_type, Symbol*);
2404
2405 void
2406 check_non_pic(Relobj*, unsigned int r_type);
2407
2408 // Almost identical to Symbol::needs_plt_entry except that it also
2409 // handles STT_ARM_TFUNC.
2410 static bool
2411 symbol_needs_plt_entry(const Symbol* sym)
2412 {
2413 // An undefined symbol from an executable does not need a PLT entry.
2414 if (sym->is_undefined() && !parameters->options().shared())
2415 return false;
2416
2417 return (!parameters->doing_static_link()
2418 && (sym->type() == elfcpp::STT_FUNC
2419 || sym->type() == elfcpp::STT_ARM_TFUNC)
2420 && (sym->is_from_dynobj()
2421 || sym->is_undefined()
2422 || sym->is_preemptible()));
2423 }
2424
2425 // Whether we have issued an error about a non-PIC compilation.
2426 bool issued_non_pic_error_;
2427 };
2428
2429 // The class which implements relocation.
2430 class Relocate
2431 {
2432 public:
2433 Relocate()
2434 { }
2435
2436 ~Relocate()
2437 { }
2438
2439 // Return whether the static relocation needs to be applied.
2440 inline bool
2441 should_apply_static_reloc(const Sized_symbol<32>* gsym,
2442 int ref_flags,
2443 bool is_32bit,
2444 Output_section* output_section);
2445
2446 // Do a relocation. Return false if the caller should not issue
2447 // any warnings about this relocation.
2448 inline bool
2449 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2450 Output_section*, size_t relnum,
2451 const elfcpp::Rel<32, big_endian>&,
2452 unsigned int r_type, const Sized_symbol<32>*,
2453 const Symbol_value<32>*,
2454 unsigned char*, Arm_address,
2455 section_size_type);
2456
2457 // Return whether we want to pass flag NON_PIC_REF for this
2458 // reloc. This means the relocation type accesses a symbol not via
2459 // GOT or PLT.
2460 static inline bool
2461 reloc_is_non_pic (unsigned int r_type)
2462 {
2463 switch (r_type)
2464 {
2465 // These relocation types reference GOT or PLT entries explicitly.
2466 case elfcpp::R_ARM_GOT_BREL:
2467 case elfcpp::R_ARM_GOT_ABS:
2468 case elfcpp::R_ARM_GOT_PREL:
2469 case elfcpp::R_ARM_GOT_BREL12:
2470 case elfcpp::R_ARM_PLT32_ABS:
2471 case elfcpp::R_ARM_TLS_GD32:
2472 case elfcpp::R_ARM_TLS_LDM32:
2473 case elfcpp::R_ARM_TLS_IE32:
2474 case elfcpp::R_ARM_TLS_IE12GP:
2475
2476 // These relocate types may use PLT entries.
2477 case elfcpp::R_ARM_CALL:
2478 case elfcpp::R_ARM_THM_CALL:
2479 case elfcpp::R_ARM_JUMP24:
2480 case elfcpp::R_ARM_THM_JUMP24:
2481 case elfcpp::R_ARM_THM_JUMP19:
2482 case elfcpp::R_ARM_PLT32:
2483 case elfcpp::R_ARM_THM_XPC22:
2484 case elfcpp::R_ARM_PREL31:
2485 case elfcpp::R_ARM_SBREL31:
2486 return false;
2487
2488 default:
2489 return true;
2490 }
2491 }
2492
2493 private:
2494 // Do a TLS relocation.
2495 inline typename Arm_relocate_functions<big_endian>::Status
2496 relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
2497 size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
2498 const Sized_symbol<32>*, const Symbol_value<32>*,
2499 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
2500 section_size_type);
2501
2502 };
2503
2504 // A class which returns the size required for a relocation type,
2505 // used while scanning relocs during a relocatable link.
2506 class Relocatable_size_for_reloc
2507 {
2508 public:
2509 unsigned int
2510 get_size_for_reloc(unsigned int, Relobj*);
2511 };
2512
2513 // Adjust TLS relocation type based on the options and whether this
2514 // is a local symbol.
2515 static tls::Tls_optimization
2516 optimize_tls_reloc(bool is_final, int r_type);
2517
2518 // Get the GOT section, creating it if necessary.
2519 Arm_output_data_got<big_endian>*
2520 got_section(Symbol_table*, Layout*);
2521
2522 // Get the GOT PLT section.
2523 Output_data_space*
2524 got_plt_section() const
2525 {
2526 gold_assert(this->got_plt_ != NULL);
2527 return this->got_plt_;
2528 }
2529
2530 // Create a PLT entry for a global symbol.
2531 void
2532 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2533
2534 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2535 void
2536 define_tls_base_symbol(Symbol_table*, Layout*);
2537
2538 // Create a GOT entry for the TLS module index.
2539 unsigned int
2540 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2541 Sized_relobj<32, big_endian>* object);
2542
2543 // Get the PLT section.
2544 const Output_data_plt_arm<big_endian>*
2545 plt_section() const
2546 {
2547 gold_assert(this->plt_ != NULL);
2548 return this->plt_;
2549 }
2550
2551 // Get the dynamic reloc section, creating it if necessary.
2552 Reloc_section*
2553 rel_dyn_section(Layout*);
2554
2555 // Get the section to use for TLS_DESC relocations.
2556 Reloc_section*
2557 rel_tls_desc_section(Layout*) const;
2558
2559 // Return true if the symbol may need a COPY relocation.
2560 // References from an executable object to non-function symbols
2561 // defined in a dynamic object may need a COPY relocation.
2562 bool
2563 may_need_copy_reloc(Symbol* gsym)
2564 {
2565 return (gsym->type() != elfcpp::STT_ARM_TFUNC
2566 && gsym->may_need_copy_reloc());
2567 }
2568
2569 // Add a potential copy relocation.
2570 void
2571 copy_reloc(Symbol_table* symtab, Layout* layout,
2572 Sized_relobj<32, big_endian>* object,
2573 unsigned int shndx, Output_section* output_section,
2574 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2575 {
2576 this->copy_relocs_.copy_reloc(symtab, layout,
2577 symtab->get_sized_symbol<32>(sym),
2578 object, shndx, output_section, reloc,
2579 this->rel_dyn_section(layout));
2580 }
2581
2582 // Whether two EABI versions are compatible.
2583 static bool
2584 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2585
2586 // Merge processor-specific flags from input object and those in the ELF
2587 // header of the output.
2588 void
2589 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2590
2591 // Get the secondary compatible architecture.
2592 static int
2593 get_secondary_compatible_arch(const Attributes_section_data*);
2594
2595 // Set the secondary compatible architecture.
2596 static void
2597 set_secondary_compatible_arch(Attributes_section_data*, int);
2598
2599 static int
2600 tag_cpu_arch_combine(const char*, int, int*, int, int);
2601
2602 // Helper to print AEABI enum tag value.
2603 static std::string
2604 aeabi_enum_name(unsigned int);
2605
2606 // Return string value for TAG_CPU_name.
2607 static std::string
2608 tag_cpu_name_value(unsigned int);
2609
2610 // Merge object attributes from input object and those in the output.
2611 void
2612 merge_object_attributes(const char*, const Attributes_section_data*);
2613
2614 // Helper to get an AEABI object attribute
2615 Object_attribute*
2616 get_aeabi_object_attribute(int tag) const
2617 {
2618 Attributes_section_data* pasd = this->attributes_section_data_;
2619 gold_assert(pasd != NULL);
2620 Object_attribute* attr =
2621 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2622 gold_assert(attr != NULL);
2623 return attr;
2624 }
2625
2626 //
2627 // Methods to support stub-generations.
2628 //
2629
2630 // Group input sections for stub generation.
2631 void
2632 group_sections(Layout*, section_size_type, bool);
2633
2634 // Scan a relocation for stub generation.
2635 void
2636 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2637 const Sized_symbol<32>*, unsigned int,
2638 const Symbol_value<32>*,
2639 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
2640
2641 // Scan a relocation section for stub.
2642 template<int sh_type>
2643 void
2644 scan_reloc_section_for_stubs(
2645 const Relocate_info<32, big_endian>* relinfo,
2646 const unsigned char* prelocs,
2647 size_t reloc_count,
2648 Output_section* output_section,
2649 bool needs_special_offset_handling,
2650 const unsigned char* view,
2651 elfcpp::Elf_types<32>::Elf_Addr view_address,
2652 section_size_type);
2653
2654 // Fix .ARM.exidx section coverage.
2655 void
2656 fix_exidx_coverage(Layout*, Arm_output_section<big_endian>*, Symbol_table*);
2657
2658 // Functors for STL set.
2659 struct output_section_address_less_than
2660 {
2661 bool
2662 operator()(const Output_section* s1, const Output_section* s2) const
2663 { return s1->address() < s2->address(); }
2664 };
2665
2666 // Information about this specific target which we pass to the
2667 // general Target structure.
2668 static const Target::Target_info arm_info;
2669
2670 // The types of GOT entries needed for this platform.
2671 enum Got_type
2672 {
2673 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2674 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
2675 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
2676 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
2677 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
2678 };
2679
2680 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2681
2682 // Map input section to Arm_input_section.
2683 typedef Unordered_map<Section_id,
2684 Arm_input_section<big_endian>*,
2685 Section_id_hash>
2686 Arm_input_section_map;
2687
2688 // Map output addresses to relocs for Cortex-A8 erratum.
2689 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2690 Cortex_a8_relocs_info;
2691
2692 // The GOT section.
2693 Arm_output_data_got<big_endian>* got_;
2694 // The PLT section.
2695 Output_data_plt_arm<big_endian>* plt_;
2696 // The GOT PLT section.
2697 Output_data_space* got_plt_;
2698 // The dynamic reloc section.
2699 Reloc_section* rel_dyn_;
2700 // Relocs saved to avoid a COPY reloc.
2701 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2702 // Space for variables copied with a COPY reloc.
2703 Output_data_space* dynbss_;
2704 // Offset of the GOT entry for the TLS module index.
2705 unsigned int got_mod_index_offset_;
2706 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2707 bool tls_base_symbol_defined_;
2708 // Vector of Stub_tables created.
2709 Stub_table_list stub_tables_;
2710 // Stub factory.
2711 const Stub_factory &stub_factory_;
2712 // Whether we can use BLX.
2713 bool may_use_blx_;
2714 // Whether we force PIC branch veneers.
2715 bool should_force_pic_veneer_;
2716 // Map for locating Arm_input_sections.
2717 Arm_input_section_map arm_input_section_map_;
2718 // Attributes section data in output.
2719 Attributes_section_data* attributes_section_data_;
2720 // Whether we want to fix code for Cortex-A8 erratum.
2721 bool fix_cortex_a8_;
2722 // Map addresses to relocs for Cortex-A8 erratum.
2723 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2724 };
2725
2726 template<bool big_endian>
2727 const Target::Target_info Target_arm<big_endian>::arm_info =
2728 {
2729 32, // size
2730 big_endian, // is_big_endian
2731 elfcpp::EM_ARM, // machine_code
2732 false, // has_make_symbol
2733 false, // has_resolve
2734 false, // has_code_fill
2735 true, // is_default_stack_executable
2736 '\0', // wrap_char
2737 "/usr/lib/libc.so.1", // dynamic_linker
2738 0x8000, // default_text_segment_address
2739 0x1000, // abi_pagesize (overridable by -z max-page-size)
2740 0x1000, // common_pagesize (overridable by -z common-page-size)
2741 elfcpp::SHN_UNDEF, // small_common_shndx
2742 elfcpp::SHN_UNDEF, // large_common_shndx
2743 0, // small_common_section_flags
2744 0, // large_common_section_flags
2745 ".ARM.attributes", // attributes_section
2746 "aeabi" // attributes_vendor
2747 };
2748
2749 // Arm relocate functions class
2750 //
2751
2752 template<bool big_endian>
2753 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2754 {
2755 public:
2756 typedef enum
2757 {
2758 STATUS_OKAY, // No error during relocation.
2759 STATUS_OVERFLOW, // Relocation oveflow.
2760 STATUS_BAD_RELOC // Relocation cannot be applied.
2761 } Status;
2762
2763 private:
2764 typedef Relocate_functions<32, big_endian> Base;
2765 typedef Arm_relocate_functions<big_endian> This;
2766
2767 // Encoding of imm16 argument for movt and movw ARM instructions
2768 // from ARM ARM:
2769 //
2770 // imm16 := imm4 | imm12
2771 //
2772 // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0
2773 // +-------+---------------+-------+-------+-----------------------+
2774 // | | |imm4 | |imm12 |
2775 // +-------+---------------+-------+-------+-----------------------+
2776
2777 // Extract the relocation addend from VAL based on the ARM
2778 // instruction encoding described above.
2779 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2780 extract_arm_movw_movt_addend(
2781 typename elfcpp::Swap<32, big_endian>::Valtype val)
2782 {
2783 // According to the Elf ABI for ARM Architecture the immediate
2784 // field is sign-extended to form the addend.
2785 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2786 }
2787
2788 // Insert X into VAL based on the ARM instruction encoding described
2789 // above.
2790 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2791 insert_val_arm_movw_movt(
2792 typename elfcpp::Swap<32, big_endian>::Valtype val,
2793 typename elfcpp::Swap<32, big_endian>::Valtype x)
2794 {
2795 val &= 0xfff0f000;
2796 val |= x & 0x0fff;
2797 val |= (x & 0xf000) << 4;
2798 return val;
2799 }
2800
2801 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2802 // from ARM ARM:
2803 //
2804 // imm16 := imm4 | i | imm3 | imm8
2805 //
2806 // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0
2807 // +---------+-+-----------+-------++-+-----+-------+---------------+
2808 // | |i| |imm4 || |imm3 | |imm8 |
2809 // +---------+-+-----------+-------++-+-----+-------+---------------+
2810
2811 // Extract the relocation addend from VAL based on the Thumb2
2812 // instruction encoding described above.
2813 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2814 extract_thumb_movw_movt_addend(
2815 typename elfcpp::Swap<32, big_endian>::Valtype val)
2816 {
2817 // According to the Elf ABI for ARM Architecture the immediate
2818 // field is sign-extended to form the addend.
2819 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2820 | ((val >> 15) & 0x0800)
2821 | ((val >> 4) & 0x0700)
2822 | (val & 0x00ff));
2823 }
2824
2825 // Insert X into VAL based on the Thumb2 instruction encoding
2826 // described above.
2827 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2828 insert_val_thumb_movw_movt(
2829 typename elfcpp::Swap<32, big_endian>::Valtype val,
2830 typename elfcpp::Swap<32, big_endian>::Valtype x)
2831 {
2832 val &= 0xfbf08f00;
2833 val |= (x & 0xf000) << 4;
2834 val |= (x & 0x0800) << 15;
2835 val |= (x & 0x0700) << 4;
2836 val |= (x & 0x00ff);
2837 return val;
2838 }
2839
2840 // Calculate the smallest constant Kn for the specified residual.
2841 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2842 static uint32_t
2843 calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
2844 {
2845 int32_t msb;
2846
2847 if (residual == 0)
2848 return 0;
2849 // Determine the most significant bit in the residual and
2850 // align the resulting value to a 2-bit boundary.
2851 for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
2852 ;
2853 // The desired shift is now (msb - 6), or zero, whichever
2854 // is the greater.
2855 return (((msb - 6) < 0) ? 0 : (msb - 6));
2856 }
2857
2858 // Calculate the final residual for the specified group index.
2859 // If the passed group index is less than zero, the method will return
2860 // the value of the specified residual without any change.
2861 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2862 static typename elfcpp::Swap<32, big_endian>::Valtype
2863 calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2864 const int group)
2865 {
2866 for (int n = 0; n <= group; n++)
2867 {
2868 // Calculate which part of the value to mask.
2869 uint32_t shift = calc_grp_kn(residual);
2870 // Calculate the residual for the next time around.
2871 residual &= ~(residual & (0xff << shift));
2872 }
2873
2874 return residual;
2875 }
2876
2877 // Calculate the value of Gn for the specified group index.
2878 // We return it in the form of an encoded constant-and-rotation.
2879 // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2880 static typename elfcpp::Swap<32, big_endian>::Valtype
2881 calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
2882 const int group)
2883 {
2884 typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
2885 uint32_t shift = 0;
2886
2887 for (int n = 0; n <= group; n++)
2888 {
2889 // Calculate which part of the value to mask.
2890 shift = calc_grp_kn(residual);
2891 // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
2892 gn = residual & (0xff << shift);
2893 // Calculate the residual for the next time around.
2894 residual &= ~gn;
2895 }
2896 // Return Gn in the form of an encoded constant-and-rotation.
2897 return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
2898 }
2899
2900 public:
2901 // Handle ARM long branches.
2902 static typename This::Status
2903 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2904 unsigned char *, const Sized_symbol<32>*,
2905 const Arm_relobj<big_endian>*, unsigned int,
2906 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2907
2908 // Handle THUMB long branches.
2909 static typename This::Status
2910 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2911 unsigned char *, const Sized_symbol<32>*,
2912 const Arm_relobj<big_endian>*, unsigned int,
2913 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2914
2915
2916 // Return the branch offset of a 32-bit THUMB branch.
2917 static inline int32_t
2918 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2919 {
2920 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2921 // involving the J1 and J2 bits.
2922 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2923 uint32_t upper = upper_insn & 0x3ffU;
2924 uint32_t lower = lower_insn & 0x7ffU;
2925 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2926 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2927 uint32_t i1 = j1 ^ s ? 0 : 1;
2928 uint32_t i2 = j2 ^ s ? 0 : 1;
2929
2930 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2931 | (upper << 12) | (lower << 1));
2932 }
2933
2934 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2935 // UPPER_INSN is the original upper instruction of the branch. Caller is
2936 // responsible for overflow checking and BLX offset adjustment.
2937 static inline uint16_t
2938 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2939 {
2940 uint32_t s = offset < 0 ? 1 : 0;
2941 uint32_t bits = static_cast<uint32_t>(offset);
2942 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2943 }
2944
2945 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2946 // LOWER_INSN is the original lower instruction of the branch. Caller is
2947 // responsible for overflow checking and BLX offset adjustment.
2948 static inline uint16_t
2949 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2950 {
2951 uint32_t s = offset < 0 ? 1 : 0;
2952 uint32_t bits = static_cast<uint32_t>(offset);
2953 return ((lower_insn & ~0x2fffU)
2954 | ((((bits >> 23) & 1) ^ !s) << 13)
2955 | ((((bits >> 22) & 1) ^ !s) << 11)
2956 | ((bits >> 1) & 0x7ffU));
2957 }
2958
2959 // Return the branch offset of a 32-bit THUMB conditional branch.
2960 static inline int32_t
2961 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2962 {
2963 uint32_t s = (upper_insn & 0x0400U) >> 10;
2964 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2965 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2966 uint32_t lower = (lower_insn & 0x07ffU);
2967 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2968
2969 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2970 }
2971
2972 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2973 // instruction. UPPER_INSN is the original upper instruction of the branch.
2974 // Caller is responsible for overflow checking.
2975 static inline uint16_t
2976 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2977 {
2978 uint32_t s = offset < 0 ? 1 : 0;
2979 uint32_t bits = static_cast<uint32_t>(offset);
2980 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2981 }
2982
2983 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2984 // instruction. LOWER_INSN is the original lower instruction of the branch.
2985 // Caller is reponsible for overflow checking.
2986 static inline uint16_t
2987 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2988 {
2989 uint32_t bits = static_cast<uint32_t>(offset);
2990 uint32_t j2 = (bits & 0x00080000U) >> 19;
2991 uint32_t j1 = (bits & 0x00040000U) >> 18;
2992 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2993
2994 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2995 }
2996
2997 // R_ARM_ABS8: S + A
2998 static inline typename This::Status
2999 abs8(unsigned char *view,
3000 const Sized_relobj<32, big_endian>* object,
3001 const Symbol_value<32>* psymval)
3002 {
3003 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
3004 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3005 Valtype* wv = reinterpret_cast<Valtype*>(view);
3006 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
3007 Reltype addend = utils::sign_extend<8>(val);
3008 Reltype x = psymval->value(object, addend);
3009 val = utils::bit_select(val, x, 0xffU);
3010 elfcpp::Swap<8, big_endian>::writeval(wv, val);
3011
3012 // R_ARM_ABS8 permits signed or unsigned results.
3013 int signed_x = static_cast<int32_t>(x);
3014 return ((signed_x < -128 || signed_x > 255)
3015 ? This::STATUS_OVERFLOW
3016 : This::STATUS_OKAY);
3017 }
3018
3019 // R_ARM_THM_ABS5: S + A
3020 static inline typename This::Status
3021 thm_abs5(unsigned char *view,
3022 const Sized_relobj<32, big_endian>* object,
3023 const Symbol_value<32>* psymval)
3024 {
3025 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3026 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3027 Valtype* wv = reinterpret_cast<Valtype*>(view);
3028 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3029 Reltype addend = (val & 0x7e0U) >> 6;
3030 Reltype x = psymval->value(object, addend);
3031 val = utils::bit_select(val, x << 6, 0x7e0U);
3032 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3033
3034 // R_ARM_ABS16 permits signed or unsigned results.
3035 int signed_x = static_cast<int32_t>(x);
3036 return ((signed_x < -32768 || signed_x > 65535)
3037 ? This::STATUS_OVERFLOW
3038 : This::STATUS_OKAY);
3039 }
3040
3041 // R_ARM_ABS12: S + A
3042 static inline typename This::Status
3043 abs12(unsigned char *view,
3044 const Sized_relobj<32, big_endian>* object,
3045 const Symbol_value<32>* psymval)
3046 {
3047 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3048 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3049 Valtype* wv = reinterpret_cast<Valtype*>(view);
3050 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3051 Reltype addend = val & 0x0fffU;
3052 Reltype x = psymval->value(object, addend);
3053 val = utils::bit_select(val, x, 0x0fffU);
3054 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3055 return (utils::has_overflow<12>(x)
3056 ? This::STATUS_OVERFLOW
3057 : This::STATUS_OKAY);
3058 }
3059
3060 // R_ARM_ABS16: S + A
3061 static inline typename This::Status
3062 abs16(unsigned char *view,
3063 const Sized_relobj<32, big_endian>* object,
3064 const Symbol_value<32>* psymval)
3065 {
3066 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3067 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3068 Valtype* wv = reinterpret_cast<Valtype*>(view);
3069 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3070 Reltype addend = utils::sign_extend<16>(val);
3071 Reltype x = psymval->value(object, addend);
3072 val = utils::bit_select(val, x, 0xffffU);
3073 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3074 return (utils::has_signed_unsigned_overflow<16>(x)
3075 ? This::STATUS_OVERFLOW
3076 : This::STATUS_OKAY);
3077 }
3078
3079 // R_ARM_ABS32: (S + A) | T
3080 static inline typename This::Status
3081 abs32(unsigned char *view,
3082 const Sized_relobj<32, big_endian>* object,
3083 const Symbol_value<32>* psymval,
3084 Arm_address thumb_bit)
3085 {
3086 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3087 Valtype* wv = reinterpret_cast<Valtype*>(view);
3088 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3089 Valtype x = psymval->value(object, addend) | thumb_bit;
3090 elfcpp::Swap<32, big_endian>::writeval(wv, x);
3091 return This::STATUS_OKAY;
3092 }
3093
3094 // R_ARM_REL32: (S + A) | T - P
3095 static inline typename This::Status
3096 rel32(unsigned char *view,
3097 const Sized_relobj<32, big_endian>* object,
3098 const Symbol_value<32>* psymval,
3099 Arm_address address,
3100 Arm_address thumb_bit)
3101 {
3102 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3103 Valtype* wv = reinterpret_cast<Valtype*>(view);
3104 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3105 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3106 elfcpp::Swap<32, big_endian>::writeval(wv, x);
3107 return This::STATUS_OKAY;
3108 }
3109
3110 // R_ARM_THM_JUMP24: (S + A) | T - P
3111 static typename This::Status
3112 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
3113 const Symbol_value<32>* psymval, Arm_address address,
3114 Arm_address thumb_bit);
3115
3116 // R_ARM_THM_JUMP6: S + A – P
3117 static inline typename This::Status
3118 thm_jump6(unsigned char *view,
3119 const Sized_relobj<32, big_endian>* object,
3120 const Symbol_value<32>* psymval,
3121 Arm_address address)
3122 {
3123 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3124 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3125 Valtype* wv = reinterpret_cast<Valtype*>(view);
3126 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3127 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
3128 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
3129 Reltype x = (psymval->value(object, addend) - address);
3130 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
3131 elfcpp::Swap<16, big_endian>::writeval(wv, val);
3132 // CZB does only forward jumps.
3133 return ((x > 0x007e)
3134 ? This::STATUS_OVERFLOW
3135 : This::STATUS_OKAY);
3136 }
3137
3138 // R_ARM_THM_JUMP8: S + A – P
3139 static inline typename This::Status
3140 thm_jump8(unsigned char *view,
3141 const Sized_relobj<32, big_endian>* object,
3142 const Symbol_value<32>* psymval,
3143 Arm_address address)
3144 {
3145 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3146 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3147 Valtype* wv = reinterpret_cast<Valtype*>(view);
3148 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3149 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
3150 Reltype x = (psymval->value(object, addend) - address);
3151 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
3152 return (utils::has_overflow<8>(x)
3153 ? This::STATUS_OVERFLOW
3154 : This::STATUS_OKAY);
3155 }
3156
3157 // R_ARM_THM_JUMP11: S + A – P
3158 static inline typename This::Status
3159 thm_jump11(unsigned char *view,
3160 const Sized_relobj<32, big_endian>* object,
3161 const Symbol_value<32>* psymval,
3162 Arm_address address)
3163 {
3164 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3165 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3166 Valtype* wv = reinterpret_cast<Valtype*>(view);
3167 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3168 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
3169 Reltype x = (psymval->value(object, addend) - address);
3170 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
3171 return (utils::has_overflow<11>(x)
3172 ? This::STATUS_OVERFLOW
3173 : This::STATUS_OKAY);
3174 }
3175
3176 // R_ARM_BASE_PREL: B(S) + A - P
3177 static inline typename This::Status
3178 base_prel(unsigned char* view,
3179 Arm_address origin,
3180 Arm_address address)
3181 {
3182 Base::rel32(view, origin - address);
3183 return STATUS_OKAY;
3184 }
3185
3186 // R_ARM_BASE_ABS: B(S) + A
3187 static inline typename This::Status
3188 base_abs(unsigned char* view,
3189 Arm_address origin)
3190 {
3191 Base::rel32(view, origin);
3192 return STATUS_OKAY;
3193 }
3194
3195 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3196 static inline typename This::Status
3197 got_brel(unsigned char* view,
3198 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
3199 {
3200 Base::rel32(view, got_offset);
3201 return This::STATUS_OKAY;
3202 }
3203
3204 // R_ARM_GOT_PREL: GOT(S) + A - P
3205 static inline typename This::Status
3206 got_prel(unsigned char *view,
3207 Arm_address got_entry,
3208 Arm_address address)
3209 {
3210 Base::rel32(view, got_entry - address);
3211 return This::STATUS_OKAY;
3212 }
3213
3214 // R_ARM_PREL: (S + A) | T - P
3215 static inline typename This::Status
3216 prel31(unsigned char *view,
3217 const Sized_relobj<32, big_endian>* object,
3218 const Symbol_value<32>* psymval,
3219 Arm_address address,
3220 Arm_address thumb_bit)
3221 {
3222 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3223 Valtype* wv = reinterpret_cast<Valtype*>(view);
3224 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3225 Valtype addend = utils::sign_extend<31>(val);
3226 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3227 val = utils::bit_select(val, x, 0x7fffffffU);
3228 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3229 return (utils::has_overflow<31>(x) ?
3230 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3231 }
3232
3233 // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
3234 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3235 // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3236 // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
3237 static inline typename This::Status
3238 movw(unsigned char* view,
3239 const Sized_relobj<32, big_endian>* object,
3240 const Symbol_value<32>* psymval,
3241 Arm_address relative_address_base,
3242 Arm_address thumb_bit,
3243 bool check_overflow)
3244 {
3245 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3246 Valtype* wv = reinterpret_cast<Valtype*>(view);
3247 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3248 Valtype addend = This::extract_arm_movw_movt_addend(val);
3249 Valtype x = ((psymval->value(object, addend) | thumb_bit)
3250 - relative_address_base);
3251 val = This::insert_val_arm_movw_movt(val, x);
3252 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3253 return ((check_overflow && utils::has_overflow<16>(x))
3254 ? This::STATUS_OVERFLOW
3255 : This::STATUS_OKAY);
3256 }
3257
3258 // R_ARM_MOVT_ABS: S + A (relative address base is 0)
3259 // R_ARM_MOVT_PREL: S + A - P
3260 // R_ARM_MOVT_BREL: S + A - B(S)
3261 static inline typename This::Status
3262 movt(unsigned char* view,
3263 const Sized_relobj<32, big_endian>* object,
3264 const Symbol_value<32>* psymval,
3265 Arm_address relative_address_base)
3266 {
3267 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3268 Valtype* wv = reinterpret_cast<Valtype*>(view);
3269 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3270 Valtype addend = This::extract_arm_movw_movt_addend(val);
3271 Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3272 val = This::insert_val_arm_movw_movt(val, x);
3273 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3274 // FIXME: IHI0044D says that we should check for overflow.
3275 return This::STATUS_OKAY;
3276 }
3277
3278 // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
3279 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3280 // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3281 // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
3282 static inline typename This::Status
3283 thm_movw(unsigned char *view,
3284 const Sized_relobj<32, big_endian>* object,
3285 const Symbol_value<32>* psymval,
3286 Arm_address relative_address_base,
3287 Arm_address thumb_bit,
3288 bool check_overflow)
3289 {
3290 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3291 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3292 Valtype* wv = reinterpret_cast<Valtype*>(view);
3293 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3294 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3295 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3296 Reltype x =
3297 (psymval->value(object, addend) | thumb_bit) - relative_address_base;
3298 val = This::insert_val_thumb_movw_movt(val, x);
3299 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3300 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3301 return ((check_overflow && utils::has_overflow<16>(x))
3302 ? This::STATUS_OVERFLOW
3303 : This::STATUS_OKAY);
3304 }
3305
3306 // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
3307 // R_ARM_THM_MOVT_PREL: S + A - P
3308 // R_ARM_THM_MOVT_BREL: S + A - B(S)
3309 static inline typename This::Status
3310 thm_movt(unsigned char* view,
3311 const Sized_relobj<32, big_endian>* object,
3312 const Symbol_value<32>* psymval,
3313 Arm_address relative_address_base)
3314 {
3315 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3316 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3317 Valtype* wv = reinterpret_cast<Valtype*>(view);
3318 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3319 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3320 Reltype addend = This::extract_thumb_movw_movt_addend(val);
3321 Reltype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3322 val = This::insert_val_thumb_movw_movt(val, x);
3323 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3324 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3325 return This::STATUS_OKAY;
3326 }
3327
3328 // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3329 static inline typename This::Status
3330 thm_alu11(unsigned char* view,
3331 const Sized_relobj<32, big_endian>* object,
3332 const Symbol_value<32>* psymval,
3333 Arm_address address,
3334 Arm_address thumb_bit)
3335 {
3336 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3337 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3338 Valtype* wv = reinterpret_cast<Valtype*>(view);
3339 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3340 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3341
3342 // f e d c b|a|9|8 7 6 5|4|3 2 1 0||f|e d c|b a 9 8|7 6 5 4 3 2 1 0
3343 // -----------------------------------------------------------------------
3344 // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
3345 // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
3346 // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
3347 // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
3348 // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
3349 // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
3350
3351 // Determine a sign for the addend.
3352 const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
3353 || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3354 // Thumb2 addend encoding:
3355 // imm12 := i | imm3 | imm8
3356 int32_t addend = (insn & 0xff)
3357 | ((insn & 0x00007000) >> 4)
3358 | ((insn & 0x04000000) >> 15);
3359 // Apply a sign to the added.
3360 addend *= sign;
3361
3362 int32_t x = (psymval->value(object, addend) | thumb_bit)
3363 - (address & 0xfffffffc);
3364 Reltype val = abs(x);
3365 // Mask out the value and a distinct part of the ADD/SUB opcode
3366 // (bits 7:5 of opword).
3367 insn = (insn & 0xfb0f8f00)
3368 | (val & 0xff)
3369 | ((val & 0x700) << 4)
3370 | ((val & 0x800) << 15);
3371 // Set the opcode according to whether the value to go in the
3372 // place is negative.
3373 if (x < 0)
3374 insn |= 0x00a00000;
3375
3376 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3377 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3378 return ((val > 0xfff) ?
3379 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3380 }
3381
3382 // R_ARM_THM_PC8: S + A - Pa (Thumb)
3383 static inline typename This::Status
3384 thm_pc8(unsigned char* view,
3385 const Sized_relobj<32, big_endian>* object,
3386 const Symbol_value<32>* psymval,
3387 Arm_address address)
3388 {
3389 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3390 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3391 Valtype* wv = reinterpret_cast<Valtype*>(view);
3392 Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
3393 Reltype addend = ((insn & 0x00ff) << 2);
3394 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3395 Reltype val = abs(x);
3396 insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
3397
3398 elfcpp::Swap<16, big_endian>::writeval(wv, insn);
3399 return ((val > 0x03fc)
3400 ? This::STATUS_OVERFLOW
3401 : This::STATUS_OKAY);
3402 }
3403
3404 // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3405 static inline typename This::Status
3406 thm_pc12(unsigned char* view,
3407 const Sized_relobj<32, big_endian>* object,
3408 const Symbol_value<32>* psymval,
3409 Arm_address address)
3410 {
3411 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3412 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3413 Valtype* wv = reinterpret_cast<Valtype*>(view);
3414 Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3415 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3416 // Determine a sign for the addend (positive if the U bit is 1).
3417 const int sign = (insn & 0x00800000) ? 1 : -1;
3418 int32_t addend = (insn & 0xfff);
3419 // Apply a sign to the added.
3420 addend *= sign;
3421
3422 int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3423 Reltype val = abs(x);
3424 // Mask out and apply the value and the U bit.
3425 insn = (insn & 0xff7ff000) | (val & 0xfff);
3426 // Set the U bit according to whether the value to go in the
3427 // place is positive.
3428 if (x >= 0)
3429 insn |= 0x00800000;
3430
3431 elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3432 elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3433 return ((val > 0xfff) ?
3434 This::STATUS_OVERFLOW : This::STATUS_OKAY);
3435 }
3436
3437 // R_ARM_V4BX
3438 static inline typename This::Status
3439 v4bx(const Relocate_info<32, big_endian>* relinfo,
3440 unsigned char *view,
3441 const Arm_relobj<big_endian>* object,
3442 const Arm_address address,
3443 const bool is_interworking)
3444 {
3445
3446 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3447 Valtype* wv = reinterpret_cast<Valtype*>(view);
3448 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3449
3450 // Ensure that we have a BX instruction.
3451 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3452 const uint32_t reg = (val & 0xf);
3453 if (is_interworking && reg != 0xf)
3454 {
3455 Stub_table<big_endian>* stub_table =
3456 object->stub_table(relinfo->data_shndx);
3457 gold_assert(stub_table != NULL);
3458
3459 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3460 gold_assert(stub != NULL);
3461
3462 int32_t veneer_address =
3463 stub_table->address() + stub->offset() - 8 - address;
3464 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3465 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3466 // Replace with a branch to veneer (B <addr>)
3467 val = (val & 0xf0000000) | 0x0a000000
3468 | ((veneer_address >> 2) & 0x00ffffff);
3469 }
3470 else
3471 {
3472 // Preserve Rm (lowest four bits) and the condition code
3473 // (highest four bits). Other bits encode MOV PC,Rm.
3474 val = (val & 0xf000000f) | 0x01a0f000;
3475 }
3476 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3477 return This::STATUS_OKAY;
3478 }
3479
3480 // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3481 // R_ARM_ALU_PC_G0: ((S + A) | T) - P
3482 // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3483 // R_ARM_ALU_PC_G1: ((S + A) | T) - P
3484 // R_ARM_ALU_PC_G2: ((S + A) | T) - P
3485 // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3486 // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
3487 // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3488 // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
3489 // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
3490 static inline typename This::Status
3491 arm_grp_alu(unsigned char* view,
3492 const Sized_relobj<32, big_endian>* object,
3493 const Symbol_value<32>* psymval,
3494 const int group,
3495 Arm_address address,
3496 Arm_address thumb_bit,
3497 bool check_overflow)
3498 {
3499 gold_assert(group >= 0 && group < 3);
3500 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3501 Valtype* wv = reinterpret_cast<Valtype*>(view);
3502 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3503
3504 // ALU group relocations are allowed only for the ADD/SUB instructions.
3505 // (0x00800000 - ADD, 0x00400000 - SUB)
3506 const Valtype opcode = insn & 0x01e00000;
3507 if (opcode != 0x00800000 && opcode != 0x00400000)
3508 return This::STATUS_BAD_RELOC;
3509
3510 // Determine a sign for the addend.
3511 const int sign = (opcode == 0x00800000) ? 1 : -1;
3512 // shifter = rotate_imm * 2
3513 const uint32_t shifter = (insn & 0xf00) >> 7;
3514 // Initial addend value.
3515 int32_t addend = insn & 0xff;
3516 // Rotate addend right by shifter.
3517 addend = (addend >> shifter) | (addend << (32 - shifter));
3518 // Apply a sign to the added.
3519 addend *= sign;
3520
3521 int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
3522 Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
3523 // Check for overflow if required
3524 if (check_overflow
3525 && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
3526 return This::STATUS_OVERFLOW;
3527
3528 // Mask out the value and the ADD/SUB part of the opcode; take care
3529 // not to destroy the S bit.
3530 insn &= 0xff1ff000;
3531 // Set the opcode according to whether the value to go in the
3532 // place is negative.
3533 insn |= ((x < 0) ? 0x00400000 : 0x00800000);
3534 // Encode the offset (encoded Gn).
3535 insn |= gn;
3536
3537 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3538 return This::STATUS_OKAY;
3539 }
3540
3541 // R_ARM_LDR_PC_G0: S + A - P
3542 // R_ARM_LDR_PC_G1: S + A - P
3543 // R_ARM_LDR_PC_G2: S + A - P
3544 // R_ARM_LDR_SB_G0: S + A - B(S)
3545 // R_ARM_LDR_SB_G1: S + A - B(S)
3546 // R_ARM_LDR_SB_G2: S + A - B(S)
3547 static inline typename This::Status
3548 arm_grp_ldr(unsigned char* view,
3549 const Sized_relobj<32, big_endian>* object,
3550 const Symbol_value<32>* psymval,
3551 const int group,
3552 Arm_address address)
3553 {
3554 gold_assert(group >= 0 && group < 3);
3555 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3556 Valtype* wv = reinterpret_cast<Valtype*>(view);
3557 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3558
3559 const int sign = (insn & 0x00800000) ? 1 : -1;
3560 int32_t addend = (insn & 0xfff) * sign;
3561 int32_t x = (psymval->value(object, addend) - address);
3562 // Calculate the relevant G(n-1) value to obtain this stage residual.
3563 Valtype residual =
3564 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3565 if (residual >= 0x1000)
3566 return This::STATUS_OVERFLOW;
3567
3568 // Mask out the value and U bit.
3569 insn &= 0xff7ff000;
3570 // Set the U bit for non-negative values.
3571 if (x >= 0)
3572 insn |= 0x00800000;
3573 insn |= residual;
3574
3575 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3576 return This::STATUS_OKAY;
3577 }
3578
3579 // R_ARM_LDRS_PC_G0: S + A - P
3580 // R_ARM_LDRS_PC_G1: S + A - P
3581 // R_ARM_LDRS_PC_G2: S + A - P
3582 // R_ARM_LDRS_SB_G0: S + A - B(S)
3583 // R_ARM_LDRS_SB_G1: S + A - B(S)
3584 // R_ARM_LDRS_SB_G2: S + A - B(S)
3585 static inline typename This::Status
3586 arm_grp_ldrs(unsigned char* view,
3587 const Sized_relobj<32, big_endian>* object,
3588 const Symbol_value<32>* psymval,
3589 const int group,
3590 Arm_address address)
3591 {
3592 gold_assert(group >= 0 && group < 3);
3593 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3594 Valtype* wv = reinterpret_cast<Valtype*>(view);
3595 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3596
3597 const int sign = (insn & 0x00800000) ? 1 : -1;
3598 int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
3599 int32_t x = (psymval->value(object, addend) - address);
3600 // Calculate the relevant G(n-1) value to obtain this stage residual.
3601 Valtype residual =
3602 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3603 if (residual >= 0x100)
3604 return This::STATUS_OVERFLOW;
3605
3606 // Mask out the value and U bit.
3607 insn &= 0xff7ff0f0;
3608 // Set the U bit for non-negative values.
3609 if (x >= 0)
3610 insn |= 0x00800000;
3611 insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
3612
3613 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3614 return This::STATUS_OKAY;
3615 }
3616
3617 // R_ARM_LDC_PC_G0: S + A - P
3618 // R_ARM_LDC_PC_G1: S + A - P
3619 // R_ARM_LDC_PC_G2: S + A - P
3620 // R_ARM_LDC_SB_G0: S + A - B(S)
3621 // R_ARM_LDC_SB_G1: S + A - B(S)
3622 // R_ARM_LDC_SB_G2: S + A - B(S)
3623 static inline typename This::Status
3624 arm_grp_ldc(unsigned char* view,
3625 const Sized_relobj<32, big_endian>* object,
3626 const Symbol_value<32>* psymval,
3627 const int group,
3628 Arm_address address)
3629 {
3630 gold_assert(group >= 0 && group < 3);
3631 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3632 Valtype* wv = reinterpret_cast<Valtype*>(view);
3633 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3634
3635 const int sign = (insn & 0x00800000) ? 1 : -1;
3636 int32_t addend = ((insn & 0xff) << 2) * sign;
3637 int32_t x = (psymval->value(object, addend) - address);
3638 // Calculate the relevant G(n-1) value to obtain this stage residual.
3639 Valtype residual =
3640 Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3641 if ((residual & 0x3) != 0 || residual >= 0x400)
3642 return This::STATUS_OVERFLOW;
3643
3644 // Mask out the value and U bit.
3645 insn &= 0xff7fff00;
3646 // Set the U bit for non-negative values.
3647 if (x >= 0)
3648 insn |= 0x00800000;
3649 insn |= (residual >> 2);
3650
3651 elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3652 return This::STATUS_OKAY;
3653 }
3654 };
3655
3656 // Relocate ARM long branches. This handles relocation types
3657 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3658 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3659 // undefined and we do not use PLT in this relocation. In such a case,
3660 // the branch is converted into an NOP.
3661
3662 template<bool big_endian>
3663 typename Arm_relocate_functions<big_endian>::Status
3664 Arm_relocate_functions<big_endian>::arm_branch_common(
3665 unsigned int r_type,
3666 const Relocate_info<32, big_endian>* relinfo,
3667 unsigned char *view,
3668 const Sized_symbol<32>* gsym,
3669 const Arm_relobj<big_endian>* object,
3670 unsigned int r_sym,
3671 const Symbol_value<32>* psymval,
3672 Arm_address address,
3673 Arm_address thumb_bit,
3674 bool is_weakly_undefined_without_plt)
3675 {
3676 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3677 Valtype* wv = reinterpret_cast<Valtype*>(view);
3678 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3679
3680 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3681 && ((val & 0x0f000000UL) == 0x0a000000UL);
3682 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3683 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3684 && ((val & 0x0f000000UL) == 0x0b000000UL);
3685 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3686 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3687
3688 // Check that the instruction is valid.
3689 if (r_type == elfcpp::R_ARM_CALL)
3690 {
3691 if (!insn_is_uncond_bl && !insn_is_blx)
3692 return This::STATUS_BAD_RELOC;
3693 }
3694 else if (r_type == elfcpp::R_ARM_JUMP24)
3695 {
3696 if (!insn_is_b && !insn_is_cond_bl)
3697 return This::STATUS_BAD_RELOC;
3698 }
3699 else if (r_type == elfcpp::R_ARM_PLT32)
3700 {
3701 if (!insn_is_any_branch)
3702 return This::STATUS_BAD_RELOC;
3703 }
3704 else if (r_type == elfcpp::R_ARM_XPC25)
3705 {
3706 // FIXME: AAELF document IH0044C does not say much about it other
3707 // than it being obsolete.
3708 if (!insn_is_any_branch)
3709 return This::STATUS_BAD_RELOC;
3710 }
3711 else
3712 gold_unreachable();
3713
3714 // A branch to an undefined weak symbol is turned into a jump to
3715 // the next instruction unless a PLT entry will be created.
3716 // Do the same for local undefined symbols.
3717 // The jump to the next instruction is optimized as a NOP depending
3718 // on the architecture.
3719 const Target_arm<big_endian>* arm_target =
3720 Target_arm<big_endian>::default_target();
3721 if (is_weakly_undefined_without_plt)
3722 {
3723 Valtype cond = val & 0xf0000000U;
3724 if (arm_target->may_use_arm_nop())
3725 val = cond | 0x0320f000;
3726 else
3727 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3728 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3729 return This::STATUS_OKAY;
3730 }
3731
3732 Valtype addend = utils::sign_extend<26>(val << 2);
3733 Valtype branch_target = psymval->value(object, addend);
3734 int32_t branch_offset = branch_target - address;
3735
3736 // We need a stub if the branch offset is too large or if we need
3737 // to switch mode.
3738 bool may_use_blx = arm_target->may_use_blx();
3739 Reloc_stub* stub = NULL;
3740 if (utils::has_overflow<26>(branch_offset)
3741 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
3742 {
3743 Valtype unadjusted_branch_target = psymval->value(object, 0);
3744
3745 Stub_type stub_type =
3746 Reloc_stub::stub_type_for_reloc(r_type, address,
3747 unadjusted_branch_target,
3748 (thumb_bit != 0));
3749 if (stub_type != arm_stub_none)
3750 {
3751 Stub_table<big_endian>* stub_table =
3752 object->stub_table(relinfo->data_shndx);
3753 gold_assert(stub_table != NULL);
3754
3755 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3756 stub = stub_table->find_reloc_stub(stub_key);
3757 gold_assert(stub != NULL);
3758 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3759 branch_target = stub_table->address() + stub->offset() + addend;
3760 branch_offset = branch_target - address;
3761 gold_assert(!utils::has_overflow<26>(branch_offset));
3762 }
3763 }
3764
3765 // At this point, if we still need to switch mode, the instruction
3766 // must either be a BLX or a BL that can be converted to a BLX.
3767 if (thumb_bit != 0)
3768 {
3769 // Turn BL to BLX.
3770 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3771 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3772 }
3773
3774 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3775 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3776 return (utils::has_overflow<26>(branch_offset)
3777 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3778 }
3779
3780 // Relocate THUMB long branches. This handles relocation types
3781 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3782 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3783 // undefined and we do not use PLT in this relocation. In such a case,
3784 // the branch is converted into an NOP.
3785
3786 template<bool big_endian>
3787 typename Arm_relocate_functions<big_endian>::Status
3788 Arm_relocate_functions<big_endian>::thumb_branch_common(
3789 unsigned int r_type,
3790 const Relocate_info<32, big_endian>* relinfo,
3791 unsigned char *view,
3792 const Sized_symbol<32>* gsym,
3793 const Arm_relobj<big_endian>* object,
3794 unsigned int r_sym,
3795 const Symbol_value<32>* psymval,
3796 Arm_address address,
3797 Arm_address thumb_bit,
3798 bool is_weakly_undefined_without_plt)
3799 {
3800 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3801 Valtype* wv = reinterpret_cast<Valtype*>(view);
3802 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3803 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3804
3805 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3806 // into account.
3807 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
3808 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
3809
3810 // Check that the instruction is valid.
3811 if (r_type == elfcpp::R_ARM_THM_CALL)
3812 {
3813 if (!is_bl_insn && !is_blx_insn)
3814 return This::STATUS_BAD_RELOC;
3815 }
3816 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
3817 {
3818 // This cannot be a BLX.
3819 if (!is_bl_insn)
3820 return This::STATUS_BAD_RELOC;
3821 }
3822 else if (r_type == elfcpp::R_ARM_THM_XPC22)
3823 {
3824 // Check for Thumb to Thumb call.
3825 if (!is_blx_insn)
3826 return This::STATUS_BAD_RELOC;
3827 if (thumb_bit != 0)
3828 {
3829 gold_warning(_("%s: Thumb BLX instruction targets "
3830 "thumb function '%s'."),
3831 object->name().c_str(),
3832 (gsym ? gsym->name() : "(local)"));
3833 // Convert BLX to BL.
3834 lower_insn |= 0x1000U;
3835 }
3836 }
3837 else
3838 gold_unreachable();
3839
3840 // A branch to an undefined weak symbol is turned into a jump to
3841 // the next instruction unless a PLT entry will be created.
3842 // The jump to the next instruction is optimized as a NOP.W for
3843 // Thumb-2 enabled architectures.
3844 const Target_arm<big_endian>* arm_target =
3845 Target_arm<big_endian>::default_target();
3846 if (is_weakly_undefined_without_plt)
3847 {
3848 if (arm_target->may_use_thumb2_nop())
3849 {
3850 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
3851 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
3852 }
3853 else
3854 {
3855 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
3856 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
3857 }
3858 return This::STATUS_OKAY;
3859 }
3860
3861 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
3862 Arm_address branch_target = psymval->value(object, addend);
3863
3864 // For BLX, bit 1 of target address comes from bit 1 of base address.
3865 bool may_use_blx = arm_target->may_use_blx();
3866 if (thumb_bit == 0 && may_use_blx)
3867 branch_target = utils::bit_select(branch_target, address, 0x2);
3868
3869 int32_t branch_offset = branch_target - address;
3870
3871 // We need a stub if the branch offset is too large or if we need
3872 // to switch mode.
3873 bool thumb2 = arm_target->using_thumb2();
3874 if ((!thumb2 && utils::has_overflow<23>(branch_offset))
3875 || (thumb2 && utils::has_overflow<25>(branch_offset))
3876 || ((thumb_bit == 0)
3877 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3878 || r_type == elfcpp::R_ARM_THM_JUMP24)))
3879 {
3880 Arm_address unadjusted_branch_target = psymval->value(object, 0);
3881
3882 Stub_type stub_type =
3883 Reloc_stub::stub_type_for_reloc(r_type, address,
3884 unadjusted_branch_target,
3885 (thumb_bit != 0));
3886
3887 if (stub_type != arm_stub_none)
3888 {
3889 Stub_table<big_endian>* stub_table =
3890 object->stub_table(relinfo->data_shndx);
3891 gold_assert(stub_table != NULL);
3892
3893 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3894 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
3895 gold_assert(stub != NULL);
3896 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3897 branch_target = stub_table->address() + stub->offset() + addend;
3898 if (thumb_bit == 0 && may_use_blx)
3899 branch_target = utils::bit_select(branch_target, address, 0x2);
3900 branch_offset = branch_target - address;
3901 }
3902 }
3903
3904 // At this point, if we still need to switch mode, the instruction
3905 // must either be a BLX or a BL that can be converted to a BLX.
3906 if (thumb_bit == 0)
3907 {
3908 gold_assert(may_use_blx
3909 && (r_type == elfcpp::R_ARM_THM_CALL
3910 || r_type == elfcpp::R_ARM_THM_XPC22));
3911 // Make sure this is a BLX.
3912 lower_insn &= ~0x1000U;
3913 }
3914 else
3915 {
3916 // Make sure this is a BL.
3917 lower_insn |= 0x1000U;
3918 }
3919
3920 // For a BLX instruction, make sure that the relocation is rounded up
3921 // to a word boundary. This follows the semantics of the instruction
3922 // which specifies that bit 1 of the target address will come from bit
3923 // 1 of the base address.
3924 if ((lower_insn & 0x5000U) == 0x4000U)
3925 gold_assert((branch_offset & 3) == 0);
3926
3927 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3928 // We use the Thumb-2 encoding, which is safe even if dealing with
3929 // a Thumb-1 instruction by virtue of our overflow check above. */
3930 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
3931 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
3932
3933 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3934 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3935
3936 gold_assert(!utils::has_overflow<25>(branch_offset));
3937
3938 return ((thumb2
3939 ? utils::has_overflow<25>(branch_offset)
3940 : utils::has_overflow<23>(branch_offset))
3941 ? This::STATUS_OVERFLOW
3942 : This::STATUS_OKAY);
3943 }
3944
3945 // Relocate THUMB-2 long conditional branches.
3946 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3947 // undefined and we do not use PLT in this relocation. In such a case,
3948 // the branch is converted into an NOP.
3949
3950 template<bool big_endian>
3951 typename Arm_relocate_functions<big_endian>::Status
3952 Arm_relocate_functions<big_endian>::thm_jump19(
3953 unsigned char *view,
3954 const Arm_relobj<big_endian>* object,
3955 const Symbol_value<32>* psymval,
3956 Arm_address address,
3957 Arm_address thumb_bit)
3958 {
3959 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3960 Valtype* wv = reinterpret_cast<Valtype*>(view);
3961 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3962 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3963 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
3964
3965 Arm_address branch_target = psymval->value(object, addend);
3966 int32_t branch_offset = branch_target - address;
3967
3968 // ??? Should handle interworking? GCC might someday try to
3969 // use this for tail calls.
3970 // FIXME: We do support thumb entry to PLT yet.
3971 if (thumb_bit == 0)
3972 {
3973 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3974 return This::STATUS_BAD_RELOC;
3975 }
3976
3977 // Put RELOCATION back into the insn.
3978 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
3979 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
3980
3981 // Put the relocated value back in the object file:
3982 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3983 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3984
3985 return (utils::has_overflow<21>(branch_offset)
3986 ? This::STATUS_OVERFLOW
3987 : This::STATUS_OKAY);
3988 }
3989
3990 // Get the GOT section, creating it if necessary.
3991
3992 template<bool big_endian>
3993 Arm_output_data_got<big_endian>*
3994 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
3995 {
3996 if (this->got_ == NULL)
3997 {
3998 gold_assert(symtab != NULL && layout != NULL);
3999
4000 this->got_ = new Arm_output_data_got<big_endian>(symtab, layout);
4001
4002 Output_section* os;
4003 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4004 (elfcpp::SHF_ALLOC
4005 | elfcpp::SHF_WRITE),
4006 this->got_, false, false, false,
4007 true);
4008 // The old GNU linker creates a .got.plt section. We just
4009 // create another set of data in the .got section. Note that we
4010 // always create a PLT if we create a GOT, although the PLT
4011 // might be empty.
4012 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
4013 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4014 (elfcpp::SHF_ALLOC
4015 | elfcpp::SHF_WRITE),
4016 this->got_plt_, false, false,
4017 false, false);
4018
4019 // The first three entries are reserved.
4020 this->got_plt_->set_current_data_size(3 * 4);
4021
4022 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
4023 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
4024 Symbol_table::PREDEFINED,
4025 this->got_plt_,
4026 0, 0, elfcpp::STT_OBJECT,
4027 elfcpp::STB_LOCAL,
4028 elfcpp::STV_HIDDEN, 0,
4029 false, false);
4030 }
4031 return this->got_;
4032 }
4033
4034 // Get the dynamic reloc section, creating it if necessary.
4035
4036 template<bool big_endian>
4037 typename Target_arm<big_endian>::Reloc_section*
4038 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
4039 {
4040 if (this->rel_dyn_ == NULL)
4041 {
4042 gold_assert(layout != NULL);
4043 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
4044 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
4045 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
4046 false, false, false);
4047 }
4048 return this->rel_dyn_;
4049 }
4050
4051 // Insn_template methods.
4052
4053 // Return byte size of an instruction template.
4054
4055 size_t
4056 Insn_template::size() const
4057 {
4058 switch (this->type())
4059 {
4060 case THUMB16_TYPE:
4061 case THUMB16_SPECIAL_TYPE:
4062 return 2;
4063 case ARM_TYPE:
4064 case THUMB32_TYPE:
4065 case DATA_TYPE:
4066 return 4;
4067 default:
4068 gold_unreachable();
4069 }
4070 }
4071
4072 // Return alignment of an instruction template.
4073
4074 unsigned
4075 Insn_template::alignment() const
4076 {
4077 switch (this->type())
4078 {
4079 case THUMB16_TYPE:
4080 case THUMB16_SPECIAL_TYPE:
4081 case THUMB32_TYPE:
4082 return 2;
4083 case ARM_TYPE:
4084 case DATA_TYPE:
4085 return 4;
4086 default:
4087 gold_unreachable();
4088 }
4089 }
4090
4091 // Stub_template methods.
4092
4093 Stub_template::Stub_template(
4094 Stub_type type, const Insn_template* insns,
4095 size_t insn_count)
4096 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
4097 entry_in_thumb_mode_(false), relocs_()
4098 {
4099 off_t offset = 0;
4100
4101 // Compute byte size and alignment of stub template.
4102 for (size_t i = 0; i < insn_count; i++)
4103 {
4104 unsigned insn_alignment = insns[i].alignment();
4105 size_t insn_size = insns[i].size();
4106 gold_assert((offset & (insn_alignment - 1)) == 0);
4107 this->alignment_ = std::max(this->alignment_, insn_alignment);
4108 switch (insns[i].type())
4109 {
4110 case Insn_template::THUMB16_TYPE:
4111 case Insn_template::THUMB16_SPECIAL_TYPE:
4112 if (i == 0)
4113 this->entry_in_thumb_mode_ = true;
4114 break;
4115
4116 case Insn_template::THUMB32_TYPE:
4117 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
4118 this->relocs_.push_back(Reloc(i, offset));
4119 if (i == 0)
4120 this->entry_in_thumb_mode_ = true;
4121 break;
4122
4123 case Insn_template::ARM_TYPE:
4124 // Handle cases where the target is encoded within the
4125 // instruction.
4126 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
4127 this->relocs_.push_back(Reloc(i, offset));
4128 break;
4129
4130 case Insn_template::DATA_TYPE:
4131 // Entry point cannot be data.
4132 gold_assert(i != 0);
4133 this->relocs_.push_back(Reloc(i, offset));
4134 break;
4135
4136 default:
4137 gold_unreachable();
4138 }
4139 offset += insn_size;
4140 }
4141 this->size_ = offset;
4142 }
4143
4144 // Stub methods.
4145
4146 // Template to implement do_write for a specific target endianness.
4147
4148 template<bool big_endian>
4149 void inline
4150 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
4151 {
4152 const Stub_template* stub_template = this->stub_template();
4153 const Insn_template* insns = stub_template->insns();
4154
4155 // FIXME: We do not handle BE8 encoding yet.
4156 unsigned char* pov = view;
4157 for (size_t i = 0; i < stub_template->insn_count(); i++)
4158 {
4159 switch (insns[i].type())
4160 {
4161 case Insn_template::THUMB16_TYPE:
4162 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
4163 break;
4164 case Insn_template::THUMB16_SPECIAL_TYPE:
4165 elfcpp::Swap<16, big_endian>::writeval(
4166 pov,
4167 this->thumb16_special(i));
4168 break;
4169 case Insn_template::THUMB32_TYPE:
4170 {
4171 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
4172 uint32_t lo = insns[i].data() & 0xffff;
4173 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
4174 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
4175 }
4176 break;
4177 case Insn_template::ARM_TYPE:
4178 case Insn_template::DATA_TYPE:
4179 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
4180 break;
4181 default:
4182 gold_unreachable();
4183 }
4184 pov += insns[i].size();
4185 }
4186 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
4187 }
4188
4189 // Reloc_stub::Key methods.
4190
4191 // Dump a Key as a string for debugging.
4192
4193 std::string
4194 Reloc_stub::Key::name() const
4195 {
4196 if (this->r_sym_ == invalid_index)
4197 {
4198 // Global symbol key name
4199 // <stub-type>:<symbol name>:<addend>.
4200 const std::string sym_name = this->u_.symbol->name();
4201 // We need to print two hex number and two colons. So just add 100 bytes
4202 // to the symbol name size.
4203 size_t len = sym_name.size() + 100;
4204 char* buffer = new char[len];
4205 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
4206 sym_name.c_str(), this->addend_);
4207 gold_assert(c > 0 && c < static_cast<int>(len));
4208 delete[] buffer;
4209 return std::string(buffer);
4210 }
4211 else
4212 {
4213 // local symbol key name
4214 // <stub-type>:<object>:<r_sym>:<addend>.
4215 const size_t len = 200;
4216 char buffer[len];
4217 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
4218 this->u_.relobj, this->r_sym_, this->addend_);
4219 gold_assert(c > 0 && c < static_cast<int>(len));
4220 return std::string(buffer);
4221 }
4222 }
4223
4224 // Reloc_stub methods.
4225
4226 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
4227 // LOCATION to DESTINATION.
4228 // This code is based on the arm_type_of_stub function in
4229 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
4230 // class simple.
4231
4232 Stub_type
4233 Reloc_stub::stub_type_for_reloc(
4234 unsigned int r_type,
4235 Arm_address location,
4236 Arm_address destination,
4237 bool target_is_thumb)
4238 {
4239 Stub_type stub_type = arm_stub_none;
4240
4241 // This is a bit ugly but we want to avoid using a templated class for
4242 // big and little endianities.
4243 bool may_use_blx;
4244 bool should_force_pic_veneer;
4245 bool thumb2;
4246 bool thumb_only;
4247 if (parameters->target().is_big_endian())
4248 {
4249 const Target_arm<true>* big_endian_target =
4250 Target_arm<true>::default_target();
4251 may_use_blx = big_endian_target->may_use_blx();
4252 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
4253 thumb2 = big_endian_target->using_thumb2();
4254 thumb_only = big_endian_target->using_thumb_only();
4255 }
4256 else
4257 {
4258 const Target_arm<false>* little_endian_target =
4259 Target_arm<false>::default_target();
4260 may_use_blx = little_endian_target->may_use_blx();
4261 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
4262 thumb2 = little_endian_target->using_thumb2();
4263 thumb_only = little_endian_target->using_thumb_only();
4264 }
4265
4266 int64_t branch_offset;
4267 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
4268 {
4269 // For THUMB BLX instruction, bit 1 of target comes from bit 1 of the
4270 // base address (instruction address + 4).
4271 if ((r_type == elfcpp::R_ARM_THM_CALL) && may_use_blx && !target_is_thumb)
4272 destination = utils::bit_select(destination, location, 0x2);
4273 branch_offset = static_cast<int64_t>(destination) - location;
4274
4275 // Handle cases where:
4276 // - this call goes too far (different Thumb/Thumb2 max
4277 // distance)
4278 // - it's a Thumb->Arm call and blx is not available, or it's a
4279 // Thumb->Arm branch (not bl). A stub is needed in this case.
4280 if ((!thumb2
4281 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4282 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4283 || (thumb2
4284 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4285 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4286 || ((!target_is_thumb)
4287 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4288 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
4289 {
4290 if (target_is_thumb)
4291 {
4292 // Thumb to thumb.
4293 if (!thumb_only)
4294 {
4295 stub_type = (parameters->options().shared()
4296 || should_force_pic_veneer)
4297 // PIC stubs.
4298 ? ((may_use_blx
4299 && (r_type == elfcpp::R_ARM_THM_CALL))
4300 // V5T and above. Stub starts with ARM code, so
4301 // we must be able to switch mode before
4302 // reaching it, which is only possible for 'bl'
4303 // (ie R_ARM_THM_CALL relocation).
4304 ? arm_stub_long_branch_any_thumb_pic
4305 // On V4T, use Thumb code only.
4306 : arm_stub_long_branch_v4t_thumb_thumb_pic)
4307
4308 // non-PIC stubs.
4309 : ((may_use_blx
4310 && (r_type == elfcpp::R_ARM_THM_CALL))
4311 ? arm_stub_long_branch_any_any // V5T and above.
4312 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
4313 }
4314 else
4315 {
4316 stub_type = (parameters->options().shared()
4317 || should_force_pic_veneer)
4318 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
4319 : arm_stub_long_branch_thumb_only; // non-PIC stub.
4320 }
4321 }
4322 else
4323 {
4324 // Thumb to arm.
4325
4326 // FIXME: We should check that the input section is from an
4327 // object that has interwork enabled.
4328
4329 stub_type = (parameters->options().shared()
4330 || should_force_pic_veneer)
4331 // PIC stubs.
4332 ? ((may_use_blx
4333 && (r_type == elfcpp::R_ARM_THM_CALL))
4334 ? arm_stub_long_branch_any_arm_pic // V5T and above.
4335 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
4336
4337 // non-PIC stubs.
4338 : ((may_use_blx
4339 && (r_type == elfcpp::R_ARM_THM_CALL))
4340 ? arm_stub_long_branch_any_any // V5T and above.
4341 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
4342
4343 // Handle v4t short branches.
4344 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4345 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4346 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4347 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4348 }
4349 }
4350 }
4351 else if (r_type == elfcpp::R_ARM_CALL
4352 || r_type == elfcpp::R_ARM_JUMP24
4353 || r_type == elfcpp::R_ARM_PLT32)
4354 {
4355 branch_offset = static_cast<int64_t>(destination) - location;
4356 if (target_is_thumb)
4357 {
4358 // Arm to thumb.
4359
4360 // FIXME: We should check that the input section is from an
4361 // object that has interwork enabled.
4362
4363 // We have an extra 2-bytes reach because of
4364 // the mode change (bit 24 (H) of BLX encoding).
4365 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4366 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4367 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
4368 || (r_type == elfcpp::R_ARM_JUMP24)
4369 || (r_type == elfcpp::R_ARM_PLT32))
4370 {
4371 stub_type = (parameters->options().shared()
4372 || should_force_pic_veneer)
4373 // PIC stubs.
4374 ? (may_use_blx
4375 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
4376 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
4377
4378 // non-PIC stubs.
4379 : (may_use_blx
4380 ? arm_stub_long_branch_any_any // V5T and above.
4381 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
4382 }
4383 }
4384 else
4385 {
4386 // Arm to arm.
4387 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4388 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4389 {
4390 stub_type = (parameters->options().shared()
4391 || should_force_pic_veneer)
4392 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
4393 : arm_stub_long_branch_any_any; /// non-PIC.
4394 }
4395 }
4396 }
4397
4398 return stub_type;
4399 }
4400
4401 // Cortex_a8_stub methods.
4402
4403 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4404 // I is the position of the instruction template in the stub template.
4405
4406 uint16_t
4407 Cortex_a8_stub::do_thumb16_special(size_t i)
4408 {
4409 // The only use of this is to copy condition code from a conditional
4410 // branch being worked around to the corresponding conditional branch in
4411 // to the stub.
4412 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4413 && i == 0);
4414 uint16_t data = this->stub_template()->insns()[i].data();
4415 gold_assert((data & 0xff00U) == 0xd000U);
4416 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
4417 return data;
4418 }
4419
4420 // Stub_factory methods.
4421
4422 Stub_factory::Stub_factory()
4423 {
4424 // The instruction template sequences are declared as static
4425 // objects and initialized first time the constructor runs.
4426
4427 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4428 // to reach the stub if necessary.
4429 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
4430 {
4431 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4432 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4433 // dcd R_ARM_ABS32(X)
4434 };
4435
4436 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4437 // available.
4438 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
4439 {
4440 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4441 Insn_template::arm_insn(0xe12fff1c), // bx ip
4442 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4443 // dcd R_ARM_ABS32(X)
4444 };
4445
4446 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4447 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
4448 {
4449 Insn_template::thumb16_insn(0xb401), // push {r0}
4450 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4451 Insn_template::thumb16_insn(0x4684), // mov ip, r0
4452 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4453 Insn_template::thumb16_insn(0x4760), // bx ip
4454 Insn_template::thumb16_insn(0xbf00), // nop
4455 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4456 // dcd R_ARM_ABS32(X)
4457 };
4458
4459 // V4T Thumb -> Thumb long branch stub. Using the stack is not
4460 // allowed.
4461 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
4462 {
4463 Insn_template::thumb16_insn(0x4778), // bx pc
4464 Insn_template::thumb16_insn(0x46c0), // nop
4465 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4466 Insn_template::arm_insn(0xe12fff1c), // bx ip
4467 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4468 // dcd R_ARM_ABS32(X)
4469 };
4470
4471 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4472 // available.
4473 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
4474 {
4475 Insn_template::thumb16_insn(0x4778), // bx pc
4476 Insn_template::thumb16_insn(0x46c0), // nop
4477 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
4478 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4479 // dcd R_ARM_ABS32(X)
4480 };
4481
4482 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4483 // one, when the destination is close enough.
4484 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
4485 {
4486 Insn_template::thumb16_insn(0x4778), // bx pc
4487 Insn_template::thumb16_insn(0x46c0), // nop
4488 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
4489 };
4490
4491 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
4492 // blx to reach the stub if necessary.
4493 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
4494 {
4495 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
4496 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
4497 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4498 // dcd R_ARM_REL32(X-4)
4499 };
4500
4501 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
4502 // blx to reach the stub if necessary. We can not add into pc;
4503 // it is not guaranteed to mode switch (different in ARMv6 and
4504 // ARMv7).
4505 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
4506 {
4507 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
4508 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4509 Insn_template::arm_insn(0xe12fff1c), // bx ip
4510 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4511 // dcd R_ARM_REL32(X)
4512 };
4513
4514 // V4T ARM -> ARM long branch stub, PIC.
4515 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4516 {
4517 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4518 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4519 Insn_template::arm_insn(0xe12fff1c), // bx ip
4520 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4521 // dcd R_ARM_REL32(X)
4522 };
4523
4524 // V4T Thumb -> ARM long branch stub, PIC.
4525 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4526 {
4527 Insn_template::thumb16_insn(0x4778), // bx pc
4528 Insn_template::thumb16_insn(0x46c0), // nop
4529 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4530 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4531 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4532 // dcd R_ARM_REL32(X)
4533 };
4534
4535 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4536 // architectures.
4537 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4538 {
4539 Insn_template::thumb16_insn(0xb401), // push {r0}
4540 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4541 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4542 Insn_template::thumb16_insn(0x4484), // add ip, r0
4543 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4544 Insn_template::thumb16_insn(0x4760), // bx ip
4545 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4546 // dcd R_ARM_REL32(X)
4547 };
4548
4549 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4550 // allowed.
4551 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4552 {
4553 Insn_template::thumb16_insn(0x4778), // bx pc
4554 Insn_template::thumb16_insn(0x46c0), // nop
4555 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4556 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4557 Insn_template::arm_insn(0xe12fff1c), // bx ip
4558 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4559 // dcd R_ARM_REL32(X)
4560 };
4561
4562 // Cortex-A8 erratum-workaround stubs.
4563
4564 // Stub used for conditional branches (which may be beyond +/-1MB away,
4565 // so we can't use a conditional branch to reach this stub).
4566
4567 // original code:
4568 //
4569 // b<cond> X
4570 // after:
4571 //
4572 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4573 {
4574 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4575 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4576 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4577 // b.w X
4578 };
4579
4580 // Stub used for b.w and bl.w instructions.
4581
4582 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4583 {
4584 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4585 };
4586
4587 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4588 {
4589 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4590 };
4591
4592 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4593 // instruction (which switches to ARM mode) to point to this stub. Jump to
4594 // the real destination using an ARM-mode branch.
4595 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
4596 {
4597 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4598 };
4599
4600 // Stub used to provide an interworking for R_ARM_V4BX relocation
4601 // (bx r[n] instruction).
4602 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4603 {
4604 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4605 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4606 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4607 };
4608
4609 // Fill in the stub template look-up table. Stub templates are constructed
4610 // per instance of Stub_factory for fast look-up without locking
4611 // in a thread-enabled environment.
4612
4613 this->stub_templates_[arm_stub_none] =
4614 new Stub_template(arm_stub_none, NULL, 0);
4615
4616 #define DEF_STUB(x) \
4617 do \
4618 { \
4619 size_t array_size \
4620 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4621 Stub_type type = arm_stub_##x; \
4622 this->stub_templates_[type] = \
4623 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4624 } \
4625 while (0);
4626
4627 DEF_STUBS
4628 #undef DEF_STUB
4629 }
4630
4631 // Stub_table methods.
4632
4633 // Removel all Cortex-A8 stub.
4634
4635 template<bool big_endian>
4636 void
4637 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4638 {
4639 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4640 p != this->cortex_a8_stubs_.end();
4641 ++p)
4642 delete p->second;
4643 this->cortex_a8_stubs_.clear();
4644 }
4645
4646 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4647
4648 template<bool big_endian>
4649 void
4650 Stub_table<big_endian>::relocate_stub(
4651 Stub* stub,
4652 const Relocate_info<32, big_endian>* relinfo,
4653 Target_arm<big_endian>* arm_target,
4654 Output_section* output_section,
4655 unsigned char* view,
4656 Arm_address address,
4657 section_size_type view_size)
4658 {
4659 const Stub_template* stub_template = stub->stub_template();
4660 if (stub_template->reloc_count() != 0)
4661 {
4662 // Adjust view to cover the stub only.
4663 section_size_type offset = stub->offset();
4664 section_size_type stub_size = stub_template->size();
4665 gold_assert(offset + stub_size <= view_size);
4666
4667 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4668 address + offset, stub_size);
4669 }
4670 }
4671
4672 // Relocate all stubs in this stub table.
4673
4674 template<bool big_endian>
4675 void
4676 Stub_table<big_endian>::relocate_stubs(
4677 const Relocate_info<32, big_endian>* relinfo,
4678 Target_arm<big_endian>* arm_target,
4679 Output_section* output_section,
4680 unsigned char* view,
4681 Arm_address address,
4682 section_size_type view_size)
4683 {
4684 // If we are passed a view bigger than the stub table's. we need to
4685 // adjust the view.
4686 gold_assert(address == this->address()
4687 && (view_size
4688 == static_cast<section_size_type>(this->data_size())));
4689
4690 // Relocate all relocation stubs.
4691 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4692 p != this->reloc_stubs_.end();
4693 ++p)
4694 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4695 address, view_size);
4696
4697 // Relocate all Cortex-A8 stubs.
4698 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4699 p != this->cortex_a8_stubs_.end();
4700 ++p)
4701 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4702 address, view_size);
4703
4704 // Relocate all ARM V4BX stubs.
4705 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4706 p != this->arm_v4bx_stubs_.end();
4707 ++p)
4708 {
4709 if (*p != NULL)
4710 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4711 address, view_size);
4712 }
4713 }
4714
4715 // Write out the stubs to file.
4716
4717 template<bool big_endian>
4718 void
4719 Stub_table<big_endian>::do_write(Output_file* of)
4720 {
4721 off_t offset = this->offset();
4722 const section_size_type oview_size =
4723 convert_to_section_size_type(this->data_size());
4724 unsigned char* const oview = of->get_output_view(offset, oview_size);
4725
4726 // Write relocation stubs.
4727 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4728 p != this->reloc_stubs_.end();
4729 ++p)
4730 {
4731 Reloc_stub* stub = p->second;
4732 Arm_address address = this->address() + stub->offset();
4733 gold_assert(address
4734 == align_address(address,
4735 stub->stub_template()->alignment()));
4736 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4737 big_endian);
4738 }
4739
4740 // Write Cortex-A8 stubs.
4741 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4742 p != this->cortex_a8_stubs_.end();
4743 ++p)
4744 {
4745 Cortex_a8_stub* stub = p->second;
4746 Arm_address address = this->address() + stub->offset();
4747 gold_assert(address
4748 == align_address(address,
4749 stub->stub_template()->alignment()));
4750 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4751 big_endian);
4752 }
4753
4754 // Write ARM V4BX relocation stubs.
4755 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4756 p != this->arm_v4bx_stubs_.end();
4757 ++p)
4758 {
4759 if (*p == NULL)
4760 continue;
4761
4762 Arm_address address = this->address() + (*p)->offset();
4763 gold_assert(address
4764 == align_address(address,
4765 (*p)->stub_template()->alignment()));
4766 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4767 big_endian);
4768 }
4769
4770 of->write_output_view(this->offset(), oview_size, oview);
4771 }
4772
4773 // Update the data size and address alignment of the stub table at the end
4774 // of a relaxation pass. Return true if either the data size or the
4775 // alignment changed in this relaxation pass.
4776
4777 template<bool big_endian>
4778 bool
4779 Stub_table<big_endian>::update_data_size_and_addralign()
4780 {
4781 // Go over all stubs in table to compute data size and address alignment.
4782 off_t size = this->reloc_stubs_size_;
4783 unsigned addralign = this->reloc_stubs_addralign_;
4784
4785 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4786 p != this->cortex_a8_stubs_.end();
4787 ++p)
4788 {
4789 const Stub_template* stub_template = p->second->stub_template();
4790 addralign = std::max(addralign, stub_template->alignment());
4791 size = (align_address(size, stub_template->alignment())
4792 + stub_template->size());
4793 }
4794
4795 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4796 p != this->arm_v4bx_stubs_.end();
4797 ++p)
4798 {
4799 if (*p == NULL)
4800 continue;
4801
4802 const Stub_template* stub_template = (*p)->stub_template();
4803 addralign = std::max(addralign, stub_template->alignment());
4804 size = (align_address(size, stub_template->alignment())
4805 + stub_template->size());
4806 }
4807
4808 // Check if either data size or alignment changed in this pass.
4809 // Update prev_data_size_ and prev_addralign_. These will be used
4810 // as the current data size and address alignment for the next pass.
4811 bool changed = size != this->prev_data_size_;
4812 this->prev_data_size_ = size;
4813
4814 if (addralign != this->prev_addralign_)
4815 changed = true;
4816 this->prev_addralign_ = addralign;
4817
4818 return changed;
4819 }
4820
4821 // Finalize the stubs. This sets the offsets of the stubs within the stub
4822 // table. It also marks all input sections needing Cortex-A8 workaround.
4823
4824 template<bool big_endian>
4825 void
4826 Stub_table<big_endian>::finalize_stubs()
4827 {
4828 off_t off = this->reloc_stubs_size_;
4829 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4830 p != this->cortex_a8_stubs_.end();
4831 ++p)
4832 {
4833 Cortex_a8_stub* stub = p->second;
4834 const Stub_template* stub_template = stub->stub_template();
4835 uint64_t stub_addralign = stub_template->alignment();
4836 off = align_address(off, stub_addralign);
4837 stub->set_offset(off);
4838 off += stub_template->size();
4839
4840 // Mark input section so that we can determine later if a code section
4841 // needs the Cortex-A8 workaround quickly.
4842 Arm_relobj<big_endian>* arm_relobj =
4843 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
4844 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
4845 }
4846
4847 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4848 p != this->arm_v4bx_stubs_.end();
4849 ++p)
4850 {
4851 if (*p == NULL)
4852 continue;
4853
4854 const Stub_template* stub_template = (*p)->stub_template();
4855 uint64_t stub_addralign = stub_template->alignment();
4856 off = align_address(off, stub_addralign);
4857 (*p)->set_offset(off);
4858 off += stub_template->size();
4859 }
4860
4861 gold_assert(off <= this->prev_data_size_);
4862 }
4863
4864 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4865 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4866 // of the address range seen by the linker.
4867
4868 template<bool big_endian>
4869 void
4870 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
4871 Target_arm<big_endian>* arm_target,
4872 unsigned char* view,
4873 Arm_address view_address,
4874 section_size_type view_size)
4875 {
4876 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4877 for (Cortex_a8_stub_list::const_iterator p =
4878 this->cortex_a8_stubs_.lower_bound(view_address);
4879 ((p != this->cortex_a8_stubs_.end())
4880 && (p->first < (view_address + view_size)));
4881 ++p)
4882 {
4883 // We do not store the THUMB bit in the LSB of either the branch address
4884 // or the stub offset. There is no need to strip the LSB.
4885 Arm_address branch_address = p->first;
4886 const Cortex_a8_stub* stub = p->second;
4887 Arm_address stub_address = this->address() + stub->offset();
4888
4889 // Offset of the branch instruction relative to this view.
4890 section_size_type offset =
4891 convert_to_section_size_type(branch_address - view_address);
4892 gold_assert((offset + 4) <= view_size);
4893
4894 arm_target->apply_cortex_a8_workaround(stub, stub_address,
4895 view + offset, branch_address);
4896 }
4897 }
4898
4899 // Arm_input_section methods.
4900
4901 // Initialize an Arm_input_section.
4902
4903 template<bool big_endian>
4904 void
4905 Arm_input_section<big_endian>::init()
4906 {
4907 Relobj* relobj = this->relobj();
4908 unsigned int shndx = this->shndx();
4909
4910 // Cache these to speed up size and alignment queries. It is too slow
4911 // to call section_addraglin and section_size every time.
4912 this->original_addralign_ = relobj->section_addralign(shndx);
4913 this->original_size_ = relobj->section_size(shndx);
4914
4915 // We want to make this look like the original input section after
4916 // output sections are finalized.
4917 Output_section* os = relobj->output_section(shndx);
4918 off_t offset = relobj->output_section_offset(shndx);
4919 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
4920 this->set_address(os->address() + offset);
4921 this->set_file_offset(os->offset() + offset);
4922
4923 this->set_current_data_size(this->original_size_);
4924 this->finalize_data_size();
4925 }
4926
4927 template<bool big_endian>
4928 void
4929 Arm_input_section<big_endian>::do_write(Output_file* of)
4930 {
4931 // We have to write out the original section content.
4932 section_size_type section_size;
4933 const unsigned char* section_contents =
4934 this->relobj()->section_contents(this->shndx(), &section_size, false);
4935 of->write(this->offset(), section_contents, section_size);
4936
4937 // If this owns a stub table and it is not empty, write it.
4938 if (this->is_stub_table_owner() && !this->stub_table_->empty())
4939 this->stub_table_->write(of);
4940 }
4941
4942 // Finalize data size.
4943
4944 template<bool big_endian>
4945 void
4946 Arm_input_section<big_endian>::set_final_data_size()
4947 {
4948 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4949
4950 if (this->is_stub_table_owner())
4951 {
4952 // The stub table comes after the original section contents.
4953 off = align_address(off, this->stub_table_->addralign());
4954 this->stub_table_->set_address_and_file_offset(this->address() + off,
4955 this->offset() + off);
4956 off += this->stub_table_->data_size();
4957 }
4958 this->set_data_size(off);
4959 }
4960
4961 // Reset address and file offset.
4962
4963 template<bool big_endian>
4964 void
4965 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
4966 {
4967 // Size of the original input section contents.
4968 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4969
4970 // If this is a stub table owner, account for the stub table size.
4971 if (this->is_stub_table_owner())
4972 {
4973 Stub_table<big_endian>* stub_table = this->stub_table_;
4974
4975 // Reset the stub table's address and file offset. The
4976 // current data size for child will be updated after that.
4977 stub_table_->reset_address_and_file_offset();
4978 off = align_address(off, stub_table_->addralign());
4979 off += stub_table->current_data_size();
4980 }
4981
4982 this->set_current_data_size(off);
4983 }
4984
4985 // Arm_exidx_cantunwind methods.
4986
4987 // Write this to Output file OF for a fixed endianness.
4988
4989 template<bool big_endian>
4990 void
4991 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
4992 {
4993 off_t offset = this->offset();
4994 const section_size_type oview_size = 8;
4995 unsigned char* const oview = of->get_output_view(offset, oview_size);
4996
4997 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4998 Valtype* wv = reinterpret_cast<Valtype*>(oview);
4999
5000 Output_section* os = this->relobj_->output_section(this->shndx_);
5001 gold_assert(os != NULL);
5002
5003 Arm_relobj<big_endian>* arm_relobj =
5004 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
5005 Arm_address output_offset =
5006 arm_relobj->get_output_section_offset(this->shndx_);
5007 Arm_address section_start;
5008 if (output_offset != Arm_relobj<big_endian>::invalid_address)
5009 section_start = os->address() + output_offset;
5010 else
5011 {
5012 // Currently this only happens for a relaxed section.
5013 const Output_relaxed_input_section* poris =
5014 os->find_relaxed_input_section(this->relobj_, this->shndx_);
5015 gold_assert(poris != NULL);
5016 section_start = poris->address();
5017 }
5018
5019 // We always append this to the end of an EXIDX section.
5020 Arm_address output_address =
5021 section_start + this->relobj_->section_size(this->shndx_);
5022
5023 // Write out the entry. The first word either points to the beginning
5024 // or after the end of a text section. The second word is the special
5025 // EXIDX_CANTUNWIND value.
5026 uint32_t prel31_offset = output_address - this->address();
5027 if (utils::has_overflow<31>(offset))
5028 gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
5029 elfcpp::Swap<32, big_endian>::writeval(wv, prel31_offset & 0x7fffffffU);
5030 elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
5031
5032 of->write_output_view(this->offset(), oview_size, oview);
5033 }
5034
5035 // Arm_exidx_merged_section methods.
5036
5037 // Constructor for Arm_exidx_merged_section.
5038 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
5039 // SECTION_OFFSET_MAP points to a section offset map describing how
5040 // parts of the input section are mapped to output. DELETED_BYTES is
5041 // the number of bytes deleted from the EXIDX input section.
5042
5043 Arm_exidx_merged_section::Arm_exidx_merged_section(
5044 const Arm_exidx_input_section& exidx_input_section,
5045 const Arm_exidx_section_offset_map& section_offset_map,
5046 uint32_t deleted_bytes)
5047 : Output_relaxed_input_section(exidx_input_section.relobj(),
5048 exidx_input_section.shndx(),
5049 exidx_input_section.addralign()),
5050 exidx_input_section_(exidx_input_section),
5051 section_offset_map_(section_offset_map)
5052 {
5053 // Fix size here so that we do not need to implement set_final_data_size.
5054 this->set_data_size(exidx_input_section.size() - deleted_bytes);
5055 this->fix_data_size();
5056 }
5057
5058 // Given an input OBJECT, an input section index SHNDX within that
5059 // object, and an OFFSET relative to the start of that input
5060 // section, return whether or not the corresponding offset within
5061 // the output section is known. If this function returns true, it
5062 // sets *POUTPUT to the output offset. The value -1 indicates that
5063 // this input offset is being discarded.
5064
5065 bool
5066 Arm_exidx_merged_section::do_output_offset(
5067 const Relobj* relobj,
5068 unsigned int shndx,
5069 section_offset_type offset,
5070 section_offset_type* poutput) const
5071 {
5072 // We only handle offsets for the original EXIDX input section.
5073 if (relobj != this->exidx_input_section_.relobj()
5074 || shndx != this->exidx_input_section_.shndx())
5075 return false;
5076
5077 section_offset_type section_size =
5078 convert_types<section_offset_type>(this->exidx_input_section_.size());
5079 if (offset < 0 || offset >= section_size)
5080 // Input offset is out of valid range.
5081 *poutput = -1;
5082 else
5083 {
5084 // We need to look up the section offset map to determine the output
5085 // offset. Find the reference point in map that is first offset
5086 // bigger than or equal to this offset.
5087 Arm_exidx_section_offset_map::const_iterator p =
5088 this->section_offset_map_.lower_bound(offset);
5089
5090 // The section offset maps are build such that this should not happen if
5091 // input offset is in the valid range.
5092 gold_assert(p != this->section_offset_map_.end());
5093
5094 // We need to check if this is dropped.
5095 section_offset_type ref = p->first;
5096 section_offset_type mapped_ref = p->second;
5097
5098 if (mapped_ref != Arm_exidx_input_section::invalid_offset)
5099 // Offset is present in output.
5100 *poutput = mapped_ref + (offset - ref);
5101 else
5102 // Offset is discarded owing to EXIDX entry merging.
5103 *poutput = -1;
5104 }
5105
5106 return true;
5107 }
5108
5109 // Write this to output file OF.
5110
5111 void
5112 Arm_exidx_merged_section::do_write(Output_file* of)
5113 {
5114 // If we retain or discard the whole EXIDX input section, we would
5115 // not be here.
5116 gold_assert(this->data_size() != this->exidx_input_section_.size()
5117 && this->data_size() != 0);
5118
5119 off_t offset = this->offset();
5120 const section_size_type oview_size = this->data_size();
5121 unsigned char* const oview = of->get_output_view(offset, oview_size);
5122
5123 Output_section* os = this->relobj()->output_section(this->shndx());
5124 gold_assert(os != NULL);
5125
5126 // Get contents of EXIDX input section.
5127 section_size_type section_size;
5128 const unsigned char* section_contents =
5129 this->relobj()->section_contents(this->shndx(), &section_size, false);
5130 gold_assert(section_size == this->exidx_input_section_.size());
5131
5132 // Go over spans of input offsets and write only those that are not
5133 // discarded.
5134 section_offset_type in_start = 0;
5135 section_offset_type out_start = 0;
5136 for(Arm_exidx_section_offset_map::const_iterator p =
5137 this->section_offset_map_.begin();
5138 p != this->section_offset_map_.end();
5139 ++p)
5140 {
5141 section_offset_type in_end = p->first;
5142 gold_assert(in_end >= in_start);
5143 section_offset_type out_end = p->second;
5144 size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
5145 if (out_end != -1)
5146 {
5147 size_t out_chunk_size =
5148 convert_types<size_t>(out_end - out_start + 1);
5149 gold_assert(out_chunk_size == in_chunk_size);
5150 memcpy(oview + out_start, section_contents + in_start,
5151 out_chunk_size);
5152 out_start += out_chunk_size;
5153 }
5154 in_start += in_chunk_size;
5155 }
5156
5157 gold_assert(convert_to_section_size_type(out_start) == oview_size);
5158 of->write_output_view(this->offset(), oview_size, oview);
5159 }
5160
5161 // Arm_exidx_fixup methods.
5162
5163 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
5164 // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
5165 // points to the end of the last seen EXIDX section.
5166
5167 void
5168 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
5169 {
5170 if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
5171 && this->last_input_section_ != NULL)
5172 {
5173 Relobj* relobj = this->last_input_section_->relobj();
5174 unsigned int text_shndx = this->last_input_section_->link();
5175 Arm_exidx_cantunwind* cantunwind =
5176 new Arm_exidx_cantunwind(relobj, text_shndx);
5177 this->exidx_output_section_->add_output_section_data(cantunwind);
5178 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5179 }
5180 }
5181
5182 // Process an EXIDX section entry in input. Return whether this entry
5183 // can be deleted in the output. SECOND_WORD in the second word of the
5184 // EXIDX entry.
5185
5186 bool
5187 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
5188 {
5189 bool delete_entry;
5190 if (second_word == elfcpp::EXIDX_CANTUNWIND)
5191 {
5192 // Merge if previous entry is also an EXIDX_CANTUNWIND.
5193 delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
5194 this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5195 }
5196 else if ((second_word & 0x80000000) != 0)
5197 {
5198 // Inlined unwinding data. Merge if equal to previous.
5199 delete_entry = (merge_exidx_entries_
5200 && this->last_unwind_type_ == UT_INLINED_ENTRY
5201 && this->last_inlined_entry_ == second_word);
5202 this->last_unwind_type_ = UT_INLINED_ENTRY;
5203 this->last_inlined_entry_ = second_word;
5204 }
5205 else
5206 {
5207 // Normal table entry. In theory we could merge these too,
5208 // but duplicate entries are likely to be much less common.
5209 delete_entry = false;
5210 this->last_unwind_type_ = UT_NORMAL_ENTRY;
5211 }
5212 return delete_entry;
5213 }
5214
5215 // Update the current section offset map during EXIDX section fix-up.
5216 // If there is no map, create one. INPUT_OFFSET is the offset of a
5217 // reference point, DELETED_BYTES is the number of deleted by in the
5218 // section so far. If DELETE_ENTRY is true, the reference point and
5219 // all offsets after the previous reference point are discarded.
5220
5221 void
5222 Arm_exidx_fixup::update_offset_map(
5223 section_offset_type input_offset,
5224 section_size_type deleted_bytes,
5225 bool delete_entry)
5226 {
5227 if (this->section_offset_map_ == NULL)
5228 this->section_offset_map_ = new Arm_exidx_section_offset_map();
5229 section_offset_type output_offset;
5230 if (delete_entry)
5231 output_offset = Arm_exidx_input_section::invalid_offset;
5232 else
5233 output_offset = input_offset - deleted_bytes;
5234 (*this->section_offset_map_)[input_offset] = output_offset;
5235 }
5236
5237 // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
5238 // bytes deleted. If some entries are merged, also store a pointer to a newly
5239 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The
5240 // caller owns the map and is responsible for releasing it after use.
5241
5242 template<bool big_endian>
5243 uint32_t
5244 Arm_exidx_fixup::process_exidx_section(
5245 const Arm_exidx_input_section* exidx_input_section,
5246 Arm_exidx_section_offset_map** psection_offset_map)
5247 {
5248 Relobj* relobj = exidx_input_section->relobj();
5249 unsigned shndx = exidx_input_section->shndx();
5250 section_size_type section_size;
5251 const unsigned char* section_contents =
5252 relobj->section_contents(shndx, &section_size, false);
5253
5254 if ((section_size % 8) != 0)
5255 {
5256 // Something is wrong with this section. Better not touch it.
5257 gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5258 relobj->name().c_str(), shndx);
5259 this->last_input_section_ = exidx_input_section;
5260 this->last_unwind_type_ = UT_NONE;
5261 return 0;
5262 }
5263
5264 uint32_t deleted_bytes = 0;
5265 bool prev_delete_entry = false;
5266 gold_assert(this->section_offset_map_ == NULL);
5267
5268 for (section_size_type i = 0; i < section_size; i += 8)
5269 {
5270 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5271 const Valtype* wv =
5272 reinterpret_cast<const Valtype*>(section_contents + i + 4);
5273 uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
5274
5275 bool delete_entry = this->process_exidx_entry(second_word);
5276
5277 // Entry deletion causes changes in output offsets. We use a std::map
5278 // to record these. And entry (x, y) means input offset x
5279 // is mapped to output offset y. If y is invalid_offset, then x is
5280 // dropped in the output. Because of the way std::map::lower_bound
5281 // works, we record the last offset in a region w.r.t to keeping or
5282 // dropping. If there is no entry (x0, y0) for an input offset x0,
5283 // the output offset y0 of it is determined by the output offset y1 of
5284 // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5285 // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Othewise, y1
5286 // y0 is also -1.
5287 if (delete_entry != prev_delete_entry && i != 0)
5288 this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
5289
5290 // Update total deleted bytes for this entry.
5291 if (delete_entry)
5292 deleted_bytes += 8;
5293
5294 prev_delete_entry = delete_entry;
5295 }
5296
5297 // If section offset map is not NULL, make an entry for the end of
5298 // section.
5299 if (this->section_offset_map_ != NULL)
5300 update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
5301
5302 *psection_offset_map = this->section_offset_map_;
5303 this->section_offset_map_ = NULL;
5304 this->last_input_section_ = exidx_input_section;
5305
5306 // Set the first output text section so that we can link the EXIDX output
5307 // section to it. Ignore any EXIDX input section that is completely merged.
5308 if (this->first_output_text_section_ == NULL
5309 && deleted_bytes != section_size)
5310 {
5311 unsigned int link = exidx_input_section->link();
5312 Output_section* os = relobj->output_section(link);
5313 gold_assert(os != NULL);
5314 this->first_output_text_section_ = os;
5315 }
5316
5317 return deleted_bytes;
5318 }
5319
5320 // Arm_output_section methods.
5321
5322 // Create a stub group for input sections from BEGIN to END. OWNER
5323 // points to the input section to be the owner a new stub table.
5324
5325 template<bool big_endian>
5326 void
5327 Arm_output_section<big_endian>::create_stub_group(
5328 Input_section_list::const_iterator begin,
5329 Input_section_list::const_iterator end,
5330 Input_section_list::const_iterator owner,
5331 Target_arm<big_endian>* target,
5332 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
5333 {
5334 // We use a different kind of relaxed section in an EXIDX section.
5335 // The static casting from Output_relaxed_input_section to
5336 // Arm_input_section is invalid in an EXIDX section. We are okay
5337 // because we should not be calling this for an EXIDX section.
5338 gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
5339
5340 // Currently we convert ordinary input sections into relaxed sections only
5341 // at this point but we may want to support creating relaxed input section
5342 // very early. So we check here to see if owner is already a relaxed
5343 // section.
5344
5345 Arm_input_section<big_endian>* arm_input_section;
5346 if (owner->is_relaxed_input_section())
5347 {
5348 arm_input_section =
5349 Arm_input_section<big_endian>::as_arm_input_section(
5350 owner->relaxed_input_section());
5351 }
5352 else
5353 {
5354 gold_assert(owner->is_input_section());
5355 // Create a new relaxed input section.
5356 arm_input_section =
5357 target->new_arm_input_section(owner->relobj(), owner->shndx());
5358 new_relaxed_sections->push_back(arm_input_section);
5359 }
5360
5361 // Create a stub table.
5362 Stub_table<big_endian>* stub_table =
5363 target->new_stub_table(arm_input_section);
5364
5365 arm_input_section->set_stub_table(stub_table);
5366
5367 Input_section_list::const_iterator p = begin;
5368 Input_section_list::const_iterator prev_p;
5369
5370 // Look for input sections or relaxed input sections in [begin ... end].
5371 do
5372 {
5373 if (p->is_input_section() || p->is_relaxed_input_section())
5374 {
5375 // The stub table information for input sections live
5376 // in their objects.
5377 Arm_relobj<big_endian>* arm_relobj =
5378 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5379 arm_relobj->set_stub_table(p->shndx(), stub_table);
5380 }
5381 prev_p = p++;
5382 }
5383 while (prev_p != end);
5384 }
5385
5386 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
5387 // of stub groups. We grow a stub group by adding input section until the
5388 // size is just below GROUP_SIZE. The last input section will be converted
5389 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5390 // input section after the stub table, effectively double the group size.
5391 //
5392 // This is similar to the group_sections() function in elf32-arm.c but is
5393 // implemented differently.
5394
5395 template<bool big_endian>
5396 void
5397 Arm_output_section<big_endian>::group_sections(
5398 section_size_type group_size,
5399 bool stubs_always_after_branch,
5400 Target_arm<big_endian>* target)
5401 {
5402 // We only care about sections containing code.
5403 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5404 return;
5405
5406 // States for grouping.
5407 typedef enum
5408 {
5409 // No group is being built.
5410 NO_GROUP,
5411 // A group is being built but the stub table is not found yet.
5412 // We keep group a stub group until the size is just under GROUP_SIZE.
5413 // The last input section in the group will be used as the stub table.
5414 FINDING_STUB_SECTION,
5415 // A group is being built and we have already found a stub table.
5416 // We enter this state to grow a stub group by adding input section
5417 // after the stub table. This effectively doubles the group size.
5418 HAS_STUB_SECTION
5419 } State;
5420
5421 // Any newly created relaxed sections are stored here.
5422 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
5423
5424 State state = NO_GROUP;
5425 section_size_type off = 0;
5426 section_size_type group_begin_offset = 0;
5427 section_size_type group_end_offset = 0;
5428 section_size_type stub_table_end_offset = 0;
5429 Input_section_list::const_iterator group_begin =
5430 this->input_sections().end();
5431 Input_section_list::const_iterator stub_table =
5432 this->input_sections().end();
5433 Input_section_list::const_iterator group_end = this->input_sections().end();
5434 for (Input_section_list::const_iterator p = this->input_sections().begin();
5435 p != this->input_sections().end();
5436 ++p)
5437 {
5438 section_size_type section_begin_offset =
5439 align_address(off, p->addralign());
5440 section_size_type section_end_offset =
5441 section_begin_offset + p->data_size();
5442
5443 // Check to see if we should group the previously seens sections.
5444 switch (state)
5445 {
5446 case NO_GROUP:
5447 break;
5448
5449 case FINDING_STUB_SECTION:
5450 // Adding this section makes the group larger than GROUP_SIZE.
5451 if (section_end_offset - group_begin_offset >= group_size)
5452 {
5453 if (stubs_always_after_branch)
5454 {
5455 gold_assert(group_end != this->input_sections().end());
5456 this->create_stub_group(group_begin, group_end, group_end,
5457 target, &new_relaxed_sections);
5458 state = NO_GROUP;
5459 }
5460 else
5461 {
5462 // But wait, there's more! Input sections up to
5463 // stub_group_size bytes after the stub table can be
5464 // handled by it too.
5465 state = HAS_STUB_SECTION;
5466 stub_table = group_end;
5467 stub_table_end_offset = group_end_offset;
5468 }
5469 }
5470 break;
5471
5472 case HAS_STUB_SECTION:
5473 // Adding this section makes the post stub-section group larger
5474 // than GROUP_SIZE.
5475 if (section_end_offset - stub_table_end_offset >= group_size)
5476 {
5477 gold_assert(group_end != this->input_sections().end());
5478 this->create_stub_group(group_begin, group_end, stub_table,
5479 target, &new_relaxed_sections);
5480 state = NO_GROUP;
5481 }
5482 break;
5483
5484 default:
5485 gold_unreachable();
5486 }
5487
5488 // If we see an input section and currently there is no group, start
5489 // a new one. Skip any empty sections.
5490 if ((p->is_input_section() || p->is_relaxed_input_section())
5491 && (p->relobj()->section_size(p->shndx()) != 0))
5492 {
5493 if (state == NO_GROUP)
5494 {
5495 state = FINDING_STUB_SECTION;
5496 group_begin = p;
5497 group_begin_offset = section_begin_offset;
5498 }
5499
5500 // Keep track of the last input section seen.
5501 group_end = p;
5502 group_end_offset = section_end_offset;
5503 }
5504
5505 off = section_end_offset;
5506 }
5507
5508 // Create a stub group for any ungrouped sections.
5509 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5510 {
5511 gold_assert(group_end != this->input_sections().end());
5512 this->create_stub_group(group_begin, group_end,
5513 (state == FINDING_STUB_SECTION
5514 ? group_end
5515 : stub_table),
5516 target, &new_relaxed_sections);
5517 }
5518
5519 // Convert input section into relaxed input section in a batch.
5520 if (!new_relaxed_sections.empty())
5521 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5522
5523 // Update the section offsets
5524 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5525 {
5526 Arm_relobj<big_endian>* arm_relobj =
5527 Arm_relobj<big_endian>::as_arm_relobj(
5528 new_relaxed_sections[i]->relobj());
5529 unsigned int shndx = new_relaxed_sections[i]->shndx();
5530 // Tell Arm_relobj that this input section is converted.
5531 arm_relobj->convert_input_section_to_relaxed_section(shndx);
5532 }
5533 }
5534
5535 // Append non empty text sections in this to LIST in ascending
5536 // order of their position in this.
5537
5538 template<bool big_endian>
5539 void
5540 Arm_output_section<big_endian>::append_text_sections_to_list(
5541 Text_section_list* list)
5542 {
5543 // We only care about text sections.
5544 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5545 return;
5546
5547 gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
5548
5549 for (Input_section_list::const_iterator p = this->input_sections().begin();
5550 p != this->input_sections().end();
5551 ++p)
5552 {
5553 // We only care about plain or relaxed input sections. We also
5554 // ignore any merged sections.
5555 if ((p->is_input_section() || p->is_relaxed_input_section())
5556 && p->data_size() != 0)
5557 list->push_back(Text_section_list::value_type(p->relobj(),
5558 p->shndx()));
5559 }
5560 }
5561
5562 template<bool big_endian>
5563 void
5564 Arm_output_section<big_endian>::fix_exidx_coverage(
5565 Layout* layout,
5566 const Text_section_list& sorted_text_sections,
5567 Symbol_table* symtab,
5568 bool merge_exidx_entries)
5569 {
5570 // We should only do this for the EXIDX output section.
5571 gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5572
5573 // We don't want the relaxation loop to undo these changes, so we discard
5574 // the current saved states and take another one after the fix-up.
5575 this->discard_states();
5576
5577 // Remove all input sections.
5578 uint64_t address = this->address();
5579 typedef std::list<Simple_input_section> Simple_input_section_list;
5580 Simple_input_section_list input_sections;
5581 this->reset_address_and_file_offset();
5582 this->get_input_sections(address, std::string(""), &input_sections);
5583
5584 if (!this->input_sections().empty())
5585 gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5586
5587 // Go through all the known input sections and record them.
5588 typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5589 Section_id_set known_input_sections;
5590 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5591 p != input_sections.end();
5592 ++p)
5593 {
5594 // This should never happen. At this point, we should only see
5595 // plain EXIDX input sections.
5596 gold_assert(!p->is_relaxed_input_section());
5597 known_input_sections.insert(Section_id(p->relobj(), p->shndx()));
5598 }
5599
5600 Arm_exidx_fixup exidx_fixup(this, merge_exidx_entries);
5601
5602 // Go over the sorted text sections.
5603 Section_id_set processed_input_sections;
5604 for (Text_section_list::const_iterator p = sorted_text_sections.begin();
5605 p != sorted_text_sections.end();
5606 ++p)
5607 {
5608 Relobj* relobj = p->first;
5609 unsigned int shndx = p->second;
5610
5611 Arm_relobj<big_endian>* arm_relobj =
5612 Arm_relobj<big_endian>::as_arm_relobj(relobj);
5613 const Arm_exidx_input_section* exidx_input_section =
5614 arm_relobj->exidx_input_section_by_link(shndx);
5615
5616 // If this text section has no EXIDX section, force an EXIDX_CANTUNWIND
5617 // entry pointing to the end of the last seen EXIDX section.
5618 if (exidx_input_section == NULL)
5619 {
5620 exidx_fixup.add_exidx_cantunwind_as_needed();
5621 continue;
5622 }
5623
5624 Relobj* exidx_relobj = exidx_input_section->relobj();
5625 unsigned int exidx_shndx = exidx_input_section->shndx();
5626 Section_id sid(exidx_relobj, exidx_shndx);
5627 if (known_input_sections.find(sid) == known_input_sections.end())
5628 {
5629 // This is odd. We have not seen this EXIDX input section before.
5630 // We cannot do fix-up. If we saw a SECTIONS clause in a script,
5631 // issue a warning instead. We assume the user knows what he
5632 // or she is doing. Otherwise, this is an error.
5633 if (layout->script_options()->saw_sections_clause())
5634 gold_warning(_("unwinding may not work because EXIDX input section"
5635 " %u of %s is not in EXIDX output section"),
5636 exidx_shndx, exidx_relobj->name().c_str());
5637 else
5638 gold_error(_("unwinding may not work because EXIDX input section"
5639 " %u of %s is not in EXIDX output section"),
5640 exidx_shndx, exidx_relobj->name().c_str());
5641
5642 exidx_fixup.add_exidx_cantunwind_as_needed();
5643 continue;
5644 }
5645
5646 // Fix up coverage and append input section to output data list.
5647 Arm_exidx_section_offset_map* section_offset_map = NULL;
5648 uint32_t deleted_bytes =
5649 exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
5650 &section_offset_map);
5651
5652 if (deleted_bytes == exidx_input_section->size())
5653 {
5654 // The whole EXIDX section got merged. Remove it from output.
5655 gold_assert(section_offset_map == NULL);
5656 exidx_relobj->set_output_section(exidx_shndx, NULL);
5657
5658 // All local symbols defined in this input section will be dropped.
5659 // We need to adjust output local symbol count.
5660 arm_relobj->set_output_local_symbol_count_needs_update();
5661 }
5662 else if (deleted_bytes > 0)
5663 {
5664 // Some entries are merged. We need to convert this EXIDX input
5665 // section into a relaxed section.
5666 gold_assert(section_offset_map != NULL);
5667 Arm_exidx_merged_section* merged_section =
5668 new Arm_exidx_merged_section(*exidx_input_section,
5669 *section_offset_map, deleted_bytes);
5670 this->add_relaxed_input_section(merged_section);
5671 arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
5672
5673 // All local symbols defined in discarded portions of this input
5674 // section will be dropped. We need to adjust output local symbol
5675 // count.
5676 arm_relobj->set_output_local_symbol_count_needs_update();
5677 }
5678 else
5679 {
5680 // Just add back the EXIDX input section.
5681 gold_assert(section_offset_map == NULL);
5682 Output_section::Simple_input_section sis(exidx_relobj, exidx_shndx);
5683 this->add_simple_input_section(sis, exidx_input_section->size(),
5684 exidx_input_section->addralign());
5685 }
5686
5687 processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx));
5688 }
5689
5690 // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5691 exidx_fixup.add_exidx_cantunwind_as_needed();
5692
5693 // Remove any known EXIDX input sections that are not processed.
5694 for (Simple_input_section_list::const_iterator p = input_sections.begin();
5695 p != input_sections.end();
5696 ++p)
5697 {
5698 if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
5699 == processed_input_sections.end())
5700 {
5701 // We only discard a known EXIDX section because its linked
5702 // text section has been folded by ICF.
5703 Arm_relobj<big_endian>* arm_relobj =
5704 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5705 const Arm_exidx_input_section* exidx_input_section =
5706 arm_relobj->exidx_input_section_by_shndx(p->shndx());
5707 gold_assert(exidx_input_section != NULL);
5708 unsigned int text_shndx = exidx_input_section->link();
5709 gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
5710
5711 // Remove this from link. We also need to recount the
5712 // local symbols.
5713 p->relobj()->set_output_section(p->shndx(), NULL);
5714 arm_relobj->set_output_local_symbol_count_needs_update();
5715 }
5716 }
5717
5718 // Link exidx output section to the first seen output section and
5719 // set correct entry size.
5720 this->set_link_section(exidx_fixup.first_output_text_section());
5721 this->set_entsize(8);
5722
5723 // Make changes permanent.
5724 this->save_states();
5725 this->set_section_offsets_need_adjustment();
5726 }
5727
5728 // Arm_relobj methods.
5729
5730 // Determine if an input section is scannable for stub processing. SHDR is
5731 // the header of the section and SHNDX is the section index. OS is the output
5732 // section for the input section and SYMTAB is the global symbol table used to
5733 // look up ICF information.
5734
5735 template<bool big_endian>
5736 bool
5737 Arm_relobj<big_endian>::section_is_scannable(
5738 const elfcpp::Shdr<32, big_endian>& shdr,
5739 unsigned int shndx,
5740 const Output_section* os,
5741 const Symbol_table *symtab)
5742 {
5743 // Skip any empty sections, unallocated sections or sections whose
5744 // type are not SHT_PROGBITS.
5745 if (shdr.get_sh_size() == 0
5746 || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
5747 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
5748 return false;
5749
5750 // Skip any discarded or ICF'ed sections.
5751 if (os == NULL || symtab->is_section_folded(this, shndx))
5752 return false;
5753
5754 // If this requires special offset handling, check to see if it is
5755 // a relaxed section. If this is not, then it is a merged section that
5756 // we cannot handle.
5757 if (this->is_output_section_offset_invalid(shndx))
5758 {
5759 const Output_relaxed_input_section* poris =
5760 os->find_relaxed_input_section(this, shndx);
5761 if (poris == NULL)
5762 return false;
5763 }
5764
5765 return true;
5766 }
5767
5768 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5769 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5770
5771 template<bool big_endian>
5772 bool
5773 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
5774 const elfcpp::Shdr<32, big_endian>& shdr,
5775 const Relobj::Output_sections& out_sections,
5776 const Symbol_table *symtab,
5777 const unsigned char* pshdrs)
5778 {
5779 unsigned int sh_type = shdr.get_sh_type();
5780 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
5781 return false;
5782
5783 // Ignore empty section.
5784 off_t sh_size = shdr.get_sh_size();
5785 if (sh_size == 0)
5786 return false;
5787
5788 // Ignore reloc section with unexpected symbol table. The
5789 // error will be reported in the final link.
5790 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
5791 return false;
5792
5793 unsigned int reloc_size;
5794 if (sh_type == elfcpp::SHT_REL)
5795 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5796 else
5797 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5798
5799 // Ignore reloc section with unexpected entsize or uneven size.
5800 // The error will be reported in the final link.
5801 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
5802 return false;
5803
5804 // Ignore reloc section with bad info. This error will be
5805 // reported in the final link.
5806 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5807 if (index >= this->shnum())
5808 return false;
5809
5810 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5811 const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
5812 return this->section_is_scannable(text_shdr, index,
5813 out_sections[index], symtab);
5814 }
5815
5816 // Return the output address of either a plain input section or a relaxed
5817 // input section. SHNDX is the section index. We define and use this
5818 // instead of calling Output_section::output_address because that is slow
5819 // for large output.
5820
5821 template<bool big_endian>
5822 Arm_address
5823 Arm_relobj<big_endian>::simple_input_section_output_address(
5824 unsigned int shndx,
5825 Output_section* os)
5826 {
5827 if (this->is_output_section_offset_invalid(shndx))
5828 {
5829 const Output_relaxed_input_section* poris =
5830 os->find_relaxed_input_section(this, shndx);
5831 // We do not handle merged sections here.
5832 gold_assert(poris != NULL);
5833 return poris->address();
5834 }
5835 else
5836 return os->address() + this->get_output_section_offset(shndx);
5837 }
5838
5839 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
5840 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5841
5842 template<bool big_endian>
5843 bool
5844 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
5845 const elfcpp::Shdr<32, big_endian>& shdr,
5846 unsigned int shndx,
5847 Output_section* os,
5848 const Symbol_table* symtab)
5849 {
5850 if (!this->section_is_scannable(shdr, shndx, os, symtab))
5851 return false;
5852
5853 // If the section does not cross any 4K-boundaries, it does not need to
5854 // be scanned.
5855 Arm_address address = this->simple_input_section_output_address(shndx, os);
5856 if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
5857 return false;
5858
5859 return true;
5860 }
5861
5862 // Scan a section for Cortex-A8 workaround.
5863
5864 template<bool big_endian>
5865 void
5866 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
5867 const elfcpp::Shdr<32, big_endian>& shdr,
5868 unsigned int shndx,
5869 Output_section* os,
5870 Target_arm<big_endian>* arm_target)
5871 {
5872 // Look for the first mapping symbol in this section. It should be
5873 // at (shndx, 0).
5874 Mapping_symbol_position section_start(shndx, 0);
5875 typename Mapping_symbols_info::const_iterator p =
5876 this->mapping_symbols_info_.lower_bound(section_start);
5877
5878 // There are no mapping symbols for this section. Treat it as a data-only
5879 // section. Issue a warning if section is marked as containing
5880 // instructions.
5881 if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
5882 {
5883 if ((this->section_flags(shndx) & elfcpp::SHF_EXECINSTR) != 0)
5884 gold_warning(_("cannot scan executable section %u of %s for Cortex-A8 "
5885 "erratum because it has no mapping symbols."),
5886 shndx, this->name().c_str());
5887 return;
5888 }
5889
5890 Arm_address output_address =
5891 this->simple_input_section_output_address(shndx, os);
5892
5893 // Get the section contents.
5894 section_size_type input_view_size = 0;
5895 const unsigned char* input_view =
5896 this->section_contents(shndx, &input_view_size, false);
5897
5898 // We need to go through the mapping symbols to determine what to
5899 // scan. There are two reasons. First, we should look at THUMB code and
5900 // THUMB code only. Second, we only want to look at the 4K-page boundary
5901 // to speed up the scanning.
5902
5903 while (p != this->mapping_symbols_info_.end()
5904 && p->first.first == shndx)
5905 {
5906 typename Mapping_symbols_info::const_iterator next =
5907 this->mapping_symbols_info_.upper_bound(p->first);
5908
5909 // Only scan part of a section with THUMB code.
5910 if (p->second == 't')
5911 {
5912 // Determine the end of this range.
5913 section_size_type span_start =
5914 convert_to_section_size_type(p->first.second);
5915 section_size_type span_end;
5916 if (next != this->mapping_symbols_info_.end()
5917 && next->first.first == shndx)
5918 span_end = convert_to_section_size_type(next->first.second);
5919 else
5920 span_end = convert_to_section_size_type(shdr.get_sh_size());
5921
5922 if (((span_start + output_address) & ~0xfffUL)
5923 != ((span_end + output_address - 1) & ~0xfffUL))
5924 {
5925 arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
5926 span_start, span_end,
5927 input_view,
5928 output_address);
5929 }
5930 }
5931
5932 p = next;
5933 }
5934 }
5935
5936 // Scan relocations for stub generation.
5937
5938 template<bool big_endian>
5939 void
5940 Arm_relobj<big_endian>::scan_sections_for_stubs(
5941 Target_arm<big_endian>* arm_target,
5942 const Symbol_table* symtab,
5943 const Layout* layout)
5944 {
5945 unsigned int shnum = this->shnum();
5946 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5947
5948 // Read the section headers.
5949 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
5950 shnum * shdr_size,
5951 true, true);
5952
5953 // To speed up processing, we set up hash tables for fast lookup of
5954 // input offsets to output addresses.
5955 this->initialize_input_to_output_maps();
5956
5957 const Relobj::Output_sections& out_sections(this->output_sections());
5958
5959 Relocate_info<32, big_endian> relinfo;
5960 relinfo.symtab = symtab;
5961 relinfo.layout = layout;
5962 relinfo.object = this;
5963
5964 // Do relocation stubs scanning.
5965 const unsigned char* p = pshdrs + shdr_size;
5966 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
5967 {
5968 const elfcpp::Shdr<32, big_endian> shdr(p);
5969 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
5970 pshdrs))
5971 {
5972 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5973 Arm_address output_offset = this->get_output_section_offset(index);
5974 Arm_address output_address;
5975 if (output_offset != invalid_address)
5976 output_address = out_sections[index]->address() + output_offset;
5977 else
5978 {
5979 // Currently this only happens for a relaxed section.
5980 const Output_relaxed_input_section* poris =
5981 out_sections[index]->find_relaxed_input_section(this, index);
5982 gold_assert(poris != NULL);
5983 output_address = poris->address();
5984 }
5985
5986 // Get the relocations.
5987 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
5988 shdr.get_sh_size(),
5989 true, false);
5990
5991 // Get the section contents. This does work for the case in which
5992 // we modify the contents of an input section. We need to pass the
5993 // output view under such circumstances.
5994 section_size_type input_view_size = 0;
5995 const unsigned char* input_view =
5996 this->section_contents(index, &input_view_size, false);
5997
5998 relinfo.reloc_shndx = i;
5999 relinfo.data_shndx = index;
6000 unsigned int sh_type = shdr.get_sh_type();
6001 unsigned int reloc_size;
6002 if (sh_type == elfcpp::SHT_REL)
6003 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6004 else
6005 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6006
6007 Output_section* os = out_sections[index];
6008 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
6009 shdr.get_sh_size() / reloc_size,
6010 os,
6011 output_offset == invalid_address,
6012 input_view, output_address,
6013 input_view_size);
6014 }
6015 }
6016
6017 // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
6018 // after its relocation section, if there is one, is processed for
6019 // relocation stubs. Merging this loop with the one above would have been
6020 // complicated since we would have had to make sure that relocation stub
6021 // scanning is done first.
6022 if (arm_target->fix_cortex_a8())
6023 {
6024 const unsigned char* p = pshdrs + shdr_size;
6025 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
6026 {
6027 const elfcpp::Shdr<32, big_endian> shdr(p);
6028 if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
6029 out_sections[i],
6030 symtab))
6031 this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
6032 arm_target);
6033 }
6034 }
6035
6036 // After we've done the relocations, we release the hash tables,
6037 // since we no longer need them.
6038 this->free_input_to_output_maps();
6039 }
6040
6041 // Count the local symbols. The ARM backend needs to know if a symbol
6042 // is a THUMB function or not. For global symbols, it is easy because
6043 // the Symbol object keeps the ELF symbol type. For local symbol it is
6044 // harder because we cannot access this information. So we override the
6045 // do_count_local_symbol in parent and scan local symbols to mark
6046 // THUMB functions. This is not the most efficient way but I do not want to
6047 // slow down other ports by calling a per symbol targer hook inside
6048 // Sized_relobj<size, big_endian>::do_count_local_symbols.
6049
6050 template<bool big_endian>
6051 void
6052 Arm_relobj<big_endian>::do_count_local_symbols(
6053 Stringpool_template<char>* pool,
6054 Stringpool_template<char>* dynpool)
6055 {
6056 // We need to fix-up the values of any local symbols whose type are
6057 // STT_ARM_TFUNC.
6058
6059 // Ask parent to count the local symbols.
6060 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
6061 const unsigned int loccount = this->local_symbol_count();
6062 if (loccount == 0)
6063 return;
6064
6065 // Intialize the thumb function bit-vector.
6066 std::vector<bool> empty_vector(loccount, false);
6067 this->local_symbol_is_thumb_function_.swap(empty_vector);
6068
6069 // Read the symbol table section header.
6070 const unsigned int symtab_shndx = this->symtab_shndx();
6071 elfcpp::Shdr<32, big_endian>
6072 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6073 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6074
6075 // Read the local symbols.
6076 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6077 gold_assert(loccount == symtabshdr.get_sh_info());
6078 off_t locsize = loccount * sym_size;
6079 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6080 locsize, true, true);
6081
6082 // For mapping symbol processing, we need to read the symbol names.
6083 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
6084 if (strtab_shndx >= this->shnum())
6085 {
6086 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
6087 return;
6088 }
6089
6090 elfcpp::Shdr<32, big_endian>
6091 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
6092 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
6093 {
6094 this->error(_("symbol table name section has wrong type: %u"),
6095 static_cast<unsigned int>(strtabshdr.get_sh_type()));
6096 return;
6097 }
6098 const char* pnames =
6099 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
6100 strtabshdr.get_sh_size(),
6101 false, false));
6102
6103 // Loop over the local symbols and mark any local symbols pointing
6104 // to THUMB functions.
6105
6106 // Skip the first dummy symbol.
6107 psyms += sym_size;
6108 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
6109 this->local_values();
6110 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6111 {
6112 elfcpp::Sym<32, big_endian> sym(psyms);
6113 elfcpp::STT st_type = sym.get_st_type();
6114 Symbol_value<32>& lv((*plocal_values)[i]);
6115 Arm_address input_value = lv.input_value();
6116
6117 // Check to see if this is a mapping symbol.
6118 const char* sym_name = pnames + sym.get_st_name();
6119 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
6120 {
6121 bool is_ordinary;
6122 unsigned int input_shndx =
6123 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
6124 gold_assert(is_ordinary);
6125
6126 // Strip of LSB in case this is a THUMB symbol.
6127 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
6128 this->mapping_symbols_info_[msp] = sym_name[1];
6129 }
6130
6131 if (st_type == elfcpp::STT_ARM_TFUNC
6132 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
6133 {
6134 // This is a THUMB function. Mark this and canonicalize the
6135 // symbol value by setting LSB.
6136 this->local_symbol_is_thumb_function_[i] = true;
6137 if ((input_value & 1) == 0)
6138 lv.set_input_value(input_value | 1);
6139 }
6140 }
6141 }
6142
6143 // Relocate sections.
6144 template<bool big_endian>
6145 void
6146 Arm_relobj<big_endian>::do_relocate_sections(
6147 const Symbol_table* symtab,
6148 const Layout* layout,
6149 const unsigned char* pshdrs,
6150 typename Sized_relobj<32, big_endian>::Views* pviews)
6151 {
6152 // Call parent to relocate sections.
6153 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
6154 pviews);
6155
6156 // We do not generate stubs if doing a relocatable link.
6157 if (parameters->options().relocatable())
6158 return;
6159
6160 // Relocate stub tables.
6161 unsigned int shnum = this->shnum();
6162
6163 Target_arm<big_endian>* arm_target =
6164 Target_arm<big_endian>::default_target();
6165
6166 Relocate_info<32, big_endian> relinfo;
6167 relinfo.symtab = symtab;
6168 relinfo.layout = layout;
6169 relinfo.object = this;
6170
6171 for (unsigned int i = 1; i < shnum; ++i)
6172 {
6173 Arm_input_section<big_endian>* arm_input_section =
6174 arm_target->find_arm_input_section(this, i);
6175
6176 if (arm_input_section != NULL
6177 && arm_input_section->is_stub_table_owner()
6178 && !arm_input_section->stub_table()->empty())
6179 {
6180 // We cannot discard a section if it owns a stub table.
6181 Output_section* os = this->output_section(i);
6182 gold_assert(os != NULL);
6183
6184 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
6185 relinfo.reloc_shdr = NULL;
6186 relinfo.data_shndx = i;
6187 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
6188
6189 gold_assert((*pviews)[i].view != NULL);
6190
6191 // We are passed the output section view. Adjust it to cover the
6192 // stub table only.
6193 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
6194 gold_assert((stub_table->address() >= (*pviews)[i].address)
6195 && ((stub_table->address() + stub_table->data_size())
6196 <= (*pviews)[i].address + (*pviews)[i].view_size));
6197
6198 off_t offset = stub_table->address() - (*pviews)[i].address;
6199 unsigned char* view = (*pviews)[i].view + offset;
6200 Arm_address address = stub_table->address();
6201 section_size_type view_size = stub_table->data_size();
6202
6203 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
6204 view_size);
6205 }
6206
6207 // Apply Cortex A8 workaround if applicable.
6208 if (this->section_has_cortex_a8_workaround(i))
6209 {
6210 unsigned char* view = (*pviews)[i].view;
6211 Arm_address view_address = (*pviews)[i].address;
6212 section_size_type view_size = (*pviews)[i].view_size;
6213 Stub_table<big_endian>* stub_table = this->stub_tables_[i];
6214
6215 // Adjust view to cover section.
6216 Output_section* os = this->output_section(i);
6217 gold_assert(os != NULL);
6218 Arm_address section_address =
6219 this->simple_input_section_output_address(i, os);
6220 uint64_t section_size = this->section_size(i);
6221
6222 gold_assert(section_address >= view_address
6223 && ((section_address + section_size)
6224 <= (view_address + view_size)));
6225
6226 unsigned char* section_view = view + (section_address - view_address);
6227
6228 // Apply the Cortex-A8 workaround to the output address range
6229 // corresponding to this input section.
6230 stub_table->apply_cortex_a8_workaround_to_address_range(
6231 arm_target,
6232 section_view,
6233 section_address,
6234 section_size);
6235 }
6236 }
6237 }
6238
6239 // Find the linked text section of an EXIDX section by looking the the first
6240 // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
6241 // must be linked to to its associated code section via the sh_link field of
6242 // its section header. However, some tools are broken and the link is not
6243 // always set. LD just drops such an EXIDX section silently, causing the
6244 // associated code not unwindabled. Here we try a little bit harder to
6245 // discover the linked code section.
6246 //
6247 // PSHDR points to the section header of a relocation section of an EXIDX
6248 // section. If we can find a linked text section, return true and
6249 // store the text section index in the location PSHNDX. Otherwise
6250 // return false.
6251
6252 template<bool big_endian>
6253 bool
6254 Arm_relobj<big_endian>::find_linked_text_section(
6255 const unsigned char* pshdr,
6256 const unsigned char* psyms,
6257 unsigned int* pshndx)
6258 {
6259 elfcpp::Shdr<32, big_endian> shdr(pshdr);
6260
6261 // If there is no relocation, we cannot find the linked text section.
6262 size_t reloc_size;
6263 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6264 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6265 else
6266 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6267 size_t reloc_count = shdr.get_sh_size() / reloc_size;
6268
6269 // Get the relocations.
6270 const unsigned char* prelocs =
6271 this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false);
6272
6273 // Find the REL31 relocation for the first word of the first EXIDX entry.
6274 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6275 {
6276 Arm_address r_offset;
6277 typename elfcpp::Elf_types<32>::Elf_WXword r_info;
6278 if (shdr.get_sh_type() == elfcpp::SHT_REL)
6279 {
6280 typename elfcpp::Rel<32, big_endian> reloc(prelocs);
6281 r_info = reloc.get_r_info();
6282 r_offset = reloc.get_r_offset();
6283 }
6284 else
6285 {
6286 typename elfcpp::Rela<32, big_endian> reloc(prelocs);
6287 r_info = reloc.get_r_info();
6288 r_offset = reloc.get_r_offset();
6289 }
6290
6291 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6292 if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
6293 continue;
6294
6295 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6296 if (r_sym == 0
6297 || r_sym >= this->local_symbol_count()
6298 || r_offset != 0)
6299 continue;
6300
6301 // This is the relocation for the first word of the first EXIDX entry.
6302 // We expect to see a local section symbol.
6303 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6304 elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
6305 if (sym.get_st_type() == elfcpp::STT_SECTION)
6306 {
6307 bool is_ordinary;
6308 *pshndx =
6309 this->adjust_sym_shndx(r_sym, sym.get_st_shndx(), &is_ordinary);
6310 gold_assert(is_ordinary);
6311 return true;
6312 }
6313 else
6314 return false;
6315 }
6316
6317 return false;
6318 }
6319
6320 // Make an EXIDX input section object for an EXIDX section whose index is
6321 // SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
6322 // is the section index of the linked text section.
6323
6324 template<bool big_endian>
6325 void
6326 Arm_relobj<big_endian>::make_exidx_input_section(
6327 unsigned int shndx,
6328 const elfcpp::Shdr<32, big_endian>& shdr,
6329 unsigned int text_shndx)
6330 {
6331 // Issue an error and ignore this EXIDX section if it points to a text
6332 // section already has an EXIDX section.
6333 if (this->exidx_section_map_[text_shndx] != NULL)
6334 {
6335 gold_error(_("EXIDX sections %u and %u both link to text section %u "
6336 "in %s"),
6337 shndx, this->exidx_section_map_[text_shndx]->shndx(),
6338 text_shndx, this->name().c_str());
6339 return;
6340 }
6341
6342 // Create an Arm_exidx_input_section object for this EXIDX section.
6343 Arm_exidx_input_section* exidx_input_section =
6344 new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
6345 shdr.get_sh_addralign());
6346 this->exidx_section_map_[text_shndx] = exidx_input_section;
6347
6348 // Also map the EXIDX section index to this.
6349 gold_assert(this->exidx_section_map_[shndx] == NULL);
6350 this->exidx_section_map_[shndx] = exidx_input_section;
6351 }
6352
6353 // Read the symbol information.
6354
6355 template<bool big_endian>
6356 void
6357 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6358 {
6359 // Call parent class to read symbol information.
6360 Sized_relobj<32, big_endian>::do_read_symbols(sd);
6361
6362 // If this input file is a binary file, it has no processor
6363 // specific flags and attributes section.
6364 Input_file::Format format = this->input_file()->format();
6365 if (format != Input_file::FORMAT_ELF)
6366 {
6367 gold_assert(format == Input_file::FORMAT_BINARY);
6368 this->merge_flags_and_attributes_ = false;
6369 return;
6370 }
6371
6372 // Read processor-specific flags in ELF file header.
6373 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6374 elfcpp::Elf_sizes<32>::ehdr_size,
6375 true, false);
6376 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6377 this->processor_specific_flags_ = ehdr.get_e_flags();
6378
6379 // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6380 // sections.
6381 std::vector<unsigned int> deferred_exidx_sections;
6382 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6383 const unsigned char* pshdrs = sd->section_headers->data();
6384 const unsigned char *ps = pshdrs + shdr_size;
6385 bool must_merge_flags_and_attributes = false;
6386 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6387 {
6388 elfcpp::Shdr<32, big_endian> shdr(ps);
6389
6390 // Sometimes an object has no contents except the section name string
6391 // table and an empty symbol table with the undefined symbol. We
6392 // don't want to merge processor-specific flags from such an object.
6393 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6394 {
6395 // Symbol table is not empty.
6396 const elfcpp::Elf_types<32>::Elf_WXword sym_size =
6397 elfcpp::Elf_sizes<32>::sym_size;
6398 if (shdr.get_sh_size() > sym_size)
6399 must_merge_flags_and_attributes = true;
6400 }
6401 else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6402 // If this is neither an empty symbol table nor a string table,
6403 // be conservative.
6404 must_merge_flags_and_attributes = true;
6405
6406 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6407 {
6408 gold_assert(this->attributes_section_data_ == NULL);
6409 section_offset_type section_offset = shdr.get_sh_offset();
6410 section_size_type section_size =
6411 convert_to_section_size_type(shdr.get_sh_size());
6412 File_view* view = this->get_lasting_view(section_offset,
6413 section_size, true, false);
6414 this->attributes_section_data_ =
6415 new Attributes_section_data(view->data(), section_size);
6416 }
6417 else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6418 {
6419 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6420 if (text_shndx >= this->shnum())
6421 gold_error(_("EXIDX section %u linked to invalid section %u"),
6422 i, text_shndx);
6423 else if (text_shndx == elfcpp::SHN_UNDEF)
6424 deferred_exidx_sections.push_back(i);
6425 else
6426 this->make_exidx_input_section(i, shdr, text_shndx);
6427 }
6428 }
6429
6430 // This is rare.
6431 if (!must_merge_flags_and_attributes)
6432 {
6433 this->merge_flags_and_attributes_ = false;
6434 return;
6435 }
6436
6437 // Some tools are broken and they do not set the link of EXIDX sections.
6438 // We look at the first relocation to figure out the linked sections.
6439 if (!deferred_exidx_sections.empty())
6440 {
6441 // We need to go over the section headers again to find the mapping
6442 // from sections being relocated to their relocation sections. This is
6443 // a bit inefficient as we could do that in the loop above. However,
6444 // we do not expect any deferred EXIDX sections normally. So we do not
6445 // want to slow down the most common path.
6446 typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
6447 Reloc_map reloc_map;
6448 ps = pshdrs + shdr_size;
6449 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6450 {
6451 elfcpp::Shdr<32, big_endian> shdr(ps);
6452 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
6453 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
6454 {
6455 unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
6456 if (info_shndx >= this->shnum())
6457 gold_error(_("relocation section %u has invalid info %u"),
6458 i, info_shndx);
6459 Reloc_map::value_type value(info_shndx, i);
6460 std::pair<Reloc_map::iterator, bool> result =
6461 reloc_map.insert(value);
6462 if (!result.second)
6463 gold_error(_("section %u has multiple relocation sections "
6464 "%u and %u"),
6465 info_shndx, i, reloc_map[info_shndx]);
6466 }
6467 }
6468
6469 // Read the symbol table section header.
6470 const unsigned int symtab_shndx = this->symtab_shndx();
6471 elfcpp::Shdr<32, big_endian>
6472 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6473 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6474
6475 // Read the local symbols.
6476 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6477 const unsigned int loccount = this->local_symbol_count();
6478 gold_assert(loccount == symtabshdr.get_sh_info());
6479 off_t locsize = loccount * sym_size;
6480 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6481 locsize, true, true);
6482
6483 // Process the deferred EXIDX sections.
6484 for(unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
6485 {
6486 unsigned int shndx = deferred_exidx_sections[i];
6487 elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
6488 unsigned int text_shndx;
6489 Reloc_map::const_iterator it = reloc_map.find(shndx);
6490 if (it != reloc_map.end()
6491 && find_linked_text_section(pshdrs + it->second * shdr_size,
6492 psyms, &text_shndx))
6493 this->make_exidx_input_section(shndx, shdr, text_shndx);
6494 else
6495 gold_error(_("EXIDX section %u has no linked text section."),
6496 shndx);
6497 }
6498 }
6499 }
6500
6501 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
6502 // sections for unwinding. These sections are referenced implicitly by
6503 // text sections linked in the section headers. If we ignore these implict
6504 // references, the .ARM.exidx sections and any .ARM.extab sections they use
6505 // will be garbage-collected incorrectly. Hence we override the same function
6506 // in the base class to handle these implicit references.
6507
6508 template<bool big_endian>
6509 void
6510 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
6511 Layout* layout,
6512 Read_relocs_data* rd)
6513 {
6514 // First, call base class method to process relocations in this object.
6515 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
6516
6517 // If --gc-sections is not specified, there is nothing more to do.
6518 // This happens when --icf is used but --gc-sections is not.
6519 if (!parameters->options().gc_sections())
6520 return;
6521
6522 unsigned int shnum = this->shnum();
6523 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6524 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6525 shnum * shdr_size,
6526 true, true);
6527
6528 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
6529 // to these from the linked text sections.
6530 const unsigned char* ps = pshdrs + shdr_size;
6531 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
6532 {
6533 elfcpp::Shdr<32, big_endian> shdr(ps);
6534 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6535 {
6536 // Found an .ARM.exidx section, add it to the set of reachable
6537 // sections from its linked text section.
6538 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6539 symtab->gc()->add_reference(this, text_shndx, this, i);
6540 }
6541 }
6542 }
6543
6544 // Update output local symbol count. Owing to EXIDX entry merging, some local
6545 // symbols will be removed in output. Adjust output local symbol count
6546 // accordingly. We can only changed the static output local symbol count. It
6547 // is too late to change the dynamic symbols.
6548
6549 template<bool big_endian>
6550 void
6551 Arm_relobj<big_endian>::update_output_local_symbol_count()
6552 {
6553 // Caller should check that this needs updating. We want caller checking
6554 // because output_local_symbol_count_needs_update() is most likely inlined.
6555 gold_assert(this->output_local_symbol_count_needs_update_);
6556
6557 gold_assert(this->symtab_shndx() != -1U);
6558 if (this->symtab_shndx() == 0)
6559 {
6560 // This object has no symbols. Weird but legal.
6561 return;
6562 }
6563
6564 // Read the symbol table section header.
6565 const unsigned int symtab_shndx = this->symtab_shndx();
6566 elfcpp::Shdr<32, big_endian>
6567 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6568 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6569
6570 // Read the local symbols.
6571 const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6572 const unsigned int loccount = this->local_symbol_count();
6573 gold_assert(loccount == symtabshdr.get_sh_info());
6574 off_t locsize = loccount * sym_size;
6575 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6576 locsize, true, true);
6577
6578 // Loop over the local symbols.
6579
6580 typedef typename Sized_relobj<32, big_endian>::Output_sections
6581 Output_sections;
6582 const Output_sections& out_sections(this->output_sections());
6583 unsigned int shnum = this->shnum();
6584 unsigned int count = 0;
6585 // Skip the first, dummy, symbol.
6586 psyms += sym_size;
6587 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6588 {
6589 elfcpp::Sym<32, big_endian> sym(psyms);
6590
6591 Symbol_value<32>& lv((*this->local_values())[i]);
6592
6593 // This local symbol was already discarded by do_count_local_symbols.
6594 if (lv.is_output_symtab_index_set() && !lv.has_output_symtab_entry())
6595 continue;
6596
6597 bool is_ordinary;
6598 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
6599 &is_ordinary);
6600
6601 if (shndx < shnum)
6602 {
6603 Output_section* os = out_sections[shndx];
6604
6605 // This local symbol no longer has an output section. Discard it.
6606 if (os == NULL)
6607 {
6608 lv.set_no_output_symtab_entry();
6609 continue;
6610 }
6611
6612 // Currently we only discard parts of EXIDX input sections.
6613 // We explicitly check for a merged EXIDX input section to avoid
6614 // calling Output_section_data::output_offset unless necessary.
6615 if ((this->get_output_section_offset(shndx) == invalid_address)
6616 && (this->exidx_input_section_by_shndx(shndx) != NULL))
6617 {
6618 section_offset_type output_offset =
6619 os->output_offset(this, shndx, lv.input_value());
6620 if (output_offset == -1)
6621 {
6622 // This symbol is defined in a part of an EXIDX input section
6623 // that is discarded due to entry merging.
6624 lv.set_no_output_symtab_entry();
6625 continue;
6626 }
6627 }
6628 }
6629
6630 ++count;
6631 }
6632
6633 this->set_output_local_symbol_count(count);
6634 this->output_local_symbol_count_needs_update_ = false;
6635 }
6636
6637 // Arm_dynobj methods.
6638
6639 // Read the symbol information.
6640
6641 template<bool big_endian>
6642 void
6643 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6644 {
6645 // Call parent class to read symbol information.
6646 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
6647
6648 // Read processor-specific flags in ELF file header.
6649 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6650 elfcpp::Elf_sizes<32>::ehdr_size,
6651 true, false);
6652 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6653 this->processor_specific_flags_ = ehdr.get_e_flags();
6654
6655 // Read the attributes section if there is one.
6656 // We read from the end because gas seems to put it near the end of
6657 // the section headers.
6658 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6659 const unsigned char *ps =
6660 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
6661 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
6662 {
6663 elfcpp::Shdr<32, big_endian> shdr(ps);
6664 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6665 {
6666 section_offset_type section_offset = shdr.get_sh_offset();
6667 section_size_type section_size =
6668 convert_to_section_size_type(shdr.get_sh_size());
6669 File_view* view = this->get_lasting_view(section_offset,
6670 section_size, true, false);
6671 this->attributes_section_data_ =
6672 new Attributes_section_data(view->data(), section_size);
6673 break;
6674 }
6675 }
6676 }
6677
6678 // Stub_addend_reader methods.
6679
6680 // Read the addend of a REL relocation of type R_TYPE at VIEW.
6681
6682 template<bool big_endian>
6683 elfcpp::Elf_types<32>::Elf_Swxword
6684 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
6685 unsigned int r_type,
6686 const unsigned char* view,
6687 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
6688 {
6689 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
6690
6691 switch (r_type)
6692 {
6693 case elfcpp::R_ARM_CALL:
6694 case elfcpp::R_ARM_JUMP24:
6695 case elfcpp::R_ARM_PLT32:
6696 {
6697 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6698 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6699 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
6700 return utils::sign_extend<26>(val << 2);
6701 }
6702
6703 case elfcpp::R_ARM_THM_CALL:
6704 case elfcpp::R_ARM_THM_JUMP24:
6705 case elfcpp::R_ARM_THM_XPC22:
6706 {
6707 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6708 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6709 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6710 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6711 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
6712 }
6713
6714 case elfcpp::R_ARM_THM_JUMP19:
6715 {
6716 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6717 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6718 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6719 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6720 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
6721 }
6722
6723 default:
6724 gold_unreachable();
6725 }
6726 }
6727
6728 // Arm_output_data_got methods.
6729
6730 // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
6731 // The first one is initialized to be 1, which is the module index for
6732 // the main executable and the second one 0. A reloc of the type
6733 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
6734 // be applied by gold. GSYM is a global symbol.
6735 //
6736 template<bool big_endian>
6737 void
6738 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
6739 unsigned int got_type,
6740 Symbol* gsym)
6741 {
6742 if (gsym->has_got_offset(got_type))
6743 return;
6744
6745 // We are doing a static link. Just mark it as belong to module 1,
6746 // the executable.
6747 unsigned int got_offset = this->add_constant(1);
6748 gsym->set_got_offset(got_type, got_offset);
6749 got_offset = this->add_constant(0);
6750 this->static_relocs_.push_back(Static_reloc(got_offset,
6751 elfcpp::R_ARM_TLS_DTPOFF32,
6752 gsym));
6753 }
6754
6755 // Same as the above but for a local symbol.
6756
6757 template<bool big_endian>
6758 void
6759 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
6760 unsigned int got_type,
6761 Sized_relobj<32, big_endian>* object,
6762 unsigned int index)
6763 {
6764 if (object->local_has_got_offset(index, got_type))
6765 return;
6766
6767 // We are doing a static link. Just mark it as belong to module 1,
6768 // the executable.
6769 unsigned int got_offset = this->add_constant(1);
6770 object->set_local_got_offset(index, got_type, got_offset);
6771 got_offset = this->add_constant(0);
6772 this->static_relocs_.push_back(Static_reloc(got_offset,
6773 elfcpp::R_ARM_TLS_DTPOFF32,
6774 object, index));
6775 }
6776
6777 template<bool big_endian>
6778 void
6779 Arm_output_data_got<big_endian>::do_write(Output_file* of)
6780 {
6781 // Call parent to write out GOT.
6782 Output_data_got<32, big_endian>::do_write(of);
6783
6784 // We are done if there is no fix up.
6785 if (this->static_relocs_.empty())
6786 return;
6787
6788 gold_assert(parameters->doing_static_link());
6789
6790 const off_t offset = this->offset();
6791 const section_size_type oview_size =
6792 convert_to_section_size_type(this->data_size());
6793 unsigned char* const oview = of->get_output_view(offset, oview_size);
6794
6795 Output_segment* tls_segment = this->layout_->tls_segment();
6796 gold_assert(tls_segment != NULL);
6797
6798 // The thread pointer $tp points to the TCB, which is followed by the
6799 // TLS. So we need to adjust $tp relative addressing by this amount.
6800 Arm_address aligned_tcb_size =
6801 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
6802
6803 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6804 {
6805 Static_reloc& reloc(this->static_relocs_[i]);
6806
6807 Arm_address value;
6808 if (!reloc.symbol_is_global())
6809 {
6810 Sized_relobj<32, big_endian>* object = reloc.relobj();
6811 const Symbol_value<32>* psymval =
6812 reloc.relobj()->local_symbol(reloc.index());
6813
6814 // We are doing static linking. Issue an error and skip this
6815 // relocation if the symbol is undefined or in a discarded_section.
6816 bool is_ordinary;
6817 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6818 if ((shndx == elfcpp::SHN_UNDEF)
6819 || (is_ordinary
6820 && shndx != elfcpp::SHN_UNDEF
6821 && !object->is_section_included(shndx)
6822 && !this->symbol_table_->is_section_folded(object, shndx)))
6823 {
6824 gold_error(_("undefined or discarded local symbol %u from "
6825 " object %s in GOT"),
6826 reloc.index(), reloc.relobj()->name().c_str());
6827 continue;
6828 }
6829
6830 value = psymval->value(object, 0);
6831 }
6832 else
6833 {
6834 const Symbol* gsym = reloc.symbol();
6835 gold_assert(gsym != NULL);
6836 if (gsym->is_forwarder())
6837 gsym = this->symbol_table_->resolve_forwards(gsym);
6838
6839 // We are doing static linking. Issue an error and skip this
6840 // relocation if the symbol is undefined or in a discarded_section
6841 // unless it is a weakly_undefined symbol.
6842 if ((gsym->is_defined_in_discarded_section()
6843 || gsym->is_undefined())
6844 && !gsym->is_weak_undefined())
6845 {
6846 gold_error(_("undefined or discarded symbol %s in GOT"),
6847 gsym->name());
6848 continue;
6849 }
6850
6851 if (!gsym->is_weak_undefined())
6852 {
6853 const Sized_symbol<32>* sym =
6854 static_cast<const Sized_symbol<32>*>(gsym);
6855 value = sym->value();
6856 }
6857 else
6858 value = 0;
6859 }
6860
6861 unsigned got_offset = reloc.got_offset();
6862 gold_assert(got_offset < oview_size);
6863
6864 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6865 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6866 Valtype x;
6867 switch (reloc.r_type())
6868 {
6869 case elfcpp::R_ARM_TLS_DTPOFF32:
6870 x = value;
6871 break;
6872 case elfcpp::R_ARM_TLS_TPOFF32:
6873 x = value + aligned_tcb_size;
6874 break;
6875 default:
6876 gold_unreachable();
6877 }
6878 elfcpp::Swap<32, big_endian>::writeval(wv, x);
6879 }
6880
6881 of->write_output_view(offset, oview_size, oview);
6882 }
6883
6884 // A class to handle the PLT data.
6885
6886 template<bool big_endian>
6887 class Output_data_plt_arm : public Output_section_data
6888 {
6889 public:
6890 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
6891 Reloc_section;
6892
6893 Output_data_plt_arm(Layout*, Output_data_space*);
6894
6895 // Add an entry to the PLT.
6896 void
6897 add_entry(Symbol* gsym);
6898
6899 // Return the .rel.plt section data.
6900 const Reloc_section*
6901 rel_plt() const
6902 { return this->rel_; }
6903
6904 protected:
6905 void
6906 do_adjust_output_section(Output_section* os);
6907
6908 // Write to a map file.
6909 void
6910 do_print_to_mapfile(Mapfile* mapfile) const
6911 { mapfile->print_output_data(this, _("** PLT")); }
6912
6913 private:
6914 // Template for the first PLT entry.
6915 static const uint32_t first_plt_entry[5];
6916
6917 // Template for subsequent PLT entries.
6918 static const uint32_t plt_entry[3];
6919
6920 // Set the final size.
6921 void
6922 set_final_data_size()
6923 {
6924 this->set_data_size(sizeof(first_plt_entry)
6925 + this->count_ * sizeof(plt_entry));
6926 }
6927
6928 // Write out the PLT data.
6929 void
6930 do_write(Output_file*);
6931
6932 // The reloc section.
6933 Reloc_section* rel_;
6934 // The .got.plt section.
6935 Output_data_space* got_plt_;
6936 // The number of PLT entries.
6937 unsigned int count_;
6938 };
6939
6940 // Create the PLT section. The ordinary .got section is an argument,
6941 // since we need to refer to the start. We also create our own .got
6942 // section just for PLT entries.
6943
6944 template<bool big_endian>
6945 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
6946 Output_data_space* got_plt)
6947 : Output_section_data(4), got_plt_(got_plt), count_(0)
6948 {
6949 this->rel_ = new Reloc_section(false);
6950 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
6951 elfcpp::SHF_ALLOC, this->rel_, true, false,
6952 false, false);
6953 }
6954
6955 template<bool big_endian>
6956 void
6957 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
6958 {
6959 os->set_entsize(0);
6960 }
6961
6962 // Add an entry to the PLT.
6963
6964 template<bool big_endian>
6965 void
6966 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
6967 {
6968 gold_assert(!gsym->has_plt_offset());
6969
6970 // Note that when setting the PLT offset we skip the initial
6971 // reserved PLT entry.
6972 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
6973 + sizeof(first_plt_entry));
6974
6975 ++this->count_;
6976
6977 section_offset_type got_offset = this->got_plt_->current_data_size();
6978
6979 // Every PLT entry needs a GOT entry which points back to the PLT
6980 // entry (this will be changed by the dynamic linker, normally
6981 // lazily when the function is called).
6982 this->got_plt_->set_current_data_size(got_offset + 4);
6983
6984 // Every PLT entry needs a reloc.
6985 gsym->set_needs_dynsym_entry();
6986 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
6987 got_offset);
6988
6989 // Note that we don't need to save the symbol. The contents of the
6990 // PLT are independent of which symbols are used. The symbols only
6991 // appear in the relocations.
6992 }
6993
6994 // ARM PLTs.
6995 // FIXME: This is not very flexible. Right now this has only been tested
6996 // on armv5te. If we are to support additional architecture features like
6997 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
6998
6999 // The first entry in the PLT.
7000 template<bool big_endian>
7001 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
7002 {
7003 0xe52de004, // str lr, [sp, #-4]!
7004 0xe59fe004, // ldr lr, [pc, #4]
7005 0xe08fe00e, // add lr, pc, lr
7006 0xe5bef008, // ldr pc, [lr, #8]!
7007 0x00000000, // &GOT[0] - .
7008 };
7009
7010 // Subsequent entries in the PLT.
7011
7012 template<bool big_endian>
7013 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
7014 {
7015 0xe28fc600, // add ip, pc, #0xNN00000
7016 0xe28cca00, // add ip, ip, #0xNN000
7017 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
7018 };
7019
7020 // Write out the PLT. This uses the hand-coded instructions above,
7021 // and adjusts them as needed. This is all specified by the arm ELF
7022 // Processor Supplement.
7023
7024 template<bool big_endian>
7025 void
7026 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
7027 {
7028 const off_t offset = this->offset();
7029 const section_size_type oview_size =
7030 convert_to_section_size_type(this->data_size());
7031 unsigned char* const oview = of->get_output_view(offset, oview_size);
7032
7033 const off_t got_file_offset = this->got_plt_->offset();
7034 const section_size_type got_size =
7035 convert_to_section_size_type(this->got_plt_->data_size());
7036 unsigned char* const got_view = of->get_output_view(got_file_offset,
7037 got_size);
7038 unsigned char* pov = oview;
7039
7040 Arm_address plt_address = this->address();
7041 Arm_address got_address = this->got_plt_->address();
7042
7043 // Write first PLT entry. All but the last word are constants.
7044 const size_t num_first_plt_words = (sizeof(first_plt_entry)
7045 / sizeof(plt_entry[0]));
7046 for (size_t i = 0; i < num_first_plt_words - 1; i++)
7047 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
7048 // Last word in first PLT entry is &GOT[0] - .
7049 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
7050 got_address - (plt_address + 16));
7051 pov += sizeof(first_plt_entry);
7052
7053 unsigned char* got_pov = got_view;
7054
7055 memset(got_pov, 0, 12);
7056 got_pov += 12;
7057
7058 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
7059 unsigned int plt_offset = sizeof(first_plt_entry);
7060 unsigned int plt_rel_offset = 0;
7061 unsigned int got_offset = 12;
7062 const unsigned int count = this->count_;
7063 for (unsigned int i = 0;
7064 i < count;
7065 ++i,
7066 pov += sizeof(plt_entry),
7067 got_pov += 4,
7068 plt_offset += sizeof(plt_entry),
7069 plt_rel_offset += rel_size,
7070 got_offset += 4)
7071 {
7072 // Set and adjust the PLT entry itself.
7073 int32_t offset = ((got_address + got_offset)
7074 - (plt_address + plt_offset + 8));
7075
7076 gold_assert(offset >= 0 && offset < 0x0fffffff);
7077 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
7078 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
7079 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
7080 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
7081 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
7082 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
7083
7084 // Set the entry in the GOT.
7085 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
7086 }
7087
7088 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
7089 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
7090
7091 of->write_output_view(offset, oview_size, oview);
7092 of->write_output_view(got_file_offset, got_size, got_view);
7093 }
7094
7095 // Create a PLT entry for a global symbol.
7096
7097 template<bool big_endian>
7098 void
7099 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
7100 Symbol* gsym)
7101 {
7102 if (gsym->has_plt_offset())
7103 return;
7104
7105 if (this->plt_ == NULL)
7106 {
7107 // Create the GOT sections first.
7108 this->got_section(symtab, layout);
7109
7110 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
7111 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
7112 (elfcpp::SHF_ALLOC
7113 | elfcpp::SHF_EXECINSTR),
7114 this->plt_, false, false, false, false);
7115 }
7116 this->plt_->add_entry(gsym);
7117 }
7118
7119 // Get the section to use for TLS_DESC relocations.
7120
7121 template<bool big_endian>
7122 typename Target_arm<big_endian>::Reloc_section*
7123 Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
7124 {
7125 return this->plt_section()->rel_tls_desc(layout);
7126 }
7127
7128 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
7129
7130 template<bool big_endian>
7131 void
7132 Target_arm<big_endian>::define_tls_base_symbol(
7133 Symbol_table* symtab,
7134 Layout* layout)
7135 {
7136 if (this->tls_base_symbol_defined_)
7137 return;
7138
7139 Output_segment* tls_segment = layout->tls_segment();
7140 if (tls_segment != NULL)
7141 {
7142 bool is_exec = parameters->options().output_is_executable();
7143 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
7144 Symbol_table::PREDEFINED,
7145 tls_segment, 0, 0,
7146 elfcpp::STT_TLS,
7147 elfcpp::STB_LOCAL,
7148 elfcpp::STV_HIDDEN, 0,
7149 (is_exec
7150 ? Symbol::SEGMENT_END
7151 : Symbol::SEGMENT_START),
7152 true);
7153 }
7154 this->tls_base_symbol_defined_ = true;
7155 }
7156
7157 // Create a GOT entry for the TLS module index.
7158
7159 template<bool big_endian>
7160 unsigned int
7161 Target_arm<big_endian>::got_mod_index_entry(
7162 Symbol_table* symtab,
7163 Layout* layout,
7164 Sized_relobj<32, big_endian>* object)
7165 {
7166 if (this->got_mod_index_offset_ == -1U)
7167 {
7168 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7169 Arm_output_data_got<big_endian>* got = this->got_section(symtab, layout);
7170 unsigned int got_offset;
7171 if (!parameters->doing_static_link())
7172 {
7173 got_offset = got->add_constant(0);
7174 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
7175 rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
7176 got_offset);
7177 }
7178 else
7179 {
7180 // We are doing a static link. Just mark it as belong to module 1,
7181 // the executable.
7182 got_offset = got->add_constant(1);
7183 }
7184
7185 got->add_constant(0);
7186 this->got_mod_index_offset_ = got_offset;
7187 }
7188 return this->got_mod_index_offset_;
7189 }
7190
7191 // Optimize the TLS relocation type based on what we know about the
7192 // symbol. IS_FINAL is true if the final address of this symbol is
7193 // known at link time.
7194
7195 template<bool big_endian>
7196 tls::Tls_optimization
7197 Target_arm<big_endian>::optimize_tls_reloc(bool, int)
7198 {
7199 // FIXME: Currently we do not do any TLS optimization.
7200 return tls::TLSOPT_NONE;
7201 }
7202
7203 // Report an unsupported relocation against a local symbol.
7204
7205 template<bool big_endian>
7206 void
7207 Target_arm<big_endian>::Scan::unsupported_reloc_local(
7208 Sized_relobj<32, big_endian>* object,
7209 unsigned int r_type)
7210 {
7211 gold_error(_("%s: unsupported reloc %u against local symbol"),
7212 object->name().c_str(), r_type);
7213 }
7214
7215 // We are about to emit a dynamic relocation of type R_TYPE. If the
7216 // dynamic linker does not support it, issue an error. The GNU linker
7217 // only issues a non-PIC error for an allocated read-only section.
7218 // Here we know the section is allocated, but we don't know that it is
7219 // read-only. But we check for all the relocation types which the
7220 // glibc dynamic linker supports, so it seems appropriate to issue an
7221 // error even if the section is not read-only.
7222
7223 template<bool big_endian>
7224 void
7225 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
7226 unsigned int r_type)
7227 {
7228 switch (r_type)
7229 {
7230 // These are the relocation types supported by glibc for ARM.
7231 case elfcpp::R_ARM_RELATIVE:
7232 case elfcpp::R_ARM_COPY:
7233 case elfcpp::R_ARM_GLOB_DAT:
7234 case elfcpp::R_ARM_JUMP_SLOT:
7235 case elfcpp::R_ARM_ABS32:
7236 case elfcpp::R_ARM_ABS32_NOI:
7237 case elfcpp::R_ARM_PC24:
7238 // FIXME: The following 3 types are not supported by Android's dynamic
7239 // linker.
7240 case elfcpp::R_ARM_TLS_DTPMOD32:
7241 case elfcpp::R_ARM_TLS_DTPOFF32:
7242 case elfcpp::R_ARM_TLS_TPOFF32:
7243 return;
7244
7245 default:
7246 {
7247 // This prevents us from issuing more than one error per reloc
7248 // section. But we can still wind up issuing more than one
7249 // error per object file.
7250 if (this->issued_non_pic_error_)
7251 return;
7252 const Arm_reloc_property* reloc_property =
7253 arm_reloc_property_table->get_reloc_property(r_type);
7254 gold_assert(reloc_property != NULL);
7255 object->error(_("requires unsupported dynamic reloc %s; "
7256 "recompile with -fPIC"),
7257 reloc_property->name().c_str());
7258 this->issued_non_pic_error_ = true;
7259 return;
7260 }
7261
7262 case elfcpp::R_ARM_NONE:
7263 gold_unreachable();
7264 }
7265 }
7266
7267 // Scan a relocation for a local symbol.
7268 // FIXME: This only handles a subset of relocation types used by Android
7269 // on ARM v5te devices.
7270
7271 template<bool big_endian>
7272 inline void
7273 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
7274 Layout* layout,
7275 Target_arm* target,
7276 Sized_relobj<32, big_endian>* object,
7277 unsigned int data_shndx,
7278 Output_section* output_section,
7279 const elfcpp::Rel<32, big_endian>& reloc,
7280 unsigned int r_type,
7281 const elfcpp::Sym<32, big_endian>& lsym)
7282 {
7283 r_type = get_real_reloc_type(r_type);
7284 switch (r_type)
7285 {
7286 case elfcpp::R_ARM_NONE:
7287 case elfcpp::R_ARM_V4BX:
7288 case elfcpp::R_ARM_GNU_VTENTRY:
7289 case elfcpp::R_ARM_GNU_VTINHERIT:
7290 break;
7291
7292 case elfcpp::R_ARM_ABS32:
7293 case elfcpp::R_ARM_ABS32_NOI:
7294 // If building a shared library (or a position-independent
7295 // executable), we need to create a dynamic relocation for
7296 // this location. The relocation applied at link time will
7297 // apply the link-time value, so we flag the location with
7298 // an R_ARM_RELATIVE relocation so the dynamic loader can
7299 // relocate it easily.
7300 if (parameters->options().output_is_position_independent())
7301 {
7302 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7303 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7304 // If we are to add more other reloc types than R_ARM_ABS32,
7305 // we need to add check_non_pic(object, r_type) here.
7306 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
7307 output_section, data_shndx,
7308 reloc.get_r_offset());
7309 }
7310 break;
7311
7312 case elfcpp::R_ARM_ABS16:
7313 case elfcpp::R_ARM_ABS12:
7314 case elfcpp::R_ARM_THM_ABS5:
7315 case elfcpp::R_ARM_ABS8:
7316 case elfcpp::R_ARM_BASE_ABS:
7317 case elfcpp::R_ARM_MOVW_ABS_NC:
7318 case elfcpp::R_ARM_MOVT_ABS:
7319 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7320 case elfcpp::R_ARM_THM_MOVT_ABS:
7321 // If building a shared library (or a position-independent
7322 // executable), we need to create a dynamic relocation for
7323 // this location. Because the addend needs to remain in the
7324 // data section, we need to be careful not to apply this
7325 // relocation statically.
7326 if (parameters->options().output_is_position_independent())
7327 {
7328 check_non_pic(object, r_type);
7329 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7330 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7331 if (lsym.get_st_type() != elfcpp::STT_SECTION)
7332 rel_dyn->add_local(object, r_sym, r_type, output_section,
7333 data_shndx, reloc.get_r_offset());
7334 else
7335 {
7336 gold_assert(lsym.get_st_value() == 0);
7337 unsigned int shndx = lsym.get_st_shndx();
7338 bool is_ordinary;
7339 shndx = object->adjust_sym_shndx(r_sym, shndx,
7340 &is_ordinary);
7341 if (!is_ordinary)
7342 object->error(_("section symbol %u has bad shndx %u"),
7343 r_sym, shndx);
7344 else
7345 rel_dyn->add_local_section(object, shndx,
7346 r_type, output_section,
7347 data_shndx, reloc.get_r_offset());
7348 }
7349 }
7350 break;
7351
7352 case elfcpp::R_ARM_PC24:
7353 case elfcpp::R_ARM_REL32:
7354 case elfcpp::R_ARM_LDR_PC_G0:
7355 case elfcpp::R_ARM_SBREL32:
7356 case elfcpp::R_ARM_THM_CALL:
7357 case elfcpp::R_ARM_THM_PC8:
7358 case elfcpp::R_ARM_BASE_PREL:
7359 case elfcpp::R_ARM_PLT32:
7360 case elfcpp::R_ARM_CALL:
7361 case elfcpp::R_ARM_JUMP24:
7362 case elfcpp::R_ARM_THM_JUMP24:
7363 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7364 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7365 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7366 case elfcpp::R_ARM_SBREL31:
7367 case elfcpp::R_ARM_PREL31:
7368 case elfcpp::R_ARM_MOVW_PREL_NC:
7369 case elfcpp::R_ARM_MOVT_PREL:
7370 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7371 case elfcpp::R_ARM_THM_MOVT_PREL:
7372 case elfcpp::R_ARM_THM_JUMP19:
7373 case elfcpp::R_ARM_THM_JUMP6:
7374 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7375 case elfcpp::R_ARM_THM_PC12:
7376 case elfcpp::R_ARM_REL32_NOI:
7377 case elfcpp::R_ARM_ALU_PC_G0_NC:
7378 case elfcpp::R_ARM_ALU_PC_G0:
7379 case elfcpp::R_ARM_ALU_PC_G1_NC:
7380 case elfcpp::R_ARM_ALU_PC_G1:
7381 case elfcpp::R_ARM_ALU_PC_G2:
7382 case elfcpp::R_ARM_LDR_PC_G1:
7383 case elfcpp::R_ARM_LDR_PC_G2:
7384 case elfcpp::R_ARM_LDRS_PC_G0:
7385 case elfcpp::R_ARM_LDRS_PC_G1:
7386 case elfcpp::R_ARM_LDRS_PC_G2:
7387 case elfcpp::R_ARM_LDC_PC_G0:
7388 case elfcpp::R_ARM_LDC_PC_G1:
7389 case elfcpp::R_ARM_LDC_PC_G2:
7390 case elfcpp::R_ARM_ALU_SB_G0_NC:
7391 case elfcpp::R_ARM_ALU_SB_G0:
7392 case elfcpp::R_ARM_ALU_SB_G1_NC:
7393 case elfcpp::R_ARM_ALU_SB_G1:
7394 case elfcpp::R_ARM_ALU_SB_G2:
7395 case elfcpp::R_ARM_LDR_SB_G0:
7396 case elfcpp::R_ARM_LDR_SB_G1:
7397 case elfcpp::R_ARM_LDR_SB_G2:
7398 case elfcpp::R_ARM_LDRS_SB_G0:
7399 case elfcpp::R_ARM_LDRS_SB_G1:
7400 case elfcpp::R_ARM_LDRS_SB_G2:
7401 case elfcpp::R_ARM_LDC_SB_G0:
7402 case elfcpp::R_ARM_LDC_SB_G1:
7403 case elfcpp::R_ARM_LDC_SB_G2:
7404 case elfcpp::R_ARM_MOVW_BREL_NC:
7405 case elfcpp::R_ARM_MOVT_BREL:
7406 case elfcpp::R_ARM_MOVW_BREL:
7407 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7408 case elfcpp::R_ARM_THM_MOVT_BREL:
7409 case elfcpp::R_ARM_THM_MOVW_BREL:
7410 case elfcpp::R_ARM_THM_JUMP11:
7411 case elfcpp::R_ARM_THM_JUMP8:
7412 // We don't need to do anything for a relative addressing relocation
7413 // against a local symbol if it does not reference the GOT.
7414 break;
7415
7416 case elfcpp::R_ARM_GOTOFF32:
7417 case elfcpp::R_ARM_GOTOFF12:
7418 // We need a GOT section:
7419 target->got_section(symtab, layout);
7420 break;
7421
7422 case elfcpp::R_ARM_GOT_BREL:
7423 case elfcpp::R_ARM_GOT_PREL:
7424 {
7425 // The symbol requires a GOT entry.
7426 Arm_output_data_got<big_endian>* got =
7427 target->got_section(symtab, layout);
7428 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7429 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
7430 {
7431 // If we are generating a shared object, we need to add a
7432 // dynamic RELATIVE relocation for this symbol's GOT entry.
7433 if (parameters->options().output_is_position_independent())
7434 {
7435 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7436 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7437 rel_dyn->add_local_relative(
7438 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
7439 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
7440 }
7441 }
7442 }
7443 break;
7444
7445 case elfcpp::R_ARM_TARGET1:
7446 case elfcpp::R_ARM_TARGET2:
7447 // This should have been mapped to another type already.
7448 // Fall through.
7449 case elfcpp::R_ARM_COPY:
7450 case elfcpp::R_ARM_GLOB_DAT:
7451 case elfcpp::R_ARM_JUMP_SLOT:
7452 case elfcpp::R_ARM_RELATIVE:
7453 // These are relocations which should only be seen by the
7454 // dynamic linker, and should never be seen here.
7455 gold_error(_("%s: unexpected reloc %u in object file"),
7456 object->name().c_str(), r_type);
7457 break;
7458
7459
7460 // These are initial TLS relocs, which are expected when
7461 // linking.
7462 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7463 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7464 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7465 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7466 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7467 {
7468 bool output_is_shared = parameters->options().shared();
7469 const tls::Tls_optimization optimized_type
7470 = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
7471 r_type);
7472 switch (r_type)
7473 {
7474 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7475 if (optimized_type == tls::TLSOPT_NONE)
7476 {
7477 // Create a pair of GOT entries for the module index and
7478 // dtv-relative offset.
7479 Arm_output_data_got<big_endian>* got
7480 = target->got_section(symtab, layout);
7481 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7482 unsigned int shndx = lsym.get_st_shndx();
7483 bool is_ordinary;
7484 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7485 if (!is_ordinary)
7486 {
7487 object->error(_("local symbol %u has bad shndx %u"),
7488 r_sym, shndx);
7489 break;
7490 }
7491
7492 if (!parameters->doing_static_link())
7493 got->add_local_pair_with_rel(object, r_sym, shndx,
7494 GOT_TYPE_TLS_PAIR,
7495 target->rel_dyn_section(layout),
7496 elfcpp::R_ARM_TLS_DTPMOD32, 0);
7497 else
7498 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR,
7499 object, r_sym);
7500 }
7501 else
7502 // FIXME: TLS optimization not supported yet.
7503 gold_unreachable();
7504 break;
7505
7506 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7507 if (optimized_type == tls::TLSOPT_NONE)
7508 {
7509 // Create a GOT entry for the module index.
7510 target->got_mod_index_entry(symtab, layout, object);
7511 }
7512 else
7513 // FIXME: TLS optimization not supported yet.
7514 gold_unreachable();
7515 break;
7516
7517 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7518 break;
7519
7520 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7521 layout->set_has_static_tls();
7522 if (optimized_type == tls::TLSOPT_NONE)
7523 {
7524 // Create a GOT entry for the tp-relative offset.
7525 Arm_output_data_got<big_endian>* got
7526 = target->got_section(symtab, layout);
7527 unsigned int r_sym =
7528 elfcpp::elf_r_sym<32>(reloc.get_r_info());
7529 if (!parameters->doing_static_link())
7530 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
7531 target->rel_dyn_section(layout),
7532 elfcpp::R_ARM_TLS_TPOFF32);
7533 else if (!object->local_has_got_offset(r_sym,
7534 GOT_TYPE_TLS_OFFSET))
7535 {
7536 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
7537 unsigned int got_offset =
7538 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
7539 got->add_static_reloc(got_offset,
7540 elfcpp::R_ARM_TLS_TPOFF32, object,
7541 r_sym);
7542 }
7543 }
7544 else
7545 // FIXME: TLS optimization not supported yet.
7546 gold_unreachable();
7547 break;
7548
7549 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7550 layout->set_has_static_tls();
7551 if (output_is_shared)
7552 {
7553 // We need to create a dynamic relocation.
7554 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
7555 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7556 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7557 rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
7558 output_section, data_shndx,
7559 reloc.get_r_offset());
7560 }
7561 break;
7562
7563 default:
7564 gold_unreachable();
7565 }
7566 }
7567 break;
7568
7569 default:
7570 unsupported_reloc_local(object, r_type);
7571 break;
7572 }
7573 }
7574
7575 // Report an unsupported relocation against a global symbol.
7576
7577 template<bool big_endian>
7578 void
7579 Target_arm<big_endian>::Scan::unsupported_reloc_global(
7580 Sized_relobj<32, big_endian>* object,
7581 unsigned int r_type,
7582 Symbol* gsym)
7583 {
7584 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7585 object->name().c_str(), r_type, gsym->demangled_name().c_str());
7586 }
7587
7588 // Scan a relocation for a global symbol.
7589
7590 template<bool big_endian>
7591 inline void
7592 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
7593 Layout* layout,
7594 Target_arm* target,
7595 Sized_relobj<32, big_endian>* object,
7596 unsigned int data_shndx,
7597 Output_section* output_section,
7598 const elfcpp::Rel<32, big_endian>& reloc,
7599 unsigned int r_type,
7600 Symbol* gsym)
7601 {
7602 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
7603 // section. We check here to avoid creating a dynamic reloc against
7604 // _GLOBAL_OFFSET_TABLE_.
7605 if (!target->has_got_section()
7606 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7607 target->got_section(symtab, layout);
7608
7609 r_type = get_real_reloc_type(r_type);
7610 switch (r_type)
7611 {
7612 case elfcpp::R_ARM_NONE:
7613 case elfcpp::R_ARM_V4BX:
7614 case elfcpp::R_ARM_GNU_VTENTRY:
7615 case elfcpp::R_ARM_GNU_VTINHERIT:
7616 break;
7617
7618 case elfcpp::R_ARM_ABS32:
7619 case elfcpp::R_ARM_ABS16:
7620 case elfcpp::R_ARM_ABS12:
7621 case elfcpp::R_ARM_THM_ABS5:
7622 case elfcpp::R_ARM_ABS8:
7623 case elfcpp::R_ARM_BASE_ABS:
7624 case elfcpp::R_ARM_MOVW_ABS_NC:
7625 case elfcpp::R_ARM_MOVT_ABS:
7626 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7627 case elfcpp::R_ARM_THM_MOVT_ABS:
7628 case elfcpp::R_ARM_ABS32_NOI:
7629 // Absolute addressing relocations.
7630 {
7631 // Make a PLT entry if necessary.
7632 if (this->symbol_needs_plt_entry(gsym))
7633 {
7634 target->make_plt_entry(symtab, layout, gsym);
7635 // Since this is not a PC-relative relocation, we may be
7636 // taking the address of a function. In that case we need to
7637 // set the entry in the dynamic symbol table to the address of
7638 // the PLT entry.
7639 if (gsym->is_from_dynobj() && !parameters->options().shared())
7640 gsym->set_needs_dynsym_value();
7641 }
7642 // Make a dynamic relocation if necessary.
7643 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
7644 {
7645 if (gsym->may_need_copy_reloc())
7646 {
7647 target->copy_reloc(symtab, layout, object,
7648 data_shndx, output_section, gsym, reloc);
7649 }
7650 else if ((r_type == elfcpp::R_ARM_ABS32
7651 || r_type == elfcpp::R_ARM_ABS32_NOI)
7652 && gsym->can_use_relative_reloc(false))
7653 {
7654 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7655 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
7656 output_section, object,
7657 data_shndx, reloc.get_r_offset());
7658 }
7659 else
7660 {
7661 check_non_pic(object, r_type);
7662 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7663 rel_dyn->add_global(gsym, r_type, output_section, object,
7664 data_shndx, reloc.get_r_offset());
7665 }
7666 }
7667 }
7668 break;
7669
7670 case elfcpp::R_ARM_GOTOFF32:
7671 case elfcpp::R_ARM_GOTOFF12:
7672 // We need a GOT section.
7673 target->got_section(symtab, layout);
7674 break;
7675
7676 case elfcpp::R_ARM_REL32:
7677 case elfcpp::R_ARM_LDR_PC_G0:
7678 case elfcpp::R_ARM_SBREL32:
7679 case elfcpp::R_ARM_THM_PC8:
7680 case elfcpp::R_ARM_BASE_PREL:
7681 case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7682 case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7683 case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7684 case elfcpp::R_ARM_MOVW_PREL_NC:
7685 case elfcpp::R_ARM_MOVT_PREL:
7686 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7687 case elfcpp::R_ARM_THM_MOVT_PREL:
7688 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7689 case elfcpp::R_ARM_THM_PC12:
7690 case elfcpp::R_ARM_REL32_NOI:
7691 case elfcpp::R_ARM_ALU_PC_G0_NC:
7692 case elfcpp::R_ARM_ALU_PC_G0:
7693 case elfcpp::R_ARM_ALU_PC_G1_NC:
7694 case elfcpp::R_ARM_ALU_PC_G1:
7695 case elfcpp::R_ARM_ALU_PC_G2:
7696 case elfcpp::R_ARM_LDR_PC_G1:
7697 case elfcpp::R_ARM_LDR_PC_G2:
7698 case elfcpp::R_ARM_LDRS_PC_G0:
7699 case elfcpp::R_ARM_LDRS_PC_G1:
7700 case elfcpp::R_ARM_LDRS_PC_G2:
7701 case elfcpp::R_ARM_LDC_PC_G0:
7702 case elfcpp::R_ARM_LDC_PC_G1:
7703 case elfcpp::R_ARM_LDC_PC_G2:
7704 case elfcpp::R_ARM_ALU_SB_G0_NC:
7705 case elfcpp::R_ARM_ALU_SB_G0:
7706 case elfcpp::R_ARM_ALU_SB_G1_NC:
7707 case elfcpp::R_ARM_ALU_SB_G1:
7708 case elfcpp::R_ARM_ALU_SB_G2:
7709 case elfcpp::R_ARM_LDR_SB_G0:
7710 case elfcpp::R_ARM_LDR_SB_G1:
7711 case elfcpp::R_ARM_LDR_SB_G2:
7712 case elfcpp::R_ARM_LDRS_SB_G0:
7713 case elfcpp::R_ARM_LDRS_SB_G1:
7714 case elfcpp::R_ARM_LDRS_SB_G2:
7715 case elfcpp::R_ARM_LDC_SB_G0:
7716 case elfcpp::R_ARM_LDC_SB_G1:
7717 case elfcpp::R_ARM_LDC_SB_G2:
7718 case elfcpp::R_ARM_MOVW_BREL_NC:
7719 case elfcpp::R_ARM_MOVT_BREL:
7720 case elfcpp::R_ARM_MOVW_BREL:
7721 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7722 case elfcpp::R_ARM_THM_MOVT_BREL:
7723 case elfcpp::R_ARM_THM_MOVW_BREL:
7724 // Relative addressing relocations.
7725 {
7726 // Make a dynamic relocation if necessary.
7727 int flags = Symbol::NON_PIC_REF;
7728 if (gsym->needs_dynamic_reloc(flags))
7729 {
7730 if (target->may_need_copy_reloc(gsym))
7731 {
7732 target->copy_reloc(symtab, layout, object,
7733 data_shndx, output_section, gsym, reloc);
7734 }
7735 else
7736 {
7737 check_non_pic(object, r_type);
7738 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7739 rel_dyn->add_global(gsym, r_type, output_section, object,
7740 data_shndx, reloc.get_r_offset());
7741 }
7742 }
7743 }
7744 break;
7745
7746 case elfcpp::R_ARM_PC24:
7747 case elfcpp::R_ARM_THM_CALL:
7748 case elfcpp::R_ARM_PLT32:
7749 case elfcpp::R_ARM_CALL:
7750 case elfcpp::R_ARM_JUMP24:
7751 case elfcpp::R_ARM_THM_JUMP24:
7752 case elfcpp::R_ARM_SBREL31:
7753 case elfcpp::R_ARM_PREL31:
7754 case elfcpp::R_ARM_THM_JUMP19:
7755 case elfcpp::R_ARM_THM_JUMP6:
7756 case elfcpp::R_ARM_THM_JUMP11:
7757 case elfcpp::R_ARM_THM_JUMP8:
7758 // All the relocation above are branches except for the PREL31 ones.
7759 // A PREL31 relocation can point to a personality function in a shared
7760 // library. In that case we want to use a PLT because we want to
7761 // call the personality routine and the dyanmic linkers we care about
7762 // do not support dynamic PREL31 relocations. An REL31 relocation may
7763 // point to a function whose unwinding behaviour is being described but
7764 // we will not mistakenly generate a PLT for that because we should use
7765 // a local section symbol.
7766
7767 // If the symbol is fully resolved, this is just a relative
7768 // local reloc. Otherwise we need a PLT entry.
7769 if (gsym->final_value_is_known())
7770 break;
7771 // If building a shared library, we can also skip the PLT entry
7772 // if the symbol is defined in the output file and is protected
7773 // or hidden.
7774 if (gsym->is_defined()
7775 && !gsym->is_from_dynobj()
7776 && !gsym->is_preemptible())
7777 break;
7778 target->make_plt_entry(symtab, layout, gsym);
7779 break;
7780
7781 case elfcpp::R_ARM_GOT_BREL:
7782 case elfcpp::R_ARM_GOT_ABS:
7783 case elfcpp::R_ARM_GOT_PREL:
7784 {
7785 // The symbol requires a GOT entry.
7786 Arm_output_data_got<big_endian>* got =
7787 target->got_section(symtab, layout);
7788 if (gsym->final_value_is_known())
7789 got->add_global(gsym, GOT_TYPE_STANDARD);
7790 else
7791 {
7792 // If this symbol is not fully resolved, we need to add a
7793 // GOT entry with a dynamic relocation.
7794 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7795 if (gsym->is_from_dynobj()
7796 || gsym->is_undefined()
7797 || gsym->is_preemptible())
7798 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
7799 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
7800 else
7801 {
7802 if (got->add_global(gsym, GOT_TYPE_STANDARD))
7803 rel_dyn->add_global_relative(
7804 gsym, elfcpp::R_ARM_RELATIVE, got,
7805 gsym->got_offset(GOT_TYPE_STANDARD));
7806 }
7807 }
7808 }
7809 break;
7810
7811 case elfcpp::R_ARM_TARGET1:
7812 case elfcpp::R_ARM_TARGET2:
7813 // These should have been mapped to other types already.
7814 // Fall through.
7815 case elfcpp::R_ARM_COPY:
7816 case elfcpp::R_ARM_GLOB_DAT:
7817 case elfcpp::R_ARM_JUMP_SLOT:
7818 case elfcpp::R_ARM_RELATIVE:
7819 // These are relocations which should only be seen by the
7820 // dynamic linker, and should never be seen here.
7821 gold_error(_("%s: unexpected reloc %u in object file"),
7822 object->name().c_str(), r_type);
7823 break;
7824
7825 // These are initial tls relocs, which are expected when
7826 // linking.
7827 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7828 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7829 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7830 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7831 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7832 {
7833 const bool is_final = gsym->final_value_is_known();
7834 const tls::Tls_optimization optimized_type
7835 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
7836 switch (r_type)
7837 {
7838 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
7839 if (optimized_type == tls::TLSOPT_NONE)
7840 {
7841 // Create a pair of GOT entries for the module index and
7842 // dtv-relative offset.
7843 Arm_output_data_got<big_endian>* got
7844 = target->got_section(symtab, layout);
7845 if (!parameters->doing_static_link())
7846 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
7847 target->rel_dyn_section(layout),
7848 elfcpp::R_ARM_TLS_DTPMOD32,
7849 elfcpp::R_ARM_TLS_DTPOFF32);
7850 else
7851 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
7852 }
7853 else
7854 // FIXME: TLS optimization not supported yet.
7855 gold_unreachable();
7856 break;
7857
7858 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
7859 if (optimized_type == tls::TLSOPT_NONE)
7860 {
7861 // Create a GOT entry for the module index.
7862 target->got_mod_index_entry(symtab, layout, object);
7863 }
7864 else
7865 // FIXME: TLS optimization not supported yet.
7866 gold_unreachable();
7867 break;
7868
7869 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
7870 break;
7871
7872 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
7873 layout->set_has_static_tls();
7874 if (optimized_type == tls::TLSOPT_NONE)
7875 {
7876 // Create a GOT entry for the tp-relative offset.
7877 Arm_output_data_got<big_endian>* got
7878 = target->got_section(symtab, layout);
7879 if (!parameters->doing_static_link())
7880 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
7881 target->rel_dyn_section(layout),
7882 elfcpp::R_ARM_TLS_TPOFF32);
7883 else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
7884 {
7885 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
7886 unsigned int got_offset =
7887 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
7888 got->add_static_reloc(got_offset,
7889 elfcpp::R_ARM_TLS_TPOFF32, gsym);
7890 }
7891 }
7892 else
7893 // FIXME: TLS optimization not supported yet.
7894 gold_unreachable();
7895 break;
7896
7897 case elfcpp::R_ARM_TLS_LE32: // Local-exec
7898 layout->set_has_static_tls();
7899 if (parameters->options().shared())
7900 {
7901 // We need to create a dynamic relocation.
7902 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7903 rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
7904 output_section, object,
7905 data_shndx, reloc.get_r_offset());
7906 }
7907 break;
7908
7909 default:
7910 gold_unreachable();
7911 }
7912 }
7913 break;
7914
7915 default:
7916 unsupported_reloc_global(object, r_type, gsym);
7917 break;
7918 }
7919 }
7920
7921 // Process relocations for gc.
7922
7923 template<bool big_endian>
7924 void
7925 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
7926 Layout* layout,
7927 Sized_relobj<32, big_endian>* object,
7928 unsigned int data_shndx,
7929 unsigned int,
7930 const unsigned char* prelocs,
7931 size_t reloc_count,
7932 Output_section* output_section,
7933 bool needs_special_offset_handling,
7934 size_t local_symbol_count,
7935 const unsigned char* plocal_symbols)
7936 {
7937 typedef Target_arm<big_endian> Arm;
7938 typedef typename Target_arm<big_endian>::Scan Scan;
7939
7940 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
7941 symtab,
7942 layout,
7943 this,
7944 object,
7945 data_shndx,
7946 prelocs,
7947 reloc_count,
7948 output_section,
7949 needs_special_offset_handling,
7950 local_symbol_count,
7951 plocal_symbols);
7952 }
7953
7954 // Scan relocations for a section.
7955
7956 template<bool big_endian>
7957 void
7958 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
7959 Layout* layout,
7960 Sized_relobj<32, big_endian>* object,
7961 unsigned int data_shndx,
7962 unsigned int sh_type,
7963 const unsigned char* prelocs,
7964 size_t reloc_count,
7965 Output_section* output_section,
7966 bool needs_special_offset_handling,
7967 size_t local_symbol_count,
7968 const unsigned char* plocal_symbols)
7969 {
7970 typedef typename Target_arm<big_endian>::Scan Scan;
7971 if (sh_type == elfcpp::SHT_RELA)
7972 {
7973 gold_error(_("%s: unsupported RELA reloc section"),
7974 object->name().c_str());
7975 return;
7976 }
7977
7978 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
7979 symtab,
7980 layout,
7981 this,
7982 object,
7983 data_shndx,
7984 prelocs,
7985 reloc_count,
7986 output_section,
7987 needs_special_offset_handling,
7988 local_symbol_count,
7989 plocal_symbols);
7990 }
7991
7992 // Finalize the sections.
7993
7994 template<bool big_endian>
7995 void
7996 Target_arm<big_endian>::do_finalize_sections(
7997 Layout* layout,
7998 const Input_objects* input_objects,
7999 Symbol_table* symtab)
8000 {
8001 // Merge processor-specific flags.
8002 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8003 p != input_objects->relobj_end();
8004 ++p)
8005 {
8006 Arm_relobj<big_endian>* arm_relobj =
8007 Arm_relobj<big_endian>::as_arm_relobj(*p);
8008 if (arm_relobj->merge_flags_and_attributes())
8009 {
8010 this->merge_processor_specific_flags(
8011 arm_relobj->name(),
8012 arm_relobj->processor_specific_flags());
8013 this->merge_object_attributes(arm_relobj->name().c_str(),
8014 arm_relobj->attributes_section_data());
8015 }
8016 }
8017
8018 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
8019 p != input_objects->dynobj_end();
8020 ++p)
8021 {
8022 Arm_dynobj<big_endian>* arm_dynobj =
8023 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
8024 this->merge_processor_specific_flags(
8025 arm_dynobj->name(),
8026 arm_dynobj->processor_specific_flags());
8027 this->merge_object_attributes(arm_dynobj->name().c_str(),
8028 arm_dynobj->attributes_section_data());
8029 }
8030
8031 // Create an empty uninitialized attribute section if we still don't have it
8032 // at this moment. This happens if there is no attributes sections in all
8033 // inputs.
8034 if (this->attributes_section_data_ == NULL)
8035 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
8036
8037 // Check BLX use.
8038 const Object_attribute* cpu_arch_attr =
8039 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
8040 if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
8041 this->set_may_use_blx(true);
8042
8043 // Check if we need to use Cortex-A8 workaround.
8044 if (parameters->options().user_set_fix_cortex_a8())
8045 this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
8046 else
8047 {
8048 // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
8049 // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
8050 // profile.
8051 const Object_attribute* cpu_arch_profile_attr =
8052 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
8053 this->fix_cortex_a8_ =
8054 (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
8055 && (cpu_arch_profile_attr->int_value() == 'A'
8056 || cpu_arch_profile_attr->int_value() == 0));
8057 }
8058
8059 // Check if we can use V4BX interworking.
8060 // The V4BX interworking stub contains BX instruction,
8061 // which is not specified for some profiles.
8062 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8063 && !this->may_use_blx())
8064 gold_error(_("unable to provide V4BX reloc interworking fix up; "
8065 "the target profile does not support BX instruction"));
8066
8067 // Fill in some more dynamic tags.
8068 const Reloc_section* rel_plt = (this->plt_ == NULL
8069 ? NULL
8070 : this->plt_->rel_plt());
8071 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
8072 this->rel_dyn_, true, false);
8073
8074 // Emit any relocs we saved in an attempt to avoid generating COPY
8075 // relocs.
8076 if (this->copy_relocs_.any_saved_relocs())
8077 this->copy_relocs_.emit(this->rel_dyn_section(layout));
8078
8079 // Handle the .ARM.exidx section.
8080 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
8081 if (exidx_section != NULL
8082 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
8083 && !parameters->options().relocatable())
8084 {
8085 // Create __exidx_start and __exdix_end symbols.
8086 symtab->define_in_output_data("__exidx_start", NULL,
8087 Symbol_table::PREDEFINED,
8088 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8089 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8090 false, true);
8091 symtab->define_in_output_data("__exidx_end", NULL,
8092 Symbol_table::PREDEFINED,
8093 exidx_section, 0, 0, elfcpp::STT_OBJECT,
8094 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8095 true, true);
8096
8097 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
8098 // the .ARM.exidx section.
8099 if (!layout->script_options()->saw_phdrs_clause())
8100 {
8101 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
8102 == NULL);
8103 Output_segment* exidx_segment =
8104 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
8105 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
8106 false);
8107 }
8108 }
8109
8110 // Create an .ARM.attributes section unless we have no regular input
8111 // object. In that case the output will be empty.
8112 if (input_objects->number_of_relobjs() != 0)
8113 {
8114 Output_attributes_section_data* attributes_section =
8115 new Output_attributes_section_data(*this->attributes_section_data_);
8116 layout->add_output_section_data(".ARM.attributes",
8117 elfcpp::SHT_ARM_ATTRIBUTES, 0,
8118 attributes_section, false, false, false,
8119 false);
8120 }
8121 }
8122
8123 // Return whether a direct absolute static relocation needs to be applied.
8124 // In cases where Scan::local() or Scan::global() has created
8125 // a dynamic relocation other than R_ARM_RELATIVE, the addend
8126 // of the relocation is carried in the data, and we must not
8127 // apply the static relocation.
8128
8129 template<bool big_endian>
8130 inline bool
8131 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
8132 const Sized_symbol<32>* gsym,
8133 int ref_flags,
8134 bool is_32bit,
8135 Output_section* output_section)
8136 {
8137 // If the output section is not allocated, then we didn't call
8138 // scan_relocs, we didn't create a dynamic reloc, and we must apply
8139 // the reloc here.
8140 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
8141 return true;
8142
8143 // For local symbols, we will have created a non-RELATIVE dynamic
8144 // relocation only if (a) the output is position independent,
8145 // (b) the relocation is absolute (not pc- or segment-relative), and
8146 // (c) the relocation is not 32 bits wide.
8147 if (gsym == NULL)
8148 return !(parameters->options().output_is_position_independent()
8149 && (ref_flags & Symbol::ABSOLUTE_REF)
8150 && !is_32bit);
8151
8152 // For global symbols, we use the same helper routines used in the
8153 // scan pass. If we did not create a dynamic relocation, or if we
8154 // created a RELATIVE dynamic relocation, we should apply the static
8155 // relocation.
8156 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
8157 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
8158 && gsym->can_use_relative_reloc(ref_flags
8159 & Symbol::FUNCTION_CALL);
8160 return !has_dyn || is_rel;
8161 }
8162
8163 // Perform a relocation.
8164
8165 template<bool big_endian>
8166 inline bool
8167 Target_arm<big_endian>::Relocate::relocate(
8168 const Relocate_info<32, big_endian>* relinfo,
8169 Target_arm* target,
8170 Output_section *output_section,
8171 size_t relnum,
8172 const elfcpp::Rel<32, big_endian>& rel,
8173 unsigned int r_type,
8174 const Sized_symbol<32>* gsym,
8175 const Symbol_value<32>* psymval,
8176 unsigned char* view,
8177 Arm_address address,
8178 section_size_type view_size)
8179 {
8180 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
8181
8182 r_type = get_real_reloc_type(r_type);
8183 const Arm_reloc_property* reloc_property =
8184 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8185 if (reloc_property == NULL)
8186 {
8187 std::string reloc_name =
8188 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8189 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8190 _("cannot relocate %s in object file"),
8191 reloc_name.c_str());
8192 return true;
8193 }
8194
8195 const Arm_relobj<big_endian>* object =
8196 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8197
8198 // If the final branch target of a relocation is THUMB instruction, this
8199 // is 1. Otherwise it is 0.
8200 Arm_address thumb_bit = 0;
8201 Symbol_value<32> symval;
8202 bool is_weakly_undefined_without_plt = false;
8203 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
8204 {
8205 if (gsym != NULL)
8206 {
8207 // This is a global symbol. Determine if we use PLT and if the
8208 // final target is THUMB.
8209 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
8210 {
8211 // This uses a PLT, change the symbol value.
8212 symval.set_output_value(target->plt_section()->address()
8213 + gsym->plt_offset());
8214 psymval = &symval;
8215 }
8216 else if (gsym->is_weak_undefined())
8217 {
8218 // This is a weakly undefined symbol and we do not use PLT
8219 // for this relocation. A branch targeting this symbol will
8220 // be converted into an NOP.
8221 is_weakly_undefined_without_plt = true;
8222 }
8223 else
8224 {
8225 // Set thumb bit if symbol:
8226 // -Has type STT_ARM_TFUNC or
8227 // -Has type STT_FUNC, is defined and with LSB in value set.
8228 thumb_bit =
8229 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
8230 || (gsym->type() == elfcpp::STT_FUNC
8231 && !gsym->is_undefined()
8232 && ((psymval->value(object, 0) & 1) != 0)))
8233 ? 1
8234 : 0);
8235 }
8236 }
8237 else
8238 {
8239 // This is a local symbol. Determine if the final target is THUMB.
8240 // We saved this information when all the local symbols were read.
8241 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
8242 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
8243 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
8244 }
8245 }
8246 else
8247 {
8248 // This is a fake relocation synthesized for a stub. It does not have
8249 // a real symbol. We just look at the LSB of the symbol value to
8250 // determine if the target is THUMB or not.
8251 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
8252 }
8253
8254 // Strip LSB if this points to a THUMB target.
8255 if (thumb_bit != 0
8256 && reloc_property->uses_thumb_bit()
8257 && ((psymval->value(object, 0) & 1) != 0))
8258 {
8259 Arm_address stripped_value =
8260 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
8261 symval.set_output_value(stripped_value);
8262 psymval = &symval;
8263 }
8264
8265 // Get the GOT offset if needed.
8266 // The GOT pointer points to the end of the GOT section.
8267 // We need to subtract the size of the GOT section to get
8268 // the actual offset to use in the relocation.
8269 bool have_got_offset = false;
8270 unsigned int got_offset = 0;
8271 switch (r_type)
8272 {
8273 case elfcpp::R_ARM_GOT_BREL:
8274 case elfcpp::R_ARM_GOT_PREL:
8275 if (gsym != NULL)
8276 {
8277 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8278 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
8279 - target->got_size());
8280 }
8281 else
8282 {
8283 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8284 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8285 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
8286 - target->got_size());
8287 }
8288 have_got_offset = true;
8289 break;
8290
8291 default:
8292 break;
8293 }
8294
8295 // To look up relocation stubs, we need to pass the symbol table index of
8296 // a local symbol.
8297 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8298
8299 // Get the addressing origin of the output segment defining the
8300 // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
8301 Arm_address sym_origin = 0;
8302 if (reloc_property->uses_symbol_base())
8303 {
8304 if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
8305 // R_ARM_BASE_ABS with the NULL symbol will give the
8306 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
8307 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
8308 sym_origin = target->got_plt_section()->address();
8309 else if (gsym == NULL)
8310 sym_origin = 0;
8311 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
8312 sym_origin = gsym->output_segment()->vaddr();
8313 else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
8314 sym_origin = gsym->output_data()->address();
8315
8316 // TODO: Assumes the segment base to be zero for the global symbols
8317 // till the proper support for the segment-base-relative addressing
8318 // will be implemented. This is consistent with GNU ld.
8319 }
8320
8321 // For relative addressing relocation, find out the relative address base.
8322 Arm_address relative_address_base = 0;
8323 switch(reloc_property->relative_address_base())
8324 {
8325 case Arm_reloc_property::RAB_NONE:
8326 // Relocations with relative address bases RAB_TLS and RAB_tp are
8327 // handled by relocate_tls. So we do not need to do anything here.
8328 case Arm_reloc_property::RAB_TLS:
8329 case Arm_reloc_property::RAB_tp:
8330 break;
8331 case Arm_reloc_property::RAB_B_S:
8332 relative_address_base = sym_origin;
8333 break;
8334 case Arm_reloc_property::RAB_GOT_ORG:
8335 relative_address_base = target->got_plt_section()->address();
8336 break;
8337 case Arm_reloc_property::RAB_P:
8338 relative_address_base = address;
8339 break;
8340 case Arm_reloc_property::RAB_Pa:
8341 relative_address_base = address & 0xfffffffcU;
8342 break;
8343 default:
8344 gold_unreachable();
8345 }
8346
8347 typename Arm_relocate_functions::Status reloc_status =
8348 Arm_relocate_functions::STATUS_OKAY;
8349 bool check_overflow = reloc_property->checks_overflow();
8350 switch (r_type)
8351 {
8352 case elfcpp::R_ARM_NONE:
8353 break;
8354
8355 case elfcpp::R_ARM_ABS8:
8356 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8357 output_section))
8358 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
8359 break;
8360
8361 case elfcpp::R_ARM_ABS12:
8362 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8363 output_section))
8364 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
8365 break;
8366
8367 case elfcpp::R_ARM_ABS16:
8368 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8369 output_section))
8370 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
8371 break;
8372
8373 case elfcpp::R_ARM_ABS32:
8374 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
8375 output_section))
8376 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8377 thumb_bit);
8378 break;
8379
8380 case elfcpp::R_ARM_ABS32_NOI:
8381 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
8382 output_section))
8383 // No thumb bit for this relocation: (S + A)
8384 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8385 0);
8386 break;
8387
8388 case elfcpp::R_ARM_MOVW_ABS_NC:
8389 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8390 output_section))
8391 reloc_status = Arm_relocate_functions::movw(view, object, psymval,
8392 0, thumb_bit,
8393 check_overflow);
8394 break;
8395
8396 case elfcpp::R_ARM_MOVT_ABS:
8397 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8398 output_section))
8399 reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
8400 break;
8401
8402 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8403 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8404 output_section))
8405 reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
8406 0, thumb_bit, false);
8407 break;
8408
8409 case elfcpp::R_ARM_THM_MOVT_ABS:
8410 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8411 output_section))
8412 reloc_status = Arm_relocate_functions::thm_movt(view, object,
8413 psymval, 0);
8414 break;
8415
8416 case elfcpp::R_ARM_MOVW_PREL_NC:
8417 case elfcpp::R_ARM_MOVW_BREL_NC:
8418 case elfcpp::R_ARM_MOVW_BREL:
8419 reloc_status =
8420 Arm_relocate_functions::movw(view, object, psymval,
8421 relative_address_base, thumb_bit,
8422 check_overflow);
8423 break;
8424
8425 case elfcpp::R_ARM_MOVT_PREL:
8426 case elfcpp::R_ARM_MOVT_BREL:
8427 reloc_status =
8428 Arm_relocate_functions::movt(view, object, psymval,
8429 relative_address_base);
8430 break;
8431
8432 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8433 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8434 case elfcpp::R_ARM_THM_MOVW_BREL:
8435 reloc_status =
8436 Arm_relocate_functions::thm_movw(view, object, psymval,
8437 relative_address_base,
8438 thumb_bit, check_overflow);
8439 break;
8440
8441 case elfcpp::R_ARM_THM_MOVT_PREL:
8442 case elfcpp::R_ARM_THM_MOVT_BREL:
8443 reloc_status =
8444 Arm_relocate_functions::thm_movt(view, object, psymval,
8445 relative_address_base);
8446 break;
8447
8448 case elfcpp::R_ARM_REL32:
8449 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8450 address, thumb_bit);
8451 break;
8452
8453 case elfcpp::R_ARM_THM_ABS5:
8454 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8455 output_section))
8456 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
8457 break;
8458
8459 // Thumb long branches.
8460 case elfcpp::R_ARM_THM_CALL:
8461 case elfcpp::R_ARM_THM_XPC22:
8462 case elfcpp::R_ARM_THM_JUMP24:
8463 reloc_status =
8464 Arm_relocate_functions::thumb_branch_common(
8465 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8466 thumb_bit, is_weakly_undefined_without_plt);
8467 break;
8468
8469 case elfcpp::R_ARM_GOTOFF32:
8470 {
8471 Arm_address got_origin;
8472 got_origin = target->got_plt_section()->address();
8473 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8474 got_origin, thumb_bit);
8475 }
8476 break;
8477
8478 case elfcpp::R_ARM_BASE_PREL:
8479 gold_assert(gsym != NULL);
8480 reloc_status =
8481 Arm_relocate_functions::base_prel(view, sym_origin, address);
8482 break;
8483
8484 case elfcpp::R_ARM_BASE_ABS:
8485 {
8486 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8487 output_section))
8488 break;
8489
8490 reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
8491 }
8492 break;
8493
8494 case elfcpp::R_ARM_GOT_BREL:
8495 gold_assert(have_got_offset);
8496 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
8497 break;
8498
8499 case elfcpp::R_ARM_GOT_PREL:
8500 gold_assert(have_got_offset);
8501 // Get the address origin for GOT PLT, which is allocated right
8502 // after the GOT section, to calculate an absolute address of
8503 // the symbol GOT entry (got_origin + got_offset).
8504 Arm_address got_origin;
8505 got_origin = target->got_plt_section()->address();
8506 reloc_status = Arm_relocate_functions::got_prel(view,
8507 got_origin + got_offset,
8508 address);
8509 break;
8510
8511 case elfcpp::R_ARM_PLT32:
8512 case elfcpp::R_ARM_CALL:
8513 case elfcpp::R_ARM_JUMP24:
8514 case elfcpp::R_ARM_XPC25:
8515 gold_assert(gsym == NULL
8516 || gsym->has_plt_offset()
8517 || gsym->final_value_is_known()
8518 || (gsym->is_defined()
8519 && !gsym->is_from_dynobj()
8520 && !gsym->is_preemptible()));
8521 reloc_status =
8522 Arm_relocate_functions::arm_branch_common(
8523 r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8524 thumb_bit, is_weakly_undefined_without_plt);
8525 break;
8526
8527 case elfcpp::R_ARM_THM_JUMP19:
8528 reloc_status =
8529 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
8530 thumb_bit);
8531 break;
8532
8533 case elfcpp::R_ARM_THM_JUMP6:
8534 reloc_status =
8535 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
8536 break;
8537
8538 case elfcpp::R_ARM_THM_JUMP8:
8539 reloc_status =
8540 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
8541 break;
8542
8543 case elfcpp::R_ARM_THM_JUMP11:
8544 reloc_status =
8545 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
8546 break;
8547
8548 case elfcpp::R_ARM_PREL31:
8549 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
8550 address, thumb_bit);
8551 break;
8552
8553 case elfcpp::R_ARM_V4BX:
8554 if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
8555 {
8556 const bool is_v4bx_interworking =
8557 (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
8558 reloc_status =
8559 Arm_relocate_functions::v4bx(relinfo, view, object, address,
8560 is_v4bx_interworking);
8561 }
8562 break;
8563
8564 case elfcpp::R_ARM_THM_PC8:
8565 reloc_status =
8566 Arm_relocate_functions::thm_pc8(view, object, psymval, address);
8567 break;
8568
8569 case elfcpp::R_ARM_THM_PC12:
8570 reloc_status =
8571 Arm_relocate_functions::thm_pc12(view, object, psymval, address);
8572 break;
8573
8574 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8575 reloc_status =
8576 Arm_relocate_functions::thm_alu11(view, object, psymval, address,
8577 thumb_bit);
8578 break;
8579
8580 case elfcpp::R_ARM_ALU_PC_G0_NC:
8581 case elfcpp::R_ARM_ALU_PC_G0:
8582 case elfcpp::R_ARM_ALU_PC_G1_NC:
8583 case elfcpp::R_ARM_ALU_PC_G1:
8584 case elfcpp::R_ARM_ALU_PC_G2:
8585 case elfcpp::R_ARM_ALU_SB_G0_NC:
8586 case elfcpp::R_ARM_ALU_SB_G0:
8587 case elfcpp::R_ARM_ALU_SB_G1_NC:
8588 case elfcpp::R_ARM_ALU_SB_G1:
8589 case elfcpp::R_ARM_ALU_SB_G2:
8590 reloc_status =
8591 Arm_relocate_functions::arm_grp_alu(view, object, psymval,
8592 reloc_property->group_index(),
8593 relative_address_base,
8594 thumb_bit, check_overflow);
8595 break;
8596
8597 case elfcpp::R_ARM_LDR_PC_G0:
8598 case elfcpp::R_ARM_LDR_PC_G1:
8599 case elfcpp::R_ARM_LDR_PC_G2:
8600 case elfcpp::R_ARM_LDR_SB_G0:
8601 case elfcpp::R_ARM_LDR_SB_G1:
8602 case elfcpp::R_ARM_LDR_SB_G2:
8603 reloc_status =
8604 Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
8605 reloc_property->group_index(),
8606 relative_address_base);
8607 break;
8608
8609 case elfcpp::R_ARM_LDRS_PC_G0:
8610 case elfcpp::R_ARM_LDRS_PC_G1:
8611 case elfcpp::R_ARM_LDRS_PC_G2:
8612 case elfcpp::R_ARM_LDRS_SB_G0:
8613 case elfcpp::R_ARM_LDRS_SB_G1:
8614 case elfcpp::R_ARM_LDRS_SB_G2:
8615 reloc_status =
8616 Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
8617 reloc_property->group_index(),
8618 relative_address_base);
8619 break;
8620
8621 case elfcpp::R_ARM_LDC_PC_G0:
8622 case elfcpp::R_ARM_LDC_PC_G1:
8623 case elfcpp::R_ARM_LDC_PC_G2:
8624 case elfcpp::R_ARM_LDC_SB_G0:
8625 case elfcpp::R_ARM_LDC_SB_G1:
8626 case elfcpp::R_ARM_LDC_SB_G2:
8627 reloc_status =
8628 Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
8629 reloc_property->group_index(),
8630 relative_address_base);
8631 break;
8632
8633 // These are initial tls relocs, which are expected when
8634 // linking.
8635 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8636 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8637 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8638 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8639 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8640 reloc_status =
8641 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
8642 view, address, view_size);
8643 break;
8644
8645 default:
8646 gold_unreachable();
8647 }
8648
8649 // Report any errors.
8650 switch (reloc_status)
8651 {
8652 case Arm_relocate_functions::STATUS_OKAY:
8653 break;
8654 case Arm_relocate_functions::STATUS_OVERFLOW:
8655 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8656 _("relocation overflow in %s"),
8657 reloc_property->name().c_str());
8658 break;
8659 case Arm_relocate_functions::STATUS_BAD_RELOC:
8660 gold_error_at_location(
8661 relinfo,
8662 relnum,
8663 rel.get_r_offset(),
8664 _("unexpected opcode while processing relocation %s"),
8665 reloc_property->name().c_str());
8666 break;
8667 default:
8668 gold_unreachable();
8669 }
8670
8671 return true;
8672 }
8673
8674 // Perform a TLS relocation.
8675
8676 template<bool big_endian>
8677 inline typename Arm_relocate_functions<big_endian>::Status
8678 Target_arm<big_endian>::Relocate::relocate_tls(
8679 const Relocate_info<32, big_endian>* relinfo,
8680 Target_arm<big_endian>* target,
8681 size_t relnum,
8682 const elfcpp::Rel<32, big_endian>& rel,
8683 unsigned int r_type,
8684 const Sized_symbol<32>* gsym,
8685 const Symbol_value<32>* psymval,
8686 unsigned char* view,
8687 elfcpp::Elf_types<32>::Elf_Addr address,
8688 section_size_type /*view_size*/ )
8689 {
8690 typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
8691 typedef Relocate_functions<32, big_endian> RelocFuncs;
8692 Output_segment* tls_segment = relinfo->layout->tls_segment();
8693
8694 const Sized_relobj<32, big_endian>* object = relinfo->object;
8695
8696 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
8697
8698 const bool is_final = (gsym == NULL
8699 ? !parameters->options().shared()
8700 : gsym->final_value_is_known());
8701 const tls::Tls_optimization optimized_type
8702 = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
8703 switch (r_type)
8704 {
8705 case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
8706 {
8707 unsigned int got_type = GOT_TYPE_TLS_PAIR;
8708 unsigned int got_offset;
8709 if (gsym != NULL)
8710 {
8711 gold_assert(gsym->has_got_offset(got_type));
8712 got_offset = gsym->got_offset(got_type) - target->got_size();
8713 }
8714 else
8715 {
8716 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8717 gold_assert(object->local_has_got_offset(r_sym, got_type));
8718 got_offset = (object->local_got_offset(r_sym, got_type)
8719 - target->got_size());
8720 }
8721 if (optimized_type == tls::TLSOPT_NONE)
8722 {
8723 Arm_address got_entry =
8724 target->got_plt_section()->address() + got_offset;
8725
8726 // Relocate the field with the PC relative offset of the pair of
8727 // GOT entries.
8728 RelocFuncs::pcrel32(view, got_entry, address);
8729 return ArmRelocFuncs::STATUS_OKAY;
8730 }
8731 }
8732 break;
8733
8734 case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
8735 if (optimized_type == tls::TLSOPT_NONE)
8736 {
8737 // Relocate the field with the offset of the GOT entry for
8738 // the module index.
8739 unsigned int got_offset;
8740 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
8741 - target->got_size());
8742 Arm_address got_entry =
8743 target->got_plt_section()->address() + got_offset;
8744
8745 // Relocate the field with the PC relative offset of the pair of
8746 // GOT entries.
8747 RelocFuncs::pcrel32(view, got_entry, address);
8748 return ArmRelocFuncs::STATUS_OKAY;
8749 }
8750 break;
8751
8752 case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
8753 RelocFuncs::rel32(view, value);
8754 return ArmRelocFuncs::STATUS_OKAY;
8755
8756 case elfcpp::R_ARM_TLS_IE32: // Initial-exec
8757 if (optimized_type == tls::TLSOPT_NONE)
8758 {
8759 // Relocate the field with the offset of the GOT entry for
8760 // the tp-relative offset of the symbol.
8761 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
8762 unsigned int got_offset;
8763 if (gsym != NULL)
8764 {
8765 gold_assert(gsym->has_got_offset(got_type));
8766 got_offset = gsym->got_offset(got_type);
8767 }
8768 else
8769 {
8770 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8771 gold_assert(object->local_has_got_offset(r_sym, got_type));
8772 got_offset = object->local_got_offset(r_sym, got_type);
8773 }
8774
8775 // All GOT offsets are relative to the end of the GOT.
8776 got_offset -= target->got_size();
8777
8778 Arm_address got_entry =
8779 target->got_plt_section()->address() + got_offset;
8780
8781 // Relocate the field with the PC relative offset of the GOT entry.
8782 RelocFuncs::pcrel32(view, got_entry, address);
8783 return ArmRelocFuncs::STATUS_OKAY;
8784 }
8785 break;
8786
8787 case elfcpp::R_ARM_TLS_LE32: // Local-exec
8788 // If we're creating a shared library, a dynamic relocation will
8789 // have been created for this location, so do not apply it now.
8790 if (!parameters->options().shared())
8791 {
8792 gold_assert(tls_segment != NULL);
8793
8794 // $tp points to the TCB, which is followed by the TLS, so we
8795 // need to add TCB size to the offset.
8796 Arm_address aligned_tcb_size =
8797 align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
8798 RelocFuncs::rel32(view, value + aligned_tcb_size);
8799
8800 }
8801 return ArmRelocFuncs::STATUS_OKAY;
8802
8803 default:
8804 gold_unreachable();
8805 }
8806
8807 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8808 _("unsupported reloc %u"),
8809 r_type);
8810 return ArmRelocFuncs::STATUS_BAD_RELOC;
8811 }
8812
8813 // Relocate section data.
8814
8815 template<bool big_endian>
8816 void
8817 Target_arm<big_endian>::relocate_section(
8818 const Relocate_info<32, big_endian>* relinfo,
8819 unsigned int sh_type,
8820 const unsigned char* prelocs,
8821 size_t reloc_count,
8822 Output_section* output_section,
8823 bool needs_special_offset_handling,
8824 unsigned char* view,
8825 Arm_address address,
8826 section_size_type view_size,
8827 const Reloc_symbol_changes* reloc_symbol_changes)
8828 {
8829 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
8830 gold_assert(sh_type == elfcpp::SHT_REL);
8831
8832 // See if we are relocating a relaxed input section. If so, the view
8833 // covers the whole output section and we need to adjust accordingly.
8834 if (needs_special_offset_handling)
8835 {
8836 const Output_relaxed_input_section* poris =
8837 output_section->find_relaxed_input_section(relinfo->object,
8838 relinfo->data_shndx);
8839 if (poris != NULL)
8840 {
8841 Arm_address section_address = poris->address();
8842 section_size_type section_size = poris->data_size();
8843
8844 gold_assert((section_address >= address)
8845 && ((section_address + section_size)
8846 <= (address + view_size)));
8847
8848 off_t offset = section_address - address;
8849 view += offset;
8850 address += offset;
8851 view_size = section_size;
8852 }
8853 }
8854
8855 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
8856 Arm_relocate>(
8857 relinfo,
8858 this,
8859 prelocs,
8860 reloc_count,
8861 output_section,
8862 needs_special_offset_handling,
8863 view,
8864 address,
8865 view_size,
8866 reloc_symbol_changes);
8867 }
8868
8869 // Return the size of a relocation while scanning during a relocatable
8870 // link.
8871
8872 template<bool big_endian>
8873 unsigned int
8874 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
8875 unsigned int r_type,
8876 Relobj* object)
8877 {
8878 r_type = get_real_reloc_type(r_type);
8879 const Arm_reloc_property* arp =
8880 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8881 if (arp != NULL)
8882 return arp->size();
8883 else
8884 {
8885 std::string reloc_name =
8886 arm_reloc_property_table->reloc_name_in_error_message(r_type);
8887 gold_error(_("%s: unexpected %s in object file"),
8888 object->name().c_str(), reloc_name.c_str());
8889 return 0;
8890 }
8891 }
8892
8893 // Scan the relocs during a relocatable link.
8894
8895 template<bool big_endian>
8896 void
8897 Target_arm<big_endian>::scan_relocatable_relocs(
8898 Symbol_table* symtab,
8899 Layout* layout,
8900 Sized_relobj<32, big_endian>* object,
8901 unsigned int data_shndx,
8902 unsigned int sh_type,
8903 const unsigned char* prelocs,
8904 size_t reloc_count,
8905 Output_section* output_section,
8906 bool needs_special_offset_handling,
8907 size_t local_symbol_count,
8908 const unsigned char* plocal_symbols,
8909 Relocatable_relocs* rr)
8910 {
8911 gold_assert(sh_type == elfcpp::SHT_REL);
8912
8913 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
8914 Relocatable_size_for_reloc> Scan_relocatable_relocs;
8915
8916 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
8917 Scan_relocatable_relocs>(
8918 symtab,
8919 layout,
8920 object,
8921 data_shndx,
8922 prelocs,
8923 reloc_count,
8924 output_section,
8925 needs_special_offset_handling,
8926 local_symbol_count,
8927 plocal_symbols,
8928 rr);
8929 }
8930
8931 // Relocate a section during a relocatable link.
8932
8933 template<bool big_endian>
8934 void
8935 Target_arm<big_endian>::relocate_for_relocatable(
8936 const Relocate_info<32, big_endian>* relinfo,
8937 unsigned int sh_type,
8938 const unsigned char* prelocs,
8939 size_t reloc_count,
8940 Output_section* output_section,
8941 off_t offset_in_output_section,
8942 const Relocatable_relocs* rr,
8943 unsigned char* view,
8944 Arm_address view_address,
8945 section_size_type view_size,
8946 unsigned char* reloc_view,
8947 section_size_type reloc_view_size)
8948 {
8949 gold_assert(sh_type == elfcpp::SHT_REL);
8950
8951 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
8952 relinfo,
8953 prelocs,
8954 reloc_count,
8955 output_section,
8956 offset_in_output_section,
8957 rr,
8958 view,
8959 view_address,
8960 view_size,
8961 reloc_view,
8962 reloc_view_size);
8963 }
8964
8965 // Return the value to use for a dynamic symbol which requires special
8966 // treatment. This is how we support equality comparisons of function
8967 // pointers across shared library boundaries, as described in the
8968 // processor specific ABI supplement.
8969
8970 template<bool big_endian>
8971 uint64_t
8972 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
8973 {
8974 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
8975 return this->plt_section()->address() + gsym->plt_offset();
8976 }
8977
8978 // Map platform-specific relocs to real relocs
8979 //
8980 template<bool big_endian>
8981 unsigned int
8982 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
8983 {
8984 switch (r_type)
8985 {
8986 case elfcpp::R_ARM_TARGET1:
8987 // This is either R_ARM_ABS32 or R_ARM_REL32;
8988 return elfcpp::R_ARM_ABS32;
8989
8990 case elfcpp::R_ARM_TARGET2:
8991 // This can be any reloc type but ususally is R_ARM_GOT_PREL
8992 return elfcpp::R_ARM_GOT_PREL;
8993
8994 default:
8995 return r_type;
8996 }
8997 }
8998
8999 // Whether if two EABI versions V1 and V2 are compatible.
9000
9001 template<bool big_endian>
9002 bool
9003 Target_arm<big_endian>::are_eabi_versions_compatible(
9004 elfcpp::Elf_Word v1,
9005 elfcpp::Elf_Word v2)
9006 {
9007 // v4 and v5 are the same spec before and after it was released,
9008 // so allow mixing them.
9009 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
9010 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
9011 return true;
9012
9013 return v1 == v2;
9014 }
9015
9016 // Combine FLAGS from an input object called NAME and the processor-specific
9017 // flags in the ELF header of the output. Much of this is adapted from the
9018 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
9019 // in bfd/elf32-arm.c.
9020
9021 template<bool big_endian>
9022 void
9023 Target_arm<big_endian>::merge_processor_specific_flags(
9024 const std::string& name,
9025 elfcpp::Elf_Word flags)
9026 {
9027 if (this->are_processor_specific_flags_set())
9028 {
9029 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
9030
9031 // Nothing to merge if flags equal to those in output.
9032 if (flags == out_flags)
9033 return;
9034
9035 // Complain about various flag mismatches.
9036 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
9037 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
9038 if (!this->are_eabi_versions_compatible(version1, version2)
9039 && parameters->options().warn_mismatch())
9040 gold_error(_("Source object %s has EABI version %d but output has "
9041 "EABI version %d."),
9042 name.c_str(),
9043 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
9044 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
9045 }
9046 else
9047 {
9048 // If the input is the default architecture and had the default
9049 // flags then do not bother setting the flags for the output
9050 // architecture, instead allow future merges to do this. If no
9051 // future merges ever set these flags then they will retain their
9052 // uninitialised values, which surprise surprise, correspond
9053 // to the default values.
9054 if (flags == 0)
9055 return;
9056
9057 // This is the first time, just copy the flags.
9058 // We only copy the EABI version for now.
9059 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
9060 }
9061 }
9062
9063 // Adjust ELF file header.
9064 template<bool big_endian>
9065 void
9066 Target_arm<big_endian>::do_adjust_elf_header(
9067 unsigned char* view,
9068 int len) const
9069 {
9070 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
9071
9072 elfcpp::Ehdr<32, big_endian> ehdr(view);
9073 unsigned char e_ident[elfcpp::EI_NIDENT];
9074 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9075
9076 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
9077 == elfcpp::EF_ARM_EABI_UNKNOWN)
9078 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
9079 else
9080 e_ident[elfcpp::EI_OSABI] = 0;
9081 e_ident[elfcpp::EI_ABIVERSION] = 0;
9082
9083 // FIXME: Do EF_ARM_BE8 adjustment.
9084
9085 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
9086 oehdr.put_e_ident(e_ident);
9087 }
9088
9089 // do_make_elf_object to override the same function in the base class.
9090 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
9091 // to store ARM specific information. Hence we need to have our own
9092 // ELF object creation.
9093
9094 template<bool big_endian>
9095 Object*
9096 Target_arm<big_endian>::do_make_elf_object(
9097 const std::string& name,
9098 Input_file* input_file,
9099 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
9100 {
9101 int et = ehdr.get_e_type();
9102 if (et == elfcpp::ET_REL)
9103 {
9104 Arm_relobj<big_endian>* obj =
9105 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
9106 obj->setup();
9107 return obj;
9108 }
9109 else if (et == elfcpp::ET_DYN)
9110 {
9111 Sized_dynobj<32, big_endian>* obj =
9112 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
9113 obj->setup();
9114 return obj;
9115 }
9116 else
9117 {
9118 gold_error(_("%s: unsupported ELF file type %d"),
9119 name.c_str(), et);
9120 return NULL;
9121 }
9122 }
9123
9124 // Read the architecture from the Tag_also_compatible_with attribute, if any.
9125 // Returns -1 if no architecture could be read.
9126 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
9127
9128 template<bool big_endian>
9129 int
9130 Target_arm<big_endian>::get_secondary_compatible_arch(
9131 const Attributes_section_data* pasd)
9132 {
9133 const Object_attribute *known_attributes =
9134 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
9135
9136 // Note: the tag and its argument below are uleb128 values, though
9137 // currently-defined values fit in one byte for each.
9138 const std::string& sv =
9139 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
9140 if (sv.size() == 2
9141 && sv.data()[0] == elfcpp::Tag_CPU_arch
9142 && (sv.data()[1] & 128) != 128)
9143 return sv.data()[1];
9144
9145 // This tag is "safely ignorable", so don't complain if it looks funny.
9146 return -1;
9147 }
9148
9149 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
9150 // The tag is removed if ARCH is -1.
9151 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
9152
9153 template<bool big_endian>
9154 void
9155 Target_arm<big_endian>::set_secondary_compatible_arch(
9156 Attributes_section_data* pasd,
9157 int arch)
9158 {
9159 Object_attribute *known_attributes =
9160 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
9161
9162 if (arch == -1)
9163 {
9164 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
9165 return;
9166 }
9167
9168 // Note: the tag and its argument below are uleb128 values, though
9169 // currently-defined values fit in one byte for each.
9170 char sv[3];
9171 sv[0] = elfcpp::Tag_CPU_arch;
9172 gold_assert(arch != 0);
9173 sv[1] = arch;
9174 sv[2] = '\0';
9175
9176 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
9177 }
9178
9179 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
9180 // into account.
9181 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
9182
9183 template<bool big_endian>
9184 int
9185 Target_arm<big_endian>::tag_cpu_arch_combine(
9186 const char* name,
9187 int oldtag,
9188 int* secondary_compat_out,
9189 int newtag,
9190 int secondary_compat)
9191 {
9192 #define T(X) elfcpp::TAG_CPU_ARCH_##X
9193 static const int v6t2[] =
9194 {
9195 T(V6T2), // PRE_V4.
9196 T(V6T2), // V4.
9197 T(V6T2), // V4T.
9198 T(V6T2), // V5T.
9199 T(V6T2), // V5TE.
9200 T(V6T2), // V5TEJ.
9201 T(V6T2), // V6.
9202 T(V7), // V6KZ.
9203 T(V6T2) // V6T2.
9204 };
9205 static const int v6k[] =
9206 {
9207 T(V6K), // PRE_V4.
9208 T(V6K), // V4.
9209 T(V6K), // V4T.
9210 T(V6K), // V5T.
9211 T(V6K), // V5TE.
9212 T(V6K), // V5TEJ.
9213 T(V6K), // V6.
9214 T(V6KZ), // V6KZ.
9215 T(V7), // V6T2.
9216 T(V6K) // V6K.
9217 };
9218 static const int v7[] =
9219 {
9220 T(V7), // PRE_V4.
9221 T(V7), // V4.
9222 T(V7), // V4T.
9223 T(V7), // V5T.
9224 T(V7), // V5TE.
9225 T(V7), // V5TEJ.
9226 T(V7), // V6.
9227 T(V7), // V6KZ.
9228 T(V7), // V6T2.
9229 T(V7), // V6K.
9230 T(V7) // V7.
9231 };
9232 static const int v6_m[] =
9233 {
9234 -1, // PRE_V4.
9235 -1, // V4.
9236 T(V6K), // V4T.
9237 T(V6K), // V5T.
9238 T(V6K), // V5TE.
9239 T(V6K), // V5TEJ.
9240 T(V6K), // V6.
9241 T(V6KZ), // V6KZ.
9242 T(V7), // V6T2.
9243 T(V6K), // V6K.
9244 T(V7), // V7.
9245 T(V6_M) // V6_M.
9246 };
9247 static const int v6s_m[] =
9248 {
9249 -1, // PRE_V4.
9250 -1, // V4.
9251 T(V6K), // V4T.
9252 T(V6K), // V5T.
9253 T(V6K), // V5TE.
9254 T(V6K), // V5TEJ.
9255 T(V6K), // V6.
9256 T(V6KZ), // V6KZ.
9257 T(V7), // V6T2.
9258 T(V6K), // V6K.
9259 T(V7), // V7.
9260 T(V6S_M), // V6_M.
9261 T(V6S_M) // V6S_M.
9262 };
9263 static const int v7e_m[] =
9264 {
9265 -1, // PRE_V4.
9266 -1, // V4.
9267 T(V7E_M), // V4T.
9268 T(V7E_M), // V5T.
9269 T(V7E_M), // V5TE.
9270 T(V7E_M), // V5TEJ.
9271 T(V7E_M), // V6.
9272 T(V7E_M), // V6KZ.
9273 T(V7E_M), // V6T2.
9274 T(V7E_M), // V6K.
9275 T(V7E_M), // V7.
9276 T(V7E_M), // V6_M.
9277 T(V7E_M), // V6S_M.
9278 T(V7E_M) // V7E_M.
9279 };
9280 static const int v4t_plus_v6_m[] =
9281 {
9282 -1, // PRE_V4.
9283 -1, // V4.
9284 T(V4T), // V4T.
9285 T(V5T), // V5T.
9286 T(V5TE), // V5TE.
9287 T(V5TEJ), // V5TEJ.
9288 T(V6), // V6.
9289 T(V6KZ), // V6KZ.
9290 T(V6T2), // V6T2.
9291 T(V6K), // V6K.
9292 T(V7), // V7.
9293 T(V6_M), // V6_M.
9294 T(V6S_M), // V6S_M.
9295 T(V7E_M), // V7E_M.
9296 T(V4T_PLUS_V6_M) // V4T plus V6_M.
9297 };
9298 static const int *comb[] =
9299 {
9300 v6t2,
9301 v6k,
9302 v7,
9303 v6_m,
9304 v6s_m,
9305 v7e_m,
9306 // Pseudo-architecture.
9307 v4t_plus_v6_m
9308 };
9309
9310 // Check we've not got a higher architecture than we know about.
9311
9312 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
9313 {
9314 gold_error(_("%s: unknown CPU architecture"), name);
9315 return -1;
9316 }
9317
9318 // Override old tag if we have a Tag_also_compatible_with on the output.
9319
9320 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
9321 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
9322 oldtag = T(V4T_PLUS_V6_M);
9323
9324 // And override the new tag if we have a Tag_also_compatible_with on the
9325 // input.
9326
9327 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
9328 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
9329 newtag = T(V4T_PLUS_V6_M);
9330
9331 // Architectures before V6KZ add features monotonically.
9332 int tagh = std::max(oldtag, newtag);
9333 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
9334 return tagh;
9335
9336 int tagl = std::min(oldtag, newtag);
9337 int result = comb[tagh - T(V6T2)][tagl];
9338
9339 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
9340 // as the canonical version.
9341 if (result == T(V4T_PLUS_V6_M))
9342 {
9343 result = T(V4T);
9344 *secondary_compat_out = T(V6_M);
9345 }
9346 else
9347 *secondary_compat_out = -1;
9348
9349 if (result == -1)
9350 {
9351 gold_error(_("%s: conflicting CPU architectures %d/%d"),
9352 name, oldtag, newtag);
9353 return -1;
9354 }
9355
9356 return result;
9357 #undef T
9358 }
9359
9360 // Helper to print AEABI enum tag value.
9361
9362 template<bool big_endian>
9363 std::string
9364 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
9365 {
9366 static const char *aeabi_enum_names[] =
9367 { "", "variable-size", "32-bit", "" };
9368 const size_t aeabi_enum_names_size =
9369 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
9370
9371 if (value < aeabi_enum_names_size)
9372 return std::string(aeabi_enum_names[value]);
9373 else
9374 {
9375 char buffer[100];
9376 sprintf(buffer, "<unknown value %u>", value);
9377 return std::string(buffer);
9378 }
9379 }
9380
9381 // Return the string value to store in TAG_CPU_name.
9382
9383 template<bool big_endian>
9384 std::string
9385 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
9386 {
9387 static const char *name_table[] = {
9388 // These aren't real CPU names, but we can't guess
9389 // that from the architecture version alone.
9390 "Pre v4",
9391 "ARM v4",
9392 "ARM v4T",
9393 "ARM v5T",
9394 "ARM v5TE",
9395 "ARM v5TEJ",
9396 "ARM v6",
9397 "ARM v6KZ",
9398 "ARM v6T2",
9399 "ARM v6K",
9400 "ARM v7",
9401 "ARM v6-M",
9402 "ARM v6S-M",
9403 "ARM v7E-M"
9404 };
9405 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
9406
9407 if (value < name_table_size)
9408 return std::string(name_table[value]);
9409 else
9410 {
9411 char buffer[100];
9412 sprintf(buffer, "<unknown CPU value %u>", value);
9413 return std::string(buffer);
9414 }
9415 }
9416
9417 // Merge object attributes from input file called NAME with those of the
9418 // output. The input object attributes are in the object pointed by PASD.
9419
9420 template<bool big_endian>
9421 void
9422 Target_arm<big_endian>::merge_object_attributes(
9423 const char* name,
9424 const Attributes_section_data* pasd)
9425 {
9426 // Return if there is no attributes section data.
9427 if (pasd == NULL)
9428 return;
9429
9430 // If output has no object attributes, just copy.
9431 const int vendor = Object_attribute::OBJ_ATTR_PROC;
9432 if (this->attributes_section_data_ == NULL)
9433 {
9434 this->attributes_section_data_ = new Attributes_section_data(*pasd);
9435 Object_attribute* out_attr =
9436 this->attributes_section_data_->known_attributes(vendor);
9437
9438 // We do not output objects with Tag_MPextension_use_legacy - we move
9439 // the attribute's value to Tag_MPextension_use. */
9440 if (out_attr[elfcpp::Tag_MPextension_use_legacy].int_value() != 0)
9441 {
9442 if (out_attr[elfcpp::Tag_MPextension_use].int_value() != 0
9443 && out_attr[elfcpp::Tag_MPextension_use_legacy].int_value()
9444 != out_attr[elfcpp::Tag_MPextension_use].int_value())
9445 {
9446 gold_error(_("%s has both the current and legacy "
9447 "Tag_MPextension_use attributes"),
9448 name);
9449 }
9450
9451 out_attr[elfcpp::Tag_MPextension_use] =
9452 out_attr[elfcpp::Tag_MPextension_use_legacy];
9453 out_attr[elfcpp::Tag_MPextension_use_legacy].set_type(0);
9454 out_attr[elfcpp::Tag_MPextension_use_legacy].set_int_value(0);
9455 }
9456
9457 return;
9458 }
9459
9460 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9461 Object_attribute* out_attr =
9462 this->attributes_section_data_->known_attributes(vendor);
9463
9464 // This needs to happen before Tag_ABI_FP_number_model is merged. */
9465 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
9466 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
9467 {
9468 // Ignore mismatches if the object doesn't use floating point. */
9469 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
9470 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
9471 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
9472 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0
9473 && parameters->options().warn_mismatch())
9474 gold_error(_("%s uses VFP register arguments, output does not"),
9475 name);
9476 }
9477
9478 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
9479 {
9480 // Merge this attribute with existing attributes.
9481 switch (i)
9482 {
9483 case elfcpp::Tag_CPU_raw_name:
9484 case elfcpp::Tag_CPU_name:
9485 // These are merged after Tag_CPU_arch.
9486 break;
9487
9488 case elfcpp::Tag_ABI_optimization_goals:
9489 case elfcpp::Tag_ABI_FP_optimization_goals:
9490 // Use the first value seen.
9491 break;
9492
9493 case elfcpp::Tag_CPU_arch:
9494 {
9495 unsigned int saved_out_attr = out_attr->int_value();
9496 // Merge Tag_CPU_arch and Tag_also_compatible_with.
9497 int secondary_compat =
9498 this->get_secondary_compatible_arch(pasd);
9499 int secondary_compat_out =
9500 this->get_secondary_compatible_arch(
9501 this->attributes_section_data_);
9502 out_attr[i].set_int_value(
9503 tag_cpu_arch_combine(name, out_attr[i].int_value(),
9504 &secondary_compat_out,
9505 in_attr[i].int_value(),
9506 secondary_compat));
9507 this->set_secondary_compatible_arch(this->attributes_section_data_,
9508 secondary_compat_out);
9509
9510 // Merge Tag_CPU_name and Tag_CPU_raw_name.
9511 if (out_attr[i].int_value() == saved_out_attr)
9512 ; // Leave the names alone.
9513 else if (out_attr[i].int_value() == in_attr[i].int_value())
9514 {
9515 // The output architecture has been changed to match the
9516 // input architecture. Use the input names.
9517 out_attr[elfcpp::Tag_CPU_name].set_string_value(
9518 in_attr[elfcpp::Tag_CPU_name].string_value());
9519 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
9520 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
9521 }
9522 else
9523 {
9524 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
9525 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
9526 }
9527
9528 // If we still don't have a value for Tag_CPU_name,
9529 // make one up now. Tag_CPU_raw_name remains blank.
9530 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
9531 {
9532 const std::string cpu_name =
9533 this->tag_cpu_name_value(out_attr[i].int_value());
9534 // FIXME: If we see an unknown CPU, this will be set
9535 // to "<unknown CPU n>", where n is the attribute value.
9536 // This is different from BFD, which leaves the name alone.
9537 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
9538 }
9539 }
9540 break;
9541
9542 case elfcpp::Tag_ARM_ISA_use:
9543 case elfcpp::Tag_THUMB_ISA_use:
9544 case elfcpp::Tag_WMMX_arch:
9545 case elfcpp::Tag_Advanced_SIMD_arch:
9546 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
9547 case elfcpp::Tag_ABI_FP_rounding:
9548 case elfcpp::Tag_ABI_FP_exceptions:
9549 case elfcpp::Tag_ABI_FP_user_exceptions:
9550 case elfcpp::Tag_ABI_FP_number_model:
9551 case elfcpp::Tag_VFP_HP_extension:
9552 case elfcpp::Tag_CPU_unaligned_access:
9553 case elfcpp::Tag_T2EE_use:
9554 case elfcpp::Tag_Virtualization_use:
9555 case elfcpp::Tag_MPextension_use:
9556 // Use the largest value specified.
9557 if (in_attr[i].int_value() > out_attr[i].int_value())
9558 out_attr[i].set_int_value(in_attr[i].int_value());
9559 break;
9560
9561 case elfcpp::Tag_ABI_align8_preserved:
9562 case elfcpp::Tag_ABI_PCS_RO_data:
9563 // Use the smallest value specified.
9564 if (in_attr[i].int_value() < out_attr[i].int_value())
9565 out_attr[i].set_int_value(in_attr[i].int_value());
9566 break;
9567
9568 case elfcpp::Tag_ABI_align8_needed:
9569 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
9570 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
9571 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
9572 == 0)))
9573 {
9574 // This error message should be enabled once all non-conformant
9575 // binaries in the toolchain have had the attributes set
9576 // properly.
9577 // gold_error(_("output 8-byte data alignment conflicts with %s"),
9578 // name);
9579 }
9580 // Fall through.
9581 case elfcpp::Tag_ABI_FP_denormal:
9582 case elfcpp::Tag_ABI_PCS_GOT_use:
9583 {
9584 // These tags have 0 = don't care, 1 = strong requirement,
9585 // 2 = weak requirement.
9586 static const int order_021[3] = {0, 2, 1};
9587
9588 // Use the "greatest" from the sequence 0, 2, 1, or the largest
9589 // value if greater than 2 (for future-proofing).
9590 if ((in_attr[i].int_value() > 2
9591 && in_attr[i].int_value() > out_attr[i].int_value())
9592 || (in_attr[i].int_value() <= 2
9593 && out_attr[i].int_value() <= 2
9594 && (order_021[in_attr[i].int_value()]
9595 > order_021[out_attr[i].int_value()])))
9596 out_attr[i].set_int_value(in_attr[i].int_value());
9597 }
9598 break;
9599
9600 case elfcpp::Tag_CPU_arch_profile:
9601 if (out_attr[i].int_value() != in_attr[i].int_value())
9602 {
9603 // 0 will merge with anything.
9604 // 'A' and 'S' merge to 'A'.
9605 // 'R' and 'S' merge to 'R'.
9606 // 'M' and 'A|R|S' is an error.
9607 if (out_attr[i].int_value() == 0
9608 || (out_attr[i].int_value() == 'S'
9609 && (in_attr[i].int_value() == 'A'
9610 || in_attr[i].int_value() == 'R')))
9611 out_attr[i].set_int_value(in_attr[i].int_value());
9612 else if (in_attr[i].int_value() == 0
9613 || (in_attr[i].int_value() == 'S'
9614 && (out_attr[i].int_value() == 'A'
9615 || out_attr[i].int_value() == 'R')))
9616 ; // Do nothing.
9617 else if (parameters->options().warn_mismatch())
9618 {
9619 gold_error
9620 (_("conflicting architecture profiles %c/%c"),
9621 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
9622 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
9623 }
9624 }
9625 break;
9626 case elfcpp::Tag_VFP_arch:
9627 {
9628 static const struct
9629 {
9630 int ver;
9631 int regs;
9632 } vfp_versions[7] =
9633 {
9634 {0, 0},
9635 {1, 16},
9636 {2, 16},
9637 {3, 32},
9638 {3, 16},
9639 {4, 32},
9640 {4, 16}
9641 };
9642
9643 // Values greater than 6 aren't defined, so just pick the
9644 // biggest.
9645 if (in_attr[i].int_value() > 6
9646 && in_attr[i].int_value() > out_attr[i].int_value())
9647 {
9648 *out_attr = *in_attr;
9649 break;
9650 }
9651 // The output uses the superset of input features
9652 // (ISA version) and registers.
9653 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
9654 vfp_versions[out_attr[i].int_value()].ver);
9655 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
9656 vfp_versions[out_attr[i].int_value()].regs);
9657 // This assumes all possible supersets are also a valid
9658 // options.
9659 int newval;
9660 for (newval = 6; newval > 0; newval--)
9661 {
9662 if (regs == vfp_versions[newval].regs
9663 && ver == vfp_versions[newval].ver)
9664 break;
9665 }
9666 out_attr[i].set_int_value(newval);
9667 }
9668 break;
9669 case elfcpp::Tag_PCS_config:
9670 if (out_attr[i].int_value() == 0)
9671 out_attr[i].set_int_value(in_attr[i].int_value());
9672 else if (in_attr[i].int_value() != 0
9673 && out_attr[i].int_value() != 0
9674 && parameters->options().warn_mismatch())
9675 {
9676 // It's sometimes ok to mix different configs, so this is only
9677 // a warning.
9678 gold_warning(_("%s: conflicting platform configuration"), name);
9679 }
9680 break;
9681 case elfcpp::Tag_ABI_PCS_R9_use:
9682 if (in_attr[i].int_value() != out_attr[i].int_value()
9683 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
9684 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused
9685 && parameters->options().warn_mismatch())
9686 {
9687 gold_error(_("%s: conflicting use of R9"), name);
9688 }
9689 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
9690 out_attr[i].set_int_value(in_attr[i].int_value());
9691 break;
9692 case elfcpp::Tag_ABI_PCS_RW_data:
9693 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
9694 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9695 != elfcpp::AEABI_R9_SB)
9696 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
9697 != elfcpp::AEABI_R9_unused)
9698 && parameters->options().warn_mismatch())
9699 {
9700 gold_error(_("%s: SB relative addressing conflicts with use "
9701 "of R9"),
9702 name);
9703 }
9704 // Use the smallest value specified.
9705 if (in_attr[i].int_value() < out_attr[i].int_value())
9706 out_attr[i].set_int_value(in_attr[i].int_value());
9707 break;
9708 case elfcpp::Tag_ABI_PCS_wchar_t:
9709 // FIXME: Make it possible to turn off this warning.
9710 if (out_attr[i].int_value()
9711 && in_attr[i].int_value()
9712 && out_attr[i].int_value() != in_attr[i].int_value()
9713 && parameters->options().warn_mismatch())
9714 {
9715 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
9716 "use %u-byte wchar_t; use of wchar_t values "
9717 "across objects may fail"),
9718 name, in_attr[i].int_value(),
9719 out_attr[i].int_value());
9720 }
9721 else if (in_attr[i].int_value() && !out_attr[i].int_value())
9722 out_attr[i].set_int_value(in_attr[i].int_value());
9723 break;
9724 case elfcpp::Tag_ABI_enum_size:
9725 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
9726 {
9727 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
9728 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
9729 {
9730 // The existing object is compatible with anything.
9731 // Use whatever requirements the new object has.
9732 out_attr[i].set_int_value(in_attr[i].int_value());
9733 }
9734 // FIXME: Make it possible to turn off this warning.
9735 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
9736 && out_attr[i].int_value() != in_attr[i].int_value()
9737 && parameters->options().warn_mismatch())
9738 {
9739 unsigned int in_value = in_attr[i].int_value();
9740 unsigned int out_value = out_attr[i].int_value();
9741 gold_warning(_("%s uses %s enums yet the output is to use "
9742 "%s enums; use of enum values across objects "
9743 "may fail"),
9744 name,
9745 this->aeabi_enum_name(in_value).c_str(),
9746 this->aeabi_enum_name(out_value).c_str());
9747 }
9748 }
9749 break;
9750 case elfcpp::Tag_ABI_VFP_args:
9751 // Aready done.
9752 break;
9753 case elfcpp::Tag_ABI_WMMX_args:
9754 if (in_attr[i].int_value() != out_attr[i].int_value()
9755 && parameters->options().warn_mismatch())
9756 {
9757 gold_error(_("%s uses iWMMXt register arguments, output does "
9758 "not"),
9759 name);
9760 }
9761 break;
9762 case Object_attribute::Tag_compatibility:
9763 // Merged in target-independent code.
9764 break;
9765 case elfcpp::Tag_ABI_HardFP_use:
9766 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
9767 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
9768 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
9769 out_attr[i].set_int_value(3);
9770 else if (in_attr[i].int_value() > out_attr[i].int_value())
9771 out_attr[i].set_int_value(in_attr[i].int_value());
9772 break;
9773 case elfcpp::Tag_ABI_FP_16bit_format:
9774 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
9775 {
9776 if (in_attr[i].int_value() != out_attr[i].int_value()
9777 && parameters->options().warn_mismatch())
9778 gold_error(_("fp16 format mismatch between %s and output"),
9779 name);
9780 }
9781 if (in_attr[i].int_value() != 0)
9782 out_attr[i].set_int_value(in_attr[i].int_value());
9783 break;
9784
9785 case elfcpp::Tag_DIV_use:
9786 // This tag is set to zero if we can use UDIV and SDIV in Thumb
9787 // mode on a v7-M or v7-R CPU; to one if we can not use UDIV or
9788 // SDIV at all; and to two if we can use UDIV or SDIV on a v7-A
9789 // CPU. We will merge as follows: If the input attribute's value
9790 // is one then the output attribute's value remains unchanged. If
9791 // the input attribute's value is zero or two then if the output
9792 // attribute's value is one the output value is set to the input
9793 // value, otherwise the output value must be the same as the
9794 // inputs. */
9795 if (in_attr[i].int_value() != 1 && out_attr[i].int_value() != 1)
9796 {
9797 if (in_attr[i].int_value() != out_attr[i].int_value())
9798 {
9799 gold_error(_("DIV usage mismatch between %s and output"),
9800 name);
9801 }
9802 }
9803
9804 if (in_attr[i].int_value() != 1)
9805 out_attr[i].set_int_value(in_attr[i].int_value());
9806
9807 break;
9808
9809 case elfcpp::Tag_MPextension_use_legacy:
9810 // We don't output objects with Tag_MPextension_use_legacy - we
9811 // move the value to Tag_MPextension_use.
9812 if (in_attr[i].int_value() != 0
9813 && in_attr[elfcpp::Tag_MPextension_use].int_value() != 0)
9814 {
9815 if (in_attr[elfcpp::Tag_MPextension_use].int_value()
9816 != in_attr[i].int_value())
9817 {
9818 gold_error(_("%s has has both the current and legacy "
9819 "Tag_MPextension_use attributes"),
9820 name);
9821 }
9822 }
9823
9824 if (in_attr[i].int_value()
9825 > out_attr[elfcpp::Tag_MPextension_use].int_value())
9826 out_attr[elfcpp::Tag_MPextension_use] = in_attr[i];
9827
9828 break;
9829
9830 case elfcpp::Tag_nodefaults:
9831 // This tag is set if it exists, but the value is unused (and is
9832 // typically zero). We don't actually need to do anything here -
9833 // the merge happens automatically when the type flags are merged
9834 // below.
9835 break;
9836 case elfcpp::Tag_also_compatible_with:
9837 // Already done in Tag_CPU_arch.
9838 break;
9839 case elfcpp::Tag_conformance:
9840 // Keep the attribute if it matches. Throw it away otherwise.
9841 // No attribute means no claim to conform.
9842 if (in_attr[i].string_value() != out_attr[i].string_value())
9843 out_attr[i].set_string_value("");
9844 break;
9845
9846 default:
9847 {
9848 const char* err_object = NULL;
9849
9850 // The "known_obj_attributes" table does contain some undefined
9851 // attributes. Ensure that there are unused.
9852 if (out_attr[i].int_value() != 0
9853 || out_attr[i].string_value() != "")
9854 err_object = "output";
9855 else if (in_attr[i].int_value() != 0
9856 || in_attr[i].string_value() != "")
9857 err_object = name;
9858
9859 if (err_object != NULL
9860 && parameters->options().warn_mismatch())
9861 {
9862 // Attribute numbers >=64 (mod 128) can be safely ignored.
9863 if ((i & 127) < 64)
9864 gold_error(_("%s: unknown mandatory EABI object attribute "
9865 "%d"),
9866 err_object, i);
9867 else
9868 gold_warning(_("%s: unknown EABI object attribute %d"),
9869 err_object, i);
9870 }
9871
9872 // Only pass on attributes that match in both inputs.
9873 if (!in_attr[i].matches(out_attr[i]))
9874 {
9875 out_attr[i].set_int_value(0);
9876 out_attr[i].set_string_value("");
9877 }
9878 }
9879 }
9880
9881 // If out_attr was copied from in_attr then it won't have a type yet.
9882 if (in_attr[i].type() && !out_attr[i].type())
9883 out_attr[i].set_type(in_attr[i].type());
9884 }
9885
9886 // Merge Tag_compatibility attributes and any common GNU ones.
9887 this->attributes_section_data_->merge(name, pasd);
9888
9889 // Check for any attributes not known on ARM.
9890 typedef Vendor_object_attributes::Other_attributes Other_attributes;
9891 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
9892 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
9893 Other_attributes* out_other_attributes =
9894 this->attributes_section_data_->other_attributes(vendor);
9895 Other_attributes::iterator out_iter = out_other_attributes->begin();
9896
9897 while (in_iter != in_other_attributes->end()
9898 || out_iter != out_other_attributes->end())
9899 {
9900 const char* err_object = NULL;
9901 int err_tag = 0;
9902
9903 // The tags for each list are in numerical order.
9904 // If the tags are equal, then merge.
9905 if (out_iter != out_other_attributes->end()
9906 && (in_iter == in_other_attributes->end()
9907 || in_iter->first > out_iter->first))
9908 {
9909 // This attribute only exists in output. We can't merge, and we
9910 // don't know what the tag means, so delete it.
9911 err_object = "output";
9912 err_tag = out_iter->first;
9913 int saved_tag = out_iter->first;
9914 delete out_iter->second;
9915 out_other_attributes->erase(out_iter);
9916 out_iter = out_other_attributes->upper_bound(saved_tag);
9917 }
9918 else if (in_iter != in_other_attributes->end()
9919 && (out_iter != out_other_attributes->end()
9920 || in_iter->first < out_iter->first))
9921 {
9922 // This attribute only exists in input. We can't merge, and we
9923 // don't know what the tag means, so ignore it.
9924 err_object = name;
9925 err_tag = in_iter->first;
9926 ++in_iter;
9927 }
9928 else // The tags are equal.
9929 {
9930 // As present, all attributes in the list are unknown, and
9931 // therefore can't be merged meaningfully.
9932 err_object = "output";
9933 err_tag = out_iter->first;
9934
9935 // Only pass on attributes that match in both inputs.
9936 if (!in_iter->second->matches(*(out_iter->second)))
9937 {
9938 // No match. Delete the attribute.
9939 int saved_tag = out_iter->first;
9940 delete out_iter->second;
9941 out_other_attributes->erase(out_iter);
9942 out_iter = out_other_attributes->upper_bound(saved_tag);
9943 }
9944 else
9945 {
9946 // Matched. Keep the attribute and move to the next.
9947 ++out_iter;
9948 ++in_iter;
9949 }
9950 }
9951
9952 if (err_object && parameters->options().warn_mismatch())
9953 {
9954 // Attribute numbers >=64 (mod 128) can be safely ignored. */
9955 if ((err_tag & 127) < 64)
9956 {
9957 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
9958 err_object, err_tag);
9959 }
9960 else
9961 {
9962 gold_warning(_("%s: unknown EABI object attribute %d"),
9963 err_object, err_tag);
9964 }
9965 }
9966 }
9967 }
9968
9969 // Stub-generation methods for Target_arm.
9970
9971 // Make a new Arm_input_section object.
9972
9973 template<bool big_endian>
9974 Arm_input_section<big_endian>*
9975 Target_arm<big_endian>::new_arm_input_section(
9976 Relobj* relobj,
9977 unsigned int shndx)
9978 {
9979 Section_id sid(relobj, shndx);
9980
9981 Arm_input_section<big_endian>* arm_input_section =
9982 new Arm_input_section<big_endian>(relobj, shndx);
9983 arm_input_section->init();
9984
9985 // Register new Arm_input_section in map for look-up.
9986 std::pair<typename Arm_input_section_map::iterator, bool> ins =
9987 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
9988
9989 // Make sure that it we have not created another Arm_input_section
9990 // for this input section already.
9991 gold_assert(ins.second);
9992
9993 return arm_input_section;
9994 }
9995
9996 // Find the Arm_input_section object corresponding to the SHNDX-th input
9997 // section of RELOBJ.
9998
9999 template<bool big_endian>
10000 Arm_input_section<big_endian>*
10001 Target_arm<big_endian>::find_arm_input_section(
10002 Relobj* relobj,
10003 unsigned int shndx) const
10004 {
10005 Section_id sid(relobj, shndx);
10006 typename Arm_input_section_map::const_iterator p =
10007 this->arm_input_section_map_.find(sid);
10008 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
10009 }
10010
10011 // Make a new stub table.
10012
10013 template<bool big_endian>
10014 Stub_table<big_endian>*
10015 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
10016 {
10017 Stub_table<big_endian>* stub_table =
10018 new Stub_table<big_endian>(owner);
10019 this->stub_tables_.push_back(stub_table);
10020
10021 stub_table->set_address(owner->address() + owner->data_size());
10022 stub_table->set_file_offset(owner->offset() + owner->data_size());
10023 stub_table->finalize_data_size();
10024
10025 return stub_table;
10026 }
10027
10028 // Scan a relocation for stub generation.
10029
10030 template<bool big_endian>
10031 void
10032 Target_arm<big_endian>::scan_reloc_for_stub(
10033 const Relocate_info<32, big_endian>* relinfo,
10034 unsigned int r_type,
10035 const Sized_symbol<32>* gsym,
10036 unsigned int r_sym,
10037 const Symbol_value<32>* psymval,
10038 elfcpp::Elf_types<32>::Elf_Swxword addend,
10039 Arm_address address)
10040 {
10041 typedef typename Target_arm<big_endian>::Relocate Relocate;
10042
10043 const Arm_relobj<big_endian>* arm_relobj =
10044 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10045
10046 bool target_is_thumb;
10047 Symbol_value<32> symval;
10048 if (gsym != NULL)
10049 {
10050 // This is a global symbol. Determine if we use PLT and if the
10051 // final target is THUMB.
10052 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
10053 {
10054 // This uses a PLT, change the symbol value.
10055 symval.set_output_value(this->plt_section()->address()
10056 + gsym->plt_offset());
10057 psymval = &symval;
10058 target_is_thumb = false;
10059 }
10060 else if (gsym->is_undefined())
10061 // There is no need to generate a stub symbol is undefined.
10062 return;
10063 else
10064 {
10065 target_is_thumb =
10066 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
10067 || (gsym->type() == elfcpp::STT_FUNC
10068 && !gsym->is_undefined()
10069 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
10070 }
10071 }
10072 else
10073 {
10074 // This is a local symbol. Determine if the final target is THUMB.
10075 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
10076 }
10077
10078 // Strip LSB if this points to a THUMB target.
10079 const Arm_reloc_property* reloc_property =
10080 arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
10081 gold_assert(reloc_property != NULL);
10082 if (target_is_thumb
10083 && reloc_property->uses_thumb_bit()
10084 && ((psymval->value(arm_relobj, 0) & 1) != 0))
10085 {
10086 Arm_address stripped_value =
10087 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
10088 symval.set_output_value(stripped_value);
10089 psymval = &symval;
10090 }
10091
10092 // Get the symbol value.
10093 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
10094
10095 // Owing to pipelining, the PC relative branches below actually skip
10096 // two instructions when the branch offset is 0.
10097 Arm_address destination;
10098 switch (r_type)
10099 {
10100 case elfcpp::R_ARM_CALL:
10101 case elfcpp::R_ARM_JUMP24:
10102 case elfcpp::R_ARM_PLT32:
10103 // ARM branches.
10104 destination = value + addend + 8;
10105 break;
10106 case elfcpp::R_ARM_THM_CALL:
10107 case elfcpp::R_ARM_THM_XPC22:
10108 case elfcpp::R_ARM_THM_JUMP24:
10109 case elfcpp::R_ARM_THM_JUMP19:
10110 // THUMB branches.
10111 destination = value + addend + 4;
10112 break;
10113 default:
10114 gold_unreachable();
10115 }
10116
10117 Reloc_stub* stub = NULL;
10118 Stub_type stub_type =
10119 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
10120 target_is_thumb);
10121 if (stub_type != arm_stub_none)
10122 {
10123 // Try looking up an existing stub from a stub table.
10124 Stub_table<big_endian>* stub_table =
10125 arm_relobj->stub_table(relinfo->data_shndx);
10126 gold_assert(stub_table != NULL);
10127
10128 // Locate stub by destination.
10129 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
10130
10131 // Create a stub if there is not one already
10132 stub = stub_table->find_reloc_stub(stub_key);
10133 if (stub == NULL)
10134 {
10135 // create a new stub and add it to stub table.
10136 stub = this->stub_factory().make_reloc_stub(stub_type);
10137 stub_table->add_reloc_stub(stub, stub_key);
10138 }
10139
10140 // Record the destination address.
10141 stub->set_destination_address(destination
10142 | (target_is_thumb ? 1 : 0));
10143 }
10144
10145 // For Cortex-A8, we need to record a relocation at 4K page boundary.
10146 if (this->fix_cortex_a8_
10147 && (r_type == elfcpp::R_ARM_THM_JUMP24
10148 || r_type == elfcpp::R_ARM_THM_JUMP19
10149 || r_type == elfcpp::R_ARM_THM_CALL
10150 || r_type == elfcpp::R_ARM_THM_XPC22)
10151 && (address & 0xfffU) == 0xffeU)
10152 {
10153 // Found a candidate. Note we haven't checked the destination is
10154 // within 4K here: if we do so (and don't create a record) we can't
10155 // tell that a branch should have been relocated when scanning later.
10156 this->cortex_a8_relocs_info_[address] =
10157 new Cortex_a8_reloc(stub, r_type,
10158 destination | (target_is_thumb ? 1 : 0));
10159 }
10160 }
10161
10162 // This function scans a relocation sections for stub generation.
10163 // The template parameter Relocate must be a class type which provides
10164 // a single function, relocate(), which implements the machine
10165 // specific part of a relocation.
10166
10167 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
10168 // SHT_REL or SHT_RELA.
10169
10170 // PRELOCS points to the relocation data. RELOC_COUNT is the number
10171 // of relocs. OUTPUT_SECTION is the output section.
10172 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
10173 // mapped to output offsets.
10174
10175 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
10176 // VIEW_SIZE is the size. These refer to the input section, unless
10177 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
10178 // the output section.
10179
10180 template<bool big_endian>
10181 template<int sh_type>
10182 void inline
10183 Target_arm<big_endian>::scan_reloc_section_for_stubs(
10184 const Relocate_info<32, big_endian>* relinfo,
10185 const unsigned char* prelocs,
10186 size_t reloc_count,
10187 Output_section* output_section,
10188 bool needs_special_offset_handling,
10189 const unsigned char* view,
10190 elfcpp::Elf_types<32>::Elf_Addr view_address,
10191 section_size_type)
10192 {
10193 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
10194 const int reloc_size =
10195 Reloc_types<sh_type, 32, big_endian>::reloc_size;
10196
10197 Arm_relobj<big_endian>* arm_object =
10198 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10199 unsigned int local_count = arm_object->local_symbol_count();
10200
10201 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
10202
10203 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
10204 {
10205 Reltype reloc(prelocs);
10206
10207 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10208 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
10209 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
10210
10211 r_type = this->get_real_reloc_type(r_type);
10212
10213 // Only a few relocation types need stubs.
10214 if ((r_type != elfcpp::R_ARM_CALL)
10215 && (r_type != elfcpp::R_ARM_JUMP24)
10216 && (r_type != elfcpp::R_ARM_PLT32)
10217 && (r_type != elfcpp::R_ARM_THM_CALL)
10218 && (r_type != elfcpp::R_ARM_THM_XPC22)
10219 && (r_type != elfcpp::R_ARM_THM_JUMP24)
10220 && (r_type != elfcpp::R_ARM_THM_JUMP19)
10221 && (r_type != elfcpp::R_ARM_V4BX))
10222 continue;
10223
10224 section_offset_type offset =
10225 convert_to_section_size_type(reloc.get_r_offset());
10226
10227 if (needs_special_offset_handling)
10228 {
10229 offset = output_section->output_offset(relinfo->object,
10230 relinfo->data_shndx,
10231 offset);
10232 if (offset == -1)
10233 continue;
10234 }
10235
10236 // Create a v4bx stub if --fix-v4bx-interworking is used.
10237 if (r_type == elfcpp::R_ARM_V4BX)
10238 {
10239 if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING)
10240 {
10241 // Get the BX instruction.
10242 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10243 const Valtype* wv =
10244 reinterpret_cast<const Valtype*>(view + offset);
10245 elfcpp::Elf_types<32>::Elf_Swxword insn =
10246 elfcpp::Swap<32, big_endian>::readval(wv);
10247 const uint32_t reg = (insn & 0xf);
10248
10249 if (reg < 0xf)
10250 {
10251 // Try looking up an existing stub from a stub table.
10252 Stub_table<big_endian>* stub_table =
10253 arm_object->stub_table(relinfo->data_shndx);
10254 gold_assert(stub_table != NULL);
10255
10256 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
10257 {
10258 // create a new stub and add it to stub table.
10259 Arm_v4bx_stub* stub =
10260 this->stub_factory().make_arm_v4bx_stub(reg);
10261 gold_assert(stub != NULL);
10262 stub_table->add_arm_v4bx_stub(stub);
10263 }
10264 }
10265 }
10266 continue;
10267 }
10268
10269 // Get the addend.
10270 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
10271 elfcpp::Elf_types<32>::Elf_Swxword addend =
10272 stub_addend_reader(r_type, view + offset, reloc);
10273
10274 const Sized_symbol<32>* sym;
10275
10276 Symbol_value<32> symval;
10277 const Symbol_value<32> *psymval;
10278 if (r_sym < local_count)
10279 {
10280 sym = NULL;
10281 psymval = arm_object->local_symbol(r_sym);
10282
10283 // If the local symbol belongs to a section we are discarding,
10284 // and that section is a debug section, try to find the
10285 // corresponding kept section and map this symbol to its
10286 // counterpart in the kept section. The symbol must not
10287 // correspond to a section we are folding.
10288 bool is_ordinary;
10289 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10290 if (is_ordinary
10291 && shndx != elfcpp::SHN_UNDEF
10292 && !arm_object->is_section_included(shndx)
10293 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
10294 {
10295 if (comdat_behavior == CB_UNDETERMINED)
10296 {
10297 std::string name =
10298 arm_object->section_name(relinfo->data_shndx);
10299 comdat_behavior = get_comdat_behavior(name.c_str());
10300 }
10301 if (comdat_behavior == CB_PRETEND)
10302 {
10303 bool found;
10304 typename elfcpp::Elf_types<32>::Elf_Addr value =
10305 arm_object->map_to_kept_section(shndx, &found);
10306 if (found)
10307 symval.set_output_value(value + psymval->input_value());
10308 else
10309 symval.set_output_value(0);
10310 }
10311 else
10312 {
10313 symval.set_output_value(0);
10314 }
10315 symval.set_no_output_symtab_entry();
10316 psymval = &symval;
10317 }
10318 }
10319 else
10320 {
10321 const Symbol* gsym = arm_object->global_symbol(r_sym);
10322 gold_assert(gsym != NULL);
10323 if (gsym->is_forwarder())
10324 gsym = relinfo->symtab->resolve_forwards(gsym);
10325
10326 sym = static_cast<const Sized_symbol<32>*>(gsym);
10327 if (sym->has_symtab_index())
10328 symval.set_output_symtab_index(sym->symtab_index());
10329 else
10330 symval.set_no_output_symtab_entry();
10331
10332 // We need to compute the would-be final value of this global
10333 // symbol.
10334 const Symbol_table* symtab = relinfo->symtab;
10335 const Sized_symbol<32>* sized_symbol =
10336 symtab->get_sized_symbol<32>(gsym);
10337 Symbol_table::Compute_final_value_status status;
10338 Arm_address value =
10339 symtab->compute_final_value<32>(sized_symbol, &status);
10340
10341 // Skip this if the symbol has not output section.
10342 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
10343 continue;
10344
10345 symval.set_output_value(value);
10346 psymval = &symval;
10347 }
10348
10349 // If symbol is a section symbol, we don't know the actual type of
10350 // destination. Give up.
10351 if (psymval->is_section_symbol())
10352 continue;
10353
10354 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
10355 addend, view_address + offset);
10356 }
10357 }
10358
10359 // Scan an input section for stub generation.
10360
10361 template<bool big_endian>
10362 void
10363 Target_arm<big_endian>::scan_section_for_stubs(
10364 const Relocate_info<32, big_endian>* relinfo,
10365 unsigned int sh_type,
10366 const unsigned char* prelocs,
10367 size_t reloc_count,
10368 Output_section* output_section,
10369 bool needs_special_offset_handling,
10370 const unsigned char* view,
10371 Arm_address view_address,
10372 section_size_type view_size)
10373 {
10374 if (sh_type == elfcpp::SHT_REL)
10375 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
10376 relinfo,
10377 prelocs,
10378 reloc_count,
10379 output_section,
10380 needs_special_offset_handling,
10381 view,
10382 view_address,
10383 view_size);
10384 else if (sh_type == elfcpp::SHT_RELA)
10385 // We do not support RELA type relocations yet. This is provided for
10386 // completeness.
10387 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
10388 relinfo,
10389 prelocs,
10390 reloc_count,
10391 output_section,
10392 needs_special_offset_handling,
10393 view,
10394 view_address,
10395 view_size);
10396 else
10397 gold_unreachable();
10398 }
10399
10400 // Group input sections for stub generation.
10401 //
10402 // We goup input sections in an output sections so that the total size,
10403 // including any padding space due to alignment is smaller than GROUP_SIZE
10404 // unless the only input section in group is bigger than GROUP_SIZE already.
10405 // Then an ARM stub table is created to follow the last input section
10406 // in group. For each group an ARM stub table is created an is placed
10407 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
10408 // extend the group after the stub table.
10409
10410 template<bool big_endian>
10411 void
10412 Target_arm<big_endian>::group_sections(
10413 Layout* layout,
10414 section_size_type group_size,
10415 bool stubs_always_after_branch)
10416 {
10417 // Group input sections and insert stub table
10418 Layout::Section_list section_list;
10419 layout->get_allocated_sections(&section_list);
10420 for (Layout::Section_list::const_iterator p = section_list.begin();
10421 p != section_list.end();
10422 ++p)
10423 {
10424 Arm_output_section<big_endian>* output_section =
10425 Arm_output_section<big_endian>::as_arm_output_section(*p);
10426 output_section->group_sections(group_size, stubs_always_after_branch,
10427 this);
10428 }
10429 }
10430
10431 // Relaxation hook. This is where we do stub generation.
10432
10433 template<bool big_endian>
10434 bool
10435 Target_arm<big_endian>::do_relax(
10436 int pass,
10437 const Input_objects* input_objects,
10438 Symbol_table* symtab,
10439 Layout* layout)
10440 {
10441 // No need to generate stubs if this is a relocatable link.
10442 gold_assert(!parameters->options().relocatable());
10443
10444 // If this is the first pass, we need to group input sections into
10445 // stub groups.
10446 bool done_exidx_fixup = false;
10447 if (pass == 1)
10448 {
10449 // Determine the stub group size. The group size is the absolute
10450 // value of the parameter --stub-group-size. If --stub-group-size
10451 // is passed a negative value, we restict stubs to be always after
10452 // the stubbed branches.
10453 int32_t stub_group_size_param =
10454 parameters->options().stub_group_size();
10455 bool stubs_always_after_branch = stub_group_size_param < 0;
10456 section_size_type stub_group_size = abs(stub_group_size_param);
10457
10458 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
10459 // page as the first half of a 32-bit branch straddling two 4K pages.
10460 // This is a crude way of enforcing that.
10461 if (this->fix_cortex_a8_)
10462 stubs_always_after_branch = true;
10463
10464 if (stub_group_size == 1)
10465 {
10466 // Default value.
10467 // Thumb branch range is +-4MB has to be used as the default
10468 // maximum size (a given section can contain both ARM and Thumb
10469 // code, so the worst case has to be taken into account). If we are
10470 // fixing cortex-a8 errata, the branch range has to be even smaller,
10471 // since wide conditional branch has a range of +-1MB only.
10472 //
10473 // This value is 24K less than that, which allows for 2025
10474 // 12-byte stubs. If we exceed that, then we will fail to link.
10475 // The user will have to relink with an explicit group size
10476 // option.
10477 if (this->fix_cortex_a8_)
10478 stub_group_size = 1024276;
10479 else
10480 stub_group_size = 4170000;
10481 }
10482
10483 group_sections(layout, stub_group_size, stubs_always_after_branch);
10484
10485 // Also fix .ARM.exidx section coverage.
10486 Output_section* os = layout->find_output_section(".ARM.exidx");
10487 if (os != NULL && os->type() == elfcpp::SHT_ARM_EXIDX)
10488 {
10489 Arm_output_section<big_endian>* exidx_output_section =
10490 Arm_output_section<big_endian>::as_arm_output_section(os);
10491 this->fix_exidx_coverage(layout, exidx_output_section, symtab);
10492 done_exidx_fixup = true;
10493 }
10494 }
10495
10496 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
10497 // beginning of each relaxation pass, just blow away all the stubs.
10498 // Alternatively, we could selectively remove only the stubs and reloc
10499 // information for code sections that have moved since the last pass.
10500 // That would require more book-keeping.
10501 typedef typename Stub_table_list::iterator Stub_table_iterator;
10502 if (this->fix_cortex_a8_)
10503 {
10504 // Clear all Cortex-A8 reloc information.
10505 for (typename Cortex_a8_relocs_info::const_iterator p =
10506 this->cortex_a8_relocs_info_.begin();
10507 p != this->cortex_a8_relocs_info_.end();
10508 ++p)
10509 delete p->second;
10510 this->cortex_a8_relocs_info_.clear();
10511
10512 // Remove all Cortex-A8 stubs.
10513 for (Stub_table_iterator sp = this->stub_tables_.begin();
10514 sp != this->stub_tables_.end();
10515 ++sp)
10516 (*sp)->remove_all_cortex_a8_stubs();
10517 }
10518
10519 // Scan relocs for relocation stubs
10520 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
10521 op != input_objects->relobj_end();
10522 ++op)
10523 {
10524 Arm_relobj<big_endian>* arm_relobj =
10525 Arm_relobj<big_endian>::as_arm_relobj(*op);
10526 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
10527 }
10528
10529 // Check all stub tables to see if any of them have their data sizes
10530 // or addresses alignments changed. These are the only things that
10531 // matter.
10532 bool any_stub_table_changed = false;
10533 Unordered_set<const Output_section*> sections_needing_adjustment;
10534 for (Stub_table_iterator sp = this->stub_tables_.begin();
10535 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
10536 ++sp)
10537 {
10538 if ((*sp)->update_data_size_and_addralign())
10539 {
10540 // Update data size of stub table owner.
10541 Arm_input_section<big_endian>* owner = (*sp)->owner();
10542 uint64_t address = owner->address();
10543 off_t offset = owner->offset();
10544 owner->reset_address_and_file_offset();
10545 owner->set_address_and_file_offset(address, offset);
10546
10547 sections_needing_adjustment.insert(owner->output_section());
10548 any_stub_table_changed = true;
10549 }
10550 }
10551
10552 // Output_section_data::output_section() returns a const pointer but we
10553 // need to update output sections, so we record all output sections needing
10554 // update above and scan the sections here to find out what sections need
10555 // to be updated.
10556 for(Layout::Section_list::const_iterator p = layout->section_list().begin();
10557 p != layout->section_list().end();
10558 ++p)
10559 {
10560 if (sections_needing_adjustment.find(*p)
10561 != sections_needing_adjustment.end())
10562 (*p)->set_section_offsets_need_adjustment();
10563 }
10564
10565 // Stop relaxation if no EXIDX fix-up and no stub table change.
10566 bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
10567
10568 // Finalize the stubs in the last relaxation pass.
10569 if (!continue_relaxation)
10570 {
10571 for (Stub_table_iterator sp = this->stub_tables_.begin();
10572 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
10573 ++sp)
10574 (*sp)->finalize_stubs();
10575
10576 // Update output local symbol counts of objects if necessary.
10577 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
10578 op != input_objects->relobj_end();
10579 ++op)
10580 {
10581 Arm_relobj<big_endian>* arm_relobj =
10582 Arm_relobj<big_endian>::as_arm_relobj(*op);
10583
10584 // Update output local symbol counts. We need to discard local
10585 // symbols defined in parts of input sections that are discarded by
10586 // relaxation.
10587 if (arm_relobj->output_local_symbol_count_needs_update())
10588 arm_relobj->update_output_local_symbol_count();
10589 }
10590 }
10591
10592 return continue_relaxation;
10593 }
10594
10595 // Relocate a stub.
10596
10597 template<bool big_endian>
10598 void
10599 Target_arm<big_endian>::relocate_stub(
10600 Stub* stub,
10601 const Relocate_info<32, big_endian>* relinfo,
10602 Output_section* output_section,
10603 unsigned char* view,
10604 Arm_address address,
10605 section_size_type view_size)
10606 {
10607 Relocate relocate;
10608 const Stub_template* stub_template = stub->stub_template();
10609 for (size_t i = 0; i < stub_template->reloc_count(); i++)
10610 {
10611 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
10612 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
10613
10614 unsigned int r_type = insn->r_type();
10615 section_size_type reloc_offset = stub_template->reloc_offset(i);
10616 section_size_type reloc_size = insn->size();
10617 gold_assert(reloc_offset + reloc_size <= view_size);
10618
10619 // This is the address of the stub destination.
10620 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
10621 Symbol_value<32> symval;
10622 symval.set_output_value(target);
10623
10624 // Synthesize a fake reloc just in case. We don't have a symbol so
10625 // we use 0.
10626 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
10627 memset(reloc_buffer, 0, sizeof(reloc_buffer));
10628 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
10629 reloc_write.put_r_offset(reloc_offset);
10630 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
10631 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
10632
10633 relocate.relocate(relinfo, this, output_section,
10634 this->fake_relnum_for_stubs, rel, r_type,
10635 NULL, &symval, view + reloc_offset,
10636 address + reloc_offset, reloc_size);
10637 }
10638 }
10639
10640 // Determine whether an object attribute tag takes an integer, a
10641 // string or both.
10642
10643 template<bool big_endian>
10644 int
10645 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
10646 {
10647 if (tag == Object_attribute::Tag_compatibility)
10648 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10649 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
10650 else if (tag == elfcpp::Tag_nodefaults)
10651 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
10652 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
10653 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
10654 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
10655 else if (tag < 32)
10656 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
10657 else
10658 return ((tag & 1) != 0
10659 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
10660 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10661 }
10662
10663 // Reorder attributes.
10664 //
10665 // The ABI defines that Tag_conformance should be emitted first, and that
10666 // Tag_nodefaults should be second (if either is defined). This sets those
10667 // two positions, and bumps up the position of all the remaining tags to
10668 // compensate.
10669
10670 template<bool big_endian>
10671 int
10672 Target_arm<big_endian>::do_attributes_order(int num) const
10673 {
10674 // Reorder the known object attributes in output. We want to move
10675 // Tag_conformance to position 4 and Tag_conformance to position 5
10676 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
10677 if (num == 4)
10678 return elfcpp::Tag_conformance;
10679 if (num == 5)
10680 return elfcpp::Tag_nodefaults;
10681 if ((num - 2) < elfcpp::Tag_nodefaults)
10682 return num - 2;
10683 if ((num - 1) < elfcpp::Tag_conformance)
10684 return num - 1;
10685 return num;
10686 }
10687
10688 // Scan a span of THUMB code for Cortex-A8 erratum.
10689
10690 template<bool big_endian>
10691 void
10692 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
10693 Arm_relobj<big_endian>* arm_relobj,
10694 unsigned int shndx,
10695 section_size_type span_start,
10696 section_size_type span_end,
10697 const unsigned char* view,
10698 Arm_address address)
10699 {
10700 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
10701 //
10702 // The opcode is BLX.W, BL.W, B.W, Bcc.W
10703 // The branch target is in the same 4KB region as the
10704 // first half of the branch.
10705 // The instruction before the branch is a 32-bit
10706 // length non-branch instruction.
10707 section_size_type i = span_start;
10708 bool last_was_32bit = false;
10709 bool last_was_branch = false;
10710 while (i < span_end)
10711 {
10712 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10713 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
10714 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
10715 bool is_blx = false, is_b = false;
10716 bool is_bl = false, is_bcc = false;
10717
10718 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
10719 if (insn_32bit)
10720 {
10721 // Load the rest of the insn (in manual-friendly order).
10722 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
10723
10724 // Encoding T4: B<c>.W.
10725 is_b = (insn & 0xf800d000U) == 0xf0009000U;
10726 // Encoding T1: BL<c>.W.
10727 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
10728 // Encoding T2: BLX<c>.W.
10729 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
10730 // Encoding T3: B<c>.W (not permitted in IT block).
10731 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
10732 && (insn & 0x07f00000U) != 0x03800000U);
10733 }
10734
10735 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
10736
10737 // If this instruction is a 32-bit THUMB branch that crosses a 4K
10738 // page boundary and it follows 32-bit non-branch instruction,
10739 // we need to work around.
10740 if (is_32bit_branch
10741 && ((address + i) & 0xfffU) == 0xffeU
10742 && last_was_32bit
10743 && !last_was_branch)
10744 {
10745 // Check to see if there is a relocation stub for this branch.
10746 bool force_target_arm = false;
10747 bool force_target_thumb = false;
10748 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
10749 Cortex_a8_relocs_info::const_iterator p =
10750 this->cortex_a8_relocs_info_.find(address + i);
10751
10752 if (p != this->cortex_a8_relocs_info_.end())
10753 {
10754 cortex_a8_reloc = p->second;
10755 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
10756
10757 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10758 && !target_is_thumb)
10759 force_target_arm = true;
10760 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
10761 && target_is_thumb)
10762 force_target_thumb = true;
10763 }
10764
10765 off_t offset;
10766 Stub_type stub_type = arm_stub_none;
10767
10768 // Check if we have an offending branch instruction.
10769 uint16_t upper_insn = (insn >> 16) & 0xffffU;
10770 uint16_t lower_insn = insn & 0xffffU;
10771 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10772
10773 if (cortex_a8_reloc != NULL
10774 && cortex_a8_reloc->reloc_stub() != NULL)
10775 // We've already made a stub for this instruction, e.g.
10776 // it's a long branch or a Thumb->ARM stub. Assume that
10777 // stub will suffice to work around the A8 erratum (see
10778 // setting of always_after_branch above).
10779 ;
10780 else if (is_bcc)
10781 {
10782 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
10783 lower_insn);
10784 stub_type = arm_stub_a8_veneer_b_cond;
10785 }
10786 else if (is_b || is_bl || is_blx)
10787 {
10788 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
10789 lower_insn);
10790 if (is_blx)
10791 offset &= ~3;
10792
10793 stub_type = (is_blx
10794 ? arm_stub_a8_veneer_blx
10795 : (is_bl
10796 ? arm_stub_a8_veneer_bl
10797 : arm_stub_a8_veneer_b));
10798 }
10799
10800 if (stub_type != arm_stub_none)
10801 {
10802 Arm_address pc_for_insn = address + i + 4;
10803
10804 // The original instruction is a BL, but the target is
10805 // an ARM instruction. If we were not making a stub,
10806 // the BL would have been converted to a BLX. Use the
10807 // BLX stub instead in that case.
10808 if (this->may_use_blx() && force_target_arm
10809 && stub_type == arm_stub_a8_veneer_bl)
10810 {
10811 stub_type = arm_stub_a8_veneer_blx;
10812 is_blx = true;
10813 is_bl = false;
10814 }
10815 // Conversely, if the original instruction was
10816 // BLX but the target is Thumb mode, use the BL stub.
10817 else if (force_target_thumb
10818 && stub_type == arm_stub_a8_veneer_blx)
10819 {
10820 stub_type = arm_stub_a8_veneer_bl;
10821 is_blx = false;
10822 is_bl = true;
10823 }
10824
10825 if (is_blx)
10826 pc_for_insn &= ~3;
10827
10828 // If we found a relocation, use the proper destination,
10829 // not the offset in the (unrelocated) instruction.
10830 // Note this is always done if we switched the stub type above.
10831 if (cortex_a8_reloc != NULL)
10832 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
10833
10834 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
10835
10836 // Add a new stub if destination address in in the same page.
10837 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
10838 {
10839 Cortex_a8_stub* stub =
10840 this->stub_factory_.make_cortex_a8_stub(stub_type,
10841 arm_relobj, shndx,
10842 address + i,
10843 target, insn);
10844 Stub_table<big_endian>* stub_table =
10845 arm_relobj->stub_table(shndx);
10846 gold_assert(stub_table != NULL);
10847 stub_table->add_cortex_a8_stub(address + i, stub);
10848 }
10849 }
10850 }
10851
10852 i += insn_32bit ? 4 : 2;
10853 last_was_32bit = insn_32bit;
10854 last_was_branch = is_32bit_branch;
10855 }
10856 }
10857
10858 // Apply the Cortex-A8 workaround.
10859
10860 template<bool big_endian>
10861 void
10862 Target_arm<big_endian>::apply_cortex_a8_workaround(
10863 const Cortex_a8_stub* stub,
10864 Arm_address stub_address,
10865 unsigned char* insn_view,
10866 Arm_address insn_address)
10867 {
10868 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
10869 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
10870 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
10871 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
10872 off_t branch_offset = stub_address - (insn_address + 4);
10873
10874 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
10875 switch (stub->stub_template()->type())
10876 {
10877 case arm_stub_a8_veneer_b_cond:
10878 gold_assert(!utils::has_overflow<21>(branch_offset));
10879 upper_insn = RelocFuncs::thumb32_cond_branch_upper(upper_insn,
10880 branch_offset);
10881 lower_insn = RelocFuncs::thumb32_cond_branch_lower(lower_insn,
10882 branch_offset);
10883 break;
10884
10885 case arm_stub_a8_veneer_b:
10886 case arm_stub_a8_veneer_bl:
10887 case arm_stub_a8_veneer_blx:
10888 if ((lower_insn & 0x5000U) == 0x4000U)
10889 // For a BLX instruction, make sure that the relocation is
10890 // rounded up to a word boundary. This follows the semantics of
10891 // the instruction which specifies that bit 1 of the target
10892 // address will come from bit 1 of the base address.
10893 branch_offset = (branch_offset + 2) & ~3;
10894
10895 // Put BRANCH_OFFSET back into the insn.
10896 gold_assert(!utils::has_overflow<25>(branch_offset));
10897 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
10898 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
10899 break;
10900
10901 default:
10902 gold_unreachable();
10903 }
10904
10905 // Put the relocated value back in the object file:
10906 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
10907 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
10908 }
10909
10910 template<bool big_endian>
10911 class Target_selector_arm : public Target_selector
10912 {
10913 public:
10914 Target_selector_arm()
10915 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
10916 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
10917 { }
10918
10919 Target*
10920 do_instantiate_target()
10921 { return new Target_arm<big_endian>(); }
10922 };
10923
10924 // Fix .ARM.exidx section coverage.
10925
10926 template<bool big_endian>
10927 void
10928 Target_arm<big_endian>::fix_exidx_coverage(
10929 Layout* layout,
10930 Arm_output_section<big_endian>* exidx_section,
10931 Symbol_table* symtab)
10932 {
10933 // We need to look at all the input sections in output in ascending
10934 // order of of output address. We do that by building a sorted list
10935 // of output sections by addresses. Then we looks at the output sections
10936 // in order. The input sections in an output section are already sorted
10937 // by addresses within the output section.
10938
10939 typedef std::set<Output_section*, output_section_address_less_than>
10940 Sorted_output_section_list;
10941 Sorted_output_section_list sorted_output_sections;
10942 Layout::Section_list section_list;
10943 layout->get_allocated_sections(&section_list);
10944 for (Layout::Section_list::const_iterator p = section_list.begin();
10945 p != section_list.end();
10946 ++p)
10947 {
10948 // We only care about output sections that contain executable code.
10949 if (((*p)->flags() & elfcpp::SHF_EXECINSTR) != 0)
10950 sorted_output_sections.insert(*p);
10951 }
10952
10953 // Go over the output sections in ascending order of output addresses.
10954 typedef typename Arm_output_section<big_endian>::Text_section_list
10955 Text_section_list;
10956 Text_section_list sorted_text_sections;
10957 for(typename Sorted_output_section_list::iterator p =
10958 sorted_output_sections.begin();
10959 p != sorted_output_sections.end();
10960 ++p)
10961 {
10962 Arm_output_section<big_endian>* arm_output_section =
10963 Arm_output_section<big_endian>::as_arm_output_section(*p);
10964 arm_output_section->append_text_sections_to_list(&sorted_text_sections);
10965 }
10966
10967 exidx_section->fix_exidx_coverage(layout, sorted_text_sections, symtab,
10968 merge_exidx_entries());
10969 }
10970
10971 Target_selector_arm<false> target_selector_arm;
10972 Target_selector_arm<true> target_selector_armbe;
10973
10974 } // End anonymous namespace.
This page took 0.256729 seconds and 5 git commands to generate.