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