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