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