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