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