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