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