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