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