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