2010-01-22 Doug Kwan <dougkwan@google.com>
[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),
a2162063 1815 cortex_a8_relocs_info_(), fix_v4bx_(0)
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)
2052 int
2053 fix_v4bx() const
2054 { return this->fix_v4bx_; }
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_;
a2162063
ILT
2418 // Whether we need to fix code for V4BX relocations.
2419 int fix_v4bx_;
4a657b0d
DK
2420};
2421
2422template<bool big_endian>
2423const Target::Target_info Target_arm<big_endian>::arm_info =
2424{
2425 32, // size
2426 big_endian, // is_big_endian
2427 elfcpp::EM_ARM, // machine_code
2428 false, // has_make_symbol
2429 false, // has_resolve
2430 false, // has_code_fill
2431 true, // is_default_stack_executable
2432 '\0', // wrap_char
2433 "/usr/lib/libc.so.1", // dynamic_linker
2434 0x8000, // default_text_segment_address
2435 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
2436 0x1000, // common_pagesize (overridable by -z common-page-size)
2437 elfcpp::SHN_UNDEF, // small_common_shndx
2438 elfcpp::SHN_UNDEF, // large_common_shndx
2439 0, // small_common_section_flags
05a352e6
DK
2440 0, // large_common_section_flags
2441 ".ARM.attributes", // attributes_section
2442 "aeabi" // attributes_vendor
4a657b0d
DK
2443};
2444
c121c671
DK
2445// Arm relocate functions class
2446//
2447
2448template<bool big_endian>
2449class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2450{
2451 public:
2452 typedef enum
2453 {
2454 STATUS_OKAY, // No error during relocation.
2455 STATUS_OVERFLOW, // Relocation oveflow.
2456 STATUS_BAD_RELOC // Relocation cannot be applied.
2457 } Status;
2458
2459 private:
2460 typedef Relocate_functions<32, big_endian> Base;
2461 typedef Arm_relocate_functions<big_endian> This;
2462
fd3c5f0b
ILT
2463 // Encoding of imm16 argument for movt and movw ARM instructions
2464 // from ARM ARM:
2465 //
2466 // imm16 := imm4 | imm12
2467 //
2468 // 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
2469 // +-------+---------------+-------+-------+-----------------------+
2470 // | | |imm4 | |imm12 |
2471 // +-------+---------------+-------+-------+-----------------------+
2472
2473 // Extract the relocation addend from VAL based on the ARM
2474 // instruction encoding described above.
2475 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2476 extract_arm_movw_movt_addend(
2477 typename elfcpp::Swap<32, big_endian>::Valtype val)
2478 {
2479 // According to the Elf ABI for ARM Architecture the immediate
2480 // field is sign-extended to form the addend.
2481 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2482 }
2483
2484 // Insert X into VAL based on the ARM instruction encoding described
2485 // above.
2486 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2487 insert_val_arm_movw_movt(
2488 typename elfcpp::Swap<32, big_endian>::Valtype val,
2489 typename elfcpp::Swap<32, big_endian>::Valtype x)
2490 {
2491 val &= 0xfff0f000;
2492 val |= x & 0x0fff;
2493 val |= (x & 0xf000) << 4;
2494 return val;
2495 }
2496
2497 // Encoding of imm16 argument for movt and movw Thumb2 instructions
2498 // from ARM ARM:
2499 //
2500 // imm16 := imm4 | i | imm3 | imm8
2501 //
2502 // 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
2503 // +---------+-+-----------+-------++-+-----+-------+---------------+
2504 // | |i| |imm4 || |imm3 | |imm8 |
2505 // +---------+-+-----------+-------++-+-----+-------+---------------+
2506
2507 // Extract the relocation addend from VAL based on the Thumb2
2508 // instruction encoding described above.
2509 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2510 extract_thumb_movw_movt_addend(
2511 typename elfcpp::Swap<32, big_endian>::Valtype val)
2512 {
2513 // According to the Elf ABI for ARM Architecture the immediate
2514 // field is sign-extended to form the addend.
2515 return utils::sign_extend<16>(((val >> 4) & 0xf000)
2516 | ((val >> 15) & 0x0800)
2517 | ((val >> 4) & 0x0700)
2518 | (val & 0x00ff));
2519 }
2520
2521 // Insert X into VAL based on the Thumb2 instruction encoding
2522 // described above.
2523 static inline typename elfcpp::Swap<32, big_endian>::Valtype
2524 insert_val_thumb_movw_movt(
2525 typename elfcpp::Swap<32, big_endian>::Valtype val,
2526 typename elfcpp::Swap<32, big_endian>::Valtype x)
2527 {
2528 val &= 0xfbf08f00;
2529 val |= (x & 0xf000) << 4;
2530 val |= (x & 0x0800) << 15;
2531 val |= (x & 0x0700) << 4;
2532 val |= (x & 0x00ff);
2533 return val;
2534 }
2535
d204b6e9
DK
2536 // Handle ARM long branches.
2537 static typename This::Status
2538 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2539 unsigned char *, const Sized_symbol<32>*,
2540 const Arm_relobj<big_endian>*, unsigned int,
2541 const Symbol_value<32>*, Arm_address, Arm_address, bool);
c121c671 2542
51938283
DK
2543 // Handle THUMB long branches.
2544 static typename This::Status
2545 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
2546 unsigned char *, const Sized_symbol<32>*,
2547 const Arm_relobj<big_endian>*, unsigned int,
2548 const Symbol_value<32>*, Arm_address, Arm_address, bool);
2549
c121c671 2550 public:
5e445df6 2551
089d69dc
DK
2552 // Return the branch offset of a 32-bit THUMB branch.
2553 static inline int32_t
2554 thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2555 {
2556 // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
2557 // involving the J1 and J2 bits.
2558 uint32_t s = (upper_insn & (1U << 10)) >> 10;
2559 uint32_t upper = upper_insn & 0x3ffU;
2560 uint32_t lower = lower_insn & 0x7ffU;
2561 uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
2562 uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
2563 uint32_t i1 = j1 ^ s ? 0 : 1;
2564 uint32_t i2 = j2 ^ s ? 0 : 1;
2565
2566 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
2567 | (upper << 12) | (lower << 1));
2568 }
2569
2570 // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
2571 // UPPER_INSN is the original upper instruction of the branch. Caller is
2572 // responsible for overflow checking and BLX offset adjustment.
2573 static inline uint16_t
2574 thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
2575 {
2576 uint32_t s = offset < 0 ? 1 : 0;
2577 uint32_t bits = static_cast<uint32_t>(offset);
2578 return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
2579 }
2580
2581 // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
2582 // LOWER_INSN is the original lower instruction of the branch. Caller is
2583 // responsible for overflow checking and BLX offset adjustment.
2584 static inline uint16_t
2585 thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
2586 {
2587 uint32_t s = offset < 0 ? 1 : 0;
2588 uint32_t bits = static_cast<uint32_t>(offset);
2589 return ((lower_insn & ~0x2fffU)
2590 | ((((bits >> 23) & 1) ^ !s) << 13)
2591 | ((((bits >> 22) & 1) ^ !s) << 11)
2592 | ((bits >> 1) & 0x7ffU));
2593 }
2594
2595 // Return the branch offset of a 32-bit THUMB conditional branch.
2596 static inline int32_t
2597 thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
2598 {
2599 uint32_t s = (upper_insn & 0x0400U) >> 10;
2600 uint32_t j1 = (lower_insn & 0x2000U) >> 13;
2601 uint32_t j2 = (lower_insn & 0x0800U) >> 11;
2602 uint32_t lower = (lower_insn & 0x07ffU);
2603 uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
2604
2605 return utils::sign_extend<21>((upper << 12) | (lower << 1));
2606 }
2607
2608 // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
2609 // instruction. UPPER_INSN is the original upper instruction of the branch.
2610 // Caller is responsible for overflow checking.
2611 static inline uint16_t
2612 thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
2613 {
2614 uint32_t s = offset < 0 ? 1 : 0;
2615 uint32_t bits = static_cast<uint32_t>(offset);
2616 return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
2617 }
2618
2619 // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
2620 // instruction. LOWER_INSN is the original lower instruction of the branch.
2621 // Caller is reponsible for overflow checking.
2622 static inline uint16_t
2623 thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
2624 {
2625 uint32_t bits = static_cast<uint32_t>(offset);
2626 uint32_t j2 = (bits & 0x00080000U) >> 19;
2627 uint32_t j1 = (bits & 0x00040000U) >> 18;
2628 uint32_t lo = (bits & 0x00000ffeU) >> 1;
2629
2630 return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
2631 }
2632
5e445df6
ILT
2633 // R_ARM_ABS8: S + A
2634 static inline typename This::Status
2635 abs8(unsigned char *view,
2636 const Sized_relobj<32, big_endian>* object,
be8fcb75 2637 const Symbol_value<32>* psymval)
5e445df6
ILT
2638 {
2639 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2640 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2641 Valtype* wv = reinterpret_cast<Valtype*>(view);
2642 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2643 Reltype addend = utils::sign_extend<8>(val);
2daedcd6 2644 Reltype x = psymval->value(object, addend);
5e445df6
ILT
2645 val = utils::bit_select(val, x, 0xffU);
2646 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2647 return (utils::has_signed_unsigned_overflow<8>(x)
2648 ? This::STATUS_OVERFLOW
2649 : This::STATUS_OKAY);
2650 }
2651
be8fcb75
ILT
2652 // R_ARM_THM_ABS5: S + A
2653 static inline typename This::Status
2654 thm_abs5(unsigned char *view,
2655 const Sized_relobj<32, big_endian>* object,
2656 const Symbol_value<32>* psymval)
2657 {
2658 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2659 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2660 Valtype* wv = reinterpret_cast<Valtype*>(view);
2661 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2662 Reltype addend = (val & 0x7e0U) >> 6;
2daedcd6 2663 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
2664 val = utils::bit_select(val, x << 6, 0x7e0U);
2665 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2666 return (utils::has_overflow<5>(x)
2667 ? This::STATUS_OVERFLOW
2668 : This::STATUS_OKAY);
2669 }
2670
2671 // R_ARM_ABS12: S + A
2672 static inline typename This::Status
2673 abs12(unsigned char *view,
51938283
DK
2674 const Sized_relobj<32, big_endian>* object,
2675 const Symbol_value<32>* psymval)
be8fcb75
ILT
2676 {
2677 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2678 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2679 Valtype* wv = reinterpret_cast<Valtype*>(view);
2680 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2681 Reltype addend = val & 0x0fffU;
2daedcd6 2682 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
2683 val = utils::bit_select(val, x, 0x0fffU);
2684 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2685 return (utils::has_overflow<12>(x)
2686 ? This::STATUS_OVERFLOW
2687 : This::STATUS_OKAY);
2688 }
2689
2690 // R_ARM_ABS16: S + A
2691 static inline typename This::Status
2692 abs16(unsigned char *view,
51938283
DK
2693 const Sized_relobj<32, big_endian>* object,
2694 const Symbol_value<32>* psymval)
be8fcb75
ILT
2695 {
2696 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2697 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2698 Valtype* wv = reinterpret_cast<Valtype*>(view);
2699 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2700 Reltype addend = utils::sign_extend<16>(val);
2daedcd6 2701 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
2702 val = utils::bit_select(val, x, 0xffffU);
2703 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2704 return (utils::has_signed_unsigned_overflow<16>(x)
2705 ? This::STATUS_OVERFLOW
2706 : This::STATUS_OKAY);
2707 }
2708
c121c671
DK
2709 // R_ARM_ABS32: (S + A) | T
2710 static inline typename This::Status
2711 abs32(unsigned char *view,
2712 const Sized_relobj<32, big_endian>* object,
2713 const Symbol_value<32>* psymval,
2daedcd6 2714 Arm_address thumb_bit)
c121c671
DK
2715 {
2716 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2717 Valtype* wv = reinterpret_cast<Valtype*>(view);
2718 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2daedcd6 2719 Valtype x = psymval->value(object, addend) | thumb_bit;
c121c671
DK
2720 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2721 return This::STATUS_OKAY;
2722 }
2723
2724 // R_ARM_REL32: (S + A) | T - P
2725 static inline typename This::Status
2726 rel32(unsigned char *view,
2727 const Sized_relobj<32, big_endian>* object,
2728 const Symbol_value<32>* psymval,
ebabffbd 2729 Arm_address address,
2daedcd6 2730 Arm_address thumb_bit)
c121c671
DK
2731 {
2732 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2733 Valtype* wv = reinterpret_cast<Valtype*>(view);
2734 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2daedcd6 2735 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c121c671
DK
2736 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2737 return This::STATUS_OKAY;
2738 }
2739
2740 // R_ARM_THM_CALL: (S + A) | T - P
2741 static inline typename This::Status
51938283
DK
2742 thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2743 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2744 unsigned int r_sym, const Symbol_value<32>* psymval,
2745 Arm_address address, Arm_address thumb_bit,
2746 bool is_weakly_undefined_without_plt)
c121c671 2747 {
51938283
DK
2748 return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
2749 object, r_sym, psymval, address, thumb_bit,
2750 is_weakly_undefined_without_plt);
2751 }
c121c671 2752
51938283
DK
2753 // R_ARM_THM_JUMP24: (S + A) | T - P
2754 static inline typename This::Status
2755 thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2756 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2757 unsigned int r_sym, const Symbol_value<32>* psymval,
2758 Arm_address address, Arm_address thumb_bit,
2759 bool is_weakly_undefined_without_plt)
2760 {
2761 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
2762 object, r_sym, psymval, address, thumb_bit,
2763 is_weakly_undefined_without_plt);
2764 }
2765
089d69dc
DK
2766 // R_ARM_THM_JUMP24: (S + A) | T - P
2767 static typename This::Status
2768 thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
2769 const Symbol_value<32>* psymval, Arm_address address,
2770 Arm_address thumb_bit);
2771
51938283
DK
2772 // R_ARM_THM_XPC22: (S + A) | T - P
2773 static inline typename This::Status
2774 thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2775 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2776 unsigned int r_sym, const Symbol_value<32>* psymval,
2777 Arm_address address, Arm_address thumb_bit,
2778 bool is_weakly_undefined_without_plt)
2779 {
2780 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
2781 object, r_sym, psymval, address, thumb_bit,
2782 is_weakly_undefined_without_plt);
c121c671
DK
2783 }
2784
800d0f56
ILT
2785 // R_ARM_THM_JUMP6: S + A – P
2786 static inline typename This::Status
2787 thm_jump6(unsigned char *view,
2788 const Sized_relobj<32, big_endian>* object,
2789 const Symbol_value<32>* psymval,
2790 Arm_address address)
2791 {
2792 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2793 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2794 Valtype* wv = reinterpret_cast<Valtype*>(view);
2795 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2796 // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
2797 Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
2798 Reltype x = (psymval->value(object, addend) - address);
2799 val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
2800 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2801 // CZB does only forward jumps.
2802 return ((x > 0x007e)
2803 ? This::STATUS_OVERFLOW
2804 : This::STATUS_OKAY);
2805 }
2806
2807 // R_ARM_THM_JUMP8: S + A – P
2808 static inline typename This::Status
2809 thm_jump8(unsigned char *view,
2810 const Sized_relobj<32, big_endian>* object,
2811 const Symbol_value<32>* psymval,
2812 Arm_address address)
2813 {
2814 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2815 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2816 Valtype* wv = reinterpret_cast<Valtype*>(view);
2817 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2818 Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
2819 Reltype x = (psymval->value(object, addend) - address);
2820 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
2821 return (utils::has_overflow<8>(x)
2822 ? This::STATUS_OVERFLOW
2823 : This::STATUS_OKAY);
2824 }
2825
2826 // R_ARM_THM_JUMP11: S + A – P
2827 static inline typename This::Status
2828 thm_jump11(unsigned char *view,
2829 const Sized_relobj<32, big_endian>* object,
2830 const Symbol_value<32>* psymval,
2831 Arm_address address)
2832 {
2833 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2834 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
2835 Valtype* wv = reinterpret_cast<Valtype*>(view);
2836 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2837 Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
2838 Reltype x = (psymval->value(object, addend) - address);
2839 elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
2840 return (utils::has_overflow<11>(x)
2841 ? This::STATUS_OVERFLOW
2842 : This::STATUS_OKAY);
2843 }
2844
c121c671
DK
2845 // R_ARM_BASE_PREL: B(S) + A - P
2846 static inline typename This::Status
2847 base_prel(unsigned char* view,
ebabffbd
DK
2848 Arm_address origin,
2849 Arm_address address)
c121c671
DK
2850 {
2851 Base::rel32(view, origin - address);
2852 return STATUS_OKAY;
2853 }
2854
be8fcb75
ILT
2855 // R_ARM_BASE_ABS: B(S) + A
2856 static inline typename This::Status
2857 base_abs(unsigned char* view,
f4e5969c 2858 Arm_address origin)
be8fcb75
ILT
2859 {
2860 Base::rel32(view, origin);
2861 return STATUS_OKAY;
2862 }
2863
c121c671
DK
2864 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2865 static inline typename This::Status
2866 got_brel(unsigned char* view,
2867 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
2868 {
2869 Base::rel32(view, got_offset);
2870 return This::STATUS_OKAY;
2871 }
2872
f4e5969c 2873 // R_ARM_GOT_PREL: GOT(S) + A - P
7f5309a5 2874 static inline typename This::Status
f4e5969c
DK
2875 got_prel(unsigned char *view,
2876 Arm_address got_entry,
ebabffbd 2877 Arm_address address)
7f5309a5 2878 {
f4e5969c 2879 Base::rel32(view, got_entry - address);
7f5309a5
ILT
2880 return This::STATUS_OKAY;
2881 }
2882
c121c671
DK
2883 // R_ARM_PLT32: (S + A) | T - P
2884 static inline typename This::Status
d204b6e9
DK
2885 plt32(const Relocate_info<32, big_endian>* relinfo,
2886 unsigned char *view,
2887 const Sized_symbol<32>* gsym,
2888 const Arm_relobj<big_endian>* object,
2889 unsigned int r_sym,
c121c671 2890 const Symbol_value<32>* psymval,
ebabffbd 2891 Arm_address address,
d204b6e9
DK
2892 Arm_address thumb_bit,
2893 bool is_weakly_undefined_without_plt)
2894 {
2895 return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
2896 object, r_sym, psymval, address, thumb_bit,
2897 is_weakly_undefined_without_plt);
2898 }
2899
2900 // R_ARM_XPC25: (S + A) | T - P
2901 static inline typename This::Status
2902 xpc25(const Relocate_info<32, big_endian>* relinfo,
2903 unsigned char *view,
2904 const Sized_symbol<32>* gsym,
2905 const Arm_relobj<big_endian>* object,
2906 unsigned int r_sym,
2907 const Symbol_value<32>* psymval,
2908 Arm_address address,
2909 Arm_address thumb_bit,
2910 bool is_weakly_undefined_without_plt)
c121c671 2911 {
d204b6e9
DK
2912 return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
2913 object, r_sym, psymval, address, thumb_bit,
2914 is_weakly_undefined_without_plt);
c121c671
DK
2915 }
2916
2917 // R_ARM_CALL: (S + A) | T - P
2918 static inline typename This::Status
d204b6e9
DK
2919 call(const Relocate_info<32, big_endian>* relinfo,
2920 unsigned char *view,
2921 const Sized_symbol<32>* gsym,
2922 const Arm_relobj<big_endian>* object,
2923 unsigned int r_sym,
c121c671 2924 const Symbol_value<32>* psymval,
ebabffbd 2925 Arm_address address,
d204b6e9
DK
2926 Arm_address thumb_bit,
2927 bool is_weakly_undefined_without_plt)
c121c671 2928 {
d204b6e9
DK
2929 return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2930 object, r_sym, psymval, address, thumb_bit,
2931 is_weakly_undefined_without_plt);
c121c671
DK
2932 }
2933
2934 // R_ARM_JUMP24: (S + A) | T - P
2935 static inline typename This::Status
d204b6e9
DK
2936 jump24(const Relocate_info<32, big_endian>* relinfo,
2937 unsigned char *view,
2938 const Sized_symbol<32>* gsym,
2939 const Arm_relobj<big_endian>* object,
2940 unsigned int r_sym,
c121c671 2941 const Symbol_value<32>* psymval,
ebabffbd 2942 Arm_address address,
d204b6e9
DK
2943 Arm_address thumb_bit,
2944 bool is_weakly_undefined_without_plt)
c121c671 2945 {
d204b6e9
DK
2946 return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2947 object, r_sym, psymval, address, thumb_bit,
2948 is_weakly_undefined_without_plt);
c121c671
DK
2949 }
2950
2951 // R_ARM_PREL: (S + A) | T - P
2952 static inline typename This::Status
2953 prel31(unsigned char *view,
2954 const Sized_relobj<32, big_endian>* object,
2955 const Symbol_value<32>* psymval,
ebabffbd 2956 Arm_address address,
2daedcd6 2957 Arm_address thumb_bit)
c121c671
DK
2958 {
2959 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2960 Valtype* wv = reinterpret_cast<Valtype*>(view);
2961 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2962 Valtype addend = utils::sign_extend<31>(val);
2daedcd6 2963 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c121c671
DK
2964 val = utils::bit_select(val, x, 0x7fffffffU);
2965 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2966 return (utils::has_overflow<31>(x) ?
2967 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2968 }
fd3c5f0b
ILT
2969
2970 // R_ARM_MOVW_ABS_NC: (S + A) | T
2971 static inline typename This::Status
2972 movw_abs_nc(unsigned char *view,
2973 const Sized_relobj<32, big_endian>* object,
2974 const Symbol_value<32>* psymval,
2daedcd6 2975 Arm_address thumb_bit)
fd3c5f0b
ILT
2976 {
2977 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2978 Valtype* wv = reinterpret_cast<Valtype*>(view);
2979 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2980 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 2981 Valtype x = psymval->value(object, addend) | thumb_bit;
fd3c5f0b
ILT
2982 val = This::insert_val_arm_movw_movt(val, x);
2983 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2984 return This::STATUS_OKAY;
2985 }
2986
2987 // R_ARM_MOVT_ABS: S + A
2988 static inline typename This::Status
2989 movt_abs(unsigned char *view,
2990 const Sized_relobj<32, big_endian>* object,
2991 const Symbol_value<32>* psymval)
2992 {
2993 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2994 Valtype* wv = reinterpret_cast<Valtype*>(view);
2995 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2996 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 2997 Valtype x = psymval->value(object, addend) >> 16;
fd3c5f0b
ILT
2998 val = This::insert_val_arm_movw_movt(val, x);
2999 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3000 return This::STATUS_OKAY;
3001 }
3002
3003 // R_ARM_THM_MOVW_ABS_NC: S + A | T
3004 static inline typename This::Status
3005 thm_movw_abs_nc(unsigned char *view,
3006 const Sized_relobj<32, big_endian>* object,
3007 const Symbol_value<32>* psymval,
2daedcd6 3008 Arm_address thumb_bit)
fd3c5f0b
ILT
3009 {
3010 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3011 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3012 Valtype* wv = reinterpret_cast<Valtype*>(view);
3013 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3014 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
3015 Reltype addend = extract_thumb_movw_movt_addend(val);
2daedcd6 3016 Reltype x = psymval->value(object, addend) | thumb_bit;
fd3c5f0b
ILT
3017 val = This::insert_val_thumb_movw_movt(val, x);
3018 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3019 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3020 return This::STATUS_OKAY;
3021 }
3022
3023 // R_ARM_THM_MOVT_ABS: S + A
3024 static inline typename This::Status
3025 thm_movt_abs(unsigned char *view,
3026 const Sized_relobj<32, big_endian>* object,
3027 const Symbol_value<32>* psymval)
3028 {
3029 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3030 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3031 Valtype* wv = reinterpret_cast<Valtype*>(view);
3032 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3033 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
3034 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2daedcd6 3035 Reltype x = psymval->value(object, addend) >> 16;
fd3c5f0b
ILT
3036 val = This::insert_val_thumb_movw_movt(val, x);
3037 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3038 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3039 return This::STATUS_OKAY;
3040 }
3041
c2a122b6
ILT
3042 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3043 static inline typename This::Status
3044 movw_prel_nc(unsigned char *view,
3045 const Sized_relobj<32, big_endian>* object,
3046 const Symbol_value<32>* psymval,
ebabffbd 3047 Arm_address address,
2daedcd6 3048 Arm_address thumb_bit)
c2a122b6
ILT
3049 {
3050 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3051 Valtype* wv = reinterpret_cast<Valtype*>(view);
3052 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3053 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 3054 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c2a122b6
ILT
3055 val = This::insert_val_arm_movw_movt(val, x);
3056 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3057 return This::STATUS_OKAY;
3058 }
3059
3060 // R_ARM_MOVT_PREL: S + A - P
3061 static inline typename This::Status
3062 movt_prel(unsigned char *view,
3063 const Sized_relobj<32, big_endian>* object,
3064 const Symbol_value<32>* psymval,
ebabffbd 3065 Arm_address address)
c2a122b6
ILT
3066 {
3067 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3068 Valtype* wv = reinterpret_cast<Valtype*>(view);
3069 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3070 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 3071 Valtype x = (psymval->value(object, addend) - address) >> 16;
c2a122b6
ILT
3072 val = This::insert_val_arm_movw_movt(val, x);
3073 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3074 return This::STATUS_OKAY;
3075 }
3076
3077 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3078 static inline typename This::Status
3079 thm_movw_prel_nc(unsigned char *view,
3080 const Sized_relobj<32, big_endian>* object,
3081 const Symbol_value<32>* psymval,
ebabffbd 3082 Arm_address address,
2daedcd6 3083 Arm_address thumb_bit)
c2a122b6
ILT
3084 {
3085 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3086 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3087 Valtype* wv = reinterpret_cast<Valtype*>(view);
3088 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3089 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3090 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2daedcd6 3091 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
c2a122b6
ILT
3092 val = This::insert_val_thumb_movw_movt(val, x);
3093 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3094 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3095 return This::STATUS_OKAY;
3096 }
3097
3098 // R_ARM_THM_MOVT_PREL: S + A - P
3099 static inline typename This::Status
3100 thm_movt_prel(unsigned char *view,
3101 const Sized_relobj<32, big_endian>* object,
3102 const Symbol_value<32>* psymval,
ebabffbd 3103 Arm_address address)
c2a122b6
ILT
3104 {
3105 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3106 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3107 Valtype* wv = reinterpret_cast<Valtype*>(view);
3108 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3109 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3110 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2daedcd6 3111 Reltype x = (psymval->value(object, addend) - address) >> 16;
c2a122b6
ILT
3112 val = This::insert_val_thumb_movw_movt(val, x);
3113 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3114 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3115 return This::STATUS_OKAY;
3116 }
a2162063
ILT
3117
3118 // R_ARM_V4BX
3119 static inline typename This::Status
3120 v4bx(const Relocate_info<32, big_endian>* relinfo,
3121 unsigned char *view,
3122 const Arm_relobj<big_endian>* object,
3123 const Arm_address address,
3124 const bool is_interworking)
3125 {
3126
3127 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3128 Valtype* wv = reinterpret_cast<Valtype*>(view);
3129 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3130
3131 // Ensure that we have a BX instruction.
3132 gold_assert((val & 0x0ffffff0) == 0x012fff10);
3133 const uint32_t reg = (val & 0xf);
3134 if (is_interworking && reg != 0xf)
3135 {
3136 Stub_table<big_endian>* stub_table =
3137 object->stub_table(relinfo->data_shndx);
3138 gold_assert(stub_table != NULL);
3139
3140 Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3141 gold_assert(stub != NULL);
3142
3143 int32_t veneer_address =
3144 stub_table->address() + stub->offset() - 8 - address;
3145 gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3146 && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3147 // Replace with a branch to veneer (B <addr>)
3148 val = (val & 0xf0000000) | 0x0a000000
3149 | ((veneer_address >> 2) & 0x00ffffff);
3150 }
3151 else
3152 {
3153 // Preserve Rm (lowest four bits) and the condition code
3154 // (highest four bits). Other bits encode MOV PC,Rm.
3155 val = (val & 0xf000000f) | 0x01a0f000;
3156 }
3157 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3158 return This::STATUS_OKAY;
3159 }
c121c671
DK
3160};
3161
d204b6e9
DK
3162// Relocate ARM long branches. This handles relocation types
3163// R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3164// If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3165// undefined and we do not use PLT in this relocation. In such a case,
3166// the branch is converted into an NOP.
3167
3168template<bool big_endian>
3169typename Arm_relocate_functions<big_endian>::Status
3170Arm_relocate_functions<big_endian>::arm_branch_common(
3171 unsigned int r_type,
3172 const Relocate_info<32, big_endian>* relinfo,
3173 unsigned char *view,
3174 const Sized_symbol<32>* gsym,
3175 const Arm_relobj<big_endian>* object,
3176 unsigned int r_sym,
3177 const Symbol_value<32>* psymval,
3178 Arm_address address,
3179 Arm_address thumb_bit,
3180 bool is_weakly_undefined_without_plt)
3181{
3182 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3183 Valtype* wv = reinterpret_cast<Valtype*>(view);
3184 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3185
3186 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3187 && ((val & 0x0f000000UL) == 0x0a000000UL);
3188 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3189 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3190 && ((val & 0x0f000000UL) == 0x0b000000UL);
3191 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3192 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3193
3194 // Check that the instruction is valid.
3195 if (r_type == elfcpp::R_ARM_CALL)
3196 {
3197 if (!insn_is_uncond_bl && !insn_is_blx)
3198 return This::STATUS_BAD_RELOC;
3199 }
3200 else if (r_type == elfcpp::R_ARM_JUMP24)
3201 {
3202 if (!insn_is_b && !insn_is_cond_bl)
3203 return This::STATUS_BAD_RELOC;
3204 }
3205 else if (r_type == elfcpp::R_ARM_PLT32)
3206 {
3207 if (!insn_is_any_branch)
3208 return This::STATUS_BAD_RELOC;
3209 }
3210 else if (r_type == elfcpp::R_ARM_XPC25)
3211 {
3212 // FIXME: AAELF document IH0044C does not say much about it other
3213 // than it being obsolete.
3214 if (!insn_is_any_branch)
3215 return This::STATUS_BAD_RELOC;
3216 }
3217 else
3218 gold_unreachable();
3219
3220 // A branch to an undefined weak symbol is turned into a jump to
3221 // the next instruction unless a PLT entry will be created.
3222 // Do the same for local undefined symbols.
3223 // The jump to the next instruction is optimized as a NOP depending
3224 // on the architecture.
3225 const Target_arm<big_endian>* arm_target =
3226 Target_arm<big_endian>::default_target();
3227 if (is_weakly_undefined_without_plt)
3228 {
3229 Valtype cond = val & 0xf0000000U;
3230 if (arm_target->may_use_arm_nop())
3231 val = cond | 0x0320f000;
3232 else
3233 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
3234 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3235 return This::STATUS_OKAY;
3236 }
3237
3238 Valtype addend = utils::sign_extend<26>(val << 2);
3239 Valtype branch_target = psymval->value(object, addend);
3240 int32_t branch_offset = branch_target - address;
3241
3242 // We need a stub if the branch offset is too large or if we need
3243 // to switch mode.
3244 bool may_use_blx = arm_target->may_use_blx();
3245 Reloc_stub* stub = NULL;
3246 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
3247 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3248 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
3249 {
3250 Stub_type stub_type =
3251 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
3252 (thumb_bit != 0));
3253 if (stub_type != arm_stub_none)
3254 {
2ea97941 3255 Stub_table<big_endian>* stub_table =
d204b6e9 3256 object->stub_table(relinfo->data_shndx);
2ea97941 3257 gold_assert(stub_table != NULL);
d204b6e9
DK
3258
3259 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2ea97941 3260 stub = stub_table->find_reloc_stub(stub_key);
d204b6e9
DK
3261 gold_assert(stub != NULL);
3262 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2ea97941 3263 branch_target = stub_table->address() + stub->offset() + addend;
d204b6e9
DK
3264 branch_offset = branch_target - address;
3265 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
3266 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
3267 }
3268 }
3269
3270 // At this point, if we still need to switch mode, the instruction
3271 // must either be a BLX or a BL that can be converted to a BLX.
3272 if (thumb_bit != 0)
3273 {
3274 // Turn BL to BLX.
3275 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3276 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3277 }
3278
3279 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3280 elfcpp::Swap<32, big_endian>::writeval(wv, val);
3281 return (utils::has_overflow<26>(branch_offset)
3282 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3283}
3284
51938283
DK
3285// Relocate THUMB long branches. This handles relocation types
3286// R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3287// If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3288// undefined and we do not use PLT in this relocation. In such a case,
3289// the branch is converted into an NOP.
3290
3291template<bool big_endian>
3292typename Arm_relocate_functions<big_endian>::Status
3293Arm_relocate_functions<big_endian>::thumb_branch_common(
3294 unsigned int r_type,
3295 const Relocate_info<32, big_endian>* relinfo,
3296 unsigned char *view,
3297 const Sized_symbol<32>* gsym,
3298 const Arm_relobj<big_endian>* object,
3299 unsigned int r_sym,
3300 const Symbol_value<32>* psymval,
3301 Arm_address address,
3302 Arm_address thumb_bit,
3303 bool is_weakly_undefined_without_plt)
3304{
3305 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3306 Valtype* wv = reinterpret_cast<Valtype*>(view);
3307 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3308 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3309
3310 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3311 // into account.
3312 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
3313 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
3314
3315 // Check that the instruction is valid.
3316 if (r_type == elfcpp::R_ARM_THM_CALL)
3317 {
3318 if (!is_bl_insn && !is_blx_insn)
3319 return This::STATUS_BAD_RELOC;
3320 }
3321 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
3322 {
3323 // This cannot be a BLX.
3324 if (!is_bl_insn)
3325 return This::STATUS_BAD_RELOC;
3326 }
3327 else if (r_type == elfcpp::R_ARM_THM_XPC22)
3328 {
3329 // Check for Thumb to Thumb call.
3330 if (!is_blx_insn)
3331 return This::STATUS_BAD_RELOC;
3332 if (thumb_bit != 0)
3333 {
3334 gold_warning(_("%s: Thumb BLX instruction targets "
3335 "thumb function '%s'."),
3336 object->name().c_str(),
3337 (gsym ? gsym->name() : "(local)"));
3338 // Convert BLX to BL.
3339 lower_insn |= 0x1000U;
3340 }
3341 }
3342 else
3343 gold_unreachable();
3344
3345 // A branch to an undefined weak symbol is turned into a jump to
3346 // the next instruction unless a PLT entry will be created.
3347 // The jump to the next instruction is optimized as a NOP.W for
3348 // Thumb-2 enabled architectures.
3349 const Target_arm<big_endian>* arm_target =
3350 Target_arm<big_endian>::default_target();
3351 if (is_weakly_undefined_without_plt)
3352 {
3353 if (arm_target->may_use_thumb2_nop())
3354 {
3355 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
3356 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
3357 }
3358 else
3359 {
3360 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
3361 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
3362 }
3363 return This::STATUS_OKAY;
3364 }
3365
089d69dc 3366 int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
51938283
DK
3367 Arm_address branch_target = psymval->value(object, addend);
3368 int32_t branch_offset = branch_target - address;
3369
3370 // We need a stub if the branch offset is too large or if we need
3371 // to switch mode.
3372 bool may_use_blx = arm_target->may_use_blx();
3373 bool thumb2 = arm_target->using_thumb2();
3374 if ((!thumb2
3375 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3376 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3377 || (thumb2
3378 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3379 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3380 || ((thumb_bit == 0)
3381 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3382 || r_type == elfcpp::R_ARM_THM_JUMP24)))
3383 {
3384 Stub_type stub_type =
3385 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
3386 (thumb_bit != 0));
3387 if (stub_type != arm_stub_none)
3388 {
2ea97941 3389 Stub_table<big_endian>* stub_table =
51938283 3390 object->stub_table(relinfo->data_shndx);
2ea97941 3391 gold_assert(stub_table != NULL);
51938283
DK
3392
3393 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2ea97941 3394 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
51938283
DK
3395 gold_assert(stub != NULL);
3396 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2ea97941 3397 branch_target = stub_table->address() + stub->offset() + addend;
51938283
DK
3398 branch_offset = branch_target - address;
3399 }
3400 }
3401
3402 // At this point, if we still need to switch mode, the instruction
3403 // must either be a BLX or a BL that can be converted to a BLX.
3404 if (thumb_bit == 0)
3405 {
3406 gold_assert(may_use_blx
3407 && (r_type == elfcpp::R_ARM_THM_CALL
3408 || r_type == elfcpp::R_ARM_THM_XPC22));
3409 // Make sure this is a BLX.
3410 lower_insn &= ~0x1000U;
3411 }
3412 else
3413 {
3414 // Make sure this is a BL.
3415 lower_insn |= 0x1000U;
3416 }
3417
51938283
DK
3418 if ((lower_insn & 0x5000U) == 0x4000U)
3419 // For a BLX instruction, make sure that the relocation is rounded up
3420 // to a word boundary. This follows the semantics of the instruction
3421 // which specifies that bit 1 of the target address will come from bit
3422 // 1 of the base address.
089d69dc 3423 branch_offset = (branch_offset + 2) & ~3;
51938283
DK
3424
3425 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
3426 // We use the Thumb-2 encoding, which is safe even if dealing with
3427 // a Thumb-1 instruction by virtue of our overflow check above. */
089d69dc
DK
3428 upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
3429 lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
51938283
DK
3430
3431 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3432 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3433
3434 return ((thumb2
089d69dc
DK
3435 ? utils::has_overflow<25>(branch_offset)
3436 : utils::has_overflow<23>(branch_offset))
3437 ? This::STATUS_OVERFLOW
3438 : This::STATUS_OKAY);
3439}
3440
3441// Relocate THUMB-2 long conditional branches.
3442// If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
3443// undefined and we do not use PLT in this relocation. In such a case,
3444// the branch is converted into an NOP.
3445
3446template<bool big_endian>
3447typename Arm_relocate_functions<big_endian>::Status
3448Arm_relocate_functions<big_endian>::thm_jump19(
3449 unsigned char *view,
3450 const Arm_relobj<big_endian>* object,
3451 const Symbol_value<32>* psymval,
3452 Arm_address address,
3453 Arm_address thumb_bit)
3454{
3455 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3456 Valtype* wv = reinterpret_cast<Valtype*>(view);
3457 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3458 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3459 int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
3460
3461 Arm_address branch_target = psymval->value(object, addend);
3462 int32_t branch_offset = branch_target - address;
3463
3464 // ??? Should handle interworking? GCC might someday try to
3465 // use this for tail calls.
3466 // FIXME: We do support thumb entry to PLT yet.
3467 if (thumb_bit == 0)
3468 {
3469 gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
3470 return This::STATUS_BAD_RELOC;
3471 }
3472
3473 // Put RELOCATION back into the insn.
3474 upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
3475 lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
3476
3477 // Put the relocated value back in the object file:
3478 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
3479 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
3480
3481 return (utils::has_overflow<21>(branch_offset)
51938283
DK
3482 ? This::STATUS_OVERFLOW
3483 : This::STATUS_OKAY);
3484}
3485
94cdfcff
DK
3486// Get the GOT section, creating it if necessary.
3487
3488template<bool big_endian>
3489Output_data_got<32, big_endian>*
3490Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
3491{
3492 if (this->got_ == NULL)
3493 {
3494 gold_assert(symtab != NULL && layout != NULL);
3495
3496 this->got_ = new Output_data_got<32, big_endian>();
3497
3498 Output_section* os;
3499 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3500 (elfcpp::SHF_ALLOC
3501 | elfcpp::SHF_WRITE),
1a2dff53
ILT
3502 this->got_, false, true, true,
3503 false);
94cdfcff
DK
3504
3505 // The old GNU linker creates a .got.plt section. We just
3506 // create another set of data in the .got section. Note that we
3507 // always create a PLT if we create a GOT, although the PLT
3508 // might be empty.
3509 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
3510 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3511 (elfcpp::SHF_ALLOC
3512 | elfcpp::SHF_WRITE),
1a2dff53
ILT
3513 this->got_plt_, false, false,
3514 false, true);
94cdfcff
DK
3515
3516 // The first three entries are reserved.
3517 this->got_plt_->set_current_data_size(3 * 4);
3518
3519 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3520 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
99fff23b 3521 Symbol_table::PREDEFINED,
94cdfcff
DK
3522 this->got_plt_,
3523 0, 0, elfcpp::STT_OBJECT,
3524 elfcpp::STB_LOCAL,
3525 elfcpp::STV_HIDDEN, 0,
3526 false, false);
3527 }
3528 return this->got_;
3529}
3530
3531// Get the dynamic reloc section, creating it if necessary.
3532
3533template<bool big_endian>
3534typename Target_arm<big_endian>::Reloc_section*
3535Target_arm<big_endian>::rel_dyn_section(Layout* layout)
3536{
3537 if (this->rel_dyn_ == NULL)
3538 {
3539 gold_assert(layout != NULL);
3540 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
3541 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1a2dff53
ILT
3542 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
3543 false, false, false);
94cdfcff
DK
3544 }
3545 return this->rel_dyn_;
3546}
3547
b569affa
DK
3548// Insn_template methods.
3549
3550// Return byte size of an instruction template.
3551
3552size_t
3553Insn_template::size() const
3554{
3555 switch (this->type())
3556 {
3557 case THUMB16_TYPE:
2fb7225c 3558 case THUMB16_SPECIAL_TYPE:
b569affa
DK
3559 return 2;
3560 case ARM_TYPE:
3561 case THUMB32_TYPE:
3562 case DATA_TYPE:
3563 return 4;
3564 default:
3565 gold_unreachable();
3566 }
3567}
3568
3569// Return alignment of an instruction template.
3570
3571unsigned
3572Insn_template::alignment() const
3573{
3574 switch (this->type())
3575 {
3576 case THUMB16_TYPE:
2fb7225c 3577 case THUMB16_SPECIAL_TYPE:
b569affa
DK
3578 case THUMB32_TYPE:
3579 return 2;
3580 case ARM_TYPE:
3581 case DATA_TYPE:
3582 return 4;
3583 default:
3584 gold_unreachable();
3585 }
3586}
3587
3588// Stub_template methods.
3589
3590Stub_template::Stub_template(
2ea97941
ILT
3591 Stub_type type, const Insn_template* insns,
3592 size_t insn_count)
3593 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
b569affa
DK
3594 entry_in_thumb_mode_(false), relocs_()
3595{
2ea97941 3596 off_t offset = 0;
b569affa
DK
3597
3598 // Compute byte size and alignment of stub template.
2ea97941 3599 for (size_t i = 0; i < insn_count; i++)
b569affa 3600 {
2ea97941
ILT
3601 unsigned insn_alignment = insns[i].alignment();
3602 size_t insn_size = insns[i].size();
3603 gold_assert((offset & (insn_alignment - 1)) == 0);
b569affa 3604 this->alignment_ = std::max(this->alignment_, insn_alignment);
2ea97941 3605 switch (insns[i].type())
b569affa
DK
3606 {
3607 case Insn_template::THUMB16_TYPE:
089d69dc 3608 case Insn_template::THUMB16_SPECIAL_TYPE:
b569affa
DK
3609 if (i == 0)
3610 this->entry_in_thumb_mode_ = true;
3611 break;
3612
3613 case Insn_template::THUMB32_TYPE:
2ea97941
ILT
3614 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
3615 this->relocs_.push_back(Reloc(i, offset));
b569affa
DK
3616 if (i == 0)
3617 this->entry_in_thumb_mode_ = true;
3618 break;
3619
3620 case Insn_template::ARM_TYPE:
3621 // Handle cases where the target is encoded within the
3622 // instruction.
2ea97941
ILT
3623 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
3624 this->relocs_.push_back(Reloc(i, offset));
b569affa
DK
3625 break;
3626
3627 case Insn_template::DATA_TYPE:
3628 // Entry point cannot be data.
3629 gold_assert(i != 0);
2ea97941 3630 this->relocs_.push_back(Reloc(i, offset));
b569affa
DK
3631 break;
3632
3633 default:
3634 gold_unreachable();
3635 }
2ea97941 3636 offset += insn_size;
b569affa 3637 }
2ea97941 3638 this->size_ = offset;
b569affa
DK
3639}
3640
bb0d3eb0
DK
3641// Stub methods.
3642
3643// Template to implement do_write for a specific target endianity.
3644
3645template<bool big_endian>
3646void inline
3647Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
3648{
3649 const Stub_template* stub_template = this->stub_template();
3650 const Insn_template* insns = stub_template->insns();
3651
3652 // FIXME: We do not handle BE8 encoding yet.
3653 unsigned char* pov = view;
3654 for (size_t i = 0; i < stub_template->insn_count(); i++)
3655 {
3656 switch (insns[i].type())
3657 {
3658 case Insn_template::THUMB16_TYPE:
3659 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
3660 break;
3661 case Insn_template::THUMB16_SPECIAL_TYPE:
3662 elfcpp::Swap<16, big_endian>::writeval(
3663 pov,
3664 this->thumb16_special(i));
3665 break;
3666 case Insn_template::THUMB32_TYPE:
3667 {
3668 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
3669 uint32_t lo = insns[i].data() & 0xffff;
3670 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
3671 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
3672 }
3673 break;
3674 case Insn_template::ARM_TYPE:
3675 case Insn_template::DATA_TYPE:
3676 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
3677 break;
3678 default:
3679 gold_unreachable();
3680 }
3681 pov += insns[i].size();
3682 }
3683 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
3684}
3685
b569affa
DK
3686// Reloc_stub::Key methods.
3687
3688// Dump a Key as a string for debugging.
3689
3690std::string
3691Reloc_stub::Key::name() const
3692{
3693 if (this->r_sym_ == invalid_index)
3694 {
3695 // Global symbol key name
3696 // <stub-type>:<symbol name>:<addend>.
3697 const std::string sym_name = this->u_.symbol->name();
3698 // We need to print two hex number and two colons. So just add 100 bytes
3699 // to the symbol name size.
3700 size_t len = sym_name.size() + 100;
3701 char* buffer = new char[len];
3702 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
3703 sym_name.c_str(), this->addend_);
3704 gold_assert(c > 0 && c < static_cast<int>(len));
3705 delete[] buffer;
3706 return std::string(buffer);
3707 }
3708 else
3709 {
3710 // local symbol key name
3711 // <stub-type>:<object>:<r_sym>:<addend>.
3712 const size_t len = 200;
3713 char buffer[len];
3714 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
3715 this->u_.relobj, this->r_sym_, this->addend_);
3716 gold_assert(c > 0 && c < static_cast<int>(len));
3717 return std::string(buffer);
3718 }
3719}
3720
3721// Reloc_stub methods.
3722
3723// Determine the type of stub needed, if any, for a relocation of R_TYPE at
3724// LOCATION to DESTINATION.
3725// This code is based on the arm_type_of_stub function in
3726// bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
3727// class simple.
3728
3729Stub_type
3730Reloc_stub::stub_type_for_reloc(
3731 unsigned int r_type,
3732 Arm_address location,
3733 Arm_address destination,
3734 bool target_is_thumb)
3735{
3736 Stub_type stub_type = arm_stub_none;
3737
3738 // This is a bit ugly but we want to avoid using a templated class for
3739 // big and little endianities.
3740 bool may_use_blx;
3741 bool should_force_pic_veneer;
3742 bool thumb2;
3743 bool thumb_only;
3744 if (parameters->target().is_big_endian())
3745 {
43d12afe 3746 const Target_arm<true>* big_endian_target =
b569affa 3747 Target_arm<true>::default_target();
43d12afe
DK
3748 may_use_blx = big_endian_target->may_use_blx();
3749 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
3750 thumb2 = big_endian_target->using_thumb2();
3751 thumb_only = big_endian_target->using_thumb_only();
b569affa
DK
3752 }
3753 else
3754 {
43d12afe 3755 const Target_arm<false>* little_endian_target =
b569affa 3756 Target_arm<false>::default_target();
43d12afe
DK
3757 may_use_blx = little_endian_target->may_use_blx();
3758 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
3759 thumb2 = little_endian_target->using_thumb2();
3760 thumb_only = little_endian_target->using_thumb_only();
b569affa
DK
3761 }
3762
3763 int64_t branch_offset = (int64_t)destination - location;
3764
3765 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
3766 {
3767 // Handle cases where:
3768 // - this call goes too far (different Thumb/Thumb2 max
3769 // distance)
3770 // - it's a Thumb->Arm call and blx is not available, or it's a
3771 // Thumb->Arm branch (not bl). A stub is needed in this case.
3772 if ((!thumb2
3773 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3774 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3775 || (thumb2
3776 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3777 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3778 || ((!target_is_thumb)
3779 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3780 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
3781 {
3782 if (target_is_thumb)
3783 {
3784 // Thumb to thumb.
3785 if (!thumb_only)
3786 {
51938283
DK
3787 stub_type = (parameters->options().shared()
3788 || should_force_pic_veneer)
b569affa
DK
3789 // PIC stubs.
3790 ? ((may_use_blx
3791 && (r_type == elfcpp::R_ARM_THM_CALL))
3792 // V5T and above. Stub starts with ARM code, so
3793 // we must be able to switch mode before
3794 // reaching it, which is only possible for 'bl'
3795 // (ie R_ARM_THM_CALL relocation).
3796 ? arm_stub_long_branch_any_thumb_pic
3797 // On V4T, use Thumb code only.
3798 : arm_stub_long_branch_v4t_thumb_thumb_pic)
3799
3800 // non-PIC stubs.
3801 : ((may_use_blx
3802 && (r_type == elfcpp::R_ARM_THM_CALL))
3803 ? arm_stub_long_branch_any_any // V5T and above.
3804 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
3805 }
3806 else
3807 {
51938283
DK
3808 stub_type = (parameters->options().shared()
3809 || should_force_pic_veneer)
b569affa
DK
3810 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
3811 : arm_stub_long_branch_thumb_only; // non-PIC stub.
3812 }
3813 }
3814 else
3815 {
3816 // Thumb to arm.
3817
3818 // FIXME: We should check that the input section is from an
3819 // object that has interwork enabled.
3820
3821 stub_type = (parameters->options().shared()
3822 || should_force_pic_veneer)
3823 // PIC stubs.
3824 ? ((may_use_blx
3825 && (r_type == elfcpp::R_ARM_THM_CALL))
3826 ? arm_stub_long_branch_any_arm_pic // V5T and above.
3827 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
3828
3829 // non-PIC stubs.
3830 : ((may_use_blx
3831 && (r_type == elfcpp::R_ARM_THM_CALL))
3832 ? arm_stub_long_branch_any_any // V5T and above.
3833 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
3834
3835 // Handle v4t short branches.
3836 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3837 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3838 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3839 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3840 }
3841 }
3842 }
3843 else if (r_type == elfcpp::R_ARM_CALL
3844 || r_type == elfcpp::R_ARM_JUMP24
3845 || r_type == elfcpp::R_ARM_PLT32)
3846 {
3847 if (target_is_thumb)
3848 {
3849 // Arm to thumb.
3850
3851 // FIXME: We should check that the input section is from an
3852 // object that has interwork enabled.
3853
3854 // We have an extra 2-bytes reach because of
3855 // the mode change (bit 24 (H) of BLX encoding).
3856 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3857 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3858 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
3859 || (r_type == elfcpp::R_ARM_JUMP24)
3860 || (r_type == elfcpp::R_ARM_PLT32))
3861 {
3862 stub_type = (parameters->options().shared()
3863 || should_force_pic_veneer)
3864 // PIC stubs.
3865 ? (may_use_blx
3866 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
3867 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
3868
3869 // non-PIC stubs.
3870 : (may_use_blx
3871 ? arm_stub_long_branch_any_any // V5T and above.
3872 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
3873 }
3874 }
3875 else
3876 {
3877 // Arm to arm.
3878 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3879 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3880 {
3881 stub_type = (parameters->options().shared()
3882 || should_force_pic_veneer)
3883 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
3884 : arm_stub_long_branch_any_any; /// non-PIC.
3885 }
3886 }
3887 }
3888
3889 return stub_type;
3890}
3891
bb0d3eb0 3892// Cortex_a8_stub methods.
b569affa 3893
bb0d3eb0
DK
3894// Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3895// I is the position of the instruction template in the stub template.
b569affa 3896
bb0d3eb0
DK
3897uint16_t
3898Cortex_a8_stub::do_thumb16_special(size_t i)
b569affa 3899{
bb0d3eb0
DK
3900 // The only use of this is to copy condition code from a conditional
3901 // branch being worked around to the corresponding conditional branch in
3902 // to the stub.
3903 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3904 && i == 0);
3905 uint16_t data = this->stub_template()->insns()[i].data();
3906 gold_assert((data & 0xff00U) == 0xd000U);
3907 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
3908 return data;
b569affa
DK
3909}
3910
3911// Stub_factory methods.
3912
3913Stub_factory::Stub_factory()
3914{
3915 // The instruction template sequences are declared as static
3916 // objects and initialized first time the constructor runs.
3917
3918 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3919 // to reach the stub if necessary.
3920 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
3921 {
3922 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3923 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3924 // dcd R_ARM_ABS32(X)
3925 };
3926
3927 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3928 // available.
3929 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
3930 {
3931 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3932 Insn_template::arm_insn(0xe12fff1c), // bx ip
3933 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3934 // dcd R_ARM_ABS32(X)
3935 };
3936
3937 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3938 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
3939 {
3940 Insn_template::thumb16_insn(0xb401), // push {r0}
3941 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3942 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3943 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3944 Insn_template::thumb16_insn(0x4760), // bx ip
3945 Insn_template::thumb16_insn(0xbf00), // nop
3946 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3947 // dcd R_ARM_ABS32(X)
3948 };
3949
3950 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3951 // allowed.
3952 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
3953 {
3954 Insn_template::thumb16_insn(0x4778), // bx pc
3955 Insn_template::thumb16_insn(0x46c0), // nop
3956 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3957 Insn_template::arm_insn(0xe12fff1c), // bx ip
3958 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3959 // dcd R_ARM_ABS32(X)
3960 };
3961
3962 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3963 // available.
3964 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
3965 {
3966 Insn_template::thumb16_insn(0x4778), // bx pc
3967 Insn_template::thumb16_insn(0x46c0), // nop
3968 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3969 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3970 // dcd R_ARM_ABS32(X)
3971 };
3972
3973 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3974 // one, when the destination is close enough.
3975 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
3976 {
3977 Insn_template::thumb16_insn(0x4778), // bx pc
3978 Insn_template::thumb16_insn(0x46c0), // nop
3979 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3980 };
3981
3982 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3983 // blx to reach the stub if necessary.
3984 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
3985 {
3986 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3987 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3988 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3989 // dcd R_ARM_REL32(X-4)
3990 };
3991
3992 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3993 // blx to reach the stub if necessary. We can not add into pc;
3994 // it is not guaranteed to mode switch (different in ARMv6 and
3995 // ARMv7).
3996 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
3997 {
3998 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3999 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4000 Insn_template::arm_insn(0xe12fff1c), // bx ip
4001 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4002 // dcd R_ARM_REL32(X)
4003 };
4004
4005 // V4T ARM -> ARM long branch stub, PIC.
4006 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4007 {
4008 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4009 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4010 Insn_template::arm_insn(0xe12fff1c), // bx ip
4011 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4012 // dcd R_ARM_REL32(X)
4013 };
4014
4015 // V4T Thumb -> ARM long branch stub, PIC.
4016 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4017 {
4018 Insn_template::thumb16_insn(0x4778), // bx pc
4019 Insn_template::thumb16_insn(0x46c0), // nop
4020 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
4021 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
4022 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4023 // dcd R_ARM_REL32(X)
4024 };
4025
4026 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4027 // architectures.
4028 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4029 {
4030 Insn_template::thumb16_insn(0xb401), // push {r0}
4031 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
4032 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
4033 Insn_template::thumb16_insn(0x4484), // add ip, r0
4034 Insn_template::thumb16_insn(0xbc01), // pop {r0}
4035 Insn_template::thumb16_insn(0x4760), // bx ip
4036 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4037 // dcd R_ARM_REL32(X)
4038 };
4039
4040 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4041 // allowed.
4042 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4043 {
4044 Insn_template::thumb16_insn(0x4778), // bx pc
4045 Insn_template::thumb16_insn(0x46c0), // nop
4046 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
4047 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
4048 Insn_template::arm_insn(0xe12fff1c), // bx ip
4049 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4050 // dcd R_ARM_REL32(X)
4051 };
4052
4053 // Cortex-A8 erratum-workaround stubs.
4054
4055 // Stub used for conditional branches (which may be beyond +/-1MB away,
4056 // so we can't use a conditional branch to reach this stub).
4057
4058 // original code:
4059 //
4060 // b<cond> X
4061 // after:
4062 //
4063 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4064 {
4065 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
4066 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
4067 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
4068 // b.w X
4069 };
4070
4071 // Stub used for b.w and bl.w instructions.
4072
4073 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4074 {
4075 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4076 };
4077
4078 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4079 {
4080 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
4081 };
4082
4083 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
4084 // instruction (which switches to ARM mode) to point to this stub. Jump to
4085 // the real destination using an ARM-mode branch.
bb0d3eb0 4086 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
b569affa
DK
4087 {
4088 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
4089 };
4090
a2162063
ILT
4091 // Stub used to provide an interworking for R_ARM_V4BX relocation
4092 // (bx r[n] instruction).
4093 static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4094 {
4095 Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
4096 Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
4097 Insn_template::arm_insn(0xe12fff10) // bx r<n>
4098 };
4099
b569affa
DK
4100 // Fill in the stub template look-up table. Stub templates are constructed
4101 // per instance of Stub_factory for fast look-up without locking
4102 // in a thread-enabled environment.
4103
4104 this->stub_templates_[arm_stub_none] =
4105 new Stub_template(arm_stub_none, NULL, 0);
4106
4107#define DEF_STUB(x) \
4108 do \
4109 { \
4110 size_t array_size \
4111 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4112 Stub_type type = arm_stub_##x; \
4113 this->stub_templates_[type] = \
4114 new Stub_template(type, elf32_arm_stub_##x, array_size); \
4115 } \
4116 while (0);
4117
4118 DEF_STUBS
4119#undef DEF_STUB
4120}
4121
56ee5e00
DK
4122// Stub_table methods.
4123
2fb7225c 4124// Removel all Cortex-A8 stub.
56ee5e00
DK
4125
4126template<bool big_endian>
4127void
2fb7225c
DK
4128Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4129{
4130 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4131 p != this->cortex_a8_stubs_.end();
4132 ++p)
4133 delete p->second;
4134 this->cortex_a8_stubs_.clear();
4135}
4136
4137// Relocate one stub. This is a helper for Stub_table::relocate_stubs().
4138
4139template<bool big_endian>
4140void
4141Stub_table<big_endian>::relocate_stub(
4142 Stub* stub,
4143 const Relocate_info<32, big_endian>* relinfo,
4144 Target_arm<big_endian>* arm_target,
4145 Output_section* output_section,
4146 unsigned char* view,
4147 Arm_address address,
4148 section_size_type view_size)
56ee5e00 4149{
2ea97941 4150 const Stub_template* stub_template = stub->stub_template();
2fb7225c
DK
4151 if (stub_template->reloc_count() != 0)
4152 {
4153 // Adjust view to cover the stub only.
4154 section_size_type offset = stub->offset();
4155 section_size_type stub_size = stub_template->size();
4156 gold_assert(offset + stub_size <= view_size);
4157
4158 arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4159 address + offset, stub_size);
4160 }
56ee5e00
DK
4161}
4162
2fb7225c
DK
4163// Relocate all stubs in this stub table.
4164
56ee5e00
DK
4165template<bool big_endian>
4166void
4167Stub_table<big_endian>::relocate_stubs(
4168 const Relocate_info<32, big_endian>* relinfo,
4169 Target_arm<big_endian>* arm_target,
2ea97941 4170 Output_section* output_section,
56ee5e00 4171 unsigned char* view,
2ea97941 4172 Arm_address address,
56ee5e00
DK
4173 section_size_type view_size)
4174{
4175 // If we are passed a view bigger than the stub table's. we need to
4176 // adjust the view.
2ea97941 4177 gold_assert(address == this->address()
56ee5e00
DK
4178 && (view_size
4179 == static_cast<section_size_type>(this->data_size())));
4180
2fb7225c
DK
4181 // Relocate all relocation stubs.
4182 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4183 p != this->reloc_stubs_.end();
4184 ++p)
4185 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4186 address, view_size);
4187
4188 // Relocate all Cortex-A8 stubs.
4189 for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4190 p != this->cortex_a8_stubs_.end();
4191 ++p)
4192 this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4193 address, view_size);
a2162063
ILT
4194
4195 // Relocate all ARM V4BX stubs.
4196 for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4197 p != this->arm_v4bx_stubs_.end();
4198 ++p)
4199 {
4200 if (*p != NULL)
4201 this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4202 address, view_size);
4203 }
2fb7225c
DK
4204}
4205
4206// Write out the stubs to file.
4207
4208template<bool big_endian>
4209void
4210Stub_table<big_endian>::do_write(Output_file* of)
4211{
4212 off_t offset = this->offset();
4213 const section_size_type oview_size =
4214 convert_to_section_size_type(this->data_size());
4215 unsigned char* const oview = of->get_output_view(offset, oview_size);
4216
4217 // Write relocation stubs.
56ee5e00
DK
4218 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4219 p != this->reloc_stubs_.end();
4220 ++p)
4221 {
4222 Reloc_stub* stub = p->second;
2fb7225c
DK
4223 Arm_address address = this->address() + stub->offset();
4224 gold_assert(address
4225 == align_address(address,
4226 stub->stub_template()->alignment()));
4227 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4228 big_endian);
56ee5e00 4229 }
2fb7225c
DK
4230
4231 // Write Cortex-A8 stubs.
4232 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4233 p != this->cortex_a8_stubs_.end();
4234 ++p)
4235 {
4236 Cortex_a8_stub* stub = p->second;
4237 Arm_address address = this->address() + stub->offset();
4238 gold_assert(address
4239 == align_address(address,
4240 stub->stub_template()->alignment()));
4241 stub->write(oview + stub->offset(), stub->stub_template()->size(),
4242 big_endian);
4243 }
4244
a2162063
ILT
4245 // Write ARM V4BX relocation stubs.
4246 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4247 p != this->arm_v4bx_stubs_.end();
4248 ++p)
4249 {
4250 if (*p == NULL)
4251 continue;
4252
4253 Arm_address address = this->address() + (*p)->offset();
4254 gold_assert(address
4255 == align_address(address,
4256 (*p)->stub_template()->alignment()));
4257 (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4258 big_endian);
4259 }
4260
2fb7225c 4261 of->write_output_view(this->offset(), oview_size, oview);
56ee5e00
DK
4262}
4263
2fb7225c
DK
4264// Update the data size and address alignment of the stub table at the end
4265// of a relaxation pass. Return true if either the data size or the
4266// alignment changed in this relaxation pass.
4267
4268template<bool big_endian>
4269bool
4270Stub_table<big_endian>::update_data_size_and_addralign()
4271{
4272 off_t size = 0;
4273 unsigned addralign = 1;
4274
4275 // Go over all stubs in table to compute data size and address alignment.
4276
4277 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4278 p != this->reloc_stubs_.end();
4279 ++p)
4280 {
4281 const Stub_template* stub_template = p->second->stub_template();
4282 addralign = std::max(addralign, stub_template->alignment());
4283 size = (align_address(size, stub_template->alignment())
4284 + stub_template->size());
4285 }
4286
4287 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4288 p != this->cortex_a8_stubs_.end();
4289 ++p)
4290 {
4291 const Stub_template* stub_template = p->second->stub_template();
4292 addralign = std::max(addralign, stub_template->alignment());
4293 size = (align_address(size, stub_template->alignment())
4294 + stub_template->size());
4295 }
4296
a2162063
ILT
4297 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4298 p != this->arm_v4bx_stubs_.end();
4299 ++p)
4300 {
4301 if (*p == NULL)
4302 continue;
4303
4304 const Stub_template* stub_template = (*p)->stub_template();
4305 addralign = std::max(addralign, stub_template->alignment());
4306 size = (align_address(size, stub_template->alignment())
4307 + stub_template->size());
4308 }
4309
2fb7225c
DK
4310 // Check if either data size or alignment changed in this pass.
4311 // Update prev_data_size_ and prev_addralign_. These will be used
4312 // as the current data size and address alignment for the next pass.
4313 bool changed = size != this->prev_data_size_;
4314 this->prev_data_size_ = size;
4315
4316 if (addralign != this->prev_addralign_)
4317 changed = true;
4318 this->prev_addralign_ = addralign;
4319
4320 return changed;
4321}
4322
4323// Finalize the stubs. This sets the offsets of the stubs within the stub
4324// table. It also marks all input sections needing Cortex-A8 workaround.
56ee5e00
DK
4325
4326template<bool big_endian>
4327void
2fb7225c 4328Stub_table<big_endian>::finalize_stubs()
56ee5e00
DK
4329{
4330 off_t off = 0;
56ee5e00
DK
4331 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4332 p != this->reloc_stubs_.end();
4333 ++p)
4334 {
4335 Reloc_stub* stub = p->second;
2ea97941
ILT
4336 const Stub_template* stub_template = stub->stub_template();
4337 uint64_t stub_addralign = stub_template->alignment();
56ee5e00
DK
4338 off = align_address(off, stub_addralign);
4339 stub->set_offset(off);
2ea97941 4340 off += stub_template->size();
56ee5e00
DK
4341 }
4342
2fb7225c
DK
4343 for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4344 p != this->cortex_a8_stubs_.end();
4345 ++p)
4346 {
4347 Cortex_a8_stub* stub = p->second;
4348 const Stub_template* stub_template = stub->stub_template();
4349 uint64_t stub_addralign = stub_template->alignment();
4350 off = align_address(off, stub_addralign);
4351 stub->set_offset(off);
4352 off += stub_template->size();
4353
4354 // Mark input section so that we can determine later if a code section
4355 // needs the Cortex-A8 workaround quickly.
4356 Arm_relobj<big_endian>* arm_relobj =
4357 Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
4358 arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
4359 }
4360
a2162063
ILT
4361 for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4362 p != this->arm_v4bx_stubs_.end();
4363 ++p)
4364 {
4365 if (*p == NULL)
4366 continue;
4367
4368 const Stub_template* stub_template = (*p)->stub_template();
4369 uint64_t stub_addralign = stub_template->alignment();
4370 off = align_address(off, stub_addralign);
4371 (*p)->set_offset(off);
4372 off += stub_template->size();
4373 }
4374
2fb7225c 4375 gold_assert(off <= this->prev_data_size_);
56ee5e00
DK
4376}
4377
2fb7225c
DK
4378// Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
4379// and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
4380// of the address range seen by the linker.
56ee5e00
DK
4381
4382template<bool big_endian>
4383void
2fb7225c
DK
4384Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
4385 Target_arm<big_endian>* arm_target,
4386 unsigned char* view,
4387 Arm_address view_address,
4388 section_size_type view_size)
56ee5e00 4389{
2fb7225c
DK
4390 // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
4391 for (Cortex_a8_stub_list::const_iterator p =
4392 this->cortex_a8_stubs_.lower_bound(view_address);
4393 ((p != this->cortex_a8_stubs_.end())
4394 && (p->first < (view_address + view_size)));
4395 ++p)
56ee5e00 4396 {
2fb7225c
DK
4397 // We do not store the THUMB bit in the LSB of either the branch address
4398 // or the stub offset. There is no need to strip the LSB.
4399 Arm_address branch_address = p->first;
4400 const Cortex_a8_stub* stub = p->second;
4401 Arm_address stub_address = this->address() + stub->offset();
4402
4403 // Offset of the branch instruction relative to this view.
4404 section_size_type offset =
4405 convert_to_section_size_type(branch_address - view_address);
4406 gold_assert((offset + 4) <= view_size);
4407
4408 arm_target->apply_cortex_a8_workaround(stub, stub_address,
4409 view + offset, branch_address);
4410 }
56ee5e00
DK
4411}
4412
10ad9fe5
DK
4413// Arm_input_section methods.
4414
4415// Initialize an Arm_input_section.
4416
4417template<bool big_endian>
4418void
4419Arm_input_section<big_endian>::init()
4420{
2ea97941
ILT
4421 Relobj* relobj = this->relobj();
4422 unsigned int shndx = this->shndx();
10ad9fe5
DK
4423
4424 // Cache these to speed up size and alignment queries. It is too slow
4425 // to call section_addraglin and section_size every time.
2ea97941
ILT
4426 this->original_addralign_ = relobj->section_addralign(shndx);
4427 this->original_size_ = relobj->section_size(shndx);
10ad9fe5
DK
4428
4429 // We want to make this look like the original input section after
4430 // output sections are finalized.
2ea97941
ILT
4431 Output_section* os = relobj->output_section(shndx);
4432 off_t offset = relobj->output_section_offset(shndx);
4433 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
4434 this->set_address(os->address() + offset);
4435 this->set_file_offset(os->offset() + offset);
10ad9fe5
DK
4436
4437 this->set_current_data_size(this->original_size_);
4438 this->finalize_data_size();
4439}
4440
4441template<bool big_endian>
4442void
4443Arm_input_section<big_endian>::do_write(Output_file* of)
4444{
4445 // We have to write out the original section content.
4446 section_size_type section_size;
4447 const unsigned char* section_contents =
4448 this->relobj()->section_contents(this->shndx(), &section_size, false);
4449 of->write(this->offset(), section_contents, section_size);
4450
4451 // If this owns a stub table and it is not empty, write it.
4452 if (this->is_stub_table_owner() && !this->stub_table_->empty())
4453 this->stub_table_->write(of);
4454}
4455
4456// Finalize data size.
4457
4458template<bool big_endian>
4459void
4460Arm_input_section<big_endian>::set_final_data_size()
4461{
4462 // If this owns a stub table, finalize its data size as well.
4463 if (this->is_stub_table_owner())
4464 {
2ea97941 4465 uint64_t address = this->address();
10ad9fe5
DK
4466
4467 // The stub table comes after the original section contents.
2ea97941
ILT
4468 address += this->original_size_;
4469 address = align_address(address, this->stub_table_->addralign());
4470 off_t offset = this->offset() + (address - this->address());
4471 this->stub_table_->set_address_and_file_offset(address, offset);
4472 address += this->stub_table_->data_size();
4473 gold_assert(address == this->address() + this->current_data_size());
10ad9fe5
DK
4474 }
4475
4476 this->set_data_size(this->current_data_size());
4477}
4478
4479// Reset address and file offset.
4480
4481template<bool big_endian>
4482void
4483Arm_input_section<big_endian>::do_reset_address_and_file_offset()
4484{
4485 // Size of the original input section contents.
4486 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
4487
4488 // If this is a stub table owner, account for the stub table size.
4489 if (this->is_stub_table_owner())
4490 {
2ea97941 4491 Stub_table<big_endian>* stub_table = this->stub_table_;
10ad9fe5
DK
4492
4493 // Reset the stub table's address and file offset. The
4494 // current data size for child will be updated after that.
4495 stub_table_->reset_address_and_file_offset();
4496 off = align_address(off, stub_table_->addralign());
2ea97941 4497 off += stub_table->current_data_size();
10ad9fe5
DK
4498 }
4499
4500 this->set_current_data_size(off);
4501}
4502
af2cdeae
DK
4503// Arm_exidx_cantunwind methods.
4504
4505// Write this to Output file OF for a fixed endianity.
4506
4507template<bool big_endian>
4508void
4509Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
4510{
4511 off_t offset = this->offset();
4512 const section_size_type oview_size = 8;
4513 unsigned char* const oview = of->get_output_view(offset, oview_size);
4514
4515 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4516 Valtype* wv = reinterpret_cast<Valtype*>(oview);
4517
4518 Output_section* os = this->relobj_->output_section(this->shndx_);
4519 gold_assert(os != NULL);
4520
4521 Arm_relobj<big_endian>* arm_relobj =
4522 Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
4523 Arm_address output_offset =
4524 arm_relobj->get_output_section_offset(this->shndx_);
4525 Arm_address section_start;
4526 if(output_offset != Arm_relobj<big_endian>::invalid_address)
4527 section_start = os->address() + output_offset;
4528 else
4529 {
4530 // Currently this only happens for a relaxed section.
4531 const Output_relaxed_input_section* poris =
4532 os->find_relaxed_input_section(this->relobj_, this->shndx_);
4533 gold_assert(poris != NULL);
4534 section_start = poris->address();
4535 }
4536
4537 // We always append this to the end of an EXIDX section.
4538 Arm_address output_address =
4539 section_start + this->relobj_->section_size(this->shndx_);
4540
4541 // Write out the entry. The first word either points to the beginning
4542 // or after the end of a text section. The second word is the special
4543 // EXIDX_CANTUNWIND value.
4544 elfcpp::Swap<32, big_endian>::writeval(wv, output_address);
4545 elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
4546
4547 of->write_output_view(this->offset(), oview_size, oview);
4548}
4549
4550// Arm_exidx_merged_section methods.
4551
4552// Constructor for Arm_exidx_merged_section.
4553// EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
4554// SECTION_OFFSET_MAP points to a section offset map describing how
4555// parts of the input section are mapped to output. DELETED_BYTES is
4556// the number of bytes deleted from the EXIDX input section.
4557
4558Arm_exidx_merged_section::Arm_exidx_merged_section(
4559 const Arm_exidx_input_section& exidx_input_section,
4560 const Arm_exidx_section_offset_map& section_offset_map,
4561 uint32_t deleted_bytes)
4562 : Output_relaxed_input_section(exidx_input_section.relobj(),
4563 exidx_input_section.shndx(),
4564 exidx_input_section.addralign()),
4565 exidx_input_section_(exidx_input_section),
4566 section_offset_map_(section_offset_map)
4567{
4568 // Fix size here so that we do not need to implement set_final_data_size.
4569 this->set_data_size(exidx_input_section.size() - deleted_bytes);
4570 this->fix_data_size();
4571}
4572
4573// Given an input OBJECT, an input section index SHNDX within that
4574// object, and an OFFSET relative to the start of that input
4575// section, return whether or not the corresponding offset within
4576// the output section is known. If this function returns true, it
4577// sets *POUTPUT to the output offset. The value -1 indicates that
4578// this input offset is being discarded.
4579
4580bool
4581Arm_exidx_merged_section::do_output_offset(
4582 const Relobj* relobj,
4583 unsigned int shndx,
4584 section_offset_type offset,
4585 section_offset_type* poutput) const
4586{
4587 // We only handle offsets for the original EXIDX input section.
4588 if (relobj != this->exidx_input_section_.relobj()
4589 || shndx != this->exidx_input_section_.shndx())
4590 return false;
4591
4592 if (offset < 0 || offset >= this->exidx_input_section_.size())
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.
6454 if (this->fix_v4bx() == 2 && !this->may_use_blx())
6455 gold_error(_("unable to provide V4BX reloc interworking fix up; "
6456 "the target profile does not support BX instruction"));
6457
94cdfcff 6458 // Fill in some more dynamic tags.
ea715a34
ILT
6459 const Reloc_section* rel_plt = (this->plt_ == NULL
6460 ? NULL
6461 : this->plt_->rel_plt());
6462 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
6463 this->rel_dyn_, true);
94cdfcff
DK
6464
6465 // Emit any relocs we saved in an attempt to avoid generating COPY
6466 // relocs.
6467 if (this->copy_relocs_.any_saved_relocs())
2ea97941 6468 this->copy_relocs_.emit(this->rel_dyn_section(layout));
11af873f 6469
f59f41f3 6470 // Handle the .ARM.exidx section.
2ea97941 6471 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
f59f41f3
DK
6472 if (exidx_section != NULL
6473 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
11af873f
DK
6474 && !parameters->options().relocatable())
6475 {
f59f41f3 6476 // Create __exidx_start and __exdix_end symbols.
99fff23b
ILT
6477 symtab->define_in_output_data("__exidx_start", NULL,
6478 Symbol_table::PREDEFINED,
6479 exidx_section, 0, 0, elfcpp::STT_OBJECT,
a0351a69 6480 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
99e5bff2 6481 false, true);
99fff23b
ILT
6482 symtab->define_in_output_data("__exidx_end", NULL,
6483 Symbol_table::PREDEFINED,
6484 exidx_section, 0, 0, elfcpp::STT_OBJECT,
a0351a69 6485 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
99e5bff2 6486 true, true);
11af873f 6487
f59f41f3
DK
6488 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
6489 // the .ARM.exidx section.
2ea97941 6490 if (!layout->script_options()->saw_phdrs_clause())
11af873f 6491 {
2ea97941 6492 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
11af873f
DK
6493 == NULL);
6494 Output_segment* exidx_segment =
2ea97941 6495 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
f5c870d2
ILT
6496 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
6497 false);
11af873f
DK
6498 }
6499 }
a0351a69
DK
6500
6501 // Create an .ARM.attributes section if there is not one already.
2ea97941 6502 Output_attributes_section_data* attributes_section =
a0351a69 6503 new Output_attributes_section_data(*this->attributes_section_data_);
2ea97941
ILT
6504 layout->add_output_section_data(".ARM.attributes",
6505 elfcpp::SHT_ARM_ATTRIBUTES, 0,
1a2dff53
ILT
6506 attributes_section, false, false, false,
6507 false);
4a657b0d
DK
6508}
6509
bec53400
DK
6510// Return whether a direct absolute static relocation needs to be applied.
6511// In cases where Scan::local() or Scan::global() has created
6512// a dynamic relocation other than R_ARM_RELATIVE, the addend
6513// of the relocation is carried in the data, and we must not
6514// apply the static relocation.
6515
6516template<bool big_endian>
6517inline bool
6518Target_arm<big_endian>::Relocate::should_apply_static_reloc(
6519 const Sized_symbol<32>* gsym,
6520 int ref_flags,
6521 bool is_32bit,
6522 Output_section* output_section)
6523{
6524 // If the output section is not allocated, then we didn't call
6525 // scan_relocs, we didn't create a dynamic reloc, and we must apply
6526 // the reloc here.
6527 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
6528 return true;
6529
6530 // For local symbols, we will have created a non-RELATIVE dynamic
6531 // relocation only if (a) the output is position independent,
6532 // (b) the relocation is absolute (not pc- or segment-relative), and
6533 // (c) the relocation is not 32 bits wide.
6534 if (gsym == NULL)
6535 return !(parameters->options().output_is_position_independent()
6536 && (ref_flags & Symbol::ABSOLUTE_REF)
6537 && !is_32bit);
6538
6539 // For global symbols, we use the same helper routines used in the
6540 // scan pass. If we did not create a dynamic relocation, or if we
6541 // created a RELATIVE dynamic relocation, we should apply the static
6542 // relocation.
6543 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
6544 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
6545 && gsym->can_use_relative_reloc(ref_flags
6546 & Symbol::FUNCTION_CALL);
6547 return !has_dyn || is_rel;
6548}
6549
4a657b0d
DK
6550// Perform a relocation.
6551
6552template<bool big_endian>
6553inline bool
6554Target_arm<big_endian>::Relocate::relocate(
c121c671
DK
6555 const Relocate_info<32, big_endian>* relinfo,
6556 Target_arm* target,
6557 Output_section *output_section,
6558 size_t relnum,
6559 const elfcpp::Rel<32, big_endian>& rel,
4a657b0d 6560 unsigned int r_type,
c121c671
DK
6561 const Sized_symbol<32>* gsym,
6562 const Symbol_value<32>* psymval,
6563 unsigned char* view,
ebabffbd 6564 Arm_address address,
4a657b0d
DK
6565 section_size_type /* view_size */ )
6566{
c121c671
DK
6567 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
6568
a6d1ef57 6569 r_type = get_real_reloc_type(r_type);
c121c671 6570
2daedcd6
DK
6571 const Arm_relobj<big_endian>* object =
6572 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
c121c671 6573
2daedcd6
DK
6574 // If the final branch target of a relocation is THUMB instruction, this
6575 // is 1. Otherwise it is 0.
6576 Arm_address thumb_bit = 0;
c121c671 6577 Symbol_value<32> symval;
d204b6e9 6578 bool is_weakly_undefined_without_plt = false;
2daedcd6 6579 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
c121c671 6580 {
2daedcd6
DK
6581 if (gsym != NULL)
6582 {
6583 // This is a global symbol. Determine if we use PLT and if the
6584 // final target is THUMB.
6585 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
6586 {
6587 // This uses a PLT, change the symbol value.
6588 symval.set_output_value(target->plt_section()->address()
6589 + gsym->plt_offset());
6590 psymval = &symval;
6591 }
d204b6e9
DK
6592 else if (gsym->is_weak_undefined())
6593 {
6594 // This is a weakly undefined symbol and we do not use PLT
6595 // for this relocation. A branch targeting this symbol will
6596 // be converted into an NOP.
6597 is_weakly_undefined_without_plt = true;
6598 }
2daedcd6
DK
6599 else
6600 {
6601 // Set thumb bit if symbol:
6602 // -Has type STT_ARM_TFUNC or
6603 // -Has type STT_FUNC, is defined and with LSB in value set.
6604 thumb_bit =
6605 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
6606 || (gsym->type() == elfcpp::STT_FUNC
6607 && !gsym->is_undefined()
6608 && ((psymval->value(object, 0) & 1) != 0)))
6609 ? 1
6610 : 0);
6611 }
6612 }
6613 else
6614 {
6615 // This is a local symbol. Determine if the final target is THUMB.
6616 // We saved this information when all the local symbols were read.
6617 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
6618 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6619 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
6620 }
6621 }
6622 else
6623 {
6624 // This is a fake relocation synthesized for a stub. It does not have
6625 // a real symbol. We just look at the LSB of the symbol value to
6626 // determine if the target is THUMB or not.
6627 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
c121c671
DK
6628 }
6629
2daedcd6
DK
6630 // Strip LSB if this points to a THUMB target.
6631 if (thumb_bit != 0
6632 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
6633 && ((psymval->value(object, 0) & 1) != 0))
6634 {
6635 Arm_address stripped_value =
6636 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
6637 symval.set_output_value(stripped_value);
6638 psymval = &symval;
6639 }
6640
c121c671
DK
6641 // Get the GOT offset if needed.
6642 // The GOT pointer points to the end of the GOT section.
6643 // We need to subtract the size of the GOT section to get
6644 // the actual offset to use in the relocation.
6645 bool have_got_offset = false;
6646 unsigned int got_offset = 0;
6647 switch (r_type)
6648 {
6649 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 6650 case elfcpp::R_ARM_GOT_PREL:
c121c671
DK
6651 if (gsym != NULL)
6652 {
6653 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6654 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
6655 - target->got_size());
6656 }
6657 else
6658 {
6659 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
6660 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6661 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
6662 - target->got_size());
6663 }
6664 have_got_offset = true;
6665 break;
6666
6667 default:
6668 break;
6669 }
6670
d204b6e9
DK
6671 // To look up relocation stubs, we need to pass the symbol table index of
6672 // a local symbol.
6673 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
6674
c121c671
DK
6675 typename Arm_relocate_functions::Status reloc_status =
6676 Arm_relocate_functions::STATUS_OKAY;
4a657b0d
DK
6677 switch (r_type)
6678 {
6679 case elfcpp::R_ARM_NONE:
6680 break;
6681
5e445df6
ILT
6682 case elfcpp::R_ARM_ABS8:
6683 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6684 output_section))
be8fcb75
ILT
6685 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
6686 break;
6687
6688 case elfcpp::R_ARM_ABS12:
6689 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6690 output_section))
6691 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
6692 break;
6693
6694 case elfcpp::R_ARM_ABS16:
6695 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6696 output_section))
6697 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5e445df6
ILT
6698 break;
6699
c121c671
DK
6700 case elfcpp::R_ARM_ABS32:
6701 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6702 output_section))
6703 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2daedcd6 6704 thumb_bit);
c121c671
DK
6705 break;
6706
be8fcb75
ILT
6707 case elfcpp::R_ARM_ABS32_NOI:
6708 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6709 output_section))
6710 // No thumb bit for this relocation: (S + A)
6711 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
f4e5969c 6712 0);
be8fcb75
ILT
6713 break;
6714
fd3c5f0b
ILT
6715 case elfcpp::R_ARM_MOVW_ABS_NC:
6716 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6717 output_section))
6718 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
6719 psymval,
2daedcd6 6720 thumb_bit);
fd3c5f0b
ILT
6721 else
6722 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
6723 "a shared object; recompile with -fPIC"));
6724 break;
6725
6726 case elfcpp::R_ARM_MOVT_ABS:
6727 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6728 output_section))
6729 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
6730 else
6731 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
6732 "a shared object; recompile with -fPIC"));
6733 break;
6734
6735 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6736 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6737 output_section))
6738 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
6739 psymval,
2daedcd6 6740 thumb_bit);
fd3c5f0b
ILT
6741 else
6742 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
6743 "making a shared object; recompile with -fPIC"));
6744 break;
6745
6746 case elfcpp::R_ARM_THM_MOVT_ABS:
6747 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6748 output_section))
6749 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
6750 psymval);
6751 else
6752 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
6753 "making a shared object; recompile with -fPIC"));
6754 break;
6755
c2a122b6
ILT
6756 case elfcpp::R_ARM_MOVW_PREL_NC:
6757 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
6758 psymval, address,
2daedcd6 6759 thumb_bit);
c2a122b6
ILT
6760 break;
6761
6762 case elfcpp::R_ARM_MOVT_PREL:
6763 reloc_status = Arm_relocate_functions::movt_prel(view, object,
6764 psymval, address);
6765 break;
6766
6767 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6768 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
6769 psymval, address,
2daedcd6 6770 thumb_bit);
c2a122b6
ILT
6771 break;
6772
6773 case elfcpp::R_ARM_THM_MOVT_PREL:
6774 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
6775 psymval, address);
6776 break;
6777
c121c671
DK
6778 case elfcpp::R_ARM_REL32:
6779 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2daedcd6 6780 address, thumb_bit);
c121c671
DK
6781 break;
6782
be8fcb75
ILT
6783 case elfcpp::R_ARM_THM_ABS5:
6784 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
6785 output_section))
6786 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
6787 break;
6788
c121c671 6789 case elfcpp::R_ARM_THM_CALL:
51938283
DK
6790 reloc_status =
6791 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
6792 psymval, address, thumb_bit,
6793 is_weakly_undefined_without_plt);
c121c671
DK
6794 break;
6795
d204b6e9
DK
6796 case elfcpp::R_ARM_XPC25:
6797 reloc_status =
6798 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
6799 psymval, address, thumb_bit,
6800 is_weakly_undefined_without_plt);
6801 break;
6802
51938283
DK
6803 case elfcpp::R_ARM_THM_XPC22:
6804 reloc_status =
6805 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
6806 psymval, address, thumb_bit,
6807 is_weakly_undefined_without_plt);
6808 break;
6809
c121c671
DK
6810 case elfcpp::R_ARM_GOTOFF32:
6811 {
ebabffbd 6812 Arm_address got_origin;
c121c671
DK
6813 got_origin = target->got_plt_section()->address();
6814 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2daedcd6 6815 got_origin, thumb_bit);
c121c671
DK
6816 }
6817 break;
6818
6819 case elfcpp::R_ARM_BASE_PREL:
6820 {
6821 uint32_t origin;
6822 // Get the addressing origin of the output segment defining the
6823 // symbol gsym (AAELF 4.6.1.2 Relocation types)
6824 gold_assert(gsym != NULL);
6825 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
6826 origin = gsym->output_segment()->vaddr();
6827 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
6828 origin = gsym->output_data()->address();
6829 else
6830 {
6831 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6832 _("cannot find origin of R_ARM_BASE_PREL"));
6833 return true;
6834 }
6835 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
6836 }
6837 break;
6838
be8fcb75
ILT
6839 case elfcpp::R_ARM_BASE_ABS:
6840 {
6841 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
6842 output_section))
6843 break;
6844
6845 uint32_t origin;
6846 // Get the addressing origin of the output segment defining
6847 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
6848 if (gsym == NULL)
6849 // R_ARM_BASE_ABS with the NULL symbol will give the
6850 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
6851 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
6852 origin = target->got_plt_section()->address();
6853 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
6854 origin = gsym->output_segment()->vaddr();
6855 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
6856 origin = gsym->output_data()->address();
6857 else
6858 {
6859 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6860 _("cannot find origin of R_ARM_BASE_ABS"));
6861 return true;
6862 }
6863
6864 reloc_status = Arm_relocate_functions::base_abs(view, origin);
6865 }
6866 break;
6867
c121c671
DK
6868 case elfcpp::R_ARM_GOT_BREL:
6869 gold_assert(have_got_offset);
6870 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
6871 break;
6872
7f5309a5
ILT
6873 case elfcpp::R_ARM_GOT_PREL:
6874 gold_assert(have_got_offset);
6875 // Get the address origin for GOT PLT, which is allocated right
6876 // after the GOT section, to calculate an absolute address of
6877 // the symbol GOT entry (got_origin + got_offset).
ebabffbd 6878 Arm_address got_origin;
7f5309a5
ILT
6879 got_origin = target->got_plt_section()->address();
6880 reloc_status = Arm_relocate_functions::got_prel(view,
6881 got_origin + got_offset,
6882 address);
6883 break;
6884
c121c671
DK
6885 case elfcpp::R_ARM_PLT32:
6886 gold_assert(gsym == NULL
6887 || gsym->has_plt_offset()
6888 || gsym->final_value_is_known()
6889 || (gsym->is_defined()
6890 && !gsym->is_from_dynobj()
6891 && !gsym->is_preemptible()));
d204b6e9
DK
6892 reloc_status =
6893 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
6894 psymval, address, thumb_bit,
6895 is_weakly_undefined_without_plt);
c121c671
DK
6896 break;
6897
6898 case elfcpp::R_ARM_CALL:
d204b6e9
DK
6899 reloc_status =
6900 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
6901 psymval, address, thumb_bit,
6902 is_weakly_undefined_without_plt);
c121c671
DK
6903 break;
6904
6905 case elfcpp::R_ARM_JUMP24:
d204b6e9
DK
6906 reloc_status =
6907 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
6908 psymval, address, thumb_bit,
6909 is_weakly_undefined_without_plt);
c121c671
DK
6910 break;
6911
51938283
DK
6912 case elfcpp::R_ARM_THM_JUMP24:
6913 reloc_status =
6914 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
6915 psymval, address, thumb_bit,
6916 is_weakly_undefined_without_plt);
6917 break;
6918
41263c05
DK
6919 case elfcpp::R_ARM_THM_JUMP19:
6920 reloc_status =
6921 Arm_relocate_functions::thm_jump19(view, object, psymval, address,
6922 thumb_bit);
6923 break;
6924
800d0f56
ILT
6925 case elfcpp::R_ARM_THM_JUMP6:
6926 reloc_status =
6927 Arm_relocate_functions::thm_jump6(view, object, psymval, address);
6928 break;
6929
6930 case elfcpp::R_ARM_THM_JUMP8:
6931 reloc_status =
6932 Arm_relocate_functions::thm_jump8(view, object, psymval, address);
6933 break;
6934
6935 case elfcpp::R_ARM_THM_JUMP11:
6936 reloc_status =
6937 Arm_relocate_functions::thm_jump11(view, object, psymval, address);
6938 break;
6939
c121c671
DK
6940 case elfcpp::R_ARM_PREL31:
6941 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
2daedcd6 6942 address, thumb_bit);
c121c671
DK
6943 break;
6944
a2162063
ILT
6945 case elfcpp::R_ARM_V4BX:
6946 if (target->fix_v4bx() > 0)
6947 reloc_status =
6948 Arm_relocate_functions::v4bx(relinfo, view, object, address,
6949 (target->fix_v4bx() == 2));
6950 break;
6951
c121c671
DK
6952 case elfcpp::R_ARM_TARGET1:
6953 // This should have been mapped to another type already.
6954 // Fall through.
6955 case elfcpp::R_ARM_COPY:
6956 case elfcpp::R_ARM_GLOB_DAT:
6957 case elfcpp::R_ARM_JUMP_SLOT:
6958 case elfcpp::R_ARM_RELATIVE:
6959 // These are relocations which should only be seen by the
6960 // dynamic linker, and should never be seen here.
6961 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6962 _("unexpected reloc %u in object file"),
6963 r_type);
6964 break;
6965
6966 default:
6967 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6968 _("unsupported reloc %u"),
6969 r_type);
6970 break;
6971 }
6972
6973 // Report any errors.
6974 switch (reloc_status)
6975 {
6976 case Arm_relocate_functions::STATUS_OKAY:
6977 break;
6978 case Arm_relocate_functions::STATUS_OVERFLOW:
6979 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
6980 _("relocation overflow in relocation %u"),
6981 r_type);
6982 break;
6983 case Arm_relocate_functions::STATUS_BAD_RELOC:
6984 gold_error_at_location(
6985 relinfo,
6986 relnum,
6987 rel.get_r_offset(),
6988 _("unexpected opcode while processing relocation %u"),
6989 r_type);
6990 break;
4a657b0d
DK
6991 default:
6992 gold_unreachable();
6993 }
6994
6995 return true;
6996}
6997
6998// Relocate section data.
6999
7000template<bool big_endian>
7001void
7002Target_arm<big_endian>::relocate_section(
7003 const Relocate_info<32, big_endian>* relinfo,
7004 unsigned int sh_type,
7005 const unsigned char* prelocs,
7006 size_t reloc_count,
7007 Output_section* output_section,
7008 bool needs_special_offset_handling,
7009 unsigned char* view,
ebabffbd 7010 Arm_address address,
364c7fa5
ILT
7011 section_size_type view_size,
7012 const Reloc_symbol_changes* reloc_symbol_changes)
4a657b0d
DK
7013{
7014 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
7015 gold_assert(sh_type == elfcpp::SHT_REL);
7016
43d12afe
DK
7017 Arm_input_section<big_endian>* arm_input_section =
7018 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
7019
7020 // This is an ARM input section and the view covers the whole output
7021 // section.
7022 if (arm_input_section != NULL)
7023 {
7024 gold_assert(needs_special_offset_handling);
7025 Arm_address section_address = arm_input_section->address();
7026 section_size_type section_size = arm_input_section->data_size();
7027
7028 gold_assert((arm_input_section->address() >= address)
7029 && ((arm_input_section->address()
7030 + arm_input_section->data_size())
7031 <= (address + view_size)));
7032
2ea97941
ILT
7033 off_t offset = section_address - address;
7034 view += offset;
7035 address += offset;
43d12afe
DK
7036 view_size = section_size;
7037 }
7038
4a657b0d
DK
7039 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
7040 Arm_relocate>(
7041 relinfo,
7042 this,
7043 prelocs,
7044 reloc_count,
7045 output_section,
7046 needs_special_offset_handling,
7047 view,
7048 address,
364c7fa5
ILT
7049 view_size,
7050 reloc_symbol_changes);
4a657b0d
DK
7051}
7052
7053// Return the size of a relocation while scanning during a relocatable
7054// link.
7055
7056template<bool big_endian>
7057unsigned int
7058Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
7059 unsigned int r_type,
7060 Relobj* object)
7061{
a6d1ef57 7062 r_type = get_real_reloc_type(r_type);
4a657b0d
DK
7063 switch (r_type)
7064 {
7065 case elfcpp::R_ARM_NONE:
7066 return 0;
7067
5e445df6
ILT
7068 case elfcpp::R_ARM_ABS8:
7069 return 1;
7070
be8fcb75
ILT
7071 case elfcpp::R_ARM_ABS16:
7072 case elfcpp::R_ARM_THM_ABS5:
800d0f56
ILT
7073 case elfcpp::R_ARM_THM_JUMP6:
7074 case elfcpp::R_ARM_THM_JUMP8:
7075 case elfcpp::R_ARM_THM_JUMP11:
be8fcb75
ILT
7076 return 2;
7077
4a657b0d 7078 case elfcpp::R_ARM_ABS32:
be8fcb75
ILT
7079 case elfcpp::R_ARM_ABS32_NOI:
7080 case elfcpp::R_ARM_ABS12:
7081 case elfcpp::R_ARM_BASE_ABS:
4a657b0d
DK
7082 case elfcpp::R_ARM_REL32:
7083 case elfcpp::R_ARM_THM_CALL:
7084 case elfcpp::R_ARM_GOTOFF32:
7085 case elfcpp::R_ARM_BASE_PREL:
7086 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 7087 case elfcpp::R_ARM_GOT_PREL:
4a657b0d
DK
7088 case elfcpp::R_ARM_PLT32:
7089 case elfcpp::R_ARM_CALL:
7090 case elfcpp::R_ARM_JUMP24:
7091 case elfcpp::R_ARM_PREL31:
fd3c5f0b
ILT
7092 case elfcpp::R_ARM_MOVW_ABS_NC:
7093 case elfcpp::R_ARM_MOVT_ABS:
7094 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7095 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
7096 case elfcpp::R_ARM_MOVW_PREL_NC:
7097 case elfcpp::R_ARM_MOVT_PREL:
7098 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7099 case elfcpp::R_ARM_THM_MOVT_PREL:
a2162063 7100 case elfcpp::R_ARM_V4BX:
4a657b0d
DK
7101 return 4;
7102
7103 case elfcpp::R_ARM_TARGET1:
7104 // This should have been mapped to another type already.
7105 // Fall through.
7106 case elfcpp::R_ARM_COPY:
7107 case elfcpp::R_ARM_GLOB_DAT:
7108 case elfcpp::R_ARM_JUMP_SLOT:
7109 case elfcpp::R_ARM_RELATIVE:
7110 // These are relocations which should only be seen by the
7111 // dynamic linker, and should never be seen here.
7112 gold_error(_("%s: unexpected reloc %u in object file"),
7113 object->name().c_str(), r_type);
7114 return 0;
7115
7116 default:
7117 object->error(_("unsupported reloc %u in object file"), r_type);
7118 return 0;
7119 }
7120}
7121
7122// Scan the relocs during a relocatable link.
7123
7124template<bool big_endian>
7125void
7126Target_arm<big_endian>::scan_relocatable_relocs(
4a657b0d 7127 Symbol_table* symtab,
2ea97941 7128 Layout* layout,
4a657b0d
DK
7129 Sized_relobj<32, big_endian>* object,
7130 unsigned int data_shndx,
7131 unsigned int sh_type,
7132 const unsigned char* prelocs,
7133 size_t reloc_count,
7134 Output_section* output_section,
7135 bool needs_special_offset_handling,
7136 size_t local_symbol_count,
7137 const unsigned char* plocal_symbols,
7138 Relocatable_relocs* rr)
7139{
7140 gold_assert(sh_type == elfcpp::SHT_REL);
7141
7142 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
7143 Relocatable_size_for_reloc> Scan_relocatable_relocs;
7144
7145 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
7146 Scan_relocatable_relocs>(
4a657b0d 7147 symtab,
2ea97941 7148 layout,
4a657b0d
DK
7149 object,
7150 data_shndx,
7151 prelocs,
7152 reloc_count,
7153 output_section,
7154 needs_special_offset_handling,
7155 local_symbol_count,
7156 plocal_symbols,
7157 rr);
7158}
7159
7160// Relocate a section during a relocatable link.
7161
7162template<bool big_endian>
7163void
7164Target_arm<big_endian>::relocate_for_relocatable(
7165 const Relocate_info<32, big_endian>* relinfo,
7166 unsigned int sh_type,
7167 const unsigned char* prelocs,
7168 size_t reloc_count,
7169 Output_section* output_section,
7170 off_t offset_in_output_section,
7171 const Relocatable_relocs* rr,
7172 unsigned char* view,
ebabffbd 7173 Arm_address view_address,
4a657b0d
DK
7174 section_size_type view_size,
7175 unsigned char* reloc_view,
7176 section_size_type reloc_view_size)
7177{
7178 gold_assert(sh_type == elfcpp::SHT_REL);
7179
7180 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
7181 relinfo,
7182 prelocs,
7183 reloc_count,
7184 output_section,
7185 offset_in_output_section,
7186 rr,
7187 view,
7188 view_address,
7189 view_size,
7190 reloc_view,
7191 reloc_view_size);
7192}
7193
94cdfcff
DK
7194// Return the value to use for a dynamic symbol which requires special
7195// treatment. This is how we support equality comparisons of function
7196// pointers across shared library boundaries, as described in the
7197// processor specific ABI supplement.
7198
4a657b0d
DK
7199template<bool big_endian>
7200uint64_t
94cdfcff 7201Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4a657b0d 7202{
94cdfcff
DK
7203 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
7204 return this->plt_section()->address() + gsym->plt_offset();
4a657b0d
DK
7205}
7206
7207// Map platform-specific relocs to real relocs
7208//
7209template<bool big_endian>
7210unsigned int
a6d1ef57 7211Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4a657b0d
DK
7212{
7213 switch (r_type)
7214 {
7215 case elfcpp::R_ARM_TARGET1:
a6d1ef57
DK
7216 // This is either R_ARM_ABS32 or R_ARM_REL32;
7217 return elfcpp::R_ARM_ABS32;
4a657b0d
DK
7218
7219 case elfcpp::R_ARM_TARGET2:
a6d1ef57
DK
7220 // This can be any reloc type but ususally is R_ARM_GOT_PREL
7221 return elfcpp::R_ARM_GOT_PREL;
4a657b0d
DK
7222
7223 default:
7224 return r_type;
7225 }
7226}
7227
d5b40221
DK
7228// Whether if two EABI versions V1 and V2 are compatible.
7229
7230template<bool big_endian>
7231bool
7232Target_arm<big_endian>::are_eabi_versions_compatible(
7233 elfcpp::Elf_Word v1,
7234 elfcpp::Elf_Word v2)
7235{
7236 // v4 and v5 are the same spec before and after it was released,
7237 // so allow mixing them.
7238 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
7239 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
7240 return true;
7241
7242 return v1 == v2;
7243}
7244
7245// Combine FLAGS from an input object called NAME and the processor-specific
7246// flags in the ELF header of the output. Much of this is adapted from the
7247// processor-specific flags merging code in elf32_arm_merge_private_bfd_data
7248// in bfd/elf32-arm.c.
7249
7250template<bool big_endian>
7251void
7252Target_arm<big_endian>::merge_processor_specific_flags(
7253 const std::string& name,
7254 elfcpp::Elf_Word flags)
7255{
7256 if (this->are_processor_specific_flags_set())
7257 {
7258 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
7259
7260 // Nothing to merge if flags equal to those in output.
7261 if (flags == out_flags)
7262 return;
7263
7264 // Complain about various flag mismatches.
7265 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
7266 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
7267 if (!this->are_eabi_versions_compatible(version1, version2))
7268 gold_error(_("Source object %s has EABI version %d but output has "
7269 "EABI version %d."),
7270 name.c_str(),
7271 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
7272 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
7273 }
7274 else
7275 {
7276 // If the input is the default architecture and had the default
7277 // flags then do not bother setting the flags for the output
7278 // architecture, instead allow future merges to do this. If no
7279 // future merges ever set these flags then they will retain their
7280 // uninitialised values, which surprise surprise, correspond
7281 // to the default values.
7282 if (flags == 0)
7283 return;
7284
7285 // This is the first time, just copy the flags.
7286 // We only copy the EABI version for now.
7287 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
7288 }
7289}
7290
7291// Adjust ELF file header.
7292template<bool big_endian>
7293void
7294Target_arm<big_endian>::do_adjust_elf_header(
7295 unsigned char* view,
7296 int len) const
7297{
7298 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
7299
7300 elfcpp::Ehdr<32, big_endian> ehdr(view);
7301 unsigned char e_ident[elfcpp::EI_NIDENT];
7302 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
7303
7304 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
7305 == elfcpp::EF_ARM_EABI_UNKNOWN)
7306 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
7307 else
7308 e_ident[elfcpp::EI_OSABI] = 0;
7309 e_ident[elfcpp::EI_ABIVERSION] = 0;
7310
7311 // FIXME: Do EF_ARM_BE8 adjustment.
7312
7313 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
7314 oehdr.put_e_ident(e_ident);
7315}
7316
7317// do_make_elf_object to override the same function in the base class.
7318// We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
7319// to store ARM specific information. Hence we need to have our own
7320// ELF object creation.
7321
7322template<bool big_endian>
7323Object*
7324Target_arm<big_endian>::do_make_elf_object(
7325 const std::string& name,
7326 Input_file* input_file,
2ea97941 7327 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
d5b40221
DK
7328{
7329 int et = ehdr.get_e_type();
7330 if (et == elfcpp::ET_REL)
7331 {
7332 Arm_relobj<big_endian>* obj =
2ea97941 7333 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
d5b40221
DK
7334 obj->setup();
7335 return obj;
7336 }
7337 else if (et == elfcpp::ET_DYN)
7338 {
7339 Sized_dynobj<32, big_endian>* obj =
2ea97941 7340 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
d5b40221
DK
7341 obj->setup();
7342 return obj;
7343 }
7344 else
7345 {
7346 gold_error(_("%s: unsupported ELF file type %d"),
7347 name.c_str(), et);
7348 return NULL;
7349 }
7350}
7351
a0351a69
DK
7352// Read the architecture from the Tag_also_compatible_with attribute, if any.
7353// Returns -1 if no architecture could be read.
7354// This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
7355
7356template<bool big_endian>
7357int
7358Target_arm<big_endian>::get_secondary_compatible_arch(
7359 const Attributes_section_data* pasd)
7360{
7361 const Object_attribute *known_attributes =
7362 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
7363
7364 // Note: the tag and its argument below are uleb128 values, though
7365 // currently-defined values fit in one byte for each.
7366 const std::string& sv =
7367 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
7368 if (sv.size() == 2
7369 && sv.data()[0] == elfcpp::Tag_CPU_arch
7370 && (sv.data()[1] & 128) != 128)
7371 return sv.data()[1];
7372
7373 // This tag is "safely ignorable", so don't complain if it looks funny.
7374 return -1;
7375}
7376
7377// Set, or unset, the architecture of the Tag_also_compatible_with attribute.
7378// The tag is removed if ARCH is -1.
7379// This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
7380
7381template<bool big_endian>
7382void
7383Target_arm<big_endian>::set_secondary_compatible_arch(
7384 Attributes_section_data* pasd,
7385 int arch)
7386{
7387 Object_attribute *known_attributes =
7388 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
7389
7390 if (arch == -1)
7391 {
7392 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
7393 return;
7394 }
7395
7396 // Note: the tag and its argument below are uleb128 values, though
7397 // currently-defined values fit in one byte for each.
7398 char sv[3];
7399 sv[0] = elfcpp::Tag_CPU_arch;
7400 gold_assert(arch != 0);
7401 sv[1] = arch;
7402 sv[2] = '\0';
7403
7404 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
7405}
7406
7407// Combine two values for Tag_CPU_arch, taking secondary compatibility tags
7408// into account.
7409// This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
7410
7411template<bool big_endian>
7412int
7413Target_arm<big_endian>::tag_cpu_arch_combine(
7414 const char* name,
7415 int oldtag,
7416 int* secondary_compat_out,
7417 int newtag,
7418 int secondary_compat)
7419{
7420#define T(X) elfcpp::TAG_CPU_ARCH_##X
7421 static const int v6t2[] =
7422 {
7423 T(V6T2), // PRE_V4.
7424 T(V6T2), // V4.
7425 T(V6T2), // V4T.
7426 T(V6T2), // V5T.
7427 T(V6T2), // V5TE.
7428 T(V6T2), // V5TEJ.
7429 T(V6T2), // V6.
7430 T(V7), // V6KZ.
7431 T(V6T2) // V6T2.
7432 };
7433 static const int v6k[] =
7434 {
7435 T(V6K), // PRE_V4.
7436 T(V6K), // V4.
7437 T(V6K), // V4T.
7438 T(V6K), // V5T.
7439 T(V6K), // V5TE.
7440 T(V6K), // V5TEJ.
7441 T(V6K), // V6.
7442 T(V6KZ), // V6KZ.
7443 T(V7), // V6T2.
7444 T(V6K) // V6K.
7445 };
7446 static const int v7[] =
7447 {
7448 T(V7), // PRE_V4.
7449 T(V7), // V4.
7450 T(V7), // V4T.
7451 T(V7), // V5T.
7452 T(V7), // V5TE.
7453 T(V7), // V5TEJ.
7454 T(V7), // V6.
7455 T(V7), // V6KZ.
7456 T(V7), // V6T2.
7457 T(V7), // V6K.
7458 T(V7) // V7.
7459 };
7460 static const int v6_m[] =
7461 {
7462 -1, // PRE_V4.
7463 -1, // V4.
7464 T(V6K), // V4T.
7465 T(V6K), // V5T.
7466 T(V6K), // V5TE.
7467 T(V6K), // V5TEJ.
7468 T(V6K), // V6.
7469 T(V6KZ), // V6KZ.
7470 T(V7), // V6T2.
7471 T(V6K), // V6K.
7472 T(V7), // V7.
7473 T(V6_M) // V6_M.
7474 };
7475 static const int v6s_m[] =
7476 {
7477 -1, // PRE_V4.
7478 -1, // V4.
7479 T(V6K), // V4T.
7480 T(V6K), // V5T.
7481 T(V6K), // V5TE.
7482 T(V6K), // V5TEJ.
7483 T(V6K), // V6.
7484 T(V6KZ), // V6KZ.
7485 T(V7), // V6T2.
7486 T(V6K), // V6K.
7487 T(V7), // V7.
7488 T(V6S_M), // V6_M.
7489 T(V6S_M) // V6S_M.
7490 };
7491 static const int v7e_m[] =
7492 {
7493 -1, // PRE_V4.
7494 -1, // V4.
7495 T(V7E_M), // V4T.
7496 T(V7E_M), // V5T.
7497 T(V7E_M), // V5TE.
7498 T(V7E_M), // V5TEJ.
7499 T(V7E_M), // V6.
7500 T(V7E_M), // V6KZ.
7501 T(V7E_M), // V6T2.
7502 T(V7E_M), // V6K.
7503 T(V7E_M), // V7.
7504 T(V7E_M), // V6_M.
7505 T(V7E_M), // V6S_M.
7506 T(V7E_M) // V7E_M.
7507 };
7508 static const int v4t_plus_v6_m[] =
7509 {
7510 -1, // PRE_V4.
7511 -1, // V4.
7512 T(V4T), // V4T.
7513 T(V5T), // V5T.
7514 T(V5TE), // V5TE.
7515 T(V5TEJ), // V5TEJ.
7516 T(V6), // V6.
7517 T(V6KZ), // V6KZ.
7518 T(V6T2), // V6T2.
7519 T(V6K), // V6K.
7520 T(V7), // V7.
7521 T(V6_M), // V6_M.
7522 T(V6S_M), // V6S_M.
7523 T(V7E_M), // V7E_M.
7524 T(V4T_PLUS_V6_M) // V4T plus V6_M.
7525 };
7526 static const int *comb[] =
7527 {
7528 v6t2,
7529 v6k,
7530 v7,
7531 v6_m,
7532 v6s_m,
7533 v7e_m,
7534 // Pseudo-architecture.
7535 v4t_plus_v6_m
7536 };
7537
7538 // Check we've not got a higher architecture than we know about.
7539
7540 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
7541 {
7542 gold_error(_("%s: unknown CPU architecture"), name);
7543 return -1;
7544 }
7545
7546 // Override old tag if we have a Tag_also_compatible_with on the output.
7547
7548 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
7549 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
7550 oldtag = T(V4T_PLUS_V6_M);
7551
7552 // And override the new tag if we have a Tag_also_compatible_with on the
7553 // input.
7554
7555 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
7556 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
7557 newtag = T(V4T_PLUS_V6_M);
7558
7559 // Architectures before V6KZ add features monotonically.
7560 int tagh = std::max(oldtag, newtag);
7561 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
7562 return tagh;
7563
7564 int tagl = std::min(oldtag, newtag);
7565 int result = comb[tagh - T(V6T2)][tagl];
7566
7567 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
7568 // as the canonical version.
7569 if (result == T(V4T_PLUS_V6_M))
7570 {
7571 result = T(V4T);
7572 *secondary_compat_out = T(V6_M);
7573 }
7574 else
7575 *secondary_compat_out = -1;
7576
7577 if (result == -1)
7578 {
7579 gold_error(_("%s: conflicting CPU architectures %d/%d"),
7580 name, oldtag, newtag);
7581 return -1;
7582 }
7583
7584 return result;
7585#undef T
7586}
7587
7588// Helper to print AEABI enum tag value.
7589
7590template<bool big_endian>
7591std::string
7592Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
7593{
7594 static const char *aeabi_enum_names[] =
7595 { "", "variable-size", "32-bit", "" };
7596 const size_t aeabi_enum_names_size =
7597 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
7598
7599 if (value < aeabi_enum_names_size)
7600 return std::string(aeabi_enum_names[value]);
7601 else
7602 {
7603 char buffer[100];
7604 sprintf(buffer, "<unknown value %u>", value);
7605 return std::string(buffer);
7606 }
7607}
7608
7609// Return the string value to store in TAG_CPU_name.
7610
7611template<bool big_endian>
7612std::string
7613Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
7614{
7615 static const char *name_table[] = {
7616 // These aren't real CPU names, but we can't guess
7617 // that from the architecture version alone.
7618 "Pre v4",
7619 "ARM v4",
7620 "ARM v4T",
7621 "ARM v5T",
7622 "ARM v5TE",
7623 "ARM v5TEJ",
7624 "ARM v6",
7625 "ARM v6KZ",
7626 "ARM v6T2",
7627 "ARM v6K",
7628 "ARM v7",
7629 "ARM v6-M",
7630 "ARM v6S-M",
7631 "ARM v7E-M"
7632 };
7633 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
7634
7635 if (value < name_table_size)
7636 return std::string(name_table[value]);
7637 else
7638 {
7639 char buffer[100];
7640 sprintf(buffer, "<unknown CPU value %u>", value);
7641 return std::string(buffer);
7642 }
7643}
7644
7645// Merge object attributes from input file called NAME with those of the
7646// output. The input object attributes are in the object pointed by PASD.
7647
7648template<bool big_endian>
7649void
7650Target_arm<big_endian>::merge_object_attributes(
7651 const char* name,
7652 const Attributes_section_data* pasd)
7653{
7654 // Return if there is no attributes section data.
7655 if (pasd == NULL)
7656 return;
7657
7658 // If output has no object attributes, just copy.
7659 if (this->attributes_section_data_ == NULL)
7660 {
7661 this->attributes_section_data_ = new Attributes_section_data(*pasd);
7662 return;
7663 }
7664
7665 const int vendor = Object_attribute::OBJ_ATTR_PROC;
7666 const Object_attribute* in_attr = pasd->known_attributes(vendor);
7667 Object_attribute* out_attr =
7668 this->attributes_section_data_->known_attributes(vendor);
7669
7670 // This needs to happen before Tag_ABI_FP_number_model is merged. */
7671 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
7672 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
7673 {
7674 // Ignore mismatches if the object doesn't use floating point. */
7675 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
7676 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
7677 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
7678 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
7679 gold_error(_("%s uses VFP register arguments, output does not"),
7680 name);
7681 }
7682
7683 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
7684 {
7685 // Merge this attribute with existing attributes.
7686 switch (i)
7687 {
7688 case elfcpp::Tag_CPU_raw_name:
7689 case elfcpp::Tag_CPU_name:
7690 // These are merged after Tag_CPU_arch.
7691 break;
7692
7693 case elfcpp::Tag_ABI_optimization_goals:
7694 case elfcpp::Tag_ABI_FP_optimization_goals:
7695 // Use the first value seen.
7696 break;
7697
7698 case elfcpp::Tag_CPU_arch:
7699 {
7700 unsigned int saved_out_attr = out_attr->int_value();
7701 // Merge Tag_CPU_arch and Tag_also_compatible_with.
7702 int secondary_compat =
7703 this->get_secondary_compatible_arch(pasd);
7704 int secondary_compat_out =
7705 this->get_secondary_compatible_arch(
7706 this->attributes_section_data_);
7707 out_attr[i].set_int_value(
7708 tag_cpu_arch_combine(name, out_attr[i].int_value(),
7709 &secondary_compat_out,
7710 in_attr[i].int_value(),
7711 secondary_compat));
7712 this->set_secondary_compatible_arch(this->attributes_section_data_,
7713 secondary_compat_out);
7714
7715 // Merge Tag_CPU_name and Tag_CPU_raw_name.
7716 if (out_attr[i].int_value() == saved_out_attr)
7717 ; // Leave the names alone.
7718 else if (out_attr[i].int_value() == in_attr[i].int_value())
7719 {
7720 // The output architecture has been changed to match the
7721 // input architecture. Use the input names.
7722 out_attr[elfcpp::Tag_CPU_name].set_string_value(
7723 in_attr[elfcpp::Tag_CPU_name].string_value());
7724 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
7725 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
7726 }
7727 else
7728 {
7729 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
7730 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
7731 }
7732
7733 // If we still don't have a value for Tag_CPU_name,
7734 // make one up now. Tag_CPU_raw_name remains blank.
7735 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
7736 {
7737 const std::string cpu_name =
7738 this->tag_cpu_name_value(out_attr[i].int_value());
7739 // FIXME: If we see an unknown CPU, this will be set
7740 // to "<unknown CPU n>", where n is the attribute value.
7741 // This is different from BFD, which leaves the name alone.
7742 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
7743 }
7744 }
7745 break;
7746
7747 case elfcpp::Tag_ARM_ISA_use:
7748 case elfcpp::Tag_THUMB_ISA_use:
7749 case elfcpp::Tag_WMMX_arch:
7750 case elfcpp::Tag_Advanced_SIMD_arch:
7751 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
7752 case elfcpp::Tag_ABI_FP_rounding:
7753 case elfcpp::Tag_ABI_FP_exceptions:
7754 case elfcpp::Tag_ABI_FP_user_exceptions:
7755 case elfcpp::Tag_ABI_FP_number_model:
7756 case elfcpp::Tag_VFP_HP_extension:
7757 case elfcpp::Tag_CPU_unaligned_access:
7758 case elfcpp::Tag_T2EE_use:
7759 case elfcpp::Tag_Virtualization_use:
7760 case elfcpp::Tag_MPextension_use:
7761 // Use the largest value specified.
7762 if (in_attr[i].int_value() > out_attr[i].int_value())
7763 out_attr[i].set_int_value(in_attr[i].int_value());
7764 break;
7765
7766 case elfcpp::Tag_ABI_align8_preserved:
7767 case elfcpp::Tag_ABI_PCS_RO_data:
7768 // Use the smallest value specified.
7769 if (in_attr[i].int_value() < out_attr[i].int_value())
7770 out_attr[i].set_int_value(in_attr[i].int_value());
7771 break;
7772
7773 case elfcpp::Tag_ABI_align8_needed:
7774 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
7775 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
7776 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
7777 == 0)))
7778 {
7779 // This error message should be enabled once all non-conformant
7780 // binaries in the toolchain have had the attributes set
7781 // properly.
7782 // gold_error(_("output 8-byte data alignment conflicts with %s"),
7783 // name);
7784 }
7785 // Fall through.
7786 case elfcpp::Tag_ABI_FP_denormal:
7787 case elfcpp::Tag_ABI_PCS_GOT_use:
7788 {
7789 // These tags have 0 = don't care, 1 = strong requirement,
7790 // 2 = weak requirement.
7791 static const int order_021[3] = {0, 2, 1};
7792
7793 // Use the "greatest" from the sequence 0, 2, 1, or the largest
7794 // value if greater than 2 (for future-proofing).
7795 if ((in_attr[i].int_value() > 2
7796 && in_attr[i].int_value() > out_attr[i].int_value())
7797 || (in_attr[i].int_value() <= 2
7798 && out_attr[i].int_value() <= 2
7799 && (order_021[in_attr[i].int_value()]
7800 > order_021[out_attr[i].int_value()])))
7801 out_attr[i].set_int_value(in_attr[i].int_value());
7802 }
7803 break;
7804
7805 case elfcpp::Tag_CPU_arch_profile:
7806 if (out_attr[i].int_value() != in_attr[i].int_value())
7807 {
7808 // 0 will merge with anything.
7809 // 'A' and 'S' merge to 'A'.
7810 // 'R' and 'S' merge to 'R'.
7811 // 'M' and 'A|R|S' is an error.
7812 if (out_attr[i].int_value() == 0
7813 || (out_attr[i].int_value() == 'S'
7814 && (in_attr[i].int_value() == 'A'
7815 || in_attr[i].int_value() == 'R')))
7816 out_attr[i].set_int_value(in_attr[i].int_value());
7817 else if (in_attr[i].int_value() == 0
7818 || (in_attr[i].int_value() == 'S'
7819 && (out_attr[i].int_value() == 'A'
7820 || out_attr[i].int_value() == 'R')))
7821 ; // Do nothing.
7822 else
7823 {
7824 gold_error
7825 (_("conflicting architecture profiles %c/%c"),
7826 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
7827 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
7828 }
7829 }
7830 break;
7831 case elfcpp::Tag_VFP_arch:
7832 {
7833 static const struct
7834 {
7835 int ver;
7836 int regs;
7837 } vfp_versions[7] =
7838 {
7839 {0, 0},
7840 {1, 16},
7841 {2, 16},
7842 {3, 32},
7843 {3, 16},
7844 {4, 32},
7845 {4, 16}
7846 };
7847
7848 // Values greater than 6 aren't defined, so just pick the
7849 // biggest.
7850 if (in_attr[i].int_value() > 6
7851 && in_attr[i].int_value() > out_attr[i].int_value())
7852 {
7853 *out_attr = *in_attr;
7854 break;
7855 }
7856 // The output uses the superset of input features
7857 // (ISA version) and registers.
7858 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
7859 vfp_versions[out_attr[i].int_value()].ver);
7860 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
7861 vfp_versions[out_attr[i].int_value()].regs);
7862 // This assumes all possible supersets are also a valid
7863 // options.
7864 int newval;
7865 for (newval = 6; newval > 0; newval--)
7866 {
7867 if (regs == vfp_versions[newval].regs
7868 && ver == vfp_versions[newval].ver)
7869 break;
7870 }
7871 out_attr[i].set_int_value(newval);
7872 }
7873 break;
7874 case elfcpp::Tag_PCS_config:
7875 if (out_attr[i].int_value() == 0)
7876 out_attr[i].set_int_value(in_attr[i].int_value());
7877 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
7878 {
7879 // It's sometimes ok to mix different configs, so this is only
7880 // a warning.
7881 gold_warning(_("%s: conflicting platform configuration"), name);
7882 }
7883 break;
7884 case elfcpp::Tag_ABI_PCS_R9_use:
7885 if (in_attr[i].int_value() != out_attr[i].int_value()
7886 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
7887 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
7888 {
7889 gold_error(_("%s: conflicting use of R9"), name);
7890 }
7891 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
7892 out_attr[i].set_int_value(in_attr[i].int_value());
7893 break;
7894 case elfcpp::Tag_ABI_PCS_RW_data:
7895 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
7896 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
7897 != elfcpp::AEABI_R9_SB)
7898 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
7899 != elfcpp::AEABI_R9_unused))
7900 {
7901 gold_error(_("%s: SB relative addressing conflicts with use "
7902 "of R9"),
7903 name);
7904 }
7905 // Use the smallest value specified.
7906 if (in_attr[i].int_value() < out_attr[i].int_value())
7907 out_attr[i].set_int_value(in_attr[i].int_value());
7908 break;
7909 case elfcpp::Tag_ABI_PCS_wchar_t:
7910 // FIXME: Make it possible to turn off this warning.
7911 if (out_attr[i].int_value()
7912 && in_attr[i].int_value()
7913 && out_attr[i].int_value() != in_attr[i].int_value())
7914 {
7915 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
7916 "use %u-byte wchar_t; use of wchar_t values "
7917 "across objects may fail"),
7918 name, in_attr[i].int_value(),
7919 out_attr[i].int_value());
7920 }
7921 else if (in_attr[i].int_value() && !out_attr[i].int_value())
7922 out_attr[i].set_int_value(in_attr[i].int_value());
7923 break;
7924 case elfcpp::Tag_ABI_enum_size:
7925 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
7926 {
7927 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
7928 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
7929 {
7930 // The existing object is compatible with anything.
7931 // Use whatever requirements the new object has.
7932 out_attr[i].set_int_value(in_attr[i].int_value());
7933 }
7934 // FIXME: Make it possible to turn off this warning.
7935 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
7936 && out_attr[i].int_value() != in_attr[i].int_value())
7937 {
7938 unsigned int in_value = in_attr[i].int_value();
7939 unsigned int out_value = out_attr[i].int_value();
7940 gold_warning(_("%s uses %s enums yet the output is to use "
7941 "%s enums; use of enum values across objects "
7942 "may fail"),
7943 name,
7944 this->aeabi_enum_name(in_value).c_str(),
7945 this->aeabi_enum_name(out_value).c_str());
7946 }
7947 }
7948 break;
7949 case elfcpp::Tag_ABI_VFP_args:
7950 // Aready done.
7951 break;
7952 case elfcpp::Tag_ABI_WMMX_args:
7953 if (in_attr[i].int_value() != out_attr[i].int_value())
7954 {
7955 gold_error(_("%s uses iWMMXt register arguments, output does "
7956 "not"),
7957 name);
7958 }
7959 break;
7960 case Object_attribute::Tag_compatibility:
7961 // Merged in target-independent code.
7962 break;
7963 case elfcpp::Tag_ABI_HardFP_use:
7964 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
7965 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
7966 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
7967 out_attr[i].set_int_value(3);
7968 else if (in_attr[i].int_value() > out_attr[i].int_value())
7969 out_attr[i].set_int_value(in_attr[i].int_value());
7970 break;
7971 case elfcpp::Tag_ABI_FP_16bit_format:
7972 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
7973 {
7974 if (in_attr[i].int_value() != out_attr[i].int_value())
7975 gold_error(_("fp16 format mismatch between %s and output"),
7976 name);
7977 }
7978 if (in_attr[i].int_value() != 0)
7979 out_attr[i].set_int_value(in_attr[i].int_value());
7980 break;
7981
7982 case elfcpp::Tag_nodefaults:
7983 // This tag is set if it exists, but the value is unused (and is
7984 // typically zero). We don't actually need to do anything here -
7985 // the merge happens automatically when the type flags are merged
7986 // below.
7987 break;
7988 case elfcpp::Tag_also_compatible_with:
7989 // Already done in Tag_CPU_arch.
7990 break;
7991 case elfcpp::Tag_conformance:
7992 // Keep the attribute if it matches. Throw it away otherwise.
7993 // No attribute means no claim to conform.
7994 if (in_attr[i].string_value() != out_attr[i].string_value())
7995 out_attr[i].set_string_value("");
7996 break;
7997
7998 default:
7999 {
8000 const char* err_object = NULL;
8001
8002 // The "known_obj_attributes" table does contain some undefined
8003 // attributes. Ensure that there are unused.
8004 if (out_attr[i].int_value() != 0
8005 || out_attr[i].string_value() != "")
8006 err_object = "output";
8007 else if (in_attr[i].int_value() != 0
8008 || in_attr[i].string_value() != "")
8009 err_object = name;
8010
8011 if (err_object != NULL)
8012 {
8013 // Attribute numbers >=64 (mod 128) can be safely ignored.
8014 if ((i & 127) < 64)
8015 gold_error(_("%s: unknown mandatory EABI object attribute "
8016 "%d"),
8017 err_object, i);
8018 else
8019 gold_warning(_("%s: unknown EABI object attribute %d"),
8020 err_object, i);
8021 }
8022
8023 // Only pass on attributes that match in both inputs.
8024 if (!in_attr[i].matches(out_attr[i]))
8025 {
8026 out_attr[i].set_int_value(0);
8027 out_attr[i].set_string_value("");
8028 }
8029 }
8030 }
8031
8032 // If out_attr was copied from in_attr then it won't have a type yet.
8033 if (in_attr[i].type() && !out_attr[i].type())
8034 out_attr[i].set_type(in_attr[i].type());
8035 }
8036
8037 // Merge Tag_compatibility attributes and any common GNU ones.
8038 this->attributes_section_data_->merge(name, pasd);
8039
8040 // Check for any attributes not known on ARM.
8041 typedef Vendor_object_attributes::Other_attributes Other_attributes;
8042 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
8043 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
8044 Other_attributes* out_other_attributes =
8045 this->attributes_section_data_->other_attributes(vendor);
8046 Other_attributes::iterator out_iter = out_other_attributes->begin();
8047
8048 while (in_iter != in_other_attributes->end()
8049 || out_iter != out_other_attributes->end())
8050 {
8051 const char* err_object = NULL;
8052 int err_tag = 0;
8053
8054 // The tags for each list are in numerical order.
8055 // If the tags are equal, then merge.
8056 if (out_iter != out_other_attributes->end()
8057 && (in_iter == in_other_attributes->end()
8058 || in_iter->first > out_iter->first))
8059 {
8060 // This attribute only exists in output. We can't merge, and we
8061 // don't know what the tag means, so delete it.
8062 err_object = "output";
8063 err_tag = out_iter->first;
8064 int saved_tag = out_iter->first;
8065 delete out_iter->second;
8066 out_other_attributes->erase(out_iter);
8067 out_iter = out_other_attributes->upper_bound(saved_tag);
8068 }
8069 else if (in_iter != in_other_attributes->end()
8070 && (out_iter != out_other_attributes->end()
8071 || in_iter->first < out_iter->first))
8072 {
8073 // This attribute only exists in input. We can't merge, and we
8074 // don't know what the tag means, so ignore it.
8075 err_object = name;
8076 err_tag = in_iter->first;
8077 ++in_iter;
8078 }
8079 else // The tags are equal.
8080 {
8081 // As present, all attributes in the list are unknown, and
8082 // therefore can't be merged meaningfully.
8083 err_object = "output";
8084 err_tag = out_iter->first;
8085
8086 // Only pass on attributes that match in both inputs.
8087 if (!in_iter->second->matches(*(out_iter->second)))
8088 {
8089 // No match. Delete the attribute.
8090 int saved_tag = out_iter->first;
8091 delete out_iter->second;
8092 out_other_attributes->erase(out_iter);
8093 out_iter = out_other_attributes->upper_bound(saved_tag);
8094 }
8095 else
8096 {
8097 // Matched. Keep the attribute and move to the next.
8098 ++out_iter;
8099 ++in_iter;
8100 }
8101 }
8102
8103 if (err_object)
8104 {
8105 // Attribute numbers >=64 (mod 128) can be safely ignored. */
8106 if ((err_tag & 127) < 64)
8107 {
8108 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
8109 err_object, err_tag);
8110 }
8111 else
8112 {
8113 gold_warning(_("%s: unknown EABI object attribute %d"),
8114 err_object, err_tag);
8115 }
8116 }
8117 }
8118}
8119
55da9579
DK
8120// Return whether a relocation type used the LSB to distinguish THUMB
8121// addresses.
8122template<bool big_endian>
8123bool
8124Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
8125{
8126 switch (r_type)
8127 {
8128 case elfcpp::R_ARM_PC24:
8129 case elfcpp::R_ARM_ABS32:
8130 case elfcpp::R_ARM_REL32:
8131 case elfcpp::R_ARM_SBREL32:
8132 case elfcpp::R_ARM_THM_CALL:
8133 case elfcpp::R_ARM_GLOB_DAT:
8134 case elfcpp::R_ARM_JUMP_SLOT:
8135 case elfcpp::R_ARM_GOTOFF32:
8136 case elfcpp::R_ARM_PLT32:
8137 case elfcpp::R_ARM_CALL:
8138 case elfcpp::R_ARM_JUMP24:
8139 case elfcpp::R_ARM_THM_JUMP24:
8140 case elfcpp::R_ARM_SBREL31:
8141 case elfcpp::R_ARM_PREL31:
8142 case elfcpp::R_ARM_MOVW_ABS_NC:
8143 case elfcpp::R_ARM_MOVW_PREL_NC:
8144 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8145 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8146 case elfcpp::R_ARM_THM_JUMP19:
8147 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8148 case elfcpp::R_ARM_ALU_PC_G0_NC:
8149 case elfcpp::R_ARM_ALU_PC_G0:
8150 case elfcpp::R_ARM_ALU_PC_G1_NC:
8151 case elfcpp::R_ARM_ALU_PC_G1:
8152 case elfcpp::R_ARM_ALU_PC_G2:
8153 case elfcpp::R_ARM_ALU_SB_G0_NC:
8154 case elfcpp::R_ARM_ALU_SB_G0:
8155 case elfcpp::R_ARM_ALU_SB_G1_NC:
8156 case elfcpp::R_ARM_ALU_SB_G1:
8157 case elfcpp::R_ARM_ALU_SB_G2:
8158 case elfcpp::R_ARM_MOVW_BREL_NC:
8159 case elfcpp::R_ARM_MOVW_BREL:
8160 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8161 case elfcpp::R_ARM_THM_MOVW_BREL:
8162 return true;
8163 default:
8164 return false;
8165 }
8166}
8167
8168// Stub-generation methods for Target_arm.
8169
8170// Make a new Arm_input_section object.
8171
8172template<bool big_endian>
8173Arm_input_section<big_endian>*
8174Target_arm<big_endian>::new_arm_input_section(
2ea97941
ILT
8175 Relobj* relobj,
8176 unsigned int shndx)
55da9579 8177{
5ac169d4 8178 Section_id sid(relobj, shndx);
55da9579
DK
8179
8180 Arm_input_section<big_endian>* arm_input_section =
2ea97941 8181 new Arm_input_section<big_endian>(relobj, shndx);
55da9579
DK
8182 arm_input_section->init();
8183
8184 // Register new Arm_input_section in map for look-up.
8185 std::pair<typename Arm_input_section_map::iterator, bool> ins =
5ac169d4 8186 this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
55da9579
DK
8187
8188 // Make sure that it we have not created another Arm_input_section
8189 // for this input section already.
8190 gold_assert(ins.second);
8191
8192 return arm_input_section;
8193}
8194
8195// Find the Arm_input_section object corresponding to the SHNDX-th input
8196// section of RELOBJ.
8197
8198template<bool big_endian>
8199Arm_input_section<big_endian>*
8200Target_arm<big_endian>::find_arm_input_section(
2ea97941
ILT
8201 Relobj* relobj,
8202 unsigned int shndx) const
55da9579 8203{
5ac169d4 8204 Section_id sid(relobj, shndx);
55da9579 8205 typename Arm_input_section_map::const_iterator p =
5ac169d4 8206 this->arm_input_section_map_.find(sid);
55da9579
DK
8207 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
8208}
8209
8210// Make a new stub table.
8211
8212template<bool big_endian>
8213Stub_table<big_endian>*
8214Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
8215{
2ea97941 8216 Stub_table<big_endian>* stub_table =
55da9579 8217 new Stub_table<big_endian>(owner);
2ea97941 8218 this->stub_tables_.push_back(stub_table);
55da9579 8219
2ea97941
ILT
8220 stub_table->set_address(owner->address() + owner->data_size());
8221 stub_table->set_file_offset(owner->offset() + owner->data_size());
8222 stub_table->finalize_data_size();
55da9579 8223
2ea97941 8224 return stub_table;
55da9579
DK
8225}
8226
eb44217c
DK
8227// Scan a relocation for stub generation.
8228
8229template<bool big_endian>
8230void
8231Target_arm<big_endian>::scan_reloc_for_stub(
8232 const Relocate_info<32, big_endian>* relinfo,
8233 unsigned int r_type,
8234 const Sized_symbol<32>* gsym,
8235 unsigned int r_sym,
8236 const Symbol_value<32>* psymval,
8237 elfcpp::Elf_types<32>::Elf_Swxword addend,
8238 Arm_address address)
8239{
2ea97941 8240 typedef typename Target_arm<big_endian>::Relocate Relocate;
eb44217c
DK
8241
8242 const Arm_relobj<big_endian>* arm_relobj =
8243 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8244
a2162063
ILT
8245 if (r_type == elfcpp::R_ARM_V4BX)
8246 {
8247 const uint32_t reg = (addend & 0xf);
8248 if (this->fix_v4bx() == 2 && reg < 0xf)
8249 {
8250 // Try looking up an existing stub from a stub table.
8251 Stub_table<big_endian>* stub_table =
8252 arm_relobj->stub_table(relinfo->data_shndx);
8253 gold_assert(stub_table != NULL);
8254
8255 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
8256 {
8257 // create a new stub and add it to stub table.
8258 Arm_v4bx_stub* stub =
8259 this->stub_factory().make_arm_v4bx_stub(reg);
8260 gold_assert(stub != NULL);
8261 stub_table->add_arm_v4bx_stub(stub);
8262 }
8263 }
8264
8265 return;
8266 }
8267
eb44217c
DK
8268 bool target_is_thumb;
8269 Symbol_value<32> symval;
8270 if (gsym != NULL)
8271 {
8272 // This is a global symbol. Determine if we use PLT and if the
8273 // final target is THUMB.
2ea97941 8274 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
eb44217c
DK
8275 {
8276 // This uses a PLT, change the symbol value.
8277 symval.set_output_value(this->plt_section()->address()
8278 + gsym->plt_offset());
8279 psymval = &symval;
8280 target_is_thumb = false;
8281 }
8282 else if (gsym->is_undefined())
8283 // There is no need to generate a stub symbol is undefined.
8284 return;
8285 else
8286 {
8287 target_is_thumb =
8288 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
8289 || (gsym->type() == elfcpp::STT_FUNC
8290 && !gsym->is_undefined()
8291 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
8292 }
8293 }
8294 else
8295 {
8296 // This is a local symbol. Determine if the final target is THUMB.
8297 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
8298 }
8299
8300 // Strip LSB if this points to a THUMB target.
8301 if (target_is_thumb
8302 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
8303 && ((psymval->value(arm_relobj, 0) & 1) != 0))
8304 {
8305 Arm_address stripped_value =
8306 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
8307 symval.set_output_value(stripped_value);
8308 psymval = &symval;
8309 }
8310
8311 // Get the symbol value.
8312 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
8313
8314 // Owing to pipelining, the PC relative branches below actually skip
8315 // two instructions when the branch offset is 0.
8316 Arm_address destination;
8317 switch (r_type)
8318 {
8319 case elfcpp::R_ARM_CALL:
8320 case elfcpp::R_ARM_JUMP24:
8321 case elfcpp::R_ARM_PLT32:
8322 // ARM branches.
8323 destination = value + addend + 8;
8324 break;
8325 case elfcpp::R_ARM_THM_CALL:
8326 case elfcpp::R_ARM_THM_XPC22:
8327 case elfcpp::R_ARM_THM_JUMP24:
8328 case elfcpp::R_ARM_THM_JUMP19:
8329 // THUMB branches.
8330 destination = value + addend + 4;
8331 break;
8332 default:
8333 gold_unreachable();
8334 }
8335
a120bc7f 8336 Reloc_stub* stub = NULL;
eb44217c
DK
8337 Stub_type stub_type =
8338 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
8339 target_is_thumb);
a120bc7f
DK
8340 if (stub_type != arm_stub_none)
8341 {
8342 // Try looking up an existing stub from a stub table.
8343 Stub_table<big_endian>* stub_table =
8344 arm_relobj->stub_table(relinfo->data_shndx);
8345 gold_assert(stub_table != NULL);
eb44217c 8346
a120bc7f
DK
8347 // Locate stub by destination.
8348 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
eb44217c 8349
a120bc7f
DK
8350 // Create a stub if there is not one already
8351 stub = stub_table->find_reloc_stub(stub_key);
8352 if (stub == NULL)
8353 {
8354 // create a new stub and add it to stub table.
8355 stub = this->stub_factory().make_reloc_stub(stub_type);
8356 stub_table->add_reloc_stub(stub, stub_key);
8357 }
8358
8359 // Record the destination address.
8360 stub->set_destination_address(destination
8361 | (target_is_thumb ? 1 : 0));
eb44217c
DK
8362 }
8363
a120bc7f
DK
8364 // For Cortex-A8, we need to record a relocation at 4K page boundary.
8365 if (this->fix_cortex_a8_
8366 && (r_type == elfcpp::R_ARM_THM_JUMP24
8367 || r_type == elfcpp::R_ARM_THM_JUMP19
8368 || r_type == elfcpp::R_ARM_THM_CALL
8369 || r_type == elfcpp::R_ARM_THM_XPC22)
8370 && (address & 0xfffU) == 0xffeU)
8371 {
8372 // Found a candidate. Note we haven't checked the destination is
8373 // within 4K here: if we do so (and don't create a record) we can't
8374 // tell that a branch should have been relocated when scanning later.
8375 this->cortex_a8_relocs_info_[address] =
8376 new Cortex_a8_reloc(stub, r_type,
8377 destination | (target_is_thumb ? 1 : 0));
8378 }
eb44217c
DK
8379}
8380
8381// This function scans a relocation sections for stub generation.
8382// The template parameter Relocate must be a class type which provides
8383// a single function, relocate(), which implements the machine
8384// specific part of a relocation.
8385
8386// BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
8387// SHT_REL or SHT_RELA.
8388
8389// PRELOCS points to the relocation data. RELOC_COUNT is the number
8390// of relocs. OUTPUT_SECTION is the output section.
8391// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
8392// mapped to output offsets.
8393
8394// VIEW is the section data, VIEW_ADDRESS is its memory address, and
8395// VIEW_SIZE is the size. These refer to the input section, unless
8396// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
8397// the output section.
8398
8399template<bool big_endian>
8400template<int sh_type>
8401void inline
8402Target_arm<big_endian>::scan_reloc_section_for_stubs(
8403 const Relocate_info<32, big_endian>* relinfo,
8404 const unsigned char* prelocs,
8405 size_t reloc_count,
8406 Output_section* output_section,
8407 bool needs_special_offset_handling,
8408 const unsigned char* view,
8409 elfcpp::Elf_types<32>::Elf_Addr view_address,
8410 section_size_type)
8411{
8412 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
8413 const int reloc_size =
8414 Reloc_types<sh_type, 32, big_endian>::reloc_size;
8415
8416 Arm_relobj<big_endian>* arm_object =
8417 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8418 unsigned int local_count = arm_object->local_symbol_count();
8419
8420 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
8421
8422 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
8423 {
8424 Reltype reloc(prelocs);
8425
8426 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
8427 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
8428 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
8429
8430 r_type = this->get_real_reloc_type(r_type);
8431
8432 // Only a few relocation types need stubs.
8433 if ((r_type != elfcpp::R_ARM_CALL)
8434 && (r_type != elfcpp::R_ARM_JUMP24)
8435 && (r_type != elfcpp::R_ARM_PLT32)
8436 && (r_type != elfcpp::R_ARM_THM_CALL)
8437 && (r_type != elfcpp::R_ARM_THM_XPC22)
8438 && (r_type != elfcpp::R_ARM_THM_JUMP24)
a2162063
ILT
8439 && (r_type != elfcpp::R_ARM_THM_JUMP19)
8440 && (r_type != elfcpp::R_ARM_V4BX))
eb44217c
DK
8441 continue;
8442
2ea97941 8443 section_offset_type offset =
eb44217c
DK
8444 convert_to_section_size_type(reloc.get_r_offset());
8445
8446 if (needs_special_offset_handling)
8447 {
2ea97941
ILT
8448 offset = output_section->output_offset(relinfo->object,
8449 relinfo->data_shndx,
8450 offset);
8451 if (offset == -1)
eb44217c
DK
8452 continue;
8453 }
8454
a2162063
ILT
8455 if (r_type == elfcpp::R_ARM_V4BX)
8456 {
8457 // Get the BX instruction.
8458 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
8459 const Valtype* wv = reinterpret_cast<const Valtype*>(view + offset);
8460 elfcpp::Elf_types<32>::Elf_Swxword insn =
8461 elfcpp::Swap<32, big_endian>::readval(wv);
8462 this->scan_reloc_for_stub(relinfo, r_type, NULL, 0, NULL,
8463 insn, NULL);
8464 continue;
8465 }
8466
eb44217c
DK
8467 // Get the addend.
8468 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
8469 elfcpp::Elf_types<32>::Elf_Swxword addend =
2ea97941 8470 stub_addend_reader(r_type, view + offset, reloc);
eb44217c
DK
8471
8472 const Sized_symbol<32>* sym;
8473
8474 Symbol_value<32> symval;
8475 const Symbol_value<32> *psymval;
8476 if (r_sym < local_count)
8477 {
8478 sym = NULL;
8479 psymval = arm_object->local_symbol(r_sym);
8480
8481 // If the local symbol belongs to a section we are discarding,
8482 // and that section is a debug section, try to find the
8483 // corresponding kept section and map this symbol to its
8484 // counterpart in the kept section. The symbol must not
8485 // correspond to a section we are folding.
8486 bool is_ordinary;
2ea97941 8487 unsigned int shndx = psymval->input_shndx(&is_ordinary);
eb44217c 8488 if (is_ordinary
2ea97941
ILT
8489 && shndx != elfcpp::SHN_UNDEF
8490 && !arm_object->is_section_included(shndx)
8491 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
eb44217c
DK
8492 {
8493 if (comdat_behavior == CB_UNDETERMINED)
8494 {
8495 std::string name =
8496 arm_object->section_name(relinfo->data_shndx);
8497 comdat_behavior = get_comdat_behavior(name.c_str());
8498 }
8499 if (comdat_behavior == CB_PRETEND)
8500 {
8501 bool found;
8502 typename elfcpp::Elf_types<32>::Elf_Addr value =
2ea97941 8503 arm_object->map_to_kept_section(shndx, &found);
eb44217c
DK
8504 if (found)
8505 symval.set_output_value(value + psymval->input_value());
8506 else
8507 symval.set_output_value(0);
8508 }
8509 else
8510 {
8511 symval.set_output_value(0);
8512 }
8513 symval.set_no_output_symtab_entry();
8514 psymval = &symval;
8515 }
8516 }
8517 else
8518 {
8519 const Symbol* gsym = arm_object->global_symbol(r_sym);
8520 gold_assert(gsym != NULL);
8521 if (gsym->is_forwarder())
8522 gsym = relinfo->symtab->resolve_forwards(gsym);
8523
8524 sym = static_cast<const Sized_symbol<32>*>(gsym);
8525 if (sym->has_symtab_index())
8526 symval.set_output_symtab_index(sym->symtab_index());
8527 else
8528 symval.set_no_output_symtab_entry();
8529
8530 // We need to compute the would-be final value of this global
8531 // symbol.
8532 const Symbol_table* symtab = relinfo->symtab;
8533 const Sized_symbol<32>* sized_symbol =
8534 symtab->get_sized_symbol<32>(gsym);
8535 Symbol_table::Compute_final_value_status status;
8536 Arm_address value =
8537 symtab->compute_final_value<32>(sized_symbol, &status);
8538
8539 // Skip this if the symbol has not output section.
8540 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
8541 continue;
8542
8543 symval.set_output_value(value);
8544 psymval = &symval;
8545 }
8546
8547 // If symbol is a section symbol, we don't know the actual type of
8548 // destination. Give up.
8549 if (psymval->is_section_symbol())
8550 continue;
8551
8552 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
2ea97941 8553 addend, view_address + offset);
eb44217c
DK
8554 }
8555}
8556
8557// Scan an input section for stub generation.
8558
8559template<bool big_endian>
8560void
8561Target_arm<big_endian>::scan_section_for_stubs(
8562 const Relocate_info<32, big_endian>* relinfo,
8563 unsigned int sh_type,
8564 const unsigned char* prelocs,
8565 size_t reloc_count,
8566 Output_section* output_section,
8567 bool needs_special_offset_handling,
8568 const unsigned char* view,
8569 Arm_address view_address,
8570 section_size_type view_size)
8571{
8572 if (sh_type == elfcpp::SHT_REL)
8573 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
8574 relinfo,
8575 prelocs,
8576 reloc_count,
8577 output_section,
8578 needs_special_offset_handling,
8579 view,
8580 view_address,
8581 view_size);
8582 else if (sh_type == elfcpp::SHT_RELA)
8583 // We do not support RELA type relocations yet. This is provided for
8584 // completeness.
8585 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
8586 relinfo,
8587 prelocs,
8588 reloc_count,
8589 output_section,
8590 needs_special_offset_handling,
8591 view,
8592 view_address,
8593 view_size);
8594 else
8595 gold_unreachable();
8596}
8597
8598// Group input sections for stub generation.
8599//
8600// We goup input sections in an output sections so that the total size,
8601// including any padding space due to alignment is smaller than GROUP_SIZE
8602// unless the only input section in group is bigger than GROUP_SIZE already.
8603// Then an ARM stub table is created to follow the last input section
8604// in group. For each group an ARM stub table is created an is placed
8605// after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
8606// extend the group after the stub table.
8607
8608template<bool big_endian>
8609void
8610Target_arm<big_endian>::group_sections(
2ea97941 8611 Layout* layout,
eb44217c
DK
8612 section_size_type group_size,
8613 bool stubs_always_after_branch)
8614{
8615 // Group input sections and insert stub table
8616 Layout::Section_list section_list;
2ea97941 8617 layout->get_allocated_sections(&section_list);
eb44217c
DK
8618 for (Layout::Section_list::const_iterator p = section_list.begin();
8619 p != section_list.end();
8620 ++p)
8621 {
8622 Arm_output_section<big_endian>* output_section =
8623 Arm_output_section<big_endian>::as_arm_output_section(*p);
8624 output_section->group_sections(group_size, stubs_always_after_branch,
8625 this);
8626 }
8627}
8628
8629// Relaxation hook. This is where we do stub generation.
8630
8631template<bool big_endian>
8632bool
8633Target_arm<big_endian>::do_relax(
8634 int pass,
8635 const Input_objects* input_objects,
8636 Symbol_table* symtab,
2ea97941 8637 Layout* layout)
eb44217c
DK
8638{
8639 // No need to generate stubs if this is a relocatable link.
8640 gold_assert(!parameters->options().relocatable());
8641
8642 // If this is the first pass, we need to group input sections into
8643 // stub groups.
8644 if (pass == 1)
8645 {
8646 // Determine the stub group size. The group size is the absolute
8647 // value of the parameter --stub-group-size. If --stub-group-size
8648 // is passed a negative value, we restict stubs to be always after
8649 // the stubbed branches.
8650 int32_t stub_group_size_param =
8651 parameters->options().stub_group_size();
8652 bool stubs_always_after_branch = stub_group_size_param < 0;
8653 section_size_type stub_group_size = abs(stub_group_size_param);
8654
44272192
DK
8655 // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
8656 // page as the first half of a 32-bit branch straddling two 4K pages.
8657 // This is a crude way of enforcing that.
8658 if (this->fix_cortex_a8_)
8659 stubs_always_after_branch = true;
8660
eb44217c
DK
8661 if (stub_group_size == 1)
8662 {
8663 // Default value.
8664 // Thumb branch range is +-4MB has to be used as the default
8665 // maximum size (a given section can contain both ARM and Thumb
8666 // code, so the worst case has to be taken into account).
8667 //
8668 // This value is 24K less than that, which allows for 2025
8669 // 12-byte stubs. If we exceed that, then we will fail to link.
8670 // The user will have to relink with an explicit group size
8671 // option.
8672 stub_group_size = 4170000;
8673 }
8674
2ea97941 8675 group_sections(layout, stub_group_size, stubs_always_after_branch);
eb44217c
DK
8676 }
8677
44272192
DK
8678 // The Cortex-A8 stubs are sensitive to layout of code sections. At the
8679 // beginning of each relaxation pass, just blow away all the stubs.
8680 // Alternatively, we could selectively remove only the stubs and reloc
8681 // information for code sections that have moved since the last pass.
8682 // That would require more book-keeping.
eb44217c 8683 typedef typename Stub_table_list::iterator Stub_table_iterator;
a120bc7f
DK
8684 if (this->fix_cortex_a8_)
8685 {
8686 // Clear all Cortex-A8 reloc information.
8687 for (typename Cortex_a8_relocs_info::const_iterator p =
8688 this->cortex_a8_relocs_info_.begin();
8689 p != this->cortex_a8_relocs_info_.end();
8690 ++p)
8691 delete p->second;
8692 this->cortex_a8_relocs_info_.clear();
44272192
DK
8693
8694 // Remove all Cortex-A8 stubs.
8695 for (Stub_table_iterator sp = this->stub_tables_.begin();
8696 sp != this->stub_tables_.end();
8697 ++sp)
8698 (*sp)->remove_all_cortex_a8_stubs();
a120bc7f
DK
8699 }
8700
44272192 8701 // Scan relocs for relocation stubs
eb44217c
DK
8702 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
8703 op != input_objects->relobj_end();
8704 ++op)
8705 {
8706 Arm_relobj<big_endian>* arm_relobj =
8707 Arm_relobj<big_endian>::as_arm_relobj(*op);
2ea97941 8708 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
eb44217c
DK
8709 }
8710
2fb7225c
DK
8711 // Check all stub tables to see if any of them have their data sizes
8712 // or addresses alignments changed. These are the only things that
8713 // matter.
eb44217c
DK
8714 bool any_stub_table_changed = false;
8715 for (Stub_table_iterator sp = this->stub_tables_.begin();
8716 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
8717 ++sp)
8718 {
2fb7225c 8719 if ((*sp)->update_data_size_and_addralign())
eb44217c
DK
8720 any_stub_table_changed = true;
8721 }
8722
2fb7225c
DK
8723 // Finalize the stubs in the last relaxation pass.
8724 if (!any_stub_table_changed)
8725 for (Stub_table_iterator sp = this->stub_tables_.begin();
8726 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
8727 ++sp)
8728 (*sp)->finalize_stubs();
8729
eb44217c
DK
8730 return any_stub_table_changed;
8731}
8732
43d12afe
DK
8733// Relocate a stub.
8734
8735template<bool big_endian>
8736void
8737Target_arm<big_endian>::relocate_stub(
2fb7225c 8738 Stub* stub,
43d12afe
DK
8739 const Relocate_info<32, big_endian>* relinfo,
8740 Output_section* output_section,
8741 unsigned char* view,
8742 Arm_address address,
8743 section_size_type view_size)
8744{
8745 Relocate relocate;
2ea97941
ILT
8746 const Stub_template* stub_template = stub->stub_template();
8747 for (size_t i = 0; i < stub_template->reloc_count(); i++)
43d12afe 8748 {
2ea97941
ILT
8749 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
8750 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
43d12afe
DK
8751
8752 unsigned int r_type = insn->r_type();
2ea97941 8753 section_size_type reloc_offset = stub_template->reloc_offset(i);
43d12afe
DK
8754 section_size_type reloc_size = insn->size();
8755 gold_assert(reloc_offset + reloc_size <= view_size);
8756
8757 // This is the address of the stub destination.
41263c05 8758 Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
43d12afe
DK
8759 Symbol_value<32> symval;
8760 symval.set_output_value(target);
8761
8762 // Synthesize a fake reloc just in case. We don't have a symbol so
8763 // we use 0.
8764 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
8765 memset(reloc_buffer, 0, sizeof(reloc_buffer));
8766 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
8767 reloc_write.put_r_offset(reloc_offset);
8768 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
8769 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
8770
8771 relocate.relocate(relinfo, this, output_section,
8772 this->fake_relnum_for_stubs, rel, r_type,
8773 NULL, &symval, view + reloc_offset,
8774 address + reloc_offset, reloc_size);
8775 }
8776}
8777
a0351a69
DK
8778// Determine whether an object attribute tag takes an integer, a
8779// string or both.
8780
8781template<bool big_endian>
8782int
8783Target_arm<big_endian>::do_attribute_arg_type(int tag) const
8784{
8785 if (tag == Object_attribute::Tag_compatibility)
8786 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
8787 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
8788 else if (tag == elfcpp::Tag_nodefaults)
8789 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
8790 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
8791 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
8792 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
8793 else if (tag < 32)
8794 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
8795 else
8796 return ((tag & 1) != 0
8797 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
8798 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
8799}
8800
8801// Reorder attributes.
8802//
8803// The ABI defines that Tag_conformance should be emitted first, and that
8804// Tag_nodefaults should be second (if either is defined). This sets those
8805// two positions, and bumps up the position of all the remaining tags to
8806// compensate.
8807
8808template<bool big_endian>
8809int
8810Target_arm<big_endian>::do_attributes_order(int num) const
8811{
8812 // Reorder the known object attributes in output. We want to move
8813 // Tag_conformance to position 4 and Tag_conformance to position 5
8814 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
8815 if (num == 4)
8816 return elfcpp::Tag_conformance;
8817 if (num == 5)
8818 return elfcpp::Tag_nodefaults;
8819 if ((num - 2) < elfcpp::Tag_nodefaults)
8820 return num - 2;
8821 if ((num - 1) < elfcpp::Tag_conformance)
8822 return num - 1;
8823 return num;
8824}
4a657b0d 8825
44272192
DK
8826// Scan a span of THUMB code for Cortex-A8 erratum.
8827
8828template<bool big_endian>
8829void
8830Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
8831 Arm_relobj<big_endian>* arm_relobj,
8832 unsigned int shndx,
8833 section_size_type span_start,
8834 section_size_type span_end,
8835 const unsigned char* view,
8836 Arm_address address)
8837{
8838 // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
8839 //
8840 // The opcode is BLX.W, BL.W, B.W, Bcc.W
8841 // The branch target is in the same 4KB region as the
8842 // first half of the branch.
8843 // The instruction before the branch is a 32-bit
8844 // length non-branch instruction.
8845 section_size_type i = span_start;
8846 bool last_was_32bit = false;
8847 bool last_was_branch = false;
8848 while (i < span_end)
8849 {
8850 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
8851 const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
8852 uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
8853 bool is_blx = false, is_b = false;
8854 bool is_bl = false, is_bcc = false;
8855
8856 bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
8857 if (insn_32bit)
8858 {
8859 // Load the rest of the insn (in manual-friendly order).
8860 insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
8861
8862 // Encoding T4: B<c>.W.
8863 is_b = (insn & 0xf800d000U) == 0xf0009000U;
8864 // Encoding T1: BL<c>.W.
8865 is_bl = (insn & 0xf800d000U) == 0xf000d000U;
8866 // Encoding T2: BLX<c>.W.
8867 is_blx = (insn & 0xf800d000U) == 0xf000c000U;
8868 // Encoding T3: B<c>.W (not permitted in IT block).
8869 is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
8870 && (insn & 0x07f00000U) != 0x03800000U);
8871 }
8872
8873 bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
8874
8875 // If this instruction is a 32-bit THUMB branch that crosses a 4K
8876 // page boundary and it follows 32-bit non-branch instruction,
8877 // we need to work around.
8878 if (is_32bit_branch
8879 && ((address + i) & 0xfffU) == 0xffeU
8880 && last_was_32bit
8881 && !last_was_branch)
8882 {
8883 // Check to see if there is a relocation stub for this branch.
8884 bool force_target_arm = false;
8885 bool force_target_thumb = false;
8886 const Cortex_a8_reloc* cortex_a8_reloc = NULL;
8887 Cortex_a8_relocs_info::const_iterator p =
8888 this->cortex_a8_relocs_info_.find(address + i);
8889
8890 if (p != this->cortex_a8_relocs_info_.end())
8891 {
8892 cortex_a8_reloc = p->second;
8893 bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
8894
8895 if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
8896 && !target_is_thumb)
8897 force_target_arm = true;
8898 else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
8899 && target_is_thumb)
8900 force_target_thumb = true;
8901 }
8902
8903 off_t offset;
8904 Stub_type stub_type = arm_stub_none;
8905
8906 // Check if we have an offending branch instruction.
8907 uint16_t upper_insn = (insn >> 16) & 0xffffU;
8908 uint16_t lower_insn = insn & 0xffffU;
8909 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
8910
8911 if (cortex_a8_reloc != NULL
8912 && cortex_a8_reloc->reloc_stub() != NULL)
8913 // We've already made a stub for this instruction, e.g.
8914 // it's a long branch or a Thumb->ARM stub. Assume that
8915 // stub will suffice to work around the A8 erratum (see
8916 // setting of always_after_branch above).
8917 ;
8918 else if (is_bcc)
8919 {
8920 offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
8921 lower_insn);
8922 stub_type = arm_stub_a8_veneer_b_cond;
8923 }
8924 else if (is_b || is_bl || is_blx)
8925 {
8926 offset = RelocFuncs::thumb32_branch_offset(upper_insn,
8927 lower_insn);
8928 if (is_blx)
8929 offset &= ~3;
8930
8931 stub_type = (is_blx
8932 ? arm_stub_a8_veneer_blx
8933 : (is_bl
8934 ? arm_stub_a8_veneer_bl
8935 : arm_stub_a8_veneer_b));
8936 }
8937
8938 if (stub_type != arm_stub_none)
8939 {
8940 Arm_address pc_for_insn = address + i + 4;
8941
8942 // The original instruction is a BL, but the target is
8943 // an ARM instruction. If we were not making a stub,
8944 // the BL would have been converted to a BLX. Use the
8945 // BLX stub instead in that case.
8946 if (this->may_use_blx() && force_target_arm
8947 && stub_type == arm_stub_a8_veneer_bl)
8948 {
8949 stub_type = arm_stub_a8_veneer_blx;
8950 is_blx = true;
8951 is_bl = false;
8952 }
8953 // Conversely, if the original instruction was
8954 // BLX but the target is Thumb mode, use the BL stub.
8955 else if (force_target_thumb
8956 && stub_type == arm_stub_a8_veneer_blx)
8957 {
8958 stub_type = arm_stub_a8_veneer_bl;
8959 is_blx = false;
8960 is_bl = true;
8961 }
8962
8963 if (is_blx)
8964 pc_for_insn &= ~3;
8965
8966 // If we found a relocation, use the proper destination,
8967 // not the offset in the (unrelocated) instruction.
8968 // Note this is always done if we switched the stub type above.
8969 if (cortex_a8_reloc != NULL)
8970 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
8971
8972 Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
8973
8974 // Add a new stub if destination address in in the same page.
8975 if (((address + i) & ~0xfffU) == (target & ~0xfffU))
8976 {
8977 Cortex_a8_stub* stub =
8978 this->stub_factory_.make_cortex_a8_stub(stub_type,
8979 arm_relobj, shndx,
8980 address + i,
8981 target, insn);
8982 Stub_table<big_endian>* stub_table =
8983 arm_relobj->stub_table(shndx);
8984 gold_assert(stub_table != NULL);
8985 stub_table->add_cortex_a8_stub(address + i, stub);
8986 }
8987 }
8988 }
8989
8990 i += insn_32bit ? 4 : 2;
8991 last_was_32bit = insn_32bit;
8992 last_was_branch = is_32bit_branch;
8993 }
8994}
8995
41263c05
DK
8996// Apply the Cortex-A8 workaround.
8997
8998template<bool big_endian>
8999void
9000Target_arm<big_endian>::apply_cortex_a8_workaround(
9001 const Cortex_a8_stub* stub,
9002 Arm_address stub_address,
9003 unsigned char* insn_view,
9004 Arm_address insn_address)
9005{
9006 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
9007 Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
9008 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
9009 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
9010 off_t branch_offset = stub_address - (insn_address + 4);
9011
9012 typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
9013 switch (stub->stub_template()->type())
9014 {
9015 case arm_stub_a8_veneer_b_cond:
9016 gold_assert(!utils::has_overflow<21>(branch_offset));
9017 upper_insn = RelocFuncs::thumb32_cond_branch_upper(upper_insn,
9018 branch_offset);
9019 lower_insn = RelocFuncs::thumb32_cond_branch_lower(lower_insn,
9020 branch_offset);
9021 break;
9022
9023 case arm_stub_a8_veneer_b:
9024 case arm_stub_a8_veneer_bl:
9025 case arm_stub_a8_veneer_blx:
9026 if ((lower_insn & 0x5000U) == 0x4000U)
9027 // For a BLX instruction, make sure that the relocation is
9028 // rounded up to a word boundary. This follows the semantics of
9029 // the instruction which specifies that bit 1 of the target
9030 // address will come from bit 1 of the base address.
9031 branch_offset = (branch_offset + 2) & ~3;
9032
9033 // Put BRANCH_OFFSET back into the insn.
9034 gold_assert(!utils::has_overflow<25>(branch_offset));
9035 upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
9036 lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
9037 break;
9038
9039 default:
9040 gold_unreachable();
9041 }
9042
9043 // Put the relocated value back in the object file:
9044 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
9045 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
9046}
9047
4a657b0d
DK
9048template<bool big_endian>
9049class Target_selector_arm : public Target_selector
9050{
9051 public:
9052 Target_selector_arm()
9053 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
9054 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
9055 { }
9056
9057 Target*
9058 do_instantiate_target()
9059 { return new Target_arm<big_endian>(); }
9060};
9061
9062Target_selector_arm<false> target_selector_arm;
9063Target_selector_arm<true> target_selector_armbe;
9064
9065} // End anonymous namespace.
This page took 0.535163 seconds and 4 git commands to generate.