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