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