gdb/testsuite/
[deliverable/binutils-gdb.git] / gold / arm.cc
CommitLineData
4a657b0d
DK
1// arm.cc -- arm target support for gold.
2
3// Copyright 2009 Free Software Foundation, Inc.
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>
4a657b0d
DK
33
34#include "elfcpp.h"
35#include "parameters.h"
36#include "reloc.h"
37#include "arm.h"
38#include "object.h"
39#include "symtab.h"
40#include "layout.h"
41#include "output.h"
42#include "copy-relocs.h"
43#include "target.h"
44#include "target-reloc.h"
45#include "target-select.h"
46#include "tls.h"
47#include "defstd.h"
f345227a 48#include "gc.h"
a0351a69 49#include "attributes.h"
4a657b0d
DK
50
51namespace
52{
53
54using namespace gold;
55
94cdfcff
DK
56template<bool big_endian>
57class Output_data_plt_arm;
58
56ee5e00
DK
59template<bool big_endian>
60class Stub_table;
61
62template<bool big_endian>
63class Arm_input_section;
64
07f508a2
DK
65template<bool big_endian>
66class Arm_output_section;
67
68template<bool big_endian>
69class Arm_relobj;
70
b569affa
DK
71template<bool big_endian>
72class Target_arm;
73
74// For convenience.
75typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
76
77// Maximum branch offsets for ARM, THUMB and THUMB2.
78const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
79const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
80const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
81const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
82const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
83const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
84
4a657b0d
DK
85// The arm target class.
86//
87// This is a very simple port of gold for ARM-EABI. It is intended for
88// supporting Android only for the time being. Only these relocation types
89// are supported.
90//
91// R_ARM_NONE
92// R_ARM_ABS32
be8fcb75
ILT
93// R_ARM_ABS32_NOI
94// R_ARM_ABS16
95// R_ARM_ABS12
96// R_ARM_ABS8
97// R_ARM_THM_ABS5
98// R_ARM_BASE_ABS
4a657b0d
DK
99// R_ARM_REL32
100// R_ARM_THM_CALL
101// R_ARM_COPY
102// R_ARM_GLOB_DAT
103// R_ARM_BASE_PREL
104// R_ARM_JUMP_SLOT
105// R_ARM_RELATIVE
106// R_ARM_GOTOFF32
107// R_ARM_GOT_BREL
7f5309a5 108// R_ARM_GOT_PREL
4a657b0d
DK
109// R_ARM_PLT32
110// R_ARM_CALL
111// R_ARM_JUMP24
112// R_ARM_TARGET1
113// R_ARM_PREL31
7f5309a5 114// R_ARM_ABS8
fd3c5f0b
ILT
115// R_ARM_MOVW_ABS_NC
116// R_ARM_MOVT_ABS
117// R_ARM_THM_MOVW_ABS_NC
c2a122b6
ILT
118// R_ARM_THM_MOVT_ABS
119// R_ARM_MOVW_PREL_NC
120// R_ARM_MOVT_PREL
121// R_ARM_THM_MOVW_PREL_NC
122// R_ARM_THM_MOVT_PREL
4a657b0d 123//
4a657b0d 124// TODOs:
4a657b0d 125// - Support more relocation types as needed.
94cdfcff
DK
126// - Make PLTs more flexible for different architecture features like
127// Thumb-2 and BE8.
11af873f 128// There are probably a lot more.
4a657b0d 129
b569affa
DK
130// Instruction template class. This class is similar to the insn_sequence
131// struct in bfd/elf32-arm.c.
132
133class Insn_template
134{
135 public:
136 // Types of instruction templates.
137 enum Type
138 {
139 THUMB16_TYPE = 1,
bb0d3eb0
DK
140 // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
141 // templates with class-specific semantics. Currently this is used
142 // only by the Cortex_a8_stub class for handling condition codes in
143 // conditional branches.
144 THUMB16_SPECIAL_TYPE,
b569affa
DK
145 THUMB32_TYPE,
146 ARM_TYPE,
147 DATA_TYPE
148 };
149
bb0d3eb0 150 // Factory methods to create instruction templates in different formats.
b569affa
DK
151
152 static const Insn_template
153 thumb16_insn(uint32_t data)
154 { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
155
bb0d3eb0
DK
156 // A Thumb conditional branch, in which the proper condition is inserted
157 // when we build the stub.
b569affa
DK
158 static const Insn_template
159 thumb16_bcond_insn(uint32_t data)
bb0d3eb0 160 { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
b569affa
DK
161
162 static const Insn_template
163 thumb32_insn(uint32_t data)
164 { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
165
166 static const Insn_template
167 thumb32_b_insn(uint32_t data, int reloc_addend)
168 {
169 return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
170 reloc_addend);
171 }
172
173 static const Insn_template
174 arm_insn(uint32_t data)
175 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
176
177 static const Insn_template
178 arm_rel_insn(unsigned data, int reloc_addend)
179 { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
180
181 static const Insn_template
182 data_word(unsigned data, unsigned int r_type, int reloc_addend)
183 { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
184
185 // Accessors. This class is used for read-only objects so no modifiers
186 // are provided.
187
188 uint32_t
189 data() const
190 { return this->data_; }
191
192 // Return the instruction sequence type of this.
193 Type
194 type() const
195 { return this->type_; }
196
197 // Return the ARM relocation type of this.
198 unsigned int
199 r_type() const
200 { return this->r_type_; }
201
202 int32_t
203 reloc_addend() const
204 { return this->reloc_addend_; }
205
bb0d3eb0 206 // Return size of instruction template in bytes.
b569affa
DK
207 size_t
208 size() const;
209
bb0d3eb0 210 // Return byte-alignment of instruction template.
b569affa
DK
211 unsigned
212 alignment() const;
213
214 private:
215 // We make the constructor private to ensure that only the factory
216 // methods are used.
217 inline
2ea97941
ILT
218 Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
219 : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
b569affa
DK
220 { }
221
222 // Instruction specific data. This is used to store information like
223 // some of the instruction bits.
224 uint32_t data_;
225 // Instruction template type.
226 Type type_;
227 // Relocation type if there is a relocation or R_ARM_NONE otherwise.
228 unsigned int r_type_;
229 // Relocation addend.
230 int32_t reloc_addend_;
231};
232
233// Macro for generating code to stub types. One entry per long/short
234// branch stub
235
236#define DEF_STUBS \
237 DEF_STUB(long_branch_any_any) \
238 DEF_STUB(long_branch_v4t_arm_thumb) \
239 DEF_STUB(long_branch_thumb_only) \
240 DEF_STUB(long_branch_v4t_thumb_thumb) \
241 DEF_STUB(long_branch_v4t_thumb_arm) \
242 DEF_STUB(short_branch_v4t_thumb_arm) \
243 DEF_STUB(long_branch_any_arm_pic) \
244 DEF_STUB(long_branch_any_thumb_pic) \
245 DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
246 DEF_STUB(long_branch_v4t_arm_thumb_pic) \
247 DEF_STUB(long_branch_v4t_thumb_arm_pic) \
248 DEF_STUB(long_branch_thumb_only_pic) \
249 DEF_STUB(a8_veneer_b_cond) \
250 DEF_STUB(a8_veneer_b) \
251 DEF_STUB(a8_veneer_bl) \
252 DEF_STUB(a8_veneer_blx)
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.
273 arm_stub_type_last = arm_stub_a8_veneer_blx
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
746// Stub factory class.
747
748class Stub_factory
749{
750 public:
751 // Return the unique instance of this class.
752 static const Stub_factory&
753 get_instance()
754 {
755 static Stub_factory singleton;
756 return singleton;
757 }
758
759 // Make a relocation stub.
760 Reloc_stub*
761 make_reloc_stub(Stub_type stub_type) const
762 {
763 gold_assert(stub_type >= arm_stub_reloc_first
764 && stub_type <= arm_stub_reloc_last);
765 return new Reloc_stub(this->stub_templates_[stub_type]);
766 }
767
bb0d3eb0
DK
768 // Make a Cortex-A8 stub.
769 Cortex_a8_stub*
770 make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
771 Arm_address source, Arm_address destination,
772 uint32_t original_insn) const
773 {
774 gold_assert(stub_type >= arm_stub_cortex_a8_first
775 && stub_type <= arm_stub_cortex_a8_last);
776 return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
777 source, destination, original_insn);
778 }
779
b569affa
DK
780 private:
781 // Constructor and destructor are protected since we only return a single
782 // instance created in Stub_factory::get_instance().
783
784 Stub_factory();
785
786 // A Stub_factory may not be copied since it is a singleton.
787 Stub_factory(const Stub_factory&);
788 Stub_factory& operator=(Stub_factory&);
789
790 // Stub templates. These are initialized in the constructor.
791 const Stub_template* stub_templates_[arm_stub_type_last+1];
792};
793
56ee5e00
DK
794// A class to hold stubs for the ARM target.
795
796template<bool big_endian>
797class Stub_table : public Output_data
798{
799 public:
2ea97941
ILT
800 Stub_table(Arm_input_section<big_endian>* owner)
801 : Output_data(), addralign_(1), owner_(owner), has_been_changed_(false),
56ee5e00
DK
802 reloc_stubs_()
803 { }
804
805 ~Stub_table()
806 { }
807
808 // Owner of this stub table.
809 Arm_input_section<big_endian>*
810 owner() const
811 { return this->owner_; }
812
813 // Whether this stub table is empty.
814 bool
815 empty() const
816 { return this->reloc_stubs_.empty(); }
817
818 // Whether this has been changed.
819 bool
820 has_been_changed() const
821 { return this->has_been_changed_; }
822
823 // Set the has-been-changed flag.
824 void
825 set_has_been_changed(bool value)
826 { this->has_been_changed_ = value; }
827
828 // Return the current data size.
829 off_t
830 current_data_size() const
831 { return this->current_data_size_for_child(); }
832
833 // Add a STUB with using KEY. Caller is reponsible for avoid adding
834 // if already a STUB with the same key has been added.
835 void
836 add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key);
837
838 // Look up a relocation stub using KEY. Return NULL if there is none.
839 Reloc_stub*
840 find_reloc_stub(const Reloc_stub::Key& key) const
841 {
842 typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
843 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
844 }
845
846 // Relocate stubs in this stub table.
847 void
848 relocate_stubs(const Relocate_info<32, big_endian>*,
849 Target_arm<big_endian>*, Output_section*,
850 unsigned char*, Arm_address, section_size_type);
851
852 protected:
853 // Write out section contents.
854 void
855 do_write(Output_file*);
856
857 // Return the required alignment.
858 uint64_t
859 do_addralign() const
860 { return this->addralign_; }
861
862 // Finalize data size.
863 void
864 set_final_data_size()
865 { this->set_data_size(this->current_data_size_for_child()); }
866
867 // Reset address and file offset.
868 void
869 do_reset_address_and_file_offset();
870
871 private:
872 // Unordered map of stubs.
873 typedef
874 Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
875 Reloc_stub::Key::equal_to>
876 Reloc_stub_map;
877
878 // Address alignment
879 uint64_t addralign_;
880 // Owner of this stub table.
881 Arm_input_section<big_endian>* owner_;
882 // This is set to true during relaxiong if the size of the stub table
883 // has been changed.
884 bool has_been_changed_;
885 // The relocation stubs.
886 Reloc_stub_map reloc_stubs_;
887};
888
10ad9fe5
DK
889// A class to wrap an ordinary input section containing executable code.
890
891template<bool big_endian>
892class Arm_input_section : public Output_relaxed_input_section
893{
894 public:
2ea97941
ILT
895 Arm_input_section(Relobj* relobj, unsigned int shndx)
896 : Output_relaxed_input_section(relobj, shndx, 1),
10ad9fe5
DK
897 original_addralign_(1), original_size_(0), stub_table_(NULL)
898 { }
899
900 ~Arm_input_section()
901 { }
902
903 // Initialize.
904 void
905 init();
906
907 // Whether this is a stub table owner.
908 bool
909 is_stub_table_owner() const
910 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
911
912 // Return the stub table.
913 Stub_table<big_endian>*
914 stub_table() const
915 { return this->stub_table_; }
916
917 // Set the stub_table.
918 void
2ea97941
ILT
919 set_stub_table(Stub_table<big_endian>* stub_table)
920 { this->stub_table_ = stub_table; }
10ad9fe5 921
07f508a2
DK
922 // Downcast a base pointer to an Arm_input_section pointer. This is
923 // not type-safe but we only use Arm_input_section not the base class.
924 static Arm_input_section<big_endian>*
925 as_arm_input_section(Output_relaxed_input_section* poris)
926 { return static_cast<Arm_input_section<big_endian>*>(poris); }
927
10ad9fe5
DK
928 protected:
929 // Write data to output file.
930 void
931 do_write(Output_file*);
932
933 // Return required alignment of this.
934 uint64_t
935 do_addralign() const
936 {
937 if (this->is_stub_table_owner())
938 return std::max(this->stub_table_->addralign(),
939 this->original_addralign_);
940 else
941 return this->original_addralign_;
942 }
943
944 // Finalize data size.
945 void
946 set_final_data_size();
947
948 // Reset address and file offset.
949 void
950 do_reset_address_and_file_offset();
951
952 // Output offset.
953 bool
2ea97941
ILT
954 do_output_offset(const Relobj* object, unsigned int shndx,
955 section_offset_type offset,
10ad9fe5
DK
956 section_offset_type* poutput) const
957 {
958 if ((object == this->relobj())
2ea97941
ILT
959 && (shndx == this->shndx())
960 && (offset >= 0)
961 && (convert_types<uint64_t, section_offset_type>(offset)
10ad9fe5
DK
962 <= this->original_size_))
963 {
2ea97941 964 *poutput = offset;
10ad9fe5
DK
965 return true;
966 }
967 else
968 return false;
969 }
970
971 private:
972 // Copying is not allowed.
973 Arm_input_section(const Arm_input_section&);
974 Arm_input_section& operator=(const Arm_input_section&);
975
976 // Address alignment of the original input section.
977 uint64_t original_addralign_;
978 // Section size of the original input section.
979 uint64_t original_size_;
980 // Stub table.
981 Stub_table<big_endian>* stub_table_;
982};
983
07f508a2
DK
984// Arm output section class. This is defined mainly to add a number of
985// stub generation methods.
986
987template<bool big_endian>
988class Arm_output_section : public Output_section
989{
990 public:
2ea97941
ILT
991 Arm_output_section(const char* name, elfcpp::Elf_Word type,
992 elfcpp::Elf_Xword flags)
993 : Output_section(name, type, flags)
07f508a2
DK
994 { }
995
996 ~Arm_output_section()
997 { }
998
999 // Group input sections for stub generation.
1000 void
1001 group_sections(section_size_type, bool, Target_arm<big_endian>*);
1002
1003 // Downcast a base pointer to an Arm_output_section pointer. This is
1004 // not type-safe but we only use Arm_output_section not the base class.
1005 static Arm_output_section<big_endian>*
1006 as_arm_output_section(Output_section* os)
1007 { return static_cast<Arm_output_section<big_endian>*>(os); }
1008
1009 private:
1010 // For convenience.
1011 typedef Output_section::Input_section Input_section;
1012 typedef Output_section::Input_section_list Input_section_list;
1013
1014 // Create a stub group.
1015 void create_stub_group(Input_section_list::const_iterator,
1016 Input_section_list::const_iterator,
1017 Input_section_list::const_iterator,
1018 Target_arm<big_endian>*,
1019 std::vector<Output_relaxed_input_section*>*);
1020};
1021
8ffa3667
DK
1022// Arm_relobj class.
1023
1024template<bool big_endian>
1025class Arm_relobj : public Sized_relobj<32, big_endian>
1026{
1027 public:
1028 static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1029
2ea97941 1030 Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
8ffa3667 1031 const typename elfcpp::Ehdr<32, big_endian>& ehdr)
2ea97941 1032 : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
a0351a69
DK
1033 stub_tables_(), local_symbol_is_thumb_function_(),
1034 attributes_section_data_(NULL)
8ffa3667
DK
1035 { }
1036
1037 ~Arm_relobj()
a0351a69 1038 { delete this->attributes_section_data_; }
8ffa3667
DK
1039
1040 // Return the stub table of the SHNDX-th section if there is one.
1041 Stub_table<big_endian>*
2ea97941 1042 stub_table(unsigned int shndx) const
8ffa3667 1043 {
2ea97941
ILT
1044 gold_assert(shndx < this->stub_tables_.size());
1045 return this->stub_tables_[shndx];
8ffa3667
DK
1046 }
1047
1048 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1049 void
2ea97941 1050 set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
8ffa3667 1051 {
2ea97941
ILT
1052 gold_assert(shndx < this->stub_tables_.size());
1053 this->stub_tables_[shndx] = stub_table;
8ffa3667
DK
1054 }
1055
1056 // Whether a local symbol is a THUMB function. R_SYM is the symbol table
1057 // index. This is only valid after do_count_local_symbol is called.
1058 bool
1059 local_symbol_is_thumb_function(unsigned int r_sym) const
1060 {
1061 gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1062 return this->local_symbol_is_thumb_function_[r_sym];
1063 }
1064
1065 // Scan all relocation sections for stub generation.
1066 void
1067 scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1068 const Layout*);
1069
1070 // Convert regular input section with index SHNDX to a relaxed section.
1071 void
2ea97941 1072 convert_input_section_to_relaxed_section(unsigned shndx)
8ffa3667
DK
1073 {
1074 // The stubs have relocations and we need to process them after writing
1075 // out the stubs. So relocation now must follow section write.
2ea97941 1076 this->invalidate_section_offset(shndx);
8ffa3667
DK
1077 this->set_relocs_must_follow_section_writes();
1078 }
1079
1080 // Downcast a base pointer to an Arm_relobj pointer. This is
1081 // not type-safe but we only use Arm_relobj not the base class.
1082 static Arm_relobj<big_endian>*
2ea97941
ILT
1083 as_arm_relobj(Relobj* relobj)
1084 { return static_cast<Arm_relobj<big_endian>*>(relobj); }
8ffa3667 1085
d5b40221
DK
1086 // Processor-specific flags in ELF file header. This is valid only after
1087 // reading symbols.
1088 elfcpp::Elf_Word
1089 processor_specific_flags() const
1090 { return this->processor_specific_flags_; }
1091
a0351a69
DK
1092 // Attribute section data This is the contents of the .ARM.attribute section
1093 // if there is one.
1094 const Attributes_section_data*
1095 attributes_section_data() const
1096 { return this->attributes_section_data_; }
1097
8ffa3667
DK
1098 protected:
1099 // Post constructor setup.
1100 void
1101 do_setup()
1102 {
1103 // Call parent's setup method.
1104 Sized_relobj<32, big_endian>::do_setup();
1105
1106 // Initialize look-up tables.
1107 Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1108 this->stub_tables_.swap(empty_stub_table_list);
1109 }
1110
1111 // Count the local symbols.
1112 void
1113 do_count_local_symbols(Stringpool_template<char>*,
1114 Stringpool_template<char>*);
1115
1116 void
43d12afe 1117 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
8ffa3667
DK
1118 const unsigned char* pshdrs,
1119 typename Sized_relobj<32, big_endian>::Views* pivews);
1120
d5b40221
DK
1121 // Read the symbol information.
1122 void
1123 do_read_symbols(Read_symbols_data* sd);
1124
8ffa3667
DK
1125 private:
1126 // List of stub tables.
1127 typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1128 Stub_table_list stub_tables_;
1129 // Bit vector to tell if a local symbol is a thumb function or not.
1130 // This is only valid after do_count_local_symbol is called.
1131 std::vector<bool> local_symbol_is_thumb_function_;
d5b40221
DK
1132 // processor-specific flags in ELF file header.
1133 elfcpp::Elf_Word processor_specific_flags_;
a0351a69
DK
1134 // Object attributes if there is an .ARM.attributes section or NULL.
1135 Attributes_section_data* attributes_section_data_;
d5b40221
DK
1136};
1137
1138// Arm_dynobj class.
1139
1140template<bool big_endian>
1141class Arm_dynobj : public Sized_dynobj<32, big_endian>
1142{
1143 public:
2ea97941 1144 Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
d5b40221 1145 const elfcpp::Ehdr<32, big_endian>& ehdr)
2ea97941
ILT
1146 : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1147 processor_specific_flags_(0), attributes_section_data_(NULL)
d5b40221
DK
1148 { }
1149
1150 ~Arm_dynobj()
a0351a69 1151 { delete this->attributes_section_data_; }
d5b40221
DK
1152
1153 // Downcast a base pointer to an Arm_relobj pointer. This is
1154 // not type-safe but we only use Arm_relobj not the base class.
1155 static Arm_dynobj<big_endian>*
1156 as_arm_dynobj(Dynobj* dynobj)
1157 { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1158
1159 // Processor-specific flags in ELF file header. This is valid only after
1160 // reading symbols.
1161 elfcpp::Elf_Word
1162 processor_specific_flags() const
1163 { return this->processor_specific_flags_; }
1164
a0351a69
DK
1165 // Attributes section data.
1166 const Attributes_section_data*
1167 attributes_section_data() const
1168 { return this->attributes_section_data_; }
1169
d5b40221
DK
1170 protected:
1171 // Read the symbol information.
1172 void
1173 do_read_symbols(Read_symbols_data* sd);
1174
1175 private:
1176 // processor-specific flags in ELF file header.
1177 elfcpp::Elf_Word processor_specific_flags_;
a0351a69
DK
1178 // Object attributes if there is an .ARM.attributes section or NULL.
1179 Attributes_section_data* attributes_section_data_;
8ffa3667
DK
1180};
1181
e9bbb538
DK
1182// Functor to read reloc addends during stub generation.
1183
1184template<int sh_type, bool big_endian>
1185struct Stub_addend_reader
1186{
1187 // Return the addend for a relocation of a particular type. Depending
1188 // on whether this is a REL or RELA relocation, read the addend from a
1189 // view or from a Reloc object.
1190 elfcpp::Elf_types<32>::Elf_Swxword
1191 operator()(
1192 unsigned int /* r_type */,
1193 const unsigned char* /* view */,
1194 const typename Reloc_types<sh_type,
ebd95253 1195 32, big_endian>::Reloc& /* reloc */) const;
e9bbb538
DK
1196};
1197
1198// Specialized Stub_addend_reader for SHT_REL type relocation sections.
1199
1200template<bool big_endian>
1201struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1202{
1203 elfcpp::Elf_types<32>::Elf_Swxword
1204 operator()(
1205 unsigned int,
1206 const unsigned char*,
1207 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1208};
1209
1210// Specialized Stub_addend_reader for RELA type relocation sections.
1211// We currently do not handle RELA type relocation sections but it is trivial
1212// to implement the addend reader. This is provided for completeness and to
1213// make it easier to add support for RELA relocation sections in the future.
1214
1215template<bool big_endian>
1216struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1217{
1218 elfcpp::Elf_types<32>::Elf_Swxword
1219 operator()(
1220 unsigned int,
1221 const unsigned char*,
1222 const typename Reloc_types<elfcpp::SHT_RELA, 32,
ebd95253
DK
1223 big_endian>::Reloc& reloc) const
1224 { return reloc.get_r_addend(); }
e9bbb538
DK
1225};
1226
c121c671
DK
1227// Utilities for manipulating integers of up to 32-bits
1228
1229namespace utils
1230{
1231 // Sign extend an n-bit unsigned integer stored in an uint32_t into
1232 // an int32_t. NO_BITS must be between 1 to 32.
1233 template<int no_bits>
1234 static inline int32_t
1235 sign_extend(uint32_t bits)
1236 {
96d49306 1237 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
1238 if (no_bits == 32)
1239 return static_cast<int32_t>(bits);
1240 uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1241 bits &= mask;
1242 uint32_t top_bit = 1U << (no_bits - 1);
1243 int32_t as_signed = static_cast<int32_t>(bits);
1244 return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1245 }
1246
1247 // Detects overflow of an NO_BITS integer stored in a uint32_t.
1248 template<int no_bits>
1249 static inline bool
1250 has_overflow(uint32_t bits)
1251 {
96d49306 1252 gold_assert(no_bits >= 0 && no_bits <= 32);
c121c671
DK
1253 if (no_bits == 32)
1254 return false;
1255 int32_t max = (1 << (no_bits - 1)) - 1;
1256 int32_t min = -(1 << (no_bits - 1));
1257 int32_t as_signed = static_cast<int32_t>(bits);
1258 return as_signed > max || as_signed < min;
1259 }
1260
5e445df6
ILT
1261 // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1262 // fits in the given number of bits as either a signed or unsigned value.
1263 // For example, has_signed_unsigned_overflow<8> would check
1264 // -128 <= bits <= 255
1265 template<int no_bits>
1266 static inline bool
1267 has_signed_unsigned_overflow(uint32_t bits)
1268 {
1269 gold_assert(no_bits >= 2 && no_bits <= 32);
1270 if (no_bits == 32)
1271 return false;
1272 int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1273 int32_t min = -(1 << (no_bits - 1));
1274 int32_t as_signed = static_cast<int32_t>(bits);
1275 return as_signed > max || as_signed < min;
1276 }
1277
c121c671
DK
1278 // Select bits from A and B using bits in MASK. For each n in [0..31],
1279 // the n-th bit in the result is chosen from the n-th bits of A and B.
1280 // A zero selects A and a one selects B.
1281 static inline uint32_t
1282 bit_select(uint32_t a, uint32_t b, uint32_t mask)
1283 { return (a & ~mask) | (b & mask); }
1284};
1285
4a657b0d
DK
1286template<bool big_endian>
1287class Target_arm : public Sized_target<32, big_endian>
1288{
1289 public:
1290 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1291 Reloc_section;
1292
2daedcd6
DK
1293 // When were are relocating a stub, we pass this as the relocation number.
1294 static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1295
a6d1ef57
DK
1296 Target_arm()
1297 : Sized_target<32, big_endian>(&arm_info),
1298 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1299 copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
a0351a69
DK
1300 stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1301 should_force_pic_veneer_(false), arm_input_section_map_(),
1302 attributes_section_data_(NULL)
a6d1ef57 1303 { }
4a657b0d 1304
b569affa
DK
1305 // Whether we can use BLX.
1306 bool
1307 may_use_blx() const
1308 { return this->may_use_blx_; }
1309
1310 // Set use-BLX flag.
1311 void
1312 set_may_use_blx(bool value)
1313 { this->may_use_blx_ = value; }
1314
1315 // Whether we force PCI branch veneers.
1316 bool
1317 should_force_pic_veneer() const
1318 { return this->should_force_pic_veneer_; }
1319
1320 // Set PIC veneer flag.
1321 void
1322 set_should_force_pic_veneer(bool value)
1323 { this->should_force_pic_veneer_ = value; }
1324
1325 // Whether we use THUMB-2 instructions.
1326 bool
1327 using_thumb2() const
1328 {
a0351a69
DK
1329 Object_attribute* attr =
1330 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1331 int arch = attr->int_value();
1332 return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
b569affa
DK
1333 }
1334
1335 // Whether we use THUMB/THUMB-2 instructions only.
1336 bool
1337 using_thumb_only() const
1338 {
a0351a69
DK
1339 Object_attribute* attr =
1340 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1341 if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1342 && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1343 return false;
1344 attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1345 return attr->int_value() == 'M';
b569affa
DK
1346 }
1347
d204b6e9
DK
1348 // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
1349 bool
1350 may_use_arm_nop() const
1351 {
a0351a69
DK
1352 Object_attribute* attr =
1353 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1354 int arch = attr->int_value();
1355 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1356 || arch == elfcpp::TAG_CPU_ARCH_V6K
1357 || arch == elfcpp::TAG_CPU_ARCH_V7
1358 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
d204b6e9
DK
1359 }
1360
51938283
DK
1361 // Whether we have THUMB-2 NOP.W instruction.
1362 bool
1363 may_use_thumb2_nop() const
1364 {
a0351a69
DK
1365 Object_attribute* attr =
1366 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1367 int arch = attr->int_value();
1368 return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1369 || arch == elfcpp::TAG_CPU_ARCH_V7
1370 || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
51938283
DK
1371 }
1372
4a657b0d
DK
1373 // Process the relocations to determine unreferenced sections for
1374 // garbage collection.
1375 void
ad0f2072 1376 gc_process_relocs(Symbol_table* symtab,
4a657b0d
DK
1377 Layout* layout,
1378 Sized_relobj<32, big_endian>* object,
1379 unsigned int data_shndx,
1380 unsigned int sh_type,
1381 const unsigned char* prelocs,
1382 size_t reloc_count,
1383 Output_section* output_section,
1384 bool needs_special_offset_handling,
1385 size_t local_symbol_count,
1386 const unsigned char* plocal_symbols);
1387
1388 // Scan the relocations to look for symbol adjustments.
1389 void
ad0f2072 1390 scan_relocs(Symbol_table* symtab,
4a657b0d
DK
1391 Layout* layout,
1392 Sized_relobj<32, big_endian>* object,
1393 unsigned int data_shndx,
1394 unsigned int sh_type,
1395 const unsigned char* prelocs,
1396 size_t reloc_count,
1397 Output_section* output_section,
1398 bool needs_special_offset_handling,
1399 size_t local_symbol_count,
1400 const unsigned char* plocal_symbols);
1401
1402 // Finalize the sections.
1403 void
f59f41f3 1404 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
4a657b0d 1405
94cdfcff 1406 // Return the value to use for a dynamic symbol which requires special
4a657b0d
DK
1407 // treatment.
1408 uint64_t
1409 do_dynsym_value(const Symbol*) const;
1410
1411 // Relocate a section.
1412 void
1413 relocate_section(const Relocate_info<32, big_endian>*,
1414 unsigned int sh_type,
1415 const unsigned char* prelocs,
1416 size_t reloc_count,
1417 Output_section* output_section,
1418 bool needs_special_offset_handling,
1419 unsigned char* view,
ebabffbd 1420 Arm_address view_address,
364c7fa5
ILT
1421 section_size_type view_size,
1422 const Reloc_symbol_changes*);
4a657b0d
DK
1423
1424 // Scan the relocs during a relocatable link.
1425 void
ad0f2072 1426 scan_relocatable_relocs(Symbol_table* symtab,
4a657b0d
DK
1427 Layout* layout,
1428 Sized_relobj<32, big_endian>* object,
1429 unsigned int data_shndx,
1430 unsigned int sh_type,
1431 const unsigned char* prelocs,
1432 size_t reloc_count,
1433 Output_section* output_section,
1434 bool needs_special_offset_handling,
1435 size_t local_symbol_count,
1436 const unsigned char* plocal_symbols,
1437 Relocatable_relocs*);
1438
1439 // Relocate a section during a relocatable link.
1440 void
1441 relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1442 unsigned int sh_type,
1443 const unsigned char* prelocs,
1444 size_t reloc_count,
1445 Output_section* output_section,
1446 off_t offset_in_output_section,
1447 const Relocatable_relocs*,
1448 unsigned char* view,
ebabffbd 1449 Arm_address view_address,
4a657b0d
DK
1450 section_size_type view_size,
1451 unsigned char* reloc_view,
1452 section_size_type reloc_view_size);
1453
1454 // Return whether SYM is defined by the ABI.
1455 bool
1456 do_is_defined_by_abi(Symbol* sym) const
1457 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1458
94cdfcff
DK
1459 // Return the size of the GOT section.
1460 section_size_type
1461 got_size()
1462 {
1463 gold_assert(this->got_ != NULL);
1464 return this->got_->data_size();
1465 }
1466
4a657b0d 1467 // Map platform-specific reloc types
a6d1ef57
DK
1468 static unsigned int
1469 get_real_reloc_type (unsigned int r_type);
4a657b0d 1470
55da9579
DK
1471 //
1472 // Methods to support stub-generations.
1473 //
1474
1475 // Return the stub factory
1476 const Stub_factory&
1477 stub_factory() const
1478 { return this->stub_factory_; }
1479
1480 // Make a new Arm_input_section object.
1481 Arm_input_section<big_endian>*
1482 new_arm_input_section(Relobj*, unsigned int);
1483
1484 // Find the Arm_input_section object corresponding to the SHNDX-th input
1485 // section of RELOBJ.
1486 Arm_input_section<big_endian>*
2ea97941 1487 find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
55da9579
DK
1488
1489 // Make a new Stub_table
1490 Stub_table<big_endian>*
1491 new_stub_table(Arm_input_section<big_endian>*);
1492
eb44217c
DK
1493 // Scan a section for stub generation.
1494 void
1495 scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
1496 const unsigned char*, size_t, Output_section*,
1497 bool, const unsigned char*, Arm_address,
1498 section_size_type);
1499
43d12afe
DK
1500 // Relocate a stub.
1501 void
1502 relocate_stub(Reloc_stub*, const Relocate_info<32, big_endian>*,
1503 Output_section*, unsigned char*, Arm_address,
1504 section_size_type);
1505
b569affa 1506 // Get the default ARM target.
43d12afe 1507 static Target_arm<big_endian>*
b569affa
DK
1508 default_target()
1509 {
1510 gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1511 && parameters->target().is_big_endian() == big_endian);
43d12afe
DK
1512 return static_cast<Target_arm<big_endian>*>(
1513 parameters->sized_target<32, big_endian>());
b569affa
DK
1514 }
1515
55da9579
DK
1516 // Whether relocation type uses LSB to distinguish THUMB addresses.
1517 static bool
1518 reloc_uses_thumb_bit(unsigned int r_type);
1519
d5b40221 1520 protected:
eb44217c
DK
1521 // Make an ELF object.
1522 Object*
1523 do_make_elf_object(const std::string&, Input_file*, off_t,
1524 const elfcpp::Ehdr<32, big_endian>& ehdr);
1525
1526 Object*
1527 do_make_elf_object(const std::string&, Input_file*, off_t,
1528 const elfcpp::Ehdr<32, !big_endian>&)
1529 { gold_unreachable(); }
1530
1531 Object*
1532 do_make_elf_object(const std::string&, Input_file*, off_t,
1533 const elfcpp::Ehdr<64, false>&)
1534 { gold_unreachable(); }
1535
1536 Object*
1537 do_make_elf_object(const std::string&, Input_file*, off_t,
1538 const elfcpp::Ehdr<64, true>&)
1539 { gold_unreachable(); }
1540
1541 // Make an output section.
1542 Output_section*
1543 do_make_output_section(const char* name, elfcpp::Elf_Word type,
1544 elfcpp::Elf_Xword flags)
1545 { return new Arm_output_section<big_endian>(name, type, flags); }
1546
d5b40221
DK
1547 void
1548 do_adjust_elf_header(unsigned char* view, int len) const;
1549
eb44217c
DK
1550 // We only need to generate stubs, and hence perform relaxation if we are
1551 // not doing relocatable linking.
1552 bool
1553 do_may_relax() const
1554 { return !parameters->options().relocatable(); }
1555
1556 bool
1557 do_relax(int, const Input_objects*, Symbol_table*, Layout*);
1558
a0351a69
DK
1559 // Determine whether an object attribute tag takes an integer, a
1560 // string or both.
1561 int
1562 do_attribute_arg_type(int tag) const;
1563
1564 // Reorder tags during output.
1565 int
1566 do_attributes_order(int num) const;
1567
4a657b0d
DK
1568 private:
1569 // The class which scans relocations.
1570 class Scan
1571 {
1572 public:
1573 Scan()
bec53400 1574 : issued_non_pic_error_(false)
4a657b0d
DK
1575 { }
1576
1577 inline void
ad0f2072 1578 local(Symbol_table* symtab, Layout* layout, Target_arm* target,
4a657b0d
DK
1579 Sized_relobj<32, big_endian>* object,
1580 unsigned int data_shndx,
1581 Output_section* output_section,
1582 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1583 const elfcpp::Sym<32, big_endian>& lsym);
1584
1585 inline void
ad0f2072 1586 global(Symbol_table* symtab, Layout* layout, Target_arm* target,
4a657b0d
DK
1587 Sized_relobj<32, big_endian>* object,
1588 unsigned int data_shndx,
1589 Output_section* output_section,
1590 const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1591 Symbol* gsym);
1592
1593 private:
1594 static void
1595 unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1596 unsigned int r_type);
1597
1598 static void
1599 unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1600 unsigned int r_type, Symbol*);
bec53400
DK
1601
1602 void
1603 check_non_pic(Relobj*, unsigned int r_type);
1604
1605 // Almost identical to Symbol::needs_plt_entry except that it also
1606 // handles STT_ARM_TFUNC.
1607 static bool
1608 symbol_needs_plt_entry(const Symbol* sym)
1609 {
1610 // An undefined symbol from an executable does not need a PLT entry.
1611 if (sym->is_undefined() && !parameters->options().shared())
1612 return false;
1613
1614 return (!parameters->doing_static_link()
1615 && (sym->type() == elfcpp::STT_FUNC
1616 || sym->type() == elfcpp::STT_ARM_TFUNC)
1617 && (sym->is_from_dynobj()
1618 || sym->is_undefined()
1619 || sym->is_preemptible()));
1620 }
1621
1622 // Whether we have issued an error about a non-PIC compilation.
1623 bool issued_non_pic_error_;
4a657b0d
DK
1624 };
1625
1626 // The class which implements relocation.
1627 class Relocate
1628 {
1629 public:
1630 Relocate()
1631 { }
1632
1633 ~Relocate()
1634 { }
1635
bec53400
DK
1636 // Return whether the static relocation needs to be applied.
1637 inline bool
1638 should_apply_static_reloc(const Sized_symbol<32>* gsym,
1639 int ref_flags,
1640 bool is_32bit,
1641 Output_section* output_section);
1642
4a657b0d
DK
1643 // Do a relocation. Return false if the caller should not issue
1644 // any warnings about this relocation.
1645 inline bool
1646 relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1647 Output_section*, size_t relnum,
1648 const elfcpp::Rel<32, big_endian>&,
1649 unsigned int r_type, const Sized_symbol<32>*,
1650 const Symbol_value<32>*,
ebabffbd 1651 unsigned char*, Arm_address,
4a657b0d 1652 section_size_type);
c121c671
DK
1653
1654 // Return whether we want to pass flag NON_PIC_REF for this
f4e5969c
DK
1655 // reloc. This means the relocation type accesses a symbol not via
1656 // GOT or PLT.
c121c671
DK
1657 static inline bool
1658 reloc_is_non_pic (unsigned int r_type)
1659 {
1660 switch (r_type)
1661 {
f4e5969c
DK
1662 // These relocation types reference GOT or PLT entries explicitly.
1663 case elfcpp::R_ARM_GOT_BREL:
1664 case elfcpp::R_ARM_GOT_ABS:
1665 case elfcpp::R_ARM_GOT_PREL:
1666 case elfcpp::R_ARM_GOT_BREL12:
1667 case elfcpp::R_ARM_PLT32_ABS:
1668 case elfcpp::R_ARM_TLS_GD32:
1669 case elfcpp::R_ARM_TLS_LDM32:
1670 case elfcpp::R_ARM_TLS_IE32:
1671 case elfcpp::R_ARM_TLS_IE12GP:
1672
1673 // These relocate types may use PLT entries.
c121c671 1674 case elfcpp::R_ARM_CALL:
f4e5969c 1675 case elfcpp::R_ARM_THM_CALL:
c121c671 1676 case elfcpp::R_ARM_JUMP24:
f4e5969c
DK
1677 case elfcpp::R_ARM_THM_JUMP24:
1678 case elfcpp::R_ARM_THM_JUMP19:
1679 case elfcpp::R_ARM_PLT32:
1680 case elfcpp::R_ARM_THM_XPC22:
c121c671 1681 return false;
f4e5969c
DK
1682
1683 default:
1684 return true;
c121c671
DK
1685 }
1686 }
4a657b0d
DK
1687 };
1688
1689 // A class which returns the size required for a relocation type,
1690 // used while scanning relocs during a relocatable link.
1691 class Relocatable_size_for_reloc
1692 {
1693 public:
1694 unsigned int
1695 get_size_for_reloc(unsigned int, Relobj*);
1696 };
1697
94cdfcff
DK
1698 // Get the GOT section, creating it if necessary.
1699 Output_data_got<32, big_endian>*
1700 got_section(Symbol_table*, Layout*);
1701
1702 // Get the GOT PLT section.
1703 Output_data_space*
1704 got_plt_section() const
1705 {
1706 gold_assert(this->got_plt_ != NULL);
1707 return this->got_plt_;
1708 }
1709
1710 // Create a PLT entry for a global symbol.
1711 void
1712 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1713
1714 // Get the PLT section.
1715 const Output_data_plt_arm<big_endian>*
1716 plt_section() const
1717 {
1718 gold_assert(this->plt_ != NULL);
1719 return this->plt_;
1720 }
1721
1722 // Get the dynamic reloc section, creating it if necessary.
1723 Reloc_section*
1724 rel_dyn_section(Layout*);
1725
1726 // Return true if the symbol may need a COPY relocation.
1727 // References from an executable object to non-function symbols
1728 // defined in a dynamic object may need a COPY relocation.
1729 bool
1730 may_need_copy_reloc(Symbol* gsym)
1731 {
966d4097
DK
1732 return (gsym->type() != elfcpp::STT_ARM_TFUNC
1733 && gsym->may_need_copy_reloc());
94cdfcff
DK
1734 }
1735
1736 // Add a potential copy relocation.
1737 void
1738 copy_reloc(Symbol_table* symtab, Layout* layout,
1739 Sized_relobj<32, big_endian>* object,
2ea97941 1740 unsigned int shndx, Output_section* output_section,
94cdfcff
DK
1741 Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1742 {
1743 this->copy_relocs_.copy_reloc(symtab, layout,
1744 symtab->get_sized_symbol<32>(sym),
2ea97941 1745 object, shndx, output_section, reloc,
94cdfcff
DK
1746 this->rel_dyn_section(layout));
1747 }
1748
d5b40221
DK
1749 // Whether two EABI versions are compatible.
1750 static bool
1751 are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1752
1753 // Merge processor-specific flags from input object and those in the ELF
1754 // header of the output.
1755 void
1756 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1757
a0351a69
DK
1758 // Get the secondary compatible architecture.
1759 static int
1760 get_secondary_compatible_arch(const Attributes_section_data*);
1761
1762 // Set the secondary compatible architecture.
1763 static void
1764 set_secondary_compatible_arch(Attributes_section_data*, int);
1765
1766 static int
1767 tag_cpu_arch_combine(const char*, int, int*, int, int);
1768
1769 // Helper to print AEABI enum tag value.
1770 static std::string
1771 aeabi_enum_name(unsigned int);
1772
1773 // Return string value for TAG_CPU_name.
1774 static std::string
1775 tag_cpu_name_value(unsigned int);
1776
1777 // Merge object attributes from input object and those in the output.
1778 void
1779 merge_object_attributes(const char*, const Attributes_section_data*);
1780
1781 // Helper to get an AEABI object attribute
1782 Object_attribute*
1783 get_aeabi_object_attribute(int tag) const
1784 {
1785 Attributes_section_data* pasd = this->attributes_section_data_;
1786 gold_assert(pasd != NULL);
1787 Object_attribute* attr =
1788 pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
1789 gold_assert(attr != NULL);
1790 return attr;
1791 }
1792
eb44217c
DK
1793 //
1794 // Methods to support stub-generations.
1795 //
d5b40221 1796
eb44217c
DK
1797 // Group input sections for stub generation.
1798 void
1799 group_sections(Layout*, section_size_type, bool);
d5b40221 1800
eb44217c
DK
1801 // Scan a relocation for stub generation.
1802 void
1803 scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
1804 const Sized_symbol<32>*, unsigned int,
1805 const Symbol_value<32>*,
1806 elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
d5b40221 1807
eb44217c
DK
1808 // Scan a relocation section for stub.
1809 template<int sh_type>
1810 void
1811 scan_reloc_section_for_stubs(
1812 const Relocate_info<32, big_endian>* relinfo,
1813 const unsigned char* prelocs,
1814 size_t reloc_count,
1815 Output_section* output_section,
1816 bool needs_special_offset_handling,
1817 const unsigned char* view,
1818 elfcpp::Elf_types<32>::Elf_Addr view_address,
1819 section_size_type);
d5b40221 1820
4a657b0d
DK
1821 // Information about this specific target which we pass to the
1822 // general Target structure.
1823 static const Target::Target_info arm_info;
94cdfcff
DK
1824
1825 // The types of GOT entries needed for this platform.
1826 enum Got_type
1827 {
1828 GOT_TYPE_STANDARD = 0 // GOT entry for a regular symbol
1829 };
1830
55da9579
DK
1831 typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
1832
1833 // Map input section to Arm_input_section.
1834 typedef Unordered_map<Input_section_specifier,
1835 Arm_input_section<big_endian>*,
1836 Input_section_specifier::hash,
1837 Input_section_specifier::equal_to>
1838 Arm_input_section_map;
1839
94cdfcff
DK
1840 // The GOT section.
1841 Output_data_got<32, big_endian>* got_;
1842 // The PLT section.
1843 Output_data_plt_arm<big_endian>* plt_;
1844 // The GOT PLT section.
1845 Output_data_space* got_plt_;
1846 // The dynamic reloc section.
1847 Reloc_section* rel_dyn_;
1848 // Relocs saved to avoid a COPY reloc.
1849 Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1850 // Space for variables copied with a COPY reloc.
1851 Output_data_space* dynbss_;
55da9579
DK
1852 // Vector of Stub_tables created.
1853 Stub_table_list stub_tables_;
1854 // Stub factory.
1855 const Stub_factory &stub_factory_;
b569affa
DK
1856 // Whether we can use BLX.
1857 bool may_use_blx_;
1858 // Whether we force PIC branch veneers.
1859 bool should_force_pic_veneer_;
eb44217c
DK
1860 // Map for locating Arm_input_sections.
1861 Arm_input_section_map arm_input_section_map_;
a0351a69
DK
1862 // Attributes section data in output.
1863 Attributes_section_data* attributes_section_data_;
4a657b0d
DK
1864};
1865
1866template<bool big_endian>
1867const Target::Target_info Target_arm<big_endian>::arm_info =
1868{
1869 32, // size
1870 big_endian, // is_big_endian
1871 elfcpp::EM_ARM, // machine_code
1872 false, // has_make_symbol
1873 false, // has_resolve
1874 false, // has_code_fill
1875 true, // is_default_stack_executable
1876 '\0', // wrap_char
1877 "/usr/lib/libc.so.1", // dynamic_linker
1878 0x8000, // default_text_segment_address
1879 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
1880 0x1000, // common_pagesize (overridable by -z common-page-size)
1881 elfcpp::SHN_UNDEF, // small_common_shndx
1882 elfcpp::SHN_UNDEF, // large_common_shndx
1883 0, // small_common_section_flags
05a352e6
DK
1884 0, // large_common_section_flags
1885 ".ARM.attributes", // attributes_section
1886 "aeabi" // attributes_vendor
4a657b0d
DK
1887};
1888
c121c671
DK
1889// Arm relocate functions class
1890//
1891
1892template<bool big_endian>
1893class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1894{
1895 public:
1896 typedef enum
1897 {
1898 STATUS_OKAY, // No error during relocation.
1899 STATUS_OVERFLOW, // Relocation oveflow.
1900 STATUS_BAD_RELOC // Relocation cannot be applied.
1901 } Status;
1902
1903 private:
1904 typedef Relocate_functions<32, big_endian> Base;
1905 typedef Arm_relocate_functions<big_endian> This;
1906
fd3c5f0b
ILT
1907 // Encoding of imm16 argument for movt and movw ARM instructions
1908 // from ARM ARM:
1909 //
1910 // imm16 := imm4 | imm12
1911 //
1912 // 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
1913 // +-------+---------------+-------+-------+-----------------------+
1914 // | | |imm4 | |imm12 |
1915 // +-------+---------------+-------+-------+-----------------------+
1916
1917 // Extract the relocation addend from VAL based on the ARM
1918 // instruction encoding described above.
1919 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1920 extract_arm_movw_movt_addend(
1921 typename elfcpp::Swap<32, big_endian>::Valtype val)
1922 {
1923 // According to the Elf ABI for ARM Architecture the immediate
1924 // field is sign-extended to form the addend.
1925 return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1926 }
1927
1928 // Insert X into VAL based on the ARM instruction encoding described
1929 // above.
1930 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1931 insert_val_arm_movw_movt(
1932 typename elfcpp::Swap<32, big_endian>::Valtype val,
1933 typename elfcpp::Swap<32, big_endian>::Valtype x)
1934 {
1935 val &= 0xfff0f000;
1936 val |= x & 0x0fff;
1937 val |= (x & 0xf000) << 4;
1938 return val;
1939 }
1940
1941 // Encoding of imm16 argument for movt and movw Thumb2 instructions
1942 // from ARM ARM:
1943 //
1944 // imm16 := imm4 | i | imm3 | imm8
1945 //
1946 // 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
1947 // +---------+-+-----------+-------++-+-----+-------+---------------+
1948 // | |i| |imm4 || |imm3 | |imm8 |
1949 // +---------+-+-----------+-------++-+-----+-------+---------------+
1950
1951 // Extract the relocation addend from VAL based on the Thumb2
1952 // instruction encoding described above.
1953 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1954 extract_thumb_movw_movt_addend(
1955 typename elfcpp::Swap<32, big_endian>::Valtype val)
1956 {
1957 // According to the Elf ABI for ARM Architecture the immediate
1958 // field is sign-extended to form the addend.
1959 return utils::sign_extend<16>(((val >> 4) & 0xf000)
1960 | ((val >> 15) & 0x0800)
1961 | ((val >> 4) & 0x0700)
1962 | (val & 0x00ff));
1963 }
1964
1965 // Insert X into VAL based on the Thumb2 instruction encoding
1966 // described above.
1967 static inline typename elfcpp::Swap<32, big_endian>::Valtype
1968 insert_val_thumb_movw_movt(
1969 typename elfcpp::Swap<32, big_endian>::Valtype val,
1970 typename elfcpp::Swap<32, big_endian>::Valtype x)
1971 {
1972 val &= 0xfbf08f00;
1973 val |= (x & 0xf000) << 4;
1974 val |= (x & 0x0800) << 15;
1975 val |= (x & 0x0700) << 4;
1976 val |= (x & 0x00ff);
1977 return val;
1978 }
1979
d204b6e9
DK
1980 // Handle ARM long branches.
1981 static typename This::Status
1982 arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
1983 unsigned char *, const Sized_symbol<32>*,
1984 const Arm_relobj<big_endian>*, unsigned int,
1985 const Symbol_value<32>*, Arm_address, Arm_address, bool);
c121c671 1986
51938283
DK
1987 // Handle THUMB long branches.
1988 static typename This::Status
1989 thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
1990 unsigned char *, const Sized_symbol<32>*,
1991 const Arm_relobj<big_endian>*, unsigned int,
1992 const Symbol_value<32>*, Arm_address, Arm_address, bool);
1993
c121c671 1994 public:
5e445df6
ILT
1995
1996 // R_ARM_ABS8: S + A
1997 static inline typename This::Status
1998 abs8(unsigned char *view,
1999 const Sized_relobj<32, big_endian>* object,
be8fcb75 2000 const Symbol_value<32>* psymval)
5e445df6
ILT
2001 {
2002 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2003 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2004 Valtype* wv = reinterpret_cast<Valtype*>(view);
2005 Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2006 Reltype addend = utils::sign_extend<8>(val);
2daedcd6 2007 Reltype x = psymval->value(object, addend);
5e445df6
ILT
2008 val = utils::bit_select(val, x, 0xffU);
2009 elfcpp::Swap<8, big_endian>::writeval(wv, val);
2010 return (utils::has_signed_unsigned_overflow<8>(x)
2011 ? This::STATUS_OVERFLOW
2012 : This::STATUS_OKAY);
2013 }
2014
be8fcb75
ILT
2015 // R_ARM_THM_ABS5: S + A
2016 static inline typename This::Status
2017 thm_abs5(unsigned char *view,
2018 const Sized_relobj<32, big_endian>* object,
2019 const Symbol_value<32>* psymval)
2020 {
2021 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2022 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2023 Valtype* wv = reinterpret_cast<Valtype*>(view);
2024 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2025 Reltype addend = (val & 0x7e0U) >> 6;
2daedcd6 2026 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
2027 val = utils::bit_select(val, x << 6, 0x7e0U);
2028 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2029 return (utils::has_overflow<5>(x)
2030 ? This::STATUS_OVERFLOW
2031 : This::STATUS_OKAY);
2032 }
2033
2034 // R_ARM_ABS12: S + A
2035 static inline typename This::Status
2036 abs12(unsigned char *view,
51938283
DK
2037 const Sized_relobj<32, big_endian>* object,
2038 const Symbol_value<32>* psymval)
be8fcb75
ILT
2039 {
2040 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2041 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2042 Valtype* wv = reinterpret_cast<Valtype*>(view);
2043 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2044 Reltype addend = val & 0x0fffU;
2daedcd6 2045 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
2046 val = utils::bit_select(val, x, 0x0fffU);
2047 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2048 return (utils::has_overflow<12>(x)
2049 ? This::STATUS_OVERFLOW
2050 : This::STATUS_OKAY);
2051 }
2052
2053 // R_ARM_ABS16: S + A
2054 static inline typename This::Status
2055 abs16(unsigned char *view,
51938283
DK
2056 const Sized_relobj<32, big_endian>* object,
2057 const Symbol_value<32>* psymval)
be8fcb75
ILT
2058 {
2059 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2060 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2061 Valtype* wv = reinterpret_cast<Valtype*>(view);
2062 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2063 Reltype addend = utils::sign_extend<16>(val);
2daedcd6 2064 Reltype x = psymval->value(object, addend);
be8fcb75
ILT
2065 val = utils::bit_select(val, x, 0xffffU);
2066 elfcpp::Swap<16, big_endian>::writeval(wv, val);
2067 return (utils::has_signed_unsigned_overflow<16>(x)
2068 ? This::STATUS_OVERFLOW
2069 : This::STATUS_OKAY);
2070 }
2071
c121c671
DK
2072 // R_ARM_ABS32: (S + A) | T
2073 static inline typename This::Status
2074 abs32(unsigned char *view,
2075 const Sized_relobj<32, big_endian>* object,
2076 const Symbol_value<32>* psymval,
2daedcd6 2077 Arm_address thumb_bit)
c121c671
DK
2078 {
2079 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2080 Valtype* wv = reinterpret_cast<Valtype*>(view);
2081 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2daedcd6 2082 Valtype x = psymval->value(object, addend) | thumb_bit;
c121c671
DK
2083 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2084 return This::STATUS_OKAY;
2085 }
2086
2087 // R_ARM_REL32: (S + A) | T - P
2088 static inline typename This::Status
2089 rel32(unsigned char *view,
2090 const Sized_relobj<32, big_endian>* object,
2091 const Symbol_value<32>* psymval,
ebabffbd 2092 Arm_address address,
2daedcd6 2093 Arm_address thumb_bit)
c121c671
DK
2094 {
2095 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2096 Valtype* wv = reinterpret_cast<Valtype*>(view);
2097 Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2daedcd6 2098 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c121c671
DK
2099 elfcpp::Swap<32, big_endian>::writeval(wv, x);
2100 return This::STATUS_OKAY;
2101 }
2102
2103 // R_ARM_THM_CALL: (S + A) | T - P
2104 static inline typename This::Status
51938283
DK
2105 thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2106 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2107 unsigned int r_sym, const Symbol_value<32>* psymval,
2108 Arm_address address, Arm_address thumb_bit,
2109 bool is_weakly_undefined_without_plt)
c121c671 2110 {
51938283
DK
2111 return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
2112 object, r_sym, psymval, address, thumb_bit,
2113 is_weakly_undefined_without_plt);
2114 }
c121c671 2115
51938283
DK
2116 // R_ARM_THM_JUMP24: (S + A) | T - P
2117 static inline typename This::Status
2118 thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2119 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2120 unsigned int r_sym, const Symbol_value<32>* psymval,
2121 Arm_address address, Arm_address thumb_bit,
2122 bool is_weakly_undefined_without_plt)
2123 {
2124 return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
2125 object, r_sym, psymval, address, thumb_bit,
2126 is_weakly_undefined_without_plt);
2127 }
2128
2129 // R_ARM_THM_XPC22: (S + A) | T - P
2130 static inline typename This::Status
2131 thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2132 const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2133 unsigned int r_sym, const Symbol_value<32>* psymval,
2134 Arm_address address, Arm_address thumb_bit,
2135 bool is_weakly_undefined_without_plt)
2136 {
2137 return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
2138 object, r_sym, psymval, address, thumb_bit,
2139 is_weakly_undefined_without_plt);
c121c671
DK
2140 }
2141
2142 // R_ARM_BASE_PREL: B(S) + A - P
2143 static inline typename This::Status
2144 base_prel(unsigned char* view,
ebabffbd
DK
2145 Arm_address origin,
2146 Arm_address address)
c121c671
DK
2147 {
2148 Base::rel32(view, origin - address);
2149 return STATUS_OKAY;
2150 }
2151
be8fcb75
ILT
2152 // R_ARM_BASE_ABS: B(S) + A
2153 static inline typename This::Status
2154 base_abs(unsigned char* view,
f4e5969c 2155 Arm_address origin)
be8fcb75
ILT
2156 {
2157 Base::rel32(view, origin);
2158 return STATUS_OKAY;
2159 }
2160
c121c671
DK
2161 // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2162 static inline typename This::Status
2163 got_brel(unsigned char* view,
2164 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
2165 {
2166 Base::rel32(view, got_offset);
2167 return This::STATUS_OKAY;
2168 }
2169
f4e5969c 2170 // R_ARM_GOT_PREL: GOT(S) + A - P
7f5309a5 2171 static inline typename This::Status
f4e5969c
DK
2172 got_prel(unsigned char *view,
2173 Arm_address got_entry,
ebabffbd 2174 Arm_address address)
7f5309a5 2175 {
f4e5969c 2176 Base::rel32(view, got_entry - address);
7f5309a5
ILT
2177 return This::STATUS_OKAY;
2178 }
2179
c121c671
DK
2180 // R_ARM_PLT32: (S + A) | T - P
2181 static inline typename This::Status
d204b6e9
DK
2182 plt32(const Relocate_info<32, big_endian>* relinfo,
2183 unsigned char *view,
2184 const Sized_symbol<32>* gsym,
2185 const Arm_relobj<big_endian>* object,
2186 unsigned int r_sym,
c121c671 2187 const Symbol_value<32>* psymval,
ebabffbd 2188 Arm_address address,
d204b6e9
DK
2189 Arm_address thumb_bit,
2190 bool is_weakly_undefined_without_plt)
2191 {
2192 return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
2193 object, r_sym, psymval, address, thumb_bit,
2194 is_weakly_undefined_without_plt);
2195 }
2196
2197 // R_ARM_XPC25: (S + A) | T - P
2198 static inline typename This::Status
2199 xpc25(const Relocate_info<32, big_endian>* relinfo,
2200 unsigned char *view,
2201 const Sized_symbol<32>* gsym,
2202 const Arm_relobj<big_endian>* object,
2203 unsigned int r_sym,
2204 const Symbol_value<32>* psymval,
2205 Arm_address address,
2206 Arm_address thumb_bit,
2207 bool is_weakly_undefined_without_plt)
c121c671 2208 {
d204b6e9
DK
2209 return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
2210 object, r_sym, psymval, address, thumb_bit,
2211 is_weakly_undefined_without_plt);
c121c671
DK
2212 }
2213
2214 // R_ARM_CALL: (S + A) | T - P
2215 static inline typename This::Status
d204b6e9
DK
2216 call(const Relocate_info<32, big_endian>* relinfo,
2217 unsigned char *view,
2218 const Sized_symbol<32>* gsym,
2219 const Arm_relobj<big_endian>* object,
2220 unsigned int r_sym,
c121c671 2221 const Symbol_value<32>* psymval,
ebabffbd 2222 Arm_address address,
d204b6e9
DK
2223 Arm_address thumb_bit,
2224 bool is_weakly_undefined_without_plt)
c121c671 2225 {
d204b6e9
DK
2226 return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2227 object, r_sym, psymval, address, thumb_bit,
2228 is_weakly_undefined_without_plt);
c121c671
DK
2229 }
2230
2231 // R_ARM_JUMP24: (S + A) | T - P
2232 static inline typename This::Status
d204b6e9
DK
2233 jump24(const Relocate_info<32, big_endian>* relinfo,
2234 unsigned char *view,
2235 const Sized_symbol<32>* gsym,
2236 const Arm_relobj<big_endian>* object,
2237 unsigned int r_sym,
c121c671 2238 const Symbol_value<32>* psymval,
ebabffbd 2239 Arm_address address,
d204b6e9
DK
2240 Arm_address thumb_bit,
2241 bool is_weakly_undefined_without_plt)
c121c671 2242 {
d204b6e9
DK
2243 return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2244 object, r_sym, psymval, address, thumb_bit,
2245 is_weakly_undefined_without_plt);
c121c671
DK
2246 }
2247
2248 // R_ARM_PREL: (S + A) | T - P
2249 static inline typename This::Status
2250 prel31(unsigned char *view,
2251 const Sized_relobj<32, big_endian>* object,
2252 const Symbol_value<32>* psymval,
ebabffbd 2253 Arm_address address,
2daedcd6 2254 Arm_address thumb_bit)
c121c671
DK
2255 {
2256 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2257 Valtype* wv = reinterpret_cast<Valtype*>(view);
2258 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2259 Valtype addend = utils::sign_extend<31>(val);
2daedcd6 2260 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c121c671
DK
2261 val = utils::bit_select(val, x, 0x7fffffffU);
2262 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2263 return (utils::has_overflow<31>(x) ?
2264 This::STATUS_OVERFLOW : This::STATUS_OKAY);
2265 }
fd3c5f0b
ILT
2266
2267 // R_ARM_MOVW_ABS_NC: (S + A) | T
2268 static inline typename This::Status
2269 movw_abs_nc(unsigned char *view,
2270 const Sized_relobj<32, big_endian>* object,
2271 const Symbol_value<32>* psymval,
2daedcd6 2272 Arm_address thumb_bit)
fd3c5f0b
ILT
2273 {
2274 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2275 Valtype* wv = reinterpret_cast<Valtype*>(view);
2276 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2277 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 2278 Valtype x = psymval->value(object, addend) | thumb_bit;
fd3c5f0b
ILT
2279 val = This::insert_val_arm_movw_movt(val, x);
2280 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2281 return This::STATUS_OKAY;
2282 }
2283
2284 // R_ARM_MOVT_ABS: S + A
2285 static inline typename This::Status
2286 movt_abs(unsigned char *view,
2287 const Sized_relobj<32, big_endian>* object,
2288 const Symbol_value<32>* psymval)
2289 {
2290 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2291 Valtype* wv = reinterpret_cast<Valtype*>(view);
2292 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2293 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 2294 Valtype x = psymval->value(object, addend) >> 16;
fd3c5f0b
ILT
2295 val = This::insert_val_arm_movw_movt(val, x);
2296 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2297 return This::STATUS_OKAY;
2298 }
2299
2300 // R_ARM_THM_MOVW_ABS_NC: S + A | T
2301 static inline typename This::Status
2302 thm_movw_abs_nc(unsigned char *view,
2303 const Sized_relobj<32, big_endian>* object,
2304 const Symbol_value<32>* psymval,
2daedcd6 2305 Arm_address thumb_bit)
fd3c5f0b
ILT
2306 {
2307 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2308 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2309 Valtype* wv = reinterpret_cast<Valtype*>(view);
2310 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2311 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2312 Reltype addend = extract_thumb_movw_movt_addend(val);
2daedcd6 2313 Reltype x = psymval->value(object, addend) | thumb_bit;
fd3c5f0b
ILT
2314 val = This::insert_val_thumb_movw_movt(val, x);
2315 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2316 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2317 return This::STATUS_OKAY;
2318 }
2319
2320 // R_ARM_THM_MOVT_ABS: S + A
2321 static inline typename This::Status
2322 thm_movt_abs(unsigned char *view,
2323 const Sized_relobj<32, big_endian>* object,
2324 const Symbol_value<32>* psymval)
2325 {
2326 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2327 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2328 Valtype* wv = reinterpret_cast<Valtype*>(view);
2329 Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2330 | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2331 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2daedcd6 2332 Reltype x = psymval->value(object, addend) >> 16;
fd3c5f0b
ILT
2333 val = This::insert_val_thumb_movw_movt(val, x);
2334 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2335 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2336 return This::STATUS_OKAY;
2337 }
2338
c2a122b6
ILT
2339 // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2340 static inline typename This::Status
2341 movw_prel_nc(unsigned char *view,
2342 const Sized_relobj<32, big_endian>* object,
2343 const Symbol_value<32>* psymval,
ebabffbd 2344 Arm_address address,
2daedcd6 2345 Arm_address thumb_bit)
c2a122b6
ILT
2346 {
2347 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2348 Valtype* wv = reinterpret_cast<Valtype*>(view);
2349 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2350 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 2351 Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
c2a122b6
ILT
2352 val = This::insert_val_arm_movw_movt(val, x);
2353 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2354 return This::STATUS_OKAY;
2355 }
2356
2357 // R_ARM_MOVT_PREL: S + A - P
2358 static inline typename This::Status
2359 movt_prel(unsigned char *view,
2360 const Sized_relobj<32, big_endian>* object,
2361 const Symbol_value<32>* psymval,
ebabffbd 2362 Arm_address address)
c2a122b6
ILT
2363 {
2364 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2365 Valtype* wv = reinterpret_cast<Valtype*>(view);
2366 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2367 Valtype addend = This::extract_arm_movw_movt_addend(val);
2daedcd6 2368 Valtype x = (psymval->value(object, addend) - address) >> 16;
c2a122b6
ILT
2369 val = This::insert_val_arm_movw_movt(val, x);
2370 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2371 return This::STATUS_OKAY;
2372 }
2373
2374 // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2375 static inline typename This::Status
2376 thm_movw_prel_nc(unsigned char *view,
2377 const Sized_relobj<32, big_endian>* object,
2378 const Symbol_value<32>* psymval,
ebabffbd 2379 Arm_address address,
2daedcd6 2380 Arm_address thumb_bit)
c2a122b6
ILT
2381 {
2382 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2383 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2384 Valtype* wv = reinterpret_cast<Valtype*>(view);
2385 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2386 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2387 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2daedcd6 2388 Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
c2a122b6
ILT
2389 val = This::insert_val_thumb_movw_movt(val, x);
2390 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2391 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2392 return This::STATUS_OKAY;
2393 }
2394
2395 // R_ARM_THM_MOVT_PREL: S + A - P
2396 static inline typename This::Status
2397 thm_movt_prel(unsigned char *view,
2398 const Sized_relobj<32, big_endian>* object,
2399 const Symbol_value<32>* psymval,
ebabffbd 2400 Arm_address address)
c2a122b6
ILT
2401 {
2402 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2403 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2404 Valtype* wv = reinterpret_cast<Valtype*>(view);
2405 Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2406 | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2407 Reltype addend = This::extract_thumb_movw_movt_addend(val);
2daedcd6 2408 Reltype x = (psymval->value(object, addend) - address) >> 16;
c2a122b6
ILT
2409 val = This::insert_val_thumb_movw_movt(val, x);
2410 elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2411 elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2412 return This::STATUS_OKAY;
2413 }
c121c671
DK
2414};
2415
d204b6e9
DK
2416// Relocate ARM long branches. This handles relocation types
2417// R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2418// If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2419// undefined and we do not use PLT in this relocation. In such a case,
2420// the branch is converted into an NOP.
2421
2422template<bool big_endian>
2423typename Arm_relocate_functions<big_endian>::Status
2424Arm_relocate_functions<big_endian>::arm_branch_common(
2425 unsigned int r_type,
2426 const Relocate_info<32, big_endian>* relinfo,
2427 unsigned char *view,
2428 const Sized_symbol<32>* gsym,
2429 const Arm_relobj<big_endian>* object,
2430 unsigned int r_sym,
2431 const Symbol_value<32>* psymval,
2432 Arm_address address,
2433 Arm_address thumb_bit,
2434 bool is_weakly_undefined_without_plt)
2435{
2436 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2437 Valtype* wv = reinterpret_cast<Valtype*>(view);
2438 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2439
2440 bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
2441 && ((val & 0x0f000000UL) == 0x0a000000UL);
2442 bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
2443 bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
2444 && ((val & 0x0f000000UL) == 0x0b000000UL);
2445 bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
2446 bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
2447
2448 // Check that the instruction is valid.
2449 if (r_type == elfcpp::R_ARM_CALL)
2450 {
2451 if (!insn_is_uncond_bl && !insn_is_blx)
2452 return This::STATUS_BAD_RELOC;
2453 }
2454 else if (r_type == elfcpp::R_ARM_JUMP24)
2455 {
2456 if (!insn_is_b && !insn_is_cond_bl)
2457 return This::STATUS_BAD_RELOC;
2458 }
2459 else if (r_type == elfcpp::R_ARM_PLT32)
2460 {
2461 if (!insn_is_any_branch)
2462 return This::STATUS_BAD_RELOC;
2463 }
2464 else if (r_type == elfcpp::R_ARM_XPC25)
2465 {
2466 // FIXME: AAELF document IH0044C does not say much about it other
2467 // than it being obsolete.
2468 if (!insn_is_any_branch)
2469 return This::STATUS_BAD_RELOC;
2470 }
2471 else
2472 gold_unreachable();
2473
2474 // A branch to an undefined weak symbol is turned into a jump to
2475 // the next instruction unless a PLT entry will be created.
2476 // Do the same for local undefined symbols.
2477 // The jump to the next instruction is optimized as a NOP depending
2478 // on the architecture.
2479 const Target_arm<big_endian>* arm_target =
2480 Target_arm<big_endian>::default_target();
2481 if (is_weakly_undefined_without_plt)
2482 {
2483 Valtype cond = val & 0xf0000000U;
2484 if (arm_target->may_use_arm_nop())
2485 val = cond | 0x0320f000;
2486 else
2487 val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
2488 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2489 return This::STATUS_OKAY;
2490 }
2491
2492 Valtype addend = utils::sign_extend<26>(val << 2);
2493 Valtype branch_target = psymval->value(object, addend);
2494 int32_t branch_offset = branch_target - address;
2495
2496 // We need a stub if the branch offset is too large or if we need
2497 // to switch mode.
2498 bool may_use_blx = arm_target->may_use_blx();
2499 Reloc_stub* stub = NULL;
2500 if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
2501 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2502 || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
2503 {
2504 Stub_type stub_type =
2505 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2506 (thumb_bit != 0));
2507 if (stub_type != arm_stub_none)
2508 {
2ea97941 2509 Stub_table<big_endian>* stub_table =
d204b6e9 2510 object->stub_table(relinfo->data_shndx);
2ea97941 2511 gold_assert(stub_table != NULL);
d204b6e9
DK
2512
2513 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2ea97941 2514 stub = stub_table->find_reloc_stub(stub_key);
d204b6e9
DK
2515 gold_assert(stub != NULL);
2516 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2ea97941 2517 branch_target = stub_table->address() + stub->offset() + addend;
d204b6e9
DK
2518 branch_offset = branch_target - address;
2519 gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
2520 && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
2521 }
2522 }
2523
2524 // At this point, if we still need to switch mode, the instruction
2525 // must either be a BLX or a BL that can be converted to a BLX.
2526 if (thumb_bit != 0)
2527 {
2528 // Turn BL to BLX.
2529 gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
2530 val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
2531 }
2532
2533 val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
2534 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2535 return (utils::has_overflow<26>(branch_offset)
2536 ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
2537}
2538
51938283
DK
2539// Relocate THUMB long branches. This handles relocation types
2540// R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2541// If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
2542// undefined and we do not use PLT in this relocation. In such a case,
2543// the branch is converted into an NOP.
2544
2545template<bool big_endian>
2546typename Arm_relocate_functions<big_endian>::Status
2547Arm_relocate_functions<big_endian>::thumb_branch_common(
2548 unsigned int r_type,
2549 const Relocate_info<32, big_endian>* relinfo,
2550 unsigned char *view,
2551 const Sized_symbol<32>* gsym,
2552 const Arm_relobj<big_endian>* object,
2553 unsigned int r_sym,
2554 const Symbol_value<32>* psymval,
2555 Arm_address address,
2556 Arm_address thumb_bit,
2557 bool is_weakly_undefined_without_plt)
2558{
2559 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2560 Valtype* wv = reinterpret_cast<Valtype*>(view);
2561 uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2562 uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2563
2564 // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2565 // into account.
2566 bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
2567 bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
2568
2569 // Check that the instruction is valid.
2570 if (r_type == elfcpp::R_ARM_THM_CALL)
2571 {
2572 if (!is_bl_insn && !is_blx_insn)
2573 return This::STATUS_BAD_RELOC;
2574 }
2575 else if (r_type == elfcpp::R_ARM_THM_JUMP24)
2576 {
2577 // This cannot be a BLX.
2578 if (!is_bl_insn)
2579 return This::STATUS_BAD_RELOC;
2580 }
2581 else if (r_type == elfcpp::R_ARM_THM_XPC22)
2582 {
2583 // Check for Thumb to Thumb call.
2584 if (!is_blx_insn)
2585 return This::STATUS_BAD_RELOC;
2586 if (thumb_bit != 0)
2587 {
2588 gold_warning(_("%s: Thumb BLX instruction targets "
2589 "thumb function '%s'."),
2590 object->name().c_str(),
2591 (gsym ? gsym->name() : "(local)"));
2592 // Convert BLX to BL.
2593 lower_insn |= 0x1000U;
2594 }
2595 }
2596 else
2597 gold_unreachable();
2598
2599 // A branch to an undefined weak symbol is turned into a jump to
2600 // the next instruction unless a PLT entry will be created.
2601 // The jump to the next instruction is optimized as a NOP.W for
2602 // Thumb-2 enabled architectures.
2603 const Target_arm<big_endian>* arm_target =
2604 Target_arm<big_endian>::default_target();
2605 if (is_weakly_undefined_without_plt)
2606 {
2607 if (arm_target->may_use_thumb2_nop())
2608 {
2609 elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
2610 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
2611 }
2612 else
2613 {
2614 elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
2615 elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
2616 }
2617 return This::STATUS_OKAY;
2618 }
2619
2620 // Fetch the addend. We use the Thumb-2 encoding (backwards compatible
2621 // with Thumb-1) involving the J1 and J2 bits.
2622 uint32_t s = (upper_insn & (1 << 10)) >> 10;
2623 uint32_t upper = upper_insn & 0x3ff;
2624 uint32_t lower = lower_insn & 0x7ff;
2625 uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
2626 uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
2627 uint32_t i1 = j1 ^ s ? 0 : 1;
2628 uint32_t i2 = j2 ^ s ? 0 : 1;
2629
2630 int32_t addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
2631 // Sign extend.
2632 addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
2633
2634 Arm_address branch_target = psymval->value(object, addend);
2635 int32_t branch_offset = branch_target - address;
2636
2637 // We need a stub if the branch offset is too large or if we need
2638 // to switch mode.
2639 bool may_use_blx = arm_target->may_use_blx();
2640 bool thumb2 = arm_target->using_thumb2();
2641 if ((!thumb2
2642 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2643 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2644 || (thumb2
2645 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2646 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2647 || ((thumb_bit == 0)
2648 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2649 || r_type == elfcpp::R_ARM_THM_JUMP24)))
2650 {
2651 Stub_type stub_type =
2652 Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2653 (thumb_bit != 0));
2654 if (stub_type != arm_stub_none)
2655 {
2ea97941 2656 Stub_table<big_endian>* stub_table =
51938283 2657 object->stub_table(relinfo->data_shndx);
2ea97941 2658 gold_assert(stub_table != NULL);
51938283
DK
2659
2660 Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2ea97941 2661 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
51938283
DK
2662 gold_assert(stub != NULL);
2663 thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2ea97941 2664 branch_target = stub_table->address() + stub->offset() + addend;
51938283
DK
2665 branch_offset = branch_target - address;
2666 }
2667 }
2668
2669 // At this point, if we still need to switch mode, the instruction
2670 // must either be a BLX or a BL that can be converted to a BLX.
2671 if (thumb_bit == 0)
2672 {
2673 gold_assert(may_use_blx
2674 && (r_type == elfcpp::R_ARM_THM_CALL
2675 || r_type == elfcpp::R_ARM_THM_XPC22));
2676 // Make sure this is a BLX.
2677 lower_insn &= ~0x1000U;
2678 }
2679 else
2680 {
2681 // Make sure this is a BL.
2682 lower_insn |= 0x1000U;
2683 }
2684
2685 uint32_t reloc_sign = (branch_offset < 0) ? 1 : 0;
2686 uint32_t relocation = static_cast<uint32_t>(branch_offset);
2687
2688 if ((lower_insn & 0x5000U) == 0x4000U)
2689 // For a BLX instruction, make sure that the relocation is rounded up
2690 // to a word boundary. This follows the semantics of the instruction
2691 // which specifies that bit 1 of the target address will come from bit
2692 // 1 of the base address.
2693 relocation = (relocation + 2U) & ~3U;
2694
2695 // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
2696 // We use the Thumb-2 encoding, which is safe even if dealing with
2697 // a Thumb-1 instruction by virtue of our overflow check above. */
2698 upper_insn = (upper_insn & ~0x7ffU)
2699 | ((relocation >> 12) & 0x3ffU)
2700 | (reloc_sign << 10);
2701 lower_insn = (lower_insn & ~0x2fffU)
2702 | (((!((relocation >> 23) & 1U)) ^ reloc_sign) << 13)
2703 | (((!((relocation >> 22) & 1U)) ^ reloc_sign) << 11)
2704 | ((relocation >> 1) & 0x7ffU);
2705
2706 elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
2707 elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
2708
2709 return ((thumb2
2710 ? utils::has_overflow<25>(relocation)
2711 : utils::has_overflow<23>(relocation))
2712 ? This::STATUS_OVERFLOW
2713 : This::STATUS_OKAY);
2714}
2715
94cdfcff
DK
2716// Get the GOT section, creating it if necessary.
2717
2718template<bool big_endian>
2719Output_data_got<32, big_endian>*
2720Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2721{
2722 if (this->got_ == NULL)
2723 {
2724 gold_assert(symtab != NULL && layout != NULL);
2725
2726 this->got_ = new Output_data_got<32, big_endian>();
2727
2728 Output_section* os;
2729 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2730 (elfcpp::SHF_ALLOC
2731 | elfcpp::SHF_WRITE),
1a2dff53
ILT
2732 this->got_, false, true, true,
2733 false);
94cdfcff
DK
2734
2735 // The old GNU linker creates a .got.plt section. We just
2736 // create another set of data in the .got section. Note that we
2737 // always create a PLT if we create a GOT, although the PLT
2738 // might be empty.
2739 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2740 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2741 (elfcpp::SHF_ALLOC
2742 | elfcpp::SHF_WRITE),
1a2dff53
ILT
2743 this->got_plt_, false, false,
2744 false, true);
94cdfcff
DK
2745
2746 // The first three entries are reserved.
2747 this->got_plt_->set_current_data_size(3 * 4);
2748
2749 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2750 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
99fff23b 2751 Symbol_table::PREDEFINED,
94cdfcff
DK
2752 this->got_plt_,
2753 0, 0, elfcpp::STT_OBJECT,
2754 elfcpp::STB_LOCAL,
2755 elfcpp::STV_HIDDEN, 0,
2756 false, false);
2757 }
2758 return this->got_;
2759}
2760
2761// Get the dynamic reloc section, creating it if necessary.
2762
2763template<bool big_endian>
2764typename Target_arm<big_endian>::Reloc_section*
2765Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2766{
2767 if (this->rel_dyn_ == NULL)
2768 {
2769 gold_assert(layout != NULL);
2770 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2771 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1a2dff53
ILT
2772 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
2773 false, false, false);
94cdfcff
DK
2774 }
2775 return this->rel_dyn_;
2776}
2777
b569affa
DK
2778// Insn_template methods.
2779
2780// Return byte size of an instruction template.
2781
2782size_t
2783Insn_template::size() const
2784{
2785 switch (this->type())
2786 {
2787 case THUMB16_TYPE:
2788 return 2;
2789 case ARM_TYPE:
2790 case THUMB32_TYPE:
2791 case DATA_TYPE:
2792 return 4;
2793 default:
2794 gold_unreachable();
2795 }
2796}
2797
2798// Return alignment of an instruction template.
2799
2800unsigned
2801Insn_template::alignment() const
2802{
2803 switch (this->type())
2804 {
2805 case THUMB16_TYPE:
2806 case THUMB32_TYPE:
2807 return 2;
2808 case ARM_TYPE:
2809 case DATA_TYPE:
2810 return 4;
2811 default:
2812 gold_unreachable();
2813 }
2814}
2815
2816// Stub_template methods.
2817
2818Stub_template::Stub_template(
2ea97941
ILT
2819 Stub_type type, const Insn_template* insns,
2820 size_t insn_count)
2821 : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
b569affa
DK
2822 entry_in_thumb_mode_(false), relocs_()
2823{
2ea97941 2824 off_t offset = 0;
b569affa
DK
2825
2826 // Compute byte size and alignment of stub template.
2ea97941 2827 for (size_t i = 0; i < insn_count; i++)
b569affa 2828 {
2ea97941
ILT
2829 unsigned insn_alignment = insns[i].alignment();
2830 size_t insn_size = insns[i].size();
2831 gold_assert((offset & (insn_alignment - 1)) == 0);
b569affa 2832 this->alignment_ = std::max(this->alignment_, insn_alignment);
2ea97941 2833 switch (insns[i].type())
b569affa
DK
2834 {
2835 case Insn_template::THUMB16_TYPE:
2836 if (i == 0)
2837 this->entry_in_thumb_mode_ = true;
2838 break;
2839
2840 case Insn_template::THUMB32_TYPE:
2ea97941
ILT
2841 if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2842 this->relocs_.push_back(Reloc(i, offset));
b569affa
DK
2843 if (i == 0)
2844 this->entry_in_thumb_mode_ = true;
2845 break;
2846
2847 case Insn_template::ARM_TYPE:
2848 // Handle cases where the target is encoded within the
2849 // instruction.
2ea97941
ILT
2850 if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2851 this->relocs_.push_back(Reloc(i, offset));
b569affa
DK
2852 break;
2853
2854 case Insn_template::DATA_TYPE:
2855 // Entry point cannot be data.
2856 gold_assert(i != 0);
2ea97941 2857 this->relocs_.push_back(Reloc(i, offset));
b569affa
DK
2858 break;
2859
2860 default:
2861 gold_unreachable();
2862 }
2ea97941 2863 offset += insn_size;
b569affa 2864 }
2ea97941 2865 this->size_ = offset;
b569affa
DK
2866}
2867
bb0d3eb0
DK
2868// Stub methods.
2869
2870// Template to implement do_write for a specific target endianity.
2871
2872template<bool big_endian>
2873void inline
2874Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
2875{
2876 const Stub_template* stub_template = this->stub_template();
2877 const Insn_template* insns = stub_template->insns();
2878
2879 // FIXME: We do not handle BE8 encoding yet.
2880 unsigned char* pov = view;
2881 for (size_t i = 0; i < stub_template->insn_count(); i++)
2882 {
2883 switch (insns[i].type())
2884 {
2885 case Insn_template::THUMB16_TYPE:
2886 elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2887 break;
2888 case Insn_template::THUMB16_SPECIAL_TYPE:
2889 elfcpp::Swap<16, big_endian>::writeval(
2890 pov,
2891 this->thumb16_special(i));
2892 break;
2893 case Insn_template::THUMB32_TYPE:
2894 {
2895 uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2896 uint32_t lo = insns[i].data() & 0xffff;
2897 elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2898 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2899 }
2900 break;
2901 case Insn_template::ARM_TYPE:
2902 case Insn_template::DATA_TYPE:
2903 elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2904 break;
2905 default:
2906 gold_unreachable();
2907 }
2908 pov += insns[i].size();
2909 }
2910 gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2911}
2912
b569affa
DK
2913// Reloc_stub::Key methods.
2914
2915// Dump a Key as a string for debugging.
2916
2917std::string
2918Reloc_stub::Key::name() const
2919{
2920 if (this->r_sym_ == invalid_index)
2921 {
2922 // Global symbol key name
2923 // <stub-type>:<symbol name>:<addend>.
2924 const std::string sym_name = this->u_.symbol->name();
2925 // We need to print two hex number and two colons. So just add 100 bytes
2926 // to the symbol name size.
2927 size_t len = sym_name.size() + 100;
2928 char* buffer = new char[len];
2929 int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2930 sym_name.c_str(), this->addend_);
2931 gold_assert(c > 0 && c < static_cast<int>(len));
2932 delete[] buffer;
2933 return std::string(buffer);
2934 }
2935 else
2936 {
2937 // local symbol key name
2938 // <stub-type>:<object>:<r_sym>:<addend>.
2939 const size_t len = 200;
2940 char buffer[len];
2941 int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2942 this->u_.relobj, this->r_sym_, this->addend_);
2943 gold_assert(c > 0 && c < static_cast<int>(len));
2944 return std::string(buffer);
2945 }
2946}
2947
2948// Reloc_stub methods.
2949
2950// Determine the type of stub needed, if any, for a relocation of R_TYPE at
2951// LOCATION to DESTINATION.
2952// This code is based on the arm_type_of_stub function in
2953// bfd/elf32-arm.c. We have changed the interface a liitle to keep the Stub
2954// class simple.
2955
2956Stub_type
2957Reloc_stub::stub_type_for_reloc(
2958 unsigned int r_type,
2959 Arm_address location,
2960 Arm_address destination,
2961 bool target_is_thumb)
2962{
2963 Stub_type stub_type = arm_stub_none;
2964
2965 // This is a bit ugly but we want to avoid using a templated class for
2966 // big and little endianities.
2967 bool may_use_blx;
2968 bool should_force_pic_veneer;
2969 bool thumb2;
2970 bool thumb_only;
2971 if (parameters->target().is_big_endian())
2972 {
43d12afe 2973 const Target_arm<true>* big_endian_target =
b569affa 2974 Target_arm<true>::default_target();
43d12afe
DK
2975 may_use_blx = big_endian_target->may_use_blx();
2976 should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
2977 thumb2 = big_endian_target->using_thumb2();
2978 thumb_only = big_endian_target->using_thumb_only();
b569affa
DK
2979 }
2980 else
2981 {
43d12afe 2982 const Target_arm<false>* little_endian_target =
b569affa 2983 Target_arm<false>::default_target();
43d12afe
DK
2984 may_use_blx = little_endian_target->may_use_blx();
2985 should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
2986 thumb2 = little_endian_target->using_thumb2();
2987 thumb_only = little_endian_target->using_thumb_only();
b569affa
DK
2988 }
2989
2990 int64_t branch_offset = (int64_t)destination - location;
2991
2992 if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2993 {
2994 // Handle cases where:
2995 // - this call goes too far (different Thumb/Thumb2 max
2996 // distance)
2997 // - it's a Thumb->Arm call and blx is not available, or it's a
2998 // Thumb->Arm branch (not bl). A stub is needed in this case.
2999 if ((!thumb2
3000 && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3001 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3002 || (thumb2
3003 && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3004 || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3005 || ((!target_is_thumb)
3006 && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3007 || (r_type == elfcpp::R_ARM_THM_JUMP24))))
3008 {
3009 if (target_is_thumb)
3010 {
3011 // Thumb to thumb.
3012 if (!thumb_only)
3013 {
51938283
DK
3014 stub_type = (parameters->options().shared()
3015 || should_force_pic_veneer)
b569affa
DK
3016 // PIC stubs.
3017 ? ((may_use_blx
3018 && (r_type == elfcpp::R_ARM_THM_CALL))
3019 // V5T and above. Stub starts with ARM code, so
3020 // we must be able to switch mode before
3021 // reaching it, which is only possible for 'bl'
3022 // (ie R_ARM_THM_CALL relocation).
3023 ? arm_stub_long_branch_any_thumb_pic
3024 // On V4T, use Thumb code only.
3025 : arm_stub_long_branch_v4t_thumb_thumb_pic)
3026
3027 // non-PIC stubs.
3028 : ((may_use_blx
3029 && (r_type == elfcpp::R_ARM_THM_CALL))
3030 ? arm_stub_long_branch_any_any // V5T and above.
3031 : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
3032 }
3033 else
3034 {
51938283
DK
3035 stub_type = (parameters->options().shared()
3036 || should_force_pic_veneer)
b569affa
DK
3037 ? arm_stub_long_branch_thumb_only_pic // PIC stub.
3038 : arm_stub_long_branch_thumb_only; // non-PIC stub.
3039 }
3040 }
3041 else
3042 {
3043 // Thumb to arm.
3044
3045 // FIXME: We should check that the input section is from an
3046 // object that has interwork enabled.
3047
3048 stub_type = (parameters->options().shared()
3049 || should_force_pic_veneer)
3050 // PIC stubs.
3051 ? ((may_use_blx
3052 && (r_type == elfcpp::R_ARM_THM_CALL))
3053 ? arm_stub_long_branch_any_arm_pic // V5T and above.
3054 : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
3055
3056 // non-PIC stubs.
3057 : ((may_use_blx
3058 && (r_type == elfcpp::R_ARM_THM_CALL))
3059 ? arm_stub_long_branch_any_any // V5T and above.
3060 : arm_stub_long_branch_v4t_thumb_arm); // V4T.
3061
3062 // Handle v4t short branches.
3063 if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3064 && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3065 && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3066 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3067 }
3068 }
3069 }
3070 else if (r_type == elfcpp::R_ARM_CALL
3071 || r_type == elfcpp::R_ARM_JUMP24
3072 || r_type == elfcpp::R_ARM_PLT32)
3073 {
3074 if (target_is_thumb)
3075 {
3076 // Arm to thumb.
3077
3078 // FIXME: We should check that the input section is from an
3079 // object that has interwork enabled.
3080
3081 // We have an extra 2-bytes reach because of
3082 // the mode change (bit 24 (H) of BLX encoding).
3083 if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3084 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3085 || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
3086 || (r_type == elfcpp::R_ARM_JUMP24)
3087 || (r_type == elfcpp::R_ARM_PLT32))
3088 {
3089 stub_type = (parameters->options().shared()
3090 || should_force_pic_veneer)
3091 // PIC stubs.
3092 ? (may_use_blx
3093 ? arm_stub_long_branch_any_thumb_pic// V5T and above.
3094 : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
3095
3096 // non-PIC stubs.
3097 : (may_use_blx
3098 ? arm_stub_long_branch_any_any // V5T and above.
3099 : arm_stub_long_branch_v4t_arm_thumb); // V4T.
3100 }
3101 }
3102 else
3103 {
3104 // Arm to arm.
3105 if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3106 || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3107 {
3108 stub_type = (parameters->options().shared()
3109 || should_force_pic_veneer)
3110 ? arm_stub_long_branch_any_arm_pic // PIC stubs.
3111 : arm_stub_long_branch_any_any; /// non-PIC.
3112 }
3113 }
3114 }
3115
3116 return stub_type;
3117}
3118
bb0d3eb0 3119// Cortex_a8_stub methods.
b569affa 3120
bb0d3eb0
DK
3121// Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3122// I is the position of the instruction template in the stub template.
b569affa 3123
bb0d3eb0
DK
3124uint16_t
3125Cortex_a8_stub::do_thumb16_special(size_t i)
b569affa 3126{
bb0d3eb0
DK
3127 // The only use of this is to copy condition code from a conditional
3128 // branch being worked around to the corresponding conditional branch in
3129 // to the stub.
3130 gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3131 && i == 0);
3132 uint16_t data = this->stub_template()->insns()[i].data();
3133 gold_assert((data & 0xff00U) == 0xd000U);
3134 data |= ((this->original_insn_ >> 22) & 0xf) << 8;
3135 return data;
b569affa
DK
3136}
3137
3138// Stub_factory methods.
3139
3140Stub_factory::Stub_factory()
3141{
3142 // The instruction template sequences are declared as static
3143 // objects and initialized first time the constructor runs.
3144
3145 // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3146 // to reach the stub if necessary.
3147 static const Insn_template elf32_arm_stub_long_branch_any_any[] =
3148 {
3149 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3150 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3151 // dcd R_ARM_ABS32(X)
3152 };
3153
3154 // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3155 // available.
3156 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
3157 {
3158 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3159 Insn_template::arm_insn(0xe12fff1c), // bx ip
3160 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3161 // dcd R_ARM_ABS32(X)
3162 };
3163
3164 // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3165 static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
3166 {
3167 Insn_template::thumb16_insn(0xb401), // push {r0}
3168 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3169 Insn_template::thumb16_insn(0x4684), // mov ip, r0
3170 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3171 Insn_template::thumb16_insn(0x4760), // bx ip
3172 Insn_template::thumb16_insn(0xbf00), // nop
3173 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3174 // dcd R_ARM_ABS32(X)
3175 };
3176
3177 // V4T Thumb -> Thumb long branch stub. Using the stack is not
3178 // allowed.
3179 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
3180 {
3181 Insn_template::thumb16_insn(0x4778), // bx pc
3182 Insn_template::thumb16_insn(0x46c0), // nop
3183 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3184 Insn_template::arm_insn(0xe12fff1c), // bx ip
3185 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3186 // dcd R_ARM_ABS32(X)
3187 };
3188
3189 // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3190 // available.
3191 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
3192 {
3193 Insn_template::thumb16_insn(0x4778), // bx pc
3194 Insn_template::thumb16_insn(0x46c0), // nop
3195 Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
3196 Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3197 // dcd R_ARM_ABS32(X)
3198 };
3199
3200 // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3201 // one, when the destination is close enough.
3202 static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
3203 {
3204 Insn_template::thumb16_insn(0x4778), // bx pc
3205 Insn_template::thumb16_insn(0x46c0), // nop
3206 Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
3207 };
3208
3209 // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
3210 // blx to reach the stub if necessary.
3211 static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
3212 {
3213 Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
3214 Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
3215 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3216 // dcd R_ARM_REL32(X-4)
3217 };
3218
3219 // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
3220 // blx to reach the stub if necessary. We can not add into pc;
3221 // it is not guaranteed to mode switch (different in ARMv6 and
3222 // ARMv7).
3223 static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
3224 {
3225 Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
3226 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3227 Insn_template::arm_insn(0xe12fff1c), // bx ip
3228 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3229 // dcd R_ARM_REL32(X)
3230 };
3231
3232 // V4T ARM -> ARM long branch stub, PIC.
3233 static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
3234 {
3235 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3236 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3237 Insn_template::arm_insn(0xe12fff1c), // bx ip
3238 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3239 // dcd R_ARM_REL32(X)
3240 };
3241
3242 // V4T Thumb -> ARM long branch stub, PIC.
3243 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
3244 {
3245 Insn_template::thumb16_insn(0x4778), // bx pc
3246 Insn_template::thumb16_insn(0x46c0), // nop
3247 Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
3248 Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
3249 Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3250 // dcd R_ARM_REL32(X)
3251 };
3252
3253 // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3254 // architectures.
3255 static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
3256 {
3257 Insn_template::thumb16_insn(0xb401), // push {r0}
3258 Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
3259 Insn_template::thumb16_insn(0x46fc), // mov ip, pc
3260 Insn_template::thumb16_insn(0x4484), // add ip, r0
3261 Insn_template::thumb16_insn(0xbc01), // pop {r0}
3262 Insn_template::thumb16_insn(0x4760), // bx ip
3263 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
3264 // dcd R_ARM_REL32(X)
3265 };
3266
3267 // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3268 // allowed.
3269 static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
3270 {
3271 Insn_template::thumb16_insn(0x4778), // bx pc
3272 Insn_template::thumb16_insn(0x46c0), // nop
3273 Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
3274 Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
3275 Insn_template::arm_insn(0xe12fff1c), // bx ip
3276 Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3277 // dcd R_ARM_REL32(X)
3278 };
3279
3280 // Cortex-A8 erratum-workaround stubs.
3281
3282 // Stub used for conditional branches (which may be beyond +/-1MB away,
3283 // so we can't use a conditional branch to reach this stub).
3284
3285 // original code:
3286 //
3287 // b<cond> X
3288 // after:
3289 //
3290 static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
3291 {
3292 Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
3293 Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
3294 Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
3295 // b.w X
3296 };
3297
3298 // Stub used for b.w and bl.w instructions.
3299
3300 static const Insn_template elf32_arm_stub_a8_veneer_b[] =
3301 {
3302 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3303 };
3304
3305 static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
3306 {
3307 Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
3308 };
3309
3310 // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
3311 // instruction (which switches to ARM mode) to point to this stub. Jump to
3312 // the real destination using an ARM-mode branch.
bb0d3eb0 3313 static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
b569affa
DK
3314 {
3315 Insn_template::arm_rel_insn(0xea000000, -8) // b dest
3316 };
3317
3318 // Fill in the stub template look-up table. Stub templates are constructed
3319 // per instance of Stub_factory for fast look-up without locking
3320 // in a thread-enabled environment.
3321
3322 this->stub_templates_[arm_stub_none] =
3323 new Stub_template(arm_stub_none, NULL, 0);
3324
3325#define DEF_STUB(x) \
3326 do \
3327 { \
3328 size_t array_size \
3329 = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3330 Stub_type type = arm_stub_##x; \
3331 this->stub_templates_[type] = \
3332 new Stub_template(type, elf32_arm_stub_##x, array_size); \
3333 } \
3334 while (0);
3335
3336 DEF_STUBS
3337#undef DEF_STUB
3338}
3339
56ee5e00
DK
3340// Stub_table methods.
3341
3342// Add a STUB with using KEY. Caller is reponsible for avoid adding
3343// if already a STUB with the same key has been added.
3344
3345template<bool big_endian>
3346void
3347Stub_table<big_endian>::add_reloc_stub(
3348 Reloc_stub* stub,
3349 const Reloc_stub::Key& key)
3350{
2ea97941
ILT
3351 const Stub_template* stub_template = stub->stub_template();
3352 gold_assert(stub_template->type() == key.stub_type());
56ee5e00 3353 this->reloc_stubs_[key] = stub;
2ea97941
ILT
3354 if (this->addralign_ < stub_template->alignment())
3355 this->addralign_ = stub_template->alignment();
56ee5e00
DK
3356 this->has_been_changed_ = true;
3357}
3358
3359template<bool big_endian>
3360void
3361Stub_table<big_endian>::relocate_stubs(
3362 const Relocate_info<32, big_endian>* relinfo,
3363 Target_arm<big_endian>* arm_target,
2ea97941 3364 Output_section* output_section,
56ee5e00 3365 unsigned char* view,
2ea97941 3366 Arm_address address,
56ee5e00
DK
3367 section_size_type view_size)
3368{
3369 // If we are passed a view bigger than the stub table's. we need to
3370 // adjust the view.
2ea97941 3371 gold_assert(address == this->address()
56ee5e00
DK
3372 && (view_size
3373 == static_cast<section_size_type>(this->data_size())));
3374
3375 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3376 p != this->reloc_stubs_.end();
3377 ++p)
3378 {
3379 Reloc_stub* stub = p->second;
2ea97941
ILT
3380 const Stub_template* stub_template = stub->stub_template();
3381 if (stub_template->reloc_count() != 0)
56ee5e00
DK
3382 {
3383 // Adjust view to cover the stub only.
2ea97941
ILT
3384 section_size_type offset = stub->offset();
3385 section_size_type stub_size = stub_template->size();
3386 gold_assert(offset + stub_size <= view_size);
56ee5e00 3387
2ea97941
ILT
3388 arm_target->relocate_stub(stub, relinfo, output_section,
3389 view + offset, address + offset,
56ee5e00
DK
3390 stub_size);
3391 }
3392 }
3393}
3394
3395// Reset address and file offset.
3396
3397template<bool big_endian>
3398void
3399Stub_table<big_endian>::do_reset_address_and_file_offset()
3400{
3401 off_t off = 0;
3402 uint64_t max_addralign = 1;
3403 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3404 p != this->reloc_stubs_.end();
3405 ++p)
3406 {
3407 Reloc_stub* stub = p->second;
2ea97941
ILT
3408 const Stub_template* stub_template = stub->stub_template();
3409 uint64_t stub_addralign = stub_template->alignment();
56ee5e00
DK
3410 max_addralign = std::max(max_addralign, stub_addralign);
3411 off = align_address(off, stub_addralign);
3412 stub->set_offset(off);
3413 stub->reset_destination_address();
2ea97941 3414 off += stub_template->size();
56ee5e00
DK
3415 }
3416
3417 this->addralign_ = max_addralign;
3418 this->set_current_data_size_for_child(off);
3419}
3420
3421// Write out the stubs to file.
3422
3423template<bool big_endian>
3424void
3425Stub_table<big_endian>::do_write(Output_file* of)
3426{
2ea97941 3427 off_t offset = this->offset();
56ee5e00
DK
3428 const section_size_type oview_size =
3429 convert_to_section_size_type(this->data_size());
2ea97941 3430 unsigned char* const oview = of->get_output_view(offset, oview_size);
56ee5e00
DK
3431
3432 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3433 p != this->reloc_stubs_.end();
3434 ++p)
3435 {
3436 Reloc_stub* stub = p->second;
2ea97941
ILT
3437 Arm_address address = this->address() + stub->offset();
3438 gold_assert(address
3439 == align_address(address,
56ee5e00
DK
3440 stub->stub_template()->alignment()));
3441 stub->write(oview + stub->offset(), stub->stub_template()->size(),
3442 big_endian);
3443 }
3444 of->write_output_view(this->offset(), oview_size, oview);
3445}
3446
10ad9fe5
DK
3447// Arm_input_section methods.
3448
3449// Initialize an Arm_input_section.
3450
3451template<bool big_endian>
3452void
3453Arm_input_section<big_endian>::init()
3454{
2ea97941
ILT
3455 Relobj* relobj = this->relobj();
3456 unsigned int shndx = this->shndx();
10ad9fe5
DK
3457
3458 // Cache these to speed up size and alignment queries. It is too slow
3459 // to call section_addraglin and section_size every time.
2ea97941
ILT
3460 this->original_addralign_ = relobj->section_addralign(shndx);
3461 this->original_size_ = relobj->section_size(shndx);
10ad9fe5
DK
3462
3463 // We want to make this look like the original input section after
3464 // output sections are finalized.
2ea97941
ILT
3465 Output_section* os = relobj->output_section(shndx);
3466 off_t offset = relobj->output_section_offset(shndx);
3467 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
3468 this->set_address(os->address() + offset);
3469 this->set_file_offset(os->offset() + offset);
10ad9fe5
DK
3470
3471 this->set_current_data_size(this->original_size_);
3472 this->finalize_data_size();
3473}
3474
3475template<bool big_endian>
3476void
3477Arm_input_section<big_endian>::do_write(Output_file* of)
3478{
3479 // We have to write out the original section content.
3480 section_size_type section_size;
3481 const unsigned char* section_contents =
3482 this->relobj()->section_contents(this->shndx(), &section_size, false);
3483 of->write(this->offset(), section_contents, section_size);
3484
3485 // If this owns a stub table and it is not empty, write it.
3486 if (this->is_stub_table_owner() && !this->stub_table_->empty())
3487 this->stub_table_->write(of);
3488}
3489
3490// Finalize data size.
3491
3492template<bool big_endian>
3493void
3494Arm_input_section<big_endian>::set_final_data_size()
3495{
3496 // If this owns a stub table, finalize its data size as well.
3497 if (this->is_stub_table_owner())
3498 {
2ea97941 3499 uint64_t address = this->address();
10ad9fe5
DK
3500
3501 // The stub table comes after the original section contents.
2ea97941
ILT
3502 address += this->original_size_;
3503 address = align_address(address, this->stub_table_->addralign());
3504 off_t offset = this->offset() + (address - this->address());
3505 this->stub_table_->set_address_and_file_offset(address, offset);
3506 address += this->stub_table_->data_size();
3507 gold_assert(address == this->address() + this->current_data_size());
10ad9fe5
DK
3508 }
3509
3510 this->set_data_size(this->current_data_size());
3511}
3512
3513// Reset address and file offset.
3514
3515template<bool big_endian>
3516void
3517Arm_input_section<big_endian>::do_reset_address_and_file_offset()
3518{
3519 // Size of the original input section contents.
3520 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
3521
3522 // If this is a stub table owner, account for the stub table size.
3523 if (this->is_stub_table_owner())
3524 {
2ea97941 3525 Stub_table<big_endian>* stub_table = this->stub_table_;
10ad9fe5
DK
3526
3527 // Reset the stub table's address and file offset. The
3528 // current data size for child will be updated after that.
3529 stub_table_->reset_address_and_file_offset();
3530 off = align_address(off, stub_table_->addralign());
2ea97941 3531 off += stub_table->current_data_size();
10ad9fe5
DK
3532 }
3533
3534 this->set_current_data_size(off);
3535}
3536
07f508a2
DK
3537// Arm_output_section methods.
3538
3539// Create a stub group for input sections from BEGIN to END. OWNER
3540// points to the input section to be the owner a new stub table.
3541
3542template<bool big_endian>
3543void
3544Arm_output_section<big_endian>::create_stub_group(
3545 Input_section_list::const_iterator begin,
3546 Input_section_list::const_iterator end,
3547 Input_section_list::const_iterator owner,
3548 Target_arm<big_endian>* target,
3549 std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
3550{
3551 // Currently we convert ordinary input sections into relaxed sections only
3552 // at this point but we may want to support creating relaxed input section
3553 // very early. So we check here to see if owner is already a relaxed
3554 // section.
3555
3556 Arm_input_section<big_endian>* arm_input_section;
3557 if (owner->is_relaxed_input_section())
3558 {
3559 arm_input_section =
3560 Arm_input_section<big_endian>::as_arm_input_section(
3561 owner->relaxed_input_section());
3562 }
3563 else
3564 {
3565 gold_assert(owner->is_input_section());
3566 // Create a new relaxed input section.
3567 arm_input_section =
3568 target->new_arm_input_section(owner->relobj(), owner->shndx());
3569 new_relaxed_sections->push_back(arm_input_section);
3570 }
3571
3572 // Create a stub table.
2ea97941 3573 Stub_table<big_endian>* stub_table =
07f508a2
DK
3574 target->new_stub_table(arm_input_section);
3575
2ea97941 3576 arm_input_section->set_stub_table(stub_table);
07f508a2
DK
3577
3578 Input_section_list::const_iterator p = begin;
3579 Input_section_list::const_iterator prev_p;
3580
3581 // Look for input sections or relaxed input sections in [begin ... end].
3582 do
3583 {
3584 if (p->is_input_section() || p->is_relaxed_input_section())
3585 {
3586 // The stub table information for input sections live
3587 // in their objects.
3588 Arm_relobj<big_endian>* arm_relobj =
3589 Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
2ea97941 3590 arm_relobj->set_stub_table(p->shndx(), stub_table);
07f508a2
DK
3591 }
3592 prev_p = p++;
3593 }
3594 while (prev_p != end);
3595}
3596
3597// Group input sections for stub generation. GROUP_SIZE is roughly the limit
3598// of stub groups. We grow a stub group by adding input section until the
3599// size is just below GROUP_SIZE. The last input section will be converted
3600// into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3601// input section after the stub table, effectively double the group size.
3602//
3603// This is similar to the group_sections() function in elf32-arm.c but is
3604// implemented differently.
3605
3606template<bool big_endian>
3607void
3608Arm_output_section<big_endian>::group_sections(
3609 section_size_type group_size,
3610 bool stubs_always_after_branch,
3611 Target_arm<big_endian>* target)
3612{
3613 // We only care about sections containing code.
3614 if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
3615 return;
3616
3617 // States for grouping.
3618 typedef enum
3619 {
3620 // No group is being built.
3621 NO_GROUP,
3622 // A group is being built but the stub table is not found yet.
3623 // We keep group a stub group until the size is just under GROUP_SIZE.
3624 // The last input section in the group will be used as the stub table.
3625 FINDING_STUB_SECTION,
3626 // A group is being built and we have already found a stub table.
3627 // We enter this state to grow a stub group by adding input section
3628 // after the stub table. This effectively doubles the group size.
3629 HAS_STUB_SECTION
3630 } State;
3631
3632 // Any newly created relaxed sections are stored here.
3633 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
3634
3635 State state = NO_GROUP;
3636 section_size_type off = 0;
3637 section_size_type group_begin_offset = 0;
3638 section_size_type group_end_offset = 0;
3639 section_size_type stub_table_end_offset = 0;
3640 Input_section_list::const_iterator group_begin =
3641 this->input_sections().end();
2ea97941 3642 Input_section_list::const_iterator stub_table =
07f508a2
DK
3643 this->input_sections().end();
3644 Input_section_list::const_iterator group_end = this->input_sections().end();
3645 for (Input_section_list::const_iterator p = this->input_sections().begin();
3646 p != this->input_sections().end();
3647 ++p)
3648 {
3649 section_size_type section_begin_offset =
3650 align_address(off, p->addralign());
3651 section_size_type section_end_offset =
3652 section_begin_offset + p->data_size();
3653
3654 // Check to see if we should group the previously seens sections.
e9bbb538 3655 switch (state)
07f508a2
DK
3656 {
3657 case NO_GROUP:
3658 break;
3659
3660 case FINDING_STUB_SECTION:
3661 // Adding this section makes the group larger than GROUP_SIZE.
3662 if (section_end_offset - group_begin_offset >= group_size)
3663 {
3664 if (stubs_always_after_branch)
3665 {
3666 gold_assert(group_end != this->input_sections().end());
3667 this->create_stub_group(group_begin, group_end, group_end,
3668 target, &new_relaxed_sections);
3669 state = NO_GROUP;
3670 }
3671 else
3672 {
3673 // But wait, there's more! Input sections up to
3674 // stub_group_size bytes after the stub table can be
3675 // handled by it too.
3676 state = HAS_STUB_SECTION;
2ea97941 3677 stub_table = group_end;
07f508a2
DK
3678 stub_table_end_offset = group_end_offset;
3679 }
3680 }
3681 break;
3682
3683 case HAS_STUB_SECTION:
3684 // Adding this section makes the post stub-section group larger
3685 // than GROUP_SIZE.
3686 if (section_end_offset - stub_table_end_offset >= group_size)
3687 {
3688 gold_assert(group_end != this->input_sections().end());
2ea97941 3689 this->create_stub_group(group_begin, group_end, stub_table,
07f508a2
DK
3690 target, &new_relaxed_sections);
3691 state = NO_GROUP;
3692 }
3693 break;
3694
3695 default:
3696 gold_unreachable();
3697 }
3698
3699 // If we see an input section and currently there is no group, start
3700 // a new one. Skip any empty sections.
3701 if ((p->is_input_section() || p->is_relaxed_input_section())
3702 && (p->relobj()->section_size(p->shndx()) != 0))
3703 {
3704 if (state == NO_GROUP)
3705 {
3706 state = FINDING_STUB_SECTION;
3707 group_begin = p;
3708 group_begin_offset = section_begin_offset;
3709 }
3710
3711 // Keep track of the last input section seen.
3712 group_end = p;
3713 group_end_offset = section_end_offset;
3714 }
3715
3716 off = section_end_offset;
3717 }
3718
3719 // Create a stub group for any ungrouped sections.
3720 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
3721 {
3722 gold_assert(group_end != this->input_sections().end());
3723 this->create_stub_group(group_begin, group_end,
3724 (state == FINDING_STUB_SECTION
3725 ? group_end
2ea97941 3726 : stub_table),
07f508a2
DK
3727 target, &new_relaxed_sections);
3728 }
3729
3730 // Convert input section into relaxed input section in a batch.
3731 if (!new_relaxed_sections.empty())
3732 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
3733
3734 // Update the section offsets
3735 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
3736 {
3737 Arm_relobj<big_endian>* arm_relobj =
3738 Arm_relobj<big_endian>::as_arm_relobj(
3739 new_relaxed_sections[i]->relobj());
2ea97941 3740 unsigned int shndx = new_relaxed_sections[i]->shndx();
07f508a2 3741 // Tell Arm_relobj that this input section is converted.
2ea97941 3742 arm_relobj->convert_input_section_to_relaxed_section(shndx);
07f508a2
DK
3743 }
3744}
3745
8ffa3667
DK
3746// Arm_relobj methods.
3747
3748// Scan relocations for stub generation.
3749
3750template<bool big_endian>
3751void
3752Arm_relobj<big_endian>::scan_sections_for_stubs(
3753 Target_arm<big_endian>* arm_target,
3754 const Symbol_table* symtab,
2ea97941 3755 const Layout* layout)
8ffa3667 3756{
2ea97941
ILT
3757 unsigned int shnum = this->shnum();
3758 const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
8ffa3667
DK
3759
3760 // Read the section headers.
3761 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
2ea97941 3762 shnum * shdr_size,
8ffa3667
DK
3763 true, true);
3764
3765 // To speed up processing, we set up hash tables for fast lookup of
3766 // input offsets to output addresses.
3767 this->initialize_input_to_output_maps();
3768
3769 const Relobj::Output_sections& out_sections(this->output_sections());
3770
3771 Relocate_info<32, big_endian> relinfo;
8ffa3667 3772 relinfo.symtab = symtab;
2ea97941 3773 relinfo.layout = layout;
8ffa3667
DK
3774 relinfo.object = this;
3775
2ea97941
ILT
3776 const unsigned char* p = pshdrs + shdr_size;
3777 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
8ffa3667
DK
3778 {
3779 typename elfcpp::Shdr<32, big_endian> shdr(p);
3780
3781 unsigned int sh_type = shdr.get_sh_type();
3782 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
3783 continue;
3784
3785 off_t sh_size = shdr.get_sh_size();
3786 if (sh_size == 0)
3787 continue;
3788
3789 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
3790 if (index >= this->shnum())
3791 {
3792 // Ignore reloc section with bad info. This error will be
3793 // reported in the final link.
3794 continue;
3795 }
3796
3797 Output_section* os = out_sections[index];
d6344fb5
DK
3798 if (os == NULL
3799 || symtab->is_section_folded(this, index))
8ffa3667
DK
3800 {
3801 // This relocation section is against a section which we
d6344fb5
DK
3802 // discarded or if the section is folded into another
3803 // section due to ICF.
8ffa3667
DK
3804 continue;
3805 }
3806 Arm_address output_offset = this->get_output_section_offset(index);
3807
3808 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
3809 {
3810 // Ignore reloc section with unexpected symbol table. The
3811 // error will be reported in the final link.
3812 continue;
3813 }
3814
3815 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
3816 sh_size, true, false);
3817
3818 unsigned int reloc_size;
3819 if (sh_type == elfcpp::SHT_REL)
3820 reloc_size = elfcpp::Elf_sizes<32>::rel_size;
3821 else
3822 reloc_size = elfcpp::Elf_sizes<32>::rela_size;
3823
3824 if (reloc_size != shdr.get_sh_entsize())
3825 {
3826 // Ignore reloc section with unexpected entsize. The error
3827 // will be reported in the final link.
3828 continue;
3829 }
3830
3831 size_t reloc_count = sh_size / reloc_size;
3832 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
3833 {
3834 // Ignore reloc section with uneven size. The error will be
3835 // reported in the final link.
3836 continue;
3837 }
3838
3839 gold_assert(output_offset != invalid_address
3840 || this->relocs_must_follow_section_writes());
3841
3842 // Get the section contents. This does work for the case in which
3843 // we modify the contents of an input section. We need to pass the
3844 // output view under such circumstances.
3845 section_size_type input_view_size = 0;
3846 const unsigned char* input_view =
3847 this->section_contents(index, &input_view_size, false);
3848
3849 relinfo.reloc_shndx = i;
3850 relinfo.data_shndx = index;
3851 arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
3852 reloc_count, os,
3853 output_offset == invalid_address,
3854 input_view,
3855 os->address(),
3856 input_view_size);
3857 }
3858
3859 // After we've done the relocations, we release the hash tables,
3860 // since we no longer need them.
3861 this->free_input_to_output_maps();
3862}
3863
3864// Count the local symbols. The ARM backend needs to know if a symbol
3865// is a THUMB function or not. For global symbols, it is easy because
3866// the Symbol object keeps the ELF symbol type. For local symbol it is
3867// harder because we cannot access this information. So we override the
3868// do_count_local_symbol in parent and scan local symbols to mark
3869// THUMB functions. This is not the most efficient way but I do not want to
3870// slow down other ports by calling a per symbol targer hook inside
3871// Sized_relobj<size, big_endian>::do_count_local_symbols.
3872
3873template<bool big_endian>
3874void
3875Arm_relobj<big_endian>::do_count_local_symbols(
3876 Stringpool_template<char>* pool,
3877 Stringpool_template<char>* dynpool)
3878{
3879 // We need to fix-up the values of any local symbols whose type are
3880 // STT_ARM_TFUNC.
3881
3882 // Ask parent to count the local symbols.
3883 Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
3884 const unsigned int loccount = this->local_symbol_count();
3885 if (loccount == 0)
3886 return;
3887
3888 // Intialize the thumb function bit-vector.
3889 std::vector<bool> empty_vector(loccount, false);
3890 this->local_symbol_is_thumb_function_.swap(empty_vector);
3891
3892 // Read the symbol table section header.
2ea97941 3893 const unsigned int symtab_shndx = this->symtab_shndx();
8ffa3667 3894 elfcpp::Shdr<32, big_endian>
2ea97941 3895 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
8ffa3667
DK
3896 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
3897
3898 // Read the local symbols.
2ea97941 3899 const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
8ffa3667 3900 gold_assert(loccount == symtabshdr.get_sh_info());
2ea97941 3901 off_t locsize = loccount * sym_size;
8ffa3667
DK
3902 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
3903 locsize, true, true);
3904
3905 // Loop over the local symbols and mark any local symbols pointing
3906 // to THUMB functions.
3907
3908 // Skip the first dummy symbol.
2ea97941 3909 psyms += sym_size;
8ffa3667
DK
3910 typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
3911 this->local_values();
2ea97941 3912 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
8ffa3667
DK
3913 {
3914 elfcpp::Sym<32, big_endian> sym(psyms);
3915 elfcpp::STT st_type = sym.get_st_type();
3916 Symbol_value<32>& lv((*plocal_values)[i]);
3917 Arm_address input_value = lv.input_value();
3918
3919 if (st_type == elfcpp::STT_ARM_TFUNC
3920 || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
3921 {
3922 // This is a THUMB function. Mark this and canonicalize the
3923 // symbol value by setting LSB.
3924 this->local_symbol_is_thumb_function_[i] = true;
3925 if ((input_value & 1) == 0)
3926 lv.set_input_value(input_value | 1);
3927 }
3928 }
3929}
3930
3931// Relocate sections.
3932template<bool big_endian>
3933void
3934Arm_relobj<big_endian>::do_relocate_sections(
8ffa3667 3935 const Symbol_table* symtab,
2ea97941 3936 const Layout* layout,
8ffa3667
DK
3937 const unsigned char* pshdrs,
3938 typename Sized_relobj<32, big_endian>::Views* pviews)
3939{
3940 // Call parent to relocate sections.
2ea97941 3941 Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
43d12afe 3942 pviews);
8ffa3667
DK
3943
3944 // We do not generate stubs if doing a relocatable link.
3945 if (parameters->options().relocatable())
3946 return;
3947
3948 // Relocate stub tables.
2ea97941 3949 unsigned int shnum = this->shnum();
8ffa3667
DK
3950
3951 Target_arm<big_endian>* arm_target =
3952 Target_arm<big_endian>::default_target();
3953
3954 Relocate_info<32, big_endian> relinfo;
8ffa3667 3955 relinfo.symtab = symtab;
2ea97941 3956 relinfo.layout = layout;
8ffa3667
DK
3957 relinfo.object = this;
3958
2ea97941 3959 for (unsigned int i = 1; i < shnum; ++i)
8ffa3667
DK
3960 {
3961 Arm_input_section<big_endian>* arm_input_section =
3962 arm_target->find_arm_input_section(this, i);
3963
3964 if (arm_input_section == NULL
3965 || !arm_input_section->is_stub_table_owner()
3966 || arm_input_section->stub_table()->empty())
3967 continue;
3968
3969 // We cannot discard a section if it owns a stub table.
3970 Output_section* os = this->output_section(i);
3971 gold_assert(os != NULL);
3972
3973 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
3974 relinfo.reloc_shdr = NULL;
3975 relinfo.data_shndx = i;
3976 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
3977
3978 gold_assert((*pviews)[i].view != NULL);
3979
3980 // We are passed the output section view. Adjust it to cover the
3981 // stub table only.
2ea97941
ILT
3982 Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
3983 gold_assert((stub_table->address() >= (*pviews)[i].address)
3984 && ((stub_table->address() + stub_table->data_size())
8ffa3667
DK
3985 <= (*pviews)[i].address + (*pviews)[i].view_size));
3986
2ea97941
ILT
3987 off_t offset = stub_table->address() - (*pviews)[i].address;
3988 unsigned char* view = (*pviews)[i].view + offset;
3989 Arm_address address = stub_table->address();
3990 section_size_type view_size = stub_table->data_size();
8ffa3667 3991
2ea97941
ILT
3992 stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
3993 view_size);
8ffa3667
DK
3994 }
3995}
3996
a0351a69
DK
3997// Helper functions for both Arm_relobj and Arm_dynobj to read ARM
3998// ABI information.
3999
4000template<bool big_endian>
4001Attributes_section_data*
4002read_arm_attributes_section(
4003 Object* object,
4004 Read_symbols_data *sd)
4005{
4006 // Read the attributes section if there is one.
4007 // We read from the end because gas seems to put it near the end of
4008 // the section headers.
4009 const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4010 const unsigned char *ps =
4011 sd->section_headers->data() + shdr_size * (object->shnum() - 1);
4012 for (unsigned int i = object->shnum(); i > 0; --i, ps -= shdr_size)
4013 {
4014 elfcpp::Shdr<32, big_endian> shdr(ps);
4015 if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
4016 {
4017 section_offset_type section_offset = shdr.get_sh_offset();
4018 section_size_type section_size =
4019 convert_to_section_size_type(shdr.get_sh_size());
4020 File_view* view = object->get_lasting_view(section_offset,
4021 section_size, true, false);
4022 return new Attributes_section_data(view->data(), section_size);
4023 }
4024 }
4025 return NULL;
4026}
4027
d5b40221
DK
4028// Read the symbol information.
4029
4030template<bool big_endian>
4031void
4032Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4033{
4034 // Call parent class to read symbol information.
4035 Sized_relobj<32, big_endian>::do_read_symbols(sd);
4036
4037 // Read processor-specific flags in ELF file header.
4038 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4039 elfcpp::Elf_sizes<32>::ehdr_size,
4040 true, false);
4041 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4042 this->processor_specific_flags_ = ehdr.get_e_flags();
a0351a69
DK
4043 this->attributes_section_data_ =
4044 read_arm_attributes_section<big_endian>(this, sd);
d5b40221
DK
4045}
4046
4047// Arm_dynobj methods.
4048
4049// Read the symbol information.
4050
4051template<bool big_endian>
4052void
4053Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4054{
4055 // Call parent class to read symbol information.
4056 Sized_dynobj<32, big_endian>::do_read_symbols(sd);
4057
4058 // Read processor-specific flags in ELF file header.
4059 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4060 elfcpp::Elf_sizes<32>::ehdr_size,
4061 true, false);
4062 elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4063 this->processor_specific_flags_ = ehdr.get_e_flags();
a0351a69
DK
4064 this->attributes_section_data_ =
4065 read_arm_attributes_section<big_endian>(this, sd);
d5b40221
DK
4066}
4067
e9bbb538
DK
4068// Stub_addend_reader methods.
4069
4070// Read the addend of a REL relocation of type R_TYPE at VIEW.
4071
4072template<bool big_endian>
4073elfcpp::Elf_types<32>::Elf_Swxword
4074Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
4075 unsigned int r_type,
4076 const unsigned char* view,
4077 const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
4078{
4079 switch (r_type)
4080 {
4081 case elfcpp::R_ARM_CALL:
4082 case elfcpp::R_ARM_JUMP24:
4083 case elfcpp::R_ARM_PLT32:
4084 {
4085 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4086 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4087 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4088 return utils::sign_extend<26>(val << 2);
4089 }
4090
4091 case elfcpp::R_ARM_THM_CALL:
4092 case elfcpp::R_ARM_THM_JUMP24:
4093 case elfcpp::R_ARM_THM_XPC22:
4094 {
4095 // Fetch the addend. We use the Thumb-2 encoding (backwards
4096 // compatible with Thumb-1) involving the J1 and J2 bits.
4097 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4098 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4099 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4100 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4101
4102 uint32_t s = (upper_insn & (1 << 10)) >> 10;
4103 uint32_t upper = upper_insn & 0x3ff;
4104 uint32_t lower = lower_insn & 0x7ff;
4105 uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
4106 uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
4107 uint32_t i1 = j1 ^ s ? 0 : 1;
4108 uint32_t i2 = j2 ^ s ? 0 : 1;
4109
4110 return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
4111 | (upper << 12) | (lower << 1));
4112 }
4113
4114 case elfcpp::R_ARM_THM_JUMP19:
4115 {
4116 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4117 const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4118 Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4119 Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4120
4121 // Reconstruct the top three bits and squish the two 11 bit pieces
4122 // together.
4123 uint32_t S = (upper_insn & 0x0400) >> 10;
4124 uint32_t J1 = (lower_insn & 0x2000) >> 13;
4125 uint32_t J2 = (lower_insn & 0x0800) >> 11;
4126 uint32_t upper =
4127 (S << 8) | (J2 << 7) | (J1 << 6) | (upper_insn & 0x003f);
4128 uint32_t lower = (lower_insn & 0x07ff);
4129 return utils::sign_extend<23>((upper << 12) | (lower << 1));
4130 }
4131
4132 default:
4133 gold_unreachable();
4134 }
4135}
4136
94cdfcff
DK
4137// A class to handle the PLT data.
4138
4139template<bool big_endian>
4140class Output_data_plt_arm : public Output_section_data
4141{
4142 public:
4143 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
4144 Reloc_section;
4145
4146 Output_data_plt_arm(Layout*, Output_data_space*);
4147
4148 // Add an entry to the PLT.
4149 void
4150 add_entry(Symbol* gsym);
4151
4152 // Return the .rel.plt section data.
4153 const Reloc_section*
4154 rel_plt() const
4155 { return this->rel_; }
4156
4157 protected:
4158 void
4159 do_adjust_output_section(Output_section* os);
4160
4161 // Write to a map file.
4162 void
4163 do_print_to_mapfile(Mapfile* mapfile) const
4164 { mapfile->print_output_data(this, _("** PLT")); }
4165
4166 private:
4167 // Template for the first PLT entry.
4168 static const uint32_t first_plt_entry[5];
4169
4170 // Template for subsequent PLT entries.
4171 static const uint32_t plt_entry[3];
4172
4173 // Set the final size.
4174 void
4175 set_final_data_size()
4176 {
4177 this->set_data_size(sizeof(first_plt_entry)
4178 + this->count_ * sizeof(plt_entry));
4179 }
4180
4181 // Write out the PLT data.
4182 void
4183 do_write(Output_file*);
4184
4185 // The reloc section.
4186 Reloc_section* rel_;
4187 // The .got.plt section.
4188 Output_data_space* got_plt_;
4189 // The number of PLT entries.
4190 unsigned int count_;
4191};
4192
4193// Create the PLT section. The ordinary .got section is an argument,
4194// since we need to refer to the start. We also create our own .got
4195// section just for PLT entries.
4196
4197template<bool big_endian>
2ea97941 4198Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
94cdfcff
DK
4199 Output_data_space* got_plt)
4200 : Output_section_data(4), got_plt_(got_plt), count_(0)
4201{
4202 this->rel_ = new Reloc_section(false);
2ea97941 4203 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1a2dff53
ILT
4204 elfcpp::SHF_ALLOC, this->rel_, true, false,
4205 false, false);
94cdfcff
DK
4206}
4207
4208template<bool big_endian>
4209void
4210Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
4211{
4212 os->set_entsize(0);
4213}
4214
4215// Add an entry to the PLT.
4216
4217template<bool big_endian>
4218void
4219Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
4220{
4221 gold_assert(!gsym->has_plt_offset());
4222
4223 // Note that when setting the PLT offset we skip the initial
4224 // reserved PLT entry.
4225 gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
4226 + sizeof(first_plt_entry));
4227
4228 ++this->count_;
4229
4230 section_offset_type got_offset = this->got_plt_->current_data_size();
4231
4232 // Every PLT entry needs a GOT entry which points back to the PLT
4233 // entry (this will be changed by the dynamic linker, normally
4234 // lazily when the function is called).
4235 this->got_plt_->set_current_data_size(got_offset + 4);
4236
4237 // Every PLT entry needs a reloc.
4238 gsym->set_needs_dynsym_entry();
4239 this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
4240 got_offset);
4241
4242 // Note that we don't need to save the symbol. The contents of the
4243 // PLT are independent of which symbols are used. The symbols only
4244 // appear in the relocations.
4245}
4246
4247// ARM PLTs.
4248// FIXME: This is not very flexible. Right now this has only been tested
4249// on armv5te. If we are to support additional architecture features like
4250// Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4251
4252// The first entry in the PLT.
4253template<bool big_endian>
4254const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
4255{
4256 0xe52de004, // str lr, [sp, #-4]!
4257 0xe59fe004, // ldr lr, [pc, #4]
4258 0xe08fe00e, // add lr, pc, lr
4259 0xe5bef008, // ldr pc, [lr, #8]!
4260 0x00000000, // &GOT[0] - .
4261};
4262
4263// Subsequent entries in the PLT.
4264
4265template<bool big_endian>
4266const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
4267{
4268 0xe28fc600, // add ip, pc, #0xNN00000
4269 0xe28cca00, // add ip, ip, #0xNN000
4270 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
4271};
4272
4273// Write out the PLT. This uses the hand-coded instructions above,
4274// and adjusts them as needed. This is all specified by the arm ELF
4275// Processor Supplement.
4276
4277template<bool big_endian>
4278void
4279Output_data_plt_arm<big_endian>::do_write(Output_file* of)
4280{
2ea97941 4281 const off_t offset = this->offset();
94cdfcff
DK
4282 const section_size_type oview_size =
4283 convert_to_section_size_type(this->data_size());
2ea97941 4284 unsigned char* const oview = of->get_output_view(offset, oview_size);
94cdfcff
DK
4285
4286 const off_t got_file_offset = this->got_plt_->offset();
4287 const section_size_type got_size =
4288 convert_to_section_size_type(this->got_plt_->data_size());
4289 unsigned char* const got_view = of->get_output_view(got_file_offset,
4290 got_size);
4291 unsigned char* pov = oview;
4292
ebabffbd
DK
4293 Arm_address plt_address = this->address();
4294 Arm_address got_address = this->got_plt_->address();
94cdfcff
DK
4295
4296 // Write first PLT entry. All but the last word are constants.
4297 const size_t num_first_plt_words = (sizeof(first_plt_entry)
4298 / sizeof(plt_entry[0]));
4299 for (size_t i = 0; i < num_first_plt_words - 1; i++)
4300 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
4301 // Last word in first PLT entry is &GOT[0] - .
4302 elfcpp::Swap<32, big_endian>::writeval(pov + 16,
4303 got_address - (plt_address + 16));
4304 pov += sizeof(first_plt_entry);
4305
4306 unsigned char* got_pov = got_view;
4307
4308 memset(got_pov, 0, 12);
4309 got_pov += 12;
4310
4311 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
4312 unsigned int plt_offset = sizeof(first_plt_entry);
4313 unsigned int plt_rel_offset = 0;
4314 unsigned int got_offset = 12;
4315 const unsigned int count = this->count_;
4316 for (unsigned int i = 0;
4317 i < count;
4318 ++i,
4319 pov += sizeof(plt_entry),
4320 got_pov += 4,
4321 plt_offset += sizeof(plt_entry),
4322 plt_rel_offset += rel_size,
4323 got_offset += 4)
4324 {
4325 // Set and adjust the PLT entry itself.
2ea97941
ILT
4326 int32_t offset = ((got_address + got_offset)
4327 - (plt_address + plt_offset + 8));
94cdfcff 4328
2ea97941
ILT
4329 gold_assert(offset >= 0 && offset < 0x0fffffff);
4330 uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
94cdfcff 4331 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
2ea97941 4332 uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
94cdfcff 4333 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
2ea97941 4334 uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
94cdfcff
DK
4335 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
4336
4337 // Set the entry in the GOT.
4338 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
4339 }
4340
4341 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4342 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4343
2ea97941 4344 of->write_output_view(offset, oview_size, oview);
94cdfcff
DK
4345 of->write_output_view(got_file_offset, got_size, got_view);
4346}
4347
4348// Create a PLT entry for a global symbol.
4349
4350template<bool big_endian>
4351void
2ea97941 4352Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
94cdfcff
DK
4353 Symbol* gsym)
4354{
4355 if (gsym->has_plt_offset())
4356 return;
4357
4358 if (this->plt_ == NULL)
4359 {
4360 // Create the GOT sections first.
2ea97941 4361 this->got_section(symtab, layout);
94cdfcff 4362
2ea97941
ILT
4363 this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
4364 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
4365 (elfcpp::SHF_ALLOC
4366 | elfcpp::SHF_EXECINSTR),
1a2dff53 4367 this->plt_, false, false, false, false);
94cdfcff
DK
4368 }
4369 this->plt_->add_entry(gsym);
4370}
4371
4a657b0d
DK
4372// Report an unsupported relocation against a local symbol.
4373
4374template<bool big_endian>
4375void
4376Target_arm<big_endian>::Scan::unsupported_reloc_local(
4377 Sized_relobj<32, big_endian>* object,
4378 unsigned int r_type)
4379{
4380 gold_error(_("%s: unsupported reloc %u against local symbol"),
4381 object->name().c_str(), r_type);
4382}
4383
bec53400
DK
4384// We are about to emit a dynamic relocation of type R_TYPE. If the
4385// dynamic linker does not support it, issue an error. The GNU linker
4386// only issues a non-PIC error for an allocated read-only section.
4387// Here we know the section is allocated, but we don't know that it is
4388// read-only. But we check for all the relocation types which the
4389// glibc dynamic linker supports, so it seems appropriate to issue an
4390// error even if the section is not read-only.
4391
4392template<bool big_endian>
4393void
4394Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
4395 unsigned int r_type)
4396{
4397 switch (r_type)
4398 {
4399 // These are the relocation types supported by glibc for ARM.
4400 case elfcpp::R_ARM_RELATIVE:
4401 case elfcpp::R_ARM_COPY:
4402 case elfcpp::R_ARM_GLOB_DAT:
4403 case elfcpp::R_ARM_JUMP_SLOT:
4404 case elfcpp::R_ARM_ABS32:
be8fcb75 4405 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
4406 case elfcpp::R_ARM_PC24:
4407 // FIXME: The following 3 types are not supported by Android's dynamic
4408 // linker.
4409 case elfcpp::R_ARM_TLS_DTPMOD32:
4410 case elfcpp::R_ARM_TLS_DTPOFF32:
4411 case elfcpp::R_ARM_TLS_TPOFF32:
4412 return;
4413
4414 default:
4415 // This prevents us from issuing more than one error per reloc
4416 // section. But we can still wind up issuing more than one
4417 // error per object file.
4418 if (this->issued_non_pic_error_)
4419 return;
4420 object->error(_("requires unsupported dynamic reloc; "
4421 "recompile with -fPIC"));
4422 this->issued_non_pic_error_ = true;
4423 return;
4424
4425 case elfcpp::R_ARM_NONE:
4426 gold_unreachable();
4427 }
4428}
4429
4a657b0d 4430// Scan a relocation for a local symbol.
bec53400
DK
4431// FIXME: This only handles a subset of relocation types used by Android
4432// on ARM v5te devices.
4a657b0d
DK
4433
4434template<bool big_endian>
4435inline void
ad0f2072 4436Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
2ea97941 4437 Layout* layout,
bec53400 4438 Target_arm* target,
4a657b0d 4439 Sized_relobj<32, big_endian>* object,
bec53400
DK
4440 unsigned int data_shndx,
4441 Output_section* output_section,
4442 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
4443 unsigned int r_type,
4444 const elfcpp::Sym<32, big_endian>&)
4445{
a6d1ef57 4446 r_type = get_real_reloc_type(r_type);
4a657b0d
DK
4447 switch (r_type)
4448 {
4449 case elfcpp::R_ARM_NONE:
4450 break;
4451
bec53400 4452 case elfcpp::R_ARM_ABS32:
be8fcb75 4453 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
4454 // If building a shared library (or a position-independent
4455 // executable), we need to create a dynamic relocation for
4456 // this location. The relocation applied at link time will
4457 // apply the link-time value, so we flag the location with
4458 // an R_ARM_RELATIVE relocation so the dynamic loader can
4459 // relocate it easily.
4460 if (parameters->options().output_is_position_independent())
4461 {
2ea97941 4462 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
4463 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4464 // If we are to add more other reloc types than R_ARM_ABS32,
4465 // we need to add check_non_pic(object, r_type) here.
4466 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
4467 output_section, data_shndx,
4468 reloc.get_r_offset());
4469 }
4470 break;
4471
4472 case elfcpp::R_ARM_REL32:
4473 case elfcpp::R_ARM_THM_CALL:
4474 case elfcpp::R_ARM_CALL:
4475 case elfcpp::R_ARM_PREL31:
4476 case elfcpp::R_ARM_JUMP24:
4477 case elfcpp::R_ARM_PLT32:
be8fcb75
ILT
4478 case elfcpp::R_ARM_THM_ABS5:
4479 case elfcpp::R_ARM_ABS8:
4480 case elfcpp::R_ARM_ABS12:
4481 case elfcpp::R_ARM_ABS16:
4482 case elfcpp::R_ARM_BASE_ABS:
fd3c5f0b
ILT
4483 case elfcpp::R_ARM_MOVW_ABS_NC:
4484 case elfcpp::R_ARM_MOVT_ABS:
4485 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4486 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
4487 case elfcpp::R_ARM_MOVW_PREL_NC:
4488 case elfcpp::R_ARM_MOVT_PREL:
4489 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4490 case elfcpp::R_ARM_THM_MOVT_PREL:
bec53400
DK
4491 break;
4492
4493 case elfcpp::R_ARM_GOTOFF32:
4494 // We need a GOT section:
2ea97941 4495 target->got_section(symtab, layout);
bec53400
DK
4496 break;
4497
4498 case elfcpp::R_ARM_BASE_PREL:
4499 // FIXME: What about this?
4500 break;
4501
4502 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 4503 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
4504 {
4505 // The symbol requires a GOT entry.
4506 Output_data_got<32, big_endian>* got =
2ea97941 4507 target->got_section(symtab, layout);
bec53400
DK
4508 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4509 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
4510 {
4511 // If we are generating a shared object, we need to add a
4512 // dynamic RELATIVE relocation for this symbol's GOT entry.
4513 if (parameters->options().output_is_position_independent())
4514 {
2ea97941
ILT
4515 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4516 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
bec53400 4517 rel_dyn->add_local_relative(
2ea97941
ILT
4518 object, r_sym, elfcpp::R_ARM_RELATIVE, got,
4519 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
bec53400
DK
4520 }
4521 }
4522 }
4523 break;
4524
4525 case elfcpp::R_ARM_TARGET1:
4526 // This should have been mapped to another type already.
4527 // Fall through.
4528 case elfcpp::R_ARM_COPY:
4529 case elfcpp::R_ARM_GLOB_DAT:
4530 case elfcpp::R_ARM_JUMP_SLOT:
4531 case elfcpp::R_ARM_RELATIVE:
4532 // These are relocations which should only be seen by the
4533 // dynamic linker, and should never be seen here.
4534 gold_error(_("%s: unexpected reloc %u in object file"),
4535 object->name().c_str(), r_type);
4536 break;
4537
4a657b0d
DK
4538 default:
4539 unsupported_reloc_local(object, r_type);
4540 break;
4541 }
4542}
4543
4544// Report an unsupported relocation against a global symbol.
4545
4546template<bool big_endian>
4547void
4548Target_arm<big_endian>::Scan::unsupported_reloc_global(
4549 Sized_relobj<32, big_endian>* object,
4550 unsigned int r_type,
4551 Symbol* gsym)
4552{
4553 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4554 object->name().c_str(), r_type, gsym->demangled_name().c_str());
4555}
4556
4557// Scan a relocation for a global symbol.
bec53400
DK
4558// FIXME: This only handles a subset of relocation types used by Android
4559// on ARM v5te devices.
4a657b0d
DK
4560
4561template<bool big_endian>
4562inline void
ad0f2072 4563Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
2ea97941 4564 Layout* layout,
bec53400 4565 Target_arm* target,
4a657b0d 4566 Sized_relobj<32, big_endian>* object,
bec53400
DK
4567 unsigned int data_shndx,
4568 Output_section* output_section,
4569 const elfcpp::Rel<32, big_endian>& reloc,
4a657b0d
DK
4570 unsigned int r_type,
4571 Symbol* gsym)
4572{
a6d1ef57 4573 r_type = get_real_reloc_type(r_type);
4a657b0d
DK
4574 switch (r_type)
4575 {
4576 case elfcpp::R_ARM_NONE:
4577 break;
4578
bec53400 4579 case elfcpp::R_ARM_ABS32:
be8fcb75 4580 case elfcpp::R_ARM_ABS32_NOI:
bec53400
DK
4581 {
4582 // Make a dynamic relocation if necessary.
4583 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
4584 {
4585 if (target->may_need_copy_reloc(gsym))
4586 {
2ea97941 4587 target->copy_reloc(symtab, layout, object,
bec53400
DK
4588 data_shndx, output_section, gsym, reloc);
4589 }
4590 else if (gsym->can_use_relative_reloc(false))
4591 {
4592 // If we are to add more other reloc types than R_ARM_ABS32,
4593 // we need to add check_non_pic(object, r_type) here.
2ea97941 4594 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
4595 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
4596 output_section, object,
4597 data_shndx, reloc.get_r_offset());
4598 }
4599 else
4600 {
4601 // If we are to add more other reloc types than R_ARM_ABS32,
4602 // we need to add check_non_pic(object, r_type) here.
2ea97941 4603 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
4604 rel_dyn->add_global(gsym, r_type, output_section, object,
4605 data_shndx, reloc.get_r_offset());
4606 }
4607 }
4608 }
4609 break;
4610
fd3c5f0b
ILT
4611 case elfcpp::R_ARM_MOVW_ABS_NC:
4612 case elfcpp::R_ARM_MOVT_ABS:
4613 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4614 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
4615 case elfcpp::R_ARM_MOVW_PREL_NC:
4616 case elfcpp::R_ARM_MOVT_PREL:
4617 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4618 case elfcpp::R_ARM_THM_MOVT_PREL:
fd3c5f0b
ILT
4619 break;
4620
be8fcb75
ILT
4621 case elfcpp::R_ARM_THM_ABS5:
4622 case elfcpp::R_ARM_ABS8:
4623 case elfcpp::R_ARM_ABS12:
4624 case elfcpp::R_ARM_ABS16:
4625 case elfcpp::R_ARM_BASE_ABS:
4626 {
4627 // No dynamic relocs of this kinds.
4628 // Report the error in case of PIC.
4629 int flags = Symbol::NON_PIC_REF;
4630 if (gsym->type() == elfcpp::STT_FUNC
4631 || gsym->type() == elfcpp::STT_ARM_TFUNC)
4632 flags |= Symbol::FUNCTION_CALL;
4633 if (gsym->needs_dynamic_reloc(flags))
4634 check_non_pic(object, r_type);
4635 }
4636 break;
4637
bec53400
DK
4638 case elfcpp::R_ARM_REL32:
4639 case elfcpp::R_ARM_PREL31:
4640 {
4641 // Make a dynamic relocation if necessary.
4642 int flags = Symbol::NON_PIC_REF;
4643 if (gsym->needs_dynamic_reloc(flags))
4644 {
4645 if (target->may_need_copy_reloc(gsym))
4646 {
2ea97941 4647 target->copy_reloc(symtab, layout, object,
bec53400
DK
4648 data_shndx, output_section, gsym, reloc);
4649 }
4650 else
4651 {
4652 check_non_pic(object, r_type);
2ea97941 4653 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
4654 rel_dyn->add_global(gsym, r_type, output_section, object,
4655 data_shndx, reloc.get_r_offset());
4656 }
4657 }
4658 }
4659 break;
4660
4661 case elfcpp::R_ARM_JUMP24:
f4e5969c 4662 case elfcpp::R_ARM_THM_JUMP24:
bec53400 4663 case elfcpp::R_ARM_CALL:
f4e5969c
DK
4664 case elfcpp::R_ARM_THM_CALL:
4665
4666 if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
2ea97941 4667 target->make_plt_entry(symtab, layout, gsym);
f4e5969c
DK
4668 else
4669 {
4670 // Check to see if this is a function that would need a PLT
4671 // but does not get one because the function symbol is untyped.
4672 // This happens in assembly code missing a proper .type directive.
4673 if ((!gsym->is_undefined() || parameters->options().shared())
4674 && !parameters->doing_static_link()
4675 && gsym->type() == elfcpp::STT_NOTYPE
4676 && (gsym->is_from_dynobj()
4677 || gsym->is_undefined()
4678 || gsym->is_preemptible()))
4679 gold_error(_("%s is not a function."),
4680 gsym->demangled_name().c_str());
4681 }
bec53400
DK
4682 break;
4683
4684 case elfcpp::R_ARM_PLT32:
4685 // If the symbol is fully resolved, this is just a relative
4686 // local reloc. Otherwise we need a PLT entry.
4687 if (gsym->final_value_is_known())
4688 break;
4689 // If building a shared library, we can also skip the PLT entry
4690 // if the symbol is defined in the output file and is protected
4691 // or hidden.
4692 if (gsym->is_defined()
4693 && !gsym->is_from_dynobj()
4694 && !gsym->is_preemptible())
4695 break;
2ea97941 4696 target->make_plt_entry(symtab, layout, gsym);
bec53400
DK
4697 break;
4698
4699 case elfcpp::R_ARM_GOTOFF32:
4700 // We need a GOT section.
2ea97941 4701 target->got_section(symtab, layout);
bec53400
DK
4702 break;
4703
4704 case elfcpp::R_ARM_BASE_PREL:
4705 // FIXME: What about this?
4706 break;
4707
4708 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 4709 case elfcpp::R_ARM_GOT_PREL:
bec53400
DK
4710 {
4711 // The symbol requires a GOT entry.
4712 Output_data_got<32, big_endian>* got =
2ea97941 4713 target->got_section(symtab, layout);
bec53400
DK
4714 if (gsym->final_value_is_known())
4715 got->add_global(gsym, GOT_TYPE_STANDARD);
4716 else
4717 {
4718 // If this symbol is not fully resolved, we need to add a
4719 // GOT entry with a dynamic relocation.
2ea97941 4720 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
bec53400
DK
4721 if (gsym->is_from_dynobj()
4722 || gsym->is_undefined()
4723 || gsym->is_preemptible())
4724 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
4725 rel_dyn, elfcpp::R_ARM_GLOB_DAT);
4726 else
4727 {
4728 if (got->add_global(gsym, GOT_TYPE_STANDARD))
4729 rel_dyn->add_global_relative(
4730 gsym, elfcpp::R_ARM_RELATIVE, got,
4731 gsym->got_offset(GOT_TYPE_STANDARD));
4732 }
4733 }
4734 }
4735 break;
4736
4737 case elfcpp::R_ARM_TARGET1:
4738 // This should have been mapped to another type already.
4739 // Fall through.
4740 case elfcpp::R_ARM_COPY:
4741 case elfcpp::R_ARM_GLOB_DAT:
4742 case elfcpp::R_ARM_JUMP_SLOT:
4743 case elfcpp::R_ARM_RELATIVE:
4744 // These are relocations which should only be seen by the
4745 // dynamic linker, and should never be seen here.
4746 gold_error(_("%s: unexpected reloc %u in object file"),
4747 object->name().c_str(), r_type);
4748 break;
4749
4a657b0d
DK
4750 default:
4751 unsupported_reloc_global(object, r_type, gsym);
4752 break;
4753 }
4754}
4755
4756// Process relocations for gc.
4757
4758template<bool big_endian>
4759void
ad0f2072 4760Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
2ea97941 4761 Layout* layout,
4a657b0d
DK
4762 Sized_relobj<32, big_endian>* object,
4763 unsigned int data_shndx,
4764 unsigned int,
4765 const unsigned char* prelocs,
4766 size_t reloc_count,
4767 Output_section* output_section,
4768 bool needs_special_offset_handling,
4769 size_t local_symbol_count,
4770 const unsigned char* plocal_symbols)
4771{
4772 typedef Target_arm<big_endian> Arm;
2ea97941 4773 typedef typename Target_arm<big_endian>::Scan Scan;
4a657b0d 4774
2ea97941 4775 gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4a657b0d 4776 symtab,
2ea97941 4777 layout,
4a657b0d
DK
4778 this,
4779 object,
4780 data_shndx,
4781 prelocs,
4782 reloc_count,
4783 output_section,
4784 needs_special_offset_handling,
4785 local_symbol_count,
4786 plocal_symbols);
4787}
4788
4789// Scan relocations for a section.
4790
4791template<bool big_endian>
4792void
ad0f2072 4793Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
2ea97941 4794 Layout* layout,
4a657b0d
DK
4795 Sized_relobj<32, big_endian>* object,
4796 unsigned int data_shndx,
4797 unsigned int sh_type,
4798 const unsigned char* prelocs,
4799 size_t reloc_count,
4800 Output_section* output_section,
4801 bool needs_special_offset_handling,
4802 size_t local_symbol_count,
4803 const unsigned char* plocal_symbols)
4804{
2ea97941 4805 typedef typename Target_arm<big_endian>::Scan Scan;
4a657b0d
DK
4806 if (sh_type == elfcpp::SHT_RELA)
4807 {
4808 gold_error(_("%s: unsupported RELA reloc section"),
4809 object->name().c_str());
4810 return;
4811 }
4812
2ea97941 4813 gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4a657b0d 4814 symtab,
2ea97941 4815 layout,
4a657b0d
DK
4816 this,
4817 object,
4818 data_shndx,
4819 prelocs,
4820 reloc_count,
4821 output_section,
4822 needs_special_offset_handling,
4823 local_symbol_count,
4824 plocal_symbols);
4825}
4826
4827// Finalize the sections.
4828
4829template<bool big_endian>
4830void
d5b40221 4831Target_arm<big_endian>::do_finalize_sections(
2ea97941 4832 Layout* layout,
f59f41f3
DK
4833 const Input_objects* input_objects,
4834 Symbol_table* symtab)
4a657b0d 4835{
d5b40221
DK
4836 // Merge processor-specific flags.
4837 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4838 p != input_objects->relobj_end();
4839 ++p)
4840 {
4841 Arm_relobj<big_endian>* arm_relobj =
4842 Arm_relobj<big_endian>::as_arm_relobj(*p);
4843 this->merge_processor_specific_flags(
4844 arm_relobj->name(),
4845 arm_relobj->processor_specific_flags());
a0351a69
DK
4846 this->merge_object_attributes(arm_relobj->name().c_str(),
4847 arm_relobj->attributes_section_data());
4848
d5b40221
DK
4849 }
4850
4851 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
4852 p != input_objects->dynobj_end();
4853 ++p)
4854 {
4855 Arm_dynobj<big_endian>* arm_dynobj =
4856 Arm_dynobj<big_endian>::as_arm_dynobj(*p);
4857 this->merge_processor_specific_flags(
4858 arm_dynobj->name(),
4859 arm_dynobj->processor_specific_flags());
a0351a69
DK
4860 this->merge_object_attributes(arm_dynobj->name().c_str(),
4861 arm_dynobj->attributes_section_data());
d5b40221
DK
4862 }
4863
a0351a69
DK
4864 // Check BLX use.
4865 Object_attribute* attr =
4866 this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
4867 if (attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
4868 this->set_may_use_blx(true);
4869
94cdfcff 4870 // Fill in some more dynamic tags.
ea715a34
ILT
4871 const Reloc_section* rel_plt = (this->plt_ == NULL
4872 ? NULL
4873 : this->plt_->rel_plt());
4874 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
4875 this->rel_dyn_, true);
94cdfcff
DK
4876
4877 // Emit any relocs we saved in an attempt to avoid generating COPY
4878 // relocs.
4879 if (this->copy_relocs_.any_saved_relocs())
2ea97941 4880 this->copy_relocs_.emit(this->rel_dyn_section(layout));
11af873f 4881
f59f41f3 4882 // Handle the .ARM.exidx section.
2ea97941 4883 Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
f59f41f3
DK
4884 if (exidx_section != NULL
4885 && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
11af873f
DK
4886 && !parameters->options().relocatable())
4887 {
f59f41f3 4888 // Create __exidx_start and __exdix_end symbols.
99fff23b
ILT
4889 symtab->define_in_output_data("__exidx_start", NULL,
4890 Symbol_table::PREDEFINED,
4891 exidx_section, 0, 0, elfcpp::STT_OBJECT,
a0351a69 4892 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
f59f41f3 4893 false, false);
99fff23b
ILT
4894 symtab->define_in_output_data("__exidx_end", NULL,
4895 Symbol_table::PREDEFINED,
4896 exidx_section, 0, 0, elfcpp::STT_OBJECT,
a0351a69 4897 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
f59f41f3 4898 true, false);
11af873f 4899
f59f41f3
DK
4900 // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4901 // the .ARM.exidx section.
2ea97941 4902 if (!layout->script_options()->saw_phdrs_clause())
11af873f 4903 {
2ea97941 4904 gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
11af873f
DK
4905 == NULL);
4906 Output_segment* exidx_segment =
2ea97941 4907 layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
f5c870d2
ILT
4908 exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
4909 false);
11af873f
DK
4910 }
4911 }
a0351a69
DK
4912
4913 // Create an .ARM.attributes section if there is not one already.
2ea97941 4914 Output_attributes_section_data* attributes_section =
a0351a69 4915 new Output_attributes_section_data(*this->attributes_section_data_);
2ea97941
ILT
4916 layout->add_output_section_data(".ARM.attributes",
4917 elfcpp::SHT_ARM_ATTRIBUTES, 0,
1a2dff53
ILT
4918 attributes_section, false, false, false,
4919 false);
4a657b0d
DK
4920}
4921
bec53400
DK
4922// Return whether a direct absolute static relocation needs to be applied.
4923// In cases where Scan::local() or Scan::global() has created
4924// a dynamic relocation other than R_ARM_RELATIVE, the addend
4925// of the relocation is carried in the data, and we must not
4926// apply the static relocation.
4927
4928template<bool big_endian>
4929inline bool
4930Target_arm<big_endian>::Relocate::should_apply_static_reloc(
4931 const Sized_symbol<32>* gsym,
4932 int ref_flags,
4933 bool is_32bit,
4934 Output_section* output_section)
4935{
4936 // If the output section is not allocated, then we didn't call
4937 // scan_relocs, we didn't create a dynamic reloc, and we must apply
4938 // the reloc here.
4939 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
4940 return true;
4941
4942 // For local symbols, we will have created a non-RELATIVE dynamic
4943 // relocation only if (a) the output is position independent,
4944 // (b) the relocation is absolute (not pc- or segment-relative), and
4945 // (c) the relocation is not 32 bits wide.
4946 if (gsym == NULL)
4947 return !(parameters->options().output_is_position_independent()
4948 && (ref_flags & Symbol::ABSOLUTE_REF)
4949 && !is_32bit);
4950
4951 // For global symbols, we use the same helper routines used in the
4952 // scan pass. If we did not create a dynamic relocation, or if we
4953 // created a RELATIVE dynamic relocation, we should apply the static
4954 // relocation.
4955 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
4956 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
4957 && gsym->can_use_relative_reloc(ref_flags
4958 & Symbol::FUNCTION_CALL);
4959 return !has_dyn || is_rel;
4960}
4961
4a657b0d
DK
4962// Perform a relocation.
4963
4964template<bool big_endian>
4965inline bool
4966Target_arm<big_endian>::Relocate::relocate(
c121c671
DK
4967 const Relocate_info<32, big_endian>* relinfo,
4968 Target_arm* target,
4969 Output_section *output_section,
4970 size_t relnum,
4971 const elfcpp::Rel<32, big_endian>& rel,
4a657b0d 4972 unsigned int r_type,
c121c671
DK
4973 const Sized_symbol<32>* gsym,
4974 const Symbol_value<32>* psymval,
4975 unsigned char* view,
ebabffbd 4976 Arm_address address,
4a657b0d
DK
4977 section_size_type /* view_size */ )
4978{
c121c671
DK
4979 typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
4980
a6d1ef57 4981 r_type = get_real_reloc_type(r_type);
c121c671 4982
2daedcd6
DK
4983 const Arm_relobj<big_endian>* object =
4984 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
c121c671 4985
2daedcd6
DK
4986 // If the final branch target of a relocation is THUMB instruction, this
4987 // is 1. Otherwise it is 0.
4988 Arm_address thumb_bit = 0;
c121c671 4989 Symbol_value<32> symval;
d204b6e9 4990 bool is_weakly_undefined_without_plt = false;
2daedcd6 4991 if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
c121c671 4992 {
2daedcd6
DK
4993 if (gsym != NULL)
4994 {
4995 // This is a global symbol. Determine if we use PLT and if the
4996 // final target is THUMB.
4997 if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
4998 {
4999 // This uses a PLT, change the symbol value.
5000 symval.set_output_value(target->plt_section()->address()
5001 + gsym->plt_offset());
5002 psymval = &symval;
5003 }
d204b6e9
DK
5004 else if (gsym->is_weak_undefined())
5005 {
5006 // This is a weakly undefined symbol and we do not use PLT
5007 // for this relocation. A branch targeting this symbol will
5008 // be converted into an NOP.
5009 is_weakly_undefined_without_plt = true;
5010 }
2daedcd6
DK
5011 else
5012 {
5013 // Set thumb bit if symbol:
5014 // -Has type STT_ARM_TFUNC or
5015 // -Has type STT_FUNC, is defined and with LSB in value set.
5016 thumb_bit =
5017 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
5018 || (gsym->type() == elfcpp::STT_FUNC
5019 && !gsym->is_undefined()
5020 && ((psymval->value(object, 0) & 1) != 0)))
5021 ? 1
5022 : 0);
5023 }
5024 }
5025 else
5026 {
5027 // This is a local symbol. Determine if the final target is THUMB.
5028 // We saved this information when all the local symbols were read.
5029 elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
5030 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
5031 thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
5032 }
5033 }
5034 else
5035 {
5036 // This is a fake relocation synthesized for a stub. It does not have
5037 // a real symbol. We just look at the LSB of the symbol value to
5038 // determine if the target is THUMB or not.
5039 thumb_bit = ((psymval->value(object, 0) & 1) != 0);
c121c671
DK
5040 }
5041
2daedcd6
DK
5042 // Strip LSB if this points to a THUMB target.
5043 if (thumb_bit != 0
5044 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
5045 && ((psymval->value(object, 0) & 1) != 0))
5046 {
5047 Arm_address stripped_value =
5048 psymval->value(object, 0) & ~static_cast<Arm_address>(1);
5049 symval.set_output_value(stripped_value);
5050 psymval = &symval;
5051 }
5052
c121c671
DK
5053 // Get the GOT offset if needed.
5054 // The GOT pointer points to the end of the GOT section.
5055 // We need to subtract the size of the GOT section to get
5056 // the actual offset to use in the relocation.
5057 bool have_got_offset = false;
5058 unsigned int got_offset = 0;
5059 switch (r_type)
5060 {
5061 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 5062 case elfcpp::R_ARM_GOT_PREL:
c121c671
DK
5063 if (gsym != NULL)
5064 {
5065 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5066 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
5067 - target->got_size());
5068 }
5069 else
5070 {
5071 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5072 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5073 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
5074 - target->got_size());
5075 }
5076 have_got_offset = true;
5077 break;
5078
5079 default:
5080 break;
5081 }
5082
d204b6e9
DK
5083 // To look up relocation stubs, we need to pass the symbol table index of
5084 // a local symbol.
5085 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5086
c121c671
DK
5087 typename Arm_relocate_functions::Status reloc_status =
5088 Arm_relocate_functions::STATUS_OKAY;
4a657b0d
DK
5089 switch (r_type)
5090 {
5091 case elfcpp::R_ARM_NONE:
5092 break;
5093
5e445df6
ILT
5094 case elfcpp::R_ARM_ABS8:
5095 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5096 output_section))
be8fcb75
ILT
5097 reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
5098 break;
5099
5100 case elfcpp::R_ARM_ABS12:
5101 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5102 output_section))
5103 reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
5104 break;
5105
5106 case elfcpp::R_ARM_ABS16:
5107 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5108 output_section))
5109 reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5e445df6
ILT
5110 break;
5111
c121c671
DK
5112 case elfcpp::R_ARM_ABS32:
5113 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5114 output_section))
5115 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2daedcd6 5116 thumb_bit);
c121c671
DK
5117 break;
5118
be8fcb75
ILT
5119 case elfcpp::R_ARM_ABS32_NOI:
5120 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5121 output_section))
5122 // No thumb bit for this relocation: (S + A)
5123 reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
f4e5969c 5124 0);
be8fcb75
ILT
5125 break;
5126
fd3c5f0b
ILT
5127 case elfcpp::R_ARM_MOVW_ABS_NC:
5128 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5129 output_section))
5130 reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
5131 psymval,
2daedcd6 5132 thumb_bit);
fd3c5f0b
ILT
5133 else
5134 gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5135 "a shared object; recompile with -fPIC"));
5136 break;
5137
5138 case elfcpp::R_ARM_MOVT_ABS:
5139 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5140 output_section))
5141 reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
5142 else
5143 gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5144 "a shared object; recompile with -fPIC"));
5145 break;
5146
5147 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5148 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5149 output_section))
5150 reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
5151 psymval,
2daedcd6 5152 thumb_bit);
fd3c5f0b
ILT
5153 else
5154 gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5155 "making a shared object; recompile with -fPIC"));
5156 break;
5157
5158 case elfcpp::R_ARM_THM_MOVT_ABS:
5159 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5160 output_section))
5161 reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
5162 psymval);
5163 else
5164 gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5165 "making a shared object; recompile with -fPIC"));
5166 break;
5167
c2a122b6
ILT
5168 case elfcpp::R_ARM_MOVW_PREL_NC:
5169 reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
5170 psymval, address,
2daedcd6 5171 thumb_bit);
c2a122b6
ILT
5172 break;
5173
5174 case elfcpp::R_ARM_MOVT_PREL:
5175 reloc_status = Arm_relocate_functions::movt_prel(view, object,
5176 psymval, address);
5177 break;
5178
5179 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5180 reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
5181 psymval, address,
2daedcd6 5182 thumb_bit);
c2a122b6
ILT
5183 break;
5184
5185 case elfcpp::R_ARM_THM_MOVT_PREL:
5186 reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
5187 psymval, address);
5188 break;
5189
c121c671
DK
5190 case elfcpp::R_ARM_REL32:
5191 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2daedcd6 5192 address, thumb_bit);
c121c671
DK
5193 break;
5194
be8fcb75
ILT
5195 case elfcpp::R_ARM_THM_ABS5:
5196 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5197 output_section))
5198 reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
5199 break;
5200
c121c671 5201 case elfcpp::R_ARM_THM_CALL:
51938283
DK
5202 reloc_status =
5203 Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
5204 psymval, address, thumb_bit,
5205 is_weakly_undefined_without_plt);
c121c671
DK
5206 break;
5207
d204b6e9
DK
5208 case elfcpp::R_ARM_XPC25:
5209 reloc_status =
5210 Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
5211 psymval, address, thumb_bit,
5212 is_weakly_undefined_without_plt);
5213 break;
5214
51938283
DK
5215 case elfcpp::R_ARM_THM_XPC22:
5216 reloc_status =
5217 Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
5218 psymval, address, thumb_bit,
5219 is_weakly_undefined_without_plt);
5220 break;
5221
c121c671
DK
5222 case elfcpp::R_ARM_GOTOFF32:
5223 {
ebabffbd 5224 Arm_address got_origin;
c121c671
DK
5225 got_origin = target->got_plt_section()->address();
5226 reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2daedcd6 5227 got_origin, thumb_bit);
c121c671
DK
5228 }
5229 break;
5230
5231 case elfcpp::R_ARM_BASE_PREL:
5232 {
5233 uint32_t origin;
5234 // Get the addressing origin of the output segment defining the
5235 // symbol gsym (AAELF 4.6.1.2 Relocation types)
5236 gold_assert(gsym != NULL);
5237 if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5238 origin = gsym->output_segment()->vaddr();
5239 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5240 origin = gsym->output_data()->address();
5241 else
5242 {
5243 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5244 _("cannot find origin of R_ARM_BASE_PREL"));
5245 return true;
5246 }
5247 reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
5248 }
5249 break;
5250
be8fcb75
ILT
5251 case elfcpp::R_ARM_BASE_ABS:
5252 {
5253 if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5254 output_section))
5255 break;
5256
5257 uint32_t origin;
5258 // Get the addressing origin of the output segment defining
5259 // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5260 if (gsym == NULL)
5261 // R_ARM_BASE_ABS with the NULL symbol will give the
5262 // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5263 // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5264 origin = target->got_plt_section()->address();
5265 else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5266 origin = gsym->output_segment()->vaddr();
5267 else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5268 origin = gsym->output_data()->address();
5269 else
5270 {
5271 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5272 _("cannot find origin of R_ARM_BASE_ABS"));
5273 return true;
5274 }
5275
5276 reloc_status = Arm_relocate_functions::base_abs(view, origin);
5277 }
5278 break;
5279
c121c671
DK
5280 case elfcpp::R_ARM_GOT_BREL:
5281 gold_assert(have_got_offset);
5282 reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
5283 break;
5284
7f5309a5
ILT
5285 case elfcpp::R_ARM_GOT_PREL:
5286 gold_assert(have_got_offset);
5287 // Get the address origin for GOT PLT, which is allocated right
5288 // after the GOT section, to calculate an absolute address of
5289 // the symbol GOT entry (got_origin + got_offset).
ebabffbd 5290 Arm_address got_origin;
7f5309a5
ILT
5291 got_origin = target->got_plt_section()->address();
5292 reloc_status = Arm_relocate_functions::got_prel(view,
5293 got_origin + got_offset,
5294 address);
5295 break;
5296
c121c671
DK
5297 case elfcpp::R_ARM_PLT32:
5298 gold_assert(gsym == NULL
5299 || gsym->has_plt_offset()
5300 || gsym->final_value_is_known()
5301 || (gsym->is_defined()
5302 && !gsym->is_from_dynobj()
5303 && !gsym->is_preemptible()));
d204b6e9
DK
5304 reloc_status =
5305 Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
5306 psymval, address, thumb_bit,
5307 is_weakly_undefined_without_plt);
c121c671
DK
5308 break;
5309
5310 case elfcpp::R_ARM_CALL:
d204b6e9
DK
5311 reloc_status =
5312 Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
5313 psymval, address, thumb_bit,
5314 is_weakly_undefined_without_plt);
c121c671
DK
5315 break;
5316
5317 case elfcpp::R_ARM_JUMP24:
d204b6e9
DK
5318 reloc_status =
5319 Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
5320 psymval, address, thumb_bit,
5321 is_weakly_undefined_without_plt);
c121c671
DK
5322 break;
5323
51938283
DK
5324 case elfcpp::R_ARM_THM_JUMP24:
5325 reloc_status =
5326 Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
5327 psymval, address, thumb_bit,
5328 is_weakly_undefined_without_plt);
5329 break;
5330
c121c671
DK
5331 case elfcpp::R_ARM_PREL31:
5332 reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
2daedcd6 5333 address, thumb_bit);
c121c671
DK
5334 break;
5335
5336 case elfcpp::R_ARM_TARGET1:
5337 // This should have been mapped to another type already.
5338 // Fall through.
5339 case elfcpp::R_ARM_COPY:
5340 case elfcpp::R_ARM_GLOB_DAT:
5341 case elfcpp::R_ARM_JUMP_SLOT:
5342 case elfcpp::R_ARM_RELATIVE:
5343 // These are relocations which should only be seen by the
5344 // dynamic linker, and should never be seen here.
5345 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5346 _("unexpected reloc %u in object file"),
5347 r_type);
5348 break;
5349
5350 default:
5351 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5352 _("unsupported reloc %u"),
5353 r_type);
5354 break;
5355 }
5356
5357 // Report any errors.
5358 switch (reloc_status)
5359 {
5360 case Arm_relocate_functions::STATUS_OKAY:
5361 break;
5362 case Arm_relocate_functions::STATUS_OVERFLOW:
5363 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5364 _("relocation overflow in relocation %u"),
5365 r_type);
5366 break;
5367 case Arm_relocate_functions::STATUS_BAD_RELOC:
5368 gold_error_at_location(
5369 relinfo,
5370 relnum,
5371 rel.get_r_offset(),
5372 _("unexpected opcode while processing relocation %u"),
5373 r_type);
5374 break;
4a657b0d
DK
5375 default:
5376 gold_unreachable();
5377 }
5378
5379 return true;
5380}
5381
5382// Relocate section data.
5383
5384template<bool big_endian>
5385void
5386Target_arm<big_endian>::relocate_section(
5387 const Relocate_info<32, big_endian>* relinfo,
5388 unsigned int sh_type,
5389 const unsigned char* prelocs,
5390 size_t reloc_count,
5391 Output_section* output_section,
5392 bool needs_special_offset_handling,
5393 unsigned char* view,
ebabffbd 5394 Arm_address address,
364c7fa5
ILT
5395 section_size_type view_size,
5396 const Reloc_symbol_changes* reloc_symbol_changes)
4a657b0d
DK
5397{
5398 typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
5399 gold_assert(sh_type == elfcpp::SHT_REL);
5400
43d12afe
DK
5401 Arm_input_section<big_endian>* arm_input_section =
5402 this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
5403
5404 // This is an ARM input section and the view covers the whole output
5405 // section.
5406 if (arm_input_section != NULL)
5407 {
5408 gold_assert(needs_special_offset_handling);
5409 Arm_address section_address = arm_input_section->address();
5410 section_size_type section_size = arm_input_section->data_size();
5411
5412 gold_assert((arm_input_section->address() >= address)
5413 && ((arm_input_section->address()
5414 + arm_input_section->data_size())
5415 <= (address + view_size)));
5416
2ea97941
ILT
5417 off_t offset = section_address - address;
5418 view += offset;
5419 address += offset;
43d12afe
DK
5420 view_size = section_size;
5421 }
5422
4a657b0d
DK
5423 gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
5424 Arm_relocate>(
5425 relinfo,
5426 this,
5427 prelocs,
5428 reloc_count,
5429 output_section,
5430 needs_special_offset_handling,
5431 view,
5432 address,
364c7fa5
ILT
5433 view_size,
5434 reloc_symbol_changes);
4a657b0d
DK
5435}
5436
5437// Return the size of a relocation while scanning during a relocatable
5438// link.
5439
5440template<bool big_endian>
5441unsigned int
5442Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
5443 unsigned int r_type,
5444 Relobj* object)
5445{
a6d1ef57 5446 r_type = get_real_reloc_type(r_type);
4a657b0d
DK
5447 switch (r_type)
5448 {
5449 case elfcpp::R_ARM_NONE:
5450 return 0;
5451
5e445df6
ILT
5452 case elfcpp::R_ARM_ABS8:
5453 return 1;
5454
be8fcb75
ILT
5455 case elfcpp::R_ARM_ABS16:
5456 case elfcpp::R_ARM_THM_ABS5:
5457 return 2;
5458
4a657b0d 5459 case elfcpp::R_ARM_ABS32:
be8fcb75
ILT
5460 case elfcpp::R_ARM_ABS32_NOI:
5461 case elfcpp::R_ARM_ABS12:
5462 case elfcpp::R_ARM_BASE_ABS:
4a657b0d
DK
5463 case elfcpp::R_ARM_REL32:
5464 case elfcpp::R_ARM_THM_CALL:
5465 case elfcpp::R_ARM_GOTOFF32:
5466 case elfcpp::R_ARM_BASE_PREL:
5467 case elfcpp::R_ARM_GOT_BREL:
7f5309a5 5468 case elfcpp::R_ARM_GOT_PREL:
4a657b0d
DK
5469 case elfcpp::R_ARM_PLT32:
5470 case elfcpp::R_ARM_CALL:
5471 case elfcpp::R_ARM_JUMP24:
5472 case elfcpp::R_ARM_PREL31:
fd3c5f0b
ILT
5473 case elfcpp::R_ARM_MOVW_ABS_NC:
5474 case elfcpp::R_ARM_MOVT_ABS:
5475 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5476 case elfcpp::R_ARM_THM_MOVT_ABS:
c2a122b6
ILT
5477 case elfcpp::R_ARM_MOVW_PREL_NC:
5478 case elfcpp::R_ARM_MOVT_PREL:
5479 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5480 case elfcpp::R_ARM_THM_MOVT_PREL:
4a657b0d
DK
5481 return 4;
5482
5483 case elfcpp::R_ARM_TARGET1:
5484 // This should have been mapped to another type already.
5485 // Fall through.
5486 case elfcpp::R_ARM_COPY:
5487 case elfcpp::R_ARM_GLOB_DAT:
5488 case elfcpp::R_ARM_JUMP_SLOT:
5489 case elfcpp::R_ARM_RELATIVE:
5490 // These are relocations which should only be seen by the
5491 // dynamic linker, and should never be seen here.
5492 gold_error(_("%s: unexpected reloc %u in object file"),
5493 object->name().c_str(), r_type);
5494 return 0;
5495
5496 default:
5497 object->error(_("unsupported reloc %u in object file"), r_type);
5498 return 0;
5499 }
5500}
5501
5502// Scan the relocs during a relocatable link.
5503
5504template<bool big_endian>
5505void
5506Target_arm<big_endian>::scan_relocatable_relocs(
4a657b0d 5507 Symbol_table* symtab,
2ea97941 5508 Layout* layout,
4a657b0d
DK
5509 Sized_relobj<32, big_endian>* object,
5510 unsigned int data_shndx,
5511 unsigned int sh_type,
5512 const unsigned char* prelocs,
5513 size_t reloc_count,
5514 Output_section* output_section,
5515 bool needs_special_offset_handling,
5516 size_t local_symbol_count,
5517 const unsigned char* plocal_symbols,
5518 Relocatable_relocs* rr)
5519{
5520 gold_assert(sh_type == elfcpp::SHT_REL);
5521
5522 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
5523 Relocatable_size_for_reloc> Scan_relocatable_relocs;
5524
5525 gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
5526 Scan_relocatable_relocs>(
4a657b0d 5527 symtab,
2ea97941 5528 layout,
4a657b0d
DK
5529 object,
5530 data_shndx,
5531 prelocs,
5532 reloc_count,
5533 output_section,
5534 needs_special_offset_handling,
5535 local_symbol_count,
5536 plocal_symbols,
5537 rr);
5538}
5539
5540// Relocate a section during a relocatable link.
5541
5542template<bool big_endian>
5543void
5544Target_arm<big_endian>::relocate_for_relocatable(
5545 const Relocate_info<32, big_endian>* relinfo,
5546 unsigned int sh_type,
5547 const unsigned char* prelocs,
5548 size_t reloc_count,
5549 Output_section* output_section,
5550 off_t offset_in_output_section,
5551 const Relocatable_relocs* rr,
5552 unsigned char* view,
ebabffbd 5553 Arm_address view_address,
4a657b0d
DK
5554 section_size_type view_size,
5555 unsigned char* reloc_view,
5556 section_size_type reloc_view_size)
5557{
5558 gold_assert(sh_type == elfcpp::SHT_REL);
5559
5560 gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
5561 relinfo,
5562 prelocs,
5563 reloc_count,
5564 output_section,
5565 offset_in_output_section,
5566 rr,
5567 view,
5568 view_address,
5569 view_size,
5570 reloc_view,
5571 reloc_view_size);
5572}
5573
94cdfcff
DK
5574// Return the value to use for a dynamic symbol which requires special
5575// treatment. This is how we support equality comparisons of function
5576// pointers across shared library boundaries, as described in the
5577// processor specific ABI supplement.
5578
4a657b0d
DK
5579template<bool big_endian>
5580uint64_t
94cdfcff 5581Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4a657b0d 5582{
94cdfcff
DK
5583 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5584 return this->plt_section()->address() + gsym->plt_offset();
4a657b0d
DK
5585}
5586
5587// Map platform-specific relocs to real relocs
5588//
5589template<bool big_endian>
5590unsigned int
a6d1ef57 5591Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4a657b0d
DK
5592{
5593 switch (r_type)
5594 {
5595 case elfcpp::R_ARM_TARGET1:
a6d1ef57
DK
5596 // This is either R_ARM_ABS32 or R_ARM_REL32;
5597 return elfcpp::R_ARM_ABS32;
4a657b0d
DK
5598
5599 case elfcpp::R_ARM_TARGET2:
a6d1ef57
DK
5600 // This can be any reloc type but ususally is R_ARM_GOT_PREL
5601 return elfcpp::R_ARM_GOT_PREL;
4a657b0d
DK
5602
5603 default:
5604 return r_type;
5605 }
5606}
5607
d5b40221
DK
5608// Whether if two EABI versions V1 and V2 are compatible.
5609
5610template<bool big_endian>
5611bool
5612Target_arm<big_endian>::are_eabi_versions_compatible(
5613 elfcpp::Elf_Word v1,
5614 elfcpp::Elf_Word v2)
5615{
5616 // v4 and v5 are the same spec before and after it was released,
5617 // so allow mixing them.
5618 if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
5619 || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
5620 return true;
5621
5622 return v1 == v2;
5623}
5624
5625// Combine FLAGS from an input object called NAME and the processor-specific
5626// flags in the ELF header of the output. Much of this is adapted from the
5627// processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5628// in bfd/elf32-arm.c.
5629
5630template<bool big_endian>
5631void
5632Target_arm<big_endian>::merge_processor_specific_flags(
5633 const std::string& name,
5634 elfcpp::Elf_Word flags)
5635{
5636 if (this->are_processor_specific_flags_set())
5637 {
5638 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
5639
5640 // Nothing to merge if flags equal to those in output.
5641 if (flags == out_flags)
5642 return;
5643
5644 // Complain about various flag mismatches.
5645 elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
5646 elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
5647 if (!this->are_eabi_versions_compatible(version1, version2))
5648 gold_error(_("Source object %s has EABI version %d but output has "
5649 "EABI version %d."),
5650 name.c_str(),
5651 (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
5652 (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
5653 }
5654 else
5655 {
5656 // If the input is the default architecture and had the default
5657 // flags then do not bother setting the flags for the output
5658 // architecture, instead allow future merges to do this. If no
5659 // future merges ever set these flags then they will retain their
5660 // uninitialised values, which surprise surprise, correspond
5661 // to the default values.
5662 if (flags == 0)
5663 return;
5664
5665 // This is the first time, just copy the flags.
5666 // We only copy the EABI version for now.
5667 this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
5668 }
5669}
5670
5671// Adjust ELF file header.
5672template<bool big_endian>
5673void
5674Target_arm<big_endian>::do_adjust_elf_header(
5675 unsigned char* view,
5676 int len) const
5677{
5678 gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
5679
5680 elfcpp::Ehdr<32, big_endian> ehdr(view);
5681 unsigned char e_ident[elfcpp::EI_NIDENT];
5682 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
5683
5684 if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5685 == elfcpp::EF_ARM_EABI_UNKNOWN)
5686 e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
5687 else
5688 e_ident[elfcpp::EI_OSABI] = 0;
5689 e_ident[elfcpp::EI_ABIVERSION] = 0;
5690
5691 // FIXME: Do EF_ARM_BE8 adjustment.
5692
5693 elfcpp::Ehdr_write<32, big_endian> oehdr(view);
5694 oehdr.put_e_ident(e_ident);
5695}
5696
5697// do_make_elf_object to override the same function in the base class.
5698// We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5699// to store ARM specific information. Hence we need to have our own
5700// ELF object creation.
5701
5702template<bool big_endian>
5703Object*
5704Target_arm<big_endian>::do_make_elf_object(
5705 const std::string& name,
5706 Input_file* input_file,
2ea97941 5707 off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
d5b40221
DK
5708{
5709 int et = ehdr.get_e_type();
5710 if (et == elfcpp::ET_REL)
5711 {
5712 Arm_relobj<big_endian>* obj =
2ea97941 5713 new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
d5b40221
DK
5714 obj->setup();
5715 return obj;
5716 }
5717 else if (et == elfcpp::ET_DYN)
5718 {
5719 Sized_dynobj<32, big_endian>* obj =
2ea97941 5720 new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
d5b40221
DK
5721 obj->setup();
5722 return obj;
5723 }
5724 else
5725 {
5726 gold_error(_("%s: unsupported ELF file type %d"),
5727 name.c_str(), et);
5728 return NULL;
5729 }
5730}
5731
a0351a69
DK
5732// Read the architecture from the Tag_also_compatible_with attribute, if any.
5733// Returns -1 if no architecture could be read.
5734// This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
5735
5736template<bool big_endian>
5737int
5738Target_arm<big_endian>::get_secondary_compatible_arch(
5739 const Attributes_section_data* pasd)
5740{
5741 const Object_attribute *known_attributes =
5742 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
5743
5744 // Note: the tag and its argument below are uleb128 values, though
5745 // currently-defined values fit in one byte for each.
5746 const std::string& sv =
5747 known_attributes[elfcpp::Tag_also_compatible_with].string_value();
5748 if (sv.size() == 2
5749 && sv.data()[0] == elfcpp::Tag_CPU_arch
5750 && (sv.data()[1] & 128) != 128)
5751 return sv.data()[1];
5752
5753 // This tag is "safely ignorable", so don't complain if it looks funny.
5754 return -1;
5755}
5756
5757// Set, or unset, the architecture of the Tag_also_compatible_with attribute.
5758// The tag is removed if ARCH is -1.
5759// This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
5760
5761template<bool big_endian>
5762void
5763Target_arm<big_endian>::set_secondary_compatible_arch(
5764 Attributes_section_data* pasd,
5765 int arch)
5766{
5767 Object_attribute *known_attributes =
5768 pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
5769
5770 if (arch == -1)
5771 {
5772 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
5773 return;
5774 }
5775
5776 // Note: the tag and its argument below are uleb128 values, though
5777 // currently-defined values fit in one byte for each.
5778 char sv[3];
5779 sv[0] = elfcpp::Tag_CPU_arch;
5780 gold_assert(arch != 0);
5781 sv[1] = arch;
5782 sv[2] = '\0';
5783
5784 known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
5785}
5786
5787// Combine two values for Tag_CPU_arch, taking secondary compatibility tags
5788// into account.
5789// This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
5790
5791template<bool big_endian>
5792int
5793Target_arm<big_endian>::tag_cpu_arch_combine(
5794 const char* name,
5795 int oldtag,
5796 int* secondary_compat_out,
5797 int newtag,
5798 int secondary_compat)
5799{
5800#define T(X) elfcpp::TAG_CPU_ARCH_##X
5801 static const int v6t2[] =
5802 {
5803 T(V6T2), // PRE_V4.
5804 T(V6T2), // V4.
5805 T(V6T2), // V4T.
5806 T(V6T2), // V5T.
5807 T(V6T2), // V5TE.
5808 T(V6T2), // V5TEJ.
5809 T(V6T2), // V6.
5810 T(V7), // V6KZ.
5811 T(V6T2) // V6T2.
5812 };
5813 static const int v6k[] =
5814 {
5815 T(V6K), // PRE_V4.
5816 T(V6K), // V4.
5817 T(V6K), // V4T.
5818 T(V6K), // V5T.
5819 T(V6K), // V5TE.
5820 T(V6K), // V5TEJ.
5821 T(V6K), // V6.
5822 T(V6KZ), // V6KZ.
5823 T(V7), // V6T2.
5824 T(V6K) // V6K.
5825 };
5826 static const int v7[] =
5827 {
5828 T(V7), // PRE_V4.
5829 T(V7), // V4.
5830 T(V7), // V4T.
5831 T(V7), // V5T.
5832 T(V7), // V5TE.
5833 T(V7), // V5TEJ.
5834 T(V7), // V6.
5835 T(V7), // V6KZ.
5836 T(V7), // V6T2.
5837 T(V7), // V6K.
5838 T(V7) // V7.
5839 };
5840 static const int v6_m[] =
5841 {
5842 -1, // PRE_V4.
5843 -1, // V4.
5844 T(V6K), // V4T.
5845 T(V6K), // V5T.
5846 T(V6K), // V5TE.
5847 T(V6K), // V5TEJ.
5848 T(V6K), // V6.
5849 T(V6KZ), // V6KZ.
5850 T(V7), // V6T2.
5851 T(V6K), // V6K.
5852 T(V7), // V7.
5853 T(V6_M) // V6_M.
5854 };
5855 static const int v6s_m[] =
5856 {
5857 -1, // PRE_V4.
5858 -1, // V4.
5859 T(V6K), // V4T.
5860 T(V6K), // V5T.
5861 T(V6K), // V5TE.
5862 T(V6K), // V5TEJ.
5863 T(V6K), // V6.
5864 T(V6KZ), // V6KZ.
5865 T(V7), // V6T2.
5866 T(V6K), // V6K.
5867 T(V7), // V7.
5868 T(V6S_M), // V6_M.
5869 T(V6S_M) // V6S_M.
5870 };
5871 static const int v7e_m[] =
5872 {
5873 -1, // PRE_V4.
5874 -1, // V4.
5875 T(V7E_M), // V4T.
5876 T(V7E_M), // V5T.
5877 T(V7E_M), // V5TE.
5878 T(V7E_M), // V5TEJ.
5879 T(V7E_M), // V6.
5880 T(V7E_M), // V6KZ.
5881 T(V7E_M), // V6T2.
5882 T(V7E_M), // V6K.
5883 T(V7E_M), // V7.
5884 T(V7E_M), // V6_M.
5885 T(V7E_M), // V6S_M.
5886 T(V7E_M) // V7E_M.
5887 };
5888 static const int v4t_plus_v6_m[] =
5889 {
5890 -1, // PRE_V4.
5891 -1, // V4.
5892 T(V4T), // V4T.
5893 T(V5T), // V5T.
5894 T(V5TE), // V5TE.
5895 T(V5TEJ), // V5TEJ.
5896 T(V6), // V6.
5897 T(V6KZ), // V6KZ.
5898 T(V6T2), // V6T2.
5899 T(V6K), // V6K.
5900 T(V7), // V7.
5901 T(V6_M), // V6_M.
5902 T(V6S_M), // V6S_M.
5903 T(V7E_M), // V7E_M.
5904 T(V4T_PLUS_V6_M) // V4T plus V6_M.
5905 };
5906 static const int *comb[] =
5907 {
5908 v6t2,
5909 v6k,
5910 v7,
5911 v6_m,
5912 v6s_m,
5913 v7e_m,
5914 // Pseudo-architecture.
5915 v4t_plus_v6_m
5916 };
5917
5918 // Check we've not got a higher architecture than we know about.
5919
5920 if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
5921 {
5922 gold_error(_("%s: unknown CPU architecture"), name);
5923 return -1;
5924 }
5925
5926 // Override old tag if we have a Tag_also_compatible_with on the output.
5927
5928 if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
5929 || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
5930 oldtag = T(V4T_PLUS_V6_M);
5931
5932 // And override the new tag if we have a Tag_also_compatible_with on the
5933 // input.
5934
5935 if ((newtag == T(V6_M) && secondary_compat == T(V4T))
5936 || (newtag == T(V4T) && secondary_compat == T(V6_M)))
5937 newtag = T(V4T_PLUS_V6_M);
5938
5939 // Architectures before V6KZ add features monotonically.
5940 int tagh = std::max(oldtag, newtag);
5941 if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
5942 return tagh;
5943
5944 int tagl = std::min(oldtag, newtag);
5945 int result = comb[tagh - T(V6T2)][tagl];
5946
5947 // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
5948 // as the canonical version.
5949 if (result == T(V4T_PLUS_V6_M))
5950 {
5951 result = T(V4T);
5952 *secondary_compat_out = T(V6_M);
5953 }
5954 else
5955 *secondary_compat_out = -1;
5956
5957 if (result == -1)
5958 {
5959 gold_error(_("%s: conflicting CPU architectures %d/%d"),
5960 name, oldtag, newtag);
5961 return -1;
5962 }
5963
5964 return result;
5965#undef T
5966}
5967
5968// Helper to print AEABI enum tag value.
5969
5970template<bool big_endian>
5971std::string
5972Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
5973{
5974 static const char *aeabi_enum_names[] =
5975 { "", "variable-size", "32-bit", "" };
5976 const size_t aeabi_enum_names_size =
5977 sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
5978
5979 if (value < aeabi_enum_names_size)
5980 return std::string(aeabi_enum_names[value]);
5981 else
5982 {
5983 char buffer[100];
5984 sprintf(buffer, "<unknown value %u>", value);
5985 return std::string(buffer);
5986 }
5987}
5988
5989// Return the string value to store in TAG_CPU_name.
5990
5991template<bool big_endian>
5992std::string
5993Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
5994{
5995 static const char *name_table[] = {
5996 // These aren't real CPU names, but we can't guess
5997 // that from the architecture version alone.
5998 "Pre v4",
5999 "ARM v4",
6000 "ARM v4T",
6001 "ARM v5T",
6002 "ARM v5TE",
6003 "ARM v5TEJ",
6004 "ARM v6",
6005 "ARM v6KZ",
6006 "ARM v6T2",
6007 "ARM v6K",
6008 "ARM v7",
6009 "ARM v6-M",
6010 "ARM v6S-M",
6011 "ARM v7E-M"
6012 };
6013 const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
6014
6015 if (value < name_table_size)
6016 return std::string(name_table[value]);
6017 else
6018 {
6019 char buffer[100];
6020 sprintf(buffer, "<unknown CPU value %u>", value);
6021 return std::string(buffer);
6022 }
6023}
6024
6025// Merge object attributes from input file called NAME with those of the
6026// output. The input object attributes are in the object pointed by PASD.
6027
6028template<bool big_endian>
6029void
6030Target_arm<big_endian>::merge_object_attributes(
6031 const char* name,
6032 const Attributes_section_data* pasd)
6033{
6034 // Return if there is no attributes section data.
6035 if (pasd == NULL)
6036 return;
6037
6038 // If output has no object attributes, just copy.
6039 if (this->attributes_section_data_ == NULL)
6040 {
6041 this->attributes_section_data_ = new Attributes_section_data(*pasd);
6042 return;
6043 }
6044
6045 const int vendor = Object_attribute::OBJ_ATTR_PROC;
6046 const Object_attribute* in_attr = pasd->known_attributes(vendor);
6047 Object_attribute* out_attr =
6048 this->attributes_section_data_->known_attributes(vendor);
6049
6050 // This needs to happen before Tag_ABI_FP_number_model is merged. */
6051 if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
6052 != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
6053 {
6054 // Ignore mismatches if the object doesn't use floating point. */
6055 if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
6056 out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
6057 in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
6058 else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
6059 gold_error(_("%s uses VFP register arguments, output does not"),
6060 name);
6061 }
6062
6063 for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
6064 {
6065 // Merge this attribute with existing attributes.
6066 switch (i)
6067 {
6068 case elfcpp::Tag_CPU_raw_name:
6069 case elfcpp::Tag_CPU_name:
6070 // These are merged after Tag_CPU_arch.
6071 break;
6072
6073 case elfcpp::Tag_ABI_optimization_goals:
6074 case elfcpp::Tag_ABI_FP_optimization_goals:
6075 // Use the first value seen.
6076 break;
6077
6078 case elfcpp::Tag_CPU_arch:
6079 {
6080 unsigned int saved_out_attr = out_attr->int_value();
6081 // Merge Tag_CPU_arch and Tag_also_compatible_with.
6082 int secondary_compat =
6083 this->get_secondary_compatible_arch(pasd);
6084 int secondary_compat_out =
6085 this->get_secondary_compatible_arch(
6086 this->attributes_section_data_);
6087 out_attr[i].set_int_value(
6088 tag_cpu_arch_combine(name, out_attr[i].int_value(),
6089 &secondary_compat_out,
6090 in_attr[i].int_value(),
6091 secondary_compat));
6092 this->set_secondary_compatible_arch(this->attributes_section_data_,
6093 secondary_compat_out);
6094
6095 // Merge Tag_CPU_name and Tag_CPU_raw_name.
6096 if (out_attr[i].int_value() == saved_out_attr)
6097 ; // Leave the names alone.
6098 else if (out_attr[i].int_value() == in_attr[i].int_value())
6099 {
6100 // The output architecture has been changed to match the
6101 // input architecture. Use the input names.
6102 out_attr[elfcpp::Tag_CPU_name].set_string_value(
6103 in_attr[elfcpp::Tag_CPU_name].string_value());
6104 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
6105 in_attr[elfcpp::Tag_CPU_raw_name].string_value());
6106 }
6107 else
6108 {
6109 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
6110 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
6111 }
6112
6113 // If we still don't have a value for Tag_CPU_name,
6114 // make one up now. Tag_CPU_raw_name remains blank.
6115 if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
6116 {
6117 const std::string cpu_name =
6118 this->tag_cpu_name_value(out_attr[i].int_value());
6119 // FIXME: If we see an unknown CPU, this will be set
6120 // to "<unknown CPU n>", where n is the attribute value.
6121 // This is different from BFD, which leaves the name alone.
6122 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
6123 }
6124 }
6125 break;
6126
6127 case elfcpp::Tag_ARM_ISA_use:
6128 case elfcpp::Tag_THUMB_ISA_use:
6129 case elfcpp::Tag_WMMX_arch:
6130 case elfcpp::Tag_Advanced_SIMD_arch:
6131 // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6132 case elfcpp::Tag_ABI_FP_rounding:
6133 case elfcpp::Tag_ABI_FP_exceptions:
6134 case elfcpp::Tag_ABI_FP_user_exceptions:
6135 case elfcpp::Tag_ABI_FP_number_model:
6136 case elfcpp::Tag_VFP_HP_extension:
6137 case elfcpp::Tag_CPU_unaligned_access:
6138 case elfcpp::Tag_T2EE_use:
6139 case elfcpp::Tag_Virtualization_use:
6140 case elfcpp::Tag_MPextension_use:
6141 // Use the largest value specified.
6142 if (in_attr[i].int_value() > out_attr[i].int_value())
6143 out_attr[i].set_int_value(in_attr[i].int_value());
6144 break;
6145
6146 case elfcpp::Tag_ABI_align8_preserved:
6147 case elfcpp::Tag_ABI_PCS_RO_data:
6148 // Use the smallest value specified.
6149 if (in_attr[i].int_value() < out_attr[i].int_value())
6150 out_attr[i].set_int_value(in_attr[i].int_value());
6151 break;
6152
6153 case elfcpp::Tag_ABI_align8_needed:
6154 if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
6155 && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
6156 || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
6157 == 0)))
6158 {
6159 // This error message should be enabled once all non-conformant
6160 // binaries in the toolchain have had the attributes set
6161 // properly.
6162 // gold_error(_("output 8-byte data alignment conflicts with %s"),
6163 // name);
6164 }
6165 // Fall through.
6166 case elfcpp::Tag_ABI_FP_denormal:
6167 case elfcpp::Tag_ABI_PCS_GOT_use:
6168 {
6169 // These tags have 0 = don't care, 1 = strong requirement,
6170 // 2 = weak requirement.
6171 static const int order_021[3] = {0, 2, 1};
6172
6173 // Use the "greatest" from the sequence 0, 2, 1, or the largest
6174 // value if greater than 2 (for future-proofing).
6175 if ((in_attr[i].int_value() > 2
6176 && in_attr[i].int_value() > out_attr[i].int_value())
6177 || (in_attr[i].int_value() <= 2
6178 && out_attr[i].int_value() <= 2
6179 && (order_021[in_attr[i].int_value()]
6180 > order_021[out_attr[i].int_value()])))
6181 out_attr[i].set_int_value(in_attr[i].int_value());
6182 }
6183 break;
6184
6185 case elfcpp::Tag_CPU_arch_profile:
6186 if (out_attr[i].int_value() != in_attr[i].int_value())
6187 {
6188 // 0 will merge with anything.
6189 // 'A' and 'S' merge to 'A'.
6190 // 'R' and 'S' merge to 'R'.
6191 // 'M' and 'A|R|S' is an error.
6192 if (out_attr[i].int_value() == 0
6193 || (out_attr[i].int_value() == 'S'
6194 && (in_attr[i].int_value() == 'A'
6195 || in_attr[i].int_value() == 'R')))
6196 out_attr[i].set_int_value(in_attr[i].int_value());
6197 else if (in_attr[i].int_value() == 0
6198 || (in_attr[i].int_value() == 'S'
6199 && (out_attr[i].int_value() == 'A'
6200 || out_attr[i].int_value() == 'R')))
6201 ; // Do nothing.
6202 else
6203 {
6204 gold_error
6205 (_("conflicting architecture profiles %c/%c"),
6206 in_attr[i].int_value() ? in_attr[i].int_value() : '0',
6207 out_attr[i].int_value() ? out_attr[i].int_value() : '0');
6208 }
6209 }
6210 break;
6211 case elfcpp::Tag_VFP_arch:
6212 {
6213 static const struct
6214 {
6215 int ver;
6216 int regs;
6217 } vfp_versions[7] =
6218 {
6219 {0, 0},
6220 {1, 16},
6221 {2, 16},
6222 {3, 32},
6223 {3, 16},
6224 {4, 32},
6225 {4, 16}
6226 };
6227
6228 // Values greater than 6 aren't defined, so just pick the
6229 // biggest.
6230 if (in_attr[i].int_value() > 6
6231 && in_attr[i].int_value() > out_attr[i].int_value())
6232 {
6233 *out_attr = *in_attr;
6234 break;
6235 }
6236 // The output uses the superset of input features
6237 // (ISA version) and registers.
6238 int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
6239 vfp_versions[out_attr[i].int_value()].ver);
6240 int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
6241 vfp_versions[out_attr[i].int_value()].regs);
6242 // This assumes all possible supersets are also a valid
6243 // options.
6244 int newval;
6245 for (newval = 6; newval > 0; newval--)
6246 {
6247 if (regs == vfp_versions[newval].regs
6248 && ver == vfp_versions[newval].ver)
6249 break;
6250 }
6251 out_attr[i].set_int_value(newval);
6252 }
6253 break;
6254 case elfcpp::Tag_PCS_config:
6255 if (out_attr[i].int_value() == 0)
6256 out_attr[i].set_int_value(in_attr[i].int_value());
6257 else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6258 {
6259 // It's sometimes ok to mix different configs, so this is only
6260 // a warning.
6261 gold_warning(_("%s: conflicting platform configuration"), name);
6262 }
6263 break;
6264 case elfcpp::Tag_ABI_PCS_R9_use:
6265 if (in_attr[i].int_value() != out_attr[i].int_value()
6266 && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
6267 && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
6268 {
6269 gold_error(_("%s: conflicting use of R9"), name);
6270 }
6271 if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
6272 out_attr[i].set_int_value(in_attr[i].int_value());
6273 break;
6274 case elfcpp::Tag_ABI_PCS_RW_data:
6275 if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6276 && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6277 != elfcpp::AEABI_R9_SB)
6278 && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6279 != elfcpp::AEABI_R9_unused))
6280 {
6281 gold_error(_("%s: SB relative addressing conflicts with use "
6282 "of R9"),
6283 name);
6284 }
6285 // Use the smallest value specified.
6286 if (in_attr[i].int_value() < out_attr[i].int_value())
6287 out_attr[i].set_int_value(in_attr[i].int_value());
6288 break;
6289 case elfcpp::Tag_ABI_PCS_wchar_t:
6290 // FIXME: Make it possible to turn off this warning.
6291 if (out_attr[i].int_value()
6292 && in_attr[i].int_value()
6293 && out_attr[i].int_value() != in_attr[i].int_value())
6294 {
6295 gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6296 "use %u-byte wchar_t; use of wchar_t values "
6297 "across objects may fail"),
6298 name, in_attr[i].int_value(),
6299 out_attr[i].int_value());
6300 }
6301 else if (in_attr[i].int_value() && !out_attr[i].int_value())
6302 out_attr[i].set_int_value(in_attr[i].int_value());
6303 break;
6304 case elfcpp::Tag_ABI_enum_size:
6305 if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
6306 {
6307 if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
6308 || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
6309 {
6310 // The existing object is compatible with anything.
6311 // Use whatever requirements the new object has.
6312 out_attr[i].set_int_value(in_attr[i].int_value());
6313 }
6314 // FIXME: Make it possible to turn off this warning.
6315 else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
6316 && out_attr[i].int_value() != in_attr[i].int_value())
6317 {
6318 unsigned int in_value = in_attr[i].int_value();
6319 unsigned int out_value = out_attr[i].int_value();
6320 gold_warning(_("%s uses %s enums yet the output is to use "
6321 "%s enums; use of enum values across objects "
6322 "may fail"),
6323 name,
6324 this->aeabi_enum_name(in_value).c_str(),
6325 this->aeabi_enum_name(out_value).c_str());
6326 }
6327 }
6328 break;
6329 case elfcpp::Tag_ABI_VFP_args:
6330 // Aready done.
6331 break;
6332 case elfcpp::Tag_ABI_WMMX_args:
6333 if (in_attr[i].int_value() != out_attr[i].int_value())
6334 {
6335 gold_error(_("%s uses iWMMXt register arguments, output does "
6336 "not"),
6337 name);
6338 }
6339 break;
6340 case Object_attribute::Tag_compatibility:
6341 // Merged in target-independent code.
6342 break;
6343 case elfcpp::Tag_ABI_HardFP_use:
6344 // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6345 if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
6346 || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
6347 out_attr[i].set_int_value(3);
6348 else if (in_attr[i].int_value() > out_attr[i].int_value())
6349 out_attr[i].set_int_value(in_attr[i].int_value());
6350 break;
6351 case elfcpp::Tag_ABI_FP_16bit_format:
6352 if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6353 {
6354 if (in_attr[i].int_value() != out_attr[i].int_value())
6355 gold_error(_("fp16 format mismatch between %s and output"),
6356 name);
6357 }
6358 if (in_attr[i].int_value() != 0)
6359 out_attr[i].set_int_value(in_attr[i].int_value());
6360 break;
6361
6362 case elfcpp::Tag_nodefaults:
6363 // This tag is set if it exists, but the value is unused (and is
6364 // typically zero). We don't actually need to do anything here -
6365 // the merge happens automatically when the type flags are merged
6366 // below.
6367 break;
6368 case elfcpp::Tag_also_compatible_with:
6369 // Already done in Tag_CPU_arch.
6370 break;
6371 case elfcpp::Tag_conformance:
6372 // Keep the attribute if it matches. Throw it away otherwise.
6373 // No attribute means no claim to conform.
6374 if (in_attr[i].string_value() != out_attr[i].string_value())
6375 out_attr[i].set_string_value("");
6376 break;
6377
6378 default:
6379 {
6380 const char* err_object = NULL;
6381
6382 // The "known_obj_attributes" table does contain some undefined
6383 // attributes. Ensure that there are unused.
6384 if (out_attr[i].int_value() != 0
6385 || out_attr[i].string_value() != "")
6386 err_object = "output";
6387 else if (in_attr[i].int_value() != 0
6388 || in_attr[i].string_value() != "")
6389 err_object = name;
6390
6391 if (err_object != NULL)
6392 {
6393 // Attribute numbers >=64 (mod 128) can be safely ignored.
6394 if ((i & 127) < 64)
6395 gold_error(_("%s: unknown mandatory EABI object attribute "
6396 "%d"),
6397 err_object, i);
6398 else
6399 gold_warning(_("%s: unknown EABI object attribute %d"),
6400 err_object, i);
6401 }
6402
6403 // Only pass on attributes that match in both inputs.
6404 if (!in_attr[i].matches(out_attr[i]))
6405 {
6406 out_attr[i].set_int_value(0);
6407 out_attr[i].set_string_value("");
6408 }
6409 }
6410 }
6411
6412 // If out_attr was copied from in_attr then it won't have a type yet.
6413 if (in_attr[i].type() && !out_attr[i].type())
6414 out_attr[i].set_type(in_attr[i].type());
6415 }
6416
6417 // Merge Tag_compatibility attributes and any common GNU ones.
6418 this->attributes_section_data_->merge(name, pasd);
6419
6420 // Check for any attributes not known on ARM.
6421 typedef Vendor_object_attributes::Other_attributes Other_attributes;
6422 const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
6423 Other_attributes::const_iterator in_iter = in_other_attributes->begin();
6424 Other_attributes* out_other_attributes =
6425 this->attributes_section_data_->other_attributes(vendor);
6426 Other_attributes::iterator out_iter = out_other_attributes->begin();
6427
6428 while (in_iter != in_other_attributes->end()
6429 || out_iter != out_other_attributes->end())
6430 {
6431 const char* err_object = NULL;
6432 int err_tag = 0;
6433
6434 // The tags for each list are in numerical order.
6435 // If the tags are equal, then merge.
6436 if (out_iter != out_other_attributes->end()
6437 && (in_iter == in_other_attributes->end()
6438 || in_iter->first > out_iter->first))
6439 {
6440 // This attribute only exists in output. We can't merge, and we
6441 // don't know what the tag means, so delete it.
6442 err_object = "output";
6443 err_tag = out_iter->first;
6444 int saved_tag = out_iter->first;
6445 delete out_iter->second;
6446 out_other_attributes->erase(out_iter);
6447 out_iter = out_other_attributes->upper_bound(saved_tag);
6448 }
6449 else if (in_iter != in_other_attributes->end()
6450 && (out_iter != out_other_attributes->end()
6451 || in_iter->first < out_iter->first))
6452 {
6453 // This attribute only exists in input. We can't merge, and we
6454 // don't know what the tag means, so ignore it.
6455 err_object = name;
6456 err_tag = in_iter->first;
6457 ++in_iter;
6458 }
6459 else // The tags are equal.
6460 {
6461 // As present, all attributes in the list are unknown, and
6462 // therefore can't be merged meaningfully.
6463 err_object = "output";
6464 err_tag = out_iter->first;
6465
6466 // Only pass on attributes that match in both inputs.
6467 if (!in_iter->second->matches(*(out_iter->second)))
6468 {
6469 // No match. Delete the attribute.
6470 int saved_tag = out_iter->first;
6471 delete out_iter->second;
6472 out_other_attributes->erase(out_iter);
6473 out_iter = out_other_attributes->upper_bound(saved_tag);
6474 }
6475 else
6476 {
6477 // Matched. Keep the attribute and move to the next.
6478 ++out_iter;
6479 ++in_iter;
6480 }
6481 }
6482
6483 if (err_object)
6484 {
6485 // Attribute numbers >=64 (mod 128) can be safely ignored. */
6486 if ((err_tag & 127) < 64)
6487 {
6488 gold_error(_("%s: unknown mandatory EABI object attribute %d"),
6489 err_object, err_tag);
6490 }
6491 else
6492 {
6493 gold_warning(_("%s: unknown EABI object attribute %d"),
6494 err_object, err_tag);
6495 }
6496 }
6497 }
6498}
6499
55da9579
DK
6500// Return whether a relocation type used the LSB to distinguish THUMB
6501// addresses.
6502template<bool big_endian>
6503bool
6504Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
6505{
6506 switch (r_type)
6507 {
6508 case elfcpp::R_ARM_PC24:
6509 case elfcpp::R_ARM_ABS32:
6510 case elfcpp::R_ARM_REL32:
6511 case elfcpp::R_ARM_SBREL32:
6512 case elfcpp::R_ARM_THM_CALL:
6513 case elfcpp::R_ARM_GLOB_DAT:
6514 case elfcpp::R_ARM_JUMP_SLOT:
6515 case elfcpp::R_ARM_GOTOFF32:
6516 case elfcpp::R_ARM_PLT32:
6517 case elfcpp::R_ARM_CALL:
6518 case elfcpp::R_ARM_JUMP24:
6519 case elfcpp::R_ARM_THM_JUMP24:
6520 case elfcpp::R_ARM_SBREL31:
6521 case elfcpp::R_ARM_PREL31:
6522 case elfcpp::R_ARM_MOVW_ABS_NC:
6523 case elfcpp::R_ARM_MOVW_PREL_NC:
6524 case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6525 case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6526 case elfcpp::R_ARM_THM_JUMP19:
6527 case elfcpp::R_ARM_THM_ALU_PREL_11_0:
6528 case elfcpp::R_ARM_ALU_PC_G0_NC:
6529 case elfcpp::R_ARM_ALU_PC_G0:
6530 case elfcpp::R_ARM_ALU_PC_G1_NC:
6531 case elfcpp::R_ARM_ALU_PC_G1:
6532 case elfcpp::R_ARM_ALU_PC_G2:
6533 case elfcpp::R_ARM_ALU_SB_G0_NC:
6534 case elfcpp::R_ARM_ALU_SB_G0:
6535 case elfcpp::R_ARM_ALU_SB_G1_NC:
6536 case elfcpp::R_ARM_ALU_SB_G1:
6537 case elfcpp::R_ARM_ALU_SB_G2:
6538 case elfcpp::R_ARM_MOVW_BREL_NC:
6539 case elfcpp::R_ARM_MOVW_BREL:
6540 case elfcpp::R_ARM_THM_MOVW_BREL_NC:
6541 case elfcpp::R_ARM_THM_MOVW_BREL:
6542 return true;
6543 default:
6544 return false;
6545 }
6546}
6547
6548// Stub-generation methods for Target_arm.
6549
6550// Make a new Arm_input_section object.
6551
6552template<bool big_endian>
6553Arm_input_section<big_endian>*
6554Target_arm<big_endian>::new_arm_input_section(
2ea97941
ILT
6555 Relobj* relobj,
6556 unsigned int shndx)
55da9579 6557{
2ea97941 6558 Input_section_specifier iss(relobj, shndx);
55da9579
DK
6559
6560 Arm_input_section<big_endian>* arm_input_section =
2ea97941 6561 new Arm_input_section<big_endian>(relobj, shndx);
55da9579
DK
6562 arm_input_section->init();
6563
6564 // Register new Arm_input_section in map for look-up.
6565 std::pair<typename Arm_input_section_map::iterator, bool> ins =
6566 this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
6567
6568 // Make sure that it we have not created another Arm_input_section
6569 // for this input section already.
6570 gold_assert(ins.second);
6571
6572 return arm_input_section;
6573}
6574
6575// Find the Arm_input_section object corresponding to the SHNDX-th input
6576// section of RELOBJ.
6577
6578template<bool big_endian>
6579Arm_input_section<big_endian>*
6580Target_arm<big_endian>::find_arm_input_section(
2ea97941
ILT
6581 Relobj* relobj,
6582 unsigned int shndx) const
55da9579 6583{
2ea97941 6584 Input_section_specifier iss(relobj, shndx);
55da9579
DK
6585 typename Arm_input_section_map::const_iterator p =
6586 this->arm_input_section_map_.find(iss);
6587 return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
6588}
6589
6590// Make a new stub table.
6591
6592template<bool big_endian>
6593Stub_table<big_endian>*
6594Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
6595{
2ea97941 6596 Stub_table<big_endian>* stub_table =
55da9579 6597 new Stub_table<big_endian>(owner);
2ea97941 6598 this->stub_tables_.push_back(stub_table);
55da9579 6599
2ea97941
ILT
6600 stub_table->set_address(owner->address() + owner->data_size());
6601 stub_table->set_file_offset(owner->offset() + owner->data_size());
6602 stub_table->finalize_data_size();
55da9579 6603
2ea97941 6604 return stub_table;
55da9579
DK
6605}
6606
eb44217c
DK
6607// Scan a relocation for stub generation.
6608
6609template<bool big_endian>
6610void
6611Target_arm<big_endian>::scan_reloc_for_stub(
6612 const Relocate_info<32, big_endian>* relinfo,
6613 unsigned int r_type,
6614 const Sized_symbol<32>* gsym,
6615 unsigned int r_sym,
6616 const Symbol_value<32>* psymval,
6617 elfcpp::Elf_types<32>::Elf_Swxword addend,
6618 Arm_address address)
6619{
2ea97941 6620 typedef typename Target_arm<big_endian>::Relocate Relocate;
eb44217c
DK
6621
6622 const Arm_relobj<big_endian>* arm_relobj =
6623 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
6624
6625 bool target_is_thumb;
6626 Symbol_value<32> symval;
6627 if (gsym != NULL)
6628 {
6629 // This is a global symbol. Determine if we use PLT and if the
6630 // final target is THUMB.
2ea97941 6631 if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
eb44217c
DK
6632 {
6633 // This uses a PLT, change the symbol value.
6634 symval.set_output_value(this->plt_section()->address()
6635 + gsym->plt_offset());
6636 psymval = &symval;
6637 target_is_thumb = false;
6638 }
6639 else if (gsym->is_undefined())
6640 // There is no need to generate a stub symbol is undefined.
6641 return;
6642 else
6643 {
6644 target_is_thumb =
6645 ((gsym->type() == elfcpp::STT_ARM_TFUNC)
6646 || (gsym->type() == elfcpp::STT_FUNC
6647 && !gsym->is_undefined()
6648 && ((psymval->value(arm_relobj, 0) & 1) != 0)));
6649 }
6650 }
6651 else
6652 {
6653 // This is a local symbol. Determine if the final target is THUMB.
6654 target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
6655 }
6656
6657 // Strip LSB if this points to a THUMB target.
6658 if (target_is_thumb
6659 && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
6660 && ((psymval->value(arm_relobj, 0) & 1) != 0))
6661 {
6662 Arm_address stripped_value =
6663 psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
6664 symval.set_output_value(stripped_value);
6665 psymval = &symval;
6666 }
6667
6668 // Get the symbol value.
6669 Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
6670
6671 // Owing to pipelining, the PC relative branches below actually skip
6672 // two instructions when the branch offset is 0.
6673 Arm_address destination;
6674 switch (r_type)
6675 {
6676 case elfcpp::R_ARM_CALL:
6677 case elfcpp::R_ARM_JUMP24:
6678 case elfcpp::R_ARM_PLT32:
6679 // ARM branches.
6680 destination = value + addend + 8;
6681 break;
6682 case elfcpp::R_ARM_THM_CALL:
6683 case elfcpp::R_ARM_THM_XPC22:
6684 case elfcpp::R_ARM_THM_JUMP24:
6685 case elfcpp::R_ARM_THM_JUMP19:
6686 // THUMB branches.
6687 destination = value + addend + 4;
6688 break;
6689 default:
6690 gold_unreachable();
6691 }
6692
6693 Stub_type stub_type =
6694 Reloc_stub::stub_type_for_reloc(r_type, address, destination,
6695 target_is_thumb);
6696
6697 // This reloc does not need a stub.
6698 if (stub_type == arm_stub_none)
6699 return;
6700
6701 // Try looking up an existing stub from a stub table.
2ea97941 6702 Stub_table<big_endian>* stub_table =
eb44217c 6703 arm_relobj->stub_table(relinfo->data_shndx);
2ea97941 6704 gold_assert(stub_table != NULL);
eb44217c
DK
6705
6706 // Locate stub by destination.
6707 Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
6708
6709 // Create a stub if there is not one already
2ea97941 6710 Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
eb44217c
DK
6711 if (stub == NULL)
6712 {
6713 // create a new stub and add it to stub table.
6714 stub = this->stub_factory().make_reloc_stub(stub_type);
2ea97941 6715 stub_table->add_reloc_stub(stub, stub_key);
eb44217c
DK
6716 }
6717
6718 // Record the destination address.
6719 stub->set_destination_address(destination
6720 | (target_is_thumb ? 1 : 0));
6721}
6722
6723// This function scans a relocation sections for stub generation.
6724// The template parameter Relocate must be a class type which provides
6725// a single function, relocate(), which implements the machine
6726// specific part of a relocation.
6727
6728// BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
6729// SHT_REL or SHT_RELA.
6730
6731// PRELOCS points to the relocation data. RELOC_COUNT is the number
6732// of relocs. OUTPUT_SECTION is the output section.
6733// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
6734// mapped to output offsets.
6735
6736// VIEW is the section data, VIEW_ADDRESS is its memory address, and
6737// VIEW_SIZE is the size. These refer to the input section, unless
6738// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
6739// the output section.
6740
6741template<bool big_endian>
6742template<int sh_type>
6743void inline
6744Target_arm<big_endian>::scan_reloc_section_for_stubs(
6745 const Relocate_info<32, big_endian>* relinfo,
6746 const unsigned char* prelocs,
6747 size_t reloc_count,
6748 Output_section* output_section,
6749 bool needs_special_offset_handling,
6750 const unsigned char* view,
6751 elfcpp::Elf_types<32>::Elf_Addr view_address,
6752 section_size_type)
6753{
6754 typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
6755 const int reloc_size =
6756 Reloc_types<sh_type, 32, big_endian>::reloc_size;
6757
6758 Arm_relobj<big_endian>* arm_object =
6759 Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
6760 unsigned int local_count = arm_object->local_symbol_count();
6761
6762 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
6763
6764 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6765 {
6766 Reltype reloc(prelocs);
6767
6768 typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
6769 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6770 unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6771
6772 r_type = this->get_real_reloc_type(r_type);
6773
6774 // Only a few relocation types need stubs.
6775 if ((r_type != elfcpp::R_ARM_CALL)
6776 && (r_type != elfcpp::R_ARM_JUMP24)
6777 && (r_type != elfcpp::R_ARM_PLT32)
6778 && (r_type != elfcpp::R_ARM_THM_CALL)
6779 && (r_type != elfcpp::R_ARM_THM_XPC22)
6780 && (r_type != elfcpp::R_ARM_THM_JUMP24)
6781 && (r_type != elfcpp::R_ARM_THM_JUMP19))
6782 continue;
6783
2ea97941 6784 section_offset_type offset =
eb44217c
DK
6785 convert_to_section_size_type(reloc.get_r_offset());
6786
6787 if (needs_special_offset_handling)
6788 {
2ea97941
ILT
6789 offset = output_section->output_offset(relinfo->object,
6790 relinfo->data_shndx,
6791 offset);
6792 if (offset == -1)
eb44217c
DK
6793 continue;
6794 }
6795
6796 // Get the addend.
6797 Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
6798 elfcpp::Elf_types<32>::Elf_Swxword addend =
2ea97941 6799 stub_addend_reader(r_type, view + offset, reloc);
eb44217c
DK
6800
6801 const Sized_symbol<32>* sym;
6802
6803 Symbol_value<32> symval;
6804 const Symbol_value<32> *psymval;
6805 if (r_sym < local_count)
6806 {
6807 sym = NULL;
6808 psymval = arm_object->local_symbol(r_sym);
6809
6810 // If the local symbol belongs to a section we are discarding,
6811 // and that section is a debug section, try to find the
6812 // corresponding kept section and map this symbol to its
6813 // counterpart in the kept section. The symbol must not
6814 // correspond to a section we are folding.
6815 bool is_ordinary;
2ea97941 6816 unsigned int shndx = psymval->input_shndx(&is_ordinary);
eb44217c 6817 if (is_ordinary
2ea97941
ILT
6818 && shndx != elfcpp::SHN_UNDEF
6819 && !arm_object->is_section_included(shndx)
6820 && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
eb44217c
DK
6821 {
6822 if (comdat_behavior == CB_UNDETERMINED)
6823 {
6824 std::string name =
6825 arm_object->section_name(relinfo->data_shndx);
6826 comdat_behavior = get_comdat_behavior(name.c_str());
6827 }
6828 if (comdat_behavior == CB_PRETEND)
6829 {
6830 bool found;
6831 typename elfcpp::Elf_types<32>::Elf_Addr value =
2ea97941 6832 arm_object->map_to_kept_section(shndx, &found);
eb44217c
DK
6833 if (found)
6834 symval.set_output_value(value + psymval->input_value());
6835 else
6836 symval.set_output_value(0);
6837 }
6838 else
6839 {
6840 symval.set_output_value(0);
6841 }
6842 symval.set_no_output_symtab_entry();
6843 psymval = &symval;
6844 }
6845 }
6846 else
6847 {
6848 const Symbol* gsym = arm_object->global_symbol(r_sym);
6849 gold_assert(gsym != NULL);
6850 if (gsym->is_forwarder())
6851 gsym = relinfo->symtab->resolve_forwards(gsym);
6852
6853 sym = static_cast<const Sized_symbol<32>*>(gsym);
6854 if (sym->has_symtab_index())
6855 symval.set_output_symtab_index(sym->symtab_index());
6856 else
6857 symval.set_no_output_symtab_entry();
6858
6859 // We need to compute the would-be final value of this global
6860 // symbol.
6861 const Symbol_table* symtab = relinfo->symtab;
6862 const Sized_symbol<32>* sized_symbol =
6863 symtab->get_sized_symbol<32>(gsym);
6864 Symbol_table::Compute_final_value_status status;
6865 Arm_address value =
6866 symtab->compute_final_value<32>(sized_symbol, &status);
6867
6868 // Skip this if the symbol has not output section.
6869 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
6870 continue;
6871
6872 symval.set_output_value(value);
6873 psymval = &symval;
6874 }
6875
6876 // If symbol is a section symbol, we don't know the actual type of
6877 // destination. Give up.
6878 if (psymval->is_section_symbol())
6879 continue;
6880
6881 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
2ea97941 6882 addend, view_address + offset);
eb44217c
DK
6883 }
6884}
6885
6886// Scan an input section for stub generation.
6887
6888template<bool big_endian>
6889void
6890Target_arm<big_endian>::scan_section_for_stubs(
6891 const Relocate_info<32, big_endian>* relinfo,
6892 unsigned int sh_type,
6893 const unsigned char* prelocs,
6894 size_t reloc_count,
6895 Output_section* output_section,
6896 bool needs_special_offset_handling,
6897 const unsigned char* view,
6898 Arm_address view_address,
6899 section_size_type view_size)
6900{
6901 if (sh_type == elfcpp::SHT_REL)
6902 this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
6903 relinfo,
6904 prelocs,
6905 reloc_count,
6906 output_section,
6907 needs_special_offset_handling,
6908 view,
6909 view_address,
6910 view_size);
6911 else if (sh_type == elfcpp::SHT_RELA)
6912 // We do not support RELA type relocations yet. This is provided for
6913 // completeness.
6914 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
6915 relinfo,
6916 prelocs,
6917 reloc_count,
6918 output_section,
6919 needs_special_offset_handling,
6920 view,
6921 view_address,
6922 view_size);
6923 else
6924 gold_unreachable();
6925}
6926
6927// Group input sections for stub generation.
6928//
6929// We goup input sections in an output sections so that the total size,
6930// including any padding space due to alignment is smaller than GROUP_SIZE
6931// unless the only input section in group is bigger than GROUP_SIZE already.
6932// Then an ARM stub table is created to follow the last input section
6933// in group. For each group an ARM stub table is created an is placed
6934// after the last group. If STUB_ALWATS_AFTER_BRANCH is false, we further
6935// extend the group after the stub table.
6936
6937template<bool big_endian>
6938void
6939Target_arm<big_endian>::group_sections(
2ea97941 6940 Layout* layout,
eb44217c
DK
6941 section_size_type group_size,
6942 bool stubs_always_after_branch)
6943{
6944 // Group input sections and insert stub table
6945 Layout::Section_list section_list;
2ea97941 6946 layout->get_allocated_sections(&section_list);
eb44217c
DK
6947 for (Layout::Section_list::const_iterator p = section_list.begin();
6948 p != section_list.end();
6949 ++p)
6950 {
6951 Arm_output_section<big_endian>* output_section =
6952 Arm_output_section<big_endian>::as_arm_output_section(*p);
6953 output_section->group_sections(group_size, stubs_always_after_branch,
6954 this);
6955 }
6956}
6957
6958// Relaxation hook. This is where we do stub generation.
6959
6960template<bool big_endian>
6961bool
6962Target_arm<big_endian>::do_relax(
6963 int pass,
6964 const Input_objects* input_objects,
6965 Symbol_table* symtab,
2ea97941 6966 Layout* layout)
eb44217c
DK
6967{
6968 // No need to generate stubs if this is a relocatable link.
6969 gold_assert(!parameters->options().relocatable());
6970
6971 // If this is the first pass, we need to group input sections into
6972 // stub groups.
6973 if (pass == 1)
6974 {
6975 // Determine the stub group size. The group size is the absolute
6976 // value of the parameter --stub-group-size. If --stub-group-size
6977 // is passed a negative value, we restict stubs to be always after
6978 // the stubbed branches.
6979 int32_t stub_group_size_param =
6980 parameters->options().stub_group_size();
6981 bool stubs_always_after_branch = stub_group_size_param < 0;
6982 section_size_type stub_group_size = abs(stub_group_size_param);
6983
6984 if (stub_group_size == 1)
6985 {
6986 // Default value.
6987 // Thumb branch range is +-4MB has to be used as the default
6988 // maximum size (a given section can contain both ARM and Thumb
6989 // code, so the worst case has to be taken into account).
6990 //
6991 // This value is 24K less than that, which allows for 2025
6992 // 12-byte stubs. If we exceed that, then we will fail to link.
6993 // The user will have to relink with an explicit group size
6994 // option.
6995 stub_group_size = 4170000;
6996 }
6997
2ea97941 6998 group_sections(layout, stub_group_size, stubs_always_after_branch);
eb44217c
DK
6999 }
7000
7001 // clear changed flags for all stub_tables
7002 typedef typename Stub_table_list::iterator Stub_table_iterator;
7003 for (Stub_table_iterator sp = this->stub_tables_.begin();
7004 sp != this->stub_tables_.end();
7005 ++sp)
7006 (*sp)->set_has_been_changed(false);
7007
7008 // scan relocs for stubs
7009 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
7010 op != input_objects->relobj_end();
7011 ++op)
7012 {
7013 Arm_relobj<big_endian>* arm_relobj =
7014 Arm_relobj<big_endian>::as_arm_relobj(*op);
2ea97941 7015 arm_relobj->scan_sections_for_stubs(this, symtab, layout);
eb44217c
DK
7016 }
7017
7018 bool any_stub_table_changed = false;
7019 for (Stub_table_iterator sp = this->stub_tables_.begin();
7020 (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7021 ++sp)
7022 {
7023 if ((*sp)->has_been_changed())
7024 any_stub_table_changed = true;
7025 }
7026
7027 return any_stub_table_changed;
7028}
7029
43d12afe
DK
7030// Relocate a stub.
7031
7032template<bool big_endian>
7033void
7034Target_arm<big_endian>::relocate_stub(
7035 Reloc_stub* stub,
7036 const Relocate_info<32, big_endian>* relinfo,
7037 Output_section* output_section,
7038 unsigned char* view,
7039 Arm_address address,
7040 section_size_type view_size)
7041{
7042 Relocate relocate;
2ea97941
ILT
7043 const Stub_template* stub_template = stub->stub_template();
7044 for (size_t i = 0; i < stub_template->reloc_count(); i++)
43d12afe 7045 {
2ea97941
ILT
7046 size_t reloc_insn_index = stub_template->reloc_insn_index(i);
7047 const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
43d12afe
DK
7048
7049 unsigned int r_type = insn->r_type();
2ea97941 7050 section_size_type reloc_offset = stub_template->reloc_offset(i);
43d12afe
DK
7051 section_size_type reloc_size = insn->size();
7052 gold_assert(reloc_offset + reloc_size <= view_size);
7053
7054 // This is the address of the stub destination.
7055 Arm_address target = stub->reloc_target(i);
7056 Symbol_value<32> symval;
7057 symval.set_output_value(target);
7058
7059 // Synthesize a fake reloc just in case. We don't have a symbol so
7060 // we use 0.
7061 unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
7062 memset(reloc_buffer, 0, sizeof(reloc_buffer));
7063 elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
7064 reloc_write.put_r_offset(reloc_offset);
7065 reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
7066 elfcpp::Rel<32, big_endian> rel(reloc_buffer);
7067
7068 relocate.relocate(relinfo, this, output_section,
7069 this->fake_relnum_for_stubs, rel, r_type,
7070 NULL, &symval, view + reloc_offset,
7071 address + reloc_offset, reloc_size);
7072 }
7073}
7074
a0351a69
DK
7075// Determine whether an object attribute tag takes an integer, a
7076// string or both.
7077
7078template<bool big_endian>
7079int
7080Target_arm<big_endian>::do_attribute_arg_type(int tag) const
7081{
7082 if (tag == Object_attribute::Tag_compatibility)
7083 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7084 | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
7085 else if (tag == elfcpp::Tag_nodefaults)
7086 return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7087 | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
7088 else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
7089 return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
7090 else if (tag < 32)
7091 return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
7092 else
7093 return ((tag & 1) != 0
7094 ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7095 : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
7096}
7097
7098// Reorder attributes.
7099//
7100// The ABI defines that Tag_conformance should be emitted first, and that
7101// Tag_nodefaults should be second (if either is defined). This sets those
7102// two positions, and bumps up the position of all the remaining tags to
7103// compensate.
7104
7105template<bool big_endian>
7106int
7107Target_arm<big_endian>::do_attributes_order(int num) const
7108{
7109 // Reorder the known object attributes in output. We want to move
7110 // Tag_conformance to position 4 and Tag_conformance to position 5
7111 // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7112 if (num == 4)
7113 return elfcpp::Tag_conformance;
7114 if (num == 5)
7115 return elfcpp::Tag_nodefaults;
7116 if ((num - 2) < elfcpp::Tag_nodefaults)
7117 return num - 2;
7118 if ((num - 1) < elfcpp::Tag_conformance)
7119 return num - 1;
7120 return num;
7121}
4a657b0d
DK
7122
7123template<bool big_endian>
7124class Target_selector_arm : public Target_selector
7125{
7126 public:
7127 Target_selector_arm()
7128 : Target_selector(elfcpp::EM_ARM, 32, big_endian,
7129 (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
7130 { }
7131
7132 Target*
7133 do_instantiate_target()
7134 { return new Target_arm<big_endian>(); }
7135};
7136
7137Target_selector_arm<false> target_selector_arm;
7138Target_selector_arm<true> target_selector_armbe;
7139
7140} // End anonymous namespace.
This page took 0.548927 seconds and 4 git commands to generate.