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