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