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