Fix Fission (broken by my previous patch)
[deliverable/binutils-gdb.git] / gold / aarch64.cc
CommitLineData
053a4d68
JY
1// aarch64.cc -- aarch64 target support for gold.
2
2571583a 3// Copyright (C) 2014-2017 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>
5019d64a 26#include <map>
a48d0c12 27#include <set>
053a4d68
JY
28
29#include "elfcpp.h"
30#include "dwarf.h"
31#include "parameters.h"
32#include "reloc.h"
33#include "aarch64.h"
34#include "object.h"
35#include "symtab.h"
36#include "layout.h"
37#include "output.h"
38#include "copy-relocs.h"
39#include "target.h"
40#include "target-reloc.h"
41#include "target-select.h"
42#include "tls.h"
43#include "freebsd.h"
44#include "nacl.h"
45#include "gc.h"
46#include "icf.h"
9363c7c3 47#include "aarch64-reloc-property.h"
053a4d68
JY
48
49// The first three .got.plt entries are reserved.
50const int32_t AARCH64_GOTPLT_RESERVE_COUNT = 3;
51
83a01957 52
053a4d68
JY
53namespace
54{
55
56using namespace gold;
57
58template<int size, bool big_endian>
59class Output_data_plt_aarch64;
60
61template<int size, bool big_endian>
62class Output_data_plt_aarch64_standard;
63
64template<int size, bool big_endian>
65class Target_aarch64;
66
9363c7c3
JY
67template<int size, bool big_endian>
68class AArch64_relocate_functions;
69
5019d64a
HS
70// Utility class dealing with insns. This is ported from macros in
71// bfd/elfnn-aarch64.cc, but wrapped inside a class as static members. This
72// class is used in erratum sequence scanning.
73
74template<bool big_endian>
75class AArch64_insn_utilities
76{
77public:
78 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
79
2f0c79aa
HS
80 static const int BYTES_PER_INSN;
81
82 // Zero register encoding - 31.
83 static const unsigned int AARCH64_ZR;
5019d64a
HS
84
85 static unsigned int
86 aarch64_bit(Insntype insn, int pos)
87 { return ((1 << pos) & insn) >> pos; }
88
89 static unsigned int
90 aarch64_bits(Insntype insn, int pos, int l)
91 { return (insn >> pos) & ((1 << l) - 1); }
92
2f0c79aa
HS
93 // Get the encoding field "op31" of 3-source data processing insns. "op31" is
94 // the name defined in armv8 insn manual C3.5.9.
95 static unsigned int
96 aarch64_op31(Insntype insn)
97 { return aarch64_bits(insn, 21, 3); }
98
99 // Get the encoding field "ra" of 3-source data processing insns. "ra" is the
100 // third source register. See armv8 insn manual C3.5.9.
101 static unsigned int
102 aarch64_ra(Insntype insn)
103 { return aarch64_bits(insn, 10, 5); }
104
0ef3814f
HS
105 static bool
106 is_adr(const Insntype insn)
107 { return (insn & 0x9F000000) == 0x10000000; }
108
5019d64a
HS
109 static bool
110 is_adrp(const Insntype insn)
111 { return (insn & 0x9F000000) == 0x90000000; }
112
37de058a
JW
113 static bool
114 is_mrs_tpidr_el0(const Insntype insn)
115 { return (insn & 0xFFFFFFE0) == 0xd53bd040; }
116
5019d64a
HS
117 static unsigned int
118 aarch64_rm(const Insntype insn)
119 { return aarch64_bits(insn, 16, 5); }
120
121 static unsigned int
122 aarch64_rn(const Insntype insn)
123 { return aarch64_bits(insn, 5, 5); }
124
125 static unsigned int
126 aarch64_rd(const Insntype insn)
127 { return aarch64_bits(insn, 0, 5); }
128
129 static unsigned int
130 aarch64_rt(const Insntype insn)
131 { return aarch64_bits(insn, 0, 5); }
132
133 static unsigned int
134 aarch64_rt2(const Insntype insn)
135 { return aarch64_bits(insn, 10, 5); }
136
0ef3814f
HS
137 // Encode imm21 into adr. Signed imm21 is in the range of [-1M, 1M).
138 static Insntype
139 aarch64_adr_encode_imm(Insntype adr, int imm21)
140 {
141 gold_assert(is_adr(adr));
142 gold_assert(-(1 << 20) <= imm21 && imm21 < (1 << 20));
143 const int mask19 = (1 << 19) - 1;
144 const int mask2 = 3;
145 adr &= ~((mask19 << 5) | (mask2 << 29));
146 adr |= ((imm21 & mask2) << 29) | (((imm21 >> 2) & mask19) << 5);
147 return adr;
148 }
149
150 // Retrieve encoded adrp 33-bit signed imm value. This value is obtained by
151 // 21-bit signed imm encoded in the insn multiplied by 4k (page size) and
152 // 64-bit sign-extended, resulting in [-4G, 4G) with 12-lsb being 0.
153 static int64_t
154 aarch64_adrp_decode_imm(const Insntype adrp)
155 {
156 const int mask19 = (1 << 19) - 1;
157 const int mask2 = 3;
158 gold_assert(is_adrp(adrp));
159 // 21-bit imm encoded in adrp.
160 uint64_t imm = ((adrp >> 29) & mask2) | (((adrp >> 5) & mask19) << 2);
161 // Retrieve msb of 21-bit-signed imm for sign extension.
162 uint64_t msbt = (imm >> 20) & 1;
5c3024d2 163 // Real value is imm multiplied by 4k. Value now has 33-bit information.
0ef3814f
HS
164 int64_t value = imm << 12;
165 // Sign extend to 64-bit by repeating msbt 31 (64-33) times and merge it
166 // with value.
167 return ((((uint64_t)(1) << 32) - msbt) << 33) | value;
168 }
169
5019d64a
HS
170 static bool
171 aarch64_b(const Insntype insn)
172 { return (insn & 0xFC000000) == 0x14000000; }
173
174 static bool
175 aarch64_bl(const Insntype insn)
176 { return (insn & 0xFC000000) == 0x94000000; }
177
178 static bool
179 aarch64_blr(const Insntype insn)
180 { return (insn & 0xFFFFFC1F) == 0xD63F0000; }
181
182 static bool
183 aarch64_br(const Insntype insn)
184 { return (insn & 0xFFFFFC1F) == 0xD61F0000; }
185
186 // All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
187 // LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops.
188 static bool
189 aarch64_ld(Insntype insn) { return aarch64_bit(insn, 22) == 1; }
190
191 static bool
192 aarch64_ldst(Insntype insn)
193 { return (insn & 0x0a000000) == 0x08000000; }
194
195 static bool
196 aarch64_ldst_ex(Insntype insn)
197 { return (insn & 0x3f000000) == 0x08000000; }
198
199 static bool
200 aarch64_ldst_pcrel(Insntype insn)
201 { return (insn & 0x3b000000) == 0x18000000; }
202
203 static bool
204 aarch64_ldst_nap(Insntype insn)
205 { return (insn & 0x3b800000) == 0x28000000; }
206
207 static bool
208 aarch64_ldstp_pi(Insntype insn)
209 { return (insn & 0x3b800000) == 0x28800000; }
210
211 static bool
212 aarch64_ldstp_o(Insntype insn)
213 { return (insn & 0x3b800000) == 0x29000000; }
214
215 static bool
216 aarch64_ldstp_pre(Insntype insn)
217 { return (insn & 0x3b800000) == 0x29800000; }
218
219 static bool
220 aarch64_ldst_ui(Insntype insn)
221 { return (insn & 0x3b200c00) == 0x38000000; }
222
223 static bool
224 aarch64_ldst_piimm(Insntype insn)
225 { return (insn & 0x3b200c00) == 0x38000400; }
226
227 static bool
228 aarch64_ldst_u(Insntype insn)
229 { return (insn & 0x3b200c00) == 0x38000800; }
230
231 static bool
232 aarch64_ldst_preimm(Insntype insn)
233 { return (insn & 0x3b200c00) == 0x38000c00; }
234
235 static bool
236 aarch64_ldst_ro(Insntype insn)
237 { return (insn & 0x3b200c00) == 0x38200800; }
238
239 static bool
240 aarch64_ldst_uimm(Insntype insn)
241 { return (insn & 0x3b000000) == 0x39000000; }
242
243 static bool
244 aarch64_ldst_simd_m(Insntype insn)
245 { return (insn & 0xbfbf0000) == 0x0c000000; }
246
247 static bool
248 aarch64_ldst_simd_m_pi(Insntype insn)
249 { return (insn & 0xbfa00000) == 0x0c800000; }
250
251 static bool
252 aarch64_ldst_simd_s(Insntype insn)
253 { return (insn & 0xbf9f0000) == 0x0d000000; }
254
255 static bool
256 aarch64_ldst_simd_s_pi(Insntype insn)
257 { return (insn & 0xbf800000) == 0x0d800000; }
258
259 // Classify an INSN if it is indeed a load/store. Return true if INSN is a
260 // LD/ST instruction otherwise return false. For scalar LD/ST instructions
261 // PAIR is FALSE, RT is returned and RT2 is set equal to RT. For LD/ST pair
262 // instructions PAIR is TRUE, RT and RT2 are returned.
263 static bool
264 aarch64_mem_op_p(Insntype insn, unsigned int *rt, unsigned int *rt2,
265 bool *pair, bool *load)
266 {
267 uint32_t opcode;
268 unsigned int r;
269 uint32_t opc = 0;
270 uint32_t v = 0;
271 uint32_t opc_v = 0;
272
273 /* Bail out quickly if INSN doesn't fall into the the load-store
274 encoding space. */
275 if (!aarch64_ldst (insn))
276 return false;
277
278 *pair = false;
279 *load = false;
280 if (aarch64_ldst_ex (insn))
281 {
282 *rt = aarch64_rt (insn);
283 *rt2 = *rt;
284 if (aarch64_bit (insn, 21) == 1)
285 {
286 *pair = true;
287 *rt2 = aarch64_rt2 (insn);
288 }
289 *load = aarch64_ld (insn);
290 return true;
291 }
292 else if (aarch64_ldst_nap (insn)
293 || aarch64_ldstp_pi (insn)
294 || aarch64_ldstp_o (insn)
295 || aarch64_ldstp_pre (insn))
296 {
297 *pair = true;
298 *rt = aarch64_rt (insn);
299 *rt2 = aarch64_rt2 (insn);
300 *load = aarch64_ld (insn);
301 return true;
302 }
303 else if (aarch64_ldst_pcrel (insn)
304 || aarch64_ldst_ui (insn)
305 || aarch64_ldst_piimm (insn)
306 || aarch64_ldst_u (insn)
307 || aarch64_ldst_preimm (insn)
308 || aarch64_ldst_ro (insn)
309 || aarch64_ldst_uimm (insn))
310 {
311 *rt = aarch64_rt (insn);
312 *rt2 = *rt;
313 if (aarch64_ldst_pcrel (insn))
314 *load = true;
315 opc = aarch64_bits (insn, 22, 2);
316 v = aarch64_bit (insn, 26);
317 opc_v = opc | (v << 2);
318 *load = (opc_v == 1 || opc_v == 2 || opc_v == 3
319 || opc_v == 5 || opc_v == 7);
320 return true;
321 }
322 else if (aarch64_ldst_simd_m (insn)
323 || aarch64_ldst_simd_m_pi (insn))
324 {
325 *rt = aarch64_rt (insn);
326 *load = aarch64_bit (insn, 22);
327 opcode = (insn >> 12) & 0xf;
328 switch (opcode)
329 {
330 case 0:
331 case 2:
332 *rt2 = *rt + 3;
333 break;
334
335 case 4:
336 case 6:
337 *rt2 = *rt + 2;
338 break;
339
340 case 7:
341 *rt2 = *rt;
342 break;
343
344 case 8:
345 case 10:
346 *rt2 = *rt + 1;
347 break;
348
349 default:
350 return false;
351 }
352 return true;
353 }
354 else if (aarch64_ldst_simd_s (insn)
355 || aarch64_ldst_simd_s_pi (insn))
356 {
357 *rt = aarch64_rt (insn);
358 r = (insn >> 21) & 1;
359 *load = aarch64_bit (insn, 22);
360 opcode = (insn >> 13) & 0x7;
361 switch (opcode)
362 {
363 case 0:
364 case 2:
365 case 4:
366 *rt2 = *rt + r;
367 break;
368
369 case 1:
370 case 3:
371 case 5:
372 *rt2 = *rt + (r == 0 ? 2 : 3);
373 break;
374
375 case 6:
376 *rt2 = *rt + r;
377 break;
378
379 case 7:
380 *rt2 = *rt + (r == 0 ? 2 : 3);
381 break;
382
383 default:
384 return false;
385 }
386 return true;
387 }
388 return false;
2f0c79aa
HS
389 } // End of "aarch64_mem_op_p".
390
391 // Return true if INSN is mac insn.
392 static bool
393 aarch64_mac(Insntype insn)
394 { return (insn & 0xff000000) == 0x9b000000; }
395
396 // Return true if INSN is multiply-accumulate.
397 // (This is similar to implementaton in elfnn-aarch64.c.)
398 static bool
399 aarch64_mlxl(Insntype insn)
400 {
401 uint32_t op31 = aarch64_op31(insn);
402 if (aarch64_mac(insn)
403 && (op31 == 0 || op31 == 1 || op31 == 5)
404 /* Exclude MUL instructions which are encoded as a multiple-accumulate
405 with RA = XZR. */
406 && aarch64_ra(insn) != AARCH64_ZR)
407 {
408 return true;
409 }
410 return false;
5019d64a 411 }
2f0c79aa
HS
412}; // End of "AArch64_insn_utilities".
413
414
415// Insn length in byte.
416
417template<bool big_endian>
418const int AArch64_insn_utilities<big_endian>::BYTES_PER_INSN = 4;
419
420
421// Zero register encoding - 31.
422
423template<bool big_endian>
424const unsigned int AArch64_insn_utilities<big_endian>::AARCH64_ZR = 0x1f;
5019d64a
HS
425
426
053a4d68
JY
427// Output_data_got_aarch64 class.
428
429template<int size, bool big_endian>
430class Output_data_got_aarch64 : public Output_data_got<size, big_endian>
431{
432 public:
433 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
434 Output_data_got_aarch64(Symbol_table* symtab, Layout* layout)
9363c7c3
JY
435 : Output_data_got<size, big_endian>(),
436 symbol_table_(symtab), layout_(layout)
053a4d68
JY
437 { }
438
8e33481e
HS
439 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
440 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
441 // applied in a static link.
442 void
443 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
444 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
445
446
447 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
448 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
449 // relocation that needs to be applied in a static link.
450 void
451 add_static_reloc(unsigned int got_offset, unsigned int r_type,
452 Sized_relobj_file<size, big_endian>* relobj,
453 unsigned int index)
454 {
455 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
456 index));
457 }
458
459
053a4d68
JY
460 protected:
461 // Write out the GOT table.
462 void
463 do_write(Output_file* of) {
464 // The first entry in the GOT is the address of the .dynamic section.
465 gold_assert(this->data_size() >= size / 8);
466 Output_section* dynamic = this->layout_->dynamic_section();
467 Valtype dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
468 this->replace_constant(0, dynamic_addr);
469 Output_data_got<size, big_endian>::do_write(of);
8e33481e
HS
470
471 // Handling static relocs
472 if (this->static_relocs_.empty())
473 return;
474
475 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
476
477 gold_assert(parameters->doing_static_link());
478 const off_t offset = this->offset();
479 const section_size_type oview_size =
480 convert_to_section_size_type(this->data_size());
481 unsigned char* const oview = of->get_output_view(offset, oview_size);
482
483 Output_segment* tls_segment = this->layout_->tls_segment();
484 gold_assert(tls_segment != NULL);
485
486 AArch64_address aligned_tcb_address =
83a01957 487 align_address(Target_aarch64<size, big_endian>::TCB_SIZE,
8e33481e
HS
488 tls_segment->maximum_alignment());
489
490 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
491 {
492 Static_reloc& reloc(this->static_relocs_[i]);
493 AArch64_address value;
494
495 if (!reloc.symbol_is_global())
496 {
497 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
498 const Symbol_value<size>* psymval =
499 reloc.relobj()->local_symbol(reloc.index());
500
501 // We are doing static linking. Issue an error and skip this
502 // relocation if the symbol is undefined or in a discarded_section.
503 bool is_ordinary;
504 unsigned int shndx = psymval->input_shndx(&is_ordinary);
505 if ((shndx == elfcpp::SHN_UNDEF)
506 || (is_ordinary
507 && shndx != elfcpp::SHN_UNDEF
508 && !object->is_section_included(shndx)
509 && !this->symbol_table_->is_section_folded(object, shndx)))
510 {
511 gold_error(_("undefined or discarded local symbol %u from "
512 " object %s in GOT"),
513 reloc.index(), reloc.relobj()->name().c_str());
514 continue;
515 }
516 value = psymval->value(object, 0);
517 }
518 else
519 {
520 const Symbol* gsym = reloc.symbol();
521 gold_assert(gsym != NULL);
522 if (gsym->is_forwarder())
523 gsym = this->symbol_table_->resolve_forwards(gsym);
524
525 // We are doing static linking. Issue an error and skip this
526 // relocation if the symbol is undefined or in a discarded_section
527 // unless it is a weakly_undefined symbol.
528 if ((gsym->is_defined_in_discarded_section()
529 || gsym->is_undefined())
530 && !gsym->is_weak_undefined())
531 {
532 gold_error(_("undefined or discarded symbol %s in GOT"),
533 gsym->name());
534 continue;
535 }
536
537 if (!gsym->is_weak_undefined())
538 {
539 const Sized_symbol<size>* sym =
540 static_cast<const Sized_symbol<size>*>(gsym);
541 value = sym->value();
542 }
543 else
544 value = 0;
545 }
546
547 unsigned got_offset = reloc.got_offset();
548 gold_assert(got_offset < oview_size);
549
550 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
551 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
552 Valtype x;
553 switch (reloc.r_type())
554 {
555 case elfcpp::R_AARCH64_TLS_DTPREL64:
556 x = value;
557 break;
558 case elfcpp::R_AARCH64_TLS_TPREL64:
559 x = value + aligned_tcb_address;
560 break;
561 default:
562 gold_unreachable();
563 }
564 elfcpp::Swap<size, big_endian>::writeval(wv, x);
565 }
566
567 of->write_output_view(offset, oview_size, oview);
053a4d68
JY
568 }
569
570 private:
9363c7c3
JY
571 // Symbol table of the output object.
572 Symbol_table* symbol_table_;
053a4d68
JY
573 // A pointer to the Layout class, so that we can find the .dynamic
574 // section when we write out the GOT section.
575 Layout* layout_;
8e33481e 576
8e33481e
HS
577 // This class represent dynamic relocations that need to be applied by
578 // gold because we are using TLS relocations in a static link.
579 class Static_reloc
580 {
581 public:
582 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
583 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
584 { this->u_.global.symbol = gsym; }
585
586 Static_reloc(unsigned int got_offset, unsigned int r_type,
587 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
588 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
589 {
590 this->u_.local.relobj = relobj;
591 this->u_.local.index = index;
592 }
593
594 // Return the GOT offset.
595 unsigned int
596 got_offset() const
597 { return this->got_offset_; }
598
599 // Relocation type.
600 unsigned int
601 r_type() const
602 { return this->r_type_; }
603
604 // Whether the symbol is global or not.
605 bool
606 symbol_is_global() const
607 { return this->symbol_is_global_; }
608
609 // For a relocation against a global symbol, the global symbol.
610 Symbol*
611 symbol() const
612 {
613 gold_assert(this->symbol_is_global_);
614 return this->u_.global.symbol;
615 }
616
617 // For a relocation against a local symbol, the defining object.
618 Sized_relobj_file<size, big_endian>*
619 relobj() const
620 {
621 gold_assert(!this->symbol_is_global_);
622 return this->u_.local.relobj;
623 }
624
625 // For a relocation against a local symbol, the local symbol index.
626 unsigned int
627 index() const
628 {
629 gold_assert(!this->symbol_is_global_);
630 return this->u_.local.index;
631 }
632
633 private:
634 // GOT offset of the entry to which this relocation is applied.
635 unsigned int got_offset_;
636 // Type of relocation.
637 unsigned int r_type_;
638 // Whether this relocation is against a global symbol.
639 bool symbol_is_global_;
640 // A global or local symbol.
641 union
642 {
83a01957
HS
643 struct
644 {
645 // For a global symbol, the symbol itself.
646 Symbol* symbol;
647 } global;
648 struct
649 {
650 // For a local symbol, the object defining the symbol.
651 Sized_relobj_file<size, big_endian>* relobj;
652 // For a local symbol, the symbol index.
653 unsigned int index;
654 } local;
655 } u_;
656 }; // End of inner class Static_reloc
657
658 std::vector<Static_reloc> static_relocs_;
659}; // End of Output_data_got_aarch64
660
661
662template<int size, bool big_endian>
663class AArch64_input_section;
664
665
666template<int size, bool big_endian>
667class AArch64_output_section;
668
669
83a01957 670template<int size, bool big_endian>
a48d0c12
HS
671class AArch64_relobj;
672
673
674// Stub type enum constants.
675
676enum
83a01957 677{
a48d0c12 678 ST_NONE = 0,
83a01957 679
a48d0c12
HS
680 // Using adrp/add pair, 4 insns (including alignment) without mem access,
681 // the fastest stub. This has a limited jump distance, which is tested by
682 // aarch64_valid_for_adrp_p.
683 ST_ADRP_BRANCH = 1,
83a01957 684
a48d0c12
HS
685 // Using ldr-absolute-address/br-register, 4 insns with 1 mem access,
686 // unlimited in jump distance.
687 ST_LONG_BRANCH_ABS = 2,
83a01957 688
a48d0c12
HS
689 // Using ldr/calculate-pcrel/jump, 8 insns (including alignment) with 1
690 // mem access, slowest one. Only used in position independent executables.
691 ST_LONG_BRANCH_PCREL = 3,
83a01957 692
a48d0c12
HS
693 // Stub for erratum 843419 handling.
694 ST_E_843419 = 4,
83a01957 695
2f0c79aa
HS
696 // Stub for erratum 835769 handling.
697 ST_E_835769 = 5,
698
a48d0c12 699 // Number of total stub types.
2f0c79aa 700 ST_NUMBER = 6
a48d0c12 701};
83a01957 702
83a01957 703
a48d0c12
HS
704// Struct that wraps insns for a particular stub. All stub templates are
705// created/initialized as constants by Stub_template_repertoire.
83a01957 706
a48d0c12
HS
707template<bool big_endian>
708struct Stub_template
709{
710 const typename AArch64_insn_utilities<big_endian>::Insntype* insns;
711 const int insn_num;
712};
83a01957 713
83a01957 714
a48d0c12
HS
715// Simple singleton class that creates/initializes/stores all types of stub
716// templates.
717
718template<bool big_endian>
719class Stub_template_repertoire
720{
721public:
722 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
723
724 // Single static method to get stub template for a given stub type.
725 static const Stub_template<big_endian>*
726 get_stub_template(int type)
83a01957 727 {
a48d0c12
HS
728 static Stub_template_repertoire<big_endian> singleton;
729 return singleton.stub_templates_[type];
83a01957
HS
730 }
731
a48d0c12
HS
732private:
733 // Constructor - creates/initializes all stub templates.
734 Stub_template_repertoire();
735 ~Stub_template_repertoire()
83a01957
HS
736 { }
737
a48d0c12
HS
738 // Disallowing copy ctor and copy assignment operator.
739 Stub_template_repertoire(Stub_template_repertoire&);
740 Stub_template_repertoire& operator=(Stub_template_repertoire&);
83a01957 741
a48d0c12
HS
742 // Data that stores all insn templates.
743 const Stub_template<big_endian>* stub_templates_[ST_NUMBER];
744}; // End of "class Stub_template_repertoire".
745
746
747// Constructor - creates/initilizes all stub templates.
748
749template<bool big_endian>
750Stub_template_repertoire<big_endian>::Stub_template_repertoire()
751{
752 // Insn array definitions.
753 const static Insntype ST_NONE_INSNS[] = {};
754
755 const static Insntype ST_ADRP_BRANCH_INSNS[] =
756 {
757 0x90000010, /* adrp ip0, X */
758 /* ADR_PREL_PG_HI21(X) */
759 0x91000210, /* add ip0, ip0, :lo12:X */
760 /* ADD_ABS_LO12_NC(X) */
761 0xd61f0200, /* br ip0 */
762 0x00000000, /* alignment padding */
763 };
764
765 const static Insntype ST_LONG_BRANCH_ABS_INSNS[] =
766 {
767 0x58000050, /* ldr ip0, 0x8 */
768 0xd61f0200, /* br ip0 */
769 0x00000000, /* address field */
770 0x00000000, /* address fields */
771 };
772
773 const static Insntype ST_LONG_BRANCH_PCREL_INSNS[] =
774 {
775 0x58000090, /* ldr ip0, 0x10 */
776 0x10000011, /* adr ip1, #0 */
777 0x8b110210, /* add ip0, ip0, ip1 */
778 0xd61f0200, /* br ip0 */
779 0x00000000, /* address field */
780 0x00000000, /* address field */
781 0x00000000, /* alignment padding */
782 0x00000000, /* alignment padding */
783 };
784
785 const static Insntype ST_E_843419_INSNS[] =
786 {
787 0x00000000, /* Placeholder for erratum insn. */
788 0x14000000, /* b <label> */
789 };
790
a24df305
NC
791 // ST_E_835769 has the same stub template as ST_E_843419
792 // but we reproduce the array here so that the sizeof
793 // expressions in install_insn_template will work.
794 const static Insntype ST_E_835769_INSNS[] =
795 {
796 0x00000000, /* Placeholder for erratum insn. */
797 0x14000000, /* b <label> */
798 };
2f0c79aa 799
a48d0c12
HS
800#define install_insn_template(T) \
801 const static Stub_template<big_endian> template_##T = { \
802 T##_INSNS, sizeof(T##_INSNS) / sizeof(T##_INSNS[0]) }; \
803 this->stub_templates_[T] = &template_##T
804
805 install_insn_template(ST_NONE);
806 install_insn_template(ST_ADRP_BRANCH);
807 install_insn_template(ST_LONG_BRANCH_ABS);
808 install_insn_template(ST_LONG_BRANCH_PCREL);
809 install_insn_template(ST_E_843419);
2f0c79aa 810 install_insn_template(ST_E_835769);
a48d0c12
HS
811
812#undef install_insn_template
813}
814
815
816// Base class for stubs.
817
818template<int size, bool big_endian>
819class Stub_base
820{
821public:
822 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
823 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
824
825 static const AArch64_address invalid_address =
826 static_cast<AArch64_address>(-1);
827
828 static const section_offset_type invalid_offset =
829 static_cast<section_offset_type>(-1);
830
831 Stub_base(int type)
832 : destination_address_(invalid_address),
833 offset_(invalid_offset),
834 type_(type)
835 {}
836
837 ~Stub_base()
838 {}
839
840 // Get stub type.
841 int
842 type() const
843 { return this->type_; }
844
845 // Get stub template that provides stub insn information.
846 const Stub_template<big_endian>*
847 stub_template() const
83a01957 848 {
a48d0c12
HS
849 return Stub_template_repertoire<big_endian>::
850 get_stub_template(this->type());
83a01957
HS
851 }
852
a48d0c12 853 // Get destination address.
83a01957
HS
854 AArch64_address
855 destination_address() const
856 {
857 gold_assert(this->destination_address_ != this->invalid_address);
858 return this->destination_address_;
859 }
860
861 // Set destination address.
862 void
863 set_destination_address(AArch64_address address)
864 {
865 gold_assert(address != this->invalid_address);
866 this->destination_address_ = address;
867 }
868
869 // Reset the destination address.
870 void
871 reset_destination_address()
872 { this->destination_address_ = this->invalid_address; }
873
a48d0c12
HS
874 // Get offset of code stub. For Reloc_stub, it is the offset from the
875 // beginning of its containing stub table; for Erratum_stub, it is the offset
876 // from the end of reloc_stubs.
877 section_offset_type
878 offset() const
879 {
880 gold_assert(this->offset_ != this->invalid_offset);
881 return this->offset_;
882 }
83a01957 883
a48d0c12
HS
884 // Set stub offset.
885 void
886 set_offset(section_offset_type offset)
887 { this->offset_ = offset; }
83a01957 888
a48d0c12
HS
889 // Return the stub insn.
890 const Insntype*
891 insns() const
892 { return this->stub_template()->insns; }
83a01957 893
a48d0c12
HS
894 // Return num of stub insns.
895 unsigned int
896 insn_num() const
897 { return this->stub_template()->insn_num; }
898
899 // Get size of the stub.
900 int
901 stub_size() const
902 {
903 return this->insn_num() *
904 AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
905 }
83a01957
HS
906
907 // Write stub to output file.
908 void
909 write(unsigned char* view, section_size_type view_size)
910 { this->do_write(view, view_size); }
911
a48d0c12
HS
912protected:
913 // Abstract method to be implemented by sub-classes.
914 virtual void
915 do_write(unsigned char*, section_size_type) = 0;
916
917private:
918 // The last insn of a stub is a jump to destination insn. This field records
919 // the destination address.
920 AArch64_address destination_address_;
921 // The stub offset. Note this has difference interpretations between an
922 // Reloc_stub and an Erratum_stub. For Reloc_stub this is the offset from the
923 // beginning of the containing stub_table, whereas for Erratum_stub, this is
924 // the offset from the end of reloc_stubs.
925 section_offset_type offset_;
926 // Stub type.
927 const int type_;
928}; // End of "Stub_base".
929
930
931// Erratum stub class. An erratum stub differs from a reloc stub in that for
932// each erratum occurrence, we generate an erratum stub. We never share erratum
c092b67b 933// stubs, whereas for reloc stubs, different branch insns share a single reloc
a48d0c12
HS
934// stub as long as the branch targets are the same. (More to the point, reloc
935// stubs can be shared because they're used to reach a specific target, whereas
936// erratum stubs branch back to the original control flow.)
937
938template<int size, bool big_endian>
939class Erratum_stub : public Stub_base<size, big_endian>
940{
941public:
942 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
943 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
56b06706 944 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
a48d0c12
HS
945 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
946
5d7908e0 947 static const int STUB_ADDR_ALIGN;
a48d0c12
HS
948
949 static const Insntype invalid_insn = static_cast<Insntype>(-1);
950
951 Erratum_stub(The_aarch64_relobj* relobj, int type,
952 unsigned shndx, unsigned int sh_offset)
953 : Stub_base<size, big_endian>(type), relobj_(relobj),
954 shndx_(shndx), sh_offset_(sh_offset),
955 erratum_insn_(invalid_insn),
956 erratum_address_(this->invalid_address)
957 {}
958
959 ~Erratum_stub() {}
960
961 // Return the object that contains the erratum.
962 The_aarch64_relobj*
963 relobj()
964 { return this->relobj_; }
965
966 // Get section index of the erratum.
967 unsigned int
968 shndx() const
969 { return this->shndx_; }
970
971 // Get section offset of the erratum.
972 unsigned int
973 sh_offset() const
974 { return this->sh_offset_; }
975
976 // Get the erratum insn. This is the insn located at erratum_insn_address.
977 Insntype
978 erratum_insn() const
979 {
980 gold_assert(this->erratum_insn_ != this->invalid_insn);
981 return this->erratum_insn_;
982 }
983
984 // Set the insn that the erratum happens to.
985 void
986 set_erratum_insn(Insntype insn)
987 { this->erratum_insn_ = insn; }
988
56b06706
HS
989 // For 843419, the erratum insn is ld/st xt, [xn, #uimm], which may be a
990 // relocation spot, in this case, the erratum_insn_ recorded at scanning phase
991 // is no longer the one we want to write out to the stub, update erratum_insn_
992 // with relocated version. Also note that in this case xn must not be "PC", so
993 // it is safe to move the erratum insn from the origin place to the stub. For
994 // 835769, the erratum insn is multiply-accumulate insn, which could not be a
995 // relocation spot (assertion added though).
996 void
997 update_erratum_insn(Insntype insn)
998 {
999 gold_assert(this->erratum_insn_ != this->invalid_insn);
1000 switch (this->type())
1001 {
1002 case ST_E_843419:
1003 gold_assert(Insn_utilities::aarch64_ldst_uimm(insn));
1004 gold_assert(Insn_utilities::aarch64_ldst_uimm(this->erratum_insn()));
1005 gold_assert(Insn_utilities::aarch64_rd(insn) ==
1006 Insn_utilities::aarch64_rd(this->erratum_insn()));
1007 gold_assert(Insn_utilities::aarch64_rn(insn) ==
1008 Insn_utilities::aarch64_rn(this->erratum_insn()));
1009 // Update plain ld/st insn with relocated insn.
1010 this->erratum_insn_ = insn;
1011 break;
1012 case ST_E_835769:
1013 gold_assert(insn == this->erratum_insn());
1014 break;
1015 default:
1016 gold_unreachable();
1017 }
1018 }
1019
1020
a48d0c12
HS
1021 // Return the address where an erratum must be done.
1022 AArch64_address
1023 erratum_address() const
1024 {
1025 gold_assert(this->erratum_address_ != this->invalid_address);
1026 return this->erratum_address_;
1027 }
1028
1029 // Set the address where an erratum must be done.
1030 void
1031 set_erratum_address(AArch64_address addr)
1032 { this->erratum_address_ = addr; }
1033
1034 // Comparator used to group Erratum_stubs in a set by (obj, shndx,
5c3024d2 1035 // sh_offset). We do not include 'type' in the calculation, because there is
a48d0c12
HS
1036 // at most one stub type at (obj, shndx, sh_offset).
1037 bool
1038 operator<(const Erratum_stub<size, big_endian>& k) const
1039 {
1040 if (this == &k)
1041 return false;
1042 // We group stubs by relobj.
1043 if (this->relobj_ != k.relobj_)
1044 return this->relobj_ < k.relobj_;
1045 // Then by section index.
1046 if (this->shndx_ != k.shndx_)
1047 return this->shndx_ < k.shndx_;
1048 // Lastly by section offset.
1049 return this->sh_offset_ < k.sh_offset_;
1050 }
1051
df2f63a6
HS
1052 void
1053 invalidate_erratum_stub()
1054 {
1055 gold_assert(this->relobj_ != NULL);
1056 this->relobj_ = NULL;
1057 }
1058
1059 bool
1060 is_invalidated_erratum_stub()
1061 { return this->relobj_ == NULL; }
1062
a48d0c12
HS
1063protected:
1064 virtual void
1065 do_write(unsigned char*, section_size_type);
1066
1067private:
1068 // The object that needs to be fixed.
1069 The_aarch64_relobj* relobj_;
1070 // The shndx in the object that needs to be fixed.
1071 const unsigned int shndx_;
1072 // The section offset in the obejct that needs to be fixed.
1073 const unsigned int sh_offset_;
1074 // The insn to be fixed.
1075 Insntype erratum_insn_;
1076 // The address of the above insn.
1077 AArch64_address erratum_address_;
1078}; // End of "Erratum_stub".
1079
0ef3814f
HS
1080
1081// Erratum sub class to wrap additional info needed by 843419. In fixing this
1082// erratum, we may choose to replace 'adrp' with 'adr', in this case, we need
1083// adrp's code position (two or three insns before erratum insn itself).
1084
1085template<int size, bool big_endian>
1086class E843419_stub : public Erratum_stub<size, big_endian>
1087{
1088public:
1089 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
1090
1091 E843419_stub(AArch64_relobj<size, big_endian>* relobj,
1092 unsigned int shndx, unsigned int sh_offset,
1093 unsigned int adrp_sh_offset)
1094 : Erratum_stub<size, big_endian>(relobj, ST_E_843419, shndx, sh_offset),
1095 adrp_sh_offset_(adrp_sh_offset)
1096 {}
1097
1098 unsigned int
1099 adrp_sh_offset() const
1100 { return this->adrp_sh_offset_; }
1101
1102private:
1103 // Section offset of "adrp". (We do not need a "adrp_shndx_" field, because we
1104 // can can obtain it from its parent.)
1105 const unsigned int adrp_sh_offset_;
1106};
1107
1108
5d7908e0
CC
1109template<int size, bool big_endian>
1110const int Erratum_stub<size, big_endian>::STUB_ADDR_ALIGN = 4;
a48d0c12
HS
1111
1112// Comparator used in set definition.
1113template<int size, bool big_endian>
1114struct Erratum_stub_less
1115{
1116 bool
1117 operator()(const Erratum_stub<size, big_endian>* s1,
1118 const Erratum_stub<size, big_endian>* s2) const
1119 { return *s1 < *s2; }
1120};
1121
1122// Erratum_stub implementation for writing stub to output file.
1123
1124template<int size, bool big_endian>
1125void
1126Erratum_stub<size, big_endian>::do_write(unsigned char* view, section_size_type)
1127{
1128 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
1129 const Insntype* insns = this->insns();
1130 uint32_t num_insns = this->insn_num();
1131 Insntype* ip = reinterpret_cast<Insntype*>(view);
2f0c79aa
HS
1132 // For current implemented erratum 843419 and 835769, the first insn in the
1133 // stub is always a copy of the problematic insn (in 843419, the mem access
1134 // insn, in 835769, the mac insn), followed by a jump-back.
a48d0c12
HS
1135 elfcpp::Swap<32, big_endian>::writeval(ip, this->erratum_insn());
1136 for (uint32_t i = 1; i < num_insns; ++i)
1137 elfcpp::Swap<32, big_endian>::writeval(ip + i, insns[i]);
1138}
1139
1140
1141// Reloc stub class.
1142
1143template<int size, bool big_endian>
1144class Reloc_stub : public Stub_base<size, big_endian>
1145{
1146 public:
1147 typedef Reloc_stub<size, big_endian> This;
1148 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1149
1150 // Branch range. This is used to calculate the section group size, as well as
1151 // determine whether a stub is needed.
1152 static const int MAX_BRANCH_OFFSET = ((1 << 25) - 1) << 2;
1153 static const int MIN_BRANCH_OFFSET = -((1 << 25) << 2);
1154
1155 // Constant used to determine if an offset fits in the adrp instruction
1156 // encoding.
1157 static const int MAX_ADRP_IMM = (1 << 20) - 1;
1158 static const int MIN_ADRP_IMM = -(1 << 20);
1159
1160 static const int BYTES_PER_INSN = 4;
5d7908e0 1161 static const int STUB_ADDR_ALIGN;
a48d0c12
HS
1162
1163 // Determine whether the offset fits in the jump/branch instruction.
1164 static bool
1165 aarch64_valid_branch_offset_p(int64_t offset)
1166 { return offset >= MIN_BRANCH_OFFSET && offset <= MAX_BRANCH_OFFSET; }
1167
1168 // Determine whether the offset fits in the adrp immediate field.
1169 static bool
1170 aarch64_valid_for_adrp_p(AArch64_address location, AArch64_address dest)
1171 {
1172 typedef AArch64_relocate_functions<size, big_endian> Reloc;
1173 int64_t adrp_imm = (Reloc::Page(dest) - Reloc::Page(location)) >> 12;
1174 return adrp_imm >= MIN_ADRP_IMM && adrp_imm <= MAX_ADRP_IMM;
1175 }
1176
1177 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1178 // needed.
1179 static int
1180 stub_type_for_reloc(unsigned int r_type, AArch64_address address,
1181 AArch64_address target);
1182
1183 Reloc_stub(int type)
1184 : Stub_base<size, big_endian>(type)
1185 { }
1186
1187 ~Reloc_stub()
1188 { }
1189
83a01957
HS
1190 // The key class used to index the stub instance in the stub table's stub map.
1191 class Key
1192 {
1193 public:
a48d0c12 1194 Key(int type, const Symbol* symbol, const Relobj* relobj,
83a01957 1195 unsigned int r_sym, int32_t addend)
a48d0c12 1196 : type_(type), addend_(addend)
83a01957
HS
1197 {
1198 if (symbol != NULL)
1199 {
1200 this->r_sym_ = Reloc_stub::invalid_index;
1201 this->u_.symbol = symbol;
1202 }
1203 else
1204 {
1205 gold_assert(relobj != NULL && r_sym != invalid_index);
1206 this->r_sym_ = r_sym;
1207 this->u_.relobj = relobj;
1208 }
1209 }
1210
1211 ~Key()
1212 { }
1213
1214 // Return stub type.
a48d0c12
HS
1215 int
1216 type() const
1217 { return this->type_; }
83a01957
HS
1218
1219 // Return the local symbol index or invalid_index.
1220 unsigned int
1221 r_sym() const
1222 { return this->r_sym_; }
1223
1224 // Return the symbol if there is one.
1225 const Symbol*
1226 symbol() const
1227 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
1228
1229 // Return the relobj if there is one.
1230 const Relobj*
1231 relobj() const
1232 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
1233
1234 // Whether this equals to another key k.
1235 bool
1236 eq(const Key& k) const
1237 {
a48d0c12 1238 return ((this->type_ == k.type_)
83a01957
HS
1239 && (this->r_sym_ == k.r_sym_)
1240 && ((this->r_sym_ != Reloc_stub::invalid_index)
1241 ? (this->u_.relobj == k.u_.relobj)
1242 : (this->u_.symbol == k.u_.symbol))
1243 && (this->addend_ == k.addend_));
1244 }
1245
1246 // Return a hash value.
1247 size_t
1248 hash_value() const
1249 {
1250 size_t name_hash_value = gold::string_hash<char>(
1251 (this->r_sym_ != Reloc_stub::invalid_index)
1252 ? this->u_.relobj->name().c_str()
1253 : this->u_.symbol->name());
1254 // We only have 4 stub types.
a48d0c12 1255 size_t stub_type_hash_value = 0x03 & this->type_;
83a01957
HS
1256 return (name_hash_value
1257 ^ stub_type_hash_value
1258 ^ ((this->r_sym_ & 0x3fff) << 2)
1259 ^ ((this->addend_ & 0xffff) << 16));
1260 }
1261
1262 // Functors for STL associative containers.
1263 struct hash
1264 {
1265 size_t
1266 operator()(const Key& k) const
1267 { return k.hash_value(); }
1268 };
1269
1270 struct equal_to
1271 {
1272 bool
1273 operator()(const Key& k1, const Key& k2) const
1274 { return k1.eq(k2); }
1275 };
1276
9726c3c1 1277 private:
83a01957 1278 // Stub type.
a48d0c12 1279 const int type_;
83a01957
HS
1280 // If this is a local symbol, this is the index in the defining object.
1281 // Otherwise, it is invalid_index for a global symbol.
1282 unsigned int r_sym_;
1283 // If r_sym_ is an invalid index, this points to a global symbol.
1284 // Otherwise, it points to a relobj. We used the unsized and target
1285 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
1286 // Arm_relobj, in order to avoid making the stub class a template
1287 // as most of the stub machinery is endianness-neutral. However, it
1288 // may require a bit of casting done by users of this class.
1289 union
1290 {
1291 const Symbol* symbol;
1292 const Relobj* relobj;
1293 } u_;
1294 // Addend associated with a reloc.
1295 int32_t addend_;
1296 }; // End of inner class Reloc_stub::Key
1297
1298 protected:
1299 // This may be overridden in the child class.
1300 virtual void
1301 do_write(unsigned char*, section_size_type);
1302
1303 private:
83a01957 1304 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
83a01957
HS
1305}; // End of Reloc_stub
1306
5d7908e0
CC
1307template<int size, bool big_endian>
1308const int Reloc_stub<size, big_endian>::STUB_ADDR_ALIGN = 4;
83a01957
HS
1309
1310// Write data to output file.
1311
1312template<int size, bool big_endian>
1313void
1314Reloc_stub<size, big_endian>::
1315do_write(unsigned char* view, section_size_type)
1316{
1317 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
a48d0c12
HS
1318 const uint32_t* insns = this->insns();
1319 uint32_t num_insns = this->insn_num();
83a01957 1320 Insntype* ip = reinterpret_cast<Insntype*>(view);
a48d0c12
HS
1321 for (uint32_t i = 0; i < num_insns; ++i)
1322 elfcpp::Swap<32, big_endian>::writeval(ip + i, insns[i]);
83a01957
HS
1323}
1324
1325
83a01957
HS
1326// Determine the stub type for a certain relocation or ST_NONE, if no stub is
1327// needed.
1328
1329template<int size, bool big_endian>
a48d0c12 1330inline int
83a01957
HS
1331Reloc_stub<size, big_endian>::stub_type_for_reloc(
1332 unsigned int r_type, AArch64_address location, AArch64_address dest)
1333{
1334 int64_t branch_offset = 0;
1335 switch(r_type)
1336 {
1337 case elfcpp::R_AARCH64_CALL26:
1338 case elfcpp::R_AARCH64_JUMP26:
1339 branch_offset = dest - location;
1340 break;
1341 default:
9726c3c1 1342 gold_unreachable();
83a01957
HS
1343 }
1344
1345 if (aarch64_valid_branch_offset_p(branch_offset))
1346 return ST_NONE;
1347
1348 if (aarch64_valid_for_adrp_p(location, dest))
1349 return ST_ADRP_BRANCH;
1350
9a472eda
HS
1351 // Always use PC-relative addressing in case of -shared or -pie.
1352 if (parameters->options().output_is_position_independent())
83a01957
HS
1353 return ST_LONG_BRANCH_PCREL;
1354
9a472eda
HS
1355 // This saves 2 insns per stub, compared to ST_LONG_BRANCH_PCREL.
1356 // But is only applicable to non-shared or non-pie.
83a01957
HS
1357 return ST_LONG_BRANCH_ABS;
1358}
1359
df2f63a6
HS
1360// A class to hold stubs for the ARM target. This contains 2 different types of
1361// stubs - reloc stubs and erratum stubs.
83a01957
HS
1362
1363template<int size, bool big_endian>
1364class Stub_table : public Output_data
1365{
1366 public:
1367 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1368 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
a48d0c12 1369 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
83a01957
HS
1370 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1371 typedef Reloc_stub<size, big_endian> The_reloc_stub;
1372 typedef typename The_reloc_stub::Key The_reloc_stub_key;
a48d0c12
HS
1373 typedef Erratum_stub<size, big_endian> The_erratum_stub;
1374 typedef Erratum_stub_less<size, big_endian> The_erratum_stub_less;
83a01957
HS
1375 typedef typename The_reloc_stub_key::hash The_reloc_stub_key_hash;
1376 typedef typename The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to;
1377 typedef Stub_table<size, big_endian> The_stub_table;
1378 typedef Unordered_map<The_reloc_stub_key, The_reloc_stub*,
1379 The_reloc_stub_key_hash, The_reloc_stub_key_equal_to>
1380 Reloc_stub_map;
1381 typedef typename Reloc_stub_map::const_iterator Reloc_stub_map_const_iter;
1382 typedef Relocate_info<size, big_endian> The_relocate_info;
1383
a48d0c12
HS
1384 typedef std::set<The_erratum_stub*, The_erratum_stub_less> Erratum_stub_set;
1385 typedef typename Erratum_stub_set::iterator Erratum_stub_set_iter;
1386
83a01957 1387 Stub_table(The_aarch64_input_section* owner)
a48d0c12
HS
1388 : Output_data(), owner_(owner), reloc_stubs_size_(0),
1389 erratum_stubs_size_(0), prev_data_size_(0)
83a01957
HS
1390 { }
1391
1392 ~Stub_table()
1393 { }
1394
1395 The_aarch64_input_section*
1396 owner() const
1397 { return owner_; }
1398
1399 // Whether this stub table is empty.
1400 bool
1401 empty() const
a48d0c12 1402 { return reloc_stubs_.empty() && erratum_stubs_.empty(); }
83a01957
HS
1403
1404 // Return the current data size.
1405 off_t
1406 current_data_size() const
1407 { return this->current_data_size_for_child(); }
1408
1409 // Add a STUB using KEY. The caller is responsible for avoiding addition
1410 // if a STUB with the same key has already been added.
1411 void
1412 add_reloc_stub(The_reloc_stub* stub, const The_reloc_stub_key& key);
1413
a48d0c12
HS
1414 // Add an erratum stub into the erratum stub set. The set is ordered by
1415 // (relobj, shndx, sh_offset).
1416 void
1417 add_erratum_stub(The_erratum_stub* stub);
1418
1419 // Find if such erratum exists for any given (obj, shndx, sh_offset).
1420 The_erratum_stub*
1421 find_erratum_stub(The_aarch64_relobj* a64relobj,
1422 unsigned int shndx, unsigned int sh_offset);
1423
1424 // Find all the erratums for a given input section. The return value is a pair
1425 // of iterators [begin, end).
1426 std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
1427 find_erratum_stubs_for_input_section(The_aarch64_relobj* a64relobj,
1428 unsigned int shndx);
1429
1430 // Compute the erratum stub address.
1431 AArch64_address
1432 erratum_stub_address(The_erratum_stub* stub) const
1433 {
1434 AArch64_address r = align_address(this->address() + this->reloc_stubs_size_,
1435 The_erratum_stub::STUB_ADDR_ALIGN);
1436 r += stub->offset();
1437 return r;
1438 }
1439
83a01957
HS
1440 // Finalize stubs. No-op here, just for completeness.
1441 void
1442 finalize_stubs()
1443 { }
1444
1445 // Look up a relocation stub using KEY. Return NULL if there is none.
1446 The_reloc_stub*
1447 find_reloc_stub(The_reloc_stub_key& key)
1448 {
1449 Reloc_stub_map_const_iter p = this->reloc_stubs_.find(key);
1450 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
1451 }
1452
df2f63a6 1453 // Relocate reloc stubs in this stub table. This does not relocate erratum stubs.
83a01957 1454 void
df2f63a6
HS
1455 relocate_reloc_stubs(const The_relocate_info*,
1456 The_target_aarch64*,
1457 Output_section*,
1458 unsigned char*,
1459 AArch64_address,
1460 section_size_type);
1461
1462 // Relocate an erratum stub.
1463 void
1464 relocate_erratum_stub(The_erratum_stub*, unsigned char*);
83a01957
HS
1465
1466 // Update data size at the end of a relaxation pass. Return true if data size
1467 // is different from that of the previous relaxation pass.
1468 bool
1469 update_data_size_changed_p()
1470 {
1471 // No addralign changed here.
a48d0c12
HS
1472 off_t s = align_address(this->reloc_stubs_size_,
1473 The_erratum_stub::STUB_ADDR_ALIGN)
1474 + this->erratum_stubs_size_;
83a01957
HS
1475 bool changed = (s != this->prev_data_size_);
1476 this->prev_data_size_ = s;
1477 return changed;
1478 }
1479
1480 protected:
1481 // Write out section contents.
1482 void
1483 do_write(Output_file*);
1484
1485 // Return the required alignment.
1486 uint64_t
1487 do_addralign() const
a48d0c12
HS
1488 {
1489 return std::max(The_reloc_stub::STUB_ADDR_ALIGN,
1490 The_erratum_stub::STUB_ADDR_ALIGN);
1491 }
83a01957
HS
1492
1493 // Reset address and file offset.
1494 void
1495 do_reset_address_and_file_offset()
1496 { this->set_current_data_size_for_child(this->prev_data_size_); }
1497
1498 // Set final data size.
1499 void
1500 set_final_data_size()
1501 { this->set_data_size(this->current_data_size()); }
1502
1503 private:
df2f63a6 1504 // Relocate one reloc stub.
83a01957 1505 void
df2f63a6
HS
1506 relocate_reloc_stub(The_reloc_stub*,
1507 const The_relocate_info*,
1508 The_target_aarch64*,
1509 Output_section*,
1510 unsigned char*,
1511 AArch64_address,
1512 section_size_type);
83a01957
HS
1513
1514 private:
1515 // Owner of this stub table.
1516 The_aarch64_input_section* owner_;
1517 // The relocation stubs.
1518 Reloc_stub_map reloc_stubs_;
a48d0c12
HS
1519 // The erratum stubs.
1520 Erratum_stub_set erratum_stubs_;
83a01957
HS
1521 // Size of reloc stubs.
1522 off_t reloc_stubs_size_;
a48d0c12
HS
1523 // Size of erratum stubs.
1524 off_t erratum_stubs_size_;
83a01957
HS
1525 // data size of this in the previous pass.
1526 off_t prev_data_size_;
1527}; // End of Stub_table
1528
1529
a48d0c12
HS
1530// Add an erratum stub into the erratum stub set. The set is ordered by
1531// (relobj, shndx, sh_offset).
1532
1533template<int size, bool big_endian>
1534void
1535Stub_table<size, big_endian>::add_erratum_stub(The_erratum_stub* stub)
1536{
1537 std::pair<Erratum_stub_set_iter, bool> ret =
1538 this->erratum_stubs_.insert(stub);
1539 gold_assert(ret.second);
1540 this->erratum_stubs_size_ = align_address(
1541 this->erratum_stubs_size_, The_erratum_stub::STUB_ADDR_ALIGN);
1542 stub->set_offset(this->erratum_stubs_size_);
1543 this->erratum_stubs_size_ += stub->stub_size();
1544}
1545
1546
56b06706 1547// Find if such erratum exists for given (obj, shndx, sh_offset).
a48d0c12
HS
1548
1549template<int size, bool big_endian>
1550Erratum_stub<size, big_endian>*
1551Stub_table<size, big_endian>::find_erratum_stub(
1552 The_aarch64_relobj* a64relobj, unsigned int shndx, unsigned int sh_offset)
1553{
1554 // A dummy object used as key to search in the set.
1555 The_erratum_stub key(a64relobj, ST_NONE,
1556 shndx, sh_offset);
1557 Erratum_stub_set_iter i = this->erratum_stubs_.find(&key);
1558 if (i != this->erratum_stubs_.end())
1559 {
1560 The_erratum_stub* stub(*i);
1561 gold_assert(stub->erratum_insn() != 0);
1562 return stub;
1563 }
1564 return NULL;
1565}
1566
1567
1568// Find all the errata for a given input section. The return value is a pair of
1569// iterators [begin, end).
1570
1571template<int size, bool big_endian>
1572std::pair<typename Stub_table<size, big_endian>::Erratum_stub_set_iter,
1573 typename Stub_table<size, big_endian>::Erratum_stub_set_iter>
1574Stub_table<size, big_endian>::find_erratum_stubs_for_input_section(
1575 The_aarch64_relobj* a64relobj, unsigned int shndx)
1576{
1577 typedef std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter> Result_pair;
1578 Erratum_stub_set_iter start, end;
1579 The_erratum_stub low_key(a64relobj, ST_NONE, shndx, 0);
1580 start = this->erratum_stubs_.lower_bound(&low_key);
1581 if (start == this->erratum_stubs_.end())
1582 return Result_pair(this->erratum_stubs_.end(),
1583 this->erratum_stubs_.end());
1584 end = start;
1585 while (end != this->erratum_stubs_.end() &&
1586 (*end)->relobj() == a64relobj && (*end)->shndx() == shndx)
1587 ++end;
1588 return Result_pair(start, end);
1589}
1590
1591
83a01957
HS
1592// Add a STUB using KEY. The caller is responsible for avoiding addition
1593// if a STUB with the same key has already been added.
1594
1595template<int size, bool big_endian>
1596void
1597Stub_table<size, big_endian>::add_reloc_stub(
1598 The_reloc_stub* stub, const The_reloc_stub_key& key)
1599{
a48d0c12 1600 gold_assert(stub->type() == key.type());
83a01957
HS
1601 this->reloc_stubs_[key] = stub;
1602
1603 // Assign stub offset early. We can do this because we never remove
1604 // reloc stubs and they are in the beginning of the stub table.
1605 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_,
1606 The_reloc_stub::STUB_ADDR_ALIGN);
1607 stub->set_offset(this->reloc_stubs_size_);
1608 this->reloc_stubs_size_ += stub->stub_size();
1609}
1610
1611
df2f63a6 1612// Relocate an erratum stub.
83a01957
HS
1613
1614template<int size, bool big_endian>
1615void
1616Stub_table<size, big_endian>::
df2f63a6
HS
1617relocate_erratum_stub(The_erratum_stub* estub,
1618 unsigned char* view)
83a01957 1619{
a48d0c12
HS
1620 // Just for convenience.
1621 const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
1622
df2f63a6
HS
1623 gold_assert(!estub->is_invalidated_erratum_stub());
1624 AArch64_address stub_address = this->erratum_stub_address(estub);
1625 // The address of "b" in the stub that is to be "relocated".
1626 AArch64_address stub_b_insn_address;
1627 // Branch offset that is to be filled in "b" insn.
1628 int b_offset = 0;
1629 switch (estub->type())
a48d0c12 1630 {
df2f63a6
HS
1631 case ST_E_843419:
1632 case ST_E_835769:
1633 // The 1st insn of the erratum could be a relocation spot,
1634 // in this case we need to fix it with
1635 // "(*i)->erratum_insn()".
1636 elfcpp::Swap<32, big_endian>::writeval(
1637 view + (stub_address - this->address()),
1638 estub->erratum_insn());
1639 // For the erratum, the 2nd insn is a b-insn to be patched
1640 // (relocated).
1641 stub_b_insn_address = stub_address + 1 * BPI;
1642 b_offset = estub->destination_address() - stub_b_insn_address;
1643 AArch64_relocate_functions<size, big_endian>::construct_b(
1644 view + (stub_b_insn_address - this->address()),
1645 ((unsigned int)(b_offset)) & 0xfffffff);
1646 break;
1647 default:
1648 gold_unreachable();
1649 break;
a48d0c12 1650 }
df2f63a6 1651 estub->invalidate_erratum_stub();
83a01957
HS
1652}
1653
1654
df2f63a6
HS
1655// Relocate only reloc stubs in this stub table. This does not relocate erratum
1656// stubs.
83a01957
HS
1657
1658template<int size, bool big_endian>
1659void
1660Stub_table<size, big_endian>::
df2f63a6
HS
1661relocate_reloc_stubs(const The_relocate_info* relinfo,
1662 The_target_aarch64* target_aarch64,
1663 Output_section* output_section,
1664 unsigned char* view,
1665 AArch64_address address,
1666 section_size_type view_size)
1667{
1668 // "view_size" is the total size of the stub_table.
1669 gold_assert(address == this->address() &&
1670 view_size == static_cast<section_size_type>(this->data_size()));
1671 for(Reloc_stub_map_const_iter p = this->reloc_stubs_.begin();
1672 p != this->reloc_stubs_.end(); ++p)
1673 relocate_reloc_stub(p->second, relinfo, target_aarch64, output_section,
1674 view, address, view_size);
1675}
1676
1677
1678// Relocate one reloc stub. This is a helper for
1679// Stub_table::relocate_reloc_stubs().
1680
1681template<int size, bool big_endian>
1682void
1683Stub_table<size, big_endian>::
1684relocate_reloc_stub(The_reloc_stub* stub,
1685 const The_relocate_info* relinfo,
1686 The_target_aarch64* target_aarch64,
1687 Output_section* output_section,
1688 unsigned char* view,
1689 AArch64_address address,
1690 section_size_type view_size)
83a01957
HS
1691{
1692 // "offset" is the offset from the beginning of the stub_table.
1693 section_size_type offset = stub->offset();
1694 section_size_type stub_size = stub->stub_size();
1695 // "view_size" is the total size of the stub_table.
1696 gold_assert(offset + stub_size <= view_size);
1697
df2f63a6
HS
1698 target_aarch64->relocate_reloc_stub(stub, relinfo, output_section,
1699 view + offset, address + offset, view_size);
83a01957
HS
1700}
1701
1702
1703// Write out the stubs to file.
1704
1705template<int size, bool big_endian>
1706void
1707Stub_table<size, big_endian>::do_write(Output_file* of)
1708{
1709 off_t offset = this->offset();
1710 const section_size_type oview_size =
1711 convert_to_section_size_type(this->data_size());
1712 unsigned char* const oview = of->get_output_view(offset, oview_size);
1713
1714 // Write relocation stubs.
1715 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
1716 p != this->reloc_stubs_.end(); ++p)
1717 {
1718 The_reloc_stub* stub = p->second;
1719 AArch64_address address = this->address() + stub->offset();
1720 gold_assert(address ==
1721 align_address(address, The_reloc_stub::STUB_ADDR_ALIGN));
1722 stub->write(oview + stub->offset(), stub->stub_size());
1723 }
1724
a48d0c12
HS
1725 // Write erratum stubs.
1726 unsigned int erratum_stub_start_offset =
1727 align_address(this->reloc_stubs_size_, The_erratum_stub::STUB_ADDR_ALIGN);
1728 for (typename Erratum_stub_set::iterator p = this->erratum_stubs_.begin();
1729 p != this->erratum_stubs_.end(); ++p)
1730 {
1731 The_erratum_stub* stub(*p);
1732 stub->write(oview + erratum_stub_start_offset + stub->offset(),
1733 stub->stub_size());
1734 }
1735
83a01957
HS
1736 of->write_output_view(this->offset(), oview_size, oview);
1737}
1738
1739
1740// AArch64_relobj class.
1741
1742template<int size, bool big_endian>
1743class AArch64_relobj : public Sized_relobj_file<size, big_endian>
1744{
1745 public:
1746 typedef AArch64_relobj<size, big_endian> This;
1747 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1748 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1749 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1750 typedef Stub_table<size, big_endian> The_stub_table;
a48d0c12
HS
1751 typedef Erratum_stub<size, big_endian> The_erratum_stub;
1752 typedef typename The_stub_table::Erratum_stub_set_iter Erratum_stub_set_iter;
83a01957
HS
1753 typedef std::vector<The_stub_table*> Stub_table_list;
1754 static const AArch64_address invalid_address =
1755 static_cast<AArch64_address>(-1);
1756
1757 AArch64_relobj(const std::string& name, Input_file* input_file, off_t offset,
1758 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1759 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1760 stub_tables_()
1761 { }
1762
1763 ~AArch64_relobj()
1764 { }
1765
1766 // Return the stub table of the SHNDX-th section if there is one.
1767 The_stub_table*
1768 stub_table(unsigned int shndx) const
1769 {
1770 gold_assert(shndx < this->stub_tables_.size());
1771 return this->stub_tables_[shndx];
1772 }
1773
1774 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1775 void
1776 set_stub_table(unsigned int shndx, The_stub_table* stub_table)
1777 {
1778 gold_assert(shndx < this->stub_tables_.size());
1779 this->stub_tables_[shndx] = stub_table;
1780 }
1781
2f0c79aa 1782 // Entrance to errata scanning.
5019d64a 1783 void
2f0c79aa
HS
1784 scan_errata(unsigned int shndx,
1785 const elfcpp::Shdr<size, big_endian>&,
1786 Output_section*, const Symbol_table*,
1787 The_target_aarch64*);
5019d64a
HS
1788
1789 // Scan all relocation sections for stub generation.
83a01957
HS
1790 void
1791 scan_sections_for_stubs(The_target_aarch64*, const Symbol_table*,
1792 const Layout*);
1793
1794 // Whether a section is a scannable text section.
1795 bool
1796 text_section_is_scannable(const elfcpp::Shdr<size, big_endian>&, unsigned int,
1797 const Output_section*, const Symbol_table*);
1798
1799 // Convert regular input section with index SHNDX to a relaxed section.
1800 void
6bf56e74 1801 convert_input_section_to_relaxed_section(unsigned shndx)
83a01957
HS
1802 {
1803 // The stubs have relocations and we need to process them after writing
1804 // out the stubs. So relocation now must follow section write.
6bf56e74 1805 this->set_section_offset(shndx, -1ULL);
83a01957
HS
1806 this->set_relocs_must_follow_section_writes();
1807 }
1808
5019d64a
HS
1809 // Structure for mapping symbol position.
1810 struct Mapping_symbol_position
1811 {
1812 Mapping_symbol_position(unsigned int shndx, AArch64_address offset):
1813 shndx_(shndx), offset_(offset)
1814 {}
1815
1816 // "<" comparator used in ordered_map container.
1817 bool
1818 operator<(const Mapping_symbol_position& p) const
1819 {
1820 return (this->shndx_ < p.shndx_
1821 || (this->shndx_ == p.shndx_ && this->offset_ < p.offset_));
1822 }
1823
1824 // Section index.
1825 unsigned int shndx_;
1826
1827 // Section offset.
1828 AArch64_address offset_;
1829 };
1830
1831 typedef std::map<Mapping_symbol_position, char> Mapping_symbol_info;
1832
83a01957
HS
1833 protected:
1834 // Post constructor setup.
1835 void
1836 do_setup()
1837 {
1838 // Call parent's setup method.
1839 Sized_relobj_file<size, big_endian>::do_setup();
1840
1841 // Initialize look-up tables.
1842 this->stub_tables_.resize(this->shnum());
1843 }
1844
1845 virtual void
1846 do_relocate_sections(
1847 const Symbol_table* symtab, const Layout* layout,
1848 const unsigned char* pshdrs, Output_file* of,
1849 typename Sized_relobj_file<size, big_endian>::Views* pviews);
1850
5019d64a
HS
1851 // Count local symbols and (optionally) record mapping info.
1852 virtual void
1853 do_count_local_symbols(Stringpool_template<char>*,
1854 Stringpool_template<char>*);
1855
83a01957 1856 private:
df2f63a6
HS
1857 // Fix all errata in the object, and for each erratum, relocate corresponding
1858 // erratum stub.
a48d0c12 1859 void
df2f63a6
HS
1860 fix_errata_and_relocate_erratum_stubs(
1861 typename Sized_relobj_file<size, big_endian>::Views* pviews);
a48d0c12 1862
0ef3814f
HS
1863 // Try to fix erratum 843419 in an optimized way. Return true if patch is
1864 // applied.
1865 bool
1866 try_fix_erratum_843419_optimized(
1867 The_erratum_stub*,
1868 typename Sized_relobj_file<size, big_endian>::View_size&);
1869
83a01957
HS
1870 // Whether a section needs to be scanned for relocation stubs.
1871 bool
1872 section_needs_reloc_stub_scanning(const elfcpp::Shdr<size, big_endian>&,
1873 const Relobj::Output_sections&,
1874 const Symbol_table*, const unsigned char*);
1875
1876 // List of stub tables.
1877 Stub_table_list stub_tables_;
5019d64a
HS
1878
1879 // Mapping symbol information sorted by (section index, section_offset).
1880 Mapping_symbol_info mapping_symbol_info_;
83a01957
HS
1881}; // End of AArch64_relobj
1882
1883
5019d64a
HS
1884// Override to record mapping symbol information.
1885template<int size, bool big_endian>
1886void
1887AArch64_relobj<size, big_endian>::do_count_local_symbols(
1888 Stringpool_template<char>* pool, Stringpool_template<char>* dynpool)
1889{
1890 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
1891
1892 // Only erratum-fixing work needs mapping symbols, so skip this time consuming
1893 // processing if not fixing erratum.
2f0c79aa
HS
1894 if (!parameters->options().fix_cortex_a53_843419()
1895 && !parameters->options().fix_cortex_a53_835769())
5019d64a
HS
1896 return;
1897
1898 const unsigned int loccount = this->local_symbol_count();
1899 if (loccount == 0)
1900 return;
1901
1902 // Read the symbol table section header.
1903 const unsigned int symtab_shndx = this->symtab_shndx();
1904 elfcpp::Shdr<size, big_endian>
1905 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
1906 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1907
1908 // Read the local symbols.
1909 const int sym_size =elfcpp::Elf_sizes<size>::sym_size;
1910 gold_assert(loccount == symtabshdr.get_sh_info());
1911 off_t locsize = loccount * sym_size;
1912 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1913 locsize, true, true);
1914
1915 // For mapping symbol processing, we need to read the symbol names.
1916 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
1917 if (strtab_shndx >= this->shnum())
1918 {
1919 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
1920 return;
1921 }
1922
1923 elfcpp::Shdr<size, big_endian>
1924 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
1925 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
1926 {
1927 this->error(_("symbol table name section has wrong type: %u"),
1928 static_cast<unsigned int>(strtabshdr.get_sh_type()));
1929 return;
1930 }
1931
1932 const char* pnames =
1933 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
1934 strtabshdr.get_sh_size(),
1935 false, false));
1936
1937 // Skip the first dummy symbol.
1938 psyms += sym_size;
1939 typename Sized_relobj_file<size, big_endian>::Local_values*
1940 plocal_values = this->local_values();
1941 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1942 {
1943 elfcpp::Sym<size, big_endian> sym(psyms);
1944 Symbol_value<size>& lv((*plocal_values)[i]);
1945 AArch64_address input_value = lv.input_value();
1946
b91deca9
HS
1947 // Check to see if this is a mapping symbol. AArch64 mapping symbols are
1948 // defined in "ELF for the ARM 64-bit Architecture", Table 4-4, Mapping
1949 // symbols.
1950 // Mapping symbols could be one of the following 4 forms -
1951 // a) $x
1952 // b) $x.<any...>
1953 // c) $d
1954 // d) $d.<any...>
5019d64a
HS
1955 const char* sym_name = pnames + sym.get_st_name();
1956 if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd')
b91deca9 1957 && (sym_name[2] == '\0' || sym_name[2] == '.'))
5019d64a
HS
1958 {
1959 bool is_ordinary;
1960 unsigned int input_shndx =
1961 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
1962 gold_assert(is_ordinary);
1963
1964 Mapping_symbol_position msp(input_shndx, input_value);
1965 // Insert mapping_symbol_info into map whose ordering is defined by
1966 // (shndx, offset_within_section).
1967 this->mapping_symbol_info_[msp] = sym_name[1];
1968 }
1969 }
1970}
1971
1972
df2f63a6
HS
1973// Fix all errata in the object and for each erratum, we relocate the
1974// corresponding erratum stub (by calling Stub_table::relocate_erratum_stub).
a48d0c12
HS
1975
1976template<int size, bool big_endian>
1977void
df2f63a6 1978AArch64_relobj<size, big_endian>::fix_errata_and_relocate_erratum_stubs(
a48d0c12
HS
1979 typename Sized_relobj_file<size, big_endian>::Views* pviews)
1980{
1981 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
1982 unsigned int shnum = this->shnum();
1983 for (unsigned int i = 1; i < shnum; ++i)
1984 {
1985 The_stub_table* stub_table = this->stub_table(i);
1986 if (!stub_table)
1987 continue;
1988 std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
1989 ipair(stub_table->find_erratum_stubs_for_input_section(this, i));
1990 Erratum_stub_set_iter p = ipair.first, end = ipair.second;
1991 while (p != end)
1992 {
1993 The_erratum_stub* stub = *p;
1994 typename Sized_relobj_file<size, big_endian>::View_size&
1995 pview((*pviews)[i]);
1996
1997 // Double check data before fix.
56b06706
HS
1998 gold_assert(pview.address + stub->sh_offset()
1999 == stub->erratum_address());
2000
2001 // Update previously recorded erratum insn with relocated
2002 // version.
a48d0c12
HS
2003 Insntype* ip =
2004 reinterpret_cast<Insntype*>(pview.view + stub->sh_offset());
2005 Insntype insn_to_fix = ip[0];
56b06706 2006 stub->update_erratum_insn(insn_to_fix);
a48d0c12 2007
0ef3814f
HS
2008 // First try to see if erratum is 843419 and if it can be fixed
2009 // without using branch-to-stub.
2010 if (!try_fix_erratum_843419_optimized(stub, pview))
2011 {
2012 // Replace the erratum insn with a branch-to-stub.
2013 AArch64_address stub_address =
2014 stub_table->erratum_stub_address(stub);
2015 unsigned int b_offset = stub_address - stub->erratum_address();
2016 AArch64_relocate_functions<size, big_endian>::construct_b(
2017 pview.view + stub->sh_offset(), b_offset & 0xfffffff);
2018 }
df2f63a6
HS
2019
2020 // Erratum fix is done (or skipped), continue to relocate erratum
2021 // stub. Note, when erratum fix is skipped (either because we
2022 // proactively change the code sequence or the code sequence is
2023 // changed by relaxation, etc), we can still safely relocate the
2024 // erratum stub, ignoring the fact the erratum could never be
2025 // executed.
2026 stub_table->relocate_erratum_stub(
2027 stub, pview.view + (stub_table->address() - pview.address));
2028
2029 // Next erratum stub.
a48d0c12
HS
2030 ++p;
2031 }
2032 }
2033}
2034
2035
0ef3814f
HS
2036// This is an optimization for 843419. This erratum requires the sequence begin
2037// with 'adrp', when final value calculated by adrp fits in adr, we can just
2038// replace 'adrp' with 'adr', so we save 2 jumps per occurrence. (Note, however,
2039// in this case, we do not delete the erratum stub (too late to do so), it is
2040// merely generated without ever being called.)
2041
2042template<int size, bool big_endian>
2043bool
2044AArch64_relobj<size, big_endian>::try_fix_erratum_843419_optimized(
2045 The_erratum_stub* stub,
2046 typename Sized_relobj_file<size, big_endian>::View_size& pview)
2047{
2048 if (stub->type() != ST_E_843419)
2049 return false;
2050
2051 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
2052 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
2053 E843419_stub<size, big_endian>* e843419_stub =
2054 reinterpret_cast<E843419_stub<size, big_endian>*>(stub);
2055 AArch64_address pc = pview.address + e843419_stub->adrp_sh_offset();
37de058a
JW
2056 unsigned int adrp_offset = e843419_stub->adrp_sh_offset ();
2057 Insntype* adrp_view = reinterpret_cast<Insntype*>(pview.view + adrp_offset);
0ef3814f 2058 Insntype adrp_insn = adrp_view[0];
37de058a
JW
2059
2060 // If the instruction at adrp_sh_offset is "mrs R, tpidr_el0", it may come
2061 // from IE -> LE relaxation etc. This is a side-effect of TLS relaxation that
2062 // ADRP has been turned into MRS, there is no erratum risk anymore.
2063 // Therefore, we return true to avoid doing unnecessary branch-to-stub.
2064 if (Insn_utilities::is_mrs_tpidr_el0(adrp_insn))
2065 return true;
2066
2067 // If the instruction at adrp_sh_offset is not ADRP and the instruction before
2068 // it is "mrs R, tpidr_el0", it may come from LD -> LE relaxation etc.
2069 // Like the above case, there is no erratum risk any more, we can safely
2070 // return true.
2071 if (!Insn_utilities::is_adrp(adrp_insn) && adrp_offset)
2072 {
2073 Insntype* prev_view
2074 = reinterpret_cast<Insntype*>(pview.view + adrp_offset - 4);
2075 Insntype prev_insn = prev_view[0];
2076
2077 if (Insn_utilities::is_mrs_tpidr_el0(prev_insn))
2078 return true;
2079 }
2080
2081 /* If we reach here, the first instruction must be ADRP. */
0ef3814f
HS
2082 gold_assert(Insn_utilities::is_adrp(adrp_insn));
2083 // Get adrp 33-bit signed imm value.
2084 int64_t adrp_imm = Insn_utilities::
2085 aarch64_adrp_decode_imm(adrp_insn);
2086 // adrp - final value transferred to target register is calculated as:
2087 // PC[11:0] = Zeros(12)
2088 // adrp_dest_value = PC + adrp_imm;
2089 int64_t adrp_dest_value = (pc & ~((1 << 12) - 1)) + adrp_imm;
2090 // adr -final value transferred to target register is calucalted as:
2091 // PC + adr_imm
2092 // So we have:
2093 // PC + adr_imm = adrp_dest_value
2094 // ==>
2095 // adr_imm = adrp_dest_value - PC
2096 int64_t adr_imm = adrp_dest_value - pc;
2097 // Check if imm fits in adr (21-bit signed).
2098 if (-(1 << 20) <= adr_imm && adr_imm < (1 << 20))
2099 {
2100 // Convert 'adrp' into 'adr'.
f945ba50 2101 Insntype adr_insn = adrp_insn & ((1u << 31) - 1);
0ef3814f
HS
2102 adr_insn = Insn_utilities::
2103 aarch64_adr_encode_imm(adr_insn, adr_imm);
2104 elfcpp::Swap<32, big_endian>::writeval(adrp_view, adr_insn);
2105 return true;
2106 }
2107 return false;
2108}
2109
2110
83a01957
HS
2111// Relocate sections.
2112
2113template<int size, bool big_endian>
2114void
2115AArch64_relobj<size, big_endian>::do_relocate_sections(
2116 const Symbol_table* symtab, const Layout* layout,
2117 const unsigned char* pshdrs, Output_file* of,
2118 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2119{
98461510
CC
2120 // Relocate the section data.
2121 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2122 1, this->shnum() - 1);
83a01957
HS
2123
2124 // We do not generate stubs if doing a relocatable link.
2125 if (parameters->options().relocatable())
2126 return;
2127
df2f63a6
HS
2128 // This part only relocates erratum stubs that belong to input sections of this
2129 // object file.
2f0c79aa
HS
2130 if (parameters->options().fix_cortex_a53_843419()
2131 || parameters->options().fix_cortex_a53_835769())
df2f63a6 2132 this->fix_errata_and_relocate_erratum_stubs(pviews);
a48d0c12 2133
83a01957
HS
2134 Relocate_info<size, big_endian> relinfo;
2135 relinfo.symtab = symtab;
2136 relinfo.layout = layout;
2137 relinfo.object = this;
2138
df2f63a6
HS
2139 // This part relocates all reloc stubs that are contained in stub_tables of
2140 // this object file.
83a01957
HS
2141 unsigned int shnum = this->shnum();
2142 The_target_aarch64* target = The_target_aarch64::current_target();
2143
2144 for (unsigned int i = 1; i < shnum; ++i)
2145 {
2146 The_aarch64_input_section* aarch64_input_section =
2147 target->find_aarch64_input_section(this, i);
2148 if (aarch64_input_section != NULL
2149 && aarch64_input_section->is_stub_table_owner()
2150 && !aarch64_input_section->stub_table()->empty())
2151 {
2152 Output_section* os = this->output_section(i);
2153 gold_assert(os != NULL);
2154
2155 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
2156 relinfo.reloc_shdr = NULL;
2157 relinfo.data_shndx = i;
2158 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
2159
2160 typename Sized_relobj_file<size, big_endian>::View_size&
2161 view_struct = (*pviews)[i];
2162 gold_assert(view_struct.view != NULL);
2163
2164 The_stub_table* stub_table = aarch64_input_section->stub_table();
2165 off_t offset = stub_table->address() - view_struct.address;
2166 unsigned char* view = view_struct.view + offset;
2167 AArch64_address address = stub_table->address();
2168 section_size_type view_size = stub_table->data_size();
df2f63a6
HS
2169 stub_table->relocate_reloc_stubs(&relinfo, target, os, view, address,
2170 view_size);
83a01957
HS
2171 }
2172 }
2173}
2174
2175
2176// Determine if an input section is scannable for stub processing. SHDR is
2177// the header of the section and SHNDX is the section index. OS is the output
2178// section for the input section and SYMTAB is the global symbol table used to
2179// look up ICF information.
2180
2181template<int size, bool big_endian>
2182bool
2183AArch64_relobj<size, big_endian>::text_section_is_scannable(
2184 const elfcpp::Shdr<size, big_endian>& text_shdr,
2185 unsigned int text_shndx,
2186 const Output_section* os,
2187 const Symbol_table* symtab)
2188{
2189 // Skip any empty sections, unallocated sections or sections whose
2190 // type are not SHT_PROGBITS.
2191 if (text_shdr.get_sh_size() == 0
2192 || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
2193 || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
2194 return false;
2195
2196 // Skip any discarded or ICF'ed sections.
2197 if (os == NULL || symtab->is_section_folded(this, text_shndx))
2198 return false;
2199
2200 // Skip exception frame.
2201 if (strcmp(os->name(), ".eh_frame") == 0)
2202 return false ;
2203
2204 gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
2205 os->find_relaxed_input_section(this, text_shndx) != NULL);
2206
2207 return true;
2208}
2209
2210
2211// Determine if we want to scan the SHNDX-th section for relocation stubs.
2212// This is a helper for AArch64_relobj::scan_sections_for_stubs().
2213
2214template<int size, bool big_endian>
2215bool
2216AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
2217 const elfcpp::Shdr<size, big_endian>& shdr,
2218 const Relobj::Output_sections& out_sections,
2219 const Symbol_table* symtab,
2220 const unsigned char* pshdrs)
2221{
2222 unsigned int sh_type = shdr.get_sh_type();
2223 if (sh_type != elfcpp::SHT_RELA)
2224 return false;
2225
2226 // Ignore empty section.
2227 off_t sh_size = shdr.get_sh_size();
2228 if (sh_size == 0)
2229 return false;
2230
2231 // Ignore reloc section with unexpected symbol table. The
2232 // error will be reported in the final link.
2233 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
2234 return false;
2235
2236 gold_assert(sh_type == elfcpp::SHT_RELA);
2237 unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2238
2239 // Ignore reloc section with unexpected entsize or uneven size.
2240 // The error will be reported in the final link.
2241 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
2242 return false;
2243
2244 // Ignore reloc section with bad info. This error will be
2245 // reported in the final link.
2246 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
2247 if (text_shndx >= this->shnum())
2248 return false;
2249
2250 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2251 const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
2252 text_shndx * shdr_size);
2253 return this->text_section_is_scannable(text_shdr, text_shndx,
2254 out_sections[text_shndx], symtab);
2255}
2256
2257
2f0c79aa 2258// Scan section SHNDX for erratum 843419 and 835769.
5019d64a
HS
2259
2260template<int size, bool big_endian>
2261void
2f0c79aa 2262AArch64_relobj<size, big_endian>::scan_errata(
5019d64a
HS
2263 unsigned int shndx, const elfcpp::Shdr<size, big_endian>& shdr,
2264 Output_section* os, const Symbol_table* symtab,
2265 The_target_aarch64* target)
2266{
2267 if (shdr.get_sh_size() == 0
2268 || (shdr.get_sh_flags() &
2269 (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) == 0
2270 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
2271 return;
2272
2273 if (!os || symtab->is_section_folded(this, shndx)) return;
2274
2275 AArch64_address output_offset = this->get_output_section_offset(shndx);
2276 AArch64_address output_address;
2277 if (output_offset != invalid_address)
2278 output_address = os->address() + output_offset;
2279 else
2280 {
2281 const Output_relaxed_input_section* poris =
2282 os->find_relaxed_input_section(this, shndx);
2283 if (!poris) return;
2284 output_address = poris->address();
2285 }
2286
2287 section_size_type input_view_size = 0;
2288 const unsigned char* input_view =
2289 this->section_contents(shndx, &input_view_size, false);
2290
2291 Mapping_symbol_position section_start(shndx, 0);
2292 // Find the first mapping symbol record within section shndx.
2293 typename Mapping_symbol_info::const_iterator p =
2294 this->mapping_symbol_info_.lower_bound(section_start);
5019d64a
HS
2295 while (p != this->mapping_symbol_info_.end() &&
2296 p->first.shndx_ == shndx)
2297 {
2298 typename Mapping_symbol_info::const_iterator prev = p;
2299 ++p;
2300 if (prev->second == 'x')
2301 {
2302 section_size_type span_start =
2303 convert_to_section_size_type(prev->first.offset_);
2304 section_size_type span_end;
2305 if (p != this->mapping_symbol_info_.end()
2306 && p->first.shndx_ == shndx)
2307 span_end = convert_to_section_size_type(p->first.offset_);
2308 else
2309 span_end = convert_to_section_size_type(shdr.get_sh_size());
2f0c79aa
HS
2310
2311 // Here we do not share the scanning code of both errata. For 843419,
2312 // only the last few insns of each page are examined, which is fast,
2313 // whereas, for 835769, every insn pair needs to be checked.
2314
2315 if (parameters->options().fix_cortex_a53_843419())
2316 target->scan_erratum_843419_span(
2317 this, shndx, span_start, span_end,
2318 const_cast<unsigned char*>(input_view), output_address);
2319
2320 if (parameters->options().fix_cortex_a53_835769())
2321 target->scan_erratum_835769_span(
5019d64a
HS
2322 this, shndx, span_start, span_end,
2323 const_cast<unsigned char*>(input_view), output_address);
2324 }
2325 }
2326}
2327
2328
83a01957
HS
2329// Scan relocations for stub generation.
2330
2331template<int size, bool big_endian>
2332void
2333AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
2334 The_target_aarch64* target,
2335 const Symbol_table* symtab,
2336 const Layout* layout)
2337{
2338 unsigned int shnum = this->shnum();
2339 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2340
2341 // Read the section headers.
2342 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
2343 shnum * shdr_size,
2344 true, true);
2345
2346 // To speed up processing, we set up hash tables for fast lookup of
2347 // input offsets to output addresses.
2348 this->initialize_input_to_output_maps();
2349
2350 const Relobj::Output_sections& out_sections(this->output_sections());
2351
2352 Relocate_info<size, big_endian> relinfo;
2353 relinfo.symtab = symtab;
2354 relinfo.layout = layout;
2355 relinfo.object = this;
2356
2357 // Do relocation stubs scanning.
2358 const unsigned char* p = pshdrs + shdr_size;
2359 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
2360 {
2361 const elfcpp::Shdr<size, big_endian> shdr(p);
2f0c79aa
HS
2362 if (parameters->options().fix_cortex_a53_843419()
2363 || parameters->options().fix_cortex_a53_835769())
2364 scan_errata(i, shdr, out_sections[i], symtab, target);
83a01957
HS
2365 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
2366 pshdrs))
2367 {
2368 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
2369 AArch64_address output_offset =
2370 this->get_output_section_offset(index);
2371 AArch64_address output_address;
2372 if (output_offset != invalid_address)
2373 {
2374 output_address = out_sections[index]->address() + output_offset;
2375 }
2376 else
2377 {
2378 // Currently this only happens for a relaxed section.
2379 const Output_relaxed_input_section* poris =
2380 out_sections[index]->find_relaxed_input_section(this, index);
2381 gold_assert(poris != NULL);
2382 output_address = poris->address();
2383 }
2384
2385 // Get the relocations.
2386 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
2387 shdr.get_sh_size(),
2388 true, false);
2389
2390 // Get the section contents.
2391 section_size_type input_view_size = 0;
2392 const unsigned char* input_view =
2393 this->section_contents(index, &input_view_size, false);
2394
2395 relinfo.reloc_shndx = i;
2396 relinfo.data_shndx = index;
2397 unsigned int sh_type = shdr.get_sh_type();
2398 unsigned int reloc_size;
2399 gold_assert (sh_type == elfcpp::SHT_RELA);
2400 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2401
2402 Output_section* os = out_sections[index];
2403 target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
2404 shdr.get_sh_size() / reloc_size,
2405 os,
2406 output_offset == invalid_address,
2407 input_view, output_address,
2408 input_view_size);
2409 }
2410 }
2411}
2412
2413
2414// A class to wrap an ordinary input section containing executable code.
2415
2416template<int size, bool big_endian>
2417class AArch64_input_section : public Output_relaxed_input_section
2418{
2419 public:
2420 typedef Stub_table<size, big_endian> The_stub_table;
2421
2422 AArch64_input_section(Relobj* relobj, unsigned int shndx)
2423 : Output_relaxed_input_section(relobj, shndx, 1),
2424 stub_table_(NULL),
2425 original_contents_(NULL), original_size_(0),
2426 original_addralign_(1)
2427 { }
2428
2429 ~AArch64_input_section()
2430 { delete[] this->original_contents_; }
2431
2432 // Initialize.
2433 void
2434 init();
2435
2436 // Set the stub_table.
2437 void
2438 set_stub_table(The_stub_table* st)
2439 { this->stub_table_ = st; }
2440
2441 // Whether this is a stub table owner.
2442 bool
2443 is_stub_table_owner() const
2444 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
2445
2446 // Return the original size of the section.
2447 uint32_t
2448 original_size() const
2449 { return this->original_size_; }
2450
2451 // Return the stub table.
2452 The_stub_table*
2453 stub_table()
2454 { return stub_table_; }
2455
2456 protected:
2457 // Write out this input section.
2458 void
2459 do_write(Output_file*);
2460
2461 // Return required alignment of this.
2462 uint64_t
2463 do_addralign() const
2464 {
2465 if (this->is_stub_table_owner())
2466 return std::max(this->stub_table_->addralign(),
2467 static_cast<uint64_t>(this->original_addralign_));
2468 else
2469 return this->original_addralign_;
2470 }
2471
2472 // Finalize data size.
2473 void
2474 set_final_data_size();
2475
2476 // Reset address and file offset.
2477 void
2478 do_reset_address_and_file_offset();
2479
2480 // Output offset.
2481 bool
2482 do_output_offset(const Relobj* object, unsigned int shndx,
2483 section_offset_type offset,
2484 section_offset_type* poutput) const
2485 {
2486 if ((object == this->relobj())
2487 && (shndx == this->shndx())
2488 && (offset >= 0)
2489 && (offset <=
2490 convert_types<section_offset_type, uint32_t>(this->original_size_)))
2491 {
2492 *poutput = offset;
2493 return true;
2494 }
2495 else
2496 return false;
2497 }
2498
2499 private:
2500 // Copying is not allowed.
2501 AArch64_input_section(const AArch64_input_section&);
2502 AArch64_input_section& operator=(const AArch64_input_section&);
2503
2504 // The relocation stubs.
2505 The_stub_table* stub_table_;
2506 // Original section contents. We have to make a copy here since the file
2507 // containing the original section may not be locked when we need to access
2508 // the contents.
2509 unsigned char* original_contents_;
2510 // Section size of the original input section.
2511 uint32_t original_size_;
2512 // Address alignment of the original input section.
2513 uint32_t original_addralign_;
2514}; // End of AArch64_input_section
2515
2516
2517// Finalize data size.
2518
2519template<int size, bool big_endian>
2520void
2521AArch64_input_section<size, big_endian>::set_final_data_size()
2522{
2523 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2524
2525 if (this->is_stub_table_owner())
2526 {
2527 this->stub_table_->finalize_data_size();
2528 off = align_address(off, this->stub_table_->addralign());
2529 off += this->stub_table_->data_size();
2530 }
2531 this->set_data_size(off);
2532}
2533
2534
2535// Reset address and file offset.
2536
2537template<int size, bool big_endian>
2538void
2539AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
2540{
2541 // Size of the original input section contents.
2542 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2543
2544 // If this is a stub table owner, account for the stub table size.
2545 if (this->is_stub_table_owner())
2546 {
2547 The_stub_table* stub_table = this->stub_table_;
2548
2549 // Reset the stub table's address and file offset. The
2550 // current data size for child will be updated after that.
2551 stub_table_->reset_address_and_file_offset();
2552 off = align_address(off, stub_table_->addralign());
2553 off += stub_table->current_data_size();
2554 }
2555
2556 this->set_current_data_size(off);
2557}
2558
2559
2560// Initialize an Arm_input_section.
2561
2562template<int size, bool big_endian>
2563void
2564AArch64_input_section<size, big_endian>::init()
2565{
2566 Relobj* relobj = this->relobj();
2567 unsigned int shndx = this->shndx();
2568
2569 // We have to cache original size, alignment and contents to avoid locking
2570 // the original file.
2571 this->original_addralign_ =
2572 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
2573
2574 // This is not efficient but we expect only a small number of relaxed
2575 // input sections for stubs.
2576 section_size_type section_size;
2577 const unsigned char* section_contents =
2578 relobj->section_contents(shndx, &section_size, false);
2579 this->original_size_ =
2580 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
2581
2582 gold_assert(this->original_contents_ == NULL);
2583 this->original_contents_ = new unsigned char[section_size];
2584 memcpy(this->original_contents_, section_contents, section_size);
2585
2586 // We want to make this look like the original input section after
2587 // output sections are finalized.
2588 Output_section* os = relobj->output_section(shndx);
2589 off_t offset = relobj->output_section_offset(shndx);
2590 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2591 this->set_address(os->address() + offset);
2592 this->set_file_offset(os->offset() + offset);
2593 this->set_current_data_size(this->original_size_);
2594 this->finalize_data_size();
2595}
2596
2597
2598// Write data to output file.
2599
2600template<int size, bool big_endian>
2601void
2602AArch64_input_section<size, big_endian>::do_write(Output_file* of)
2603{
2604 // We have to write out the original section content.
2605 gold_assert(this->original_contents_ != NULL);
2606 of->write(this->offset(), this->original_contents_,
2607 this->original_size_);
2608
2609 // If this owns a stub table and it is not empty, write it.
2610 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2611 this->stub_table_->write(of);
2612}
2613
2614
2615// Arm output section class. This is defined mainly to add a number of stub
2616// generation methods.
2617
2618template<int size, bool big_endian>
2619class AArch64_output_section : public Output_section
2620{
2621 public:
2622 typedef Target_aarch64<size, big_endian> The_target_aarch64;
2623 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2624 typedef Stub_table<size, big_endian> The_stub_table;
2625 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2626
2627 public:
2628 AArch64_output_section(const char* name, elfcpp::Elf_Word type,
2629 elfcpp::Elf_Xword flags)
2630 : Output_section(name, type, flags)
2631 { }
2632
2633 ~AArch64_output_section() {}
2634
2635 // Group input sections for stub generation.
2636 void
2637 group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
2638 const Task*);
2639
2640 private:
2641 typedef Output_section::Input_section Input_section;
2642 typedef Output_section::Input_section_list Input_section_list;
2643
2644 // Create a stub group.
2645 void
2646 create_stub_group(Input_section_list::const_iterator,
2647 Input_section_list::const_iterator,
2648 Input_section_list::const_iterator,
2649 The_target_aarch64*,
2650 std::vector<Output_relaxed_input_section*>&,
2651 const Task*);
2652}; // End of AArch64_output_section
2653
2654
2655// Create a stub group for input sections from FIRST to LAST. OWNER points to
2656// the input section that will be the owner of the stub table.
2657
2658template<int size, bool big_endian> void
2659AArch64_output_section<size, big_endian>::create_stub_group(
2660 Input_section_list::const_iterator first,
2661 Input_section_list::const_iterator last,
2662 Input_section_list::const_iterator owner,
2663 The_target_aarch64* target,
2664 std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
2665 const Task* task)
2666{
2667 // Currently we convert ordinary input sections into relaxed sections only
2668 // at this point.
2669 The_aarch64_input_section* input_section;
2670 if (owner->is_relaxed_input_section())
2671 gold_unreachable();
2672 else
2673 {
2674 gold_assert(owner->is_input_section());
2675 // Create a new relaxed input section. We need to lock the original
2676 // file.
2677 Task_lock_obj<Object> tl(task, owner->relobj());
2678 input_section =
2679 target->new_aarch64_input_section(owner->relobj(), owner->shndx());
2680 new_relaxed_sections.push_back(input_section);
2681 }
2682
2683 // Create a stub table.
2684 The_stub_table* stub_table =
2685 target->new_stub_table(input_section);
2686
2687 input_section->set_stub_table(stub_table);
2688
2689 Input_section_list::const_iterator p = first;
2690 // Look for input sections or relaxed input sections in [first ... last].
2691 do
2692 {
2693 if (p->is_input_section() || p->is_relaxed_input_section())
2694 {
2695 // The stub table information for input sections live
2696 // in their objects.
2697 The_aarch64_relobj* aarch64_relobj =
2698 static_cast<The_aarch64_relobj*>(p->relobj());
2699 aarch64_relobj->set_stub_table(p->shndx(), stub_table);
2700 }
2701 }
2702 while (p++ != last);
2703}
2704
2705
2706// Group input sections for stub generation. GROUP_SIZE is roughly the limit of
2707// stub groups. We grow a stub group by adding input section until the size is
2708// just below GROUP_SIZE. The last input section will be converted into a stub
2709// table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
2710// after the stub table, effectively doubling the group size.
2711//
2712// This is similar to the group_sections() function in elf32-arm.c but is
2713// implemented differently.
2714
2715template<int size, bool big_endian>
2716void AArch64_output_section<size, big_endian>::group_sections(
2717 section_size_type group_size,
2718 bool stubs_always_after_branch,
2719 Target_aarch64<size, big_endian>* target,
2720 const Task* task)
2721{
2722 typedef enum
2723 {
2724 NO_GROUP,
2725 FINDING_STUB_SECTION,
2726 HAS_STUB_SECTION
2727 } State;
2728
2729 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
2730
2731 State state = NO_GROUP;
2732 section_size_type off = 0;
2733 section_size_type group_begin_offset = 0;
2734 section_size_type group_end_offset = 0;
2735 section_size_type stub_table_end_offset = 0;
2736 Input_section_list::const_iterator group_begin =
2737 this->input_sections().end();
2738 Input_section_list::const_iterator stub_table =
2739 this->input_sections().end();
2740 Input_section_list::const_iterator group_end = this->input_sections().end();
2741 for (Input_section_list::const_iterator p = this->input_sections().begin();
2742 p != this->input_sections().end();
2743 ++p)
2744 {
2745 section_size_type section_begin_offset =
2746 align_address(off, p->addralign());
2747 section_size_type section_end_offset =
2748 section_begin_offset + p->data_size();
2749
2750 // Check to see if we should group the previously seen sections.
2751 switch (state)
2752 {
2753 case NO_GROUP:
2754 break;
2755
2756 case FINDING_STUB_SECTION:
2757 // Adding this section makes the group larger than GROUP_SIZE.
2758 if (section_end_offset - group_begin_offset >= group_size)
2759 {
2760 if (stubs_always_after_branch)
2761 {
2762 gold_assert(group_end != this->input_sections().end());
2763 this->create_stub_group(group_begin, group_end, group_end,
2764 target, new_relaxed_sections,
2765 task);
2766 state = NO_GROUP;
2767 }
2768 else
2769 {
2770 // Input sections up to stub_group_size bytes after the stub
2771 // table can be handled by it too.
2772 state = HAS_STUB_SECTION;
2773 stub_table = group_end;
2774 stub_table_end_offset = group_end_offset;
2775 }
2776 }
2777 break;
2778
2779 case HAS_STUB_SECTION:
2780 // Adding this section makes the post stub-section group larger
2781 // than GROUP_SIZE.
2782 gold_unreachable();
2783 // NOT SUPPORTED YET. For completeness only.
2784 if (section_end_offset - stub_table_end_offset >= group_size)
2785 {
2786 gold_assert(group_end != this->input_sections().end());
2787 this->create_stub_group(group_begin, group_end, stub_table,
2788 target, new_relaxed_sections, task);
2789 state = NO_GROUP;
2790 }
2791 break;
2792
2793 default:
2794 gold_unreachable();
2795 }
2796
2797 // If we see an input section and currently there is no group, start
2798 // a new one. Skip any empty sections. We look at the data size
2799 // instead of calling p->relobj()->section_size() to avoid locking.
2800 if ((p->is_input_section() || p->is_relaxed_input_section())
2801 && (p->data_size() != 0))
2802 {
2803 if (state == NO_GROUP)
2804 {
2805 state = FINDING_STUB_SECTION;
2806 group_begin = p;
2807 group_begin_offset = section_begin_offset;
2808 }
2809
2810 // Keep track of the last input section seen.
2811 group_end = p;
2812 group_end_offset = section_end_offset;
2813 }
2814
2815 off = section_end_offset;
2816 }
2817
2818 // Create a stub group for any ungrouped sections.
2819 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
2820 {
2821 gold_assert(group_end != this->input_sections().end());
2822 this->create_stub_group(group_begin, group_end,
2823 (state == FINDING_STUB_SECTION
2824 ? group_end
2825 : stub_table),
2826 target, new_relaxed_sections, task);
2827 }
8e33481e 2828
83a01957
HS
2829 if (!new_relaxed_sections.empty())
2830 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
2831
2832 // Update the section offsets
2833 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
2834 {
2835 The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
2836 new_relaxed_sections[i]->relobj());
2837 unsigned int shndx = new_relaxed_sections[i]->shndx();
2838 // Tell AArch64_relobj that this input section is converted.
2839 relobj->convert_input_section_to_relaxed_section(shndx);
2840 }
2841} // End of AArch64_output_section::group_sections
3a531937 2842
053a4d68 2843
9363c7c3
JY
2844AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
2845
3a531937 2846
053a4d68
JY
2847// The aarch64 target class.
2848// See the ABI at
2849// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
2850template<int size, bool big_endian>
2851class Target_aarch64 : public Sized_target<size, big_endian>
2852{
2853 public:
83a01957 2854 typedef Target_aarch64<size, big_endian> This;
053a4d68
JY
2855 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
2856 Reloc_section;
83a01957 2857 typedef Relocate_info<size, big_endian> The_relocate_info;
053a4d68 2858 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
83a01957
HS
2859 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2860 typedef Reloc_stub<size, big_endian> The_reloc_stub;
a48d0c12 2861 typedef Erratum_stub<size, big_endian> The_erratum_stub;
83a01957
HS
2862 typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
2863 typedef Stub_table<size, big_endian> The_stub_table;
2864 typedef std::vector<The_stub_table*> Stub_table_list;
2865 typedef typename Stub_table_list::iterator Stub_table_iterator;
2866 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2867 typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
2868 typedef Unordered_map<Section_id,
2869 AArch64_input_section<size, big_endian>*,
2870 Section_id_hash> AArch64_input_section_map;
5019d64a 2871 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
8e33481e 2872 const static int TCB_SIZE = size / 8 * 2;
053a4d68
JY
2873
2874 Target_aarch64(const Target::Target_info* info = &aarch64_info)
2875 : Sized_target<size, big_endian>(info),
3a531937
JY
2876 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
2877 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
2878 rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
83a01957
HS
2879 got_mod_index_offset_(-1U),
2880 tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
0bf32ea9 2881 stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
053a4d68
JY
2882 { }
2883
2884 // Scan the relocations to determine unreferenced sections for
2885 // garbage collection.
2886 void
2887 gc_process_relocs(Symbol_table* symtab,
2888 Layout* layout,
2889 Sized_relobj_file<size, big_endian>* object,
2890 unsigned int data_shndx,
2891 unsigned int sh_type,
2892 const unsigned char* prelocs,
2893 size_t reloc_count,
2894 Output_section* output_section,
2895 bool needs_special_offset_handling,
2896 size_t local_symbol_count,
2897 const unsigned char* plocal_symbols);
2898
2899 // Scan the relocations to look for symbol adjustments.
2900 void
2901 scan_relocs(Symbol_table* symtab,
2902 Layout* layout,
2903 Sized_relobj_file<size, big_endian>* object,
2904 unsigned int data_shndx,
2905 unsigned int sh_type,
2906 const unsigned char* prelocs,
2907 size_t reloc_count,
2908 Output_section* output_section,
2909 bool needs_special_offset_handling,
2910 size_t local_symbol_count,
2911 const unsigned char* plocal_symbols);
2912
2913 // Finalize the sections.
2914 void
2915 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2916
3a531937
JY
2917 // Return the value to use for a dynamic which requires special
2918 // treatment.
2919 uint64_t
2920 do_dynsym_value(const Symbol*) const;
2921
053a4d68
JY
2922 // Relocate a section.
2923 void
2924 relocate_section(const Relocate_info<size, big_endian>*,
2925 unsigned int sh_type,
2926 const unsigned char* prelocs,
2927 size_t reloc_count,
2928 Output_section* output_section,
2929 bool needs_special_offset_handling,
2930 unsigned char* view,
2931 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2932 section_size_type view_size,
2933 const Reloc_symbol_changes*);
2934
2935 // Scan the relocs during a relocatable link.
2936 void
2937 scan_relocatable_relocs(Symbol_table* symtab,
2938 Layout* layout,
2939 Sized_relobj_file<size, big_endian>* object,
2940 unsigned int data_shndx,
2941 unsigned int sh_type,
2942 const unsigned char* prelocs,
2943 size_t reloc_count,
2944 Output_section* output_section,
2945 bool needs_special_offset_handling,
2946 size_t local_symbol_count,
2947 const unsigned char* plocal_symbols,
2948 Relocatable_relocs*);
2949
4d625b70
CC
2950 // Scan the relocs for --emit-relocs.
2951 void
2952 emit_relocs_scan(Symbol_table* symtab,
2953 Layout* layout,
2954 Sized_relobj_file<size, big_endian>* object,
2955 unsigned int data_shndx,
2956 unsigned int sh_type,
2957 const unsigned char* prelocs,
2958 size_t reloc_count,
2959 Output_section* output_section,
2960 bool needs_special_offset_handling,
2961 size_t local_symbol_count,
2962 const unsigned char* plocal_syms,
2963 Relocatable_relocs* rr);
2964
053a4d68
JY
2965 // Relocate a section during a relocatable link.
2966 void
2967 relocate_relocs(
2968 const Relocate_info<size, big_endian>*,
2969 unsigned int sh_type,
2970 const unsigned char* prelocs,
2971 size_t reloc_count,
2972 Output_section* output_section,
2973 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
053a4d68
JY
2974 unsigned char* view,
2975 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2976 section_size_type view_size,
2977 unsigned char* reloc_view,
2978 section_size_type reloc_view_size);
2979
3a531937
JY
2980 // Return the symbol index to use for a target specific relocation.
2981 // The only target specific relocation is R_AARCH64_TLSDESC for a
2982 // local symbol, which is an absolute reloc.
2983 unsigned int
2984 do_reloc_symbol_index(void*, unsigned int r_type) const
2985 {
2986 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
2987 return 0;
2988 }
2989
2990 // Return the addend to use for a target specific relocation.
7fa5525f
RÁE
2991 uint64_t
2992 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
3a531937 2993
9363c7c3
JY
2994 // Return the PLT section.
2995 uint64_t
2996 do_plt_address_for_global(const Symbol* gsym) const
2997 { return this->plt_section()->address_for_global(gsym); }
2998
2999 uint64_t
3000 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
3001 { return this->plt_section()->address_for_local(relobj, symndx); }
3002
9726c3c1
HS
3003 // This function should be defined in targets that can use relocation
3004 // types to determine (implemented in local_reloc_may_be_function_pointer
3005 // and global_reloc_may_be_function_pointer)
3006 // if a function's pointer is taken. ICF uses this in safe mode to only
3007 // fold those functions whose pointer is defintely not taken.
3008 bool
3009 do_can_check_for_function_pointers() const
3010 { return true; }
3011
053a4d68
JY
3012 // Return the number of entries in the PLT.
3013 unsigned int
3014 plt_entry_count() const;
3015
3016 //Return the offset of the first non-reserved PLT entry.
3017 unsigned int
3018 first_plt_entry_offset() const;
3019
3020 // Return the size of each PLT entry.
3021 unsigned int
3022 plt_entry_size() const;
3023
83a01957
HS
3024 // Create a stub table.
3025 The_stub_table*
3026 new_stub_table(The_aarch64_input_section*);
3027
3028 // Create an aarch64 input section.
3029 The_aarch64_input_section*
3030 new_aarch64_input_section(Relobj*, unsigned int);
3031
3032 // Find an aarch64 input section instance for a given OBJ and SHNDX.
3033 The_aarch64_input_section*
3034 find_aarch64_input_section(Relobj*, unsigned int) const;
3035
3036 // Return the thread control block size.
8e33481e
HS
3037 unsigned int
3038 tcb_size() const { return This::TCB_SIZE; }
3039
83a01957
HS
3040 // Scan a section for stub generation.
3041 void
3042 scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
3043 const unsigned char*, size_t, Output_section*,
3044 bool, const unsigned char*,
3045 Address,
3046 section_size_type);
3047
3048 // Scan a relocation section for stub.
3049 template<int sh_type>
3050 void
3051 scan_reloc_section_for_stubs(
3052 const The_relocate_info* relinfo,
3053 const unsigned char* prelocs,
3054 size_t reloc_count,
3055 Output_section* output_section,
3056 bool needs_special_offset_handling,
3057 const unsigned char* view,
3058 Address view_address,
3059 section_size_type);
3060
df2f63a6 3061 // Relocate a single reloc stub.
83a01957 3062 void
df2f63a6
HS
3063 relocate_reloc_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
3064 Output_section*, unsigned char*, Address,
3065 section_size_type);
83a01957
HS
3066
3067 // Get the default AArch64 target.
3068 static This*
3069 current_target()
3070 {
3071 gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
3072 && parameters->target().get_size() == size
3073 && parameters->target().is_big_endian() == big_endian);
3074 return static_cast<This*>(parameters->sized_target<size, big_endian>());
3075 }
3076
5019d64a 3077
2f0c79aa 3078 // Scan erratum 843419 for a part of a section.
5019d64a
HS
3079 void
3080 scan_erratum_843419_span(
3081 AArch64_relobj<size, big_endian>*,
2f0c79aa
HS
3082 unsigned int,
3083 const section_size_type,
3084 const section_size_type,
3085 unsigned char*,
3086 Address);
3087
3088 // Scan erratum 835769 for a part of a section.
3089 void
3090 scan_erratum_835769_span(
3091 AArch64_relobj<size, big_endian>*,
3092 unsigned int,
5019d64a
HS
3093 const section_size_type,
3094 const section_size_type,
3095 unsigned char*,
3096 Address);
3097
9363c7c3
JY
3098 protected:
3099 void
3100 do_select_as_default_target()
3101 {
3102 gold_assert(aarch64_reloc_property_table == NULL);
3103 aarch64_reloc_property_table = new AArch64_reloc_property_table();
3104 }
3105
3a531937
JY
3106 // Add a new reloc argument, returning the index in the vector.
3107 size_t
3108 add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
3109 unsigned int r_sym)
3110 {
3111 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
3112 return this->tlsdesc_reloc_info_.size() - 1;
3113 }
3114
9363c7c3 3115 virtual Output_data_plt_aarch64<size, big_endian>*
3a531937
JY
3116 do_make_data_plt(Layout* layout,
3117 Output_data_got_aarch64<size, big_endian>* got,
3118 Output_data_space* got_plt,
3119 Output_data_space* got_irelative)
9363c7c3 3120 {
3a531937
JY
3121 return new Output_data_plt_aarch64_standard<size, big_endian>(
3122 layout, got, got_plt, got_irelative);
9363c7c3
JY
3123 }
3124
83a01957
HS
3125
3126 // do_make_elf_object to override the same function in the base class.
3127 Object*
3128 do_make_elf_object(const std::string&, Input_file*, off_t,
3129 const elfcpp::Ehdr<size, big_endian>&);
3130
9363c7c3 3131 Output_data_plt_aarch64<size, big_endian>*
3a531937
JY
3132 make_data_plt(Layout* layout,
3133 Output_data_got_aarch64<size, big_endian>* got,
3134 Output_data_space* got_plt,
3135 Output_data_space* got_irelative)
9363c7c3 3136 {
3a531937 3137 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
9363c7c3
JY
3138 }
3139
83a01957
HS
3140 // We only need to generate stubs, and hence perform relaxation if we are
3141 // not doing relocatable linking.
3142 virtual bool
3143 do_may_relax() const
3144 { return !parameters->options().relocatable(); }
3145
3146 // Relaxation hook. This is where we do stub generation.
3147 virtual bool
3148 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
3149
3150 void
3151 group_sections(Layout* layout,
3152 section_size_type group_size,
3153 bool stubs_always_after_branch,
3154 const Task* task);
3155
3156 void
3157 scan_reloc_for_stub(const The_relocate_info*, unsigned int,
3158 const Sized_symbol<size>*, unsigned int,
3159 const Symbol_value<size>*,
3160 typename elfcpp::Elf_types<size>::Elf_Swxword,
3161 Address Elf_Addr);
3162
3163 // Make an output section.
3164 Output_section*
3165 do_make_output_section(const char* name, elfcpp::Elf_Word type,
3166 elfcpp::Elf_Xword flags)
3167 { return new The_aarch64_output_section(name, type, flags); }
3168
053a4d68
JY
3169 private:
3170 // The class which scans relocations.
3171 class Scan
3172 {
3173 public:
3174 Scan()
3175 : issued_non_pic_error_(false)
3176 { }
3177
053a4d68
JY
3178 inline void
3179 local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
3180 Sized_relobj_file<size, big_endian>* object,
3181 unsigned int data_shndx,
3182 Output_section* output_section,
3183 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
3184 const elfcpp::Sym<size, big_endian>& lsym,
3185 bool is_discarded);
3186
3187 inline void
3188 global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
3189 Sized_relobj_file<size, big_endian>* object,
3190 unsigned int data_shndx,
3191 Output_section* output_section,
3192 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
3193 Symbol* gsym);
3194
3195 inline bool
3196 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
9363c7c3
JY
3197 Target_aarch64<size, big_endian>* ,
3198 Sized_relobj_file<size, big_endian>* ,
3199 unsigned int ,
3200 Output_section* ,
3201 const elfcpp::Rela<size, big_endian>& ,
3202 unsigned int r_type,
3203 const elfcpp::Sym<size, big_endian>&);
053a4d68
JY
3204
3205 inline bool
3206 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
9363c7c3
JY
3207 Target_aarch64<size, big_endian>* ,
3208 Sized_relobj_file<size, big_endian>* ,
3209 unsigned int ,
3210 Output_section* ,
3211 const elfcpp::Rela<size, big_endian>& ,
3212 unsigned int r_type,
3213 Symbol* gsym);
053a4d68
JY
3214
3215 private:
3216 static void
3217 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3218 unsigned int r_type);
3219
3220 static void
3221 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3222 unsigned int r_type, Symbol*);
3223
3224 inline bool
3225 possible_function_pointer_reloc(unsigned int r_type);
3226
3227 void
3228 check_non_pic(Relobj*, unsigned int r_type);
3229
9726c3c1
HS
3230 bool
3231 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
3232 unsigned int r_type);
3233
053a4d68
JY
3234 // Whether we have issued an error about a non-PIC compilation.
3235 bool issued_non_pic_error_;
3236 };
3237
3238 // The class which implements relocation.
3239 class Relocate
3240 {
3241 public:
3242 Relocate()
3a531937 3243 : skip_call_tls_get_addr_(false)
053a4d68
JY
3244 { }
3245
3246 ~Relocate()
3247 { }
3248
3249 // Do a relocation. Return false if the caller should not issue
3250 // any warnings about this relocation.
3251 inline bool
91a65d2f
AM
3252 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3253 Target_aarch64*, Output_section*, size_t, const unsigned char*,
3254 const Sized_symbol<size>*, const Symbol_value<size>*,
053a4d68
JY
3255 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
3256 section_size_type);
3257
8e33481e 3258 private:
83a01957
HS
3259 inline typename AArch64_relocate_functions<size, big_endian>::Status
3260 relocate_tls(const Relocate_info<size, big_endian>*,
8e33481e
HS
3261 Target_aarch64<size, big_endian>*,
3262 size_t,
3263 const elfcpp::Rela<size, big_endian>&,
3264 unsigned int r_type, const Sized_symbol<size>*,
3265 const Symbol_value<size>*,
3266 unsigned char*,
3267 typename elfcpp::Elf_types<size>::Elf_Addr);
3268
83a01957 3269 inline typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 3270 tls_gd_to_le(
83a01957 3271 const Relocate_info<size, big_endian>*,
3a531937
JY
3272 Target_aarch64<size, big_endian>*,
3273 const elfcpp::Rela<size, big_endian>&,
3274 unsigned int,
3275 unsigned char*,
3276 const Symbol_value<size>*);
3277
9726c3c1
HS
3278 inline typename AArch64_relocate_functions<size, big_endian>::Status
3279 tls_ld_to_le(
3280 const Relocate_info<size, big_endian>*,
3281 Target_aarch64<size, big_endian>*,
3282 const elfcpp::Rela<size, big_endian>&,
3283 unsigned int,
3284 unsigned char*,
3285 const Symbol_value<size>*);
3286
83a01957 3287 inline typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 3288 tls_ie_to_le(
83a01957 3289 const Relocate_info<size, big_endian>*,
3a531937
JY
3290 Target_aarch64<size, big_endian>*,
3291 const elfcpp::Rela<size, big_endian>&,
3292 unsigned int,
3293 unsigned char*,
3294 const Symbol_value<size>*);
3295
83a01957 3296 inline typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 3297 tls_desc_gd_to_le(
83a01957 3298 const Relocate_info<size, big_endian>*,
3a531937
JY
3299 Target_aarch64<size, big_endian>*,
3300 const elfcpp::Rela<size, big_endian>&,
3301 unsigned int,
3302 unsigned char*,
3303 const Symbol_value<size>*);
3304
83a01957 3305 inline typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 3306 tls_desc_gd_to_ie(
83a01957 3307 const Relocate_info<size, big_endian>*,
3a531937
JY
3308 Target_aarch64<size, big_endian>*,
3309 const elfcpp::Rela<size, big_endian>&,
3310 unsigned int,
3311 unsigned char*,
3312 const Symbol_value<size>*,
3313 typename elfcpp::Elf_types<size>::Elf_Addr,
3314 typename elfcpp::Elf_types<size>::Elf_Addr);
3315
3316 bool skip_call_tls_get_addr_;
3317
8e33481e 3318 }; // End of class Relocate
053a4d68 3319
053a4d68
JY
3320 // Adjust TLS relocation type based on the options and whether this
3321 // is a local symbol.
3322 static tls::Tls_optimization
3323 optimize_tls_reloc(bool is_final, int r_type);
3324
3325 // Get the GOT section, creating it if necessary.
3326 Output_data_got_aarch64<size, big_endian>*
3327 got_section(Symbol_table*, Layout*);
3328
3329 // Get the GOT PLT section.
3330 Output_data_space*
3331 got_plt_section() const
3332 {
3333 gold_assert(this->got_plt_ != NULL);
3334 return this->got_plt_;
3335 }
3336
3a531937
JY
3337 // Get the GOT section for TLSDESC entries.
3338 Output_data_got<size, big_endian>*
3339 got_tlsdesc_section() const
3340 {
3341 gold_assert(this->got_tlsdesc_ != NULL);
3342 return this->got_tlsdesc_;
3343 }
3344
053a4d68
JY
3345 // Create the PLT section.
3346 void
3347 make_plt_section(Symbol_table* symtab, Layout* layout);
3348
3349 // Create a PLT entry for a global symbol.
3350 void
3351 make_plt_entry(Symbol_table*, Layout*, Symbol*);
3352
3a531937
JY
3353 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
3354 void
3355 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
3356 Sized_relobj_file<size, big_endian>* relobj,
3357 unsigned int local_sym_index);
3358
3359 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3360 void
3361 define_tls_base_symbol(Symbol_table*, Layout*);
3362
3363 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3364 void
3365 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
3366
3367 // Create a GOT entry for the TLS module index.
3368 unsigned int
3369 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
3370 Sized_relobj_file<size, big_endian>* object);
3371
053a4d68
JY
3372 // Get the PLT section.
3373 Output_data_plt_aarch64<size, big_endian>*
3374 plt_section() const
3375 {
3376 gold_assert(this->plt_ != NULL);
3377 return this->plt_;
3378 }
3379
0ef3814f
HS
3380 // Helper method to create erratum stubs for ST_E_843419 and ST_E_835769. For
3381 // ST_E_843419, we need an additional field for adrp offset.
2f0c79aa
HS
3382 void create_erratum_stub(
3383 AArch64_relobj<size, big_endian>* relobj,
3384 unsigned int shndx,
3385 section_size_type erratum_insn_offset,
3386 Address erratum_address,
3387 typename Insn_utilities::Insntype erratum_insn,
0ef3814f
HS
3388 int erratum_type,
3389 unsigned int e843419_adrp_offset=0);
2f0c79aa 3390
5019d64a
HS
3391 // Return whether this is a 3-insn erratum sequence.
3392 bool is_erratum_843419_sequence(
3393 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
3394 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
3395 typename elfcpp::Swap<32,big_endian>::Valtype insn3);
3396
2f0c79aa
HS
3397 // Return whether this is a 835769 sequence.
3398 // (Similarly implemented as in elfnn-aarch64.c.)
3399 bool is_erratum_835769_sequence(
3400 typename elfcpp::Swap<32,big_endian>::Valtype,
3401 typename elfcpp::Swap<32,big_endian>::Valtype);
3402
053a4d68
JY
3403 // Get the dynamic reloc section, creating it if necessary.
3404 Reloc_section*
3405 rela_dyn_section(Layout*);
3406
3a531937
JY
3407 // Get the section to use for TLSDESC relocations.
3408 Reloc_section*
3409 rela_tlsdesc_section(Layout*) const;
3410
3411 // Get the section to use for IRELATIVE relocations.
3412 Reloc_section*
3413 rela_irelative_section(Layout*);
3414
053a4d68
JY
3415 // Add a potential copy relocation.
3416 void
3417 copy_reloc(Symbol_table* symtab, Layout* layout,
3418 Sized_relobj_file<size, big_endian>* object,
3419 unsigned int shndx, Output_section* output_section,
3420 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
3421 {
859d7987 3422 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
053a4d68
JY
3423 this->copy_relocs_.copy_reloc(symtab, layout,
3424 symtab->get_sized_symbol<size>(sym),
3425 object, shndx, output_section,
859d7987
CC
3426 r_type, reloc.get_r_offset(),
3427 reloc.get_r_addend(),
3428 this->rela_dyn_section(layout));
053a4d68
JY
3429 }
3430
3431 // Information about this specific target which we pass to the
3432 // general Target structure.
3433 static const Target::Target_info aarch64_info;
3434
3435 // The types of GOT entries needed for this platform.
3436 // These values are exposed to the ABI in an incremental link.
3437 // Do not renumber existing values without changing the version
3438 // number of the .gnu_incremental_inputs section.
3439 enum Got_type
3440 {
3441 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
3442 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
3443 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
3444 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
3445 };
3446
3a531937
JY
3447 // This type is used as the argument to the target specific
3448 // relocation routines. The only target specific reloc is
3449 // R_AARCh64_TLSDESC against a local symbol.
3450 struct Tlsdesc_info
3451 {
3452 Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
3453 unsigned int a_r_sym)
3454 : object(a_object), r_sym(a_r_sym)
3455 { }
3456
3457 // The object in which the local symbol is defined.
3458 Sized_relobj_file<size, big_endian>* object;
3459 // The local symbol index in the object.
3460 unsigned int r_sym;
3461 };
3462
053a4d68
JY
3463 // The GOT section.
3464 Output_data_got_aarch64<size, big_endian>* got_;
3465 // The PLT section.
3466 Output_data_plt_aarch64<size, big_endian>* plt_;
3467 // The GOT PLT section.
3468 Output_data_space* got_plt_;
3a531937
JY
3469 // The GOT section for IRELATIVE relocations.
3470 Output_data_space* got_irelative_;
3471 // The GOT section for TLSDESC relocations.
3472 Output_data_got<size, big_endian>* got_tlsdesc_;
053a4d68
JY
3473 // The _GLOBAL_OFFSET_TABLE_ symbol.
3474 Symbol* global_offset_table_;
3475 // The dynamic reloc section.
3476 Reloc_section* rela_dyn_;
3a531937
JY
3477 // The section to use for IRELATIVE relocs.
3478 Reloc_section* rela_irelative_;
053a4d68
JY
3479 // Relocs saved to avoid a COPY reloc.
3480 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
3a531937
JY
3481 // Offset of the GOT entry for the TLS module index.
3482 unsigned int got_mod_index_offset_;
3483 // We handle R_AARCH64_TLSDESC against a local symbol as a target
3484 // specific relocation. Here we store the object and local symbol
3485 // index for the relocation.
3486 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
3487 // True if the _TLS_MODULE_BASE_ symbol has been defined.
3488 bool tls_base_symbol_defined_;
83a01957
HS
3489 // List of stub_tables
3490 Stub_table_list stub_tables_;
0bf32ea9
JY
3491 // Actual stub group size
3492 section_size_type stub_group_size_;
83a01957 3493 AArch64_input_section_map aarch64_input_section_map_;
8e33481e 3494}; // End of Target_aarch64
053a4d68 3495
3a531937 3496
053a4d68
JY
3497template<>
3498const Target::Target_info Target_aarch64<64, false>::aarch64_info =
3499{
3500 64, // size
3501 false, // is_big_endian
3502 elfcpp::EM_AARCH64, // machine_code
3503 false, // has_make_symbol
3504 false, // has_resolve
3505 false, // has_code_fill
3506 true, // is_default_stack_executable
9726c3c1 3507 true, // can_icf_inline_merge_sections
053a4d68
JY
3508 '\0', // wrap_char
3509 "/lib/ld.so.1", // program interpreter
3510 0x400000, // default_text_segment_address
3b0357da 3511 0x10000, // abi_pagesize (overridable by -z max-page-size)
053a4d68
JY
3512 0x1000, // common_pagesize (overridable by -z common-page-size)
3513 false, // isolate_execinstr
3514 0, // rosegment_gap
3515 elfcpp::SHN_UNDEF, // small_common_shndx
3516 elfcpp::SHN_UNDEF, // large_common_shndx
3517 0, // small_common_section_flags
3518 0, // large_common_section_flags
3519 NULL, // attributes_section
3520 NULL, // attributes_vendor
8d9743bd
MK
3521 "_start", // entry_symbol_name
3522 32, // hash_entry_size
053a4d68
JY
3523};
3524
3525template<>
3526const Target::Target_info Target_aarch64<32, false>::aarch64_info =
3527{
3528 32, // size
3529 false, // is_big_endian
3530 elfcpp::EM_AARCH64, // machine_code
3531 false, // has_make_symbol
3532 false, // has_resolve
3533 false, // has_code_fill
3534 true, // is_default_stack_executable
3535 false, // can_icf_inline_merge_sections
3536 '\0', // wrap_char
3537 "/lib/ld.so.1", // program interpreter
3538 0x400000, // default_text_segment_address
3b0357da 3539 0x10000, // abi_pagesize (overridable by -z max-page-size)
053a4d68
JY
3540 0x1000, // common_pagesize (overridable by -z common-page-size)
3541 false, // isolate_execinstr
3542 0, // rosegment_gap
3543 elfcpp::SHN_UNDEF, // small_common_shndx
3544 elfcpp::SHN_UNDEF, // large_common_shndx
3545 0, // small_common_section_flags
3546 0, // large_common_section_flags
3547 NULL, // attributes_section
3548 NULL, // attributes_vendor
8d9743bd
MK
3549 "_start", // entry_symbol_name
3550 32, // hash_entry_size
053a4d68
JY
3551};
3552
3553template<>
3554const Target::Target_info Target_aarch64<64, true>::aarch64_info =
3555{
3556 64, // size
3557 true, // is_big_endian
3558 elfcpp::EM_AARCH64, // machine_code
3559 false, // has_make_symbol
3560 false, // has_resolve
3561 false, // has_code_fill
3562 true, // is_default_stack_executable
9726c3c1 3563 true, // can_icf_inline_merge_sections
053a4d68
JY
3564 '\0', // wrap_char
3565 "/lib/ld.so.1", // program interpreter
3566 0x400000, // default_text_segment_address
3b0357da 3567 0x10000, // abi_pagesize (overridable by -z max-page-size)
053a4d68
JY
3568 0x1000, // common_pagesize (overridable by -z common-page-size)
3569 false, // isolate_execinstr
3570 0, // rosegment_gap
3571 elfcpp::SHN_UNDEF, // small_common_shndx
3572 elfcpp::SHN_UNDEF, // large_common_shndx
3573 0, // small_common_section_flags
3574 0, // large_common_section_flags
3575 NULL, // attributes_section
3576 NULL, // attributes_vendor
8d9743bd
MK
3577 "_start", // entry_symbol_name
3578 32, // hash_entry_size
053a4d68
JY
3579};
3580
3581template<>
3582const Target::Target_info Target_aarch64<32, true>::aarch64_info =
3583{
3584 32, // size
3585 true, // is_big_endian
3586 elfcpp::EM_AARCH64, // machine_code
3587 false, // has_make_symbol
3588 false, // has_resolve
3589 false, // has_code_fill
3590 true, // is_default_stack_executable
3591 false, // can_icf_inline_merge_sections
3592 '\0', // wrap_char
3593 "/lib/ld.so.1", // program interpreter
3594 0x400000, // default_text_segment_address
3b0357da 3595 0x10000, // abi_pagesize (overridable by -z max-page-size)
053a4d68
JY
3596 0x1000, // common_pagesize (overridable by -z common-page-size)
3597 false, // isolate_execinstr
3598 0, // rosegment_gap
3599 elfcpp::SHN_UNDEF, // small_common_shndx
3600 elfcpp::SHN_UNDEF, // large_common_shndx
3601 0, // small_common_section_flags
3602 0, // large_common_section_flags
3603 NULL, // attributes_section
3604 NULL, // attributes_vendor
8d9743bd
MK
3605 "_start", // entry_symbol_name
3606 32, // hash_entry_size
053a4d68
JY
3607};
3608
3609// Get the GOT section, creating it if necessary.
3610
3611template<int size, bool big_endian>
3612Output_data_got_aarch64<size, big_endian>*
3613Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
9363c7c3 3614 Layout* layout)
053a4d68
JY
3615{
3616 if (this->got_ == NULL)
3617 {
3618 gold_assert(symtab != NULL && layout != NULL);
3619
3620 // When using -z now, we can treat .got.plt as a relro section.
3621 // Without -z now, it is modified after program startup by lazy
3622 // PLT relocations.
3623 bool is_got_plt_relro = parameters->options().now();
3624 Output_section_order got_order = (is_got_plt_relro
3625 ? ORDER_RELRO
3626 : ORDER_RELRO_LAST);
3627 Output_section_order got_plt_order = (is_got_plt_relro
3628 ? ORDER_RELRO
3629 : ORDER_NON_RELRO_FIRST);
3630
3631 // Layout of .got and .got.plt sections.
3632 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
3633 // ...
3634 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
3635 // .gotplt[1] reserved for ld.so (resolver)
3636 // .gotplt[2] reserved
3637
3638 // Generate .got section.
3639 this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
9363c7c3 3640 layout);
053a4d68 3641 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
9363c7c3
JY
3642 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
3643 this->got_, got_order, true);
053a4d68
JY
3644 // The first word of GOT is reserved for the address of .dynamic.
3645 // We put 0 here now. The value will be replaced later in
3646 // Output_data_got_aarch64::do_write.
3647 this->got_->add_constant(0);
3648
3649 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3650 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
3651 // even if there is a .got.plt section.
3652 this->global_offset_table_ =
9363c7c3
JY
3653 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3654 Symbol_table::PREDEFINED,
3655 this->got_,
3656 0, 0, elfcpp::STT_OBJECT,
3657 elfcpp::STB_LOCAL,
3658 elfcpp::STV_HIDDEN, 0,
3659 false, false);
053a4d68
JY
3660
3661 // Generate .got.plt section.
3662 this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
3663 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
9363c7c3
JY
3664 (elfcpp::SHF_ALLOC
3665 | elfcpp::SHF_WRITE),
3666 this->got_plt_, got_plt_order,
3667 is_got_plt_relro);
053a4d68
JY
3668
3669 // The first three entries are reserved.
9363c7c3
JY
3670 this->got_plt_->set_current_data_size(
3671 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
053a4d68 3672
3a531937
JY
3673 // If there are any IRELATIVE relocations, they get GOT entries
3674 // in .got.plt after the jump slot entries.
3675 this->got_irelative_ = new Output_data_space(size / 8,
3676 "** GOT IRELATIVE PLT");
3677 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3678 (elfcpp::SHF_ALLOC
3679 | elfcpp::SHF_WRITE),
3680 this->got_irelative_,
3681 got_plt_order,
3682 is_got_plt_relro);
3683
3684 // If there are any TLSDESC relocations, they get GOT entries in
3685 // .got.plt after the jump slot and IRELATIVE entries.
3686 this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
3687 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3688 (elfcpp::SHF_ALLOC
3689 | elfcpp::SHF_WRITE),
3690 this->got_tlsdesc_,
3691 got_plt_order,
3692 is_got_plt_relro);
3693
053a4d68 3694 if (!is_got_plt_relro)
9363c7c3
JY
3695 {
3696 // Those bytes can go into the relro segment.
3697 layout->increase_relro(
3698 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3699 }
053a4d68
JY
3700
3701 }
3702 return this->got_;
3703}
3704
3705// Get the dynamic reloc section, creating it if necessary.
3706
3707template<int size, bool big_endian>
3708typename Target_aarch64<size, big_endian>::Reloc_section*
3709Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
3710{
3711 if (this->rela_dyn_ == NULL)
3712 {
3713 gold_assert(layout != NULL);
3714 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3715 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3716 elfcpp::SHF_ALLOC, this->rela_dyn_,
3717 ORDER_DYNAMIC_RELOCS, false);
3718 }
3719 return this->rela_dyn_;
3720}
3721
3a531937
JY
3722// Get the section to use for IRELATIVE relocs, creating it if
3723// necessary. These go in .rela.dyn, but only after all other dynamic
3724// relocations. They need to follow the other dynamic relocations so
3725// that they can refer to global variables initialized by those
3726// relocs.
3727
3728template<int size, bool big_endian>
3729typename Target_aarch64<size, big_endian>::Reloc_section*
3730Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
3731{
3732 if (this->rela_irelative_ == NULL)
3733 {
3734 // Make sure we have already created the dynamic reloc section.
3735 this->rela_dyn_section(layout);
3736 this->rela_irelative_ = new Reloc_section(false);
3737 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3738 elfcpp::SHF_ALLOC, this->rela_irelative_,
3739 ORDER_DYNAMIC_RELOCS, false);
3740 gold_assert(this->rela_dyn_->output_section()
3741 == this->rela_irelative_->output_section());
3742 }
3743 return this->rela_irelative_;
3744}
3745
3746
83a01957
HS
3747// do_make_elf_object to override the same function in the base class. We need
3748// to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
3749// store backend specific information. Hence we need to have our own ELF object
3750// creation.
3751
3752template<int size, bool big_endian>
3753Object*
3754Target_aarch64<size, big_endian>::do_make_elf_object(
3755 const std::string& name,
3756 Input_file* input_file,
3757 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
3758{
3759 int et = ehdr.get_e_type();
3760 // ET_EXEC files are valid input for --just-symbols/-R,
3761 // and we treat them as relocatable objects.
3762 if (et == elfcpp::ET_EXEC && input_file->just_symbols())
3763 return Sized_target<size, big_endian>::do_make_elf_object(
3764 name, input_file, offset, ehdr);
3765 else if (et == elfcpp::ET_REL)
3766 {
3767 AArch64_relobj<size, big_endian>* obj =
3768 new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
3769 obj->setup();
3770 return obj;
3771 }
3772 else if (et == elfcpp::ET_DYN)
3773 {
3774 // Keep base implementation.
3775 Sized_dynobj<size, big_endian>* obj =
3776 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
3777 obj->setup();
3778 return obj;
3779 }
3780 else
3781 {
3782 gold_error(_("%s: unsupported ELF file type %d"),
3783 name.c_str(), et);
3784 return NULL;
3785 }
3786}
3787
3788
3789// Scan a relocation for stub generation.
3790
3791template<int size, bool big_endian>
3792void
3793Target_aarch64<size, big_endian>::scan_reloc_for_stub(
3794 const Relocate_info<size, big_endian>* relinfo,
3795 unsigned int r_type,
3796 const Sized_symbol<size>* gsym,
3797 unsigned int r_sym,
3798 const Symbol_value<size>* psymval,
3799 typename elfcpp::Elf_types<size>::Elf_Swxword addend,
3800 Address address)
3801{
3802 const AArch64_relobj<size, big_endian>* aarch64_relobj =
3803 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3804
3805 Symbol_value<size> symval;
3806 if (gsym != NULL)
3807 {
3808 const AArch64_reloc_property* arp = aarch64_reloc_property_table->
3809 get_reloc_property(r_type);
3810 if (gsym->use_plt_offset(arp->reference_flags()))
3811 {
3812 // This uses a PLT, change the symbol value.
69431bab 3813 symval.set_output_value(this->plt_address_for_global(gsym));
83a01957
HS
3814 psymval = &symval;
3815 }
3816 else if (gsym->is_undefined())
81b6fe3b 3817 {
c092b67b
EC
3818 // There is no need to generate a stub symbol if the original symbol
3819 // is undefined.
81b6fe3b
EC
3820 gold_debug(DEBUG_TARGET,
3821 "stub: not creating a stub for undefined symbol %s in file %s",
3822 gsym->name(), aarch64_relobj->name().c_str());
3823 return;
3824 }
83a01957
HS
3825 }
3826
3827 // Get the symbol value.
3828 typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
3829
3830 // Owing to pipelining, the PC relative branches below actually skip
3831 // two instructions when the branch offset is 0.
3832 Address destination = static_cast<Address>(-1);
3833 switch (r_type)
3834 {
3835 case elfcpp::R_AARCH64_CALL26:
3836 case elfcpp::R_AARCH64_JUMP26:
3837 destination = value + addend;
3838 break;
3839 default:
9726c3c1 3840 gold_unreachable();
83a01957
HS
3841 }
3842
a48d0c12 3843 int stub_type = The_reloc_stub::
83a01957 3844 stub_type_for_reloc(r_type, address, destination);
a48d0c12 3845 if (stub_type == ST_NONE)
5019d64a 3846 return;
83a01957
HS
3847
3848 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
3849 gold_assert(stub_table != NULL);
3850
3851 The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
3852 The_reloc_stub* stub = stub_table->find_reloc_stub(key);
3853 if (stub == NULL)
3854 {
3855 stub = new The_reloc_stub(stub_type);
3856 stub_table->add_reloc_stub(stub, key);
3857 }
3858 stub->set_destination_address(destination);
3859} // End of Target_aarch64::scan_reloc_for_stub
3860
3861
3862// This function scans a relocation section for stub generation.
3863// The template parameter Relocate must be a class type which provides
3864// a single function, relocate(), which implements the machine
3865// specific part of a relocation.
3866
3867// BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
3868// SHT_REL or SHT_RELA.
3869
3870// PRELOCS points to the relocation data. RELOC_COUNT is the number
3871// of relocs. OUTPUT_SECTION is the output section.
3872// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
3873// mapped to output offsets.
3874
3875// VIEW is the section data, VIEW_ADDRESS is its memory address, and
3876// VIEW_SIZE is the size. These refer to the input section, unless
3877// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
3878// the output section.
3879
3880template<int size, bool big_endian>
3881template<int sh_type>
3882void inline
3883Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
3884 const Relocate_info<size, big_endian>* relinfo,
3885 const unsigned char* prelocs,
3886 size_t reloc_count,
3887 Output_section* /*output_section*/,
3888 bool /*needs_special_offset_handling*/,
3889 const unsigned char* /*view*/,
3890 Address view_address,
3891 section_size_type)
3892{
3893 typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
3894
3895 const int reloc_size =
3896 Reloc_types<sh_type,size,big_endian>::reloc_size;
3897 AArch64_relobj<size, big_endian>* object =
3898 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3899 unsigned int local_count = object->local_symbol_count();
3900
3901 gold::Default_comdat_behavior default_comdat_behavior;
3902 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
3903
3904 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
3905 {
3906 Reltype reloc(prelocs);
3907 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
3908 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3909 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3910 if (r_type != elfcpp::R_AARCH64_CALL26
3911 && r_type != elfcpp::R_AARCH64_JUMP26)
3912 continue;
3913
3914 section_offset_type offset =
3915 convert_to_section_size_type(reloc.get_r_offset());
3916
3917 // Get the addend.
3918 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
3919 reloc.get_r_addend();
3920
3921 const Sized_symbol<size>* sym;
3922 Symbol_value<size> symval;
3923 const Symbol_value<size> *psymval;
3924 bool is_defined_in_discarded_section;
3925 unsigned int shndx;
3926 if (r_sym < local_count)
3927 {
3928 sym = NULL;
3929 psymval = object->local_symbol(r_sym);
3930
3931 // If the local symbol belongs to a section we are discarding,
3932 // and that section is a debug section, try to find the
3933 // corresponding kept section and map this symbol to its
3934 // counterpart in the kept section. The symbol must not
3935 // correspond to a section we are folding.
3936 bool is_ordinary;
3937 shndx = psymval->input_shndx(&is_ordinary);
3938 is_defined_in_discarded_section =
3939 (is_ordinary
3940 && shndx != elfcpp::SHN_UNDEF
3941 && !object->is_section_included(shndx)
3942 && !relinfo->symtab->is_section_folded(object, shndx));
3943
3944 // We need to compute the would-be final value of this local
3945 // symbol.
3946 if (!is_defined_in_discarded_section)
3947 {
3948 typedef Sized_relobj_file<size, big_endian> ObjType;
0f125432
CC
3949 if (psymval->is_section_symbol())
3950 symval.set_is_section_symbol();
83a01957
HS
3951 typename ObjType::Compute_final_local_value_status status =
3952 object->compute_final_local_value(r_sym, psymval, &symval,
3953 relinfo->symtab);
3954 if (status == ObjType::CFLV_OK)
3955 {
3956 // Currently we cannot handle a branch to a target in
3957 // a merged section. If this is the case, issue an error
3958 // and also free the merge symbol value.
3959 if (!symval.has_output_value())
3960 {
3961 const std::string& section_name =
3962 object->section_name(shndx);
3963 object->error(_("cannot handle branch to local %u "
3964 "in a merged section %s"),
3965 r_sym, section_name.c_str());
3966 }
3967 psymval = &symval;
3968 }
3969 else
3970 {
3971 // We cannot determine the final value.
3972 continue;
3973 }
3974 }
3975 }
3976 else
3977 {
3978 const Symbol* gsym;
3979 gsym = object->global_symbol(r_sym);
3980 gold_assert(gsym != NULL);
3981 if (gsym->is_forwarder())
3982 gsym = relinfo->symtab->resolve_forwards(gsym);
3983
3984 sym = static_cast<const Sized_symbol<size>*>(gsym);
3985 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
3986 symval.set_output_symtab_index(sym->symtab_index());
3987 else
3988 symval.set_no_output_symtab_entry();
3989
3990 // We need to compute the would-be final value of this global
3991 // symbol.
3992 const Symbol_table* symtab = relinfo->symtab;
3993 const Sized_symbol<size>* sized_symbol =
3994 symtab->get_sized_symbol<size>(gsym);
3995 Symbol_table::Compute_final_value_status status;
3996 typename elfcpp::Elf_types<size>::Elf_Addr value =
3997 symtab->compute_final_value<size>(sized_symbol, &status);
3998
3999 // Skip this if the symbol has not output section.
4000 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
4001 continue;
4002 symval.set_output_value(value);
4003
4004 if (gsym->type() == elfcpp::STT_TLS)
4005 symval.set_is_tls_symbol();
4006 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
4007 symval.set_is_ifunc_symbol();
4008 psymval = &symval;
4009
4010 is_defined_in_discarded_section =
4011 (gsym->is_defined_in_discarded_section()
4012 && gsym->is_undefined());
4013 shndx = 0;
4014 }
4015
4016 Symbol_value<size> symval2;
4017 if (is_defined_in_discarded_section)
4018 {
4019 if (comdat_behavior == CB_UNDETERMINED)
4020 {
4021 std::string name = object->section_name(relinfo->data_shndx);
4022 comdat_behavior = default_comdat_behavior.get(name.c_str());
4023 }
4024 if (comdat_behavior == CB_PRETEND)
4025 {
4026 bool found;
4027 typename elfcpp::Elf_types<size>::Elf_Addr value =
4028 object->map_to_kept_section(shndx, &found);
4029 if (found)
4030 symval2.set_output_value(value + psymval->input_value());
4031 else
4032 symval2.set_output_value(0);
4033 }
4034 else
4035 {
4036 if (comdat_behavior == CB_WARNING)
4037 gold_warning_at_location(relinfo, i, offset,
4038 _("relocation refers to discarded "
4039 "section"));
4040 symval2.set_output_value(0);
4041 }
4042 symval2.set_no_output_symtab_entry();
4043 psymval = &symval2;
4044 }
4045
83a01957
HS
4046 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
4047 addend, view_address + offset);
4048 } // End of iterating relocs in a section
4049} // End of Target_aarch64::scan_reloc_section_for_stubs
4050
4051
4052// Scan an input section for stub generation.
4053
4054template<int size, bool big_endian>
4055void
4056Target_aarch64<size, big_endian>::scan_section_for_stubs(
4057 const Relocate_info<size, big_endian>* relinfo,
4058 unsigned int sh_type,
4059 const unsigned char* prelocs,
4060 size_t reloc_count,
4061 Output_section* output_section,
4062 bool needs_special_offset_handling,
4063 const unsigned char* view,
4064 Address view_address,
4065 section_size_type view_size)
4066{
4067 gold_assert(sh_type == elfcpp::SHT_RELA);
4068 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
4069 relinfo,
4070 prelocs,
4071 reloc_count,
4072 output_section,
4073 needs_special_offset_handling,
4074 view,
4075 view_address,
4076 view_size);
4077}
4078
4079
df2f63a6 4080// Relocate a single reloc stub.
83a01957
HS
4081
4082template<int size, bool big_endian>
4083void Target_aarch64<size, big_endian>::
df2f63a6
HS
4084relocate_reloc_stub(The_reloc_stub* stub,
4085 const The_relocate_info*,
4086 Output_section*,
4087 unsigned char* view,
4088 Address address,
4089 section_size_type)
83a01957
HS
4090{
4091 typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
4092 typedef typename The_reloc_functions::Status The_reloc_functions_status;
4093 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
4094
4095 Insntype* ip = reinterpret_cast<Insntype*>(view);
a48d0c12
HS
4096 int insn_number = stub->insn_num();
4097 const uint32_t* insns = stub->insns();
83a01957
HS
4098 // Check the insns are really those stub insns.
4099 for (int i = 0; i < insn_number; ++i)
4100 {
4101 Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
a48d0c12 4102 gold_assert(((uint32_t)insn == insns[i]));
83a01957
HS
4103 }
4104
4105 Address dest = stub->destination_address();
4106
a48d0c12 4107 switch(stub->type())
83a01957 4108 {
a48d0c12 4109 case ST_ADRP_BRANCH:
83a01957
HS
4110 {
4111 // 1st reloc is ADR_PREL_PG_HI21
4112 The_reloc_functions_status status =
4113 The_reloc_functions::adrp(view, dest, address);
4114 // An error should never arise in the above step. If so, please
4115 // check 'aarch64_valid_for_adrp_p'.
4116 gold_assert(status == The_reloc_functions::STATUS_OKAY);
4117
4118 // 2nd reloc is ADD_ABS_LO12_NC
4119 const AArch64_reloc_property* arp =
4120 aarch64_reloc_property_table->get_reloc_property(
4121 elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
4122 gold_assert(arp != NULL);
4123 status = The_reloc_functions::template
4124 rela_general<32>(view + 4, dest, 0, arp);
4125 // An error should never arise, it is an "_NC" relocation.
4126 gold_assert(status == The_reloc_functions::STATUS_OKAY);
4127 }
4128 break;
4129
a48d0c12 4130 case ST_LONG_BRANCH_ABS:
83a01957
HS
4131 // 1st reloc is R_AARCH64_PREL64, at offset 8
4132 elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
4133 break;
4134
a48d0c12 4135 case ST_LONG_BRANCH_PCREL:
83a01957
HS
4136 {
4137 // "PC" calculation is the 2nd insn in the stub.
4138 uint64_t offset = dest - (address + 4);
4139 // Offset is placed at offset 4 and 5.
4140 elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
4141 }
4142 break;
4143
4144 default:
9726c3c1 4145 gold_unreachable();
83a01957
HS
4146 }
4147}
4148
4149
053a4d68
JY
4150// A class to handle the PLT data.
4151// This is an abstract base class that handles most of the linker details
4152// but does not know the actual contents of PLT entries. The derived
4153// classes below fill in those details.
4154
4155template<int size, bool big_endian>
4156class Output_data_plt_aarch64 : public Output_section_data
4157{
4158 public:
4159 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
4160 Reloc_section;
4161 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4162
4163 Output_data_plt_aarch64(Layout* layout,
9363c7c3 4164 uint64_t addralign,
3a531937
JY
4165 Output_data_got_aarch64<size, big_endian>* got,
4166 Output_data_space* got_plt,
4167 Output_data_space* got_irelative)
9726c3c1 4168 : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
3a531937
JY
4169 got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
4170 count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
053a4d68
JY
4171 { this->init(layout); }
4172
4173 // Initialize the PLT section.
4174 void
4175 init(Layout* layout);
4176
4177 // Add an entry to the PLT.
4178 void
9726c3c1
HS
4179 add_entry(Symbol_table*, Layout*, Symbol* gsym);
4180
4181 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
4182 unsigned int
4183 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
4184 Sized_relobj_file<size, big_endian>* relobj,
4185 unsigned int local_sym_index);
4186
4187 // Add the relocation for a PLT entry.
4188 void
4189 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
4190 unsigned int got_offset);
053a4d68 4191
3a531937
JY
4192 // Add the reserved TLSDESC_PLT entry to the PLT.
4193 void
4194 reserve_tlsdesc_entry(unsigned int got_offset)
4195 { this->tlsdesc_got_offset_ = got_offset; }
4196
4197 // Return true if a TLSDESC_PLT entry has been reserved.
4198 bool
4199 has_tlsdesc_entry() const
4200 { return this->tlsdesc_got_offset_ != -1U; }
4201
4202 // Return the GOT offset for the reserved TLSDESC_PLT entry.
4203 unsigned int
4204 get_tlsdesc_got_offset() const
4205 { return this->tlsdesc_got_offset_; }
4206
4207 // Return the PLT offset of the reserved TLSDESC_PLT entry.
4208 unsigned int
4209 get_tlsdesc_plt_offset() const
4210 {
4211 return (this->first_plt_entry_offset() +
4212 (this->count_ + this->irelative_count_)
4213 * this->get_plt_entry_size());
4214 }
4215
053a4d68
JY
4216 // Return the .rela.plt section data.
4217 Reloc_section*
4218 rela_plt()
4219 { return this->rel_; }
4220
3a531937
JY
4221 // Return where the TLSDESC relocations should go.
4222 Reloc_section*
4223 rela_tlsdesc(Layout*);
4224
4225 // Return where the IRELATIVE relocations should go in the PLT
4226 // relocations.
4227 Reloc_section*
4228 rela_irelative(Symbol_table*, Layout*);
4229
9363c7c3
JY
4230 // Return whether we created a section for IRELATIVE relocations.
4231 bool
4232 has_irelative_section() const
4233 { return this->irelative_rel_ != NULL; }
4234
053a4d68
JY
4235 // Return the number of PLT entries.
4236 unsigned int
4237 entry_count() const
3a531937 4238 { return this->count_ + this->irelative_count_; }
053a4d68
JY
4239
4240 // Return the offset of the first non-reserved PLT entry.
4241 unsigned int
3a531937 4242 first_plt_entry_offset() const
053a4d68
JY
4243 { return this->do_first_plt_entry_offset(); }
4244
4245 // Return the size of a PLT entry.
4246 unsigned int
4247 get_plt_entry_size() const
4248 { return this->do_get_plt_entry_size(); }
4249
3a531937
JY
4250 // Return the reserved tlsdesc entry size.
4251 unsigned int
4252 get_plt_tlsdesc_entry_size() const
4253 { return this->do_get_plt_tlsdesc_entry_size(); }
4254
9363c7c3
JY
4255 // Return the PLT address to use for a global symbol.
4256 uint64_t
4257 address_for_global(const Symbol*);
4258
4259 // Return the PLT address to use for a local symbol.
4260 uint64_t
4261 address_for_local(const Relobj*, unsigned int symndx);
4262
053a4d68
JY
4263 protected:
4264 // Fill in the first PLT entry.
4265 void
4266 fill_first_plt_entry(unsigned char* pov,
4267 Address got_address,
4268 Address plt_address)
4269 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
4270
4271 // Fill in a normal PLT entry.
4272 void
4273 fill_plt_entry(unsigned char* pov,
4274 Address got_address,
4275 Address plt_address,
4276 unsigned int got_offset,
4277 unsigned int plt_offset)
4278 {
4279 this->do_fill_plt_entry(pov, got_address, plt_address,
4280 got_offset, plt_offset);
4281 }
4282
3a531937
JY
4283 // Fill in the reserved TLSDESC PLT entry.
4284 void
4285 fill_tlsdesc_entry(unsigned char* pov,
4286 Address gotplt_address,
4287 Address plt_address,
4288 Address got_base,
4289 unsigned int tlsdesc_got_offset,
4290 unsigned int plt_offset)
4291 {
4292 this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
4293 tlsdesc_got_offset, plt_offset);
4294 }
4295
053a4d68
JY
4296 virtual unsigned int
4297 do_first_plt_entry_offset() const = 0;
4298
4299 virtual unsigned int
4300 do_get_plt_entry_size() const = 0;
4301
3a531937
JY
4302 virtual unsigned int
4303 do_get_plt_tlsdesc_entry_size() const = 0;
4304
053a4d68
JY
4305 virtual void
4306 do_fill_first_plt_entry(unsigned char* pov,
4307 Address got_addr,
4308 Address plt_addr) = 0;
4309
4310 virtual void
4311 do_fill_plt_entry(unsigned char* pov,
4312 Address got_address,
4313 Address plt_address,
4314 unsigned int got_offset,
4315 unsigned int plt_offset) = 0;
4316
3a531937
JY
4317 virtual void
4318 do_fill_tlsdesc_entry(unsigned char* pov,
4319 Address gotplt_address,
4320 Address plt_address,
4321 Address got_base,
4322 unsigned int tlsdesc_got_offset,
4323 unsigned int plt_offset) = 0;
4324
053a4d68
JY
4325 void
4326 do_adjust_output_section(Output_section* os);
4327
4328 // Write to a map file.
4329 void
4330 do_print_to_mapfile(Mapfile* mapfile) const
4331 { mapfile->print_output_data(this, _("** PLT")); }
4332
4333 private:
4334 // Set the final size.
4335 void
4336 set_final_data_size();
4337
4338 // Write out the PLT data.
4339 void
4340 do_write(Output_file*);
4341
4342 // The reloc section.
4343 Reloc_section* rel_;
3a531937
JY
4344
4345 // The TLSDESC relocs, if necessary. These must follow the regular
4346 // PLT relocs.
4347 Reloc_section* tlsdesc_rel_;
4348
9363c7c3
JY
4349 // The IRELATIVE relocs, if necessary. These must follow the
4350 // regular PLT relocations.
4351 Reloc_section* irelative_rel_;
3a531937 4352
053a4d68
JY
4353 // The .got section.
4354 Output_data_got_aarch64<size, big_endian>* got_;
3a531937 4355
053a4d68
JY
4356 // The .got.plt section.
4357 Output_data_space* got_plt_;
3a531937
JY
4358
4359 // The part of the .got.plt section used for IRELATIVE relocs.
4360 Output_data_space* got_irelative_;
4361
053a4d68
JY
4362 // The number of PLT entries.
4363 unsigned int count_;
3a531937 4364
fa89cc82 4365 // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
3a531937
JY
4366 // follow the regular PLT entries.
4367 unsigned int irelative_count_;
4368
4369 // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
4370 // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
4371 // indicates an offset is not allocated.
4372 unsigned int tlsdesc_got_offset_;
053a4d68
JY
4373};
4374
4375// Initialize the PLT section.
4376
4377template<int size, bool big_endian>
4378void
4379Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
4380{
4381 this->rel_ = new Reloc_section(false);
4382 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4383 elfcpp::SHF_ALLOC, this->rel_,
4384 ORDER_DYNAMIC_PLT_RELOCS, false);
4385}
4386
4387template<int size, bool big_endian>
4388void
4389Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
4390 Output_section* os)
4391{
4392 os->set_entsize(this->get_plt_entry_size());
4393}
4394
4395// Add an entry to the PLT.
4396
4397template<int size, bool big_endian>
4398void
9726c3c1
HS
4399Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
4400 Layout* layout, Symbol* gsym)
053a4d68
JY
4401{
4402 gold_assert(!gsym->has_plt_offset());
9363c7c3 4403
9726c3c1
HS
4404 unsigned int* pcount;
4405 unsigned int plt_reserved;
4406 Output_section_data_build* got;
9363c7c3 4407
9726c3c1
HS
4408 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4409 && gsym->can_use_relative_reloc(false))
4410 {
4411 pcount = &this->irelative_count_;
4412 plt_reserved = 0;
4413 got = this->got_irelative_;
4414 }
4415 else
4416 {
4417 pcount = &this->count_;
4418 plt_reserved = this->first_plt_entry_offset();
4419 got = this->got_plt_;
4420 }
9363c7c3 4421
9726c3c1
HS
4422 gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
4423 + plt_reserved);
4424
4425 ++*pcount;
4426
4427 section_offset_type got_offset = got->current_data_size();
9363c7c3
JY
4428
4429 // Every PLT entry needs a GOT entry which points back to the PLT
4430 // entry (this will be changed by the dynamic linker, normally
4431 // lazily when the function is called).
9726c3c1 4432 got->set_current_data_size(got_offset + size / 8);
9363c7c3
JY
4433
4434 // Every PLT entry needs a reloc.
9726c3c1 4435 this->add_relocation(symtab, layout, gsym, got_offset);
9363c7c3
JY
4436
4437 // Note that we don't need to save the symbol. The contents of the
4438 // PLT are independent of which symbols are used. The symbols only
4439 // appear in the relocations.
4440}
4441
9726c3c1
HS
4442// Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
4443// the PLT offset.
4444
4445template<int size, bool big_endian>
4446unsigned int
4447Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
4448 Symbol_table* symtab,
4449 Layout* layout,
4450 Sized_relobj_file<size, big_endian>* relobj,
4451 unsigned int local_sym_index)
4452{
4453 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
4454 ++this->irelative_count_;
4455
4456 section_offset_type got_offset = this->got_irelative_->current_data_size();
4457
4458 // Every PLT entry needs a GOT entry which points back to the PLT
4459 // entry.
4460 this->got_irelative_->set_current_data_size(got_offset + size / 8);
4461
4462 // Every PLT entry needs a reloc.
4463 Reloc_section* rela = this->rela_irelative(symtab, layout);
4464 rela->add_symbolless_local_addend(relobj, local_sym_index,
4465 elfcpp::R_AARCH64_IRELATIVE,
4466 this->got_irelative_, got_offset, 0);
4467
4468 return plt_offset;
4469}
4470
4471// Add the relocation for a PLT entry.
4472
4473template<int size, bool big_endian>
4474void
4475Output_data_plt_aarch64<size, big_endian>::add_relocation(
4476 Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
4477{
4478 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4479 && gsym->can_use_relative_reloc(false))
4480 {
4481 Reloc_section* rela = this->rela_irelative(symtab, layout);
4482 rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
4483 this->got_irelative_, got_offset, 0);
4484 }
4485 else
4486 {
4487 gsym->set_needs_dynsym_entry();
4488 this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
4489 got_offset, 0);
4490 }
4491}
4492
3a531937
JY
4493// Return where the TLSDESC relocations should go, creating it if
4494// necessary. These follow the JUMP_SLOT relocations.
4495
4496template<int size, bool big_endian>
4497typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4498Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
4499{
4500 if (this->tlsdesc_rel_ == NULL)
4501 {
4502 this->tlsdesc_rel_ = new Reloc_section(false);
4503 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4504 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
4505 ORDER_DYNAMIC_PLT_RELOCS, false);
4506 gold_assert(this->tlsdesc_rel_->output_section()
4507 == this->rel_->output_section());
4508 }
4509 return this->tlsdesc_rel_;
4510}
4511
4512// Return where the IRELATIVE relocations should go in the PLT. These
4513// follow the JUMP_SLOT and the TLSDESC relocations.
4514
4515template<int size, bool big_endian>
4516typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4517Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
4518 Layout* layout)
4519{
4520 if (this->irelative_rel_ == NULL)
4521 {
4522 // Make sure we have a place for the TLSDESC relocations, in
4523 // case we see any later on.
4524 this->rela_tlsdesc(layout);
4525 this->irelative_rel_ = new Reloc_section(false);
4526 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4527 elfcpp::SHF_ALLOC, this->irelative_rel_,
4528 ORDER_DYNAMIC_PLT_RELOCS, false);
4529 gold_assert(this->irelative_rel_->output_section()
4530 == this->rel_->output_section());
4531
4532 if (parameters->doing_static_link())
4533 {
4534 // A statically linked executable will only have a .rela.plt
4535 // section to hold R_AARCH64_IRELATIVE relocs for
4536 // STT_GNU_IFUNC symbols. The library will use these
4537 // symbols to locate the IRELATIVE relocs at program startup
4538 // time.
4539 symtab->define_in_output_data("__rela_iplt_start", NULL,
4540 Symbol_table::PREDEFINED,
4541 this->irelative_rel_, 0, 0,
4542 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4543 elfcpp::STV_HIDDEN, 0, false, true);
4544 symtab->define_in_output_data("__rela_iplt_end", NULL,
4545 Symbol_table::PREDEFINED,
4546 this->irelative_rel_, 0, 0,
4547 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4548 elfcpp::STV_HIDDEN, 0, true, true);
4549 }
4550 }
4551 return this->irelative_rel_;
4552}
4553
9363c7c3
JY
4554// Return the PLT address to use for a global symbol.
4555
4556template<int size, bool big_endian>
4557uint64_t
4558Output_data_plt_aarch64<size, big_endian>::address_for_global(
4559 const Symbol* gsym)
4560{
4561 uint64_t offset = 0;
4562 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4563 && gsym->can_use_relative_reloc(false))
4564 offset = (this->first_plt_entry_offset() +
4565 this->count_ * this->get_plt_entry_size());
4566 return this->address() + offset + gsym->plt_offset();
4567}
4568
4569// Return the PLT address to use for a local symbol. These are always
4570// IRELATIVE relocs.
4571
4572template<int size, bool big_endian>
4573uint64_t
4574Output_data_plt_aarch64<size, big_endian>::address_for_local(
4575 const Relobj* object,
4576 unsigned int r_sym)
4577{
4578 return (this->address()
4579 + this->first_plt_entry_offset()
4580 + this->count_ * this->get_plt_entry_size()
4581 + object->local_plt_offset(r_sym));
053a4d68
JY
4582}
4583
4584// Set the final size.
9363c7c3 4585
053a4d68
JY
4586template<int size, bool big_endian>
4587void
4588Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
4589{
3a531937
JY
4590 unsigned int count = this->count_ + this->irelative_count_;
4591 unsigned int extra_size = 0;
4592 if (this->has_tlsdesc_entry())
4593 extra_size += this->get_plt_tlsdesc_entry_size();
053a4d68 4594 this->set_data_size(this->first_plt_entry_offset()
3a531937
JY
4595 + count * this->get_plt_entry_size()
4596 + extra_size);
053a4d68
JY
4597}
4598
4599template<int size, bool big_endian>
4600class Output_data_plt_aarch64_standard :
4601 public Output_data_plt_aarch64<size, big_endian>
4602{
4603 public:
4604 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3a531937
JY
4605 Output_data_plt_aarch64_standard(
4606 Layout* layout,
4607 Output_data_got_aarch64<size, big_endian>* got,
4608 Output_data_space* got_plt,
4609 Output_data_space* got_irelative)
053a4d68 4610 : Output_data_plt_aarch64<size, big_endian>(layout,
9363c7c3 4611 size == 32 ? 4 : 8,
3a531937
JY
4612 got, got_plt,
4613 got_irelative)
053a4d68
JY
4614 { }
4615
4616 protected:
4617 // Return the offset of the first non-reserved PLT entry.
4618 virtual unsigned int
4619 do_first_plt_entry_offset() const
4620 { return this->first_plt_entry_size; }
4621
4622 // Return the size of a PLT entry
4623 virtual unsigned int
4624 do_get_plt_entry_size() const
4625 { return this->plt_entry_size; }
4626
3a531937
JY
4627 // Return the size of a tlsdesc entry
4628 virtual unsigned int
4629 do_get_plt_tlsdesc_entry_size() const
4630 { return this->plt_tlsdesc_entry_size; }
4631
053a4d68
JY
4632 virtual void
4633 do_fill_first_plt_entry(unsigned char* pov,
9363c7c3
JY
4634 Address got_address,
4635 Address plt_address);
053a4d68
JY
4636
4637 virtual void
4638 do_fill_plt_entry(unsigned char* pov,
9363c7c3
JY
4639 Address got_address,
4640 Address plt_address,
4641 unsigned int got_offset,
4642 unsigned int plt_offset);
053a4d68 4643
3a531937
JY
4644 virtual void
4645 do_fill_tlsdesc_entry(unsigned char* pov,
4646 Address gotplt_address,
4647 Address plt_address,
4648 Address got_base,
4649 unsigned int tlsdesc_got_offset,
4650 unsigned int plt_offset);
4651
053a4d68
JY
4652 private:
4653 // The size of the first plt entry size.
4654 static const int first_plt_entry_size = 32;
4655 // The size of the plt entry size.
4656 static const int plt_entry_size = 16;
3a531937
JY
4657 // The size of the plt tlsdesc entry size.
4658 static const int plt_tlsdesc_entry_size = 32;
053a4d68
JY
4659 // Template for the first PLT entry.
4660 static const uint32_t first_plt_entry[first_plt_entry_size / 4];
4661 // Template for subsequent PLT entries.
4662 static const uint32_t plt_entry[plt_entry_size / 4];
3a531937
JY
4663 // The reserved TLSDESC entry in the PLT for an executable.
4664 static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
053a4d68
JY
4665};
4666
4667// The first entry in the PLT for an executable.
4668
4669template<>
4670const uint32_t
4671Output_data_plt_aarch64_standard<32, false>::
4672 first_plt_entry[first_plt_entry_size / 4] =
4673{
4674 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4675 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4676 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4677 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4678 0xd61f0220, /* br x17 */
4679 0xd503201f, /* nop */
4680 0xd503201f, /* nop */
4681 0xd503201f, /* nop */
4682};
4683
83a01957 4684
053a4d68
JY
4685template<>
4686const uint32_t
4687Output_data_plt_aarch64_standard<32, true>::
4688 first_plt_entry[first_plt_entry_size / 4] =
4689{
4690 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4691 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4692 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4693 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4694 0xd61f0220, /* br x17 */
4695 0xd503201f, /* nop */
4696 0xd503201f, /* nop */
4697 0xd503201f, /* nop */
4698};
4699
83a01957 4700
053a4d68
JY
4701template<>
4702const uint32_t
4703Output_data_plt_aarch64_standard<64, false>::
4704 first_plt_entry[first_plt_entry_size / 4] =
4705{
4706 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4707 0x90000010, /* adrp x16, PLT_GOT+16 */
4708 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4709 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4710 0xd61f0220, /* br x17 */
4711 0xd503201f, /* nop */
4712 0xd503201f, /* nop */
4713 0xd503201f, /* nop */
4714};
4715
83a01957 4716
053a4d68
JY
4717template<>
4718const uint32_t
4719Output_data_plt_aarch64_standard<64, true>::
4720 first_plt_entry[first_plt_entry_size / 4] =
4721{
4722 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4723 0x90000010, /* adrp x16, PLT_GOT+16 */
4724 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4725 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4726 0xd61f0220, /* br x17 */
4727 0xd503201f, /* nop */
4728 0xd503201f, /* nop */
4729 0xd503201f, /* nop */
4730};
4731
83a01957 4732
053a4d68
JY
4733template<>
4734const uint32_t
4735Output_data_plt_aarch64_standard<32, false>::
4736 plt_entry[plt_entry_size / 4] =
4737{
4738 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4739 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4740 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4741 0xd61f0220, /* br x17. */
4742};
4743
83a01957 4744
053a4d68
JY
4745template<>
4746const uint32_t
4747Output_data_plt_aarch64_standard<32, true>::
4748 plt_entry[plt_entry_size / 4] =
4749{
4750 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4751 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4752 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4753 0xd61f0220, /* br x17. */
4754};
4755
83a01957 4756
053a4d68
JY
4757template<>
4758const uint32_t
4759Output_data_plt_aarch64_standard<64, false>::
4760 plt_entry[plt_entry_size / 4] =
4761{
4762 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4763 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4764 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4765 0xd61f0220, /* br x17. */
4766};
4767
83a01957 4768
053a4d68
JY
4769template<>
4770const uint32_t
4771Output_data_plt_aarch64_standard<64, true>::
4772 plt_entry[plt_entry_size / 4] =
4773{
4774 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4775 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4776 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4777 0xd61f0220, /* br x17. */
4778};
4779
83a01957 4780
053a4d68
JY
4781template<int size, bool big_endian>
4782void
4783Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
4784 unsigned char* pov,
9363c7c3
JY
4785 Address got_address,
4786 Address plt_address)
053a4d68
JY
4787{
4788 // PLT0 of the small PLT looks like this in ELF64 -
4789 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
4790 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
4791 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
4792 // symbol resolver
4793 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
4794 // GOTPLT entry for this.
4795 // br x17
4796 // PLT0 will be slightly different in ELF32 due to different got entry
4797 // size.
4798 memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
9363c7c3
JY
4799 Address gotplt_2nd_ent = got_address + (size / 8) * 2;
4800
4801 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
4802 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
4803 // FIXME: This only works for 64bit
4804 AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
4805 gotplt_2nd_ent, plt_address + 4);
4806
4807 // Fill in R_AARCH64_LDST8_LO12
4808 elfcpp::Swap<32, big_endian>::writeval(
4809 pov + 8,
4810 ((this->first_plt_entry[2] & 0xffc003ff)
4811 | ((gotplt_2nd_ent & 0xff8) << 7)));
4812
4813 // Fill in R_AARCH64_ADD_ABS_LO12
4814 elfcpp::Swap<32, big_endian>::writeval(
4815 pov + 12,
4816 ((this->first_plt_entry[3] & 0xffc003ff)
4817 | ((gotplt_2nd_ent & 0xfff) << 10)));
053a4d68
JY
4818}
4819
83a01957 4820
053a4d68 4821// Subsequent entries in the PLT for an executable.
9363c7c3 4822// FIXME: This only works for 64bit
053a4d68
JY
4823
4824template<int size, bool big_endian>
4825void
4826Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
4827 unsigned char* pov,
9363c7c3
JY
4828 Address got_address,
4829 Address plt_address,
4830 unsigned int got_offset,
4831 unsigned int plt_offset)
053a4d68
JY
4832{
4833 memcpy(pov, this->plt_entry, this->plt_entry_size);
9363c7c3
JY
4834
4835 Address gotplt_entry_address = got_address + got_offset;
4836 Address plt_entry_address = plt_address + plt_offset;
4837
4838 // Fill in R_AARCH64_PCREL_ADR_HI21
4839 AArch64_relocate_functions<size, big_endian>::adrp(
4840 pov,
4841 gotplt_entry_address,
4842 plt_entry_address);
4843
4844 // Fill in R_AARCH64_LDST64_ABS_LO12
4845 elfcpp::Swap<32, big_endian>::writeval(
4846 pov + 4,
4847 ((this->plt_entry[1] & 0xffc003ff)
4848 | ((gotplt_entry_address & 0xff8) << 7)));
4849
4850 // Fill in R_AARCH64_ADD_ABS_LO12
4851 elfcpp::Swap<32, big_endian>::writeval(
4852 pov + 8,
4853 ((this->plt_entry[2] & 0xffc003ff)
4854 | ((gotplt_entry_address & 0xfff) <<10)));
4855
053a4d68
JY
4856}
4857
3a531937
JY
4858
4859template<>
4860const uint32_t
4861Output_data_plt_aarch64_standard<32, false>::
4862 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4863{
4864 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4865 0x90000002, /* adrp x2, 0 */
4866 0x90000003, /* adrp x3, 0 */
4867 0xb9400042, /* ldr w2, [w2, #0] */
4868 0x11000063, /* add w3, w3, 0 */
4869 0xd61f0040, /* br x2 */
4870 0xd503201f, /* nop */
4871 0xd503201f, /* nop */
4872};
4873
4874template<>
4875const uint32_t
4876Output_data_plt_aarch64_standard<32, true>::
4877 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4878{
4879 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4880 0x90000002, /* adrp x2, 0 */
4881 0x90000003, /* adrp x3, 0 */
4882 0xb9400042, /* ldr w2, [w2, #0] */
4883 0x11000063, /* add w3, w3, 0 */
4884 0xd61f0040, /* br x2 */
4885 0xd503201f, /* nop */
4886 0xd503201f, /* nop */
4887};
4888
4889template<>
4890const uint32_t
4891Output_data_plt_aarch64_standard<64, false>::
4892 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4893{
4894 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4895 0x90000002, /* adrp x2, 0 */
4896 0x90000003, /* adrp x3, 0 */
4897 0xf9400042, /* ldr x2, [x2, #0] */
4898 0x91000063, /* add x3, x3, 0 */
4899 0xd61f0040, /* br x2 */
4900 0xd503201f, /* nop */
4901 0xd503201f, /* nop */
4902};
4903
4904template<>
4905const uint32_t
4906Output_data_plt_aarch64_standard<64, true>::
4907 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4908{
4909 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4910 0x90000002, /* adrp x2, 0 */
4911 0x90000003, /* adrp x3, 0 */
4912 0xf9400042, /* ldr x2, [x2, #0] */
4913 0x91000063, /* add x3, x3, 0 */
4914 0xd61f0040, /* br x2 */
4915 0xd503201f, /* nop */
4916 0xd503201f, /* nop */
4917};
4918
4919template<int size, bool big_endian>
4920void
4921Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
4922 unsigned char* pov,
4923 Address gotplt_address,
4924 Address plt_address,
4925 Address got_base,
4926 unsigned int tlsdesc_got_offset,
4927 unsigned int plt_offset)
4928{
4929 memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
4930
4931 // move DT_TLSDESC_GOT address into x2
4932 // move .got.plt address into x3
4933 Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
4934 Address plt_entry_address = plt_address + plt_offset;
4935
4936 // R_AARCH64_ADR_PREL_PG_HI21
4937 AArch64_relocate_functions<size, big_endian>::adrp(
4938 pov + 4,
4939 tlsdesc_got_entry,
4940 plt_entry_address + 4);
4941
4942 // R_AARCH64_ADR_PREL_PG_HI21
4943 AArch64_relocate_functions<size, big_endian>::adrp(
4944 pov + 8,
4945 gotplt_address,
4946 plt_entry_address + 8);
4947
4948 // R_AARCH64_LDST64_ABS_LO12
4949 elfcpp::Swap<32, big_endian>::writeval(
4950 pov + 12,
4951 ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
4952 | ((tlsdesc_got_entry & 0xff8) << 7)));
4953
4954 // R_AARCH64_ADD_ABS_LO12
4955 elfcpp::Swap<32, big_endian>::writeval(
4956 pov + 16,
4957 ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
4958 | ((gotplt_address & 0xfff) << 10)));
4959}
4960
053a4d68
JY
4961// Write out the PLT. This uses the hand-coded instructions above,
4962// and adjusts them as needed. This is specified by the AMD64 ABI.
4963
4964template<int size, bool big_endian>
4965void
4966Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
4967{
4968 const off_t offset = this->offset();
4969 const section_size_type oview_size =
4970 convert_to_section_size_type(this->data_size());
4971 unsigned char* const oview = of->get_output_view(offset, oview_size);
4972
4973 const off_t got_file_offset = this->got_plt_->offset();
9726c3c1
HS
4974 gold_assert(got_file_offset + this->got_plt_->data_size()
4975 == this->got_irelative_->offset());
4976
053a4d68 4977 const section_size_type got_size =
9726c3c1
HS
4978 convert_to_section_size_type(this->got_plt_->data_size()
4979 + this->got_irelative_->data_size());
053a4d68
JY
4980 unsigned char* const got_view = of->get_output_view(got_file_offset,
4981 got_size);
4982
4983 unsigned char* pov = oview;
4984
4985 // The base address of the .plt section.
4986 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
053a4d68 4987 // The base address of the PLT portion of the .got section.
3a531937
JY
4988 typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
4989 = this->got_plt_->address();
053a4d68 4990
3a531937 4991 this->fill_first_plt_entry(pov, gotplt_address, plt_address);
053a4d68
JY
4992 pov += this->first_plt_entry_offset();
4993
4994 // The first three entries in .got.plt are reserved.
4995 unsigned char* got_pov = got_view;
4996 memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
4997 got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4998
4999 unsigned int plt_offset = this->first_plt_entry_offset();
5000 unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
3a531937 5001 const unsigned int count = this->count_ + this->irelative_count_;
053a4d68
JY
5002 for (unsigned int plt_index = 0;
5003 plt_index < count;
5004 ++plt_index,
5005 pov += this->get_plt_entry_size(),
5006 got_pov += size / 8,
5007 plt_offset += this->get_plt_entry_size(),
5008 got_offset += size / 8)
5009 {
5010 // Set and adjust the PLT entry itself.
3a531937 5011 this->fill_plt_entry(pov, gotplt_address, plt_address,
9363c7c3 5012 got_offset, plt_offset);
053a4d68 5013
9363c7c3
JY
5014 // Set the entry in the GOT, which points to plt0.
5015 elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
053a4d68
JY
5016 }
5017
3a531937
JY
5018 if (this->has_tlsdesc_entry())
5019 {
5020 // Set and adjust the reserved TLSDESC PLT entry.
5021 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
5022 // The base address of the .base section.
5023 typename elfcpp::Elf_types<size>::Elf_Addr got_base =
5024 this->got_->address();
5025 this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
5026 tlsdesc_got_offset, plt_offset);
5027 pov += this->get_plt_tlsdesc_entry_size();
5028 }
5029
053a4d68
JY
5030 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
5031 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
5032
5033 of->write_output_view(offset, oview_size, oview);
5034 of->write_output_view(got_file_offset, got_size, got_view);
5035}
5036
9363c7c3
JY
5037// Telling how to update the immediate field of an instruction.
5038struct AArch64_howto
5039{
5040 // The immediate field mask.
5041 elfcpp::Elf_Xword dst_mask;
5042
5043 // The offset to apply relocation immediate
5044 int doffset;
5045
5046 // The second part offset, if the immediate field has two parts.
5047 // -1 if the immediate field has only one part.
5048 int doffset2;
5049};
5050
5051static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
5052{
5053 {0, -1, -1}, // DATA
5054 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
5055 {0xffffe0, 5, -1}, // LD [23:5]-imm19
5056 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
5057 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
5058 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
5059 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
5060 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
5061 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
5062 {0x3ffffff, 0, -1}, // B [25:0]-imm26
5063 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
5064};
5065
5066// AArch64 relocate function class
5067
5068template<int size, bool big_endian>
5069class AArch64_relocate_functions
5070{
5071 public:
5072 typedef enum
5073 {
5074 STATUS_OKAY, // No error during relocation.
5075 STATUS_OVERFLOW, // Relocation overflow.
5076 STATUS_BAD_RELOC, // Relocation cannot be applied.
5077 } Status;
5078
9363c7c3
JY
5079 typedef AArch64_relocate_functions<size, big_endian> This;
5080 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
83a01957
HS
5081 typedef Relocate_info<size, big_endian> The_relocate_info;
5082 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
5083 typedef Reloc_stub<size, big_endian> The_reloc_stub;
83a01957
HS
5084 typedef Stub_table<size, big_endian> The_stub_table;
5085 typedef elfcpp::Rela<size, big_endian> The_rela;
9726c3c1 5086 typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
9363c7c3
JY
5087
5088 // Return the page address of the address.
5089 // Page(address) = address & ~0xFFF
5090
9726c3c1 5091 static inline AArch64_valtype
9363c7c3
JY
5092 Page(Address address)
5093 {
5094 return (address & (~static_cast<Address>(0xFFF)));
5095 }
5096
83a01957 5097 private:
9363c7c3
JY
5098 // Update instruction (pointed by view) with selected bits (immed).
5099 // val = (val & ~dst_mask) | (immed << doffset)
5100
5101 template<int valsize>
5102 static inline void
5103 update_view(unsigned char* view,
9726c3c1 5104 AArch64_valtype immed,
9363c7c3
JY
5105 elfcpp::Elf_Xword doffset,
5106 elfcpp::Elf_Xword dst_mask)
5107 {
5108 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
5109 Valtype* wv = reinterpret_cast<Valtype*>(view);
5110 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
5111
5112 // Clear immediate fields.
5113 val &= ~dst_mask;
5114 elfcpp::Swap<valsize, big_endian>::writeval(wv,
5115 static_cast<Valtype>(val | (immed << doffset)));
5116 }
5117
5118 // Update two parts of an instruction (pointed by view) with selected
5119 // bits (immed1 and immed2).
5120 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
5121
5122 template<int valsize>
5123 static inline void
5124 update_view_two_parts(
5125 unsigned char* view,
9726c3c1
HS
5126 AArch64_valtype immed1,
5127 AArch64_valtype immed2,
9363c7c3
JY
5128 elfcpp::Elf_Xword doffset1,
5129 elfcpp::Elf_Xword doffset2,
5130 elfcpp::Elf_Xword dst_mask)
5131 {
5132 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
5133 Valtype* wv = reinterpret_cast<Valtype*>(view);
5134 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
5135 val &= ~dst_mask;
5136 elfcpp::Swap<valsize, big_endian>::writeval(wv,
5137 static_cast<Valtype>(val | (immed1 << doffset1) |
5138 (immed2 << doffset2)));
5139 }
5140
9726c3c1 5141 // Update adr or adrp instruction with immed.
9363c7c3
JY
5142 // In adr and adrp: [30:29] immlo [23:5] immhi
5143
5144 static inline void
9726c3c1 5145 update_adr(unsigned char* view, AArch64_valtype immed)
9363c7c3
JY
5146 {
5147 elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
9363c7c3
JY
5148 This::template update_view_two_parts<32>(
5149 view,
5150 immed & 0x3,
5151 (immed & 0x1ffffc) >> 2,
5152 29,
5153 5,
5154 dst_mask);
5155 }
5156
3a531937
JY
5157 // Update movz/movn instruction with bits immed.
5158 // Set instruction to movz if is_movz is true, otherwise set instruction
5159 // to movn.
9726c3c1 5160
3a531937
JY
5161 static inline void
5162 update_movnz(unsigned char* view,
9726c3c1 5163 AArch64_valtype immed,
3a531937
JY
5164 bool is_movz)
5165 {
5166 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5167 Valtype* wv = reinterpret_cast<Valtype*>(view);
5168 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
5169
5170 const elfcpp::Elf_Xword doffset =
5171 aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
5172 const elfcpp::Elf_Xword dst_mask =
5173 aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
5174
5175 // Clear immediate fields and opc code.
9726c3c1 5176 val &= ~(dst_mask | (0x3 << 29));
3a531937
JY
5177
5178 // Set instruction to movz or movn.
5179 // movz: [30:29] is 10 movn: [30:29] is 00
5180 if (is_movz)
9726c3c1 5181 val |= (0x2 << 29);
3a531937
JY
5182
5183 elfcpp::Swap<32, big_endian>::writeval(wv,
5184 static_cast<Valtype>(val | (immed << doffset)));
5185 }
5186
5c28a503
HS
5187 public:
5188
9726c3c1
HS
5189 // Update selected bits in text.
5190
5191 template<int valsize>
5192 static inline typename This::Status
5193 reloc_common(unsigned char* view, Address x,
5194 const AArch64_reloc_property* reloc_property)
5195 {
5196 // Select bits from X.
5197 Address immed = reloc_property->select_x_value(x);
5198
5199 // Update view.
5200 const AArch64_reloc_property::Reloc_inst inst =
5201 reloc_property->reloc_inst();
5202 // If it is a data relocation or instruction has 2 parts of immediate
5203 // fields, you should not call pcrela_general.
5204 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
5205 aarch64_howto[inst].doffset != -1);
5206 This::template update_view<valsize>(view, immed,
5207 aarch64_howto[inst].doffset,
5208 aarch64_howto[inst].dst_mask);
5209
5210 // Do check overflow or alignment if needed.
5211 return (reloc_property->checkup_x_value(x)
5212 ? This::STATUS_OKAY
5213 : This::STATUS_OVERFLOW);
5214 }
5215
a48d0c12
HS
5216 // Construct a B insn. Note, although we group it here with other relocation
5217 // operation, there is actually no 'relocation' involved here.
5218 static inline void
5219 construct_b(unsigned char* view, unsigned int branch_offset)
5220 {
5221 update_view_two_parts<32>(view, 0x05, (branch_offset >> 2),
5222 26, 0, 0xffffffff);
5223 }
5224
9363c7c3
JY
5225 // Do a simple rela relocation at unaligned addresses.
5226
5227 template<int valsize>
5228 static inline typename This::Status
5229 rela_ua(unsigned char* view,
5230 const Sized_relobj_file<size, big_endian>* object,
5231 const Symbol_value<size>* psymval,
9726c3c1 5232 AArch64_valtype addend,
9363c7c3
JY
5233 const AArch64_reloc_property* reloc_property)
5234 {
5235 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
5236 Valtype;
5237 typename elfcpp::Elf_types<size>::Elf_Addr x =
5238 psymval->value(object, addend);
5239 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
5240 static_cast<Valtype>(x));
5241 return (reloc_property->checkup_x_value(x)
5242 ? This::STATUS_OKAY
5243 : This::STATUS_OVERFLOW);
5244 }
5245
5246 // Do a simple pc-relative relocation at unaligned addresses.
5247
5248 template<int valsize>
5249 static inline typename This::Status
5250 pcrela_ua(unsigned char* view,
5251 const Sized_relobj_file<size, big_endian>* object,
5252 const Symbol_value<size>* psymval,
9726c3c1 5253 AArch64_valtype addend,
9363c7c3
JY
5254 Address address,
5255 const AArch64_reloc_property* reloc_property)
5256 {
5257 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
5258 Valtype;
3a531937 5259 Address x = psymval->value(object, addend) - address;
9363c7c3
JY
5260 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
5261 static_cast<Valtype>(x));
5262 return (reloc_property->checkup_x_value(x)
5263 ? This::STATUS_OKAY
5264 : This::STATUS_OVERFLOW);
5265 }
5266
5267 // Do a simple rela relocation at aligned addresses.
5268
5269 template<int valsize>
5270 static inline typename This::Status
5271 rela(
5272 unsigned char* view,
5273 const Sized_relobj_file<size, big_endian>* object,
5274 const Symbol_value<size>* psymval,
9726c3c1 5275 AArch64_valtype addend,
9363c7c3
JY
5276 const AArch64_reloc_property* reloc_property)
5277 {
9726c3c1 5278 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
9363c7c3 5279 Valtype* wv = reinterpret_cast<Valtype*>(view);
3a531937 5280 Address x = psymval->value(object, addend);
9726c3c1 5281 elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
9363c7c3
JY
5282 return (reloc_property->checkup_x_value(x)
5283 ? This::STATUS_OKAY
5284 : This::STATUS_OVERFLOW);
5285 }
5286
5287 // Do relocate. Update selected bits in text.
5288 // new_val = (val & ~dst_mask) | (immed << doffset)
5289
5290 template<int valsize>
5291 static inline typename This::Status
5292 rela_general(unsigned char* view,
5293 const Sized_relobj_file<size, big_endian>* object,
5294 const Symbol_value<size>* psymval,
9726c3c1 5295 AArch64_valtype addend,
9363c7c3
JY
5296 const AArch64_reloc_property* reloc_property)
5297 {
5298 // Calculate relocation.
83a01957 5299 Address x = psymval->value(object, addend);
9726c3c1 5300 return This::template reloc_common<valsize>(view, x, reloc_property);
9363c7c3
JY
5301 }
5302
5303 // Do relocate. Update selected bits in text.
5304 // new val = (val & ~dst_mask) | (immed << doffset)
5305
5306 template<int valsize>
5307 static inline typename This::Status
5308 rela_general(
5309 unsigned char* view,
9726c3c1
HS
5310 AArch64_valtype s,
5311 AArch64_valtype addend,
9363c7c3
JY
5312 const AArch64_reloc_property* reloc_property)
5313 {
5314 // Calculate relocation.
5315 Address x = s + addend;
9726c3c1 5316 return This::template reloc_common<valsize>(view, x, reloc_property);
9363c7c3
JY
5317 }
5318
5319 // Do address relative relocate. Update selected bits in text.
5320 // new val = (val & ~dst_mask) | (immed << doffset)
5321
5322 template<int valsize>
5323 static inline typename This::Status
5324 pcrela_general(
5325 unsigned char* view,
5326 const Sized_relobj_file<size, big_endian>* object,
5327 const Symbol_value<size>* psymval,
9726c3c1 5328 AArch64_valtype addend,
9363c7c3
JY
5329 Address address,
5330 const AArch64_reloc_property* reloc_property)
5331 {
5332 // Calculate relocation.
3a531937 5333 Address x = psymval->value(object, addend) - address;
9726c3c1
HS
5334 return This::template reloc_common<valsize>(view, x, reloc_property);
5335 }
9363c7c3 5336
9363c7c3 5337
9726c3c1 5338 // Calculate (S + A) - address, update adr instruction.
9363c7c3 5339
9726c3c1
HS
5340 static inline typename This::Status
5341 adr(unsigned char* view,
5342 const Sized_relobj_file<size, big_endian>* object,
5343 const Symbol_value<size>* psymval,
5344 Address addend,
5345 Address address,
5346 const AArch64_reloc_property* /* reloc_property */)
5347 {
5348 AArch64_valtype x = psymval->value(object, addend) - address;
5349 // Pick bits [20:0] of X.
5350 AArch64_valtype immed = x & 0x1fffff;
5351 update_adr(view, immed);
5352 // Check -2^20 <= X < 2^20
5353 return (size == 64 && Bits<21>::has_overflow((x))
5354 ? This::STATUS_OVERFLOW
5355 : This::STATUS_OKAY);
9363c7c3
JY
5356 }
5357
5358 // Calculate PG(S+A) - PG(address), update adrp instruction.
5359 // R_AARCH64_ADR_PREL_PG_HI21
5360
5361 static inline typename This::Status
5362 adrp(
5363 unsigned char* view,
5364 Address sa,
5365 Address address)
5366 {
9726c3c1
HS
5367 AArch64_valtype x = This::Page(sa) - This::Page(address);
5368 // Pick [32:12] of X.
5369 AArch64_valtype immed = (x >> 12) & 0x1fffff;
5370 update_adr(view, immed);
83a01957
HS
5371 // Check -2^32 <= X < 2^32
5372 return (size == 64 && Bits<33>::has_overflow((x))
9363c7c3
JY
5373 ? This::STATUS_OVERFLOW
5374 : This::STATUS_OKAY);
5375 }
5376
5377 // Calculate PG(S+A) - PG(address), update adrp instruction.
5378 // R_AARCH64_ADR_PREL_PG_HI21
5379
5380 static inline typename This::Status
5381 adrp(unsigned char* view,
5382 const Sized_relobj_file<size, big_endian>* object,
5383 const Symbol_value<size>* psymval,
5384 Address addend,
5385 Address address,
5386 const AArch64_reloc_property* reloc_property)
5387 {
5388 Address sa = psymval->value(object, addend);
9726c3c1
HS
5389 AArch64_valtype x = This::Page(sa) - This::Page(address);
5390 // Pick [32:12] of X.
5391 AArch64_valtype immed = (x >> 12) & 0x1fffff;
5392 update_adr(view, immed);
9363c7c3
JY
5393 return (reloc_property->checkup_x_value(x)
5394 ? This::STATUS_OKAY
5395 : This::STATUS_OVERFLOW);
5396 }
5397
3a531937
JY
5398 // Update mov[n/z] instruction. Check overflow if needed.
5399 // If X >=0, set the instruction to movz and its immediate value to the
5400 // selected bits S.
5401 // If X < 0, set the instruction to movn and its immediate value to
5402 // NOT (selected bits of).
5403
5404 static inline typename This::Status
5405 movnz(unsigned char* view,
9726c3c1 5406 AArch64_valtype x,
3a531937
JY
5407 const AArch64_reloc_property* reloc_property)
5408 {
5409 // Select bits from X.
9726c3c1
HS
5410 Address immed;
5411 bool is_movz;
5412 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
5413 if (static_cast<SignedW>(x) >= 0)
5414 {
5415 immed = reloc_property->select_x_value(x);
5416 is_movz = true;
5417 }
5418 else
3a531937 5419 {
9726c3c1 5420 immed = reloc_property->select_x_value(~x);;
3a531937
JY
5421 is_movz = false;
5422 }
5423
5424 // Update movnz instruction.
5425 update_movnz(view, immed, is_movz);
5426
5427 // Do check overflow or alignment if needed.
5428 return (reloc_property->checkup_x_value(x)
5429 ? This::STATUS_OKAY
5430 : This::STATUS_OVERFLOW);
5431 }
5432
83a01957
HS
5433 static inline bool
5434 maybe_apply_stub(unsigned int,
5435 const The_relocate_info*,
5436 const The_rela&,
5437 unsigned char*,
5438 Address,
5439 const Sized_symbol<size>*,
5440 const Symbol_value<size>*,
0bf32ea9
JY
5441 const Sized_relobj_file<size, big_endian>*,
5442 section_size_type);
83a01957 5443
3a531937
JY
5444}; // End of AArch64_relocate_functions
5445
5446
83a01957
HS
5447// For a certain relocation type (usually jump/branch), test to see if the
5448// destination needs a stub to fulfil. If so, re-route the destination of the
5449// original instruction to the stub, note, at this time, the stub has already
5450// been generated.
5451
5452template<int size, bool big_endian>
5453bool
5454AArch64_relocate_functions<size, big_endian>::
5455maybe_apply_stub(unsigned int r_type,
5456 const The_relocate_info* relinfo,
5457 const The_rela& rela,
5458 unsigned char* view,
5459 Address address,
5460 const Sized_symbol<size>* gsym,
5461 const Symbol_value<size>* psymval,
0bf32ea9
JY
5462 const Sized_relobj_file<size, big_endian>* object,
5463 section_size_type current_group_size)
83a01957
HS
5464{
5465 if (parameters->options().relocatable())
5466 return false;
5467
5468 typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
5469 Address branch_target = psymval->value(object, 0) + addend;
a48d0c12
HS
5470 int stub_type =
5471 The_reloc_stub::stub_type_for_reloc(r_type, address, branch_target);
5472 if (stub_type == ST_NONE)
83a01957
HS
5473 return false;
5474
5475 const The_aarch64_relobj* aarch64_relobj =
5476 static_cast<const The_aarch64_relobj*>(object);
69431bab
EC
5477 const AArch64_reloc_property* arp =
5478 aarch64_reloc_property_table->get_reloc_property(r_type);
5479 gold_assert(arp != NULL);
5480
5481 // We don't create stubs for undefined symbols, but do for weak.
5482 if (gsym
5483 && !gsym->use_plt_offset(arp->reference_flags())
5484 && gsym->is_undefined())
81b6fe3b
EC
5485 {
5486 gold_debug(DEBUG_TARGET,
5487 "stub: looking for a stub for undefined symbol %s in file %s",
5488 gsym->name(), aarch64_relobj->name().c_str());
5489 return false;
5490 }
5491
83a01957
HS
5492 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
5493 gold_assert(stub_table != NULL);
5494
5495 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5496 typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
5497 The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
5498 gold_assert(stub != NULL);
5499
5500 Address new_branch_target = stub_table->address() + stub->offset();
5501 typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
5502 new_branch_target - address;
aed56ec5 5503 typename This::Status status = This::template
83a01957
HS
5504 rela_general<32>(view, branch_offset, 0, arp);
5505 if (status != This::STATUS_OKAY)
5506 gold_error(_("Stub is too far away, try a smaller value "
0bf32ea9 5507 "for '--stub-group-size'. The current value is 0x%lx."),
add6016b 5508 static_cast<unsigned long>(current_group_size));
83a01957
HS
5509 return true;
5510}
5511
5512
5513// Group input sections for stub generation.
5514//
5515// We group input sections in an output section so that the total size,
5516// including any padding space due to alignment is smaller than GROUP_SIZE
5517// unless the only input section in group is bigger than GROUP_SIZE already.
5518// Then an ARM stub table is created to follow the last input section
5519// in group. For each group an ARM stub table is created an is placed
5520// after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
5521// extend the group after the stub table.
5522
5523template<int size, bool big_endian>
5524void
5525Target_aarch64<size, big_endian>::group_sections(
5526 Layout* layout,
5527 section_size_type group_size,
5528 bool stubs_always_after_branch,
5529 const Task* task)
5530{
5531 // Group input sections and insert stub table
5532 Layout::Section_list section_list;
5533 layout->get_executable_sections(&section_list);
5534 for (Layout::Section_list::const_iterator p = section_list.begin();
5535 p != section_list.end();
5536 ++p)
5537 {
5538 AArch64_output_section<size, big_endian>* output_section =
5539 static_cast<AArch64_output_section<size, big_endian>*>(*p);
5540 output_section->group_sections(group_size, stubs_always_after_branch,
5541 this, task);
5542 }
5543}
5544
5545
5546// Find the AArch64_input_section object corresponding to the SHNDX-th input
5547// section of RELOBJ.
5548
5549template<int size, bool big_endian>
5550AArch64_input_section<size, big_endian>*
5551Target_aarch64<size, big_endian>::find_aarch64_input_section(
5552 Relobj* relobj, unsigned int shndx) const
5553{
5554 Section_id sid(relobj, shndx);
5555 typename AArch64_input_section_map::const_iterator p =
5556 this->aarch64_input_section_map_.find(sid);
5557 return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
5558}
5559
5560
5561// Make a new AArch64_input_section object.
5562
5563template<int size, bool big_endian>
5564AArch64_input_section<size, big_endian>*
5565Target_aarch64<size, big_endian>::new_aarch64_input_section(
5566 Relobj* relobj, unsigned int shndx)
5567{
5568 Section_id sid(relobj, shndx);
5569
5570 AArch64_input_section<size, big_endian>* input_section =
5571 new AArch64_input_section<size, big_endian>(relobj, shndx);
5572 input_section->init();
5573
5574 // Register new AArch64_input_section in map for look-up.
5575 std::pair<typename AArch64_input_section_map::iterator,bool> ins =
5576 this->aarch64_input_section_map_.insert(
5577 std::make_pair(sid, input_section));
5578
5579 // Make sure that it we have not created another AArch64_input_section
5580 // for this input section already.
5581 gold_assert(ins.second);
5582
5583 return input_section;
5584}
5585
5586
5587// Relaxation hook. This is where we do stub generation.
5588
5589template<int size, bool big_endian>
5590bool
5591Target_aarch64<size, big_endian>::do_relax(
5592 int pass,
5593 const Input_objects* input_objects,
5594 Symbol_table* symtab,
5595 Layout* layout ,
5596 const Task* task)
5597{
5598 gold_assert(!parameters->options().relocatable());
5599 if (pass == 1)
5600 {
0bf32ea9
JY
5601 // We don't handle negative stub_group_size right now.
5602 this->stub_group_size_ = abs(parameters->options().stub_group_size());
5603 if (this->stub_group_size_ == 1)
83a01957
HS
5604 {
5605 // Leave room for 4096 4-byte stub entries. If we exceed that, then we
5606 // will fail to link. The user will have to relink with an explicit
5607 // group size option.
0bf32ea9
JY
5608 this->stub_group_size_ = The_reloc_stub::MAX_BRANCH_OFFSET -
5609 4096 * 4;
83a01957 5610 }
0bf32ea9 5611 group_sections(layout, this->stub_group_size_, true, task);
83a01957
HS
5612 }
5613 else
5614 {
5615 // If this is not the first pass, addresses and file offsets have
5616 // been reset at this point, set them here.
5617 for (Stub_table_iterator sp = this->stub_tables_.begin();
5618 sp != this->stub_tables_.end(); ++sp)
5619 {
5620 The_stub_table* stt = *sp;
5621 The_aarch64_input_section* owner = stt->owner();
5622 off_t off = align_address(owner->original_size(),
5623 stt->addralign());
5624 stt->set_address_and_file_offset(owner->address() + off,
5625 owner->offset() + off);
5626 }
5627 }
5628
5629 // Scan relocs for relocation stubs
5630 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
5631 op != input_objects->relobj_end();
5632 ++op)
5633 {
5634 The_aarch64_relobj* aarch64_relobj =
5635 static_cast<The_aarch64_relobj*>(*op);
5636 // Lock the object so we can read from it. This is only called
5637 // single-threaded from Layout::finalize, so it is OK to lock.
5638 Task_lock_obj<Object> tl(task, aarch64_relobj);
5639 aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
5640 }
5641
5642 bool any_stub_table_changed = false;
5643 for (Stub_table_iterator siter = this->stub_tables_.begin();
5644 siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
5645 {
5646 The_stub_table* stub_table = *siter;
5647 if (stub_table->update_data_size_changed_p())
5648 {
5649 The_aarch64_input_section* owner = stub_table->owner();
5650 uint64_t address = owner->address();
5651 off_t offset = owner->offset();
5652 owner->reset_address_and_file_offset();
5653 owner->set_address_and_file_offset(address, offset);
5654
5655 any_stub_table_changed = true;
5656 }
5657 }
5658
5659 // Do not continue relaxation.
5660 bool continue_relaxation = any_stub_table_changed;
5661 if (!continue_relaxation)
5662 for (Stub_table_iterator sp = this->stub_tables_.begin();
5663 (sp != this->stub_tables_.end());
5664 ++sp)
5665 (*sp)->finalize_stubs();
5666
5667 return continue_relaxation;
5668}
5669
5670
5671// Make a new Stub_table.
5672
5673template<int size, bool big_endian>
5674Stub_table<size, big_endian>*
5675Target_aarch64<size, big_endian>::new_stub_table(
5676 AArch64_input_section<size, big_endian>* owner)
5677{
5678 Stub_table<size, big_endian>* stub_table =
5679 new Stub_table<size, big_endian>(owner);
5680 stub_table->set_address(align_address(
5681 owner->address() + owner->data_size(), 8));
5682 stub_table->set_file_offset(owner->offset() + owner->data_size());
5683 stub_table->finalize_data_size();
5684
5685 this->stub_tables_.push_back(stub_table);
5686
5687 return stub_table;
5688}
5689
5690
3a531937 5691template<int size, bool big_endian>
7fa5525f 5692uint64_t
3a531937 5693Target_aarch64<size, big_endian>::do_reloc_addend(
7fa5525f 5694 void* arg, unsigned int r_type, uint64_t) const
3a531937
JY
5695{
5696 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
5697 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5698 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5699 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5700 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5701 gold_assert(psymval->is_tls_symbol());
5702 // The value of a TLS symbol is the offset in the TLS segment.
5703 return psymval->value(ti.object, 0);
5704}
9363c7c3 5705
053a4d68
JY
5706// Return the number of entries in the PLT.
5707
5708template<int size, bool big_endian>
5709unsigned int
5710Target_aarch64<size, big_endian>::plt_entry_count() const
5711{
5712 if (this->plt_ == NULL)
5713 return 0;
5714 return this->plt_->entry_count();
5715}
5716
5717// Return the offset of the first non-reserved PLT entry.
5718
5719template<int size, bool big_endian>
5720unsigned int
5721Target_aarch64<size, big_endian>::first_plt_entry_offset() const
5722{
5723 return this->plt_->first_plt_entry_offset();
5724}
5725
5726// Return the size of each PLT entry.
5727
5728template<int size, bool big_endian>
5729unsigned int
5730Target_aarch64<size, big_endian>::plt_entry_size() const
5731{
5732 return this->plt_->get_plt_entry_size();
5733}
5734
3a531937 5735// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
053a4d68
JY
5736
5737template<int size, bool big_endian>
3a531937
JY
5738void
5739Target_aarch64<size, big_endian>::define_tls_base_symbol(
5740 Symbol_table* symtab, Layout* layout)
053a4d68 5741{
3a531937
JY
5742 if (this->tls_base_symbol_defined_)
5743 return;
5744
5745 Output_segment* tls_segment = layout->tls_segment();
5746 if (tls_segment != NULL)
5747 {
6b0ad2eb 5748 // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
3a531937
JY
5749 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
5750 Symbol_table::PREDEFINED,
5751 tls_segment, 0, 0,
5752 elfcpp::STT_TLS,
5753 elfcpp::STB_LOCAL,
5754 elfcpp::STV_HIDDEN, 0,
6b0ad2eb 5755 Symbol::SEGMENT_START,
3a531937
JY
5756 true);
5757 }
5758 this->tls_base_symbol_defined_ = true;
053a4d68
JY
5759}
5760
3a531937 5761// Create the reserved PLT and GOT entries for the TLS descriptor resolver.
053a4d68
JY
5762
5763template<int size, bool big_endian>
3a531937
JY
5764void
5765Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
5766 Symbol_table* symtab, Layout* layout)
053a4d68 5767{
3a531937
JY
5768 if (this->plt_ == NULL)
5769 this->make_plt_section(symtab, layout);
5770
5771 if (!this->plt_->has_tlsdesc_entry())
053a4d68 5772 {
3a531937
JY
5773 // Allocate the TLSDESC_GOT entry.
5774 Output_data_got_aarch64<size, big_endian>* got =
5775 this->got_section(symtab, layout);
5776 unsigned int got_offset = got->add_constant(0);
5777
5778 // Allocate the TLSDESC_PLT entry.
5779 this->plt_->reserve_tlsdesc_entry(got_offset);
053a4d68 5780 }
053a4d68
JY
5781}
5782
3a531937 5783// Create a GOT entry for the TLS module index.
053a4d68
JY
5784
5785template<int size, bool big_endian>
3a531937
JY
5786unsigned int
5787Target_aarch64<size, big_endian>::got_mod_index_entry(
5788 Symbol_table* symtab, Layout* layout,
5789 Sized_relobj_file<size, big_endian>* object)
5790{
5791 if (this->got_mod_index_offset_ == -1U)
5792 {
5793 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5794 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5795 Output_data_got_aarch64<size, big_endian>* got =
5796 this->got_section(symtab, layout);
5797 unsigned int got_offset = got->add_constant(0);
5798 rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
5799 got_offset, 0);
5800 got->add_constant(0);
5801 this->got_mod_index_offset_ = got_offset;
5802 }
5803 return this->got_mod_index_offset_;
5804}
5805
5806// Optimize the TLS relocation type based on what we know about the
5807// symbol. IS_FINAL is true if the final address of this symbol is
5808// known at link time.
5809
5810template<int size, bool big_endian>
5811tls::Tls_optimization
5812Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
5813 int r_type)
5814{
5815 // If we are generating a shared library, then we can't do anything
5816 // in the linker
5817 if (parameters->options().shared())
5818 return tls::TLSOPT_NONE;
5819
5820 switch (r_type)
5821 {
5822 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5823 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5824 case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
5825 case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
5826 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5827 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5828 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5829 case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
5830 case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
5831 case elfcpp::R_AARCH64_TLSDESC_LDR:
5832 case elfcpp::R_AARCH64_TLSDESC_ADD:
5833 case elfcpp::R_AARCH64_TLSDESC_CALL:
5834 // These are General-Dynamic which permits fully general TLS
5835 // access. Since we know that we are generating an executable,
5836 // we can convert this to Initial-Exec. If we also know that
5837 // this is a local symbol, we can further switch to Local-Exec.
5838 if (is_final)
5839 return tls::TLSOPT_TO_LE;
5840 return tls::TLSOPT_TO_IE;
5841
9726c3c1
HS
5842 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5843 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5844 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5845 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6b0ad2eb
JY
5846 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5847 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
9726c3c1
HS
5848 // These are Local-Dynamic, which refer to local symbols in the
5849 // dynamic TLS block. Since we know that we generating an
5850 // executable, we can switch to Local-Exec.
5851 return tls::TLSOPT_TO_LE;
5852
3a531937
JY
5853 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5854 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5855 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5856 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5857 case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5858 // These are Initial-Exec relocs which get the thread offset
5859 // from the GOT. If we know that we are linking against the
5860 // local symbol, we can switch to Local-Exec, which links the
5861 // thread offset into the instruction.
5862 if (is_final)
5863 return tls::TLSOPT_TO_LE;
5864 return tls::TLSOPT_NONE;
5865
5866 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5867 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5868 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5869 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5870 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5871 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5872 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5873 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5874 // When we already have Local-Exec, there is nothing further we
5875 // can do.
5876 return tls::TLSOPT_NONE;
5877
5878 default:
5879 gold_unreachable();
5880 }
5881}
5882
5883// Returns true if this relocation type could be that of a function pointer.
5884
5885template<int size, bool big_endian>
5886inline bool
5887Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
5888 unsigned int r_type)
5889{
5890 switch (r_type)
5891 {
9726c3c1
HS
5892 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
5893 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
5894 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
5895 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5896 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
3a531937
JY
5897 {
5898 return true;
5899 }
5900 }
5901 return false;
5902}
5903
5904// For safe ICF, scan a relocation for a local symbol to check if it
5905// corresponds to a function pointer being taken. In that case mark
5906// the function whose pointer was taken as not foldable.
5907
5908template<int size, bool big_endian>
5909inline bool
5910Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
5911 Symbol_table* ,
053a4d68
JY
5912 Layout* ,
5913 Target_aarch64<size, big_endian>* ,
5914 Sized_relobj_file<size, big_endian>* ,
5915 unsigned int ,
5916 Output_section* ,
5917 const elfcpp::Rela<size, big_endian>& ,
5918 unsigned int r_type,
5919 const elfcpp::Sym<size, big_endian>&)
5920{
9726c3c1 5921 // When building a shared library, do not fold any local symbols.
053a4d68 5922 return (parameters->options().shared()
9363c7c3 5923 || possible_function_pointer_reloc(r_type));
053a4d68
JY
5924}
5925
5926// For safe ICF, scan a relocation for a global symbol to check if it
5927// corresponds to a function pointer being taken. In that case mark
5928// the function whose pointer was taken as not foldable.
5929
5930template<int size, bool big_endian>
5931inline bool
5932Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
5933 Symbol_table* ,
5934 Layout* ,
5935 Target_aarch64<size, big_endian>* ,
5936 Sized_relobj_file<size, big_endian>* ,
5937 unsigned int ,
5938 Output_section* ,
5939 const elfcpp::Rela<size, big_endian>& ,
5940 unsigned int r_type,
5941 Symbol* gsym)
5942{
5943 // When building a shared library, do not fold symbols whose visibility
5944 // is hidden, internal or protected.
5945 return ((parameters->options().shared()
9363c7c3
JY
5946 && (gsym->visibility() == elfcpp::STV_INTERNAL
5947 || gsym->visibility() == elfcpp::STV_PROTECTED
5948 || gsym->visibility() == elfcpp::STV_HIDDEN))
5949 || possible_function_pointer_reloc(r_type));
053a4d68
JY
5950}
5951
5952// Report an unsupported relocation against a local symbol.
5953
5954template<int size, bool big_endian>
5955void
5956Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
5957 Sized_relobj_file<size, big_endian>* object,
5958 unsigned int r_type)
5959{
5960 gold_error(_("%s: unsupported reloc %u against local symbol"),
5961 object->name().c_str(), r_type);
5962}
5963
5964// We are about to emit a dynamic relocation of type R_TYPE. If the
5965// dynamic linker does not support it, issue an error.
5966
5967template<int size, bool big_endian>
5968void
5969Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
9363c7c3 5970 unsigned int r_type)
053a4d68
JY
5971{
5972 gold_assert(r_type != elfcpp::R_AARCH64_NONE);
5973
5974 switch (r_type)
5975 {
5976 // These are the relocation types supported by glibc for AARCH64.
5977 case elfcpp::R_AARCH64_NONE:
5978 case elfcpp::R_AARCH64_COPY:
5979 case elfcpp::R_AARCH64_GLOB_DAT:
5980 case elfcpp::R_AARCH64_JUMP_SLOT:
5981 case elfcpp::R_AARCH64_RELATIVE:
5982 case elfcpp::R_AARCH64_TLS_DTPREL64:
5983 case elfcpp::R_AARCH64_TLS_DTPMOD64:
5984 case elfcpp::R_AARCH64_TLS_TPREL64:
5985 case elfcpp::R_AARCH64_TLSDESC:
5986 case elfcpp::R_AARCH64_IRELATIVE:
5987 case elfcpp::R_AARCH64_ABS32:
5988 case elfcpp::R_AARCH64_ABS64:
5989 return;
5990
5991 default:
5992 break;
5993 }
5994
5995 // This prevents us from issuing more than one error per reloc
5996 // section. But we can still wind up issuing more than one
5997 // error per object file.
5998 if (this->issued_non_pic_error_)
5999 return;
6000 gold_assert(parameters->options().output_is_position_independent());
6001 object->error(_("requires unsupported dynamic reloc; "
9363c7c3 6002 "recompile with -fPIC"));
053a4d68
JY
6003 this->issued_non_pic_error_ = true;
6004 return;
6005}
6006
9726c3c1
HS
6007// Return whether we need to make a PLT entry for a relocation of the
6008// given type against a STT_GNU_IFUNC symbol.
6009
6010template<int size, bool big_endian>
6011bool
6012Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
6013 Sized_relobj_file<size, big_endian>* object,
6014 unsigned int r_type)
6015{
6016 const AArch64_reloc_property* arp =
6017 aarch64_reloc_property_table->get_reloc_property(r_type);
6018 gold_assert(arp != NULL);
6019
6020 int flags = arp->reference_flags();
6021 if (flags & Symbol::TLS_REF)
6022 {
6023 gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
6024 object->name().c_str(), arp->name().c_str());
6025 return false;
6026 }
6027 return flags != 0;
6028}
6029
053a4d68
JY
6030// Scan a relocation for a local symbol.
6031
6032template<int size, bool big_endian>
6033inline void
6034Target_aarch64<size, big_endian>::Scan::local(
8e33481e
HS
6035 Symbol_table* symtab,
6036 Layout* layout,
6037 Target_aarch64<size, big_endian>* target,
9363c7c3 6038 Sized_relobj_file<size, big_endian>* object,
8e33481e
HS
6039 unsigned int data_shndx,
6040 Output_section* output_section,
6041 const elfcpp::Rela<size, big_endian>& rela,
053a4d68 6042 unsigned int r_type,
9726c3c1 6043 const elfcpp::Sym<size, big_endian>& lsym,
053a4d68
JY
6044 bool is_discarded)
6045{
6046 if (is_discarded)
6047 return;
6048
8e33481e 6049 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
3a531937 6050 Reloc_section;
3a531937 6051 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
8e33481e 6052
9726c3c1
HS
6053 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6054 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
6055 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
6056 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
6057
053a4d68
JY
6058 switch (r_type)
6059 {
5627d875
IK
6060 case elfcpp::R_AARCH64_NONE:
6061 break;
6062
9363c7c3
JY
6063 case elfcpp::R_AARCH64_ABS32:
6064 case elfcpp::R_AARCH64_ABS16:
8e33481e
HS
6065 if (parameters->options().output_is_position_independent())
6066 {
6067 gold_error(_("%s: unsupported reloc %u in pos independent link."),
6068 object->name().c_str(), r_type);
6069 }
6070 break;
6071
6072 case elfcpp::R_AARCH64_ABS64:
9363c7c3
JY
6073 // If building a shared library or pie, we need to mark this as a dynmic
6074 // reloction, so that the dynamic loader can relocate it.
9363c7c3
JY
6075 if (parameters->options().output_is_position_independent())
6076 {
8e33481e 6077 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8e33481e
HS
6078 rela_dyn->add_local_relative(object, r_sym,
6079 elfcpp::R_AARCH64_RELATIVE,
6080 output_section,
6081 data_shndx,
6082 rela.get_r_offset(),
6083 rela.get_r_addend(),
9726c3c1 6084 is_ifunc);
9363c7c3
JY
6085 }
6086 break;
6087
8e33481e
HS
6088 case elfcpp::R_AARCH64_PREL64:
6089 case elfcpp::R_AARCH64_PREL32:
6090 case elfcpp::R_AARCH64_PREL16:
6091 break;
6092
4d2f5d58
HS
6093 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6094 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5c28a503
HS
6095 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
6096 // The above relocations are used to access GOT entries.
4d2f5d58 6097 {
8032ac03
IK
6098 Output_data_got_aarch64<size, big_endian>* got =
6099 target->got_section(symtab, layout);
4d2f5d58
HS
6100 bool is_new = false;
6101 // This symbol requires a GOT entry.
6102 if (is_ifunc)
6103 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6104 else
6105 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6106 if (is_new && parameters->options().output_is_position_independent())
6107 target->rela_dyn_section(layout)->
6108 add_local_relative(object,
6109 r_sym,
6110 elfcpp::R_AARCH64_RELATIVE,
6111 got,
6112 object->local_got_offset(r_sym,
6113 GOT_TYPE_STANDARD),
6114 0,
6115 false);
6116 }
6117 break;
6118
8769bc4b
HS
6119 case elfcpp::R_AARCH64_MOVW_UABS_G0: // 263
6120 case elfcpp::R_AARCH64_MOVW_UABS_G0_NC: // 264
6121 case elfcpp::R_AARCH64_MOVW_UABS_G1: // 265
6122 case elfcpp::R_AARCH64_MOVW_UABS_G1_NC: // 266
6123 case elfcpp::R_AARCH64_MOVW_UABS_G2: // 267
6124 case elfcpp::R_AARCH64_MOVW_UABS_G2_NC: // 268
6125 case elfcpp::R_AARCH64_MOVW_UABS_G3: // 269
6126 case elfcpp::R_AARCH64_MOVW_SABS_G0: // 270
6127 case elfcpp::R_AARCH64_MOVW_SABS_G1: // 271
6128 case elfcpp::R_AARCH64_MOVW_SABS_G2: // 272
6129 if (parameters->options().output_is_position_independent())
6130 {
6131 gold_error(_("%s: unsupported reloc %u in pos independent link."),
6132 object->name().c_str(), r_type);
6133 }
6134 break;
6135
3a531937
JY
6136 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
6137 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
6138 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
6139 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
6140 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
6141 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
6142 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
6143 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
6144 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
8e33481e 6145 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
3a531937 6146 break;
053a4d68 6147
8e33481e
HS
6148 // Control flow, pc-relative. We don't need to do anything for a relative
6149 // addressing relocation against a local symbol if it does not reference
6150 // the GOT.
6151 case elfcpp::R_AARCH64_TSTBR14:
6152 case elfcpp::R_AARCH64_CONDBR19:
6153 case elfcpp::R_AARCH64_JUMP26:
6154 case elfcpp::R_AARCH64_CALL26:
6155 break;
6156
6157 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6158 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6159 {
3a531937
JY
6160 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6161 optimize_tls_reloc(!parameters->options().shared(), r_type);
6162 if (tlsopt == tls::TLSOPT_TO_LE)
6163 break;
6164
8e33481e
HS
6165 layout->set_has_static_tls();
6166 // Create a GOT entry for the tp-relative offset.
8e33481e
HS
6167 if (!parameters->doing_static_link())
6168 {
8032ac03
IK
6169 Output_data_got_aarch64<size, big_endian>* got =
6170 target->got_section(symtab, layout);
8e33481e
HS
6171 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
6172 target->rela_dyn_section(layout),
6173 elfcpp::R_AARCH64_TLS_TPREL64);
6174 }
6175 else if (!object->local_has_got_offset(r_sym,
6176 GOT_TYPE_TLS_OFFSET))
6177 {
8032ac03
IK
6178 Output_data_got_aarch64<size, big_endian>* got =
6179 target->got_section(symtab, layout);
8e33481e
HS
6180 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
6181 unsigned int got_offset =
6182 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
6183 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6184 gold_assert(addend == 0);
6185 got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
6186 object, r_sym);
6187 }
6188 }
6189 break;
6190
3a531937
JY
6191 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6192 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6193 {
6194 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6195 optimize_tls_reloc(!parameters->options().shared(), r_type);
6196 if (tlsopt == tls::TLSOPT_TO_LE)
6197 {
6198 layout->set_has_static_tls();
6199 break;
6200 }
6201 gold_assert(tlsopt == tls::TLSOPT_NONE);
6202
8032ac03
IK
6203 Output_data_got_aarch64<size, big_endian>* got =
6204 target->got_section(symtab, layout);
3a531937
JY
6205 got->add_local_pair_with_rel(object,r_sym, data_shndx,
6206 GOT_TYPE_TLS_PAIR,
6207 target->rela_dyn_section(layout),
6208 elfcpp::R_AARCH64_TLS_DTPMOD64);
6209 }
6210 break;
6211
1a920511
JY
6212 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6213 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6214 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6215 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6216 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
8e33481e
HS
6217 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6218 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6219 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6220 {
6221 layout->set_has_static_tls();
6222 bool output_is_shared = parameters->options().shared();
6223 if (output_is_shared)
3a531937 6224 gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
8e33481e
HS
6225 object->name().c_str(), r_type);
6226 }
9363c7c3
JY
6227 break;
6228
9726c3c1
HS
6229 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6230 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6231 {
6232 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6233 optimize_tls_reloc(!parameters->options().shared(), r_type);
6234 if (tlsopt == tls::TLSOPT_NONE)
6235 {
6236 // Create a GOT entry for the module index.
6237 target->got_mod_index_entry(symtab, layout, object);
6238 }
6239 else if (tlsopt != tls::TLSOPT_TO_LE)
6240 unsupported_reloc_local(object, r_type);
6241 }
6242 break;
6243
6244 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6245 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6b0ad2eb
JY
6246 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6247 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
9726c3c1
HS
6248 break;
6249
3a531937
JY
6250 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6251 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6252 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6253 {
6254 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6255 optimize_tls_reloc(!parameters->options().shared(), r_type);
6256 target->define_tls_base_symbol(symtab, layout);
6257 if (tlsopt == tls::TLSOPT_NONE)
6258 {
6259 // Create reserved PLT and GOT entries for the resolver.
6260 target->reserve_tlsdesc_entries(symtab, layout);
6261
6262 // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
6263 // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
6264 // entry needs to be in an area in .got.plt, not .got. Call
6265 // got_section to make sure the section has been created.
6266 target->got_section(symtab, layout);
6267 Output_data_got<size, big_endian>* got =
6268 target->got_tlsdesc_section();
6269 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6270 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
6271 {
6272 unsigned int got_offset = got->add_constant(0);
6273 got->add_constant(0);
6274 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
6275 got_offset);
6276 Reloc_section* rt = target->rela_tlsdesc_section(layout);
6277 // We store the arguments we need in a vector, and use
6278 // the index into the vector as the parameter to pass
6279 // to the target specific routines.
6280 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
6281 void* arg = reinterpret_cast<void*>(intarg);
6282 rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
6283 got, got_offset, 0);
6284 }
6285 }
6286 else if (tlsopt != tls::TLSOPT_TO_LE)
6287 unsupported_reloc_local(object, r_type);
6288 }
6289 break;
6290
6291 case elfcpp::R_AARCH64_TLSDESC_CALL:
6292 break;
6293
9363c7c3
JY
6294 default:
6295 unsupported_reloc_local(object, r_type);
053a4d68
JY
6296 }
6297}
6298
6299
6300// Report an unsupported relocation against a global symbol.
6301
6302template<int size, bool big_endian>
6303void
6304Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
6305 Sized_relobj_file<size, big_endian>* object,
6306 unsigned int r_type,
6307 Symbol* gsym)
6308{
6309 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6310 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6311}
6312
6313template<int size, bool big_endian>
6314inline void
6315Target_aarch64<size, big_endian>::Scan::global(
9363c7c3
JY
6316 Symbol_table* symtab,
6317 Layout* layout,
6318 Target_aarch64<size, big_endian>* target,
8e33481e
HS
6319 Sized_relobj_file<size, big_endian> * object,
6320 unsigned int data_shndx,
6321 Output_section* output_section,
6322 const elfcpp::Rela<size, big_endian>& rela,
9363c7c3
JY
6323 unsigned int r_type,
6324 Symbol* gsym)
053a4d68 6325{
9726c3c1
HS
6326 // A STT_GNU_IFUNC symbol may require a PLT entry.
6327 if (gsym->type() == elfcpp::STT_GNU_IFUNC
6328 && this->reloc_needs_plt_for_ifunc(object, r_type))
6329 target->make_plt_entry(symtab, layout, gsym);
6330
8e33481e
HS
6331 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
6332 Reloc_section;
3a531937
JY
6333 const AArch64_reloc_property* arp =
6334 aarch64_reloc_property_table->get_reloc_property(r_type);
6335 gold_assert(arp != NULL);
6336
9363c7c3
JY
6337 switch (r_type)
6338 {
5627d875
IK
6339 case elfcpp::R_AARCH64_NONE:
6340 break;
6341
8e33481e
HS
6342 case elfcpp::R_AARCH64_ABS16:
6343 case elfcpp::R_AARCH64_ABS32:
9363c7c3 6344 case elfcpp::R_AARCH64_ABS64:
8e33481e
HS
6345 {
6346 // Make a PLT entry if necessary.
6347 if (gsym->needs_plt_entry())
6348 {
6349 target->make_plt_entry(symtab, layout, gsym);
6350 // Since this is not a PC-relative relocation, we may be
6351 // taking the address of a function. In that case we need to
6352 // set the entry in the dynamic symbol table to the address of
6353 // the PLT entry.
6354 if (gsym->is_from_dynobj() && !parameters->options().shared())
6355 gsym->set_needs_dynsym_value();
6356 }
6357 // Make a dynamic relocation if necessary.
8e33481e
HS
6358 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
6359 {
6360 if (!parameters->options().output_is_position_independent()
6361 && gsym->may_need_copy_reloc())
6362 {
3a531937
JY
6363 target->copy_reloc(symtab, layout, object,
6364 data_shndx, output_section, gsym, rela);
8e33481e 6365 }
9726c3c1
HS
6366 else if (r_type == elfcpp::R_AARCH64_ABS64
6367 && gsym->type() == elfcpp::STT_GNU_IFUNC
6368 && gsym->can_use_relative_reloc(false)
6369 && !gsym->is_from_dynobj()
6370 && !gsym->is_undefined()
6371 && !gsym->is_preemptible())
6372 {
6373 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
6374 // symbol. This makes a function address in a PIE executable
6375 // match the address in a shared library that it links against.
6376 Reloc_section* rela_dyn =
6377 target->rela_irelative_section(layout);
6378 unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
6379 rela_dyn->add_symbolless_global_addend(gsym, r_type,
6380 output_section, object,
6381 data_shndx,
6382 rela.get_r_offset(),
6383 rela.get_r_addend());
6384 }
8e33481e
HS
6385 else if (r_type == elfcpp::R_AARCH64_ABS64
6386 && gsym->can_use_relative_reloc(false))
6387 {
6388 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6389 rela_dyn->add_global_relative(gsym,
6390 elfcpp::R_AARCH64_RELATIVE,
6391 output_section,
6392 object,
6393 data_shndx,
6394 rela.get_r_offset(),
6395 rela.get_r_addend(),
6396 false);
6397 }
6398 else
6399 {
6400 check_non_pic(object, r_type);
6401 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
6402 rela_dyn = target->rela_dyn_section(layout);
6403 rela_dyn->add_global(
6404 gsym, r_type, output_section, object,
6405 data_shndx, rela.get_r_offset(),rela.get_r_addend());
6406 }
6407 }
6408 }
6409 break;
6410
6411 case elfcpp::R_AARCH64_PREL16:
6412 case elfcpp::R_AARCH64_PREL32:
6413 case elfcpp::R_AARCH64_PREL64:
9363c7c3
JY
6414 // This is used to fill the GOT absolute address.
6415 if (gsym->needs_plt_entry())
6416 {
6417 target->make_plt_entry(symtab, layout, gsym);
6418 }
6419 break;
6420
8769bc4b
HS
6421 case elfcpp::R_AARCH64_MOVW_UABS_G0: // 263
6422 case elfcpp::R_AARCH64_MOVW_UABS_G0_NC: // 264
6423 case elfcpp::R_AARCH64_MOVW_UABS_G1: // 265
6424 case elfcpp::R_AARCH64_MOVW_UABS_G1_NC: // 266
6425 case elfcpp::R_AARCH64_MOVW_UABS_G2: // 267
6426 case elfcpp::R_AARCH64_MOVW_UABS_G2_NC: // 268
6427 case elfcpp::R_AARCH64_MOVW_UABS_G3: // 269
6428 case elfcpp::R_AARCH64_MOVW_SABS_G0: // 270
6429 case elfcpp::R_AARCH64_MOVW_SABS_G1: // 271
6430 case elfcpp::R_AARCH64_MOVW_SABS_G2: // 272
6431 if (parameters->options().output_is_position_independent())
6432 {
6433 gold_error(_("%s: unsupported reloc %u in pos independent link."),
6434 object->name().c_str(), r_type);
6435 }
6436 break;
6437
3a531937
JY
6438 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
6439 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
6440 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
6441 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
6442 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
6443 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
6444 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
6445 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
6446 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
8e33481e 6447 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
9363c7c3 6448 {
3a531937
JY
6449 if (gsym->needs_plt_entry())
6450 target->make_plt_entry(symtab, layout, gsym);
6451 // Make a dynamic relocation if necessary.
6452 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
6453 {
6454 if (parameters->options().output_is_executable()
6455 && gsym->may_need_copy_reloc())
6456 {
6457 target->copy_reloc(symtab, layout, object,
6458 data_shndx, output_section, gsym, rela);
6459 }
6460 }
9363c7c3
JY
6461 break;
6462 }
6463
6464 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6465 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5c28a503 6466 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
9363c7c3 6467 {
5c28a503 6468 // The above relocations are used to access GOT entries.
9363c7c3
JY
6469 // Note a GOT entry is an *address* to a symbol.
6470 // The symbol requires a GOT entry
6471 Output_data_got_aarch64<size, big_endian>* got =
6472 target->got_section(symtab, layout);
6473 if (gsym->final_value_is_known())
6474 {
9726c3c1
HS
6475 // For a STT_GNU_IFUNC symbol we want the PLT address.
6476 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
6477 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6478 else
6479 got->add_global(gsym, GOT_TYPE_STANDARD);
9363c7c3
JY
6480 }
6481 else
6482 {
9726c3c1
HS
6483 // If this symbol is not fully resolved, we need to add a dynamic
6484 // relocation for it.
9363c7c3 6485 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9726c3c1
HS
6486
6487 // Use a GLOB_DAT rather than a RELATIVE reloc if:
6488 //
6489 // 1) The symbol may be defined in some other module.
6490 // 2) We are building a shared library and this is a protected
6491 // symbol; using GLOB_DAT means that the dynamic linker can use
6492 // the address of the PLT in the main executable when appropriate
6493 // so that function address comparisons work.
6494 // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
6495 // again so that function address comparisons work.
9363c7c3
JY
6496 if (gsym->is_from_dynobj()
6497 || gsym->is_undefined()
6498 || gsym->is_preemptible()
6499 || (gsym->visibility() == elfcpp::STV_PROTECTED
9726c3c1
HS
6500 && parameters->options().shared())
6501 || (gsym->type() == elfcpp::STT_GNU_IFUNC
6502 && parameters->options().output_is_position_independent()))
9363c7c3
JY
6503 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
6504 rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
6505 else
6506 {
9726c3c1
HS
6507 // For a STT_GNU_IFUNC symbol we want to write the PLT
6508 // offset into the GOT, so that function pointer
6509 // comparisons work correctly.
6510 bool is_new;
6511 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
6512 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
6513 else
6514 {
6515 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6516 // Tell the dynamic linker to use the PLT address
6517 // when resolving relocations.
6518 if (gsym->is_from_dynobj()
6519 && !parameters->options().shared())
6520 gsym->set_needs_dynsym_value();
6521 }
6522 if (is_new)
3a531937
JY
6523 {
6524 rela_dyn->add_global_relative(
6525 gsym, elfcpp::R_AARCH64_RELATIVE,
6526 got,
6527 gsym->got_offset(GOT_TYPE_STANDARD),
6528 0,
6529 false);
6530 }
9363c7c3
JY
6531 }
6532 }
6533 break;
6534 }
6535
8e33481e
HS
6536 case elfcpp::R_AARCH64_TSTBR14:
6537 case elfcpp::R_AARCH64_CONDBR19:
9363c7c3
JY
6538 case elfcpp::R_AARCH64_JUMP26:
6539 case elfcpp::R_AARCH64_CALL26:
6540 {
6541 if (gsym->final_value_is_known())
6542 break;
6543
6544 if (gsym->is_defined() &&
6545 !gsym->is_from_dynobj() &&
6546 !gsym->is_preemptible())
6547 break;
6548
6549 // Make plt entry for function call.
9363c7c3
JY
6550 target->make_plt_entry(symtab, layout, gsym);
6551 break;
6552 }
6553
3a531937
JY
6554 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6555 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // General dynamic
6556 {
6557 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6558 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6559 if (tlsopt == tls::TLSOPT_TO_LE)
6560 {
6561 layout->set_has_static_tls();
6562 break;
6563 }
6564 gold_assert(tlsopt == tls::TLSOPT_NONE);
6565
6566 // General dynamic.
6567 Output_data_got_aarch64<size, big_endian>* got =
6568 target->got_section(symtab, layout);
6569 // Create 2 consecutive entries for module index and offset.
6570 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
6571 target->rela_dyn_section(layout),
6572 elfcpp::R_AARCH64_TLS_DTPMOD64,
6573 elfcpp::R_AARCH64_TLS_DTPREL64);
6574 }
6575 break;
6576
9726c3c1
HS
6577 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6578 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local dynamic
6579 {
6580 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6581 optimize_tls_reloc(!parameters->options().shared(), r_type);
6582 if (tlsopt == tls::TLSOPT_NONE)
6583 {
6584 // Create a GOT entry for the module index.
6585 target->got_mod_index_entry(symtab, layout, object);
6586 }
6587 else if (tlsopt != tls::TLSOPT_TO_LE)
6588 unsupported_reloc_local(object, r_type);
6589 }
6590 break;
6591
6592 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6b0ad2eb
JY
6593 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6594 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6595 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local dynamic
9726c3c1
HS
6596 break;
6597
8e33481e 6598 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3a531937 6599 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial executable
8e33481e 6600 {
9726c3c1 6601 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
3a531937
JY
6602 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6603 if (tlsopt == tls::TLSOPT_TO_LE)
6604 break;
6605
8e33481e
HS
6606 layout->set_has_static_tls();
6607 // Create a GOT entry for the tp-relative offset.
6608 Output_data_got_aarch64<size, big_endian>* got
6609 = target->got_section(symtab, layout);
6610 if (!parameters->doing_static_link())
6611 {
6612 got->add_global_with_rel(
6613 gsym, GOT_TYPE_TLS_OFFSET,
6614 target->rela_dyn_section(layout),
6615 elfcpp::R_AARCH64_TLS_TPREL64);
6616 }
6617 if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
6618 {
6619 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
6620 unsigned int got_offset =
6621 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
6622 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6623 gold_assert(addend == 0);
6624 got->add_static_reloc(got_offset,
6625 elfcpp::R_AARCH64_TLS_TPREL64, gsym);
6626 }
6627 }
6628 break;
6629
1a920511
JY
6630 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6631 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6632 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6633 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6634 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
8e33481e
HS
6635 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6636 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
3a531937 6637 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: // Local executable
8e33481e
HS
6638 layout->set_has_static_tls();
6639 if (parameters->options().shared())
6640 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
6641 object->name().c_str(), r_type);
6642 break;
6643
3a531937
JY
6644 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6645 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6646 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12: // TLS descriptor
6647 {
6648 target->define_tls_base_symbol(symtab, layout);
6649 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6650 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6651 if (tlsopt == tls::TLSOPT_NONE)
6652 {
6653 // Create reserved PLT and GOT entries for the resolver.
6654 target->reserve_tlsdesc_entries(symtab, layout);
6655
6656 // Create a double GOT entry with an R_AARCH64_TLSDESC
6657 // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
6658 // entry needs to be in an area in .got.plt, not .got. Call
6659 // got_section to make sure the section has been created.
6660 target->got_section(symtab, layout);
6661 Output_data_got<size, big_endian>* got =
6662 target->got_tlsdesc_section();
6663 Reloc_section* rt = target->rela_tlsdesc_section(layout);
6664 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
6665 elfcpp::R_AARCH64_TLSDESC, 0);
6666 }
6667 else if (tlsopt == tls::TLSOPT_TO_IE)
6668 {
6669 // Create a GOT entry for the tp-relative offset.
6670 Output_data_got<size, big_endian>* got
6671 = target->got_section(symtab, layout);
6672 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
6673 target->rela_dyn_section(layout),
6674 elfcpp::R_AARCH64_TLS_TPREL64);
6675 }
6676 else if (tlsopt != tls::TLSOPT_TO_LE)
6677 unsupported_reloc_global(object, r_type, gsym);
6678 }
6679 break;
6680
6681 case elfcpp::R_AARCH64_TLSDESC_CALL:
6682 break;
6683
9363c7c3 6684 default:
8e33481e 6685 gold_error(_("%s: unsupported reloc type in global scan"),
3a531937
JY
6686 aarch64_reloc_property_table->
6687 reloc_name_in_error_message(r_type).c_str());
9363c7c3 6688 }
053a4d68 6689 return;
9363c7c3
JY
6690} // End of Scan::global
6691
3a531937 6692
9363c7c3
JY
6693// Create the PLT section.
6694template<int size, bool big_endian>
6695void
6696Target_aarch64<size, big_endian>::make_plt_section(
6697 Symbol_table* symtab, Layout* layout)
6698{
6699 if (this->plt_ == NULL)
6700 {
6701 // Create the GOT section first.
6702 this->got_section(symtab, layout);
6703
3a531937
JY
6704 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
6705 this->got_irelative_);
9363c7c3
JY
6706
6707 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
6708 (elfcpp::SHF_ALLOC
6709 | elfcpp::SHF_EXECINSTR),
6710 this->plt_, ORDER_PLT, false);
6711
6712 // Make the sh_info field of .rela.plt point to .plt.
6713 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
6714 rela_plt_os->set_info_section(this->plt_->output_section());
6715 }
6716}
6717
3a531937
JY
6718// Return the section for TLSDESC relocations.
6719
6720template<int size, bool big_endian>
6721typename Target_aarch64<size, big_endian>::Reloc_section*
6722Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
6723{
6724 return this->plt_section()->rela_tlsdesc(layout);
6725}
6726
9363c7c3
JY
6727// Create a PLT entry for a global symbol.
6728
6729template<int size, bool big_endian>
6730void
6731Target_aarch64<size, big_endian>::make_plt_entry(
6732 Symbol_table* symtab,
6733 Layout* layout,
6734 Symbol* gsym)
6735{
6736 if (gsym->has_plt_offset())
6737 return;
6738
6739 if (this->plt_ == NULL)
6740 this->make_plt_section(symtab, layout);
6741
9726c3c1
HS
6742 this->plt_->add_entry(symtab, layout, gsym);
6743}
6744
6745// Make a PLT entry for a local STT_GNU_IFUNC symbol.
6746
6747template<int size, bool big_endian>
6748void
6749Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
6750 Symbol_table* symtab, Layout* layout,
6751 Sized_relobj_file<size, big_endian>* relobj,
6752 unsigned int local_sym_index)
6753{
6754 if (relobj->local_has_plt_offset(local_sym_index))
6755 return;
6756 if (this->plt_ == NULL)
6757 this->make_plt_section(symtab, layout);
6758 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
6759 relobj,
6760 local_sym_index);
6761 relobj->set_local_plt_offset(local_sym_index, plt_offset);
053a4d68
JY
6762}
6763
6764template<int size, bool big_endian>
6765void
6766Target_aarch64<size, big_endian>::gc_process_relocs(
6767 Symbol_table* symtab,
6768 Layout* layout,
6769 Sized_relobj_file<size, big_endian>* object,
6770 unsigned int data_shndx,
6771 unsigned int sh_type,
6772 const unsigned char* prelocs,
6773 size_t reloc_count,
6774 Output_section* output_section,
6775 bool needs_special_offset_handling,
6776 size_t local_symbol_count,
6777 const unsigned char* plocal_symbols)
6778{
4d625b70
CC
6779 typedef Target_aarch64<size, big_endian> Aarch64;
6780 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
6781 Classify_reloc;
6782
053a4d68
JY
6783 if (sh_type == elfcpp::SHT_REL)
6784 {
6785 return;
6786 }
6787
4d625b70 6788 gold::gc_process_relocs<size, big_endian, Aarch64, Scan, Classify_reloc>(
053a4d68
JY
6789 symtab,
6790 layout,
6791 this,
6792 object,
6793 data_shndx,
6794 prelocs,
6795 reloc_count,
6796 output_section,
6797 needs_special_offset_handling,
6798 local_symbol_count,
6799 plocal_symbols);
6800}
6801
6802// Scan relocations for a section.
6803
6804template<int size, bool big_endian>
6805void
6806Target_aarch64<size, big_endian>::scan_relocs(
6807 Symbol_table* symtab,
6808 Layout* layout,
6809 Sized_relobj_file<size, big_endian>* object,
6810 unsigned int data_shndx,
6811 unsigned int sh_type,
6812 const unsigned char* prelocs,
6813 size_t reloc_count,
6814 Output_section* output_section,
6815 bool needs_special_offset_handling,
6816 size_t local_symbol_count,
6817 const unsigned char* plocal_symbols)
6818{
4d625b70
CC
6819 typedef Target_aarch64<size, big_endian> Aarch64;
6820 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
6821 Classify_reloc;
6822
053a4d68
JY
6823 if (sh_type == elfcpp::SHT_REL)
6824 {
6825 gold_error(_("%s: unsupported REL reloc section"),
6826 object->name().c_str());
6827 return;
6828 }
4d625b70
CC
6829
6830 gold::scan_relocs<size, big_endian, Aarch64, Scan, Classify_reloc>(
053a4d68
JY
6831 symtab,
6832 layout,
6833 this,
6834 object,
6835 data_shndx,
6836 prelocs,
6837 reloc_count,
6838 output_section,
6839 needs_special_offset_handling,
6840 local_symbol_count,
6841 plocal_symbols);
6842}
6843
3a531937
JY
6844// Return the value to use for a dynamic which requires special
6845// treatment. This is how we support equality comparisons of function
6846// pointers across shared library boundaries, as described in the
6847// processor specific ABI supplement.
6848
83a01957 6849template<int size, bool big_endian>
3a531937 6850uint64_t
83a01957 6851Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
3a531937
JY
6852{
6853 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6854 return this->plt_address_for_global(gsym);
6855}
6856
83a01957 6857
053a4d68
JY
6858// Finalize the sections.
6859
6860template<int size, bool big_endian>
6861void
6862Target_aarch64<size, big_endian>::do_finalize_sections(
9363c7c3 6863 Layout* layout,
053a4d68 6864 const Input_objects*,
9363c7c3 6865 Symbol_table* symtab)
053a4d68 6866{
9363c7c3
JY
6867 const Reloc_section* rel_plt = (this->plt_ == NULL
6868 ? NULL
6869 : this->plt_->rela_plt());
6870 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
6871 this->rela_dyn_, true, false);
6872
3a531937
JY
6873 // Emit any relocs we saved in an attempt to avoid generating COPY
6874 // relocs.
6875 if (this->copy_relocs_.any_saved_relocs())
6876 this->copy_relocs_.emit(this->rela_dyn_section(layout));
6877
6878 // Fill in some more dynamic tags.
6879 Output_data_dynamic* const odyn = layout->dynamic_data();
6880 if (odyn != NULL)
6881 {
6882 if (this->plt_ != NULL
6883 && this->plt_->output_section() != NULL
6884 && this->plt_ ->has_tlsdesc_entry())
6885 {
6886 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
6887 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
6888 this->got_->finalize_data_size();
6889 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
6890 this->plt_, plt_offset);
6891 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
6892 this->got_, got_offset);
6893 }
6894 }
6895
9363c7c3
JY
6896 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
6897 // the .got.plt section.
6898 Symbol* sym = this->global_offset_table_;
6899 if (sym != NULL)
6900 {
6901 uint64_t data_size = this->got_plt_->current_data_size();
6902 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
6903
6904 // If the .got section is more than 0x8000 bytes, we add
6905 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
6906 // bit relocations have a greater chance of working.
6907 if (data_size >= 0x8000)
6908 symtab->get_sized_symbol<size>(sym)->set_value(
6909 symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
6910 }
6911
6912 if (parameters->doing_static_link()
6913 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
6914 {
6915 // If linking statically, make sure that the __rela_iplt symbols
6916 // were defined if necessary, even if we didn't create a PLT.
6917 static const Define_symbol_in_segment syms[] =
6918 {
6919 {
6920 "__rela_iplt_start", // name
6921 elfcpp::PT_LOAD, // segment_type
6922 elfcpp::PF_W, // segment_flags_set
6923 elfcpp::PF(0), // segment_flags_clear
6924 0, // value
6925 0, // size
6926 elfcpp::STT_NOTYPE, // type
6927 elfcpp::STB_GLOBAL, // binding
6928 elfcpp::STV_HIDDEN, // visibility
6929 0, // nonvis
6930 Symbol::SEGMENT_START, // offset_from_base
6931 true // only_if_ref
6932 },
6933 {
6934 "__rela_iplt_end", // name
6935 elfcpp::PT_LOAD, // segment_type
6936 elfcpp::PF_W, // segment_flags_set
6937 elfcpp::PF(0), // segment_flags_clear
6938 0, // value
6939 0, // size
6940 elfcpp::STT_NOTYPE, // type
6941 elfcpp::STB_GLOBAL, // binding
6942 elfcpp::STV_HIDDEN, // visibility
6943 0, // nonvis
6944 Symbol::SEGMENT_START, // offset_from_base
6945 true // only_if_ref
6946 }
6947 };
6948
6949 symtab->define_symbols(layout, 2, syms,
6950 layout->script_options()->saw_sections_clause());
6951 }
6952
053a4d68
JY
6953 return;
6954}
6955
6956// Perform a relocation.
6957
6958template<int size, bool big_endian>
6959inline bool
6960Target_aarch64<size, big_endian>::Relocate::relocate(
9363c7c3 6961 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 6962 unsigned int,
9363c7c3 6963 Target_aarch64<size, big_endian>* target,
053a4d68 6964 Output_section* ,
9363c7c3 6965 size_t relnum,
91a65d2f 6966 const unsigned char* preloc,
9363c7c3
JY
6967 const Sized_symbol<size>* gsym,
6968 const Symbol_value<size>* psymval,
6969 unsigned char* view,
6970 typename elfcpp::Elf_types<size>::Elf_Addr address,
053a4d68
JY
6971 section_size_type /* view_size */)
6972{
9363c7c3
JY
6973 if (view == NULL)
6974 return true;
6975
6976 typedef AArch64_relocate_functions<size, big_endian> Reloc;
6977
91a65d2f
AM
6978 const elfcpp::Rela<size, big_endian> rela(preloc);
6979 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
9363c7c3
JY
6980 const AArch64_reloc_property* reloc_property =
6981 aarch64_reloc_property_table->get_reloc_property(r_type);
6982
6983 if (reloc_property == NULL)
6984 {
6985 std::string reloc_name =
6986 aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
6987 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6988 _("cannot relocate %s in object file"),
6989 reloc_name.c_str());
6990 return true;
6991 }
6992
6993 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
6994
6995 // Pick the value to use for symbols defined in the PLT.
6996 Symbol_value<size> symval;
6997 if (gsym != NULL
6998 && gsym->use_plt_offset(reloc_property->reference_flags()))
6999 {
7000 symval.set_output_value(target->plt_address_for_global(gsym));
7001 psymval = &symval;
7002 }
7003 else if (gsym == NULL && psymval->is_ifunc_symbol())
7004 {
7005 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7006 if (object->local_has_plt_offset(r_sym))
7007 {
7008 symval.set_output_value(target->plt_address_for_local(object, r_sym));
7009 psymval = &symval;
7010 }
7011 }
7012
7013 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7014
7015 // Get the GOT offset if needed.
7016 // For aarch64, the GOT pointer points to the start of the GOT section.
7017 bool have_got_offset = false;
7018 int got_offset = 0;
7019 int got_base = (target->got_ != NULL
7020 ? (target->got_->current_data_size() >= 0x8000
7021 ? 0x8000 : 0)
7022 : 0);
7023 switch (r_type)
7024 {
7025 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
7026 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
7027 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
7028 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
7029 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
7030 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
7031 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
7032 case elfcpp::R_AARCH64_GOTREL64:
7033 case elfcpp::R_AARCH64_GOTREL32:
7034 case elfcpp::R_AARCH64_GOT_LD_PREL19:
7035 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
7036 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
7037 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
7038 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
7039 if (gsym != NULL)
7040 {
7041 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
7042 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
7043 }
7044 else
7045 {
7046 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7047 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
7048 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
7049 - got_base);
7050 }
7051 have_got_offset = true;
7052 break;
8e33481e 7053
9363c7c3
JY
7054 default:
7055 break;
7056 }
7057
7058 typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
7059 typename elfcpp::Elf_types<size>::Elf_Addr value;
7060 switch (r_type)
7061 {
7062 case elfcpp::R_AARCH64_NONE:
7063 break;
7064
7065 case elfcpp::R_AARCH64_ABS64:
0eccf19f
CC
7066 if (!parameters->options().apply_dynamic_relocs()
7067 && parameters->options().output_is_position_independent()
7068 && gsym != NULL
7069 && gsym->needs_dynamic_reloc(reloc_property->reference_flags())
7070 && !gsym->can_use_relative_reloc(false))
7071 // We have generated an absolute dynamic relocation, so do not
7072 // apply the relocation statically. (Works around bugs in older
7073 // Android dynamic linkers.)
7074 break;
9363c7c3
JY
7075 reloc_status = Reloc::template rela_ua<64>(
7076 view, object, psymval, addend, reloc_property);
7077 break;
7078
7079 case elfcpp::R_AARCH64_ABS32:
0eccf19f
CC
7080 if (!parameters->options().apply_dynamic_relocs()
7081 && parameters->options().output_is_position_independent()
7082 && gsym != NULL
7083 && gsym->needs_dynamic_reloc(reloc_property->reference_flags()))
7084 // We have generated an absolute dynamic relocation, so do not
7085 // apply the relocation statically. (Works around bugs in older
7086 // Android dynamic linkers.)
7087 break;
9363c7c3
JY
7088 reloc_status = Reloc::template rela_ua<32>(
7089 view, object, psymval, addend, reloc_property);
7090 break;
7091
7092 case elfcpp::R_AARCH64_ABS16:
0eccf19f
CC
7093 if (!parameters->options().apply_dynamic_relocs()
7094 && parameters->options().output_is_position_independent()
7095 && gsym != NULL
7096 && gsym->needs_dynamic_reloc(reloc_property->reference_flags()))
7097 // We have generated an absolute dynamic relocation, so do not
7098 // apply the relocation statically. (Works around bugs in older
7099 // Android dynamic linkers.)
7100 break;
9363c7c3
JY
7101 reloc_status = Reloc::template rela_ua<16>(
7102 view, object, psymval, addend, reloc_property);
7103 break;
7104
7105 case elfcpp::R_AARCH64_PREL64:
7106 reloc_status = Reloc::template pcrela_ua<64>(
7107 view, object, psymval, addend, address, reloc_property);
83a01957 7108 break;
9363c7c3
JY
7109
7110 case elfcpp::R_AARCH64_PREL32:
7111 reloc_status = Reloc::template pcrela_ua<32>(
7112 view, object, psymval, addend, address, reloc_property);
83a01957 7113 break;
9363c7c3
JY
7114
7115 case elfcpp::R_AARCH64_PREL16:
7116 reloc_status = Reloc::template pcrela_ua<16>(
7117 view, object, psymval, addend, address, reloc_property);
83a01957 7118 break;
9363c7c3 7119
8769bc4b
HS
7120 case elfcpp::R_AARCH64_MOVW_UABS_G0:
7121 case elfcpp::R_AARCH64_MOVW_UABS_G0_NC:
7122 case elfcpp::R_AARCH64_MOVW_UABS_G1:
7123 case elfcpp::R_AARCH64_MOVW_UABS_G1_NC:
7124 case elfcpp::R_AARCH64_MOVW_UABS_G2:
7125 case elfcpp::R_AARCH64_MOVW_UABS_G2_NC:
7126 case elfcpp::R_AARCH64_MOVW_UABS_G3:
7127 reloc_status = Reloc::template rela_general<32>(
7128 view, object, psymval, addend, reloc_property);
7129 break;
7130 case elfcpp::R_AARCH64_MOVW_SABS_G0:
7131 case elfcpp::R_AARCH64_MOVW_SABS_G1:
7132 case elfcpp::R_AARCH64_MOVW_SABS_G2:
7133 reloc_status = Reloc::movnz(view, psymval->value(object, addend),
7134 reloc_property);
7135 break;
7136
9726c3c1
HS
7137 case elfcpp::R_AARCH64_LD_PREL_LO19:
7138 reloc_status = Reloc::template pcrela_general<32>(
7139 view, object, psymval, addend, address, reloc_property);
7140 break;
7141
7142 case elfcpp::R_AARCH64_ADR_PREL_LO21:
7143 reloc_status = Reloc::adr(view, object, psymval, addend,
7144 address, reloc_property);
7145 break;
7146
9363c7c3
JY
7147 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
7148 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
7149 reloc_status = Reloc::adrp(view, object, psymval, addend, address,
7150 reloc_property);
7151 break;
7152
7153 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
7154 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
7155 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
7156 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
7157 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
7158 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
7159 reloc_status = Reloc::template rela_general<32>(
7160 view, object, psymval, addend, reloc_property);
7161 break;
7162
3a531937
JY
7163 case elfcpp::R_AARCH64_CALL26:
7164 if (this->skip_call_tls_get_addr_)
7165 {
7166 // Double check that the TLSGD insn has been optimized away.
7167 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7168 Insntype insn = elfcpp::Swap<32, big_endian>::readval(
7169 reinterpret_cast<Insntype*>(view));
7170 gold_assert((insn & 0xff000000) == 0x91000000);
7171
7172 reloc_status = Reloc::STATUS_OKAY;
7173 this->skip_call_tls_get_addr_ = false;
7174 // Return false to stop further processing this reloc.
7175 return false;
7176 }
d8e90251 7177 // Fall through.
83a01957
HS
7178 case elfcpp::R_AARCH64_JUMP26:
7179 if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
0bf32ea9
JY
7180 gsym, psymval, object,
7181 target->stub_group_size_))
83a01957 7182 break;
d8e90251 7183 // Fall through.
8e33481e
HS
7184 case elfcpp::R_AARCH64_TSTBR14:
7185 case elfcpp::R_AARCH64_CONDBR19:
9363c7c3
JY
7186 reloc_status = Reloc::template pcrela_general<32>(
7187 view, object, psymval, addend, address, reloc_property);
7188 break;
7189
7190 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
7191 gold_assert(have_got_offset);
7192 value = target->got_->address() + got_base + got_offset;
7193 reloc_status = Reloc::adrp(view, value + addend, address);
7194 break;
7195
7196 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
7197 gold_assert(have_got_offset);
7198 value = target->got_->address() + got_base + got_offset;
7199 reloc_status = Reloc::template rela_general<32>(
7200 view, value, addend, reloc_property);
7201 break;
7202
5c28a503
HS
7203 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
7204 {
7205 gold_assert(have_got_offset);
7206 value = target->got_->address() + got_base + got_offset + addend -
7207 Reloc::Page(target->got_->address() + got_base);
7208 if ((value & 7) != 0)
7209 reloc_status = Reloc::STATUS_OVERFLOW;
7210 else
7211 reloc_status = Reloc::template reloc_common<32>(
7212 view, value, reloc_property);
7213 break;
7214 }
7215
3a531937
JY
7216 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
7217 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
9726c3c1
HS
7218 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7219 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
7220 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
7221 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6b0ad2eb
JY
7222 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7223 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
8e33481e
HS
7224 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7225 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1a920511
JY
7226 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7227 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7228 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7229 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7230 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
8e33481e
HS
7231 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
7232 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
7233 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
3a531937
JY
7234 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7235 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7236 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7237 case elfcpp::R_AARCH64_TLSDESC_CALL:
8e33481e
HS
7238 reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
7239 gsym, psymval, view, address);
7240 break;
7241
3a531937
JY
7242 // These are dynamic relocations, which are unexpected when linking.
7243 case elfcpp::R_AARCH64_COPY:
7244 case elfcpp::R_AARCH64_GLOB_DAT:
7245 case elfcpp::R_AARCH64_JUMP_SLOT:
7246 case elfcpp::R_AARCH64_RELATIVE:
7247 case elfcpp::R_AARCH64_IRELATIVE:
7248 case elfcpp::R_AARCH64_TLS_DTPREL64:
7249 case elfcpp::R_AARCH64_TLS_DTPMOD64:
7250 case elfcpp::R_AARCH64_TLS_TPREL64:
7251 case elfcpp::R_AARCH64_TLSDESC:
9363c7c3 7252 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3a531937 7253 _("unexpected reloc %u in object file"),
9363c7c3
JY
7254 r_type);
7255 break;
3a531937
JY
7256
7257 default:
7258 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7259 _("unsupported reloc %s"),
7260 reloc_property->name().c_str());
7261 break;
9363c7c3
JY
7262 }
7263
7264 // Report any errors.
7265 switch (reloc_status)
7266 {
7267 case Reloc::STATUS_OKAY:
7268 break;
7269 case Reloc::STATUS_OVERFLOW:
7270 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7271 _("relocation overflow in %s"),
7272 reloc_property->name().c_str());
7273 break;
7274 case Reloc::STATUS_BAD_RELOC:
7275 gold_error_at_location(
7276 relinfo,
7277 relnum,
7278 rela.get_r_offset(),
7279 _("unexpected opcode while processing relocation %s"),
7280 reloc_property->name().c_str());
7281 break;
7282 default:
7283 gold_unreachable();
7284 }
7285
053a4d68
JY
7286 return true;
7287}
7288
3a531937 7289
8e33481e
HS
7290template<int size, bool big_endian>
7291inline
83a01957 7292typename AArch64_relocate_functions<size, big_endian>::Status
8e33481e 7293Target_aarch64<size, big_endian>::Relocate::relocate_tls(
83a01957 7294 const Relocate_info<size, big_endian>* relinfo,
3a531937
JY
7295 Target_aarch64<size, big_endian>* target,
7296 size_t relnum,
7297 const elfcpp::Rela<size, big_endian>& rela,
7298 unsigned int r_type, const Sized_symbol<size>* gsym,
7299 const Symbol_value<size>* psymval,
7300 unsigned char* view,
8e33481e
HS
7301 typename elfcpp::Elf_types<size>::Elf_Addr address)
7302{
83a01957 7303 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
3a531937 7304 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
8e33481e 7305
3a531937
JY
7306 Output_segment* tls_segment = relinfo->layout->tls_segment();
7307 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7308 const AArch64_reloc_property* reloc_property =
7309 aarch64_reloc_property_table->get_reloc_property(r_type);
8e33481e
HS
7310 gold_assert(reloc_property != NULL);
7311
3a531937
JY
7312 const bool is_final = (gsym == NULL
7313 ? !parameters->options().shared()
7314 : gsym->final_value_is_known());
7315 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
7316 optimize_tls_reloc(is_final, r_type);
7317
83a01957 7318 Sized_relobj_file<size, big_endian>* object = relinfo->object;
3a531937 7319 int tls_got_offset_type;
8e33481e
HS
7320 switch (r_type)
7321 {
3a531937
JY
7322 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
7323 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // Global-dynamic
7324 {
7325 if (tlsopt == tls::TLSOPT_TO_LE)
7326 {
7327 if (tls_segment == NULL)
7328 {
7329 gold_assert(parameters->errors()->error_count() > 0
7330 || issue_undefined_symbol_error(gsym));
7331 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7332 }
7333 return tls_gd_to_le(relinfo, target, rela, r_type, view,
7334 psymval);
7335 }
7336 else if (tlsopt == tls::TLSOPT_NONE)
7337 {
7338 tls_got_offset_type = GOT_TYPE_TLS_PAIR;
7339 // Firstly get the address for the got entry.
7340 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7341 if (gsym != NULL)
7342 {
7343 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7344 got_entry_address = target->got_->address() +
7345 gsym->got_offset(tls_got_offset_type);
7346 }
7347 else
7348 {
7349 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7350 gold_assert(
7351 object->local_has_got_offset(r_sym, tls_got_offset_type));
7352 got_entry_address = target->got_->address() +
7353 object->local_got_offset(r_sym, tls_got_offset_type);
7354 }
7355
7356 // Relocate the address into adrp/ld, adrp/add pair.
7357 switch (r_type)
7358 {
7359 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
7360 return aarch64_reloc_funcs::adrp(
7361 view, got_entry_address + addend, address);
7362
7363 break;
7364
7365 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
7366 return aarch64_reloc_funcs::template rela_general<32>(
7367 view, got_entry_address, addend, reloc_property);
7368 break;
7369
7370 default:
9726c3c1 7371 gold_unreachable();
3a531937
JY
7372 }
7373 }
7374 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7375 _("unsupported gd_to_ie relaxation on %u"),
7376 r_type);
7377 }
7378 break;
7379
9726c3c1
HS
7380 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7381 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local-dynamic
7382 {
7383 if (tlsopt == tls::TLSOPT_TO_LE)
7384 {
7385 if (tls_segment == NULL)
7386 {
7387 gold_assert(parameters->errors()->error_count() > 0
7388 || issue_undefined_symbol_error(gsym));
7389 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7390 }
7391 return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
7392 psymval);
7393 }
7394
7395 gold_assert(tlsopt == tls::TLSOPT_NONE);
7396 // Relocate the field with the offset of the GOT entry for
7397 // the module index.
7398 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7399 got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
7400 target->got_->address());
7401
7402 switch (r_type)
7403 {
7404 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7405 return aarch64_reloc_funcs::adrp(
7406 view, got_entry_address + addend, address);
7407 break;
7408
7409 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
7410 return aarch64_reloc_funcs::template rela_general<32>(
7411 view, got_entry_address, addend, reloc_property);
7412 break;
7413
7414 default:
7415 gold_unreachable();
7416 }
7417 }
7418 break;
7419
7420 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6b0ad2eb
JY
7421 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7422 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7423 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local-dynamic
9726c3c1
HS
7424 {
7425 AArch64_address value = psymval->value(object, 0);
7426 if (tlsopt == tls::TLSOPT_TO_LE)
7427 {
7428 if (tls_segment == NULL)
7429 {
7430 gold_assert(parameters->errors()->error_count() > 0
7431 || issue_undefined_symbol_error(gsym));
7432 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7433 }
9726c3c1
HS
7434 }
7435 switch (r_type)
7436 {
7437 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
7438 return aarch64_reloc_funcs::movnz(view, value + addend,
7439 reloc_property);
7440 break;
7441
7442 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6b0ad2eb
JY
7443 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7444 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
9726c3c1
HS
7445 return aarch64_reloc_funcs::template rela_general<32>(
7446 view, value, addend, reloc_property);
7447 break;
7448
7449 default:
7450 gold_unreachable();
7451 }
7452 // We should never reach here.
7453 }
7454 break;
7455
8e33481e 7456 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3a531937 7457 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial-exec
8e33481e 7458 {
3a531937
JY
7459 if (tlsopt == tls::TLSOPT_TO_LE)
7460 {
7461 if (tls_segment == NULL)
7462 {
7463 gold_assert(parameters->errors()->error_count() > 0
7464 || issue_undefined_symbol_error(gsym));
7465 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7466 }
7467 return tls_ie_to_le(relinfo, target, rela, r_type, view,
7468 psymval);
7469 }
7470 tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
7471
7472 // Firstly get the address for the got entry.
8e33481e
HS
7473 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7474 if (gsym != NULL)
7475 {
3a531937 7476 gold_assert(gsym->has_got_offset(tls_got_offset_type));
8e33481e 7477 got_entry_address = target->got_->address() +
3a531937 7478 gsym->got_offset(tls_got_offset_type);
8e33481e
HS
7479 }
7480 else
7481 {
7482 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7483 gold_assert(
3a531937 7484 object->local_has_got_offset(r_sym, tls_got_offset_type));
8e33481e 7485 got_entry_address = target->got_->address() +
3a531937 7486 object->local_got_offset(r_sym, tls_got_offset_type);
8e33481e 7487 }
3a531937
JY
7488 // Relocate the address into adrp/ld, adrp/add pair.
7489 switch (r_type)
8e33481e 7490 {
3a531937
JY
7491 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7492 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
7493 address);
7494 break;
7495 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7496 return aarch64_reloc_funcs::template rela_general<32>(
7497 view, got_entry_address, addend, reloc_property);
7498 default:
9726c3c1 7499 gold_unreachable();
8e33481e 7500 }
8e33481e 7501 }
3a531937 7502 // We shall never reach here.
8e33481e
HS
7503 break;
7504
1a920511
JY
7505 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7506 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7507 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7508 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7509 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
8e33481e
HS
7510 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
7511 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
7512 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7513 {
8e33481e 7514 gold_assert(tls_segment != NULL);
3a531937 7515 AArch64_address value = psymval->value(object, 0);
8e33481e
HS
7516
7517 if (!parameters->options().shared())
7518 {
3a531937
JY
7519 AArch64_address aligned_tcb_size =
7520 align_address(target->tcb_size(),
7521 tls_segment->maximum_alignment());
1a920511
JY
7522 value += aligned_tcb_size;
7523 switch (r_type)
7524 {
7525 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7526 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7527 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7528 return aarch64_reloc_funcs::movnz(view, value + addend,
7529 reloc_property);
7530 default:
7531 return aarch64_reloc_funcs::template
7532 rela_general<32>(view,
7533 value,
7534 addend,
7535 reloc_property);
7536 }
8e33481e
HS
7537 }
7538 else
7539 gold_error(_("%s: unsupported reloc %u "
7540 "in non-static TLSLE mode."),
7541 object->name().c_str(), r_type);
7542 }
7543 break;
7544
3a531937
JY
7545 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7546 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7547 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7548 case elfcpp::R_AARCH64_TLSDESC_CALL:
7549 {
7550 if (tlsopt == tls::TLSOPT_TO_LE)
7551 {
7552 if (tls_segment == NULL)
7553 {
7554 gold_assert(parameters->errors()->error_count() > 0
7555 || issue_undefined_symbol_error(gsym));
7556 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7557 }
7558 return tls_desc_gd_to_le(relinfo, target, rela, r_type,
7559 view, psymval);
7560 }
7561 else
7562 {
7563 tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
7564 ? GOT_TYPE_TLS_OFFSET
7565 : GOT_TYPE_TLS_DESC);
7566 unsigned int got_tlsdesc_offset = 0;
7567 if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
7568 && tlsopt == tls::TLSOPT_NONE)
7569 {
7570 // We created GOT entries in the .got.tlsdesc portion of the
7571 // .got.plt section, but the offset stored in the symbol is the
7572 // offset within .got.tlsdesc.
7573 got_tlsdesc_offset = (target->got_->data_size()
7574 + target->got_plt_section()->data_size());
7575 }
7576 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7577 if (gsym != NULL)
7578 {
7579 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7580 got_entry_address = target->got_->address()
7581 + got_tlsdesc_offset
7582 + gsym->got_offset(tls_got_offset_type);
7583 }
7584 else
7585 {
7586 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7587 gold_assert(
7588 object->local_has_got_offset(r_sym, tls_got_offset_type));
7589 got_entry_address = target->got_->address() +
7590 got_tlsdesc_offset +
7591 object->local_got_offset(r_sym, tls_got_offset_type);
7592 }
7593 if (tlsopt == tls::TLSOPT_TO_IE)
7594 {
3a531937
JY
7595 return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
7596 view, psymval, got_entry_address,
7597 address);
7598 }
7599
7600 // Now do tlsdesc relocation.
7601 switch (r_type)
7602 {
7603 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7604 return aarch64_reloc_funcs::adrp(view,
7605 got_entry_address + addend,
7606 address);
7607 break;
7608 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7609 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7610 return aarch64_reloc_funcs::template rela_general<32>(
7611 view, got_entry_address, addend, reloc_property);
7612 break;
7613 case elfcpp::R_AARCH64_TLSDESC_CALL:
7614 return aarch64_reloc_funcs::STATUS_OKAY;
7615 break;
7616 default:
7617 gold_unreachable();
7618 }
7619 }
7620 }
7621 break;
7622
8e33481e
HS
7623 default:
7624 gold_error(_("%s: unsupported TLS reloc %u."),
7625 object->name().c_str(), r_type);
7626 }
7627 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
3a531937
JY
7628} // End of relocate_tls.
7629
7630
7631template<int size, bool big_endian>
7632inline
83a01957 7633typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 7634Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
83a01957 7635 const Relocate_info<size, big_endian>* relinfo,
3a531937
JY
7636 Target_aarch64<size, big_endian>* target,
7637 const elfcpp::Rela<size, big_endian>& rela,
7638 unsigned int r_type,
7639 unsigned char* view,
7640 const Symbol_value<size>* psymval)
7641{
83a01957 7642 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
3a531937
JY
7643 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7644 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7645
7646 Insntype* ip = reinterpret_cast<Insntype*>(view);
7647 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7648 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7649 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7650
7651 if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
7652 {
7653 // This is the 2nd relocs, optimization should already have been
7654 // done.
7655 gold_assert((insn1 & 0xfff00000) == 0x91400000);
7656 return aarch64_reloc_funcs::STATUS_OKAY;
7657 }
7658
7659 // The original sequence is -
7660 // 90000000 adrp x0, 0 <main>
7661 // 91000000 add x0, x0, #0x0
7662 // 94000000 bl 0 <__tls_get_addr>
7663 // optimized to sequence -
7664 // d53bd040 mrs x0, tpidr_el0
7665 // 91400000 add x0, x0, #0x0, lsl #12
7666 // 91000000 add x0, x0, #0x0
7667
7668 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7669 // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
7670 // have to change "bl tls_get_addr", which does not have a corresponding tls
7671 // relocation type. So before proceeding, we need to make sure compiler
7672 // does not change the sequence.
7673 if(!(insn1 == 0x90000000 // adrp x0,0
7674 && insn2 == 0x91000000 // add x0, x0, #0x0
7675 && insn3 == 0x94000000)) // bl 0
7676 {
7677 // Ideally we should give up gd_to_le relaxation and do gd access.
7678 // However the gd_to_le relaxation decision has been made early
7679 // in the scan stage, where we did not allocate any GOT entry for
7680 // this symbol. Therefore we have to exit and report error now.
7681 gold_error(_("unexpected reloc insn sequence while relaxing "
7682 "tls gd to le for reloc %u."), r_type);
7683 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7684 }
7685
7686 // Write new insns.
7687 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
7688 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
7689 insn3 = 0x91000000; // add x0, x0, #0x0
7690 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7691 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7692 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7693
7694 // Calculate tprel value.
7695 Output_segment* tls_segment = relinfo->layout->tls_segment();
7696 gold_assert(tls_segment != NULL);
7697 AArch64_address value = psymval->value(relinfo->object, 0);
7698 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7699 AArch64_address aligned_tcb_size =
7700 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7701 AArch64_address x = value + aligned_tcb_size;
7702
7703 // After new insns are written, apply TLSLE relocs.
7704 const AArch64_reloc_property* rp1 =
7705 aarch64_reloc_property_table->get_reloc_property(
7706 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7707 const AArch64_reloc_property* rp2 =
7708 aarch64_reloc_property_table->get_reloc_property(
7709 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7710 gold_assert(rp1 != NULL && rp2 != NULL);
7711
7712 typename aarch64_reloc_funcs::Status s1 =
7713 aarch64_reloc_funcs::template rela_general<32>(view + 4,
7714 x,
7715 addend,
7716 rp1);
7717 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7718 return s1;
7719
7720 typename aarch64_reloc_funcs::Status s2 =
7721 aarch64_reloc_funcs::template rela_general<32>(view + 8,
7722 x,
7723 addend,
7724 rp2);
7725
7726 this->skip_call_tls_get_addr_ = true;
7727 return s2;
7728} // End of tls_gd_to_le
7729
7730
9726c3c1
HS
7731template<int size, bool big_endian>
7732inline
7733typename AArch64_relocate_functions<size, big_endian>::Status
7734Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
7735 const Relocate_info<size, big_endian>* relinfo,
7736 Target_aarch64<size, big_endian>* target,
7737 const elfcpp::Rela<size, big_endian>& rela,
7738 unsigned int r_type,
7739 unsigned char* view,
7740 const Symbol_value<size>* psymval)
7741{
7742 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7743 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7744 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7745
7746 Insntype* ip = reinterpret_cast<Insntype*>(view);
7747 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7748 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7749 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7750
7751 if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
7752 {
7753 // This is the 2nd relocs, optimization should already have been
7754 // done.
7755 gold_assert((insn1 & 0xfff00000) == 0x91400000);
7756 return aarch64_reloc_funcs::STATUS_OKAY;
7757 }
7758
7759 // The original sequence is -
7760 // 90000000 adrp x0, 0 <main>
7761 // 91000000 add x0, x0, #0x0
7762 // 94000000 bl 0 <__tls_get_addr>
7763 // optimized to sequence -
7764 // d53bd040 mrs x0, tpidr_el0
7765 // 91400000 add x0, x0, #0x0, lsl #12
7766 // 91000000 add x0, x0, #0x0
7767
7768 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7769 // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
7770 // have to change "bl tls_get_addr", which does not have a corresponding tls
7771 // relocation type. So before proceeding, we need to make sure compiler
7772 // does not change the sequence.
7773 if(!(insn1 == 0x90000000 // adrp x0,0
7774 && insn2 == 0x91000000 // add x0, x0, #0x0
7775 && insn3 == 0x94000000)) // bl 0
7776 {
7777 // Ideally we should give up gd_to_le relaxation and do gd access.
7778 // However the gd_to_le relaxation decision has been made early
c092b67b
EC
7779 // in the scan stage, where we did not allocate a GOT entry for
7780 // this symbol. Therefore we have to exit and report an error now.
9726c3c1
HS
7781 gold_error(_("unexpected reloc insn sequence while relaxing "
7782 "tls gd to le for reloc %u."), r_type);
7783 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7784 }
7785
7786 // Write new insns.
7787 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
7788 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
7789 insn3 = 0x91000000; // add x0, x0, #0x0
7790 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7791 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7792 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7793
7794 // Calculate tprel value.
7795 Output_segment* tls_segment = relinfo->layout->tls_segment();
7796 gold_assert(tls_segment != NULL);
7797 AArch64_address value = psymval->value(relinfo->object, 0);
7798 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7799 AArch64_address aligned_tcb_size =
7800 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7801 AArch64_address x = value + aligned_tcb_size;
7802
7803 // After new insns are written, apply TLSLE relocs.
7804 const AArch64_reloc_property* rp1 =
7805 aarch64_reloc_property_table->get_reloc_property(
7806 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7807 const AArch64_reloc_property* rp2 =
7808 aarch64_reloc_property_table->get_reloc_property(
7809 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7810 gold_assert(rp1 != NULL && rp2 != NULL);
7811
7812 typename aarch64_reloc_funcs::Status s1 =
7813 aarch64_reloc_funcs::template rela_general<32>(view + 4,
7814 x,
7815 addend,
7816 rp1);
7817 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7818 return s1;
7819
7820 typename aarch64_reloc_funcs::Status s2 =
7821 aarch64_reloc_funcs::template rela_general<32>(view + 8,
7822 x,
7823 addend,
7824 rp2);
7825
7826 this->skip_call_tls_get_addr_ = true;
7827 return s2;
7828
7829} // End of tls_ld_to_le
7830
3a531937
JY
7831template<int size, bool big_endian>
7832inline
83a01957 7833typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 7834Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
83a01957 7835 const Relocate_info<size, big_endian>* relinfo,
3a531937
JY
7836 Target_aarch64<size, big_endian>* target,
7837 const elfcpp::Rela<size, big_endian>& rela,
7838 unsigned int r_type,
7839 unsigned char* view,
7840 const Symbol_value<size>* psymval)
7841{
7842 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7843 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
83a01957 7844 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
3a531937
JY
7845
7846 AArch64_address value = psymval->value(relinfo->object, 0);
7847 Output_segment* tls_segment = relinfo->layout->tls_segment();
7848 AArch64_address aligned_tcb_address =
7849 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7850 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7851 AArch64_address x = value + addend + aligned_tcb_address;
7852 // "x" is the offset to tp, we can only do this if x is within
7853 // range [0, 2^32-1]
7854 if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
7855 {
7856 gold_error(_("TLS variable referred by reloc %u is too far from TP."),
7857 r_type);
7858 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7859 }
7860
7861 Insntype* ip = reinterpret_cast<Insntype*>(view);
7862 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7863 unsigned int regno;
7864 Insntype newinsn;
7865 if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
7866 {
7867 // Generate movz.
7868 regno = (insn & 0x1f);
7869 newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
7870 }
7871 else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
7872 {
7873 // Generate movk.
7874 regno = (insn & 0x1f);
7875 gold_assert(regno == ((insn >> 5) & 0x1f));
7876 newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
7877 }
7878 else
9726c3c1 7879 gold_unreachable();
3a531937
JY
7880
7881 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7882 return aarch64_reloc_funcs::STATUS_OKAY;
7883} // End of tls_ie_to_le
7884
7885
7886template<int size, bool big_endian>
7887inline
83a01957 7888typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 7889Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
83a01957 7890 const Relocate_info<size, big_endian>* relinfo,
3a531937
JY
7891 Target_aarch64<size, big_endian>* target,
7892 const elfcpp::Rela<size, big_endian>& rela,
7893 unsigned int r_type,
7894 unsigned char* view,
7895 const Symbol_value<size>* psymval)
7896{
7897 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7898 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
83a01957 7899 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
3a531937
JY
7900
7901 // TLSDESC-GD sequence is like:
7902 // adrp x0, :tlsdesc:v1
7903 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7904 // add x0, x0, :tlsdesc_lo12:v1
7905 // .tlsdesccall v1
7906 // blr x1
7907 // After desc_gd_to_le optimization, the sequence will be like:
7908 // movz x0, #0x0, lsl #16
7909 // movk x0, #0x10
7910 // nop
7911 // nop
7912
7913 // Calculate tprel value.
7914 Output_segment* tls_segment = relinfo->layout->tls_segment();
7915 gold_assert(tls_segment != NULL);
7916 Insntype* ip = reinterpret_cast<Insntype*>(view);
7917 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7918 AArch64_address value = psymval->value(relinfo->object, addend);
7919 AArch64_address aligned_tcb_size =
7920 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7921 AArch64_address x = value + aligned_tcb_size;
7922 // x is the offset to tp, we can only do this if x is within range
7923 // [0, 2^32-1]. If x is out of range, fail and exit.
7924 if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
7925 {
7926 gold_error(_("TLS variable referred by reloc %u is too far from TP. "
7927 "We Can't do gd_to_le relaxation.\n"), r_type);
7928 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7929 }
7930 Insntype newinsn;
7931 switch (r_type)
7932 {
7933 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7934 case elfcpp::R_AARCH64_TLSDESC_CALL:
7935 // Change to nop
7936 newinsn = 0xd503201f;
7937 break;
7938
7939 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7940 // Change to movz.
7941 newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
7942 break;
7943
7944 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7945 // Change to movk.
7946 newinsn = 0xf2800000 | ((x & 0xffff) << 5);
7947 break;
7948
7949 default:
7950 gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
7951 r_type);
7952 gold_unreachable();
7953 }
7954 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7955 return aarch64_reloc_funcs::STATUS_OKAY;
7956} // End of tls_desc_gd_to_le
7957
7958
7959template<int size, bool big_endian>
7960inline
83a01957 7961typename AArch64_relocate_functions<size, big_endian>::Status
3a531937 7962Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
83a01957 7963 const Relocate_info<size, big_endian>* /* relinfo */,
3a531937
JY
7964 Target_aarch64<size, big_endian>* /* target */,
7965 const elfcpp::Rela<size, big_endian>& rela,
7966 unsigned int r_type,
7967 unsigned char* view,
7968 const Symbol_value<size>* /* psymval */,
7969 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
7970 typename elfcpp::Elf_types<size>::Elf_Addr address)
7971{
7972 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
83a01957 7973 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
3a531937
JY
7974
7975 // TLSDESC-GD sequence is like:
7976 // adrp x0, :tlsdesc:v1
7977 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7978 // add x0, x0, :tlsdesc_lo12:v1
7979 // .tlsdesccall v1
7980 // blr x1
7981 // After desc_gd_to_ie optimization, the sequence will be like:
7982 // adrp x0, :tlsie:v1
7983 // ldr x0, [x0, :tlsie_lo12:v1]
7984 // nop
7985 // nop
7986
7987 Insntype* ip = reinterpret_cast<Insntype*>(view);
7988 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7989 Insntype newinsn;
7990 switch (r_type)
7991 {
7992 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7993 case elfcpp::R_AARCH64_TLSDESC_CALL:
7994 // Change to nop
7995 newinsn = 0xd503201f;
7996 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7997 break;
7998
7999 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
8000 {
8001 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
8002 address);
8003 }
8004 break;
8005
8006 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
8007 {
bb779192
HS
8008 // Set ldr target register to be x0.
8009 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
8010 insn &= 0xffffffe0;
8011 elfcpp::Swap<32, big_endian>::writeval(ip, insn);
8012 // Do relocation.
3a531937
JY
8013 const AArch64_reloc_property* reloc_property =
8014 aarch64_reloc_property_table->get_reloc_property(
8015 elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
8016 return aarch64_reloc_funcs::template rela_general<32>(
8017 view, got_entry_address, addend, reloc_property);
8018 }
8019 break;
8e33481e 8020
3a531937
JY
8021 default:
8022 gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
8023 r_type);
8024 gold_unreachable();
8025 }
8026 return aarch64_reloc_funcs::STATUS_OKAY;
8027} // End of tls_desc_gd_to_ie
8e33481e 8028
053a4d68
JY
8029// Relocate section data.
8030
8031template<int size, bool big_endian>
8032void
8033Target_aarch64<size, big_endian>::relocate_section(
9363c7c3 8034 const Relocate_info<size, big_endian>* relinfo,
053a4d68 8035 unsigned int sh_type,
9363c7c3
JY
8036 const unsigned char* prelocs,
8037 size_t reloc_count,
8038 Output_section* output_section,
8039 bool needs_special_offset_handling,
8040 unsigned char* view,
8041 typename elfcpp::Elf_types<size>::Elf_Addr address,
8042 section_size_type view_size,
8043 const Reloc_symbol_changes* reloc_symbol_changes)
053a4d68 8044{
6bf56e74 8045 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4d625b70 8046 typedef Target_aarch64<size, big_endian> Aarch64;
9363c7c3 8047 typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
4d625b70
CC
8048 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8049 Classify_reloc;
8050
8051 gold_assert(sh_type == elfcpp::SHT_RELA);
8052
6bf56e74
IK
8053 // See if we are relocating a relaxed input section. If so, the view
8054 // covers the whole output section and we need to adjust accordingly.
8055 if (needs_special_offset_handling)
8056 {
8057 const Output_relaxed_input_section* poris =
8058 output_section->find_relaxed_input_section(relinfo->object,
8059 relinfo->data_shndx);
8060 if (poris != NULL)
8061 {
8062 Address section_address = poris->address();
8063 section_size_type section_size = poris->data_size();
8064
8065 gold_assert((section_address >= address)
8066 && ((section_address + section_size)
8067 <= (address + view_size)));
8068
8069 off_t offset = section_address - address;
8070 view += offset;
8071 address += offset;
8072 view_size = section_size;
8073 }
8074 }
8075
4d625b70
CC
8076 gold::relocate_section<size, big_endian, Aarch64, AArch64_relocate,
8077 gold::Default_comdat_behavior, Classify_reloc>(
9363c7c3
JY
8078 relinfo,
8079 this,
8080 prelocs,
8081 reloc_count,
8082 output_section,
8083 needs_special_offset_handling,
8084 view,
8085 address,
8086 view_size,
8087 reloc_symbol_changes);
053a4d68
JY
8088}
8089
4d625b70 8090// Scan the relocs during a relocatable link.
053a4d68
JY
8091
8092template<int size, bool big_endian>
4d625b70
CC
8093void
8094Target_aarch64<size, big_endian>::scan_relocatable_relocs(
8095 Symbol_table* symtab,
8096 Layout* layout,
8097 Sized_relobj_file<size, big_endian>* object,
8098 unsigned int data_shndx,
8099 unsigned int sh_type,
8100 const unsigned char* prelocs,
8101 size_t reloc_count,
8102 Output_section* output_section,
8103 bool needs_special_offset_handling,
8104 size_t local_symbol_count,
8105 const unsigned char* plocal_symbols,
8106 Relocatable_relocs* rr)
053a4d68 8107{
4d625b70
CC
8108 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8109 Classify_reloc;
8110 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
8111 Scan_relocatable_relocs;
8112
8113 gold_assert(sh_type == elfcpp::SHT_RELA);
8114
8115 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
8116 symtab,
8117 layout,
8118 object,
8119 data_shndx,
8120 prelocs,
8121 reloc_count,
8122 output_section,
8123 needs_special_offset_handling,
8124 local_symbol_count,
8125 plocal_symbols,
8126 rr);
053a4d68
JY
8127}
8128
4d625b70 8129// Scan the relocs for --emit-relocs.
053a4d68
JY
8130
8131template<int size, bool big_endian>
8132void
4d625b70 8133Target_aarch64<size, big_endian>::emit_relocs_scan(
8e33481e
HS
8134 Symbol_table* symtab,
8135 Layout* layout,
8136 Sized_relobj_file<size, big_endian>* object,
8137 unsigned int data_shndx,
053a4d68 8138 unsigned int sh_type,
8e33481e
HS
8139 const unsigned char* prelocs,
8140 size_t reloc_count,
8141 Output_section* output_section,
8142 bool needs_special_offset_handling,
8143 size_t local_symbol_count,
4d625b70 8144 const unsigned char* plocal_syms,
8e33481e 8145 Relocatable_relocs* rr)
053a4d68 8146{
4d625b70
CC
8147 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8148 Classify_reloc;
8149 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
8150 Emit_relocs_strategy;
8e33481e 8151
4d625b70 8152 gold_assert(sh_type == elfcpp::SHT_RELA);
8e33481e 8153
4d625b70 8154 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
8e33481e
HS
8155 symtab,
8156 layout,
8157 object,
8158 data_shndx,
8159 prelocs,
8160 reloc_count,
8161 output_section,
8162 needs_special_offset_handling,
8163 local_symbol_count,
4d625b70 8164 plocal_syms,
8e33481e 8165 rr);
053a4d68
JY
8166}
8167
8168// Relocate a section during a relocatable link.
8169
8170template<int size, bool big_endian>
8171void
8172Target_aarch64<size, big_endian>::relocate_relocs(
8e33481e 8173 const Relocate_info<size, big_endian>* relinfo,
053a4d68 8174 unsigned int sh_type,
8e33481e
HS
8175 const unsigned char* prelocs,
8176 size_t reloc_count,
8177 Output_section* output_section,
8178 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
8e33481e
HS
8179 unsigned char* view,
8180 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
8181 section_size_type view_size,
8182 unsigned char* reloc_view,
8183 section_size_type reloc_view_size)
053a4d68 8184{
4d625b70
CC
8185 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8186 Classify_reloc;
8187
053a4d68 8188 gold_assert(sh_type == elfcpp::SHT_RELA);
8e33481e 8189
4d625b70 8190 gold::relocate_relocs<size, big_endian, Classify_reloc>(
8e33481e
HS
8191 relinfo,
8192 prelocs,
8193 reloc_count,
8194 output_section,
8195 offset_in_output_section,
8e33481e
HS
8196 view,
8197 view_address,
8198 view_size,
8199 reloc_view,
8200 reloc_view_size);
053a4d68
JY
8201}
8202
83a01957 8203
5019d64a
HS
8204// Return whether this is a 3-insn erratum sequence.
8205
8206template<int size, bool big_endian>
8207bool
8208Target_aarch64<size, big_endian>::is_erratum_843419_sequence(
8209 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
8210 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
8211 typename elfcpp::Swap<32,big_endian>::Valtype insn3)
8212{
8213 unsigned rt1, rt2;
8214 bool load, pair;
8215
8216 // The 2nd insn is a single register load or store; or register pair
8217 // store.
8218 if (Insn_utilities::aarch64_mem_op_p(insn2, &rt1, &rt2, &pair, &load)
8219 && (!pair || (pair && !load)))
8220 {
8221 // The 3rd insn is a load or store instruction from the "Load/store
8222 // register (unsigned immediate)" encoding class, using Rn as the
8223 // base address register.
8224 if (Insn_utilities::aarch64_ldst_uimm(insn3)
8225 && (Insn_utilities::aarch64_rn(insn3)
8226 == Insn_utilities::aarch64_rd(insn1)))
8227 return true;
8228 }
8229 return false;
8230}
8231
8232
2f0c79aa
HS
8233// Return whether this is a 835769 sequence.
8234// (Similarly implemented as in elfnn-aarch64.c.)
8235
8236template<int size, bool big_endian>
8237bool
8238Target_aarch64<size, big_endian>::is_erratum_835769_sequence(
8239 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
8240 typename elfcpp::Swap<32,big_endian>::Valtype insn2)
8241{
8242 uint32_t rt;
24228130 8243 uint32_t rt2 = 0;
2f0c79aa
HS
8244 uint32_t rn;
8245 uint32_t rm;
8246 uint32_t ra;
8247 bool pair;
8248 bool load;
8249
8250 if (Insn_utilities::aarch64_mlxl(insn2)
8251 && Insn_utilities::aarch64_mem_op_p (insn1, &rt, &rt2, &pair, &load))
8252 {
8253 /* Any SIMD memory op is independent of the subsequent MLA
8254 by definition of the erratum. */
8255 if (Insn_utilities::aarch64_bit(insn1, 26))
8256 return true;
8257
8258 /* If not SIMD, check for integer memory ops and MLA relationship. */
8259 rn = Insn_utilities::aarch64_rn(insn2);
8260 ra = Insn_utilities::aarch64_ra(insn2);
8261 rm = Insn_utilities::aarch64_rm(insn2);
8262
8263 /* If this is a load and there's a true(RAW) dependency, we are safe
8264 and this is not an erratum sequence. */
8265 if (load &&
8266 (rt == rn || rt == rm || rt == ra
8267 || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
8268 return false;
8269
8270 /* We conservatively put out stubs for all other cases (including
8271 writebacks). */
8272 return true;
8273 }
8274
8275 return false;
8276}
8277
8278
8279// Helper method to create erratum stub for ST_E_843419 and ST_E_835769.
8280
8281template<int size, bool big_endian>
8282void
8283Target_aarch64<size, big_endian>::create_erratum_stub(
8284 AArch64_relobj<size, big_endian>* relobj,
8285 unsigned int shndx,
8286 section_size_type erratum_insn_offset,
8287 Address erratum_address,
8288 typename Insn_utilities::Insntype erratum_insn,
0ef3814f
HS
8289 int erratum_type,
8290 unsigned int e843419_adrp_offset)
2f0c79aa
HS
8291{
8292 gold_assert(erratum_type == ST_E_843419 || erratum_type == ST_E_835769);
8293 The_stub_table* stub_table = relobj->stub_table(shndx);
8294 gold_assert(stub_table != NULL);
8295 if (stub_table->find_erratum_stub(relobj,
8296 shndx,
8297 erratum_insn_offset) == NULL)
8298 {
8299 const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
0ef3814f
HS
8300 The_erratum_stub* stub;
8301 if (erratum_type == ST_E_835769)
8302 stub = new The_erratum_stub(relobj, erratum_type, shndx,
8303 erratum_insn_offset);
8304 else if (erratum_type == ST_E_843419)
8305 stub = new E843419_stub<size, big_endian>(
8306 relobj, shndx, erratum_insn_offset, e843419_adrp_offset);
8307 else
8308 gold_unreachable();
2f0c79aa
HS
8309 stub->set_erratum_insn(erratum_insn);
8310 stub->set_erratum_address(erratum_address);
8311 // For erratum ST_E_843419 and ST_E_835769, the destination address is
8312 // always the next insn after erratum insn.
8313 stub->set_destination_address(erratum_address + BPI);
8314 stub_table->add_erratum_stub(stub);
8315 }
8316}
8317
8318
8319// Scan erratum for section SHNDX range [output_address + span_start,
8320// output_address + span_end). Note here we do not share the code with
8321// scan_erratum_843419_span function, because for 843419 we optimize by only
8322// scanning the last few insns of a page, whereas for 835769, we need to scan
8323// every insn.
8324
8325template<int size, bool big_endian>
8326void
8327Target_aarch64<size, big_endian>::scan_erratum_835769_span(
8328 AArch64_relobj<size, big_endian>* relobj,
8329 unsigned int shndx,
8330 const section_size_type span_start,
8331 const section_size_type span_end,
8332 unsigned char* input_view,
8333 Address output_address)
8334{
8335 typedef typename Insn_utilities::Insntype Insntype;
8336
8337 const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
8338
8339 // Adjust output_address and view to the start of span.
8340 output_address += span_start;
8341 input_view += span_start;
8342
8343 section_size_type span_length = span_end - span_start;
8344 section_size_type offset = 0;
8345 for (offset = 0; offset + BPI < span_length; offset += BPI)
8346 {
8347 Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
8348 Insntype insn1 = ip[0];
8349 Insntype insn2 = ip[1];
8350 if (is_erratum_835769_sequence(insn1, insn2))
8351 {
8352 Insntype erratum_insn = insn2;
8353 // "span_start + offset" is the offset for insn1. So for insn2, it is
8354 // "span_start + offset + BPI".
8355 section_size_type erratum_insn_offset = span_start + offset + BPI;
8356 Address erratum_address = output_address + offset + BPI;
73854cdd 8357 gold_info(_("Erratum 835769 found and fixed at \"%s\", "
2f0c79aa
HS
8358 "section %d, offset 0x%08x."),
8359 relobj->name().c_str(), shndx,
8360 (unsigned int)(span_start + offset));
8361
8362 this->create_erratum_stub(relobj, shndx,
8363 erratum_insn_offset, erratum_address,
8364 erratum_insn, ST_E_835769);
8365 offset += BPI; // Skip mac insn.
8366 }
8367 }
8368} // End of "Target_aarch64::scan_erratum_835769_span".
8369
8370
5019d64a
HS
8371// Scan erratum for section SHNDX range
8372// [output_address + span_start, output_address + span_end).
8373
8374template<int size, bool big_endian>
8375void
8376Target_aarch64<size, big_endian>::scan_erratum_843419_span(
8377 AArch64_relobj<size, big_endian>* relobj,
8378 unsigned int shndx,
8379 const section_size_type span_start,
8380 const section_size_type span_end,
8381 unsigned char* input_view,
8382 Address output_address)
8383{
8384 typedef typename Insn_utilities::Insntype Insntype;
8385
8386 // Adjust output_address and view to the start of span.
8387 output_address += span_start;
8388 input_view += span_start;
8389
8390 if ((output_address & 0x03) != 0)
8391 return;
8392
8393 section_size_type offset = 0;
8394 section_size_type span_length = span_end - span_start;
8395 // The first instruction must be ending at 0xFF8 or 0xFFC.
8396 unsigned int page_offset = output_address & 0xFFF;
8397 // Make sure starting position, that is "output_address+offset",
8398 // starts at page position 0xff8 or 0xffc.
8399 if (page_offset < 0xff8)
8400 offset = 0xff8 - page_offset;
8401 while (offset + 3 * Insn_utilities::BYTES_PER_INSN <= span_length)
8402 {
8403 Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
8404 Insntype insn1 = ip[0];
8405 if (Insn_utilities::is_adrp(insn1))
8406 {
8407 Insntype insn2 = ip[1];
8408 Insntype insn3 = ip[2];
a48d0c12
HS
8409 Insntype erratum_insn;
8410 unsigned insn_offset;
5019d64a
HS
8411 bool do_report = false;
8412 if (is_erratum_843419_sequence(insn1, insn2, insn3))
a48d0c12
HS
8413 {
8414 do_report = true;
8415 erratum_insn = insn3;
8416 insn_offset = 2 * Insn_utilities::BYTES_PER_INSN;
8417 }
5019d64a
HS
8418 else if (offset + 4 * Insn_utilities::BYTES_PER_INSN <= span_length)
8419 {
8420 // Optionally we can have an insn between ins2 and ins3
8421 Insntype insn_opt = ip[2];
8422 // And insn_opt must not be a branch.
8423 if (!Insn_utilities::aarch64_b(insn_opt)
8424 && !Insn_utilities::aarch64_bl(insn_opt)
8425 && !Insn_utilities::aarch64_blr(insn_opt)
8426 && !Insn_utilities::aarch64_br(insn_opt))
8427 {
8428 // And insn_opt must not write to dest reg in insn1. However
8429 // we do a conservative scan, which means we may fix/report
8430 // more than necessary, but it doesn't hurt.
8431
8432 Insntype insn4 = ip[3];
8433 if (is_erratum_843419_sequence(insn1, insn2, insn4))
a48d0c12
HS
8434 {
8435 do_report = true;
8436 erratum_insn = insn4;
8437 insn_offset = 3 * Insn_utilities::BYTES_PER_INSN;
8438 }
5019d64a
HS
8439 }
8440 }
8441 if (do_report)
8442 {
2f0c79aa 8443 unsigned int erratum_insn_offset =
a48d0c12 8444 span_start + offset + insn_offset;
2f0c79aa
HS
8445 Address erratum_address =
8446 output_address + offset + insn_offset;
8447 create_erratum_stub(relobj, shndx,
8448 erratum_insn_offset, erratum_address,
0ef3814f
HS
8449 erratum_insn, ST_E_843419,
8450 span_start + offset);
5019d64a
HS
8451 }
8452 }
8453
8454 // Advance to next candidate instruction. We only consider instruction
8455 // sequences starting at a page offset of 0xff8 or 0xffc.
8456 page_offset = (output_address + offset) & 0xfff;
8457 if (page_offset == 0xff8)
8458 offset += 4;
8459 else // (page_offset == 0xffc), we move to next page's 0xff8.
8460 offset += 0xffc;
8461 }
a48d0c12 8462} // End of "Target_aarch64::scan_erratum_843419_span".
5019d64a
HS
8463
8464
053a4d68
JY
8465// The selector for aarch64 object files.
8466
8467template<int size, bool big_endian>
8468class Target_selector_aarch64 : public Target_selector
8469{
8470 public:
9363c7c3 8471 Target_selector_aarch64();
053a4d68
JY
8472
8473 virtual Target*
8474 do_instantiate_target()
8475 { return new Target_aarch64<size, big_endian>(); }
8476};
8477
9363c7c3
JY
8478template<>
8479Target_selector_aarch64<32, true>::Target_selector_aarch64()
8480 : Target_selector(elfcpp::EM_AARCH64, 32, true,
8481 "elf32-bigaarch64", "aarch64_elf32_be_vec")
8482{ }
8483
8484template<>
8485Target_selector_aarch64<32, false>::Target_selector_aarch64()
8486 : Target_selector(elfcpp::EM_AARCH64, 32, false,
8487 "elf32-littleaarch64", "aarch64_elf32_le_vec")
8488{ }
8489
8490template<>
8491Target_selector_aarch64<64, true>::Target_selector_aarch64()
8492 : Target_selector(elfcpp::EM_AARCH64, 64, true,
8493 "elf64-bigaarch64", "aarch64_elf64_be_vec")
8494{ }
8495
8496template<>
8497Target_selector_aarch64<64, false>::Target_selector_aarch64()
8498 : Target_selector(elfcpp::EM_AARCH64, 64, false,
8499 "elf64-littleaarch64", "aarch64_elf64_le_vec")
8500{ }
8501
053a4d68
JY
8502Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
8503Target_selector_aarch64<32, false> target_selector_aarch64elf32;
8504Target_selector_aarch64<64, true> target_selector_aarch64elfb;
8505Target_selector_aarch64<64, false> target_selector_aarch64elf;
8506
053a4d68 8507} // End anonymous namespace.
This page took 0.808727 seconds and 4 git commands to generate.