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