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