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