PR binutils/15462
[platform/upstream/binutils.git] / bfd / elf64-aarch64.c
1 /* ELF support for AArch64.
2    Copyright 2009-2013 Free Software Foundation, Inc.
3    Contributed by ARM Ltd.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; see the file COPYING3. If not,
19    see <http://www.gnu.org/licenses/>.  */
20
21 /* Notes on implementation:
22
23   Thread Local Store (TLS)
24
25   Overview:
26
27   The implementation currently supports both traditional TLS and TLS
28   descriptors, but only general dynamic (GD).
29
30   For traditional TLS the assembler will present us with code
31   fragments of the form:
32
33   adrp x0, :tlsgd:foo
34                            R_AARCH64_TLSGD_ADR_PAGE21(foo)
35   add  x0, :tlsgd_lo12:foo
36                            R_AARCH64_TLSGD_ADD_LO12_NC(foo)
37   bl   __tls_get_addr
38   nop
39
40   For TLS descriptors the assembler will present us with code
41   fragments of the form:
42
43   adrp  x0, :tlsdesc:foo                      R_AARCH64_TLSDESC_ADR_PAGE(foo)
44   ldr   x1, [x0, #:tlsdesc_lo12:foo]          R_AARCH64_TLSDESC_LD64_LO12(foo)
45   add   x0, x0, #:tlsdesc_lo12:foo            R_AARCH64_TLSDESC_ADD_LO12(foo)
46   .tlsdesccall foo
47   blr   x1                                    R_AARCH64_TLSDESC_CALL(foo)
48
49   The relocations R_AARCH64_TLSGD_{ADR_PREL21,ADD_LO12_NC} against foo
50   indicate that foo is thread local and should be accessed via the
51   traditional TLS mechanims.
52
53   The relocations R_AARCH64_TLSDESC_{ADR_PAGE,LD64_LO12_NC,ADD_LO12_NC}
54   against foo indicate that 'foo' is thread local and should be accessed
55   via a TLS descriptor mechanism.
56
57   The precise instruction sequence is only relevant from the
58   perspective of linker relaxation which is currently not implemented.
59
60   The static linker must detect that 'foo' is a TLS object and
61   allocate a double GOT entry. The GOT entry must be created for both
62   global and local TLS symbols. Note that this is different to none
63   TLS local objects which do not need a GOT entry.
64
65   In the traditional TLS mechanism, the double GOT entry is used to
66   provide the tls_index structure, containing module and offset
67   entries. The static linker places the relocation R_AARCH64_TLS_DTPMOD64
68   on the module entry. The loader will subsequently fixup this
69   relocation with the module identity.
70
71   For global traditional TLS symbols the static linker places an
72   R_AARCH64_TLS_DTPREL64 relocation on the offset entry. The loader
73   will subsequently fixup the offset. For local TLS symbols the static
74   linker fixes up offset.
75
76   In the TLS descriptor mechanism the double GOT entry is used to
77   provide the descriptor. The static linker places the relocation
78   R_AARCH64_TLSDESC on the first GOT slot. The loader will
79   subsequently fix this up.
80
81   Implementation:
82
83   The handling of TLS symbols is implemented across a number of
84   different backend functions. The following is a top level view of
85   what processing is performed where.
86
87   The TLS implementation maintains state information for each TLS
88   symbol. The state information for local and global symbols is kept
89   in different places. Global symbols use generic BFD structures while
90   local symbols use backend specific structures that are allocated and
91   maintained entirely by the backend.
92
93   The flow:
94
95   aarch64_check_relocs()
96
97   This function is invoked for each relocation.
98
99   The TLS relocations R_AARCH64_TLSGD_{ADR_PREL21,ADD_LO12_NC} and
100   R_AARCH64_TLSDESC_{ADR_PAGE,LD64_LO12_NC,ADD_LO12_NC} are
101   spotted. One time creation of local symbol data structures are
102   created when the first local symbol is seen.
103
104   The reference count for a symbol is incremented.  The GOT type for
105   each symbol is marked as general dynamic.
106
107   elf64_aarch64_allocate_dynrelocs ()
108
109   For each global with positive reference count we allocate a double
110   GOT slot. For a traditional TLS symbol we allocate space for two
111   relocation entries on the GOT, for a TLS descriptor symbol we
112   allocate space for one relocation on the slot. Record the GOT offset
113   for this symbol.
114
115   elf64_aarch64_size_dynamic_sections ()
116
117   Iterate all input BFDS, look for in the local symbol data structure
118   constructed earlier for local TLS symbols and allocate them double
119   GOT slots along with space for a single GOT relocation. Update the
120   local symbol structure to record the GOT offset allocated.
121
122   elf64_aarch64_relocate_section ()
123
124   Calls elf64_aarch64_final_link_relocate ()
125
126   Emit the relevant TLS relocations against the GOT for each TLS
127   symbol. For local TLS symbols emit the GOT offset directly. The GOT
128   relocations are emitted once the first time a TLS symbol is
129   encountered. The implementation uses the LSB of the GOT offset to
130   flag that the relevant GOT relocations for a symbol have been
131   emitted. All of the TLS code that uses the GOT offset needs to take
132   care to mask out this flag bit before using the offset.
133
134   elf64_aarch64_final_link_relocate ()
135
136   Fixup the R_AARCH64_TLSGD_{ADR_PREL21, ADD_LO12_NC} relocations.  */
137
138 #include "sysdep.h"
139 #include "bfd.h"
140 #include "libiberty.h"
141 #include "libbfd.h"
142 #include "bfd_stdint.h"
143 #include "elf-bfd.h"
144 #include "bfdlink.h"
145 #include "elf/aarch64.h"
146
147 static bfd_reloc_status_type
148 bfd_elf_aarch64_put_addend (bfd *abfd,
149                             bfd_byte *address,
150                             reloc_howto_type *howto, bfd_signed_vma addend);
151
152 #define IS_AARCH64_TLS_RELOC(R_TYPE)                    \
153   ((R_TYPE) == R_AARCH64_TLSGD_ADR_PAGE21               \
154    || (R_TYPE) == R_AARCH64_TLSGD_ADD_LO12_NC           \
155    || (R_TYPE) == R_AARCH64_TLSIE_MOVW_GOTTPREL_G1      \
156    || (R_TYPE) == R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC   \
157    || (R_TYPE) == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21   \
158    || (R_TYPE) == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC \
159    || (R_TYPE) == R_AARCH64_TLSIE_LD_GOTTPREL_PREL19    \
160    || (R_TYPE) == R_AARCH64_TLSLE_ADD_TPREL_LO12        \
161    || (R_TYPE) == R_AARCH64_TLSLE_ADD_TPREL_HI12        \
162    || (R_TYPE) == R_AARCH64_TLSLE_ADD_TPREL_LO12_NC     \
163    || (R_TYPE) == R_AARCH64_TLSLE_MOVW_TPREL_G2         \
164    || (R_TYPE) == R_AARCH64_TLSLE_MOVW_TPREL_G1         \
165    || (R_TYPE) == R_AARCH64_TLSLE_MOVW_TPREL_G1_NC      \
166    || (R_TYPE) == R_AARCH64_TLSLE_MOVW_TPREL_G0         \
167    || (R_TYPE) == R_AARCH64_TLSLE_MOVW_TPREL_G0_NC      \
168    || (R_TYPE) == R_AARCH64_TLS_DTPMOD64                \
169    || (R_TYPE) == R_AARCH64_TLS_DTPREL64                \
170    || (R_TYPE) == R_AARCH64_TLS_TPREL64                 \
171    || IS_AARCH64_TLSDESC_RELOC ((R_TYPE)))
172
173 #define IS_AARCH64_TLSDESC_RELOC(R_TYPE)                \
174   ((R_TYPE) == R_AARCH64_TLSDESC_LD64_PREL19            \
175    || (R_TYPE) == R_AARCH64_TLSDESC_ADR_PREL21          \
176    || (R_TYPE) == R_AARCH64_TLSDESC_ADR_PAGE            \
177    || (R_TYPE) == R_AARCH64_TLSDESC_ADD_LO12_NC         \
178    || (R_TYPE) == R_AARCH64_TLSDESC_LD64_LO12_NC        \
179    || (R_TYPE) == R_AARCH64_TLSDESC_OFF_G1              \
180    || (R_TYPE) == R_AARCH64_TLSDESC_OFF_G0_NC           \
181    || (R_TYPE) == R_AARCH64_TLSDESC_LDR                 \
182    || (R_TYPE) == R_AARCH64_TLSDESC_ADD                 \
183    || (R_TYPE) == R_AARCH64_TLSDESC_CALL                \
184    || (R_TYPE) == R_AARCH64_TLSDESC)
185
186 #define ELIMINATE_COPY_RELOCS 0
187
188 /* Return the relocation section associated with NAME.  HTAB is the
189    bfd's elf64_aarch64_link_hash_entry.  */
190 #define RELOC_SECTION(HTAB, NAME) \
191   ((HTAB)->use_rel ? ".rel" NAME : ".rela" NAME)
192
193 /* Return size of a relocation entry.  HTAB is the bfd's
194    elf64_aarch64_link_hash_entry.  */
195 #define RELOC_SIZE(HTAB) (sizeof (Elf64_External_Rela))
196
197 /* Return function to swap relocations in.  HTAB is the bfd's
198    elf64_aarch64_link_hash_entry.  */
199 #define SWAP_RELOC_IN(HTAB) (bfd_elf64_swap_reloca_in)
200
201 /* Return function to swap relocations out.  HTAB is the bfd's
202    elf64_aarch64_link_hash_entry.  */
203 #define SWAP_RELOC_OUT(HTAB) (bfd_elf64_swap_reloca_out)
204
205 /* GOT Entry size - 8 bytes.  */
206 #define GOT_ENTRY_SIZE                  (8)
207 #define PLT_ENTRY_SIZE                  (32)
208 #define PLT_SMALL_ENTRY_SIZE            (16)
209 #define PLT_TLSDESC_ENTRY_SIZE          (32)
210
211 /* Take the PAGE component of an address or offset.  */
212 #define PG(x) ((x) & ~ 0xfff)
213 #define PG_OFFSET(x) ((x) & 0xfff)
214
215 /* Encoding of the nop instruction */
216 #define INSN_NOP 0xd503201f
217
218 #define aarch64_compute_jump_table_size(htab)           \
219   (((htab)->root.srelplt == NULL) ? 0                   \
220    : (htab)->root.srelplt->reloc_count * GOT_ENTRY_SIZE)
221
222 /* The first entry in a procedure linkage table looks like this
223    if the distance between the PLTGOT and the PLT is < 4GB use
224    these PLT entries. Note that the dynamic linker gets &PLTGOT[2]
225    in x16 and needs to work out PLTGOT[1] by using an address of
226    [x16,#-8].  */
227 static const bfd_byte elf64_aarch64_small_plt0_entry[PLT_ENTRY_SIZE] =
228 {
229   0xf0, 0x7b, 0xbf, 0xa9,       /* stp x16, x30, [sp, #-16]!  */
230   0x10, 0x00, 0x00, 0x90,       /* adrp x16, (GOT+16)  */
231   0x11, 0x0A, 0x40, 0xf9,       /* ldr x17, [x16, #PLT_GOT+0x10]  */
232   0x10, 0x42, 0x00, 0x91,       /* add x16, x16,#PLT_GOT+0x10   */
233   0x20, 0x02, 0x1f, 0xd6,       /* br x17  */
234   0x1f, 0x20, 0x03, 0xd5,       /* nop */
235   0x1f, 0x20, 0x03, 0xd5,       /* nop */
236   0x1f, 0x20, 0x03, 0xd5,       /* nop */
237 };
238
239 /* Per function entry in a procedure linkage table looks like this
240    if the distance between the PLTGOT and the PLT is < 4GB use
241    these PLT entries.  */
242 static const bfd_byte elf64_aarch64_small_plt_entry[PLT_SMALL_ENTRY_SIZE] =
243 {
244   0x10, 0x00, 0x00, 0x90,       /* adrp x16, PLTGOT + n * 8  */
245   0x11, 0x02, 0x40, 0xf9,       /* ldr x17, [x16, PLTGOT + n * 8] */
246   0x10, 0x02, 0x00, 0x91,       /* add x16, x16, :lo12:PLTGOT + n * 8  */
247   0x20, 0x02, 0x1f, 0xd6,       /* br x17.  */
248 };
249
250 static const bfd_byte
251 elf64_aarch64_tlsdesc_small_plt_entry[PLT_TLSDESC_ENTRY_SIZE] =
252 {
253   0xe2, 0x0f, 0xbf, 0xa9,       /* stp x2, x3, [sp, #-16]! */
254   0x02, 0x00, 0x00, 0x90,       /* adrp x2, 0 */
255   0x03, 0x00, 0x00, 0x90,       /* adrp x3, 0 */
256   0x42, 0x08, 0x40, 0xF9,       /* ldr x2, [x2, #0] */
257   0x63, 0x00, 0x00, 0x91,       /* add x3, x3, 0 */
258   0x40, 0x00, 0x1F, 0xD6,       /* br x2 */
259   0x1f, 0x20, 0x03, 0xd5,       /* nop */
260   0x1f, 0x20, 0x03, 0xd5,       /* nop */
261 };
262
263 #define elf_info_to_howto               elf64_aarch64_info_to_howto
264 #define elf_info_to_howto_rel           elf64_aarch64_info_to_howto
265
266 #define AARCH64_ELF_ABI_VERSION         0
267 #define AARCH64_ELF_OS_ABI_VERSION      0
268
269 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
270 #define ALL_ONES (~ (bfd_vma) 0)
271
272 static reloc_howto_type elf64_aarch64_howto_none =
273   HOWTO (R_AARCH64_NONE,        /* type */
274          0,                     /* rightshift */
275          0,                     /* size (0 = byte, 1 = short, 2 = long) */
276          0,                     /* bitsize */
277          FALSE,                 /* pc_relative */
278          0,                     /* bitpos */
279          complain_overflow_dont,/* complain_on_overflow */
280          bfd_elf_generic_reloc, /* special_function */
281          "R_AARCH64_NONE",      /* name */
282          FALSE,                 /* partial_inplace */
283          0,                     /* src_mask */
284          0,                     /* dst_mask */
285          FALSE);                /* pcrel_offset */
286
287 static reloc_howto_type elf64_aarch64_howto_dynrelocs[] =
288 {
289   HOWTO (R_AARCH64_COPY,        /* type */
290          0,                     /* rightshift */
291          2,                     /* size (0 = byte, 1 = short, 2 = long) */
292          64,                    /* bitsize */
293          FALSE,                 /* pc_relative */
294          0,                     /* bitpos */
295          complain_overflow_bitfield,    /* complain_on_overflow */
296          bfd_elf_generic_reloc, /* special_function */
297          "R_AARCH64_COPY",      /* name */
298          TRUE,                  /* partial_inplace */
299          0xffffffff,            /* src_mask */
300          0xffffffff,            /* dst_mask */
301          FALSE),                /* pcrel_offset */
302
303   HOWTO (R_AARCH64_GLOB_DAT,    /* type */
304          0,                     /* rightshift */
305          2,                     /* size (0 = byte, 1 = short, 2 = long) */
306          64,                    /* bitsize */
307          FALSE,                 /* pc_relative */
308          0,                     /* bitpos */
309          complain_overflow_bitfield,    /* complain_on_overflow */
310          bfd_elf_generic_reloc, /* special_function */
311          "R_AARCH64_GLOB_DAT",  /* name */
312          TRUE,                  /* partial_inplace */
313          0xffffffff,            /* src_mask */
314          0xffffffff,            /* dst_mask */
315          FALSE),                /* pcrel_offset */
316
317   HOWTO (R_AARCH64_JUMP_SLOT,   /* type */
318          0,                     /* rightshift */
319          2,                     /* size (0 = byte, 1 = short, 2 = long) */
320          64,                    /* bitsize */
321          FALSE,                 /* pc_relative */
322          0,                     /* bitpos */
323          complain_overflow_bitfield,    /* complain_on_overflow */
324          bfd_elf_generic_reloc, /* special_function */
325          "R_AARCH64_JUMP_SLOT", /* name */
326          TRUE,                  /* partial_inplace */
327          0xffffffff,            /* src_mask */
328          0xffffffff,            /* dst_mask */
329          FALSE),                /* pcrel_offset */
330
331   HOWTO (R_AARCH64_RELATIVE,    /* type */
332          0,                     /* rightshift */
333          2,                     /* size (0 = byte, 1 = short, 2 = long) */
334          64,                    /* bitsize */
335          FALSE,                 /* pc_relative */
336          0,                     /* bitpos */
337          complain_overflow_bitfield,    /* complain_on_overflow */
338          bfd_elf_generic_reloc, /* special_function */
339          "R_AARCH64_RELATIVE",  /* name */
340          TRUE,                  /* partial_inplace */
341          ALL_ONES,              /* src_mask */
342          ALL_ONES,              /* dst_mask */
343          FALSE),                /* pcrel_offset */
344
345   HOWTO (R_AARCH64_TLS_DTPMOD64,        /* type */
346          0,                     /* rightshift */
347          2,                     /* size (0 = byte, 1 = short, 2 = long) */
348          64,                    /* bitsize */
349          FALSE,                 /* pc_relative */
350          0,                     /* bitpos */
351          complain_overflow_dont,        /* complain_on_overflow */
352          bfd_elf_generic_reloc, /* special_function */
353          "R_AARCH64_TLS_DTPMOD64",      /* name */
354          FALSE,                 /* partial_inplace */
355          0,                     /* src_mask */
356          ALL_ONES,              /* dst_mask */
357          FALSE),                /* pc_reloffset */
358
359   HOWTO (R_AARCH64_TLS_DTPREL64,        /* type */
360          0,                     /* rightshift */
361          2,                     /* size (0 = byte, 1 = short, 2 = long) */
362          64,                    /* bitsize */
363          FALSE,                 /* pc_relative */
364          0,                     /* bitpos */
365          complain_overflow_dont,        /* complain_on_overflow */
366          bfd_elf_generic_reloc, /* special_function */
367          "R_AARCH64_TLS_DTPREL64",      /* name */
368          FALSE,                 /* partial_inplace */
369          0,                     /* src_mask */
370          ALL_ONES,              /* dst_mask */
371          FALSE),                /* pcrel_offset */
372
373   HOWTO (R_AARCH64_TLS_TPREL64, /* type */
374          0,                     /* rightshift */
375          2,                     /* size (0 = byte, 1 = short, 2 = long) */
376          64,                    /* bitsize */
377          FALSE,                 /* pc_relative */
378          0,                     /* bitpos */
379          complain_overflow_dont,        /* complain_on_overflow */
380          bfd_elf_generic_reloc, /* special_function */
381          "R_AARCH64_TLS_TPREL64",       /* name */
382          FALSE,                 /* partial_inplace */
383          0,                     /* src_mask */
384          ALL_ONES,              /* dst_mask */
385          FALSE),                /* pcrel_offset */
386
387   HOWTO (R_AARCH64_TLSDESC,     /* type */
388          0,                     /* rightshift */
389          2,                     /* size (0 = byte, 1 = short, 2 = long) */
390          64,                    /* bitsize */
391          FALSE,                 /* pc_relative */
392          0,                     /* bitpos */
393          complain_overflow_dont,        /* complain_on_overflow */
394          bfd_elf_generic_reloc, /* special_function */
395          "R_AARCH64_TLSDESC",   /* name */
396          FALSE,                 /* partial_inplace */
397          0,                     /* src_mask */
398          ALL_ONES,              /* dst_mask */
399          FALSE),                /* pcrel_offset */
400
401 };
402
403 /* Note: code such as elf64_aarch64_reloc_type_lookup expect to use e.g.
404    R_AARCH64_PREL64 as an index into this, and find the R_AARCH64_PREL64 HOWTO
405    in that slot.  */
406
407 static reloc_howto_type elf64_aarch64_howto_table[] =
408 {
409   /* Basic data relocations.  */
410
411   HOWTO (R_AARCH64_NULL,        /* type */
412          0,                     /* rightshift */
413          0,                     /* size (0 = byte, 1 = short, 2 = long) */
414          0,                     /* bitsize */
415          FALSE,                 /* pc_relative */
416          0,                     /* bitpos */
417          complain_overflow_dont,        /* complain_on_overflow */
418          bfd_elf_generic_reloc, /* special_function */
419          "R_AARCH64_NULL",      /* name */
420          FALSE,                 /* partial_inplace */
421          0,                     /* src_mask */
422          0,                     /* dst_mask */
423          FALSE),                /* pcrel_offset */
424
425   /* .xword: (S+A) */
426   HOWTO (R_AARCH64_ABS64,       /* type */
427          0,                     /* rightshift */
428          4,                     /* size (4 = long long) */
429          64,                    /* bitsize */
430          FALSE,                 /* pc_relative */
431          0,                     /* bitpos */
432          complain_overflow_unsigned,    /* complain_on_overflow */
433          bfd_elf_generic_reloc, /* special_function */
434          "R_AARCH64_ABS64",     /* name */
435          FALSE,                 /* partial_inplace */
436          ALL_ONES,              /* src_mask */
437          ALL_ONES,              /* dst_mask */
438          FALSE),                /* pcrel_offset */
439
440   /* .word: (S+A) */
441   HOWTO (R_AARCH64_ABS32,       /* type */
442          0,                     /* rightshift */
443          2,                     /* size (0 = byte, 1 = short, 2 = long) */
444          32,                    /* bitsize */
445          FALSE,                 /* pc_relative */
446          0,                     /* bitpos */
447          complain_overflow_unsigned,    /* complain_on_overflow */
448          bfd_elf_generic_reloc, /* special_function */
449          "R_AARCH64_ABS32",     /* name */
450          FALSE,                 /* partial_inplace */
451          0xffffffff,            /* src_mask */
452          0xffffffff,            /* dst_mask */
453          FALSE),                /* pcrel_offset */
454
455   /* .half:  (S+A) */
456   HOWTO (R_AARCH64_ABS16,       /* type */
457          0,                     /* rightshift */
458          1,                     /* size (0 = byte, 1 = short, 2 = long) */
459          16,                    /* bitsize */
460          FALSE,                 /* pc_relative */
461          0,                     /* bitpos */
462          complain_overflow_unsigned,    /* complain_on_overflow */
463          bfd_elf_generic_reloc, /* special_function */
464          "R_AARCH64_ABS16",     /* name */
465          FALSE,                 /* partial_inplace */
466          0xffff,                /* src_mask */
467          0xffff,                /* dst_mask */
468          FALSE),                /* pcrel_offset */
469
470   /* .xword: (S+A-P) */
471   HOWTO (R_AARCH64_PREL64,      /* type */
472          0,                     /* rightshift */
473          4,                     /* size (4 = long long) */
474          64,                    /* bitsize */
475          TRUE,                  /* pc_relative */
476          0,                     /* bitpos */
477          complain_overflow_signed,      /* complain_on_overflow */
478          bfd_elf_generic_reloc, /* special_function */
479          "R_AARCH64_PREL64",    /* name */
480          FALSE,                 /* partial_inplace */
481          ALL_ONES,              /* src_mask */
482          ALL_ONES,              /* dst_mask */
483          TRUE),                 /* pcrel_offset */
484
485   /* .word: (S+A-P) */
486   HOWTO (R_AARCH64_PREL32,      /* type */
487          0,                     /* rightshift */
488          2,                     /* size (0 = byte, 1 = short, 2 = long) */
489          32,                    /* bitsize */
490          TRUE,                  /* pc_relative */
491          0,                     /* bitpos */
492          complain_overflow_signed,      /* complain_on_overflow */
493          bfd_elf_generic_reloc, /* special_function */
494          "R_AARCH64_PREL32",    /* name */
495          FALSE,                 /* partial_inplace */
496          0xffffffff,            /* src_mask */
497          0xffffffff,            /* dst_mask */
498          TRUE),                 /* pcrel_offset */
499
500   /* .half: (S+A-P) */
501   HOWTO (R_AARCH64_PREL16,      /* type */
502          0,                     /* rightshift */
503          1,                     /* size (0 = byte, 1 = short, 2 = long) */
504          16,                    /* bitsize */
505          TRUE,                  /* pc_relative */
506          0,                     /* bitpos */
507          complain_overflow_signed,      /* complain_on_overflow */
508          bfd_elf_generic_reloc, /* special_function */
509          "R_AARCH64_PREL16",    /* name */
510          FALSE,                 /* partial_inplace */
511          0xffff,                /* src_mask */
512          0xffff,                /* dst_mask */
513          TRUE),                 /* pcrel_offset */
514
515   /* Group relocations to create a 16, 32, 48 or 64 bit
516      unsigned data or abs address inline.  */
517
518   /* MOVZ:   ((S+A) >>  0) & 0xffff */
519   HOWTO (R_AARCH64_MOVW_UABS_G0,        /* type */
520          0,                     /* rightshift */
521          2,                     /* size (0 = byte, 1 = short, 2 = long) */
522          16,                    /* bitsize */
523          FALSE,                 /* pc_relative */
524          0,                     /* bitpos */
525          complain_overflow_unsigned,    /* complain_on_overflow */
526          bfd_elf_generic_reloc, /* special_function */
527          "R_AARCH64_MOVW_UABS_G0",      /* name */
528          FALSE,                 /* partial_inplace */
529          0xffff,                /* src_mask */
530          0xffff,                /* dst_mask */
531          FALSE),                /* pcrel_offset */
532
533   /* MOVK:   ((S+A) >>  0) & 0xffff [no overflow check] */
534   HOWTO (R_AARCH64_MOVW_UABS_G0_NC,     /* type */
535          0,                     /* rightshift */
536          2,                     /* size (0 = byte, 1 = short, 2 = long) */
537          16,                    /* bitsize */
538          FALSE,                 /* pc_relative */
539          0,                     /* bitpos */
540          complain_overflow_dont,        /* complain_on_overflow */
541          bfd_elf_generic_reloc, /* special_function */
542          "R_AARCH64_MOVW_UABS_G0_NC",   /* name */
543          FALSE,                 /* partial_inplace */
544          0xffff,                /* src_mask */
545          0xffff,                /* dst_mask */
546          FALSE),                /* pcrel_offset */
547
548   /* MOVZ:   ((S+A) >> 16) & 0xffff */
549   HOWTO (R_AARCH64_MOVW_UABS_G1,        /* type */
550          16,                    /* rightshift */
551          2,                     /* size (0 = byte, 1 = short, 2 = long) */
552          16,                    /* bitsize */
553          FALSE,                 /* pc_relative */
554          0,                     /* bitpos */
555          complain_overflow_unsigned,    /* complain_on_overflow */
556          bfd_elf_generic_reloc, /* special_function */
557          "R_AARCH64_MOVW_UABS_G1",      /* name */
558          FALSE,                 /* partial_inplace */
559          0xffff,                /* src_mask */
560          0xffff,                /* dst_mask */
561          FALSE),                /* pcrel_offset */
562
563   /* MOVK:   ((S+A) >> 16) & 0xffff [no overflow check] */
564   HOWTO (R_AARCH64_MOVW_UABS_G1_NC,     /* type */
565          16,                    /* rightshift */
566          2,                     /* size (0 = byte, 1 = short, 2 = long) */
567          16,                    /* bitsize */
568          FALSE,                 /* pc_relative */
569          0,                     /* bitpos */
570          complain_overflow_dont,        /* complain_on_overflow */
571          bfd_elf_generic_reloc, /* special_function */
572          "R_AARCH64_MOVW_UABS_G1_NC",   /* name */
573          FALSE,                 /* partial_inplace */
574          0xffff,                /* src_mask */
575          0xffff,                /* dst_mask */
576          FALSE),                /* pcrel_offset */
577
578   /* MOVZ:   ((S+A) >> 32) & 0xffff */
579   HOWTO (R_AARCH64_MOVW_UABS_G2,        /* type */
580          32,                    /* rightshift */
581          2,                     /* size (0 = byte, 1 = short, 2 = long) */
582          16,                    /* bitsize */
583          FALSE,                 /* pc_relative */
584          0,                     /* bitpos */
585          complain_overflow_unsigned,    /* complain_on_overflow */
586          bfd_elf_generic_reloc, /* special_function */
587          "R_AARCH64_MOVW_UABS_G2",      /* name */
588          FALSE,                 /* partial_inplace */
589          0xffff,                /* src_mask */
590          0xffff,                /* dst_mask */
591          FALSE),                /* pcrel_offset */
592
593   /* MOVK:   ((S+A) >> 32) & 0xffff [no overflow check] */
594   HOWTO (R_AARCH64_MOVW_UABS_G2_NC,     /* type */
595          32,                    /* rightshift */
596          2,                     /* size (0 = byte, 1 = short, 2 = long) */
597          16,                    /* bitsize */
598          FALSE,                 /* pc_relative */
599          0,                     /* bitpos */
600          complain_overflow_dont,        /* complain_on_overflow */
601          bfd_elf_generic_reloc, /* special_function */
602          "R_AARCH64_MOVW_UABS_G2_NC",   /* name */
603          FALSE,                 /* partial_inplace */
604          0xffff,                /* src_mask */
605          0xffff,                /* dst_mask */
606          FALSE),                /* pcrel_offset */
607
608   /* MOVZ:   ((S+A) >> 48) & 0xffff */
609   HOWTO (R_AARCH64_MOVW_UABS_G3,        /* type */
610          48,                    /* rightshift */
611          2,                     /* size (0 = byte, 1 = short, 2 = long) */
612          16,                    /* bitsize */
613          FALSE,                 /* pc_relative */
614          0,                     /* bitpos */
615          complain_overflow_unsigned,    /* complain_on_overflow */
616          bfd_elf_generic_reloc, /* special_function */
617          "R_AARCH64_MOVW_UABS_G3",      /* name */
618          FALSE,                 /* partial_inplace */
619          0xffff,                /* src_mask */
620          0xffff,                /* dst_mask */
621          FALSE),                /* pcrel_offset */
622
623   /* Group relocations to create high part of a 16, 32, 48 or 64 bit
624      signed data or abs address inline. Will change instruction
625      to MOVN or MOVZ depending on sign of calculated value.  */
626
627   /* MOV[ZN]:   ((S+A) >>  0) & 0xffff */
628   HOWTO (R_AARCH64_MOVW_SABS_G0,        /* type */
629          0,                     /* rightshift */
630          2,                     /* size (0 = byte, 1 = short, 2 = long) */
631          16,                    /* bitsize */
632          FALSE,                 /* pc_relative */
633          0,                     /* bitpos */
634          complain_overflow_signed,      /* complain_on_overflow */
635          bfd_elf_generic_reloc, /* special_function */
636          "R_AARCH64_MOVW_SABS_G0",      /* name */
637          FALSE,                 /* partial_inplace */
638          0xffff,                /* src_mask */
639          0xffff,                /* dst_mask */
640          FALSE),                /* pcrel_offset */
641
642   /* MOV[ZN]:   ((S+A) >> 16) & 0xffff */
643   HOWTO (R_AARCH64_MOVW_SABS_G1,        /* type */
644          16,                    /* rightshift */
645          2,                     /* size (0 = byte, 1 = short, 2 = long) */
646          16,                    /* bitsize */
647          FALSE,                 /* pc_relative */
648          0,                     /* bitpos */
649          complain_overflow_signed,      /* complain_on_overflow */
650          bfd_elf_generic_reloc, /* special_function */
651          "R_AARCH64_MOVW_SABS_G1",      /* name */
652          FALSE,                 /* partial_inplace */
653          0xffff,                /* src_mask */
654          0xffff,                /* dst_mask */
655          FALSE),                /* pcrel_offset */
656
657   /* MOV[ZN]:   ((S+A) >> 32) & 0xffff */
658   HOWTO (R_AARCH64_MOVW_SABS_G2,        /* type */
659          32,                    /* rightshift */
660          2,                     /* size (0 = byte, 1 = short, 2 = long) */
661          16,                    /* bitsize */
662          FALSE,                 /* pc_relative */
663          0,                     /* bitpos */
664          complain_overflow_signed,      /* complain_on_overflow */
665          bfd_elf_generic_reloc, /* special_function */
666          "R_AARCH64_MOVW_SABS_G2",      /* name */
667          FALSE,                 /* partial_inplace */
668          0xffff,                /* src_mask */
669          0xffff,                /* dst_mask */
670          FALSE),                /* pcrel_offset */
671
672 /* Relocations to generate 19, 21 and 33 bit PC-relative load/store
673    addresses: PG(x) is (x & ~0xfff).  */
674
675   /* LD-lit: ((S+A-P) >> 2) & 0x7ffff */
676   HOWTO (R_AARCH64_LD_PREL_LO19,        /* type */
677          2,                     /* rightshift */
678          2,                     /* size (0 = byte, 1 = short, 2 = long) */
679          19,                    /* bitsize */
680          TRUE,                  /* pc_relative */
681          0,                     /* bitpos */
682          complain_overflow_signed,      /* complain_on_overflow */
683          bfd_elf_generic_reloc, /* special_function */
684          "R_AARCH64_LD_PREL_LO19",      /* name */
685          FALSE,                 /* partial_inplace */
686          0x7ffff,               /* src_mask */
687          0x7ffff,               /* dst_mask */
688          TRUE),                 /* pcrel_offset */
689
690   /* ADR:    (S+A-P) & 0x1fffff */
691   HOWTO (R_AARCH64_ADR_PREL_LO21,       /* type */
692          0,                     /* rightshift */
693          2,                     /* size (0 = byte, 1 = short, 2 = long) */
694          21,                    /* bitsize */
695          TRUE,                  /* pc_relative */
696          0,                     /* bitpos */
697          complain_overflow_signed,      /* complain_on_overflow */
698          bfd_elf_generic_reloc, /* special_function */
699          "R_AARCH64_ADR_PREL_LO21",     /* name */
700          FALSE,                 /* partial_inplace */
701          0x1fffff,              /* src_mask */
702          0x1fffff,              /* dst_mask */
703          TRUE),                 /* pcrel_offset */
704
705   /* ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
706   HOWTO (R_AARCH64_ADR_PREL_PG_HI21,    /* type */
707          12,                    /* rightshift */
708          2,                     /* size (0 = byte, 1 = short, 2 = long) */
709          21,                    /* bitsize */
710          TRUE,                  /* pc_relative */
711          0,                     /* bitpos */
712          complain_overflow_signed,      /* complain_on_overflow */
713          bfd_elf_generic_reloc, /* special_function */
714          "R_AARCH64_ADR_PREL_PG_HI21",  /* name */
715          FALSE,                 /* partial_inplace */
716          0x1fffff,              /* src_mask */
717          0x1fffff,              /* dst_mask */
718          TRUE),                 /* pcrel_offset */
719
720   /* ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff [no overflow check] */
721   HOWTO (R_AARCH64_ADR_PREL_PG_HI21_NC, /* type */
722          12,                    /* rightshift */
723          2,                     /* size (0 = byte, 1 = short, 2 = long) */
724          21,                    /* bitsize */
725          TRUE,                  /* pc_relative */
726          0,                     /* bitpos */
727          complain_overflow_dont,        /* complain_on_overflow */
728          bfd_elf_generic_reloc, /* special_function */
729          "R_AARCH64_ADR_PREL_PG_HI21_NC",       /* name */
730          FALSE,                 /* partial_inplace */
731          0x1fffff,              /* src_mask */
732          0x1fffff,              /* dst_mask */
733          TRUE),                 /* pcrel_offset */
734
735   /* ADD:    (S+A) & 0xfff [no overflow check] */
736   HOWTO (R_AARCH64_ADD_ABS_LO12_NC,     /* type */
737          0,                     /* rightshift */
738          2,                     /* size (0 = byte, 1 = short, 2 = long) */
739          12,                    /* bitsize */
740          FALSE,                 /* pc_relative */
741          10,                    /* bitpos */
742          complain_overflow_dont,        /* complain_on_overflow */
743          bfd_elf_generic_reloc, /* special_function */
744          "R_AARCH64_ADD_ABS_LO12_NC",   /* name */
745          FALSE,                 /* partial_inplace */
746          0x3ffc00,              /* src_mask */
747          0x3ffc00,              /* dst_mask */
748          FALSE),                /* pcrel_offset */
749
750   /* LD/ST8:  (S+A) & 0xfff */
751   HOWTO (R_AARCH64_LDST8_ABS_LO12_NC,   /* type */
752          0,                     /* rightshift */
753          2,                     /* size (0 = byte, 1 = short, 2 = long) */
754          12,                    /* bitsize */
755          FALSE,                 /* pc_relative */
756          0,                     /* bitpos */
757          complain_overflow_dont,        /* complain_on_overflow */
758          bfd_elf_generic_reloc, /* special_function */
759          "R_AARCH64_LDST8_ABS_LO12_NC", /* name */
760          FALSE,                 /* partial_inplace */
761          0xfff,                 /* src_mask */
762          0xfff,                 /* dst_mask */
763          FALSE),                /* pcrel_offset */
764
765   /* Relocations for control-flow instructions.  */
766
767   /* TBZ/NZ: ((S+A-P) >> 2) & 0x3fff */
768   HOWTO (R_AARCH64_TSTBR14,     /* type */
769          2,                     /* rightshift */
770          2,                     /* size (0 = byte, 1 = short, 2 = long) */
771          14,                    /* bitsize */
772          TRUE,                  /* pc_relative */
773          0,                     /* bitpos */
774          complain_overflow_signed,      /* complain_on_overflow */
775          bfd_elf_generic_reloc, /* special_function */
776          "R_AARCH64_TSTBR14",   /* name */
777          FALSE,                 /* partial_inplace */
778          0x3fff,                /* src_mask */
779          0x3fff,                /* dst_mask */
780          TRUE),                 /* pcrel_offset */
781
782   /* B.cond: ((S+A-P) >> 2) & 0x7ffff */
783   HOWTO (R_AARCH64_CONDBR19,    /* type */
784          2,                     /* rightshift */
785          2,                     /* size (0 = byte, 1 = short, 2 = long) */
786          19,                    /* bitsize */
787          TRUE,                  /* pc_relative */
788          0,                     /* bitpos */
789          complain_overflow_signed,      /* complain_on_overflow */
790          bfd_elf_generic_reloc, /* special_function */
791          "R_AARCH64_CONDBR19",  /* name */
792          FALSE,                 /* partial_inplace */
793          0x7ffff,               /* src_mask */
794          0x7ffff,               /* dst_mask */
795          TRUE),                 /* pcrel_offset */
796
797   EMPTY_HOWTO (281),
798
799   /* B:      ((S+A-P) >> 2) & 0x3ffffff */
800   HOWTO (R_AARCH64_JUMP26,      /* type */
801          2,                     /* rightshift */
802          2,                     /* size (0 = byte, 1 = short, 2 = long) */
803          26,                    /* bitsize */
804          TRUE,                  /* pc_relative */
805          0,                     /* bitpos */
806          complain_overflow_signed,      /* complain_on_overflow */
807          bfd_elf_generic_reloc, /* special_function */
808          "R_AARCH64_JUMP26",    /* name */
809          FALSE,                 /* partial_inplace */
810          0x3ffffff,             /* src_mask */
811          0x3ffffff,             /* dst_mask */
812          TRUE),                 /* pcrel_offset */
813
814   /* BL:     ((S+A-P) >> 2) & 0x3ffffff */
815   HOWTO (R_AARCH64_CALL26,      /* type */
816          2,                     /* rightshift */
817          2,                     /* size (0 = byte, 1 = short, 2 = long) */
818          26,                    /* bitsize */
819          TRUE,                  /* pc_relative */
820          0,                     /* bitpos */
821          complain_overflow_signed,      /* complain_on_overflow */
822          bfd_elf_generic_reloc, /* special_function */
823          "R_AARCH64_CALL26",    /* name */
824          FALSE,                 /* partial_inplace */
825          0x3ffffff,             /* src_mask */
826          0x3ffffff,             /* dst_mask */
827          TRUE),                 /* pcrel_offset */
828
829   /* LD/ST16:  (S+A) & 0xffe */
830   HOWTO (R_AARCH64_LDST16_ABS_LO12_NC,  /* type */
831          1,                     /* rightshift */
832          2,                     /* size (0 = byte, 1 = short, 2 = long) */
833          12,                    /* bitsize */
834          FALSE,                 /* pc_relative */
835          0,                     /* bitpos */
836          complain_overflow_dont,        /* complain_on_overflow */
837          bfd_elf_generic_reloc, /* special_function */
838          "R_AARCH64_LDST16_ABS_LO12_NC",        /* name */
839          FALSE,                 /* partial_inplace */
840          0xffe,                 /* src_mask */
841          0xffe,                 /* dst_mask */
842          FALSE),                /* pcrel_offset */
843
844   /* LD/ST32:  (S+A) & 0xffc */
845   HOWTO (R_AARCH64_LDST32_ABS_LO12_NC,  /* type */
846          2,                     /* rightshift */
847          2,                     /* size (0 = byte, 1 = short, 2 = long) */
848          12,                    /* bitsize */
849          FALSE,                 /* pc_relative */
850          0,                     /* bitpos */
851          complain_overflow_dont,        /* complain_on_overflow */
852          bfd_elf_generic_reloc, /* special_function */
853          "R_AARCH64_LDST32_ABS_LO12_NC",        /* name */
854          FALSE,                 /* partial_inplace */
855          0xffc,                 /* src_mask */
856          0xffc,                 /* dst_mask */
857          FALSE),                /* pcrel_offset */
858
859   /* LD/ST64:  (S+A) & 0xff8 */
860   HOWTO (R_AARCH64_LDST64_ABS_LO12_NC,  /* type */
861          3,                     /* rightshift */
862          2,                     /* size (0 = byte, 1 = short, 2 = long) */
863          12,                    /* bitsize */
864          FALSE,                 /* pc_relative */
865          0,                     /* bitpos */
866          complain_overflow_dont,        /* complain_on_overflow */
867          bfd_elf_generic_reloc, /* special_function */
868          "R_AARCH64_LDST64_ABS_LO12_NC",        /* name */
869          FALSE,                 /* partial_inplace */
870          0xff8,                 /* src_mask */
871          0xff8,                 /* dst_mask */
872          FALSE),                /* pcrel_offset */
873
874   EMPTY_HOWTO (287),
875   EMPTY_HOWTO (288),
876   EMPTY_HOWTO (289),
877   EMPTY_HOWTO (290),
878   EMPTY_HOWTO (291),
879   EMPTY_HOWTO (292),
880   EMPTY_HOWTO (293),
881   EMPTY_HOWTO (294),
882   EMPTY_HOWTO (295),
883   EMPTY_HOWTO (296),
884   EMPTY_HOWTO (297),
885   EMPTY_HOWTO (298),
886
887   /* LD/ST128:  (S+A) & 0xff0 */
888   HOWTO (R_AARCH64_LDST128_ABS_LO12_NC, /* type */
889          4,                     /* rightshift */
890          2,                     /* size (0 = byte, 1 = short, 2 = long) */
891          12,                    /* bitsize */
892          FALSE,                 /* pc_relative */
893          0,                     /* bitpos */
894          complain_overflow_dont,        /* complain_on_overflow */
895          bfd_elf_generic_reloc, /* special_function */
896          "R_AARCH64_LDST128_ABS_LO12_NC",       /* name */
897          FALSE,                 /* partial_inplace */
898          0xff0,                 /* src_mask */
899          0xff0,                 /* dst_mask */
900          FALSE),                /* pcrel_offset */
901
902   EMPTY_HOWTO (300),
903   EMPTY_HOWTO (301),
904   EMPTY_HOWTO (302),
905   EMPTY_HOWTO (303),
906   EMPTY_HOWTO (304),
907   EMPTY_HOWTO (305),
908   EMPTY_HOWTO (306),
909   EMPTY_HOWTO (307),
910   EMPTY_HOWTO (308),
911
912   /* Set a load-literal immediate field to bits
913      0x1FFFFC of G(S)-P */
914   HOWTO (R_AARCH64_GOT_LD_PREL19,       /* type */
915          2,                             /* rightshift */
916          2,                             /* size (0 = byte,1 = short,2 = long) */
917          19,                            /* bitsize */
918          TRUE,                          /* pc_relative */
919          0,                             /* bitpos */
920          complain_overflow_signed,      /* complain_on_overflow */
921          bfd_elf_generic_reloc,         /* special_function */
922          "R_AARCH64_GOT_LD_PREL19",     /* name */
923          FALSE,                         /* partial_inplace */
924          0xffffe0,                      /* src_mask */
925          0xffffe0,                      /* dst_mask */
926          TRUE),                         /* pcrel_offset */
927
928   EMPTY_HOWTO (310),
929
930   /* Get to the page for the GOT entry for the symbol
931      (G(S) - P) using an ADRP instruction.  */
932   HOWTO (R_AARCH64_ADR_GOT_PAGE,        /* type */
933          12,                    /* rightshift */
934          2,                     /* size (0 = byte, 1 = short, 2 = long) */
935          21,                    /* bitsize */
936          TRUE,                  /* pc_relative */
937          0,                     /* bitpos */
938          complain_overflow_dont,        /* complain_on_overflow */
939          bfd_elf_generic_reloc, /* special_function */
940          "R_AARCH64_ADR_GOT_PAGE",      /* name */
941          FALSE,                 /* partial_inplace */
942          0x1fffff,              /* src_mask */
943          0x1fffff,              /* dst_mask */
944          TRUE),                 /* pcrel_offset */
945
946   /* LD64: GOT offset G(S) & 0xff8 */
947   HOWTO (R_AARCH64_LD64_GOT_LO12_NC,    /* type */
948          3,                     /* rightshift */
949          2,                     /* size (0 = byte, 1 = short, 2 = long) */
950          12,                    /* bitsize */
951          FALSE,                 /* pc_relative */
952          0,                     /* bitpos */
953          complain_overflow_dont,        /* complain_on_overflow */
954          bfd_elf_generic_reloc, /* special_function */
955          "R_AARCH64_LD64_GOT_LO12_NC",  /* name */
956          FALSE,                 /* partial_inplace */
957          0xff8,                 /* src_mask */
958          0xff8,                 /* dst_mask */
959          FALSE)                 /* pcrel_offset */
960 };
961
962 static reloc_howto_type elf64_aarch64_tls_howto_table[] =
963 {
964   EMPTY_HOWTO (512),
965
966   /* Get to the page for the GOT entry for the symbol
967      (G(S) - P) using an ADRP instruction.  */
968   HOWTO (R_AARCH64_TLSGD_ADR_PAGE21,    /* type */
969          12,                    /* rightshift */
970          2,                     /* size (0 = byte, 1 = short, 2 = long) */
971          21,                    /* bitsize */
972          TRUE,                  /* pc_relative */
973          0,                     /* bitpos */
974          complain_overflow_dont,        /* complain_on_overflow */
975          bfd_elf_generic_reloc, /* special_function */
976          "R_AARCH64_TLSGD_ADR_PAGE21",  /* name */
977          FALSE,                 /* partial_inplace */
978          0x1fffff,              /* src_mask */
979          0x1fffff,              /* dst_mask */
980          TRUE),                 /* pcrel_offset */
981
982   /* ADD: GOT offset G(S) & 0xff8 [no overflow check] */
983   HOWTO (R_AARCH64_TLSGD_ADD_LO12_NC,   /* type */
984          0,                     /* rightshift */
985          2,                     /* size (0 = byte, 1 = short, 2 = long) */
986          12,                    /* bitsize */
987          FALSE,                 /* pc_relative */
988          0,                     /* bitpos */
989          complain_overflow_dont,        /* complain_on_overflow */
990          bfd_elf_generic_reloc, /* special_function */
991          "R_AARCH64_TLSGD_ADD_LO12_NC", /* name */
992          FALSE,                 /* partial_inplace */
993          0xfff,                 /* src_mask */
994          0xfff,                 /* dst_mask */
995          FALSE),                /* pcrel_offset */
996
997   EMPTY_HOWTO (515),
998   EMPTY_HOWTO (516),
999   EMPTY_HOWTO (517),
1000   EMPTY_HOWTO (518),
1001   EMPTY_HOWTO (519),
1002   EMPTY_HOWTO (520),
1003   EMPTY_HOWTO (521),
1004   EMPTY_HOWTO (522),
1005   EMPTY_HOWTO (523),
1006   EMPTY_HOWTO (524),
1007   EMPTY_HOWTO (525),
1008   EMPTY_HOWTO (526),
1009   EMPTY_HOWTO (527),
1010   EMPTY_HOWTO (528),
1011   EMPTY_HOWTO (529),
1012   EMPTY_HOWTO (530),
1013   EMPTY_HOWTO (531),
1014   EMPTY_HOWTO (532),
1015   EMPTY_HOWTO (533),
1016   EMPTY_HOWTO (534),
1017   EMPTY_HOWTO (535),
1018   EMPTY_HOWTO (536),
1019   EMPTY_HOWTO (537),
1020   EMPTY_HOWTO (538),
1021
1022   HOWTO (R_AARCH64_TLSIE_MOVW_GOTTPREL_G1,      /* type */
1023          16,                    /* rightshift */
1024          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1025          16,                    /* bitsize */
1026          FALSE,                 /* pc_relative */
1027          0,                     /* bitpos */
1028          complain_overflow_dont,        /* complain_on_overflow */
1029          bfd_elf_generic_reloc, /* special_function */
1030          "R_AARCH64_TLSIE_MOVW_GOTTPREL_G1",    /* name */
1031          FALSE,                 /* partial_inplace */
1032          0xffff,                /* src_mask */
1033          0xffff,                /* dst_mask */
1034          FALSE),                /* pcrel_offset */
1035
1036   HOWTO (R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC,   /* type */
1037          0,                     /* rightshift */
1038          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1039          32,                    /* bitsize */
1040          FALSE,                 /* pc_relative */
1041          0,                     /* bitpos */
1042          complain_overflow_dont,        /* complain_on_overflow */
1043          bfd_elf_generic_reloc, /* special_function */
1044          "R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC", /* name */
1045          FALSE,                 /* partial_inplace */
1046          0xffff,                /* src_mask */
1047          0xffff,                /* dst_mask */
1048          FALSE),                /* pcrel_offset */
1049
1050   HOWTO (R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21,   /* type */
1051          12,                    /* rightshift */
1052          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1053          21,                    /* bitsize */
1054          FALSE,                 /* pc_relative */
1055          0,                     /* bitpos */
1056          complain_overflow_dont,        /* complain_on_overflow */
1057          bfd_elf_generic_reloc, /* special_function */
1058          "R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21", /* name */
1059          FALSE,                 /* partial_inplace */
1060          0x1fffff,              /* src_mask */
1061          0x1fffff,              /* dst_mask */
1062          FALSE),                /* pcrel_offset */
1063
1064   HOWTO (R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, /* type */
1065          3,                     /* rightshift */
1066          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1067          12,                    /* bitsize */
1068          FALSE,                 /* pc_relative */
1069          0,                     /* bitpos */
1070          complain_overflow_dont,        /* complain_on_overflow */
1071          bfd_elf_generic_reloc, /* special_function */
1072          "R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC",       /* name */
1073          FALSE,                 /* partial_inplace */
1074          0xff8,                 /* src_mask */
1075          0xff8,                 /* dst_mask */
1076          FALSE),                /* pcrel_offset */
1077
1078   HOWTO (R_AARCH64_TLSIE_LD_GOTTPREL_PREL19,    /* type */
1079          2,                     /* rightshift */
1080          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1081          21,                    /* bitsize */
1082          FALSE,                 /* pc_relative */
1083          0,                     /* bitpos */
1084          complain_overflow_dont,        /* complain_on_overflow */
1085          bfd_elf_generic_reloc, /* special_function */
1086          "R_AARCH64_TLSIE_LD_GOTTPREL_PREL19",  /* name */
1087          FALSE,                 /* partial_inplace */
1088          0x1ffffc,              /* src_mask */
1089          0x1ffffc,              /* dst_mask */
1090          FALSE),                /* pcrel_offset */
1091
1092   HOWTO (R_AARCH64_TLSLE_MOVW_TPREL_G2, /* type */
1093          32,                    /* rightshift */
1094          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1095          12,                    /* bitsize */
1096          FALSE,                 /* pc_relative */
1097          0,                     /* bitpos */
1098          complain_overflow_dont,        /* complain_on_overflow */
1099          bfd_elf_generic_reloc, /* special_function */
1100          "R_AARCH64_TLSLE_MOVW_TPREL_G2",       /* name */
1101          FALSE,                 /* partial_inplace */
1102          0xffff,                /* src_mask */
1103          0xffff,                /* dst_mask */
1104          FALSE),                /* pcrel_offset */
1105
1106   HOWTO (R_AARCH64_TLSLE_MOVW_TPREL_G1, /* type */
1107          16,                    /* rightshift */
1108          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1109          12,                    /* bitsize */
1110          FALSE,                 /* pc_relative */
1111          0,                     /* bitpos */
1112          complain_overflow_dont,        /* complain_on_overflow */
1113          bfd_elf_generic_reloc, /* special_function */
1114          "R_AARCH64_TLSLE_MOVW_TPREL_G1",       /* name */
1115          FALSE,                 /* partial_inplace */
1116          0xffff,                /* src_mask */
1117          0xffff,                /* dst_mask */
1118          FALSE),                /* pcrel_offset */
1119
1120   HOWTO (R_AARCH64_TLSLE_MOVW_TPREL_G1_NC,      /* type */
1121          16,                    /* rightshift */
1122          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1123          12,                    /* bitsize */
1124          FALSE,                 /* pc_relative */
1125          0,                     /* bitpos */
1126          complain_overflow_dont,        /* complain_on_overflow */
1127          bfd_elf_generic_reloc, /* special_function */
1128          "R_AARCH64_TLSLE_MOVW_TPREL_G1_NC",    /* name */
1129          FALSE,                 /* partial_inplace */
1130          0xffff,                /* src_mask */
1131          0xffff,                /* dst_mask */
1132          FALSE),                /* pcrel_offset */
1133
1134   HOWTO (R_AARCH64_TLSLE_MOVW_TPREL_G0, /* type */
1135          0,                     /* rightshift */
1136          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1137          12,                    /* bitsize */
1138          FALSE,                 /* pc_relative */
1139          0,                     /* bitpos */
1140          complain_overflow_dont,        /* complain_on_overflow */
1141          bfd_elf_generic_reloc, /* special_function */
1142          "R_AARCH64_TLSLE_MOVW_TPREL_G0",       /* name */
1143          FALSE,                 /* partial_inplace */
1144          0xffff,                /* src_mask */
1145          0xffff,                /* dst_mask */
1146          FALSE),                /* pcrel_offset */
1147
1148   HOWTO (R_AARCH64_TLSLE_MOVW_TPREL_G0_NC,      /* type */
1149          0,                     /* rightshift */
1150          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1151          12,                    /* bitsize */
1152          FALSE,                 /* pc_relative */
1153          0,                     /* bitpos */
1154          complain_overflow_dont,        /* complain_on_overflow */
1155          bfd_elf_generic_reloc, /* special_function */
1156          "R_AARCH64_TLSLE_MOVW_TPREL_G0_NC",    /* name */
1157          FALSE,                 /* partial_inplace */
1158          0xffff,                /* src_mask */
1159          0xffff,                /* dst_mask */
1160          FALSE),                /* pcrel_offset */
1161
1162   HOWTO (R_AARCH64_TLSLE_ADD_TPREL_HI12,        /* type */
1163          12,                    /* rightshift */
1164          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1165          12,                    /* bitsize */
1166          FALSE,                 /* pc_relative */
1167          0,                     /* bitpos */
1168          complain_overflow_dont,        /* complain_on_overflow */
1169          bfd_elf_generic_reloc, /* special_function */
1170          "R_AARCH64_TLSLE_ADD_TPREL_HI12",      /* name */
1171          FALSE,                 /* partial_inplace */
1172          0xfff,                 /* src_mask */
1173          0xfff,                 /* dst_mask */
1174          FALSE),                /* pcrel_offset */
1175
1176   HOWTO (R_AARCH64_TLSLE_ADD_TPREL_LO12,        /* type */
1177          0,                     /* rightshift */
1178          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1179          12,                    /* bitsize */
1180          FALSE,                 /* pc_relative */
1181          0,                     /* bitpos */
1182          complain_overflow_dont,        /* complain_on_overflow */
1183          bfd_elf_generic_reloc, /* special_function */
1184          "R_AARCH64_TLSLE_ADD_TPREL_LO12",      /* name */
1185          FALSE,                 /* partial_inplace */
1186          0xfff,                 /* src_mask */
1187          0xfff,                 /* dst_mask */
1188          FALSE),                /* pcrel_offset */
1189
1190   HOWTO (R_AARCH64_TLSLE_ADD_TPREL_LO12_NC,     /* type */
1191          0,                     /* rightshift */
1192          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1193          12,                    /* bitsize */
1194          FALSE,                 /* pc_relative */
1195          0,                     /* bitpos */
1196          complain_overflow_dont,        /* complain_on_overflow */
1197          bfd_elf_generic_reloc, /* special_function */
1198          "R_AARCH64_TLSLE_ADD_TPREL_LO12_NC",   /* name */
1199          FALSE,                 /* partial_inplace */
1200          0xfff,                 /* src_mask */
1201          0xfff,                 /* dst_mask */
1202          FALSE),                /* pcrel_offset */
1203 };
1204
1205 static reloc_howto_type elf64_aarch64_tlsdesc_howto_table[] =
1206 {
1207   HOWTO (R_AARCH64_TLSDESC_LD64_PREL19, /* type */
1208          2,                     /* rightshift */
1209          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1210          21,                    /* bitsize */
1211          TRUE,                  /* pc_relative */
1212          0,                     /* bitpos */
1213          complain_overflow_dont,        /* complain_on_overflow */
1214          bfd_elf_generic_reloc, /* special_function */
1215          "R_AARCH64_TLSDESC_LD64_PREL19",       /* name */
1216          FALSE,                 /* partial_inplace */
1217          0x1ffffc,              /* src_mask */
1218          0x1ffffc,              /* dst_mask */
1219          TRUE),                 /* pcrel_offset */
1220
1221   HOWTO (R_AARCH64_TLSDESC_ADR_PREL21,  /* type */
1222          0,                     /* rightshift */
1223          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1224          21,                    /* bitsize */
1225          TRUE,                  /* pc_relative */
1226          0,                     /* bitpos */
1227          complain_overflow_dont,        /* complain_on_overflow */
1228          bfd_elf_generic_reloc, /* special_function */
1229          "R_AARCH64_TLSDESC_ADR_PREL21",        /* name */
1230          FALSE,                 /* partial_inplace */
1231          0x1fffff,              /* src_mask */
1232          0x1fffff,              /* dst_mask */
1233          TRUE),                 /* pcrel_offset */
1234
1235   /* Get to the page for the GOT entry for the symbol
1236      (G(S) - P) using an ADRP instruction.  */
1237   HOWTO (R_AARCH64_TLSDESC_ADR_PAGE,    /* type */
1238          12,                    /* rightshift */
1239          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1240          21,                    /* bitsize */
1241          TRUE,                  /* pc_relative */
1242          0,                     /* bitpos */
1243          complain_overflow_dont,        /* complain_on_overflow */
1244          bfd_elf_generic_reloc, /* special_function */
1245          "R_AARCH64_TLSDESC_ADR_PAGE",  /* name */
1246          FALSE,                 /* partial_inplace */
1247          0x1fffff,              /* src_mask */
1248          0x1fffff,              /* dst_mask */
1249          TRUE),                 /* pcrel_offset */
1250
1251   /* LD64: GOT offset G(S) & 0xfff.  */
1252   HOWTO (R_AARCH64_TLSDESC_LD64_LO12_NC,        /* type */
1253          3,                     /* rightshift */
1254          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1255          12,                    /* bitsize */
1256          FALSE,                 /* pc_relative */
1257          0,                     /* bitpos */
1258          complain_overflow_dont,        /* complain_on_overflow */
1259          bfd_elf_generic_reloc, /* special_function */
1260          "R_AARCH64_TLSDESC_LD64_LO12_NC",      /* name */
1261          FALSE,                 /* partial_inplace */
1262          0xfff,                 /* src_mask */
1263          0xfff,                 /* dst_mask */
1264          FALSE),                /* pcrel_offset */
1265
1266   /* ADD: GOT offset G(S) & 0xfff.  */
1267   HOWTO (R_AARCH64_TLSDESC_ADD_LO12_NC, /* type */
1268          0,                     /* rightshift */
1269          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1270          12,                    /* bitsize */
1271          FALSE,                 /* pc_relative */
1272          0,                     /* bitpos */
1273          complain_overflow_dont,        /* complain_on_overflow */
1274          bfd_elf_generic_reloc, /* special_function */
1275          "R_AARCH64_TLSDESC_ADD_LO12_NC",       /* name */
1276          FALSE,                 /* partial_inplace */
1277          0xfff,                 /* src_mask */
1278          0xfff,                 /* dst_mask */
1279          FALSE),                /* pcrel_offset */
1280
1281   HOWTO (R_AARCH64_TLSDESC_OFF_G1,      /* type */
1282          16,                    /* rightshift */
1283          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1284          12,                    /* bitsize */
1285          FALSE,                 /* pc_relative */
1286          0,                     /* bitpos */
1287          complain_overflow_dont,        /* complain_on_overflow */
1288          bfd_elf_generic_reloc, /* special_function */
1289          "R_AARCH64_TLSDESC_OFF_G1",    /* name */
1290          FALSE,                 /* partial_inplace */
1291          0xffff,                /* src_mask */
1292          0xffff,                /* dst_mask */
1293          FALSE),                /* pcrel_offset */
1294
1295   HOWTO (R_AARCH64_TLSDESC_OFF_G0_NC,   /* type */
1296          0,                     /* rightshift */
1297          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1298          12,                    /* bitsize */
1299          FALSE,                 /* pc_relative */
1300          0,                     /* bitpos */
1301          complain_overflow_dont,        /* complain_on_overflow */
1302          bfd_elf_generic_reloc, /* special_function */
1303          "R_AARCH64_TLSDESC_OFF_G0_NC", /* name */
1304          FALSE,                 /* partial_inplace */
1305          0xffff,                /* src_mask */
1306          0xffff,                /* dst_mask */
1307          FALSE),                /* pcrel_offset */
1308
1309   HOWTO (R_AARCH64_TLSDESC_LDR, /* type */
1310          0,                     /* rightshift */
1311          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1312          12,                    /* bitsize */
1313          FALSE,                 /* pc_relative */
1314          0,                     /* bitpos */
1315          complain_overflow_dont,        /* complain_on_overflow */
1316          bfd_elf_generic_reloc, /* special_function */
1317          "R_AARCH64_TLSDESC_LDR",       /* name */
1318          FALSE,                 /* partial_inplace */
1319          0x0,                   /* src_mask */
1320          0x0,                   /* dst_mask */
1321          FALSE),                /* pcrel_offset */
1322
1323   HOWTO (R_AARCH64_TLSDESC_ADD, /* type */
1324          0,                     /* rightshift */
1325          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1326          12,                    /* bitsize */
1327          FALSE,                 /* pc_relative */
1328          0,                     /* bitpos */
1329          complain_overflow_dont,        /* complain_on_overflow */
1330          bfd_elf_generic_reloc, /* special_function */
1331          "R_AARCH64_TLSDESC_ADD",       /* name */
1332          FALSE,                 /* partial_inplace */
1333          0x0,                   /* src_mask */
1334          0x0,                   /* dst_mask */
1335          FALSE),                /* pcrel_offset */
1336
1337   HOWTO (R_AARCH64_TLSDESC_CALL,        /* type */
1338          0,                     /* rightshift */
1339          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1340          12,                    /* bitsize */
1341          FALSE,                 /* pc_relative */
1342          0,                     /* bitpos */
1343          complain_overflow_dont,        /* complain_on_overflow */
1344          bfd_elf_generic_reloc, /* special_function */
1345          "R_AARCH64_TLSDESC_CALL",      /* name */
1346          FALSE,                 /* partial_inplace */
1347          0x0,                   /* src_mask */
1348          0x0,                   /* dst_mask */
1349          FALSE),                /* pcrel_offset */
1350 };
1351
1352 static reloc_howto_type *
1353 elf64_aarch64_howto_from_type (unsigned int r_type)
1354 {
1355   if (r_type >= R_AARCH64_static_min && r_type < R_AARCH64_static_max)
1356     return &elf64_aarch64_howto_table[r_type - R_AARCH64_static_min];
1357
1358   if (r_type >= R_AARCH64_tls_min && r_type < R_AARCH64_tls_max)
1359     return &elf64_aarch64_tls_howto_table[r_type - R_AARCH64_tls_min];
1360
1361   if (r_type >= R_AARCH64_tlsdesc_min && r_type < R_AARCH64_tlsdesc_max)
1362     return &elf64_aarch64_tlsdesc_howto_table[r_type - R_AARCH64_tlsdesc_min];
1363
1364   if (r_type >= R_AARCH64_dyn_min && r_type < R_AARCH64_dyn_max)
1365     return &elf64_aarch64_howto_dynrelocs[r_type - R_AARCH64_dyn_min];
1366
1367   switch (r_type)
1368     {
1369     case R_AARCH64_NONE:
1370       return &elf64_aarch64_howto_none;
1371
1372     }
1373   bfd_set_error (bfd_error_bad_value);
1374   return NULL;
1375 }
1376
1377 static void
1378 elf64_aarch64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
1379                              Elf_Internal_Rela *elf_reloc)
1380 {
1381   unsigned int r_type;
1382
1383   r_type = ELF64_R_TYPE (elf_reloc->r_info);
1384   bfd_reloc->howto = elf64_aarch64_howto_from_type (r_type);
1385 }
1386
1387 struct elf64_aarch64_reloc_map
1388 {
1389   bfd_reloc_code_real_type bfd_reloc_val;
1390   unsigned int elf_reloc_val;
1391 };
1392
1393 /* All entries in this list must also be present in
1394    elf64_aarch64_howto_table.  */
1395 static const struct elf64_aarch64_reloc_map elf64_aarch64_reloc_map[] =
1396 {
1397   {BFD_RELOC_NONE, R_AARCH64_NONE},
1398
1399   /* Basic data relocations.  */
1400   {BFD_RELOC_CTOR, R_AARCH64_ABS64},
1401   {BFD_RELOC_64, R_AARCH64_ABS64},
1402   {BFD_RELOC_32, R_AARCH64_ABS32},
1403   {BFD_RELOC_16, R_AARCH64_ABS16},
1404   {BFD_RELOC_64_PCREL, R_AARCH64_PREL64},
1405   {BFD_RELOC_32_PCREL, R_AARCH64_PREL32},
1406   {BFD_RELOC_16_PCREL, R_AARCH64_PREL16},
1407
1408   /* Group relocations to low order bits of a 16, 32, 48 or 64 bit
1409      value inline.  */
1410   {BFD_RELOC_AARCH64_MOVW_G0_NC, R_AARCH64_MOVW_UABS_G0_NC},
1411   {BFD_RELOC_AARCH64_MOVW_G1_NC, R_AARCH64_MOVW_UABS_G1_NC},
1412   {BFD_RELOC_AARCH64_MOVW_G2_NC, R_AARCH64_MOVW_UABS_G2_NC},
1413
1414   /* Group relocations to create high bits of a 16, 32, 48 or 64 bit
1415      signed value inline.  */
1416   {BFD_RELOC_AARCH64_MOVW_G0_S, R_AARCH64_MOVW_SABS_G0},
1417   {BFD_RELOC_AARCH64_MOVW_G1_S, R_AARCH64_MOVW_SABS_G1},
1418   {BFD_RELOC_AARCH64_MOVW_G2_S, R_AARCH64_MOVW_SABS_G2},
1419
1420   /* Group relocations to create high bits of a 16, 32, 48 or 64 bit
1421      unsigned value inline.  */
1422   {BFD_RELOC_AARCH64_MOVW_G0, R_AARCH64_MOVW_UABS_G0},
1423   {BFD_RELOC_AARCH64_MOVW_G1, R_AARCH64_MOVW_UABS_G1},
1424   {BFD_RELOC_AARCH64_MOVW_G2, R_AARCH64_MOVW_UABS_G2},
1425   {BFD_RELOC_AARCH64_MOVW_G3, R_AARCH64_MOVW_UABS_G3},
1426
1427   /* Relocations to generate 19, 21 and 33 bit PC-relative load/store.  */
1428   {BFD_RELOC_AARCH64_LD_LO19_PCREL, R_AARCH64_LD_PREL_LO19},
1429   {BFD_RELOC_AARCH64_ADR_LO21_PCREL, R_AARCH64_ADR_PREL_LO21},
1430   {BFD_RELOC_AARCH64_ADR_HI21_PCREL, R_AARCH64_ADR_PREL_PG_HI21},
1431   {BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL, R_AARCH64_ADR_PREL_PG_HI21_NC},
1432   {BFD_RELOC_AARCH64_ADD_LO12, R_AARCH64_ADD_ABS_LO12_NC},
1433   {BFD_RELOC_AARCH64_LDST8_LO12, R_AARCH64_LDST8_ABS_LO12_NC},
1434   {BFD_RELOC_AARCH64_LDST16_LO12, R_AARCH64_LDST16_ABS_LO12_NC},
1435   {BFD_RELOC_AARCH64_LDST32_LO12, R_AARCH64_LDST32_ABS_LO12_NC},
1436   {BFD_RELOC_AARCH64_LDST64_LO12, R_AARCH64_LDST64_ABS_LO12_NC},
1437   {BFD_RELOC_AARCH64_LDST128_LO12, R_AARCH64_LDST128_ABS_LO12_NC},
1438
1439   /* Relocations for control-flow instructions.  */
1440   {BFD_RELOC_AARCH64_TSTBR14, R_AARCH64_TSTBR14},
1441   {BFD_RELOC_AARCH64_BRANCH19, R_AARCH64_CONDBR19},
1442   {BFD_RELOC_AARCH64_JUMP26, R_AARCH64_JUMP26},
1443   {BFD_RELOC_AARCH64_CALL26, R_AARCH64_CALL26},
1444
1445   /* Relocations for PIC.  */
1446   {BFD_RELOC_AARCH64_GOT_LD_PREL19, R_AARCH64_GOT_LD_PREL19},
1447   {BFD_RELOC_AARCH64_ADR_GOT_PAGE, R_AARCH64_ADR_GOT_PAGE},
1448   {BFD_RELOC_AARCH64_LD64_GOT_LO12_NC, R_AARCH64_LD64_GOT_LO12_NC},
1449
1450   /* Relocations for TLS.  */
1451   {BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21, R_AARCH64_TLSGD_ADR_PAGE21},
1452   {BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC, R_AARCH64_TLSGD_ADD_LO12_NC},
1453   {BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1,
1454    R_AARCH64_TLSIE_MOVW_GOTTPREL_G1},
1455   {BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC,
1456    R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC},
1457   {BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21,
1458    R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21},
1459   {BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC,
1460    R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC},
1461   {BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19,
1462    R_AARCH64_TLSIE_LD_GOTTPREL_PREL19},
1463   {BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2, R_AARCH64_TLSLE_MOVW_TPREL_G2},
1464   {BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1, R_AARCH64_TLSLE_MOVW_TPREL_G1},
1465   {BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC,
1466    R_AARCH64_TLSLE_MOVW_TPREL_G1_NC},
1467   {BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0, R_AARCH64_TLSLE_MOVW_TPREL_G0},
1468   {BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC,
1469    R_AARCH64_TLSLE_MOVW_TPREL_G0_NC},
1470   {BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12, R_AARCH64_TLSLE_ADD_TPREL_LO12},
1471   {BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12, R_AARCH64_TLSLE_ADD_TPREL_HI12},
1472   {BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC,
1473    R_AARCH64_TLSLE_ADD_TPREL_LO12_NC},
1474   {BFD_RELOC_AARCH64_TLSDESC_LD64_PREL19, R_AARCH64_TLSDESC_LD64_PREL19},
1475   {BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21, R_AARCH64_TLSDESC_ADR_PREL21},
1476   {BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE, R_AARCH64_TLSDESC_ADR_PAGE},
1477   {BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC, R_AARCH64_TLSDESC_ADD_LO12_NC},
1478   {BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC, R_AARCH64_TLSDESC_LD64_LO12_NC},
1479   {BFD_RELOC_AARCH64_TLSDESC_OFF_G1, R_AARCH64_TLSDESC_OFF_G1},
1480   {BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC, R_AARCH64_TLSDESC_OFF_G0_NC},
1481   {BFD_RELOC_AARCH64_TLSDESC_LDR, R_AARCH64_TLSDESC_LDR},
1482   {BFD_RELOC_AARCH64_TLSDESC_ADD, R_AARCH64_TLSDESC_ADD},
1483   {BFD_RELOC_AARCH64_TLSDESC_CALL, R_AARCH64_TLSDESC_CALL},
1484   {BFD_RELOC_AARCH64_TLS_DTPMOD64, R_AARCH64_TLS_DTPMOD64},
1485   {BFD_RELOC_AARCH64_TLS_DTPREL64, R_AARCH64_TLS_DTPREL64},
1486   {BFD_RELOC_AARCH64_TLS_TPREL64, R_AARCH64_TLS_TPREL64},
1487   {BFD_RELOC_AARCH64_TLSDESC, R_AARCH64_TLSDESC},
1488 };
1489
1490 static reloc_howto_type *
1491 elf64_aarch64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1492                                  bfd_reloc_code_real_type code)
1493 {
1494   unsigned int i;
1495
1496   for (i = 0; i < ARRAY_SIZE (elf64_aarch64_reloc_map); i++)
1497     if (elf64_aarch64_reloc_map[i].bfd_reloc_val == code)
1498       return elf64_aarch64_howto_from_type
1499         (elf64_aarch64_reloc_map[i].elf_reloc_val);
1500
1501   bfd_set_error (bfd_error_bad_value);
1502   return NULL;
1503 }
1504
1505 static reloc_howto_type *
1506 elf64_aarch64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1507                                  const char *r_name)
1508 {
1509   unsigned int i;
1510
1511   for (i = 0; i < ARRAY_SIZE (elf64_aarch64_howto_table); i++)
1512     if (elf64_aarch64_howto_table[i].name != NULL
1513         && strcasecmp (elf64_aarch64_howto_table[i].name, r_name) == 0)
1514       return &elf64_aarch64_howto_table[i];
1515
1516   return NULL;
1517 }
1518
1519 /* Support for core dump NOTE sections.  */
1520
1521 static bfd_boolean
1522 elf64_aarch64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1523 {
1524   int offset;
1525   size_t size;
1526
1527   switch (note->descsz)
1528     {
1529       default:
1530         return FALSE;
1531
1532       case 408:         /* sizeof(struct elf_prstatus) on Linux/arm64.  */
1533         /* pr_cursig */
1534         elf_tdata (abfd)->core->signal
1535           = bfd_get_16 (abfd, note->descdata + 12);
1536
1537         /* pr_pid */
1538         elf_tdata (abfd)->core->lwpid
1539           = bfd_get_32 (abfd, note->descdata + 32);
1540
1541         /* pr_reg */
1542         offset = 112;
1543         size = 272;
1544
1545         break;
1546     }
1547
1548   /* Make a ".reg/999" section.  */
1549   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1550                                           size, note->descpos + offset);
1551 }
1552
1553 #define TARGET_LITTLE_SYM               bfd_elf64_littleaarch64_vec
1554 #define TARGET_LITTLE_NAME              "elf64-littleaarch64"
1555 #define TARGET_BIG_SYM                  bfd_elf64_bigaarch64_vec
1556 #define TARGET_BIG_NAME                 "elf64-bigaarch64"
1557
1558 #define elf_backend_grok_prstatus       elf64_aarch64_grok_prstatus
1559
1560 typedef unsigned long int insn32;
1561
1562 /* The linker script knows the section names for placement.
1563    The entry_names are used to do simple name mangling on the stubs.
1564    Given a function name, and its type, the stub can be found. The
1565    name can be changed. The only requirement is the %s be present.  */
1566 #define STUB_ENTRY_NAME   "__%s_veneer"
1567
1568 /* The name of the dynamic interpreter.  This is put in the .interp
1569    section.  */
1570 #define ELF_DYNAMIC_INTERPRETER     "/lib/ld.so.1"
1571
1572 #define AARCH64_MAX_FWD_BRANCH_OFFSET \
1573   (((1 << 25) - 1) << 2)
1574 #define AARCH64_MAX_BWD_BRANCH_OFFSET \
1575   (-((1 << 25) << 2))
1576
1577 #define AARCH64_MAX_ADRP_IMM ((1 << 20) - 1)
1578 #define AARCH64_MIN_ADRP_IMM (-(1 << 20))
1579
1580 static int
1581 aarch64_valid_for_adrp_p (bfd_vma value, bfd_vma place)
1582 {
1583   bfd_signed_vma offset = (bfd_signed_vma) (PG (value) - PG (place)) >> 12;
1584   return offset <= AARCH64_MAX_ADRP_IMM && offset >= AARCH64_MIN_ADRP_IMM;
1585 }
1586
1587 static int
1588 aarch64_valid_branch_p (bfd_vma value, bfd_vma place)
1589 {
1590   bfd_signed_vma offset = (bfd_signed_vma) (value - place);
1591   return (offset <= AARCH64_MAX_FWD_BRANCH_OFFSET
1592           && offset >= AARCH64_MAX_BWD_BRANCH_OFFSET);
1593 }
1594
1595 static const uint32_t aarch64_adrp_branch_stub [] =
1596 {
1597   0x90000010,                   /*      adrp    ip0, X */
1598                                 /*              R_AARCH64_ADR_HI21_PCREL(X) */
1599   0x91000210,                   /*      add     ip0, ip0, :lo12:X */
1600                                 /*              R_AARCH64_ADD_ABS_LO12_NC(X) */
1601   0xd61f0200,                   /*      br      ip0 */
1602 };
1603
1604 static const uint32_t aarch64_long_branch_stub[] =
1605 {
1606   0x58000090,                   /*      ldr   ip0, 1f */
1607   0x10000011,                   /*      adr   ip1, #0 */
1608   0x8b110210,                   /*      add   ip0, ip0, ip1 */
1609   0xd61f0200,                   /*      br      ip0 */
1610   0x00000000,                   /* 1:   .xword
1611                                    R_AARCH64_PREL64(X) + 12
1612                                  */
1613   0x00000000,
1614 };
1615
1616 /* Section name for stubs is the associated section name plus this
1617    string.  */
1618 #define STUB_SUFFIX ".stub"
1619
1620 enum elf64_aarch64_stub_type
1621 {
1622   aarch64_stub_none,
1623   aarch64_stub_adrp_branch,
1624   aarch64_stub_long_branch,
1625 };
1626
1627 struct elf64_aarch64_stub_hash_entry
1628 {
1629   /* Base hash table entry structure.  */
1630   struct bfd_hash_entry root;
1631
1632   /* The stub section.  */
1633   asection *stub_sec;
1634
1635   /* Offset within stub_sec of the beginning of this stub.  */
1636   bfd_vma stub_offset;
1637
1638   /* Given the symbol's value and its section we can determine its final
1639      value when building the stubs (so the stub knows where to jump).  */
1640   bfd_vma target_value;
1641   asection *target_section;
1642
1643   enum elf64_aarch64_stub_type stub_type;
1644
1645   /* The symbol table entry, if any, that this was derived from.  */
1646   struct elf64_aarch64_link_hash_entry *h;
1647
1648   /* Destination symbol type */
1649   unsigned char st_type;
1650
1651   /* Where this stub is being called from, or, in the case of combined
1652      stub sections, the first input section in the group.  */
1653   asection *id_sec;
1654
1655   /* The name for the local symbol at the start of this stub.  The
1656      stub name in the hash table has to be unique; this does not, so
1657      it can be friendlier.  */
1658   char *output_name;
1659 };
1660
1661 /* Used to build a map of a section.  This is required for mixed-endian
1662    code/data.  */
1663
1664 typedef struct elf64_elf_section_map
1665 {
1666   bfd_vma vma;
1667   char type;
1668 }
1669 elf64_aarch64_section_map;
1670
1671
1672 typedef struct _aarch64_elf_section_data
1673 {
1674   struct bfd_elf_section_data elf;
1675   unsigned int mapcount;
1676   unsigned int mapsize;
1677   elf64_aarch64_section_map *map;
1678 }
1679 _aarch64_elf_section_data;
1680
1681 #define elf64_aarch64_section_data(sec) \
1682   ((_aarch64_elf_section_data *) elf_section_data (sec))
1683
1684 /* The size of the thread control block.  */
1685 #define TCB_SIZE        16
1686
1687 struct elf_aarch64_local_symbol
1688 {
1689   unsigned int got_type;
1690   bfd_signed_vma got_refcount;
1691   bfd_vma got_offset;
1692
1693   /* Offset of the GOTPLT entry reserved for the TLS descriptor. The
1694      offset is from the end of the jump table and reserved entries
1695      within the PLTGOT.
1696
1697      The magic value (bfd_vma) -1 indicates that an offset has not be
1698      allocated.  */
1699   bfd_vma tlsdesc_got_jump_table_offset;
1700 };
1701
1702 struct elf_aarch64_obj_tdata
1703 {
1704   struct elf_obj_tdata root;
1705
1706   /* local symbol descriptors */
1707   struct elf_aarch64_local_symbol *locals;
1708
1709   /* Zero to warn when linking objects with incompatible enum sizes.  */
1710   int no_enum_size_warning;
1711
1712   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
1713   int no_wchar_size_warning;
1714 };
1715
1716 #define elf_aarch64_tdata(bfd)                          \
1717   ((struct elf_aarch64_obj_tdata *) (bfd)->tdata.any)
1718
1719 #define elf64_aarch64_locals(bfd) (elf_aarch64_tdata (bfd)->locals)
1720
1721 #define is_aarch64_elf(bfd)                             \
1722   (bfd_get_flavour (bfd) == bfd_target_elf_flavour      \
1723    && elf_tdata (bfd) != NULL                           \
1724    && elf_object_id (bfd) == AARCH64_ELF_DATA)
1725
1726 static bfd_boolean
1727 elf64_aarch64_mkobject (bfd *abfd)
1728 {
1729   return bfd_elf_allocate_object (abfd, sizeof (struct elf_aarch64_obj_tdata),
1730                                   AARCH64_ELF_DATA);
1731 }
1732
1733 #define elf64_aarch64_hash_entry(ent) \
1734   ((struct elf64_aarch64_link_hash_entry *)(ent))
1735
1736 #define GOT_UNKNOWN    0
1737 #define GOT_NORMAL     1
1738 #define GOT_TLS_GD     2
1739 #define GOT_TLS_IE     4
1740 #define GOT_TLSDESC_GD 8
1741
1742 #define GOT_TLS_GD_ANY_P(type)  ((type & GOT_TLS_GD) || (type & GOT_TLSDESC_GD))
1743
1744 /* AArch64 ELF linker hash entry.  */
1745 struct elf64_aarch64_link_hash_entry
1746 {
1747   struct elf_link_hash_entry root;
1748
1749   /* Track dynamic relocs copied for this symbol.  */
1750   struct elf_dyn_relocs *dyn_relocs;
1751
1752   /* Since PLT entries have variable size, we need to record the
1753      index into .got.plt instead of recomputing it from the PLT
1754      offset.  */
1755   bfd_signed_vma plt_got_offset;
1756
1757   /* Bit mask representing the type of GOT entry(s) if any required by
1758      this symbol.  */
1759   unsigned int got_type;
1760
1761   /* A pointer to the most recently used stub hash entry against this
1762      symbol.  */
1763   struct elf64_aarch64_stub_hash_entry *stub_cache;
1764
1765   /* Offset of the GOTPLT entry reserved for the TLS descriptor.  The offset
1766      is from the end of the jump table and reserved entries within the PLTGOT.
1767
1768      The magic value (bfd_vma) -1 indicates that an offset has not
1769      be allocated.  */
1770   bfd_vma tlsdesc_got_jump_table_offset;
1771 };
1772
1773 static unsigned int
1774 elf64_aarch64_symbol_got_type (struct elf_link_hash_entry *h,
1775                                bfd *abfd,
1776                                unsigned long r_symndx)
1777 {
1778   if (h)
1779     return elf64_aarch64_hash_entry (h)->got_type;
1780
1781   if (! elf64_aarch64_locals (abfd))
1782     return GOT_UNKNOWN;
1783
1784   return elf64_aarch64_locals (abfd)[r_symndx].got_type;
1785 }
1786
1787 /* Traverse an AArch64 ELF linker hash table.  */
1788 #define elf64_aarch64_link_hash_traverse(table, func, info)             \
1789   (elf_link_hash_traverse                                               \
1790    (&(table)->root,                                                     \
1791     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
1792     (info)))
1793
1794 /* Get the AArch64 elf linker hash table from a link_info structure.  */
1795 #define elf64_aarch64_hash_table(info)                                  \
1796   ((struct elf64_aarch64_link_hash_table *) ((info)->hash))
1797
1798 #define aarch64_stub_hash_lookup(table, string, create, copy)           \
1799   ((struct elf64_aarch64_stub_hash_entry *)                             \
1800    bfd_hash_lookup ((table), (string), (create), (copy)))
1801
1802 /* AArch64 ELF linker hash table.  */
1803 struct elf64_aarch64_link_hash_table
1804 {
1805   /* The main hash table.  */
1806   struct elf_link_hash_table root;
1807
1808   /* Nonzero to force PIC branch veneers.  */
1809   int pic_veneer;
1810
1811   /* The number of bytes in the initial entry in the PLT.  */
1812   bfd_size_type plt_header_size;
1813
1814   /* The number of bytes in the subsequent PLT etries.  */
1815   bfd_size_type plt_entry_size;
1816
1817   /* Short-cuts to get to dynamic linker sections.  */
1818   asection *sdynbss;
1819   asection *srelbss;
1820
1821   /* Small local sym cache.  */
1822   struct sym_cache sym_cache;
1823
1824   /* For convenience in allocate_dynrelocs.  */
1825   bfd *obfd;
1826
1827   /* The amount of space used by the reserved portion of the sgotplt
1828      section, plus whatever space is used by the jump slots.  */
1829   bfd_vma sgotplt_jump_table_size;
1830
1831   /* The stub hash table.  */
1832   struct bfd_hash_table stub_hash_table;
1833
1834   /* Linker stub bfd.  */
1835   bfd *stub_bfd;
1836
1837   /* Linker call-backs.  */
1838   asection *(*add_stub_section) (const char *, asection *);
1839   void (*layout_sections_again) (void);
1840
1841   /* Array to keep track of which stub sections have been created, and
1842      information on stub grouping.  */
1843   struct map_stub
1844   {
1845     /* This is the section to which stubs in the group will be
1846        attached.  */
1847     asection *link_sec;
1848     /* The stub section.  */
1849     asection *stub_sec;
1850   } *stub_group;
1851
1852   /* Assorted information used by elf64_aarch64_size_stubs.  */
1853   unsigned int bfd_count;
1854   int top_index;
1855   asection **input_list;
1856
1857   /* The offset into splt of the PLT entry for the TLS descriptor
1858      resolver.  Special values are 0, if not necessary (or not found
1859      to be necessary yet), and -1 if needed but not determined
1860      yet.  */
1861   bfd_vma tlsdesc_plt;
1862
1863   /* The GOT offset for the lazy trampoline.  Communicated to the
1864      loader via DT_TLSDESC_GOT.  The magic value (bfd_vma) -1
1865      indicates an offset is not allocated.  */
1866   bfd_vma dt_tlsdesc_got;
1867 };
1868
1869
1870 /* Return non-zero if the indicated VALUE has overflowed the maximum
1871    range expressible by a unsigned number with the indicated number of
1872    BITS.  */
1873
1874 static bfd_reloc_status_type
1875 aarch64_unsigned_overflow (bfd_vma value, unsigned int bits)
1876 {
1877   bfd_vma lim;
1878   if (bits >= sizeof (bfd_vma) * 8)
1879     return bfd_reloc_ok;
1880   lim = (bfd_vma) 1 << bits;
1881   if (value >= lim)
1882     return bfd_reloc_overflow;
1883   return bfd_reloc_ok;
1884 }
1885
1886
1887 /* Return non-zero if the indicated VALUE has overflowed the maximum
1888    range expressible by an signed number with the indicated number of
1889    BITS.  */
1890
1891 static bfd_reloc_status_type
1892 aarch64_signed_overflow (bfd_vma value, unsigned int bits)
1893 {
1894   bfd_signed_vma svalue = (bfd_signed_vma) value;
1895   bfd_signed_vma lim;
1896
1897   if (bits >= sizeof (bfd_vma) * 8)
1898     return bfd_reloc_ok;
1899   lim = (bfd_signed_vma) 1 << (bits - 1);
1900   if (svalue < -lim || svalue >= lim)
1901     return bfd_reloc_overflow;
1902   return bfd_reloc_ok;
1903 }
1904
1905 /* Create an entry in an AArch64 ELF linker hash table.  */
1906
1907 static struct bfd_hash_entry *
1908 elf64_aarch64_link_hash_newfunc (struct bfd_hash_entry *entry,
1909                                  struct bfd_hash_table *table,
1910                                  const char *string)
1911 {
1912   struct elf64_aarch64_link_hash_entry *ret =
1913     (struct elf64_aarch64_link_hash_entry *) entry;
1914
1915   /* Allocate the structure if it has not already been allocated by a
1916      subclass.  */
1917   if (ret == NULL)
1918     ret = bfd_hash_allocate (table,
1919                              sizeof (struct elf64_aarch64_link_hash_entry));
1920   if (ret == NULL)
1921     return (struct bfd_hash_entry *) ret;
1922
1923   /* Call the allocation method of the superclass.  */
1924   ret = ((struct elf64_aarch64_link_hash_entry *)
1925          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1926                                      table, string));
1927   if (ret != NULL)
1928     {
1929       ret->dyn_relocs = NULL;
1930       ret->got_type = GOT_UNKNOWN;
1931       ret->plt_got_offset = (bfd_vma) - 1;
1932       ret->stub_cache = NULL;
1933       ret->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
1934     }
1935
1936   return (struct bfd_hash_entry *) ret;
1937 }
1938
1939 /* Initialize an entry in the stub hash table.  */
1940
1941 static struct bfd_hash_entry *
1942 stub_hash_newfunc (struct bfd_hash_entry *entry,
1943                    struct bfd_hash_table *table, const char *string)
1944 {
1945   /* Allocate the structure if it has not already been allocated by a
1946      subclass.  */
1947   if (entry == NULL)
1948     {
1949       entry = bfd_hash_allocate (table,
1950                                  sizeof (struct
1951                                          elf64_aarch64_stub_hash_entry));
1952       if (entry == NULL)
1953         return entry;
1954     }
1955
1956   /* Call the allocation method of the superclass.  */
1957   entry = bfd_hash_newfunc (entry, table, string);
1958   if (entry != NULL)
1959     {
1960       struct elf64_aarch64_stub_hash_entry *eh;
1961
1962       /* Initialize the local fields.  */
1963       eh = (struct elf64_aarch64_stub_hash_entry *) entry;
1964       eh->stub_sec = NULL;
1965       eh->stub_offset = 0;
1966       eh->target_value = 0;
1967       eh->target_section = NULL;
1968       eh->stub_type = aarch64_stub_none;
1969       eh->h = NULL;
1970       eh->id_sec = NULL;
1971     }
1972
1973   return entry;
1974 }
1975
1976
1977 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
1978
1979 static void
1980 elf64_aarch64_copy_indirect_symbol (struct bfd_link_info *info,
1981                                     struct elf_link_hash_entry *dir,
1982                                     struct elf_link_hash_entry *ind)
1983 {
1984   struct elf64_aarch64_link_hash_entry *edir, *eind;
1985
1986   edir = (struct elf64_aarch64_link_hash_entry *) dir;
1987   eind = (struct elf64_aarch64_link_hash_entry *) ind;
1988
1989   if (eind->dyn_relocs != NULL)
1990     {
1991       if (edir->dyn_relocs != NULL)
1992         {
1993           struct elf_dyn_relocs **pp;
1994           struct elf_dyn_relocs *p;
1995
1996           /* Add reloc counts against the indirect sym to the direct sym
1997              list.  Merge any entries against the same section.  */
1998           for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
1999             {
2000               struct elf_dyn_relocs *q;
2001
2002               for (q = edir->dyn_relocs; q != NULL; q = q->next)
2003                 if (q->sec == p->sec)
2004                   {
2005                     q->pc_count += p->pc_count;
2006                     q->count += p->count;
2007                     *pp = p->next;
2008                     break;
2009                   }
2010               if (q == NULL)
2011                 pp = &p->next;
2012             }
2013           *pp = edir->dyn_relocs;
2014         }
2015
2016       edir->dyn_relocs = eind->dyn_relocs;
2017       eind->dyn_relocs = NULL;
2018     }
2019
2020   if (ind->root.type == bfd_link_hash_indirect)
2021     {
2022       /* Copy over PLT info.  */
2023       if (dir->got.refcount <= 0)
2024         {
2025           edir->got_type = eind->got_type;
2026           eind->got_type = GOT_UNKNOWN;
2027         }
2028     }
2029
2030   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2031 }
2032
2033 /* Create an AArch64 elf linker hash table.  */
2034
2035 static struct bfd_link_hash_table *
2036 elf64_aarch64_link_hash_table_create (bfd *abfd)
2037 {
2038   struct elf64_aarch64_link_hash_table *ret;
2039   bfd_size_type amt = sizeof (struct elf64_aarch64_link_hash_table);
2040
2041   ret = bfd_zmalloc (amt);
2042   if (ret == NULL)
2043     return NULL;
2044
2045   if (!_bfd_elf_link_hash_table_init
2046       (&ret->root, abfd, elf64_aarch64_link_hash_newfunc,
2047        sizeof (struct elf64_aarch64_link_hash_entry), AARCH64_ELF_DATA))
2048     {
2049       free (ret);
2050       return NULL;
2051     }
2052
2053   ret->plt_header_size = PLT_ENTRY_SIZE;
2054   ret->plt_entry_size = PLT_SMALL_ENTRY_SIZE;
2055   ret->obfd = abfd;
2056   ret->dt_tlsdesc_got = (bfd_vma) - 1;
2057
2058   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
2059                             sizeof (struct elf64_aarch64_stub_hash_entry)))
2060     {
2061       free (ret);
2062       return NULL;
2063     }
2064
2065   return &ret->root.root;
2066 }
2067
2068 /* Free the derived linker hash table.  */
2069
2070 static void
2071 elf64_aarch64_hash_table_free (struct bfd_link_hash_table *hash)
2072 {
2073   struct elf64_aarch64_link_hash_table *ret
2074     = (struct elf64_aarch64_link_hash_table *) hash;
2075
2076   bfd_hash_table_free (&ret->stub_hash_table);
2077   _bfd_elf_link_hash_table_free (hash);
2078 }
2079
2080 static bfd_vma
2081 aarch64_resolve_relocation (unsigned int r_type, bfd_vma place, bfd_vma value,
2082                             bfd_vma addend, bfd_boolean weak_undef_p)
2083 {
2084   switch (r_type)
2085     {
2086     case R_AARCH64_TLSDESC_CALL:
2087     case R_AARCH64_NONE:
2088     case R_AARCH64_NULL:
2089       break;
2090
2091     case R_AARCH64_ADR_PREL_LO21:
2092     case R_AARCH64_CONDBR19:
2093     case R_AARCH64_LD_PREL_LO19:
2094     case R_AARCH64_PREL16:
2095     case R_AARCH64_PREL32:
2096     case R_AARCH64_PREL64:
2097     case R_AARCH64_TSTBR14:
2098       if (weak_undef_p)
2099         value = place;
2100       value = value + addend - place;
2101       break;
2102
2103     case R_AARCH64_CALL26:
2104     case R_AARCH64_JUMP26:
2105       value = value + addend - place;
2106       break;
2107
2108     case R_AARCH64_ABS16:
2109     case R_AARCH64_ABS32:
2110     case R_AARCH64_MOVW_SABS_G0:
2111     case R_AARCH64_MOVW_SABS_G1:
2112     case R_AARCH64_MOVW_SABS_G2:
2113     case R_AARCH64_MOVW_UABS_G0:
2114     case R_AARCH64_MOVW_UABS_G0_NC:
2115     case R_AARCH64_MOVW_UABS_G1:
2116     case R_AARCH64_MOVW_UABS_G1_NC:
2117     case R_AARCH64_MOVW_UABS_G2:
2118     case R_AARCH64_MOVW_UABS_G2_NC:
2119     case R_AARCH64_MOVW_UABS_G3:
2120       value = value + addend;
2121       break;
2122
2123     case R_AARCH64_ADR_PREL_PG_HI21:
2124     case R_AARCH64_ADR_PREL_PG_HI21_NC:
2125       if (weak_undef_p)
2126         value = PG (place);
2127       value = PG (value + addend) - PG (place);
2128       break;
2129
2130     case R_AARCH64_GOT_LD_PREL19:
2131       value = value + addend - place;
2132       break;
2133
2134     case R_AARCH64_ADR_GOT_PAGE:
2135     case R_AARCH64_TLSDESC_ADR_PAGE:
2136     case R_AARCH64_TLSGD_ADR_PAGE21:
2137     case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
2138       value = PG (value + addend) - PG (place);
2139       break;
2140
2141     case R_AARCH64_ADD_ABS_LO12_NC:
2142     case R_AARCH64_LD64_GOT_LO12_NC:
2143     case R_AARCH64_LDST8_ABS_LO12_NC:
2144     case R_AARCH64_LDST16_ABS_LO12_NC:
2145     case R_AARCH64_LDST32_ABS_LO12_NC:
2146     case R_AARCH64_LDST64_ABS_LO12_NC:
2147     case R_AARCH64_LDST128_ABS_LO12_NC:
2148     case R_AARCH64_TLSDESC_ADD_LO12_NC:
2149     case R_AARCH64_TLSDESC_ADD:
2150     case R_AARCH64_TLSDESC_LD64_LO12_NC:
2151     case R_AARCH64_TLSDESC_LDR:
2152     case R_AARCH64_TLSGD_ADD_LO12_NC:
2153     case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
2154     case R_AARCH64_TLSLE_ADD_TPREL_LO12:
2155     case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
2156       value = PG_OFFSET (value + addend);
2157       break;
2158
2159     case R_AARCH64_TLSLE_MOVW_TPREL_G1:
2160     case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
2161       value = (value + addend) & (bfd_vma) 0xffff0000;
2162       break;
2163     case R_AARCH64_TLSLE_ADD_TPREL_HI12:
2164       value = (value + addend) & (bfd_vma) 0xfff000;
2165       break;
2166
2167     case R_AARCH64_TLSLE_MOVW_TPREL_G0:
2168     case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
2169       value = (value + addend) & (bfd_vma) 0xffff;
2170       break;
2171
2172     case R_AARCH64_TLSLE_MOVW_TPREL_G2:
2173       value = (value + addend) & ~(bfd_vma) 0xffffffff;
2174       value -= place & ~(bfd_vma) 0xffffffff;
2175       break;
2176     }
2177   return value;
2178 }
2179
2180 static bfd_boolean
2181 aarch64_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
2182                   bfd_vma offset, bfd_vma value)
2183 {
2184   reloc_howto_type *howto;
2185   bfd_vma place;
2186
2187   howto = elf64_aarch64_howto_from_type (r_type);
2188   place = (input_section->output_section->vma + input_section->output_offset
2189            + offset);
2190   value = aarch64_resolve_relocation (r_type, place, value, 0, FALSE);
2191   return bfd_elf_aarch64_put_addend (input_bfd,
2192                                      input_section->contents + offset,
2193                                      howto, value);
2194 }
2195
2196 static enum elf64_aarch64_stub_type
2197 aarch64_select_branch_stub (bfd_vma value, bfd_vma place)
2198 {
2199   if (aarch64_valid_for_adrp_p (value, place))
2200     return aarch64_stub_adrp_branch;
2201   return aarch64_stub_long_branch;
2202 }
2203
2204 /* Determine the type of stub needed, if any, for a call.  */
2205
2206 static enum elf64_aarch64_stub_type
2207 aarch64_type_of_stub (struct bfd_link_info *info,
2208                       asection *input_sec,
2209                       const Elf_Internal_Rela *rel,
2210                       unsigned char st_type,
2211                       struct elf64_aarch64_link_hash_entry *hash,
2212                       bfd_vma destination)
2213 {
2214   bfd_vma location;
2215   bfd_signed_vma branch_offset;
2216   unsigned int r_type;
2217   struct elf64_aarch64_link_hash_table *globals;
2218   enum elf64_aarch64_stub_type stub_type = aarch64_stub_none;
2219   bfd_boolean via_plt_p;
2220
2221   if (st_type != STT_FUNC)
2222     return stub_type;
2223
2224   globals = elf64_aarch64_hash_table (info);
2225   via_plt_p = (globals->root.splt != NULL && hash != NULL
2226                && hash->root.plt.offset != (bfd_vma) - 1);
2227
2228   if (via_plt_p)
2229     return stub_type;
2230
2231   /* Determine where the call point is.  */
2232   location = (input_sec->output_offset
2233               + input_sec->output_section->vma + rel->r_offset);
2234
2235   branch_offset = (bfd_signed_vma) (destination - location);
2236
2237   r_type = ELF64_R_TYPE (rel->r_info);
2238
2239   /* We don't want to redirect any old unconditional jump in this way,
2240      only one which is being used for a sibcall, where it is
2241      acceptable for the IP0 and IP1 registers to be clobbered.  */
2242   if ((r_type == R_AARCH64_CALL26 || r_type == R_AARCH64_JUMP26)
2243       && (branch_offset > AARCH64_MAX_FWD_BRANCH_OFFSET
2244           || branch_offset < AARCH64_MAX_BWD_BRANCH_OFFSET))
2245     {
2246       stub_type = aarch64_stub_long_branch;
2247     }
2248
2249   return stub_type;
2250 }
2251
2252 /* Build a name for an entry in the stub hash table.  */
2253
2254 static char *
2255 elf64_aarch64_stub_name (const asection *input_section,
2256                          const asection *sym_sec,
2257                          const struct elf64_aarch64_link_hash_entry *hash,
2258                          const Elf_Internal_Rela *rel)
2259 {
2260   char *stub_name;
2261   bfd_size_type len;
2262
2263   if (hash)
2264     {
2265       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 16 + 1;
2266       stub_name = bfd_malloc (len);
2267       if (stub_name != NULL)
2268         snprintf (stub_name, len, "%08x_%s+%" BFD_VMA_FMT "x",
2269                   (unsigned int) input_section->id,
2270                   hash->root.root.root.string,
2271                   rel->r_addend);
2272     }
2273   else
2274     {
2275       len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
2276       stub_name = bfd_malloc (len);
2277       if (stub_name != NULL)
2278         snprintf (stub_name, len, "%08x_%x:%x+%" BFD_VMA_FMT "x",
2279                   (unsigned int) input_section->id,
2280                   (unsigned int) sym_sec->id,
2281                   (unsigned int) ELF64_R_SYM (rel->r_info),
2282                   rel->r_addend);
2283     }
2284
2285   return stub_name;
2286 }
2287
2288 /* Look up an entry in the stub hash.  Stub entries are cached because
2289    creating the stub name takes a bit of time.  */
2290
2291 static struct elf64_aarch64_stub_hash_entry *
2292 elf64_aarch64_get_stub_entry (const asection *input_section,
2293                               const asection *sym_sec,
2294                               struct elf_link_hash_entry *hash,
2295                               const Elf_Internal_Rela *rel,
2296                               struct elf64_aarch64_link_hash_table *htab)
2297 {
2298   struct elf64_aarch64_stub_hash_entry *stub_entry;
2299   struct elf64_aarch64_link_hash_entry *h =
2300     (struct elf64_aarch64_link_hash_entry *) hash;
2301   const asection *id_sec;
2302
2303   if ((input_section->flags & SEC_CODE) == 0)
2304     return NULL;
2305
2306   /* If this input section is part of a group of sections sharing one
2307      stub section, then use the id of the first section in the group.
2308      Stub names need to include a section id, as there may well be
2309      more than one stub used to reach say, printf, and we need to
2310      distinguish between them.  */
2311   id_sec = htab->stub_group[input_section->id].link_sec;
2312
2313   if (h != NULL && h->stub_cache != NULL
2314       && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
2315     {
2316       stub_entry = h->stub_cache;
2317     }
2318   else
2319     {
2320       char *stub_name;
2321
2322       stub_name = elf64_aarch64_stub_name (id_sec, sym_sec, h, rel);
2323       if (stub_name == NULL)
2324         return NULL;
2325
2326       stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table,
2327                                              stub_name, FALSE, FALSE);
2328       if (h != NULL)
2329         h->stub_cache = stub_entry;
2330
2331       free (stub_name);
2332     }
2333
2334   return stub_entry;
2335 }
2336
2337 /* Add a new stub entry to the stub hash.  Not all fields of the new
2338    stub entry are initialised.  */
2339
2340 static struct elf64_aarch64_stub_hash_entry *
2341 elf64_aarch64_add_stub (const char *stub_name,
2342                         asection *section,
2343                         struct elf64_aarch64_link_hash_table *htab)
2344 {
2345   asection *link_sec;
2346   asection *stub_sec;
2347   struct elf64_aarch64_stub_hash_entry *stub_entry;
2348
2349   link_sec = htab->stub_group[section->id].link_sec;
2350   stub_sec = htab->stub_group[section->id].stub_sec;
2351   if (stub_sec == NULL)
2352     {
2353       stub_sec = htab->stub_group[link_sec->id].stub_sec;
2354       if (stub_sec == NULL)
2355         {
2356           size_t namelen;
2357           bfd_size_type len;
2358           char *s_name;
2359
2360           namelen = strlen (link_sec->name);
2361           len = namelen + sizeof (STUB_SUFFIX);
2362           s_name = bfd_alloc (htab->stub_bfd, len);
2363           if (s_name == NULL)
2364             return NULL;
2365
2366           memcpy (s_name, link_sec->name, namelen);
2367           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2368           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
2369           if (stub_sec == NULL)
2370             return NULL;
2371           htab->stub_group[link_sec->id].stub_sec = stub_sec;
2372         }
2373       htab->stub_group[section->id].stub_sec = stub_sec;
2374     }
2375
2376   /* Enter this entry into the linker stub hash table.  */
2377   stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2378                                          TRUE, FALSE);
2379   if (stub_entry == NULL)
2380     {
2381       (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
2382                              section->owner, stub_name);
2383       return NULL;
2384     }
2385
2386   stub_entry->stub_sec = stub_sec;
2387   stub_entry->stub_offset = 0;
2388   stub_entry->id_sec = link_sec;
2389
2390   return stub_entry;
2391 }
2392
2393 static bfd_boolean
2394 aarch64_build_one_stub (struct bfd_hash_entry *gen_entry,
2395                         void *in_arg ATTRIBUTE_UNUSED)
2396 {
2397   struct elf64_aarch64_stub_hash_entry *stub_entry;
2398   asection *stub_sec;
2399   bfd *stub_bfd;
2400   bfd_byte *loc;
2401   bfd_vma sym_value;
2402   unsigned int template_size;
2403   const uint32_t *template;
2404   unsigned int i;
2405
2406   /* Massage our args to the form they really have.  */
2407   stub_entry = (struct elf64_aarch64_stub_hash_entry *) gen_entry;
2408
2409   stub_sec = stub_entry->stub_sec;
2410
2411   /* Make a note of the offset within the stubs for this entry.  */
2412   stub_entry->stub_offset = stub_sec->size;
2413   loc = stub_sec->contents + stub_entry->stub_offset;
2414
2415   stub_bfd = stub_sec->owner;
2416
2417   /* This is the address of the stub destination.  */
2418   sym_value = (stub_entry->target_value
2419                + stub_entry->target_section->output_offset
2420                + stub_entry->target_section->output_section->vma);
2421
2422   if (stub_entry->stub_type == aarch64_stub_long_branch)
2423     {
2424       bfd_vma place = (stub_entry->stub_offset + stub_sec->output_section->vma
2425                        + stub_sec->output_offset);
2426
2427       /* See if we can relax the stub.  */
2428       if (aarch64_valid_for_adrp_p (sym_value, place))
2429         stub_entry->stub_type = aarch64_select_branch_stub (sym_value, place);
2430     }
2431
2432   switch (stub_entry->stub_type)
2433     {
2434     case aarch64_stub_adrp_branch:
2435       template = aarch64_adrp_branch_stub;
2436       template_size = sizeof (aarch64_adrp_branch_stub);
2437       break;
2438     case aarch64_stub_long_branch:
2439       template = aarch64_long_branch_stub;
2440       template_size = sizeof (aarch64_long_branch_stub);
2441       break;
2442     default:
2443       BFD_FAIL ();
2444       return FALSE;
2445     }
2446
2447   for (i = 0; i < (template_size / sizeof template[0]); i++)
2448     {
2449       bfd_putl32 (template[i], loc);
2450       loc += 4;
2451     }
2452
2453   template_size = (template_size + 7) & ~7;
2454   stub_sec->size += template_size;
2455
2456   switch (stub_entry->stub_type)
2457     {
2458     case aarch64_stub_adrp_branch:
2459       if (aarch64_relocate (R_AARCH64_ADR_PREL_PG_HI21, stub_bfd, stub_sec,
2460                             stub_entry->stub_offset, sym_value))
2461         /* The stub would not have been relaxed if the offset was out
2462            of range.  */
2463         BFD_FAIL ();
2464
2465       _bfd_final_link_relocate
2466         (elf64_aarch64_howto_from_type (R_AARCH64_ADD_ABS_LO12_NC),
2467          stub_bfd,
2468          stub_sec,
2469          stub_sec->contents,
2470          stub_entry->stub_offset + 4,
2471          sym_value,
2472          0);
2473       break;
2474
2475     case aarch64_stub_long_branch:
2476       /* We want the value relative to the address 12 bytes back from the
2477          value itself.  */
2478       _bfd_final_link_relocate (elf64_aarch64_howto_from_type
2479                                 (R_AARCH64_PREL64), stub_bfd, stub_sec,
2480                                 stub_sec->contents,
2481                                 stub_entry->stub_offset + 16,
2482                                 sym_value + 12, 0);
2483       break;
2484     default:
2485       break;
2486     }
2487
2488   return TRUE;
2489 }
2490
2491 /* As above, but don't actually build the stub.  Just bump offset so
2492    we know stub section sizes.  */
2493
2494 static bfd_boolean
2495 aarch64_size_one_stub (struct bfd_hash_entry *gen_entry,
2496                        void *in_arg ATTRIBUTE_UNUSED)
2497 {
2498   struct elf64_aarch64_stub_hash_entry *stub_entry;
2499   int size;
2500
2501   /* Massage our args to the form they really have.  */
2502   stub_entry = (struct elf64_aarch64_stub_hash_entry *) gen_entry;
2503
2504   switch (stub_entry->stub_type)
2505     {
2506     case aarch64_stub_adrp_branch:
2507       size = sizeof (aarch64_adrp_branch_stub);
2508       break;
2509     case aarch64_stub_long_branch:
2510       size = sizeof (aarch64_long_branch_stub);
2511       break;
2512     default:
2513       BFD_FAIL ();
2514       return FALSE;
2515       break;
2516     }
2517
2518   size = (size + 7) & ~7;
2519   stub_entry->stub_sec->size += size;
2520   return TRUE;
2521 }
2522
2523 /* External entry points for sizing and building linker stubs.  */
2524
2525 /* Set up various things so that we can make a list of input sections
2526    for each output section included in the link.  Returns -1 on error,
2527    0 when no stubs will be needed, and 1 on success.  */
2528
2529 int
2530 elf64_aarch64_setup_section_lists (bfd *output_bfd,
2531                                    struct bfd_link_info *info)
2532 {
2533   bfd *input_bfd;
2534   unsigned int bfd_count;
2535   int top_id, top_index;
2536   asection *section;
2537   asection **input_list, **list;
2538   bfd_size_type amt;
2539   struct elf64_aarch64_link_hash_table *htab =
2540     elf64_aarch64_hash_table (info);
2541
2542   if (!is_elf_hash_table (htab))
2543     return 0;
2544
2545   /* Count the number of input BFDs and find the top input section id.  */
2546   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2547        input_bfd != NULL; input_bfd = input_bfd->link_next)
2548     {
2549       bfd_count += 1;
2550       for (section = input_bfd->sections;
2551            section != NULL; section = section->next)
2552         {
2553           if (top_id < section->id)
2554             top_id = section->id;
2555         }
2556     }
2557   htab->bfd_count = bfd_count;
2558
2559   amt = sizeof (struct map_stub) * (top_id + 1);
2560   htab->stub_group = bfd_zmalloc (amt);
2561   if (htab->stub_group == NULL)
2562     return -1;
2563
2564   /* We can't use output_bfd->section_count here to find the top output
2565      section index as some sections may have been removed, and
2566      _bfd_strip_section_from_output doesn't renumber the indices.  */
2567   for (section = output_bfd->sections, top_index = 0;
2568        section != NULL; section = section->next)
2569     {
2570       if (top_index < section->index)
2571         top_index = section->index;
2572     }
2573
2574   htab->top_index = top_index;
2575   amt = sizeof (asection *) * (top_index + 1);
2576   input_list = bfd_malloc (amt);
2577   htab->input_list = input_list;
2578   if (input_list == NULL)
2579     return -1;
2580
2581   /* For sections we aren't interested in, mark their entries with a
2582      value we can check later.  */
2583   list = input_list + top_index;
2584   do
2585     *list = bfd_abs_section_ptr;
2586   while (list-- != input_list);
2587
2588   for (section = output_bfd->sections;
2589        section != NULL; section = section->next)
2590     {
2591       if ((section->flags & SEC_CODE) != 0)
2592         input_list[section->index] = NULL;
2593     }
2594
2595   return 1;
2596 }
2597
2598 /* Used by elf64_aarch64_next_input_section and group_sections.  */
2599 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2600
2601 /* The linker repeatedly calls this function for each input section,
2602    in the order that input sections are linked into output sections.
2603    Build lists of input sections to determine groupings between which
2604    we may insert linker stubs.  */
2605
2606 void
2607 elf64_aarch64_next_input_section (struct bfd_link_info *info, asection *isec)
2608 {
2609   struct elf64_aarch64_link_hash_table *htab =
2610     elf64_aarch64_hash_table (info);
2611
2612   if (isec->output_section->index <= htab->top_index)
2613     {
2614       asection **list = htab->input_list + isec->output_section->index;
2615
2616       if (*list != bfd_abs_section_ptr)
2617         {
2618           /* Steal the link_sec pointer for our list.  */
2619           /* This happens to make the list in reverse order,
2620              which is what we want.  */
2621           PREV_SEC (isec) = *list;
2622           *list = isec;
2623         }
2624     }
2625 }
2626
2627 /* See whether we can group stub sections together.  Grouping stub
2628    sections may result in fewer stubs.  More importantly, we need to
2629    put all .init* and .fini* stubs at the beginning of the .init or
2630    .fini output sections respectively, because glibc splits the
2631    _init and _fini functions into multiple parts.  Putting a stub in
2632    the middle of a function is not a good idea.  */
2633
2634 static void
2635 group_sections (struct elf64_aarch64_link_hash_table *htab,
2636                 bfd_size_type stub_group_size,
2637                 bfd_boolean stubs_always_before_branch)
2638 {
2639   asection **list = htab->input_list + htab->top_index;
2640
2641   do
2642     {
2643       asection *tail = *list;
2644
2645       if (tail == bfd_abs_section_ptr)
2646         continue;
2647
2648       while (tail != NULL)
2649         {
2650           asection *curr;
2651           asection *prev;
2652           bfd_size_type total;
2653
2654           curr = tail;
2655           total = tail->size;
2656           while ((prev = PREV_SEC (curr)) != NULL
2657                  && ((total += curr->output_offset - prev->output_offset)
2658                      < stub_group_size))
2659             curr = prev;
2660
2661           /* OK, the size from the start of CURR to the end is less
2662              than stub_group_size and thus can be handled by one stub
2663              section.  (Or the tail section is itself larger than
2664              stub_group_size, in which case we may be toast.)
2665              We should really be keeping track of the total size of
2666              stubs added here, as stubs contribute to the final output
2667              section size.  */
2668           do
2669             {
2670               prev = PREV_SEC (tail);
2671               /* Set up this stub group.  */
2672               htab->stub_group[tail->id].link_sec = curr;
2673             }
2674           while (tail != curr && (tail = prev) != NULL);
2675
2676           /* But wait, there's more!  Input sections up to stub_group_size
2677              bytes before the stub section can be handled by it too.  */
2678           if (!stubs_always_before_branch)
2679             {
2680               total = 0;
2681               while (prev != NULL
2682                      && ((total += tail->output_offset - prev->output_offset)
2683                          < stub_group_size))
2684                 {
2685                   tail = prev;
2686                   prev = PREV_SEC (tail);
2687                   htab->stub_group[tail->id].link_sec = curr;
2688                 }
2689             }
2690           tail = prev;
2691         }
2692     }
2693   while (list-- != htab->input_list);
2694
2695   free (htab->input_list);
2696 }
2697
2698 #undef PREV_SEC
2699
2700 /* Determine and set the size of the stub section for a final link.
2701
2702    The basic idea here is to examine all the relocations looking for
2703    PC-relative calls to a target that is unreachable with a "bl"
2704    instruction.  */
2705
2706 bfd_boolean
2707 elf64_aarch64_size_stubs (bfd *output_bfd,
2708                           bfd *stub_bfd,
2709                           struct bfd_link_info *info,
2710                           bfd_signed_vma group_size,
2711                           asection * (*add_stub_section) (const char *,
2712                                                           asection *),
2713                           void (*layout_sections_again) (void))
2714 {
2715   bfd_size_type stub_group_size;
2716   bfd_boolean stubs_always_before_branch;
2717   bfd_boolean stub_changed = 0;
2718   struct elf64_aarch64_link_hash_table *htab = elf64_aarch64_hash_table (info);
2719
2720   /* Propagate mach to stub bfd, because it may not have been
2721      finalized when we created stub_bfd.  */
2722   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
2723                      bfd_get_mach (output_bfd));
2724
2725   /* Stash our params away.  */
2726   htab->stub_bfd = stub_bfd;
2727   htab->add_stub_section = add_stub_section;
2728   htab->layout_sections_again = layout_sections_again;
2729   stubs_always_before_branch = group_size < 0;
2730   if (group_size < 0)
2731     stub_group_size = -group_size;
2732   else
2733     stub_group_size = group_size;
2734
2735   if (stub_group_size == 1)
2736     {
2737       /* Default values.  */
2738       /* Aarch64 branch range is +-128MB. The value used is 1MB less.  */
2739       stub_group_size = 127 * 1024 * 1024;
2740     }
2741
2742   group_sections (htab, stub_group_size, stubs_always_before_branch);
2743
2744   while (1)
2745     {
2746       bfd *input_bfd;
2747       unsigned int bfd_indx;
2748       asection *stub_sec;
2749
2750       for (input_bfd = info->input_bfds, bfd_indx = 0;
2751            input_bfd != NULL; input_bfd = input_bfd->link_next, bfd_indx++)
2752         {
2753           Elf_Internal_Shdr *symtab_hdr;
2754           asection *section;
2755           Elf_Internal_Sym *local_syms = NULL;
2756
2757           /* We'll need the symbol table in a second.  */
2758           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2759           if (symtab_hdr->sh_info == 0)
2760             continue;
2761
2762           /* Walk over each section attached to the input bfd.  */
2763           for (section = input_bfd->sections;
2764                section != NULL; section = section->next)
2765             {
2766               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2767
2768               /* If there aren't any relocs, then there's nothing more
2769                  to do.  */
2770               if ((section->flags & SEC_RELOC) == 0
2771                   || section->reloc_count == 0
2772                   || (section->flags & SEC_CODE) == 0)
2773                 continue;
2774
2775               /* If this section is a link-once section that will be
2776                  discarded, then don't create any stubs.  */
2777               if (section->output_section == NULL
2778                   || section->output_section->owner != output_bfd)
2779                 continue;
2780
2781               /* Get the relocs.  */
2782               internal_relocs
2783                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
2784                                              NULL, info->keep_memory);
2785               if (internal_relocs == NULL)
2786                 goto error_ret_free_local;
2787
2788               /* Now examine each relocation.  */
2789               irela = internal_relocs;
2790               irelaend = irela + section->reloc_count;
2791               for (; irela < irelaend; irela++)
2792                 {
2793                   unsigned int r_type, r_indx;
2794                   enum elf64_aarch64_stub_type stub_type;
2795                   struct elf64_aarch64_stub_hash_entry *stub_entry;
2796                   asection *sym_sec;
2797                   bfd_vma sym_value;
2798                   bfd_vma destination;
2799                   struct elf64_aarch64_link_hash_entry *hash;
2800                   const char *sym_name;
2801                   char *stub_name;
2802                   const asection *id_sec;
2803                   unsigned char st_type;
2804                   bfd_size_type len;
2805
2806                   r_type = ELF64_R_TYPE (irela->r_info);
2807                   r_indx = ELF64_R_SYM (irela->r_info);
2808
2809                   if (r_type >= (unsigned int) R_AARCH64_end)
2810                     {
2811                       bfd_set_error (bfd_error_bad_value);
2812                     error_ret_free_internal:
2813                       if (elf_section_data (section)->relocs == NULL)
2814                         free (internal_relocs);
2815                       goto error_ret_free_local;
2816                     }
2817
2818                   /* Only look for stubs on unconditional branch and
2819                      branch and link instructions.  */
2820                   if (r_type != (unsigned int) R_AARCH64_CALL26
2821                       && r_type != (unsigned int) R_AARCH64_JUMP26)
2822                     continue;
2823
2824                   /* Now determine the call target, its name, value,
2825                      section.  */
2826                   sym_sec = NULL;
2827                   sym_value = 0;
2828                   destination = 0;
2829                   hash = NULL;
2830                   sym_name = NULL;
2831                   if (r_indx < symtab_hdr->sh_info)
2832                     {
2833                       /* It's a local symbol.  */
2834                       Elf_Internal_Sym *sym;
2835                       Elf_Internal_Shdr *hdr;
2836
2837                       if (local_syms == NULL)
2838                         {
2839                           local_syms
2840                             = (Elf_Internal_Sym *) symtab_hdr->contents;
2841                           if (local_syms == NULL)
2842                             local_syms
2843                               = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2844                                                       symtab_hdr->sh_info, 0,
2845                                                       NULL, NULL, NULL);
2846                           if (local_syms == NULL)
2847                             goto error_ret_free_internal;
2848                         }
2849
2850                       sym = local_syms + r_indx;
2851                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2852                       sym_sec = hdr->bfd_section;
2853                       if (!sym_sec)
2854                         /* This is an undefined symbol.  It can never
2855                            be resolved.  */
2856                         continue;
2857
2858                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2859                         sym_value = sym->st_value;
2860                       destination = (sym_value + irela->r_addend
2861                                      + sym_sec->output_offset
2862                                      + sym_sec->output_section->vma);
2863                       st_type = ELF_ST_TYPE (sym->st_info);
2864                       sym_name
2865                         = bfd_elf_string_from_elf_section (input_bfd,
2866                                                            symtab_hdr->sh_link,
2867                                                            sym->st_name);
2868                     }
2869                   else
2870                     {
2871                       int e_indx;
2872
2873                       e_indx = r_indx - symtab_hdr->sh_info;
2874                       hash = ((struct elf64_aarch64_link_hash_entry *)
2875                               elf_sym_hashes (input_bfd)[e_indx]);
2876
2877                       while (hash->root.root.type == bfd_link_hash_indirect
2878                              || hash->root.root.type == bfd_link_hash_warning)
2879                         hash = ((struct elf64_aarch64_link_hash_entry *)
2880                                 hash->root.root.u.i.link);
2881
2882                       if (hash->root.root.type == bfd_link_hash_defined
2883                           || hash->root.root.type == bfd_link_hash_defweak)
2884                         {
2885                           struct elf64_aarch64_link_hash_table *globals =
2886                             elf64_aarch64_hash_table (info);
2887                           sym_sec = hash->root.root.u.def.section;
2888                           sym_value = hash->root.root.u.def.value;
2889                           /* For a destination in a shared library,
2890                              use the PLT stub as target address to
2891                              decide whether a branch stub is
2892                              needed.  */
2893                           if (globals->root.splt != NULL && hash != NULL
2894                               && hash->root.plt.offset != (bfd_vma) - 1)
2895                             {
2896                               sym_sec = globals->root.splt;
2897                               sym_value = hash->root.plt.offset;
2898                               if (sym_sec->output_section != NULL)
2899                                 destination = (sym_value
2900                                                + sym_sec->output_offset
2901                                                +
2902                                                sym_sec->output_section->vma);
2903                             }
2904                           else if (sym_sec->output_section != NULL)
2905                             destination = (sym_value + irela->r_addend
2906                                            + sym_sec->output_offset
2907                                            + sym_sec->output_section->vma);
2908                         }
2909                       else if (hash->root.root.type == bfd_link_hash_undefined
2910                                || (hash->root.root.type
2911                                    == bfd_link_hash_undefweak))
2912                         {
2913                           /* For a shared library, use the PLT stub as
2914                              target address to decide whether a long
2915                              branch stub is needed.
2916                              For absolute code, they cannot be handled.  */
2917                           struct elf64_aarch64_link_hash_table *globals =
2918                             elf64_aarch64_hash_table (info);
2919
2920                           if (globals->root.splt != NULL && hash != NULL
2921                               && hash->root.plt.offset != (bfd_vma) - 1)
2922                             {
2923                               sym_sec = globals->root.splt;
2924                               sym_value = hash->root.plt.offset;
2925                               if (sym_sec->output_section != NULL)
2926                                 destination = (sym_value
2927                                                + sym_sec->output_offset
2928                                                +
2929                                                sym_sec->output_section->vma);
2930                             }
2931                           else
2932                             continue;
2933                         }
2934                       else
2935                         {
2936                           bfd_set_error (bfd_error_bad_value);
2937                           goto error_ret_free_internal;
2938                         }
2939                       st_type = ELF_ST_TYPE (hash->root.type);
2940                       sym_name = hash->root.root.root.string;
2941                     }
2942
2943                   /* Determine what (if any) linker stub is needed.  */
2944                   stub_type = aarch64_type_of_stub
2945                     (info, section, irela, st_type, hash, destination);
2946                   if (stub_type == aarch64_stub_none)
2947                     continue;
2948
2949                   /* Support for grouping stub sections.  */
2950                   id_sec = htab->stub_group[section->id].link_sec;
2951
2952                   /* Get the name of this stub.  */
2953                   stub_name = elf64_aarch64_stub_name (id_sec, sym_sec, hash,
2954                                                        irela);
2955                   if (!stub_name)
2956                     goto error_ret_free_internal;
2957
2958                   stub_entry =
2959                     aarch64_stub_hash_lookup (&htab->stub_hash_table,
2960                                               stub_name, FALSE, FALSE);
2961                   if (stub_entry != NULL)
2962                     {
2963                       /* The proper stub has already been created.  */
2964                       free (stub_name);
2965                       continue;
2966                     }
2967
2968                   stub_entry = elf64_aarch64_add_stub (stub_name, section,
2969                                                        htab);
2970                   if (stub_entry == NULL)
2971                     {
2972                       free (stub_name);
2973                       goto error_ret_free_internal;
2974                     }
2975
2976                   stub_entry->target_value = sym_value;
2977                   stub_entry->target_section = sym_sec;
2978                   stub_entry->stub_type = stub_type;
2979                   stub_entry->h = hash;
2980                   stub_entry->st_type = st_type;
2981
2982                   if (sym_name == NULL)
2983                     sym_name = "unnamed";
2984                   len = sizeof (STUB_ENTRY_NAME) + strlen (sym_name);
2985                   stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
2986                   if (stub_entry->output_name == NULL)
2987                     {
2988                       free (stub_name);
2989                       goto error_ret_free_internal;
2990                     }
2991
2992                   snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
2993                             sym_name);
2994
2995                   stub_changed = TRUE;
2996                 }
2997
2998               /* We're done with the internal relocs, free them.  */
2999               if (elf_section_data (section)->relocs == NULL)
3000                 free (internal_relocs);
3001             }
3002         }
3003
3004       if (!stub_changed)
3005         break;
3006
3007       /* OK, we've added some stubs.  Find out the new size of the
3008          stub sections.  */
3009       for (stub_sec = htab->stub_bfd->sections;
3010            stub_sec != NULL; stub_sec = stub_sec->next)
3011         stub_sec->size = 0;
3012
3013       bfd_hash_traverse (&htab->stub_hash_table, aarch64_size_one_stub, htab);
3014
3015       /* Ask the linker to do its stuff.  */
3016       (*htab->layout_sections_again) ();
3017       stub_changed = FALSE;
3018     }
3019
3020   return TRUE;
3021
3022 error_ret_free_local:
3023   return FALSE;
3024 }
3025
3026 /* Build all the stubs associated with the current output file.  The
3027    stubs are kept in a hash table attached to the main linker hash
3028    table.  We also set up the .plt entries for statically linked PIC
3029    functions here.  This function is called via aarch64_elf_finish in the
3030    linker.  */
3031
3032 bfd_boolean
3033 elf64_aarch64_build_stubs (struct bfd_link_info *info)
3034 {
3035   asection *stub_sec;
3036   struct bfd_hash_table *table;
3037   struct elf64_aarch64_link_hash_table *htab;
3038
3039   htab = elf64_aarch64_hash_table (info);
3040
3041   for (stub_sec = htab->stub_bfd->sections;
3042        stub_sec != NULL; stub_sec = stub_sec->next)
3043     {
3044       bfd_size_type size;
3045
3046       /* Ignore non-stub sections.  */
3047       if (!strstr (stub_sec->name, STUB_SUFFIX))
3048         continue;
3049
3050       /* Allocate memory to hold the linker stubs.  */
3051       size = stub_sec->size;
3052       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3053       if (stub_sec->contents == NULL && size != 0)
3054         return FALSE;
3055       stub_sec->size = 0;
3056     }
3057
3058   /* Build the stubs as directed by the stub hash table.  */
3059   table = &htab->stub_hash_table;
3060   bfd_hash_traverse (table, aarch64_build_one_stub, info);
3061
3062   return TRUE;
3063 }
3064
3065
3066 /* Add an entry to the code/data map for section SEC.  */
3067
3068 static void
3069 elf64_aarch64_section_map_add (asection *sec, char type, bfd_vma vma)
3070 {
3071   struct _aarch64_elf_section_data *sec_data =
3072     elf64_aarch64_section_data (sec);
3073   unsigned int newidx;
3074
3075   if (sec_data->map == NULL)
3076     {
3077       sec_data->map = bfd_malloc (sizeof (elf64_aarch64_section_map));
3078       sec_data->mapcount = 0;
3079       sec_data->mapsize = 1;
3080     }
3081
3082   newidx = sec_data->mapcount++;
3083
3084   if (sec_data->mapcount > sec_data->mapsize)
3085     {
3086       sec_data->mapsize *= 2;
3087       sec_data->map = bfd_realloc_or_free
3088         (sec_data->map, sec_data->mapsize * sizeof (elf64_aarch64_section_map));
3089     }
3090
3091   if (sec_data->map)
3092     {
3093       sec_data->map[newidx].vma = vma;
3094       sec_data->map[newidx].type = type;
3095     }
3096 }
3097
3098
3099 /* Initialise maps of insn/data for input BFDs.  */
3100 void
3101 bfd_elf64_aarch64_init_maps (bfd *abfd)
3102 {
3103   Elf_Internal_Sym *isymbuf;
3104   Elf_Internal_Shdr *hdr;
3105   unsigned int i, localsyms;
3106
3107   /* Make sure that we are dealing with an AArch64 elf binary.  */
3108   if (!is_aarch64_elf (abfd))
3109     return;
3110
3111   if ((abfd->flags & DYNAMIC) != 0)
3112     return;
3113
3114   hdr = &elf_symtab_hdr (abfd);
3115   localsyms = hdr->sh_info;
3116
3117   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
3118      should contain the number of local symbols, which should come before any
3119      global symbols.  Mapping symbols are always local.  */
3120   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL, NULL);
3121
3122   /* No internal symbols read?  Skip this BFD.  */
3123   if (isymbuf == NULL)
3124     return;
3125
3126   for (i = 0; i < localsyms; i++)
3127     {
3128       Elf_Internal_Sym *isym = &isymbuf[i];
3129       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3130       const char *name;
3131
3132       if (sec != NULL && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
3133         {
3134           name = bfd_elf_string_from_elf_section (abfd,
3135                                                   hdr->sh_link,
3136                                                   isym->st_name);
3137
3138           if (bfd_is_aarch64_special_symbol_name
3139               (name, BFD_AARCH64_SPECIAL_SYM_TYPE_MAP))
3140             elf64_aarch64_section_map_add (sec, name[1], isym->st_value);
3141         }
3142     }
3143 }
3144
3145 /* Set option values needed during linking.  */
3146 void
3147 bfd_elf64_aarch64_set_options (struct bfd *output_bfd,
3148                                struct bfd_link_info *link_info,
3149                                int no_enum_warn,
3150                                int no_wchar_warn, int pic_veneer)
3151 {
3152   struct elf64_aarch64_link_hash_table *globals;
3153
3154   globals = elf64_aarch64_hash_table (link_info);
3155   globals->pic_veneer = pic_veneer;
3156
3157   BFD_ASSERT (is_aarch64_elf (output_bfd));
3158   elf_aarch64_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
3159   elf_aarch64_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
3160 }
3161
3162 #define MASK(n) ((1u << (n)) - 1)
3163
3164 /* Decode the 26-bit offset of unconditional branch.  */
3165 static inline uint32_t
3166 decode_branch_ofs_26 (uint32_t insn)
3167 {
3168   return insn & MASK (26);
3169 }
3170
3171 /* Decode the 19-bit offset of conditional branch and compare & branch.  */
3172 static inline uint32_t
3173 decode_cond_branch_ofs_19 (uint32_t insn)
3174 {
3175   return (insn >> 5) & MASK (19);
3176 }
3177
3178 /* Decode the 19-bit offset of load literal.  */
3179 static inline uint32_t
3180 decode_ld_lit_ofs_19 (uint32_t insn)
3181 {
3182   return (insn >> 5) & MASK (19);
3183 }
3184
3185 /* Decode the 14-bit offset of test & branch.  */
3186 static inline uint32_t
3187 decode_tst_branch_ofs_14 (uint32_t insn)
3188 {
3189   return (insn >> 5) & MASK (14);
3190 }
3191
3192 /* Decode the 16-bit imm of move wide.  */
3193 static inline uint32_t
3194 decode_movw_imm (uint32_t insn)
3195 {
3196   return (insn >> 5) & MASK (16);
3197 }
3198
3199 /* Decode the 21-bit imm of adr.  */
3200 static inline uint32_t
3201 decode_adr_imm (uint32_t insn)
3202 {
3203   return ((insn >> 29) & MASK (2)) | ((insn >> 3) & (MASK (19) << 2));
3204 }
3205
3206 /* Decode the 12-bit imm of add immediate.  */
3207 static inline uint32_t
3208 decode_add_imm (uint32_t insn)
3209 {
3210   return (insn >> 10) & MASK (12);
3211 }
3212
3213
3214 /* Encode the 26-bit offset of unconditional branch.  */
3215 static inline uint32_t
3216 reencode_branch_ofs_26 (uint32_t insn, uint32_t ofs)
3217 {
3218   return (insn & ~MASK (26)) | (ofs & MASK (26));
3219 }
3220
3221 /* Encode the 19-bit offset of conditional branch and compare & branch.  */
3222 static inline uint32_t
3223 reencode_cond_branch_ofs_19 (uint32_t insn, uint32_t ofs)
3224 {
3225   return (insn & ~(MASK (19) << 5)) | ((ofs & MASK (19)) << 5);
3226 }
3227
3228 /* Decode the 19-bit offset of load literal.  */
3229 static inline uint32_t
3230 reencode_ld_lit_ofs_19 (uint32_t insn, uint32_t ofs)
3231 {
3232   return (insn & ~(MASK (19) << 5)) | ((ofs & MASK (19)) << 5);
3233 }
3234
3235 /* Encode the 14-bit offset of test & branch.  */
3236 static inline uint32_t
3237 reencode_tst_branch_ofs_14 (uint32_t insn, uint32_t ofs)
3238 {
3239   return (insn & ~(MASK (14) << 5)) | ((ofs & MASK (14)) << 5);
3240 }
3241
3242 /* Reencode the imm field of move wide.  */
3243 static inline uint32_t
3244 reencode_movw_imm (uint32_t insn, uint32_t imm)
3245 {
3246   return (insn & ~(MASK (16) << 5)) | ((imm & MASK (16)) << 5);
3247 }
3248
3249 /* Reencode the imm field of adr.  */
3250 static inline uint32_t
3251 reencode_adr_imm (uint32_t insn, uint32_t imm)
3252 {
3253   return (insn & ~((MASK (2) << 29) | (MASK (19) << 5)))
3254     | ((imm & MASK (2)) << 29) | ((imm & (MASK (19) << 2)) << 3);
3255 }
3256
3257 /* Reencode the imm field of ld/st pos immediate.  */
3258 static inline uint32_t
3259 reencode_ldst_pos_imm (uint32_t insn, uint32_t imm)
3260 {
3261   return (insn & ~(MASK (12) << 10)) | ((imm & MASK (12)) << 10);
3262 }
3263
3264 /* Reencode the imm field of add immediate.  */
3265 static inline uint32_t
3266 reencode_add_imm (uint32_t insn, uint32_t imm)
3267 {
3268   return (insn & ~(MASK (12) << 10)) | ((imm & MASK (12)) << 10);
3269 }
3270
3271 /* Reencode mov[zn] to movz.  */
3272 static inline uint32_t
3273 reencode_movzn_to_movz (uint32_t opcode)
3274 {
3275   return opcode | (1 << 30);
3276 }
3277
3278 /* Reencode mov[zn] to movn.  */
3279 static inline uint32_t
3280 reencode_movzn_to_movn (uint32_t opcode)
3281 {
3282   return opcode & ~(1 << 30);
3283 }
3284
3285 /* Insert the addend/value into the instruction or data object being
3286    relocated.  */
3287 static bfd_reloc_status_type
3288 bfd_elf_aarch64_put_addend (bfd *abfd,
3289                             bfd_byte *address,
3290                             reloc_howto_type *howto, bfd_signed_vma addend)
3291 {
3292   bfd_reloc_status_type status = bfd_reloc_ok;
3293   bfd_signed_vma old_addend = addend;
3294   bfd_vma contents;
3295   int size;
3296
3297   size = bfd_get_reloc_size (howto);
3298   switch (size)
3299     {
3300     case 2:
3301       contents = bfd_get_16 (abfd, address);
3302       break;
3303     case 4:
3304       if (howto->src_mask != 0xffffffff)
3305         /* Must be 32-bit instruction, always little-endian.  */
3306         contents = bfd_getl32 (address);
3307       else
3308         /* Must be 32-bit data (endianness dependent).  */
3309         contents = bfd_get_32 (abfd, address);
3310       break;
3311     case 8:
3312       contents = bfd_get_64 (abfd, address);
3313       break;
3314     default:
3315       abort ();
3316     }
3317
3318   switch (howto->complain_on_overflow)
3319     {
3320     case complain_overflow_dont:
3321       break;
3322     case complain_overflow_signed:
3323       status = aarch64_signed_overflow (addend,
3324                                         howto->bitsize + howto->rightshift);
3325       break;
3326     case complain_overflow_unsigned:
3327       status = aarch64_unsigned_overflow (addend,
3328                                           howto->bitsize + howto->rightshift);
3329       break;
3330     case complain_overflow_bitfield:
3331     default:
3332       abort ();
3333     }
3334
3335   addend >>= howto->rightshift;
3336
3337   switch (howto->type)
3338     {
3339     case R_AARCH64_JUMP26:
3340     case R_AARCH64_CALL26:
3341       contents = reencode_branch_ofs_26 (contents, addend);
3342       break;
3343
3344     case R_AARCH64_CONDBR19:
3345       contents = reencode_cond_branch_ofs_19 (contents, addend);
3346       break;
3347
3348     case R_AARCH64_TSTBR14:
3349       contents = reencode_tst_branch_ofs_14 (contents, addend);
3350       break;
3351
3352     case R_AARCH64_LD_PREL_LO19:
3353     case R_AARCH64_GOT_LD_PREL19:
3354       if (old_addend & ((1 << howto->rightshift) - 1))
3355         return bfd_reloc_overflow;
3356       contents = reencode_ld_lit_ofs_19 (contents, addend);
3357       break;
3358
3359     case R_AARCH64_TLSDESC_CALL:
3360       break;
3361
3362     case R_AARCH64_TLSGD_ADR_PAGE21:
3363     case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3364     case R_AARCH64_TLSDESC_ADR_PAGE:
3365     case R_AARCH64_ADR_GOT_PAGE:
3366     case R_AARCH64_ADR_PREL_LO21:
3367     case R_AARCH64_ADR_PREL_PG_HI21:
3368     case R_AARCH64_ADR_PREL_PG_HI21_NC:
3369       contents = reencode_adr_imm (contents, addend);
3370       break;
3371
3372     case R_AARCH64_TLSGD_ADD_LO12_NC:
3373     case R_AARCH64_TLSLE_ADD_TPREL_LO12:
3374     case R_AARCH64_TLSLE_ADD_TPREL_HI12:
3375     case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
3376     case R_AARCH64_TLSDESC_ADD_LO12_NC:
3377     case R_AARCH64_ADD_ABS_LO12_NC:
3378       /* Corresponds to: add rd, rn, #uimm12 to provide the low order
3379          12 bits of the page offset following
3380          R_AARCH64_ADR_PREL_PG_HI21 which computes the
3381          (pc-relative) page base.  */
3382       contents = reencode_add_imm (contents, addend);
3383       break;
3384
3385     case R_AARCH64_LDST8_ABS_LO12_NC:
3386     case R_AARCH64_LDST16_ABS_LO12_NC:
3387     case R_AARCH64_LDST32_ABS_LO12_NC:
3388     case R_AARCH64_LDST64_ABS_LO12_NC:
3389     case R_AARCH64_LDST128_ABS_LO12_NC:
3390     case R_AARCH64_TLSDESC_LD64_LO12_NC:
3391     case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
3392     case R_AARCH64_LD64_GOT_LO12_NC:
3393       if (old_addend & ((1 << howto->rightshift) - 1))
3394         return bfd_reloc_overflow;
3395       /* Used for ldr*|str* rt, [rn, #uimm12] to provide the low order
3396          12 bits of the page offset following R_AARCH64_ADR_PREL_PG_HI21
3397          which computes the (pc-relative) page base.  */
3398       contents = reencode_ldst_pos_imm (contents, addend);
3399       break;
3400
3401       /* Group relocations to create high bits of a 16, 32, 48 or 64
3402          bit signed data or abs address inline. Will change
3403          instruction to MOVN or MOVZ depending on sign of calculated
3404          value.  */
3405
3406     case R_AARCH64_TLSLE_MOVW_TPREL_G2:
3407     case R_AARCH64_TLSLE_MOVW_TPREL_G1:
3408     case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
3409     case R_AARCH64_TLSLE_MOVW_TPREL_G0:
3410     case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
3411     case R_AARCH64_MOVW_SABS_G0:
3412     case R_AARCH64_MOVW_SABS_G1:
3413     case R_AARCH64_MOVW_SABS_G2:
3414       /* NOTE: We can only come here with movz or movn.  */
3415       if (addend < 0)
3416         {
3417           /* Force use of MOVN.  */
3418           addend = ~addend;
3419           contents = reencode_movzn_to_movn (contents);
3420         }
3421       else
3422         {
3423           /* Force use of MOVZ.  */
3424           contents = reencode_movzn_to_movz (contents);
3425         }
3426       /* fall through */
3427
3428       /* Group relocations to create a 16, 32, 48 or 64 bit unsigned
3429          data or abs address inline.  */
3430
3431     case R_AARCH64_MOVW_UABS_G0:
3432     case R_AARCH64_MOVW_UABS_G0_NC:
3433     case R_AARCH64_MOVW_UABS_G1:
3434     case R_AARCH64_MOVW_UABS_G1_NC:
3435     case R_AARCH64_MOVW_UABS_G2:
3436     case R_AARCH64_MOVW_UABS_G2_NC:
3437     case R_AARCH64_MOVW_UABS_G3:
3438       contents = reencode_movw_imm (contents, addend);
3439       break;
3440
3441     default:
3442       /* Repack simple data */
3443       if (howto->dst_mask & (howto->dst_mask + 1))
3444         return bfd_reloc_notsupported;
3445
3446       contents = ((contents & ~howto->dst_mask) | (addend & howto->dst_mask));
3447       break;
3448     }
3449
3450   switch (size)
3451     {
3452     case 2:
3453       bfd_put_16 (abfd, contents, address);
3454       break;
3455     case 4:
3456       if (howto->dst_mask != 0xffffffff)
3457         /* must be 32-bit instruction, always little-endian */
3458         bfd_putl32 (contents, address);
3459       else
3460         /* must be 32-bit data (endianness dependent) */
3461         bfd_put_32 (abfd, contents, address);
3462       break;
3463     case 8:
3464       bfd_put_64 (abfd, contents, address);
3465       break;
3466     default:
3467       abort ();
3468     }
3469
3470   return status;
3471 }
3472
3473 static bfd_vma
3474 aarch64_calculate_got_entry_vma (struct elf_link_hash_entry *h,
3475                                  struct elf64_aarch64_link_hash_table
3476                                  *globals, struct bfd_link_info *info,
3477                                  bfd_vma value, bfd *output_bfd,
3478                                  bfd_boolean *unresolved_reloc_p)
3479 {
3480   bfd_vma off = (bfd_vma) - 1;
3481   asection *basegot = globals->root.sgot;
3482   bfd_boolean dyn = globals->root.dynamic_sections_created;
3483
3484   if (h != NULL)
3485     {
3486       off = h->got.offset;
3487       BFD_ASSERT (off != (bfd_vma) - 1);
3488       if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3489           || (info->shared
3490               && SYMBOL_REFERENCES_LOCAL (info, h))
3491           || (ELF_ST_VISIBILITY (h->other)
3492               && h->root.type == bfd_link_hash_undefweak))
3493         {
3494           /* This is actually a static link, or it is a -Bsymbolic link
3495              and the symbol is defined locally.  We must initialize this
3496              entry in the global offset table.  Since the offset must
3497              always be a multiple of 8, we use the least significant bit
3498              to record whether we have initialized it already.
3499              When doing a dynamic link, we create a .rel(a).got relocation
3500              entry to initialize the value.  This is done in the
3501              finish_dynamic_symbol routine.  */
3502           if ((off & 1) != 0)
3503             off &= ~1;
3504           else
3505             {
3506               bfd_put_64 (output_bfd, value, basegot->contents + off);
3507               h->got.offset |= 1;
3508             }
3509         }
3510       else
3511         *unresolved_reloc_p = FALSE;
3512
3513       off = off + basegot->output_section->vma + basegot->output_offset;
3514     }
3515
3516   return off;
3517 }
3518
3519 /* Change R_TYPE to a more efficient access model where possible,
3520    return the new reloc type.  */
3521
3522 static unsigned int
3523 aarch64_tls_transition_without_check (unsigned int r_type,
3524                                       struct elf_link_hash_entry *h)
3525 {
3526   bfd_boolean is_local = h == NULL;
3527   switch (r_type)
3528     {
3529     case R_AARCH64_TLSGD_ADR_PAGE21:
3530     case R_AARCH64_TLSDESC_ADR_PAGE:
3531       return is_local
3532         ? R_AARCH64_TLSLE_MOVW_TPREL_G1 : R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21;
3533
3534     case R_AARCH64_TLSGD_ADD_LO12_NC:
3535     case R_AARCH64_TLSDESC_LD64_LO12_NC:
3536       return is_local
3537         ? R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
3538         : R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
3539
3540     case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3541       return is_local ? R_AARCH64_TLSLE_MOVW_TPREL_G1 : r_type;
3542
3543     case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
3544       return is_local ? R_AARCH64_TLSLE_MOVW_TPREL_G0_NC : r_type;
3545
3546     case R_AARCH64_TLSDESC_ADD_LO12_NC:
3547     case R_AARCH64_TLSDESC_CALL:
3548       /* Instructions with these relocations will become NOPs.  */
3549       return R_AARCH64_NONE;
3550     }
3551
3552   return r_type;
3553 }
3554
3555 static unsigned int
3556 aarch64_reloc_got_type (unsigned int r_type)
3557 {
3558   switch (r_type)
3559     {
3560     case R_AARCH64_LD64_GOT_LO12_NC:
3561     case R_AARCH64_ADR_GOT_PAGE:
3562     case R_AARCH64_GOT_LD_PREL19:
3563       return GOT_NORMAL;
3564
3565     case R_AARCH64_TLSGD_ADR_PAGE21:
3566     case R_AARCH64_TLSGD_ADD_LO12_NC:
3567       return GOT_TLS_GD;
3568
3569     case R_AARCH64_TLSDESC_ADD_LO12_NC:
3570     case R_AARCH64_TLSDESC_ADR_PAGE:
3571     case R_AARCH64_TLSDESC_CALL:
3572     case R_AARCH64_TLSDESC_LD64_LO12_NC:
3573       return GOT_TLSDESC_GD;
3574
3575     case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3576     case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
3577       return GOT_TLS_IE;
3578
3579     case R_AARCH64_TLSLE_ADD_TPREL_HI12:
3580     case R_AARCH64_TLSLE_ADD_TPREL_LO12:
3581     case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
3582     case R_AARCH64_TLSLE_MOVW_TPREL_G0:
3583     case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
3584     case R_AARCH64_TLSLE_MOVW_TPREL_G1:
3585     case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
3586     case R_AARCH64_TLSLE_MOVW_TPREL_G2:
3587       return GOT_UNKNOWN;
3588     }
3589   return GOT_UNKNOWN;
3590 }
3591
3592 static bfd_boolean
3593 aarch64_can_relax_tls (bfd *input_bfd,
3594                        struct bfd_link_info *info,
3595                        unsigned int r_type,
3596                        struct elf_link_hash_entry *h,
3597                        unsigned long r_symndx)
3598 {
3599   unsigned int symbol_got_type;
3600   unsigned int reloc_got_type;
3601
3602   if (! IS_AARCH64_TLS_RELOC (r_type))
3603     return FALSE;
3604
3605   symbol_got_type = elf64_aarch64_symbol_got_type (h, input_bfd, r_symndx);
3606   reloc_got_type = aarch64_reloc_got_type (r_type);
3607
3608   if (symbol_got_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (reloc_got_type))
3609     return TRUE;
3610
3611   if (info->shared)
3612     return FALSE;
3613
3614   if  (h && h->root.type == bfd_link_hash_undefweak)
3615     return FALSE;
3616
3617   return TRUE;
3618 }
3619
3620 static unsigned int
3621 aarch64_tls_transition (bfd *input_bfd,
3622                         struct bfd_link_info *info,
3623                         unsigned int r_type,
3624                         struct elf_link_hash_entry *h,
3625                         unsigned long r_symndx)
3626 {
3627   if (! aarch64_can_relax_tls (input_bfd, info, r_type, h, r_symndx))
3628     return r_type;
3629
3630   return aarch64_tls_transition_without_check (r_type, h);
3631 }
3632
3633 /* Return the base VMA address which should be subtracted from real addresses
3634    when resolving R_AARCH64_TLS_DTPREL64 relocation.  */
3635
3636 static bfd_vma
3637 dtpoff_base (struct bfd_link_info *info)
3638 {
3639   /* If tls_sec is NULL, we should have signalled an error already.  */
3640   BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
3641   return elf_hash_table (info)->tls_sec->vma;
3642 }
3643
3644
3645 /* Return the base VMA address which should be subtracted from real addresses
3646    when resolving R_AARCH64_TLS_GOTTPREL64 relocations.  */
3647
3648 static bfd_vma
3649 tpoff_base (struct bfd_link_info *info)
3650 {
3651   struct elf_link_hash_table *htab = elf_hash_table (info);
3652
3653   /* If tls_sec is NULL, we should have signalled an error already.  */
3654   if (htab->tls_sec == NULL)
3655     return 0;
3656
3657   bfd_vma base = align_power ((bfd_vma) TCB_SIZE,
3658                               htab->tls_sec->alignment_power);
3659   return htab->tls_sec->vma - base;
3660 }
3661
3662 static bfd_vma *
3663 symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
3664                        unsigned long r_symndx)
3665 {
3666   /* Calculate the address of the GOT entry for symbol
3667      referred to in h.  */
3668   if (h != NULL)
3669     return &h->got.offset;
3670   else
3671     {
3672       /* local symbol */
3673       struct elf_aarch64_local_symbol *l;
3674
3675       l = elf64_aarch64_locals (input_bfd);
3676       return &l[r_symndx].got_offset;
3677     }
3678 }
3679
3680 static void
3681 symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
3682                         unsigned long r_symndx)
3683 {
3684   bfd_vma *p;
3685   p = symbol_got_offset_ref (input_bfd, h, r_symndx);
3686   *p |= 1;
3687 }
3688
3689 static int
3690 symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
3691                           unsigned long r_symndx)
3692 {
3693   bfd_vma value;
3694   value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
3695   return value & 1;
3696 }
3697
3698 static bfd_vma
3699 symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
3700                    unsigned long r_symndx)
3701 {
3702   bfd_vma value;
3703   value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
3704   value &= ~1;
3705   return value;
3706 }
3707
3708 static bfd_vma *
3709 symbol_tlsdesc_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
3710                                unsigned long r_symndx)
3711 {
3712   /* Calculate the address of the GOT entry for symbol
3713      referred to in h.  */
3714   if (h != NULL)
3715     {
3716       struct elf64_aarch64_link_hash_entry *eh;
3717       eh = (struct elf64_aarch64_link_hash_entry *) h;
3718       return &eh->tlsdesc_got_jump_table_offset;
3719     }
3720   else
3721     {
3722       /* local symbol */
3723       struct elf_aarch64_local_symbol *l;
3724
3725       l = elf64_aarch64_locals (input_bfd);
3726       return &l[r_symndx].tlsdesc_got_jump_table_offset;
3727     }
3728 }
3729
3730 static void
3731 symbol_tlsdesc_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
3732                                 unsigned long r_symndx)
3733 {
3734   bfd_vma *p;
3735   p = symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
3736   *p |= 1;
3737 }
3738
3739 static int
3740 symbol_tlsdesc_got_offset_mark_p (bfd *input_bfd,
3741                                   struct elf_link_hash_entry *h,
3742                                   unsigned long r_symndx)
3743 {
3744   bfd_vma value;
3745   value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
3746   return value & 1;
3747 }
3748
3749 static bfd_vma
3750 symbol_tlsdesc_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
3751                           unsigned long r_symndx)
3752 {
3753   bfd_vma value;
3754   value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
3755   value &= ~1;
3756   return value;
3757 }
3758
3759 /* Perform a relocation as part of a final link.  */
3760 static bfd_reloc_status_type
3761 elf64_aarch64_final_link_relocate (reloc_howto_type *howto,
3762                                    bfd *input_bfd,
3763                                    bfd *output_bfd,
3764                                    asection *input_section,
3765                                    bfd_byte *contents,
3766                                    Elf_Internal_Rela *rel,
3767                                    bfd_vma value,
3768                                    struct bfd_link_info *info,
3769                                    asection *sym_sec,
3770                                    struct elf_link_hash_entry *h,
3771                                    bfd_boolean *unresolved_reloc_p,
3772                                    bfd_boolean save_addend,
3773                                    bfd_vma *saved_addend)
3774 {
3775   unsigned int r_type = howto->type;
3776   unsigned long r_symndx;
3777   bfd_byte *hit_data = contents + rel->r_offset;
3778   bfd_vma place;
3779   bfd_signed_vma signed_addend;
3780   struct elf64_aarch64_link_hash_table *globals;
3781   bfd_boolean weak_undef_p;
3782
3783   globals = elf64_aarch64_hash_table (info);
3784
3785   BFD_ASSERT (is_aarch64_elf (input_bfd));
3786
3787   r_symndx = ELF64_R_SYM (rel->r_info);
3788
3789   /* It is possible to have linker relaxations on some TLS access
3790      models.  Update our information here.  */
3791   r_type = aarch64_tls_transition (input_bfd, info, r_type, h, r_symndx);
3792
3793   if (r_type != howto->type)
3794     howto = elf64_aarch64_howto_from_type (r_type);
3795
3796   place = input_section->output_section->vma
3797     + input_section->output_offset + rel->r_offset;
3798
3799   /* Get addend, accumulating the addend for consecutive relocs
3800      which refer to the same offset.  */
3801   signed_addend = saved_addend ? *saved_addend : 0;
3802   signed_addend += rel->r_addend;
3803
3804   weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
3805                   : bfd_is_und_section (sym_sec));
3806   switch (r_type)
3807     {
3808     case R_AARCH64_NONE:
3809     case R_AARCH64_NULL:
3810     case R_AARCH64_TLSDESC_CALL:
3811       *unresolved_reloc_p = FALSE;
3812       return bfd_reloc_ok;
3813
3814     case R_AARCH64_ABS64:
3815
3816       /* When generating a shared object or relocatable executable, these
3817          relocations are copied into the output file to be resolved at
3818          run time.  */
3819       if (((info->shared == TRUE) || globals->root.is_relocatable_executable)
3820           && (input_section->flags & SEC_ALLOC)
3821           && (h == NULL
3822               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3823               || h->root.type != bfd_link_hash_undefweak))
3824         {
3825           Elf_Internal_Rela outrel;
3826           bfd_byte *loc;
3827           bfd_boolean skip, relocate;
3828           asection *sreloc;
3829
3830           *unresolved_reloc_p = FALSE;
3831
3832           sreloc = _bfd_elf_get_dynamic_reloc_section (input_bfd,
3833                                                        input_section, 1);
3834           if (sreloc == NULL)
3835             return bfd_reloc_notsupported;
3836
3837           skip = FALSE;
3838           relocate = FALSE;
3839
3840           outrel.r_addend = signed_addend;
3841           outrel.r_offset =
3842             _bfd_elf_section_offset (output_bfd, info, input_section,
3843                                      rel->r_offset);
3844           if (outrel.r_offset == (bfd_vma) - 1)
3845             skip = TRUE;
3846           else if (outrel.r_offset == (bfd_vma) - 2)
3847             {
3848               skip = TRUE;
3849               relocate = TRUE;
3850             }
3851
3852           outrel.r_offset += (input_section->output_section->vma
3853                               + input_section->output_offset);
3854
3855           if (skip)
3856             memset (&outrel, 0, sizeof outrel);
3857           else if (h != NULL
3858                    && h->dynindx != -1
3859                    && (!info->shared || !info->symbolic || !h->def_regular))
3860             outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3861           else
3862             {
3863               int symbol;
3864
3865               /* On SVR4-ish systems, the dynamic loader cannot
3866                  relocate the text and data segments independently,
3867                  so the symbol does not matter.  */
3868               symbol = 0;
3869               outrel.r_info = ELF64_R_INFO (symbol, R_AARCH64_RELATIVE);
3870               outrel.r_addend += value;
3871             }
3872
3873           loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (htab);
3874           bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3875
3876           if (sreloc->reloc_count * RELOC_SIZE (htab) > sreloc->size)
3877             {
3878               /* Sanity to check that we have previously allocated
3879                  sufficient space in the relocation section for the
3880                  number of relocations we actually want to emit.  */
3881               abort ();
3882             }
3883
3884           /* If this reloc is against an external symbol, we do not want to
3885              fiddle with the addend.  Otherwise, we need to include the symbol
3886              value so that it becomes an addend for the dynamic reloc.  */
3887           if (!relocate)
3888             return bfd_reloc_ok;
3889
3890           return _bfd_final_link_relocate (howto, input_bfd, input_section,
3891                                            contents, rel->r_offset, value,
3892                                            signed_addend);
3893         }
3894       else
3895         value += signed_addend;
3896       break;
3897
3898     case R_AARCH64_JUMP26:
3899     case R_AARCH64_CALL26:
3900       {
3901         asection *splt = globals->root.splt;
3902         bfd_boolean via_plt_p =
3903           splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
3904
3905         /* A call to an undefined weak symbol is converted to a jump to
3906            the next instruction unless a PLT entry will be created.
3907            The jump to the next instruction is optimized as a NOP.
3908            Do the same for local undefined symbols.  */
3909         if (weak_undef_p && ! via_plt_p)
3910           {
3911             bfd_putl32 (INSN_NOP, hit_data);
3912             return bfd_reloc_ok;
3913           }
3914
3915         /* If the call goes through a PLT entry, make sure to
3916            check distance to the right destination address.  */
3917         if (via_plt_p)
3918           {
3919             value = (splt->output_section->vma
3920                      + splt->output_offset + h->plt.offset);
3921             *unresolved_reloc_p = FALSE;
3922           }
3923
3924         /* If the target symbol is global and marked as a function the
3925            relocation applies a function call or a tail call.  In this
3926            situation we can veneer out of range branches.  The veneers
3927            use IP0 and IP1 hence cannot be used arbitrary out of range
3928            branches that occur within the body of a function.  */
3929         if (h && h->type == STT_FUNC)
3930           {
3931             /* Check if a stub has to be inserted because the destination
3932                is too far away.  */
3933             if (! aarch64_valid_branch_p (value, place))
3934               {
3935                 /* The target is out of reach, so redirect the branch to
3936                    the local stub for this function.  */
3937                 struct elf64_aarch64_stub_hash_entry *stub_entry;
3938                 stub_entry = elf64_aarch64_get_stub_entry (input_section,
3939                                                            sym_sec, h,
3940                                                            rel, globals);
3941                 if (stub_entry != NULL)
3942                   value = (stub_entry->stub_offset
3943                            + stub_entry->stub_sec->output_offset
3944                            + stub_entry->stub_sec->output_section->vma);
3945               }
3946           }
3947       }
3948       value = aarch64_resolve_relocation (r_type, place, value,
3949                                           signed_addend, weak_undef_p);
3950       break;
3951
3952     case R_AARCH64_ABS16:
3953     case R_AARCH64_ABS32:
3954     case R_AARCH64_ADD_ABS_LO12_NC:
3955     case R_AARCH64_ADR_PREL_LO21:
3956     case R_AARCH64_ADR_PREL_PG_HI21:
3957     case R_AARCH64_ADR_PREL_PG_HI21_NC:
3958     case R_AARCH64_CONDBR19:
3959     case R_AARCH64_LD_PREL_LO19:
3960     case R_AARCH64_LDST8_ABS_LO12_NC:
3961     case R_AARCH64_LDST16_ABS_LO12_NC:
3962     case R_AARCH64_LDST32_ABS_LO12_NC:
3963     case R_AARCH64_LDST64_ABS_LO12_NC:
3964     case R_AARCH64_LDST128_ABS_LO12_NC:
3965     case R_AARCH64_MOVW_SABS_G0:
3966     case R_AARCH64_MOVW_SABS_G1:
3967     case R_AARCH64_MOVW_SABS_G2:
3968     case R_AARCH64_MOVW_UABS_G0:
3969     case R_AARCH64_MOVW_UABS_G0_NC:
3970     case R_AARCH64_MOVW_UABS_G1:
3971     case R_AARCH64_MOVW_UABS_G1_NC:
3972     case R_AARCH64_MOVW_UABS_G2:
3973     case R_AARCH64_MOVW_UABS_G2_NC:
3974     case R_AARCH64_MOVW_UABS_G3:
3975     case R_AARCH64_PREL16:
3976     case R_AARCH64_PREL32:
3977     case R_AARCH64_PREL64:
3978     case R_AARCH64_TSTBR14:
3979       value = aarch64_resolve_relocation (r_type, place, value,
3980                                           signed_addend, weak_undef_p);
3981       break;
3982
3983     case R_AARCH64_LD64_GOT_LO12_NC:
3984     case R_AARCH64_ADR_GOT_PAGE:
3985     case R_AARCH64_GOT_LD_PREL19:
3986       if (globals->root.sgot == NULL)
3987         BFD_ASSERT (h != NULL);
3988
3989       if (h != NULL)
3990         {
3991           value = aarch64_calculate_got_entry_vma (h, globals, info, value,
3992                                                    output_bfd,
3993                                                    unresolved_reloc_p);
3994           value = aarch64_resolve_relocation (r_type, place, value,
3995                                               0, weak_undef_p);
3996         }
3997       break;
3998
3999     case R_AARCH64_TLSGD_ADR_PAGE21:
4000     case R_AARCH64_TLSGD_ADD_LO12_NC:
4001     case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4002     case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4003       if (globals->root.sgot == NULL)
4004         return bfd_reloc_notsupported;
4005
4006       value = (symbol_got_offset (input_bfd, h, r_symndx)
4007                + globals->root.sgot->output_section->vma
4008                + globals->root.sgot->output_section->output_offset);
4009
4010       value = aarch64_resolve_relocation (r_type, place, value,
4011                                           0, weak_undef_p);
4012       *unresolved_reloc_p = FALSE;
4013       break;
4014
4015     case R_AARCH64_TLSLE_ADD_TPREL_HI12:
4016     case R_AARCH64_TLSLE_ADD_TPREL_LO12:
4017     case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4018     case R_AARCH64_TLSLE_MOVW_TPREL_G0:
4019     case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4020     case R_AARCH64_TLSLE_MOVW_TPREL_G1:
4021     case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4022     case R_AARCH64_TLSLE_MOVW_TPREL_G2:
4023       value = aarch64_resolve_relocation (r_type, place, value,
4024                                           signed_addend - tpoff_base (info), weak_undef_p);
4025       *unresolved_reloc_p = FALSE;
4026       break;
4027
4028     case R_AARCH64_TLSDESC_ADR_PAGE:
4029     case R_AARCH64_TLSDESC_LD64_LO12_NC:
4030     case R_AARCH64_TLSDESC_ADD_LO12_NC:
4031     case R_AARCH64_TLSDESC_ADD:
4032     case R_AARCH64_TLSDESC_LDR:
4033       if (globals->root.sgot == NULL)
4034         return bfd_reloc_notsupported;
4035
4036       value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
4037                + globals->root.sgotplt->output_section->vma
4038                + globals->root.sgotplt->output_section->output_offset
4039                + globals->sgotplt_jump_table_size);
4040
4041       value = aarch64_resolve_relocation (r_type, place, value,
4042                                           0, weak_undef_p);
4043       *unresolved_reloc_p = FALSE;
4044       break;
4045
4046     default:
4047       return bfd_reloc_notsupported;
4048     }
4049
4050   if (saved_addend)
4051     *saved_addend = value;
4052
4053   /* Only apply the final relocation in a sequence.  */
4054   if (save_addend)
4055     return bfd_reloc_continue;
4056
4057   return bfd_elf_aarch64_put_addend (input_bfd, hit_data, howto, value);
4058 }
4059
4060 /* Handle TLS relaxations.  Relaxing is possible for symbols that use
4061    R_AARCH64_TLSDESC_ADR_{PAGE, LD64_LO12_NC, ADD_LO12_NC} during a static
4062    link.
4063
4064    Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
4065    is to then call final_link_relocate.  Return other values in the
4066    case of error.  */
4067
4068 static bfd_reloc_status_type
4069 elf64_aarch64_tls_relax (struct elf64_aarch64_link_hash_table *globals,
4070                          bfd *input_bfd, bfd_byte *contents,
4071                          Elf_Internal_Rela *rel, struct elf_link_hash_entry *h)
4072 {
4073   bfd_boolean is_local = h == NULL;
4074   unsigned int r_type = ELF64_R_TYPE (rel->r_info);
4075   unsigned long insn;
4076
4077   BFD_ASSERT (globals && input_bfd && contents && rel);
4078
4079   switch (r_type)
4080     {
4081     case R_AARCH64_TLSGD_ADR_PAGE21:
4082     case R_AARCH64_TLSDESC_ADR_PAGE:
4083       if (is_local)
4084         {
4085           /* GD->LE relaxation:
4086              adrp x0, :tlsgd:var     =>   movz x0, :tprel_g1:var
4087              or
4088              adrp x0, :tlsdesc:var   =>   movz x0, :tprel_g1:var
4089            */
4090           bfd_putl32 (0xd2a00000, contents + rel->r_offset);
4091           return bfd_reloc_continue;
4092         }
4093       else
4094         {
4095           /* GD->IE relaxation:
4096              adrp x0, :tlsgd:var     =>   adrp x0, :gottprel:var
4097              or
4098              adrp x0, :tlsdesc:var   =>   adrp x0, :gottprel:var
4099            */
4100           insn = bfd_getl32 (contents + rel->r_offset);
4101           return bfd_reloc_continue;
4102         }
4103
4104     case R_AARCH64_TLSDESC_LD64_LO12_NC:
4105       if (is_local)
4106         {
4107           /* GD->LE relaxation:
4108              ldr xd, [x0, #:tlsdesc_lo12:var]   =>   movk x0, :tprel_g0_nc:var
4109            */
4110           bfd_putl32 (0xf2800000, contents + rel->r_offset);
4111           return bfd_reloc_continue;
4112         }
4113       else
4114         {
4115           /* GD->IE relaxation:
4116              ldr xd, [x0, #:tlsdesc_lo12:var] => ldr x0, [x0, #:gottprel_lo12:var]
4117            */
4118           insn = bfd_getl32 (contents + rel->r_offset);
4119           insn &= 0xfffffff0;
4120           bfd_putl32 (insn, contents + rel->r_offset);
4121           return bfd_reloc_continue;
4122         }
4123
4124     case R_AARCH64_TLSGD_ADD_LO12_NC:
4125       if (is_local)
4126         {
4127           /* GD->LE relaxation
4128              add  x0, #:tlsgd_lo12:var  => movk x0, :tprel_g0_nc:var
4129              bl   __tls_get_addr        => mrs  x1, tpidr_el0
4130              nop                        => add  x0, x1, x0
4131            */
4132
4133           /* First kill the tls_get_addr reloc on the bl instruction.  */
4134           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
4135           rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
4136
4137           bfd_putl32 (0xf2800000, contents + rel->r_offset);
4138           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
4139           bfd_putl32 (0x8b000020, contents + rel->r_offset + 8);
4140           return bfd_reloc_continue;
4141         }
4142       else
4143         {
4144           /* GD->IE relaxation
4145              ADD  x0, #:tlsgd_lo12:var  => ldr  x0, [x0, #:gottprel_lo12:var]
4146              BL   __tls_get_addr        => mrs  x1, tpidr_el0
4147                R_AARCH64_CALL26
4148              NOP                        => add  x0, x1, x0
4149            */
4150
4151           BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_AARCH64_CALL26);
4152
4153           /* Remove the relocation on the BL instruction.  */
4154           rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
4155
4156           bfd_putl32 (0xf9400000, contents + rel->r_offset);
4157
4158           /* We choose to fixup the BL and NOP instructions using the
4159              offset from the second relocation to allow flexibility in
4160              scheduling instructions between the ADD and BL.  */
4161           bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
4162           bfd_putl32 (0x8b000020, contents + rel[1].r_offset + 4);
4163           return bfd_reloc_continue;
4164         }
4165
4166     case R_AARCH64_TLSDESC_ADD_LO12_NC:
4167     case R_AARCH64_TLSDESC_CALL:
4168       /* GD->IE/LE relaxation:
4169          add x0, x0, #:tlsdesc_lo12:var   =>   nop
4170          blr xd                           =>   nop
4171        */
4172       bfd_putl32 (INSN_NOP, contents + rel->r_offset);
4173       return bfd_reloc_ok;
4174
4175     case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4176       /* IE->LE relaxation:
4177          adrp xd, :gottprel:var   =>   movz xd, :tprel_g1:var
4178        */
4179       if (is_local)
4180         {
4181           insn = bfd_getl32 (contents + rel->r_offset);
4182           bfd_putl32 (0xd2a00000 | (insn & 0x1f), contents + rel->r_offset);
4183         }
4184       return bfd_reloc_continue;
4185
4186     case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4187       /* IE->LE relaxation:
4188          ldr  xd, [xm, #:gottprel_lo12:var]   =>   movk xd, :tprel_g0_nc:var
4189        */
4190       if (is_local)
4191         {
4192           insn = bfd_getl32 (contents + rel->r_offset);
4193           bfd_putl32 (0xf2800000 | (insn & 0x1f), contents + rel->r_offset);
4194         }
4195       return bfd_reloc_continue;
4196
4197     default:
4198       return bfd_reloc_continue;
4199     }
4200
4201   return bfd_reloc_ok;
4202 }
4203
4204 /* Relocate an AArch64 ELF section.  */
4205
4206 static bfd_boolean
4207 elf64_aarch64_relocate_section (bfd *output_bfd,
4208                                 struct bfd_link_info *info,
4209                                 bfd *input_bfd,
4210                                 asection *input_section,
4211                                 bfd_byte *contents,
4212                                 Elf_Internal_Rela *relocs,
4213                                 Elf_Internal_Sym *local_syms,
4214                                 asection **local_sections)
4215 {
4216   Elf_Internal_Shdr *symtab_hdr;
4217   struct elf_link_hash_entry **sym_hashes;
4218   Elf_Internal_Rela *rel;
4219   Elf_Internal_Rela *relend;
4220   const char *name;
4221   struct elf64_aarch64_link_hash_table *globals;
4222   bfd_boolean save_addend = FALSE;
4223   bfd_vma addend = 0;
4224
4225   globals = elf64_aarch64_hash_table (info);
4226
4227   symtab_hdr = &elf_symtab_hdr (input_bfd);
4228   sym_hashes = elf_sym_hashes (input_bfd);
4229
4230   rel = relocs;
4231   relend = relocs + input_section->reloc_count;
4232   for (; rel < relend; rel++)
4233     {
4234       unsigned int r_type;
4235       unsigned int relaxed_r_type;
4236       reloc_howto_type *howto;
4237       unsigned long r_symndx;
4238       Elf_Internal_Sym *sym;
4239       asection *sec;
4240       struct elf_link_hash_entry *h;
4241       bfd_vma relocation;
4242       bfd_reloc_status_type r;
4243       arelent bfd_reloc;
4244       char sym_type;
4245       bfd_boolean unresolved_reloc = FALSE;
4246       char *error_message = NULL;
4247
4248       r_symndx = ELF64_R_SYM (rel->r_info);
4249       r_type = ELF64_R_TYPE (rel->r_info);
4250
4251       bfd_reloc.howto = elf64_aarch64_howto_from_type (r_type);
4252       howto = bfd_reloc.howto;
4253
4254       if (howto == NULL)
4255         {
4256           (*_bfd_error_handler)
4257             (_("%B: unrecognized relocation (0x%x) in section `%A'"),
4258              input_bfd, input_section, r_type);
4259           return FALSE;
4260         }
4261
4262       h = NULL;
4263       sym = NULL;
4264       sec = NULL;
4265
4266       if (r_symndx < symtab_hdr->sh_info)
4267         {
4268           sym = local_syms + r_symndx;
4269           sym_type = ELF64_ST_TYPE (sym->st_info);
4270           sec = local_sections[r_symndx];
4271
4272           /* An object file might have a reference to a local
4273              undefined symbol.  This is a daft object file, but we
4274              should at least do something about it.  */
4275           if (r_type != R_AARCH64_NONE && r_type != R_AARCH64_NULL
4276               && bfd_is_und_section (sec)
4277               && ELF_ST_BIND (sym->st_info) != STB_WEAK)
4278             {
4279               if (!info->callbacks->undefined_symbol
4280                   (info, bfd_elf_string_from_elf_section
4281                    (input_bfd, symtab_hdr->sh_link, sym->st_name),
4282                    input_bfd, input_section, rel->r_offset, TRUE))
4283                 return FALSE;
4284             }
4285
4286           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4287         }
4288       else
4289         {
4290           bfd_boolean warned;
4291
4292           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4293                                    r_symndx, symtab_hdr, sym_hashes,
4294                                    h, sec, relocation,
4295                                    unresolved_reloc, warned);
4296
4297           sym_type = h->type;
4298         }
4299
4300       if (sec != NULL && discarded_section (sec))
4301         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4302                                          rel, 1, relend, howto, 0, contents);
4303
4304       if (info->relocatable)
4305         {
4306           /* This is a relocatable link.  We don't have to change
4307              anything, unless the reloc is against a section symbol,
4308              in which case we have to adjust according to where the
4309              section symbol winds up in the output section.  */
4310           if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4311             rel->r_addend += sec->output_offset;
4312           continue;
4313         }
4314
4315       if (h != NULL)
4316         name = h->root.root.string;
4317       else
4318         {
4319           name = (bfd_elf_string_from_elf_section
4320                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
4321           if (name == NULL || *name == '\0')
4322             name = bfd_section_name (input_bfd, sec);
4323         }
4324
4325       if (r_symndx != 0
4326           && r_type != R_AARCH64_NONE
4327           && r_type != R_AARCH64_NULL
4328           && (h == NULL
4329               || h->root.type == bfd_link_hash_defined
4330               || h->root.type == bfd_link_hash_defweak)
4331           && IS_AARCH64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
4332         {
4333           (*_bfd_error_handler)
4334             ((sym_type == STT_TLS
4335               ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
4336               : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
4337              input_bfd,
4338              input_section, (long) rel->r_offset, howto->name, name);
4339         }
4340
4341
4342       /* We relax only if we can see that there can be a valid transition
4343          from a reloc type to another.
4344          We call elf64_aarch64_final_link_relocate unless we're completely
4345          done, i.e., the relaxation produced the final output we want.  */
4346
4347       relaxed_r_type = aarch64_tls_transition (input_bfd, info, r_type,
4348                                                h, r_symndx);
4349       if (relaxed_r_type != r_type)
4350         {
4351           r_type = relaxed_r_type;
4352           howto = elf64_aarch64_howto_from_type (r_type);
4353
4354           r = elf64_aarch64_tls_relax (globals, input_bfd, contents, rel, h);
4355           unresolved_reloc = 0;
4356         }
4357       else
4358         r = bfd_reloc_continue;
4359
4360       /* There may be multiple consecutive relocations for the
4361          same offset.  In that case we are supposed to treat the
4362          output of each relocation as the addend for the next.  */
4363       if (rel + 1 < relend
4364           && rel->r_offset == rel[1].r_offset
4365           && ELF64_R_TYPE (rel[1].r_info) != R_AARCH64_NONE
4366           && ELF64_R_TYPE (rel[1].r_info) != R_AARCH64_NULL)
4367         save_addend = TRUE;
4368       else
4369         save_addend = FALSE;
4370
4371       if (r == bfd_reloc_continue)
4372         r = elf64_aarch64_final_link_relocate (howto, input_bfd, output_bfd,
4373                                                input_section, contents, rel,
4374                                                relocation, info, sec,
4375                                                h, &unresolved_reloc,
4376                                                save_addend, &addend);
4377
4378       switch (r_type)
4379         {
4380         case R_AARCH64_TLSGD_ADR_PAGE21:
4381         case R_AARCH64_TLSGD_ADD_LO12_NC:
4382           if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
4383             {
4384               bfd_boolean need_relocs = FALSE;
4385               bfd_byte *loc;
4386               int indx;
4387               bfd_vma off;
4388
4389               off = symbol_got_offset (input_bfd, h, r_symndx);
4390               indx = h && h->dynindx != -1 ? h->dynindx : 0;
4391
4392               need_relocs =
4393                 (info->shared || indx != 0) &&
4394                 (h == NULL
4395                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4396                  || h->root.type != bfd_link_hash_undefweak);
4397
4398               BFD_ASSERT (globals->root.srelgot != NULL);
4399
4400               if (need_relocs)
4401                 {
4402                   Elf_Internal_Rela rela;
4403                   rela.r_info = ELF64_R_INFO (indx, R_AARCH64_TLS_DTPMOD64);
4404                   rela.r_addend = 0;
4405                   rela.r_offset = globals->root.sgot->output_section->vma +
4406                     globals->root.sgot->output_offset + off;
4407
4408
4409                   loc = globals->root.srelgot->contents;
4410                   loc += globals->root.srelgot->reloc_count++
4411                     * RELOC_SIZE (htab);
4412                   bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
4413
4414                   if (indx == 0)
4415                     {
4416                       bfd_put_64 (output_bfd,
4417                                   relocation - dtpoff_base (info),
4418                                   globals->root.sgot->contents + off
4419                                   + GOT_ENTRY_SIZE);
4420                     }
4421                   else
4422                     {
4423                       /* This TLS symbol is global. We emit a
4424                          relocation to fixup the tls offset at load
4425                          time.  */
4426                       rela.r_info =
4427                         ELF64_R_INFO (indx, R_AARCH64_TLS_DTPREL64);
4428                       rela.r_addend = 0;
4429                       rela.r_offset =
4430                         (globals->root.sgot->output_section->vma
4431                          + globals->root.sgot->output_offset + off
4432                          + GOT_ENTRY_SIZE);
4433
4434                       loc = globals->root.srelgot->contents;
4435                       loc += globals->root.srelgot->reloc_count++
4436                         * RELOC_SIZE (globals);
4437                       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
4438                       bfd_put_64 (output_bfd, (bfd_vma) 0,
4439                                   globals->root.sgot->contents + off
4440                                   + GOT_ENTRY_SIZE);
4441                     }
4442                 }
4443               else
4444                 {
4445                   bfd_put_64 (output_bfd, (bfd_vma) 1,
4446                               globals->root.sgot->contents + off);
4447                   bfd_put_64 (output_bfd,
4448                               relocation - dtpoff_base (info),
4449                               globals->root.sgot->contents + off
4450                               + GOT_ENTRY_SIZE);
4451                 }
4452
4453               symbol_got_offset_mark (input_bfd, h, r_symndx);
4454             }
4455           break;
4456
4457         case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4458         case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4459           if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
4460             {
4461               bfd_boolean need_relocs = FALSE;
4462               bfd_byte *loc;
4463               int indx;
4464               bfd_vma off;
4465
4466               off = symbol_got_offset (input_bfd, h, r_symndx);
4467
4468               indx = h && h->dynindx != -1 ? h->dynindx : 0;
4469
4470               need_relocs =
4471                 (info->shared || indx != 0) &&
4472                 (h == NULL
4473                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4474                  || h->root.type != bfd_link_hash_undefweak);
4475
4476               BFD_ASSERT (globals->root.srelgot != NULL);
4477
4478               if (need_relocs)
4479                 {
4480                   Elf_Internal_Rela rela;
4481
4482                   if (indx == 0)
4483                     rela.r_addend = relocation - dtpoff_base (info);
4484                   else
4485                     rela.r_addend = 0;
4486
4487                   rela.r_info = ELF64_R_INFO (indx, R_AARCH64_TLS_TPREL64);
4488                   rela.r_offset = globals->root.sgot->output_section->vma +
4489                     globals->root.sgot->output_offset + off;
4490
4491                   loc = globals->root.srelgot->contents;
4492                   loc += globals->root.srelgot->reloc_count++
4493                     * RELOC_SIZE (htab);
4494
4495                   bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
4496
4497                   bfd_put_64 (output_bfd, rela.r_addend,
4498                               globals->root.sgot->contents + off);
4499                 }
4500               else
4501                 bfd_put_64 (output_bfd, relocation - tpoff_base (info),
4502                             globals->root.sgot->contents + off);
4503
4504               symbol_got_offset_mark (input_bfd, h, r_symndx);
4505             }
4506           break;
4507
4508         case R_AARCH64_TLSLE_ADD_TPREL_LO12:
4509         case R_AARCH64_TLSLE_ADD_TPREL_HI12:
4510         case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4511         case R_AARCH64_TLSLE_MOVW_TPREL_G2:
4512         case R_AARCH64_TLSLE_MOVW_TPREL_G1:
4513         case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4514         case R_AARCH64_TLSLE_MOVW_TPREL_G0:
4515         case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4516           break;
4517
4518         case R_AARCH64_TLSDESC_ADR_PAGE:
4519         case R_AARCH64_TLSDESC_LD64_LO12_NC:
4520         case R_AARCH64_TLSDESC_ADD_LO12_NC:
4521           if (! symbol_tlsdesc_got_offset_mark_p (input_bfd, h, r_symndx))
4522             {
4523               bfd_boolean need_relocs = FALSE;
4524               int indx = h && h->dynindx != -1 ? h->dynindx : 0;
4525               bfd_vma off = symbol_tlsdesc_got_offset (input_bfd, h, r_symndx);
4526
4527               need_relocs = (h == NULL
4528                              || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4529                              || h->root.type != bfd_link_hash_undefweak);
4530
4531               BFD_ASSERT (globals->root.srelgot != NULL);
4532               BFD_ASSERT (globals->root.sgot != NULL);
4533
4534               if (need_relocs)
4535                 {
4536                   bfd_byte *loc;
4537                   Elf_Internal_Rela rela;
4538                   rela.r_info = ELF64_R_INFO (indx, R_AARCH64_TLSDESC);
4539                   rela.r_addend = 0;
4540                   rela.r_offset = (globals->root.sgotplt->output_section->vma
4541                                    + globals->root.sgotplt->output_offset
4542                                    + off + globals->sgotplt_jump_table_size);
4543
4544                   if (indx == 0)
4545                     rela.r_addend = relocation - dtpoff_base (info);
4546
4547                   /* Allocate the next available slot in the PLT reloc
4548                      section to hold our R_AARCH64_TLSDESC, the next
4549                      available slot is determined from reloc_count,
4550                      which we step. But note, reloc_count was
4551                      artifically moved down while allocating slots for
4552                      real PLT relocs such that all of the PLT relocs
4553                      will fit above the initial reloc_count and the
4554                      extra stuff will fit below.  */
4555                   loc = globals->root.srelplt->contents;
4556                   loc += globals->root.srelplt->reloc_count++
4557                     * RELOC_SIZE (globals);
4558
4559                   bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
4560
4561                   bfd_put_64 (output_bfd, (bfd_vma) 0,
4562                               globals->root.sgotplt->contents + off +
4563                               globals->sgotplt_jump_table_size);
4564                   bfd_put_64 (output_bfd, (bfd_vma) 0,
4565                               globals->root.sgotplt->contents + off +
4566                               globals->sgotplt_jump_table_size +
4567                               GOT_ENTRY_SIZE);
4568                 }
4569
4570               symbol_tlsdesc_got_offset_mark (input_bfd, h, r_symndx);
4571             }
4572           break;
4573         }
4574
4575       if (!save_addend)
4576         addend = 0;
4577
4578
4579       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4580          because such sections are not SEC_ALLOC and thus ld.so will
4581          not process them.  */
4582       if (unresolved_reloc
4583           && !((input_section->flags & SEC_DEBUGGING) != 0
4584                && h->def_dynamic)
4585           && _bfd_elf_section_offset (output_bfd, info, input_section,
4586                                       +rel->r_offset) != (bfd_vma) - 1)
4587         {
4588           (*_bfd_error_handler)
4589             (_
4590              ("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4591              input_bfd, input_section, (long) rel->r_offset, howto->name,
4592              h->root.root.string);
4593           return FALSE;
4594         }
4595
4596       if (r != bfd_reloc_ok && r != bfd_reloc_continue)
4597         {
4598           switch (r)
4599             {
4600             case bfd_reloc_overflow:
4601               /* If the overflowing reloc was to an undefined symbol,
4602                  we have already printed one error message and there
4603                  is no point complaining again.  */
4604               if ((!h ||
4605                    h->root.type != bfd_link_hash_undefined)
4606                   && (!((*info->callbacks->reloc_overflow)
4607                         (info, (h ? &h->root : NULL), name, howto->name,
4608                          (bfd_vma) 0, input_bfd, input_section,
4609                          rel->r_offset))))
4610                 return FALSE;
4611               break;
4612
4613             case bfd_reloc_undefined:
4614               if (!((*info->callbacks->undefined_symbol)
4615                     (info, name, input_bfd, input_section,
4616                      rel->r_offset, TRUE)))
4617                 return FALSE;
4618               break;
4619
4620             case bfd_reloc_outofrange:
4621               error_message = _("out of range");
4622               goto common_error;
4623
4624             case bfd_reloc_notsupported:
4625               error_message = _("unsupported relocation");
4626               goto common_error;
4627
4628             case bfd_reloc_dangerous:
4629               /* error_message should already be set.  */
4630               goto common_error;
4631
4632             default:
4633               error_message = _("unknown error");
4634               /* Fall through.  */
4635
4636             common_error:
4637               BFD_ASSERT (error_message != NULL);
4638               if (!((*info->callbacks->reloc_dangerous)
4639                     (info, error_message, input_bfd, input_section,
4640                      rel->r_offset)))
4641                 return FALSE;
4642               break;
4643             }
4644         }
4645     }
4646
4647   return TRUE;
4648 }
4649
4650 /* Set the right machine number.  */
4651
4652 static bfd_boolean
4653 elf64_aarch64_object_p (bfd *abfd)
4654 {
4655   bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64);
4656   return TRUE;
4657 }
4658
4659 /* Function to keep AArch64 specific flags in the ELF header.  */
4660
4661 static bfd_boolean
4662 elf64_aarch64_set_private_flags (bfd *abfd, flagword flags)
4663 {
4664   if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
4665     {
4666     }
4667   else
4668     {
4669       elf_elfheader (abfd)->e_flags = flags;
4670       elf_flags_init (abfd) = TRUE;
4671     }
4672
4673   return TRUE;
4674 }
4675
4676 /* Copy backend specific data from one object module to another.  */
4677
4678 static bfd_boolean
4679 elf64_aarch64_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4680 {
4681   flagword in_flags;
4682
4683   if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
4684     return TRUE;
4685
4686   in_flags = elf_elfheader (ibfd)->e_flags;
4687
4688   elf_elfheader (obfd)->e_flags = in_flags;
4689   elf_flags_init (obfd) = TRUE;
4690
4691   /* Also copy the EI_OSABI field.  */
4692   elf_elfheader (obfd)->e_ident[EI_OSABI] =
4693     elf_elfheader (ibfd)->e_ident[EI_OSABI];
4694
4695   /* Copy object attributes.  */
4696   _bfd_elf_copy_obj_attributes (ibfd, obfd);
4697
4698   return TRUE;
4699 }
4700
4701 /* Merge backend specific data from an object file to the output
4702    object file when linking.  */
4703
4704 static bfd_boolean
4705 elf64_aarch64_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4706 {
4707   flagword out_flags;
4708   flagword in_flags;
4709   bfd_boolean flags_compatible = TRUE;
4710   asection *sec;
4711
4712   /* Check if we have the same endianess.  */
4713   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
4714     return FALSE;
4715
4716   if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
4717     return TRUE;
4718
4719   /* The input BFD must have had its flags initialised.  */
4720   /* The following seems bogus to me -- The flags are initialized in
4721      the assembler but I don't think an elf_flags_init field is
4722      written into the object.  */
4723   /* BFD_ASSERT (elf_flags_init (ibfd)); */
4724
4725   in_flags = elf_elfheader (ibfd)->e_flags;
4726   out_flags = elf_elfheader (obfd)->e_flags;
4727
4728   if (!elf_flags_init (obfd))
4729     {
4730       /* If the input is the default architecture and had the default
4731          flags then do not bother setting the flags for the output
4732          architecture, instead allow future merges to do this.  If no
4733          future merges ever set these flags then they will retain their
4734          uninitialised values, which surprise surprise, correspond
4735          to the default values.  */
4736       if (bfd_get_arch_info (ibfd)->the_default
4737           && elf_elfheader (ibfd)->e_flags == 0)
4738         return TRUE;
4739
4740       elf_flags_init (obfd) = TRUE;
4741       elf_elfheader (obfd)->e_flags = in_flags;
4742
4743       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4744           && bfd_get_arch_info (obfd)->the_default)
4745         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4746                                   bfd_get_mach (ibfd));
4747
4748       return TRUE;
4749     }
4750
4751   /* Identical flags must be compatible.  */
4752   if (in_flags == out_flags)
4753     return TRUE;
4754
4755   /* Check to see if the input BFD actually contains any sections.  If
4756      not, its flags may not have been initialised either, but it
4757      cannot actually cause any incompatiblity.  Do not short-circuit
4758      dynamic objects; their section list may be emptied by
4759      elf_link_add_object_symbols.
4760
4761      Also check to see if there are no code sections in the input.
4762      In this case there is no need to check for code specific flags.
4763      XXX - do we need to worry about floating-point format compatability
4764      in data sections ?  */
4765   if (!(ibfd->flags & DYNAMIC))
4766     {
4767       bfd_boolean null_input_bfd = TRUE;
4768       bfd_boolean only_data_sections = TRUE;
4769
4770       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4771         {
4772           if ((bfd_get_section_flags (ibfd, sec)
4773                & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
4774               == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
4775             only_data_sections = FALSE;
4776
4777           null_input_bfd = FALSE;
4778           break;
4779         }
4780
4781       if (null_input_bfd || only_data_sections)
4782         return TRUE;
4783     }
4784
4785   return flags_compatible;
4786 }
4787
4788 /* Display the flags field.  */
4789
4790 static bfd_boolean
4791 elf64_aarch64_print_private_bfd_data (bfd *abfd, void *ptr)
4792 {
4793   FILE *file = (FILE *) ptr;
4794   unsigned long flags;
4795
4796   BFD_ASSERT (abfd != NULL && ptr != NULL);
4797
4798   /* Print normal ELF private data.  */
4799   _bfd_elf_print_private_bfd_data (abfd, ptr);
4800
4801   flags = elf_elfheader (abfd)->e_flags;
4802   /* Ignore init flag - it may not be set, despite the flags field
4803      containing valid data.  */
4804
4805   /* xgettext:c-format */
4806   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
4807
4808   if (flags)
4809     fprintf (file, _("<Unrecognised flag bits set>"));
4810
4811   fputc ('\n', file);
4812
4813   return TRUE;
4814 }
4815
4816 /* Update the got entry reference counts for the section being removed.  */
4817
4818 static bfd_boolean
4819 elf64_aarch64_gc_sweep_hook (bfd *abfd,
4820                              struct bfd_link_info *info,
4821                              asection *sec,
4822                              const Elf_Internal_Rela * relocs)
4823 {
4824   struct elf64_aarch64_link_hash_table *htab;
4825   Elf_Internal_Shdr *symtab_hdr;
4826   struct elf_link_hash_entry **sym_hashes;
4827   struct elf_aarch64_local_symbol *locals;
4828   const Elf_Internal_Rela *rel, *relend;
4829
4830   if (info->relocatable)
4831     return TRUE;
4832
4833   htab = elf64_aarch64_hash_table (info);
4834
4835   if (htab == NULL)
4836     return FALSE;
4837
4838   elf_section_data (sec)->local_dynrel = NULL;
4839
4840   symtab_hdr = &elf_symtab_hdr (abfd);
4841   sym_hashes = elf_sym_hashes (abfd);
4842
4843   locals = elf64_aarch64_locals (abfd);
4844
4845   relend = relocs + sec->reloc_count;
4846   for (rel = relocs; rel < relend; rel++)
4847     {
4848       unsigned long r_symndx;
4849       unsigned int r_type;
4850       struct elf_link_hash_entry *h = NULL;
4851
4852       r_symndx = ELF64_R_SYM (rel->r_info);
4853
4854       if (r_symndx >= symtab_hdr->sh_info)
4855         {
4856           struct elf64_aarch64_link_hash_entry *eh;
4857           struct elf_dyn_relocs **pp;
4858           struct elf_dyn_relocs *p;
4859
4860           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4861           while (h->root.type == bfd_link_hash_indirect
4862                  || h->root.type == bfd_link_hash_warning)
4863             h = (struct elf_link_hash_entry *) h->root.u.i.link;
4864           eh = (struct elf64_aarch64_link_hash_entry *) h;
4865
4866           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4867             {
4868               if (p->sec == sec)
4869                 {
4870                   /* Everything must go for SEC.  */
4871                   *pp = p->next;
4872                   break;
4873                 }
4874             }
4875         }
4876       else
4877         {
4878           Elf_Internal_Sym *isym;
4879
4880           /* A local symbol.  */
4881           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4882                                         abfd, r_symndx);
4883           if (isym == NULL)
4884             return FALSE;
4885         }
4886
4887       r_type = ELF64_R_TYPE (rel->r_info);
4888       r_type = aarch64_tls_transition (abfd,info, r_type, h ,r_symndx);
4889       switch (r_type)
4890         {
4891         case R_AARCH64_LD64_GOT_LO12_NC:
4892         case R_AARCH64_GOT_LD_PREL19:
4893         case R_AARCH64_ADR_GOT_PAGE:
4894         case R_AARCH64_TLSGD_ADR_PAGE21:
4895         case R_AARCH64_TLSGD_ADD_LO12_NC:
4896         case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4897         case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4898         case R_AARCH64_TLSLE_ADD_TPREL_LO12:
4899         case R_AARCH64_TLSLE_ADD_TPREL_HI12:
4900         case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4901         case R_AARCH64_TLSLE_MOVW_TPREL_G2:
4902         case R_AARCH64_TLSLE_MOVW_TPREL_G1:
4903         case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4904         case R_AARCH64_TLSLE_MOVW_TPREL_G0:
4905         case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4906         case R_AARCH64_TLSDESC_ADR_PAGE:
4907         case R_AARCH64_TLSDESC_ADD_LO12_NC:
4908         case R_AARCH64_TLSDESC_LD64_LO12_NC:
4909           if (h != NULL)
4910             {
4911               if (h->got.refcount > 0)
4912                 h->got.refcount -= 1;
4913             }
4914           else if (locals != NULL)
4915             {
4916               if (locals[r_symndx].got_refcount > 0)
4917                 locals[r_symndx].got_refcount -= 1;
4918             }
4919           break;
4920
4921         case R_AARCH64_ADR_PREL_PG_HI21_NC:
4922         case R_AARCH64_ADR_PREL_PG_HI21:
4923         case R_AARCH64_ADR_PREL_LO21:
4924           if (h != NULL && info->executable)
4925             {
4926               if (h->plt.refcount > 0)
4927                 h->plt.refcount -= 1;
4928             }
4929           break;
4930
4931         case R_AARCH64_CALL26:
4932         case R_AARCH64_JUMP26:
4933           /* If this is a local symbol then we resolve it
4934              directly without creating a PLT entry.  */
4935           if (h == NULL)
4936             continue;
4937
4938           if (h->plt.refcount > 0)
4939             h->plt.refcount -= 1;
4940           break;
4941
4942         case R_AARCH64_ABS64:
4943           if (h != NULL && info->executable)
4944             {
4945               if (h->plt.refcount > 0)
4946                 h->plt.refcount -= 1;
4947             }
4948           break;
4949         
4950         default:
4951           break;
4952         }
4953     }
4954
4955   return TRUE;
4956 }
4957
4958 /* Adjust a symbol defined by a dynamic object and referenced by a
4959    regular object.  The current definition is in some section of the
4960    dynamic object, but we're not including those sections.  We have to
4961    change the definition to something the rest of the link can
4962    understand.  */
4963
4964 static bfd_boolean
4965 elf64_aarch64_adjust_dynamic_symbol (struct bfd_link_info *info,
4966                                      struct elf_link_hash_entry *h)
4967 {
4968   struct elf64_aarch64_link_hash_table *htab;
4969   asection *s;
4970
4971   /* If this is a function, put it in the procedure linkage table.  We
4972      will fill in the contents of the procedure linkage table later,
4973      when we know the address of the .got section.  */
4974   if (h->type == STT_FUNC || h->needs_plt)
4975     {
4976       if (h->plt.refcount <= 0
4977           || SYMBOL_CALLS_LOCAL (info, h)
4978           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4979               && h->root.type == bfd_link_hash_undefweak))
4980         {
4981           /* This case can occur if we saw a CALL26 reloc in
4982              an input file, but the symbol wasn't referred to
4983              by a dynamic object or all references were
4984              garbage collected. In which case we can end up
4985              resolving.  */
4986           h->plt.offset = (bfd_vma) - 1;
4987           h->needs_plt = 0;
4988         }
4989
4990       return TRUE;
4991     }
4992   else
4993     /* It's possible that we incorrectly decided a .plt reloc was
4994        needed for an R_X86_64_PC32 reloc to a non-function sym in
4995        check_relocs.  We can't decide accurately between function and
4996        non-function syms in check-relocs;  Objects loaded later in
4997        the link may change h->type.  So fix it now.  */
4998     h->plt.offset = (bfd_vma) - 1;
4999
5000
5001   /* If this is a weak symbol, and there is a real definition, the
5002      processor independent code will have arranged for us to see the
5003      real definition first, and we can just use the same value.  */
5004   if (h->u.weakdef != NULL)
5005     {
5006       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5007                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
5008       h->root.u.def.section = h->u.weakdef->root.u.def.section;
5009       h->root.u.def.value = h->u.weakdef->root.u.def.value;
5010       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
5011         h->non_got_ref = h->u.weakdef->non_got_ref;
5012       return TRUE;
5013     }
5014
5015   /* If we are creating a shared library, we must presume that the
5016      only references to the symbol are via the global offset table.
5017      For such cases we need not do anything here; the relocations will
5018      be handled correctly by relocate_section.  */
5019   if (info->shared)
5020     return TRUE;
5021
5022   /* If there are no references to this symbol that do not use the
5023      GOT, we don't need to generate a copy reloc.  */
5024   if (!h->non_got_ref)
5025     return TRUE;
5026
5027   /* If -z nocopyreloc was given, we won't generate them either.  */
5028   if (info->nocopyreloc)
5029     {
5030       h->non_got_ref = 0;
5031       return TRUE;
5032     }
5033
5034   /* We must allocate the symbol in our .dynbss section, which will
5035      become part of the .bss section of the executable.  There will be
5036      an entry for this symbol in the .dynsym section.  The dynamic
5037      object will contain position independent code, so all references
5038      from the dynamic object to this symbol will go through the global
5039      offset table.  The dynamic linker will use the .dynsym entry to
5040      determine the address it must put in the global offset table, so
5041      both the dynamic object and the regular object will refer to the
5042      same memory location for the variable.  */
5043
5044   htab = elf64_aarch64_hash_table (info);
5045
5046   /* We must generate a R_AARCH64_COPY reloc to tell the dynamic linker
5047      to copy the initial value out of the dynamic object and into the
5048      runtime process image.  */
5049   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5050     {
5051       htab->srelbss->size += RELOC_SIZE (htab);
5052       h->needs_copy = 1;
5053     }
5054
5055   s = htab->sdynbss;
5056
5057   return _bfd_elf_adjust_dynamic_copy (h, s);
5058
5059 }
5060
5061 static bfd_boolean
5062 elf64_aarch64_allocate_local_symbols (bfd *abfd, unsigned number)
5063 {
5064   struct elf_aarch64_local_symbol *locals;
5065   locals = elf64_aarch64_locals (abfd);
5066   if (locals == NULL)
5067     {
5068       locals = (struct elf_aarch64_local_symbol *)
5069         bfd_zalloc (abfd, number * sizeof (struct elf_aarch64_local_symbol));
5070       if (locals == NULL)
5071         return FALSE;
5072       elf64_aarch64_locals (abfd) = locals;
5073     }
5074   return TRUE;
5075 }
5076
5077 /* Look through the relocs for a section during the first phase.  */
5078
5079 static bfd_boolean
5080 elf64_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
5081                             asection *sec, const Elf_Internal_Rela *relocs)
5082 {
5083   Elf_Internal_Shdr *symtab_hdr;
5084   struct elf_link_hash_entry **sym_hashes;
5085   const Elf_Internal_Rela *rel;
5086   const Elf_Internal_Rela *rel_end;
5087   asection *sreloc;
5088
5089   struct elf64_aarch64_link_hash_table *htab;
5090
5091   if (info->relocatable)
5092     return TRUE;
5093
5094   BFD_ASSERT (is_aarch64_elf (abfd));
5095
5096   htab = elf64_aarch64_hash_table (info);
5097   sreloc = NULL;
5098
5099   symtab_hdr = &elf_symtab_hdr (abfd);
5100   sym_hashes = elf_sym_hashes (abfd);
5101
5102   rel_end = relocs + sec->reloc_count;
5103   for (rel = relocs; rel < rel_end; rel++)
5104     {
5105       struct elf_link_hash_entry *h;
5106       unsigned long r_symndx;
5107       unsigned int r_type;
5108
5109       r_symndx = ELF64_R_SYM (rel->r_info);
5110       r_type = ELF64_R_TYPE (rel->r_info);
5111
5112       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
5113         {
5114           (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
5115                                  r_symndx);
5116           return FALSE;
5117         }
5118
5119       if (r_symndx < symtab_hdr->sh_info)
5120         h = NULL;
5121       else
5122         {
5123           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5124           while (h->root.type == bfd_link_hash_indirect
5125                  || h->root.type == bfd_link_hash_warning)
5126             h = (struct elf_link_hash_entry *) h->root.u.i.link;
5127
5128           /* PR15323, ref flags aren't set for references in the same
5129              object.  */
5130           h->root.non_ir_ref = 1;
5131         }
5132
5133       /* Could be done earlier, if h were already available.  */
5134       r_type = aarch64_tls_transition (abfd, info, r_type, h, r_symndx);
5135
5136       switch (r_type)
5137         {
5138         case R_AARCH64_ABS64:
5139
5140           /* We don't need to handle relocs into sections not going into
5141              the "real" output.  */
5142           if ((sec->flags & SEC_ALLOC) == 0)
5143             break;
5144
5145           if (h != NULL)
5146             {
5147               if (!info->shared)
5148                 h->non_got_ref = 1;
5149
5150               h->plt.refcount += 1;
5151               h->pointer_equality_needed = 1;
5152             }
5153
5154           /* No need to do anything if we're not creating a shared
5155              object.  */
5156           if (! info->shared)
5157             break;
5158
5159           {
5160             struct elf_dyn_relocs *p;
5161             struct elf_dyn_relocs **head;
5162
5163             /* We must copy these reloc types into the output file.
5164                Create a reloc section in dynobj and make room for
5165                this reloc.  */
5166             if (sreloc == NULL)
5167               {
5168                 if (htab->root.dynobj == NULL)
5169                   htab->root.dynobj = abfd;
5170
5171                 sreloc = _bfd_elf_make_dynamic_reloc_section
5172                   (sec, htab->root.dynobj, 3, abfd, /*rela? */ TRUE);
5173
5174                 if (sreloc == NULL)
5175                   return FALSE;
5176               }
5177
5178             /* If this is a global symbol, we count the number of
5179                relocations we need for this symbol.  */
5180             if (h != NULL)
5181               {
5182                 struct elf64_aarch64_link_hash_entry *eh;
5183                 eh = (struct elf64_aarch64_link_hash_entry *) h;
5184                 head = &eh->dyn_relocs;
5185               }
5186             else
5187               {
5188                 /* Track dynamic relocs needed for local syms too.
5189                    We really need local syms available to do this
5190                    easily.  Oh well.  */
5191
5192                 asection *s;
5193                 void **vpp;
5194                 Elf_Internal_Sym *isym;
5195
5196                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5197                                               abfd, r_symndx);
5198                 if (isym == NULL)
5199                   return FALSE;
5200
5201                 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5202                 if (s == NULL)
5203                   s = sec;
5204
5205                 /* Beware of type punned pointers vs strict aliasing
5206                    rules.  */
5207                 vpp = &(elf_section_data (s)->local_dynrel);
5208                 head = (struct elf_dyn_relocs **) vpp;
5209               }
5210
5211             p = *head;
5212             if (p == NULL || p->sec != sec)
5213               {
5214                 bfd_size_type amt = sizeof *p;
5215                 p = ((struct elf_dyn_relocs *)
5216                      bfd_zalloc (htab->root.dynobj, amt));
5217                 if (p == NULL)
5218                   return FALSE;
5219                 p->next = *head;
5220                 *head = p;
5221                 p->sec = sec;
5222               }
5223
5224             p->count += 1;
5225
5226           }
5227           break;
5228
5229           /* RR: We probably want to keep a consistency check that
5230              there are no dangling GOT_PAGE relocs.  */
5231         case R_AARCH64_LD64_GOT_LO12_NC:
5232         case R_AARCH64_GOT_LD_PREL19:
5233         case R_AARCH64_ADR_GOT_PAGE:
5234         case R_AARCH64_TLSGD_ADR_PAGE21:
5235         case R_AARCH64_TLSGD_ADD_LO12_NC:
5236         case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5237         case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5238         case R_AARCH64_TLSLE_ADD_TPREL_LO12:
5239         case R_AARCH64_TLSLE_ADD_TPREL_HI12:
5240         case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5241         case R_AARCH64_TLSLE_MOVW_TPREL_G2:
5242         case R_AARCH64_TLSLE_MOVW_TPREL_G1:
5243         case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5244         case R_AARCH64_TLSLE_MOVW_TPREL_G0:
5245         case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5246         case R_AARCH64_TLSDESC_ADR_PAGE:
5247         case R_AARCH64_TLSDESC_ADD_LO12_NC:
5248         case R_AARCH64_TLSDESC_LD64_LO12_NC:
5249           {
5250             unsigned got_type;
5251             unsigned old_got_type;
5252
5253             got_type = aarch64_reloc_got_type (r_type);
5254
5255             if (h)
5256               {
5257                 h->got.refcount += 1;
5258                 old_got_type = elf64_aarch64_hash_entry (h)->got_type;
5259               }
5260             else
5261               {
5262                 struct elf_aarch64_local_symbol *locals;
5263
5264                 if (!elf64_aarch64_allocate_local_symbols
5265                     (abfd, symtab_hdr->sh_info))
5266                   return FALSE;
5267
5268                 locals = elf64_aarch64_locals (abfd);
5269                 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
5270                 locals[r_symndx].got_refcount += 1;
5271                 old_got_type = locals[r_symndx].got_type;
5272               }
5273
5274             /* If a variable is accessed with both general dynamic TLS
5275                methods, two slots may be created.  */
5276             if (GOT_TLS_GD_ANY_P (old_got_type) && GOT_TLS_GD_ANY_P (got_type))
5277               got_type |= old_got_type;
5278
5279             /* We will already have issued an error message if there
5280                is a TLS/non-TLS mismatch, based on the symbol type.
5281                So just combine any TLS types needed.  */
5282             if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
5283                 && got_type != GOT_NORMAL)
5284               got_type |= old_got_type;
5285
5286             /* If the symbol is accessed by both IE and GD methods, we
5287                are able to relax.  Turn off the GD flag, without
5288                messing up with any other kind of TLS types that may be
5289                involved.  */
5290             if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type))
5291               got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD);
5292
5293             if (old_got_type != got_type)
5294               {
5295                 if (h != NULL)
5296                   elf64_aarch64_hash_entry (h)->got_type = got_type;
5297                 else
5298                   {
5299                     struct elf_aarch64_local_symbol *locals;
5300                     locals = elf64_aarch64_locals (abfd);
5301                     BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
5302                     locals[r_symndx].got_type = got_type;
5303                   }
5304               }
5305
5306             if (htab->root.sgot == NULL)
5307               {
5308                 if (htab->root.dynobj == NULL)
5309                   htab->root.dynobj = abfd;
5310                 if (!_bfd_elf_create_got_section (htab->root.dynobj, info))
5311                   return FALSE;
5312               }
5313             break;
5314           }
5315
5316         case R_AARCH64_ADR_PREL_PG_HI21_NC:
5317         case R_AARCH64_ADR_PREL_PG_HI21:
5318         case R_AARCH64_ADR_PREL_LO21:
5319           if (h != NULL && info->executable)
5320             {
5321               /* If this reloc is in a read-only section, we might
5322                  need a copy reloc.  We can't check reliably at this
5323                  stage whether the section is read-only, as input
5324                  sections have not yet been mapped to output sections.
5325                  Tentatively set the flag for now, and correct in
5326                  adjust_dynamic_symbol.  */
5327               h->non_got_ref = 1;
5328               h->plt.refcount += 1;
5329               h->pointer_equality_needed = 1;
5330             }
5331           /* FIXME:: RR need to handle these in shared libraries
5332              and essentially bomb out as these being non-PIC
5333              relocations in shared libraries.  */
5334           break;
5335
5336         case R_AARCH64_CALL26:
5337         case R_AARCH64_JUMP26:
5338           /* If this is a local symbol then we resolve it
5339              directly without creating a PLT entry.  */
5340           if (h == NULL)
5341             continue;
5342
5343           h->needs_plt = 1;
5344           h->plt.refcount += 1;
5345           break;
5346         }
5347     }
5348   return TRUE;
5349 }
5350
5351 /* Treat mapping symbols as special target symbols.  */
5352
5353 static bfd_boolean
5354 elf64_aarch64_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED,
5355                                         asymbol *sym)
5356 {
5357   return bfd_is_aarch64_special_symbol_name (sym->name,
5358                                              BFD_AARCH64_SPECIAL_SYM_TYPE_ANY);
5359 }
5360
5361 /* This is a copy of elf_find_function () from elf.c except that
5362    AArch64 mapping symbols are ignored when looking for function names.  */
5363
5364 static bfd_boolean
5365 aarch64_elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
5366                            asection *section,
5367                            asymbol **symbols,
5368                            bfd_vma offset,
5369                            const char **filename_ptr,
5370                            const char **functionname_ptr)
5371 {
5372   const char *filename = NULL;
5373   asymbol *func = NULL;
5374   bfd_vma low_func = 0;
5375   asymbol **p;
5376
5377   for (p = symbols; *p != NULL; p++)
5378     {
5379       elf_symbol_type *q;
5380
5381       q = (elf_symbol_type *) * p;
5382
5383       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5384         {
5385         default:
5386           break;
5387         case STT_FILE:
5388           filename = bfd_asymbol_name (&q->symbol);
5389           break;
5390         case STT_FUNC:
5391         case STT_NOTYPE:
5392           /* Skip mapping symbols.  */
5393           if ((q->symbol.flags & BSF_LOCAL)
5394               && (bfd_is_aarch64_special_symbol_name
5395                   (q->symbol.name, BFD_AARCH64_SPECIAL_SYM_TYPE_ANY)))
5396             continue;
5397           /* Fall through.  */
5398           if (bfd_get_section (&q->symbol) == section
5399               && q->symbol.value >= low_func && q->symbol.value <= offset)
5400             {
5401               func = (asymbol *) q;
5402               low_func = q->symbol.value;
5403             }
5404           break;
5405         }
5406     }
5407
5408   if (func == NULL)
5409     return FALSE;
5410
5411   if (filename_ptr)
5412     *filename_ptr = filename;
5413   if (functionname_ptr)
5414     *functionname_ptr = bfd_asymbol_name (func);
5415
5416   return TRUE;
5417 }
5418
5419
5420 /* Find the nearest line to a particular section and offset, for error
5421    reporting.   This code is a duplicate of the code in elf.c, except
5422    that it uses aarch64_elf_find_function.  */
5423
5424 static bfd_boolean
5425 elf64_aarch64_find_nearest_line (bfd *abfd,
5426                                  asection *section,
5427                                  asymbol **symbols,
5428                                  bfd_vma offset,
5429                                  const char **filename_ptr,
5430                                  const char **functionname_ptr,
5431                                  unsigned int *line_ptr)
5432 {
5433   bfd_boolean found = FALSE;
5434
5435   /* We skip _bfd_dwarf1_find_nearest_line since no known AArch64
5436      toolchain uses it.  */
5437
5438   if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
5439                                      section, symbols, offset,
5440                                      filename_ptr, functionname_ptr,
5441                                      line_ptr, NULL, 0,
5442                                      &elf_tdata (abfd)->dwarf2_find_line_info))
5443     {
5444       if (!*functionname_ptr)
5445         aarch64_elf_find_function (abfd, section, symbols, offset,
5446                                    *filename_ptr ? NULL : filename_ptr,
5447                                    functionname_ptr);
5448
5449       return TRUE;
5450     }
5451
5452   if (!_bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5453                                             &found, filename_ptr,
5454                                             functionname_ptr, line_ptr,
5455                                             &elf_tdata (abfd)->line_info))
5456     return FALSE;
5457
5458   if (found && (*functionname_ptr || *line_ptr))
5459     return TRUE;
5460
5461   if (symbols == NULL)
5462     return FALSE;
5463
5464   if (!aarch64_elf_find_function (abfd, section, symbols, offset,
5465                                   filename_ptr, functionname_ptr))
5466     return FALSE;
5467
5468   *line_ptr = 0;
5469   return TRUE;
5470 }
5471
5472 static bfd_boolean
5473 elf64_aarch64_find_inliner_info (bfd *abfd,
5474                                  const char **filename_ptr,
5475                                  const char **functionname_ptr,
5476                                  unsigned int *line_ptr)
5477 {
5478   bfd_boolean found;
5479   found = _bfd_dwarf2_find_inliner_info
5480     (abfd, filename_ptr,
5481      functionname_ptr, line_ptr, &elf_tdata (abfd)->dwarf2_find_line_info);
5482   return found;
5483 }
5484
5485
5486 static void
5487 elf64_aarch64_post_process_headers (bfd *abfd,
5488                                     struct bfd_link_info *link_info
5489                                     ATTRIBUTE_UNUSED)
5490 {
5491   Elf_Internal_Ehdr *i_ehdrp;   /* ELF file header, internal form.  */
5492
5493   i_ehdrp = elf_elfheader (abfd);
5494   i_ehdrp->e_ident[EI_OSABI] = 0;
5495   i_ehdrp->e_ident[EI_ABIVERSION] = AARCH64_ELF_ABI_VERSION;
5496 }
5497
5498 static enum elf_reloc_type_class
5499 elf64_aarch64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5500                                 const asection *rel_sec ATTRIBUTE_UNUSED,
5501                                 const Elf_Internal_Rela *rela)
5502 {
5503   switch ((int) ELF64_R_TYPE (rela->r_info))
5504     {
5505     case R_AARCH64_RELATIVE:
5506       return reloc_class_relative;
5507     case R_AARCH64_JUMP_SLOT:
5508       return reloc_class_plt;
5509     case R_AARCH64_COPY:
5510       return reloc_class_copy;
5511     default:
5512       return reloc_class_normal;
5513     }
5514 }
5515
5516 /* Set the right machine number for an AArch64 ELF file.  */
5517
5518 static bfd_boolean
5519 elf64_aarch64_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
5520 {
5521   if (hdr->sh_type == SHT_NOTE)
5522     *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
5523
5524   return TRUE;
5525 }
5526
5527 /* Handle an AArch64 specific section when reading an object file.  This is
5528    called when bfd_section_from_shdr finds a section with an unknown
5529    type.  */
5530
5531 static bfd_boolean
5532 elf64_aarch64_section_from_shdr (bfd *abfd,
5533                                  Elf_Internal_Shdr *hdr,
5534                                  const char *name, int shindex)
5535 {
5536   /* There ought to be a place to keep ELF backend specific flags, but
5537      at the moment there isn't one.  We just keep track of the
5538      sections by their name, instead.  Fortunately, the ABI gives
5539      names for all the AArch64 specific sections, so we will probably get
5540      away with this.  */
5541   switch (hdr->sh_type)
5542     {
5543     case SHT_AARCH64_ATTRIBUTES:
5544       break;
5545
5546     default:
5547       return FALSE;
5548     }
5549
5550   if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5551     return FALSE;
5552
5553   return TRUE;
5554 }
5555
5556 /* A structure used to record a list of sections, independently
5557    of the next and prev fields in the asection structure.  */
5558 typedef struct section_list
5559 {
5560   asection *sec;
5561   struct section_list *next;
5562   struct section_list *prev;
5563 }
5564 section_list;
5565
5566 /* Unfortunately we need to keep a list of sections for which
5567    an _aarch64_elf_section_data structure has been allocated.  This
5568    is because it is possible for functions like elf64_aarch64_write_section
5569    to be called on a section which has had an elf_data_structure
5570    allocated for it (and so the used_by_bfd field is valid) but
5571    for which the AArch64 extended version of this structure - the
5572    _aarch64_elf_section_data structure - has not been allocated.  */
5573 static section_list *sections_with_aarch64_elf_section_data = NULL;
5574
5575 static void
5576 record_section_with_aarch64_elf_section_data (asection *sec)
5577 {
5578   struct section_list *entry;
5579
5580   entry = bfd_malloc (sizeof (*entry));
5581   if (entry == NULL)
5582     return;
5583   entry->sec = sec;
5584   entry->next = sections_with_aarch64_elf_section_data;
5585   entry->prev = NULL;
5586   if (entry->next != NULL)
5587     entry->next->prev = entry;
5588   sections_with_aarch64_elf_section_data = entry;
5589 }
5590
5591 static struct section_list *
5592 find_aarch64_elf_section_entry (asection *sec)
5593 {
5594   struct section_list *entry;
5595   static struct section_list *last_entry = NULL;
5596
5597   /* This is a short cut for the typical case where the sections are added
5598      to the sections_with_aarch64_elf_section_data list in forward order and
5599      then looked up here in backwards order.  This makes a real difference
5600      to the ld-srec/sec64k.exp linker test.  */
5601   entry = sections_with_aarch64_elf_section_data;
5602   if (last_entry != NULL)
5603     {
5604       if (last_entry->sec == sec)
5605         entry = last_entry;
5606       else if (last_entry->next != NULL && last_entry->next->sec == sec)
5607         entry = last_entry->next;
5608     }
5609
5610   for (; entry; entry = entry->next)
5611     if (entry->sec == sec)
5612       break;
5613
5614   if (entry)
5615     /* Record the entry prior to this one - it is the entry we are
5616        most likely to want to locate next time.  Also this way if we
5617        have been called from
5618        unrecord_section_with_aarch64_elf_section_data () we will not
5619        be caching a pointer that is about to be freed.  */
5620     last_entry = entry->prev;
5621
5622   return entry;
5623 }
5624
5625 static void
5626 unrecord_section_with_aarch64_elf_section_data (asection *sec)
5627 {
5628   struct section_list *entry;
5629
5630   entry = find_aarch64_elf_section_entry (sec);
5631
5632   if (entry)
5633     {
5634       if (entry->prev != NULL)
5635         entry->prev->next = entry->next;
5636       if (entry->next != NULL)
5637         entry->next->prev = entry->prev;
5638       if (entry == sections_with_aarch64_elf_section_data)
5639         sections_with_aarch64_elf_section_data = entry->next;
5640       free (entry);
5641     }
5642 }
5643
5644
5645 typedef struct
5646 {
5647   void *finfo;
5648   struct bfd_link_info *info;
5649   asection *sec;
5650   int sec_shndx;
5651   int (*func) (void *, const char *, Elf_Internal_Sym *,
5652                asection *, struct elf_link_hash_entry *);
5653 } output_arch_syminfo;
5654
5655 enum map_symbol_type
5656 {
5657   AARCH64_MAP_INSN,
5658   AARCH64_MAP_DATA
5659 };
5660
5661
5662 /* Output a single mapping symbol.  */
5663
5664 static bfd_boolean
5665 elf64_aarch64_output_map_sym (output_arch_syminfo *osi,
5666                               enum map_symbol_type type, bfd_vma offset)
5667 {
5668   static const char *names[2] = { "$x", "$d" };
5669   Elf_Internal_Sym sym;
5670
5671   sym.st_value = (osi->sec->output_section->vma
5672                   + osi->sec->output_offset + offset);
5673   sym.st_size = 0;
5674   sym.st_other = 0;
5675   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
5676   sym.st_shndx = osi->sec_shndx;
5677   return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
5678 }
5679
5680
5681
5682 /* Output mapping symbols for PLT entries associated with H.  */
5683
5684 static bfd_boolean
5685 elf64_aarch64_output_plt_map (struct elf_link_hash_entry *h, void *inf)
5686 {
5687   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
5688   bfd_vma addr;
5689
5690   if (h->root.type == bfd_link_hash_indirect)
5691     return TRUE;
5692
5693   if (h->root.type == bfd_link_hash_warning)
5694     /* When warning symbols are created, they **replace** the "real"
5695        entry in the hash table, thus we never get to see the real
5696        symbol in a hash traversal.  So look at it now.  */
5697     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5698
5699   if (h->plt.offset == (bfd_vma) - 1)
5700     return TRUE;
5701
5702   addr = h->plt.offset;
5703   if (addr == 32)
5704     {
5705       if (!elf64_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
5706         return FALSE;
5707     }
5708   return TRUE;
5709 }
5710
5711
5712 /* Output a single local symbol for a generated stub.  */
5713
5714 static bfd_boolean
5715 elf64_aarch64_output_stub_sym (output_arch_syminfo *osi, const char *name,
5716                                bfd_vma offset, bfd_vma size)
5717 {
5718   Elf_Internal_Sym sym;
5719
5720   sym.st_value = (osi->sec->output_section->vma
5721                   + osi->sec->output_offset + offset);
5722   sym.st_size = size;
5723   sym.st_other = 0;
5724   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
5725   sym.st_shndx = osi->sec_shndx;
5726   return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
5727 }
5728
5729 static bfd_boolean
5730 aarch64_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5731 {
5732   struct elf64_aarch64_stub_hash_entry *stub_entry;
5733   asection *stub_sec;
5734   bfd_vma addr;
5735   char *stub_name;
5736   output_arch_syminfo *osi;
5737
5738   /* Massage our args to the form they really have.  */
5739   stub_entry = (struct elf64_aarch64_stub_hash_entry *) gen_entry;
5740   osi = (output_arch_syminfo *) in_arg;
5741
5742   stub_sec = stub_entry->stub_sec;
5743
5744   /* Ensure this stub is attached to the current section being
5745      processed.  */
5746   if (stub_sec != osi->sec)
5747     return TRUE;
5748
5749   addr = (bfd_vma) stub_entry->stub_offset;
5750
5751   stub_name = stub_entry->output_name;
5752
5753   switch (stub_entry->stub_type)
5754     {
5755     case aarch64_stub_adrp_branch:
5756       if (!elf64_aarch64_output_stub_sym (osi, stub_name, addr,
5757                                           sizeof (aarch64_adrp_branch_stub)))
5758         return FALSE;
5759       if (!elf64_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
5760         return FALSE;
5761       break;
5762     case aarch64_stub_long_branch:
5763       if (!elf64_aarch64_output_stub_sym
5764           (osi, stub_name, addr, sizeof (aarch64_long_branch_stub)))
5765         return FALSE;
5766       if (!elf64_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
5767         return FALSE;
5768       if (!elf64_aarch64_output_map_sym (osi, AARCH64_MAP_DATA, addr + 16))
5769         return FALSE;
5770       break;
5771     default:
5772       BFD_FAIL ();
5773     }
5774
5775   return TRUE;
5776 }
5777
5778 /* Output mapping symbols for linker generated sections.  */
5779
5780 static bfd_boolean
5781 elf64_aarch64_output_arch_local_syms (bfd *output_bfd,
5782                                       struct bfd_link_info *info,
5783                                       void *finfo,
5784                                       int (*func) (void *, const char *,
5785                                                    Elf_Internal_Sym *,
5786                                                    asection *,
5787                                                    struct elf_link_hash_entry
5788                                                    *))
5789 {
5790   output_arch_syminfo osi;
5791   struct elf64_aarch64_link_hash_table *htab;
5792
5793   htab = elf64_aarch64_hash_table (info);
5794
5795   osi.finfo = finfo;
5796   osi.info = info;
5797   osi.func = func;
5798
5799   /* Long calls stubs.  */
5800   if (htab->stub_bfd && htab->stub_bfd->sections)
5801     {
5802       asection *stub_sec;
5803
5804       for (stub_sec = htab->stub_bfd->sections;
5805            stub_sec != NULL; stub_sec = stub_sec->next)
5806         {
5807           /* Ignore non-stub sections.  */
5808           if (!strstr (stub_sec->name, STUB_SUFFIX))
5809             continue;
5810
5811           osi.sec = stub_sec;
5812
5813           osi.sec_shndx = _bfd_elf_section_from_bfd_section
5814             (output_bfd, osi.sec->output_section);
5815
5816           bfd_hash_traverse (&htab->stub_hash_table, aarch64_map_one_stub,
5817                              &osi);
5818         }
5819     }
5820
5821   /* Finally, output mapping symbols for the PLT.  */
5822   if (!htab->root.splt || htab->root.splt->size == 0)
5823     return TRUE;
5824
5825   /* For now live without mapping symbols for the plt.  */
5826   osi.sec_shndx = _bfd_elf_section_from_bfd_section
5827     (output_bfd, htab->root.splt->output_section);
5828   osi.sec = htab->root.splt;
5829
5830   elf_link_hash_traverse (&htab->root, elf64_aarch64_output_plt_map,
5831                           (void *) &osi);
5832
5833   return TRUE;
5834
5835 }
5836
5837 /* Allocate target specific section data.  */
5838
5839 static bfd_boolean
5840 elf64_aarch64_new_section_hook (bfd *abfd, asection *sec)
5841 {
5842   if (!sec->used_by_bfd)
5843     {
5844       _aarch64_elf_section_data *sdata;
5845       bfd_size_type amt = sizeof (*sdata);
5846
5847       sdata = bfd_zalloc (abfd, amt);
5848       if (sdata == NULL)
5849         return FALSE;
5850       sec->used_by_bfd = sdata;
5851     }
5852
5853   record_section_with_aarch64_elf_section_data (sec);
5854
5855   return _bfd_elf_new_section_hook (abfd, sec);
5856 }
5857
5858
5859 static void
5860 unrecord_section_via_map_over_sections (bfd *abfd ATTRIBUTE_UNUSED,
5861                                         asection *sec,
5862                                         void *ignore ATTRIBUTE_UNUSED)
5863 {
5864   unrecord_section_with_aarch64_elf_section_data (sec);
5865 }
5866
5867 static bfd_boolean
5868 elf64_aarch64_close_and_cleanup (bfd *abfd)
5869 {
5870   if (abfd->sections)
5871     bfd_map_over_sections (abfd,
5872                            unrecord_section_via_map_over_sections, NULL);
5873
5874   return _bfd_elf_close_and_cleanup (abfd);
5875 }
5876
5877 static bfd_boolean
5878 elf64_aarch64_bfd_free_cached_info (bfd *abfd)
5879 {
5880   if (abfd->sections)
5881     bfd_map_over_sections (abfd,
5882                            unrecord_section_via_map_over_sections, NULL);
5883
5884   return _bfd_free_cached_info (abfd);
5885 }
5886
5887 static bfd_boolean
5888 elf64_aarch64_is_function_type (unsigned int type)
5889 {
5890   return type == STT_FUNC;
5891 }
5892
5893 /* Create dynamic sections. This is different from the ARM backend in that
5894    the got, plt, gotplt and their relocation sections are all created in the
5895    standard part of the bfd elf backend.  */
5896
5897 static bfd_boolean
5898 elf64_aarch64_create_dynamic_sections (bfd *dynobj,
5899                                        struct bfd_link_info *info)
5900 {
5901   struct elf64_aarch64_link_hash_table *htab;
5902   struct elf_link_hash_entry *h;
5903
5904   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
5905     return FALSE;
5906
5907   htab = elf64_aarch64_hash_table (info);
5908   htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
5909   if (!info->shared)
5910     htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
5911
5912   if (!htab->sdynbss || (!info->shared && !htab->srelbss))
5913     abort ();
5914
5915   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
5916      dynobj's .got section.  We don't do this in the linker script
5917      because we don't want to define the symbol if we are not creating
5918      a global offset table.  */
5919   h = _bfd_elf_define_linkage_sym (dynobj, info,
5920                                    htab->root.sgot, "_GLOBAL_OFFSET_TABLE_");
5921   elf_hash_table (info)->hgot = h;
5922   if (h == NULL)
5923     return FALSE;
5924
5925   return TRUE;
5926 }
5927
5928
5929 /* Allocate space in .plt, .got and associated reloc sections for
5930    dynamic relocs.  */
5931
5932 static bfd_boolean
5933 elf64_aarch64_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5934 {
5935   struct bfd_link_info *info;
5936   struct elf64_aarch64_link_hash_table *htab;
5937   struct elf64_aarch64_link_hash_entry *eh;
5938   struct elf_dyn_relocs *p;
5939
5940   /* An example of a bfd_link_hash_indirect symbol is versioned
5941      symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
5942      -> __gxx_personality_v0(bfd_link_hash_defined)
5943
5944      There is no need to process bfd_link_hash_indirect symbols here
5945      because we will also be presented with the concrete instance of
5946      the symbol and elf64_aarch64_copy_indirect_symbol () will have been
5947      called to copy all relevant data from the generic to the concrete
5948      symbol instance.
5949    */
5950   if (h->root.type == bfd_link_hash_indirect)
5951     return TRUE;
5952
5953   if (h->root.type == bfd_link_hash_warning)
5954     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5955
5956   info = (struct bfd_link_info *) inf;
5957   htab = elf64_aarch64_hash_table (info);
5958
5959   if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
5960     {
5961       /* Make sure this symbol is output as a dynamic symbol.
5962          Undefined weak syms won't yet be marked as dynamic.  */
5963       if (h->dynindx == -1 && !h->forced_local)
5964         {
5965           if (!bfd_elf_link_record_dynamic_symbol (info, h))
5966             return FALSE;
5967         }
5968
5969       if (info->shared || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
5970         {
5971           asection *s = htab->root.splt;
5972
5973           /* If this is the first .plt entry, make room for the special
5974              first entry.  */
5975           if (s->size == 0)
5976             s->size += htab->plt_header_size;
5977
5978           h->plt.offset = s->size;
5979
5980           /* If this symbol is not defined in a regular file, and we are
5981              not generating a shared library, then set the symbol to this
5982              location in the .plt.  This is required to make function
5983              pointers compare as equal between the normal executable and
5984              the shared library.  */
5985           if (!info->shared && !h->def_regular)
5986             {
5987               h->root.u.def.section = s;
5988               h->root.u.def.value = h->plt.offset;
5989             }
5990
5991           /* Make room for this entry. For now we only create the
5992              small model PLT entries. We later need to find a way
5993              of relaxing into these from the large model PLT entries.  */
5994           s->size += PLT_SMALL_ENTRY_SIZE;
5995
5996           /* We also need to make an entry in the .got.plt section, which
5997              will be placed in the .got section by the linker script.  */
5998           htab->root.sgotplt->size += GOT_ENTRY_SIZE;
5999
6000           /* We also need to make an entry in the .rela.plt section.  */
6001           htab->root.srelplt->size += RELOC_SIZE (htab);
6002
6003           /* We need to ensure that all GOT entries that serve the PLT
6004              are consecutive with the special GOT slots [0] [1] and
6005              [2]. Any addtional relocations, such as
6006              R_AARCH64_TLSDESC, must be placed after the PLT related
6007              entries.  We abuse the reloc_count such that during
6008              sizing we adjust reloc_count to indicate the number of
6009              PLT related reserved entries.  In subsequent phases when
6010              filling in the contents of the reloc entries, PLT related
6011              entries are placed by computing their PLT index (0
6012              .. reloc_count). While other none PLT relocs are placed
6013              at the slot indicated by reloc_count and reloc_count is
6014              updated.  */
6015
6016           htab->root.srelplt->reloc_count++;
6017         }
6018       else
6019         {
6020           h->plt.offset = (bfd_vma) - 1;
6021           h->needs_plt = 0;
6022         }
6023     }
6024   else
6025     {
6026       h->plt.offset = (bfd_vma) - 1;
6027       h->needs_plt = 0;
6028     }
6029
6030   eh = (struct elf64_aarch64_link_hash_entry *) h;
6031   eh->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
6032
6033   if (h->got.refcount > 0)
6034     {
6035       bfd_boolean dyn;
6036       unsigned got_type = elf64_aarch64_hash_entry (h)->got_type;
6037
6038       h->got.offset = (bfd_vma) - 1;
6039
6040       dyn = htab->root.dynamic_sections_created;
6041
6042       /* Make sure this symbol is output as a dynamic symbol.
6043          Undefined weak syms won't yet be marked as dynamic.  */
6044       if (dyn && h->dynindx == -1 && !h->forced_local)
6045         {
6046           if (!bfd_elf_link_record_dynamic_symbol (info, h))
6047             return FALSE;
6048         }
6049
6050       if (got_type == GOT_UNKNOWN)
6051         {
6052         }
6053       else if (got_type == GOT_NORMAL)
6054         {
6055           h->got.offset = htab->root.sgot->size;
6056           htab->root.sgot->size += GOT_ENTRY_SIZE;
6057           if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6058                || h->root.type != bfd_link_hash_undefweak)
6059               && (info->shared
6060                   || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
6061             {
6062               htab->root.srelgot->size += RELOC_SIZE (htab);
6063             }
6064         }
6065       else
6066         {
6067           int indx;
6068           if (got_type & GOT_TLSDESC_GD)
6069             {
6070               eh->tlsdesc_got_jump_table_offset =
6071                 (htab->root.sgotplt->size
6072                  - aarch64_compute_jump_table_size (htab));
6073               htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
6074               h->got.offset = (bfd_vma) - 2;
6075             }
6076
6077           if (got_type & GOT_TLS_GD)
6078             {
6079               h->got.offset = htab->root.sgot->size;
6080               htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
6081             }
6082
6083           if (got_type & GOT_TLS_IE)
6084             {
6085               h->got.offset = htab->root.sgot->size;
6086               htab->root.sgot->size += GOT_ENTRY_SIZE;
6087             }
6088
6089           indx = h && h->dynindx != -1 ? h->dynindx : 0;
6090           if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6091                || h->root.type != bfd_link_hash_undefweak)
6092               && (info->shared
6093                   || indx != 0
6094                   || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
6095             {
6096               if (got_type & GOT_TLSDESC_GD)
6097                 {
6098                   htab->root.srelplt->size += RELOC_SIZE (htab);
6099                   /* Note reloc_count not incremented here!  We have
6100                      already adjusted reloc_count for this relocation
6101                      type.  */
6102
6103                   /* TLSDESC PLT is now needed, but not yet determined.  */
6104                   htab->tlsdesc_plt = (bfd_vma) - 1;
6105                 }
6106
6107               if (got_type & GOT_TLS_GD)
6108                 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
6109
6110               if (got_type & GOT_TLS_IE)
6111                 htab->root.srelgot->size += RELOC_SIZE (htab);
6112             }
6113         }
6114     }
6115   else
6116     {
6117       h->got.offset = (bfd_vma) - 1;
6118     }
6119
6120   if (eh->dyn_relocs == NULL)
6121     return TRUE;
6122
6123   /* In the shared -Bsymbolic case, discard space allocated for
6124      dynamic pc-relative relocs against symbols which turn out to be
6125      defined in regular objects.  For the normal shared case, discard
6126      space for pc-relative relocs that have become local due to symbol
6127      visibility changes.  */
6128
6129   if (info->shared)
6130     {
6131       /* Relocs that use pc_count are those that appear on a call
6132          insn, or certain REL relocs that can generated via assembly.
6133          We want calls to protected symbols to resolve directly to the
6134          function rather than going via the plt.  If people want
6135          function pointer comparisons to work as expected then they
6136          should avoid writing weird assembly.  */
6137       if (SYMBOL_CALLS_LOCAL (info, h))
6138         {
6139           struct elf_dyn_relocs **pp;
6140
6141           for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
6142             {
6143               p->count -= p->pc_count;
6144               p->pc_count = 0;
6145               if (p->count == 0)
6146                 *pp = p->next;
6147               else
6148                 pp = &p->next;
6149             }
6150         }
6151
6152       /* Also discard relocs on undefined weak syms with non-default
6153          visibility.  */
6154       if (eh->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
6155         {
6156           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
6157             eh->dyn_relocs = NULL;
6158
6159           /* Make sure undefined weak symbols are output as a dynamic
6160              symbol in PIEs.  */
6161           else if (h->dynindx == -1
6162                    && !h->forced_local
6163                    && !bfd_elf_link_record_dynamic_symbol (info, h))
6164             return FALSE;
6165         }
6166
6167     }
6168   else if (ELIMINATE_COPY_RELOCS)
6169     {
6170       /* For the non-shared case, discard space for relocs against
6171          symbols which turn out to need copy relocs or are not
6172          dynamic.  */
6173
6174       if (!h->non_got_ref
6175           && ((h->def_dynamic
6176                && !h->def_regular)
6177               || (htab->root.dynamic_sections_created
6178                   && (h->root.type == bfd_link_hash_undefweak
6179                       || h->root.type == bfd_link_hash_undefined))))
6180         {
6181           /* Make sure this symbol is output as a dynamic symbol.
6182              Undefined weak syms won't yet be marked as dynamic.  */
6183           if (h->dynindx == -1
6184               && !h->forced_local
6185               && !bfd_elf_link_record_dynamic_symbol (info, h))
6186             return FALSE;
6187
6188           /* If that succeeded, we know we'll be keeping all the
6189              relocs.  */
6190           if (h->dynindx != -1)
6191             goto keep;
6192         }
6193
6194       eh->dyn_relocs = NULL;
6195
6196     keep:;
6197     }
6198
6199   /* Finally, allocate space.  */
6200   for (p = eh->dyn_relocs; p != NULL; p = p->next)
6201     {
6202       asection *sreloc;
6203
6204       sreloc = elf_section_data (p->sec)->sreloc;
6205
6206       BFD_ASSERT (sreloc != NULL);
6207
6208       sreloc->size += p->count * RELOC_SIZE (htab);
6209     }
6210
6211   return TRUE;
6212 }
6213
6214
6215
6216
6217 /* This is the most important function of all . Innocuosly named
6218    though !  */
6219 static bfd_boolean
6220 elf64_aarch64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
6221                                      struct bfd_link_info *info)
6222 {
6223   struct elf64_aarch64_link_hash_table *htab;
6224   bfd *dynobj;
6225   asection *s;
6226   bfd_boolean relocs;
6227   bfd *ibfd;
6228
6229   htab = elf64_aarch64_hash_table ((info));
6230   dynobj = htab->root.dynobj;
6231
6232   BFD_ASSERT (dynobj != NULL);
6233
6234   if (htab->root.dynamic_sections_created)
6235     {
6236       if (info->executable)
6237         {
6238           s = bfd_get_linker_section (dynobj, ".interp");
6239           if (s == NULL)
6240             abort ();
6241           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
6242           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6243         }
6244     }
6245
6246   /* Set up .got offsets for local syms, and space for local dynamic
6247      relocs.  */
6248   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6249     {
6250       struct elf_aarch64_local_symbol *locals = NULL;
6251       Elf_Internal_Shdr *symtab_hdr;
6252       asection *srel;
6253       unsigned int i;
6254
6255       if (!is_aarch64_elf (ibfd))
6256         continue;
6257
6258       for (s = ibfd->sections; s != NULL; s = s->next)
6259         {
6260           struct elf_dyn_relocs *p;
6261
6262           for (p = (struct elf_dyn_relocs *)
6263                (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
6264             {
6265               if (!bfd_is_abs_section (p->sec)
6266                   && bfd_is_abs_section (p->sec->output_section))
6267                 {
6268                   /* Input section has been discarded, either because
6269                      it is a copy of a linkonce section or due to
6270                      linker script /DISCARD/, so we'll be discarding
6271                      the relocs too.  */
6272                 }
6273               else if (p->count != 0)
6274                 {
6275                   srel = elf_section_data (p->sec)->sreloc;
6276                   srel->size += p->count * RELOC_SIZE (htab);
6277                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
6278                     info->flags |= DF_TEXTREL;
6279                 }
6280             }
6281         }
6282
6283       locals = elf64_aarch64_locals (ibfd);
6284       if (!locals)
6285         continue;
6286
6287       symtab_hdr = &elf_symtab_hdr (ibfd);
6288       srel = htab->root.srelgot;
6289       for (i = 0; i < symtab_hdr->sh_info; i++)
6290         {
6291           locals[i].got_offset = (bfd_vma) - 1;
6292           locals[i].tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
6293           if (locals[i].got_refcount > 0)
6294             {
6295               unsigned got_type = locals[i].got_type;
6296               if (got_type & GOT_TLSDESC_GD)
6297                 {
6298                   locals[i].tlsdesc_got_jump_table_offset =
6299                     (htab->root.sgotplt->size
6300                      - aarch64_compute_jump_table_size (htab));
6301                   htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
6302                   locals[i].got_offset = (bfd_vma) - 2;
6303                 }
6304
6305               if (got_type & GOT_TLS_GD)
6306                 {
6307                   locals[i].got_offset = htab->root.sgot->size;
6308                   htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
6309                 }
6310
6311               if (got_type & GOT_TLS_IE)
6312                 {
6313                   locals[i].got_offset = htab->root.sgot->size;
6314                   htab->root.sgot->size += GOT_ENTRY_SIZE;
6315                 }
6316
6317               if (got_type == GOT_UNKNOWN)
6318                 {
6319                 }
6320
6321               if (got_type == GOT_NORMAL)
6322                 {
6323                 }
6324
6325               if (info->shared)
6326                 {
6327                   if (got_type & GOT_TLSDESC_GD)
6328                     {
6329                       htab->root.srelplt->size += RELOC_SIZE (htab);
6330                       /* Note RELOC_COUNT not incremented here! */
6331                       htab->tlsdesc_plt = (bfd_vma) - 1;
6332                     }
6333
6334                   if (got_type & GOT_TLS_GD)
6335                     htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
6336
6337                   if (got_type & GOT_TLS_IE)
6338                     htab->root.srelgot->size += RELOC_SIZE (htab);
6339                 }
6340             }
6341           else
6342             {
6343               locals[i].got_refcount = (bfd_vma) - 1;
6344             }
6345         }
6346     }
6347
6348
6349   /* Allocate global sym .plt and .got entries, and space for global
6350      sym dynamic relocs.  */
6351   elf_link_hash_traverse (&htab->root, elf64_aarch64_allocate_dynrelocs,
6352                           info);
6353
6354
6355   /* For every jump slot reserved in the sgotplt, reloc_count is
6356      incremented.  However, when we reserve space for TLS descriptors,
6357      it's not incremented, so in order to compute the space reserved
6358      for them, it suffices to multiply the reloc count by the jump
6359      slot size.  */
6360
6361   if (htab->root.srelplt)
6362     htab->sgotplt_jump_table_size = aarch64_compute_jump_table_size (htab);
6363
6364   if (htab->tlsdesc_plt)
6365     {
6366       if (htab->root.splt->size == 0)
6367         htab->root.splt->size += PLT_ENTRY_SIZE;
6368
6369       htab->tlsdesc_plt = htab->root.splt->size;
6370       htab->root.splt->size += PLT_TLSDESC_ENTRY_SIZE;
6371
6372       /* If we're not using lazy TLS relocations, don't generate the
6373          GOT entry required.  */
6374       if (!(info->flags & DF_BIND_NOW))
6375         {
6376           htab->dt_tlsdesc_got = htab->root.sgot->size;
6377           htab->root.sgot->size += GOT_ENTRY_SIZE;
6378         }
6379     }
6380
6381   /* We now have determined the sizes of the various dynamic sections.
6382      Allocate memory for them.  */
6383   relocs = FALSE;
6384   for (s = dynobj->sections; s != NULL; s = s->next)
6385     {
6386       if ((s->flags & SEC_LINKER_CREATED) == 0)
6387         continue;
6388
6389       if (s == htab->root.splt
6390           || s == htab->root.sgot
6391           || s == htab->root.sgotplt
6392           || s == htab->root.iplt
6393           || s == htab->root.igotplt || s == htab->sdynbss)
6394         {
6395           /* Strip this section if we don't need it; see the
6396              comment below.  */
6397         }
6398       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
6399         {
6400           if (s->size != 0 && s != htab->root.srelplt)
6401             relocs = TRUE;
6402
6403           /* We use the reloc_count field as a counter if we need
6404              to copy relocs into the output file.  */
6405           if (s != htab->root.srelplt)
6406             s->reloc_count = 0;
6407         }
6408       else
6409         {
6410           /* It's not one of our sections, so don't allocate space.  */
6411           continue;
6412         }
6413
6414       if (s->size == 0)
6415         {
6416           /* If we don't need this section, strip it from the
6417              output file.  This is mostly to handle .rela.bss and
6418              .rela.plt.  We must create both sections in
6419              create_dynamic_sections, because they must be created
6420              before the linker maps input sections to output
6421              sections.  The linker does that before
6422              adjust_dynamic_symbol is called, and it is that
6423              function which decides whether anything needs to go
6424              into these sections.  */
6425
6426           s->flags |= SEC_EXCLUDE;
6427           continue;
6428         }
6429
6430       if ((s->flags & SEC_HAS_CONTENTS) == 0)
6431         continue;
6432
6433       /* Allocate memory for the section contents.  We use bfd_zalloc
6434          here in case unused entries are not reclaimed before the
6435          section's contents are written out.  This should not happen,
6436          but this way if it does, we get a R_AARCH64_NONE reloc instead
6437          of garbage.  */
6438       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
6439       if (s->contents == NULL)
6440         return FALSE;
6441     }
6442
6443   if (htab->root.dynamic_sections_created)
6444     {
6445       /* Add some entries to the .dynamic section.  We fill in the
6446          values later, in elf64_aarch64_finish_dynamic_sections, but we
6447          must add the entries now so that we get the correct size for
6448          the .dynamic section.  The DT_DEBUG entry is filled in by the
6449          dynamic linker and used by the debugger.  */
6450 #define add_dynamic_entry(TAG, VAL)                     \
6451       _bfd_elf_add_dynamic_entry (info, TAG, VAL)
6452
6453       if (info->executable)
6454         {
6455           if (!add_dynamic_entry (DT_DEBUG, 0))
6456             return FALSE;
6457         }
6458
6459       if (htab->root.splt->size != 0)
6460         {
6461           if (!add_dynamic_entry (DT_PLTGOT, 0)
6462               || !add_dynamic_entry (DT_PLTRELSZ, 0)
6463               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
6464               || !add_dynamic_entry (DT_JMPREL, 0))
6465             return FALSE;
6466
6467           if (htab->tlsdesc_plt
6468               && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
6469                   || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
6470             return FALSE;
6471         }
6472
6473       if (relocs)
6474         {
6475           if (!add_dynamic_entry (DT_RELA, 0)
6476               || !add_dynamic_entry (DT_RELASZ, 0)
6477               || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
6478             return FALSE;
6479
6480           /* If any dynamic relocs apply to a read-only section,
6481              then we need a DT_TEXTREL entry.  */
6482           if ((info->flags & DF_TEXTREL) != 0)
6483             {
6484               if (!add_dynamic_entry (DT_TEXTREL, 0))
6485                 return FALSE;
6486             }
6487         }
6488     }
6489 #undef add_dynamic_entry
6490
6491   return TRUE;
6492
6493
6494 }
6495
6496 static inline void
6497 elf64_aarch64_update_plt_entry (bfd *output_bfd,
6498                                 unsigned int r_type,
6499                                 bfd_byte *plt_entry, bfd_vma value)
6500 {
6501   reloc_howto_type *howto;
6502   howto = elf64_aarch64_howto_from_type (r_type);
6503   bfd_elf_aarch64_put_addend (output_bfd, plt_entry, howto, value);
6504 }
6505
6506 static void
6507 elf64_aarch64_create_small_pltn_entry (struct elf_link_hash_entry *h,
6508                                        struct elf64_aarch64_link_hash_table
6509                                        *htab, bfd *output_bfd)
6510 {
6511   bfd_byte *plt_entry;
6512   bfd_vma plt_index;
6513   bfd_vma got_offset;
6514   bfd_vma gotplt_entry_address;
6515   bfd_vma plt_entry_address;
6516   Elf_Internal_Rela rela;
6517   bfd_byte *loc;
6518
6519   plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
6520
6521   /* Offset in the GOT is PLT index plus got GOT headers(3)
6522      times 8.  */
6523   got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
6524   plt_entry = htab->root.splt->contents + h->plt.offset;
6525   plt_entry_address = htab->root.splt->output_section->vma
6526     + htab->root.splt->output_section->output_offset + h->plt.offset;
6527   gotplt_entry_address = htab->root.sgotplt->output_section->vma +
6528     htab->root.sgotplt->output_offset + got_offset;
6529
6530   /* Copy in the boiler-plate for the PLTn entry.  */
6531   memcpy (plt_entry, elf64_aarch64_small_plt_entry, PLT_SMALL_ENTRY_SIZE);
6532
6533   /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
6534      ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
6535   elf64_aarch64_update_plt_entry (output_bfd, R_AARCH64_ADR_PREL_PG_HI21,
6536                                   plt_entry,
6537                                   PG (gotplt_entry_address) -
6538                                   PG (plt_entry_address));
6539
6540   /* Fill in the lo12 bits for the load from the pltgot.  */
6541   elf64_aarch64_update_plt_entry (output_bfd, R_AARCH64_LDST64_ABS_LO12_NC,
6542                                   plt_entry + 4,
6543                                   PG_OFFSET (gotplt_entry_address));
6544
6545   /* Fill in the the lo12 bits for the add from the pltgot entry.  */
6546   elf64_aarch64_update_plt_entry (output_bfd, R_AARCH64_ADD_ABS_LO12_NC,
6547                                   plt_entry + 8,
6548                                   PG_OFFSET (gotplt_entry_address));
6549
6550   /* All the GOTPLT Entries are essentially initialized to PLT0.  */
6551   bfd_put_64 (output_bfd,
6552               (htab->root.splt->output_section->vma
6553                + htab->root.splt->output_offset),
6554               htab->root.sgotplt->contents + got_offset);
6555
6556   /* Fill in the entry in the .rela.plt section.  */
6557   rela.r_offset = gotplt_entry_address;
6558   rela.r_info = ELF64_R_INFO (h->dynindx, R_AARCH64_JUMP_SLOT);
6559   rela.r_addend = 0;
6560
6561   /* Compute the relocation entry to used based on PLT index and do
6562      not adjust reloc_count. The reloc_count has already been adjusted
6563      to account for this entry.  */
6564   loc = htab->root.srelplt->contents + plt_index * RELOC_SIZE (htab);
6565   bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6566 }
6567
6568 /* Size sections even though they're not dynamic.  We use it to setup
6569    _TLS_MODULE_BASE_, if needed.  */
6570
6571 static bfd_boolean
6572 elf64_aarch64_always_size_sections (bfd *output_bfd,
6573                                     struct bfd_link_info *info)
6574 {
6575   asection *tls_sec;
6576
6577   if (info->relocatable)
6578     return TRUE;
6579
6580   tls_sec = elf_hash_table (info)->tls_sec;
6581
6582   if (tls_sec)
6583     {
6584       struct elf_link_hash_entry *tlsbase;
6585
6586       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
6587                                       "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
6588
6589       if (tlsbase)
6590         {
6591           struct bfd_link_hash_entry *h = NULL;
6592           const struct elf_backend_data *bed =
6593             get_elf_backend_data (output_bfd);
6594
6595           if (!(_bfd_generic_link_add_one_symbol
6596                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
6597                  tls_sec, 0, NULL, FALSE, bed->collect, &h)))
6598             return FALSE;
6599
6600           tlsbase->type = STT_TLS;
6601           tlsbase = (struct elf_link_hash_entry *) h;
6602           tlsbase->def_regular = 1;
6603           tlsbase->other = STV_HIDDEN;
6604           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
6605         }
6606     }
6607
6608   return TRUE;
6609 }
6610
6611 /* Finish up dynamic symbol handling.  We set the contents of various
6612    dynamic sections here.  */
6613 static bfd_boolean
6614 elf64_aarch64_finish_dynamic_symbol (bfd *output_bfd,
6615                                      struct bfd_link_info *info,
6616                                      struct elf_link_hash_entry *h,
6617                                      Elf_Internal_Sym *sym)
6618 {
6619   struct elf64_aarch64_link_hash_table *htab;
6620   htab = elf64_aarch64_hash_table (info);
6621
6622   if (h->plt.offset != (bfd_vma) - 1)
6623     {
6624       /* This symbol has an entry in the procedure linkage table.  Set
6625          it up.  */
6626
6627       if (h->dynindx == -1
6628           || htab->root.splt == NULL
6629           || htab->root.sgotplt == NULL || htab->root.srelplt == NULL)
6630         abort ();
6631
6632       elf64_aarch64_create_small_pltn_entry (h, htab, output_bfd);
6633       if (!h->def_regular)
6634         {
6635           /* Mark the symbol as undefined, rather than as defined in
6636              the .plt section.  Leave the value alone.  This is a clue
6637              for the dynamic linker, to make function pointer
6638              comparisons work between an application and shared
6639              library.  */
6640           sym->st_shndx = SHN_UNDEF;
6641         }
6642     }
6643
6644   if (h->got.offset != (bfd_vma) - 1
6645       && elf64_aarch64_hash_entry (h)->got_type == GOT_NORMAL)
6646     {
6647       Elf_Internal_Rela rela;
6648       bfd_byte *loc;
6649
6650       /* This symbol has an entry in the global offset table.  Set it
6651          up.  */
6652       if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
6653         abort ();
6654
6655       rela.r_offset = (htab->root.sgot->output_section->vma
6656                        + htab->root.sgot->output_offset
6657                        + (h->got.offset & ~(bfd_vma) 1));
6658
6659       if (info->shared && SYMBOL_REFERENCES_LOCAL (info, h))
6660         {
6661           if (!h->def_regular)
6662             return FALSE;
6663
6664           BFD_ASSERT ((h->got.offset & 1) != 0);
6665           rela.r_info = ELF64_R_INFO (0, R_AARCH64_RELATIVE);
6666           rela.r_addend = (h->root.u.def.value
6667                            + h->root.u.def.section->output_section->vma
6668                            + h->root.u.def.section->output_offset);
6669         }
6670       else
6671         {
6672           BFD_ASSERT ((h->got.offset & 1) == 0);
6673           bfd_put_64 (output_bfd, (bfd_vma) 0,
6674                       htab->root.sgot->contents + h->got.offset);
6675           rela.r_info = ELF64_R_INFO (h->dynindx, R_AARCH64_GLOB_DAT);
6676           rela.r_addend = 0;
6677         }
6678
6679       loc = htab->root.srelgot->contents;
6680       loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
6681       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6682     }
6683
6684   if (h->needs_copy)
6685     {
6686       Elf_Internal_Rela rela;
6687       bfd_byte *loc;
6688
6689       /* This symbol needs a copy reloc.  Set it up.  */
6690
6691       if (h->dynindx == -1
6692           || (h->root.type != bfd_link_hash_defined
6693               && h->root.type != bfd_link_hash_defweak)
6694           || htab->srelbss == NULL)
6695         abort ();
6696
6697       rela.r_offset = (h->root.u.def.value
6698                        + h->root.u.def.section->output_section->vma
6699                        + h->root.u.def.section->output_offset);
6700       rela.r_info = ELF64_R_INFO (h->dynindx, R_AARCH64_COPY);
6701       rela.r_addend = 0;
6702       loc = htab->srelbss->contents;
6703       loc += htab->srelbss->reloc_count++ * RELOC_SIZE (htab);
6704       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6705     }
6706
6707   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
6708      be NULL for local symbols.  */
6709   if (sym != NULL
6710       && (h == elf_hash_table (info)->hdynamic
6711           || h == elf_hash_table (info)->hgot))
6712     sym->st_shndx = SHN_ABS;
6713
6714   return TRUE;
6715 }
6716
6717 static void
6718 elf64_aarch64_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
6719                                      struct elf64_aarch64_link_hash_table
6720                                      *htab)
6721 {
6722   /* Fill in PLT0. Fixme:RR Note this doesn't distinguish between
6723      small and large plts and at the minute just generates
6724      the small PLT.  */
6725
6726   /* PLT0 of the small PLT looks like this -
6727      stp x16, x30, [sp, #-16]!          // Save the reloc and lr on stack.
6728      adrp x16, PLT_GOT + 16             // Get the page base of the GOTPLT
6729      ldr  x17, [x16, #:lo12:PLT_GOT+16] // Load the address of the
6730                                         // symbol resolver
6731      add  x16, x16, #:lo12:PLT_GOT+16   // Load the lo12 bits of the
6732                                         // GOTPLT entry for this.
6733      br   x17
6734    */
6735   bfd_vma plt_got_base;
6736   bfd_vma plt_base;
6737
6738
6739   memcpy (htab->root.splt->contents, elf64_aarch64_small_plt0_entry,
6740           PLT_ENTRY_SIZE);
6741   elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize =
6742     PLT_ENTRY_SIZE;
6743
6744   plt_got_base = (htab->root.sgotplt->output_section->vma
6745                   + htab->root.sgotplt->output_offset);
6746
6747   plt_base = htab->root.splt->output_section->vma +
6748     htab->root.splt->output_section->output_offset;
6749
6750   /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
6751      ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
6752   elf64_aarch64_update_plt_entry (output_bfd, R_AARCH64_ADR_PREL_PG_HI21,
6753                                   htab->root.splt->contents + 4,
6754                                   PG (plt_got_base + 16) - PG (plt_base + 4));
6755
6756   elf64_aarch64_update_plt_entry (output_bfd, R_AARCH64_LDST64_ABS_LO12_NC,
6757                                   htab->root.splt->contents + 8,
6758                                   PG_OFFSET (plt_got_base + 16));
6759
6760   elf64_aarch64_update_plt_entry (output_bfd, R_AARCH64_ADD_ABS_LO12_NC,
6761                                   htab->root.splt->contents + 12,
6762                                   PG_OFFSET (plt_got_base + 16));
6763 }
6764
6765 static bfd_boolean
6766 elf64_aarch64_finish_dynamic_sections (bfd *output_bfd,
6767                                        struct bfd_link_info *info)
6768 {
6769   struct elf64_aarch64_link_hash_table *htab;
6770   bfd *dynobj;
6771   asection *sdyn;
6772
6773   htab = elf64_aarch64_hash_table (info);
6774   dynobj = htab->root.dynobj;
6775   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
6776
6777   if (htab->root.dynamic_sections_created)
6778     {
6779       Elf64_External_Dyn *dyncon, *dynconend;
6780
6781       if (sdyn == NULL || htab->root.sgot == NULL)
6782         abort ();
6783
6784       dyncon = (Elf64_External_Dyn *) sdyn->contents;
6785       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
6786       for (; dyncon < dynconend; dyncon++)
6787         {
6788           Elf_Internal_Dyn dyn;
6789           asection *s;
6790
6791           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
6792
6793           switch (dyn.d_tag)
6794             {
6795             default:
6796               continue;
6797
6798             case DT_PLTGOT:
6799               s = htab->root.sgotplt;
6800               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6801               break;
6802
6803             case DT_JMPREL:
6804               dyn.d_un.d_ptr = htab->root.srelplt->output_section->vma;
6805               break;
6806
6807             case DT_PLTRELSZ:
6808               s = htab->root.srelplt->output_section;
6809               dyn.d_un.d_val = s->size;
6810               break;
6811
6812             case DT_RELASZ:
6813               /* The procedure linkage table relocs (DT_JMPREL) should
6814                  not be included in the overall relocs (DT_RELA).
6815                  Therefore, we override the DT_RELASZ entry here to
6816                  make it not include the JMPREL relocs.  Since the
6817                  linker script arranges for .rela.plt to follow all
6818                  other relocation sections, we don't have to worry
6819                  about changing the DT_RELA entry.  */
6820               if (htab->root.srelplt != NULL)
6821                 {
6822                   s = htab->root.srelplt->output_section;
6823                   dyn.d_un.d_val -= s->size;
6824                 }
6825               break;
6826
6827             case DT_TLSDESC_PLT:
6828               s = htab->root.splt;
6829               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
6830                 + htab->tlsdesc_plt;
6831               break;
6832
6833             case DT_TLSDESC_GOT:
6834               s = htab->root.sgot;
6835               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
6836                 + htab->dt_tlsdesc_got;
6837               break;
6838             }
6839
6840           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
6841         }
6842
6843     }
6844
6845   /* Fill in the special first entry in the procedure linkage table.  */
6846   if (htab->root.splt && htab->root.splt->size > 0)
6847     {
6848       elf64_aarch64_init_small_plt0_entry (output_bfd, htab);
6849
6850       elf_section_data (htab->root.splt->output_section)->
6851         this_hdr.sh_entsize = htab->plt_entry_size;
6852
6853
6854       if (htab->tlsdesc_plt)
6855         {
6856           bfd_put_64 (output_bfd, (bfd_vma) 0,
6857                       htab->root.sgot->contents + htab->dt_tlsdesc_got);
6858
6859           memcpy (htab->root.splt->contents + htab->tlsdesc_plt,
6860                   elf64_aarch64_tlsdesc_small_plt_entry,
6861                   sizeof (elf64_aarch64_tlsdesc_small_plt_entry));
6862
6863           {
6864             bfd_vma adrp1_addr =
6865               htab->root.splt->output_section->vma
6866               + htab->root.splt->output_offset + htab->tlsdesc_plt + 4;
6867
6868             bfd_vma adrp2_addr =
6869               htab->root.splt->output_section->vma
6870               + htab->root.splt->output_offset + htab->tlsdesc_plt + 8;
6871
6872             bfd_vma got_addr =
6873               htab->root.sgot->output_section->vma
6874               + htab->root.sgot->output_offset;
6875
6876             bfd_vma pltgot_addr =
6877               htab->root.sgotplt->output_section->vma
6878               + htab->root.sgotplt->output_offset;
6879
6880             bfd_vma dt_tlsdesc_got = got_addr + htab->dt_tlsdesc_got;
6881             bfd_vma opcode;
6882
6883             /* adrp x2, DT_TLSDESC_GOT */
6884             opcode = bfd_get_32 (output_bfd,
6885                                  htab->root.splt->contents
6886                                  + htab->tlsdesc_plt + 4);
6887             opcode = reencode_adr_imm
6888               (opcode, (PG (dt_tlsdesc_got) - PG (adrp1_addr)) >> 12);
6889             bfd_put_32 (output_bfd, opcode,
6890                         htab->root.splt->contents + htab->tlsdesc_plt + 4);
6891
6892             /* adrp x3, 0 */
6893             opcode = bfd_get_32 (output_bfd,
6894                                  htab->root.splt->contents
6895                                  + htab->tlsdesc_plt + 8);
6896             opcode = reencode_adr_imm
6897               (opcode, (PG (pltgot_addr) - PG (adrp2_addr)) >> 12);
6898             bfd_put_32 (output_bfd, opcode,
6899                         htab->root.splt->contents + htab->tlsdesc_plt + 8);
6900
6901             /* ldr x2, [x2, #0] */
6902             opcode = bfd_get_32 (output_bfd,
6903                                  htab->root.splt->contents
6904                                  + htab->tlsdesc_plt + 12);
6905             opcode = reencode_ldst_pos_imm (opcode,
6906                                             PG_OFFSET (dt_tlsdesc_got) >> 3);
6907             bfd_put_32 (output_bfd, opcode,
6908                         htab->root.splt->contents + htab->tlsdesc_plt + 12);
6909
6910             /* add x3, x3, 0 */
6911             opcode = bfd_get_32 (output_bfd,
6912                                  htab->root.splt->contents
6913                                  + htab->tlsdesc_plt + 16);
6914             opcode = reencode_add_imm (opcode, PG_OFFSET (pltgot_addr));
6915             bfd_put_32 (output_bfd, opcode,
6916                         htab->root.splt->contents + htab->tlsdesc_plt + 16);
6917           }
6918         }
6919     }
6920
6921   if (htab->root.sgotplt)
6922     {
6923       if (bfd_is_abs_section (htab->root.sgotplt->output_section))
6924         {
6925           (*_bfd_error_handler)
6926             (_("discarded output section: `%A'"), htab->root.sgotplt);
6927           return FALSE;
6928         }
6929
6930       /* Fill in the first three entries in the global offset table.  */
6931       if (htab->root.sgotplt->size > 0)
6932         {
6933           /* Set the first entry in the global offset table to the address of
6934              the dynamic section.  */
6935           if (sdyn == NULL)
6936             bfd_put_64 (output_bfd, (bfd_vma) 0,
6937                         htab->root.sgotplt->contents);
6938           else
6939             bfd_put_64 (output_bfd,
6940                         sdyn->output_section->vma + sdyn->output_offset,
6941                         htab->root.sgotplt->contents);
6942           /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
6943           bfd_put_64 (output_bfd,
6944                       (bfd_vma) 0,
6945                       htab->root.sgotplt->contents + GOT_ENTRY_SIZE);
6946           bfd_put_64 (output_bfd,
6947                       (bfd_vma) 0,
6948                       htab->root.sgotplt->contents + GOT_ENTRY_SIZE * 2);
6949         }
6950
6951       elf_section_data (htab->root.sgotplt->output_section)->
6952         this_hdr.sh_entsize = GOT_ENTRY_SIZE;
6953     }
6954
6955   if (htab->root.sgot && htab->root.sgot->size > 0)
6956     elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
6957       = GOT_ENTRY_SIZE;
6958
6959   return TRUE;
6960 }
6961
6962 /* Return address for Ith PLT stub in section PLT, for relocation REL
6963    or (bfd_vma) -1 if it should not be included.  */
6964
6965 static bfd_vma
6966 elf64_aarch64_plt_sym_val (bfd_vma i, const asection *plt,
6967                            const arelent *rel ATTRIBUTE_UNUSED)
6968 {
6969   return plt->vma + PLT_ENTRY_SIZE + i * PLT_SMALL_ENTRY_SIZE;
6970 }
6971
6972
6973 /* We use this so we can override certain functions
6974    (though currently we don't).  */
6975
6976 const struct elf_size_info elf64_aarch64_size_info =
6977 {
6978   sizeof (Elf64_External_Ehdr),
6979   sizeof (Elf64_External_Phdr),
6980   sizeof (Elf64_External_Shdr),
6981   sizeof (Elf64_External_Rel),
6982   sizeof (Elf64_External_Rela),
6983   sizeof (Elf64_External_Sym),
6984   sizeof (Elf64_External_Dyn),
6985   sizeof (Elf_External_Note),
6986   4,                            /* Hash table entry size.  */
6987   1,                            /* Internal relocs per external relocs.  */
6988   64,                           /* Arch size.  */
6989   3,                            /* Log_file_align.  */
6990   ELFCLASS64, EV_CURRENT,
6991   bfd_elf64_write_out_phdrs,
6992   bfd_elf64_write_shdrs_and_ehdr,
6993   bfd_elf64_checksum_contents,
6994   bfd_elf64_write_relocs,
6995   bfd_elf64_swap_symbol_in,
6996   bfd_elf64_swap_symbol_out,
6997   bfd_elf64_slurp_reloc_table,
6998   bfd_elf64_slurp_symbol_table,
6999   bfd_elf64_swap_dyn_in,
7000   bfd_elf64_swap_dyn_out,
7001   bfd_elf64_swap_reloc_in,
7002   bfd_elf64_swap_reloc_out,
7003   bfd_elf64_swap_reloca_in,
7004   bfd_elf64_swap_reloca_out
7005 };
7006
7007 #define ELF_ARCH                        bfd_arch_aarch64
7008 #define ELF_MACHINE_CODE                EM_AARCH64
7009 #define ELF_MAXPAGESIZE                 0x10000
7010 #define ELF_MINPAGESIZE                 0x1000
7011 #define ELF_COMMONPAGESIZE              0x1000
7012
7013 #define bfd_elf64_close_and_cleanup             \
7014   elf64_aarch64_close_and_cleanup
7015
7016 #define bfd_elf64_bfd_copy_private_bfd_data     \
7017   elf64_aarch64_copy_private_bfd_data
7018
7019 #define bfd_elf64_bfd_free_cached_info          \
7020   elf64_aarch64_bfd_free_cached_info
7021
7022 #define bfd_elf64_bfd_is_target_special_symbol  \
7023   elf64_aarch64_is_target_special_symbol
7024
7025 #define bfd_elf64_bfd_link_hash_table_create    \
7026   elf64_aarch64_link_hash_table_create
7027
7028 #define bfd_elf64_bfd_link_hash_table_free      \
7029   elf64_aarch64_hash_table_free
7030
7031 #define bfd_elf64_bfd_merge_private_bfd_data    \
7032   elf64_aarch64_merge_private_bfd_data
7033
7034 #define bfd_elf64_bfd_print_private_bfd_data    \
7035   elf64_aarch64_print_private_bfd_data
7036
7037 #define bfd_elf64_bfd_reloc_type_lookup         \
7038   elf64_aarch64_reloc_type_lookup
7039
7040 #define bfd_elf64_bfd_reloc_name_lookup         \
7041   elf64_aarch64_reloc_name_lookup
7042
7043 #define bfd_elf64_bfd_set_private_flags         \
7044   elf64_aarch64_set_private_flags
7045
7046 #define bfd_elf64_find_inliner_info             \
7047   elf64_aarch64_find_inliner_info
7048
7049 #define bfd_elf64_find_nearest_line             \
7050   elf64_aarch64_find_nearest_line
7051
7052 #define bfd_elf64_mkobject                      \
7053   elf64_aarch64_mkobject
7054
7055 #define bfd_elf64_new_section_hook              \
7056   elf64_aarch64_new_section_hook
7057
7058 #define elf_backend_adjust_dynamic_symbol       \
7059   elf64_aarch64_adjust_dynamic_symbol
7060
7061 #define elf_backend_always_size_sections        \
7062   elf64_aarch64_always_size_sections
7063
7064 #define elf_backend_check_relocs                \
7065   elf64_aarch64_check_relocs
7066
7067 #define elf_backend_copy_indirect_symbol        \
7068   elf64_aarch64_copy_indirect_symbol
7069
7070 /* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
7071    to them in our hash.  */
7072 #define elf_backend_create_dynamic_sections     \
7073   elf64_aarch64_create_dynamic_sections
7074
7075 #define elf_backend_init_index_section          \
7076   _bfd_elf_init_2_index_sections
7077
7078 #define elf_backend_is_function_type            \
7079   elf64_aarch64_is_function_type
7080
7081 #define elf_backend_finish_dynamic_sections     \
7082   elf64_aarch64_finish_dynamic_sections
7083
7084 #define elf_backend_finish_dynamic_symbol       \
7085   elf64_aarch64_finish_dynamic_symbol
7086
7087 #define elf_backend_gc_sweep_hook               \
7088   elf64_aarch64_gc_sweep_hook
7089
7090 #define elf_backend_object_p                    \
7091   elf64_aarch64_object_p
7092
7093 #define elf_backend_output_arch_local_syms      \
7094   elf64_aarch64_output_arch_local_syms
7095
7096 #define elf_backend_plt_sym_val                 \
7097   elf64_aarch64_plt_sym_val
7098
7099 #define elf_backend_post_process_headers        \
7100   elf64_aarch64_post_process_headers
7101
7102 #define elf_backend_relocate_section            \
7103   elf64_aarch64_relocate_section
7104
7105 #define elf_backend_reloc_type_class            \
7106   elf64_aarch64_reloc_type_class
7107
7108 #define elf_backend_section_flags               \
7109   elf64_aarch64_section_flags
7110
7111 #define elf_backend_section_from_shdr           \
7112   elf64_aarch64_section_from_shdr
7113
7114 #define elf_backend_size_dynamic_sections       \
7115   elf64_aarch64_size_dynamic_sections
7116
7117 #define elf_backend_size_info                   \
7118   elf64_aarch64_size_info
7119
7120 #define elf_backend_can_refcount       1
7121 #define elf_backend_can_gc_sections    1
7122 #define elf_backend_plt_readonly       1
7123 #define elf_backend_want_got_plt       1
7124 #define elf_backend_want_plt_sym       0
7125 #define elf_backend_may_use_rel_p      0
7126 #define elf_backend_may_use_rela_p     1
7127 #define elf_backend_default_use_rela_p 1
7128 #define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
7129 #define elf_backend_default_execstack  0
7130
7131 #undef  elf_backend_obj_attrs_section
7132 #define elf_backend_obj_attrs_section           ".ARM.attributes"
7133
7134 #include "elf64-target.h"