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