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