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