Fix {mi-tracepoint-changed, mi-tsv-changed}.exp with native-extended-gdbserver
[deliverable/binutils-gdb.git] / gold / aarch64.cc
CommitLineData
053a4d68
JY
1// aarch64.cc -- aarch64 target support for gold.
2
b90efa5b 3// Copyright (C) 2014-2015 Free Software Foundation, Inc.
9363c7c3 4// Written by Jing Yu <jingyu@google.com> and Han Shen <shenhan@google.com>.
053a4d68
JY
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
23#include "gold.h"
24
25#include <cstring>
26
27#include "elfcpp.h"
28#include "dwarf.h"
29#include "parameters.h"
30#include "reloc.h"
31#include "aarch64.h"
32#include "object.h"
33#include "symtab.h"
34#include "layout.h"
35#include "output.h"
36#include "copy-relocs.h"
37#include "target.h"
38#include "target-reloc.h"
39#include "target-select.h"
40#include "tls.h"
41#include "freebsd.h"
42#include "nacl.h"
43#include "gc.h"
44#include "icf.h"
9363c7c3 45#include "aarch64-reloc-property.h"
053a4d68
JY
46
47// The first three .got.plt entries are reserved.
48const int32_t AARCH64_GOTPLT_RESERVE_COUNT = 3;
49
83a01957 50
053a4d68
JY
51namespace
52{
53
54using namespace gold;
55
56template<int size, bool big_endian>
57class Output_data_plt_aarch64;
58
59template<int size, bool big_endian>
60class Output_data_plt_aarch64_standard;
61
62template<int size, bool big_endian>
63class Target_aarch64;
64
9363c7c3
JY
65template<int size, bool big_endian>
66class AArch64_relocate_functions;
67
053a4d68
JY
68// Output_data_got_aarch64 class.
69
70template<int size, bool big_endian>
71class Output_data_got_aarch64 : public Output_data_got<size, big_endian>
72{
73 public:
74 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
75 Output_data_got_aarch64(Symbol_table* symtab, Layout* layout)
9363c7c3
JY
76 : Output_data_got<size, big_endian>(),
77 symbol_table_(symtab), layout_(layout)
053a4d68
JY
78 { }
79
8e33481e
HS
80 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
81 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
82 // applied in a static link.
83 void
84 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
85 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
86
87
88 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
89 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
90 // relocation that needs to be applied in a static link.
91 void
92 add_static_reloc(unsigned int got_offset, unsigned int r_type,
93 Sized_relobj_file<size, big_endian>* relobj,
94 unsigned int index)
95 {
96 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
97 index));
98 }
99
100
053a4d68
JY
101 protected:
102 // Write out the GOT table.
103 void
104 do_write(Output_file* of) {
105 // The first entry in the GOT is the address of the .dynamic section.
106 gold_assert(this->data_size() >= size / 8);
107 Output_section* dynamic = this->layout_->dynamic_section();
108 Valtype dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
109 this->replace_constant(0, dynamic_addr);
110 Output_data_got<size, big_endian>::do_write(of);
8e33481e
HS
111
112 // Handling static relocs
113 if (this->static_relocs_.empty())
114 return;
115
116 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
117
118 gold_assert(parameters->doing_static_link());
119 const off_t offset = this->offset();
120 const section_size_type oview_size =
121 convert_to_section_size_type(this->data_size());
122 unsigned char* const oview = of->get_output_view(offset, oview_size);
123
124 Output_segment* tls_segment = this->layout_->tls_segment();
125 gold_assert(tls_segment != NULL);
126
127 AArch64_address aligned_tcb_address =
83a01957 128 align_address(Target_aarch64<size, big_endian>::TCB_SIZE,
8e33481e
HS
129 tls_segment->maximum_alignment());
130
131 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
132 {
133 Static_reloc& reloc(this->static_relocs_[i]);
134 AArch64_address value;
135
136 if (!reloc.symbol_is_global())
137 {
138 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
139 const Symbol_value<size>* psymval =
140 reloc.relobj()->local_symbol(reloc.index());
141
142 // We are doing static linking. Issue an error and skip this
143 // relocation if the symbol is undefined or in a discarded_section.
144 bool is_ordinary;
145 unsigned int shndx = psymval->input_shndx(&is_ordinary);
146 if ((shndx == elfcpp::SHN_UNDEF)
147 || (is_ordinary
148 && shndx != elfcpp::SHN_UNDEF
149 && !object->is_section_included(shndx)
150 && !this->symbol_table_->is_section_folded(object, shndx)))
151 {
152 gold_error(_("undefined or discarded local symbol %u from "
153 " object %s in GOT"),
154 reloc.index(), reloc.relobj()->name().c_str());
155 continue;
156 }
157 value = psymval->value(object, 0);
158 }
159 else
160 {
161 const Symbol* gsym = reloc.symbol();
162 gold_assert(gsym != NULL);
163 if (gsym->is_forwarder())
164 gsym = this->symbol_table_->resolve_forwards(gsym);
165
166 // We are doing static linking. Issue an error and skip this
167 // relocation if the symbol is undefined or in a discarded_section
168 // unless it is a weakly_undefined symbol.
169 if ((gsym->is_defined_in_discarded_section()
170 || gsym->is_undefined())
171 && !gsym->is_weak_undefined())
172 {
173 gold_error(_("undefined or discarded symbol %s in GOT"),
174 gsym->name());
175 continue;
176 }
177
178 if (!gsym->is_weak_undefined())
179 {
180 const Sized_symbol<size>* sym =
181 static_cast<const Sized_symbol<size>*>(gsym);
182 value = sym->value();
183 }
184 else
185 value = 0;
186 }
187
188 unsigned got_offset = reloc.got_offset();
189 gold_assert(got_offset < oview_size);
190
191 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
192 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
193 Valtype x;
194 switch (reloc.r_type())
195 {
196 case elfcpp::R_AARCH64_TLS_DTPREL64:
197 x = value;
198 break;
199 case elfcpp::R_AARCH64_TLS_TPREL64:
200 x = value + aligned_tcb_address;
201 break;
202 default:
203 gold_unreachable();
204 }
205 elfcpp::Swap<size, big_endian>::writeval(wv, x);
206 }
207
208 of->write_output_view(offset, oview_size, oview);
053a4d68
JY
209 }
210
211 private:
9363c7c3
JY
212 // Symbol table of the output object.
213 Symbol_table* symbol_table_;
053a4d68
JY
214 // A pointer to the Layout class, so that we can find the .dynamic
215 // section when we write out the GOT section.
216 Layout* layout_;
8e33481e 217
8e33481e
HS
218 // This class represent dynamic relocations that need to be applied by
219 // gold because we are using TLS relocations in a static link.
220 class Static_reloc
221 {
222 public:
223 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
224 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
225 { this->u_.global.symbol = gsym; }
226
227 Static_reloc(unsigned int got_offset, unsigned int r_type,
228 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
229 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
230 {
231 this->u_.local.relobj = relobj;
232 this->u_.local.index = index;
233 }
234
235 // Return the GOT offset.
236 unsigned int
237 got_offset() const
238 { return this->got_offset_; }
239
240 // Relocation type.
241 unsigned int
242 r_type() const
243 { return this->r_type_; }
244
245 // Whether the symbol is global or not.
246 bool
247 symbol_is_global() const
248 { return this->symbol_is_global_; }
249
250 // For a relocation against a global symbol, the global symbol.
251 Symbol*
252 symbol() const
253 {
254 gold_assert(this->symbol_is_global_);
255 return this->u_.global.symbol;
256 }
257
258 // For a relocation against a local symbol, the defining object.
259 Sized_relobj_file<size, big_endian>*
260 relobj() const
261 {
262 gold_assert(!this->symbol_is_global_);
263 return this->u_.local.relobj;
264 }
265
266 // For a relocation against a local symbol, the local symbol index.
267 unsigned int
268 index() const
269 {
270 gold_assert(!this->symbol_is_global_);
271 return this->u_.local.index;
272 }
273
274 private:
275 // GOT offset of the entry to which this relocation is applied.
276 unsigned int got_offset_;
277 // Type of relocation.
278 unsigned int r_type_;
279 // Whether this relocation is against a global symbol.
280 bool symbol_is_global_;
281 // A global or local symbol.
282 union
283 {
83a01957
HS
284 struct
285 {
286 // For a global symbol, the symbol itself.
287 Symbol* symbol;
288 } global;
289 struct
290 {
291 // For a local symbol, the object defining the symbol.
292 Sized_relobj_file<size, big_endian>* relobj;
293 // For a local symbol, the symbol index.
294 unsigned int index;
295 } local;
296 } u_;
297 }; // End of inner class Static_reloc
298
299 std::vector<Static_reloc> static_relocs_;
300}; // End of Output_data_got_aarch64
301
302
303template<int size, bool big_endian>
304class AArch64_input_section;
305
306
307template<int size, bool big_endian>
308class AArch64_output_section;
309
310
311// Reloc stub class.
312
313template<int size, bool big_endian>
314class Reloc_stub
315{
316 public:
317 typedef Reloc_stub<size, big_endian> This;
318 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
319
320 // Do not change the value of the enums, they are used to index into
321 // stub_insns array.
322 typedef enum
323 {
324 ST_NONE = 0,
325
326 // Using adrp/add pair, 4 insns (including alignment) without mem access,
327 // the fastest stub. This has a limited jump distance, which is tested by
328 // aarch64_valid_for_adrp_p.
329 ST_ADRP_BRANCH = 1,
330
331 // Using ldr-absolute-address/br-register, 4 insns with 1 mem access,
332 // unlimited in jump distance.
333 ST_LONG_BRANCH_ABS = 2,
334
335 // Using ldr/calculate-pcrel/jump, 8 insns (including alignment) with 1 mem
336 // access, slowest one. Only used in position independent executables.
337 ST_LONG_BRANCH_PCREL = 3,
338
339 } Stub_type;
340
341 // Branch range. This is used to calculate the section group size, as well as
342 // determine whether a stub is needed.
343 static const int MAX_BRANCH_OFFSET = ((1 << 25) - 1) << 2;
344 static const int MIN_BRANCH_OFFSET = -((1 << 25) << 2);
345
346 // Constant used to determine if an offset fits in the adrp instruction
347 // encoding.
348 static const int MAX_ADRP_IMM = (1 << 20) - 1;
349 static const int MIN_ADRP_IMM = -(1 << 20);
350
351 static const int BYTES_PER_INSN = 4;
352 static const int STUB_ADDR_ALIGN = 4;
353
354 // Determine whether the offset fits in the jump/branch instruction.
355 static bool
356 aarch64_valid_branch_offset_p(int64_t offset)
357 { return offset >= MIN_BRANCH_OFFSET && offset <= MAX_BRANCH_OFFSET; }
358
359 // Determine whether the offset fits in the adrp immediate field.
360 static bool
361 aarch64_valid_for_adrp_p(AArch64_address location, AArch64_address dest)
362 {
363 typedef AArch64_relocate_functions<size, big_endian> Reloc;
364 int64_t adrp_imm = (Reloc::Page(dest) - Reloc::Page(location)) >> 12;
365 return adrp_imm >= MIN_ADRP_IMM && adrp_imm <= MAX_ADRP_IMM;
366 }
367
368 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
369 // needed.
370 static Stub_type
371 stub_type_for_reloc(unsigned int r_type, AArch64_address address,
372 AArch64_address target);
373
374 Reloc_stub(Stub_type stub_type)
375 : stub_type_(stub_type), offset_(invalid_offset),
376 destination_address_(invalid_address)
377 { }
378
379 ~Reloc_stub()
380 { }
381
382 // Return offset of code stub from beginning of its containing stub table.
383 section_offset_type
384 offset() const
385 {
386 gold_assert(this->offset_ != invalid_offset);
387 return this->offset_;
388 }
389
390 // Set offset of code stub from beginning of its containing stub table.
391 void
392 set_offset(section_offset_type offset)
393 { this->offset_ = offset; }
394
395 // Return destination address.
396 AArch64_address
397 destination_address() const
398 {
399 gold_assert(this->destination_address_ != this->invalid_address);
400 return this->destination_address_;
401 }
402
403 // Set destination address.
404 void
405 set_destination_address(AArch64_address address)
406 {
407 gold_assert(address != this->invalid_address);
408 this->destination_address_ = address;
409 }
410
411 // Reset the destination address.
412 void
413 reset_destination_address()
414 { this->destination_address_ = this->invalid_address; }
415
416 // Return the stub type.
417 Stub_type
418 stub_type() const
419 { return stub_type_; }
420
421 // Return the stub size.
422 uint32_t
423 stub_size() const
424 { return this->stub_insn_number() * BYTES_PER_INSN; }
425
426 // Return the instruction number of this stub instance.
427 int
428 stub_insn_number() const
429 { return stub_insns_[this->stub_type_][0]; }
430
431 // Note the first "insn" is the number of total insns in this array.
432 const uint32_t*
433 stub_insns() const
434 { return stub_insns_[this->stub_type_]; }
435
436 // Write stub to output file.
437 void
438 write(unsigned char* view, section_size_type view_size)
439 { this->do_write(view, view_size); }
440
441 // The key class used to index the stub instance in the stub table's stub map.
442 class Key
443 {
444 public:
445 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
446 unsigned int r_sym, int32_t addend)
447 : stub_type_(stub_type), addend_(addend)
448 {
449 if (symbol != NULL)
450 {
451 this->r_sym_ = Reloc_stub::invalid_index;
452 this->u_.symbol = symbol;
453 }
454 else
455 {
456 gold_assert(relobj != NULL && r_sym != invalid_index);
457 this->r_sym_ = r_sym;
458 this->u_.relobj = relobj;
459 }
460 }
461
462 ~Key()
463 { }
464
465 // Return stub type.
466 Stub_type
467 stub_type() const
468 { return this->stub_type_; }
469
470 // Return the local symbol index or invalid_index.
471 unsigned int
472 r_sym() const
473 { return this->r_sym_; }
474
475 // Return the symbol if there is one.
476 const Symbol*
477 symbol() const
478 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
479
480 // Return the relobj if there is one.
481 const Relobj*
482 relobj() const
483 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
484
485 // Whether this equals to another key k.
486 bool
487 eq(const Key& k) const
488 {
489 return ((this->stub_type_ == k.stub_type_)
490 && (this->r_sym_ == k.r_sym_)
491 && ((this->r_sym_ != Reloc_stub::invalid_index)
492 ? (this->u_.relobj == k.u_.relobj)
493 : (this->u_.symbol == k.u_.symbol))
494 && (this->addend_ == k.addend_));
495 }
496
497 // Return a hash value.
498 size_t
499 hash_value() const
500 {
501 size_t name_hash_value = gold::string_hash<char>(
502 (this->r_sym_ != Reloc_stub::invalid_index)
503 ? this->u_.relobj->name().c_str()
504 : this->u_.symbol->name());
505 // We only have 4 stub types.
506 size_t stub_type_hash_value = 0x03 & this->stub_type_;
507 return (name_hash_value
508 ^ stub_type_hash_value
509 ^ ((this->r_sym_ & 0x3fff) << 2)
510 ^ ((this->addend_ & 0xffff) << 16));
511 }
512
513 // Functors for STL associative containers.
514 struct hash
515 {
516 size_t
517 operator()(const Key& k) const
518 { return k.hash_value(); }
519 };
520
521 struct equal_to
522 {
523 bool
524 operator()(const Key& k1, const Key& k2) const
525 { return k1.eq(k2); }
526 };
527
9726c3c1 528 private:
83a01957
HS
529 // Stub type.
530 const Stub_type stub_type_;
531 // If this is a local symbol, this is the index in the defining object.
532 // Otherwise, it is invalid_index for a global symbol.
533 unsigned int r_sym_;
534 // If r_sym_ is an invalid index, this points to a global symbol.
535 // Otherwise, it points to a relobj. We used the unsized and target
536 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
537 // Arm_relobj, in order to avoid making the stub class a template
538 // as most of the stub machinery is endianness-neutral. However, it
539 // may require a bit of casting done by users of this class.
540 union
541 {
542 const Symbol* symbol;
543 const Relobj* relobj;
544 } u_;
545 // Addend associated with a reloc.
546 int32_t addend_;
547 }; // End of inner class Reloc_stub::Key
548
549 protected:
550 // This may be overridden in the child class.
551 virtual void
552 do_write(unsigned char*, section_size_type);
553
554 private:
555 static const section_offset_type invalid_offset =
556 static_cast<section_offset_type>(-1);
557 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
558 static const AArch64_address invalid_address =
559 static_cast<AArch64_address>(-1);
560
561 static const uint32_t stub_insns_[][10];
562
563 const Stub_type stub_type_;
564 section_offset_type offset_;
565 AArch64_address destination_address_;
566}; // End of Reloc_stub
567
568
569// Write data to output file.
570
571template<int size, bool big_endian>
572void
573Reloc_stub<size, big_endian>::
574do_write(unsigned char* view, section_size_type)
575{
576 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
577 const uint32_t* insns = this->stub_insns();
578 uint32_t num_insns = this->stub_insn_number();
579 Insntype* ip = reinterpret_cast<Insntype*>(view);
580 for (uint32_t i = 1; i <= num_insns; ++i)
581 elfcpp::Swap<32, big_endian>::writeval(ip + i - 1, insns[i]);
582}
583
584
585// Stubs instructions definition.
586
587template<int size, bool big_endian>
588const uint32_t
589Reloc_stub<size, big_endian>::stub_insns_[][10] =
590 {
591 // The first element of each group is the num of the insns.
592
593 // ST_NONE
594 {0, 0},
595
596 // ST_ADRP_BRANCH
597 {
598 4,
599 0x90000010, /* adrp ip0, X */
600 /* ADR_PREL_PG_HI21(X) */
601 0x91000210, /* add ip0, ip0, :lo12:X */
602 /* ADD_ABS_LO12_NC(X) */
603 0xd61f0200, /* br ip0 */
604 0x00000000, /* alignment padding */
605 },
606
607 // ST_LONG_BRANCH_ABS
608 {
609 4,
610 0x58000050, /* ldr ip0, 0x8 */
611 0xd61f0200, /* br ip0 */
612 0x00000000, /* address field */
613 0x00000000, /* address fields */
614 },
615
616 // ST_LONG_BRANCH_PCREL
617 {
618 8,
619 0x58000090, /* ldr ip0, 0x10 */
620 0x10000011, /* adr ip1, #0 */
621 0x8b110210, /* add ip0, ip0, ip1 */
622 0xd61f0200, /* br ip0 */
623 0x00000000, /* address field */
624 0x00000000, /* address field */
625 0x00000000, /* alignment padding */
626 0x00000000, /* alignment padding */
627 }
628 };
629
630
631// Determine the stub type for a certain relocation or ST_NONE, if no stub is
632// needed.
633
634template<int size, bool big_endian>
635inline
636typename Reloc_stub<size, big_endian>::Stub_type
637Reloc_stub<size, big_endian>::stub_type_for_reloc(
638 unsigned int r_type, AArch64_address location, AArch64_address dest)
639{
640 int64_t branch_offset = 0;
641 switch(r_type)
642 {
643 case elfcpp::R_AARCH64_CALL26:
644 case elfcpp::R_AARCH64_JUMP26:
645 branch_offset = dest - location;
646 break;
647 default:
9726c3c1 648 gold_unreachable();
83a01957
HS
649 }
650
651 if (aarch64_valid_branch_offset_p(branch_offset))
652 return ST_NONE;
653
654 if (aarch64_valid_for_adrp_p(location, dest))
655 return ST_ADRP_BRANCH;
656
657 if (parameters->options().output_is_position_independent()
658 && parameters->options().output_is_executable())
659 return ST_LONG_BRANCH_PCREL;
660
661 return ST_LONG_BRANCH_ABS;
662}
663
664// A class to hold stubs for the ARM target.
665
666template<int size, bool big_endian>
667class Stub_table : public Output_data
668{
669 public:
670 typedef Target_aarch64<size, big_endian> The_target_aarch64;
671 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
672 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
673 typedef Reloc_stub<size, big_endian> The_reloc_stub;
674 typedef typename The_reloc_stub::Key The_reloc_stub_key;
675 typedef typename The_reloc_stub_key::hash The_reloc_stub_key_hash;
676 typedef typename The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to;
677 typedef Stub_table<size, big_endian> The_stub_table;
678 typedef Unordered_map<The_reloc_stub_key, The_reloc_stub*,
679 The_reloc_stub_key_hash, The_reloc_stub_key_equal_to>
680 Reloc_stub_map;
681 typedef typename Reloc_stub_map::const_iterator Reloc_stub_map_const_iter;
682 typedef Relocate_info<size, big_endian> The_relocate_info;
683
684 Stub_table(The_aarch64_input_section* owner)
685 : Output_data(), owner_(owner), reloc_stubs_size_(0), prev_data_size_(0)
686 { }
687
688 ~Stub_table()
689 { }
690
691 The_aarch64_input_section*
692 owner() const
693 { return owner_; }
694
695 // Whether this stub table is empty.
696 bool
697 empty() const
698 { return reloc_stubs_.empty(); }
699
700 // Return the current data size.
701 off_t
702 current_data_size() const
703 { return this->current_data_size_for_child(); }
704
705 // Add a STUB using KEY. The caller is responsible for avoiding addition
706 // if a STUB with the same key has already been added.
707 void
708 add_reloc_stub(The_reloc_stub* stub, const The_reloc_stub_key& key);
709
710 // Finalize stubs. No-op here, just for completeness.
711 void
712 finalize_stubs()
713 { }
714
715 // Look up a relocation stub using KEY. Return NULL if there is none.
716 The_reloc_stub*
717 find_reloc_stub(The_reloc_stub_key& key)
718 {
719 Reloc_stub_map_const_iter p = this->reloc_stubs_.find(key);
720 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
721 }
722
723 // Relocate stubs in this stub table.
724 void
725 relocate_stubs(const The_relocate_info*,
726 The_target_aarch64*,
727 Output_section*,
728 unsigned char*,
729 AArch64_address,
730 section_size_type);
731
732 // Update data size at the end of a relaxation pass. Return true if data size
733 // is different from that of the previous relaxation pass.
734 bool
735 update_data_size_changed_p()
736 {
737 // No addralign changed here.
738 off_t s = this->reloc_stubs_size_;
739 bool changed = (s != this->prev_data_size_);
740 this->prev_data_size_ = s;
741 return changed;
742 }
743
744 protected:
745 // Write out section contents.
746 void
747 do_write(Output_file*);
748
749 // Return the required alignment.
750 uint64_t
751 do_addralign() const
752 { return The_reloc_stub::STUB_ADDR_ALIGN; }
753
754 // Reset address and file offset.
755 void
756 do_reset_address_and_file_offset()
757 { this->set_current_data_size_for_child(this->prev_data_size_); }
758
759 // Set final data size.
760 void
761 set_final_data_size()
762 { this->set_data_size(this->current_data_size()); }
763
764 private:
765 // Relocate one stub.
766 void
767 relocate_stub(The_reloc_stub*,
768 const The_relocate_info*,
769 The_target_aarch64*,
770 Output_section*,
771 unsigned char*,
772 AArch64_address,
773 section_size_type);
774
775 private:
776 // Owner of this stub table.
777 The_aarch64_input_section* owner_;
778 // The relocation stubs.
779 Reloc_stub_map reloc_stubs_;
780 // Size of reloc stubs.
781 off_t reloc_stubs_size_;
782 // data size of this in the previous pass.
783 off_t prev_data_size_;
784}; // End of Stub_table
785
786
787// Add a STUB using KEY. The caller is responsible for avoiding addition
788// if a STUB with the same key has already been added.
789
790template<int size, bool big_endian>
791void
792Stub_table<size, big_endian>::add_reloc_stub(
793 The_reloc_stub* stub, const The_reloc_stub_key& key)
794{
795 gold_assert(stub->stub_type() == key.stub_type());
796 this->reloc_stubs_[key] = stub;
797
798 // Assign stub offset early. We can do this because we never remove
799 // reloc stubs and they are in the beginning of the stub table.
800 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_,
801 The_reloc_stub::STUB_ADDR_ALIGN);
802 stub->set_offset(this->reloc_stubs_size_);
803 this->reloc_stubs_size_ += stub->stub_size();
804}
805
806
807// Relocate all stubs in this stub table.
808
809template<int size, bool big_endian>
810void
811Stub_table<size, big_endian>::
812relocate_stubs(const The_relocate_info* relinfo,
813 The_target_aarch64* target_aarch64,
814 Output_section* output_section,
815 unsigned char* view,
816 AArch64_address address,
817 section_size_type view_size)
818{
819 // "view_size" is the total size of the stub_table.
820 gold_assert(address == this->address() &&
821 view_size == static_cast<section_size_type>(this->data_size()));
822 for(Reloc_stub_map_const_iter p = this->reloc_stubs_.begin();
823 p != this->reloc_stubs_.end(); ++p)
824 relocate_stub(p->second, relinfo, target_aarch64, output_section,
825 view, address, view_size);
826}
827
828
829// Relocate one stub. This is a helper for Stub_table::relocate_stubs().
830
831template<int size, bool big_endian>
832void
833Stub_table<size, big_endian>::
834relocate_stub(The_reloc_stub* stub,
835 const The_relocate_info* relinfo,
836 The_target_aarch64* target_aarch64,
837 Output_section* output_section,
838 unsigned char* view,
839 AArch64_address address,
840 section_size_type view_size)
841{
842 // "offset" is the offset from the beginning of the stub_table.
843 section_size_type offset = stub->offset();
844 section_size_type stub_size = stub->stub_size();
845 // "view_size" is the total size of the stub_table.
846 gold_assert(offset + stub_size <= view_size);
847
848 target_aarch64->relocate_stub(stub, relinfo, output_section,
849 view + offset, address + offset, view_size);
850}
851
852
853// Write out the stubs to file.
854
855template<int size, bool big_endian>
856void
857Stub_table<size, big_endian>::do_write(Output_file* of)
858{
859 off_t offset = this->offset();
860 const section_size_type oview_size =
861 convert_to_section_size_type(this->data_size());
862 unsigned char* const oview = of->get_output_view(offset, oview_size);
863
864 // Write relocation stubs.
865 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
866 p != this->reloc_stubs_.end(); ++p)
867 {
868 The_reloc_stub* stub = p->second;
869 AArch64_address address = this->address() + stub->offset();
870 gold_assert(address ==
871 align_address(address, The_reloc_stub::STUB_ADDR_ALIGN));
872 stub->write(oview + stub->offset(), stub->stub_size());
873 }
874
875 of->write_output_view(this->offset(), oview_size, oview);
876}
877
878
879// AArch64_relobj class.
880
881template<int size, bool big_endian>
882class AArch64_relobj : public Sized_relobj_file<size, big_endian>
883{
884 public:
885 typedef AArch64_relobj<size, big_endian> This;
886 typedef Target_aarch64<size, big_endian> The_target_aarch64;
887 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
888 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
889 typedef Stub_table<size, big_endian> The_stub_table;
890 typedef std::vector<The_stub_table*> Stub_table_list;
891 static const AArch64_address invalid_address =
892 static_cast<AArch64_address>(-1);
893
894 AArch64_relobj(const std::string& name, Input_file* input_file, off_t offset,
895 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
896 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
897 stub_tables_()
898 { }
899
900 ~AArch64_relobj()
901 { }
902
903 // Return the stub table of the SHNDX-th section if there is one.
904 The_stub_table*
905 stub_table(unsigned int shndx) const
906 {
907 gold_assert(shndx < this->stub_tables_.size());
908 return this->stub_tables_[shndx];
909 }
910
911 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
912 void
913 set_stub_table(unsigned int shndx, The_stub_table* stub_table)
914 {
915 gold_assert(shndx < this->stub_tables_.size());
916 this->stub_tables_[shndx] = stub_table;
917 }
918
919 // Scan all relocation sections for stub generation.
920 void
921 scan_sections_for_stubs(The_target_aarch64*, const Symbol_table*,
922 const Layout*);
923
924 // Whether a section is a scannable text section.
925 bool
926 text_section_is_scannable(const elfcpp::Shdr<size, big_endian>&, unsigned int,
927 const Output_section*, const Symbol_table*);
928
929 // Convert regular input section with index SHNDX to a relaxed section.
930 void
931 convert_input_section_to_relaxed_section(unsigned /* shndx */)
932 {
933 // The stubs have relocations and we need to process them after writing
934 // out the stubs. So relocation now must follow section write.
935 this->set_relocs_must_follow_section_writes();
936 }
937
938 protected:
939 // Post constructor setup.
940 void
941 do_setup()
942 {
943 // Call parent's setup method.
944 Sized_relobj_file<size, big_endian>::do_setup();
945
946 // Initialize look-up tables.
947 this->stub_tables_.resize(this->shnum());
948 }
949
950 virtual void
951 do_relocate_sections(
952 const Symbol_table* symtab, const Layout* layout,
953 const unsigned char* pshdrs, Output_file* of,
954 typename Sized_relobj_file<size, big_endian>::Views* pviews);
955
956 private:
957 // Whether a section needs to be scanned for relocation stubs.
958 bool
959 section_needs_reloc_stub_scanning(const elfcpp::Shdr<size, big_endian>&,
960 const Relobj::Output_sections&,
961 const Symbol_table*, const unsigned char*);
962
963 // List of stub tables.
964 Stub_table_list stub_tables_;
965}; // End of AArch64_relobj
966
967
968// Relocate sections.
969
970template<int size, bool big_endian>
971void
972AArch64_relobj<size, big_endian>::do_relocate_sections(
973 const Symbol_table* symtab, const Layout* layout,
974 const unsigned char* pshdrs, Output_file* of,
975 typename Sized_relobj_file<size, big_endian>::Views* pviews)
976{
977 // Call parent to relocate sections.
978 Sized_relobj_file<size, big_endian>::do_relocate_sections(symtab, layout,
979 pshdrs, of, pviews);
980
981 // We do not generate stubs if doing a relocatable link.
982 if (parameters->options().relocatable())
983 return;
984
985 Relocate_info<size, big_endian> relinfo;
986 relinfo.symtab = symtab;
987 relinfo.layout = layout;
988 relinfo.object = this;
989
990 // Relocate stub tables.
991 unsigned int shnum = this->shnum();
992 The_target_aarch64* target = The_target_aarch64::current_target();
993
994 for (unsigned int i = 1; i < shnum; ++i)
995 {
996 The_aarch64_input_section* aarch64_input_section =
997 target->find_aarch64_input_section(this, i);
998 if (aarch64_input_section != NULL
999 && aarch64_input_section->is_stub_table_owner()
1000 && !aarch64_input_section->stub_table()->empty())
1001 {
1002 Output_section* os = this->output_section(i);
1003 gold_assert(os != NULL);
1004
1005 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
1006 relinfo.reloc_shdr = NULL;
1007 relinfo.data_shndx = i;
1008 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
1009
1010 typename Sized_relobj_file<size, big_endian>::View_size&
1011 view_struct = (*pviews)[i];
1012 gold_assert(view_struct.view != NULL);
1013
1014 The_stub_table* stub_table = aarch64_input_section->stub_table();
1015 off_t offset = stub_table->address() - view_struct.address;
1016 unsigned char* view = view_struct.view + offset;
1017 AArch64_address address = stub_table->address();
1018 section_size_type view_size = stub_table->data_size();
1019 stub_table->relocate_stubs(&relinfo, target, os, view, address,
1020 view_size);
1021 }
1022 }
1023}
1024
1025
1026// Determine if an input section is scannable for stub processing. SHDR is
1027// the header of the section and SHNDX is the section index. OS is the output
1028// section for the input section and SYMTAB is the global symbol table used to
1029// look up ICF information.
1030
1031template<int size, bool big_endian>
1032bool
1033AArch64_relobj<size, big_endian>::text_section_is_scannable(
1034 const elfcpp::Shdr<size, big_endian>& text_shdr,
1035 unsigned int text_shndx,
1036 const Output_section* os,
1037 const Symbol_table* symtab)
1038{
1039 // Skip any empty sections, unallocated sections or sections whose
1040 // type are not SHT_PROGBITS.
1041 if (text_shdr.get_sh_size() == 0
1042 || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
1043 || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1044 return false;
1045
1046 // Skip any discarded or ICF'ed sections.
1047 if (os == NULL || symtab->is_section_folded(this, text_shndx))
1048 return false;
1049
1050 // Skip exception frame.
1051 if (strcmp(os->name(), ".eh_frame") == 0)
1052 return false ;
1053
1054 gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
1055 os->find_relaxed_input_section(this, text_shndx) != NULL);
1056
1057 return true;
1058}
1059
1060
1061// Determine if we want to scan the SHNDX-th section for relocation stubs.
1062// This is a helper for AArch64_relobj::scan_sections_for_stubs().
1063
1064template<int size, bool big_endian>
1065bool
1066AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
1067 const elfcpp::Shdr<size, big_endian>& shdr,
1068 const Relobj::Output_sections& out_sections,
1069 const Symbol_table* symtab,
1070 const unsigned char* pshdrs)
1071{
1072 unsigned int sh_type = shdr.get_sh_type();
1073 if (sh_type != elfcpp::SHT_RELA)
1074 return false;
1075
1076 // Ignore empty section.
1077 off_t sh_size = shdr.get_sh_size();
1078 if (sh_size == 0)
1079 return false;
1080
1081 // Ignore reloc section with unexpected symbol table. The
1082 // error will be reported in the final link.
1083 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
1084 return false;
1085
1086 gold_assert(sh_type == elfcpp::SHT_RELA);
1087 unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
1088
1089 // Ignore reloc section with unexpected entsize or uneven size.
1090 // The error will be reported in the final link.
1091 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
1092 return false;
1093
1094 // Ignore reloc section with bad info. This error will be
1095 // reported in the final link.
1096 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
1097 if (text_shndx >= this->shnum())
1098 return false;
1099
1100 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1101 const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
1102 text_shndx * shdr_size);
1103 return this->text_section_is_scannable(text_shdr, text_shndx,
1104 out_sections[text_shndx], symtab);
1105}
1106
1107
1108// Scan relocations for stub generation.
1109
1110template<int size, bool big_endian>
1111void
1112AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
1113 The_target_aarch64* target,
1114 const Symbol_table* symtab,
1115 const Layout* layout)
1116{
1117 unsigned int shnum = this->shnum();
1118 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1119
1120 // Read the section headers.
1121 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
1122 shnum * shdr_size,
1123 true, true);
1124
1125 // To speed up processing, we set up hash tables for fast lookup of
1126 // input offsets to output addresses.
1127 this->initialize_input_to_output_maps();
1128
1129 const Relobj::Output_sections& out_sections(this->output_sections());
1130
1131 Relocate_info<size, big_endian> relinfo;
1132 relinfo.symtab = symtab;
1133 relinfo.layout = layout;
1134 relinfo.object = this;
1135
1136 // Do relocation stubs scanning.
1137 const unsigned char* p = pshdrs + shdr_size;
1138 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
1139 {
1140 const elfcpp::Shdr<size, big_endian> shdr(p);
1141 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
1142 pshdrs))
1143 {
1144 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
1145 AArch64_address output_offset =
1146 this->get_output_section_offset(index);
1147 AArch64_address output_address;
1148 if (output_offset != invalid_address)
1149 {
1150 output_address = out_sections[index]->address() + output_offset;
1151 }
1152 else
1153 {
1154 // Currently this only happens for a relaxed section.
1155 const Output_relaxed_input_section* poris =
1156 out_sections[index]->find_relaxed_input_section(this, index);
1157 gold_assert(poris != NULL);
1158 output_address = poris->address();
1159 }
1160
1161 // Get the relocations.
1162 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
1163 shdr.get_sh_size(),
1164 true, false);
1165
1166 // Get the section contents.
1167 section_size_type input_view_size = 0;
1168 const unsigned char* input_view =
1169 this->section_contents(index, &input_view_size, false);
1170
1171 relinfo.reloc_shndx = i;
1172 relinfo.data_shndx = index;
1173 unsigned int sh_type = shdr.get_sh_type();
1174 unsigned int reloc_size;
1175 gold_assert (sh_type == elfcpp::SHT_RELA);
1176 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
1177
1178 Output_section* os = out_sections[index];
1179 target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
1180 shdr.get_sh_size() / reloc_size,
1181 os,
1182 output_offset == invalid_address,
1183 input_view, output_address,
1184 input_view_size);
1185 }
1186 }
1187}
1188
1189
1190// A class to wrap an ordinary input section containing executable code.
1191
1192template<int size, bool big_endian>
1193class AArch64_input_section : public Output_relaxed_input_section
1194{
1195 public:
1196 typedef Stub_table<size, big_endian> The_stub_table;
1197
1198 AArch64_input_section(Relobj* relobj, unsigned int shndx)
1199 : Output_relaxed_input_section(relobj, shndx, 1),
1200 stub_table_(NULL),
1201 original_contents_(NULL), original_size_(0),
1202 original_addralign_(1)
1203 { }
1204
1205 ~AArch64_input_section()
1206 { delete[] this->original_contents_; }
1207
1208 // Initialize.
1209 void
1210 init();
1211
1212 // Set the stub_table.
1213 void
1214 set_stub_table(The_stub_table* st)
1215 { this->stub_table_ = st; }
1216
1217 // Whether this is a stub table owner.
1218 bool
1219 is_stub_table_owner() const
1220 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1221
1222 // Return the original size of the section.
1223 uint32_t
1224 original_size() const
1225 { return this->original_size_; }
1226
1227 // Return the stub table.
1228 The_stub_table*
1229 stub_table()
1230 { return stub_table_; }
1231
1232 protected:
1233 // Write out this input section.
1234 void
1235 do_write(Output_file*);
1236
1237 // Return required alignment of this.
1238 uint64_t
1239 do_addralign() const
1240 {
1241 if (this->is_stub_table_owner())
1242 return std::max(this->stub_table_->addralign(),
1243 static_cast<uint64_t>(this->original_addralign_));
1244 else
1245 return this->original_addralign_;
1246 }
1247
1248 // Finalize data size.
1249 void
1250 set_final_data_size();
1251
1252 // Reset address and file offset.
1253 void
1254 do_reset_address_and_file_offset();
1255
1256 // Output offset.
1257 bool
1258 do_output_offset(const Relobj* object, unsigned int shndx,
1259 section_offset_type offset,
1260 section_offset_type* poutput) const
1261 {
1262 if ((object == this->relobj())
1263 && (shndx == this->shndx())
1264 && (offset >= 0)
1265 && (offset <=
1266 convert_types<section_offset_type, uint32_t>(this->original_size_)))
1267 {
1268 *poutput = offset;
1269 return true;
1270 }
1271 else
1272 return false;
1273 }
1274
1275 private:
1276 // Copying is not allowed.
1277 AArch64_input_section(const AArch64_input_section&);
1278 AArch64_input_section& operator=(const AArch64_input_section&);
1279
1280 // The relocation stubs.
1281 The_stub_table* stub_table_;
1282 // Original section contents. We have to make a copy here since the file
1283 // containing the original section may not be locked when we need to access
1284 // the contents.
1285 unsigned char* original_contents_;
1286 // Section size of the original input section.
1287 uint32_t original_size_;
1288 // Address alignment of the original input section.
1289 uint32_t original_addralign_;
1290}; // End of AArch64_input_section
1291
1292
1293// Finalize data size.
1294
1295template<int size, bool big_endian>
1296void
1297AArch64_input_section<size, big_endian>::set_final_data_size()
1298{
1299 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
1300
1301 if (this->is_stub_table_owner())
1302 {
1303 this->stub_table_->finalize_data_size();
1304 off = align_address(off, this->stub_table_->addralign());
1305 off += this->stub_table_->data_size();
1306 }
1307 this->set_data_size(off);
1308}
1309
1310
1311// Reset address and file offset.
1312
1313template<int size, bool big_endian>
1314void
1315AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
1316{
1317 // Size of the original input section contents.
1318 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
1319
1320 // If this is a stub table owner, account for the stub table size.
1321 if (this->is_stub_table_owner())
1322 {
1323 The_stub_table* stub_table = this->stub_table_;
1324
1325 // Reset the stub table's address and file offset. The
1326 // current data size for child will be updated after that.
1327 stub_table_->reset_address_and_file_offset();
1328 off = align_address(off, stub_table_->addralign());
1329 off += stub_table->current_data_size();
1330 }
1331
1332 this->set_current_data_size(off);
1333}
1334
1335
1336// Initialize an Arm_input_section.
1337
1338template<int size, bool big_endian>
1339void
1340AArch64_input_section<size, big_endian>::init()
1341{
1342 Relobj* relobj = this->relobj();
1343 unsigned int shndx = this->shndx();
1344
1345 // We have to cache original size, alignment and contents to avoid locking
1346 // the original file.
1347 this->original_addralign_ =
1348 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
1349
1350 // This is not efficient but we expect only a small number of relaxed
1351 // input sections for stubs.
1352 section_size_type section_size;
1353 const unsigned char* section_contents =
1354 relobj->section_contents(shndx, &section_size, false);
1355 this->original_size_ =
1356 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
1357
1358 gold_assert(this->original_contents_ == NULL);
1359 this->original_contents_ = new unsigned char[section_size];
1360 memcpy(this->original_contents_, section_contents, section_size);
1361
1362 // We want to make this look like the original input section after
1363 // output sections are finalized.
1364 Output_section* os = relobj->output_section(shndx);
1365 off_t offset = relobj->output_section_offset(shndx);
1366 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
1367 this->set_address(os->address() + offset);
1368 this->set_file_offset(os->offset() + offset);
1369 this->set_current_data_size(this->original_size_);
1370 this->finalize_data_size();
1371}
1372
1373
1374// Write data to output file.
1375
1376template<int size, bool big_endian>
1377void
1378AArch64_input_section<size, big_endian>::do_write(Output_file* of)
1379{
1380 // We have to write out the original section content.
1381 gold_assert(this->original_contents_ != NULL);
1382 of->write(this->offset(), this->original_contents_,
1383 this->original_size_);
1384
1385 // If this owns a stub table and it is not empty, write it.
1386 if (this->is_stub_table_owner() && !this->stub_table_->empty())
1387 this->stub_table_->write(of);
1388}
1389
1390
1391// Arm output section class. This is defined mainly to add a number of stub
1392// generation methods.
1393
1394template<int size, bool big_endian>
1395class AArch64_output_section : public Output_section
1396{
1397 public:
1398 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1399 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
1400 typedef Stub_table<size, big_endian> The_stub_table;
1401 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1402
1403 public:
1404 AArch64_output_section(const char* name, elfcpp::Elf_Word type,
1405 elfcpp::Elf_Xword flags)
1406 : Output_section(name, type, flags)
1407 { }
1408
1409 ~AArch64_output_section() {}
1410
1411 // Group input sections for stub generation.
1412 void
1413 group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
1414 const Task*);
1415
1416 private:
1417 typedef Output_section::Input_section Input_section;
1418 typedef Output_section::Input_section_list Input_section_list;
1419
1420 // Create a stub group.
1421 void
1422 create_stub_group(Input_section_list::const_iterator,
1423 Input_section_list::const_iterator,
1424 Input_section_list::const_iterator,
1425 The_target_aarch64*,
1426 std::vector<Output_relaxed_input_section*>&,
1427 const Task*);
1428}; // End of AArch64_output_section
1429
1430
1431// Create a stub group for input sections from FIRST to LAST. OWNER points to
1432// the input section that will be the owner of the stub table.
1433
1434template<int size, bool big_endian> void
1435AArch64_output_section<size, big_endian>::create_stub_group(
1436 Input_section_list::const_iterator first,
1437 Input_section_list::const_iterator last,
1438 Input_section_list::const_iterator owner,
1439 The_target_aarch64* target,
1440 std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
1441 const Task* task)
1442{
1443 // Currently we convert ordinary input sections into relaxed sections only
1444 // at this point.
1445 The_aarch64_input_section* input_section;
1446 if (owner->is_relaxed_input_section())
1447 gold_unreachable();
1448 else
1449 {
1450 gold_assert(owner->is_input_section());
1451 // Create a new relaxed input section. We need to lock the original
1452 // file.
1453 Task_lock_obj<Object> tl(task, owner->relobj());
1454 input_section =
1455 target->new_aarch64_input_section(owner->relobj(), owner->shndx());
1456 new_relaxed_sections.push_back(input_section);
1457 }
1458
1459 // Create a stub table.
1460 The_stub_table* stub_table =
1461 target->new_stub_table(input_section);
1462
1463 input_section->set_stub_table(stub_table);
1464
1465 Input_section_list::const_iterator p = first;
1466 // Look for input sections or relaxed input sections in [first ... last].
1467 do
1468 {
1469 if (p->is_input_section() || p->is_relaxed_input_section())
1470 {
1471 // The stub table information for input sections live
1472 // in their objects.
1473 The_aarch64_relobj* aarch64_relobj =
1474 static_cast<The_aarch64_relobj*>(p->relobj());
1475 aarch64_relobj->set_stub_table(p->shndx(), stub_table);
1476 }
1477 }
1478 while (p++ != last);
1479}
1480
1481
1482// Group input sections for stub generation. GROUP_SIZE is roughly the limit of
1483// stub groups. We grow a stub group by adding input section until the size is
1484// just below GROUP_SIZE. The last input section will be converted into a stub
1485// table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
1486// after the stub table, effectively doubling the group size.
1487//
1488// This is similar to the group_sections() function in elf32-arm.c but is
1489// implemented differently.
1490
1491template<int size, bool big_endian>
1492void AArch64_output_section<size, big_endian>::group_sections(
1493 section_size_type group_size,
1494 bool stubs_always_after_branch,
1495 Target_aarch64<size, big_endian>* target,
1496 const Task* task)
1497{
1498 typedef enum
1499 {
1500 NO_GROUP,
1501 FINDING_STUB_SECTION,
1502 HAS_STUB_SECTION
1503 } State;
1504
1505 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
1506
1507 State state = NO_GROUP;
1508 section_size_type off = 0;
1509 section_size_type group_begin_offset = 0;
1510 section_size_type group_end_offset = 0;
1511 section_size_type stub_table_end_offset = 0;
1512 Input_section_list::const_iterator group_begin =
1513 this->input_sections().end();
1514 Input_section_list::const_iterator stub_table =
1515 this->input_sections().end();
1516 Input_section_list::const_iterator group_end = this->input_sections().end();
1517 for (Input_section_list::const_iterator p = this->input_sections().begin();
1518 p != this->input_sections().end();
1519 ++p)
1520 {
1521 section_size_type section_begin_offset =
1522 align_address(off, p->addralign());
1523 section_size_type section_end_offset =
1524 section_begin_offset + p->data_size();
1525
1526 // Check to see if we should group the previously seen sections.
1527 switch (state)
1528 {
1529 case NO_GROUP:
1530 break;
1531
1532 case FINDING_STUB_SECTION:
1533 // Adding this section makes the group larger than GROUP_SIZE.
1534 if (section_end_offset - group_begin_offset >= group_size)
1535 {
1536 if (stubs_always_after_branch)
1537 {
1538 gold_assert(group_end != this->input_sections().end());
1539 this->create_stub_group(group_begin, group_end, group_end,
1540 target, new_relaxed_sections,
1541 task);
1542 state = NO_GROUP;
1543 }
1544 else
1545 {
1546 // Input sections up to stub_group_size bytes after the stub
1547 // table can be handled by it too.
1548 state = HAS_STUB_SECTION;
1549 stub_table = group_end;
1550 stub_table_end_offset = group_end_offset;
1551 }
1552 }
1553 break;
1554
1555 case HAS_STUB_SECTION:
1556 // Adding this section makes the post stub-section group larger
1557 // than GROUP_SIZE.
1558 gold_unreachable();
1559 // NOT SUPPORTED YET. For completeness only.
1560 if (section_end_offset - stub_table_end_offset >= group_size)
1561 {
1562 gold_assert(group_end != this->input_sections().end());
1563 this->create_stub_group(group_begin, group_end, stub_table,
1564 target, new_relaxed_sections, task);
1565 state = NO_GROUP;
1566 }
1567 break;
1568
1569 default:
1570 gold_unreachable();
1571 }
1572
1573 // If we see an input section and currently there is no group, start
1574 // a new one. Skip any empty sections. We look at the data size
1575 // instead of calling p->relobj()->section_size() to avoid locking.
1576 if ((p->is_input_section() || p->is_relaxed_input_section())
1577 && (p->data_size() != 0))
1578 {
1579 if (state == NO_GROUP)
1580 {
1581 state = FINDING_STUB_SECTION;
1582 group_begin = p;
1583 group_begin_offset = section_begin_offset;
1584 }
1585
1586 // Keep track of the last input section seen.
1587 group_end = p;
1588 group_end_offset = section_end_offset;
1589 }
1590
1591 off = section_end_offset;
1592 }
1593
1594 // Create a stub group for any ungrouped sections.
1595 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
1596 {
1597 gold_assert(group_end != this->input_sections().end());
1598 this->create_stub_group(group_begin, group_end,
1599 (state == FINDING_STUB_SECTION
1600 ? group_end
1601 : stub_table),
1602 target, new_relaxed_sections, task);
1603 }
8e33481e 1604
83a01957
HS
1605 if (!new_relaxed_sections.empty())
1606 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
1607
1608 // Update the section offsets
1609 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
1610 {
1611 The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
1612 new_relaxed_sections[i]->relobj());
1613 unsigned int shndx = new_relaxed_sections[i]->shndx();
1614 // Tell AArch64_relobj that this input section is converted.
1615 relobj->convert_input_section_to_relaxed_section(shndx);
1616 }
1617} // End of AArch64_output_section::group_sections
3a531937 1618
053a4d68 1619
9363c7c3
JY
1620AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
1621
3a531937 1622
053a4d68
JY
1623// The aarch64 target class.
1624// See the ABI at
1625// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
1626template<int size, bool big_endian>
1627class Target_aarch64 : public Sized_target<size, big_endian>
1628{
1629 public:
83a01957 1630 typedef Target_aarch64<size, big_endian> This;
053a4d68
JY
1631 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
1632 Reloc_section;
83a01957 1633 typedef Relocate_info<size, big_endian> The_relocate_info;
053a4d68 1634 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
83a01957
HS
1635 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
1636 typedef Reloc_stub<size, big_endian> The_reloc_stub;
1637 typedef typename The_reloc_stub::Stub_type The_reloc_stub_type;
1638 typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
1639 typedef Stub_table<size, big_endian> The_stub_table;
1640 typedef std::vector<The_stub_table*> Stub_table_list;
1641 typedef typename Stub_table_list::iterator Stub_table_iterator;
1642 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1643 typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
1644 typedef Unordered_map<Section_id,
1645 AArch64_input_section<size, big_endian>*,
1646 Section_id_hash> AArch64_input_section_map;
8e33481e 1647 const static int TCB_SIZE = size / 8 * 2;
053a4d68
JY
1648
1649 Target_aarch64(const Target::Target_info* info = &aarch64_info)
1650 : Sized_target<size, big_endian>(info),
3a531937
JY
1651 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
1652 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
1653 rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
83a01957
HS
1654 got_mod_index_offset_(-1U),
1655 tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
0bf32ea9 1656 stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
053a4d68
JY
1657 { }
1658
1659 // Scan the relocations to determine unreferenced sections for
1660 // garbage collection.
1661 void
1662 gc_process_relocs(Symbol_table* symtab,
1663 Layout* layout,
1664 Sized_relobj_file<size, big_endian>* object,
1665 unsigned int data_shndx,
1666 unsigned int sh_type,
1667 const unsigned char* prelocs,
1668 size_t reloc_count,
1669 Output_section* output_section,
1670 bool needs_special_offset_handling,
1671 size_t local_symbol_count,
1672 const unsigned char* plocal_symbols);
1673
1674 // Scan the relocations to look for symbol adjustments.
1675 void
1676 scan_relocs(Symbol_table* symtab,
1677 Layout* layout,
1678 Sized_relobj_file<size, big_endian>* object,
1679 unsigned int data_shndx,
1680 unsigned int sh_type,
1681 const unsigned char* prelocs,
1682 size_t reloc_count,
1683 Output_section* output_section,
1684 bool needs_special_offset_handling,
1685 size_t local_symbol_count,
1686 const unsigned char* plocal_symbols);
1687
1688 // Finalize the sections.
1689 void
1690 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1691
3a531937
JY
1692 // Return the value to use for a dynamic which requires special
1693 // treatment.
1694 uint64_t
1695 do_dynsym_value(const Symbol*) const;
1696
053a4d68
JY
1697 // Relocate a section.
1698 void
1699 relocate_section(const Relocate_info<size, big_endian>*,
1700 unsigned int sh_type,
1701 const unsigned char* prelocs,
1702 size_t reloc_count,
1703 Output_section* output_section,
1704 bool needs_special_offset_handling,
1705 unsigned char* view,
1706 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1707 section_size_type view_size,
1708 const Reloc_symbol_changes*);
1709
1710 // Scan the relocs during a relocatable link.
1711 void
1712 scan_relocatable_relocs(Symbol_table* symtab,
1713 Layout* layout,
1714 Sized_relobj_file<size, big_endian>* object,
1715 unsigned int data_shndx,
1716 unsigned int sh_type,
1717 const unsigned char* prelocs,
1718 size_t reloc_count,
1719 Output_section* output_section,
1720 bool needs_special_offset_handling,
1721 size_t local_symbol_count,
1722 const unsigned char* plocal_symbols,
1723 Relocatable_relocs*);
1724
1725 // Relocate a section during a relocatable link.
1726 void
1727 relocate_relocs(
1728 const Relocate_info<size, big_endian>*,
1729 unsigned int sh_type,
1730 const unsigned char* prelocs,
1731 size_t reloc_count,
1732 Output_section* output_section,
1733 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
1734 const Relocatable_relocs*,
1735 unsigned char* view,
1736 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1737 section_size_type view_size,
1738 unsigned char* reloc_view,
1739 section_size_type reloc_view_size);
1740
3a531937
JY
1741 // Return the symbol index to use for a target specific relocation.
1742 // The only target specific relocation is R_AARCH64_TLSDESC for a
1743 // local symbol, which is an absolute reloc.
1744 unsigned int
1745 do_reloc_symbol_index(void*, unsigned int r_type) const
1746 {
1747 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
1748 return 0;
1749 }
1750
1751 // Return the addend to use for a target specific relocation.
7fa5525f
RÁE
1752 uint64_t
1753 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
3a531937 1754
9363c7c3
JY
1755 // Return the PLT section.
1756 uint64_t
1757 do_plt_address_for_global(const Symbol* gsym) const
1758 { return this->plt_section()->address_for_global(gsym); }
1759
1760 uint64_t
1761 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
1762 { return this->plt_section()->address_for_local(relobj, symndx); }
1763
9726c3c1
HS
1764 // This function should be defined in targets that can use relocation
1765 // types to determine (implemented in local_reloc_may_be_function_pointer
1766 // and global_reloc_may_be_function_pointer)
1767 // if a function's pointer is taken. ICF uses this in safe mode to only
1768 // fold those functions whose pointer is defintely not taken.
1769 bool
1770 do_can_check_for_function_pointers() const
1771 { return true; }
1772
053a4d68
JY
1773 // Return the number of entries in the PLT.
1774 unsigned int
1775 plt_entry_count() const;
1776
1777 //Return the offset of the first non-reserved PLT entry.
1778 unsigned int
1779 first_plt_entry_offset() const;
1780
1781 // Return the size of each PLT entry.
1782 unsigned int
1783 plt_entry_size() const;
1784
83a01957
HS
1785 // Create a stub table.
1786 The_stub_table*
1787 new_stub_table(The_aarch64_input_section*);
1788
1789 // Create an aarch64 input section.
1790 The_aarch64_input_section*
1791 new_aarch64_input_section(Relobj*, unsigned int);
1792
1793 // Find an aarch64 input section instance for a given OBJ and SHNDX.
1794 The_aarch64_input_section*
1795 find_aarch64_input_section(Relobj*, unsigned int) const;
1796
1797 // Return the thread control block size.
8e33481e
HS
1798 unsigned int
1799 tcb_size() const { return This::TCB_SIZE; }
1800
83a01957
HS
1801 // Scan a section for stub generation.
1802 void
1803 scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
1804 const unsigned char*, size_t, Output_section*,
1805 bool, const unsigned char*,
1806 Address,
1807 section_size_type);
1808
1809 // Scan a relocation section for stub.
1810 template<int sh_type>
1811 void
1812 scan_reloc_section_for_stubs(
1813 const The_relocate_info* relinfo,
1814 const unsigned char* prelocs,
1815 size_t reloc_count,
1816 Output_section* output_section,
1817 bool needs_special_offset_handling,
1818 const unsigned char* view,
1819 Address view_address,
1820 section_size_type);
1821
1822 // Relocate a single stub.
1823 void
1824 relocate_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
1825 Output_section*, unsigned char*, Address,
1826 section_size_type);
1827
1828 // Get the default AArch64 target.
1829 static This*
1830 current_target()
1831 {
1832 gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
1833 && parameters->target().get_size() == size
1834 && parameters->target().is_big_endian() == big_endian);
1835 return static_cast<This*>(parameters->sized_target<size, big_endian>());
1836 }
1837
9363c7c3
JY
1838 protected:
1839 void
1840 do_select_as_default_target()
1841 {
1842 gold_assert(aarch64_reloc_property_table == NULL);
1843 aarch64_reloc_property_table = new AArch64_reloc_property_table();
1844 }
1845
3a531937
JY
1846 // Add a new reloc argument, returning the index in the vector.
1847 size_t
1848 add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
1849 unsigned int r_sym)
1850 {
1851 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
1852 return this->tlsdesc_reloc_info_.size() - 1;
1853 }
1854
9363c7c3 1855 virtual Output_data_plt_aarch64<size, big_endian>*
3a531937
JY
1856 do_make_data_plt(Layout* layout,
1857 Output_data_got_aarch64<size, big_endian>* got,
1858 Output_data_space* got_plt,
1859 Output_data_space* got_irelative)
9363c7c3 1860 {
3a531937
JY
1861 return new Output_data_plt_aarch64_standard<size, big_endian>(
1862 layout, got, got_plt, got_irelative);
9363c7c3
JY
1863 }
1864
83a01957
HS
1865
1866 // do_make_elf_object to override the same function in the base class.
1867 Object*
1868 do_make_elf_object(const std::string&, Input_file*, off_t,
1869 const elfcpp::Ehdr<size, big_endian>&);
1870
9363c7c3 1871 Output_data_plt_aarch64<size, big_endian>*
3a531937
JY
1872 make_data_plt(Layout* layout,
1873 Output_data_got_aarch64<size, big_endian>* got,
1874 Output_data_space* got_plt,
1875 Output_data_space* got_irelative)
9363c7c3 1876 {
3a531937 1877 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
9363c7c3
JY
1878 }
1879
83a01957
HS
1880 // We only need to generate stubs, and hence perform relaxation if we are
1881 // not doing relocatable linking.
1882 virtual bool
1883 do_may_relax() const
1884 { return !parameters->options().relocatable(); }
1885
1886 // Relaxation hook. This is where we do stub generation.
1887 virtual bool
1888 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
1889
1890 void
1891 group_sections(Layout* layout,
1892 section_size_type group_size,
1893 bool stubs_always_after_branch,
1894 const Task* task);
1895
1896 void
1897 scan_reloc_for_stub(const The_relocate_info*, unsigned int,
1898 const Sized_symbol<size>*, unsigned int,
1899 const Symbol_value<size>*,
1900 typename elfcpp::Elf_types<size>::Elf_Swxword,
1901 Address Elf_Addr);
1902
1903 // Make an output section.
1904 Output_section*
1905 do_make_output_section(const char* name, elfcpp::Elf_Word type,
1906 elfcpp::Elf_Xword flags)
1907 { return new The_aarch64_output_section(name, type, flags); }
1908
053a4d68
JY
1909 private:
1910 // The class which scans relocations.
1911 class Scan
1912 {
1913 public:
1914 Scan()
1915 : issued_non_pic_error_(false)
1916 { }
1917
053a4d68
JY
1918 inline void
1919 local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
1920 Sized_relobj_file<size, big_endian>* object,
1921 unsigned int data_shndx,
1922 Output_section* output_section,
1923 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1924 const elfcpp::Sym<size, big_endian>& lsym,
1925 bool is_discarded);
1926
1927 inline void
1928 global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
1929 Sized_relobj_file<size, big_endian>* object,
1930 unsigned int data_shndx,
1931 Output_section* output_section,
1932 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1933 Symbol* gsym);
1934
1935 inline bool
1936 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
9363c7c3
JY
1937 Target_aarch64<size, big_endian>* ,
1938 Sized_relobj_file<size, big_endian>* ,
1939 unsigned int ,
1940 Output_section* ,
1941 const elfcpp::Rela<size, big_endian>& ,
1942 unsigned int r_type,
1943 const elfcpp::Sym<size, big_endian>&);
053a4d68
JY
1944
1945 inline bool
1946 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
9363c7c3
JY
1947 Target_aarch64<size, big_endian>* ,
1948 Sized_relobj_file<size, big_endian>* ,
1949 unsigned int ,
1950 Output_section* ,
1951 const elfcpp::Rela<size, big_endian>& ,
1952 unsigned int r_type,
1953 Symbol* gsym);
053a4d68
JY
1954
1955 private:
1956 static void
1957 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1958 unsigned int r_type);
1959
1960 static void
1961 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1962 unsigned int r_type, Symbol*);
1963
1964 inline bool
1965 possible_function_pointer_reloc(unsigned int r_type);
1966
1967 void
1968 check_non_pic(Relobj*, unsigned int r_type);
1969
9726c3c1
HS
1970 bool
1971 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
1972 unsigned int r_type);
1973
053a4d68
JY
1974 // Whether we have issued an error about a non-PIC compilation.
1975 bool issued_non_pic_error_;
1976 };
1977
1978 // The class which implements relocation.
1979 class Relocate
1980 {
1981 public:
1982 Relocate()
3a531937 1983 : skip_call_tls_get_addr_(false)
053a4d68
JY
1984 { }
1985
1986 ~Relocate()
1987 { }
1988
1989 // Do a relocation. Return false if the caller should not issue
1990 // any warnings about this relocation.
1991 inline bool
1992 relocate(const Relocate_info<size, big_endian>*, Target_aarch64*,
1993 Output_section*,
1994 size_t relnum, const elfcpp::Rela<size, big_endian>&,
1995 unsigned int r_type, const Sized_symbol<size>*,
1996 const Symbol_value<size>*,
1997 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1998 section_size_type);
1999
8e33481e 2000 private:
83a01957
HS
2001 inline typename AArch64_relocate_functions<size, big_endian>::Status
2002 relocate_tls(const Relocate_info<size, big_endian>*,
8e33481e
HS
2003 Target_aarch64<size, big_endian>*,
2004 size_t,
2005 const elfcpp::Rela<size, big_endian>&,
2006 unsigned int r_type, const Sized_symbol<size>*,
2007 const Symbol_value<size>*,
2008 unsigned char*,
2009 typename elfcpp::Elf_types<size>::Elf_Addr);
2010
83a01957 2011 inline typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 2012 tls_gd_to_le(
83a01957 2013 const Relocate_info<size, big_endian>*,
3a531937
JY
2014 Target_aarch64<size, big_endian>*,
2015 const elfcpp::Rela<size, big_endian>&,
2016 unsigned int,
2017 unsigned char*,
2018 const Symbol_value<size>*);
2019
9726c3c1
HS
2020 inline typename AArch64_relocate_functions<size, big_endian>::Status
2021 tls_ld_to_le(
2022 const Relocate_info<size, big_endian>*,
2023 Target_aarch64<size, big_endian>*,
2024 const elfcpp::Rela<size, big_endian>&,
2025 unsigned int,
2026 unsigned char*,
2027 const Symbol_value<size>*);
2028
83a01957 2029 inline typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 2030 tls_ie_to_le(
83a01957 2031 const Relocate_info<size, big_endian>*,
3a531937
JY
2032 Target_aarch64<size, big_endian>*,
2033 const elfcpp::Rela<size, big_endian>&,
2034 unsigned int,
2035 unsigned char*,
2036 const Symbol_value<size>*);
2037
83a01957 2038 inline typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 2039 tls_desc_gd_to_le(
83a01957 2040 const Relocate_info<size, big_endian>*,
3a531937
JY
2041 Target_aarch64<size, big_endian>*,
2042 const elfcpp::Rela<size, big_endian>&,
2043 unsigned int,
2044 unsigned char*,
2045 const Symbol_value<size>*);
2046
83a01957 2047 inline typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 2048 tls_desc_gd_to_ie(
83a01957 2049 const Relocate_info<size, big_endian>*,
3a531937
JY
2050 Target_aarch64<size, big_endian>*,
2051 const elfcpp::Rela<size, big_endian>&,
2052 unsigned int,
2053 unsigned char*,
2054 const Symbol_value<size>*,
2055 typename elfcpp::Elf_types<size>::Elf_Addr,
2056 typename elfcpp::Elf_types<size>::Elf_Addr);
2057
2058 bool skip_call_tls_get_addr_;
2059
8e33481e 2060 }; // End of class Relocate
053a4d68
JY
2061
2062 // A class which returns the size required for a relocation type,
2063 // used while scanning relocs during a relocatable link.
2064 class Relocatable_size_for_reloc
2065 {
2066 public:
2067 unsigned int
2068 get_size_for_reloc(unsigned int, Relobj*);
2069 };
2070
2071 // Adjust TLS relocation type based on the options and whether this
2072 // is a local symbol.
2073 static tls::Tls_optimization
2074 optimize_tls_reloc(bool is_final, int r_type);
2075
2076 // Get the GOT section, creating it if necessary.
2077 Output_data_got_aarch64<size, big_endian>*
2078 got_section(Symbol_table*, Layout*);
2079
2080 // Get the GOT PLT section.
2081 Output_data_space*
2082 got_plt_section() const
2083 {
2084 gold_assert(this->got_plt_ != NULL);
2085 return this->got_plt_;
2086 }
2087
3a531937
JY
2088 // Get the GOT section for TLSDESC entries.
2089 Output_data_got<size, big_endian>*
2090 got_tlsdesc_section() const
2091 {
2092 gold_assert(this->got_tlsdesc_ != NULL);
2093 return this->got_tlsdesc_;
2094 }
2095
053a4d68
JY
2096 // Create the PLT section.
2097 void
2098 make_plt_section(Symbol_table* symtab, Layout* layout);
2099
2100 // Create a PLT entry for a global symbol.
2101 void
2102 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2103
3a531937
JY
2104 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
2105 void
2106 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
2107 Sized_relobj_file<size, big_endian>* relobj,
2108 unsigned int local_sym_index);
2109
2110 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2111 void
2112 define_tls_base_symbol(Symbol_table*, Layout*);
2113
2114 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2115 void
2116 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
2117
2118 // Create a GOT entry for the TLS module index.
2119 unsigned int
2120 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2121 Sized_relobj_file<size, big_endian>* object);
2122
053a4d68
JY
2123 // Get the PLT section.
2124 Output_data_plt_aarch64<size, big_endian>*
2125 plt_section() const
2126 {
2127 gold_assert(this->plt_ != NULL);
2128 return this->plt_;
2129 }
2130
2131 // Get the dynamic reloc section, creating it if necessary.
2132 Reloc_section*
2133 rela_dyn_section(Layout*);
2134
3a531937
JY
2135 // Get the section to use for TLSDESC relocations.
2136 Reloc_section*
2137 rela_tlsdesc_section(Layout*) const;
2138
2139 // Get the section to use for IRELATIVE relocations.
2140 Reloc_section*
2141 rela_irelative_section(Layout*);
2142
053a4d68
JY
2143 // Add a potential copy relocation.
2144 void
2145 copy_reloc(Symbol_table* symtab, Layout* layout,
2146 Sized_relobj_file<size, big_endian>* object,
2147 unsigned int shndx, Output_section* output_section,
2148 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
2149 {
2150 this->copy_relocs_.copy_reloc(symtab, layout,
2151 symtab->get_sized_symbol<size>(sym),
2152 object, shndx, output_section,
2153 reloc, this->rela_dyn_section(layout));
2154 }
2155
2156 // Information about this specific target which we pass to the
2157 // general Target structure.
2158 static const Target::Target_info aarch64_info;
2159
2160 // The types of GOT entries needed for this platform.
2161 // These values are exposed to the ABI in an incremental link.
2162 // Do not renumber existing values without changing the version
2163 // number of the .gnu_incremental_inputs section.
2164 enum Got_type
2165 {
2166 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2167 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
2168 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
2169 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
2170 };
2171
3a531937
JY
2172 // This type is used as the argument to the target specific
2173 // relocation routines. The only target specific reloc is
2174 // R_AARCh64_TLSDESC against a local symbol.
2175 struct Tlsdesc_info
2176 {
2177 Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
2178 unsigned int a_r_sym)
2179 : object(a_object), r_sym(a_r_sym)
2180 { }
2181
2182 // The object in which the local symbol is defined.
2183 Sized_relobj_file<size, big_endian>* object;
2184 // The local symbol index in the object.
2185 unsigned int r_sym;
2186 };
2187
053a4d68
JY
2188 // The GOT section.
2189 Output_data_got_aarch64<size, big_endian>* got_;
2190 // The PLT section.
2191 Output_data_plt_aarch64<size, big_endian>* plt_;
2192 // The GOT PLT section.
2193 Output_data_space* got_plt_;
3a531937
JY
2194 // The GOT section for IRELATIVE relocations.
2195 Output_data_space* got_irelative_;
2196 // The GOT section for TLSDESC relocations.
2197 Output_data_got<size, big_endian>* got_tlsdesc_;
053a4d68
JY
2198 // The _GLOBAL_OFFSET_TABLE_ symbol.
2199 Symbol* global_offset_table_;
2200 // The dynamic reloc section.
2201 Reloc_section* rela_dyn_;
3a531937
JY
2202 // The section to use for IRELATIVE relocs.
2203 Reloc_section* rela_irelative_;
053a4d68
JY
2204 // Relocs saved to avoid a COPY reloc.
2205 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
3a531937
JY
2206 // Offset of the GOT entry for the TLS module index.
2207 unsigned int got_mod_index_offset_;
2208 // We handle R_AARCH64_TLSDESC against a local symbol as a target
2209 // specific relocation. Here we store the object and local symbol
2210 // index for the relocation.
2211 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
2212 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2213 bool tls_base_symbol_defined_;
83a01957
HS
2214 // List of stub_tables
2215 Stub_table_list stub_tables_;
0bf32ea9
JY
2216 // Actual stub group size
2217 section_size_type stub_group_size_;
83a01957 2218 AArch64_input_section_map aarch64_input_section_map_;
8e33481e 2219}; // End of Target_aarch64
053a4d68 2220
3a531937 2221
053a4d68
JY
2222template<>
2223const Target::Target_info Target_aarch64<64, false>::aarch64_info =
2224{
2225 64, // size
2226 false, // is_big_endian
2227 elfcpp::EM_AARCH64, // machine_code
2228 false, // has_make_symbol
2229 false, // has_resolve
2230 false, // has_code_fill
2231 true, // is_default_stack_executable
9726c3c1 2232 true, // can_icf_inline_merge_sections
053a4d68
JY
2233 '\0', // wrap_char
2234 "/lib/ld.so.1", // program interpreter
2235 0x400000, // default_text_segment_address
2236 0x1000, // abi_pagesize (overridable by -z max-page-size)
2237 0x1000, // common_pagesize (overridable by -z common-page-size)
2238 false, // isolate_execinstr
2239 0, // rosegment_gap
2240 elfcpp::SHN_UNDEF, // small_common_shndx
2241 elfcpp::SHN_UNDEF, // large_common_shndx
2242 0, // small_common_section_flags
2243 0, // large_common_section_flags
2244 NULL, // attributes_section
2245 NULL, // attributes_vendor
2246 "_start" // entry_symbol_name
2247};
2248
2249template<>
2250const Target::Target_info Target_aarch64<32, false>::aarch64_info =
2251{
2252 32, // size
2253 false, // is_big_endian
2254 elfcpp::EM_AARCH64, // machine_code
2255 false, // has_make_symbol
2256 false, // has_resolve
2257 false, // has_code_fill
2258 true, // is_default_stack_executable
2259 false, // can_icf_inline_merge_sections
2260 '\0', // wrap_char
2261 "/lib/ld.so.1", // program interpreter
2262 0x400000, // default_text_segment_address
2263 0x1000, // abi_pagesize (overridable by -z max-page-size)
2264 0x1000, // common_pagesize (overridable by -z common-page-size)
2265 false, // isolate_execinstr
2266 0, // rosegment_gap
2267 elfcpp::SHN_UNDEF, // small_common_shndx
2268 elfcpp::SHN_UNDEF, // large_common_shndx
2269 0, // small_common_section_flags
2270 0, // large_common_section_flags
2271 NULL, // attributes_section
2272 NULL, // attributes_vendor
2273 "_start" // entry_symbol_name
2274};
2275
2276template<>
2277const Target::Target_info Target_aarch64<64, true>::aarch64_info =
2278{
2279 64, // size
2280 true, // is_big_endian
2281 elfcpp::EM_AARCH64, // machine_code
2282 false, // has_make_symbol
2283 false, // has_resolve
2284 false, // has_code_fill
2285 true, // is_default_stack_executable
9726c3c1 2286 true, // can_icf_inline_merge_sections
053a4d68
JY
2287 '\0', // wrap_char
2288 "/lib/ld.so.1", // program interpreter
2289 0x400000, // default_text_segment_address
2290 0x1000, // abi_pagesize (overridable by -z max-page-size)
2291 0x1000, // common_pagesize (overridable by -z common-page-size)
2292 false, // isolate_execinstr
2293 0, // rosegment_gap
2294 elfcpp::SHN_UNDEF, // small_common_shndx
2295 elfcpp::SHN_UNDEF, // large_common_shndx
2296 0, // small_common_section_flags
2297 0, // large_common_section_flags
2298 NULL, // attributes_section
2299 NULL, // attributes_vendor
2300 "_start" // entry_symbol_name
2301};
2302
2303template<>
2304const Target::Target_info Target_aarch64<32, true>::aarch64_info =
2305{
2306 32, // size
2307 true, // is_big_endian
2308 elfcpp::EM_AARCH64, // machine_code
2309 false, // has_make_symbol
2310 false, // has_resolve
2311 false, // has_code_fill
2312 true, // is_default_stack_executable
2313 false, // can_icf_inline_merge_sections
2314 '\0', // wrap_char
2315 "/lib/ld.so.1", // program interpreter
2316 0x400000, // default_text_segment_address
2317 0x1000, // abi_pagesize (overridable by -z max-page-size)
2318 0x1000, // common_pagesize (overridable by -z common-page-size)
2319 false, // isolate_execinstr
2320 0, // rosegment_gap
2321 elfcpp::SHN_UNDEF, // small_common_shndx
2322 elfcpp::SHN_UNDEF, // large_common_shndx
2323 0, // small_common_section_flags
2324 0, // large_common_section_flags
2325 NULL, // attributes_section
2326 NULL, // attributes_vendor
2327 "_start" // entry_symbol_name
2328};
2329
2330// Get the GOT section, creating it if necessary.
2331
2332template<int size, bool big_endian>
2333Output_data_got_aarch64<size, big_endian>*
2334Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
9363c7c3 2335 Layout* layout)
053a4d68
JY
2336{
2337 if (this->got_ == NULL)
2338 {
2339 gold_assert(symtab != NULL && layout != NULL);
2340
2341 // When using -z now, we can treat .got.plt as a relro section.
2342 // Without -z now, it is modified after program startup by lazy
2343 // PLT relocations.
2344 bool is_got_plt_relro = parameters->options().now();
2345 Output_section_order got_order = (is_got_plt_relro
2346 ? ORDER_RELRO
2347 : ORDER_RELRO_LAST);
2348 Output_section_order got_plt_order = (is_got_plt_relro
2349 ? ORDER_RELRO
2350 : ORDER_NON_RELRO_FIRST);
2351
2352 // Layout of .got and .got.plt sections.
2353 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
2354 // ...
2355 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
2356 // .gotplt[1] reserved for ld.so (resolver)
2357 // .gotplt[2] reserved
2358
2359 // Generate .got section.
2360 this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
9363c7c3 2361 layout);
053a4d68 2362 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
9363c7c3
JY
2363 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
2364 this->got_, got_order, true);
053a4d68
JY
2365 // The first word of GOT is reserved for the address of .dynamic.
2366 // We put 0 here now. The value will be replaced later in
2367 // Output_data_got_aarch64::do_write.
2368 this->got_->add_constant(0);
2369
2370 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2371 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
2372 // even if there is a .got.plt section.
2373 this->global_offset_table_ =
9363c7c3
JY
2374 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2375 Symbol_table::PREDEFINED,
2376 this->got_,
2377 0, 0, elfcpp::STT_OBJECT,
2378 elfcpp::STB_LOCAL,
2379 elfcpp::STV_HIDDEN, 0,
2380 false, false);
053a4d68
JY
2381
2382 // Generate .got.plt section.
2383 this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
2384 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
9363c7c3
JY
2385 (elfcpp::SHF_ALLOC
2386 | elfcpp::SHF_WRITE),
2387 this->got_plt_, got_plt_order,
2388 is_got_plt_relro);
053a4d68
JY
2389
2390 // The first three entries are reserved.
9363c7c3
JY
2391 this->got_plt_->set_current_data_size(
2392 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
053a4d68 2393
3a531937
JY
2394 // If there are any IRELATIVE relocations, they get GOT entries
2395 // in .got.plt after the jump slot entries.
2396 this->got_irelative_ = new Output_data_space(size / 8,
2397 "** GOT IRELATIVE PLT");
2398 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2399 (elfcpp::SHF_ALLOC
2400 | elfcpp::SHF_WRITE),
2401 this->got_irelative_,
2402 got_plt_order,
2403 is_got_plt_relro);
2404
2405 // If there are any TLSDESC relocations, they get GOT entries in
2406 // .got.plt after the jump slot and IRELATIVE entries.
2407 this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
2408 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2409 (elfcpp::SHF_ALLOC
2410 | elfcpp::SHF_WRITE),
2411 this->got_tlsdesc_,
2412 got_plt_order,
2413 is_got_plt_relro);
2414
053a4d68 2415 if (!is_got_plt_relro)
9363c7c3
JY
2416 {
2417 // Those bytes can go into the relro segment.
2418 layout->increase_relro(
2419 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
2420 }
053a4d68
JY
2421
2422 }
2423 return this->got_;
2424}
2425
2426// Get the dynamic reloc section, creating it if necessary.
2427
2428template<int size, bool big_endian>
2429typename Target_aarch64<size, big_endian>::Reloc_section*
2430Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
2431{
2432 if (this->rela_dyn_ == NULL)
2433 {
2434 gold_assert(layout != NULL);
2435 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2436 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2437 elfcpp::SHF_ALLOC, this->rela_dyn_,
2438 ORDER_DYNAMIC_RELOCS, false);
2439 }
2440 return this->rela_dyn_;
2441}
2442
3a531937
JY
2443// Get the section to use for IRELATIVE relocs, creating it if
2444// necessary. These go in .rela.dyn, but only after all other dynamic
2445// relocations. They need to follow the other dynamic relocations so
2446// that they can refer to global variables initialized by those
2447// relocs.
2448
2449template<int size, bool big_endian>
2450typename Target_aarch64<size, big_endian>::Reloc_section*
2451Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
2452{
2453 if (this->rela_irelative_ == NULL)
2454 {
2455 // Make sure we have already created the dynamic reloc section.
2456 this->rela_dyn_section(layout);
2457 this->rela_irelative_ = new Reloc_section(false);
2458 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2459 elfcpp::SHF_ALLOC, this->rela_irelative_,
2460 ORDER_DYNAMIC_RELOCS, false);
2461 gold_assert(this->rela_dyn_->output_section()
2462 == this->rela_irelative_->output_section());
2463 }
2464 return this->rela_irelative_;
2465}
2466
2467
83a01957
HS
2468// do_make_elf_object to override the same function in the base class. We need
2469// to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
2470// store backend specific information. Hence we need to have our own ELF object
2471// creation.
2472
2473template<int size, bool big_endian>
2474Object*
2475Target_aarch64<size, big_endian>::do_make_elf_object(
2476 const std::string& name,
2477 Input_file* input_file,
2478 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2479{
2480 int et = ehdr.get_e_type();
2481 // ET_EXEC files are valid input for --just-symbols/-R,
2482 // and we treat them as relocatable objects.
2483 if (et == elfcpp::ET_EXEC && input_file->just_symbols())
2484 return Sized_target<size, big_endian>::do_make_elf_object(
2485 name, input_file, offset, ehdr);
2486 else if (et == elfcpp::ET_REL)
2487 {
2488 AArch64_relobj<size, big_endian>* obj =
2489 new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
2490 obj->setup();
2491 return obj;
2492 }
2493 else if (et == elfcpp::ET_DYN)
2494 {
2495 // Keep base implementation.
2496 Sized_dynobj<size, big_endian>* obj =
2497 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2498 obj->setup();
2499 return obj;
2500 }
2501 else
2502 {
2503 gold_error(_("%s: unsupported ELF file type %d"),
2504 name.c_str(), et);
2505 return NULL;
2506 }
2507}
2508
2509
2510// Scan a relocation for stub generation.
2511
2512template<int size, bool big_endian>
2513void
2514Target_aarch64<size, big_endian>::scan_reloc_for_stub(
2515 const Relocate_info<size, big_endian>* relinfo,
2516 unsigned int r_type,
2517 const Sized_symbol<size>* gsym,
2518 unsigned int r_sym,
2519 const Symbol_value<size>* psymval,
2520 typename elfcpp::Elf_types<size>::Elf_Swxword addend,
2521 Address address)
2522{
2523 const AArch64_relobj<size, big_endian>* aarch64_relobj =
2524 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
2525
2526 Symbol_value<size> symval;
2527 if (gsym != NULL)
2528 {
2529 const AArch64_reloc_property* arp = aarch64_reloc_property_table->
2530 get_reloc_property(r_type);
2531 if (gsym->use_plt_offset(arp->reference_flags()))
2532 {
2533 // This uses a PLT, change the symbol value.
2534 symval.set_output_value(this->plt_section()->address()
2535 + gsym->plt_offset());
2536 psymval = &symval;
2537 }
2538 else if (gsym->is_undefined())
2539 // There is no need to generate a stub symbol is undefined.
2540 return;
2541 }
2542
2543 // Get the symbol value.
2544 typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
2545
2546 // Owing to pipelining, the PC relative branches below actually skip
2547 // two instructions when the branch offset is 0.
2548 Address destination = static_cast<Address>(-1);
2549 switch (r_type)
2550 {
2551 case elfcpp::R_AARCH64_CALL26:
2552 case elfcpp::R_AARCH64_JUMP26:
2553 destination = value + addend;
2554 break;
2555 default:
9726c3c1 2556 gold_unreachable();
83a01957
HS
2557 }
2558
2559 typename The_reloc_stub::Stub_type stub_type = The_reloc_stub::
2560 stub_type_for_reloc(r_type, address, destination);
2561 if (stub_type == The_reloc_stub::ST_NONE)
2562 return ;
2563
2564 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
2565 gold_assert(stub_table != NULL);
2566
2567 The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
2568 The_reloc_stub* stub = stub_table->find_reloc_stub(key);
2569 if (stub == NULL)
2570 {
2571 stub = new The_reloc_stub(stub_type);
2572 stub_table->add_reloc_stub(stub, key);
2573 }
2574 stub->set_destination_address(destination);
2575} // End of Target_aarch64::scan_reloc_for_stub
2576
2577
2578// This function scans a relocation section for stub generation.
2579// The template parameter Relocate must be a class type which provides
2580// a single function, relocate(), which implements the machine
2581// specific part of a relocation.
2582
2583// BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
2584// SHT_REL or SHT_RELA.
2585
2586// PRELOCS points to the relocation data. RELOC_COUNT is the number
2587// of relocs. OUTPUT_SECTION is the output section.
2588// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
2589// mapped to output offsets.
2590
2591// VIEW is the section data, VIEW_ADDRESS is its memory address, and
2592// VIEW_SIZE is the size. These refer to the input section, unless
2593// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
2594// the output section.
2595
2596template<int size, bool big_endian>
2597template<int sh_type>
2598void inline
2599Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
2600 const Relocate_info<size, big_endian>* relinfo,
2601 const unsigned char* prelocs,
2602 size_t reloc_count,
2603 Output_section* /*output_section*/,
2604 bool /*needs_special_offset_handling*/,
2605 const unsigned char* /*view*/,
2606 Address view_address,
2607 section_size_type)
2608{
2609 typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
2610
2611 const int reloc_size =
2612 Reloc_types<sh_type,size,big_endian>::reloc_size;
2613 AArch64_relobj<size, big_endian>* object =
2614 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
2615 unsigned int local_count = object->local_symbol_count();
2616
2617 gold::Default_comdat_behavior default_comdat_behavior;
2618 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
2619
2620 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2621 {
2622 Reltype reloc(prelocs);
2623 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
2624 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2625 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2626 if (r_type != elfcpp::R_AARCH64_CALL26
2627 && r_type != elfcpp::R_AARCH64_JUMP26)
2628 continue;
2629
2630 section_offset_type offset =
2631 convert_to_section_size_type(reloc.get_r_offset());
2632
2633 // Get the addend.
2634 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
2635 reloc.get_r_addend();
2636
2637 const Sized_symbol<size>* sym;
2638 Symbol_value<size> symval;
2639 const Symbol_value<size> *psymval;
2640 bool is_defined_in_discarded_section;
2641 unsigned int shndx;
2642 if (r_sym < local_count)
2643 {
2644 sym = NULL;
2645 psymval = object->local_symbol(r_sym);
2646
2647 // If the local symbol belongs to a section we are discarding,
2648 // and that section is a debug section, try to find the
2649 // corresponding kept section and map this symbol to its
2650 // counterpart in the kept section. The symbol must not
2651 // correspond to a section we are folding.
2652 bool is_ordinary;
2653 shndx = psymval->input_shndx(&is_ordinary);
2654 is_defined_in_discarded_section =
2655 (is_ordinary
2656 && shndx != elfcpp::SHN_UNDEF
2657 && !object->is_section_included(shndx)
2658 && !relinfo->symtab->is_section_folded(object, shndx));
2659
2660 // We need to compute the would-be final value of this local
2661 // symbol.
2662 if (!is_defined_in_discarded_section)
2663 {
2664 typedef Sized_relobj_file<size, big_endian> ObjType;
2665 typename ObjType::Compute_final_local_value_status status =
2666 object->compute_final_local_value(r_sym, psymval, &symval,
2667 relinfo->symtab);
2668 if (status == ObjType::CFLV_OK)
2669 {
2670 // Currently we cannot handle a branch to a target in
2671 // a merged section. If this is the case, issue an error
2672 // and also free the merge symbol value.
2673 if (!symval.has_output_value())
2674 {
2675 const std::string& section_name =
2676 object->section_name(shndx);
2677 object->error(_("cannot handle branch to local %u "
2678 "in a merged section %s"),
2679 r_sym, section_name.c_str());
2680 }
2681 psymval = &symval;
2682 }
2683 else
2684 {
2685 // We cannot determine the final value.
2686 continue;
2687 }
2688 }
2689 }
2690 else
2691 {
2692 const Symbol* gsym;
2693 gsym = object->global_symbol(r_sym);
2694 gold_assert(gsym != NULL);
2695 if (gsym->is_forwarder())
2696 gsym = relinfo->symtab->resolve_forwards(gsym);
2697
2698 sym = static_cast<const Sized_symbol<size>*>(gsym);
2699 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
2700 symval.set_output_symtab_index(sym->symtab_index());
2701 else
2702 symval.set_no_output_symtab_entry();
2703
2704 // We need to compute the would-be final value of this global
2705 // symbol.
2706 const Symbol_table* symtab = relinfo->symtab;
2707 const Sized_symbol<size>* sized_symbol =
2708 symtab->get_sized_symbol<size>(gsym);
2709 Symbol_table::Compute_final_value_status status;
2710 typename elfcpp::Elf_types<size>::Elf_Addr value =
2711 symtab->compute_final_value<size>(sized_symbol, &status);
2712
2713 // Skip this if the symbol has not output section.
2714 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
2715 continue;
2716 symval.set_output_value(value);
2717
2718 if (gsym->type() == elfcpp::STT_TLS)
2719 symval.set_is_tls_symbol();
2720 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2721 symval.set_is_ifunc_symbol();
2722 psymval = &symval;
2723
2724 is_defined_in_discarded_section =
2725 (gsym->is_defined_in_discarded_section()
2726 && gsym->is_undefined());
2727 shndx = 0;
2728 }
2729
2730 Symbol_value<size> symval2;
2731 if (is_defined_in_discarded_section)
2732 {
2733 if (comdat_behavior == CB_UNDETERMINED)
2734 {
2735 std::string name = object->section_name(relinfo->data_shndx);
2736 comdat_behavior = default_comdat_behavior.get(name.c_str());
2737 }
2738 if (comdat_behavior == CB_PRETEND)
2739 {
2740 bool found;
2741 typename elfcpp::Elf_types<size>::Elf_Addr value =
2742 object->map_to_kept_section(shndx, &found);
2743 if (found)
2744 symval2.set_output_value(value + psymval->input_value());
2745 else
2746 symval2.set_output_value(0);
2747 }
2748 else
2749 {
2750 if (comdat_behavior == CB_WARNING)
2751 gold_warning_at_location(relinfo, i, offset,
2752 _("relocation refers to discarded "
2753 "section"));
2754 symval2.set_output_value(0);
2755 }
2756 symval2.set_no_output_symtab_entry();
2757 psymval = &symval2;
2758 }
2759
2760 // If symbol is a section symbol, we don't know the actual type of
2761 // destination. Give up.
2762 if (psymval->is_section_symbol())
2763 continue;
2764
2765 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
2766 addend, view_address + offset);
2767 } // End of iterating relocs in a section
2768} // End of Target_aarch64::scan_reloc_section_for_stubs
2769
2770
2771// Scan an input section for stub generation.
2772
2773template<int size, bool big_endian>
2774void
2775Target_aarch64<size, big_endian>::scan_section_for_stubs(
2776 const Relocate_info<size, big_endian>* relinfo,
2777 unsigned int sh_type,
2778 const unsigned char* prelocs,
2779 size_t reloc_count,
2780 Output_section* output_section,
2781 bool needs_special_offset_handling,
2782 const unsigned char* view,
2783 Address view_address,
2784 section_size_type view_size)
2785{
2786 gold_assert(sh_type == elfcpp::SHT_RELA);
2787 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
2788 relinfo,
2789 prelocs,
2790 reloc_count,
2791 output_section,
2792 needs_special_offset_handling,
2793 view,
2794 view_address,
2795 view_size);
2796}
2797
2798
2799// Relocate a single stub.
2800
2801template<int size, bool big_endian>
2802void Target_aarch64<size, big_endian>::
2803relocate_stub(The_reloc_stub* stub,
2804 const The_relocate_info*,
2805 Output_section*,
2806 unsigned char* view,
2807 Address address,
2808 section_size_type)
2809{
2810 typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
2811 typedef typename The_reloc_functions::Status The_reloc_functions_status;
2812 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
2813
2814 Insntype* ip = reinterpret_cast<Insntype*>(view);
2815 int insn_number = stub->stub_insn_number();
2816 const uint32_t* insns = stub->stub_insns();
2817 // Check the insns are really those stub insns.
2818 for (int i = 0; i < insn_number; ++i)
2819 {
2820 Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
2821 gold_assert(((uint32_t)insn == insns[i+1]));
2822 }
2823
2824 Address dest = stub->destination_address();
2825
2826 switch(stub->stub_type())
2827 {
2828 case The_reloc_stub::ST_ADRP_BRANCH:
2829 {
2830 // 1st reloc is ADR_PREL_PG_HI21
2831 The_reloc_functions_status status =
2832 The_reloc_functions::adrp(view, dest, address);
2833 // An error should never arise in the above step. If so, please
2834 // check 'aarch64_valid_for_adrp_p'.
2835 gold_assert(status == The_reloc_functions::STATUS_OKAY);
2836
2837 // 2nd reloc is ADD_ABS_LO12_NC
2838 const AArch64_reloc_property* arp =
2839 aarch64_reloc_property_table->get_reloc_property(
2840 elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
2841 gold_assert(arp != NULL);
2842 status = The_reloc_functions::template
2843 rela_general<32>(view + 4, dest, 0, arp);
2844 // An error should never arise, it is an "_NC" relocation.
2845 gold_assert(status == The_reloc_functions::STATUS_OKAY);
2846 }
2847 break;
2848
2849 case The_reloc_stub::ST_LONG_BRANCH_ABS:
2850 // 1st reloc is R_AARCH64_PREL64, at offset 8
2851 elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
2852 break;
2853
2854 case The_reloc_stub::ST_LONG_BRANCH_PCREL:
2855 {
2856 // "PC" calculation is the 2nd insn in the stub.
2857 uint64_t offset = dest - (address + 4);
2858 // Offset is placed at offset 4 and 5.
2859 elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
2860 }
2861 break;
2862
2863 default:
9726c3c1 2864 gold_unreachable();
83a01957
HS
2865 }
2866}
2867
2868
053a4d68
JY
2869// A class to handle the PLT data.
2870// This is an abstract base class that handles most of the linker details
2871// but does not know the actual contents of PLT entries. The derived
2872// classes below fill in those details.
2873
2874template<int size, bool big_endian>
2875class Output_data_plt_aarch64 : public Output_section_data
2876{
2877 public:
2878 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
2879 Reloc_section;
2880 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2881
2882 Output_data_plt_aarch64(Layout* layout,
9363c7c3 2883 uint64_t addralign,
3a531937
JY
2884 Output_data_got_aarch64<size, big_endian>* got,
2885 Output_data_space* got_plt,
2886 Output_data_space* got_irelative)
9726c3c1 2887 : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
3a531937
JY
2888 got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
2889 count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
053a4d68
JY
2890 { this->init(layout); }
2891
2892 // Initialize the PLT section.
2893 void
2894 init(Layout* layout);
2895
2896 // Add an entry to the PLT.
2897 void
9726c3c1
HS
2898 add_entry(Symbol_table*, Layout*, Symbol* gsym);
2899
2900 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
2901 unsigned int
2902 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
2903 Sized_relobj_file<size, big_endian>* relobj,
2904 unsigned int local_sym_index);
2905
2906 // Add the relocation for a PLT entry.
2907 void
2908 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
2909 unsigned int got_offset);
053a4d68 2910
3a531937
JY
2911 // Add the reserved TLSDESC_PLT entry to the PLT.
2912 void
2913 reserve_tlsdesc_entry(unsigned int got_offset)
2914 { this->tlsdesc_got_offset_ = got_offset; }
2915
2916 // Return true if a TLSDESC_PLT entry has been reserved.
2917 bool
2918 has_tlsdesc_entry() const
2919 { return this->tlsdesc_got_offset_ != -1U; }
2920
2921 // Return the GOT offset for the reserved TLSDESC_PLT entry.
2922 unsigned int
2923 get_tlsdesc_got_offset() const
2924 { return this->tlsdesc_got_offset_; }
2925
2926 // Return the PLT offset of the reserved TLSDESC_PLT entry.
2927 unsigned int
2928 get_tlsdesc_plt_offset() const
2929 {
2930 return (this->first_plt_entry_offset() +
2931 (this->count_ + this->irelative_count_)
2932 * this->get_plt_entry_size());
2933 }
2934
053a4d68
JY
2935 // Return the .rela.plt section data.
2936 Reloc_section*
2937 rela_plt()
2938 { return this->rel_; }
2939
3a531937
JY
2940 // Return where the TLSDESC relocations should go.
2941 Reloc_section*
2942 rela_tlsdesc(Layout*);
2943
2944 // Return where the IRELATIVE relocations should go in the PLT
2945 // relocations.
2946 Reloc_section*
2947 rela_irelative(Symbol_table*, Layout*);
2948
9363c7c3
JY
2949 // Return whether we created a section for IRELATIVE relocations.
2950 bool
2951 has_irelative_section() const
2952 { return this->irelative_rel_ != NULL; }
2953
053a4d68
JY
2954 // Return the number of PLT entries.
2955 unsigned int
2956 entry_count() const
3a531937 2957 { return this->count_ + this->irelative_count_; }
053a4d68
JY
2958
2959 // Return the offset of the first non-reserved PLT entry.
2960 unsigned int
3a531937 2961 first_plt_entry_offset() const
053a4d68
JY
2962 { return this->do_first_plt_entry_offset(); }
2963
2964 // Return the size of a PLT entry.
2965 unsigned int
2966 get_plt_entry_size() const
2967 { return this->do_get_plt_entry_size(); }
2968
3a531937
JY
2969 // Return the reserved tlsdesc entry size.
2970 unsigned int
2971 get_plt_tlsdesc_entry_size() const
2972 { return this->do_get_plt_tlsdesc_entry_size(); }
2973
9363c7c3
JY
2974 // Return the PLT address to use for a global symbol.
2975 uint64_t
2976 address_for_global(const Symbol*);
2977
2978 // Return the PLT address to use for a local symbol.
2979 uint64_t
2980 address_for_local(const Relobj*, unsigned int symndx);
2981
053a4d68
JY
2982 protected:
2983 // Fill in the first PLT entry.
2984 void
2985 fill_first_plt_entry(unsigned char* pov,
2986 Address got_address,
2987 Address plt_address)
2988 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
2989
2990 // Fill in a normal PLT entry.
2991 void
2992 fill_plt_entry(unsigned char* pov,
2993 Address got_address,
2994 Address plt_address,
2995 unsigned int got_offset,
2996 unsigned int plt_offset)
2997 {
2998 this->do_fill_plt_entry(pov, got_address, plt_address,
2999 got_offset, plt_offset);
3000 }
3001
3a531937
JY
3002 // Fill in the reserved TLSDESC PLT entry.
3003 void
3004 fill_tlsdesc_entry(unsigned char* pov,
3005 Address gotplt_address,
3006 Address plt_address,
3007 Address got_base,
3008 unsigned int tlsdesc_got_offset,
3009 unsigned int plt_offset)
3010 {
3011 this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
3012 tlsdesc_got_offset, plt_offset);
3013 }
3014
053a4d68
JY
3015 virtual unsigned int
3016 do_first_plt_entry_offset() const = 0;
3017
3018 virtual unsigned int
3019 do_get_plt_entry_size() const = 0;
3020
3a531937
JY
3021 virtual unsigned int
3022 do_get_plt_tlsdesc_entry_size() const = 0;
3023
053a4d68
JY
3024 virtual void
3025 do_fill_first_plt_entry(unsigned char* pov,
3026 Address got_addr,
3027 Address plt_addr) = 0;
3028
3029 virtual void
3030 do_fill_plt_entry(unsigned char* pov,
3031 Address got_address,
3032 Address plt_address,
3033 unsigned int got_offset,
3034 unsigned int plt_offset) = 0;
3035
3a531937
JY
3036 virtual void
3037 do_fill_tlsdesc_entry(unsigned char* pov,
3038 Address gotplt_address,
3039 Address plt_address,
3040 Address got_base,
3041 unsigned int tlsdesc_got_offset,
3042 unsigned int plt_offset) = 0;
3043
053a4d68
JY
3044 void
3045 do_adjust_output_section(Output_section* os);
3046
3047 // Write to a map file.
3048 void
3049 do_print_to_mapfile(Mapfile* mapfile) const
3050 { mapfile->print_output_data(this, _("** PLT")); }
3051
3052 private:
3053 // Set the final size.
3054 void
3055 set_final_data_size();
3056
3057 // Write out the PLT data.
3058 void
3059 do_write(Output_file*);
3060
3061 // The reloc section.
3062 Reloc_section* rel_;
3a531937
JY
3063
3064 // The TLSDESC relocs, if necessary. These must follow the regular
3065 // PLT relocs.
3066 Reloc_section* tlsdesc_rel_;
3067
9363c7c3
JY
3068 // The IRELATIVE relocs, if necessary. These must follow the
3069 // regular PLT relocations.
3070 Reloc_section* irelative_rel_;
3a531937 3071
053a4d68
JY
3072 // The .got section.
3073 Output_data_got_aarch64<size, big_endian>* got_;
3a531937 3074
053a4d68
JY
3075 // The .got.plt section.
3076 Output_data_space* got_plt_;
3a531937
JY
3077
3078 // The part of the .got.plt section used for IRELATIVE relocs.
3079 Output_data_space* got_irelative_;
3080
053a4d68
JY
3081 // The number of PLT entries.
3082 unsigned int count_;
3a531937 3083
fa89cc82 3084 // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
3a531937
JY
3085 // follow the regular PLT entries.
3086 unsigned int irelative_count_;
3087
3088 // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
3089 // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
3090 // indicates an offset is not allocated.
3091 unsigned int tlsdesc_got_offset_;
053a4d68
JY
3092};
3093
3094// Initialize the PLT section.
3095
3096template<int size, bool big_endian>
3097void
3098Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
3099{
3100 this->rel_ = new Reloc_section(false);
3101 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3102 elfcpp::SHF_ALLOC, this->rel_,
3103 ORDER_DYNAMIC_PLT_RELOCS, false);
3104}
3105
3106template<int size, bool big_endian>
3107void
3108Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
3109 Output_section* os)
3110{
3111 os->set_entsize(this->get_plt_entry_size());
3112}
3113
3114// Add an entry to the PLT.
3115
3116template<int size, bool big_endian>
3117void
9726c3c1
HS
3118Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
3119 Layout* layout, Symbol* gsym)
053a4d68
JY
3120{
3121 gold_assert(!gsym->has_plt_offset());
9363c7c3 3122
9726c3c1
HS
3123 unsigned int* pcount;
3124 unsigned int plt_reserved;
3125 Output_section_data_build* got;
9363c7c3 3126
9726c3c1
HS
3127 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3128 && gsym->can_use_relative_reloc(false))
3129 {
3130 pcount = &this->irelative_count_;
3131 plt_reserved = 0;
3132 got = this->got_irelative_;
3133 }
3134 else
3135 {
3136 pcount = &this->count_;
3137 plt_reserved = this->first_plt_entry_offset();
3138 got = this->got_plt_;
3139 }
9363c7c3 3140
9726c3c1
HS
3141 gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
3142 + plt_reserved);
3143
3144 ++*pcount;
3145
3146 section_offset_type got_offset = got->current_data_size();
9363c7c3
JY
3147
3148 // Every PLT entry needs a GOT entry which points back to the PLT
3149 // entry (this will be changed by the dynamic linker, normally
3150 // lazily when the function is called).
9726c3c1 3151 got->set_current_data_size(got_offset + size / 8);
9363c7c3
JY
3152
3153 // Every PLT entry needs a reloc.
9726c3c1 3154 this->add_relocation(symtab, layout, gsym, got_offset);
9363c7c3
JY
3155
3156 // Note that we don't need to save the symbol. The contents of the
3157 // PLT are independent of which symbols are used. The symbols only
3158 // appear in the relocations.
3159}
3160
9726c3c1
HS
3161// Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
3162// the PLT offset.
3163
3164template<int size, bool big_endian>
3165unsigned int
3166Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
3167 Symbol_table* symtab,
3168 Layout* layout,
3169 Sized_relobj_file<size, big_endian>* relobj,
3170 unsigned int local_sym_index)
3171{
3172 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
3173 ++this->irelative_count_;
3174
3175 section_offset_type got_offset = this->got_irelative_->current_data_size();
3176
3177 // Every PLT entry needs a GOT entry which points back to the PLT
3178 // entry.
3179 this->got_irelative_->set_current_data_size(got_offset + size / 8);
3180
3181 // Every PLT entry needs a reloc.
3182 Reloc_section* rela = this->rela_irelative(symtab, layout);
3183 rela->add_symbolless_local_addend(relobj, local_sym_index,
3184 elfcpp::R_AARCH64_IRELATIVE,
3185 this->got_irelative_, got_offset, 0);
3186
3187 return plt_offset;
3188}
3189
3190// Add the relocation for a PLT entry.
3191
3192template<int size, bool big_endian>
3193void
3194Output_data_plt_aarch64<size, big_endian>::add_relocation(
3195 Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
3196{
3197 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3198 && gsym->can_use_relative_reloc(false))
3199 {
3200 Reloc_section* rela = this->rela_irelative(symtab, layout);
3201 rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
3202 this->got_irelative_, got_offset, 0);
3203 }
3204 else
3205 {
3206 gsym->set_needs_dynsym_entry();
3207 this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
3208 got_offset, 0);
3209 }
3210}
3211
3a531937
JY
3212// Return where the TLSDESC relocations should go, creating it if
3213// necessary. These follow the JUMP_SLOT relocations.
3214
3215template<int size, bool big_endian>
3216typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
3217Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
3218{
3219 if (this->tlsdesc_rel_ == NULL)
3220 {
3221 this->tlsdesc_rel_ = new Reloc_section(false);
3222 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3223 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
3224 ORDER_DYNAMIC_PLT_RELOCS, false);
3225 gold_assert(this->tlsdesc_rel_->output_section()
3226 == this->rel_->output_section());
3227 }
3228 return this->tlsdesc_rel_;
3229}
3230
3231// Return where the IRELATIVE relocations should go in the PLT. These
3232// follow the JUMP_SLOT and the TLSDESC relocations.
3233
3234template<int size, bool big_endian>
3235typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
3236Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
3237 Layout* layout)
3238{
3239 if (this->irelative_rel_ == NULL)
3240 {
3241 // Make sure we have a place for the TLSDESC relocations, in
3242 // case we see any later on.
3243 this->rela_tlsdesc(layout);
3244 this->irelative_rel_ = new Reloc_section(false);
3245 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3246 elfcpp::SHF_ALLOC, this->irelative_rel_,
3247 ORDER_DYNAMIC_PLT_RELOCS, false);
3248 gold_assert(this->irelative_rel_->output_section()
3249 == this->rel_->output_section());
3250
3251 if (parameters->doing_static_link())
3252 {
3253 // A statically linked executable will only have a .rela.plt
3254 // section to hold R_AARCH64_IRELATIVE relocs for
3255 // STT_GNU_IFUNC symbols. The library will use these
3256 // symbols to locate the IRELATIVE relocs at program startup
3257 // time.
3258 symtab->define_in_output_data("__rela_iplt_start", NULL,
3259 Symbol_table::PREDEFINED,
3260 this->irelative_rel_, 0, 0,
3261 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3262 elfcpp::STV_HIDDEN, 0, false, true);
3263 symtab->define_in_output_data("__rela_iplt_end", NULL,
3264 Symbol_table::PREDEFINED,
3265 this->irelative_rel_, 0, 0,
3266 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3267 elfcpp::STV_HIDDEN, 0, true, true);
3268 }
3269 }
3270 return this->irelative_rel_;
3271}
3272
9363c7c3
JY
3273// Return the PLT address to use for a global symbol.
3274
3275template<int size, bool big_endian>
3276uint64_t
3277Output_data_plt_aarch64<size, big_endian>::address_for_global(
3278 const Symbol* gsym)
3279{
3280 uint64_t offset = 0;
3281 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3282 && gsym->can_use_relative_reloc(false))
3283 offset = (this->first_plt_entry_offset() +
3284 this->count_ * this->get_plt_entry_size());
3285 return this->address() + offset + gsym->plt_offset();
3286}
3287
3288// Return the PLT address to use for a local symbol. These are always
3289// IRELATIVE relocs.
3290
3291template<int size, bool big_endian>
3292uint64_t
3293Output_data_plt_aarch64<size, big_endian>::address_for_local(
3294 const Relobj* object,
3295 unsigned int r_sym)
3296{
3297 return (this->address()
3298 + this->first_plt_entry_offset()
3299 + this->count_ * this->get_plt_entry_size()
3300 + object->local_plt_offset(r_sym));
053a4d68
JY
3301}
3302
3303// Set the final size.
9363c7c3 3304
053a4d68
JY
3305template<int size, bool big_endian>
3306void
3307Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
3308{
3a531937
JY
3309 unsigned int count = this->count_ + this->irelative_count_;
3310 unsigned int extra_size = 0;
3311 if (this->has_tlsdesc_entry())
3312 extra_size += this->get_plt_tlsdesc_entry_size();
053a4d68 3313 this->set_data_size(this->first_plt_entry_offset()
3a531937
JY
3314 + count * this->get_plt_entry_size()
3315 + extra_size);
053a4d68
JY
3316}
3317
3318template<int size, bool big_endian>
3319class Output_data_plt_aarch64_standard :
3320 public Output_data_plt_aarch64<size, big_endian>
3321{
3322 public:
3323 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3a531937
JY
3324 Output_data_plt_aarch64_standard(
3325 Layout* layout,
3326 Output_data_got_aarch64<size, big_endian>* got,
3327 Output_data_space* got_plt,
3328 Output_data_space* got_irelative)
053a4d68 3329 : Output_data_plt_aarch64<size, big_endian>(layout,
9363c7c3 3330 size == 32 ? 4 : 8,
3a531937
JY
3331 got, got_plt,
3332 got_irelative)
053a4d68
JY
3333 { }
3334
3335 protected:
3336 // Return the offset of the first non-reserved PLT entry.
3337 virtual unsigned int
3338 do_first_plt_entry_offset() const
3339 { return this->first_plt_entry_size; }
3340
3341 // Return the size of a PLT entry
3342 virtual unsigned int
3343 do_get_plt_entry_size() const
3344 { return this->plt_entry_size; }
3345
3a531937
JY
3346 // Return the size of a tlsdesc entry
3347 virtual unsigned int
3348 do_get_plt_tlsdesc_entry_size() const
3349 { return this->plt_tlsdesc_entry_size; }
3350
053a4d68
JY
3351 virtual void
3352 do_fill_first_plt_entry(unsigned char* pov,
9363c7c3
JY
3353 Address got_address,
3354 Address plt_address);
053a4d68
JY
3355
3356 virtual void
3357 do_fill_plt_entry(unsigned char* pov,
9363c7c3
JY
3358 Address got_address,
3359 Address plt_address,
3360 unsigned int got_offset,
3361 unsigned int plt_offset);
053a4d68 3362
3a531937
JY
3363 virtual void
3364 do_fill_tlsdesc_entry(unsigned char* pov,
3365 Address gotplt_address,
3366 Address plt_address,
3367 Address got_base,
3368 unsigned int tlsdesc_got_offset,
3369 unsigned int plt_offset);
3370
053a4d68
JY
3371 private:
3372 // The size of the first plt entry size.
3373 static const int first_plt_entry_size = 32;
3374 // The size of the plt entry size.
3375 static const int plt_entry_size = 16;
3a531937
JY
3376 // The size of the plt tlsdesc entry size.
3377 static const int plt_tlsdesc_entry_size = 32;
053a4d68
JY
3378 // Template for the first PLT entry.
3379 static const uint32_t first_plt_entry[first_plt_entry_size / 4];
3380 // Template for subsequent PLT entries.
3381 static const uint32_t plt_entry[plt_entry_size / 4];
3a531937
JY
3382 // The reserved TLSDESC entry in the PLT for an executable.
3383 static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
053a4d68
JY
3384};
3385
3386// The first entry in the PLT for an executable.
3387
3388template<>
3389const uint32_t
3390Output_data_plt_aarch64_standard<32, false>::
3391 first_plt_entry[first_plt_entry_size / 4] =
3392{
3393 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3394 0x90000010, /* adrp x16, PLT_GOT+0x8 */
3395 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
3396 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
3397 0xd61f0220, /* br x17 */
3398 0xd503201f, /* nop */
3399 0xd503201f, /* nop */
3400 0xd503201f, /* nop */
3401};
3402
83a01957 3403
053a4d68
JY
3404template<>
3405const uint32_t
3406Output_data_plt_aarch64_standard<32, true>::
3407 first_plt_entry[first_plt_entry_size / 4] =
3408{
3409 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3410 0x90000010, /* adrp x16, PLT_GOT+0x8 */
3411 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
3412 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
3413 0xd61f0220, /* br x17 */
3414 0xd503201f, /* nop */
3415 0xd503201f, /* nop */
3416 0xd503201f, /* nop */
3417};
3418
83a01957 3419
053a4d68
JY
3420template<>
3421const uint32_t
3422Output_data_plt_aarch64_standard<64, false>::
3423 first_plt_entry[first_plt_entry_size / 4] =
3424{
3425 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3426 0x90000010, /* adrp x16, PLT_GOT+16 */
3427 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
3428 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
3429 0xd61f0220, /* br x17 */
3430 0xd503201f, /* nop */
3431 0xd503201f, /* nop */
3432 0xd503201f, /* nop */
3433};
3434
83a01957 3435
053a4d68
JY
3436template<>
3437const uint32_t
3438Output_data_plt_aarch64_standard<64, true>::
3439 first_plt_entry[first_plt_entry_size / 4] =
3440{
3441 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3442 0x90000010, /* adrp x16, PLT_GOT+16 */
3443 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
3444 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
3445 0xd61f0220, /* br x17 */
3446 0xd503201f, /* nop */
3447 0xd503201f, /* nop */
3448 0xd503201f, /* nop */
3449};
3450
83a01957 3451
053a4d68
JY
3452template<>
3453const uint32_t
3454Output_data_plt_aarch64_standard<32, false>::
3455 plt_entry[plt_entry_size / 4] =
3456{
3457 0x90000010, /* adrp x16, PLTGOT + n * 4 */
3458 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
3459 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
3460 0xd61f0220, /* br x17. */
3461};
3462
83a01957 3463
053a4d68
JY
3464template<>
3465const uint32_t
3466Output_data_plt_aarch64_standard<32, true>::
3467 plt_entry[plt_entry_size / 4] =
3468{
3469 0x90000010, /* adrp x16, PLTGOT + n * 4 */
3470 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
3471 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
3472 0xd61f0220, /* br x17. */
3473};
3474
83a01957 3475
053a4d68
JY
3476template<>
3477const uint32_t
3478Output_data_plt_aarch64_standard<64, false>::
3479 plt_entry[plt_entry_size / 4] =
3480{
3481 0x90000010, /* adrp x16, PLTGOT + n * 8 */
3482 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
3483 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
3484 0xd61f0220, /* br x17. */
3485};
3486
83a01957 3487
053a4d68
JY
3488template<>
3489const uint32_t
3490Output_data_plt_aarch64_standard<64, true>::
3491 plt_entry[plt_entry_size / 4] =
3492{
3493 0x90000010, /* adrp x16, PLTGOT + n * 8 */
3494 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
3495 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
3496 0xd61f0220, /* br x17. */
3497};
3498
83a01957 3499
053a4d68
JY
3500template<int size, bool big_endian>
3501void
3502Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
3503 unsigned char* pov,
9363c7c3
JY
3504 Address got_address,
3505 Address plt_address)
053a4d68
JY
3506{
3507 // PLT0 of the small PLT looks like this in ELF64 -
3508 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
3509 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
3510 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
3511 // symbol resolver
3512 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
3513 // GOTPLT entry for this.
3514 // br x17
3515 // PLT0 will be slightly different in ELF32 due to different got entry
3516 // size.
3517 memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
9363c7c3
JY
3518 Address gotplt_2nd_ent = got_address + (size / 8) * 2;
3519
3520 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
3521 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
3522 // FIXME: This only works for 64bit
3523 AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
3524 gotplt_2nd_ent, plt_address + 4);
3525
3526 // Fill in R_AARCH64_LDST8_LO12
3527 elfcpp::Swap<32, big_endian>::writeval(
3528 pov + 8,
3529 ((this->first_plt_entry[2] & 0xffc003ff)
3530 | ((gotplt_2nd_ent & 0xff8) << 7)));
3531
3532 // Fill in R_AARCH64_ADD_ABS_LO12
3533 elfcpp::Swap<32, big_endian>::writeval(
3534 pov + 12,
3535 ((this->first_plt_entry[3] & 0xffc003ff)
3536 | ((gotplt_2nd_ent & 0xfff) << 10)));
053a4d68
JY
3537}
3538
83a01957 3539
053a4d68 3540// Subsequent entries in the PLT for an executable.
9363c7c3 3541// FIXME: This only works for 64bit
053a4d68
JY
3542
3543template<int size, bool big_endian>
3544void
3545Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
3546 unsigned char* pov,
9363c7c3
JY
3547 Address got_address,
3548 Address plt_address,
3549 unsigned int got_offset,
3550 unsigned int plt_offset)
053a4d68
JY
3551{
3552 memcpy(pov, this->plt_entry, this->plt_entry_size);
9363c7c3
JY
3553
3554 Address gotplt_entry_address = got_address + got_offset;
3555 Address plt_entry_address = plt_address + plt_offset;
3556
3557 // Fill in R_AARCH64_PCREL_ADR_HI21
3558 AArch64_relocate_functions<size, big_endian>::adrp(
3559 pov,
3560 gotplt_entry_address,
3561 plt_entry_address);
3562
3563 // Fill in R_AARCH64_LDST64_ABS_LO12
3564 elfcpp::Swap<32, big_endian>::writeval(
3565 pov + 4,
3566 ((this->plt_entry[1] & 0xffc003ff)
3567 | ((gotplt_entry_address & 0xff8) << 7)));
3568
3569 // Fill in R_AARCH64_ADD_ABS_LO12
3570 elfcpp::Swap<32, big_endian>::writeval(
3571 pov + 8,
3572 ((this->plt_entry[2] & 0xffc003ff)
3573 | ((gotplt_entry_address & 0xfff) <<10)));
3574
053a4d68
JY
3575}
3576
3a531937
JY
3577
3578template<>
3579const uint32_t
3580Output_data_plt_aarch64_standard<32, false>::
3581 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
3582{
3583 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
3584 0x90000002, /* adrp x2, 0 */
3585 0x90000003, /* adrp x3, 0 */
3586 0xb9400042, /* ldr w2, [w2, #0] */
3587 0x11000063, /* add w3, w3, 0 */
3588 0xd61f0040, /* br x2 */
3589 0xd503201f, /* nop */
3590 0xd503201f, /* nop */
3591};
3592
3593template<>
3594const uint32_t
3595Output_data_plt_aarch64_standard<32, true>::
3596 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
3597{
3598 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
3599 0x90000002, /* adrp x2, 0 */
3600 0x90000003, /* adrp x3, 0 */
3601 0xb9400042, /* ldr w2, [w2, #0] */
3602 0x11000063, /* add w3, w3, 0 */
3603 0xd61f0040, /* br x2 */
3604 0xd503201f, /* nop */
3605 0xd503201f, /* nop */
3606};
3607
3608template<>
3609const uint32_t
3610Output_data_plt_aarch64_standard<64, false>::
3611 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
3612{
3613 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
3614 0x90000002, /* adrp x2, 0 */
3615 0x90000003, /* adrp x3, 0 */
3616 0xf9400042, /* ldr x2, [x2, #0] */
3617 0x91000063, /* add x3, x3, 0 */
3618 0xd61f0040, /* br x2 */
3619 0xd503201f, /* nop */
3620 0xd503201f, /* nop */
3621};
3622
3623template<>
3624const uint32_t
3625Output_data_plt_aarch64_standard<64, true>::
3626 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
3627{
3628 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
3629 0x90000002, /* adrp x2, 0 */
3630 0x90000003, /* adrp x3, 0 */
3631 0xf9400042, /* ldr x2, [x2, #0] */
3632 0x91000063, /* add x3, x3, 0 */
3633 0xd61f0040, /* br x2 */
3634 0xd503201f, /* nop */
3635 0xd503201f, /* nop */
3636};
3637
3638template<int size, bool big_endian>
3639void
3640Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
3641 unsigned char* pov,
3642 Address gotplt_address,
3643 Address plt_address,
3644 Address got_base,
3645 unsigned int tlsdesc_got_offset,
3646 unsigned int plt_offset)
3647{
3648 memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
3649
3650 // move DT_TLSDESC_GOT address into x2
3651 // move .got.plt address into x3
3652 Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
3653 Address plt_entry_address = plt_address + plt_offset;
3654
3655 // R_AARCH64_ADR_PREL_PG_HI21
3656 AArch64_relocate_functions<size, big_endian>::adrp(
3657 pov + 4,
3658 tlsdesc_got_entry,
3659 plt_entry_address + 4);
3660
3661 // R_AARCH64_ADR_PREL_PG_HI21
3662 AArch64_relocate_functions<size, big_endian>::adrp(
3663 pov + 8,
3664 gotplt_address,
3665 plt_entry_address + 8);
3666
3667 // R_AARCH64_LDST64_ABS_LO12
3668 elfcpp::Swap<32, big_endian>::writeval(
3669 pov + 12,
3670 ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
3671 | ((tlsdesc_got_entry & 0xff8) << 7)));
3672
3673 // R_AARCH64_ADD_ABS_LO12
3674 elfcpp::Swap<32, big_endian>::writeval(
3675 pov + 16,
3676 ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
3677 | ((gotplt_address & 0xfff) << 10)));
3678}
3679
053a4d68
JY
3680// Write out the PLT. This uses the hand-coded instructions above,
3681// and adjusts them as needed. This is specified by the AMD64 ABI.
3682
3683template<int size, bool big_endian>
3684void
3685Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
3686{
3687 const off_t offset = this->offset();
3688 const section_size_type oview_size =
3689 convert_to_section_size_type(this->data_size());
3690 unsigned char* const oview = of->get_output_view(offset, oview_size);
3691
3692 const off_t got_file_offset = this->got_plt_->offset();
9726c3c1
HS
3693 gold_assert(got_file_offset + this->got_plt_->data_size()
3694 == this->got_irelative_->offset());
3695
053a4d68 3696 const section_size_type got_size =
9726c3c1
HS
3697 convert_to_section_size_type(this->got_plt_->data_size()
3698 + this->got_irelative_->data_size());
053a4d68
JY
3699 unsigned char* const got_view = of->get_output_view(got_file_offset,
3700 got_size);
3701
3702 unsigned char* pov = oview;
3703
3704 // The base address of the .plt section.
3705 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
053a4d68 3706 // The base address of the PLT portion of the .got section.
3a531937
JY
3707 typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
3708 = this->got_plt_->address();
053a4d68 3709
3a531937 3710 this->fill_first_plt_entry(pov, gotplt_address, plt_address);
053a4d68
JY
3711 pov += this->first_plt_entry_offset();
3712
3713 // The first three entries in .got.plt are reserved.
3714 unsigned char* got_pov = got_view;
3715 memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
3716 got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
3717
3718 unsigned int plt_offset = this->first_plt_entry_offset();
3719 unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
3a531937 3720 const unsigned int count = this->count_ + this->irelative_count_;
053a4d68
JY
3721 for (unsigned int plt_index = 0;
3722 plt_index < count;
3723 ++plt_index,
3724 pov += this->get_plt_entry_size(),
3725 got_pov += size / 8,
3726 plt_offset += this->get_plt_entry_size(),
3727 got_offset += size / 8)
3728 {
3729 // Set and adjust the PLT entry itself.
3a531937 3730 this->fill_plt_entry(pov, gotplt_address, plt_address,
9363c7c3 3731 got_offset, plt_offset);
053a4d68 3732
9363c7c3
JY
3733 // Set the entry in the GOT, which points to plt0.
3734 elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
053a4d68
JY
3735 }
3736
3a531937
JY
3737 if (this->has_tlsdesc_entry())
3738 {
3739 // Set and adjust the reserved TLSDESC PLT entry.
3740 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
3741 // The base address of the .base section.
3742 typename elfcpp::Elf_types<size>::Elf_Addr got_base =
3743 this->got_->address();
3744 this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
3745 tlsdesc_got_offset, plt_offset);
3746 pov += this->get_plt_tlsdesc_entry_size();
3747 }
3748
053a4d68
JY
3749 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
3750 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
3751
3752 of->write_output_view(offset, oview_size, oview);
3753 of->write_output_view(got_file_offset, got_size, got_view);
3754}
3755
9363c7c3
JY
3756// Telling how to update the immediate field of an instruction.
3757struct AArch64_howto
3758{
3759 // The immediate field mask.
3760 elfcpp::Elf_Xword dst_mask;
3761
3762 // The offset to apply relocation immediate
3763 int doffset;
3764
3765 // The second part offset, if the immediate field has two parts.
3766 // -1 if the immediate field has only one part.
3767 int doffset2;
3768};
3769
3770static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
3771{
3772 {0, -1, -1}, // DATA
3773 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
3774 {0xffffe0, 5, -1}, // LD [23:5]-imm19
3775 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
3776 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
3777 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
3778 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
3779 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
3780 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
3781 {0x3ffffff, 0, -1}, // B [25:0]-imm26
3782 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
3783};
3784
3785// AArch64 relocate function class
3786
3787template<int size, bool big_endian>
3788class AArch64_relocate_functions
3789{
3790 public:
3791 typedef enum
3792 {
3793 STATUS_OKAY, // No error during relocation.
3794 STATUS_OVERFLOW, // Relocation overflow.
3795 STATUS_BAD_RELOC, // Relocation cannot be applied.
3796 } Status;
3797
9363c7c3
JY
3798 typedef AArch64_relocate_functions<size, big_endian> This;
3799 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
83a01957
HS
3800 typedef Relocate_info<size, big_endian> The_relocate_info;
3801 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
3802 typedef Reloc_stub<size, big_endian> The_reloc_stub;
3803 typedef typename The_reloc_stub::Stub_type The_reloc_stub_type;
3804 typedef Stub_table<size, big_endian> The_stub_table;
3805 typedef elfcpp::Rela<size, big_endian> The_rela;
9726c3c1 3806 typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
9363c7c3
JY
3807
3808 // Return the page address of the address.
3809 // Page(address) = address & ~0xFFF
3810
9726c3c1 3811 static inline AArch64_valtype
9363c7c3
JY
3812 Page(Address address)
3813 {
3814 return (address & (~static_cast<Address>(0xFFF)));
3815 }
3816
83a01957 3817 private:
9363c7c3
JY
3818 // Update instruction (pointed by view) with selected bits (immed).
3819 // val = (val & ~dst_mask) | (immed << doffset)
3820
3821 template<int valsize>
3822 static inline void
3823 update_view(unsigned char* view,
9726c3c1 3824 AArch64_valtype immed,
9363c7c3
JY
3825 elfcpp::Elf_Xword doffset,
3826 elfcpp::Elf_Xword dst_mask)
3827 {
3828 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
3829 Valtype* wv = reinterpret_cast<Valtype*>(view);
3830 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
3831
3832 // Clear immediate fields.
3833 val &= ~dst_mask;
3834 elfcpp::Swap<valsize, big_endian>::writeval(wv,
3835 static_cast<Valtype>(val | (immed << doffset)));
3836 }
3837
3838 // Update two parts of an instruction (pointed by view) with selected
3839 // bits (immed1 and immed2).
3840 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
3841
3842 template<int valsize>
3843 static inline void
3844 update_view_two_parts(
3845 unsigned char* view,
9726c3c1
HS
3846 AArch64_valtype immed1,
3847 AArch64_valtype immed2,
9363c7c3
JY
3848 elfcpp::Elf_Xword doffset1,
3849 elfcpp::Elf_Xword doffset2,
3850 elfcpp::Elf_Xword dst_mask)
3851 {
3852 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
3853 Valtype* wv = reinterpret_cast<Valtype*>(view);
3854 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
3855 val &= ~dst_mask;
3856 elfcpp::Swap<valsize, big_endian>::writeval(wv,
3857 static_cast<Valtype>(val | (immed1 << doffset1) |
3858 (immed2 << doffset2)));
3859 }
3860
9726c3c1 3861 // Update adr or adrp instruction with immed.
9363c7c3
JY
3862 // In adr and adrp: [30:29] immlo [23:5] immhi
3863
3864 static inline void
9726c3c1 3865 update_adr(unsigned char* view, AArch64_valtype immed)
9363c7c3
JY
3866 {
3867 elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
9363c7c3
JY
3868 This::template update_view_two_parts<32>(
3869 view,
3870 immed & 0x3,
3871 (immed & 0x1ffffc) >> 2,
3872 29,
3873 5,
3874 dst_mask);
3875 }
3876
3a531937
JY
3877 // Update movz/movn instruction with bits immed.
3878 // Set instruction to movz if is_movz is true, otherwise set instruction
3879 // to movn.
9726c3c1 3880
3a531937
JY
3881 static inline void
3882 update_movnz(unsigned char* view,
9726c3c1 3883 AArch64_valtype immed,
3a531937
JY
3884 bool is_movz)
3885 {
3886 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3887 Valtype* wv = reinterpret_cast<Valtype*>(view);
3888 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3889
3890 const elfcpp::Elf_Xword doffset =
3891 aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
3892 const elfcpp::Elf_Xword dst_mask =
3893 aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
3894
3895 // Clear immediate fields and opc code.
9726c3c1 3896 val &= ~(dst_mask | (0x3 << 29));
3a531937
JY
3897
3898 // Set instruction to movz or movn.
3899 // movz: [30:29] is 10 movn: [30:29] is 00
3900 if (is_movz)
9726c3c1 3901 val |= (0x2 << 29);
3a531937
JY
3902
3903 elfcpp::Swap<32, big_endian>::writeval(wv,
3904 static_cast<Valtype>(val | (immed << doffset)));
3905 }
3906
9726c3c1
HS
3907 // Update selected bits in text.
3908
3909 template<int valsize>
3910 static inline typename This::Status
3911 reloc_common(unsigned char* view, Address x,
3912 const AArch64_reloc_property* reloc_property)
3913 {
3914 // Select bits from X.
3915 Address immed = reloc_property->select_x_value(x);
3916
3917 // Update view.
3918 const AArch64_reloc_property::Reloc_inst inst =
3919 reloc_property->reloc_inst();
3920 // If it is a data relocation or instruction has 2 parts of immediate
3921 // fields, you should not call pcrela_general.
3922 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
3923 aarch64_howto[inst].doffset != -1);
3924 This::template update_view<valsize>(view, immed,
3925 aarch64_howto[inst].doffset,
3926 aarch64_howto[inst].dst_mask);
3927
3928 // Do check overflow or alignment if needed.
3929 return (reloc_property->checkup_x_value(x)
3930 ? This::STATUS_OKAY
3931 : This::STATUS_OVERFLOW);
3932 }
3933
9363c7c3
JY
3934 public:
3935
3936 // Do a simple rela relocation at unaligned addresses.
3937
3938 template<int valsize>
3939 static inline typename This::Status
3940 rela_ua(unsigned char* view,
3941 const Sized_relobj_file<size, big_endian>* object,
3942 const Symbol_value<size>* psymval,
9726c3c1 3943 AArch64_valtype addend,
9363c7c3
JY
3944 const AArch64_reloc_property* reloc_property)
3945 {
3946 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
3947 Valtype;
3948 typename elfcpp::Elf_types<size>::Elf_Addr x =
3949 psymval->value(object, addend);
3950 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
3951 static_cast<Valtype>(x));
3952 return (reloc_property->checkup_x_value(x)
3953 ? This::STATUS_OKAY
3954 : This::STATUS_OVERFLOW);
3955 }
3956
3957 // Do a simple pc-relative relocation at unaligned addresses.
3958
3959 template<int valsize>
3960 static inline typename This::Status
3961 pcrela_ua(unsigned char* view,
3962 const Sized_relobj_file<size, big_endian>* object,
3963 const Symbol_value<size>* psymval,
9726c3c1 3964 AArch64_valtype addend,
9363c7c3
JY
3965 Address address,
3966 const AArch64_reloc_property* reloc_property)
3967 {
3968 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
3969 Valtype;
3a531937 3970 Address x = psymval->value(object, addend) - address;
9363c7c3
JY
3971 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
3972 static_cast<Valtype>(x));
3973 return (reloc_property->checkup_x_value(x)
3974 ? This::STATUS_OKAY
3975 : This::STATUS_OVERFLOW);
3976 }
3977
3978 // Do a simple rela relocation at aligned addresses.
3979
3980 template<int valsize>
3981 static inline typename This::Status
3982 rela(
3983 unsigned char* view,
3984 const Sized_relobj_file<size, big_endian>* object,
3985 const Symbol_value<size>* psymval,
9726c3c1 3986 AArch64_valtype addend,
9363c7c3
JY
3987 const AArch64_reloc_property* reloc_property)
3988 {
9726c3c1 3989 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
9363c7c3 3990 Valtype* wv = reinterpret_cast<Valtype*>(view);
3a531937 3991 Address x = psymval->value(object, addend);
9726c3c1 3992 elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
9363c7c3
JY
3993 return (reloc_property->checkup_x_value(x)
3994 ? This::STATUS_OKAY
3995 : This::STATUS_OVERFLOW);
3996 }
3997
3998 // Do relocate. Update selected bits in text.
3999 // new_val = (val & ~dst_mask) | (immed << doffset)
4000
4001 template<int valsize>
4002 static inline typename This::Status
4003 rela_general(unsigned char* view,
4004 const Sized_relobj_file<size, big_endian>* object,
4005 const Symbol_value<size>* psymval,
9726c3c1 4006 AArch64_valtype addend,
9363c7c3
JY
4007 const AArch64_reloc_property* reloc_property)
4008 {
4009 // Calculate relocation.
83a01957 4010 Address x = psymval->value(object, addend);
9726c3c1 4011 return This::template reloc_common<valsize>(view, x, reloc_property);
9363c7c3
JY
4012 }
4013
4014 // Do relocate. Update selected bits in text.
4015 // new val = (val & ~dst_mask) | (immed << doffset)
4016
4017 template<int valsize>
4018 static inline typename This::Status
4019 rela_general(
4020 unsigned char* view,
9726c3c1
HS
4021 AArch64_valtype s,
4022 AArch64_valtype addend,
9363c7c3
JY
4023 const AArch64_reloc_property* reloc_property)
4024 {
4025 // Calculate relocation.
4026 Address x = s + addend;
9726c3c1 4027 return This::template reloc_common<valsize>(view, x, reloc_property);
9363c7c3
JY
4028 }
4029
4030 // Do address relative relocate. Update selected bits in text.
4031 // new val = (val & ~dst_mask) | (immed << doffset)
4032
4033 template<int valsize>
4034 static inline typename This::Status
4035 pcrela_general(
4036 unsigned char* view,
4037 const Sized_relobj_file<size, big_endian>* object,
4038 const Symbol_value<size>* psymval,
9726c3c1 4039 AArch64_valtype addend,
9363c7c3
JY
4040 Address address,
4041 const AArch64_reloc_property* reloc_property)
4042 {
4043 // Calculate relocation.
3a531937 4044 Address x = psymval->value(object, addend) - address;
9726c3c1
HS
4045 return This::template reloc_common<valsize>(view, x, reloc_property);
4046 }
9363c7c3 4047
9363c7c3 4048
9726c3c1 4049 // Calculate (S + A) - address, update adr instruction.
9363c7c3 4050
9726c3c1
HS
4051 static inline typename This::Status
4052 adr(unsigned char* view,
4053 const Sized_relobj_file<size, big_endian>* object,
4054 const Symbol_value<size>* psymval,
4055 Address addend,
4056 Address address,
4057 const AArch64_reloc_property* /* reloc_property */)
4058 {
4059 AArch64_valtype x = psymval->value(object, addend) - address;
4060 // Pick bits [20:0] of X.
4061 AArch64_valtype immed = x & 0x1fffff;
4062 update_adr(view, immed);
4063 // Check -2^20 <= X < 2^20
4064 return (size == 64 && Bits<21>::has_overflow((x))
4065 ? This::STATUS_OVERFLOW
4066 : This::STATUS_OKAY);
9363c7c3
JY
4067 }
4068
4069 // Calculate PG(S+A) - PG(address), update adrp instruction.
4070 // R_AARCH64_ADR_PREL_PG_HI21
4071
4072 static inline typename This::Status
4073 adrp(
4074 unsigned char* view,
4075 Address sa,
4076 Address address)
4077 {
9726c3c1
HS
4078 AArch64_valtype x = This::Page(sa) - This::Page(address);
4079 // Pick [32:12] of X.
4080 AArch64_valtype immed = (x >> 12) & 0x1fffff;
4081 update_adr(view, immed);
83a01957
HS
4082 // Check -2^32 <= X < 2^32
4083 return (size == 64 && Bits<33>::has_overflow((x))
9363c7c3
JY
4084 ? This::STATUS_OVERFLOW
4085 : This::STATUS_OKAY);
4086 }
4087
4088 // Calculate PG(S+A) - PG(address), update adrp instruction.
4089 // R_AARCH64_ADR_PREL_PG_HI21
4090
4091 static inline typename This::Status
4092 adrp(unsigned char* view,
4093 const Sized_relobj_file<size, big_endian>* object,
4094 const Symbol_value<size>* psymval,
4095 Address addend,
4096 Address address,
4097 const AArch64_reloc_property* reloc_property)
4098 {
4099 Address sa = psymval->value(object, addend);
9726c3c1
HS
4100 AArch64_valtype x = This::Page(sa) - This::Page(address);
4101 // Pick [32:12] of X.
4102 AArch64_valtype immed = (x >> 12) & 0x1fffff;
4103 update_adr(view, immed);
9363c7c3
JY
4104 return (reloc_property->checkup_x_value(x)
4105 ? This::STATUS_OKAY
4106 : This::STATUS_OVERFLOW);
4107 }
4108
3a531937
JY
4109 // Update mov[n/z] instruction. Check overflow if needed.
4110 // If X >=0, set the instruction to movz and its immediate value to the
4111 // selected bits S.
4112 // If X < 0, set the instruction to movn and its immediate value to
4113 // NOT (selected bits of).
4114
4115 static inline typename This::Status
4116 movnz(unsigned char* view,
9726c3c1 4117 AArch64_valtype x,
3a531937
JY
4118 const AArch64_reloc_property* reloc_property)
4119 {
4120 // Select bits from X.
9726c3c1
HS
4121 Address immed;
4122 bool is_movz;
4123 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
4124 if (static_cast<SignedW>(x) >= 0)
4125 {
4126 immed = reloc_property->select_x_value(x);
4127 is_movz = true;
4128 }
4129 else
3a531937 4130 {
9726c3c1 4131 immed = reloc_property->select_x_value(~x);;
3a531937
JY
4132 is_movz = false;
4133 }
4134
4135 // Update movnz instruction.
4136 update_movnz(view, immed, is_movz);
4137
4138 // Do check overflow or alignment if needed.
4139 return (reloc_property->checkup_x_value(x)
4140 ? This::STATUS_OKAY
4141 : This::STATUS_OVERFLOW);
4142 }
4143
83a01957
HS
4144 static inline bool
4145 maybe_apply_stub(unsigned int,
4146 const The_relocate_info*,
4147 const The_rela&,
4148 unsigned char*,
4149 Address,
4150 const Sized_symbol<size>*,
4151 const Symbol_value<size>*,
0bf32ea9
JY
4152 const Sized_relobj_file<size, big_endian>*,
4153 section_size_type);
83a01957 4154
3a531937
JY
4155}; // End of AArch64_relocate_functions
4156
4157
83a01957
HS
4158// For a certain relocation type (usually jump/branch), test to see if the
4159// destination needs a stub to fulfil. If so, re-route the destination of the
4160// original instruction to the stub, note, at this time, the stub has already
4161// been generated.
4162
4163template<int size, bool big_endian>
4164bool
4165AArch64_relocate_functions<size, big_endian>::
4166maybe_apply_stub(unsigned int r_type,
4167 const The_relocate_info* relinfo,
4168 const The_rela& rela,
4169 unsigned char* view,
4170 Address address,
4171 const Sized_symbol<size>* gsym,
4172 const Symbol_value<size>* psymval,
0bf32ea9
JY
4173 const Sized_relobj_file<size, big_endian>* object,
4174 section_size_type current_group_size)
83a01957
HS
4175{
4176 if (parameters->options().relocatable())
4177 return false;
4178
4179 typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
4180 Address branch_target = psymval->value(object, 0) + addend;
4181 The_reloc_stub_type stub_type = The_reloc_stub::
4182 stub_type_for_reloc(r_type, address, branch_target);
4183 if (stub_type == The_reloc_stub::ST_NONE)
4184 return false;
4185
4186 const The_aarch64_relobj* aarch64_relobj =
4187 static_cast<const The_aarch64_relobj*>(object);
4188 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
4189 gold_assert(stub_table != NULL);
4190
4191 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4192 typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
4193 The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
4194 gold_assert(stub != NULL);
4195
4196 Address new_branch_target = stub_table->address() + stub->offset();
4197 typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
4198 new_branch_target - address;
4199 const AArch64_reloc_property* arp =
4200 aarch64_reloc_property_table->get_reloc_property(r_type);
4201 gold_assert(arp != NULL);
aed56ec5 4202 typename This::Status status = This::template
83a01957
HS
4203 rela_general<32>(view, branch_offset, 0, arp);
4204 if (status != This::STATUS_OKAY)
4205 gold_error(_("Stub is too far away, try a smaller value "
0bf32ea9 4206 "for '--stub-group-size'. The current value is 0x%lx."),
add6016b 4207 static_cast<unsigned long>(current_group_size));
83a01957
HS
4208 return true;
4209}
4210
4211
4212// Group input sections for stub generation.
4213//
4214// We group input sections in an output section so that the total size,
4215// including any padding space due to alignment is smaller than GROUP_SIZE
4216// unless the only input section in group is bigger than GROUP_SIZE already.
4217// Then an ARM stub table is created to follow the last input section
4218// in group. For each group an ARM stub table is created an is placed
4219// after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
4220// extend the group after the stub table.
4221
4222template<int size, bool big_endian>
4223void
4224Target_aarch64<size, big_endian>::group_sections(
4225 Layout* layout,
4226 section_size_type group_size,
4227 bool stubs_always_after_branch,
4228 const Task* task)
4229{
4230 // Group input sections and insert stub table
4231 Layout::Section_list section_list;
4232 layout->get_executable_sections(&section_list);
4233 for (Layout::Section_list::const_iterator p = section_list.begin();
4234 p != section_list.end();
4235 ++p)
4236 {
4237 AArch64_output_section<size, big_endian>* output_section =
4238 static_cast<AArch64_output_section<size, big_endian>*>(*p);
4239 output_section->group_sections(group_size, stubs_always_after_branch,
4240 this, task);
4241 }
4242}
4243
4244
4245// Find the AArch64_input_section object corresponding to the SHNDX-th input
4246// section of RELOBJ.
4247
4248template<int size, bool big_endian>
4249AArch64_input_section<size, big_endian>*
4250Target_aarch64<size, big_endian>::find_aarch64_input_section(
4251 Relobj* relobj, unsigned int shndx) const
4252{
4253 Section_id sid(relobj, shndx);
4254 typename AArch64_input_section_map::const_iterator p =
4255 this->aarch64_input_section_map_.find(sid);
4256 return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
4257}
4258
4259
4260// Make a new AArch64_input_section object.
4261
4262template<int size, bool big_endian>
4263AArch64_input_section<size, big_endian>*
4264Target_aarch64<size, big_endian>::new_aarch64_input_section(
4265 Relobj* relobj, unsigned int shndx)
4266{
4267 Section_id sid(relobj, shndx);
4268
4269 AArch64_input_section<size, big_endian>* input_section =
4270 new AArch64_input_section<size, big_endian>(relobj, shndx);
4271 input_section->init();
4272
4273 // Register new AArch64_input_section in map for look-up.
4274 std::pair<typename AArch64_input_section_map::iterator,bool> ins =
4275 this->aarch64_input_section_map_.insert(
4276 std::make_pair(sid, input_section));
4277
4278 // Make sure that it we have not created another AArch64_input_section
4279 // for this input section already.
4280 gold_assert(ins.second);
4281
4282 return input_section;
4283}
4284
4285
4286// Relaxation hook. This is where we do stub generation.
4287
4288template<int size, bool big_endian>
4289bool
4290Target_aarch64<size, big_endian>::do_relax(
4291 int pass,
4292 const Input_objects* input_objects,
4293 Symbol_table* symtab,
4294 Layout* layout ,
4295 const Task* task)
4296{
4297 gold_assert(!parameters->options().relocatable());
4298 if (pass == 1)
4299 {
0bf32ea9
JY
4300 // We don't handle negative stub_group_size right now.
4301 this->stub_group_size_ = abs(parameters->options().stub_group_size());
4302 if (this->stub_group_size_ == 1)
83a01957
HS
4303 {
4304 // Leave room for 4096 4-byte stub entries. If we exceed that, then we
4305 // will fail to link. The user will have to relink with an explicit
4306 // group size option.
0bf32ea9
JY
4307 this->stub_group_size_ = The_reloc_stub::MAX_BRANCH_OFFSET -
4308 4096 * 4;
83a01957 4309 }
0bf32ea9 4310 group_sections(layout, this->stub_group_size_, true, task);
83a01957
HS
4311 }
4312 else
4313 {
4314 // If this is not the first pass, addresses and file offsets have
4315 // been reset at this point, set them here.
4316 for (Stub_table_iterator sp = this->stub_tables_.begin();
4317 sp != this->stub_tables_.end(); ++sp)
4318 {
4319 The_stub_table* stt = *sp;
4320 The_aarch64_input_section* owner = stt->owner();
4321 off_t off = align_address(owner->original_size(),
4322 stt->addralign());
4323 stt->set_address_and_file_offset(owner->address() + off,
4324 owner->offset() + off);
4325 }
4326 }
4327
4328 // Scan relocs for relocation stubs
4329 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
4330 op != input_objects->relobj_end();
4331 ++op)
4332 {
4333 The_aarch64_relobj* aarch64_relobj =
4334 static_cast<The_aarch64_relobj*>(*op);
4335 // Lock the object so we can read from it. This is only called
4336 // single-threaded from Layout::finalize, so it is OK to lock.
4337 Task_lock_obj<Object> tl(task, aarch64_relobj);
4338 aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
4339 }
4340
4341 bool any_stub_table_changed = false;
4342 for (Stub_table_iterator siter = this->stub_tables_.begin();
4343 siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
4344 {
4345 The_stub_table* stub_table = *siter;
4346 if (stub_table->update_data_size_changed_p())
4347 {
4348 The_aarch64_input_section* owner = stub_table->owner();
4349 uint64_t address = owner->address();
4350 off_t offset = owner->offset();
4351 owner->reset_address_and_file_offset();
4352 owner->set_address_and_file_offset(address, offset);
4353
4354 any_stub_table_changed = true;
4355 }
4356 }
4357
4358 // Do not continue relaxation.
4359 bool continue_relaxation = any_stub_table_changed;
4360 if (!continue_relaxation)
4361 for (Stub_table_iterator sp = this->stub_tables_.begin();
4362 (sp != this->stub_tables_.end());
4363 ++sp)
4364 (*sp)->finalize_stubs();
4365
4366 return continue_relaxation;
4367}
4368
4369
4370// Make a new Stub_table.
4371
4372template<int size, bool big_endian>
4373Stub_table<size, big_endian>*
4374Target_aarch64<size, big_endian>::new_stub_table(
4375 AArch64_input_section<size, big_endian>* owner)
4376{
4377 Stub_table<size, big_endian>* stub_table =
4378 new Stub_table<size, big_endian>(owner);
4379 stub_table->set_address(align_address(
4380 owner->address() + owner->data_size(), 8));
4381 stub_table->set_file_offset(owner->offset() + owner->data_size());
4382 stub_table->finalize_data_size();
4383
4384 this->stub_tables_.push_back(stub_table);
4385
4386 return stub_table;
4387}
4388
4389
3a531937 4390template<int size, bool big_endian>
7fa5525f 4391uint64_t
3a531937 4392Target_aarch64<size, big_endian>::do_reloc_addend(
7fa5525f 4393 void* arg, unsigned int r_type, uint64_t) const
3a531937
JY
4394{
4395 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
4396 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4397 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4398 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
4399 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
4400 gold_assert(psymval->is_tls_symbol());
4401 // The value of a TLS symbol is the offset in the TLS segment.
4402 return psymval->value(ti.object, 0);
4403}
9363c7c3 4404
053a4d68
JY
4405// Return the number of entries in the PLT.
4406
4407template<int size, bool big_endian>
4408unsigned int
4409Target_aarch64<size, big_endian>::plt_entry_count() const
4410{
4411 if (this->plt_ == NULL)
4412 return 0;
4413 return this->plt_->entry_count();
4414}
4415
4416// Return the offset of the first non-reserved PLT entry.
4417
4418template<int size, bool big_endian>
4419unsigned int
4420Target_aarch64<size, big_endian>::first_plt_entry_offset() const
4421{
4422 return this->plt_->first_plt_entry_offset();
4423}
4424
4425// Return the size of each PLT entry.
4426
4427template<int size, bool big_endian>
4428unsigned int
4429Target_aarch64<size, big_endian>::plt_entry_size() const
4430{
4431 return this->plt_->get_plt_entry_size();
4432}
4433
3a531937 4434// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
053a4d68
JY
4435
4436template<int size, bool big_endian>
3a531937
JY
4437void
4438Target_aarch64<size, big_endian>::define_tls_base_symbol(
4439 Symbol_table* symtab, Layout* layout)
053a4d68 4440{
3a531937
JY
4441 if (this->tls_base_symbol_defined_)
4442 return;
4443
4444 Output_segment* tls_segment = layout->tls_segment();
4445 if (tls_segment != NULL)
4446 {
6b0ad2eb 4447 // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
3a531937
JY
4448 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
4449 Symbol_table::PREDEFINED,
4450 tls_segment, 0, 0,
4451 elfcpp::STT_TLS,
4452 elfcpp::STB_LOCAL,
4453 elfcpp::STV_HIDDEN, 0,
6b0ad2eb 4454 Symbol::SEGMENT_START,
3a531937
JY
4455 true);
4456 }
4457 this->tls_base_symbol_defined_ = true;
053a4d68
JY
4458}
4459
3a531937 4460// Create the reserved PLT and GOT entries for the TLS descriptor resolver.
053a4d68
JY
4461
4462template<int size, bool big_endian>
3a531937
JY
4463void
4464Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
4465 Symbol_table* symtab, Layout* layout)
053a4d68 4466{
3a531937
JY
4467 if (this->plt_ == NULL)
4468 this->make_plt_section(symtab, layout);
4469
4470 if (!this->plt_->has_tlsdesc_entry())
053a4d68 4471 {
3a531937
JY
4472 // Allocate the TLSDESC_GOT entry.
4473 Output_data_got_aarch64<size, big_endian>* got =
4474 this->got_section(symtab, layout);
4475 unsigned int got_offset = got->add_constant(0);
4476
4477 // Allocate the TLSDESC_PLT entry.
4478 this->plt_->reserve_tlsdesc_entry(got_offset);
053a4d68 4479 }
053a4d68
JY
4480}
4481
3a531937 4482// Create a GOT entry for the TLS module index.
053a4d68
JY
4483
4484template<int size, bool big_endian>
3a531937
JY
4485unsigned int
4486Target_aarch64<size, big_endian>::got_mod_index_entry(
4487 Symbol_table* symtab, Layout* layout,
4488 Sized_relobj_file<size, big_endian>* object)
4489{
4490 if (this->got_mod_index_offset_ == -1U)
4491 {
4492 gold_assert(symtab != NULL && layout != NULL && object != NULL);
4493 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
4494 Output_data_got_aarch64<size, big_endian>* got =
4495 this->got_section(symtab, layout);
4496 unsigned int got_offset = got->add_constant(0);
4497 rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
4498 got_offset, 0);
4499 got->add_constant(0);
4500 this->got_mod_index_offset_ = got_offset;
4501 }
4502 return this->got_mod_index_offset_;
4503}
4504
4505// Optimize the TLS relocation type based on what we know about the
4506// symbol. IS_FINAL is true if the final address of this symbol is
4507// known at link time.
4508
4509template<int size, bool big_endian>
4510tls::Tls_optimization
4511Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
4512 int r_type)
4513{
4514 // If we are generating a shared library, then we can't do anything
4515 // in the linker
4516 if (parameters->options().shared())
4517 return tls::TLSOPT_NONE;
4518
4519 switch (r_type)
4520 {
4521 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
4522 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
4523 case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
4524 case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
4525 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
4526 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
4527 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
4528 case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
4529 case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
4530 case elfcpp::R_AARCH64_TLSDESC_LDR:
4531 case elfcpp::R_AARCH64_TLSDESC_ADD:
4532 case elfcpp::R_AARCH64_TLSDESC_CALL:
4533 // These are General-Dynamic which permits fully general TLS
4534 // access. Since we know that we are generating an executable,
4535 // we can convert this to Initial-Exec. If we also know that
4536 // this is a local symbol, we can further switch to Local-Exec.
4537 if (is_final)
4538 return tls::TLSOPT_TO_LE;
4539 return tls::TLSOPT_TO_IE;
4540
9726c3c1
HS
4541 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
4542 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
4543 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
4544 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6b0ad2eb
JY
4545 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
4546 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
9726c3c1
HS
4547 // These are Local-Dynamic, which refer to local symbols in the
4548 // dynamic TLS block. Since we know that we generating an
4549 // executable, we can switch to Local-Exec.
4550 return tls::TLSOPT_TO_LE;
4551
3a531937
JY
4552 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
4553 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
4554 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4555 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4556 case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
4557 // These are Initial-Exec relocs which get the thread offset
4558 // from the GOT. If we know that we are linking against the
4559 // local symbol, we can switch to Local-Exec, which links the
4560 // thread offset into the instruction.
4561 if (is_final)
4562 return tls::TLSOPT_TO_LE;
4563 return tls::TLSOPT_NONE;
4564
4565 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
4566 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
4567 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4568 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
4569 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4570 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
4571 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
4572 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4573 // When we already have Local-Exec, there is nothing further we
4574 // can do.
4575 return tls::TLSOPT_NONE;
4576
4577 default:
4578 gold_unreachable();
4579 }
4580}
4581
4582// Returns true if this relocation type could be that of a function pointer.
4583
4584template<int size, bool big_endian>
4585inline bool
4586Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
4587 unsigned int r_type)
4588{
4589 switch (r_type)
4590 {
9726c3c1
HS
4591 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
4592 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
4593 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
4594 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
4595 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
3a531937
JY
4596 {
4597 return true;
4598 }
4599 }
4600 return false;
4601}
4602
4603// For safe ICF, scan a relocation for a local symbol to check if it
4604// corresponds to a function pointer being taken. In that case mark
4605// the function whose pointer was taken as not foldable.
4606
4607template<int size, bool big_endian>
4608inline bool
4609Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
4610 Symbol_table* ,
053a4d68
JY
4611 Layout* ,
4612 Target_aarch64<size, big_endian>* ,
4613 Sized_relobj_file<size, big_endian>* ,
4614 unsigned int ,
4615 Output_section* ,
4616 const elfcpp::Rela<size, big_endian>& ,
4617 unsigned int r_type,
4618 const elfcpp::Sym<size, big_endian>&)
4619{
9726c3c1 4620 // When building a shared library, do not fold any local symbols.
053a4d68 4621 return (parameters->options().shared()
9363c7c3 4622 || possible_function_pointer_reloc(r_type));
053a4d68
JY
4623}
4624
4625// For safe ICF, scan a relocation for a global symbol to check if it
4626// corresponds to a function pointer being taken. In that case mark
4627// the function whose pointer was taken as not foldable.
4628
4629template<int size, bool big_endian>
4630inline bool
4631Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
4632 Symbol_table* ,
4633 Layout* ,
4634 Target_aarch64<size, big_endian>* ,
4635 Sized_relobj_file<size, big_endian>* ,
4636 unsigned int ,
4637 Output_section* ,
4638 const elfcpp::Rela<size, big_endian>& ,
4639 unsigned int r_type,
4640 Symbol* gsym)
4641{
4642 // When building a shared library, do not fold symbols whose visibility
4643 // is hidden, internal or protected.
4644 return ((parameters->options().shared()
9363c7c3
JY
4645 && (gsym->visibility() == elfcpp::STV_INTERNAL
4646 || gsym->visibility() == elfcpp::STV_PROTECTED
4647 || gsym->visibility() == elfcpp::STV_HIDDEN))
4648 || possible_function_pointer_reloc(r_type));
053a4d68
JY
4649}
4650
4651// Report an unsupported relocation against a local symbol.
4652
4653template<int size, bool big_endian>
4654void
4655Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
4656 Sized_relobj_file<size, big_endian>* object,
4657 unsigned int r_type)
4658{
4659 gold_error(_("%s: unsupported reloc %u against local symbol"),
4660 object->name().c_str(), r_type);
4661}
4662
4663// We are about to emit a dynamic relocation of type R_TYPE. If the
4664// dynamic linker does not support it, issue an error.
4665
4666template<int size, bool big_endian>
4667void
4668Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
9363c7c3 4669 unsigned int r_type)
053a4d68
JY
4670{
4671 gold_assert(r_type != elfcpp::R_AARCH64_NONE);
4672
4673 switch (r_type)
4674 {
4675 // These are the relocation types supported by glibc for AARCH64.
4676 case elfcpp::R_AARCH64_NONE:
4677 case elfcpp::R_AARCH64_COPY:
4678 case elfcpp::R_AARCH64_GLOB_DAT:
4679 case elfcpp::R_AARCH64_JUMP_SLOT:
4680 case elfcpp::R_AARCH64_RELATIVE:
4681 case elfcpp::R_AARCH64_TLS_DTPREL64:
4682 case elfcpp::R_AARCH64_TLS_DTPMOD64:
4683 case elfcpp::R_AARCH64_TLS_TPREL64:
4684 case elfcpp::R_AARCH64_TLSDESC:
4685 case elfcpp::R_AARCH64_IRELATIVE:
4686 case elfcpp::R_AARCH64_ABS32:
4687 case elfcpp::R_AARCH64_ABS64:
4688 return;
4689
4690 default:
4691 break;
4692 }
4693
4694 // This prevents us from issuing more than one error per reloc
4695 // section. But we can still wind up issuing more than one
4696 // error per object file.
4697 if (this->issued_non_pic_error_)
4698 return;
4699 gold_assert(parameters->options().output_is_position_independent());
4700 object->error(_("requires unsupported dynamic reloc; "
9363c7c3 4701 "recompile with -fPIC"));
053a4d68
JY
4702 this->issued_non_pic_error_ = true;
4703 return;
4704}
4705
9726c3c1
HS
4706// Return whether we need to make a PLT entry for a relocation of the
4707// given type against a STT_GNU_IFUNC symbol.
4708
4709template<int size, bool big_endian>
4710bool
4711Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
4712 Sized_relobj_file<size, big_endian>* object,
4713 unsigned int r_type)
4714{
4715 const AArch64_reloc_property* arp =
4716 aarch64_reloc_property_table->get_reloc_property(r_type);
4717 gold_assert(arp != NULL);
4718
4719 int flags = arp->reference_flags();
4720 if (flags & Symbol::TLS_REF)
4721 {
4722 gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
4723 object->name().c_str(), arp->name().c_str());
4724 return false;
4725 }
4726 return flags != 0;
4727}
4728
053a4d68
JY
4729// Scan a relocation for a local symbol.
4730
4731template<int size, bool big_endian>
4732inline void
4733Target_aarch64<size, big_endian>::Scan::local(
8e33481e
HS
4734 Symbol_table* symtab,
4735 Layout* layout,
4736 Target_aarch64<size, big_endian>* target,
9363c7c3 4737 Sized_relobj_file<size, big_endian>* object,
8e33481e
HS
4738 unsigned int data_shndx,
4739 Output_section* output_section,
4740 const elfcpp::Rela<size, big_endian>& rela,
053a4d68 4741 unsigned int r_type,
9726c3c1 4742 const elfcpp::Sym<size, big_endian>& lsym,
053a4d68
JY
4743 bool is_discarded)
4744{
4745 if (is_discarded)
4746 return;
4747
8e33481e 4748 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
3a531937
JY
4749 Reloc_section;
4750 Output_data_got_aarch64<size, big_endian>* got =
4751 target->got_section(symtab, layout);
4752 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
8e33481e 4753
9726c3c1
HS
4754 // A local STT_GNU_IFUNC symbol may require a PLT entry.
4755 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
4756 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
4757 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
4758
053a4d68
JY
4759 switch (r_type)
4760 {
9363c7c3
JY
4761 case elfcpp::R_AARCH64_ABS32:
4762 case elfcpp::R_AARCH64_ABS16:
8e33481e
HS
4763 if (parameters->options().output_is_position_independent())
4764 {
4765 gold_error(_("%s: unsupported reloc %u in pos independent link."),
4766 object->name().c_str(), r_type);
4767 }
4768 break;
4769
4770 case elfcpp::R_AARCH64_ABS64:
9363c7c3
JY
4771 // If building a shared library or pie, we need to mark this as a dynmic
4772 // reloction, so that the dynamic loader can relocate it.
9363c7c3
JY
4773 if (parameters->options().output_is_position_independent())
4774 {
8e33481e 4775 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8e33481e
HS
4776 rela_dyn->add_local_relative(object, r_sym,
4777 elfcpp::R_AARCH64_RELATIVE,
4778 output_section,
4779 data_shndx,
4780 rela.get_r_offset(),
4781 rela.get_r_addend(),
9726c3c1 4782 is_ifunc);
9363c7c3
JY
4783 }
4784 break;
4785
8e33481e
HS
4786 case elfcpp::R_AARCH64_PREL64:
4787 case elfcpp::R_AARCH64_PREL32:
4788 case elfcpp::R_AARCH64_PREL16:
4789 break;
4790
3a531937
JY
4791 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
4792 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
4793 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
4794 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
4795 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
4796 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
4797 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
4798 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
4799 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
8e33481e 4800 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
3a531937 4801 break;
053a4d68 4802
8e33481e
HS
4803 // Control flow, pc-relative. We don't need to do anything for a relative
4804 // addressing relocation against a local symbol if it does not reference
4805 // the GOT.
4806 case elfcpp::R_AARCH64_TSTBR14:
4807 case elfcpp::R_AARCH64_CONDBR19:
4808 case elfcpp::R_AARCH64_JUMP26:
4809 case elfcpp::R_AARCH64_CALL26:
4810 break;
4811
4812 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4813 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4814 {
3a531937
JY
4815 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
4816 optimize_tls_reloc(!parameters->options().shared(), r_type);
4817 if (tlsopt == tls::TLSOPT_TO_LE)
4818 break;
4819
8e33481e
HS
4820 layout->set_has_static_tls();
4821 // Create a GOT entry for the tp-relative offset.
8e33481e
HS
4822 if (!parameters->doing_static_link())
4823 {
4824 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
4825 target->rela_dyn_section(layout),
4826 elfcpp::R_AARCH64_TLS_TPREL64);
4827 }
4828 else if (!object->local_has_got_offset(r_sym,
4829 GOT_TYPE_TLS_OFFSET))
4830 {
4831 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
4832 unsigned int got_offset =
4833 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
4834 const elfcpp::Elf_Xword addend = rela.get_r_addend();
4835 gold_assert(addend == 0);
4836 got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
4837 object, r_sym);
4838 }
4839 }
4840 break;
4841
3a531937
JY
4842 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
4843 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
4844 {
4845 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
4846 optimize_tls_reloc(!parameters->options().shared(), r_type);
4847 if (tlsopt == tls::TLSOPT_TO_LE)
4848 {
4849 layout->set_has_static_tls();
4850 break;
4851 }
4852 gold_assert(tlsopt == tls::TLSOPT_NONE);
4853
4854 got->add_local_pair_with_rel(object,r_sym, data_shndx,
4855 GOT_TYPE_TLS_PAIR,
4856 target->rela_dyn_section(layout),
4857 elfcpp::R_AARCH64_TLS_DTPMOD64);
4858 }
4859 break;
4860
1a920511
JY
4861 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
4862 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
4863 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4864 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
4865 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
8e33481e
HS
4866 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
4867 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
4868 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4869 {
4870 layout->set_has_static_tls();
4871 bool output_is_shared = parameters->options().shared();
4872 if (output_is_shared)
3a531937 4873 gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
8e33481e
HS
4874 object->name().c_str(), r_type);
4875 }
9363c7c3
JY
4876 break;
4877
9726c3c1
HS
4878 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
4879 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
4880 {
4881 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
4882 optimize_tls_reloc(!parameters->options().shared(), r_type);
4883 if (tlsopt == tls::TLSOPT_NONE)
4884 {
4885 // Create a GOT entry for the module index.
4886 target->got_mod_index_entry(symtab, layout, object);
4887 }
4888 else if (tlsopt != tls::TLSOPT_TO_LE)
4889 unsupported_reloc_local(object, r_type);
4890 }
4891 break;
4892
4893 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
4894 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6b0ad2eb
JY
4895 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
4896 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
9726c3c1
HS
4897 break;
4898
3a531937
JY
4899 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
4900 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
4901 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
4902 {
4903 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
4904 optimize_tls_reloc(!parameters->options().shared(), r_type);
4905 target->define_tls_base_symbol(symtab, layout);
4906 if (tlsopt == tls::TLSOPT_NONE)
4907 {
4908 // Create reserved PLT and GOT entries for the resolver.
4909 target->reserve_tlsdesc_entries(symtab, layout);
4910
4911 // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
4912 // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
4913 // entry needs to be in an area in .got.plt, not .got. Call
4914 // got_section to make sure the section has been created.
4915 target->got_section(symtab, layout);
4916 Output_data_got<size, big_endian>* got =
4917 target->got_tlsdesc_section();
4918 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4919 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
4920 {
4921 unsigned int got_offset = got->add_constant(0);
4922 got->add_constant(0);
4923 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
4924 got_offset);
4925 Reloc_section* rt = target->rela_tlsdesc_section(layout);
4926 // We store the arguments we need in a vector, and use
4927 // the index into the vector as the parameter to pass
4928 // to the target specific routines.
4929 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
4930 void* arg = reinterpret_cast<void*>(intarg);
4931 rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
4932 got, got_offset, 0);
4933 }
4934 }
4935 else if (tlsopt != tls::TLSOPT_TO_LE)
4936 unsupported_reloc_local(object, r_type);
4937 }
4938 break;
4939
4940 case elfcpp::R_AARCH64_TLSDESC_CALL:
4941 break;
4942
9363c7c3
JY
4943 default:
4944 unsupported_reloc_local(object, r_type);
053a4d68
JY
4945 }
4946}
4947
4948
4949// Report an unsupported relocation against a global symbol.
4950
4951template<int size, bool big_endian>
4952void
4953Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
4954 Sized_relobj_file<size, big_endian>* object,
4955 unsigned int r_type,
4956 Symbol* gsym)
4957{
4958 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4959 object->name().c_str(), r_type, gsym->demangled_name().c_str());
4960}
4961
4962template<int size, bool big_endian>
4963inline void
4964Target_aarch64<size, big_endian>::Scan::global(
9363c7c3
JY
4965 Symbol_table* symtab,
4966 Layout* layout,
4967 Target_aarch64<size, big_endian>* target,
8e33481e
HS
4968 Sized_relobj_file<size, big_endian> * object,
4969 unsigned int data_shndx,
4970 Output_section* output_section,
4971 const elfcpp::Rela<size, big_endian>& rela,
9363c7c3
JY
4972 unsigned int r_type,
4973 Symbol* gsym)
053a4d68 4974{
9726c3c1
HS
4975 // A STT_GNU_IFUNC symbol may require a PLT entry.
4976 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4977 && this->reloc_needs_plt_for_ifunc(object, r_type))
4978 target->make_plt_entry(symtab, layout, gsym);
4979
8e33481e
HS
4980 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
4981 Reloc_section;
3a531937
JY
4982 const AArch64_reloc_property* arp =
4983 aarch64_reloc_property_table->get_reloc_property(r_type);
4984 gold_assert(arp != NULL);
4985
9363c7c3
JY
4986 switch (r_type)
4987 {
8e33481e
HS
4988 case elfcpp::R_AARCH64_ABS16:
4989 case elfcpp::R_AARCH64_ABS32:
9363c7c3 4990 case elfcpp::R_AARCH64_ABS64:
8e33481e
HS
4991 {
4992 // Make a PLT entry if necessary.
4993 if (gsym->needs_plt_entry())
4994 {
4995 target->make_plt_entry(symtab, layout, gsym);
4996 // Since this is not a PC-relative relocation, we may be
4997 // taking the address of a function. In that case we need to
4998 // set the entry in the dynamic symbol table to the address of
4999 // the PLT entry.
5000 if (gsym->is_from_dynobj() && !parameters->options().shared())
5001 gsym->set_needs_dynsym_value();
5002 }
5003 // Make a dynamic relocation if necessary.
8e33481e
HS
5004 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
5005 {
5006 if (!parameters->options().output_is_position_independent()
5007 && gsym->may_need_copy_reloc())
5008 {
3a531937
JY
5009 target->copy_reloc(symtab, layout, object,
5010 data_shndx, output_section, gsym, rela);
8e33481e 5011 }
9726c3c1
HS
5012 else if (r_type == elfcpp::R_AARCH64_ABS64
5013 && gsym->type() == elfcpp::STT_GNU_IFUNC
5014 && gsym->can_use_relative_reloc(false)
5015 && !gsym->is_from_dynobj()
5016 && !gsym->is_undefined()
5017 && !gsym->is_preemptible())
5018 {
5019 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
5020 // symbol. This makes a function address in a PIE executable
5021 // match the address in a shared library that it links against.
5022 Reloc_section* rela_dyn =
5023 target->rela_irelative_section(layout);
5024 unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
5025 rela_dyn->add_symbolless_global_addend(gsym, r_type,
5026 output_section, object,
5027 data_shndx,
5028 rela.get_r_offset(),
5029 rela.get_r_addend());
5030 }
8e33481e
HS
5031 else if (r_type == elfcpp::R_AARCH64_ABS64
5032 && gsym->can_use_relative_reloc(false))
5033 {
5034 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5035 rela_dyn->add_global_relative(gsym,
5036 elfcpp::R_AARCH64_RELATIVE,
5037 output_section,
5038 object,
5039 data_shndx,
5040 rela.get_r_offset(),
5041 rela.get_r_addend(),
5042 false);
5043 }
5044 else
5045 {
5046 check_non_pic(object, r_type);
5047 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
5048 rela_dyn = target->rela_dyn_section(layout);
5049 rela_dyn->add_global(
5050 gsym, r_type, output_section, object,
5051 data_shndx, rela.get_r_offset(),rela.get_r_addend());
5052 }
5053 }
5054 }
5055 break;
5056
5057 case elfcpp::R_AARCH64_PREL16:
5058 case elfcpp::R_AARCH64_PREL32:
5059 case elfcpp::R_AARCH64_PREL64:
9363c7c3
JY
5060 // This is used to fill the GOT absolute address.
5061 if (gsym->needs_plt_entry())
5062 {
5063 target->make_plt_entry(symtab, layout, gsym);
5064 }
5065 break;
5066
3a531937
JY
5067 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
5068 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
5069 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
5070 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5071 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
5072 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
5073 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
5074 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
5075 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
8e33481e 5076 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
9363c7c3 5077 {
3a531937
JY
5078 if (gsym->needs_plt_entry())
5079 target->make_plt_entry(symtab, layout, gsym);
5080 // Make a dynamic relocation if necessary.
5081 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
5082 {
5083 if (parameters->options().output_is_executable()
5084 && gsym->may_need_copy_reloc())
5085 {
5086 target->copy_reloc(symtab, layout, object,
5087 data_shndx, output_section, gsym, rela);
5088 }
5089 }
9363c7c3
JY
5090 break;
5091 }
5092
5093 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5094 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5095 {
5096 // This pair of relocations is used to access a specific GOT entry.
5097 // Note a GOT entry is an *address* to a symbol.
5098 // The symbol requires a GOT entry
5099 Output_data_got_aarch64<size, big_endian>* got =
5100 target->got_section(symtab, layout);
5101 if (gsym->final_value_is_known())
5102 {
9726c3c1
HS
5103 // For a STT_GNU_IFUNC symbol we want the PLT address.
5104 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
5105 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5106 else
5107 got->add_global(gsym, GOT_TYPE_STANDARD);
9363c7c3
JY
5108 }
5109 else
5110 {
9726c3c1
HS
5111 // If this symbol is not fully resolved, we need to add a dynamic
5112 // relocation for it.
9363c7c3 5113 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9726c3c1
HS
5114
5115 // Use a GLOB_DAT rather than a RELATIVE reloc if:
5116 //
5117 // 1) The symbol may be defined in some other module.
5118 // 2) We are building a shared library and this is a protected
5119 // symbol; using GLOB_DAT means that the dynamic linker can use
5120 // the address of the PLT in the main executable when appropriate
5121 // so that function address comparisons work.
5122 // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
5123 // again so that function address comparisons work.
9363c7c3
JY
5124 if (gsym->is_from_dynobj()
5125 || gsym->is_undefined()
5126 || gsym->is_preemptible()
5127 || (gsym->visibility() == elfcpp::STV_PROTECTED
9726c3c1
HS
5128 && parameters->options().shared())
5129 || (gsym->type() == elfcpp::STT_GNU_IFUNC
5130 && parameters->options().output_is_position_independent()))
9363c7c3
JY
5131 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
5132 rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
5133 else
5134 {
9726c3c1
HS
5135 // For a STT_GNU_IFUNC symbol we want to write the PLT
5136 // offset into the GOT, so that function pointer
5137 // comparisons work correctly.
5138 bool is_new;
5139 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
5140 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
5141 else
5142 {
5143 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5144 // Tell the dynamic linker to use the PLT address
5145 // when resolving relocations.
5146 if (gsym->is_from_dynobj()
5147 && !parameters->options().shared())
5148 gsym->set_needs_dynsym_value();
5149 }
5150 if (is_new)
3a531937
JY
5151 {
5152 rela_dyn->add_global_relative(
5153 gsym, elfcpp::R_AARCH64_RELATIVE,
5154 got,
5155 gsym->got_offset(GOT_TYPE_STANDARD),
5156 0,
5157 false);
5158 }
9363c7c3
JY
5159 }
5160 }
5161 break;
5162 }
5163
8e33481e
HS
5164 case elfcpp::R_AARCH64_TSTBR14:
5165 case elfcpp::R_AARCH64_CONDBR19:
9363c7c3
JY
5166 case elfcpp::R_AARCH64_JUMP26:
5167 case elfcpp::R_AARCH64_CALL26:
5168 {
5169 if (gsym->final_value_is_known())
5170 break;
5171
5172 if (gsym->is_defined() &&
5173 !gsym->is_from_dynobj() &&
5174 !gsym->is_preemptible())
5175 break;
5176
5177 // Make plt entry for function call.
9363c7c3
JY
5178 target->make_plt_entry(symtab, layout, gsym);
5179 break;
5180 }
5181
3a531937
JY
5182 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5183 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // General dynamic
5184 {
5185 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5186 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5187 if (tlsopt == tls::TLSOPT_TO_LE)
5188 {
5189 layout->set_has_static_tls();
5190 break;
5191 }
5192 gold_assert(tlsopt == tls::TLSOPT_NONE);
5193
5194 // General dynamic.
5195 Output_data_got_aarch64<size, big_endian>* got =
5196 target->got_section(symtab, layout);
5197 // Create 2 consecutive entries for module index and offset.
5198 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
5199 target->rela_dyn_section(layout),
5200 elfcpp::R_AARCH64_TLS_DTPMOD64,
5201 elfcpp::R_AARCH64_TLS_DTPREL64);
5202 }
5203 break;
5204
9726c3c1
HS
5205 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5206 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local dynamic
5207 {
5208 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5209 optimize_tls_reloc(!parameters->options().shared(), r_type);
5210 if (tlsopt == tls::TLSOPT_NONE)
5211 {
5212 // Create a GOT entry for the module index.
5213 target->got_mod_index_entry(symtab, layout, object);
5214 }
5215 else if (tlsopt != tls::TLSOPT_TO_LE)
5216 unsupported_reloc_local(object, r_type);
5217 }
5218 break;
5219
5220 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6b0ad2eb
JY
5221 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5222 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5223 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local dynamic
9726c3c1
HS
5224 break;
5225
8e33481e 5226 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3a531937 5227 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial executable
8e33481e 5228 {
9726c3c1 5229 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
3a531937
JY
5230 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5231 if (tlsopt == tls::TLSOPT_TO_LE)
5232 break;
5233
8e33481e
HS
5234 layout->set_has_static_tls();
5235 // Create a GOT entry for the tp-relative offset.
5236 Output_data_got_aarch64<size, big_endian>* got
5237 = target->got_section(symtab, layout);
5238 if (!parameters->doing_static_link())
5239 {
5240 got->add_global_with_rel(
5241 gsym, GOT_TYPE_TLS_OFFSET,
5242 target->rela_dyn_section(layout),
5243 elfcpp::R_AARCH64_TLS_TPREL64);
5244 }
5245 if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
5246 {
5247 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
5248 unsigned int got_offset =
5249 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
5250 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5251 gold_assert(addend == 0);
5252 got->add_static_reloc(got_offset,
5253 elfcpp::R_AARCH64_TLS_TPREL64, gsym);
5254 }
5255 }
5256 break;
5257
1a920511
JY
5258 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5259 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5260 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5261 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5262 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
8e33481e
HS
5263 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5264 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
3a531937 5265 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: // Local executable
8e33481e
HS
5266 layout->set_has_static_tls();
5267 if (parameters->options().shared())
5268 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
5269 object->name().c_str(), r_type);
5270 break;
5271
3a531937
JY
5272 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5273 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5274 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12: // TLS descriptor
5275 {
5276 target->define_tls_base_symbol(symtab, layout);
5277 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5278 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5279 if (tlsopt == tls::TLSOPT_NONE)
5280 {
5281 // Create reserved PLT and GOT entries for the resolver.
5282 target->reserve_tlsdesc_entries(symtab, layout);
5283
5284 // Create a double GOT entry with an R_AARCH64_TLSDESC
5285 // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
5286 // entry needs to be in an area in .got.plt, not .got. Call
5287 // got_section to make sure the section has been created.
5288 target->got_section(symtab, layout);
5289 Output_data_got<size, big_endian>* got =
5290 target->got_tlsdesc_section();
5291 Reloc_section* rt = target->rela_tlsdesc_section(layout);
5292 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
5293 elfcpp::R_AARCH64_TLSDESC, 0);
5294 }
5295 else if (tlsopt == tls::TLSOPT_TO_IE)
5296 {
5297 // Create a GOT entry for the tp-relative offset.
5298 Output_data_got<size, big_endian>* got
5299 = target->got_section(symtab, layout);
5300 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
5301 target->rela_dyn_section(layout),
5302 elfcpp::R_AARCH64_TLS_TPREL64);
5303 }
5304 else if (tlsopt != tls::TLSOPT_TO_LE)
5305 unsupported_reloc_global(object, r_type, gsym);
5306 }
5307 break;
5308
5309 case elfcpp::R_AARCH64_TLSDESC_CALL:
5310 break;
5311
9363c7c3 5312 default:
8e33481e 5313 gold_error(_("%s: unsupported reloc type in global scan"),
3a531937
JY
5314 aarch64_reloc_property_table->
5315 reloc_name_in_error_message(r_type).c_str());
9363c7c3 5316 }
053a4d68 5317 return;
9363c7c3
JY
5318} // End of Scan::global
5319
3a531937 5320
9363c7c3
JY
5321// Create the PLT section.
5322template<int size, bool big_endian>
5323void
5324Target_aarch64<size, big_endian>::make_plt_section(
5325 Symbol_table* symtab, Layout* layout)
5326{
5327 if (this->plt_ == NULL)
5328 {
5329 // Create the GOT section first.
5330 this->got_section(symtab, layout);
5331
3a531937
JY
5332 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
5333 this->got_irelative_);
9363c7c3
JY
5334
5335 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
5336 (elfcpp::SHF_ALLOC
5337 | elfcpp::SHF_EXECINSTR),
5338 this->plt_, ORDER_PLT, false);
5339
5340 // Make the sh_info field of .rela.plt point to .plt.
5341 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
5342 rela_plt_os->set_info_section(this->plt_->output_section());
5343 }
5344}
5345
3a531937
JY
5346// Return the section for TLSDESC relocations.
5347
5348template<int size, bool big_endian>
5349typename Target_aarch64<size, big_endian>::Reloc_section*
5350Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
5351{
5352 return this->plt_section()->rela_tlsdesc(layout);
5353}
5354
9363c7c3
JY
5355// Create a PLT entry for a global symbol.
5356
5357template<int size, bool big_endian>
5358void
5359Target_aarch64<size, big_endian>::make_plt_entry(
5360 Symbol_table* symtab,
5361 Layout* layout,
5362 Symbol* gsym)
5363{
5364 if (gsym->has_plt_offset())
5365 return;
5366
5367 if (this->plt_ == NULL)
5368 this->make_plt_section(symtab, layout);
5369
9726c3c1
HS
5370 this->plt_->add_entry(symtab, layout, gsym);
5371}
5372
5373// Make a PLT entry for a local STT_GNU_IFUNC symbol.
5374
5375template<int size, bool big_endian>
5376void
5377Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
5378 Symbol_table* symtab, Layout* layout,
5379 Sized_relobj_file<size, big_endian>* relobj,
5380 unsigned int local_sym_index)
5381{
5382 if (relobj->local_has_plt_offset(local_sym_index))
5383 return;
5384 if (this->plt_ == NULL)
5385 this->make_plt_section(symtab, layout);
5386 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
5387 relobj,
5388 local_sym_index);
5389 relobj->set_local_plt_offset(local_sym_index, plt_offset);
053a4d68
JY
5390}
5391
5392template<int size, bool big_endian>
5393void
5394Target_aarch64<size, big_endian>::gc_process_relocs(
5395 Symbol_table* symtab,
5396 Layout* layout,
5397 Sized_relobj_file<size, big_endian>* object,
5398 unsigned int data_shndx,
5399 unsigned int sh_type,
5400 const unsigned char* prelocs,
5401 size_t reloc_count,
5402 Output_section* output_section,
5403 bool needs_special_offset_handling,
5404 size_t local_symbol_count,
5405 const unsigned char* plocal_symbols)
5406{
5407 if (sh_type == elfcpp::SHT_REL)
5408 {
5409 return;
5410 }
5411
9363c7c3
JY
5412 gold::gc_process_relocs<
5413 size, big_endian,
5414 Target_aarch64<size, big_endian>,
5415 elfcpp::SHT_RELA,
5416 typename Target_aarch64<size, big_endian>::Scan,
5417 typename Target_aarch64<size, big_endian>::Relocatable_size_for_reloc>(
053a4d68
JY
5418 symtab,
5419 layout,
5420 this,
5421 object,
5422 data_shndx,
5423 prelocs,
5424 reloc_count,
5425 output_section,
5426 needs_special_offset_handling,
5427 local_symbol_count,
5428 plocal_symbols);
5429}
5430
5431// Scan relocations for a section.
5432
5433template<int size, bool big_endian>
5434void
5435Target_aarch64<size, big_endian>::scan_relocs(
5436 Symbol_table* symtab,
5437 Layout* layout,
5438 Sized_relobj_file<size, big_endian>* object,
5439 unsigned int data_shndx,
5440 unsigned int sh_type,
5441 const unsigned char* prelocs,
5442 size_t reloc_count,
5443 Output_section* output_section,
5444 bool needs_special_offset_handling,
5445 size_t local_symbol_count,
5446 const unsigned char* plocal_symbols)
5447{
5448 if (sh_type == elfcpp::SHT_REL)
5449 {
5450 gold_error(_("%s: unsupported REL reloc section"),
5451 object->name().c_str());
5452 return;
5453 }
9363c7c3 5454 gold::scan_relocs<size, big_endian, Target_aarch64, elfcpp::SHT_RELA, Scan>(
053a4d68
JY
5455 symtab,
5456 layout,
5457 this,
5458 object,
5459 data_shndx,
5460 prelocs,
5461 reloc_count,
5462 output_section,
5463 needs_special_offset_handling,
5464 local_symbol_count,
5465 plocal_symbols);
5466}
5467
3a531937
JY
5468// Return the value to use for a dynamic which requires special
5469// treatment. This is how we support equality comparisons of function
5470// pointers across shared library boundaries, as described in the
5471// processor specific ABI supplement.
5472
83a01957 5473template<int size, bool big_endian>
3a531937 5474uint64_t
83a01957 5475Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
3a531937
JY
5476{
5477 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5478 return this->plt_address_for_global(gsym);
5479}
5480
83a01957 5481
053a4d68
JY
5482// Finalize the sections.
5483
5484template<int size, bool big_endian>
5485void
5486Target_aarch64<size, big_endian>::do_finalize_sections(
9363c7c3 5487 Layout* layout,
053a4d68 5488 const Input_objects*,
9363c7c3 5489 Symbol_table* symtab)
053a4d68 5490{
9363c7c3
JY
5491 const Reloc_section* rel_plt = (this->plt_ == NULL
5492 ? NULL
5493 : this->plt_->rela_plt());
5494 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
5495 this->rela_dyn_, true, false);
5496
3a531937
JY
5497 // Emit any relocs we saved in an attempt to avoid generating COPY
5498 // relocs.
5499 if (this->copy_relocs_.any_saved_relocs())
5500 this->copy_relocs_.emit(this->rela_dyn_section(layout));
5501
5502 // Fill in some more dynamic tags.
5503 Output_data_dynamic* const odyn = layout->dynamic_data();
5504 if (odyn != NULL)
5505 {
5506 if (this->plt_ != NULL
5507 && this->plt_->output_section() != NULL
5508 && this->plt_ ->has_tlsdesc_entry())
5509 {
5510 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
5511 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
5512 this->got_->finalize_data_size();
5513 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
5514 this->plt_, plt_offset);
5515 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
5516 this->got_, got_offset);
5517 }
5518 }
5519
9363c7c3
JY
5520 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
5521 // the .got.plt section.
5522 Symbol* sym = this->global_offset_table_;
5523 if (sym != NULL)
5524 {
5525 uint64_t data_size = this->got_plt_->current_data_size();
5526 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
5527
5528 // If the .got section is more than 0x8000 bytes, we add
5529 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
5530 // bit relocations have a greater chance of working.
5531 if (data_size >= 0x8000)
5532 symtab->get_sized_symbol<size>(sym)->set_value(
5533 symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
5534 }
5535
5536 if (parameters->doing_static_link()
5537 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
5538 {
5539 // If linking statically, make sure that the __rela_iplt symbols
5540 // were defined if necessary, even if we didn't create a PLT.
5541 static const Define_symbol_in_segment syms[] =
5542 {
5543 {
5544 "__rela_iplt_start", // name
5545 elfcpp::PT_LOAD, // segment_type
5546 elfcpp::PF_W, // segment_flags_set
5547 elfcpp::PF(0), // segment_flags_clear
5548 0, // value
5549 0, // size
5550 elfcpp::STT_NOTYPE, // type
5551 elfcpp::STB_GLOBAL, // binding
5552 elfcpp::STV_HIDDEN, // visibility
5553 0, // nonvis
5554 Symbol::SEGMENT_START, // offset_from_base
5555 true // only_if_ref
5556 },
5557 {
5558 "__rela_iplt_end", // name
5559 elfcpp::PT_LOAD, // segment_type
5560 elfcpp::PF_W, // segment_flags_set
5561 elfcpp::PF(0), // segment_flags_clear
5562 0, // value
5563 0, // size
5564 elfcpp::STT_NOTYPE, // type
5565 elfcpp::STB_GLOBAL, // binding
5566 elfcpp::STV_HIDDEN, // visibility
5567 0, // nonvis
5568 Symbol::SEGMENT_START, // offset_from_base
5569 true // only_if_ref
5570 }
5571 };
5572
5573 symtab->define_symbols(layout, 2, syms,
5574 layout->script_options()->saw_sections_clause());
5575 }
5576
053a4d68
JY
5577 return;
5578}
5579
5580// Perform a relocation.
5581
5582template<int size, bool big_endian>
5583inline bool
5584Target_aarch64<size, big_endian>::Relocate::relocate(
9363c7c3
JY
5585 const Relocate_info<size, big_endian>* relinfo,
5586 Target_aarch64<size, big_endian>* target,
053a4d68 5587 Output_section* ,
9363c7c3
JY
5588 size_t relnum,
5589 const elfcpp::Rela<size, big_endian>& rela,
5590 unsigned int r_type,
5591 const Sized_symbol<size>* gsym,
5592 const Symbol_value<size>* psymval,
5593 unsigned char* view,
5594 typename elfcpp::Elf_types<size>::Elf_Addr address,
053a4d68
JY
5595 section_size_type /* view_size */)
5596{
9363c7c3
JY
5597 if (view == NULL)
5598 return true;
5599
5600 typedef AArch64_relocate_functions<size, big_endian> Reloc;
5601
5602 const AArch64_reloc_property* reloc_property =
5603 aarch64_reloc_property_table->get_reloc_property(r_type);
5604
5605 if (reloc_property == NULL)
5606 {
5607 std::string reloc_name =
5608 aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
5609 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5610 _("cannot relocate %s in object file"),
5611 reloc_name.c_str());
5612 return true;
5613 }
5614
5615 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
5616
5617 // Pick the value to use for symbols defined in the PLT.
5618 Symbol_value<size> symval;
5619 if (gsym != NULL
5620 && gsym->use_plt_offset(reloc_property->reference_flags()))
5621 {
5622 symval.set_output_value(target->plt_address_for_global(gsym));
5623 psymval = &symval;
5624 }
5625 else if (gsym == NULL && psymval->is_ifunc_symbol())
5626 {
5627 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5628 if (object->local_has_plt_offset(r_sym))
5629 {
5630 symval.set_output_value(target->plt_address_for_local(object, r_sym));
5631 psymval = &symval;
5632 }
5633 }
5634
5635 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5636
5637 // Get the GOT offset if needed.
5638 // For aarch64, the GOT pointer points to the start of the GOT section.
5639 bool have_got_offset = false;
5640 int got_offset = 0;
5641 int got_base = (target->got_ != NULL
5642 ? (target->got_->current_data_size() >= 0x8000
5643 ? 0x8000 : 0)
5644 : 0);
5645 switch (r_type)
5646 {
5647 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
5648 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
5649 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
5650 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
5651 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
5652 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
5653 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
5654 case elfcpp::R_AARCH64_GOTREL64:
5655 case elfcpp::R_AARCH64_GOTREL32:
5656 case elfcpp::R_AARCH64_GOT_LD_PREL19:
5657 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
5658 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5659 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5660 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
5661 if (gsym != NULL)
5662 {
5663 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5664 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
5665 }
5666 else
5667 {
5668 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5669 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5670 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
5671 - got_base);
5672 }
5673 have_got_offset = true;
5674 break;
8e33481e 5675
9363c7c3
JY
5676 default:
5677 break;
5678 }
5679
5680 typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
5681 typename elfcpp::Elf_types<size>::Elf_Addr value;
5682 switch (r_type)
5683 {
5684 case elfcpp::R_AARCH64_NONE:
5685 break;
5686
5687 case elfcpp::R_AARCH64_ABS64:
5688 reloc_status = Reloc::template rela_ua<64>(
5689 view, object, psymval, addend, reloc_property);
5690 break;
5691
5692 case elfcpp::R_AARCH64_ABS32:
5693 reloc_status = Reloc::template rela_ua<32>(
5694 view, object, psymval, addend, reloc_property);
5695 break;
5696
5697 case elfcpp::R_AARCH64_ABS16:
5698 reloc_status = Reloc::template rela_ua<16>(
5699 view, object, psymval, addend, reloc_property);
5700 break;
5701
5702 case elfcpp::R_AARCH64_PREL64:
5703 reloc_status = Reloc::template pcrela_ua<64>(
5704 view, object, psymval, addend, address, reloc_property);
83a01957 5705 break;
9363c7c3
JY
5706
5707 case elfcpp::R_AARCH64_PREL32:
5708 reloc_status = Reloc::template pcrela_ua<32>(
5709 view, object, psymval, addend, address, reloc_property);
83a01957 5710 break;
9363c7c3
JY
5711
5712 case elfcpp::R_AARCH64_PREL16:
5713 reloc_status = Reloc::template pcrela_ua<16>(
5714 view, object, psymval, addend, address, reloc_property);
83a01957 5715 break;
9363c7c3 5716
9726c3c1
HS
5717 case elfcpp::R_AARCH64_LD_PREL_LO19:
5718 reloc_status = Reloc::template pcrela_general<32>(
5719 view, object, psymval, addend, address, reloc_property);
5720 break;
5721
5722 case elfcpp::R_AARCH64_ADR_PREL_LO21:
5723 reloc_status = Reloc::adr(view, object, psymval, addend,
5724 address, reloc_property);
5725 break;
5726
9363c7c3
JY
5727 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
5728 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
5729 reloc_status = Reloc::adrp(view, object, psymval, addend, address,
5730 reloc_property);
5731 break;
5732
5733 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
5734 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
5735 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
5736 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
5737 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
5738 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
5739 reloc_status = Reloc::template rela_general<32>(
5740 view, object, psymval, addend, reloc_property);
5741 break;
5742
3a531937
JY
5743 case elfcpp::R_AARCH64_CALL26:
5744 if (this->skip_call_tls_get_addr_)
5745 {
5746 // Double check that the TLSGD insn has been optimized away.
5747 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
5748 Insntype insn = elfcpp::Swap<32, big_endian>::readval(
5749 reinterpret_cast<Insntype*>(view));
5750 gold_assert((insn & 0xff000000) == 0x91000000);
5751
5752 reloc_status = Reloc::STATUS_OKAY;
5753 this->skip_call_tls_get_addr_ = false;
5754 // Return false to stop further processing this reloc.
5755 return false;
5756 }
83a01957
HS
5757 // Fallthrough
5758 case elfcpp::R_AARCH64_JUMP26:
5759 if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
0bf32ea9
JY
5760 gsym, psymval, object,
5761 target->stub_group_size_))
83a01957
HS
5762 break;
5763 // Fallthrough
8e33481e
HS
5764 case elfcpp::R_AARCH64_TSTBR14:
5765 case elfcpp::R_AARCH64_CONDBR19:
9363c7c3
JY
5766 reloc_status = Reloc::template pcrela_general<32>(
5767 view, object, psymval, addend, address, reloc_property);
5768 break;
5769
5770 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5771 gold_assert(have_got_offset);
5772 value = target->got_->address() + got_base + got_offset;
5773 reloc_status = Reloc::adrp(view, value + addend, address);
5774 break;
5775
5776 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5777 gold_assert(have_got_offset);
5778 value = target->got_->address() + got_base + got_offset;
5779 reloc_status = Reloc::template rela_general<32>(
5780 view, value, addend, reloc_property);
5781 break;
5782
3a531937
JY
5783 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5784 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
9726c3c1
HS
5785 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5786 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5787 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5788 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6b0ad2eb
JY
5789 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5790 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
8e33481e
HS
5791 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5792 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1a920511
JY
5793 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5794 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5795 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5796 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5797 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
8e33481e
HS
5798 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5799 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5800 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
3a531937
JY
5801 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5802 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5803 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5804 case elfcpp::R_AARCH64_TLSDESC_CALL:
8e33481e
HS
5805 reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
5806 gsym, psymval, view, address);
5807 break;
5808
3a531937
JY
5809 // These are dynamic relocations, which are unexpected when linking.
5810 case elfcpp::R_AARCH64_COPY:
5811 case elfcpp::R_AARCH64_GLOB_DAT:
5812 case elfcpp::R_AARCH64_JUMP_SLOT:
5813 case elfcpp::R_AARCH64_RELATIVE:
5814 case elfcpp::R_AARCH64_IRELATIVE:
5815 case elfcpp::R_AARCH64_TLS_DTPREL64:
5816 case elfcpp::R_AARCH64_TLS_DTPMOD64:
5817 case elfcpp::R_AARCH64_TLS_TPREL64:
5818 case elfcpp::R_AARCH64_TLSDESC:
9363c7c3 5819 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3a531937 5820 _("unexpected reloc %u in object file"),
9363c7c3
JY
5821 r_type);
5822 break;
3a531937
JY
5823
5824 default:
5825 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5826 _("unsupported reloc %s"),
5827 reloc_property->name().c_str());
5828 break;
9363c7c3
JY
5829 }
5830
5831 // Report any errors.
5832 switch (reloc_status)
5833 {
5834 case Reloc::STATUS_OKAY:
5835 break;
5836 case Reloc::STATUS_OVERFLOW:
5837 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5838 _("relocation overflow in %s"),
5839 reloc_property->name().c_str());
5840 break;
5841 case Reloc::STATUS_BAD_RELOC:
5842 gold_error_at_location(
5843 relinfo,
5844 relnum,
5845 rela.get_r_offset(),
5846 _("unexpected opcode while processing relocation %s"),
5847 reloc_property->name().c_str());
5848 break;
5849 default:
5850 gold_unreachable();
5851 }
5852
053a4d68
JY
5853 return true;
5854}
5855
3a531937 5856
8e33481e
HS
5857template<int size, bool big_endian>
5858inline
83a01957 5859typename AArch64_relocate_functions<size, big_endian>::Status
8e33481e 5860Target_aarch64<size, big_endian>::Relocate::relocate_tls(
83a01957 5861 const Relocate_info<size, big_endian>* relinfo,
3a531937
JY
5862 Target_aarch64<size, big_endian>* target,
5863 size_t relnum,
5864 const elfcpp::Rela<size, big_endian>& rela,
5865 unsigned int r_type, const Sized_symbol<size>* gsym,
5866 const Symbol_value<size>* psymval,
5867 unsigned char* view,
8e33481e
HS
5868 typename elfcpp::Elf_types<size>::Elf_Addr address)
5869{
83a01957 5870 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
3a531937 5871 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
8e33481e 5872
3a531937
JY
5873 Output_segment* tls_segment = relinfo->layout->tls_segment();
5874 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5875 const AArch64_reloc_property* reloc_property =
5876 aarch64_reloc_property_table->get_reloc_property(r_type);
8e33481e
HS
5877 gold_assert(reloc_property != NULL);
5878
3a531937
JY
5879 const bool is_final = (gsym == NULL
5880 ? !parameters->options().shared()
5881 : gsym->final_value_is_known());
5882 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5883 optimize_tls_reloc(is_final, r_type);
5884
83a01957 5885 Sized_relobj_file<size, big_endian>* object = relinfo->object;
3a531937 5886 int tls_got_offset_type;
8e33481e
HS
5887 switch (r_type)
5888 {
3a531937
JY
5889 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5890 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // Global-dynamic
5891 {
5892 if (tlsopt == tls::TLSOPT_TO_LE)
5893 {
5894 if (tls_segment == NULL)
5895 {
5896 gold_assert(parameters->errors()->error_count() > 0
5897 || issue_undefined_symbol_error(gsym));
5898 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
5899 }
5900 return tls_gd_to_le(relinfo, target, rela, r_type, view,
5901 psymval);
5902 }
5903 else if (tlsopt == tls::TLSOPT_NONE)
5904 {
5905 tls_got_offset_type = GOT_TYPE_TLS_PAIR;
5906 // Firstly get the address for the got entry.
5907 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
5908 if (gsym != NULL)
5909 {
5910 gold_assert(gsym->has_got_offset(tls_got_offset_type));
5911 got_entry_address = target->got_->address() +
5912 gsym->got_offset(tls_got_offset_type);
5913 }
5914 else
5915 {
5916 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5917 gold_assert(
5918 object->local_has_got_offset(r_sym, tls_got_offset_type));
5919 got_entry_address = target->got_->address() +
5920 object->local_got_offset(r_sym, tls_got_offset_type);
5921 }
5922
5923 // Relocate the address into adrp/ld, adrp/add pair.
5924 switch (r_type)
5925 {
5926 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5927 return aarch64_reloc_funcs::adrp(
5928 view, got_entry_address + addend, address);
5929
5930 break;
5931
5932 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5933 return aarch64_reloc_funcs::template rela_general<32>(
5934 view, got_entry_address, addend, reloc_property);
5935 break;
5936
5937 default:
9726c3c1 5938 gold_unreachable();
3a531937
JY
5939 }
5940 }
5941 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5942 _("unsupported gd_to_ie relaxation on %u"),
5943 r_type);
5944 }
5945 break;
5946
9726c3c1
HS
5947 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5948 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local-dynamic
5949 {
5950 if (tlsopt == tls::TLSOPT_TO_LE)
5951 {
5952 if (tls_segment == NULL)
5953 {
5954 gold_assert(parameters->errors()->error_count() > 0
5955 || issue_undefined_symbol_error(gsym));
5956 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
5957 }
5958 return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
5959 psymval);
5960 }
5961
5962 gold_assert(tlsopt == tls::TLSOPT_NONE);
5963 // Relocate the field with the offset of the GOT entry for
5964 // the module index.
5965 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
5966 got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
5967 target->got_->address());
5968
5969 switch (r_type)
5970 {
5971 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5972 return aarch64_reloc_funcs::adrp(
5973 view, got_entry_address + addend, address);
5974 break;
5975
5976 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5977 return aarch64_reloc_funcs::template rela_general<32>(
5978 view, got_entry_address, addend, reloc_property);
5979 break;
5980
5981 default:
5982 gold_unreachable();
5983 }
5984 }
5985 break;
5986
5987 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6b0ad2eb
JY
5988 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5989 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5990 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local-dynamic
9726c3c1
HS
5991 {
5992 AArch64_address value = psymval->value(object, 0);
5993 if (tlsopt == tls::TLSOPT_TO_LE)
5994 {
5995 if (tls_segment == NULL)
5996 {
5997 gold_assert(parameters->errors()->error_count() > 0
5998 || issue_undefined_symbol_error(gsym));
5999 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6000 }
9726c3c1
HS
6001 }
6002 switch (r_type)
6003 {
6004 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6005 return aarch64_reloc_funcs::movnz(view, value + addend,
6006 reloc_property);
6007 break;
6008
6009 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6b0ad2eb
JY
6010 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6011 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
9726c3c1
HS
6012 return aarch64_reloc_funcs::template rela_general<32>(
6013 view, value, addend, reloc_property);
6014 break;
6015
6016 default:
6017 gold_unreachable();
6018 }
6019 // We should never reach here.
6020 }
6021 break;
6022
8e33481e 6023 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3a531937 6024 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial-exec
8e33481e 6025 {
3a531937
JY
6026 if (tlsopt == tls::TLSOPT_TO_LE)
6027 {
6028 if (tls_segment == NULL)
6029 {
6030 gold_assert(parameters->errors()->error_count() > 0
6031 || issue_undefined_symbol_error(gsym));
6032 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6033 }
6034 return tls_ie_to_le(relinfo, target, rela, r_type, view,
6035 psymval);
6036 }
6037 tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
6038
6039 // Firstly get the address for the got entry.
8e33481e
HS
6040 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6041 if (gsym != NULL)
6042 {
3a531937 6043 gold_assert(gsym->has_got_offset(tls_got_offset_type));
8e33481e 6044 got_entry_address = target->got_->address() +
3a531937 6045 gsym->got_offset(tls_got_offset_type);
8e33481e
HS
6046 }
6047 else
6048 {
6049 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6050 gold_assert(
3a531937 6051 object->local_has_got_offset(r_sym, tls_got_offset_type));
8e33481e 6052 got_entry_address = target->got_->address() +
3a531937 6053 object->local_got_offset(r_sym, tls_got_offset_type);
8e33481e 6054 }
3a531937
JY
6055 // Relocate the address into adrp/ld, adrp/add pair.
6056 switch (r_type)
8e33481e 6057 {
3a531937
JY
6058 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6059 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
6060 address);
6061 break;
6062 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6063 return aarch64_reloc_funcs::template rela_general<32>(
6064 view, got_entry_address, addend, reloc_property);
6065 default:
9726c3c1 6066 gold_unreachable();
8e33481e 6067 }
8e33481e 6068 }
3a531937 6069 // We shall never reach here.
8e33481e
HS
6070 break;
6071
1a920511
JY
6072 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6073 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6074 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6075 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6076 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
8e33481e
HS
6077 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6078 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6079 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6080 {
8e33481e 6081 gold_assert(tls_segment != NULL);
3a531937 6082 AArch64_address value = psymval->value(object, 0);
8e33481e
HS
6083
6084 if (!parameters->options().shared())
6085 {
3a531937
JY
6086 AArch64_address aligned_tcb_size =
6087 align_address(target->tcb_size(),
6088 tls_segment->maximum_alignment());
1a920511
JY
6089 value += aligned_tcb_size;
6090 switch (r_type)
6091 {
6092 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6093 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6094 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6095 return aarch64_reloc_funcs::movnz(view, value + addend,
6096 reloc_property);
6097 default:
6098 return aarch64_reloc_funcs::template
6099 rela_general<32>(view,
6100 value,
6101 addend,
6102 reloc_property);
6103 }
8e33481e
HS
6104 }
6105 else
6106 gold_error(_("%s: unsupported reloc %u "
6107 "in non-static TLSLE mode."),
6108 object->name().c_str(), r_type);
6109 }
6110 break;
6111
3a531937
JY
6112 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6113 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6114 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6115 case elfcpp::R_AARCH64_TLSDESC_CALL:
6116 {
6117 if (tlsopt == tls::TLSOPT_TO_LE)
6118 {
6119 if (tls_segment == NULL)
6120 {
6121 gold_assert(parameters->errors()->error_count() > 0
6122 || issue_undefined_symbol_error(gsym));
6123 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6124 }
6125 return tls_desc_gd_to_le(relinfo, target, rela, r_type,
6126 view, psymval);
6127 }
6128 else
6129 {
6130 tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
6131 ? GOT_TYPE_TLS_OFFSET
6132 : GOT_TYPE_TLS_DESC);
6133 unsigned int got_tlsdesc_offset = 0;
6134 if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
6135 && tlsopt == tls::TLSOPT_NONE)
6136 {
6137 // We created GOT entries in the .got.tlsdesc portion of the
6138 // .got.plt section, but the offset stored in the symbol is the
6139 // offset within .got.tlsdesc.
6140 got_tlsdesc_offset = (target->got_->data_size()
6141 + target->got_plt_section()->data_size());
6142 }
6143 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6144 if (gsym != NULL)
6145 {
6146 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6147 got_entry_address = target->got_->address()
6148 + got_tlsdesc_offset
6149 + gsym->got_offset(tls_got_offset_type);
6150 }
6151 else
6152 {
6153 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6154 gold_assert(
6155 object->local_has_got_offset(r_sym, tls_got_offset_type));
6156 got_entry_address = target->got_->address() +
6157 got_tlsdesc_offset +
6158 object->local_got_offset(r_sym, tls_got_offset_type);
6159 }
6160 if (tlsopt == tls::TLSOPT_TO_IE)
6161 {
6162 if (tls_segment == NULL)
6163 {
6164 gold_assert(parameters->errors()->error_count() > 0
6165 || issue_undefined_symbol_error(gsym));
6166 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6167 }
6168 return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
6169 view, psymval, got_entry_address,
6170 address);
6171 }
6172
6173 // Now do tlsdesc relocation.
6174 switch (r_type)
6175 {
6176 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6177 return aarch64_reloc_funcs::adrp(view,
6178 got_entry_address + addend,
6179 address);
6180 break;
6181 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6182 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6183 return aarch64_reloc_funcs::template rela_general<32>(
6184 view, got_entry_address, addend, reloc_property);
6185 break;
6186 case elfcpp::R_AARCH64_TLSDESC_CALL:
6187 return aarch64_reloc_funcs::STATUS_OKAY;
6188 break;
6189 default:
6190 gold_unreachable();
6191 }
6192 }
6193 }
6194 break;
6195
8e33481e
HS
6196 default:
6197 gold_error(_("%s: unsupported TLS reloc %u."),
6198 object->name().c_str(), r_type);
6199 }
6200 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
3a531937
JY
6201} // End of relocate_tls.
6202
6203
6204template<int size, bool big_endian>
6205inline
83a01957 6206typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 6207Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
83a01957 6208 const Relocate_info<size, big_endian>* relinfo,
3a531937
JY
6209 Target_aarch64<size, big_endian>* target,
6210 const elfcpp::Rela<size, big_endian>& rela,
6211 unsigned int r_type,
6212 unsigned char* view,
6213 const Symbol_value<size>* psymval)
6214{
83a01957 6215 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
3a531937
JY
6216 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6217 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6218
6219 Insntype* ip = reinterpret_cast<Insntype*>(view);
6220 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
6221 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
6222 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
6223
6224 if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
6225 {
6226 // This is the 2nd relocs, optimization should already have been
6227 // done.
6228 gold_assert((insn1 & 0xfff00000) == 0x91400000);
6229 return aarch64_reloc_funcs::STATUS_OKAY;
6230 }
6231
6232 // The original sequence is -
6233 // 90000000 adrp x0, 0 <main>
6234 // 91000000 add x0, x0, #0x0
6235 // 94000000 bl 0 <__tls_get_addr>
6236 // optimized to sequence -
6237 // d53bd040 mrs x0, tpidr_el0
6238 // 91400000 add x0, x0, #0x0, lsl #12
6239 // 91000000 add x0, x0, #0x0
6240
6241 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
6242 // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
6243 // have to change "bl tls_get_addr", which does not have a corresponding tls
6244 // relocation type. So before proceeding, we need to make sure compiler
6245 // does not change the sequence.
6246 if(!(insn1 == 0x90000000 // adrp x0,0
6247 && insn2 == 0x91000000 // add x0, x0, #0x0
6248 && insn3 == 0x94000000)) // bl 0
6249 {
6250 // Ideally we should give up gd_to_le relaxation and do gd access.
6251 // However the gd_to_le relaxation decision has been made early
6252 // in the scan stage, where we did not allocate any GOT entry for
6253 // this symbol. Therefore we have to exit and report error now.
6254 gold_error(_("unexpected reloc insn sequence while relaxing "
6255 "tls gd to le for reloc %u."), r_type);
6256 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6257 }
6258
6259 // Write new insns.
6260 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
6261 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
6262 insn3 = 0x91000000; // add x0, x0, #0x0
6263 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
6264 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
6265 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
6266
6267 // Calculate tprel value.
6268 Output_segment* tls_segment = relinfo->layout->tls_segment();
6269 gold_assert(tls_segment != NULL);
6270 AArch64_address value = psymval->value(relinfo->object, 0);
6271 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6272 AArch64_address aligned_tcb_size =
6273 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6274 AArch64_address x = value + aligned_tcb_size;
6275
6276 // After new insns are written, apply TLSLE relocs.
6277 const AArch64_reloc_property* rp1 =
6278 aarch64_reloc_property_table->get_reloc_property(
6279 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
6280 const AArch64_reloc_property* rp2 =
6281 aarch64_reloc_property_table->get_reloc_property(
6282 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
6283 gold_assert(rp1 != NULL && rp2 != NULL);
6284
6285 typename aarch64_reloc_funcs::Status s1 =
6286 aarch64_reloc_funcs::template rela_general<32>(view + 4,
6287 x,
6288 addend,
6289 rp1);
6290 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
6291 return s1;
6292
6293 typename aarch64_reloc_funcs::Status s2 =
6294 aarch64_reloc_funcs::template rela_general<32>(view + 8,
6295 x,
6296 addend,
6297 rp2);
6298
6299 this->skip_call_tls_get_addr_ = true;
6300 return s2;
6301} // End of tls_gd_to_le
6302
6303
9726c3c1
HS
6304template<int size, bool big_endian>
6305inline
6306typename AArch64_relocate_functions<size, big_endian>::Status
6307Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
6308 const Relocate_info<size, big_endian>* relinfo,
6309 Target_aarch64<size, big_endian>* target,
6310 const elfcpp::Rela<size, big_endian>& rela,
6311 unsigned int r_type,
6312 unsigned char* view,
6313 const Symbol_value<size>* psymval)
6314{
6315 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6316 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6317 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6318
6319 Insntype* ip = reinterpret_cast<Insntype*>(view);
6320 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
6321 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
6322 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
6323
6324 if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
6325 {
6326 // This is the 2nd relocs, optimization should already have been
6327 // done.
6328 gold_assert((insn1 & 0xfff00000) == 0x91400000);
6329 return aarch64_reloc_funcs::STATUS_OKAY;
6330 }
6331
6332 // The original sequence is -
6333 // 90000000 adrp x0, 0 <main>
6334 // 91000000 add x0, x0, #0x0
6335 // 94000000 bl 0 <__tls_get_addr>
6336 // optimized to sequence -
6337 // d53bd040 mrs x0, tpidr_el0
6338 // 91400000 add x0, x0, #0x0, lsl #12
6339 // 91000000 add x0, x0, #0x0
6340
6341 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
6342 // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
6343 // have to change "bl tls_get_addr", which does not have a corresponding tls
6344 // relocation type. So before proceeding, we need to make sure compiler
6345 // does not change the sequence.
6346 if(!(insn1 == 0x90000000 // adrp x0,0
6347 && insn2 == 0x91000000 // add x0, x0, #0x0
6348 && insn3 == 0x94000000)) // bl 0
6349 {
6350 // Ideally we should give up gd_to_le relaxation and do gd access.
6351 // However the gd_to_le relaxation decision has been made early
6352 // in the scan stage, where we did not allocate any GOT entry for
6353 // this symbol. Therefore we have to exit and report error now.
6354 gold_error(_("unexpected reloc insn sequence while relaxing "
6355 "tls gd to le for reloc %u."), r_type);
6356 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6357 }
6358
6359 // Write new insns.
6360 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
6361 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
6362 insn3 = 0x91000000; // add x0, x0, #0x0
6363 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
6364 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
6365 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
6366
6367 // Calculate tprel value.
6368 Output_segment* tls_segment = relinfo->layout->tls_segment();
6369 gold_assert(tls_segment != NULL);
6370 AArch64_address value = psymval->value(relinfo->object, 0);
6371 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6372 AArch64_address aligned_tcb_size =
6373 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6374 AArch64_address x = value + aligned_tcb_size;
6375
6376 // After new insns are written, apply TLSLE relocs.
6377 const AArch64_reloc_property* rp1 =
6378 aarch64_reloc_property_table->get_reloc_property(
6379 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
6380 const AArch64_reloc_property* rp2 =
6381 aarch64_reloc_property_table->get_reloc_property(
6382 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
6383 gold_assert(rp1 != NULL && rp2 != NULL);
6384
6385 typename aarch64_reloc_funcs::Status s1 =
6386 aarch64_reloc_funcs::template rela_general<32>(view + 4,
6387 x,
6388 addend,
6389 rp1);
6390 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
6391 return s1;
6392
6393 typename aarch64_reloc_funcs::Status s2 =
6394 aarch64_reloc_funcs::template rela_general<32>(view + 8,
6395 x,
6396 addend,
6397 rp2);
6398
6399 this->skip_call_tls_get_addr_ = true;
6400 return s2;
6401
6402} // End of tls_ld_to_le
6403
3a531937
JY
6404template<int size, bool big_endian>
6405inline
83a01957 6406typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 6407Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
83a01957 6408 const Relocate_info<size, big_endian>* relinfo,
3a531937
JY
6409 Target_aarch64<size, big_endian>* target,
6410 const elfcpp::Rela<size, big_endian>& rela,
6411 unsigned int r_type,
6412 unsigned char* view,
6413 const Symbol_value<size>* psymval)
6414{
6415 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6416 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
83a01957 6417 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
3a531937
JY
6418
6419 AArch64_address value = psymval->value(relinfo->object, 0);
6420 Output_segment* tls_segment = relinfo->layout->tls_segment();
6421 AArch64_address aligned_tcb_address =
6422 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6423 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6424 AArch64_address x = value + addend + aligned_tcb_address;
6425 // "x" is the offset to tp, we can only do this if x is within
6426 // range [0, 2^32-1]
6427 if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
6428 {
6429 gold_error(_("TLS variable referred by reloc %u is too far from TP."),
6430 r_type);
6431 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6432 }
6433
6434 Insntype* ip = reinterpret_cast<Insntype*>(view);
6435 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
6436 unsigned int regno;
6437 Insntype newinsn;
6438 if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
6439 {
6440 // Generate movz.
6441 regno = (insn & 0x1f);
6442 newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
6443 }
6444 else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
6445 {
6446 // Generate movk.
6447 regno = (insn & 0x1f);
6448 gold_assert(regno == ((insn >> 5) & 0x1f));
6449 newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
6450 }
6451 else
9726c3c1 6452 gold_unreachable();
3a531937
JY
6453
6454 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
6455 return aarch64_reloc_funcs::STATUS_OKAY;
6456} // End of tls_ie_to_le
6457
6458
6459template<int size, bool big_endian>
6460inline
83a01957 6461typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 6462Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
83a01957 6463 const Relocate_info<size, big_endian>* relinfo,
3a531937
JY
6464 Target_aarch64<size, big_endian>* target,
6465 const elfcpp::Rela<size, big_endian>& rela,
6466 unsigned int r_type,
6467 unsigned char* view,
6468 const Symbol_value<size>* psymval)
6469{
6470 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6471 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
83a01957 6472 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
3a531937
JY
6473
6474 // TLSDESC-GD sequence is like:
6475 // adrp x0, :tlsdesc:v1
6476 // ldr x1, [x0, #:tlsdesc_lo12:v1]
6477 // add x0, x0, :tlsdesc_lo12:v1
6478 // .tlsdesccall v1
6479 // blr x1
6480 // After desc_gd_to_le optimization, the sequence will be like:
6481 // movz x0, #0x0, lsl #16
6482 // movk x0, #0x10
6483 // nop
6484 // nop
6485
6486 // Calculate tprel value.
6487 Output_segment* tls_segment = relinfo->layout->tls_segment();
6488 gold_assert(tls_segment != NULL);
6489 Insntype* ip = reinterpret_cast<Insntype*>(view);
6490 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6491 AArch64_address value = psymval->value(relinfo->object, addend);
6492 AArch64_address aligned_tcb_size =
6493 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6494 AArch64_address x = value + aligned_tcb_size;
6495 // x is the offset to tp, we can only do this if x is within range
6496 // [0, 2^32-1]. If x is out of range, fail and exit.
6497 if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
6498 {
6499 gold_error(_("TLS variable referred by reloc %u is too far from TP. "
6500 "We Can't do gd_to_le relaxation.\n"), r_type);
6501 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6502 }
6503 Insntype newinsn;
6504 switch (r_type)
6505 {
6506 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6507 case elfcpp::R_AARCH64_TLSDESC_CALL:
6508 // Change to nop
6509 newinsn = 0xd503201f;
6510 break;
6511
6512 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6513 // Change to movz.
6514 newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
6515 break;
6516
6517 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6518 // Change to movk.
6519 newinsn = 0xf2800000 | ((x & 0xffff) << 5);
6520 break;
6521
6522 default:
6523 gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
6524 r_type);
6525 gold_unreachable();
6526 }
6527 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
6528 return aarch64_reloc_funcs::STATUS_OKAY;
6529} // End of tls_desc_gd_to_le
6530
6531
6532template<int size, bool big_endian>
6533inline
83a01957 6534typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 6535Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
83a01957 6536 const Relocate_info<size, big_endian>* /* relinfo */,
3a531937
JY
6537 Target_aarch64<size, big_endian>* /* target */,
6538 const elfcpp::Rela<size, big_endian>& rela,
6539 unsigned int r_type,
6540 unsigned char* view,
6541 const Symbol_value<size>* /* psymval */,
6542 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
6543 typename elfcpp::Elf_types<size>::Elf_Addr address)
6544{
6545 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
83a01957 6546 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
3a531937
JY
6547
6548 // TLSDESC-GD sequence is like:
6549 // adrp x0, :tlsdesc:v1
6550 // ldr x1, [x0, #:tlsdesc_lo12:v1]
6551 // add x0, x0, :tlsdesc_lo12:v1
6552 // .tlsdesccall v1
6553 // blr x1
6554 // After desc_gd_to_ie optimization, the sequence will be like:
6555 // adrp x0, :tlsie:v1
6556 // ldr x0, [x0, :tlsie_lo12:v1]
6557 // nop
6558 // nop
6559
6560 Insntype* ip = reinterpret_cast<Insntype*>(view);
6561 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6562 Insntype newinsn;
6563 switch (r_type)
6564 {
6565 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6566 case elfcpp::R_AARCH64_TLSDESC_CALL:
6567 // Change to nop
6568 newinsn = 0xd503201f;
6569 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
6570 break;
6571
6572 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6573 {
6574 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
6575 address);
6576 }
6577 break;
6578
6579 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6580 {
bb779192
HS
6581 // Set ldr target register to be x0.
6582 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
6583 insn &= 0xffffffe0;
6584 elfcpp::Swap<32, big_endian>::writeval(ip, insn);
6585 // Do relocation.
3a531937
JY
6586 const AArch64_reloc_property* reloc_property =
6587 aarch64_reloc_property_table->get_reloc_property(
6588 elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
6589 return aarch64_reloc_funcs::template rela_general<32>(
6590 view, got_entry_address, addend, reloc_property);
6591 }
6592 break;
8e33481e 6593
3a531937
JY
6594 default:
6595 gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
6596 r_type);
6597 gold_unreachable();
6598 }
6599 return aarch64_reloc_funcs::STATUS_OKAY;
6600} // End of tls_desc_gd_to_ie
8e33481e 6601
053a4d68
JY
6602// Relocate section data.
6603
6604template<int size, bool big_endian>
6605void
6606Target_aarch64<size, big_endian>::relocate_section(
9363c7c3 6607 const Relocate_info<size, big_endian>* relinfo,
053a4d68 6608 unsigned int sh_type,
9363c7c3
JY
6609 const unsigned char* prelocs,
6610 size_t reloc_count,
6611 Output_section* output_section,
6612 bool needs_special_offset_handling,
6613 unsigned char* view,
6614 typename elfcpp::Elf_types<size>::Elf_Addr address,
6615 section_size_type view_size,
6616 const Reloc_symbol_changes* reloc_symbol_changes)
053a4d68 6617{
053a4d68 6618 gold_assert(sh_type == elfcpp::SHT_RELA);
9363c7c3
JY
6619 typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
6620 gold::relocate_section<size, big_endian, Target_aarch64, elfcpp::SHT_RELA,
6621 AArch64_relocate, gold::Default_comdat_behavior>(
6622 relinfo,
6623 this,
6624 prelocs,
6625 reloc_count,
6626 output_section,
6627 needs_special_offset_handling,
6628 view,
6629 address,
6630 view_size,
6631 reloc_symbol_changes);
053a4d68
JY
6632}
6633
6634// Return the size of a relocation while scanning during a relocatable
6635// link.
6636
6637template<int size, bool big_endian>
6638unsigned int
6639Target_aarch64<size, big_endian>::Relocatable_size_for_reloc::
6640get_size_for_reloc(
6641 unsigned int ,
6642 Relobj* )
6643{
6644 // We will never support SHT_REL relocations.
6645 gold_unreachable();
6646 return 0;
6647}
6648
6649// Scan the relocs during a relocatable link.
6650
6651template<int size, bool big_endian>
6652void
6653Target_aarch64<size, big_endian>::scan_relocatable_relocs(
8e33481e
HS
6654 Symbol_table* symtab,
6655 Layout* layout,
6656 Sized_relobj_file<size, big_endian>* object,
6657 unsigned int data_shndx,
053a4d68 6658 unsigned int sh_type,
8e33481e
HS
6659 const unsigned char* prelocs,
6660 size_t reloc_count,
6661 Output_section* output_section,
6662 bool needs_special_offset_handling,
6663 size_t local_symbol_count,
6664 const unsigned char* plocal_symbols,
6665 Relocatable_relocs* rr)
053a4d68 6666{
053a4d68 6667 gold_assert(sh_type == elfcpp::SHT_RELA);
8e33481e
HS
6668
6669 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
6670 Relocatable_size_for_reloc> Scan_relocatable_relocs;
6671
6672 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
6673 Scan_relocatable_relocs>(
6674 symtab,
6675 layout,
6676 object,
6677 data_shndx,
6678 prelocs,
6679 reloc_count,
6680 output_section,
6681 needs_special_offset_handling,
6682 local_symbol_count,
6683 plocal_symbols,
6684 rr);
053a4d68
JY
6685}
6686
6687// Relocate a section during a relocatable link.
6688
6689template<int size, bool big_endian>
6690void
6691Target_aarch64<size, big_endian>::relocate_relocs(
8e33481e 6692 const Relocate_info<size, big_endian>* relinfo,
053a4d68 6693 unsigned int sh_type,
8e33481e
HS
6694 const unsigned char* prelocs,
6695 size_t reloc_count,
6696 Output_section* output_section,
6697 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
6698 const Relocatable_relocs* rr,
6699 unsigned char* view,
6700 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
6701 section_size_type view_size,
6702 unsigned char* reloc_view,
6703 section_size_type reloc_view_size)
053a4d68 6704{
053a4d68 6705 gold_assert(sh_type == elfcpp::SHT_RELA);
8e33481e
HS
6706
6707 gold::relocate_relocs<size, big_endian, elfcpp::SHT_RELA>(
6708 relinfo,
6709 prelocs,
6710 reloc_count,
6711 output_section,
6712 offset_in_output_section,
6713 rr,
6714 view,
6715 view_address,
6716 view_size,
6717 reloc_view,
6718 reloc_view_size);
053a4d68
JY
6719}
6720
83a01957 6721
053a4d68
JY
6722// The selector for aarch64 object files.
6723
6724template<int size, bool big_endian>
6725class Target_selector_aarch64 : public Target_selector
6726{
6727 public:
9363c7c3 6728 Target_selector_aarch64();
053a4d68
JY
6729
6730 virtual Target*
6731 do_instantiate_target()
6732 { return new Target_aarch64<size, big_endian>(); }
6733};
6734
9363c7c3
JY
6735template<>
6736Target_selector_aarch64<32, true>::Target_selector_aarch64()
6737 : Target_selector(elfcpp::EM_AARCH64, 32, true,
6738 "elf32-bigaarch64", "aarch64_elf32_be_vec")
6739{ }
6740
6741template<>
6742Target_selector_aarch64<32, false>::Target_selector_aarch64()
6743 : Target_selector(elfcpp::EM_AARCH64, 32, false,
6744 "elf32-littleaarch64", "aarch64_elf32_le_vec")
6745{ }
6746
6747template<>
6748Target_selector_aarch64<64, true>::Target_selector_aarch64()
6749 : Target_selector(elfcpp::EM_AARCH64, 64, true,
6750 "elf64-bigaarch64", "aarch64_elf64_be_vec")
6751{ }
6752
6753template<>
6754Target_selector_aarch64<64, false>::Target_selector_aarch64()
6755 : Target_selector(elfcpp::EM_AARCH64, 64, false,
6756 "elf64-littleaarch64", "aarch64_elf64_le_vec")
6757{ }
6758
053a4d68
JY
6759Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
6760Target_selector_aarch64<32, false> target_selector_aarch64elf32;
6761Target_selector_aarch64<64, true> target_selector_aarch64elfb;
6762Target_selector_aarch64<64, false> target_selector_aarch64elf;
6763
053a4d68 6764} // End anonymous namespace.
This page took 0.362442 seconds and 4 git commands to generate.