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