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