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