2010-01-12 Doug Kwan <dougkwan@google.com>
[binutils-gdb.git] / gold / arm.cc
1 // arm.cc -- arm target support for gold.
2
3 // Copyright 2009 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
34 #include "elfcpp.h"
35 #include "parameters.h"
36 #include "reloc.h"
37 #include "arm.h"
38 #include "object.h"
39 #include "symtab.h"
40 #include "layout.h"
41 #include "output.h"
42 #include "copy-relocs.h"
43 #include "target.h"
44 #include "target-reloc.h"
45 #include "target-select.h"
46 #include "tls.h"
47 #include "defstd.h"
48 #include "gc.h"
49 #include "attributes.h"
50
51 namespace
52 {
53
54 using namespace gold;
55
56 template<bool big_endian>
57 class Output_data_plt_arm;
58
59 template<bool big_endian>
60 class Stub_table;
61
62 template<bool big_endian>
63 class Arm_input_section;
64
65 template<bool big_endian>
66 class Arm_output_section;
67
68 template<bool big_endian>
69 class Arm_relobj;
70
71 template<bool big_endian>
72 class Target_arm;
73
74 // For convenience.
75 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
76
77 // Maximum branch offsets for ARM, THUMB and THUMB2.
78 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
79 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
80 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
81 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
82 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
83 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
84
85 // The arm target class.
86 //
87 // This is a very simple port of gold for ARM-EABI. It is intended for
88 // supporting Android only for the time being. Only these relocation types
89 // are supported.
90 //
91 // R_ARM_NONE
92 // R_ARM_ABS32
93 // R_ARM_ABS32_NOI
94 // R_ARM_ABS16
95 // R_ARM_ABS12
96 // R_ARM_ABS8
97 // R_ARM_THM_ABS5
98 // R_ARM_BASE_ABS
99 // R_ARM_REL32
100 // R_ARM_THM_CALL
101 // R_ARM_COPY
102 // R_ARM_GLOB_DAT
103 // R_ARM_BASE_PREL
104 // R_ARM_JUMP_SLOT
105 // R_ARM_RELATIVE
106 // R_ARM_GOTOFF32
107 // R_ARM_GOT_BREL
108 // R_ARM_GOT_PREL
109 // R_ARM_PLT32
110 // R_ARM_CALL
111 // R_ARM_JUMP24
112 // R_ARM_TARGET1
113 // R_ARM_PREL31
114 // R_ARM_ABS8
115 // R_ARM_MOVW_ABS_NC
116 // R_ARM_MOVT_ABS
117 // R_ARM_THM_MOVW_ABS_NC
118 // R_ARM_THM_MOVT_ABS
119 // R_ARM_MOVW_PREL_NC
120 // R_ARM_MOVT_PREL
121 // R_ARM_THM_MOVW_PREL_NC
122 // R_ARM_THM_MOVT_PREL
123 //
124 // TODOs:
125 // - Support more relocation types as needed.
126 // - Make PLTs more flexible for different architecture features like
127 // Thumb-2 and BE8.
128 // There are probably a lot more.
129
130 // Instruction template class. This class is similar to the insn_sequence
131 // struct in bfd/elf32-arm.c.
132
133 class Insn_template
134 {
135 public:
136 // Types of instruction templates.
137 enum Type
138 {
139 THUMB16_TYPE = 1,
140 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
141 // templates with class-specific semantics. Currently this is used
142 // only by the Cortex_a8_stub class for handling condition codes in
143 // conditional branches.
144 THUMB16_SPECIAL_TYPE,
145 THUMB32_TYPE,
146 ARM_TYPE,
147 DATA_TYPE
148 };
149
150 // Factory methods to create instruction templates in different formats.
151
152 static const Insn_template
153 thumb16_insn(uint32_t data)
154 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
155
156 // A Thumb conditional branch, in which the proper condition is inserted
157 // when we build the stub.
158 static const Insn_template
159 thumb16_bcond_insn(uint32_t data)
160 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
161
162 static const Insn_template
163 thumb32_insn(uint32_t data)
164 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
165
166 static const Insn_template
167 thumb32_b_insn(uint32_t data, int reloc_addend)
168 {
169 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
170 reloc_addend);
171 }
172
173 static const Insn_template
174 arm_insn(uint32_t data)
175 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
176
177 static const Insn_template
178 arm_rel_insn(unsigned data, int reloc_addend)
179 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
180
181 static const Insn_template
182 data_word(unsigned data, unsigned int r_type, int reloc_addend)
183 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
184
185 // Accessors. This class is used for read-only objects so no modifiers
186 // are provided.
187
188 uint32_t
189 data() const
190 { return this->data_; }
191
192 // Return the instruction sequence type of this.
193 Type
194 type() const
195 { return this->type_; }
196
197 // Return the ARM relocation type of this.
198 unsigned int
199 r_type() const
200 { return this->r_type_; }
201
202 int32_t
203 reloc_addend() const
204 { return this->reloc_addend_; }
205
206 // Return size of instruction template in bytes.
207 size_t
208 size() const;
209
210 // Return byte-alignment of instruction template.
211 unsigned
212 alignment() const;
213
214 private:
215 // We make the constructor private to ensure that only the factory
216 // methods are used.
217 inline
218 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
219 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
220 { }
221
222 // Instruction specific data. This is used to store information like
223 // some of the instruction bits.
224 uint32_t data_;
225 // Instruction template type.
226 Type type_;
227 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
228 unsigned int r_type_;
229 // Relocation addend.
230 int32_t reloc_addend_;
231 };
232
233 // Macro for generating code to stub types. One entry per long/short
234 // branch stub
235
236 #define DEF_STUBS \
237 DEF_STUB(long_branch_any_any) \
238 DEF_STUB(long_branch_v4t_arm_thumb) \
239 DEF_STUB(long_branch_thumb_only) \
240 DEF_STUB(long_branch_v4t_thumb_thumb) \
241 DEF_STUB(long_branch_v4t_thumb_arm) \
242 DEF_STUB(short_branch_v4t_thumb_arm) \
243 DEF_STUB(long_branch_any_arm_pic) \
244 DEF_STUB(long_branch_any_thumb_pic) \
245 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
246 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
247 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
248 DEF_STUB(long_branch_thumb_only_pic) \
249 DEF_STUB(a8_veneer_b_cond) \
250 DEF_STUB(a8_veneer_b) \
251 DEF_STUB(a8_veneer_bl) \
252 DEF_STUB(a8_veneer_blx)
253
254 // Stub types.
255
256 #define DEF_STUB(x) arm_stub_##x,
257 typedef enum
258 {
259 arm_stub_none,
260 DEF_STUBS
261
262 // First reloc stub type.
263 arm_stub_reloc_first = arm_stub_long_branch_any_any,
264 // Last reloc stub type.
265 arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
266
267 // First Cortex-A8 stub type.
268 arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
269 // Last Cortex-A8 stub type.
270 arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
271
272 // Last stub type.
273 arm_stub_type_last = arm_stub_a8_veneer_blx
274 } Stub_type;
275 #undef DEF_STUB
276
277 // Stub template class. Templates are meant to be read-only objects.
278 // A stub template for a stub type contains all read-only attributes
279 // common to all stubs of the same type.
280
281 class Stub_template
282 {
283 public:
284 Stub_template(Stub_type, const Insn_template*, size_t);
285
286 ~Stub_template()
287 { }
288
289 // Return stub type.
290 Stub_type
291 type() const
292 { return this->type_; }
293
294 // Return an array of instruction templates.
295 const Insn_template*
296 insns() const
297 { return this->insns_; }
298
299 // Return size of template in number of instructions.
300 size_t
301 insn_count() const
302 { return this->insn_count_; }
303
304 // Return size of template in bytes.
305 size_t
306 size() const
307 { return this->size_; }
308
309 // Return alignment of the stub template.
310 unsigned
311 alignment() const
312 { return this->alignment_; }
313
314 // Return whether entry point is in thumb mode.
315 bool
316 entry_in_thumb_mode() const
317 { return this->entry_in_thumb_mode_; }
318
319 // Return number of relocations in this template.
320 size_t
321 reloc_count() const
322 { return this->relocs_.size(); }
323
324 // Return index of the I-th instruction with relocation.
325 size_t
326 reloc_insn_index(size_t i) const
327 {
328 gold_assert(i < this->relocs_.size());
329 return this->relocs_[i].first;
330 }
331
332 // Return the offset of the I-th instruction with relocation from the
333 // beginning of the stub.
334 section_size_type
335 reloc_offset(size_t i) const
336 {
337 gold_assert(i < this->relocs_.size());
338 return this->relocs_[i].second;
339 }
340
341 private:
342 // This contains information about an instruction template with a relocation
343 // and its offset from start of stub.
344 typedef std::pair<size_t, section_size_type> Reloc;
345
346 // A Stub_template may not be copied. We want to share templates as much
347 // as possible.
348 Stub_template(const Stub_template&);
349 Stub_template& operator=(const Stub_template&);
350
351 // Stub type.
352 Stub_type type_;
353 // Points to an array of Insn_templates.
354 const Insn_template* insns_;
355 // Number of Insn_templates in insns_[].
356 size_t insn_count_;
357 // Size of templated instructions in bytes.
358 size_t size_;
359 // Alignment of templated instructions.
360 unsigned alignment_;
361 // Flag to indicate if entry is in thumb mode.
362 bool entry_in_thumb_mode_;
363 // A table of reloc instruction indices and offsets. We can find these by
364 // looking at the instruction templates but we pre-compute and then stash
365 // them here for speed.
366 std::vector<Reloc> relocs_;
367 };
368
369 //
370 // A class for code stubs. This is a base class for different type of
371 // stubs used in the ARM target.
372 //
373
374 class Stub
375 {
376 private:
377 static const section_offset_type invalid_offset =
378 static_cast<section_offset_type>(-1);
379
380 public:
381 Stub(const Stub_template* stub_template)
382 : stub_template_(stub_template), offset_(invalid_offset)
383 { }
384
385 virtual
386 ~Stub()
387 { }
388
389 // Return the stub template.
390 const Stub_template*
391 stub_template() const
392 { return this->stub_template_; }
393
394 // Return offset of code stub from beginning of its containing stub table.
395 section_offset_type
396 offset() const
397 {
398 gold_assert(this->offset_ != invalid_offset);
399 return this->offset_;
400 }
401
402 // Set offset of code stub from beginning of its containing stub table.
403 void
404 set_offset(section_offset_type offset)
405 { this->offset_ = offset; }
406
407 // Return the relocation target address of the i-th relocation in the
408 // stub. This must be defined in a child class.
409 Arm_address
410 reloc_target(size_t i)
411 { return this->do_reloc_target(i); }
412
413 // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
414 void
415 write(unsigned char* view, section_size_type view_size, bool big_endian)
416 { this->do_write(view, view_size, big_endian); }
417
418 // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
419 // for the i-th instruction.
420 uint16_t
421 thumb16_special(size_t i)
422 { return this->do_thumb16_special(i); }
423
424 protected:
425 // This must be defined in the child class.
426 virtual Arm_address
427 do_reloc_target(size_t) = 0;
428
429 // This may be overridden in the child class.
430 virtual void
431 do_write(unsigned char* view, section_size_type view_size, bool big_endian)
432 {
433 if (big_endian)
434 this->do_fixed_endian_write<true>(view, view_size);
435 else
436 this->do_fixed_endian_write<false>(view, view_size);
437 }
438
439 // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
440 // instruction template.
441 virtual uint16_t
442 do_thumb16_special(size_t)
443 { gold_unreachable(); }
444
445 private:
446 // A template to implement do_write.
447 template<bool big_endian>
448 void inline
449 do_fixed_endian_write(unsigned char*, section_size_type);
450
451 // Its template.
452 const Stub_template* stub_template_;
453 // Offset within the section of containing this stub.
454 section_offset_type offset_;
455 };
456
457 // Reloc stub class. These are stubs we use to fix up relocation because
458 // of limited branch ranges.
459
460 class Reloc_stub : public Stub
461 {
462 public:
463 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
464 // We assume we never jump to this address.
465 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
466
467 // Return destination address.
468 Arm_address
469 destination_address() const
470 {
471 gold_assert(this->destination_address_ != this->invalid_address);
472 return this->destination_address_;
473 }
474
475 // Set destination address.
476 void
477 set_destination_address(Arm_address address)
478 {
479 gold_assert(address != this->invalid_address);
480 this->destination_address_ = address;
481 }
482
483 // Reset destination address.
484 void
485 reset_destination_address()
486 { this->destination_address_ = this->invalid_address; }
487
488 // Determine stub type for a branch of a relocation of R_TYPE going
489 // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
490 // the branch target is a thumb instruction. TARGET is used for look
491 // up ARM-specific linker settings.
492 static Stub_type
493 stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
494 Arm_address branch_target, bool target_is_thumb);
495
496 // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
497 // and an addend. Since we treat global and local symbol differently, we
498 // use a Symbol object for a global symbol and a object-index pair for
499 // a local symbol.
500 class Key
501 {
502 public:
503 // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
504 // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
505 // and R_SYM must not be invalid_index.
506 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
507 unsigned int r_sym, int32_t addend)
508 : stub_type_(stub_type), addend_(addend)
509 {
510 if (symbol != NULL)
511 {
512 this->r_sym_ = Reloc_stub::invalid_index;
513 this->u_.symbol = symbol;
514 }
515 else
516 {
517 gold_assert(relobj != NULL && r_sym != invalid_index);
518 this->r_sym_ = r_sym;
519 this->u_.relobj = relobj;
520 }
521 }
522
523 ~Key()
524 { }
525
526 // Accessors: Keys are meant to be read-only object so no modifiers are
527 // provided.
528
529 // Return stub type.
530 Stub_type
531 stub_type() const
532 { return this->stub_type_; }
533
534 // Return the local symbol index or invalid_index.
535 unsigned int
536 r_sym() const
537 { return this->r_sym_; }
538
539 // Return the symbol if there is one.
540 const Symbol*
541 symbol() const
542 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
543
544 // Return the relobj if there is one.
545 const Relobj*
546 relobj() const
547 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
548
549 // Whether this equals to another key k.
550 bool
551 eq(const Key& k) const
552 {
553 return ((this->stub_type_ == k.stub_type_)
554 && (this->r_sym_ == k.r_sym_)
555 && ((this->r_sym_ != Reloc_stub::invalid_index)
556 ? (this->u_.relobj == k.u_.relobj)
557 : (this->u_.symbol == k.u_.symbol))
558 && (this->addend_ == k.addend_));
559 }
560
561 // Return a hash value.
562 size_t
563 hash_value() const
564 {
565 return (this->stub_type_
566 ^ this->r_sym_
567 ^ gold::string_hash<char>(
568 (this->r_sym_ != Reloc_stub::invalid_index)
569 ? this->u_.relobj->name().c_str()
570 : this->u_.symbol->name())
571 ^ this->addend_);
572 }
573
574 // Functors for STL associative containers.
575 struct hash
576 {
577 size_t
578 operator()(const Key& k) const
579 { return k.hash_value(); }
580 };
581
582 struct equal_to
583 {
584 bool
585 operator()(const Key& k1, const Key& k2) const
586 { return k1.eq(k2); }
587 };
588
589 // Name of key. This is mainly for debugging.
590 std::string
591 name() const;
592
593 private:
594 // Stub type.
595 Stub_type stub_type_;
596 // If this is a local symbol, this is the index in the defining object.
597 // Otherwise, it is invalid_index for a global symbol.
598 unsigned int r_sym_;
599 // If r_sym_ is invalid index. This points to a global symbol.
600 // Otherwise, this points a relobj. We used the unsized and target
601 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
602 // Arm_relobj. This is done to avoid making the stub class a template
603 // as most of the stub machinery is endianity-neutral. However, it
604 // may require a bit of casting done by users of this class.
605 union
606 {
607 const Symbol* symbol;
608 const Relobj* relobj;
609 } u_;
610 // Addend associated with a reloc.
611 int32_t addend_;
612 };
613
614 protected:
615 // Reloc_stubs are created via a stub factory. So these are protected.
616 Reloc_stub(const Stub_template* stub_template)
617 : Stub(stub_template), destination_address_(invalid_address)
618 { }
619
620 ~Reloc_stub()
621 { }
622
623 friend class Stub_factory;
624
625 // Return the relocation target address of the i-th relocation in the
626 // stub.
627 Arm_address
628 do_reloc_target(size_t i)
629 {
630 // All reloc stub have only one relocation.
631 gold_assert(i == 0);
632 return this->destination_address_;
633 }
634
635 private:
636 // Address of destination.
637 Arm_address destination_address_;
638 };
639
640 // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
641 // THUMB branch that meets the following conditions:
642 //
643 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
644 // branch address is 0xffe.
645 // 2. The branch target address is in the same page as the first word of the
646 // branch.
647 // 3. The branch follows a 32-bit instruction which is not a branch.
648 //
649 // To do the fix up, we need to store the address of the branch instruction
650 // and its target at least. We also need to store the original branch
651 // instruction bits for the condition code in a conditional branch. The
652 // condition code is used in a special instruction template. We also want
653 // to identify input sections needing Cortex-A8 workaround quickly. We store
654 // extra information about object and section index of the code section
655 // containing a branch being fixed up. The information is used to mark
656 // the code section when we finalize the Cortex-A8 stubs.
657 //
658
659 class Cortex_a8_stub : public Stub
660 {
661 public:
662 ~Cortex_a8_stub()
663 { }
664
665 // Return the object of the code section containing the branch being fixed
666 // up.
667 Relobj*
668 relobj() const
669 { return this->relobj_; }
670
671 // Return the section index of the code section containing the branch being
672 // fixed up.
673 unsigned int
674 shndx() const
675 { return this->shndx_; }
676
677 // Return the source address of stub. This is the address of the original
678 // branch instruction. LSB is 1 always set to indicate that it is a THUMB
679 // instruction.
680 Arm_address
681 source_address() const
682 { return this->source_address_; }
683
684 // Return the destination address of the stub. This is the branch taken
685 // address of the original branch instruction. LSB is 1 if it is a THUMB
686 // instruction address.
687 Arm_address
688 destination_address() const
689 { return this->destination_address_; }
690
691 // Return the instruction being fixed up.
692 uint32_t
693 original_insn() const
694 { return this->original_insn_; }
695
696 protected:
697 // Cortex_a8_stubs are created via a stub factory. So these are protected.
698 Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
699 unsigned int shndx, Arm_address source_address,
700 Arm_address destination_address, uint32_t original_insn)
701 : Stub(stub_template), relobj_(relobj), shndx_(shndx),
702 source_address_(source_address | 1U),
703 destination_address_(destination_address),
704 original_insn_(original_insn)
705 { }
706
707 friend class Stub_factory;
708
709 // Return the relocation target address of the i-th relocation in the
710 // stub.
711 Arm_address
712 do_reloc_target(size_t i)
713 {
714 if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
715 {
716 // The conditional branch veneer has two relocations.
717 gold_assert(i < 2);
718 return i == 0 ? this->source_address_ + 4 : this->destination_address_;
719 }
720 else
721 {
722 // All other Cortex-A8 stubs have only one relocation.
723 gold_assert(i == 0);
724 return this->destination_address_;
725 }
726 }
727
728 // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
729 uint16_t
730 do_thumb16_special(size_t);
731
732 private:
733 // Object of the code section containing the branch being fixed up.
734 Relobj* relobj_;
735 // Section index of the code section containing the branch begin fixed up.
736 unsigned int shndx_;
737 // Source address of original branch.
738 Arm_address source_address_;
739 // Destination address of the original branch.
740 Arm_address destination_address_;
741 // Original branch instruction. This is needed for copying the condition
742 // code from a condition branch to its stub.
743 uint32_t original_insn_;
744 };
745
746 // Stub factory class.
747
748 class Stub_factory
749 {
750 public:
751 // Return the unique instance of this class.
752 static const Stub_factory&
753 get_instance()
754 {
755 static Stub_factory singleton;
756 return singleton;
757 }
758
759 // Make a relocation stub.
760 Reloc_stub*
761 make_reloc_stub(Stub_type stub_type) const
762 {
763 gold_assert(stub_type >= arm_stub_reloc_first
764 && stub_type <= arm_stub_reloc_last);
765 return new Reloc_stub(this->stub_templates_[stub_type]);
766 }
767
768 // Make a Cortex-A8 stub.
769 Cortex_a8_stub*
770 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
771 Arm_address source, Arm_address destination,
772 uint32_t original_insn) const
773 {
774 gold_assert(stub_type >= arm_stub_cortex_a8_first
775 && stub_type <= arm_stub_cortex_a8_last);
776 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
777 source, destination, original_insn);
778 }
779
780 private:
781 // Constructor and destructor are protected since we only return a single
782 // instance created in Stub_factory::get_instance().
783
784 Stub_factory();
785
786 // A Stub_factory may not be copied since it is a singleton.
787 Stub_factory(const Stub_factory&);
788 Stub_factory& operator=(Stub_factory&);
789
790 // Stub templates. These are initialized in the constructor.
791 const Stub_template* stub_templates_[arm_stub_type_last+1];
792 };
793
794 // A class to hold stubs for the ARM target.
795
796 template<bool big_endian>
797 class Stub_table : public Output_data
798 {
799 public:
800 Stub_table(Arm_input_section<big_endian>* owner)
801 : Output_data(), owner_(owner), reloc_stubs_(), cortex_a8_stubs_(),
802 prev_data_size_(0), prev_addralign_(1)
803 { }
804
805 ~Stub_table()
806 { }
807
808 // Owner of this stub table.
809 Arm_input_section<big_endian>*
810 owner() const
811 { return this->owner_; }
812
813 // Whether this stub table is empty.
814 bool
815 empty() const
816 { return this->reloc_stubs_.empty() && this->cortex_a8_stubs_.empty(); }
817
818 // Return the current data size.
819 off_t
820 current_data_size() const
821 { return this->current_data_size_for_child(); }
822
823 // Add a STUB with using KEY. Caller is reponsible for avoid adding
824 // if already a STUB with the same key has been added.
825 void
826 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
827 {
828 const Stub_template* stub_template = stub->stub_template();
829 gold_assert(stub_template->type() == key.stub_type());
830 this->reloc_stubs_[key] = stub;
831 }
832
833 // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
834 // Caller is reponsible for avoid adding if already a STUB with the same
835 // address has been added.
836 void
837 add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
838 {
839 std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
840 this->cortex_a8_stubs_.insert(value);
841 }
842
843 // Remove all Cortex-A8 stubs.
844 void
845 remove_all_cortex_a8_stubs();
846
847 // Look up a relocation stub using KEY. Return NULL if there is none.
848 Reloc_stub*
849 find_reloc_stub(const Reloc_stub::Key& key) const
850 {
851 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
852 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
853 }
854
855 // Relocate stubs in this stub table.
856 void
857 relocate_stubs(const Relocate_info<32, big_endian>*,
858 Target_arm<big_endian>*, Output_section*,
859 unsigned char*, Arm_address, section_size_type);
860
861 // Update data size and alignment at the end of a relaxation pass. Return
862 // true if either data size or alignment is different from that of the
863 // previous relaxation pass.
864 bool
865 update_data_size_and_addralign();
866
867 // Finalize stubs. Set the offsets of all stubs and mark input sections
868 // needing the Cortex-A8 workaround.
869 void
870 finalize_stubs();
871
872 // Apply Cortex-A8 workaround to an address range.
873 void
874 apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
875 unsigned char*, Arm_address,
876 section_size_type);
877
878 protected:
879 // Write out section contents.
880 void
881 do_write(Output_file*);
882
883 // Return the required alignment.
884 uint64_t
885 do_addralign() const
886 { return this->prev_addralign_; }
887
888 // Reset address and file offset.
889 void
890 do_reset_address_and_file_offset()
891 { this->set_current_data_size_for_child(this->prev_data_size_); }
892
893 // Set final data size.
894 void
895 set_final_data_size()
896 { this->set_data_size(this->current_data_size()); }
897
898 private:
899 // Relocate one stub.
900 void
901 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
902 Target_arm<big_endian>*, Output_section*,
903 unsigned char*, Arm_address, section_size_type);
904
905 // Unordered map of relocation stubs.
906 typedef
907 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
908 Reloc_stub::Key::equal_to>
909 Reloc_stub_map;
910
911 // List of Cortex-A8 stubs ordered by addresses of branches being
912 // fixed up in output.
913 typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
914
915 // Owner of this stub table.
916 Arm_input_section<big_endian>* owner_;
917 // The relocation stubs.
918 Reloc_stub_map reloc_stubs_;
919 // The cortex_a8_stubs.
920 Cortex_a8_stub_list cortex_a8_stubs_;
921 // data size of this in the previous pass.
922 off_t prev_data_size_;
923 // address alignment of this in the previous pass.
924 uint64_t prev_addralign_;
925 };
926
927 // A class to wrap an ordinary input section containing executable code.
928
929 template<bool big_endian>
930 class Arm_input_section : public Output_relaxed_input_section
931 {
932 public:
933 Arm_input_section(Relobj* relobj, unsigned int shndx)
934 : Output_relaxed_input_section(relobj, shndx, 1),
935 original_addralign_(1), original_size_(0), stub_table_(NULL)
936 { }
937
938 ~Arm_input_section()
939 { }
940
941 // Initialize.
942 void
943 init();
944
945 // Whether this is a stub table owner.
946 bool
947 is_stub_table_owner() const
948 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
949
950 // Return the stub table.
951 Stub_table<big_endian>*
952 stub_table() const
953 { return this->stub_table_; }
954
955 // Set the stub_table.
956 void
957 set_stub_table(Stub_table<big_endian>* stub_table)
958 { this->stub_table_ = stub_table; }
959
960 // Downcast a base pointer to an Arm_input_section pointer. This is
961 // not type-safe but we only use Arm_input_section not the base class.
962 static Arm_input_section<big_endian>*
963 as_arm_input_section(Output_relaxed_input_section* poris)
964 { return static_cast<Arm_input_section<big_endian>*>(poris); }
965
966 protected:
967 // Write data to output file.
968 void
969 do_write(Output_file*);
970
971 // Return required alignment of this.
972 uint64_t
973 do_addralign() const
974 {
975 if (this->is_stub_table_owner())
976 return std::max(this->stub_table_->addralign(),
977 this->original_addralign_);
978 else
979 return this->original_addralign_;
980 }
981
982 // Finalize data size.
983 void
984 set_final_data_size();
985
986 // Reset address and file offset.
987 void
988 do_reset_address_and_file_offset();
989
990 // Output offset.
991 bool
992 do_output_offset(const Relobj* object, unsigned int shndx,
993 section_offset_type offset,
994 section_offset_type* poutput) const
995 {
996 if ((object == this->relobj())
997 && (shndx == this->shndx())
998 && (offset >= 0)
999 && (convert_types<uint64_t, section_offset_type>(offset)
1000 <= this->original_size_))
1001 {
1002 *poutput = offset;
1003 return true;
1004 }
1005 else
1006 return false;
1007 }
1008
1009 private:
1010 // Copying is not allowed.
1011 Arm_input_section(const Arm_input_section&);
1012 Arm_input_section& operator=(const Arm_input_section&);
1013
1014 // Address alignment of the original input section.
1015 uint64_t original_addralign_;
1016 // Section size of the original input section.
1017 uint64_t original_size_;
1018 // Stub table.
1019 Stub_table<big_endian>* stub_table_;
1020 };
1021
1022 // Arm output section class. This is defined mainly to add a number of
1023 // stub generation methods.
1024
1025 template<bool big_endian>
1026 class Arm_output_section : public Output_section
1027 {
1028 public:
1029 Arm_output_section(const char* name, elfcpp::Elf_Word type,
1030 elfcpp::Elf_Xword flags)
1031 : Output_section(name, type, flags)
1032 { }
1033
1034 ~Arm_output_section()
1035 { }
1036
1037 // Group input sections for stub generation.
1038 void
1039 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1040
1041 // Downcast a base pointer to an Arm_output_section pointer. This is
1042 // not type-safe but we only use Arm_output_section not the base class.
1043 static Arm_output_section<big_endian>*
1044 as_arm_output_section(Output_section* os)
1045 { return static_cast<Arm_output_section<big_endian>*>(os); }
1046
1047 private:
1048 // For convenience.
1049 typedef Output_section::Input_section Input_section;
1050 typedef Output_section::Input_section_list Input_section_list;
1051
1052 // Create a stub group.
1053 void create_stub_group(Input_section_list::const_iterator,
1054 Input_section_list::const_iterator,
1055 Input_section_list::const_iterator,
1056 Target_arm<big_endian>*,
1057 std::vector<Output_relaxed_input_section*>*);
1058 };
1059
1060 // Arm_relobj class.
1061
1062 template<bool big_endian>
1063 class Arm_relobj : public Sized_relobj<32, big_endian>
1064 {
1065 public:
1066 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1067
1068 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1069 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1070 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1071 stub_tables_(), local_symbol_is_thumb_function_(),
1072 attributes_section_data_(NULL), mapping_symbols_info_(),
1073 section_has_cortex_a8_workaround_(NULL)
1074 { }
1075
1076 ~Arm_relobj()
1077 { delete this->attributes_section_data_; }
1078
1079 // Return the stub table of the SHNDX-th section if there is one.
1080 Stub_table<big_endian>*
1081 stub_table(unsigned int shndx) const
1082 {
1083 gold_assert(shndx < this->stub_tables_.size());
1084 return this->stub_tables_[shndx];
1085 }
1086
1087 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1088 void
1089 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1090 {
1091 gold_assert(shndx < this->stub_tables_.size());
1092 this->stub_tables_[shndx] = stub_table;
1093 }
1094
1095 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1096 // index. This is only valid after do_count_local_symbol is called.
1097 bool
1098 local_symbol_is_thumb_function(unsigned int r_sym) const
1099 {
1100 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1101 return this->local_symbol_is_thumb_function_[r_sym];
1102 }
1103
1104 // Scan all relocation sections for stub generation.
1105 void
1106 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1107 const Layout*);
1108
1109 // Convert regular input section with index SHNDX to a relaxed section.
1110 void
1111 convert_input_section_to_relaxed_section(unsigned shndx)
1112 {
1113 // The stubs have relocations and we need to process them after writing
1114 // out the stubs. So relocation now must follow section write.
1115 this->invalidate_section_offset(shndx);
1116 this->set_relocs_must_follow_section_writes();
1117 }
1118
1119 // Downcast a base pointer to an Arm_relobj pointer. This is
1120 // not type-safe but we only use Arm_relobj not the base class.
1121 static Arm_relobj<big_endian>*
1122 as_arm_relobj(Relobj* relobj)
1123 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1124
1125 // Processor-specific flags in ELF file header. This is valid only after
1126 // reading symbols.
1127 elfcpp::Elf_Word
1128 processor_specific_flags() const
1129 { return this->processor_specific_flags_; }
1130
1131 // Attribute section data This is the contents of the .ARM.attribute section
1132 // if there is one.
1133 const Attributes_section_data*
1134 attributes_section_data() const
1135 { return this->attributes_section_data_; }
1136
1137 // Mapping symbol location.
1138 typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1139
1140 // Functor for STL container.
1141 struct Mapping_symbol_position_less
1142 {
1143 bool
1144 operator()(const Mapping_symbol_position& p1,
1145 const Mapping_symbol_position& p2) const
1146 {
1147 return (p1.first < p2.first
1148 || (p1.first == p2.first && p1.second < p2.second));
1149 }
1150 };
1151
1152 // We only care about the first character of a mapping symbol, so
1153 // we only store that instead of the whole symbol name.
1154 typedef std::map<Mapping_symbol_position, char,
1155 Mapping_symbol_position_less> Mapping_symbols_info;
1156
1157 // Whether a section contains any Cortex-A8 workaround.
1158 bool
1159 section_has_cortex_a8_workaround(unsigned int shndx) const
1160 {
1161 return (this->section_has_cortex_a8_workaround_ != NULL
1162 && (*this->section_has_cortex_a8_workaround_)[shndx]);
1163 }
1164
1165 // Mark a section that has Cortex-A8 workaround.
1166 void
1167 mark_section_for_cortex_a8_workaround(unsigned int shndx)
1168 {
1169 if (this->section_has_cortex_a8_workaround_ == NULL)
1170 this->section_has_cortex_a8_workaround_ =
1171 new std::vector<bool>(this->shnum(), false);
1172 (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1173 }
1174
1175 protected:
1176 // Post constructor setup.
1177 void
1178 do_setup()
1179 {
1180 // Call parent's setup method.
1181 Sized_relobj<32, big_endian>::do_setup();
1182
1183 // Initialize look-up tables.
1184 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1185 this->stub_tables_.swap(empty_stub_table_list);
1186 }
1187
1188 // Count the local symbols.
1189 void
1190 do_count_local_symbols(Stringpool_template<char>*,
1191 Stringpool_template<char>*);
1192
1193 void
1194 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1195 const unsigned char* pshdrs,
1196 typename Sized_relobj<32, big_endian>::Views* pivews);
1197
1198 // Read the symbol information.
1199 void
1200 do_read_symbols(Read_symbols_data* sd);
1201
1202 // Process relocs for garbage collection.
1203 void
1204 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1205
1206 private:
1207 // List of stub tables.
1208 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1209 Stub_table_list stub_tables_;
1210 // Bit vector to tell if a local symbol is a thumb function or not.
1211 // This is only valid after do_count_local_symbol is called.
1212 std::vector<bool> local_symbol_is_thumb_function_;
1213 // processor-specific flags in ELF file header.
1214 elfcpp::Elf_Word processor_specific_flags_;
1215 // Object attributes if there is an .ARM.attributes section or NULL.
1216 Attributes_section_data* attributes_section_data_;
1217 // Mapping symbols information.
1218 Mapping_symbols_info mapping_symbols_info_;
1219 // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1220 std::vector<bool>* section_has_cortex_a8_workaround_;
1221 };
1222
1223 // Arm_dynobj class.
1224
1225 template<bool big_endian>
1226 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1227 {
1228 public:
1229 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1230 const elfcpp::Ehdr<32, big_endian>& ehdr)
1231 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1232 processor_specific_flags_(0), attributes_section_data_(NULL)
1233 { }
1234
1235 ~Arm_dynobj()
1236 { delete this->attributes_section_data_; }
1237
1238 // Downcast a base pointer to an Arm_relobj pointer. This is
1239 // not type-safe but we only use Arm_relobj not the base class.
1240 static Arm_dynobj<big_endian>*
1241 as_arm_dynobj(Dynobj* dynobj)
1242 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1243
1244 // Processor-specific flags in ELF file header. This is valid only after
1245 // reading symbols.
1246 elfcpp::Elf_Word
1247 processor_specific_flags() const
1248 { return this->processor_specific_flags_; }
1249
1250 // Attributes section data.
1251 const Attributes_section_data*
1252 attributes_section_data() const
1253 { return this->attributes_section_data_; }
1254
1255 protected:
1256 // Read the symbol information.
1257 void
1258 do_read_symbols(Read_symbols_data* sd);
1259
1260 private:
1261 // processor-specific flags in ELF file header.
1262 elfcpp::Elf_Word processor_specific_flags_;
1263 // Object attributes if there is an .ARM.attributes section or NULL.
1264 Attributes_section_data* attributes_section_data_;
1265 };
1266
1267 // Functor to read reloc addends during stub generation.
1268
1269 template<int sh_type, bool big_endian>
1270 struct Stub_addend_reader
1271 {
1272 // Return the addend for a relocation of a particular type. Depending
1273 // on whether this is a REL or RELA relocation, read the addend from a
1274 // view or from a Reloc object.
1275 elfcpp::Elf_types<32>::Elf_Swxword
1276 operator()(
1277 unsigned int /* r_type */,
1278 const unsigned char* /* view */,
1279 const typename Reloc_types<sh_type,
1280 32, big_endian>::Reloc& /* reloc */) const;
1281 };
1282
1283 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1284
1285 template<bool big_endian>
1286 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1287 {
1288 elfcpp::Elf_types<32>::Elf_Swxword
1289 operator()(
1290 unsigned int,
1291 const unsigned char*,
1292 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1293 };
1294
1295 // Specialized Stub_addend_reader for RELA type relocation sections.
1296 // We currently do not handle RELA type relocation sections but it is trivial
1297 // to implement the addend reader. This is provided for completeness and to
1298 // make it easier to add support for RELA relocation sections in the future.
1299
1300 template<bool big_endian>
1301 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1302 {
1303 elfcpp::Elf_types<32>::Elf_Swxword
1304 operator()(
1305 unsigned int,
1306 const unsigned char*,
1307 const typename Reloc_types<elfcpp::SHT_RELA, 32,
1308 big_endian>::Reloc& reloc) const
1309 { return reloc.get_r_addend(); }
1310 };
1311
1312 // Cortex_a8_reloc class. We keep record of relocation that may need
1313 // the Cortex-A8 erratum workaround.
1314
1315 class Cortex_a8_reloc
1316 {
1317 public:
1318 Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1319 Arm_address destination)
1320 : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1321 { }
1322
1323 ~Cortex_a8_reloc()
1324 { }
1325
1326 // Accessors: This is a read-only class.
1327
1328 // Return the relocation stub associated with this relocation if there is
1329 // one.
1330 const Reloc_stub*
1331 reloc_stub() const
1332 { return this->reloc_stub_; }
1333
1334 // Return the relocation type.
1335 unsigned int
1336 r_type() const
1337 { return this->r_type_; }
1338
1339 // Return the destination address of the relocation. LSB stores the THUMB
1340 // bit.
1341 Arm_address
1342 destination() const
1343 { return this->destination_; }
1344
1345 private:
1346 // Associated relocation stub if there is one, or NULL.
1347 const Reloc_stub* reloc_stub_;
1348 // Relocation type.
1349 unsigned int r_type_;
1350 // Destination address of this relocation. LSB is used to distinguish
1351 // ARM/THUMB mode.
1352 Arm_address destination_;
1353 };
1354
1355 // Utilities for manipulating integers of up to 32-bits
1356
1357 namespace utils
1358 {
1359 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1360 // an int32_t. NO_BITS must be between 1 to 32.
1361 template<int no_bits>
1362 static inline int32_t
1363 sign_extend(uint32_t bits)
1364 {
1365 gold_assert(no_bits >= 0 && no_bits <= 32);
1366 if (no_bits == 32)
1367 return static_cast<int32_t>(bits);
1368 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1369 bits &= mask;
1370 uint32_t top_bit = 1U << (no_bits - 1);
1371 int32_t as_signed = static_cast<int32_t>(bits);
1372 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1373 }
1374
1375 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1376 template<int no_bits>
1377 static inline bool
1378 has_overflow(uint32_t bits)
1379 {
1380 gold_assert(no_bits >= 0 && no_bits <= 32);
1381 if (no_bits == 32)
1382 return false;
1383 int32_t max = (1 << (no_bits - 1)) - 1;
1384 int32_t min = -(1 << (no_bits - 1));
1385 int32_t as_signed = static_cast<int32_t>(bits);
1386 return as_signed > max || as_signed < min;
1387 }
1388
1389 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1390 // fits in the given number of bits as either a signed or unsigned value.
1391 // For example, has_signed_unsigned_overflow<8> would check
1392 // -128 <= bits <= 255
1393 template<int no_bits>
1394 static inline bool
1395 has_signed_unsigned_overflow(uint32_t bits)
1396 {
1397 gold_assert(no_bits >= 2 && no_bits <= 32);
1398 if (no_bits == 32)
1399 return false;
1400 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1401 int32_t min = -(1 << (no_bits - 1));
1402 int32_t as_signed = static_cast<int32_t>(bits);
1403 return as_signed > max || as_signed < min;
1404 }
1405
1406 // Select bits from A and B using bits in MASK. For each n in [0..31],
1407 // the n-th bit in the result is chosen from the n-th bits of A and B.
1408 // A zero selects A and a one selects B.
1409 static inline uint32_t
1410 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1411 { return (a & ~mask) | (b & mask); }
1412 };
1413
1414 template<bool big_endian>
1415 class Target_arm : public Sized_target<32, big_endian>
1416 {
1417 public:
1418 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1419 Reloc_section;
1420
1421 // When were are relocating a stub, we pass this as the relocation number.
1422 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1423
1424 Target_arm()
1425 : Sized_target<32, big_endian>(&arm_info),
1426 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1427 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1428 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1429 should_force_pic_veneer_(false), arm_input_section_map_(),
1430 attributes_section_data_(NULL), fix_cortex_a8_(false),
1431 cortex_a8_relocs_info_()
1432 { }
1433
1434 // Whether we can use BLX.
1435 bool
1436 may_use_blx() const
1437 { return this->may_use_blx_; }
1438
1439 // Set use-BLX flag.
1440 void
1441 set_may_use_blx(bool value)
1442 { this->may_use_blx_ = value; }
1443
1444 // Whether we force PCI branch veneers.
1445 bool
1446 should_force_pic_veneer() const
1447 { return this->should_force_pic_veneer_; }
1448
1449 // Set PIC veneer flag.
1450 void
1451 set_should_force_pic_veneer(bool value)
1452 { this->should_force_pic_veneer_ = value; }
1453
1454 // Whether we use THUMB-2 instructions.
1455 bool
1456 using_thumb2() const
1457 {
1458 Object_attribute* attr =
1459 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1460 int arch = attr->int_value();
1461 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
1462 }
1463
1464 // Whether we use THUMB/THUMB-2 instructions only.
1465 bool
1466 using_thumb_only() const
1467 {
1468 Object_attribute* attr =
1469 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1470 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1471 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1472 return false;
1473 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1474 return attr->int_value() == 'M';
1475 }
1476
1477 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1478 bool
1479 may_use_arm_nop() const
1480 {
1481 Object_attribute* attr =
1482 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1483 int arch = attr->int_value();
1484 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1485 || arch == elfcpp::TAG_CPU_ARCH_V6K
1486 || arch == elfcpp::TAG_CPU_ARCH_V7
1487 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1488 }
1489
1490 // Whether we have THUMB-2 NOP.W instruction.
1491 bool
1492 may_use_thumb2_nop() const
1493 {
1494 Object_attribute* attr =
1495 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1496 int arch = attr->int_value();
1497 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1498 || arch == elfcpp::TAG_CPU_ARCH_V7
1499 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1500 }
1501
1502 // Process the relocations to determine unreferenced sections for
1503 // garbage collection.
1504 void
1505 gc_process_relocs(Symbol_table* symtab,
1506 Layout* layout,
1507 Sized_relobj<32, big_endian>* object,
1508 unsigned int data_shndx,
1509 unsigned int sh_type,
1510 const unsigned char* prelocs,
1511 size_t reloc_count,
1512 Output_section* output_section,
1513 bool needs_special_offset_handling,
1514 size_t local_symbol_count,
1515 const unsigned char* plocal_symbols);
1516
1517 // Scan the relocations to look for symbol adjustments.
1518 void
1519 scan_relocs(Symbol_table* symtab,
1520 Layout* layout,
1521 Sized_relobj<32, big_endian>* object,
1522 unsigned int data_shndx,
1523 unsigned int sh_type,
1524 const unsigned char* prelocs,
1525 size_t reloc_count,
1526 Output_section* output_section,
1527 bool needs_special_offset_handling,
1528 size_t local_symbol_count,
1529 const unsigned char* plocal_symbols);
1530
1531 // Finalize the sections.
1532 void
1533 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1534
1535 // Return the value to use for a dynamic symbol which requires special
1536 // treatment.
1537 uint64_t
1538 do_dynsym_value(const Symbol*) const;
1539
1540 // Relocate a section.
1541 void
1542 relocate_section(const Relocate_info<32, big_endian>*,
1543 unsigned int sh_type,
1544 const unsigned char* prelocs,
1545 size_t reloc_count,
1546 Output_section* output_section,
1547 bool needs_special_offset_handling,
1548 unsigned char* view,
1549 Arm_address view_address,
1550 section_size_type view_size,
1551 const Reloc_symbol_changes*);
1552
1553 // Scan the relocs during a relocatable link.
1554 void
1555 scan_relocatable_relocs(Symbol_table* symtab,
1556 Layout* layout,
1557 Sized_relobj<32, big_endian>* object,
1558 unsigned int data_shndx,
1559 unsigned int sh_type,
1560 const unsigned char* prelocs,
1561 size_t reloc_count,
1562 Output_section* output_section,
1563 bool needs_special_offset_handling,
1564 size_t local_symbol_count,
1565 const unsigned char* plocal_symbols,
1566 Relocatable_relocs*);
1567
1568 // Relocate a section during a relocatable link.
1569 void
1570 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1571 unsigned int sh_type,
1572 const unsigned char* prelocs,
1573 size_t reloc_count,
1574 Output_section* output_section,
1575 off_t offset_in_output_section,
1576 const Relocatable_relocs*,
1577 unsigned char* view,
1578 Arm_address view_address,
1579 section_size_type view_size,
1580 unsigned char* reloc_view,
1581 section_size_type reloc_view_size);
1582
1583 // Return whether SYM is defined by the ABI.
1584 bool
1585 do_is_defined_by_abi(Symbol* sym) const
1586 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1587
1588 // Return the size of the GOT section.
1589 section_size_type
1590 got_size()
1591 {
1592 gold_assert(this->got_ != NULL);
1593 return this->got_->data_size();
1594 }
1595
1596 // Map platform-specific reloc types
1597 static unsigned int
1598 get_real_reloc_type (unsigned int r_type);
1599
1600 //
1601 // Methods to support stub-generations.
1602 //
1603
1604 // Return the stub factory
1605 const Stub_factory&
1606 stub_factory() const
1607 { return this->stub_factory_; }
1608
1609 // Make a new Arm_input_section object.
1610 Arm_input_section<big_endian>*
1611 new_arm_input_section(Relobj*, unsigned int);
1612
1613 // Find the Arm_input_section object corresponding to the SHNDX-th input
1614 // section of RELOBJ.
1615 Arm_input_section<big_endian>*
1616 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
1617
1618 // Make a new Stub_table
1619 Stub_table<big_endian>*
1620 new_stub_table(Arm_input_section<big_endian>*);
1621
1622 // Scan a section for stub generation.
1623 void
1624 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
1625 const unsigned char*, size_t, Output_section*,
1626 bool, const unsigned char*, Arm_address,
1627 section_size_type);
1628
1629 // Relocate a stub.
1630 void
1631 relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1632 Output_section*, unsigned char*, Arm_address,
1633 section_size_type);
1634
1635 // Get the default ARM target.
1636 static Target_arm<big_endian>*
1637 default_target()
1638 {
1639 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1640 && parameters->target().is_big_endian() == big_endian);
1641 return static_cast<Target_arm<big_endian>*>(
1642 parameters->sized_target<32, big_endian>());
1643 }
1644
1645 // Whether relocation type uses LSB to distinguish THUMB addresses.
1646 static bool
1647 reloc_uses_thumb_bit(unsigned int r_type);
1648
1649 // Whether NAME belongs to a mapping symbol.
1650 static bool
1651 is_mapping_symbol_name(const char* name)
1652 {
1653 return (name
1654 && name[0] == '$'
1655 && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
1656 && (name[2] == '\0' || name[2] == '.'));
1657 }
1658
1659 // Whether we work around the Cortex-A8 erratum.
1660 bool
1661 fix_cortex_a8() const
1662 { return this->fix_cortex_a8_; }
1663
1664 protected:
1665 // Make an ELF object.
1666 Object*
1667 do_make_elf_object(const std::string&, Input_file*, off_t,
1668 const elfcpp::Ehdr<32, big_endian>& ehdr);
1669
1670 Object*
1671 do_make_elf_object(const std::string&, Input_file*, off_t,
1672 const elfcpp::Ehdr<32, !big_endian>&)
1673 { gold_unreachable(); }
1674
1675 Object*
1676 do_make_elf_object(const std::string&, Input_file*, off_t,
1677 const elfcpp::Ehdr<64, false>&)
1678 { gold_unreachable(); }
1679
1680 Object*
1681 do_make_elf_object(const std::string&, Input_file*, off_t,
1682 const elfcpp::Ehdr<64, true>&)
1683 { gold_unreachable(); }
1684
1685 // Make an output section.
1686 Output_section*
1687 do_make_output_section(const char* name, elfcpp::Elf_Word type,
1688 elfcpp::Elf_Xword flags)
1689 { return new Arm_output_section<big_endian>(name, type, flags); }
1690
1691 void
1692 do_adjust_elf_header(unsigned char* view, int len) const;
1693
1694 // We only need to generate stubs, and hence perform relaxation if we are
1695 // not doing relocatable linking.
1696 bool
1697 do_may_relax() const
1698 { return !parameters->options().relocatable(); }
1699
1700 bool
1701 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
1702
1703 // Determine whether an object attribute tag takes an integer, a
1704 // string or both.
1705 int
1706 do_attribute_arg_type(int tag) const;
1707
1708 // Reorder tags during output.
1709 int
1710 do_attributes_order(int num) const;
1711
1712 private:
1713 // The class which scans relocations.
1714 class Scan
1715 {
1716 public:
1717 Scan()
1718 : issued_non_pic_error_(false)
1719 { }
1720
1721 inline void
1722 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1723 Sized_relobj<32, big_endian>* object,
1724 unsigned int data_shndx,
1725 Output_section* output_section,
1726 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1727 const elfcpp::Sym<32, big_endian>& lsym);
1728
1729 inline void
1730 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1731 Sized_relobj<32, big_endian>* object,
1732 unsigned int data_shndx,
1733 Output_section* output_section,
1734 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1735 Symbol* gsym);
1736
1737 private:
1738 static void
1739 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1740 unsigned int r_type);
1741
1742 static void
1743 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1744 unsigned int r_type, Symbol*);
1745
1746 void
1747 check_non_pic(Relobj*, unsigned int r_type);
1748
1749 // Almost identical to Symbol::needs_plt_entry except that it also
1750 // handles STT_ARM_TFUNC.
1751 static bool
1752 symbol_needs_plt_entry(const Symbol* sym)
1753 {
1754 // An undefined symbol from an executable does not need a PLT entry.
1755 if (sym->is_undefined() && !parameters->options().shared())
1756 return false;
1757
1758 return (!parameters->doing_static_link()
1759 && (sym->type() == elfcpp::STT_FUNC
1760 || sym->type() == elfcpp::STT_ARM_TFUNC)
1761 && (sym->is_from_dynobj()
1762 || sym->is_undefined()
1763 || sym->is_preemptible()));
1764 }
1765
1766 // Whether we have issued an error about a non-PIC compilation.
1767 bool issued_non_pic_error_;
1768 };
1769
1770 // The class which implements relocation.
1771 class Relocate
1772 {
1773 public:
1774 Relocate()
1775 { }
1776
1777 ~Relocate()
1778 { }
1779
1780 // Return whether the static relocation needs to be applied.
1781 inline bool
1782 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1783 int ref_flags,
1784 bool is_32bit,
1785 Output_section* output_section);
1786
1787 // Do a relocation. Return false if the caller should not issue
1788 // any warnings about this relocation.
1789 inline bool
1790 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1791 Output_section*, size_t relnum,
1792 const elfcpp::Rel<32, big_endian>&,
1793 unsigned int r_type, const Sized_symbol<32>*,
1794 const Symbol_value<32>*,
1795 unsigned char*, Arm_address,
1796 section_size_type);
1797
1798 // Return whether we want to pass flag NON_PIC_REF for this
1799 // reloc. This means the relocation type accesses a symbol not via
1800 // GOT or PLT.
1801 static inline bool
1802 reloc_is_non_pic (unsigned int r_type)
1803 {
1804 switch (r_type)
1805 {
1806 // These relocation types reference GOT or PLT entries explicitly.
1807 case elfcpp::R_ARM_GOT_BREL:
1808 case elfcpp::R_ARM_GOT_ABS:
1809 case elfcpp::R_ARM_GOT_PREL:
1810 case elfcpp::R_ARM_GOT_BREL12:
1811 case elfcpp::R_ARM_PLT32_ABS:
1812 case elfcpp::R_ARM_TLS_GD32:
1813 case elfcpp::R_ARM_TLS_LDM32:
1814 case elfcpp::R_ARM_TLS_IE32:
1815 case elfcpp::R_ARM_TLS_IE12GP:
1816
1817 // These relocate types may use PLT entries.
1818 case elfcpp::R_ARM_CALL:
1819 case elfcpp::R_ARM_THM_CALL:
1820 case elfcpp::R_ARM_JUMP24:
1821 case elfcpp::R_ARM_THM_JUMP24:
1822 case elfcpp::R_ARM_THM_JUMP19:
1823 case elfcpp::R_ARM_PLT32:
1824 case elfcpp::R_ARM_THM_XPC22:
1825 return false;
1826
1827 default:
1828 return true;
1829 }
1830 }
1831 };
1832
1833 // A class which returns the size required for a relocation type,
1834 // used while scanning relocs during a relocatable link.
1835 class Relocatable_size_for_reloc
1836 {
1837 public:
1838 unsigned int
1839 get_size_for_reloc(unsigned int, Relobj*);
1840 };
1841
1842 // Get the GOT section, creating it if necessary.
1843 Output_data_got<32, big_endian>*
1844 got_section(Symbol_table*, Layout*);
1845
1846 // Get the GOT PLT section.
1847 Output_data_space*
1848 got_plt_section() const
1849 {
1850 gold_assert(this->got_plt_ != NULL);
1851 return this->got_plt_;
1852 }
1853
1854 // Create a PLT entry for a global symbol.
1855 void
1856 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1857
1858 // Get the PLT section.
1859 const Output_data_plt_arm<big_endian>*
1860 plt_section() const
1861 {
1862 gold_assert(this->plt_ != NULL);
1863 return this->plt_;
1864 }
1865
1866 // Get the dynamic reloc section, creating it if necessary.
1867 Reloc_section*
1868 rel_dyn_section(Layout*);
1869
1870 // Return true if the symbol may need a COPY relocation.
1871 // References from an executable object to non-function symbols
1872 // defined in a dynamic object may need a COPY relocation.
1873 bool
1874 may_need_copy_reloc(Symbol* gsym)
1875 {
1876 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1877 && gsym->may_need_copy_reloc());
1878 }
1879
1880 // Add a potential copy relocation.
1881 void
1882 copy_reloc(Symbol_table* symtab, Layout* layout,
1883 Sized_relobj<32, big_endian>* object,
1884 unsigned int shndx, Output_section* output_section,
1885 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1886 {
1887 this->copy_relocs_.copy_reloc(symtab, layout,
1888 symtab->get_sized_symbol<32>(sym),
1889 object, shndx, output_section, reloc,
1890 this->rel_dyn_section(layout));
1891 }
1892
1893 // Whether two EABI versions are compatible.
1894 static bool
1895 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1896
1897 // Merge processor-specific flags from input object and those in the ELF
1898 // header of the output.
1899 void
1900 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1901
1902 // Get the secondary compatible architecture.
1903 static int
1904 get_secondary_compatible_arch(const Attributes_section_data*);
1905
1906 // Set the secondary compatible architecture.
1907 static void
1908 set_secondary_compatible_arch(Attributes_section_data*, int);
1909
1910 static int
1911 tag_cpu_arch_combine(const char*, int, int*, int, int);
1912
1913 // Helper to print AEABI enum tag value.
1914 static std::string
1915 aeabi_enum_name(unsigned int);
1916
1917 // Return string value for TAG_CPU_name.
1918 static std::string
1919 tag_cpu_name_value(unsigned int);
1920
1921 // Merge object attributes from input object and those in the output.
1922 void
1923 merge_object_attributes(const char*, const Attributes_section_data*);
1924
1925 // Helper to get an AEABI object attribute
1926 Object_attribute*
1927 get_aeabi_object_attribute(int tag) const
1928 {
1929 Attributes_section_data* pasd = this->attributes_section_data_;
1930 gold_assert(pasd != NULL);
1931 Object_attribute* attr =
1932 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
1933 gold_assert(attr != NULL);
1934 return attr;
1935 }
1936
1937 //
1938 // Methods to support stub-generations.
1939 //
1940
1941 // Group input sections for stub generation.
1942 void
1943 group_sections(Layout*, section_size_type, bool);
1944
1945 // Scan a relocation for stub generation.
1946 void
1947 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
1948 const Sized_symbol<32>*, unsigned int,
1949 const Symbol_value<32>*,
1950 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
1951
1952 // Scan a relocation section for stub.
1953 template<int sh_type>
1954 void
1955 scan_reloc_section_for_stubs(
1956 const Relocate_info<32, big_endian>* relinfo,
1957 const unsigned char* prelocs,
1958 size_t reloc_count,
1959 Output_section* output_section,
1960 bool needs_special_offset_handling,
1961 const unsigned char* view,
1962 elfcpp::Elf_types<32>::Elf_Addr view_address,
1963 section_size_type);
1964
1965 // Information about this specific target which we pass to the
1966 // general Target structure.
1967 static const Target::Target_info arm_info;
1968
1969 // The types of GOT entries needed for this platform.
1970 enum Got_type
1971 {
1972 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1973 };
1974
1975 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
1976
1977 // Map input section to Arm_input_section.
1978 typedef Unordered_map<Input_section_specifier,
1979 Arm_input_section<big_endian>*,
1980 Input_section_specifier::hash,
1981 Input_section_specifier::equal_to>
1982 Arm_input_section_map;
1983
1984 // Map output addresses to relocs for Cortex-A8 erratum.
1985 typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
1986 Cortex_a8_relocs_info;
1987
1988 // The GOT section.
1989 Output_data_got<32, big_endian>* got_;
1990 // The PLT section.
1991 Output_data_plt_arm<big_endian>* plt_;
1992 // The GOT PLT section.
1993 Output_data_space* got_plt_;
1994 // The dynamic reloc section.
1995 Reloc_section* rel_dyn_;
1996 // Relocs saved to avoid a COPY reloc.
1997 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1998 // Space for variables copied with a COPY reloc.
1999 Output_data_space* dynbss_;
2000 // Vector of Stub_tables created.
2001 Stub_table_list stub_tables_;
2002 // Stub factory.
2003 const Stub_factory &stub_factory_;
2004 // Whether we can use BLX.
2005 bool may_use_blx_;
2006 // Whether we force PIC branch veneers.
2007 bool should_force_pic_veneer_;
2008 // Map for locating Arm_input_sections.
2009 Arm_input_section_map arm_input_section_map_;
2010 // Attributes section data in output.
2011 Attributes_section_data* attributes_section_data_;
2012 // Whether we want to fix code for Cortex-A8 erratum.
2013 bool fix_cortex_a8_;
2014 // Map addresses to relocs for Cortex-A8 erratum.
2015 Cortex_a8_relocs_info cortex_a8_relocs_info_;
2016 };
2017
2018 template<bool big_endian>
2019 const Target::Target_info Target_arm<big_endian>::arm_info =
2020 {
2021 32, // size
2022 big_endian, // is_big_endian
2023 elfcpp::EM_ARM, // machine_code
2024 false, // has_make_symbol
2025 false, // has_resolve
2026 false, // has_code_fill
2027 true, // is_default_stack_executable
2028 '\0', // wrap_char
2029 "/usr/lib/libc.so.1", // dynamic_linker
2030 0x8000, // default_text_segment_address
2031 0x1000, // abi_pagesize (overridable by -z max-page-size)
2032 0x1000, // common_pagesize (overridable by -z common-page-size)
2033 elfcpp::SHN_UNDEF, // small_common_shndx
2034 elfcpp::SHN_UNDEF, // large_common_shndx
2035 0, // small_common_section_flags
2036 0, // large_common_section_flags
2037 ".ARM.attributes", // attributes_section
2038 "aeabi" // attributes_vendor
2039 };
2040
2041 // Arm relocate functions class
2042 //
2043
2044 template<bool big_endian>
2045 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2046 {
2047 public:
2048 typedef enum
2049 {
2050 STATUS_OKAY, // No error during relocation.
2051 STATUS_OVERFLOW, // Relocation oveflow.
2052 STATUS_BAD_RELOC // Relocation cannot be applied.
2053 } Status;
2054
2055 private:
2056 typedef Relocate_functions<32, big_endian> Base;
2057 typedef Arm_relocate_functions<big_endian> This;
2058
2059 // Encoding of imm16 argument for movt and movw ARM instructions
2060 // from ARM ARM:
2061 //
2062 // imm16 := imm4 | imm12
2063 //
2064 // 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
2065 // +-------+---------------+-------+-------+-----------------------+
2066 // | | |imm4 | |imm12 |
2067 // +-------+---------------+-------+-------+-----------------------+
2068
2069 // Extract the relocation addend from VAL based on the ARM
2070 // instruction encoding described above.
2071 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2072 extract_arm_movw_movt_addend(
2073 typename elfcpp::Swap<32, big_endian>::Valtype val)
2074 {
2075 // According to the Elf ABI for ARM Architecture the immediate
2076 // field is sign-extended to form the addend.
2077 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2078 }
2079
2080 // Insert X into VAL based on the ARM instruction encoding described
2081 // above.
2082 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2083 insert_val_arm_movw_movt(
2084 typename elfcpp::Swap<32, big_endian>::Valtype val,
2085 typename elfcpp::Swap<32, big_endian>::Valtype x)
2086 {
2087 val &= 0xfff0f000;
2088 val |= x & 0x0fff;
2089 val |= (x & 0xf000) << 4;
2090 return val;
2091 }
2092
2093 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2094 // from ARM ARM:
2095 //
2096 // imm16 := imm4 | i | imm3 | imm8
2097 //
2098 // 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
2099 // +---------+-+-----------+-------++-+-----+-------+---------------+
2100 // | |i| |imm4 || |imm3 | |imm8 |
2101 // +---------+-+-----------+-------++-+-----+-------+---------------+
2102
2103 // Extract the relocation addend from VAL based on the Thumb2
2104 // instruction encoding described above.
2105 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2106 extract_thumb_movw_movt_addend(
2107 typename elfcpp::Swap<32, big_endian>::Valtype val)
2108 {
2109 // According to the Elf ABI for ARM Architecture the immediate
2110 // field is sign-extended to form the addend.
2111 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2112 | ((val >> 15) & 0x0800)
2113 | ((val >> 4) & 0x0700)
2114 | (val & 0x00ff));
2115 }
2116
2117 // Insert X into VAL based on the Thumb2 instruction encoding
2118 // described above.
2119 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2120 insert_val_thumb_movw_movt(
2121 typename elfcpp::Swap<32, big_endian>::Valtype val,
2122 typename elfcpp::Swap<32, big_endian>::Valtype x)
2123 {
2124 val &= 0xfbf08f00;
2125 val |= (x & 0xf000) << 4;
2126 val |= (x & 0x0800) << 15;
2127 val |= (x & 0x0700) << 4;
2128 val |= (x & 0x00ff);
2129 return val;
2130 }
2131
2132 // Handle ARM long branches.
2133 static typename This::Status
2134 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2135 unsigned char *, const Sized_symbol<32>*,
2136 const Arm_relobj<big_endian>*, unsigned int,
2137 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2138
2139 // Handle THUMB long branches.
2140 static typename This::Status
2141 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2142 unsigned char *, const Sized_symbol<32>*,
2143 const Arm_relobj<big_endian>*, unsigned int,
2144 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2145
2146 public:
2147
2148 // Return the branch offset of a 32-bit THUMB branch.
2149 static inline int32_t
2150 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2151 {
2152 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2153 // involving the J1 and J2 bits.
2154 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2155 uint32_t upper = upper_insn & 0x3ffU;
2156 uint32_t lower = lower_insn & 0x7ffU;
2157 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2158 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2159 uint32_t i1 = j1 ^ s ? 0 : 1;
2160 uint32_t i2 = j2 ^ s ? 0 : 1;
2161
2162 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2163 | (upper << 12) | (lower << 1));
2164 }
2165
2166 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2167 // UPPER_INSN is the original upper instruction of the branch. Caller is
2168 // responsible for overflow checking and BLX offset adjustment.
2169 static inline uint16_t
2170 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2171 {
2172 uint32_t s = offset < 0 ? 1 : 0;
2173 uint32_t bits = static_cast<uint32_t>(offset);
2174 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2175 }
2176
2177 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2178 // LOWER_INSN is the original lower instruction of the branch. Caller is
2179 // responsible for overflow checking and BLX offset adjustment.
2180 static inline uint16_t
2181 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2182 {
2183 uint32_t s = offset < 0 ? 1 : 0;
2184 uint32_t bits = static_cast<uint32_t>(offset);
2185 return ((lower_insn & ~0x2fffU)
2186 | ((((bits >> 23) & 1) ^ !s) << 13)
2187 | ((((bits >> 22) & 1) ^ !s) << 11)
2188 | ((bits >> 1) & 0x7ffU));
2189 }
2190
2191 // Return the branch offset of a 32-bit THUMB conditional branch.
2192 static inline int32_t
2193 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2194 {
2195 uint32_t s = (upper_insn & 0x0400U) >> 10;
2196 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2197 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2198 uint32_t lower = (lower_insn & 0x07ffU);
2199 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2200
2201 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2202 }
2203
2204 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2205 // instruction. UPPER_INSN is the original upper instruction of the branch.
2206 // Caller is responsible for overflow checking.
2207 static inline uint16_t
2208 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2209 {
2210 uint32_t s = offset < 0 ? 1 : 0;
2211 uint32_t bits = static_cast<uint32_t>(offset);
2212 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2213 }
2214
2215 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2216 // instruction. LOWER_INSN is the original lower instruction of the branch.
2217 // Caller is reponsible for overflow checking.
2218 static inline uint16_t
2219 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2220 {
2221 uint32_t bits = static_cast<uint32_t>(offset);
2222 uint32_t j2 = (bits & 0x00080000U) >> 19;
2223 uint32_t j1 = (bits & 0x00040000U) >> 18;
2224 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2225
2226 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2227 }
2228
2229 // R_ARM_ABS8: S + A
2230 static inline typename This::Status
2231 abs8(unsigned char *view,
2232 const Sized_relobj<32, big_endian>* object,
2233 const Symbol_value<32>* psymval)
2234 {
2235 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2236 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2237 Valtype* wv = reinterpret_cast<Valtype*>(view);
2238 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2239 Reltype addend = utils::sign_extend<8>(val);
2240 Reltype x = psymval->value(object, addend);
2241 val = utils::bit_select(val, x, 0xffU);
2242 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2243 return (utils::has_signed_unsigned_overflow<8>(x)
2244 ? This::STATUS_OVERFLOW
2245 : This::STATUS_OKAY);
2246 }
2247
2248 // R_ARM_THM_ABS5: S + A
2249 static inline typename This::Status
2250 thm_abs5(unsigned char *view,
2251 const Sized_relobj<32, big_endian>* object,
2252 const Symbol_value<32>* psymval)
2253 {
2254 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2255 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2256 Valtype* wv = reinterpret_cast<Valtype*>(view);
2257 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2258 Reltype addend = (val & 0x7e0U) >> 6;
2259 Reltype x = psymval->value(object, addend);
2260 val = utils::bit_select(val, x << 6, 0x7e0U);
2261 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2262 return (utils::has_overflow<5>(x)
2263 ? This::STATUS_OVERFLOW
2264 : This::STATUS_OKAY);
2265 }
2266
2267 // R_ARM_ABS12: S + A
2268 static inline typename This::Status
2269 abs12(unsigned char *view,
2270 const Sized_relobj<32, big_endian>* object,
2271 const Symbol_value<32>* psymval)
2272 {
2273 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2274 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2275 Valtype* wv = reinterpret_cast<Valtype*>(view);
2276 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2277 Reltype addend = val & 0x0fffU;
2278 Reltype x = psymval->value(object, addend);
2279 val = utils::bit_select(val, x, 0x0fffU);
2280 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2281 return (utils::has_overflow<12>(x)
2282 ? This::STATUS_OVERFLOW
2283 : This::STATUS_OKAY);
2284 }
2285
2286 // R_ARM_ABS16: S + A
2287 static inline typename This::Status
2288 abs16(unsigned char *view,
2289 const Sized_relobj<32, big_endian>* object,
2290 const Symbol_value<32>* psymval)
2291 {
2292 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2293 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2294 Valtype* wv = reinterpret_cast<Valtype*>(view);
2295 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2296 Reltype addend = utils::sign_extend<16>(val);
2297 Reltype x = psymval->value(object, addend);
2298 val = utils::bit_select(val, x, 0xffffU);
2299 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2300 return (utils::has_signed_unsigned_overflow<16>(x)
2301 ? This::STATUS_OVERFLOW
2302 : This::STATUS_OKAY);
2303 }
2304
2305 // R_ARM_ABS32: (S + A) | T
2306 static inline typename This::Status
2307 abs32(unsigned char *view,
2308 const Sized_relobj<32, big_endian>* object,
2309 const Symbol_value<32>* psymval,
2310 Arm_address thumb_bit)
2311 {
2312 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2313 Valtype* wv = reinterpret_cast<Valtype*>(view);
2314 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2315 Valtype x = psymval->value(object, addend) | thumb_bit;
2316 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2317 return This::STATUS_OKAY;
2318 }
2319
2320 // R_ARM_REL32: (S + A) | T - P
2321 static inline typename This::Status
2322 rel32(unsigned char *view,
2323 const Sized_relobj<32, big_endian>* object,
2324 const Symbol_value<32>* psymval,
2325 Arm_address address,
2326 Arm_address thumb_bit)
2327 {
2328 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2329 Valtype* wv = reinterpret_cast<Valtype*>(view);
2330 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2331 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2332 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2333 return This::STATUS_OKAY;
2334 }
2335
2336 // R_ARM_THM_CALL: (S + A) | T - P
2337 static inline typename This::Status
2338 thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2339 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2340 unsigned int r_sym, const Symbol_value<32>* psymval,
2341 Arm_address address, Arm_address thumb_bit,
2342 bool is_weakly_undefined_without_plt)
2343 {
2344 return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
2345 object, r_sym, psymval, address, thumb_bit,
2346 is_weakly_undefined_without_plt);
2347 }
2348
2349 // R_ARM_THM_JUMP24: (S + A) | T - P
2350 static inline typename This::Status
2351 thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2352 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2353 unsigned int r_sym, const Symbol_value<32>* psymval,
2354 Arm_address address, Arm_address thumb_bit,
2355 bool is_weakly_undefined_without_plt)
2356 {
2357 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
2358 object, r_sym, psymval, address, thumb_bit,
2359 is_weakly_undefined_without_plt);
2360 }
2361
2362 // R_ARM_THM_JUMP24: (S + A) | T - P
2363 static typename This::Status
2364 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
2365 const Symbol_value<32>* psymval, Arm_address address,
2366 Arm_address thumb_bit);
2367
2368 // R_ARM_THM_XPC22: (S + A) | T - P
2369 static inline typename This::Status
2370 thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2371 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2372 unsigned int r_sym, const Symbol_value<32>* psymval,
2373 Arm_address address, Arm_address thumb_bit,
2374 bool is_weakly_undefined_without_plt)
2375 {
2376 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
2377 object, r_sym, psymval, address, thumb_bit,
2378 is_weakly_undefined_without_plt);
2379 }
2380
2381 // R_ARM_BASE_PREL: B(S) + A - P
2382 static inline typename This::Status
2383 base_prel(unsigned char* view,
2384 Arm_address origin,
2385 Arm_address address)
2386 {
2387 Base::rel32(view, origin - address);
2388 return STATUS_OKAY;
2389 }
2390
2391 // R_ARM_BASE_ABS: B(S) + A
2392 static inline typename This::Status
2393 base_abs(unsigned char* view,
2394 Arm_address origin)
2395 {
2396 Base::rel32(view, origin);
2397 return STATUS_OKAY;
2398 }
2399
2400 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2401 static inline typename This::Status
2402 got_brel(unsigned char* view,
2403 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
2404 {
2405 Base::rel32(view, got_offset);
2406 return This::STATUS_OKAY;
2407 }
2408
2409 // R_ARM_GOT_PREL: GOT(S) + A - P
2410 static inline typename This::Status
2411 got_prel(unsigned char *view,
2412 Arm_address got_entry,
2413 Arm_address address)
2414 {
2415 Base::rel32(view, got_entry - address);
2416 return This::STATUS_OKAY;
2417 }
2418
2419 // R_ARM_PLT32: (S + A) | T - P
2420 static inline typename This::Status
2421 plt32(const Relocate_info<32, big_endian>* relinfo,
2422 unsigned char *view,
2423 const Sized_symbol<32>* gsym,
2424 const Arm_relobj<big_endian>* object,
2425 unsigned int r_sym,
2426 const Symbol_value<32>* psymval,
2427 Arm_address address,
2428 Arm_address thumb_bit,
2429 bool is_weakly_undefined_without_plt)
2430 {
2431 return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
2432 object, r_sym, psymval, address, thumb_bit,
2433 is_weakly_undefined_without_plt);
2434 }
2435
2436 // R_ARM_XPC25: (S + A) | T - P
2437 static inline typename This::Status
2438 xpc25(const Relocate_info<32, big_endian>* relinfo,
2439 unsigned char *view,
2440 const Sized_symbol<32>* gsym,
2441 const Arm_relobj<big_endian>* object,
2442 unsigned int r_sym,
2443 const Symbol_value<32>* psymval,
2444 Arm_address address,
2445 Arm_address thumb_bit,
2446 bool is_weakly_undefined_without_plt)
2447 {
2448 return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
2449 object, r_sym, psymval, address, thumb_bit,
2450 is_weakly_undefined_without_plt);
2451 }
2452
2453 // R_ARM_CALL: (S + A) | T - P
2454 static inline typename This::Status
2455 call(const Relocate_info<32, big_endian>* relinfo,
2456 unsigned char *view,
2457 const Sized_symbol<32>* gsym,
2458 const Arm_relobj<big_endian>* object,
2459 unsigned int r_sym,
2460 const Symbol_value<32>* psymval,
2461 Arm_address address,
2462 Arm_address thumb_bit,
2463 bool is_weakly_undefined_without_plt)
2464 {
2465 return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2466 object, r_sym, psymval, address, thumb_bit,
2467 is_weakly_undefined_without_plt);
2468 }
2469
2470 // R_ARM_JUMP24: (S + A) | T - P
2471 static inline typename This::Status
2472 jump24(const Relocate_info<32, big_endian>* relinfo,
2473 unsigned char *view,
2474 const Sized_symbol<32>* gsym,
2475 const Arm_relobj<big_endian>* object,
2476 unsigned int r_sym,
2477 const Symbol_value<32>* psymval,
2478 Arm_address address,
2479 Arm_address thumb_bit,
2480 bool is_weakly_undefined_without_plt)
2481 {
2482 return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2483 object, r_sym, psymval, address, thumb_bit,
2484 is_weakly_undefined_without_plt);
2485 }
2486
2487 // R_ARM_PREL: (S + A) | T - P
2488 static inline typename This::Status
2489 prel31(unsigned char *view,
2490 const Sized_relobj<32, big_endian>* object,
2491 const Symbol_value<32>* psymval,
2492 Arm_address address,
2493 Arm_address thumb_bit)
2494 {
2495 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2496 Valtype* wv = reinterpret_cast<Valtype*>(view);
2497 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2498 Valtype addend = utils::sign_extend<31>(val);
2499 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2500 val = utils::bit_select(val, x, 0x7fffffffU);
2501 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2502 return (utils::has_overflow<31>(x) ?
2503 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2504 }
2505
2506 // R_ARM_MOVW_ABS_NC: (S + A) | T
2507 static inline typename This::Status
2508 movw_abs_nc(unsigned char *view,
2509 const Sized_relobj<32, big_endian>* object,
2510 const Symbol_value<32>* psymval,
2511 Arm_address thumb_bit)
2512 {
2513 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2514 Valtype* wv = reinterpret_cast<Valtype*>(view);
2515 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2516 Valtype addend = This::extract_arm_movw_movt_addend(val);
2517 Valtype x = psymval->value(object, addend) | thumb_bit;
2518 val = This::insert_val_arm_movw_movt(val, x);
2519 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2520 return This::STATUS_OKAY;
2521 }
2522
2523 // R_ARM_MOVT_ABS: S + A
2524 static inline typename This::Status
2525 movt_abs(unsigned char *view,
2526 const Sized_relobj<32, big_endian>* object,
2527 const Symbol_value<32>* psymval)
2528 {
2529 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2530 Valtype* wv = reinterpret_cast<Valtype*>(view);
2531 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2532 Valtype addend = This::extract_arm_movw_movt_addend(val);
2533 Valtype x = psymval->value(object, addend) >> 16;
2534 val = This::insert_val_arm_movw_movt(val, x);
2535 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2536 return This::STATUS_OKAY;
2537 }
2538
2539 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2540 static inline typename This::Status
2541 thm_movw_abs_nc(unsigned char *view,
2542 const Sized_relobj<32, big_endian>* object,
2543 const Symbol_value<32>* psymval,
2544 Arm_address thumb_bit)
2545 {
2546 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2547 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2548 Valtype* wv = reinterpret_cast<Valtype*>(view);
2549 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2550 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2551 Reltype addend = extract_thumb_movw_movt_addend(val);
2552 Reltype x = psymval->value(object, addend) | thumb_bit;
2553 val = This::insert_val_thumb_movw_movt(val, x);
2554 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2555 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2556 return This::STATUS_OKAY;
2557 }
2558
2559 // R_ARM_THM_MOVT_ABS: S + A
2560 static inline typename This::Status
2561 thm_movt_abs(unsigned char *view,
2562 const Sized_relobj<32, big_endian>* object,
2563 const Symbol_value<32>* psymval)
2564 {
2565 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2566 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2567 Valtype* wv = reinterpret_cast<Valtype*>(view);
2568 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2569 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2570 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2571 Reltype x = psymval->value(object, addend) >> 16;
2572 val = This::insert_val_thumb_movw_movt(val, x);
2573 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2574 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2575 return This::STATUS_OKAY;
2576 }
2577
2578 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2579 static inline typename This::Status
2580 movw_prel_nc(unsigned char *view,
2581 const Sized_relobj<32, big_endian>* object,
2582 const Symbol_value<32>* psymval,
2583 Arm_address address,
2584 Arm_address thumb_bit)
2585 {
2586 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2587 Valtype* wv = reinterpret_cast<Valtype*>(view);
2588 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2589 Valtype addend = This::extract_arm_movw_movt_addend(val);
2590 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2591 val = This::insert_val_arm_movw_movt(val, x);
2592 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2593 return This::STATUS_OKAY;
2594 }
2595
2596 // R_ARM_MOVT_PREL: S + A - P
2597 static inline typename This::Status
2598 movt_prel(unsigned char *view,
2599 const Sized_relobj<32, big_endian>* object,
2600 const Symbol_value<32>* psymval,
2601 Arm_address address)
2602 {
2603 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2604 Valtype* wv = reinterpret_cast<Valtype*>(view);
2605 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2606 Valtype addend = This::extract_arm_movw_movt_addend(val);
2607 Valtype x = (psymval->value(object, addend) - address) >> 16;
2608 val = This::insert_val_arm_movw_movt(val, x);
2609 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2610 return This::STATUS_OKAY;
2611 }
2612
2613 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2614 static inline typename This::Status
2615 thm_movw_prel_nc(unsigned char *view,
2616 const Sized_relobj<32, big_endian>* object,
2617 const Symbol_value<32>* psymval,
2618 Arm_address address,
2619 Arm_address thumb_bit)
2620 {
2621 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2622 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2623 Valtype* wv = reinterpret_cast<Valtype*>(view);
2624 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2625 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2626 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2627 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
2628 val = This::insert_val_thumb_movw_movt(val, x);
2629 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2630 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2631 return This::STATUS_OKAY;
2632 }
2633
2634 // R_ARM_THM_MOVT_PREL: S + A - P
2635 static inline typename This::Status
2636 thm_movt_prel(unsigned char *view,
2637 const Sized_relobj<32, big_endian>* object,
2638 const Symbol_value<32>* psymval,
2639 Arm_address address)
2640 {
2641 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2642 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2643 Valtype* wv = reinterpret_cast<Valtype*>(view);
2644 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2645 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2646 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2647 Reltype x = (psymval->value(object, addend) - address) >> 16;
2648 val = This::insert_val_thumb_movw_movt(val, x);
2649 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2650 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2651 return This::STATUS_OKAY;
2652 }
2653 };
2654
2655 // Relocate ARM long branches. This handles relocation types
2656 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2657 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2658 // undefined and we do not use PLT in this relocation. In such a case,
2659 // the branch is converted into an NOP.
2660
2661 template<bool big_endian>
2662 typename Arm_relocate_functions<big_endian>::Status
2663 Arm_relocate_functions<big_endian>::arm_branch_common(
2664 unsigned int r_type,
2665 const Relocate_info<32, big_endian>* relinfo,
2666 unsigned char *view,
2667 const Sized_symbol<32>* gsym,
2668 const Arm_relobj<big_endian>* object,
2669 unsigned int r_sym,
2670 const Symbol_value<32>* psymval,
2671 Arm_address address,
2672 Arm_address thumb_bit,
2673 bool is_weakly_undefined_without_plt)
2674 {
2675 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2676 Valtype* wv = reinterpret_cast<Valtype*>(view);
2677 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2678
2679 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
2680 && ((val & 0x0f000000UL) == 0x0a000000UL);
2681 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
2682 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
2683 && ((val & 0x0f000000UL) == 0x0b000000UL);
2684 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
2685 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
2686
2687 // Check that the instruction is valid.
2688 if (r_type == elfcpp::R_ARM_CALL)
2689 {
2690 if (!insn_is_uncond_bl && !insn_is_blx)
2691 return This::STATUS_BAD_RELOC;
2692 }
2693 else if (r_type == elfcpp::R_ARM_JUMP24)
2694 {
2695 if (!insn_is_b && !insn_is_cond_bl)
2696 return This::STATUS_BAD_RELOC;
2697 }
2698 else if (r_type == elfcpp::R_ARM_PLT32)
2699 {
2700 if (!insn_is_any_branch)
2701 return This::STATUS_BAD_RELOC;
2702 }
2703 else if (r_type == elfcpp::R_ARM_XPC25)
2704 {
2705 // FIXME: AAELF document IH0044C does not say much about it other
2706 // than it being obsolete.
2707 if (!insn_is_any_branch)
2708 return This::STATUS_BAD_RELOC;
2709 }
2710 else
2711 gold_unreachable();
2712
2713 // A branch to an undefined weak symbol is turned into a jump to
2714 // the next instruction unless a PLT entry will be created.
2715 // Do the same for local undefined symbols.
2716 // The jump to the next instruction is optimized as a NOP depending
2717 // on the architecture.
2718 const Target_arm<big_endian>* arm_target =
2719 Target_arm<big_endian>::default_target();
2720 if (is_weakly_undefined_without_plt)
2721 {
2722 Valtype cond = val & 0xf0000000U;
2723 if (arm_target->may_use_arm_nop())
2724 val = cond | 0x0320f000;
2725 else
2726 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2727 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2728 return This::STATUS_OKAY;
2729 }
2730
2731 Valtype addend = utils::sign_extend<26>(val << 2);
2732 Valtype branch_target = psymval->value(object, addend);
2733 int32_t branch_offset = branch_target - address;
2734
2735 // We need a stub if the branch offset is too large or if we need
2736 // to switch mode.
2737 bool may_use_blx = arm_target->may_use_blx();
2738 Reloc_stub* stub = NULL;
2739 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
2740 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2741 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
2742 {
2743 Stub_type stub_type =
2744 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2745 (thumb_bit != 0));
2746 if (stub_type != arm_stub_none)
2747 {
2748 Stub_table<big_endian>* stub_table =
2749 object->stub_table(relinfo->data_shndx);
2750 gold_assert(stub_table != NULL);
2751
2752 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2753 stub = stub_table->find_reloc_stub(stub_key);
2754 gold_assert(stub != NULL);
2755 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2756 branch_target = stub_table->address() + stub->offset() + addend;
2757 branch_offset = branch_target - address;
2758 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
2759 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
2760 }
2761 }
2762
2763 // At this point, if we still need to switch mode, the instruction
2764 // must either be a BLX or a BL that can be converted to a BLX.
2765 if (thumb_bit != 0)
2766 {
2767 // Turn BL to BLX.
2768 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
2769 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
2770 }
2771
2772 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
2773 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2774 return (utils::has_overflow<26>(branch_offset)
2775 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
2776 }
2777
2778 // Relocate THUMB long branches. This handles relocation types
2779 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2780 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2781 // undefined and we do not use PLT in this relocation. In such a case,
2782 // the branch is converted into an NOP.
2783
2784 template<bool big_endian>
2785 typename Arm_relocate_functions<big_endian>::Status
2786 Arm_relocate_functions<big_endian>::thumb_branch_common(
2787 unsigned int r_type,
2788 const Relocate_info<32, big_endian>* relinfo,
2789 unsigned char *view,
2790 const Sized_symbol<32>* gsym,
2791 const Arm_relobj<big_endian>* object,
2792 unsigned int r_sym,
2793 const Symbol_value<32>* psymval,
2794 Arm_address address,
2795 Arm_address thumb_bit,
2796 bool is_weakly_undefined_without_plt)
2797 {
2798 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2799 Valtype* wv = reinterpret_cast<Valtype*>(view);
2800 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2801 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2802
2803 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2804 // into account.
2805 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
2806 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
2807
2808 // Check that the instruction is valid.
2809 if (r_type == elfcpp::R_ARM_THM_CALL)
2810 {
2811 if (!is_bl_insn && !is_blx_insn)
2812 return This::STATUS_BAD_RELOC;
2813 }
2814 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
2815 {
2816 // This cannot be a BLX.
2817 if (!is_bl_insn)
2818 return This::STATUS_BAD_RELOC;
2819 }
2820 else if (r_type == elfcpp::R_ARM_THM_XPC22)
2821 {
2822 // Check for Thumb to Thumb call.
2823 if (!is_blx_insn)
2824 return This::STATUS_BAD_RELOC;
2825 if (thumb_bit != 0)
2826 {
2827 gold_warning(_("%s: Thumb BLX instruction targets "
2828 "thumb function '%s'."),
2829 object->name().c_str(),
2830 (gsym ? gsym->name() : "(local)"));
2831 // Convert BLX to BL.
2832 lower_insn |= 0x1000U;
2833 }
2834 }
2835 else
2836 gold_unreachable();
2837
2838 // A branch to an undefined weak symbol is turned into a jump to
2839 // the next instruction unless a PLT entry will be created.
2840 // The jump to the next instruction is optimized as a NOP.W for
2841 // Thumb-2 enabled architectures.
2842 const Target_arm<big_endian>* arm_target =
2843 Target_arm<big_endian>::default_target();
2844 if (is_weakly_undefined_without_plt)
2845 {
2846 if (arm_target->may_use_thumb2_nop())
2847 {
2848 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
2849 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
2850 }
2851 else
2852 {
2853 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
2854 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
2855 }
2856 return This::STATUS_OKAY;
2857 }
2858
2859 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
2860 Arm_address branch_target = psymval->value(object, addend);
2861 int32_t branch_offset = branch_target - address;
2862
2863 // We need a stub if the branch offset is too large or if we need
2864 // to switch mode.
2865 bool may_use_blx = arm_target->may_use_blx();
2866 bool thumb2 = arm_target->using_thumb2();
2867 if ((!thumb2
2868 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2869 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2870 || (thumb2
2871 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2872 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2873 || ((thumb_bit == 0)
2874 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2875 || r_type == elfcpp::R_ARM_THM_JUMP24)))
2876 {
2877 Stub_type stub_type =
2878 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2879 (thumb_bit != 0));
2880 if (stub_type != arm_stub_none)
2881 {
2882 Stub_table<big_endian>* stub_table =
2883 object->stub_table(relinfo->data_shndx);
2884 gold_assert(stub_table != NULL);
2885
2886 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2887 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
2888 gold_assert(stub != NULL);
2889 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2890 branch_target = stub_table->address() + stub->offset() + addend;
2891 branch_offset = branch_target - address;
2892 }
2893 }
2894
2895 // At this point, if we still need to switch mode, the instruction
2896 // must either be a BLX or a BL that can be converted to a BLX.
2897 if (thumb_bit == 0)
2898 {
2899 gold_assert(may_use_blx
2900 && (r_type == elfcpp::R_ARM_THM_CALL
2901 || r_type == elfcpp::R_ARM_THM_XPC22));
2902 // Make sure this is a BLX.
2903 lower_insn &= ~0x1000U;
2904 }
2905 else
2906 {
2907 // Make sure this is a BL.
2908 lower_insn |= 0x1000U;
2909 }
2910
2911 if ((lower_insn & 0x5000U) == 0x4000U)
2912 // For a BLX instruction, make sure that the relocation is rounded up
2913 // to a word boundary. This follows the semantics of the instruction
2914 // which specifies that bit 1 of the target address will come from bit
2915 // 1 of the base address.
2916 branch_offset = (branch_offset + 2) & ~3;
2917
2918 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2919 // We use the Thumb-2 encoding, which is safe even if dealing with
2920 // a Thumb-1 instruction by virtue of our overflow check above. */
2921 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
2922 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
2923
2924 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
2925 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
2926
2927 return ((thumb2
2928 ? utils::has_overflow<25>(branch_offset)
2929 : utils::has_overflow<23>(branch_offset))
2930 ? This::STATUS_OVERFLOW
2931 : This::STATUS_OKAY);
2932 }
2933
2934 // Relocate THUMB-2 long conditional branches.
2935 // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2936 // undefined and we do not use PLT in this relocation. In such a case,
2937 // the branch is converted into an NOP.
2938
2939 template<bool big_endian>
2940 typename Arm_relocate_functions<big_endian>::Status
2941 Arm_relocate_functions<big_endian>::thm_jump19(
2942 unsigned char *view,
2943 const Arm_relobj<big_endian>* object,
2944 const Symbol_value<32>* psymval,
2945 Arm_address address,
2946 Arm_address thumb_bit)
2947 {
2948 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2949 Valtype* wv = reinterpret_cast<Valtype*>(view);
2950 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2951 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2952 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
2953
2954 Arm_address branch_target = psymval->value(object, addend);
2955 int32_t branch_offset = branch_target - address;
2956
2957 // ??? Should handle interworking? GCC might someday try to
2958 // use this for tail calls.
2959 // FIXME: We do support thumb entry to PLT yet.
2960 if (thumb_bit == 0)
2961 {
2962 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
2963 return This::STATUS_BAD_RELOC;
2964 }
2965
2966 // Put RELOCATION back into the insn.
2967 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
2968 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
2969
2970 // Put the relocated value back in the object file:
2971 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
2972 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
2973
2974 return (utils::has_overflow<21>(branch_offset)
2975 ? This::STATUS_OVERFLOW
2976 : This::STATUS_OKAY);
2977 }
2978
2979 // Get the GOT section, creating it if necessary.
2980
2981 template<bool big_endian>
2982 Output_data_got<32, big_endian>*
2983 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2984 {
2985 if (this->got_ == NULL)
2986 {
2987 gold_assert(symtab != NULL && layout != NULL);
2988
2989 this->got_ = new Output_data_got<32, big_endian>();
2990
2991 Output_section* os;
2992 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2993 (elfcpp::SHF_ALLOC
2994 | elfcpp::SHF_WRITE),
2995 this->got_, false, true, true,
2996 false);
2997
2998 // The old GNU linker creates a .got.plt section. We just
2999 // create another set of data in the .got section. Note that we
3000 // always create a PLT if we create a GOT, although the PLT
3001 // might be empty.
3002 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
3003 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3004 (elfcpp::SHF_ALLOC
3005 | elfcpp::SHF_WRITE),
3006 this->got_plt_, false, false,
3007 false, true);
3008
3009 // The first three entries are reserved.
3010 this->got_plt_->set_current_data_size(3 * 4);
3011
3012 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3013 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3014 Symbol_table::PREDEFINED,
3015 this->got_plt_,
3016 0, 0, elfcpp::STT_OBJECT,
3017 elfcpp::STB_LOCAL,
3018 elfcpp::STV_HIDDEN, 0,
3019 false, false);
3020 }
3021 return this->got_;
3022 }
3023
3024 // Get the dynamic reloc section, creating it if necessary.
3025
3026 template<bool big_endian>
3027 typename Target_arm<big_endian>::Reloc_section*
3028 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
3029 {
3030 if (this->rel_dyn_ == NULL)
3031 {
3032 gold_assert(layout != NULL);
3033 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
3034 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
3035 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
3036 false, false, false);
3037 }
3038 return this->rel_dyn_;
3039 }
3040
3041 // Insn_template methods.
3042
3043 // Return byte size of an instruction template.
3044
3045 size_t
3046 Insn_template::size() const
3047 {
3048 switch (this->type())
3049 {
3050 case THUMB16_TYPE:
3051 case THUMB16_SPECIAL_TYPE:
3052 return 2;
3053 case ARM_TYPE:
3054 case THUMB32_TYPE:
3055 case DATA_TYPE:
3056 return 4;
3057 default:
3058 gold_unreachable();
3059 }
3060 }
3061
3062 // Return alignment of an instruction template.
3063
3064 unsigned
3065 Insn_template::alignment() const
3066 {
3067 switch (this->type())
3068 {
3069 case THUMB16_TYPE:
3070 case THUMB16_SPECIAL_TYPE:
3071 case THUMB32_TYPE:
3072 return 2;
3073 case ARM_TYPE:
3074 case DATA_TYPE:
3075 return 4;
3076 default:
3077 gold_unreachable();
3078 }
3079 }
3080
3081 // Stub_template methods.
3082
3083 Stub_template::Stub_template(
3084 Stub_type type, const Insn_template* insns,
3085 size_t insn_count)
3086 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
3087 entry_in_thumb_mode_(false), relocs_()
3088 {
3089 off_t offset = 0;
3090
3091 // Compute byte size and alignment of stub template.
3092 for (size_t i = 0; i < insn_count; i++)
3093 {
3094 unsigned insn_alignment = insns[i].alignment();
3095 size_t insn_size = insns[i].size();
3096 gold_assert((offset & (insn_alignment - 1)) == 0);
3097 this->alignment_ = std::max(this->alignment_, insn_alignment);
3098 switch (insns[i].type())
3099 {
3100 case Insn_template::THUMB16_TYPE:
3101 case Insn_template::THUMB16_SPECIAL_TYPE:
3102 if (i == 0)
3103 this->entry_in_thumb_mode_ = true;
3104 break;
3105
3106 case Insn_template::THUMB32_TYPE:
3107 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
3108 this->relocs_.push_back(Reloc(i, offset));
3109 if (i == 0)
3110 this->entry_in_thumb_mode_ = true;
3111 break;
3112
3113 case Insn_template::ARM_TYPE:
3114 // Handle cases where the target is encoded within the
3115 // instruction.
3116 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
3117 this->relocs_.push_back(Reloc(i, offset));
3118 break;
3119
3120 case Insn_template::DATA_TYPE:
3121 // Entry point cannot be data.
3122 gold_assert(i != 0);
3123 this->relocs_.push_back(Reloc(i, offset));
3124 break;
3125
3126 default:
3127 gold_unreachable();
3128 }
3129 offset += insn_size;
3130 }
3131 this->size_ = offset;
3132 }
3133
3134 // Stub methods.
3135
3136 // Template to implement do_write for a specific target endianity.
3137
3138 template<bool big_endian>
3139 void inline
3140 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
3141 {
3142 const Stub_template* stub_template = this->stub_template();
3143 const Insn_template* insns = stub_template->insns();
3144
3145 // FIXME: We do not handle BE8 encoding yet.
3146 unsigned char* pov = view;
3147 for (size_t i = 0; i < stub_template->insn_count(); i++)
3148 {
3149 switch (insns[i].type())
3150 {
3151 case Insn_template::THUMB16_TYPE:
3152 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
3153 break;
3154 case Insn_template::THUMB16_SPECIAL_TYPE:
3155 elfcpp::Swap<16, big_endian>::writeval(
3156 pov,
3157 this->thumb16_special(i));
3158 break;
3159 case Insn_template::THUMB32_TYPE:
3160 {
3161 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
3162 uint32_t lo = insns[i].data() & 0xffff;
3163 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
3164 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
3165 }
3166 break;
3167 case Insn_template::ARM_TYPE:
3168 case Insn_template::DATA_TYPE:
3169 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
3170 break;
3171 default:
3172 gold_unreachable();
3173 }
3174 pov += insns[i].size();
3175 }
3176 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
3177 }
3178
3179 // Reloc_stub::Key methods.
3180
3181 // Dump a Key as a string for debugging.
3182
3183 std::string
3184 Reloc_stub::Key::name() const
3185 {
3186 if (this->r_sym_ == invalid_index)
3187 {
3188 // Global symbol key name
3189 // <stub-type>:<symbol name>:<addend>.
3190 const std::string sym_name = this->u_.symbol->name();
3191 // We need to print two hex number and two colons. So just add 100 bytes
3192 // to the symbol name size.
3193 size_t len = sym_name.size() + 100;
3194 char* buffer = new char[len];
3195 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
3196 sym_name.c_str(), this->addend_);
3197 gold_assert(c > 0 && c < static_cast<int>(len));
3198 delete[] buffer;
3199 return std::string(buffer);
3200 }
3201 else
3202 {
3203 // local symbol key name
3204 // <stub-type>:<object>:<r_sym>:<addend>.
3205 const size_t len = 200;
3206 char buffer[len];
3207 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
3208 this->u_.relobj, this->r_sym_, this->addend_);
3209 gold_assert(c > 0 && c < static_cast<int>(len));
3210 return std::string(buffer);
3211 }
3212 }
3213
3214 // Reloc_stub methods.
3215
3216 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
3217 // LOCATION to DESTINATION.
3218 // This code is based on the arm_type_of_stub function in
3219 // bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3220 // class simple.
3221
3222 Stub_type
3223 Reloc_stub::stub_type_for_reloc(
3224 unsigned int r_type,
3225 Arm_address location,
3226 Arm_address destination,
3227 bool target_is_thumb)
3228 {
3229 Stub_type stub_type = arm_stub_none;
3230
3231 // This is a bit ugly but we want to avoid using a templated class for
3232 // big and little endianities.
3233 bool may_use_blx;
3234 bool should_force_pic_veneer;
3235 bool thumb2;
3236 bool thumb_only;
3237 if (parameters->target().is_big_endian())
3238 {
3239 const Target_arm<true>* big_endian_target =
3240 Target_arm<true>::default_target();
3241 may_use_blx = big_endian_target->may_use_blx();
3242 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
3243 thumb2 = big_endian_target->using_thumb2();
3244 thumb_only = big_endian_target->using_thumb_only();
3245 }
3246 else
3247 {
3248 const Target_arm<false>* little_endian_target =
3249 Target_arm<false>::default_target();
3250 may_use_blx = little_endian_target->may_use_blx();
3251 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
3252 thumb2 = little_endian_target->using_thumb2();
3253 thumb_only = little_endian_target->using_thumb_only();
3254 }
3255
3256 int64_t branch_offset = (int64_t)destination - location;
3257
3258 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
3259 {
3260 // Handle cases where:
3261 // - this call goes too far (different Thumb/Thumb2 max
3262 // distance)
3263 // - it's a Thumb->Arm call and blx is not available, or it's a
3264 // Thumb->Arm branch (not bl). A stub is needed in this case.
3265 if ((!thumb2
3266 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3267 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3268 || (thumb2
3269 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3270 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3271 || ((!target_is_thumb)
3272 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3273 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
3274 {
3275 if (target_is_thumb)
3276 {
3277 // Thumb to thumb.
3278 if (!thumb_only)
3279 {
3280 stub_type = (parameters->options().shared()
3281 || should_force_pic_veneer)
3282 // PIC stubs.
3283 ? ((may_use_blx
3284 && (r_type == elfcpp::R_ARM_THM_CALL))
3285 // V5T and above. Stub starts with ARM code, so
3286 // we must be able to switch mode before
3287 // reaching it, which is only possible for 'bl'
3288 // (ie R_ARM_THM_CALL relocation).
3289 ? arm_stub_long_branch_any_thumb_pic
3290 // On V4T, use Thumb code only.
3291 : arm_stub_long_branch_v4t_thumb_thumb_pic)
3292
3293 // non-PIC stubs.
3294 : ((may_use_blx
3295 && (r_type == elfcpp::R_ARM_THM_CALL))
3296 ? arm_stub_long_branch_any_any // V5T and above.
3297 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
3298 }
3299 else
3300 {
3301 stub_type = (parameters->options().shared()
3302 || should_force_pic_veneer)
3303 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
3304 : arm_stub_long_branch_thumb_only; // non-PIC stub.
3305 }
3306 }
3307 else
3308 {
3309 // Thumb to arm.
3310
3311 // FIXME: We should check that the input section is from an
3312 // object that has interwork enabled.
3313
3314 stub_type = (parameters->options().shared()
3315 || should_force_pic_veneer)
3316 // PIC stubs.
3317 ? ((may_use_blx
3318 && (r_type == elfcpp::R_ARM_THM_CALL))
3319 ? arm_stub_long_branch_any_arm_pic // V5T and above.
3320 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
3321
3322 // non-PIC stubs.
3323 : ((may_use_blx
3324 && (r_type == elfcpp::R_ARM_THM_CALL))
3325 ? arm_stub_long_branch_any_any // V5T and above.
3326 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
3327
3328 // Handle v4t short branches.
3329 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3330 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3331 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3332 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3333 }
3334 }
3335 }
3336 else if (r_type == elfcpp::R_ARM_CALL
3337 || r_type == elfcpp::R_ARM_JUMP24
3338 || r_type == elfcpp::R_ARM_PLT32)
3339 {
3340 if (target_is_thumb)
3341 {
3342 // Arm to thumb.
3343
3344 // FIXME: We should check that the input section is from an
3345 // object that has interwork enabled.
3346
3347 // We have an extra 2-bytes reach because of
3348 // the mode change (bit 24 (H) of BLX encoding).
3349 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3350 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3351 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
3352 || (r_type == elfcpp::R_ARM_JUMP24)
3353 || (r_type == elfcpp::R_ARM_PLT32))
3354 {
3355 stub_type = (parameters->options().shared()
3356 || should_force_pic_veneer)
3357 // PIC stubs.
3358 ? (may_use_blx
3359 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
3360 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
3361
3362 // non-PIC stubs.
3363 : (may_use_blx
3364 ? arm_stub_long_branch_any_any // V5T and above.
3365 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
3366 }
3367 }
3368 else
3369 {
3370 // Arm to arm.
3371 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3372 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3373 {
3374 stub_type = (parameters->options().shared()
3375 || should_force_pic_veneer)
3376 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
3377 : arm_stub_long_branch_any_any; /// non-PIC.
3378 }
3379 }
3380 }
3381
3382 return stub_type;
3383 }
3384
3385 // Cortex_a8_stub methods.
3386
3387 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3388 // I is the position of the instruction template in the stub template.
3389
3390 uint16_t
3391 Cortex_a8_stub::do_thumb16_special(size_t i)
3392 {
3393 // The only use of this is to copy condition code from a conditional
3394 // branch being worked around to the corresponding conditional branch in
3395 // to the stub.
3396 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3397 && i == 0);
3398 uint16_t data = this->stub_template()->insns()[i].data();
3399 gold_assert((data & 0xff00U) == 0xd000U);
3400 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
3401 return data;
3402 }
3403
3404 // Stub_factory methods.
3405
3406 Stub_factory::Stub_factory()
3407 {
3408 // The instruction template sequences are declared as static
3409 // objects and initialized first time the constructor runs.
3410
3411 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3412 // to reach the stub if necessary.
3413 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
3414 {
3415 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3416 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3417 // dcd R_ARM_ABS32(X)
3418 };
3419
3420 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3421 // available.
3422 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
3423 {
3424 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3425 Insn_template::arm_insn(0xe12fff1c), // bx ip
3426 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3427 // dcd R_ARM_ABS32(X)
3428 };
3429
3430 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3431 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
3432 {
3433 Insn_template::thumb16_insn(0xb401), // push {r0}
3434 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3435 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3436 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3437 Insn_template::thumb16_insn(0x4760), // bx ip
3438 Insn_template::thumb16_insn(0xbf00), // nop
3439 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3440 // dcd R_ARM_ABS32(X)
3441 };
3442
3443 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3444 // allowed.
3445 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
3446 {
3447 Insn_template::thumb16_insn(0x4778), // bx pc
3448 Insn_template::thumb16_insn(0x46c0), // nop
3449 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3450 Insn_template::arm_insn(0xe12fff1c), // bx ip
3451 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3452 // dcd R_ARM_ABS32(X)
3453 };
3454
3455 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3456 // available.
3457 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
3458 {
3459 Insn_template::thumb16_insn(0x4778), // bx pc
3460 Insn_template::thumb16_insn(0x46c0), // nop
3461 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3462 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3463 // dcd R_ARM_ABS32(X)
3464 };
3465
3466 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3467 // one, when the destination is close enough.
3468 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
3469 {
3470 Insn_template::thumb16_insn(0x4778), // bx pc
3471 Insn_template::thumb16_insn(0x46c0), // nop
3472 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3473 };
3474
3475 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3476 // blx to reach the stub if necessary.
3477 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
3478 {
3479 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3480 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3481 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3482 // dcd R_ARM_REL32(X-4)
3483 };
3484
3485 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3486 // blx to reach the stub if necessary. We can not add into pc;
3487 // it is not guaranteed to mode switch (different in ARMv6 and
3488 // ARMv7).
3489 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
3490 {
3491 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3492 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3493 Insn_template::arm_insn(0xe12fff1c), // bx ip
3494 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3495 // dcd R_ARM_REL32(X)
3496 };
3497
3498 // V4T ARM -> ARM long branch stub, PIC.
3499 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
3500 {
3501 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3502 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3503 Insn_template::arm_insn(0xe12fff1c), // bx ip
3504 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3505 // dcd R_ARM_REL32(X)
3506 };
3507
3508 // V4T Thumb -> ARM long branch stub, PIC.
3509 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
3510 {
3511 Insn_template::thumb16_insn(0x4778), // bx pc
3512 Insn_template::thumb16_insn(0x46c0), // nop
3513 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3514 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3515 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3516 // dcd R_ARM_REL32(X)
3517 };
3518
3519 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3520 // architectures.
3521 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
3522 {
3523 Insn_template::thumb16_insn(0xb401), // push {r0}
3524 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3525 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3526 Insn_template::thumb16_insn(0x4484), // add ip, r0
3527 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3528 Insn_template::thumb16_insn(0x4760), // bx ip
3529 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
3530 // dcd R_ARM_REL32(X)
3531 };
3532
3533 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3534 // allowed.
3535 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
3536 {
3537 Insn_template::thumb16_insn(0x4778), // bx pc
3538 Insn_template::thumb16_insn(0x46c0), // nop
3539 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3540 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3541 Insn_template::arm_insn(0xe12fff1c), // bx ip
3542 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3543 // dcd R_ARM_REL32(X)
3544 };
3545
3546 // Cortex-A8 erratum-workaround stubs.
3547
3548 // Stub used for conditional branches (which may be beyond +/-1MB away,
3549 // so we can't use a conditional branch to reach this stub).
3550
3551 // original code:
3552 //
3553 // b<cond> X
3554 // after:
3555 //
3556 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
3557 {
3558 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3559 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3560 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3561 // b.w X
3562 };
3563
3564 // Stub used for b.w and bl.w instructions.
3565
3566 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
3567 {
3568 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3569 };
3570
3571 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
3572 {
3573 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3574 };
3575
3576 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3577 // instruction (which switches to ARM mode) to point to this stub. Jump to
3578 // the real destination using an ARM-mode branch.
3579 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
3580 {
3581 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3582 };
3583
3584 // Fill in the stub template look-up table. Stub templates are constructed
3585 // per instance of Stub_factory for fast look-up without locking
3586 // in a thread-enabled environment.
3587
3588 this->stub_templates_[arm_stub_none] =
3589 new Stub_template(arm_stub_none, NULL, 0);
3590
3591 #define DEF_STUB(x) \
3592 do \
3593 { \
3594 size_t array_size \
3595 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3596 Stub_type type = arm_stub_##x; \
3597 this->stub_templates_[type] = \
3598 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3599 } \
3600 while (0);
3601
3602 DEF_STUBS
3603 #undef DEF_STUB
3604 }
3605
3606 // Stub_table methods.
3607
3608 // Removel all Cortex-A8 stub.
3609
3610 template<bool big_endian>
3611 void
3612 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
3613 {
3614 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
3615 p != this->cortex_a8_stubs_.end();
3616 ++p)
3617 delete p->second;
3618 this->cortex_a8_stubs_.clear();
3619 }
3620
3621 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
3622
3623 template<bool big_endian>
3624 void
3625 Stub_table<big_endian>::relocate_stub(
3626 Stub* stub,
3627 const Relocate_info<32, big_endian>* relinfo,
3628 Target_arm<big_endian>* arm_target,
3629 Output_section* output_section,
3630 unsigned char* view,
3631 Arm_address address,
3632 section_size_type view_size)
3633 {
3634 const Stub_template* stub_template = stub->stub_template();
3635 if (stub_template->reloc_count() != 0)
3636 {
3637 // Adjust view to cover the stub only.
3638 section_size_type offset = stub->offset();
3639 section_size_type stub_size = stub_template->size();
3640 gold_assert(offset + stub_size <= view_size);
3641
3642 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
3643 address + offset, stub_size);
3644 }
3645 }
3646
3647 // Relocate all stubs in this stub table.
3648
3649 template<bool big_endian>
3650 void
3651 Stub_table<big_endian>::relocate_stubs(
3652 const Relocate_info<32, big_endian>* relinfo,
3653 Target_arm<big_endian>* arm_target,
3654 Output_section* output_section,
3655 unsigned char* view,
3656 Arm_address address,
3657 section_size_type view_size)
3658 {
3659 // If we are passed a view bigger than the stub table's. we need to
3660 // adjust the view.
3661 gold_assert(address == this->address()
3662 && (view_size
3663 == static_cast<section_size_type>(this->data_size())));
3664
3665 // Relocate all relocation stubs.
3666 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3667 p != this->reloc_stubs_.end();
3668 ++p)
3669 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
3670 address, view_size);
3671
3672 // Relocate all Cortex-A8 stubs.
3673 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
3674 p != this->cortex_a8_stubs_.end();
3675 ++p)
3676 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
3677 address, view_size);
3678 }
3679
3680 // Write out the stubs to file.
3681
3682 template<bool big_endian>
3683 void
3684 Stub_table<big_endian>::do_write(Output_file* of)
3685 {
3686 off_t offset = this->offset();
3687 const section_size_type oview_size =
3688 convert_to_section_size_type(this->data_size());
3689 unsigned char* const oview = of->get_output_view(offset, oview_size);
3690
3691 // Write relocation stubs.
3692 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3693 p != this->reloc_stubs_.end();
3694 ++p)
3695 {
3696 Reloc_stub* stub = p->second;
3697 Arm_address address = this->address() + stub->offset();
3698 gold_assert(address
3699 == align_address(address,
3700 stub->stub_template()->alignment()));
3701 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3702 big_endian);
3703 }
3704
3705 // Write Cortex-A8 stubs.
3706 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3707 p != this->cortex_a8_stubs_.end();
3708 ++p)
3709 {
3710 Cortex_a8_stub* stub = p->second;
3711 Arm_address address = this->address() + stub->offset();
3712 gold_assert(address
3713 == align_address(address,
3714 stub->stub_template()->alignment()));
3715 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3716 big_endian);
3717 }
3718
3719 of->write_output_view(this->offset(), oview_size, oview);
3720 }
3721
3722 // Update the data size and address alignment of the stub table at the end
3723 // of a relaxation pass. Return true if either the data size or the
3724 // alignment changed in this relaxation pass.
3725
3726 template<bool big_endian>
3727 bool
3728 Stub_table<big_endian>::update_data_size_and_addralign()
3729 {
3730 off_t size = 0;
3731 unsigned addralign = 1;
3732
3733 // Go over all stubs in table to compute data size and address alignment.
3734
3735 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3736 p != this->reloc_stubs_.end();
3737 ++p)
3738 {
3739 const Stub_template* stub_template = p->second->stub_template();
3740 addralign = std::max(addralign, stub_template->alignment());
3741 size = (align_address(size, stub_template->alignment())
3742 + stub_template->size());
3743 }
3744
3745 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3746 p != this->cortex_a8_stubs_.end();
3747 ++p)
3748 {
3749 const Stub_template* stub_template = p->second->stub_template();
3750 addralign = std::max(addralign, stub_template->alignment());
3751 size = (align_address(size, stub_template->alignment())
3752 + stub_template->size());
3753 }
3754
3755 // Check if either data size or alignment changed in this pass.
3756 // Update prev_data_size_ and prev_addralign_. These will be used
3757 // as the current data size and address alignment for the next pass.
3758 bool changed = size != this->prev_data_size_;
3759 this->prev_data_size_ = size;
3760
3761 if (addralign != this->prev_addralign_)
3762 changed = true;
3763 this->prev_addralign_ = addralign;
3764
3765 return changed;
3766 }
3767
3768 // Finalize the stubs. This sets the offsets of the stubs within the stub
3769 // table. It also marks all input sections needing Cortex-A8 workaround.
3770
3771 template<bool big_endian>
3772 void
3773 Stub_table<big_endian>::finalize_stubs()
3774 {
3775 off_t off = 0;
3776 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3777 p != this->reloc_stubs_.end();
3778 ++p)
3779 {
3780 Reloc_stub* stub = p->second;
3781 const Stub_template* stub_template = stub->stub_template();
3782 uint64_t stub_addralign = stub_template->alignment();
3783 off = align_address(off, stub_addralign);
3784 stub->set_offset(off);
3785 off += stub_template->size();
3786 }
3787
3788 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
3789 p != this->cortex_a8_stubs_.end();
3790 ++p)
3791 {
3792 Cortex_a8_stub* stub = p->second;
3793 const Stub_template* stub_template = stub->stub_template();
3794 uint64_t stub_addralign = stub_template->alignment();
3795 off = align_address(off, stub_addralign);
3796 stub->set_offset(off);
3797 off += stub_template->size();
3798
3799 // Mark input section so that we can determine later if a code section
3800 // needs the Cortex-A8 workaround quickly.
3801 Arm_relobj<big_endian>* arm_relobj =
3802 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
3803 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
3804 }
3805
3806 gold_assert(off <= this->prev_data_size_);
3807 }
3808
3809 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
3810 // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
3811 // of the address range seen by the linker.
3812
3813 template<bool big_endian>
3814 void
3815 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
3816 Target_arm<big_endian>* arm_target,
3817 unsigned char* view,
3818 Arm_address view_address,
3819 section_size_type view_size)
3820 {
3821 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
3822 for (Cortex_a8_stub_list::const_iterator p =
3823 this->cortex_a8_stubs_.lower_bound(view_address);
3824 ((p != this->cortex_a8_stubs_.end())
3825 && (p->first < (view_address + view_size)));
3826 ++p)
3827 {
3828 // We do not store the THUMB bit in the LSB of either the branch address
3829 // or the stub offset. There is no need to strip the LSB.
3830 Arm_address branch_address = p->first;
3831 const Cortex_a8_stub* stub = p->second;
3832 Arm_address stub_address = this->address() + stub->offset();
3833
3834 // Offset of the branch instruction relative to this view.
3835 section_size_type offset =
3836 convert_to_section_size_type(branch_address - view_address);
3837 gold_assert((offset + 4) <= view_size);
3838
3839 arm_target->apply_cortex_a8_workaround(stub, stub_address,
3840 view + offset, branch_address);
3841 }
3842 }
3843
3844 // Arm_input_section methods.
3845
3846 // Initialize an Arm_input_section.
3847
3848 template<bool big_endian>
3849 void
3850 Arm_input_section<big_endian>::init()
3851 {
3852 Relobj* relobj = this->relobj();
3853 unsigned int shndx = this->shndx();
3854
3855 // Cache these to speed up size and alignment queries. It is too slow
3856 // to call section_addraglin and section_size every time.
3857 this->original_addralign_ = relobj->section_addralign(shndx);
3858 this->original_size_ = relobj->section_size(shndx);
3859
3860 // We want to make this look like the original input section after
3861 // output sections are finalized.
3862 Output_section* os = relobj->output_section(shndx);
3863 off_t offset = relobj->output_section_offset(shndx);
3864 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
3865 this->set_address(os->address() + offset);
3866 this->set_file_offset(os->offset() + offset);
3867
3868 this->set_current_data_size(this->original_size_);
3869 this->finalize_data_size();
3870 }
3871
3872 template<bool big_endian>
3873 void
3874 Arm_input_section<big_endian>::do_write(Output_file* of)
3875 {
3876 // We have to write out the original section content.
3877 section_size_type section_size;
3878 const unsigned char* section_contents =
3879 this->relobj()->section_contents(this->shndx(), &section_size, false);
3880 of->write(this->offset(), section_contents, section_size);
3881
3882 // If this owns a stub table and it is not empty, write it.
3883 if (this->is_stub_table_owner() && !this->stub_table_->empty())
3884 this->stub_table_->write(of);
3885 }
3886
3887 // Finalize data size.
3888
3889 template<bool big_endian>
3890 void
3891 Arm_input_section<big_endian>::set_final_data_size()
3892 {
3893 // If this owns a stub table, finalize its data size as well.
3894 if (this->is_stub_table_owner())
3895 {
3896 uint64_t address = this->address();
3897
3898 // The stub table comes after the original section contents.
3899 address += this->original_size_;
3900 address = align_address(address, this->stub_table_->addralign());
3901 off_t offset = this->offset() + (address - this->address());
3902 this->stub_table_->set_address_and_file_offset(address, offset);
3903 address += this->stub_table_->data_size();
3904 gold_assert(address == this->address() + this->current_data_size());
3905 }
3906
3907 this->set_data_size(this->current_data_size());
3908 }
3909
3910 // Reset address and file offset.
3911
3912 template<bool big_endian>
3913 void
3914 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
3915 {
3916 // Size of the original input section contents.
3917 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
3918
3919 // If this is a stub table owner, account for the stub table size.
3920 if (this->is_stub_table_owner())
3921 {
3922 Stub_table<big_endian>* stub_table = this->stub_table_;
3923
3924 // Reset the stub table's address and file offset. The
3925 // current data size for child will be updated after that.
3926 stub_table_->reset_address_and_file_offset();
3927 off = align_address(off, stub_table_->addralign());
3928 off += stub_table->current_data_size();
3929 }
3930
3931 this->set_current_data_size(off);
3932 }
3933
3934 // Arm_output_section methods.
3935
3936 // Create a stub group for input sections from BEGIN to END. OWNER
3937 // points to the input section to be the owner a new stub table.
3938
3939 template<bool big_endian>
3940 void
3941 Arm_output_section<big_endian>::create_stub_group(
3942 Input_section_list::const_iterator begin,
3943 Input_section_list::const_iterator end,
3944 Input_section_list::const_iterator owner,
3945 Target_arm<big_endian>* target,
3946 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
3947 {
3948 // Currently we convert ordinary input sections into relaxed sections only
3949 // at this point but we may want to support creating relaxed input section
3950 // very early. So we check here to see if owner is already a relaxed
3951 // section.
3952
3953 Arm_input_section<big_endian>* arm_input_section;
3954 if (owner->is_relaxed_input_section())
3955 {
3956 arm_input_section =
3957 Arm_input_section<big_endian>::as_arm_input_section(
3958 owner->relaxed_input_section());
3959 }
3960 else
3961 {
3962 gold_assert(owner->is_input_section());
3963 // Create a new relaxed input section.
3964 arm_input_section =
3965 target->new_arm_input_section(owner->relobj(), owner->shndx());
3966 new_relaxed_sections->push_back(arm_input_section);
3967 }
3968
3969 // Create a stub table.
3970 Stub_table<big_endian>* stub_table =
3971 target->new_stub_table(arm_input_section);
3972
3973 arm_input_section->set_stub_table(stub_table);
3974
3975 Input_section_list::const_iterator p = begin;
3976 Input_section_list::const_iterator prev_p;
3977
3978 // Look for input sections or relaxed input sections in [begin ... end].
3979 do
3980 {
3981 if (p->is_input_section() || p->is_relaxed_input_section())
3982 {
3983 // The stub table information for input sections live
3984 // in their objects.
3985 Arm_relobj<big_endian>* arm_relobj =
3986 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
3987 arm_relobj->set_stub_table(p->shndx(), stub_table);
3988 }
3989 prev_p = p++;
3990 }
3991 while (prev_p != end);
3992 }
3993
3994 // Group input sections for stub generation. GROUP_SIZE is roughly the limit
3995 // of stub groups. We grow a stub group by adding input section until the
3996 // size is just below GROUP_SIZE. The last input section will be converted
3997 // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3998 // input section after the stub table, effectively double the group size.
3999 //
4000 // This is similar to the group_sections() function in elf32-arm.c but is
4001 // implemented differently.
4002
4003 template<bool big_endian>
4004 void
4005 Arm_output_section<big_endian>::group_sections(
4006 section_size_type group_size,
4007 bool stubs_always_after_branch,
4008 Target_arm<big_endian>* target)
4009 {
4010 // We only care about sections containing code.
4011 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
4012 return;
4013
4014 // States for grouping.
4015 typedef enum
4016 {
4017 // No group is being built.
4018 NO_GROUP,
4019 // A group is being built but the stub table is not found yet.
4020 // We keep group a stub group until the size is just under GROUP_SIZE.
4021 // The last input section in the group will be used as the stub table.
4022 FINDING_STUB_SECTION,
4023 // A group is being built and we have already found a stub table.
4024 // We enter this state to grow a stub group by adding input section
4025 // after the stub table. This effectively doubles the group size.
4026 HAS_STUB_SECTION
4027 } State;
4028
4029 // Any newly created relaxed sections are stored here.
4030 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
4031
4032 State state = NO_GROUP;
4033 section_size_type off = 0;
4034 section_size_type group_begin_offset = 0;
4035 section_size_type group_end_offset = 0;
4036 section_size_type stub_table_end_offset = 0;
4037 Input_section_list::const_iterator group_begin =
4038 this->input_sections().end();
4039 Input_section_list::const_iterator stub_table =
4040 this->input_sections().end();
4041 Input_section_list::const_iterator group_end = this->input_sections().end();
4042 for (Input_section_list::const_iterator p = this->input_sections().begin();
4043 p != this->input_sections().end();
4044 ++p)
4045 {
4046 section_size_type section_begin_offset =
4047 align_address(off, p->addralign());
4048 section_size_type section_end_offset =
4049 section_begin_offset + p->data_size();
4050
4051 // Check to see if we should group the previously seens sections.
4052 switch (state)
4053 {
4054 case NO_GROUP:
4055 break;
4056
4057 case FINDING_STUB_SECTION:
4058 // Adding this section makes the group larger than GROUP_SIZE.
4059 if (section_end_offset - group_begin_offset >= group_size)
4060 {
4061 if (stubs_always_after_branch)
4062 {
4063 gold_assert(group_end != this->input_sections().end());
4064 this->create_stub_group(group_begin, group_end, group_end,
4065 target, &new_relaxed_sections);
4066 state = NO_GROUP;
4067 }
4068 else
4069 {
4070 // But wait, there's more! Input sections up to
4071 // stub_group_size bytes after the stub table can be
4072 // handled by it too.
4073 state = HAS_STUB_SECTION;
4074 stub_table = group_end;
4075 stub_table_end_offset = group_end_offset;
4076 }
4077 }
4078 break;
4079
4080 case HAS_STUB_SECTION:
4081 // Adding this section makes the post stub-section group larger
4082 // than GROUP_SIZE.
4083 if (section_end_offset - stub_table_end_offset >= group_size)
4084 {
4085 gold_assert(group_end != this->input_sections().end());
4086 this->create_stub_group(group_begin, group_end, stub_table,
4087 target, &new_relaxed_sections);
4088 state = NO_GROUP;
4089 }
4090 break;
4091
4092 default:
4093 gold_unreachable();
4094 }
4095
4096 // If we see an input section and currently there is no group, start
4097 // a new one. Skip any empty sections.
4098 if ((p->is_input_section() || p->is_relaxed_input_section())
4099 && (p->relobj()->section_size(p->shndx()) != 0))
4100 {
4101 if (state == NO_GROUP)
4102 {
4103 state = FINDING_STUB_SECTION;
4104 group_begin = p;
4105 group_begin_offset = section_begin_offset;
4106 }
4107
4108 // Keep track of the last input section seen.
4109 group_end = p;
4110 group_end_offset = section_end_offset;
4111 }
4112
4113 off = section_end_offset;
4114 }
4115
4116 // Create a stub group for any ungrouped sections.
4117 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
4118 {
4119 gold_assert(group_end != this->input_sections().end());
4120 this->create_stub_group(group_begin, group_end,
4121 (state == FINDING_STUB_SECTION
4122 ? group_end
4123 : stub_table),
4124 target, &new_relaxed_sections);
4125 }
4126
4127 // Convert input section into relaxed input section in a batch.
4128 if (!new_relaxed_sections.empty())
4129 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
4130
4131 // Update the section offsets
4132 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
4133 {
4134 Arm_relobj<big_endian>* arm_relobj =
4135 Arm_relobj<big_endian>::as_arm_relobj(
4136 new_relaxed_sections[i]->relobj());
4137 unsigned int shndx = new_relaxed_sections[i]->shndx();
4138 // Tell Arm_relobj that this input section is converted.
4139 arm_relobj->convert_input_section_to_relaxed_section(shndx);
4140 }
4141 }
4142
4143 // Arm_relobj methods.
4144
4145 // Scan relocations for stub generation.
4146
4147 template<bool big_endian>
4148 void
4149 Arm_relobj<big_endian>::scan_sections_for_stubs(
4150 Target_arm<big_endian>* arm_target,
4151 const Symbol_table* symtab,
4152 const Layout* layout)
4153 {
4154 unsigned int shnum = this->shnum();
4155 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4156
4157 // Read the section headers.
4158 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
4159 shnum * shdr_size,
4160 true, true);
4161
4162 // To speed up processing, we set up hash tables for fast lookup of
4163 // input offsets to output addresses.
4164 this->initialize_input_to_output_maps();
4165
4166 const Relobj::Output_sections& out_sections(this->output_sections());
4167
4168 Relocate_info<32, big_endian> relinfo;
4169 relinfo.symtab = symtab;
4170 relinfo.layout = layout;
4171 relinfo.object = this;
4172
4173 const unsigned char* p = pshdrs + shdr_size;
4174 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
4175 {
4176 typename elfcpp::Shdr<32, big_endian> shdr(p);
4177
4178 unsigned int sh_type = shdr.get_sh_type();
4179 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
4180 continue;
4181
4182 off_t sh_size = shdr.get_sh_size();
4183 if (sh_size == 0)
4184 continue;
4185
4186 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
4187 if (index >= this->shnum())
4188 {
4189 // Ignore reloc section with bad info. This error will be
4190 // reported in the final link.
4191 continue;
4192 }
4193
4194 Output_section* os = out_sections[index];
4195 if (os == NULL
4196 || symtab->is_section_folded(this, index))
4197 {
4198 // This relocation section is against a section which we
4199 // discarded or if the section is folded into another
4200 // section due to ICF.
4201 continue;
4202 }
4203 Arm_address output_offset = this->get_output_section_offset(index);
4204
4205 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
4206 {
4207 // Ignore reloc section with unexpected symbol table. The
4208 // error will be reported in the final link.
4209 continue;
4210 }
4211
4212 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
4213 sh_size, true, false);
4214
4215 unsigned int reloc_size;
4216 if (sh_type == elfcpp::SHT_REL)
4217 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
4218 else
4219 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
4220
4221 if (reloc_size != shdr.get_sh_entsize())
4222 {
4223 // Ignore reloc section with unexpected entsize. The error
4224 // will be reported in the final link.
4225 continue;
4226 }
4227
4228 size_t reloc_count = sh_size / reloc_size;
4229 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
4230 {
4231 // Ignore reloc section with uneven size. The error will be
4232 // reported in the final link.
4233 continue;
4234 }
4235
4236 gold_assert(output_offset != invalid_address
4237 || this->relocs_must_follow_section_writes());
4238
4239 // Get the section contents. This does work for the case in which
4240 // we modify the contents of an input section. We need to pass the
4241 // output view under such circumstances.
4242 section_size_type input_view_size = 0;
4243 const unsigned char* input_view =
4244 this->section_contents(index, &input_view_size, false);
4245
4246 relinfo.reloc_shndx = i;
4247 relinfo.data_shndx = index;
4248 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
4249 reloc_count, os,
4250 output_offset == invalid_address,
4251 input_view,
4252 os->address(),
4253 input_view_size);
4254 }
4255
4256 // After we've done the relocations, we release the hash tables,
4257 // since we no longer need them.
4258 this->free_input_to_output_maps();
4259 }
4260
4261 // Count the local symbols. The ARM backend needs to know if a symbol
4262 // is a THUMB function or not. For global symbols, it is easy because
4263 // the Symbol object keeps the ELF symbol type. For local symbol it is
4264 // harder because we cannot access this information. So we override the
4265 // do_count_local_symbol in parent and scan local symbols to mark
4266 // THUMB functions. This is not the most efficient way but I do not want to
4267 // slow down other ports by calling a per symbol targer hook inside
4268 // Sized_relobj<size, big_endian>::do_count_local_symbols.
4269
4270 template<bool big_endian>
4271 void
4272 Arm_relobj<big_endian>::do_count_local_symbols(
4273 Stringpool_template<char>* pool,
4274 Stringpool_template<char>* dynpool)
4275 {
4276 // We need to fix-up the values of any local symbols whose type are
4277 // STT_ARM_TFUNC.
4278
4279 // Ask parent to count the local symbols.
4280 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
4281 const unsigned int loccount = this->local_symbol_count();
4282 if (loccount == 0)
4283 return;
4284
4285 // Intialize the thumb function bit-vector.
4286 std::vector<bool> empty_vector(loccount, false);
4287 this->local_symbol_is_thumb_function_.swap(empty_vector);
4288
4289 // Read the symbol table section header.
4290 const unsigned int symtab_shndx = this->symtab_shndx();
4291 elfcpp::Shdr<32, big_endian>
4292 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
4293 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
4294
4295 // Read the local symbols.
4296 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
4297 gold_assert(loccount == symtabshdr.get_sh_info());
4298 off_t locsize = loccount * sym_size;
4299 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
4300 locsize, true, true);
4301
4302 // For mapping symbol processing, we need to read the symbol names.
4303 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
4304 if (strtab_shndx >= this->shnum())
4305 {
4306 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
4307 return;
4308 }
4309
4310 elfcpp::Shdr<32, big_endian>
4311 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
4312 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
4313 {
4314 this->error(_("symbol table name section has wrong type: %u"),
4315 static_cast<unsigned int>(strtabshdr.get_sh_type()));
4316 return;
4317 }
4318 const char* pnames =
4319 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
4320 strtabshdr.get_sh_size(),
4321 false, false));
4322
4323 // Loop over the local symbols and mark any local symbols pointing
4324 // to THUMB functions.
4325
4326 // Skip the first dummy symbol.
4327 psyms += sym_size;
4328 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
4329 this->local_values();
4330 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
4331 {
4332 elfcpp::Sym<32, big_endian> sym(psyms);
4333 elfcpp::STT st_type = sym.get_st_type();
4334 Symbol_value<32>& lv((*plocal_values)[i]);
4335 Arm_address input_value = lv.input_value();
4336
4337 // Check to see if this is a mapping symbol.
4338 const char* sym_name = pnames + sym.get_st_name();
4339 if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
4340 {
4341 unsigned int input_shndx = sym.get_st_shndx();
4342
4343 // Strip of LSB in case this is a THUMB symbol.
4344 Mapping_symbol_position msp(input_shndx, input_value & ~1U);
4345 this->mapping_symbols_info_[msp] = sym_name[1];
4346 }
4347
4348 if (st_type == elfcpp::STT_ARM_TFUNC
4349 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
4350 {
4351 // This is a THUMB function. Mark this and canonicalize the
4352 // symbol value by setting LSB.
4353 this->local_symbol_is_thumb_function_[i] = true;
4354 if ((input_value & 1) == 0)
4355 lv.set_input_value(input_value | 1);
4356 }
4357 }
4358 }
4359
4360 // Relocate sections.
4361 template<bool big_endian>
4362 void
4363 Arm_relobj<big_endian>::do_relocate_sections(
4364 const Symbol_table* symtab,
4365 const Layout* layout,
4366 const unsigned char* pshdrs,
4367 typename Sized_relobj<32, big_endian>::Views* pviews)
4368 {
4369 // Call parent to relocate sections.
4370 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
4371 pviews);
4372
4373 // We do not generate stubs if doing a relocatable link.
4374 if (parameters->options().relocatable())
4375 return;
4376
4377 // Relocate stub tables.
4378 unsigned int shnum = this->shnum();
4379
4380 Target_arm<big_endian>* arm_target =
4381 Target_arm<big_endian>::default_target();
4382
4383 Relocate_info<32, big_endian> relinfo;
4384 relinfo.symtab = symtab;
4385 relinfo.layout = layout;
4386 relinfo.object = this;
4387
4388 for (unsigned int i = 1; i < shnum; ++i)
4389 {
4390 Arm_input_section<big_endian>* arm_input_section =
4391 arm_target->find_arm_input_section(this, i);
4392
4393 if (arm_input_section == NULL
4394 || !arm_input_section->is_stub_table_owner()
4395 || arm_input_section->stub_table()->empty())
4396 continue;
4397
4398 // We cannot discard a section if it owns a stub table.
4399 Output_section* os = this->output_section(i);
4400 gold_assert(os != NULL);
4401
4402 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
4403 relinfo.reloc_shdr = NULL;
4404 relinfo.data_shndx = i;
4405 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
4406
4407 gold_assert((*pviews)[i].view != NULL);
4408
4409 // We are passed the output section view. Adjust it to cover the
4410 // stub table only.
4411 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
4412 gold_assert((stub_table->address() >= (*pviews)[i].address)
4413 && ((stub_table->address() + stub_table->data_size())
4414 <= (*pviews)[i].address + (*pviews)[i].view_size));
4415
4416 off_t offset = stub_table->address() - (*pviews)[i].address;
4417 unsigned char* view = (*pviews)[i].view + offset;
4418 Arm_address address = stub_table->address();
4419 section_size_type view_size = stub_table->data_size();
4420
4421 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
4422 view_size);
4423 }
4424 }
4425
4426 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
4427 // ABI information.
4428
4429 template<bool big_endian>
4430 Attributes_section_data*
4431 read_arm_attributes_section(
4432 Object* object,
4433 Read_symbols_data *sd)
4434 {
4435 // Read the attributes section if there is one.
4436 // We read from the end because gas seems to put it near the end of
4437 // the section headers.
4438 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4439 const unsigned char *ps =
4440 sd->section_headers->data() + shdr_size * (object->shnum() - 1);
4441 for (unsigned int i = object->shnum(); i > 0; --i, ps -= shdr_size)
4442 {
4443 elfcpp::Shdr<32, big_endian> shdr(ps);
4444 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
4445 {
4446 section_offset_type section_offset = shdr.get_sh_offset();
4447 section_size_type section_size =
4448 convert_to_section_size_type(shdr.get_sh_size());
4449 File_view* view = object->get_lasting_view(section_offset,
4450 section_size, true, false);
4451 return new Attributes_section_data(view->data(), section_size);
4452 }
4453 }
4454 return NULL;
4455 }
4456
4457 // Read the symbol information.
4458
4459 template<bool big_endian>
4460 void
4461 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4462 {
4463 // Call parent class to read symbol information.
4464 Sized_relobj<32, big_endian>::do_read_symbols(sd);
4465
4466 // Read processor-specific flags in ELF file header.
4467 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4468 elfcpp::Elf_sizes<32>::ehdr_size,
4469 true, false);
4470 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4471 this->processor_specific_flags_ = ehdr.get_e_flags();
4472 this->attributes_section_data_ =
4473 read_arm_attributes_section<big_endian>(this, sd);
4474 }
4475
4476 // Process relocations for garbage collection. The ARM target uses .ARM.exidx
4477 // sections for unwinding. These sections are referenced implicitly by
4478 // text sections linked in the section headers. If we ignore these implict
4479 // references, the .ARM.exidx sections and any .ARM.extab sections they use
4480 // will be garbage-collected incorrectly. Hence we override the same function
4481 // in the base class to handle these implicit references.
4482
4483 template<bool big_endian>
4484 void
4485 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
4486 Layout* layout,
4487 Read_relocs_data* rd)
4488 {
4489 // First, call base class method to process relocations in this object.
4490 Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
4491
4492 unsigned int shnum = this->shnum();
4493 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4494 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
4495 shnum * shdr_size,
4496 true, true);
4497
4498 // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
4499 // to these from the linked text sections.
4500 const unsigned char* ps = pshdrs + shdr_size;
4501 for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
4502 {
4503 elfcpp::Shdr<32, big_endian> shdr(ps);
4504 if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
4505 {
4506 // Found an .ARM.exidx section, add it to the set of reachable
4507 // sections from its linked text section.
4508 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
4509 symtab->gc()->add_reference(this, text_shndx, this, i);
4510 }
4511 }
4512 }
4513
4514 // Arm_dynobj methods.
4515
4516 // Read the symbol information.
4517
4518 template<bool big_endian>
4519 void
4520 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4521 {
4522 // Call parent class to read symbol information.
4523 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
4524
4525 // Read processor-specific flags in ELF file header.
4526 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4527 elfcpp::Elf_sizes<32>::ehdr_size,
4528 true, false);
4529 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4530 this->processor_specific_flags_ = ehdr.get_e_flags();
4531 this->attributes_section_data_ =
4532 read_arm_attributes_section<big_endian>(this, sd);
4533 }
4534
4535 // Stub_addend_reader methods.
4536
4537 // Read the addend of a REL relocation of type R_TYPE at VIEW.
4538
4539 template<bool big_endian>
4540 elfcpp::Elf_types<32>::Elf_Swxword
4541 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
4542 unsigned int r_type,
4543 const unsigned char* view,
4544 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
4545 {
4546 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
4547
4548 switch (r_type)
4549 {
4550 case elfcpp::R_ARM_CALL:
4551 case elfcpp::R_ARM_JUMP24:
4552 case elfcpp::R_ARM_PLT32:
4553 {
4554 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4555 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4556 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4557 return utils::sign_extend<26>(val << 2);
4558 }
4559
4560 case elfcpp::R_ARM_THM_CALL:
4561 case elfcpp::R_ARM_THM_JUMP24:
4562 case elfcpp::R_ARM_THM_XPC22:
4563 {
4564 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4565 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4566 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4567 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4568 return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
4569 }
4570
4571 case elfcpp::R_ARM_THM_JUMP19:
4572 {
4573 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4574 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4575 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4576 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4577 return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
4578 }
4579
4580 default:
4581 gold_unreachable();
4582 }
4583 }
4584
4585 // A class to handle the PLT data.
4586
4587 template<bool big_endian>
4588 class Output_data_plt_arm : public Output_section_data
4589 {
4590 public:
4591 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
4592 Reloc_section;
4593
4594 Output_data_plt_arm(Layout*, Output_data_space*);
4595
4596 // Add an entry to the PLT.
4597 void
4598 add_entry(Symbol* gsym);
4599
4600 // Return the .rel.plt section data.
4601 const Reloc_section*
4602 rel_plt() const
4603 { return this->rel_; }
4604
4605 protected:
4606 void
4607 do_adjust_output_section(Output_section* os);
4608
4609 // Write to a map file.
4610 void
4611 do_print_to_mapfile(Mapfile* mapfile) const
4612 { mapfile->print_output_data(this, _("** PLT")); }
4613
4614 private:
4615 // Template for the first PLT entry.
4616 static const uint32_t first_plt_entry[5];
4617
4618 // Template for subsequent PLT entries.
4619 static const uint32_t plt_entry[3];
4620
4621 // Set the final size.
4622 void
4623 set_final_data_size()
4624 {
4625 this->set_data_size(sizeof(first_plt_entry)
4626 + this->count_ * sizeof(plt_entry));
4627 }
4628
4629 // Write out the PLT data.
4630 void
4631 do_write(Output_file*);
4632
4633 // The reloc section.
4634 Reloc_section* rel_;
4635 // The .got.plt section.
4636 Output_data_space* got_plt_;
4637 // The number of PLT entries.
4638 unsigned int count_;
4639 };
4640
4641 // Create the PLT section. The ordinary .got section is an argument,
4642 // since we need to refer to the start. We also create our own .got
4643 // section just for PLT entries.
4644
4645 template<bool big_endian>
4646 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
4647 Output_data_space* got_plt)
4648 : Output_section_data(4), got_plt_(got_plt), count_(0)
4649 {
4650 this->rel_ = new Reloc_section(false);
4651 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
4652 elfcpp::SHF_ALLOC, this->rel_, true, false,
4653 false, false);
4654 }
4655
4656 template<bool big_endian>
4657 void
4658 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
4659 {
4660 os->set_entsize(0);
4661 }
4662
4663 // Add an entry to the PLT.
4664
4665 template<bool big_endian>
4666 void
4667 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
4668 {
4669 gold_assert(!gsym->has_plt_offset());
4670
4671 // Note that when setting the PLT offset we skip the initial
4672 // reserved PLT entry.
4673 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
4674 + sizeof(first_plt_entry));
4675
4676 ++this->count_;
4677
4678 section_offset_type got_offset = this->got_plt_->current_data_size();
4679
4680 // Every PLT entry needs a GOT entry which points back to the PLT
4681 // entry (this will be changed by the dynamic linker, normally
4682 // lazily when the function is called).
4683 this->got_plt_->set_current_data_size(got_offset + 4);
4684
4685 // Every PLT entry needs a reloc.
4686 gsym->set_needs_dynsym_entry();
4687 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
4688 got_offset);
4689
4690 // Note that we don't need to save the symbol. The contents of the
4691 // PLT are independent of which symbols are used. The symbols only
4692 // appear in the relocations.
4693 }
4694
4695 // ARM PLTs.
4696 // FIXME: This is not very flexible. Right now this has only been tested
4697 // on armv5te. If we are to support additional architecture features like
4698 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4699
4700 // The first entry in the PLT.
4701 template<bool big_endian>
4702 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
4703 {
4704 0xe52de004, // str lr, [sp, #-4]!
4705 0xe59fe004, // ldr lr, [pc, #4]
4706 0xe08fe00e, // add lr, pc, lr
4707 0xe5bef008, // ldr pc, [lr, #8]!
4708 0x00000000, // &GOT[0] - .
4709 };
4710
4711 // Subsequent entries in the PLT.
4712
4713 template<bool big_endian>
4714 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
4715 {
4716 0xe28fc600, // add ip, pc, #0xNN00000
4717 0xe28cca00, // add ip, ip, #0xNN000
4718 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4719 };
4720
4721 // Write out the PLT. This uses the hand-coded instructions above,
4722 // and adjusts them as needed. This is all specified by the arm ELF
4723 // Processor Supplement.
4724
4725 template<bool big_endian>
4726 void
4727 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
4728 {
4729 const off_t offset = this->offset();
4730 const section_size_type oview_size =
4731 convert_to_section_size_type(this->data_size());
4732 unsigned char* const oview = of->get_output_view(offset, oview_size);
4733
4734 const off_t got_file_offset = this->got_plt_->offset();
4735 const section_size_type got_size =
4736 convert_to_section_size_type(this->got_plt_->data_size());
4737 unsigned char* const got_view = of->get_output_view(got_file_offset,
4738 got_size);
4739 unsigned char* pov = oview;
4740
4741 Arm_address plt_address = this->address();
4742 Arm_address got_address = this->got_plt_->address();
4743
4744 // Write first PLT entry. All but the last word are constants.
4745 const size_t num_first_plt_words = (sizeof(first_plt_entry)
4746 / sizeof(plt_entry[0]));
4747 for (size_t i = 0; i < num_first_plt_words - 1; i++)
4748 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
4749 // Last word in first PLT entry is &GOT[0] - .
4750 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
4751 got_address - (plt_address + 16));
4752 pov += sizeof(first_plt_entry);
4753
4754 unsigned char* got_pov = got_view;
4755
4756 memset(got_pov, 0, 12);
4757 got_pov += 12;
4758
4759 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
4760 unsigned int plt_offset = sizeof(first_plt_entry);
4761 unsigned int plt_rel_offset = 0;
4762 unsigned int got_offset = 12;
4763 const unsigned int count = this->count_;
4764 for (unsigned int i = 0;
4765 i < count;
4766 ++i,
4767 pov += sizeof(plt_entry),
4768 got_pov += 4,
4769 plt_offset += sizeof(plt_entry),
4770 plt_rel_offset += rel_size,
4771 got_offset += 4)
4772 {
4773 // Set and adjust the PLT entry itself.
4774 int32_t offset = ((got_address + got_offset)
4775 - (plt_address + plt_offset + 8));
4776
4777 gold_assert(offset >= 0 && offset < 0x0fffffff);
4778 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
4779 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
4780 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
4781 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
4782 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
4783 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
4784
4785 // Set the entry in the GOT.
4786 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
4787 }
4788
4789 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4790 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4791
4792 of->write_output_view(offset, oview_size, oview);
4793 of->write_output_view(got_file_offset, got_size, got_view);
4794 }
4795
4796 // Create a PLT entry for a global symbol.
4797
4798 template<bool big_endian>
4799 void
4800 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
4801 Symbol* gsym)
4802 {
4803 if (gsym->has_plt_offset())
4804 return;
4805
4806 if (this->plt_ == NULL)
4807 {
4808 // Create the GOT sections first.
4809 this->got_section(symtab, layout);
4810
4811 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
4812 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
4813 (elfcpp::SHF_ALLOC
4814 | elfcpp::SHF_EXECINSTR),
4815 this->plt_, false, false, false, false);
4816 }
4817 this->plt_->add_entry(gsym);
4818 }
4819
4820 // Report an unsupported relocation against a local symbol.
4821
4822 template<bool big_endian>
4823 void
4824 Target_arm<big_endian>::Scan::unsupported_reloc_local(
4825 Sized_relobj<32, big_endian>* object,
4826 unsigned int r_type)
4827 {
4828 gold_error(_("%s: unsupported reloc %u against local symbol"),
4829 object->name().c_str(), r_type);
4830 }
4831
4832 // We are about to emit a dynamic relocation of type R_TYPE. If the
4833 // dynamic linker does not support it, issue an error. The GNU linker
4834 // only issues a non-PIC error for an allocated read-only section.
4835 // Here we know the section is allocated, but we don't know that it is
4836 // read-only. But we check for all the relocation types which the
4837 // glibc dynamic linker supports, so it seems appropriate to issue an
4838 // error even if the section is not read-only.
4839
4840 template<bool big_endian>
4841 void
4842 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
4843 unsigned int r_type)
4844 {
4845 switch (r_type)
4846 {
4847 // These are the relocation types supported by glibc for ARM.
4848 case elfcpp::R_ARM_RELATIVE:
4849 case elfcpp::R_ARM_COPY:
4850 case elfcpp::R_ARM_GLOB_DAT:
4851 case elfcpp::R_ARM_JUMP_SLOT:
4852 case elfcpp::R_ARM_ABS32:
4853 case elfcpp::R_ARM_ABS32_NOI:
4854 case elfcpp::R_ARM_PC24:
4855 // FIXME: The following 3 types are not supported by Android's dynamic
4856 // linker.
4857 case elfcpp::R_ARM_TLS_DTPMOD32:
4858 case elfcpp::R_ARM_TLS_DTPOFF32:
4859 case elfcpp::R_ARM_TLS_TPOFF32:
4860 return;
4861
4862 default:
4863 // This prevents us from issuing more than one error per reloc
4864 // section. But we can still wind up issuing more than one
4865 // error per object file.
4866 if (this->issued_non_pic_error_)
4867 return;
4868 object->error(_("requires unsupported dynamic reloc; "
4869 "recompile with -fPIC"));
4870 this->issued_non_pic_error_ = true;
4871 return;
4872
4873 case elfcpp::R_ARM_NONE:
4874 gold_unreachable();
4875 }
4876 }
4877
4878 // Scan a relocation for a local symbol.
4879 // FIXME: This only handles a subset of relocation types used by Android
4880 // on ARM v5te devices.
4881
4882 template<bool big_endian>
4883 inline void
4884 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
4885 Layout* layout,
4886 Target_arm* target,
4887 Sized_relobj<32, big_endian>* object,
4888 unsigned int data_shndx,
4889 Output_section* output_section,
4890 const elfcpp::Rel<32, big_endian>& reloc,
4891 unsigned int r_type,
4892 const elfcpp::Sym<32, big_endian>&)
4893 {
4894 r_type = get_real_reloc_type(r_type);
4895 switch (r_type)
4896 {
4897 case elfcpp::R_ARM_NONE:
4898 break;
4899
4900 case elfcpp::R_ARM_ABS32:
4901 case elfcpp::R_ARM_ABS32_NOI:
4902 // If building a shared library (or a position-independent
4903 // executable), we need to create a dynamic relocation for
4904 // this location. The relocation applied at link time will
4905 // apply the link-time value, so we flag the location with
4906 // an R_ARM_RELATIVE relocation so the dynamic loader can
4907 // relocate it easily.
4908 if (parameters->options().output_is_position_independent())
4909 {
4910 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4911 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4912 // If we are to add more other reloc types than R_ARM_ABS32,
4913 // we need to add check_non_pic(object, r_type) here.
4914 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
4915 output_section, data_shndx,
4916 reloc.get_r_offset());
4917 }
4918 break;
4919
4920 case elfcpp::R_ARM_REL32:
4921 case elfcpp::R_ARM_THM_CALL:
4922 case elfcpp::R_ARM_CALL:
4923 case elfcpp::R_ARM_PREL31:
4924 case elfcpp::R_ARM_JUMP24:
4925 case elfcpp::R_ARM_PLT32:
4926 case elfcpp::R_ARM_THM_ABS5:
4927 case elfcpp::R_ARM_ABS8:
4928 case elfcpp::R_ARM_ABS12:
4929 case elfcpp::R_ARM_ABS16:
4930 case elfcpp::R_ARM_BASE_ABS:
4931 case elfcpp::R_ARM_MOVW_ABS_NC:
4932 case elfcpp::R_ARM_MOVT_ABS:
4933 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4934 case elfcpp::R_ARM_THM_MOVT_ABS:
4935 case elfcpp::R_ARM_MOVW_PREL_NC:
4936 case elfcpp::R_ARM_MOVT_PREL:
4937 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4938 case elfcpp::R_ARM_THM_MOVT_PREL:
4939 break;
4940
4941 case elfcpp::R_ARM_GOTOFF32:
4942 // We need a GOT section:
4943 target->got_section(symtab, layout);
4944 break;
4945
4946 case elfcpp::R_ARM_BASE_PREL:
4947 // FIXME: What about this?
4948 break;
4949
4950 case elfcpp::R_ARM_GOT_BREL:
4951 case elfcpp::R_ARM_GOT_PREL:
4952 {
4953 // The symbol requires a GOT entry.
4954 Output_data_got<32, big_endian>* got =
4955 target->got_section(symtab, layout);
4956 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4957 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
4958 {
4959 // If we are generating a shared object, we need to add a
4960 // dynamic RELATIVE relocation for this symbol's GOT entry.
4961 if (parameters->options().output_is_position_independent())
4962 {
4963 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4964 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4965 rel_dyn->add_local_relative(
4966 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
4967 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
4968 }
4969 }
4970 }
4971 break;
4972
4973 case elfcpp::R_ARM_TARGET1:
4974 // This should have been mapped to another type already.
4975 // Fall through.
4976 case elfcpp::R_ARM_COPY:
4977 case elfcpp::R_ARM_GLOB_DAT:
4978 case elfcpp::R_ARM_JUMP_SLOT:
4979 case elfcpp::R_ARM_RELATIVE:
4980 // These are relocations which should only be seen by the
4981 // dynamic linker, and should never be seen here.
4982 gold_error(_("%s: unexpected reloc %u in object file"),
4983 object->name().c_str(), r_type);
4984 break;
4985
4986 default:
4987 unsupported_reloc_local(object, r_type);
4988 break;
4989 }
4990 }
4991
4992 // Report an unsupported relocation against a global symbol.
4993
4994 template<bool big_endian>
4995 void
4996 Target_arm<big_endian>::Scan::unsupported_reloc_global(
4997 Sized_relobj<32, big_endian>* object,
4998 unsigned int r_type,
4999 Symbol* gsym)
5000 {
5001 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5002 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5003 }
5004
5005 // Scan a relocation for a global symbol.
5006 // FIXME: This only handles a subset of relocation types used by Android
5007 // on ARM v5te devices.
5008
5009 template<bool big_endian>
5010 inline void
5011 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
5012 Layout* layout,
5013 Target_arm* target,
5014 Sized_relobj<32, big_endian>* object,
5015 unsigned int data_shndx,
5016 Output_section* output_section,
5017 const elfcpp::Rel<32, big_endian>& reloc,
5018 unsigned int r_type,
5019 Symbol* gsym)
5020 {
5021 r_type = get_real_reloc_type(r_type);
5022 switch (r_type)
5023 {
5024 case elfcpp::R_ARM_NONE:
5025 break;
5026
5027 case elfcpp::R_ARM_ABS32:
5028 case elfcpp::R_ARM_ABS32_NOI:
5029 {
5030 // Make a dynamic relocation if necessary.
5031 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
5032 {
5033 if (target->may_need_copy_reloc(gsym))
5034 {
5035 target->copy_reloc(symtab, layout, object,
5036 data_shndx, output_section, gsym, reloc);
5037 }
5038 else if (gsym->can_use_relative_reloc(false))
5039 {
5040 // If we are to add more other reloc types than R_ARM_ABS32,
5041 // we need to add check_non_pic(object, r_type) here.
5042 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5043 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
5044 output_section, object,
5045 data_shndx, reloc.get_r_offset());
5046 }
5047 else
5048 {
5049 // If we are to add more other reloc types than R_ARM_ABS32,
5050 // we need to add check_non_pic(object, r_type) here.
5051 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5052 rel_dyn->add_global(gsym, r_type, output_section, object,
5053 data_shndx, reloc.get_r_offset());
5054 }
5055 }
5056 }
5057 break;
5058
5059 case elfcpp::R_ARM_MOVW_ABS_NC:
5060 case elfcpp::R_ARM_MOVT_ABS:
5061 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5062 case elfcpp::R_ARM_THM_MOVT_ABS:
5063 case elfcpp::R_ARM_MOVW_PREL_NC:
5064 case elfcpp::R_ARM_MOVT_PREL:
5065 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5066 case elfcpp::R_ARM_THM_MOVT_PREL:
5067 break;
5068
5069 case elfcpp::R_ARM_THM_ABS5:
5070 case elfcpp::R_ARM_ABS8:
5071 case elfcpp::R_ARM_ABS12:
5072 case elfcpp::R_ARM_ABS16:
5073 case elfcpp::R_ARM_BASE_ABS:
5074 {
5075 // No dynamic relocs of this kinds.
5076 // Report the error in case of PIC.
5077 int flags = Symbol::NON_PIC_REF;
5078 if (gsym->type() == elfcpp::STT_FUNC
5079 || gsym->type() == elfcpp::STT_ARM_TFUNC)
5080 flags |= Symbol::FUNCTION_CALL;
5081 if (gsym->needs_dynamic_reloc(flags))
5082 check_non_pic(object, r_type);
5083 }
5084 break;
5085
5086 case elfcpp::R_ARM_REL32:
5087 case elfcpp::R_ARM_PREL31:
5088 {
5089 // Make a dynamic relocation if necessary.
5090 int flags = Symbol::NON_PIC_REF;
5091 if (gsym->needs_dynamic_reloc(flags))
5092 {
5093 if (target->may_need_copy_reloc(gsym))
5094 {
5095 target->copy_reloc(symtab, layout, object,
5096 data_shndx, output_section, gsym, reloc);
5097 }
5098 else
5099 {
5100 check_non_pic(object, r_type);
5101 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5102 rel_dyn->add_global(gsym, r_type, output_section, object,
5103 data_shndx, reloc.get_r_offset());
5104 }
5105 }
5106 }
5107 break;
5108
5109 case elfcpp::R_ARM_JUMP24:
5110 case elfcpp::R_ARM_THM_JUMP24:
5111 case elfcpp::R_ARM_CALL:
5112 case elfcpp::R_ARM_THM_CALL:
5113
5114 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
5115 target->make_plt_entry(symtab, layout, gsym);
5116 else
5117 {
5118 // Check to see if this is a function that would need a PLT
5119 // but does not get one because the function symbol is untyped.
5120 // This happens in assembly code missing a proper .type directive.
5121 if ((!gsym->is_undefined() || parameters->options().shared())
5122 && !parameters->doing_static_link()
5123 && gsym->type() == elfcpp::STT_NOTYPE
5124 && (gsym->is_from_dynobj()
5125 || gsym->is_undefined()
5126 || gsym->is_preemptible()))
5127 gold_error(_("%s is not a function."),
5128 gsym->demangled_name().c_str());
5129 }
5130 break;
5131
5132 case elfcpp::R_ARM_PLT32:
5133 // If the symbol is fully resolved, this is just a relative
5134 // local reloc. Otherwise we need a PLT entry.
5135 if (gsym->final_value_is_known())
5136 break;
5137 // If building a shared library, we can also skip the PLT entry
5138 // if the symbol is defined in the output file and is protected
5139 // or hidden.
5140 if (gsym->is_defined()
5141 && !gsym->is_from_dynobj()
5142 && !gsym->is_preemptible())
5143 break;
5144 target->make_plt_entry(symtab, layout, gsym);
5145 break;
5146
5147 case elfcpp::R_ARM_GOTOFF32:
5148 // We need a GOT section.
5149 target->got_section(symtab, layout);
5150 break;
5151
5152 case elfcpp::R_ARM_BASE_PREL:
5153 // FIXME: What about this?
5154 break;
5155
5156 case elfcpp::R_ARM_GOT_BREL:
5157 case elfcpp::R_ARM_GOT_PREL:
5158 {
5159 // The symbol requires a GOT entry.
5160 Output_data_got<32, big_endian>* got =
5161 target->got_section(symtab, layout);
5162 if (gsym->final_value_is_known())
5163 got->add_global(gsym, GOT_TYPE_STANDARD);
5164 else
5165 {
5166 // If this symbol is not fully resolved, we need to add a
5167 // GOT entry with a dynamic relocation.
5168 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
5169 if (gsym->is_from_dynobj()
5170 || gsym->is_undefined()
5171 || gsym->is_preemptible())
5172 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
5173 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
5174 else
5175 {
5176 if (got->add_global(gsym, GOT_TYPE_STANDARD))
5177 rel_dyn->add_global_relative(
5178 gsym, elfcpp::R_ARM_RELATIVE, got,
5179 gsym->got_offset(GOT_TYPE_STANDARD));
5180 }
5181 }
5182 }
5183 break;
5184
5185 case elfcpp::R_ARM_TARGET1:
5186 // This should have been mapped to another type already.
5187 // Fall through.
5188 case elfcpp::R_ARM_COPY:
5189 case elfcpp::R_ARM_GLOB_DAT:
5190 case elfcpp::R_ARM_JUMP_SLOT:
5191 case elfcpp::R_ARM_RELATIVE:
5192 // These are relocations which should only be seen by the
5193 // dynamic linker, and should never be seen here.
5194 gold_error(_("%s: unexpected reloc %u in object file"),
5195 object->name().c_str(), r_type);
5196 break;
5197
5198 default:
5199 unsupported_reloc_global(object, r_type, gsym);
5200 break;
5201 }
5202 }
5203
5204 // Process relocations for gc.
5205
5206 template<bool big_endian>
5207 void
5208 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
5209 Layout* layout,
5210 Sized_relobj<32, big_endian>* object,
5211 unsigned int data_shndx,
5212 unsigned int,
5213 const unsigned char* prelocs,
5214 size_t reloc_count,
5215 Output_section* output_section,
5216 bool needs_special_offset_handling,
5217 size_t local_symbol_count,
5218 const unsigned char* plocal_symbols)
5219 {
5220 typedef Target_arm<big_endian> Arm;
5221 typedef typename Target_arm<big_endian>::Scan Scan;
5222
5223 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
5224 symtab,
5225 layout,
5226 this,
5227 object,
5228 data_shndx,
5229 prelocs,
5230 reloc_count,
5231 output_section,
5232 needs_special_offset_handling,
5233 local_symbol_count,
5234 plocal_symbols);
5235 }
5236
5237 // Scan relocations for a section.
5238
5239 template<bool big_endian>
5240 void
5241 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
5242 Layout* layout,
5243 Sized_relobj<32, big_endian>* object,
5244 unsigned int data_shndx,
5245 unsigned int sh_type,
5246 const unsigned char* prelocs,
5247 size_t reloc_count,
5248 Output_section* output_section,
5249 bool needs_special_offset_handling,
5250 size_t local_symbol_count,
5251 const unsigned char* plocal_symbols)
5252 {
5253 typedef typename Target_arm<big_endian>::Scan Scan;
5254 if (sh_type == elfcpp::SHT_RELA)
5255 {
5256 gold_error(_("%s: unsupported RELA reloc section"),
5257 object->name().c_str());
5258 return;
5259 }
5260
5261 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
5262 symtab,
5263 layout,
5264 this,
5265 object,
5266 data_shndx,
5267 prelocs,
5268 reloc_count,
5269 output_section,
5270 needs_special_offset_handling,
5271 local_symbol_count,
5272 plocal_symbols);
5273 }
5274
5275 // Finalize the sections.
5276
5277 template<bool big_endian>
5278 void
5279 Target_arm<big_endian>::do_finalize_sections(
5280 Layout* layout,
5281 const Input_objects* input_objects,
5282 Symbol_table* symtab)
5283 {
5284 // Merge processor-specific flags.
5285 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
5286 p != input_objects->relobj_end();
5287 ++p)
5288 {
5289 Arm_relobj<big_endian>* arm_relobj =
5290 Arm_relobj<big_endian>::as_arm_relobj(*p);
5291 this->merge_processor_specific_flags(
5292 arm_relobj->name(),
5293 arm_relobj->processor_specific_flags());
5294 this->merge_object_attributes(arm_relobj->name().c_str(),
5295 arm_relobj->attributes_section_data());
5296
5297 }
5298
5299 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
5300 p != input_objects->dynobj_end();
5301 ++p)
5302 {
5303 Arm_dynobj<big_endian>* arm_dynobj =
5304 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
5305 this->merge_processor_specific_flags(
5306 arm_dynobj->name(),
5307 arm_dynobj->processor_specific_flags());
5308 this->merge_object_attributes(arm_dynobj->name().c_str(),
5309 arm_dynobj->attributes_section_data());
5310 }
5311
5312 // Check BLX use.
5313 Object_attribute* attr =
5314 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
5315 if (attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
5316 this->set_may_use_blx(true);
5317
5318 // Fill in some more dynamic tags.
5319 const Reloc_section* rel_plt = (this->plt_ == NULL
5320 ? NULL
5321 : this->plt_->rel_plt());
5322 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
5323 this->rel_dyn_, true);
5324
5325 // Emit any relocs we saved in an attempt to avoid generating COPY
5326 // relocs.
5327 if (this->copy_relocs_.any_saved_relocs())
5328 this->copy_relocs_.emit(this->rel_dyn_section(layout));
5329
5330 // Handle the .ARM.exidx section.
5331 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
5332 if (exidx_section != NULL
5333 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
5334 && !parameters->options().relocatable())
5335 {
5336 // Create __exidx_start and __exdix_end symbols.
5337 symtab->define_in_output_data("__exidx_start", NULL,
5338 Symbol_table::PREDEFINED,
5339 exidx_section, 0, 0, elfcpp::STT_OBJECT,
5340 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
5341 false, true);
5342 symtab->define_in_output_data("__exidx_end", NULL,
5343 Symbol_table::PREDEFINED,
5344 exidx_section, 0, 0, elfcpp::STT_OBJECT,
5345 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
5346 true, true);
5347
5348 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
5349 // the .ARM.exidx section.
5350 if (!layout->script_options()->saw_phdrs_clause())
5351 {
5352 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
5353 == NULL);
5354 Output_segment* exidx_segment =
5355 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
5356 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
5357 false);
5358 }
5359 }
5360
5361 // Create an .ARM.attributes section if there is not one already.
5362 Output_attributes_section_data* attributes_section =
5363 new Output_attributes_section_data(*this->attributes_section_data_);
5364 layout->add_output_section_data(".ARM.attributes",
5365 elfcpp::SHT_ARM_ATTRIBUTES, 0,
5366 attributes_section, false, false, false,
5367 false);
5368 }
5369
5370 // Return whether a direct absolute static relocation needs to be applied.
5371 // In cases where Scan::local() or Scan::global() has created
5372 // a dynamic relocation other than R_ARM_RELATIVE, the addend
5373 // of the relocation is carried in the data, and we must not
5374 // apply the static relocation.
5375
5376 template<bool big_endian>
5377 inline bool
5378 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
5379 const Sized_symbol<32>* gsym,
5380 int ref_flags,
5381 bool is_32bit,
5382 Output_section* output_section)
5383 {
5384 // If the output section is not allocated, then we didn't call
5385 // scan_relocs, we didn't create a dynamic reloc, and we must apply
5386 // the reloc here.
5387 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
5388 return true;
5389
5390 // For local symbols, we will have created a non-RELATIVE dynamic
5391 // relocation only if (a) the output is position independent,
5392 // (b) the relocation is absolute (not pc- or segment-relative), and
5393 // (c) the relocation is not 32 bits wide.
5394 if (gsym == NULL)
5395 return !(parameters->options().output_is_position_independent()
5396 && (ref_flags & Symbol::ABSOLUTE_REF)
5397 && !is_32bit);
5398
5399 // For global symbols, we use the same helper routines used in the
5400 // scan pass. If we did not create a dynamic relocation, or if we
5401 // created a RELATIVE dynamic relocation, we should apply the static
5402 // relocation.
5403 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
5404 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
5405 && gsym->can_use_relative_reloc(ref_flags
5406 & Symbol::FUNCTION_CALL);
5407 return !has_dyn || is_rel;
5408 }
5409
5410 // Perform a relocation.
5411
5412 template<bool big_endian>
5413 inline bool
5414 Target_arm<big_endian>::Relocate::relocate(
5415 const Relocate_info<32, big_endian>* relinfo,
5416 Target_arm* target,
5417 Output_section *output_section,
5418 size_t relnum,
5419 const elfcpp::Rel<32, big_endian>& rel,
5420 unsigned int r_type,
5421 const Sized_symbol<32>* gsym,
5422 const Symbol_value<32>* psymval,
5423 unsigned char* view,
5424 Arm_address address,
5425 section_size_type /* view_size */ )
5426 {
5427 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
5428
5429 r_type = get_real_reloc_type(r_type);
5430
5431 const Arm_relobj<big_endian>* object =
5432 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
5433
5434 // If the final branch target of a relocation is THUMB instruction, this
5435 // is 1. Otherwise it is 0.
5436 Arm_address thumb_bit = 0;
5437 Symbol_value<32> symval;
5438 bool is_weakly_undefined_without_plt = false;
5439 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
5440 {
5441 if (gsym != NULL)
5442 {
5443 // This is a global symbol. Determine if we use PLT and if the
5444 // final target is THUMB.
5445 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
5446 {
5447 // This uses a PLT, change the symbol value.
5448 symval.set_output_value(target->plt_section()->address()
5449 + gsym->plt_offset());
5450 psymval = &symval;
5451 }
5452 else if (gsym->is_weak_undefined())
5453 {
5454 // This is a weakly undefined symbol and we do not use PLT
5455 // for this relocation. A branch targeting this symbol will
5456 // be converted into an NOP.
5457 is_weakly_undefined_without_plt = true;
5458 }
5459 else
5460 {
5461 // Set thumb bit if symbol:
5462 // -Has type STT_ARM_TFUNC or
5463 // -Has type STT_FUNC, is defined and with LSB in value set.
5464 thumb_bit =
5465 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
5466 || (gsym->type() == elfcpp::STT_FUNC
5467 && !gsym->is_undefined()
5468 && ((psymval->value(object, 0) & 1) != 0)))
5469 ? 1
5470 : 0);
5471 }
5472 }
5473 else
5474 {
5475 // This is a local symbol. Determine if the final target is THUMB.
5476 // We saved this information when all the local symbols were read.
5477 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
5478 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
5479 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
5480 }
5481 }
5482 else
5483 {
5484 // This is a fake relocation synthesized for a stub. It does not have
5485 // a real symbol. We just look at the LSB of the symbol value to
5486 // determine if the target is THUMB or not.
5487 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
5488 }
5489
5490 // Strip LSB if this points to a THUMB target.
5491 if (thumb_bit != 0
5492 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
5493 && ((psymval->value(object, 0) & 1) != 0))
5494 {
5495 Arm_address stripped_value =
5496 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
5497 symval.set_output_value(stripped_value);
5498 psymval = &symval;
5499 }
5500
5501 // Get the GOT offset if needed.
5502 // The GOT pointer points to the end of the GOT section.
5503 // We need to subtract the size of the GOT section to get
5504 // the actual offset to use in the relocation.
5505 bool have_got_offset = false;
5506 unsigned int got_offset = 0;
5507 switch (r_type)
5508 {
5509 case elfcpp::R_ARM_GOT_BREL:
5510 case elfcpp::R_ARM_GOT_PREL:
5511 if (gsym != NULL)
5512 {
5513 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5514 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
5515 - target->got_size());
5516 }
5517 else
5518 {
5519 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5520 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5521 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
5522 - target->got_size());
5523 }
5524 have_got_offset = true;
5525 break;
5526
5527 default:
5528 break;
5529 }
5530
5531 // To look up relocation stubs, we need to pass the symbol table index of
5532 // a local symbol.
5533 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5534
5535 typename Arm_relocate_functions::Status reloc_status =
5536 Arm_relocate_functions::STATUS_OKAY;
5537 switch (r_type)
5538 {
5539 case elfcpp::R_ARM_NONE:
5540 break;
5541
5542 case elfcpp::R_ARM_ABS8:
5543 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5544 output_section))
5545 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
5546 break;
5547
5548 case elfcpp::R_ARM_ABS12:
5549 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5550 output_section))
5551 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
5552 break;
5553
5554 case elfcpp::R_ARM_ABS16:
5555 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5556 output_section))
5557 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5558 break;
5559
5560 case elfcpp::R_ARM_ABS32:
5561 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5562 output_section))
5563 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5564 thumb_bit);
5565 break;
5566
5567 case elfcpp::R_ARM_ABS32_NOI:
5568 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5569 output_section))
5570 // No thumb bit for this relocation: (S + A)
5571 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5572 0);
5573 break;
5574
5575 case elfcpp::R_ARM_MOVW_ABS_NC:
5576 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5577 output_section))
5578 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
5579 psymval,
5580 thumb_bit);
5581 else
5582 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5583 "a shared object; recompile with -fPIC"));
5584 break;
5585
5586 case elfcpp::R_ARM_MOVT_ABS:
5587 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5588 output_section))
5589 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
5590 else
5591 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5592 "a shared object; recompile with -fPIC"));
5593 break;
5594
5595 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5596 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5597 output_section))
5598 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
5599 psymval,
5600 thumb_bit);
5601 else
5602 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5603 "making a shared object; recompile with -fPIC"));
5604 break;
5605
5606 case elfcpp::R_ARM_THM_MOVT_ABS:
5607 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5608 output_section))
5609 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
5610 psymval);
5611 else
5612 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5613 "making a shared object; recompile with -fPIC"));
5614 break;
5615
5616 case elfcpp::R_ARM_MOVW_PREL_NC:
5617 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
5618 psymval, address,
5619 thumb_bit);
5620 break;
5621
5622 case elfcpp::R_ARM_MOVT_PREL:
5623 reloc_status = Arm_relocate_functions::movt_prel(view, object,
5624 psymval, address);
5625 break;
5626
5627 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5628 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
5629 psymval, address,
5630 thumb_bit);
5631 break;
5632
5633 case elfcpp::R_ARM_THM_MOVT_PREL:
5634 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
5635 psymval, address);
5636 break;
5637
5638 case elfcpp::R_ARM_REL32:
5639 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5640 address, thumb_bit);
5641 break;
5642
5643 case elfcpp::R_ARM_THM_ABS5:
5644 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5645 output_section))
5646 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
5647 break;
5648
5649 case elfcpp::R_ARM_THM_CALL:
5650 reloc_status =
5651 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
5652 psymval, address, thumb_bit,
5653 is_weakly_undefined_without_plt);
5654 break;
5655
5656 case elfcpp::R_ARM_XPC25:
5657 reloc_status =
5658 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
5659 psymval, address, thumb_bit,
5660 is_weakly_undefined_without_plt);
5661 break;
5662
5663 case elfcpp::R_ARM_THM_XPC22:
5664 reloc_status =
5665 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
5666 psymval, address, thumb_bit,
5667 is_weakly_undefined_without_plt);
5668 break;
5669
5670 case elfcpp::R_ARM_GOTOFF32:
5671 {
5672 Arm_address got_origin;
5673 got_origin = target->got_plt_section()->address();
5674 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5675 got_origin, thumb_bit);
5676 }
5677 break;
5678
5679 case elfcpp::R_ARM_BASE_PREL:
5680 {
5681 uint32_t origin;
5682 // Get the addressing origin of the output segment defining the
5683 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5684 gold_assert(gsym != NULL);
5685 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5686 origin = gsym->output_segment()->vaddr();
5687 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5688 origin = gsym->output_data()->address();
5689 else
5690 {
5691 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5692 _("cannot find origin of R_ARM_BASE_PREL"));
5693 return true;
5694 }
5695 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
5696 }
5697 break;
5698
5699 case elfcpp::R_ARM_BASE_ABS:
5700 {
5701 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5702 output_section))
5703 break;
5704
5705 uint32_t origin;
5706 // Get the addressing origin of the output segment defining
5707 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5708 if (gsym == NULL)
5709 // R_ARM_BASE_ABS with the NULL symbol will give the
5710 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5711 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5712 origin = target->got_plt_section()->address();
5713 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5714 origin = gsym->output_segment()->vaddr();
5715 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5716 origin = gsym->output_data()->address();
5717 else
5718 {
5719 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5720 _("cannot find origin of R_ARM_BASE_ABS"));
5721 return true;
5722 }
5723
5724 reloc_status = Arm_relocate_functions::base_abs(view, origin);
5725 }
5726 break;
5727
5728 case elfcpp::R_ARM_GOT_BREL:
5729 gold_assert(have_got_offset);
5730 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
5731 break;
5732
5733 case elfcpp::R_ARM_GOT_PREL:
5734 gold_assert(have_got_offset);
5735 // Get the address origin for GOT PLT, which is allocated right
5736 // after the GOT section, to calculate an absolute address of
5737 // the symbol GOT entry (got_origin + got_offset).
5738 Arm_address got_origin;
5739 got_origin = target->got_plt_section()->address();
5740 reloc_status = Arm_relocate_functions::got_prel(view,
5741 got_origin + got_offset,
5742 address);
5743 break;
5744
5745 case elfcpp::R_ARM_PLT32:
5746 gold_assert(gsym == NULL
5747 || gsym->has_plt_offset()
5748 || gsym->final_value_is_known()
5749 || (gsym->is_defined()
5750 && !gsym->is_from_dynobj()
5751 && !gsym->is_preemptible()));
5752 reloc_status =
5753 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
5754 psymval, address, thumb_bit,
5755 is_weakly_undefined_without_plt);
5756 break;
5757
5758 case elfcpp::R_ARM_CALL:
5759 reloc_status =
5760 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
5761 psymval, address, thumb_bit,
5762 is_weakly_undefined_without_plt);
5763 break;
5764
5765 case elfcpp::R_ARM_JUMP24:
5766 reloc_status =
5767 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
5768 psymval, address, thumb_bit,
5769 is_weakly_undefined_without_plt);
5770 break;
5771
5772 case elfcpp::R_ARM_THM_JUMP24:
5773 reloc_status =
5774 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
5775 psymval, address, thumb_bit,
5776 is_weakly_undefined_without_plt);
5777 break;
5778
5779 case elfcpp::R_ARM_PREL31:
5780 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
5781 address, thumb_bit);
5782 break;
5783
5784 case elfcpp::R_ARM_TARGET1:
5785 // This should have been mapped to another type already.
5786 // Fall through.
5787 case elfcpp::R_ARM_COPY:
5788 case elfcpp::R_ARM_GLOB_DAT:
5789 case elfcpp::R_ARM_JUMP_SLOT:
5790 case elfcpp::R_ARM_RELATIVE:
5791 // These are relocations which should only be seen by the
5792 // dynamic linker, and should never be seen here.
5793 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5794 _("unexpected reloc %u in object file"),
5795 r_type);
5796 break;
5797
5798 default:
5799 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5800 _("unsupported reloc %u"),
5801 r_type);
5802 break;
5803 }
5804
5805 // Report any errors.
5806 switch (reloc_status)
5807 {
5808 case Arm_relocate_functions::STATUS_OKAY:
5809 break;
5810 case Arm_relocate_functions::STATUS_OVERFLOW:
5811 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5812 _("relocation overflow in relocation %u"),
5813 r_type);
5814 break;
5815 case Arm_relocate_functions::STATUS_BAD_RELOC:
5816 gold_error_at_location(
5817 relinfo,
5818 relnum,
5819 rel.get_r_offset(),
5820 _("unexpected opcode while processing relocation %u"),
5821 r_type);
5822 break;
5823 default:
5824 gold_unreachable();
5825 }
5826
5827 return true;
5828 }
5829
5830 // Relocate section data.
5831
5832 template<bool big_endian>
5833 void
5834 Target_arm<big_endian>::relocate_section(
5835 const Relocate_info<32, big_endian>* relinfo,
5836 unsigned int sh_type,
5837 const unsigned char* prelocs,
5838 size_t reloc_count,
5839 Output_section* output_section,
5840 bool needs_special_offset_handling,
5841 unsigned char* view,
5842 Arm_address address,
5843 section_size_type view_size,
5844 const Reloc_symbol_changes* reloc_symbol_changes)
5845 {
5846 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
5847 gold_assert(sh_type == elfcpp::SHT_REL);
5848
5849 Arm_input_section<big_endian>* arm_input_section =
5850 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
5851
5852 // This is an ARM input section and the view covers the whole output
5853 // section.
5854 if (arm_input_section != NULL)
5855 {
5856 gold_assert(needs_special_offset_handling);
5857 Arm_address section_address = arm_input_section->address();
5858 section_size_type section_size = arm_input_section->data_size();
5859
5860 gold_assert((arm_input_section->address() >= address)
5861 && ((arm_input_section->address()
5862 + arm_input_section->data_size())
5863 <= (address + view_size)));
5864
5865 off_t offset = section_address - address;
5866 view += offset;
5867 address += offset;
5868 view_size = section_size;
5869 }
5870
5871 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
5872 Arm_relocate>(
5873 relinfo,
5874 this,
5875 prelocs,
5876 reloc_count,
5877 output_section,
5878 needs_special_offset_handling,
5879 view,
5880 address,
5881 view_size,
5882 reloc_symbol_changes);
5883 }
5884
5885 // Return the size of a relocation while scanning during a relocatable
5886 // link.
5887
5888 template<bool big_endian>
5889 unsigned int
5890 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
5891 unsigned int r_type,
5892 Relobj* object)
5893 {
5894 r_type = get_real_reloc_type(r_type);
5895 switch (r_type)
5896 {
5897 case elfcpp::R_ARM_NONE:
5898 return 0;
5899
5900 case elfcpp::R_ARM_ABS8:
5901 return 1;
5902
5903 case elfcpp::R_ARM_ABS16:
5904 case elfcpp::R_ARM_THM_ABS5:
5905 return 2;
5906
5907 case elfcpp::R_ARM_ABS32:
5908 case elfcpp::R_ARM_ABS32_NOI:
5909 case elfcpp::R_ARM_ABS12:
5910 case elfcpp::R_ARM_BASE_ABS:
5911 case elfcpp::R_ARM_REL32:
5912 case elfcpp::R_ARM_THM_CALL:
5913 case elfcpp::R_ARM_GOTOFF32:
5914 case elfcpp::R_ARM_BASE_PREL:
5915 case elfcpp::R_ARM_GOT_BREL:
5916 case elfcpp::R_ARM_GOT_PREL:
5917 case elfcpp::R_ARM_PLT32:
5918 case elfcpp::R_ARM_CALL:
5919 case elfcpp::R_ARM_JUMP24:
5920 case elfcpp::R_ARM_PREL31:
5921 case elfcpp::R_ARM_MOVW_ABS_NC:
5922 case elfcpp::R_ARM_MOVT_ABS:
5923 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5924 case elfcpp::R_ARM_THM_MOVT_ABS:
5925 case elfcpp::R_ARM_MOVW_PREL_NC:
5926 case elfcpp::R_ARM_MOVT_PREL:
5927 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5928 case elfcpp::R_ARM_THM_MOVT_PREL:
5929 return 4;
5930
5931 case elfcpp::R_ARM_TARGET1:
5932 // This should have been mapped to another type already.
5933 // Fall through.
5934 case elfcpp::R_ARM_COPY:
5935 case elfcpp::R_ARM_GLOB_DAT:
5936 case elfcpp::R_ARM_JUMP_SLOT:
5937 case elfcpp::R_ARM_RELATIVE:
5938 // These are relocations which should only be seen by the
5939 // dynamic linker, and should never be seen here.
5940 gold_error(_("%s: unexpected reloc %u in object file"),
5941 object->name().c_str(), r_type);
5942 return 0;
5943
5944 default:
5945 object->error(_("unsupported reloc %u in object file"), r_type);
5946 return 0;
5947 }
5948 }
5949
5950 // Scan the relocs during a relocatable link.
5951
5952 template<bool big_endian>
5953 void
5954 Target_arm<big_endian>::scan_relocatable_relocs(
5955 Symbol_table* symtab,
5956 Layout* layout,
5957 Sized_relobj<32, big_endian>* object,
5958 unsigned int data_shndx,
5959 unsigned int sh_type,
5960 const unsigned char* prelocs,
5961 size_t reloc_count,
5962 Output_section* output_section,
5963 bool needs_special_offset_handling,
5964 size_t local_symbol_count,
5965 const unsigned char* plocal_symbols,
5966 Relocatable_relocs* rr)
5967 {
5968 gold_assert(sh_type == elfcpp::SHT_REL);
5969
5970 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
5971 Relocatable_size_for_reloc> Scan_relocatable_relocs;
5972
5973 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
5974 Scan_relocatable_relocs>(
5975 symtab,
5976 layout,
5977 object,
5978 data_shndx,
5979 prelocs,
5980 reloc_count,
5981 output_section,
5982 needs_special_offset_handling,
5983 local_symbol_count,
5984 plocal_symbols,
5985 rr);
5986 }
5987
5988 // Relocate a section during a relocatable link.
5989
5990 template<bool big_endian>
5991 void
5992 Target_arm<big_endian>::relocate_for_relocatable(
5993 const Relocate_info<32, big_endian>* relinfo,
5994 unsigned int sh_type,
5995 const unsigned char* prelocs,
5996 size_t reloc_count,
5997 Output_section* output_section,
5998 off_t offset_in_output_section,
5999 const Relocatable_relocs* rr,
6000 unsigned char* view,
6001 Arm_address view_address,
6002 section_size_type view_size,
6003 unsigned char* reloc_view,
6004 section_size_type reloc_view_size)
6005 {
6006 gold_assert(sh_type == elfcpp::SHT_REL);
6007
6008 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
6009 relinfo,
6010 prelocs,
6011 reloc_count,
6012 output_section,
6013 offset_in_output_section,
6014 rr,
6015 view,
6016 view_address,
6017 view_size,
6018 reloc_view,
6019 reloc_view_size);
6020 }
6021
6022 // Return the value to use for a dynamic symbol which requires special
6023 // treatment. This is how we support equality comparisons of function
6024 // pointers across shared library boundaries, as described in the
6025 // processor specific ABI supplement.
6026
6027 template<bool big_endian>
6028 uint64_t
6029 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
6030 {
6031 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6032 return this->plt_section()->address() + gsym->plt_offset();
6033 }
6034
6035 // Map platform-specific relocs to real relocs
6036 //
6037 template<bool big_endian>
6038 unsigned int
6039 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
6040 {
6041 switch (r_type)
6042 {
6043 case elfcpp::R_ARM_TARGET1:
6044 // This is either R_ARM_ABS32 or R_ARM_REL32;
6045 return elfcpp::R_ARM_ABS32;
6046
6047 case elfcpp::R_ARM_TARGET2:
6048 // This can be any reloc type but ususally is R_ARM_GOT_PREL
6049 return elfcpp::R_ARM_GOT_PREL;
6050
6051 default:
6052 return r_type;
6053 }
6054 }
6055
6056 // Whether if two EABI versions V1 and V2 are compatible.
6057
6058 template<bool big_endian>
6059 bool
6060 Target_arm<big_endian>::are_eabi_versions_compatible(
6061 elfcpp::Elf_Word v1,
6062 elfcpp::Elf_Word v2)
6063 {
6064 // v4 and v5 are the same spec before and after it was released,
6065 // so allow mixing them.
6066 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
6067 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
6068 return true;
6069
6070 return v1 == v2;
6071 }
6072
6073 // Combine FLAGS from an input object called NAME and the processor-specific
6074 // flags in the ELF header of the output. Much of this is adapted from the
6075 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
6076 // in bfd/elf32-arm.c.
6077
6078 template<bool big_endian>
6079 void
6080 Target_arm<big_endian>::merge_processor_specific_flags(
6081 const std::string& name,
6082 elfcpp::Elf_Word flags)
6083 {
6084 if (this->are_processor_specific_flags_set())
6085 {
6086 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
6087
6088 // Nothing to merge if flags equal to those in output.
6089 if (flags == out_flags)
6090 return;
6091
6092 // Complain about various flag mismatches.
6093 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
6094 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
6095 if (!this->are_eabi_versions_compatible(version1, version2))
6096 gold_error(_("Source object %s has EABI version %d but output has "
6097 "EABI version %d."),
6098 name.c_str(),
6099 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
6100 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
6101 }
6102 else
6103 {
6104 // If the input is the default architecture and had the default
6105 // flags then do not bother setting the flags for the output
6106 // architecture, instead allow future merges to do this. If no
6107 // future merges ever set these flags then they will retain their
6108 // uninitialised values, which surprise surprise, correspond
6109 // to the default values.
6110 if (flags == 0)
6111 return;
6112
6113 // This is the first time, just copy the flags.
6114 // We only copy the EABI version for now.
6115 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
6116 }
6117 }
6118
6119 // Adjust ELF file header.
6120 template<bool big_endian>
6121 void
6122 Target_arm<big_endian>::do_adjust_elf_header(
6123 unsigned char* view,
6124 int len) const
6125 {
6126 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
6127
6128 elfcpp::Ehdr<32, big_endian> ehdr(view);
6129 unsigned char e_ident[elfcpp::EI_NIDENT];
6130 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
6131
6132 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
6133 == elfcpp::EF_ARM_EABI_UNKNOWN)
6134 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
6135 else
6136 e_ident[elfcpp::EI_OSABI] = 0;
6137 e_ident[elfcpp::EI_ABIVERSION] = 0;
6138
6139 // FIXME: Do EF_ARM_BE8 adjustment.
6140
6141 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
6142 oehdr.put_e_ident(e_ident);
6143 }
6144
6145 // do_make_elf_object to override the same function in the base class.
6146 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
6147 // to store ARM specific information. Hence we need to have our own
6148 // ELF object creation.
6149
6150 template<bool big_endian>
6151 Object*
6152 Target_arm<big_endian>::do_make_elf_object(
6153 const std::string& name,
6154 Input_file* input_file,
6155 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
6156 {
6157 int et = ehdr.get_e_type();
6158 if (et == elfcpp::ET_REL)
6159 {
6160 Arm_relobj<big_endian>* obj =
6161 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
6162 obj->setup();
6163 return obj;
6164 }
6165 else if (et == elfcpp::ET_DYN)
6166 {
6167 Sized_dynobj<32, big_endian>* obj =
6168 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
6169 obj->setup();
6170 return obj;
6171 }
6172 else
6173 {
6174 gold_error(_("%s: unsupported ELF file type %d"),
6175 name.c_str(), et);
6176 return NULL;
6177 }
6178 }
6179
6180 // Read the architecture from the Tag_also_compatible_with attribute, if any.
6181 // Returns -1 if no architecture could be read.
6182 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
6183
6184 template<bool big_endian>
6185 int
6186 Target_arm<big_endian>::get_secondary_compatible_arch(
6187 const Attributes_section_data* pasd)
6188 {
6189 const Object_attribute *known_attributes =
6190 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
6191
6192 // Note: the tag and its argument below are uleb128 values, though
6193 // currently-defined values fit in one byte for each.
6194 const std::string& sv =
6195 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
6196 if (sv.size() == 2
6197 && sv.data()[0] == elfcpp::Tag_CPU_arch
6198 && (sv.data()[1] & 128) != 128)
6199 return sv.data()[1];
6200
6201 // This tag is "safely ignorable", so don't complain if it looks funny.
6202 return -1;
6203 }
6204
6205 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
6206 // The tag is removed if ARCH is -1.
6207 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
6208
6209 template<bool big_endian>
6210 void
6211 Target_arm<big_endian>::set_secondary_compatible_arch(
6212 Attributes_section_data* pasd,
6213 int arch)
6214 {
6215 Object_attribute *known_attributes =
6216 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
6217
6218 if (arch == -1)
6219 {
6220 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
6221 return;
6222 }
6223
6224 // Note: the tag and its argument below are uleb128 values, though
6225 // currently-defined values fit in one byte for each.
6226 char sv[3];
6227 sv[0] = elfcpp::Tag_CPU_arch;
6228 gold_assert(arch != 0);
6229 sv[1] = arch;
6230 sv[2] = '\0';
6231
6232 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
6233 }
6234
6235 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
6236 // into account.
6237 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
6238
6239 template<bool big_endian>
6240 int
6241 Target_arm<big_endian>::tag_cpu_arch_combine(
6242 const char* name,
6243 int oldtag,
6244 int* secondary_compat_out,
6245 int newtag,
6246 int secondary_compat)
6247 {
6248 #define T(X) elfcpp::TAG_CPU_ARCH_##X
6249 static const int v6t2[] =
6250 {
6251 T(V6T2), // PRE_V4.
6252 T(V6T2), // V4.
6253 T(V6T2), // V4T.
6254 T(V6T2), // V5T.
6255 T(V6T2), // V5TE.
6256 T(V6T2), // V5TEJ.
6257 T(V6T2), // V6.
6258 T(V7), // V6KZ.
6259 T(V6T2) // V6T2.
6260 };
6261 static const int v6k[] =
6262 {
6263 T(V6K), // PRE_V4.
6264 T(V6K), // V4.
6265 T(V6K), // V4T.
6266 T(V6K), // V5T.
6267 T(V6K), // V5TE.
6268 T(V6K), // V5TEJ.
6269 T(V6K), // V6.
6270 T(V6KZ), // V6KZ.
6271 T(V7), // V6T2.
6272 T(V6K) // V6K.
6273 };
6274 static const int v7[] =
6275 {
6276 T(V7), // PRE_V4.
6277 T(V7), // V4.
6278 T(V7), // V4T.
6279 T(V7), // V5T.
6280 T(V7), // V5TE.
6281 T(V7), // V5TEJ.
6282 T(V7), // V6.
6283 T(V7), // V6KZ.
6284 T(V7), // V6T2.
6285 T(V7), // V6K.
6286 T(V7) // V7.
6287 };
6288 static const int v6_m[] =
6289 {
6290 -1, // PRE_V4.
6291 -1, // V4.
6292 T(V6K), // V4T.
6293 T(V6K), // V5T.
6294 T(V6K), // V5TE.
6295 T(V6K), // V5TEJ.
6296 T(V6K), // V6.
6297 T(V6KZ), // V6KZ.
6298 T(V7), // V6T2.
6299 T(V6K), // V6K.
6300 T(V7), // V7.
6301 T(V6_M) // V6_M.
6302 };
6303 static const int v6s_m[] =
6304 {
6305 -1, // PRE_V4.
6306 -1, // V4.
6307 T(V6K), // V4T.
6308 T(V6K), // V5T.
6309 T(V6K), // V5TE.
6310 T(V6K), // V5TEJ.
6311 T(V6K), // V6.
6312 T(V6KZ), // V6KZ.
6313 T(V7), // V6T2.
6314 T(V6K), // V6K.
6315 T(V7), // V7.
6316 T(V6S_M), // V6_M.
6317 T(V6S_M) // V6S_M.
6318 };
6319 static const int v7e_m[] =
6320 {
6321 -1, // PRE_V4.
6322 -1, // V4.
6323 T(V7E_M), // V4T.
6324 T(V7E_M), // V5T.
6325 T(V7E_M), // V5TE.
6326 T(V7E_M), // V5TEJ.
6327 T(V7E_M), // V6.
6328 T(V7E_M), // V6KZ.
6329 T(V7E_M), // V6T2.
6330 T(V7E_M), // V6K.
6331 T(V7E_M), // V7.
6332 T(V7E_M), // V6_M.
6333 T(V7E_M), // V6S_M.
6334 T(V7E_M) // V7E_M.
6335 };
6336 static const int v4t_plus_v6_m[] =
6337 {
6338 -1, // PRE_V4.
6339 -1, // V4.
6340 T(V4T), // V4T.
6341 T(V5T), // V5T.
6342 T(V5TE), // V5TE.
6343 T(V5TEJ), // V5TEJ.
6344 T(V6), // V6.
6345 T(V6KZ), // V6KZ.
6346 T(V6T2), // V6T2.
6347 T(V6K), // V6K.
6348 T(V7), // V7.
6349 T(V6_M), // V6_M.
6350 T(V6S_M), // V6S_M.
6351 T(V7E_M), // V7E_M.
6352 T(V4T_PLUS_V6_M) // V4T plus V6_M.
6353 };
6354 static const int *comb[] =
6355 {
6356 v6t2,
6357 v6k,
6358 v7,
6359 v6_m,
6360 v6s_m,
6361 v7e_m,
6362 // Pseudo-architecture.
6363 v4t_plus_v6_m
6364 };
6365
6366 // Check we've not got a higher architecture than we know about.
6367
6368 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
6369 {
6370 gold_error(_("%s: unknown CPU architecture"), name);
6371 return -1;
6372 }
6373
6374 // Override old tag if we have a Tag_also_compatible_with on the output.
6375
6376 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
6377 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
6378 oldtag = T(V4T_PLUS_V6_M);
6379
6380 // And override the new tag if we have a Tag_also_compatible_with on the
6381 // input.
6382
6383 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
6384 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
6385 newtag = T(V4T_PLUS_V6_M);
6386
6387 // Architectures before V6KZ add features monotonically.
6388 int tagh = std::max(oldtag, newtag);
6389 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
6390 return tagh;
6391
6392 int tagl = std::min(oldtag, newtag);
6393 int result = comb[tagh - T(V6T2)][tagl];
6394
6395 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
6396 // as the canonical version.
6397 if (result == T(V4T_PLUS_V6_M))
6398 {
6399 result = T(V4T);
6400 *secondary_compat_out = T(V6_M);
6401 }
6402 else
6403 *secondary_compat_out = -1;
6404
6405 if (result == -1)
6406 {
6407 gold_error(_("%s: conflicting CPU architectures %d/%d"),
6408 name, oldtag, newtag);
6409 return -1;
6410 }
6411
6412 return result;
6413 #undef T
6414 }
6415
6416 // Helper to print AEABI enum tag value.
6417
6418 template<bool big_endian>
6419 std::string
6420 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
6421 {
6422 static const char *aeabi_enum_names[] =
6423 { "", "variable-size", "32-bit", "" };
6424 const size_t aeabi_enum_names_size =
6425 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
6426
6427 if (value < aeabi_enum_names_size)
6428 return std::string(aeabi_enum_names[value]);
6429 else
6430 {
6431 char buffer[100];
6432 sprintf(buffer, "<unknown value %u>", value);
6433 return std::string(buffer);
6434 }
6435 }
6436
6437 // Return the string value to store in TAG_CPU_name.
6438
6439 template<bool big_endian>
6440 std::string
6441 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
6442 {
6443 static const char *name_table[] = {
6444 // These aren't real CPU names, but we can't guess
6445 // that from the architecture version alone.
6446 "Pre v4",
6447 "ARM v4",
6448 "ARM v4T",
6449 "ARM v5T",
6450 "ARM v5TE",
6451 "ARM v5TEJ",
6452 "ARM v6",
6453 "ARM v6KZ",
6454 "ARM v6T2",
6455 "ARM v6K",
6456 "ARM v7",
6457 "ARM v6-M",
6458 "ARM v6S-M",
6459 "ARM v7E-M"
6460 };
6461 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
6462
6463 if (value < name_table_size)
6464 return std::string(name_table[value]);
6465 else
6466 {
6467 char buffer[100];
6468 sprintf(buffer, "<unknown CPU value %u>", value);
6469 return std::string(buffer);
6470 }
6471 }
6472
6473 // Merge object attributes from input file called NAME with those of the
6474 // output. The input object attributes are in the object pointed by PASD.
6475
6476 template<bool big_endian>
6477 void
6478 Target_arm<big_endian>::merge_object_attributes(
6479 const char* name,
6480 const Attributes_section_data* pasd)
6481 {
6482 // Return if there is no attributes section data.
6483 if (pasd == NULL)
6484 return;
6485
6486 // If output has no object attributes, just copy.
6487 if (this->attributes_section_data_ == NULL)
6488 {
6489 this->attributes_section_data_ = new Attributes_section_data(*pasd);
6490 return;
6491 }
6492
6493 const int vendor = Object_attribute::OBJ_ATTR_PROC;
6494 const Object_attribute* in_attr = pasd->known_attributes(vendor);
6495 Object_attribute* out_attr =
6496 this->attributes_section_data_->known_attributes(vendor);
6497
6498 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6499 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
6500 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
6501 {
6502 // Ignore mismatches if the object doesn't use floating point. */
6503 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
6504 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
6505 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
6506 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
6507 gold_error(_("%s uses VFP register arguments, output does not"),
6508 name);
6509 }
6510
6511 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
6512 {
6513 // Merge this attribute with existing attributes.
6514 switch (i)
6515 {
6516 case elfcpp::Tag_CPU_raw_name:
6517 case elfcpp::Tag_CPU_name:
6518 // These are merged after Tag_CPU_arch.
6519 break;
6520
6521 case elfcpp::Tag_ABI_optimization_goals:
6522 case elfcpp::Tag_ABI_FP_optimization_goals:
6523 // Use the first value seen.
6524 break;
6525
6526 case elfcpp::Tag_CPU_arch:
6527 {
6528 unsigned int saved_out_attr = out_attr->int_value();
6529 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6530 int secondary_compat =
6531 this->get_secondary_compatible_arch(pasd);
6532 int secondary_compat_out =
6533 this->get_secondary_compatible_arch(
6534 this->attributes_section_data_);
6535 out_attr[i].set_int_value(
6536 tag_cpu_arch_combine(name, out_attr[i].int_value(),
6537 &secondary_compat_out,
6538 in_attr[i].int_value(),
6539 secondary_compat));
6540 this->set_secondary_compatible_arch(this->attributes_section_data_,
6541 secondary_compat_out);
6542
6543 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6544 if (out_attr[i].int_value() == saved_out_attr)
6545 ; // Leave the names alone.
6546 else if (out_attr[i].int_value() == in_attr[i].int_value())
6547 {
6548 // The output architecture has been changed to match the
6549 // input architecture. Use the input names.
6550 out_attr[elfcpp::Tag_CPU_name].set_string_value(
6551 in_attr[elfcpp::Tag_CPU_name].string_value());
6552 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
6553 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
6554 }
6555 else
6556 {
6557 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
6558 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
6559 }
6560
6561 // If we still don't have a value for Tag_CPU_name,
6562 // make one up now. Tag_CPU_raw_name remains blank.
6563 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
6564 {
6565 const std::string cpu_name =
6566 this->tag_cpu_name_value(out_attr[i].int_value());
6567 // FIXME: If we see an unknown CPU, this will be set
6568 // to "<unknown CPU n>", where n is the attribute value.
6569 // This is different from BFD, which leaves the name alone.
6570 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
6571 }
6572 }
6573 break;
6574
6575 case elfcpp::Tag_ARM_ISA_use:
6576 case elfcpp::Tag_THUMB_ISA_use:
6577 case elfcpp::Tag_WMMX_arch:
6578 case elfcpp::Tag_Advanced_SIMD_arch:
6579 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6580 case elfcpp::Tag_ABI_FP_rounding:
6581 case elfcpp::Tag_ABI_FP_exceptions:
6582 case elfcpp::Tag_ABI_FP_user_exceptions:
6583 case elfcpp::Tag_ABI_FP_number_model:
6584 case elfcpp::Tag_VFP_HP_extension:
6585 case elfcpp::Tag_CPU_unaligned_access:
6586 case elfcpp::Tag_T2EE_use:
6587 case elfcpp::Tag_Virtualization_use:
6588 case elfcpp::Tag_MPextension_use:
6589 // Use the largest value specified.
6590 if (in_attr[i].int_value() > out_attr[i].int_value())
6591 out_attr[i].set_int_value(in_attr[i].int_value());
6592 break;
6593
6594 case elfcpp::Tag_ABI_align8_preserved:
6595 case elfcpp::Tag_ABI_PCS_RO_data:
6596 // Use the smallest value specified.
6597 if (in_attr[i].int_value() < out_attr[i].int_value())
6598 out_attr[i].set_int_value(in_attr[i].int_value());
6599 break;
6600
6601 case elfcpp::Tag_ABI_align8_needed:
6602 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
6603 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
6604 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
6605 == 0)))
6606 {
6607 // This error message should be enabled once all non-conformant
6608 // binaries in the toolchain have had the attributes set
6609 // properly.
6610 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6611 // name);
6612 }
6613 // Fall through.
6614 case elfcpp::Tag_ABI_FP_denormal:
6615 case elfcpp::Tag_ABI_PCS_GOT_use:
6616 {
6617 // These tags have 0 = don't care, 1 = strong requirement,
6618 // 2 = weak requirement.
6619 static const int order_021[3] = {0, 2, 1};
6620
6621 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6622 // value if greater than 2 (for future-proofing).
6623 if ((in_attr[i].int_value() > 2
6624 && in_attr[i].int_value() > out_attr[i].int_value())
6625 || (in_attr[i].int_value() <= 2
6626 && out_attr[i].int_value() <= 2
6627 && (order_021[in_attr[i].int_value()]
6628 > order_021[out_attr[i].int_value()])))
6629 out_attr[i].set_int_value(in_attr[i].int_value());
6630 }
6631 break;
6632
6633 case elfcpp::Tag_CPU_arch_profile:
6634 if (out_attr[i].int_value() != in_attr[i].int_value())
6635 {
6636 // 0 will merge with anything.
6637 // 'A' and 'S' merge to 'A'.
6638 // 'R' and 'S' merge to 'R'.
6639 // 'M' and 'A|R|S' is an error.
6640 if (out_attr[i].int_value() == 0
6641 || (out_attr[i].int_value() == 'S'
6642 && (in_attr[i].int_value() == 'A'
6643 || in_attr[i].int_value() == 'R')))
6644 out_attr[i].set_int_value(in_attr[i].int_value());
6645 else if (in_attr[i].int_value() == 0
6646 || (in_attr[i].int_value() == 'S'
6647 && (out_attr[i].int_value() == 'A'
6648 || out_attr[i].int_value() == 'R')))
6649 ; // Do nothing.
6650 else
6651 {
6652 gold_error
6653 (_("conflicting architecture profiles %c/%c"),
6654 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
6655 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
6656 }
6657 }
6658 break;
6659 case elfcpp::Tag_VFP_arch:
6660 {
6661 static const struct
6662 {
6663 int ver;
6664 int regs;
6665 } vfp_versions[7] =
6666 {
6667 {0, 0},
6668 {1, 16},
6669 {2, 16},
6670 {3, 32},
6671 {3, 16},
6672 {4, 32},
6673 {4, 16}
6674 };
6675
6676 // Values greater than 6 aren't defined, so just pick the
6677 // biggest.
6678 if (in_attr[i].int_value() > 6
6679 && in_attr[i].int_value() > out_attr[i].int_value())
6680 {
6681 *out_attr = *in_attr;
6682 break;
6683 }
6684 // The output uses the superset of input features
6685 // (ISA version) and registers.
6686 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
6687 vfp_versions[out_attr[i].int_value()].ver);
6688 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
6689 vfp_versions[out_attr[i].int_value()].regs);
6690 // This assumes all possible supersets are also a valid
6691 // options.
6692 int newval;
6693 for (newval = 6; newval > 0; newval--)
6694 {
6695 if (regs == vfp_versions[newval].regs
6696 && ver == vfp_versions[newval].ver)
6697 break;
6698 }
6699 out_attr[i].set_int_value(newval);
6700 }
6701 break;
6702 case elfcpp::Tag_PCS_config:
6703 if (out_attr[i].int_value() == 0)
6704 out_attr[i].set_int_value(in_attr[i].int_value());
6705 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6706 {
6707 // It's sometimes ok to mix different configs, so this is only
6708 // a warning.
6709 gold_warning(_("%s: conflicting platform configuration"), name);
6710 }
6711 break;
6712 case elfcpp::Tag_ABI_PCS_R9_use:
6713 if (in_attr[i].int_value() != out_attr[i].int_value()
6714 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
6715 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
6716 {
6717 gold_error(_("%s: conflicting use of R9"), name);
6718 }
6719 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
6720 out_attr[i].set_int_value(in_attr[i].int_value());
6721 break;
6722 case elfcpp::Tag_ABI_PCS_RW_data:
6723 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6724 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6725 != elfcpp::AEABI_R9_SB)
6726 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6727 != elfcpp::AEABI_R9_unused))
6728 {
6729 gold_error(_("%s: SB relative addressing conflicts with use "
6730 "of R9"),
6731 name);
6732 }
6733 // Use the smallest value specified.
6734 if (in_attr[i].int_value() < out_attr[i].int_value())
6735 out_attr[i].set_int_value(in_attr[i].int_value());
6736 break;
6737 case elfcpp::Tag_ABI_PCS_wchar_t:
6738 // FIXME: Make it possible to turn off this warning.
6739 if (out_attr[i].int_value()
6740 && in_attr[i].int_value()
6741 && out_attr[i].int_value() != in_attr[i].int_value())
6742 {
6743 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6744 "use %u-byte wchar_t; use of wchar_t values "
6745 "across objects may fail"),
6746 name, in_attr[i].int_value(),
6747 out_attr[i].int_value());
6748 }
6749 else if (in_attr[i].int_value() && !out_attr[i].int_value())
6750 out_attr[i].set_int_value(in_attr[i].int_value());
6751 break;
6752 case elfcpp::Tag_ABI_enum_size:
6753 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
6754 {
6755 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
6756 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
6757 {
6758 // The existing object is compatible with anything.
6759 // Use whatever requirements the new object has.
6760 out_attr[i].set_int_value(in_attr[i].int_value());
6761 }
6762 // FIXME: Make it possible to turn off this warning.
6763 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
6764 && out_attr[i].int_value() != in_attr[i].int_value())
6765 {
6766 unsigned int in_value = in_attr[i].int_value();
6767 unsigned int out_value = out_attr[i].int_value();
6768 gold_warning(_("%s uses %s enums yet the output is to use "
6769 "%s enums; use of enum values across objects "
6770 "may fail"),
6771 name,
6772 this->aeabi_enum_name(in_value).c_str(),
6773 this->aeabi_enum_name(out_value).c_str());
6774 }
6775 }
6776 break;
6777 case elfcpp::Tag_ABI_VFP_args:
6778 // Aready done.
6779 break;
6780 case elfcpp::Tag_ABI_WMMX_args:
6781 if (in_attr[i].int_value() != out_attr[i].int_value())
6782 {
6783 gold_error(_("%s uses iWMMXt register arguments, output does "
6784 "not"),
6785 name);
6786 }
6787 break;
6788 case Object_attribute::Tag_compatibility:
6789 // Merged in target-independent code.
6790 break;
6791 case elfcpp::Tag_ABI_HardFP_use:
6792 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6793 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
6794 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
6795 out_attr[i].set_int_value(3);
6796 else if (in_attr[i].int_value() > out_attr[i].int_value())
6797 out_attr[i].set_int_value(in_attr[i].int_value());
6798 break;
6799 case elfcpp::Tag_ABI_FP_16bit_format:
6800 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6801 {
6802 if (in_attr[i].int_value() != out_attr[i].int_value())
6803 gold_error(_("fp16 format mismatch between %s and output"),
6804 name);
6805 }
6806 if (in_attr[i].int_value() != 0)
6807 out_attr[i].set_int_value(in_attr[i].int_value());
6808 break;
6809
6810 case elfcpp::Tag_nodefaults:
6811 // This tag is set if it exists, but the value is unused (and is
6812 // typically zero). We don't actually need to do anything here -
6813 // the merge happens automatically when the type flags are merged
6814 // below.
6815 break;
6816 case elfcpp::Tag_also_compatible_with:
6817 // Already done in Tag_CPU_arch.
6818 break;
6819 case elfcpp::Tag_conformance:
6820 // Keep the attribute if it matches. Throw it away otherwise.
6821 // No attribute means no claim to conform.
6822 if (in_attr[i].string_value() != out_attr[i].string_value())
6823 out_attr[i].set_string_value("");
6824 break;
6825
6826 default:
6827 {
6828 const char* err_object = NULL;
6829
6830 // The "known_obj_attributes" table does contain some undefined
6831 // attributes. Ensure that there are unused.
6832 if (out_attr[i].int_value() != 0
6833 || out_attr[i].string_value() != "")
6834 err_object = "output";
6835 else if (in_attr[i].int_value() != 0
6836 || in_attr[i].string_value() != "")
6837 err_object = name;
6838
6839 if (err_object != NULL)
6840 {
6841 // Attribute numbers >=64 (mod 128) can be safely ignored.
6842 if ((i & 127) < 64)
6843 gold_error(_("%s: unknown mandatory EABI object attribute "
6844 "%d"),
6845 err_object, i);
6846 else
6847 gold_warning(_("%s: unknown EABI object attribute %d"),
6848 err_object, i);
6849 }
6850
6851 // Only pass on attributes that match in both inputs.
6852 if (!in_attr[i].matches(out_attr[i]))
6853 {
6854 out_attr[i].set_int_value(0);
6855 out_attr[i].set_string_value("");
6856 }
6857 }
6858 }
6859
6860 // If out_attr was copied from in_attr then it won't have a type yet.
6861 if (in_attr[i].type() && !out_attr[i].type())
6862 out_attr[i].set_type(in_attr[i].type());
6863 }
6864
6865 // Merge Tag_compatibility attributes and any common GNU ones.
6866 this->attributes_section_data_->merge(name, pasd);
6867
6868 // Check for any attributes not known on ARM.
6869 typedef Vendor_object_attributes::Other_attributes Other_attributes;
6870 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
6871 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
6872 Other_attributes* out_other_attributes =
6873 this->attributes_section_data_->other_attributes(vendor);
6874 Other_attributes::iterator out_iter = out_other_attributes->begin();
6875
6876 while (in_iter != in_other_attributes->end()
6877 || out_iter != out_other_attributes->end())
6878 {
6879 const char* err_object = NULL;
6880 int err_tag = 0;
6881
6882 // The tags for each list are in numerical order.
6883 // If the tags are equal, then merge.
6884 if (out_iter != out_other_attributes->end()
6885 && (in_iter == in_other_attributes->end()
6886 || in_iter->first > out_iter->first))
6887 {
6888 // This attribute only exists in output. We can't merge, and we
6889 // don't know what the tag means, so delete it.
6890 err_object = "output";
6891 err_tag = out_iter->first;
6892 int saved_tag = out_iter->first;
6893 delete out_iter->second;
6894 out_other_attributes->erase(out_iter);
6895 out_iter = out_other_attributes->upper_bound(saved_tag);
6896 }
6897 else if (in_iter != in_other_attributes->end()
6898 && (out_iter != out_other_attributes->end()
6899 || in_iter->first < out_iter->first))
6900 {
6901 // This attribute only exists in input. We can't merge, and we
6902 // don't know what the tag means, so ignore it.
6903 err_object = name;
6904 err_tag = in_iter->first;
6905 ++in_iter;
6906 }
6907 else // The tags are equal.
6908 {
6909 // As present, all attributes in the list are unknown, and
6910 // therefore can't be merged meaningfully.
6911 err_object = "output";
6912 err_tag = out_iter->first;
6913
6914 // Only pass on attributes that match in both inputs.
6915 if (!in_iter->second->matches(*(out_iter->second)))
6916 {
6917 // No match. Delete the attribute.
6918 int saved_tag = out_iter->first;
6919 delete out_iter->second;
6920 out_other_attributes->erase(out_iter);
6921 out_iter = out_other_attributes->upper_bound(saved_tag);
6922 }
6923 else
6924 {
6925 // Matched. Keep the attribute and move to the next.
6926 ++out_iter;
6927 ++in_iter;
6928 }
6929 }
6930
6931 if (err_object)
6932 {
6933 // Attribute numbers >=64 (mod 128) can be safely ignored. */
6934 if ((err_tag & 127) < 64)
6935 {
6936 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
6937 err_object, err_tag);
6938 }
6939 else
6940 {
6941 gold_warning(_("%s: unknown EABI object attribute %d"),
6942 err_object, err_tag);
6943 }
6944 }
6945 }
6946 }
6947
6948 // Return whether a relocation type used the LSB to distinguish THUMB
6949 // addresses.
6950 template<bool big_endian>
6951 bool
6952 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
6953 {
6954 switch (r_type)
6955 {
6956 case elfcpp::R_ARM_PC24:
6957 case elfcpp::R_ARM_ABS32:
6958 case elfcpp::R_ARM_REL32:
6959 case elfcpp::R_ARM_SBREL32:
6960 case elfcpp::R_ARM_THM_CALL:
6961 case elfcpp::R_ARM_GLOB_DAT:
6962 case elfcpp::R_ARM_JUMP_SLOT:
6963 case elfcpp::R_ARM_GOTOFF32:
6964 case elfcpp::R_ARM_PLT32:
6965 case elfcpp::R_ARM_CALL:
6966 case elfcpp::R_ARM_JUMP24:
6967 case elfcpp::R_ARM_THM_JUMP24:
6968 case elfcpp::R_ARM_SBREL31:
6969 case elfcpp::R_ARM_PREL31:
6970 case elfcpp::R_ARM_MOVW_ABS_NC:
6971 case elfcpp::R_ARM_MOVW_PREL_NC:
6972 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6973 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6974 case elfcpp::R_ARM_THM_JUMP19:
6975 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
6976 case elfcpp::R_ARM_ALU_PC_G0_NC:
6977 case elfcpp::R_ARM_ALU_PC_G0:
6978 case elfcpp::R_ARM_ALU_PC_G1_NC:
6979 case elfcpp::R_ARM_ALU_PC_G1:
6980 case elfcpp::R_ARM_ALU_PC_G2:
6981 case elfcpp::R_ARM_ALU_SB_G0_NC:
6982 case elfcpp::R_ARM_ALU_SB_G0:
6983 case elfcpp::R_ARM_ALU_SB_G1_NC:
6984 case elfcpp::R_ARM_ALU_SB_G1:
6985 case elfcpp::R_ARM_ALU_SB_G2:
6986 case elfcpp::R_ARM_MOVW_BREL_NC:
6987 case elfcpp::R_ARM_MOVW_BREL:
6988 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
6989 case elfcpp::R_ARM_THM_MOVW_BREL:
6990 return true;
6991 default:
6992 return false;
6993 }
6994 }
6995
6996 // Stub-generation methods for Target_arm.
6997
6998 // Make a new Arm_input_section object.
6999
7000 template<bool big_endian>
7001 Arm_input_section<big_endian>*
7002 Target_arm<big_endian>::new_arm_input_section(
7003 Relobj* relobj,
7004 unsigned int shndx)
7005 {
7006 Input_section_specifier iss(relobj, shndx);
7007
7008 Arm_input_section<big_endian>* arm_input_section =
7009 new Arm_input_section<big_endian>(relobj, shndx);
7010 arm_input_section->init();
7011
7012 // Register new Arm_input_section in map for look-up.
7013 std::pair<typename Arm_input_section_map::iterator, bool> ins =
7014 this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
7015
7016 // Make sure that it we have not created another Arm_input_section
7017 // for this input section already.
7018 gold_assert(ins.second);
7019
7020 return arm_input_section;
7021 }
7022
7023 // Find the Arm_input_section object corresponding to the SHNDX-th input
7024 // section of RELOBJ.
7025
7026 template<bool big_endian>
7027 Arm_input_section<big_endian>*
7028 Target_arm<big_endian>::find_arm_input_section(
7029 Relobj* relobj,
7030 unsigned int shndx) const
7031 {
7032 Input_section_specifier iss(relobj, shndx);
7033 typename Arm_input_section_map::const_iterator p =
7034 this->arm_input_section_map_.find(iss);
7035 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
7036 }
7037
7038 // Make a new stub table.
7039
7040 template<bool big_endian>
7041 Stub_table<big_endian>*
7042 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
7043 {
7044 Stub_table<big_endian>* stub_table =
7045 new Stub_table<big_endian>(owner);
7046 this->stub_tables_.push_back(stub_table);
7047
7048 stub_table->set_address(owner->address() + owner->data_size());
7049 stub_table->set_file_offset(owner->offset() + owner->data_size());
7050 stub_table->finalize_data_size();
7051
7052 return stub_table;
7053 }
7054
7055 // Scan a relocation for stub generation.
7056
7057 template<bool big_endian>
7058 void
7059 Target_arm<big_endian>::scan_reloc_for_stub(
7060 const Relocate_info<32, big_endian>* relinfo,
7061 unsigned int r_type,
7062 const Sized_symbol<32>* gsym,
7063 unsigned int r_sym,
7064 const Symbol_value<32>* psymval,
7065 elfcpp::Elf_types<32>::Elf_Swxword addend,
7066 Arm_address address)
7067 {
7068 typedef typename Target_arm<big_endian>::Relocate Relocate;
7069
7070 const Arm_relobj<big_endian>* arm_relobj =
7071 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
7072
7073 bool target_is_thumb;
7074 Symbol_value<32> symval;
7075 if (gsym != NULL)
7076 {
7077 // This is a global symbol. Determine if we use PLT and if the
7078 // final target is THUMB.
7079 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
7080 {
7081 // This uses a PLT, change the symbol value.
7082 symval.set_output_value(this->plt_section()->address()
7083 + gsym->plt_offset());
7084 psymval = &symval;
7085 target_is_thumb = false;
7086 }
7087 else if (gsym->is_undefined())
7088 // There is no need to generate a stub symbol is undefined.
7089 return;
7090 else
7091 {
7092 target_is_thumb =
7093 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
7094 || (gsym->type() == elfcpp::STT_FUNC
7095 && !gsym->is_undefined()
7096 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
7097 }
7098 }
7099 else
7100 {
7101 // This is a local symbol. Determine if the final target is THUMB.
7102 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
7103 }
7104
7105 // Strip LSB if this points to a THUMB target.
7106 if (target_is_thumb
7107 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
7108 && ((psymval->value(arm_relobj, 0) & 1) != 0))
7109 {
7110 Arm_address stripped_value =
7111 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
7112 symval.set_output_value(stripped_value);
7113 psymval = &symval;
7114 }
7115
7116 // Get the symbol value.
7117 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
7118
7119 // Owing to pipelining, the PC relative branches below actually skip
7120 // two instructions when the branch offset is 0.
7121 Arm_address destination;
7122 switch (r_type)
7123 {
7124 case elfcpp::R_ARM_CALL:
7125 case elfcpp::R_ARM_JUMP24:
7126 case elfcpp::R_ARM_PLT32:
7127 // ARM branches.
7128 destination = value + addend + 8;
7129 break;
7130 case elfcpp::R_ARM_THM_CALL:
7131 case elfcpp::R_ARM_THM_XPC22:
7132 case elfcpp::R_ARM_THM_JUMP24:
7133 case elfcpp::R_ARM_THM_JUMP19:
7134 // THUMB branches.
7135 destination = value + addend + 4;
7136 break;
7137 default:
7138 gold_unreachable();
7139 }
7140
7141 Reloc_stub* stub = NULL;
7142 Stub_type stub_type =
7143 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
7144 target_is_thumb);
7145 if (stub_type != arm_stub_none)
7146 {
7147 // Try looking up an existing stub from a stub table.
7148 Stub_table<big_endian>* stub_table =
7149 arm_relobj->stub_table(relinfo->data_shndx);
7150 gold_assert(stub_table != NULL);
7151
7152 // Locate stub by destination.
7153 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
7154
7155 // Create a stub if there is not one already
7156 stub = stub_table->find_reloc_stub(stub_key);
7157 if (stub == NULL)
7158 {
7159 // create a new stub and add it to stub table.
7160 stub = this->stub_factory().make_reloc_stub(stub_type);
7161 stub_table->add_reloc_stub(stub, stub_key);
7162 }
7163
7164 // Record the destination address.
7165 stub->set_destination_address(destination
7166 | (target_is_thumb ? 1 : 0));
7167 }
7168
7169 // For Cortex-A8, we need to record a relocation at 4K page boundary.
7170 if (this->fix_cortex_a8_
7171 && (r_type == elfcpp::R_ARM_THM_JUMP24
7172 || r_type == elfcpp::R_ARM_THM_JUMP19
7173 || r_type == elfcpp::R_ARM_THM_CALL
7174 || r_type == elfcpp::R_ARM_THM_XPC22)
7175 && (address & 0xfffU) == 0xffeU)
7176 {
7177 // Found a candidate. Note we haven't checked the destination is
7178 // within 4K here: if we do so (and don't create a record) we can't
7179 // tell that a branch should have been relocated when scanning later.
7180 this->cortex_a8_relocs_info_[address] =
7181 new Cortex_a8_reloc(stub, r_type,
7182 destination | (target_is_thumb ? 1 : 0));
7183 }
7184 }
7185
7186 // This function scans a relocation sections for stub generation.
7187 // The template parameter Relocate must be a class type which provides
7188 // a single function, relocate(), which implements the machine
7189 // specific part of a relocation.
7190
7191 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
7192 // SHT_REL or SHT_RELA.
7193
7194 // PRELOCS points to the relocation data. RELOC_COUNT is the number
7195 // of relocs. OUTPUT_SECTION is the output section.
7196 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
7197 // mapped to output offsets.
7198
7199 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
7200 // VIEW_SIZE is the size. These refer to the input section, unless
7201 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
7202 // the output section.
7203
7204 template<bool big_endian>
7205 template<int sh_type>
7206 void inline
7207 Target_arm<big_endian>::scan_reloc_section_for_stubs(
7208 const Relocate_info<32, big_endian>* relinfo,
7209 const unsigned char* prelocs,
7210 size_t reloc_count,
7211 Output_section* output_section,
7212 bool needs_special_offset_handling,
7213 const unsigned char* view,
7214 elfcpp::Elf_types<32>::Elf_Addr view_address,
7215 section_size_type)
7216 {
7217 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
7218 const int reloc_size =
7219 Reloc_types<sh_type, 32, big_endian>::reloc_size;
7220
7221 Arm_relobj<big_endian>* arm_object =
7222 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
7223 unsigned int local_count = arm_object->local_symbol_count();
7224
7225 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
7226
7227 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7228 {
7229 Reltype reloc(prelocs);
7230
7231 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
7232 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
7233 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
7234
7235 r_type = this->get_real_reloc_type(r_type);
7236
7237 // Only a few relocation types need stubs.
7238 if ((r_type != elfcpp::R_ARM_CALL)
7239 && (r_type != elfcpp::R_ARM_JUMP24)
7240 && (r_type != elfcpp::R_ARM_PLT32)
7241 && (r_type != elfcpp::R_ARM_THM_CALL)
7242 && (r_type != elfcpp::R_ARM_THM_XPC22)
7243 && (r_type != elfcpp::R_ARM_THM_JUMP24)
7244 && (r_type != elfcpp::R_ARM_THM_JUMP19))
7245 continue;
7246
7247 section_offset_type offset =
7248 convert_to_section_size_type(reloc.get_r_offset());
7249
7250 if (needs_special_offset_handling)
7251 {
7252 offset = output_section->output_offset(relinfo->object,
7253 relinfo->data_shndx,
7254 offset);
7255 if (offset == -1)
7256 continue;
7257 }
7258
7259 // Get the addend.
7260 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
7261 elfcpp::Elf_types<32>::Elf_Swxword addend =
7262 stub_addend_reader(r_type, view + offset, reloc);
7263
7264 const Sized_symbol<32>* sym;
7265
7266 Symbol_value<32> symval;
7267 const Symbol_value<32> *psymval;
7268 if (r_sym < local_count)
7269 {
7270 sym = NULL;
7271 psymval = arm_object->local_symbol(r_sym);
7272
7273 // If the local symbol belongs to a section we are discarding,
7274 // and that section is a debug section, try to find the
7275 // corresponding kept section and map this symbol to its
7276 // counterpart in the kept section. The symbol must not
7277 // correspond to a section we are folding.
7278 bool is_ordinary;
7279 unsigned int shndx = psymval->input_shndx(&is_ordinary);
7280 if (is_ordinary
7281 && shndx != elfcpp::SHN_UNDEF
7282 && !arm_object->is_section_included(shndx)
7283 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
7284 {
7285 if (comdat_behavior == CB_UNDETERMINED)
7286 {
7287 std::string name =
7288 arm_object->section_name(relinfo->data_shndx);
7289 comdat_behavior = get_comdat_behavior(name.c_str());
7290 }
7291 if (comdat_behavior == CB_PRETEND)
7292 {
7293 bool found;
7294 typename elfcpp::Elf_types<32>::Elf_Addr value =
7295 arm_object->map_to_kept_section(shndx, &found);
7296 if (found)
7297 symval.set_output_value(value + psymval->input_value());
7298 else
7299 symval.set_output_value(0);
7300 }
7301 else
7302 {
7303 symval.set_output_value(0);
7304 }
7305 symval.set_no_output_symtab_entry();
7306 psymval = &symval;
7307 }
7308 }
7309 else
7310 {
7311 const Symbol* gsym = arm_object->global_symbol(r_sym);
7312 gold_assert(gsym != NULL);
7313 if (gsym->is_forwarder())
7314 gsym = relinfo->symtab->resolve_forwards(gsym);
7315
7316 sym = static_cast<const Sized_symbol<32>*>(gsym);
7317 if (sym->has_symtab_index())
7318 symval.set_output_symtab_index(sym->symtab_index());
7319 else
7320 symval.set_no_output_symtab_entry();
7321
7322 // We need to compute the would-be final value of this global
7323 // symbol.
7324 const Symbol_table* symtab = relinfo->symtab;
7325 const Sized_symbol<32>* sized_symbol =
7326 symtab->get_sized_symbol<32>(gsym);
7327 Symbol_table::Compute_final_value_status status;
7328 Arm_address value =
7329 symtab->compute_final_value<32>(sized_symbol, &status);
7330
7331 // Skip this if the symbol has not output section.
7332 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
7333 continue;
7334
7335 symval.set_output_value(value);
7336 psymval = &symval;
7337 }
7338
7339 // If symbol is a section symbol, we don't know the actual type of
7340 // destination. Give up.
7341 if (psymval->is_section_symbol())
7342 continue;
7343
7344 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
7345 addend, view_address + offset);
7346 }
7347 }
7348
7349 // Scan an input section for stub generation.
7350
7351 template<bool big_endian>
7352 void
7353 Target_arm<big_endian>::scan_section_for_stubs(
7354 const Relocate_info<32, big_endian>* relinfo,
7355 unsigned int sh_type,
7356 const unsigned char* prelocs,
7357 size_t reloc_count,
7358 Output_section* output_section,
7359 bool needs_special_offset_handling,
7360 const unsigned char* view,
7361 Arm_address view_address,
7362 section_size_type view_size)
7363 {
7364 if (sh_type == elfcpp::SHT_REL)
7365 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
7366 relinfo,
7367 prelocs,
7368 reloc_count,
7369 output_section,
7370 needs_special_offset_handling,
7371 view,
7372 view_address,
7373 view_size);
7374 else if (sh_type == elfcpp::SHT_RELA)
7375 // We do not support RELA type relocations yet. This is provided for
7376 // completeness.
7377 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
7378 relinfo,
7379 prelocs,
7380 reloc_count,
7381 output_section,
7382 needs_special_offset_handling,
7383 view,
7384 view_address,
7385 view_size);
7386 else
7387 gold_unreachable();
7388 }
7389
7390 // Group input sections for stub generation.
7391 //
7392 // We goup input sections in an output sections so that the total size,
7393 // including any padding space due to alignment is smaller than GROUP_SIZE
7394 // unless the only input section in group is bigger than GROUP_SIZE already.
7395 // Then an ARM stub table is created to follow the last input section
7396 // in group. For each group an ARM stub table is created an is placed
7397 // after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
7398 // extend the group after the stub table.
7399
7400 template<bool big_endian>
7401 void
7402 Target_arm<big_endian>::group_sections(
7403 Layout* layout,
7404 section_size_type group_size,
7405 bool stubs_always_after_branch)
7406 {
7407 // Group input sections and insert stub table
7408 Layout::Section_list section_list;
7409 layout->get_allocated_sections(&section_list);
7410 for (Layout::Section_list::const_iterator p = section_list.begin();
7411 p != section_list.end();
7412 ++p)
7413 {
7414 Arm_output_section<big_endian>* output_section =
7415 Arm_output_section<big_endian>::as_arm_output_section(*p);
7416 output_section->group_sections(group_size, stubs_always_after_branch,
7417 this);
7418 }
7419 }
7420
7421 // Relaxation hook. This is where we do stub generation.
7422
7423 template<bool big_endian>
7424 bool
7425 Target_arm<big_endian>::do_relax(
7426 int pass,
7427 const Input_objects* input_objects,
7428 Symbol_table* symtab,
7429 Layout* layout)
7430 {
7431 // No need to generate stubs if this is a relocatable link.
7432 gold_assert(!parameters->options().relocatable());
7433
7434 // If this is the first pass, we need to group input sections into
7435 // stub groups.
7436 if (pass == 1)
7437 {
7438 // Determine the stub group size. The group size is the absolute
7439 // value of the parameter --stub-group-size. If --stub-group-size
7440 // is passed a negative value, we restict stubs to be always after
7441 // the stubbed branches.
7442 int32_t stub_group_size_param =
7443 parameters->options().stub_group_size();
7444 bool stubs_always_after_branch = stub_group_size_param < 0;
7445 section_size_type stub_group_size = abs(stub_group_size_param);
7446
7447 if (stub_group_size == 1)
7448 {
7449 // Default value.
7450 // Thumb branch range is +-4MB has to be used as the default
7451 // maximum size (a given section can contain both ARM and Thumb
7452 // code, so the worst case has to be taken into account).
7453 //
7454 // This value is 24K less than that, which allows for 2025
7455 // 12-byte stubs. If we exceed that, then we will fail to link.
7456 // The user will have to relink with an explicit group size
7457 // option.
7458 stub_group_size = 4170000;
7459 }
7460
7461 group_sections(layout, stub_group_size, stubs_always_after_branch);
7462 }
7463
7464 typedef typename Stub_table_list::iterator Stub_table_iterator;
7465 if (this->fix_cortex_a8_)
7466 {
7467 // Clear all Cortex-A8 reloc information.
7468 for (typename Cortex_a8_relocs_info::const_iterator p =
7469 this->cortex_a8_relocs_info_.begin();
7470 p != this->cortex_a8_relocs_info_.end();
7471 ++p)
7472 delete p->second;
7473 this->cortex_a8_relocs_info_.clear();
7474 }
7475
7476 // scan relocs for stubs
7477 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
7478 op != input_objects->relobj_end();
7479 ++op)
7480 {
7481 Arm_relobj<big_endian>* arm_relobj =
7482 Arm_relobj<big_endian>::as_arm_relobj(*op);
7483 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
7484 }
7485
7486 // Check all stub tables to see if any of them have their data sizes
7487 // or addresses alignments changed. These are the only things that
7488 // matter.
7489 bool any_stub_table_changed = false;
7490 for (Stub_table_iterator sp = this->stub_tables_.begin();
7491 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7492 ++sp)
7493 {
7494 if ((*sp)->update_data_size_and_addralign())
7495 any_stub_table_changed = true;
7496 }
7497
7498 // Finalize the stubs in the last relaxation pass.
7499 if (!any_stub_table_changed)
7500 for (Stub_table_iterator sp = this->stub_tables_.begin();
7501 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7502 ++sp)
7503 (*sp)->finalize_stubs();
7504
7505 return any_stub_table_changed;
7506 }
7507
7508 // Relocate a stub.
7509
7510 template<bool big_endian>
7511 void
7512 Target_arm<big_endian>::relocate_stub(
7513 Stub* stub,
7514 const Relocate_info<32, big_endian>* relinfo,
7515 Output_section* output_section,
7516 unsigned char* view,
7517 Arm_address address,
7518 section_size_type view_size)
7519 {
7520 Relocate relocate;
7521 const Stub_template* stub_template = stub->stub_template();
7522 for (size_t i = 0; i < stub_template->reloc_count(); i++)
7523 {
7524 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
7525 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
7526
7527 unsigned int r_type = insn->r_type();
7528 section_size_type reloc_offset = stub_template->reloc_offset(i);
7529 section_size_type reloc_size = insn->size();
7530 gold_assert(reloc_offset + reloc_size <= view_size);
7531
7532 // This is the address of the stub destination.
7533 Arm_address target = stub->reloc_target(i);
7534 Symbol_value<32> symval;
7535 symval.set_output_value(target);
7536
7537 // Synthesize a fake reloc just in case. We don't have a symbol so
7538 // we use 0.
7539 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
7540 memset(reloc_buffer, 0, sizeof(reloc_buffer));
7541 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
7542 reloc_write.put_r_offset(reloc_offset);
7543 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
7544 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
7545
7546 relocate.relocate(relinfo, this, output_section,
7547 this->fake_relnum_for_stubs, rel, r_type,
7548 NULL, &symval, view + reloc_offset,
7549 address + reloc_offset, reloc_size);
7550 }
7551 }
7552
7553 // Determine whether an object attribute tag takes an integer, a
7554 // string or both.
7555
7556 template<bool big_endian>
7557 int
7558 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
7559 {
7560 if (tag == Object_attribute::Tag_compatibility)
7561 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7562 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
7563 else if (tag == elfcpp::Tag_nodefaults)
7564 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7565 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
7566 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
7567 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
7568 else if (tag < 32)
7569 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
7570 else
7571 return ((tag & 1) != 0
7572 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7573 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
7574 }
7575
7576 // Reorder attributes.
7577 //
7578 // The ABI defines that Tag_conformance should be emitted first, and that
7579 // Tag_nodefaults should be second (if either is defined). This sets those
7580 // two positions, and bumps up the position of all the remaining tags to
7581 // compensate.
7582
7583 template<bool big_endian>
7584 int
7585 Target_arm<big_endian>::do_attributes_order(int num) const
7586 {
7587 // Reorder the known object attributes in output. We want to move
7588 // Tag_conformance to position 4 and Tag_conformance to position 5
7589 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7590 if (num == 4)
7591 return elfcpp::Tag_conformance;
7592 if (num == 5)
7593 return elfcpp::Tag_nodefaults;
7594 if ((num - 2) < elfcpp::Tag_nodefaults)
7595 return num - 2;
7596 if ((num - 1) < elfcpp::Tag_conformance)
7597 return num - 1;
7598 return num;
7599 }
7600
7601 template<bool big_endian>
7602 class Target_selector_arm : public Target_selector
7603 {
7604 public:
7605 Target_selector_arm()
7606 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
7607 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
7608 { }
7609
7610 Target*
7611 do_instantiate_target()
7612 { return new Target_arm<big_endian>(); }
7613 };
7614
7615 Target_selector_arm<false> target_selector_arm;
7616 Target_selector_arm<true> target_selector_armbe;
7617
7618 } // End anonymous namespace.