Home | History | Annotate | Download | only in bfd
      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 static const uint32_t aarch64_erratum_835769_stub[] =
   1615 {
   1616   0x00000000,    /* Placeholder for multiply accumulate.  */
   1617   0x14000000,    /* b <label> */
   1618 };
   1619 
   1620 static const uint32_t aarch64_erratum_843419_stub[] =
   1621 {
   1622   0x00000000,    /* Placeholder for LDR instruction.  */
   1623   0x14000000,    /* b <label> */
   1624 };
   1625 
   1626 /* Section name for stubs is the associated section name plus this
   1627    string.  */
   1628 #define STUB_SUFFIX ".stub"
   1629 
   1630 enum elf_aarch64_stub_type
   1631 {
   1632   aarch64_stub_none,
   1633   aarch64_stub_adrp_branch,
   1634   aarch64_stub_long_branch,
   1635   aarch64_stub_erratum_835769_veneer,
   1636   aarch64_stub_erratum_843419_veneer,
   1637 };
   1638 
   1639 struct elf_aarch64_stub_hash_entry
   1640 {
   1641   /* Base hash table entry structure.  */
   1642   struct bfd_hash_entry root;
   1643 
   1644   /* The stub section.  */
   1645   asection *stub_sec;
   1646 
   1647   /* Offset within stub_sec of the beginning of this stub.  */
   1648   bfd_vma stub_offset;
   1649 
   1650   /* Given the symbol's value and its section we can determine its final
   1651      value when building the stubs (so the stub knows where to jump).  */
   1652   bfd_vma target_value;
   1653   asection *target_section;
   1654 
   1655   enum elf_aarch64_stub_type stub_type;
   1656 
   1657   /* The symbol table entry, if any, that this was derived from.  */
   1658   struct elf_aarch64_link_hash_entry *h;
   1659 
   1660   /* Destination symbol type */
   1661   unsigned char st_type;
   1662 
   1663   /* Where this stub is being called from, or, in the case of combined
   1664      stub sections, the first input section in the group.  */
   1665   asection *id_sec;
   1666 
   1667   /* The name for the local symbol at the start of this stub.  The
   1668      stub name in the hash table has to be unique; this does not, so
   1669      it can be friendlier.  */
   1670   char *output_name;
   1671 
   1672   /* The instruction which caused this stub to be generated (only valid for
   1673      erratum 835769 workaround stubs at present).  */
   1674   uint32_t veneered_insn;
   1675 
   1676   /* In an erratum 843419 workaround stub, the ADRP instruction offset.  */
   1677   bfd_vma adrp_offset;
   1678 };
   1679 
   1680 /* Used to build a map of a section.  This is required for mixed-endian
   1681    code/data.  */
   1682 
   1683 typedef struct elf_elf_section_map
   1684 {
   1685   bfd_vma vma;
   1686   char type;
   1687 }
   1688 elf_aarch64_section_map;
   1689 
   1690 
   1691 typedef struct _aarch64_elf_section_data
   1692 {
   1693   struct bfd_elf_section_data elf;
   1694   unsigned int mapcount;
   1695   unsigned int mapsize;
   1696   elf_aarch64_section_map *map;
   1697 }
   1698 _aarch64_elf_section_data;
   1699 
   1700 #define elf_aarch64_section_data(sec) \
   1701   ((_aarch64_elf_section_data *) elf_section_data (sec))
   1702 
   1703 /* The size of the thread control block which is defined to be two pointers.  */
   1704 #define TCB_SIZE	(ARCH_SIZE/8)*2
   1705 
   1706 struct elf_aarch64_local_symbol
   1707 {
   1708   unsigned int got_type;
   1709   bfd_signed_vma got_refcount;
   1710   bfd_vma got_offset;
   1711 
   1712   /* Offset of the GOTPLT entry reserved for the TLS descriptor. The
   1713      offset is from the end of the jump table and reserved entries
   1714      within the PLTGOT.
   1715 
   1716      The magic value (bfd_vma) -1 indicates that an offset has not be
   1717      allocated.  */
   1718   bfd_vma tlsdesc_got_jump_table_offset;
   1719 };
   1720 
   1721 struct elf_aarch64_obj_tdata
   1722 {
   1723   struct elf_obj_tdata root;
   1724 
   1725   /* local symbol descriptors */
   1726   struct elf_aarch64_local_symbol *locals;
   1727 
   1728   /* Zero to warn when linking objects with incompatible enum sizes.  */
   1729   int no_enum_size_warning;
   1730 
   1731   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
   1732   int no_wchar_size_warning;
   1733 };
   1734 
   1735 #define elf_aarch64_tdata(bfd)				\
   1736   ((struct elf_aarch64_obj_tdata *) (bfd)->tdata.any)
   1737 
   1738 #define elf_aarch64_locals(bfd) (elf_aarch64_tdata (bfd)->locals)
   1739 
   1740 #define is_aarch64_elf(bfd)				\
   1741   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
   1742    && elf_tdata (bfd) != NULL				\
   1743    && elf_object_id (bfd) == AARCH64_ELF_DATA)
   1744 
   1745 static bfd_boolean
   1746 elfNN_aarch64_mkobject (bfd *abfd)
   1747 {
   1748   return bfd_elf_allocate_object (abfd, sizeof (struct elf_aarch64_obj_tdata),
   1749 				  AARCH64_ELF_DATA);
   1750 }
   1751 
   1752 #define elf_aarch64_hash_entry(ent) \
   1753   ((struct elf_aarch64_link_hash_entry *)(ent))
   1754 
   1755 #define GOT_UNKNOWN    0
   1756 #define GOT_NORMAL     1
   1757 #define GOT_TLS_GD     2
   1758 #define GOT_TLS_IE     4
   1759 #define GOT_TLSDESC_GD 8
   1760 
   1761 #define GOT_TLS_GD_ANY_P(type)	((type & GOT_TLS_GD) || (type & GOT_TLSDESC_GD))
   1762 
   1763 /* AArch64 ELF linker hash entry.  */
   1764 struct elf_aarch64_link_hash_entry
   1765 {
   1766   struct elf_link_hash_entry root;
   1767 
   1768   /* Track dynamic relocs copied for this symbol.  */
   1769   struct elf_dyn_relocs *dyn_relocs;
   1770 
   1771   /* Since PLT entries have variable size, we need to record the
   1772      index into .got.plt instead of recomputing it from the PLT
   1773      offset.  */
   1774   bfd_signed_vma plt_got_offset;
   1775 
   1776   /* Bit mask representing the type of GOT entry(s) if any required by
   1777      this symbol.  */
   1778   unsigned int got_type;
   1779 
   1780   /* A pointer to the most recently used stub hash entry against this
   1781      symbol.  */
   1782   struct elf_aarch64_stub_hash_entry *stub_cache;
   1783 
   1784   /* Offset of the GOTPLT entry reserved for the TLS descriptor.  The offset
   1785      is from the end of the jump table and reserved entries within the PLTGOT.
   1786 
   1787      The magic value (bfd_vma) -1 indicates that an offset has not
   1788      be allocated.  */
   1789   bfd_vma tlsdesc_got_jump_table_offset;
   1790 };
   1791 
   1792 static unsigned int
   1793 elfNN_aarch64_symbol_got_type (struct elf_link_hash_entry *h,
   1794 			       bfd *abfd,
   1795 			       unsigned long r_symndx)
   1796 {
   1797   if (h)
   1798     return elf_aarch64_hash_entry (h)->got_type;
   1799 
   1800   if (! elf_aarch64_locals (abfd))
   1801     return GOT_UNKNOWN;
   1802 
   1803   return elf_aarch64_locals (abfd)[r_symndx].got_type;
   1804 }
   1805 
   1806 /* Get the AArch64 elf linker hash table from a link_info structure.  */
   1807 #define elf_aarch64_hash_table(info)					\
   1808   ((struct elf_aarch64_link_hash_table *) ((info)->hash))
   1809 
   1810 #define aarch64_stub_hash_lookup(table, string, create, copy)		\
   1811   ((struct elf_aarch64_stub_hash_entry *)				\
   1812    bfd_hash_lookup ((table), (string), (create), (copy)))
   1813 
   1814 /* AArch64 ELF linker hash table.  */
   1815 struct elf_aarch64_link_hash_table
   1816 {
   1817   /* The main hash table.  */
   1818   struct elf_link_hash_table root;
   1819 
   1820   /* Nonzero to force PIC branch veneers.  */
   1821   int pic_veneer;
   1822 
   1823   /* Fix erratum 835769.  */
   1824   int fix_erratum_835769;
   1825 
   1826   /* Fix erratum 843419.  */
   1827   int fix_erratum_843419;
   1828 
   1829   /* Enable ADRP->ADR rewrite for erratum 843419 workaround.  */
   1830   int fix_erratum_843419_adr;
   1831 
   1832   /* The number of bytes in the initial entry in the PLT.  */
   1833   bfd_size_type plt_header_size;
   1834 
   1835   /* The number of bytes in the subsequent PLT etries.  */
   1836   bfd_size_type plt_entry_size;
   1837 
   1838   /* Short-cuts to get to dynamic linker sections.  */
   1839   asection *sdynbss;
   1840   asection *srelbss;
   1841 
   1842   /* Small local sym cache.  */
   1843   struct sym_cache sym_cache;
   1844 
   1845   /* For convenience in allocate_dynrelocs.  */
   1846   bfd *obfd;
   1847 
   1848   /* The amount of space used by the reserved portion of the sgotplt
   1849      section, plus whatever space is used by the jump slots.  */
   1850   bfd_vma sgotplt_jump_table_size;
   1851 
   1852   /* The stub hash table.  */
   1853   struct bfd_hash_table stub_hash_table;
   1854 
   1855   /* Linker stub bfd.  */
   1856   bfd *stub_bfd;
   1857 
   1858   /* Linker call-backs.  */
   1859   asection *(*add_stub_section) (const char *, asection *);
   1860   void (*layout_sections_again) (void);
   1861 
   1862   /* Array to keep track of which stub sections have been created, and
   1863      information on stub grouping.  */
   1864   struct map_stub
   1865   {
   1866     /* This is the section to which stubs in the group will be
   1867        attached.  */
   1868     asection *link_sec;
   1869     /* The stub section.  */
   1870     asection *stub_sec;
   1871   } *stub_group;
   1872 
   1873   /* Assorted information used by elfNN_aarch64_size_stubs.  */
   1874   unsigned int bfd_count;
   1875   int top_index;
   1876   asection **input_list;
   1877 
   1878   /* The offset into splt of the PLT entry for the TLS descriptor
   1879      resolver.  Special values are 0, if not necessary (or not found
   1880      to be necessary yet), and -1 if needed but not determined
   1881      yet.  */
   1882   bfd_vma tlsdesc_plt;
   1883 
   1884   /* The GOT offset for the lazy trampoline.  Communicated to the
   1885      loader via DT_TLSDESC_GOT.  The magic value (bfd_vma) -1
   1886      indicates an offset is not allocated.  */
   1887   bfd_vma dt_tlsdesc_got;
   1888 
   1889   /* Used by local STT_GNU_IFUNC symbols.  */
   1890   htab_t loc_hash_table;
   1891   void * loc_hash_memory;
   1892 };
   1893 
   1894 /* Create an entry in an AArch64 ELF linker hash table.  */
   1895 
   1896 static struct bfd_hash_entry *
   1897 elfNN_aarch64_link_hash_newfunc (struct bfd_hash_entry *entry,
   1898 				 struct bfd_hash_table *table,
   1899 				 const char *string)
   1900 {
   1901   struct elf_aarch64_link_hash_entry *ret =
   1902     (struct elf_aarch64_link_hash_entry *) entry;
   1903 
   1904   /* Allocate the structure if it has not already been allocated by a
   1905      subclass.  */
   1906   if (ret == NULL)
   1907     ret = bfd_hash_allocate (table,
   1908 			     sizeof (struct elf_aarch64_link_hash_entry));
   1909   if (ret == NULL)
   1910     return (struct bfd_hash_entry *) ret;
   1911 
   1912   /* Call the allocation method of the superclass.  */
   1913   ret = ((struct elf_aarch64_link_hash_entry *)
   1914 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
   1915 				     table, string));
   1916   if (ret != NULL)
   1917     {
   1918       ret->dyn_relocs = NULL;
   1919       ret->got_type = GOT_UNKNOWN;
   1920       ret->plt_got_offset = (bfd_vma) - 1;
   1921       ret->stub_cache = NULL;
   1922       ret->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
   1923     }
   1924 
   1925   return (struct bfd_hash_entry *) ret;
   1926 }
   1927 
   1928 /* Initialize an entry in the stub hash table.  */
   1929 
   1930 static struct bfd_hash_entry *
   1931 stub_hash_newfunc (struct bfd_hash_entry *entry,
   1932 		   struct bfd_hash_table *table, const char *string)
   1933 {
   1934   /* Allocate the structure if it has not already been allocated by a
   1935      subclass.  */
   1936   if (entry == NULL)
   1937     {
   1938       entry = bfd_hash_allocate (table,
   1939 				 sizeof (struct
   1940 					 elf_aarch64_stub_hash_entry));
   1941       if (entry == NULL)
   1942 	return entry;
   1943     }
   1944 
   1945   /* Call the allocation method of the superclass.  */
   1946   entry = bfd_hash_newfunc (entry, table, string);
   1947   if (entry != NULL)
   1948     {
   1949       struct elf_aarch64_stub_hash_entry *eh;
   1950 
   1951       /* Initialize the local fields.  */
   1952       eh = (struct elf_aarch64_stub_hash_entry *) entry;
   1953       eh->adrp_offset = 0;
   1954       eh->stub_sec = NULL;
   1955       eh->stub_offset = 0;
   1956       eh->target_value = 0;
   1957       eh->target_section = NULL;
   1958       eh->stub_type = aarch64_stub_none;
   1959       eh->h = NULL;
   1960       eh->id_sec = NULL;
   1961     }
   1962 
   1963   return entry;
   1964 }
   1965 
   1966 /* Compute a hash of a local hash entry.  We use elf_link_hash_entry
   1967   for local symbol so that we can handle local STT_GNU_IFUNC symbols
   1968   as global symbol.  We reuse indx and dynstr_index for local symbol
   1969   hash since they aren't used by global symbols in this backend.  */
   1970 
   1971 static hashval_t
   1972 elfNN_aarch64_local_htab_hash (const void *ptr)
   1973 {
   1974   struct elf_link_hash_entry *h
   1975     = (struct elf_link_hash_entry *) ptr;
   1976   return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
   1977 }
   1978 
   1979 /* Compare local hash entries.  */
   1980 
   1981 static int
   1982 elfNN_aarch64_local_htab_eq (const void *ptr1, const void *ptr2)
   1983 {
   1984   struct elf_link_hash_entry *h1
   1985      = (struct elf_link_hash_entry *) ptr1;
   1986   struct elf_link_hash_entry *h2
   1987     = (struct elf_link_hash_entry *) ptr2;
   1988 
   1989   return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
   1990 }
   1991 
   1992 /* Find and/or create a hash entry for local symbol.  */
   1993 
   1994 static struct elf_link_hash_entry *
   1995 elfNN_aarch64_get_local_sym_hash (struct elf_aarch64_link_hash_table *htab,
   1996 				  bfd *abfd, const Elf_Internal_Rela *rel,
   1997 				  bfd_boolean create)
   1998 {
   1999   struct elf_aarch64_link_hash_entry e, *ret;
   2000   asection *sec = abfd->sections;
   2001   hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
   2002 				       ELFNN_R_SYM (rel->r_info));
   2003   void **slot;
   2004 
   2005   e.root.indx = sec->id;
   2006   e.root.dynstr_index = ELFNN_R_SYM (rel->r_info);
   2007   slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
   2008 				   create ? INSERT : NO_INSERT);
   2009 
   2010   if (!slot)
   2011     return NULL;
   2012 
   2013   if (*slot)
   2014     {
   2015       ret = (struct elf_aarch64_link_hash_entry *) *slot;
   2016       return &ret->root;
   2017     }
   2018 
   2019   ret = (struct elf_aarch64_link_hash_entry *)
   2020 	objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
   2021 			sizeof (struct elf_aarch64_link_hash_entry));
   2022   if (ret)
   2023     {
   2024       memset (ret, 0, sizeof (*ret));
   2025       ret->root.indx = sec->id;
   2026       ret->root.dynstr_index = ELFNN_R_SYM (rel->r_info);
   2027       ret->root.dynindx = -1;
   2028       *slot = ret;
   2029     }
   2030   return &ret->root;
   2031 }
   2032 
   2033 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   2034 
   2035 static void
   2036 elfNN_aarch64_copy_indirect_symbol (struct bfd_link_info *info,
   2037 				    struct elf_link_hash_entry *dir,
   2038 				    struct elf_link_hash_entry *ind)
   2039 {
   2040   struct elf_aarch64_link_hash_entry *edir, *eind;
   2041 
   2042   edir = (struct elf_aarch64_link_hash_entry *) dir;
   2043   eind = (struct elf_aarch64_link_hash_entry *) ind;
   2044 
   2045   if (eind->dyn_relocs != NULL)
   2046     {
   2047       if (edir->dyn_relocs != NULL)
   2048 	{
   2049 	  struct elf_dyn_relocs **pp;
   2050 	  struct elf_dyn_relocs *p;
   2051 
   2052 	  /* Add reloc counts against the indirect sym to the direct sym
   2053 	     list.  Merge any entries against the same section.  */
   2054 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
   2055 	    {
   2056 	      struct elf_dyn_relocs *q;
   2057 
   2058 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   2059 		if (q->sec == p->sec)
   2060 		  {
   2061 		    q->pc_count += p->pc_count;
   2062 		    q->count += p->count;
   2063 		    *pp = p->next;
   2064 		    break;
   2065 		  }
   2066 	      if (q == NULL)
   2067 		pp = &p->next;
   2068 	    }
   2069 	  *pp = edir->dyn_relocs;
   2070 	}
   2071 
   2072       edir->dyn_relocs = eind->dyn_relocs;
   2073       eind->dyn_relocs = NULL;
   2074     }
   2075 
   2076   if (ind->root.type == bfd_link_hash_indirect)
   2077     {
   2078       /* Copy over PLT info.  */
   2079       if (dir->got.refcount <= 0)
   2080 	{
   2081 	  edir->got_type = eind->got_type;
   2082 	  eind->got_type = GOT_UNKNOWN;
   2083 	}
   2084     }
   2085 
   2086   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   2087 }
   2088 
   2089 /* Destroy an AArch64 elf linker hash table.  */
   2090 
   2091 static void
   2092 elfNN_aarch64_link_hash_table_free (bfd *obfd)
   2093 {
   2094   struct elf_aarch64_link_hash_table *ret
   2095     = (struct elf_aarch64_link_hash_table *) obfd->link.hash;
   2096 
   2097   if (ret->loc_hash_table)
   2098     htab_delete (ret->loc_hash_table);
   2099   if (ret->loc_hash_memory)
   2100     objalloc_free ((struct objalloc *) ret->loc_hash_memory);
   2101 
   2102   bfd_hash_table_free (&ret->stub_hash_table);
   2103   _bfd_elf_link_hash_table_free (obfd);
   2104 }
   2105 
   2106 /* Create an AArch64 elf linker hash table.  */
   2107 
   2108 static struct bfd_link_hash_table *
   2109 elfNN_aarch64_link_hash_table_create (bfd *abfd)
   2110 {
   2111   struct elf_aarch64_link_hash_table *ret;
   2112   bfd_size_type amt = sizeof (struct elf_aarch64_link_hash_table);
   2113 
   2114   ret = bfd_zmalloc (amt);
   2115   if (ret == NULL)
   2116     return NULL;
   2117 
   2118   if (!_bfd_elf_link_hash_table_init
   2119       (&ret->root, abfd, elfNN_aarch64_link_hash_newfunc,
   2120        sizeof (struct elf_aarch64_link_hash_entry), AARCH64_ELF_DATA))
   2121     {
   2122       free (ret);
   2123       return NULL;
   2124     }
   2125 
   2126   ret->plt_header_size = PLT_ENTRY_SIZE;
   2127   ret->plt_entry_size = PLT_SMALL_ENTRY_SIZE;
   2128   ret->obfd = abfd;
   2129   ret->dt_tlsdesc_got = (bfd_vma) - 1;
   2130 
   2131   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
   2132 			    sizeof (struct elf_aarch64_stub_hash_entry)))
   2133     {
   2134       _bfd_elf_link_hash_table_free (abfd);
   2135       return NULL;
   2136     }
   2137 
   2138   ret->loc_hash_table = htab_try_create (1024,
   2139 					 elfNN_aarch64_local_htab_hash,
   2140 					 elfNN_aarch64_local_htab_eq,
   2141 					 NULL);
   2142   ret->loc_hash_memory = objalloc_create ();
   2143   if (!ret->loc_hash_table || !ret->loc_hash_memory)
   2144     {
   2145       elfNN_aarch64_link_hash_table_free (abfd);
   2146       return NULL;
   2147     }
   2148   ret->root.root.hash_table_free = elfNN_aarch64_link_hash_table_free;
   2149 
   2150   return &ret->root.root;
   2151 }
   2152 
   2153 static bfd_boolean
   2154 aarch64_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
   2155 		  bfd_vma offset, bfd_vma value)
   2156 {
   2157   reloc_howto_type *howto;
   2158   bfd_vma place;
   2159 
   2160   howto = elfNN_aarch64_howto_from_type (r_type);
   2161   place = (input_section->output_section->vma + input_section->output_offset
   2162 	   + offset);
   2163 
   2164   r_type = elfNN_aarch64_bfd_reloc_from_type (r_type);
   2165   value = _bfd_aarch64_elf_resolve_relocation (r_type, place, value, 0, FALSE);
   2166   return _bfd_aarch64_elf_put_addend (input_bfd,
   2167 				      input_section->contents + offset, r_type,
   2168 				      howto, value);
   2169 }
   2170 
   2171 static enum elf_aarch64_stub_type
   2172 aarch64_select_branch_stub (bfd_vma value, bfd_vma place)
   2173 {
   2174   if (aarch64_valid_for_adrp_p (value, place))
   2175     return aarch64_stub_adrp_branch;
   2176   return aarch64_stub_long_branch;
   2177 }
   2178 
   2179 /* Determine the type of stub needed, if any, for a call.  */
   2180 
   2181 static enum elf_aarch64_stub_type
   2182 aarch64_type_of_stub (struct bfd_link_info *info,
   2183 		      asection *input_sec,
   2184 		      const Elf_Internal_Rela *rel,
   2185 		      unsigned char st_type,
   2186 		      struct elf_aarch64_link_hash_entry *hash,
   2187 		      bfd_vma destination)
   2188 {
   2189   bfd_vma location;
   2190   bfd_signed_vma branch_offset;
   2191   unsigned int r_type;
   2192   struct elf_aarch64_link_hash_table *globals;
   2193   enum elf_aarch64_stub_type stub_type = aarch64_stub_none;
   2194   bfd_boolean via_plt_p;
   2195 
   2196   if (st_type != STT_FUNC)
   2197     return stub_type;
   2198 
   2199   globals = elf_aarch64_hash_table (info);
   2200   via_plt_p = (globals->root.splt != NULL && hash != NULL
   2201 	       && hash->root.plt.offset != (bfd_vma) - 1);
   2202 
   2203   if (via_plt_p)
   2204     return stub_type;
   2205 
   2206   /* Determine where the call point is.  */
   2207   location = (input_sec->output_offset
   2208 	      + input_sec->output_section->vma + rel->r_offset);
   2209 
   2210   branch_offset = (bfd_signed_vma) (destination - location);
   2211 
   2212   r_type = ELFNN_R_TYPE (rel->r_info);
   2213 
   2214   /* We don't want to redirect any old unconditional jump in this way,
   2215      only one which is being used for a sibcall, where it is
   2216      acceptable for the IP0 and IP1 registers to be clobbered.  */
   2217   if ((r_type == AARCH64_R (CALL26) || r_type == AARCH64_R (JUMP26))
   2218       && (branch_offset > AARCH64_MAX_FWD_BRANCH_OFFSET
   2219 	  || branch_offset < AARCH64_MAX_BWD_BRANCH_OFFSET))
   2220     {
   2221       stub_type = aarch64_stub_long_branch;
   2222     }
   2223 
   2224   return stub_type;
   2225 }
   2226 
   2227 /* Build a name for an entry in the stub hash table.  */
   2228 
   2229 static char *
   2230 elfNN_aarch64_stub_name (const asection *input_section,
   2231 			 const asection *sym_sec,
   2232 			 const struct elf_aarch64_link_hash_entry *hash,
   2233 			 const Elf_Internal_Rela *rel)
   2234 {
   2235   char *stub_name;
   2236   bfd_size_type len;
   2237 
   2238   if (hash)
   2239     {
   2240       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 16 + 1;
   2241       stub_name = bfd_malloc (len);
   2242       if (stub_name != NULL)
   2243 	snprintf (stub_name, len, "%08x_%s+%" BFD_VMA_FMT "x",
   2244 		  (unsigned int) input_section->id,
   2245 		  hash->root.root.root.string,
   2246 		  rel->r_addend);
   2247     }
   2248   else
   2249     {
   2250       len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
   2251       stub_name = bfd_malloc (len);
   2252       if (stub_name != NULL)
   2253 	snprintf (stub_name, len, "%08x_%x:%x+%" BFD_VMA_FMT "x",
   2254 		  (unsigned int) input_section->id,
   2255 		  (unsigned int) sym_sec->id,
   2256 		  (unsigned int) ELFNN_R_SYM (rel->r_info),
   2257 		  rel->r_addend);
   2258     }
   2259 
   2260   return stub_name;
   2261 }
   2262 
   2263 /* Look up an entry in the stub hash.  Stub entries are cached because
   2264    creating the stub name takes a bit of time.  */
   2265 
   2266 static struct elf_aarch64_stub_hash_entry *
   2267 elfNN_aarch64_get_stub_entry (const asection *input_section,
   2268 			      const asection *sym_sec,
   2269 			      struct elf_link_hash_entry *hash,
   2270 			      const Elf_Internal_Rela *rel,
   2271 			      struct elf_aarch64_link_hash_table *htab)
   2272 {
   2273   struct elf_aarch64_stub_hash_entry *stub_entry;
   2274   struct elf_aarch64_link_hash_entry *h =
   2275     (struct elf_aarch64_link_hash_entry *) hash;
   2276   const asection *id_sec;
   2277 
   2278   if ((input_section->flags & SEC_CODE) == 0)
   2279     return NULL;
   2280 
   2281   /* If this input section is part of a group of sections sharing one
   2282      stub section, then use the id of the first section in the group.
   2283      Stub names need to include a section id, as there may well be
   2284      more than one stub used to reach say, printf, and we need to
   2285      distinguish between them.  */
   2286   id_sec = htab->stub_group[input_section->id].link_sec;
   2287 
   2288   if (h != NULL && h->stub_cache != NULL
   2289       && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
   2290     {
   2291       stub_entry = h->stub_cache;
   2292     }
   2293   else
   2294     {
   2295       char *stub_name;
   2296 
   2297       stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, h, rel);
   2298       if (stub_name == NULL)
   2299 	return NULL;
   2300 
   2301       stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table,
   2302 					     stub_name, FALSE, FALSE);
   2303       if (h != NULL)
   2304 	h->stub_cache = stub_entry;
   2305 
   2306       free (stub_name);
   2307     }
   2308 
   2309   return stub_entry;
   2310 }
   2311 
   2312 
   2313 /* Create a stub section.  */
   2314 
   2315 static asection *
   2316 _bfd_aarch64_create_stub_section (asection *section,
   2317 				  struct elf_aarch64_link_hash_table *htab)
   2318 {
   2319   size_t namelen;
   2320   bfd_size_type len;
   2321   char *s_name;
   2322 
   2323   namelen = strlen (section->name);
   2324   len = namelen + sizeof (STUB_SUFFIX);
   2325   s_name = bfd_alloc (htab->stub_bfd, len);
   2326   if (s_name == NULL)
   2327     return NULL;
   2328 
   2329   memcpy (s_name, section->name, namelen);
   2330   memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
   2331   return (*htab->add_stub_section) (s_name, section);
   2332 }
   2333 
   2334 
   2335 /* Find or create a stub section for a link section.
   2336 
   2337    Fix or create the stub section used to collect stubs attached to
   2338    the specified link section.  */
   2339 
   2340 static asection *
   2341 _bfd_aarch64_get_stub_for_link_section (asection *link_section,
   2342 					struct elf_aarch64_link_hash_table *htab)
   2343 {
   2344   if (htab->stub_group[link_section->id].stub_sec == NULL)
   2345     htab->stub_group[link_section->id].stub_sec
   2346       = _bfd_aarch64_create_stub_section (link_section, htab);
   2347   return htab->stub_group[link_section->id].stub_sec;
   2348 }
   2349 
   2350 
   2351 /* Find or create a stub section in the stub group for an input
   2352    section.  */
   2353 
   2354 static asection *
   2355 _bfd_aarch64_create_or_find_stub_sec (asection *section,
   2356 				      struct elf_aarch64_link_hash_table *htab)
   2357 {
   2358   asection *link_sec = htab->stub_group[section->id].link_sec;
   2359   return _bfd_aarch64_get_stub_for_link_section (link_sec, htab);
   2360 }
   2361 
   2362 
   2363 /* Add a new stub entry in the stub group associated with an input
   2364    section to the stub hash.  Not all fields of the new stub entry are
   2365    initialised.  */
   2366 
   2367 static struct elf_aarch64_stub_hash_entry *
   2368 _bfd_aarch64_add_stub_entry_in_group (const char *stub_name,
   2369 				      asection *section,
   2370 				      struct elf_aarch64_link_hash_table *htab)
   2371 {
   2372   asection *link_sec;
   2373   asection *stub_sec;
   2374   struct elf_aarch64_stub_hash_entry *stub_entry;
   2375 
   2376   link_sec = htab->stub_group[section->id].link_sec;
   2377   stub_sec = _bfd_aarch64_create_or_find_stub_sec (section, htab);
   2378 
   2379   /* Enter this entry into the linker stub hash table.  */
   2380   stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   2381 					 TRUE, FALSE);
   2382   if (stub_entry == NULL)
   2383     {
   2384       (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
   2385 			     section->owner, stub_name);
   2386       return NULL;
   2387     }
   2388 
   2389   stub_entry->stub_sec = stub_sec;
   2390   stub_entry->stub_offset = 0;
   2391   stub_entry->id_sec = link_sec;
   2392 
   2393   return stub_entry;
   2394 }
   2395 
   2396 /* Add a new stub entry in the final stub section to the stub hash.
   2397    Not all fields of the new stub entry are initialised.  */
   2398 
   2399 static struct elf_aarch64_stub_hash_entry *
   2400 _bfd_aarch64_add_stub_entry_after (const char *stub_name,
   2401 				   asection *link_section,
   2402 				   struct elf_aarch64_link_hash_table *htab)
   2403 {
   2404   asection *stub_sec;
   2405   struct elf_aarch64_stub_hash_entry *stub_entry;
   2406 
   2407   stub_sec = _bfd_aarch64_get_stub_for_link_section (link_section, htab);
   2408   stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   2409 					 TRUE, FALSE);
   2410   if (stub_entry == NULL)
   2411     {
   2412       (*_bfd_error_handler) (_("cannot create stub entry %s"), stub_name);
   2413       return NULL;
   2414     }
   2415 
   2416   stub_entry->stub_sec = stub_sec;
   2417   stub_entry->stub_offset = 0;
   2418   stub_entry->id_sec = link_section;
   2419 
   2420   return stub_entry;
   2421 }
   2422 
   2423 
   2424 static bfd_boolean
   2425 aarch64_build_one_stub (struct bfd_hash_entry *gen_entry,
   2426 			void *in_arg ATTRIBUTE_UNUSED)
   2427 {
   2428   struct elf_aarch64_stub_hash_entry *stub_entry;
   2429   asection *stub_sec;
   2430   bfd *stub_bfd;
   2431   bfd_byte *loc;
   2432   bfd_vma sym_value;
   2433   bfd_vma veneered_insn_loc;
   2434   bfd_vma veneer_entry_loc;
   2435   bfd_signed_vma branch_offset = 0;
   2436   unsigned int template_size;
   2437   const uint32_t *template;
   2438   unsigned int i;
   2439 
   2440   /* Massage our args to the form they really have.  */
   2441   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
   2442 
   2443   stub_sec = stub_entry->stub_sec;
   2444 
   2445   /* Make a note of the offset within the stubs for this entry.  */
   2446   stub_entry->stub_offset = stub_sec->size;
   2447   loc = stub_sec->contents + stub_entry->stub_offset;
   2448 
   2449   stub_bfd = stub_sec->owner;
   2450 
   2451   /* This is the address of the stub destination.  */
   2452   sym_value = (stub_entry->target_value
   2453 	       + stub_entry->target_section->output_offset
   2454 	       + stub_entry->target_section->output_section->vma);
   2455 
   2456   if (stub_entry->stub_type == aarch64_stub_long_branch)
   2457     {
   2458       bfd_vma place = (stub_entry->stub_offset + stub_sec->output_section->vma
   2459 		       + stub_sec->output_offset);
   2460 
   2461       /* See if we can relax the stub.  */
   2462       if (aarch64_valid_for_adrp_p (sym_value, place))
   2463 	stub_entry->stub_type = aarch64_select_branch_stub (sym_value, place);
   2464     }
   2465 
   2466   switch (stub_entry->stub_type)
   2467     {
   2468     case aarch64_stub_adrp_branch:
   2469       template = aarch64_adrp_branch_stub;
   2470       template_size = sizeof (aarch64_adrp_branch_stub);
   2471       break;
   2472     case aarch64_stub_long_branch:
   2473       template = aarch64_long_branch_stub;
   2474       template_size = sizeof (aarch64_long_branch_stub);
   2475       break;
   2476     case aarch64_stub_erratum_835769_veneer:
   2477       template = aarch64_erratum_835769_stub;
   2478       template_size = sizeof (aarch64_erratum_835769_stub);
   2479       break;
   2480     case aarch64_stub_erratum_843419_veneer:
   2481       template = aarch64_erratum_843419_stub;
   2482       template_size = sizeof (aarch64_erratum_843419_stub);
   2483       break;
   2484     default:
   2485       BFD_FAIL ();
   2486       return FALSE;
   2487     }
   2488 
   2489   for (i = 0; i < (template_size / sizeof template[0]); i++)
   2490     {
   2491       bfd_putl32 (template[i], loc);
   2492       loc += 4;
   2493     }
   2494 
   2495   template_size = (template_size + 7) & ~7;
   2496   stub_sec->size += template_size;
   2497 
   2498   switch (stub_entry->stub_type)
   2499     {
   2500     case aarch64_stub_adrp_branch:
   2501       if (aarch64_relocate (AARCH64_R (ADR_PREL_PG_HI21), stub_bfd, stub_sec,
   2502 			    stub_entry->stub_offset, sym_value))
   2503 	/* The stub would not have been relaxed if the offset was out
   2504 	   of range.  */
   2505 	BFD_FAIL ();
   2506 
   2507       _bfd_final_link_relocate
   2508 	(elfNN_aarch64_howto_from_type (AARCH64_R (ADD_ABS_LO12_NC)),
   2509 	 stub_bfd,
   2510 	 stub_sec,
   2511 	 stub_sec->contents,
   2512 	 stub_entry->stub_offset + 4,
   2513 	 sym_value,
   2514 	 0);
   2515       break;
   2516 
   2517     case aarch64_stub_long_branch:
   2518       /* We want the value relative to the address 12 bytes back from the
   2519          value itself.  */
   2520       _bfd_final_link_relocate (elfNN_aarch64_howto_from_type
   2521 				(AARCH64_R (PRELNN)), stub_bfd, stub_sec,
   2522 				stub_sec->contents,
   2523 				stub_entry->stub_offset + 16,
   2524 				sym_value + 12, 0);
   2525       break;
   2526 
   2527     case aarch64_stub_erratum_835769_veneer:
   2528       veneered_insn_loc = stub_entry->target_section->output_section->vma
   2529 			  + stub_entry->target_section->output_offset
   2530 			  + stub_entry->target_value;
   2531       veneer_entry_loc = stub_entry->stub_sec->output_section->vma
   2532 			  + stub_entry->stub_sec->output_offset
   2533 			  + stub_entry->stub_offset;
   2534       branch_offset = veneered_insn_loc - veneer_entry_loc;
   2535       branch_offset >>= 2;
   2536       branch_offset &= 0x3ffffff;
   2537       bfd_putl32 (stub_entry->veneered_insn,
   2538 		  stub_sec->contents + stub_entry->stub_offset);
   2539       bfd_putl32 (template[1] | branch_offset,
   2540 		  stub_sec->contents + stub_entry->stub_offset + 4);
   2541       break;
   2542 
   2543     case aarch64_stub_erratum_843419_veneer:
   2544       if (aarch64_relocate (AARCH64_R (JUMP26), stub_bfd, stub_sec,
   2545 			    stub_entry->stub_offset + 4, sym_value + 4))
   2546 	BFD_FAIL ();
   2547       break;
   2548 
   2549     default:
   2550       break;
   2551     }
   2552 
   2553   return TRUE;
   2554 }
   2555 
   2556 /* As above, but don't actually build the stub.  Just bump offset so
   2557    we know stub section sizes.  */
   2558 
   2559 static bfd_boolean
   2560 aarch64_size_one_stub (struct bfd_hash_entry *gen_entry,
   2561 		       void *in_arg ATTRIBUTE_UNUSED)
   2562 {
   2563   struct elf_aarch64_stub_hash_entry *stub_entry;
   2564   int size;
   2565 
   2566   /* Massage our args to the form they really have.  */
   2567   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
   2568 
   2569   switch (stub_entry->stub_type)
   2570     {
   2571     case aarch64_stub_adrp_branch:
   2572       size = sizeof (aarch64_adrp_branch_stub);
   2573       break;
   2574     case aarch64_stub_long_branch:
   2575       size = sizeof (aarch64_long_branch_stub);
   2576       break;
   2577     case aarch64_stub_erratum_835769_veneer:
   2578       size = sizeof (aarch64_erratum_835769_stub);
   2579       break;
   2580     case aarch64_stub_erratum_843419_veneer:
   2581       size = sizeof (aarch64_erratum_843419_stub);
   2582       break;
   2583     default:
   2584       BFD_FAIL ();
   2585       return FALSE;
   2586       break;
   2587     }
   2588 
   2589   size = (size + 7) & ~7;
   2590   stub_entry->stub_sec->size += size;
   2591   return TRUE;
   2592 }
   2593 
   2594 /* External entry points for sizing and building linker stubs.  */
   2595 
   2596 /* Set up various things so that we can make a list of input sections
   2597    for each output section included in the link.  Returns -1 on error,
   2598    0 when no stubs will be needed, and 1 on success.  */
   2599 
   2600 int
   2601 elfNN_aarch64_setup_section_lists (bfd *output_bfd,
   2602 				   struct bfd_link_info *info)
   2603 {
   2604   bfd *input_bfd;
   2605   unsigned int bfd_count;
   2606   int top_id, top_index;
   2607   asection *section;
   2608   asection **input_list, **list;
   2609   bfd_size_type amt;
   2610   struct elf_aarch64_link_hash_table *htab =
   2611     elf_aarch64_hash_table (info);
   2612 
   2613   if (!is_elf_hash_table (htab))
   2614     return 0;
   2615 
   2616   /* Count the number of input BFDs and find the top input section id.  */
   2617   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
   2618        input_bfd != NULL; input_bfd = input_bfd->link.next)
   2619     {
   2620       bfd_count += 1;
   2621       for (section = input_bfd->sections;
   2622 	   section != NULL; section = section->next)
   2623 	{
   2624 	  if (top_id < section->id)
   2625 	    top_id = section->id;
   2626 	}
   2627     }
   2628   htab->bfd_count = bfd_count;
   2629 
   2630   amt = sizeof (struct map_stub) * (top_id + 1);
   2631   htab->stub_group = bfd_zmalloc (amt);
   2632   if (htab->stub_group == NULL)
   2633     return -1;
   2634 
   2635   /* We can't use output_bfd->section_count here to find the top output
   2636      section index as some sections may have been removed, and
   2637      _bfd_strip_section_from_output doesn't renumber the indices.  */
   2638   for (section = output_bfd->sections, top_index = 0;
   2639        section != NULL; section = section->next)
   2640     {
   2641       if (top_index < section->index)
   2642 	top_index = section->index;
   2643     }
   2644 
   2645   htab->top_index = top_index;
   2646   amt = sizeof (asection *) * (top_index + 1);
   2647   input_list = bfd_malloc (amt);
   2648   htab->input_list = input_list;
   2649   if (input_list == NULL)
   2650     return -1;
   2651 
   2652   /* For sections we aren't interested in, mark their entries with a
   2653      value we can check later.  */
   2654   list = input_list + top_index;
   2655   do
   2656     *list = bfd_abs_section_ptr;
   2657   while (list-- != input_list);
   2658 
   2659   for (section = output_bfd->sections;
   2660        section != NULL; section = section->next)
   2661     {
   2662       if ((section->flags & SEC_CODE) != 0)
   2663 	input_list[section->index] = NULL;
   2664     }
   2665 
   2666   return 1;
   2667 }
   2668 
   2669 /* Used by elfNN_aarch64_next_input_section and group_sections.  */
   2670 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
   2671 
   2672 /* The linker repeatedly calls this function for each input section,
   2673    in the order that input sections are linked into output sections.
   2674    Build lists of input sections to determine groupings between which
   2675    we may insert linker stubs.  */
   2676 
   2677 void
   2678 elfNN_aarch64_next_input_section (struct bfd_link_info *info, asection *isec)
   2679 {
   2680   struct elf_aarch64_link_hash_table *htab =
   2681     elf_aarch64_hash_table (info);
   2682 
   2683   if (isec->output_section->index <= htab->top_index)
   2684     {
   2685       asection **list = htab->input_list + isec->output_section->index;
   2686 
   2687       if (*list != bfd_abs_section_ptr)
   2688 	{
   2689 	  /* Steal the link_sec pointer for our list.  */
   2690 	  /* This happens to make the list in reverse order,
   2691 	     which is what we want.  */
   2692 	  PREV_SEC (isec) = *list;
   2693 	  *list = isec;
   2694 	}
   2695     }
   2696 }
   2697 
   2698 /* See whether we can group stub sections together.  Grouping stub
   2699    sections may result in fewer stubs.  More importantly, we need to
   2700    put all .init* and .fini* stubs at the beginning of the .init or
   2701    .fini output sections respectively, because glibc splits the
   2702    _init and _fini functions into multiple parts.  Putting a stub in
   2703    the middle of a function is not a good idea.  */
   2704 
   2705 static void
   2706 group_sections (struct elf_aarch64_link_hash_table *htab,
   2707 		bfd_size_type stub_group_size,
   2708 		bfd_boolean stubs_always_before_branch)
   2709 {
   2710   asection **list = htab->input_list + htab->top_index;
   2711 
   2712   do
   2713     {
   2714       asection *tail = *list;
   2715 
   2716       if (tail == bfd_abs_section_ptr)
   2717 	continue;
   2718 
   2719       while (tail != NULL)
   2720 	{
   2721 	  asection *curr;
   2722 	  asection *prev;
   2723 	  bfd_size_type total;
   2724 
   2725 	  curr = tail;
   2726 	  total = tail->size;
   2727 	  while ((prev = PREV_SEC (curr)) != NULL
   2728 		 && ((total += curr->output_offset - prev->output_offset)
   2729 		     < stub_group_size))
   2730 	    curr = prev;
   2731 
   2732 	  /* OK, the size from the start of CURR to the end is less
   2733 	     than stub_group_size and thus can be handled by one stub
   2734 	     section.  (Or the tail section is itself larger than
   2735 	     stub_group_size, in which case we may be toast.)
   2736 	     We should really be keeping track of the total size of
   2737 	     stubs added here, as stubs contribute to the final output
   2738 	     section size.  */
   2739 	  do
   2740 	    {
   2741 	      prev = PREV_SEC (tail);
   2742 	      /* Set up this stub group.  */
   2743 	      htab->stub_group[tail->id].link_sec = curr;
   2744 	    }
   2745 	  while (tail != curr && (tail = prev) != NULL);
   2746 
   2747 	  /* But wait, there's more!  Input sections up to stub_group_size
   2748 	     bytes before the stub section can be handled by it too.  */
   2749 	  if (!stubs_always_before_branch)
   2750 	    {
   2751 	      total = 0;
   2752 	      while (prev != NULL
   2753 		     && ((total += tail->output_offset - prev->output_offset)
   2754 			 < stub_group_size))
   2755 		{
   2756 		  tail = prev;
   2757 		  prev = PREV_SEC (tail);
   2758 		  htab->stub_group[tail->id].link_sec = curr;
   2759 		}
   2760 	    }
   2761 	  tail = prev;
   2762 	}
   2763     }
   2764   while (list-- != htab->input_list);
   2765 
   2766   free (htab->input_list);
   2767 }
   2768 
   2769 #undef PREV_SEC
   2770 
   2771 #define AARCH64_BITS(x, pos, n) (((x) >> (pos)) & ((1 << (n)) - 1))
   2772 
   2773 #define AARCH64_RT(insn) AARCH64_BITS (insn, 0, 5)
   2774 #define AARCH64_RT2(insn) AARCH64_BITS (insn, 10, 5)
   2775 #define AARCH64_RA(insn) AARCH64_BITS (insn, 10, 5)
   2776 #define AARCH64_RD(insn) AARCH64_BITS (insn, 0, 5)
   2777 #define AARCH64_RN(insn) AARCH64_BITS (insn, 5, 5)
   2778 #define AARCH64_RM(insn) AARCH64_BITS (insn, 16, 5)
   2779 
   2780 #define AARCH64_MAC(insn) (((insn) & 0xff000000) == 0x9b000000)
   2781 #define AARCH64_BIT(insn, n) AARCH64_BITS (insn, n, 1)
   2782 #define AARCH64_OP31(insn) AARCH64_BITS (insn, 21, 3)
   2783 #define AARCH64_ZR 0x1f
   2784 
   2785 /* All ld/st ops.  See C4-182 of the ARM ARM.  The encoding space for
   2786    LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops.  */
   2787 
   2788 #define AARCH64_LD(insn) (AARCH64_BIT (insn, 22) == 1)
   2789 #define AARCH64_LDST(insn) (((insn) & 0x0a000000) == 0x08000000)
   2790 #define AARCH64_LDST_EX(insn) (((insn) & 0x3f000000) == 0x08000000)
   2791 #define AARCH64_LDST_PCREL(insn) (((insn) & 0x3b000000) == 0x18000000)
   2792 #define AARCH64_LDST_NAP(insn) (((insn) & 0x3b800000) == 0x28000000)
   2793 #define AARCH64_LDSTP_PI(insn) (((insn) & 0x3b800000) == 0x28800000)
   2794 #define AARCH64_LDSTP_O(insn) (((insn) & 0x3b800000) == 0x29000000)
   2795 #define AARCH64_LDSTP_PRE(insn) (((insn) & 0x3b800000) == 0x29800000)
   2796 #define AARCH64_LDST_UI(insn) (((insn) & 0x3b200c00) == 0x38000000)
   2797 #define AARCH64_LDST_PIIMM(insn) (((insn) & 0x3b200c00) == 0x38000400)
   2798 #define AARCH64_LDST_U(insn) (((insn) & 0x3b200c00) == 0x38000800)
   2799 #define AARCH64_LDST_PREIMM(insn) (((insn) & 0x3b200c00) == 0x38000c00)
   2800 #define AARCH64_LDST_RO(insn) (((insn) & 0x3b200c00) == 0x38200800)
   2801 #define AARCH64_LDST_UIMM(insn) (((insn) & 0x3b000000) == 0x39000000)
   2802 #define AARCH64_LDST_SIMD_M(insn) (((insn) & 0xbfbf0000) == 0x0c000000)
   2803 #define AARCH64_LDST_SIMD_M_PI(insn) (((insn) & 0xbfa00000) == 0x0c800000)
   2804 #define AARCH64_LDST_SIMD_S(insn) (((insn) & 0xbf9f0000) == 0x0d000000)
   2805 #define AARCH64_LDST_SIMD_S_PI(insn) (((insn) & 0xbf800000) == 0x0d800000)
   2806 
   2807 /* Classify an INSN if it is indeed a load/store.  Return TRUE if INSN
   2808    is a load/store along with the Rt and Rtn.  Return FALSE if not a
   2809    load/store.  */
   2810 
   2811 static bfd_boolean
   2812 aarch64_mem_op_p (uint32_t insn, unsigned int *rt, unsigned int *rtn,
   2813 		  bfd_boolean *pair, bfd_boolean *load)
   2814 {
   2815   uint32_t opcode;
   2816   unsigned int r;
   2817   uint32_t opc = 0;
   2818   uint32_t v = 0;
   2819   uint32_t opc_v = 0;
   2820 
   2821   /* Bail out quickly if INSN doesn't fall into the the load-store
   2822      encoding space.  */
   2823   if (!AARCH64_LDST (insn))
   2824     return FALSE;
   2825 
   2826   *pair = FALSE;
   2827   *load = FALSE;
   2828   if (AARCH64_LDST_EX (insn))
   2829     {
   2830       *rt = AARCH64_RT (insn);
   2831       *rtn = *rt;
   2832       if (AARCH64_BIT (insn, 21) == 1)
   2833         {
   2834 	  *pair = TRUE;
   2835 	  *rtn = AARCH64_RT2 (insn);
   2836 	}
   2837       *load = AARCH64_LD (insn);
   2838       return TRUE;
   2839     }
   2840   else if (AARCH64_LDST_NAP (insn)
   2841 	   || AARCH64_LDSTP_PI (insn)
   2842 	   || AARCH64_LDSTP_O (insn)
   2843 	   || AARCH64_LDSTP_PRE (insn))
   2844     {
   2845       *pair = TRUE;
   2846       *rt = AARCH64_RT (insn);
   2847       *rtn = AARCH64_RT2 (insn);
   2848       *load = AARCH64_LD (insn);
   2849       return TRUE;
   2850     }
   2851   else if (AARCH64_LDST_PCREL (insn)
   2852 	   || AARCH64_LDST_UI (insn)
   2853 	   || AARCH64_LDST_PIIMM (insn)
   2854 	   || AARCH64_LDST_U (insn)
   2855 	   || AARCH64_LDST_PREIMM (insn)
   2856 	   || AARCH64_LDST_RO (insn)
   2857 	   || AARCH64_LDST_UIMM (insn))
   2858    {
   2859       *rt = AARCH64_RT (insn);
   2860       *rtn = *rt;
   2861       if (AARCH64_LDST_PCREL (insn))
   2862 	*load = TRUE;
   2863       opc = AARCH64_BITS (insn, 22, 2);
   2864       v = AARCH64_BIT (insn, 26);
   2865       opc_v = opc | (v << 2);
   2866       *load =  (opc_v == 1 || opc_v == 2 || opc_v == 3
   2867 		|| opc_v == 5 || opc_v == 7);
   2868       return TRUE;
   2869    }
   2870   else if (AARCH64_LDST_SIMD_M (insn)
   2871 	   || AARCH64_LDST_SIMD_M_PI (insn))
   2872     {
   2873       *rt = AARCH64_RT (insn);
   2874       *load = AARCH64_BIT (insn, 22);
   2875       opcode = (insn >> 12) & 0xf;
   2876       switch (opcode)
   2877 	{
   2878 	case 0:
   2879 	case 2:
   2880 	  *rtn = *rt + 3;
   2881 	  break;
   2882 
   2883 	case 4:
   2884 	case 6:
   2885 	  *rtn = *rt + 2;
   2886 	  break;
   2887 
   2888 	case 7:
   2889 	  *rtn = *rt;
   2890 	  break;
   2891 
   2892 	case 8:
   2893 	case 10:
   2894 	  *rtn = *rt + 1;
   2895 	  break;
   2896 
   2897 	default:
   2898 	  return FALSE;
   2899 	}
   2900       return TRUE;
   2901     }
   2902   else if (AARCH64_LDST_SIMD_S (insn)
   2903 	   || AARCH64_LDST_SIMD_S_PI (insn))
   2904     {
   2905       *rt = AARCH64_RT (insn);
   2906       r = (insn >> 21) & 1;
   2907       *load = AARCH64_BIT (insn, 22);
   2908       opcode = (insn >> 13) & 0x7;
   2909       switch (opcode)
   2910 	{
   2911 	case 0:
   2912 	case 2:
   2913 	case 4:
   2914 	  *rtn = *rt + r;
   2915 	  break;
   2916 
   2917 	case 1:
   2918 	case 3:
   2919 	case 5:
   2920 	  *rtn = *rt + (r == 0 ? 2 : 3);
   2921 	  break;
   2922 
   2923 	case 6:
   2924 	  *rtn = *rt + r;
   2925 	  break;
   2926 
   2927 	case 7:
   2928 	  *rtn = *rt + (r == 0 ? 2 : 3);
   2929 	  break;
   2930 
   2931 	default:
   2932 	  return FALSE;
   2933 	}
   2934       return TRUE;
   2935     }
   2936 
   2937   return FALSE;
   2938 }
   2939 
   2940 /* Return TRUE if INSN is multiply-accumulate.  */
   2941 
   2942 static bfd_boolean
   2943 aarch64_mlxl_p (uint32_t insn)
   2944 {
   2945   uint32_t op31 = AARCH64_OP31 (insn);
   2946 
   2947   if (AARCH64_MAC (insn)
   2948       && (op31 == 0 || op31 == 1 || op31 == 5)
   2949       /* Exclude MUL instructions which are encoded as a multiple accumulate
   2950 	 with RA = XZR.  */
   2951       && AARCH64_RA (insn) != AARCH64_ZR)
   2952     return TRUE;
   2953 
   2954   return FALSE;
   2955 }
   2956 
   2957 /* Some early revisions of the Cortex-A53 have an erratum (835769) whereby
   2958    it is possible for a 64-bit multiply-accumulate instruction to generate an
   2959    incorrect result.  The details are quite complex and hard to
   2960    determine statically, since branches in the code may exist in some
   2961    circumstances, but all cases end with a memory (load, store, or
   2962    prefetch) instruction followed immediately by the multiply-accumulate
   2963    operation.  We employ a linker patching technique, by moving the potentially
   2964    affected multiply-accumulate instruction into a patch region and replacing
   2965    the original instruction with a branch to the patch.  This function checks
   2966    if INSN_1 is the memory operation followed by a multiply-accumulate
   2967    operation (INSN_2).  Return TRUE if an erratum sequence is found, FALSE
   2968    if INSN_1 and INSN_2 are safe.  */
   2969 
   2970 static bfd_boolean
   2971 aarch64_erratum_sequence (uint32_t insn_1, uint32_t insn_2)
   2972 {
   2973   uint32_t rt;
   2974   uint32_t rtn;
   2975   uint32_t rn;
   2976   uint32_t rm;
   2977   uint32_t ra;
   2978   bfd_boolean pair;
   2979   bfd_boolean load;
   2980 
   2981   if (aarch64_mlxl_p (insn_2)
   2982       && aarch64_mem_op_p (insn_1, &rt, &rtn, &pair, &load))
   2983     {
   2984       /* Any SIMD memory op is independent of the subsequent MLA
   2985 	 by definition of the erratum.  */
   2986       if (AARCH64_BIT (insn_1, 26))
   2987 	return TRUE;
   2988 
   2989       /* If not SIMD, check for integer memory ops and MLA relationship.  */
   2990       rn = AARCH64_RN (insn_2);
   2991       ra = AARCH64_RA (insn_2);
   2992       rm = AARCH64_RM (insn_2);
   2993 
   2994       /* If this is a load and there's a true(RAW) dependency, we are safe
   2995 	 and this is not an erratum sequence.  */
   2996       if (load &&
   2997 	  (rt == rn || rt == rm || rt == ra
   2998 	   || (pair && (rtn == rn || rtn == rm || rtn == ra))))
   2999 	return FALSE;
   3000 
   3001       /* We conservatively put out stubs for all other cases (including
   3002 	 writebacks).  */
   3003       return TRUE;
   3004     }
   3005 
   3006   return FALSE;
   3007 }
   3008 
   3009 /* Used to order a list of mapping symbols by address.  */
   3010 
   3011 static int
   3012 elf_aarch64_compare_mapping (const void *a, const void *b)
   3013 {
   3014   const elf_aarch64_section_map *amap = (const elf_aarch64_section_map *) a;
   3015   const elf_aarch64_section_map *bmap = (const elf_aarch64_section_map *) b;
   3016 
   3017   if (amap->vma > bmap->vma)
   3018     return 1;
   3019   else if (amap->vma < bmap->vma)
   3020     return -1;
   3021   else if (amap->type > bmap->type)
   3022     /* Ensure results do not depend on the host qsort for objects with
   3023        multiple mapping symbols at the same address by sorting on type
   3024        after vma.  */
   3025     return 1;
   3026   else if (amap->type < bmap->type)
   3027     return -1;
   3028   else
   3029     return 0;
   3030 }
   3031 
   3032 
   3033 static char *
   3034 _bfd_aarch64_erratum_835769_stub_name (unsigned num_fixes)
   3035 {
   3036   char *stub_name = (char *) bfd_malloc
   3037     (strlen ("__erratum_835769_veneer_") + 16);
   3038   sprintf (stub_name,"__erratum_835769_veneer_%d", num_fixes);
   3039   return stub_name;
   3040 }
   3041 
   3042 /* Scan for Cortex-A53 erratum 835769 sequence.
   3043 
   3044    Return TRUE else FALSE on abnormal termination.  */
   3045 
   3046 static bfd_boolean
   3047 _bfd_aarch64_erratum_835769_scan (bfd *input_bfd,
   3048 				  struct bfd_link_info *info,
   3049 				  unsigned int *num_fixes_p)
   3050 {
   3051   asection *section;
   3052   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
   3053   unsigned int num_fixes = *num_fixes_p;
   3054 
   3055   if (htab == NULL)
   3056     return TRUE;
   3057 
   3058   for (section = input_bfd->sections;
   3059        section != NULL;
   3060        section = section->next)
   3061     {
   3062       bfd_byte *contents = NULL;
   3063       struct _aarch64_elf_section_data *sec_data;
   3064       unsigned int span;
   3065 
   3066       if (elf_section_type (section) != SHT_PROGBITS
   3067 	  || (elf_section_flags (section) & SHF_EXECINSTR) == 0
   3068 	  || (section->flags & SEC_EXCLUDE) != 0
   3069 	  || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   3070 	  || (section->output_section == bfd_abs_section_ptr))
   3071 	continue;
   3072 
   3073       if (elf_section_data (section)->this_hdr.contents != NULL)
   3074 	contents = elf_section_data (section)->this_hdr.contents;
   3075       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
   3076 	return FALSE;
   3077 
   3078       sec_data = elf_aarch64_section_data (section);
   3079 
   3080       qsort (sec_data->map, sec_data->mapcount,
   3081 	     sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
   3082 
   3083       for (span = 0; span < sec_data->mapcount; span++)
   3084 	{
   3085 	  unsigned int span_start = sec_data->map[span].vma;
   3086 	  unsigned int span_end = ((span == sec_data->mapcount - 1)
   3087 				   ? sec_data->map[0].vma + section->size
   3088 				   : sec_data->map[span + 1].vma);
   3089 	  unsigned int i;
   3090 	  char span_type = sec_data->map[span].type;
   3091 
   3092 	  if (span_type == 'd')
   3093 	    continue;
   3094 
   3095 	  for (i = span_start; i + 4 < span_end; i += 4)
   3096 	    {
   3097 	      uint32_t insn_1 = bfd_getl32 (contents + i);
   3098 	      uint32_t insn_2 = bfd_getl32 (contents + i + 4);
   3099 
   3100 	      if (aarch64_erratum_sequence (insn_1, insn_2))
   3101 		{
   3102 		  struct elf_aarch64_stub_hash_entry *stub_entry;
   3103 		  char *stub_name = _bfd_aarch64_erratum_835769_stub_name (num_fixes);
   3104 		  if (! stub_name)
   3105 		    return FALSE;
   3106 
   3107 		  stub_entry = _bfd_aarch64_add_stub_entry_in_group (stub_name,
   3108 								     section,
   3109 								     htab);
   3110 		  if (! stub_entry)
   3111 		    return FALSE;
   3112 
   3113 		  stub_entry->stub_type = aarch64_stub_erratum_835769_veneer;
   3114 		  stub_entry->target_section = section;
   3115 		  stub_entry->target_value = i + 4;
   3116 		  stub_entry->veneered_insn = insn_2;
   3117 		  stub_entry->output_name = stub_name;
   3118 		  num_fixes++;
   3119 		}
   3120 	    }
   3121 	}
   3122       if (elf_section_data (section)->this_hdr.contents == NULL)
   3123 	free (contents);
   3124     }
   3125 
   3126   *num_fixes_p = num_fixes;
   3127 
   3128   return TRUE;
   3129 }
   3130 
   3131 
   3132 /* Test if instruction INSN is ADRP.  */
   3133 
   3134 static bfd_boolean
   3135 _bfd_aarch64_adrp_p (uint32_t insn)
   3136 {
   3137   return ((insn & 0x9f000000) == 0x90000000);
   3138 }
   3139 
   3140 
   3141 /* Helper predicate to look for cortex-a53 erratum 843419 sequence 1.  */
   3142 
   3143 static bfd_boolean
   3144 _bfd_aarch64_erratum_843419_sequence_p (uint32_t insn_1, uint32_t insn_2,
   3145 					uint32_t insn_3)
   3146 {
   3147   uint32_t rt;
   3148   uint32_t rt2;
   3149   bfd_boolean pair;
   3150   bfd_boolean load;
   3151 
   3152   return (aarch64_mem_op_p (insn_2, &rt, &rt2, &pair, &load)
   3153 	  && (!pair
   3154 	      || (pair && !load))
   3155 	  && AARCH64_LDST_UIMM (insn_3)
   3156 	  && AARCH64_RN (insn_3) == AARCH64_RD (insn_1));
   3157 }
   3158 
   3159 
   3160 /* Test for the presence of Cortex-A53 erratum 843419 instruction sequence.
   3161 
   3162    Return TRUE if section CONTENTS at offset I contains one of the
   3163    erratum 843419 sequences, otherwise return FALSE.  If a sequence is
   3164    seen set P_VENEER_I to the offset of the final LOAD/STORE
   3165    instruction in the sequence.
   3166  */
   3167 
   3168 static bfd_boolean
   3169 _bfd_aarch64_erratum_843419_p (bfd_byte *contents, bfd_vma vma,
   3170 			       bfd_vma i, bfd_vma span_end,
   3171 			       bfd_vma *p_veneer_i)
   3172 {
   3173   uint32_t insn_1 = bfd_getl32 (contents + i);
   3174 
   3175   if (!_bfd_aarch64_adrp_p (insn_1))
   3176     return FALSE;
   3177 
   3178   if (span_end < i + 12)
   3179     return FALSE;
   3180 
   3181   uint32_t insn_2 = bfd_getl32 (contents + i + 4);
   3182   uint32_t insn_3 = bfd_getl32 (contents + i + 8);
   3183 
   3184   if ((vma & 0xfff) != 0xff8 && (vma & 0xfff) != 0xffc)
   3185     return FALSE;
   3186 
   3187   if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_3))
   3188     {
   3189       *p_veneer_i = i + 8;
   3190       return TRUE;
   3191     }
   3192 
   3193   if (span_end < i + 16)
   3194     return FALSE;
   3195 
   3196   uint32_t insn_4 = bfd_getl32 (contents + i + 12);
   3197 
   3198   if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_4))
   3199     {
   3200       *p_veneer_i = i + 12;
   3201       return TRUE;
   3202     }
   3203 
   3204   return FALSE;
   3205 }
   3206 
   3207 
   3208 /* Resize all stub sections.  */
   3209 
   3210 static void
   3211 _bfd_aarch64_resize_stubs (struct elf_aarch64_link_hash_table *htab)
   3212 {
   3213   asection *section;
   3214 
   3215   /* OK, we've added some stubs.  Find out the new size of the
   3216      stub sections.  */
   3217   for (section = htab->stub_bfd->sections;
   3218        section != NULL; section = section->next)
   3219     {
   3220       /* Ignore non-stub sections.  */
   3221       if (!strstr (section->name, STUB_SUFFIX))
   3222 	continue;
   3223       section->size = 0;
   3224     }
   3225 
   3226   bfd_hash_traverse (&htab->stub_hash_table, aarch64_size_one_stub, htab);
   3227 
   3228   for (section = htab->stub_bfd->sections;
   3229        section != NULL; section = section->next)
   3230     {
   3231       if (!strstr (section->name, STUB_SUFFIX))
   3232 	continue;
   3233 
   3234       if (section->size)
   3235 	section->size += 4;
   3236 
   3237       /* Ensure all stub sections have a size which is a multiple of
   3238 	 4096.  This is important in order to ensure that the insertion
   3239 	 of stub sections does not in itself move existing code around
   3240 	 in such a way that new errata sequences are created.  */
   3241       if (htab->fix_erratum_843419)
   3242 	if (section->size)
   3243 	  section->size = BFD_ALIGN (section->size, 0x1000);
   3244     }
   3245 }
   3246 
   3247 
   3248 /* Construct an erratum 843419 workaround stub name.
   3249  */
   3250 
   3251 static char *
   3252 _bfd_aarch64_erratum_843419_stub_name (asection *input_section,
   3253 				       bfd_vma offset)
   3254 {
   3255   const bfd_size_type len = 8 + 4 + 1 + 8 + 1 + 16 + 1;
   3256   char *stub_name = bfd_malloc (len);
   3257 
   3258   if (stub_name != NULL)
   3259     snprintf (stub_name, len, "e843419@%04x_%08x_%" BFD_VMA_FMT "x",
   3260 	      input_section->owner->id,
   3261 	      input_section->id,
   3262 	      offset);
   3263   return stub_name;
   3264 }
   3265 
   3266 /*  Build a stub_entry structure describing an 843419 fixup.
   3267 
   3268     The stub_entry constructed is populated with the bit pattern INSN
   3269     of the instruction located at OFFSET within input SECTION.
   3270 
   3271     Returns TRUE on success.  */
   3272 
   3273 static bfd_boolean
   3274 _bfd_aarch64_erratum_843419_fixup (uint32_t insn,
   3275 				   bfd_vma adrp_offset,
   3276 				   bfd_vma ldst_offset,
   3277 				   asection *section,
   3278 				   struct bfd_link_info *info)
   3279 {
   3280   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
   3281   char *stub_name;
   3282   struct elf_aarch64_stub_hash_entry *stub_entry;
   3283 
   3284   stub_name = _bfd_aarch64_erratum_843419_stub_name (section, ldst_offset);
   3285   stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   3286 					 FALSE, FALSE);
   3287   if (stub_entry)
   3288     {
   3289       free (stub_name);
   3290       return TRUE;
   3291     }
   3292 
   3293   /* We always place an 843419 workaround veneer in the stub section
   3294      attached to the input section in which an erratum sequence has
   3295      been found.  This ensures that later in the link process (in
   3296      elfNN_aarch64_write_section) when we copy the veneered
   3297      instruction from the input section into the stub section the
   3298      copied instruction will have had any relocations applied to it.
   3299      If we placed workaround veneers in any other stub section then we
   3300      could not assume that all relocations have been processed on the
   3301      corresponding input section at the point we output the stub
   3302      section.
   3303    */
   3304 
   3305   stub_entry = _bfd_aarch64_add_stub_entry_after (stub_name, section, htab);
   3306   if (stub_entry == NULL)
   3307     {
   3308       free (stub_name);
   3309       return FALSE;
   3310     }
   3311 
   3312   stub_entry->adrp_offset = adrp_offset;
   3313   stub_entry->target_value = ldst_offset;
   3314   stub_entry->target_section = section;
   3315   stub_entry->stub_type = aarch64_stub_erratum_843419_veneer;
   3316   stub_entry->veneered_insn = insn;
   3317   stub_entry->output_name = stub_name;
   3318 
   3319   return TRUE;
   3320 }
   3321 
   3322 
   3323 /* Scan an input section looking for the signature of erratum 843419.
   3324 
   3325    Scans input SECTION in INPUT_BFD looking for erratum 843419
   3326    signatures, for each signature found a stub_entry is created
   3327    describing the location of the erratum for subsequent fixup.
   3328 
   3329    Return TRUE on successful scan, FALSE on failure to scan.
   3330  */
   3331 
   3332 static bfd_boolean
   3333 _bfd_aarch64_erratum_843419_scan (bfd *input_bfd, asection *section,
   3334 				  struct bfd_link_info *info)
   3335 {
   3336   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
   3337 
   3338   if (htab == NULL)
   3339     return TRUE;
   3340 
   3341   if (elf_section_type (section) != SHT_PROGBITS
   3342       || (elf_section_flags (section) & SHF_EXECINSTR) == 0
   3343       || (section->flags & SEC_EXCLUDE) != 0
   3344       || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   3345       || (section->output_section == bfd_abs_section_ptr))
   3346     return TRUE;
   3347 
   3348   do
   3349     {
   3350       bfd_byte *contents = NULL;
   3351       struct _aarch64_elf_section_data *sec_data;
   3352       unsigned int span;
   3353 
   3354       if (elf_section_data (section)->this_hdr.contents != NULL)
   3355 	contents = elf_section_data (section)->this_hdr.contents;
   3356       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
   3357 	return FALSE;
   3358 
   3359       sec_data = elf_aarch64_section_data (section);
   3360 
   3361       qsort (sec_data->map, sec_data->mapcount,
   3362 	     sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
   3363 
   3364       for (span = 0; span < sec_data->mapcount; span++)
   3365 	{
   3366 	  unsigned int span_start = sec_data->map[span].vma;
   3367 	  unsigned int span_end = ((span == sec_data->mapcount - 1)
   3368 				   ? sec_data->map[0].vma + section->size
   3369 				   : sec_data->map[span + 1].vma);
   3370 	  unsigned int i;
   3371 	  char span_type = sec_data->map[span].type;
   3372 
   3373 	  if (span_type == 'd')
   3374 	    continue;
   3375 
   3376 	  for (i = span_start; i + 8 < span_end; i += 4)
   3377 	    {
   3378 	      bfd_vma vma = (section->output_section->vma
   3379 			     + section->output_offset
   3380 			     + i);
   3381 	      bfd_vma veneer_i;
   3382 
   3383 	      if (_bfd_aarch64_erratum_843419_p
   3384 		  (contents, vma, i, span_end, &veneer_i))
   3385 		{
   3386 		  uint32_t insn = bfd_getl32 (contents + veneer_i);
   3387 
   3388 		  if (!_bfd_aarch64_erratum_843419_fixup (insn, i, veneer_i,
   3389 							  section, info))
   3390 		    return FALSE;
   3391 		}
   3392 	    }
   3393 	}
   3394 
   3395       if (elf_section_data (section)->this_hdr.contents == NULL)
   3396 	free (contents);
   3397     }
   3398   while (0);
   3399 
   3400   return TRUE;
   3401 }
   3402 
   3403 
   3404 /* Determine and set the size of the stub section for a final link.
   3405 
   3406    The basic idea here is to examine all the relocations looking for
   3407    PC-relative calls to a target that is unreachable with a "bl"
   3408    instruction.  */
   3409 
   3410 bfd_boolean
   3411 elfNN_aarch64_size_stubs (bfd *output_bfd,
   3412 			  bfd *stub_bfd,
   3413 			  struct bfd_link_info *info,
   3414 			  bfd_signed_vma group_size,
   3415 			  asection * (*add_stub_section) (const char *,
   3416 							  asection *),
   3417 			  void (*layout_sections_again) (void))
   3418 {
   3419   bfd_size_type stub_group_size;
   3420   bfd_boolean stubs_always_before_branch;
   3421   bfd_boolean stub_changed = FALSE;
   3422   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
   3423   unsigned int num_erratum_835769_fixes = 0;
   3424 
   3425   /* Propagate mach to stub bfd, because it may not have been
   3426      finalized when we created stub_bfd.  */
   3427   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
   3428 		     bfd_get_mach (output_bfd));
   3429 
   3430   /* Stash our params away.  */
   3431   htab->stub_bfd = stub_bfd;
   3432   htab->add_stub_section = add_stub_section;
   3433   htab->layout_sections_again = layout_sections_again;
   3434   stubs_always_before_branch = group_size < 0;
   3435   if (group_size < 0)
   3436     stub_group_size = -group_size;
   3437   else
   3438     stub_group_size = group_size;
   3439 
   3440   if (stub_group_size == 1)
   3441     {
   3442       /* Default values.  */
   3443       /* AArch64 branch range is +-128MB. The value used is 1MB less.  */
   3444       stub_group_size = 127 * 1024 * 1024;
   3445     }
   3446 
   3447   group_sections (htab, stub_group_size, stubs_always_before_branch);
   3448 
   3449   (*htab->layout_sections_again) ();
   3450 
   3451   if (htab->fix_erratum_835769)
   3452     {
   3453       bfd *input_bfd;
   3454 
   3455       for (input_bfd = info->input_bfds;
   3456 	   input_bfd != NULL; input_bfd = input_bfd->link.next)
   3457 	if (!_bfd_aarch64_erratum_835769_scan (input_bfd, info,
   3458 					       &num_erratum_835769_fixes))
   3459 	  return FALSE;
   3460 
   3461       _bfd_aarch64_resize_stubs (htab);
   3462       (*htab->layout_sections_again) ();
   3463     }
   3464 
   3465   if (htab->fix_erratum_843419)
   3466     {
   3467       bfd *input_bfd;
   3468 
   3469       for (input_bfd = info->input_bfds;
   3470 	   input_bfd != NULL;
   3471 	   input_bfd = input_bfd->link.next)
   3472 	{
   3473 	  asection *section;
   3474 
   3475 	  for (section = input_bfd->sections;
   3476 	       section != NULL;
   3477 	       section = section->next)
   3478 	    if (!_bfd_aarch64_erratum_843419_scan (input_bfd, section, info))
   3479 	      return FALSE;
   3480 	}
   3481 
   3482       _bfd_aarch64_resize_stubs (htab);
   3483       (*htab->layout_sections_again) ();
   3484     }
   3485 
   3486   while (1)
   3487     {
   3488       bfd *input_bfd;
   3489 
   3490       for (input_bfd = info->input_bfds;
   3491 	   input_bfd != NULL; input_bfd = input_bfd->link.next)
   3492 	{
   3493 	  Elf_Internal_Shdr *symtab_hdr;
   3494 	  asection *section;
   3495 	  Elf_Internal_Sym *local_syms = NULL;
   3496 
   3497 	  /* We'll need the symbol table in a second.  */
   3498 	  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   3499 	  if (symtab_hdr->sh_info == 0)
   3500 	    continue;
   3501 
   3502 	  /* Walk over each section attached to the input bfd.  */
   3503 	  for (section = input_bfd->sections;
   3504 	       section != NULL; section = section->next)
   3505 	    {
   3506 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   3507 
   3508 	      /* If there aren't any relocs, then there's nothing more
   3509 		 to do.  */
   3510 	      if ((section->flags & SEC_RELOC) == 0
   3511 		  || section->reloc_count == 0
   3512 		  || (section->flags & SEC_CODE) == 0)
   3513 		continue;
   3514 
   3515 	      /* If this section is a link-once section that will be
   3516 		 discarded, then don't create any stubs.  */
   3517 	      if (section->output_section == NULL
   3518 		  || section->output_section->owner != output_bfd)
   3519 		continue;
   3520 
   3521 	      /* Get the relocs.  */
   3522 	      internal_relocs
   3523 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL,
   3524 					     NULL, info->keep_memory);
   3525 	      if (internal_relocs == NULL)
   3526 		goto error_ret_free_local;
   3527 
   3528 	      /* Now examine each relocation.  */
   3529 	      irela = internal_relocs;
   3530 	      irelaend = irela + section->reloc_count;
   3531 	      for (; irela < irelaend; irela++)
   3532 		{
   3533 		  unsigned int r_type, r_indx;
   3534 		  enum elf_aarch64_stub_type stub_type;
   3535 		  struct elf_aarch64_stub_hash_entry *stub_entry;
   3536 		  asection *sym_sec;
   3537 		  bfd_vma sym_value;
   3538 		  bfd_vma destination;
   3539 		  struct elf_aarch64_link_hash_entry *hash;
   3540 		  const char *sym_name;
   3541 		  char *stub_name;
   3542 		  const asection *id_sec;
   3543 		  unsigned char st_type;
   3544 		  bfd_size_type len;
   3545 
   3546 		  r_type = ELFNN_R_TYPE (irela->r_info);
   3547 		  r_indx = ELFNN_R_SYM (irela->r_info);
   3548 
   3549 		  if (r_type >= (unsigned int) R_AARCH64_end)
   3550 		    {
   3551 		      bfd_set_error (bfd_error_bad_value);
   3552 		    error_ret_free_internal:
   3553 		      if (elf_section_data (section)->relocs == NULL)
   3554 			free (internal_relocs);
   3555 		      goto error_ret_free_local;
   3556 		    }
   3557 
   3558 		  /* Only look for stubs on unconditional branch and
   3559 		     branch and link instructions.  */
   3560 		  if (r_type != (unsigned int) AARCH64_R (CALL26)
   3561 		      && r_type != (unsigned int) AARCH64_R (JUMP26))
   3562 		    continue;
   3563 
   3564 		  /* Now determine the call target, its name, value,
   3565 		     section.  */
   3566 		  sym_sec = NULL;
   3567 		  sym_value = 0;
   3568 		  destination = 0;
   3569 		  hash = NULL;
   3570 		  sym_name = NULL;
   3571 		  if (r_indx < symtab_hdr->sh_info)
   3572 		    {
   3573 		      /* It's a local symbol.  */
   3574 		      Elf_Internal_Sym *sym;
   3575 		      Elf_Internal_Shdr *hdr;
   3576 
   3577 		      if (local_syms == NULL)
   3578 			{
   3579 			  local_syms
   3580 			    = (Elf_Internal_Sym *) symtab_hdr->contents;
   3581 			  if (local_syms == NULL)
   3582 			    local_syms
   3583 			      = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
   3584 						      symtab_hdr->sh_info, 0,
   3585 						      NULL, NULL, NULL);
   3586 			  if (local_syms == NULL)
   3587 			    goto error_ret_free_internal;
   3588 			}
   3589 
   3590 		      sym = local_syms + r_indx;
   3591 		      hdr = elf_elfsections (input_bfd)[sym->st_shndx];
   3592 		      sym_sec = hdr->bfd_section;
   3593 		      if (!sym_sec)
   3594 			/* This is an undefined symbol.  It can never
   3595 			   be resolved.  */
   3596 			continue;
   3597 
   3598 		      if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
   3599 			sym_value = sym->st_value;
   3600 		      destination = (sym_value + irela->r_addend
   3601 				     + sym_sec->output_offset
   3602 				     + sym_sec->output_section->vma);
   3603 		      st_type = ELF_ST_TYPE (sym->st_info);
   3604 		      sym_name
   3605 			= bfd_elf_string_from_elf_section (input_bfd,
   3606 							   symtab_hdr->sh_link,
   3607 							   sym->st_name);
   3608 		    }
   3609 		  else
   3610 		    {
   3611 		      int e_indx;
   3612 
   3613 		      e_indx = r_indx - symtab_hdr->sh_info;
   3614 		      hash = ((struct elf_aarch64_link_hash_entry *)
   3615 			      elf_sym_hashes (input_bfd)[e_indx]);
   3616 
   3617 		      while (hash->root.root.type == bfd_link_hash_indirect
   3618 			     || hash->root.root.type == bfd_link_hash_warning)
   3619 			hash = ((struct elf_aarch64_link_hash_entry *)
   3620 				hash->root.root.u.i.link);
   3621 
   3622 		      if (hash->root.root.type == bfd_link_hash_defined
   3623 			  || hash->root.root.type == bfd_link_hash_defweak)
   3624 			{
   3625 			  struct elf_aarch64_link_hash_table *globals =
   3626 			    elf_aarch64_hash_table (info);
   3627 			  sym_sec = hash->root.root.u.def.section;
   3628 			  sym_value = hash->root.root.u.def.value;
   3629 			  /* For a destination in a shared library,
   3630 			     use the PLT stub as target address to
   3631 			     decide whether a branch stub is
   3632 			     needed.  */
   3633 			  if (globals->root.splt != NULL && hash != NULL
   3634 			      && hash->root.plt.offset != (bfd_vma) - 1)
   3635 			    {
   3636 			      sym_sec = globals->root.splt;
   3637 			      sym_value = hash->root.plt.offset;
   3638 			      if (sym_sec->output_section != NULL)
   3639 				destination = (sym_value
   3640 					       + sym_sec->output_offset
   3641 					       +
   3642 					       sym_sec->output_section->vma);
   3643 			    }
   3644 			  else if (sym_sec->output_section != NULL)
   3645 			    destination = (sym_value + irela->r_addend
   3646 					   + sym_sec->output_offset
   3647 					   + sym_sec->output_section->vma);
   3648 			}
   3649 		      else if (hash->root.root.type == bfd_link_hash_undefined
   3650 			       || (hash->root.root.type
   3651 				   == bfd_link_hash_undefweak))
   3652 			{
   3653 			  /* For a shared library, use the PLT stub as
   3654 			     target address to decide whether a long
   3655 			     branch stub is needed.
   3656 			     For absolute code, they cannot be handled.  */
   3657 			  struct elf_aarch64_link_hash_table *globals =
   3658 			    elf_aarch64_hash_table (info);
   3659 
   3660 			  if (globals->root.splt != NULL && hash != NULL
   3661 			      && hash->root.plt.offset != (bfd_vma) - 1)
   3662 			    {
   3663 			      sym_sec = globals->root.splt;
   3664 			      sym_value = hash->root.plt.offset;
   3665 			      if (sym_sec->output_section != NULL)
   3666 				destination = (sym_value
   3667 					       + sym_sec->output_offset
   3668 					       +
   3669 					       sym_sec->output_section->vma);
   3670 			    }
   3671 			  else
   3672 			    continue;
   3673 			}
   3674 		      else
   3675 			{
   3676 			  bfd_set_error (bfd_error_bad_value);
   3677 			  goto error_ret_free_internal;
   3678 			}
   3679 		      st_type = ELF_ST_TYPE (hash->root.type);
   3680 		      sym_name = hash->root.root.root.string;
   3681 		    }
   3682 
   3683 		  /* Determine what (if any) linker stub is needed.  */
   3684 		  stub_type = aarch64_type_of_stub
   3685 		    (info, section, irela, st_type, hash, destination);
   3686 		  if (stub_type == aarch64_stub_none)
   3687 		    continue;
   3688 
   3689 		  /* Support for grouping stub sections.  */
   3690 		  id_sec = htab->stub_group[section->id].link_sec;
   3691 
   3692 		  /* Get the name of this stub.  */
   3693 		  stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, hash,
   3694 						       irela);
   3695 		  if (!stub_name)
   3696 		    goto error_ret_free_internal;
   3697 
   3698 		  stub_entry =
   3699 		    aarch64_stub_hash_lookup (&htab->stub_hash_table,
   3700 					      stub_name, FALSE, FALSE);
   3701 		  if (stub_entry != NULL)
   3702 		    {
   3703 		      /* The proper stub has already been created.  */
   3704 		      free (stub_name);
   3705 		      continue;
   3706 		    }
   3707 
   3708 		  stub_entry = _bfd_aarch64_add_stub_entry_in_group
   3709 		    (stub_name, section, htab);
   3710 		  if (stub_entry == NULL)
   3711 		    {
   3712 		      free (stub_name);
   3713 		      goto error_ret_free_internal;
   3714 		    }
   3715 
   3716 		  stub_entry->target_value = sym_value;
   3717 		  stub_entry->target_section = sym_sec;
   3718 		  stub_entry->stub_type = stub_type;
   3719 		  stub_entry->h = hash;
   3720 		  stub_entry->st_type = st_type;
   3721 
   3722 		  if (sym_name == NULL)
   3723 		    sym_name = "unnamed";
   3724 		  len = sizeof (STUB_ENTRY_NAME) + strlen (sym_name);
   3725 		  stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
   3726 		  if (stub_entry->output_name == NULL)
   3727 		    {
   3728 		      free (stub_name);
   3729 		      goto error_ret_free_internal;
   3730 		    }
   3731 
   3732 		  snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
   3733 			    sym_name);
   3734 
   3735 		  stub_changed = TRUE;
   3736 		}
   3737 
   3738 	      /* We're done with the internal relocs, free them.  */
   3739 	      if (elf_section_data (section)->relocs == NULL)
   3740 		free (internal_relocs);
   3741 	    }
   3742 	}
   3743 
   3744       if (!stub_changed)
   3745 	break;
   3746 
   3747       _bfd_aarch64_resize_stubs (htab);
   3748 
   3749       /* Ask the linker to do its stuff.  */
   3750       (*htab->layout_sections_again) ();
   3751       stub_changed = FALSE;
   3752     }
   3753 
   3754   return TRUE;
   3755 
   3756 error_ret_free_local:
   3757   return FALSE;
   3758 }
   3759 
   3760 /* Build all the stubs associated with the current output file.  The
   3761    stubs are kept in a hash table attached to the main linker hash
   3762    table.  We also set up the .plt entries for statically linked PIC
   3763    functions here.  This function is called via aarch64_elf_finish in the
   3764    linker.  */
   3765 
   3766 bfd_boolean
   3767 elfNN_aarch64_build_stubs (struct bfd_link_info *info)
   3768 {
   3769   asection *stub_sec;
   3770   struct bfd_hash_table *table;
   3771   struct elf_aarch64_link_hash_table *htab;
   3772 
   3773   htab = elf_aarch64_hash_table (info);
   3774 
   3775   for (stub_sec = htab->stub_bfd->sections;
   3776        stub_sec != NULL; stub_sec = stub_sec->next)
   3777     {
   3778       bfd_size_type size;
   3779 
   3780       /* Ignore non-stub sections.  */
   3781       if (!strstr (stub_sec->name, STUB_SUFFIX))
   3782 	continue;
   3783 
   3784       /* Allocate memory to hold the linker stubs.  */
   3785       size = stub_sec->size;
   3786       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
   3787       if (stub_sec->contents == NULL && size != 0)
   3788 	return FALSE;
   3789       stub_sec->size = 0;
   3790 
   3791       bfd_putl32 (0x14000000 | (size >> 2), stub_sec->contents);
   3792       stub_sec->size += 4;
   3793     }
   3794 
   3795   /* Build the stubs as directed by the stub hash table.  */
   3796   table = &htab->stub_hash_table;
   3797   bfd_hash_traverse (table, aarch64_build_one_stub, info);
   3798 
   3799   return TRUE;
   3800 }
   3801 
   3802 
   3803 /* Add an entry to the code/data map for section SEC.  */
   3804 
   3805 static void
   3806 elfNN_aarch64_section_map_add (asection *sec, char type, bfd_vma vma)
   3807 {
   3808   struct _aarch64_elf_section_data *sec_data =
   3809     elf_aarch64_section_data (sec);
   3810   unsigned int newidx;
   3811 
   3812   if (sec_data->map == NULL)
   3813     {
   3814       sec_data->map = bfd_malloc (sizeof (elf_aarch64_section_map));
   3815       sec_data->mapcount = 0;
   3816       sec_data->mapsize = 1;
   3817     }
   3818 
   3819   newidx = sec_data->mapcount++;
   3820 
   3821   if (sec_data->mapcount > sec_data->mapsize)
   3822     {
   3823       sec_data->mapsize *= 2;
   3824       sec_data->map = bfd_realloc_or_free
   3825 	(sec_data->map, sec_data->mapsize * sizeof (elf_aarch64_section_map));
   3826     }
   3827 
   3828   if (sec_data->map)
   3829     {
   3830       sec_data->map[newidx].vma = vma;
   3831       sec_data->map[newidx].type = type;
   3832     }
   3833 }
   3834 
   3835 
   3836 /* Initialise maps of insn/data for input BFDs.  */
   3837 void
   3838 bfd_elfNN_aarch64_init_maps (bfd *abfd)
   3839 {
   3840   Elf_Internal_Sym *isymbuf;
   3841   Elf_Internal_Shdr *hdr;
   3842   unsigned int i, localsyms;
   3843 
   3844   /* Make sure that we are dealing with an AArch64 elf binary.  */
   3845   if (!is_aarch64_elf (abfd))
   3846     return;
   3847 
   3848   if ((abfd->flags & DYNAMIC) != 0)
   3849    return;
   3850 
   3851   hdr = &elf_symtab_hdr (abfd);
   3852   localsyms = hdr->sh_info;
   3853 
   3854   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
   3855      should contain the number of local symbols, which should come before any
   3856      global symbols.  Mapping symbols are always local.  */
   3857   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL, NULL);
   3858 
   3859   /* No internal symbols read?  Skip this BFD.  */
   3860   if (isymbuf == NULL)
   3861     return;
   3862 
   3863   for (i = 0; i < localsyms; i++)
   3864     {
   3865       Elf_Internal_Sym *isym = &isymbuf[i];
   3866       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   3867       const char *name;
   3868 
   3869       if (sec != NULL && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
   3870 	{
   3871 	  name = bfd_elf_string_from_elf_section (abfd,
   3872 						  hdr->sh_link,
   3873 						  isym->st_name);
   3874 
   3875 	  if (bfd_is_aarch64_special_symbol_name
   3876 	      (name, BFD_AARCH64_SPECIAL_SYM_TYPE_MAP))
   3877 	    elfNN_aarch64_section_map_add (sec, name[1], isym->st_value);
   3878 	}
   3879     }
   3880 }
   3881 
   3882 /* Set option values needed during linking.  */
   3883 void
   3884 bfd_elfNN_aarch64_set_options (struct bfd *output_bfd,
   3885 			       struct bfd_link_info *link_info,
   3886 			       int no_enum_warn,
   3887 			       int no_wchar_warn, int pic_veneer,
   3888 			       int fix_erratum_835769,
   3889 			       int fix_erratum_843419)
   3890 {
   3891   struct elf_aarch64_link_hash_table *globals;
   3892 
   3893   globals = elf_aarch64_hash_table (link_info);
   3894   globals->pic_veneer = pic_veneer;
   3895   globals->fix_erratum_835769 = fix_erratum_835769;
   3896   globals->fix_erratum_843419 = fix_erratum_843419;
   3897   globals->fix_erratum_843419_adr = TRUE;
   3898 
   3899   BFD_ASSERT (is_aarch64_elf (output_bfd));
   3900   elf_aarch64_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
   3901   elf_aarch64_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
   3902 }
   3903 
   3904 static bfd_vma
   3905 aarch64_calculate_got_entry_vma (struct elf_link_hash_entry *h,
   3906 				 struct elf_aarch64_link_hash_table
   3907 				 *globals, struct bfd_link_info *info,
   3908 				 bfd_vma value, bfd *output_bfd,
   3909 				 bfd_boolean *unresolved_reloc_p)
   3910 {
   3911   bfd_vma off = (bfd_vma) - 1;
   3912   asection *basegot = globals->root.sgot;
   3913   bfd_boolean dyn = globals->root.dynamic_sections_created;
   3914 
   3915   if (h != NULL)
   3916     {
   3917       BFD_ASSERT (basegot != NULL);
   3918       off = h->got.offset;
   3919       BFD_ASSERT (off != (bfd_vma) - 1);
   3920       if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
   3921 	  || (info->shared
   3922 	      && SYMBOL_REFERENCES_LOCAL (info, h))
   3923 	  || (ELF_ST_VISIBILITY (h->other)
   3924 	      && h->root.type == bfd_link_hash_undefweak))
   3925 	{
   3926 	  /* This is actually a static link, or it is a -Bsymbolic link
   3927 	     and the symbol is defined locally.  We must initialize this
   3928 	     entry in the global offset table.  Since the offset must
   3929 	     always be a multiple of 8 (4 in the case of ILP32), we use
   3930 	     the least significant bit to record whether we have
   3931 	     initialized it already.
   3932 	     When doing a dynamic link, we create a .rel(a).got relocation
   3933 	     entry to initialize the value.  This is done in the
   3934 	     finish_dynamic_symbol routine.  */
   3935 	  if ((off & 1) != 0)
   3936 	    off &= ~1;
   3937 	  else
   3938 	    {
   3939 	      bfd_put_NN (output_bfd, value, basegot->contents + off);
   3940 	      h->got.offset |= 1;
   3941 	    }
   3942 	}
   3943       else
   3944 	*unresolved_reloc_p = FALSE;
   3945 
   3946       off = off + basegot->output_section->vma + basegot->output_offset;
   3947     }
   3948 
   3949   return off;
   3950 }
   3951 
   3952 /* Change R_TYPE to a more efficient access model where possible,
   3953    return the new reloc type.  */
   3954 
   3955 static bfd_reloc_code_real_type
   3956 aarch64_tls_transition_without_check (bfd_reloc_code_real_type r_type,
   3957 				      struct elf_link_hash_entry *h)
   3958 {
   3959   bfd_boolean is_local = h == NULL;
   3960 
   3961   switch (r_type)
   3962     {
   3963     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
   3964     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
   3965       return (is_local
   3966 	      ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
   3967 	      : BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21);
   3968 
   3969     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
   3970     case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
   3971       return (is_local
   3972 	      ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
   3973 	      : BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC);
   3974 
   3975     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
   3976       return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 : r_type;
   3977 
   3978     case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
   3979       return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC : r_type;
   3980 
   3981     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
   3982     case BFD_RELOC_AARCH64_TLSDESC_CALL:
   3983       /* Instructions with these relocations will become NOPs.  */
   3984       return BFD_RELOC_AARCH64_NONE;
   3985 
   3986     default:
   3987       break;
   3988     }
   3989 
   3990   return r_type;
   3991 }
   3992 
   3993 static unsigned int
   3994 aarch64_reloc_got_type (bfd_reloc_code_real_type r_type)
   3995 {
   3996   switch (r_type)
   3997     {
   3998     case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
   3999     case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
   4000     case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
   4001     case BFD_RELOC_AARCH64_GOT_LD_PREL19:
   4002       return GOT_NORMAL;
   4003 
   4004     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
   4005     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
   4006       return GOT_TLS_GD;
   4007 
   4008     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
   4009     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
   4010     case BFD_RELOC_AARCH64_TLSDESC_CALL:
   4011     case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
   4012     case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
   4013       return GOT_TLSDESC_GD;
   4014 
   4015     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
   4016     case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
   4017     case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
   4018       return GOT_TLS_IE;
   4019 
   4020     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
   4021     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
   4022     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
   4023     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
   4024     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
   4025     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
   4026     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
   4027     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
   4028       return GOT_UNKNOWN;
   4029 
   4030     default:
   4031       break;
   4032     }
   4033   return GOT_UNKNOWN;
   4034 }
   4035 
   4036 static bfd_boolean
   4037 aarch64_can_relax_tls (bfd *input_bfd,
   4038 		       struct bfd_link_info *info,
   4039 		       bfd_reloc_code_real_type r_type,
   4040 		       struct elf_link_hash_entry *h,
   4041 		       unsigned long r_symndx)
   4042 {
   4043   unsigned int symbol_got_type;
   4044   unsigned int reloc_got_type;
   4045 
   4046   if (! IS_AARCH64_TLS_RELOC (r_type))
   4047     return FALSE;
   4048 
   4049   symbol_got_type = elfNN_aarch64_symbol_got_type (h, input_bfd, r_symndx);
   4050   reloc_got_type = aarch64_reloc_got_type (r_type);
   4051 
   4052   if (symbol_got_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (reloc_got_type))
   4053     return TRUE;
   4054 
   4055   if (info->shared)
   4056     return FALSE;
   4057 
   4058   if  (h && h->root.type == bfd_link_hash_undefweak)
   4059     return FALSE;
   4060 
   4061   return TRUE;
   4062 }
   4063 
   4064 /* Given the relocation code R_TYPE, return the relaxed bfd reloc
   4065    enumerator.  */
   4066 
   4067 static bfd_reloc_code_real_type
   4068 aarch64_tls_transition (bfd *input_bfd,
   4069 			struct bfd_link_info *info,
   4070 			unsigned int r_type,
   4071 			struct elf_link_hash_entry *h,
   4072 			unsigned long r_symndx)
   4073 {
   4074   bfd_reloc_code_real_type bfd_r_type
   4075     = elfNN_aarch64_bfd_reloc_from_type (r_type);
   4076 
   4077   if (! aarch64_can_relax_tls (input_bfd, info, bfd_r_type, h, r_symndx))
   4078     return bfd_r_type;
   4079 
   4080   return aarch64_tls_transition_without_check (bfd_r_type, h);
   4081 }
   4082 
   4083 /* Return the base VMA address which should be subtracted from real addresses
   4084    when resolving R_AARCH64_TLS_DTPREL relocation.  */
   4085 
   4086 static bfd_vma
   4087 dtpoff_base (struct bfd_link_info *info)
   4088 {
   4089   /* If tls_sec is NULL, we should have signalled an error already.  */
   4090   BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
   4091   return elf_hash_table (info)->tls_sec->vma;
   4092 }
   4093 
   4094 /* Return the base VMA address which should be subtracted from real addresses
   4095    when resolving R_AARCH64_TLS_GOTTPREL64 relocations.  */
   4096 
   4097 static bfd_vma
   4098 tpoff_base (struct bfd_link_info *info)
   4099 {
   4100   struct elf_link_hash_table *htab = elf_hash_table (info);
   4101 
   4102   /* If tls_sec is NULL, we should have signalled an error already.  */
   4103   BFD_ASSERT (htab->tls_sec != NULL);
   4104 
   4105   bfd_vma base = align_power ((bfd_vma) TCB_SIZE,
   4106 			      htab->tls_sec->alignment_power);
   4107   return htab->tls_sec->vma - base;
   4108 }
   4109 
   4110 static bfd_vma *
   4111 symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
   4112 		       unsigned long r_symndx)
   4113 {
   4114   /* Calculate the address of the GOT entry for symbol
   4115      referred to in h.  */
   4116   if (h != NULL)
   4117     return &h->got.offset;
   4118   else
   4119     {
   4120       /* local symbol */
   4121       struct elf_aarch64_local_symbol *l;
   4122 
   4123       l = elf_aarch64_locals (input_bfd);
   4124       return &l[r_symndx].got_offset;
   4125     }
   4126 }
   4127 
   4128 static void
   4129 symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
   4130 			unsigned long r_symndx)
   4131 {
   4132   bfd_vma *p;
   4133   p = symbol_got_offset_ref (input_bfd, h, r_symndx);
   4134   *p |= 1;
   4135 }
   4136 
   4137 static int
   4138 symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
   4139 			  unsigned long r_symndx)
   4140 {
   4141   bfd_vma value;
   4142   value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
   4143   return value & 1;
   4144 }
   4145 
   4146 static bfd_vma
   4147 symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
   4148 		   unsigned long r_symndx)
   4149 {
   4150   bfd_vma value;
   4151   value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
   4152   value &= ~1;
   4153   return value;
   4154 }
   4155 
   4156 static bfd_vma *
   4157 symbol_tlsdesc_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
   4158 			       unsigned long r_symndx)
   4159 {
   4160   /* Calculate the address of the GOT entry for symbol
   4161      referred to in h.  */
   4162   if (h != NULL)
   4163     {
   4164       struct elf_aarch64_link_hash_entry *eh;
   4165       eh = (struct elf_aarch64_link_hash_entry *) h;
   4166       return &eh->tlsdesc_got_jump_table_offset;
   4167     }
   4168   else
   4169     {
   4170       /* local symbol */
   4171       struct elf_aarch64_local_symbol *l;
   4172 
   4173       l = elf_aarch64_locals (input_bfd);
   4174       return &l[r_symndx].tlsdesc_got_jump_table_offset;
   4175     }
   4176 }
   4177 
   4178 static void
   4179 symbol_tlsdesc_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
   4180 				unsigned long r_symndx)
   4181 {
   4182   bfd_vma *p;
   4183   p = symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
   4184   *p |= 1;
   4185 }
   4186 
   4187 static int
   4188 symbol_tlsdesc_got_offset_mark_p (bfd *input_bfd,
   4189 				  struct elf_link_hash_entry *h,
   4190 				  unsigned long r_symndx)
   4191 {
   4192   bfd_vma value;
   4193   value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
   4194   return value & 1;
   4195 }
   4196 
   4197 static bfd_vma
   4198 symbol_tlsdesc_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
   4199 			  unsigned long r_symndx)
   4200 {
   4201   bfd_vma value;
   4202   value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
   4203   value &= ~1;
   4204   return value;
   4205 }
   4206 
   4207 /* Data for make_branch_to_erratum_835769_stub().  */
   4208 
   4209 struct erratum_835769_branch_to_stub_data
   4210 {
   4211   struct bfd_link_info *info;
   4212   asection *output_section;
   4213   bfd_byte *contents;
   4214 };
   4215 
   4216 /* Helper to insert branches to erratum 835769 stubs in the right
   4217    places for a particular section.  */
   4218 
   4219 static bfd_boolean
   4220 make_branch_to_erratum_835769_stub (struct bfd_hash_entry *gen_entry,
   4221 				    void *in_arg)
   4222 {
   4223   struct elf_aarch64_stub_hash_entry *stub_entry;
   4224   struct erratum_835769_branch_to_stub_data *data;
   4225   bfd_byte *contents;
   4226   unsigned long branch_insn = 0;
   4227   bfd_vma veneered_insn_loc, veneer_entry_loc;
   4228   bfd_signed_vma branch_offset;
   4229   unsigned int target;
   4230   bfd *abfd;
   4231 
   4232   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
   4233   data = (struct erratum_835769_branch_to_stub_data *) in_arg;
   4234 
   4235   if (stub_entry->target_section != data->output_section
   4236       || stub_entry->stub_type != aarch64_stub_erratum_835769_veneer)
   4237     return TRUE;
   4238 
   4239   contents = data->contents;
   4240   veneered_insn_loc = stub_entry->target_section->output_section->vma
   4241 		      + stub_entry->target_section->output_offset
   4242 		      + stub_entry->target_value;
   4243   veneer_entry_loc = stub_entry->stub_sec->output_section->vma
   4244 		     + stub_entry->stub_sec->output_offset
   4245 		     + stub_entry->stub_offset;
   4246   branch_offset = veneer_entry_loc - veneered_insn_loc;
   4247 
   4248   abfd = stub_entry->target_section->owner;
   4249   if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
   4250 	    (*_bfd_error_handler)
   4251 		(_("%B: error: Erratum 835769 stub out "
   4252 		   "of range (input file too large)"), abfd);
   4253 
   4254   target = stub_entry->target_value;
   4255   branch_insn = 0x14000000;
   4256   branch_offset >>= 2;
   4257   branch_offset &= 0x3ffffff;
   4258   branch_insn |= branch_offset;
   4259   bfd_putl32 (branch_insn, &contents[target]);
   4260 
   4261   return TRUE;
   4262 }
   4263 
   4264 
   4265 static bfd_boolean
   4266 _bfd_aarch64_erratum_843419_branch_to_stub (struct bfd_hash_entry *gen_entry,
   4267 					    void *in_arg)
   4268 {
   4269   struct elf_aarch64_stub_hash_entry *stub_entry
   4270     = (struct elf_aarch64_stub_hash_entry *) gen_entry;
   4271   struct erratum_835769_branch_to_stub_data *data
   4272     = (struct erratum_835769_branch_to_stub_data *) in_arg;
   4273   struct bfd_link_info *info;
   4274   struct elf_aarch64_link_hash_table *htab;
   4275   bfd_byte *contents;
   4276   asection *section;
   4277   bfd *abfd;
   4278   bfd_vma place;
   4279   uint32_t insn;
   4280 
   4281   info = data->info;
   4282   contents = data->contents;
   4283   section = data->output_section;
   4284 
   4285   htab = elf_aarch64_hash_table (info);
   4286 
   4287   if (stub_entry->target_section != section
   4288       || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer)
   4289     return TRUE;
   4290 
   4291   insn = bfd_getl32 (contents + stub_entry->target_value);
   4292   bfd_putl32 (insn,
   4293 	      stub_entry->stub_sec->contents + stub_entry->stub_offset);
   4294 
   4295   place = (section->output_section->vma + section->output_offset
   4296 	   + stub_entry->adrp_offset);
   4297   insn = bfd_getl32 (contents + stub_entry->adrp_offset);
   4298 
   4299   if ((insn & AARCH64_ADRP_OP_MASK) !=  AARCH64_ADRP_OP)
   4300     abort ();
   4301 
   4302   bfd_signed_vma imm =
   4303     (_bfd_aarch64_sign_extend
   4304      ((bfd_vma) _bfd_aarch64_decode_adrp_imm (insn) << 12, 33)
   4305      - (place & 0xfff));
   4306 
   4307   if (htab->fix_erratum_843419_adr
   4308       && (imm >= AARCH64_MIN_ADRP_IMM  && imm <= AARCH64_MAX_ADRP_IMM))
   4309     {
   4310       insn = (_bfd_aarch64_reencode_adr_imm (AARCH64_ADR_OP, imm)
   4311 	      | AARCH64_RT (insn));
   4312       bfd_putl32 (insn, contents + stub_entry->adrp_offset);
   4313     }
   4314   else
   4315     {
   4316       bfd_vma veneered_insn_loc;
   4317       bfd_vma veneer_entry_loc;
   4318       bfd_signed_vma branch_offset;
   4319       uint32_t branch_insn;
   4320 
   4321       veneered_insn_loc = stub_entry->target_section->output_section->vma
   4322 	+ stub_entry->target_section->output_offset
   4323 	+ stub_entry->target_value;
   4324       veneer_entry_loc = stub_entry->stub_sec->output_section->vma
   4325 	+ stub_entry->stub_sec->output_offset
   4326 	+ stub_entry->stub_offset;
   4327       branch_offset = veneer_entry_loc - veneered_insn_loc;
   4328 
   4329       abfd = stub_entry->target_section->owner;
   4330       if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
   4331 	(*_bfd_error_handler)
   4332 	  (_("%B: error: Erratum 843419 stub out "
   4333 	     "of range (input file too large)"), abfd);
   4334 
   4335       branch_insn = 0x14000000;
   4336       branch_offset >>= 2;
   4337       branch_offset &= 0x3ffffff;
   4338       branch_insn |= branch_offset;
   4339       bfd_putl32 (branch_insn, contents + stub_entry->target_value);
   4340     }
   4341   return TRUE;
   4342 }
   4343 
   4344 
   4345 static bfd_boolean
   4346 elfNN_aarch64_write_section (bfd *output_bfd  ATTRIBUTE_UNUSED,
   4347 			     struct bfd_link_info *link_info,
   4348 			     asection *sec,
   4349 			     bfd_byte *contents)
   4350 
   4351 {
   4352   struct elf_aarch64_link_hash_table *globals =
   4353 					elf_aarch64_hash_table (link_info);
   4354 
   4355   if (globals == NULL)
   4356     return FALSE;
   4357 
   4358   /* Fix code to point to erratum 835769 stubs.  */
   4359   if (globals->fix_erratum_835769)
   4360     {
   4361       struct erratum_835769_branch_to_stub_data data;
   4362 
   4363       data.info = link_info;
   4364       data.output_section = sec;
   4365       data.contents = contents;
   4366       bfd_hash_traverse (&globals->stub_hash_table,
   4367 			 make_branch_to_erratum_835769_stub, &data);
   4368     }
   4369 
   4370   if (globals->fix_erratum_843419)
   4371     {
   4372       struct erratum_835769_branch_to_stub_data data;
   4373 
   4374       data.info = link_info;
   4375       data.output_section = sec;
   4376       data.contents = contents;
   4377       bfd_hash_traverse (&globals->stub_hash_table,
   4378 			 _bfd_aarch64_erratum_843419_branch_to_stub, &data);
   4379     }
   4380 
   4381   return FALSE;
   4382 }
   4383 
   4384 /* Perform a relocation as part of a final link.  */
   4385 static bfd_reloc_status_type
   4386 elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
   4387 				   bfd *input_bfd,
   4388 				   bfd *output_bfd,
   4389 				   asection *input_section,
   4390 				   bfd_byte *contents,
   4391 				   Elf_Internal_Rela *rel,
   4392 				   bfd_vma value,
   4393 				   struct bfd_link_info *info,
   4394 				   asection *sym_sec,
   4395 				   struct elf_link_hash_entry *h,
   4396 				   bfd_boolean *unresolved_reloc_p,
   4397 				   bfd_boolean save_addend,
   4398 				   bfd_vma *saved_addend,
   4399 				   Elf_Internal_Sym *sym)
   4400 {
   4401   Elf_Internal_Shdr *symtab_hdr;
   4402   unsigned int r_type = howto->type;
   4403   bfd_reloc_code_real_type bfd_r_type
   4404     = elfNN_aarch64_bfd_reloc_from_howto (howto);
   4405   bfd_reloc_code_real_type new_bfd_r_type;
   4406   unsigned long r_symndx;
   4407   bfd_byte *hit_data = contents + rel->r_offset;
   4408   bfd_vma place;
   4409   bfd_signed_vma signed_addend;
   4410   struct elf_aarch64_link_hash_table *globals;
   4411   bfd_boolean weak_undef_p;
   4412 
   4413   globals = elf_aarch64_hash_table (info);
   4414 
   4415   symtab_hdr = &elf_symtab_hdr (input_bfd);
   4416 
   4417   BFD_ASSERT (is_aarch64_elf (input_bfd));
   4418 
   4419   r_symndx = ELFNN_R_SYM (rel->r_info);
   4420 
   4421   /* It is possible to have linker relaxations on some TLS access
   4422      models.  Update our information here.  */
   4423   new_bfd_r_type = aarch64_tls_transition (input_bfd, info, r_type, h, r_symndx);
   4424   if (new_bfd_r_type != bfd_r_type)
   4425     {
   4426       bfd_r_type = new_bfd_r_type;
   4427       howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
   4428       BFD_ASSERT (howto != NULL);
   4429       r_type = howto->type;
   4430     }
   4431 
   4432   place = input_section->output_section->vma
   4433     + input_section->output_offset + rel->r_offset;
   4434 
   4435   /* Get addend, accumulating the addend for consecutive relocs
   4436      which refer to the same offset.  */
   4437   signed_addend = saved_addend ? *saved_addend : 0;
   4438   signed_addend += rel->r_addend;
   4439 
   4440   weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
   4441 		  : bfd_is_und_section (sym_sec));
   4442 
   4443   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
   4444      it here if it is defined in a non-shared object.  */
   4445   if (h != NULL
   4446       && h->type == STT_GNU_IFUNC
   4447       && h->def_regular)
   4448     {
   4449       asection *plt;
   4450       const char *name;
   4451       asection *base_got;
   4452       bfd_vma off;
   4453 
   4454       if ((input_section->flags & SEC_ALLOC) == 0
   4455 	  || h->plt.offset == (bfd_vma) -1)
   4456 	abort ();
   4457 
   4458       /* STT_GNU_IFUNC symbol must go through PLT.  */
   4459       plt = globals->root.splt ? globals->root.splt : globals->root.iplt;
   4460       value = (plt->output_section->vma + plt->output_offset + h->plt.offset);
   4461 
   4462       switch (bfd_r_type)
   4463 	{
   4464 	default:
   4465 	  if (h->root.root.string)
   4466 	    name = h->root.root.string;
   4467 	  else
   4468 	    name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
   4469 				     NULL);
   4470 	  (*_bfd_error_handler)
   4471 	    (_("%B: relocation %s against STT_GNU_IFUNC "
   4472 	       "symbol `%s' isn't handled by %s"), input_bfd,
   4473 	     howto->name, name, __FUNCTION__);
   4474 	  bfd_set_error (bfd_error_bad_value);
   4475 	  return FALSE;
   4476 
   4477 	case BFD_RELOC_AARCH64_NN:
   4478 	  if (rel->r_addend != 0)
   4479 	    {
   4480 	      if (h->root.root.string)
   4481 		name = h->root.root.string;
   4482 	      else
   4483 		name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   4484 					 sym, NULL);
   4485 	      (*_bfd_error_handler)
   4486 		(_("%B: relocation %s against STT_GNU_IFUNC "
   4487 		   "symbol `%s' has non-zero addend: %d"),
   4488 		 input_bfd, howto->name, name, rel->r_addend);
   4489 	      bfd_set_error (bfd_error_bad_value);
   4490 	      return FALSE;
   4491 	    }
   4492 
   4493 	  /* Generate dynamic relocation only when there is a
   4494 	     non-GOT reference in a shared object.  */
   4495 	  if (info->shared && h->non_got_ref)
   4496 	    {
   4497 	      Elf_Internal_Rela outrel;
   4498 	      asection *sreloc;
   4499 
   4500 	      /* Need a dynamic relocation to get the real function
   4501 		 address.  */
   4502 	      outrel.r_offset = _bfd_elf_section_offset (output_bfd,
   4503 							 info,
   4504 							 input_section,
   4505 							 rel->r_offset);
   4506 	      if (outrel.r_offset == (bfd_vma) -1
   4507 		  || outrel.r_offset == (bfd_vma) -2)
   4508 		abort ();
   4509 
   4510 	      outrel.r_offset += (input_section->output_section->vma
   4511 				  + input_section->output_offset);
   4512 
   4513 	      if (h->dynindx == -1
   4514 		  || h->forced_local
   4515 		  || info->executable)
   4516 		{
   4517 		  /* This symbol is resolved locally.  */
   4518 		  outrel.r_info = ELFNN_R_INFO (0, AARCH64_R (IRELATIVE));
   4519 		  outrel.r_addend = (h->root.u.def.value
   4520 				     + h->root.u.def.section->output_section->vma
   4521 				     + h->root.u.def.section->output_offset);
   4522 		}
   4523 	      else
   4524 		{
   4525 		  outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
   4526 		  outrel.r_addend = 0;
   4527 		}
   4528 
   4529 	      sreloc = globals->root.irelifunc;
   4530 	      elf_append_rela (output_bfd, sreloc, &outrel);
   4531 
   4532 	      /* If this reloc is against an external symbol, we
   4533 		 do not want to fiddle with the addend.  Otherwise,
   4534 		 we need to include the symbol value so that it
   4535 		 becomes an addend for the dynamic reloc.  For an
   4536 		 internal symbol, we have updated addend.  */
   4537 	      return bfd_reloc_ok;
   4538 	    }
   4539 	  /* FALLTHROUGH */
   4540 	case BFD_RELOC_AARCH64_JUMP26:
   4541 	case BFD_RELOC_AARCH64_CALL26:
   4542 	  value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
   4543 						       signed_addend,
   4544 						       weak_undef_p);
   4545 	  return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
   4546 					      howto, value);
   4547 	case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
   4548 	case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
   4549 	case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
   4550 	case BFD_RELOC_AARCH64_GOT_LD_PREL19:
   4551 	  base_got = globals->root.sgot;
   4552 	  off = h->got.offset;
   4553 
   4554 	  if (base_got == NULL)
   4555 	    abort ();
   4556 
   4557 	  if (off == (bfd_vma) -1)
   4558 	    {
   4559 	      bfd_vma plt_index;
   4560 
   4561 	      /* We can't use h->got.offset here to save state, or
   4562 		 even just remember the offset, as finish_dynamic_symbol
   4563 		 would use that as offset into .got.  */
   4564 
   4565 	      if (globals->root.splt != NULL)
   4566 		{
   4567 		  plt_index = ((h->plt.offset - globals->plt_header_size) /
   4568 			       globals->plt_entry_size);
   4569 		  off = (plt_index + 3) * GOT_ENTRY_SIZE;
   4570 		  base_got = globals->root.sgotplt;
   4571 		}
   4572 	      else
   4573 		{
   4574 		  plt_index = h->plt.offset / globals->plt_entry_size;
   4575 		  off = plt_index * GOT_ENTRY_SIZE;
   4576 		  base_got = globals->root.igotplt;
   4577 		}
   4578 
   4579 	      if (h->dynindx == -1
   4580 		  || h->forced_local
   4581 		  || info->symbolic)
   4582 		{
   4583 		  /* This references the local definition.  We must
   4584 		     initialize this entry in the global offset table.
   4585 		     Since the offset must always be a multiple of 8,
   4586 		     we use the least significant bit to record
   4587 		     whether we have initialized it already.
   4588 
   4589 		     When doing a dynamic link, we create a .rela.got
   4590 		     relocation entry to initialize the value.  This
   4591 		     is done in the finish_dynamic_symbol routine.	 */
   4592 		  if ((off & 1) != 0)
   4593 		    off &= ~1;
   4594 		  else
   4595 		    {
   4596 		      bfd_put_NN (output_bfd, value,
   4597 				  base_got->contents + off);
   4598 		      /* Note that this is harmless as -1 | 1 still is -1.  */
   4599 		      h->got.offset |= 1;
   4600 		    }
   4601 		}
   4602 	      value = (base_got->output_section->vma
   4603 		       + base_got->output_offset + off);
   4604 	    }
   4605 	  else
   4606 	    value = aarch64_calculate_got_entry_vma (h, globals, info,
   4607 						     value, output_bfd,
   4608 						     unresolved_reloc_p);
   4609 	  value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
   4610 						       0, weak_undef_p);
   4611 	  return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type, howto, value);
   4612 	case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
   4613 	case BFD_RELOC_AARCH64_ADD_LO12:
   4614 	  break;
   4615 	}
   4616     }
   4617 
   4618   switch (bfd_r_type)
   4619     {
   4620     case BFD_RELOC_AARCH64_NONE:
   4621     case BFD_RELOC_AARCH64_TLSDESC_CALL:
   4622       *unresolved_reloc_p = FALSE;
   4623       return bfd_reloc_ok;
   4624 
   4625     case BFD_RELOC_AARCH64_NN:
   4626 
   4627       /* When generating a shared object or relocatable executable, these
   4628          relocations are copied into the output file to be resolved at
   4629          run time.  */
   4630       if (((info->shared == TRUE) || globals->root.is_relocatable_executable)
   4631 	  && (input_section->flags & SEC_ALLOC)
   4632 	  && (h == NULL
   4633 	      || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   4634 	      || h->root.type != bfd_link_hash_undefweak))
   4635 	{
   4636 	  Elf_Internal_Rela outrel;
   4637 	  bfd_byte *loc;
   4638 	  bfd_boolean skip, relocate;
   4639 	  asection *sreloc;
   4640 
   4641 	  *unresolved_reloc_p = FALSE;
   4642 
   4643 	  skip = FALSE;
   4644 	  relocate = FALSE;
   4645 
   4646 	  outrel.r_addend = signed_addend;
   4647 	  outrel.r_offset =
   4648 	    _bfd_elf_section_offset (output_bfd, info, input_section,
   4649 				     rel->r_offset);
   4650 	  if (outrel.r_offset == (bfd_vma) - 1)
   4651 	    skip = TRUE;
   4652 	  else if (outrel.r_offset == (bfd_vma) - 2)
   4653 	    {
   4654 	      skip = TRUE;
   4655 	      relocate = TRUE;
   4656 	    }
   4657 
   4658 	  outrel.r_offset += (input_section->output_section->vma
   4659 			      + input_section->output_offset);
   4660 
   4661 	  if (skip)
   4662 	    memset (&outrel, 0, sizeof outrel);
   4663 	  else if (h != NULL
   4664 		   && h->dynindx != -1
   4665 		   && (!info->shared || !info->symbolic || !h->def_regular))
   4666 	    outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
   4667 	  else
   4668 	    {
   4669 	      int symbol;
   4670 
   4671 	      /* On SVR4-ish systems, the dynamic loader cannot
   4672 		 relocate the text and data segments independently,
   4673 		 so the symbol does not matter.  */
   4674 	      symbol = 0;
   4675 	      outrel.r_info = ELFNN_R_INFO (symbol, AARCH64_R (RELATIVE));
   4676 	      outrel.r_addend += value;
   4677 	    }
   4678 
   4679 	  sreloc = elf_section_data (input_section)->sreloc;
   4680 	  if (sreloc == NULL || sreloc->contents == NULL)
   4681 	    return bfd_reloc_notsupported;
   4682 
   4683 	  loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (globals);
   4684 	  bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
   4685 
   4686 	  if (sreloc->reloc_count * RELOC_SIZE (globals) > sreloc->size)
   4687 	    {
   4688 	      /* Sanity to check that we have previously allocated
   4689 		 sufficient space in the relocation section for the
   4690 		 number of relocations we actually want to emit.  */
   4691 	      abort ();
   4692 	    }
   4693 
   4694 	  /* If this reloc is against an external symbol, we do not want to
   4695 	     fiddle with the addend.  Otherwise, we need to include the symbol
   4696 	     value so that it becomes an addend for the dynamic reloc.  */
   4697 	  if (!relocate)
   4698 	    return bfd_reloc_ok;
   4699 
   4700 	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
   4701 					   contents, rel->r_offset, value,
   4702 					   signed_addend);
   4703 	}
   4704       else
   4705 	value += signed_addend;
   4706       break;
   4707 
   4708     case BFD_RELOC_AARCH64_JUMP26:
   4709     case BFD_RELOC_AARCH64_CALL26:
   4710       {
   4711 	asection *splt = globals->root.splt;
   4712 	bfd_boolean via_plt_p =
   4713 	  splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
   4714 
   4715 	/* A call to an undefined weak symbol is converted to a jump to
   4716 	   the next instruction unless a PLT entry will be created.
   4717 	   The jump to the next instruction is optimized as a NOP.
   4718 	   Do the same for local undefined symbols.  */
   4719 	if (weak_undef_p && ! via_plt_p)
   4720 	  {
   4721 	    bfd_putl32 (INSN_NOP, hit_data);
   4722 	    return bfd_reloc_ok;
   4723 	  }
   4724 
   4725 	/* If the call goes through a PLT entry, make sure to
   4726 	   check distance to the right destination address.  */
   4727 	if (via_plt_p)
   4728 	  {
   4729 	    value = (splt->output_section->vma
   4730 		     + splt->output_offset + h->plt.offset);
   4731 	    *unresolved_reloc_p = FALSE;
   4732 	  }
   4733 
   4734 	/* If the target symbol is global and marked as a function the
   4735 	   relocation applies a function call or a tail call.  In this
   4736 	   situation we can veneer out of range branches.  The veneers
   4737 	   use IP0 and IP1 hence cannot be used arbitrary out of range
   4738 	   branches that occur within the body of a function.  */
   4739 	if (h && h->type == STT_FUNC)
   4740 	  {
   4741 	    /* Check if a stub has to be inserted because the destination
   4742 	       is too far away.  */
   4743 	    if (! aarch64_valid_branch_p (value, place))
   4744 	      {
   4745 		/* The target is out of reach, so redirect the branch to
   4746 		   the local stub for this function.  */
   4747 		struct elf_aarch64_stub_hash_entry *stub_entry;
   4748 		stub_entry = elfNN_aarch64_get_stub_entry (input_section,
   4749 							   sym_sec, h,
   4750 							   rel, globals);
   4751 		if (stub_entry != NULL)
   4752 		  value = (stub_entry->stub_offset
   4753 			   + stub_entry->stub_sec->output_offset
   4754 			   + stub_entry->stub_sec->output_section->vma);
   4755 	      }
   4756 	  }
   4757       }
   4758       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
   4759 						   signed_addend, weak_undef_p);
   4760       break;
   4761 
   4762     case BFD_RELOC_AARCH64_16:
   4763 #if ARCH_SIZE == 64
   4764     case BFD_RELOC_AARCH64_32:
   4765 #endif
   4766     case BFD_RELOC_AARCH64_ADD_LO12:
   4767     case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
   4768     case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
   4769     case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
   4770     case BFD_RELOC_AARCH64_BRANCH19:
   4771     case BFD_RELOC_AARCH64_LD_LO19_PCREL:
   4772     case BFD_RELOC_AARCH64_LDST8_LO12:
   4773     case BFD_RELOC_AARCH64_LDST16_LO12:
   4774     case BFD_RELOC_AARCH64_LDST32_LO12:
   4775     case BFD_RELOC_AARCH64_LDST64_LO12:
   4776     case BFD_RELOC_AARCH64_LDST128_LO12:
   4777     case BFD_RELOC_AARCH64_MOVW_G0_S:
   4778     case BFD_RELOC_AARCH64_MOVW_G1_S:
   4779     case BFD_RELOC_AARCH64_MOVW_G2_S:
   4780     case BFD_RELOC_AARCH64_MOVW_G0:
   4781     case BFD_RELOC_AARCH64_MOVW_G0_NC:
   4782     case BFD_RELOC_AARCH64_MOVW_G1:
   4783     case BFD_RELOC_AARCH64_MOVW_G1_NC:
   4784     case BFD_RELOC_AARCH64_MOVW_G2:
   4785     case BFD_RELOC_AARCH64_MOVW_G2_NC:
   4786     case BFD_RELOC_AARCH64_MOVW_G3:
   4787     case BFD_RELOC_AARCH64_16_PCREL:
   4788     case BFD_RELOC_AARCH64_32_PCREL:
   4789     case BFD_RELOC_AARCH64_64_PCREL:
   4790     case BFD_RELOC_AARCH64_TSTBR14:
   4791       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
   4792 						   signed_addend, weak_undef_p);
   4793       break;
   4794 
   4795     case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
   4796     case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
   4797     case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
   4798     case BFD_RELOC_AARCH64_GOT_LD_PREL19:
   4799       if (globals->root.sgot == NULL)
   4800 	BFD_ASSERT (h != NULL);
   4801 
   4802       if (h != NULL)
   4803 	{
   4804 	  value = aarch64_calculate_got_entry_vma (h, globals, info, value,
   4805 						   output_bfd,
   4806 						   unresolved_reloc_p);
   4807 	  value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
   4808 						       0, weak_undef_p);
   4809 	}
   4810       break;
   4811 
   4812     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
   4813     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
   4814     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
   4815     case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
   4816     case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
   4817       if (globals->root.sgot == NULL)
   4818 	return bfd_reloc_notsupported;
   4819 
   4820       value = (symbol_got_offset (input_bfd, h, r_symndx)
   4821 	       + globals->root.sgot->output_section->vma
   4822 	       + globals->root.sgot->output_offset);
   4823 
   4824       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
   4825 						   0, weak_undef_p);
   4826       *unresolved_reloc_p = FALSE;
   4827       break;
   4828 
   4829     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
   4830     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
   4831     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
   4832     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
   4833     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
   4834     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
   4835     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
   4836     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
   4837       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
   4838 						   signed_addend - tpoff_base (info),
   4839 						   weak_undef_p);
   4840       *unresolved_reloc_p = FALSE;
   4841       break;
   4842 
   4843     case BFD_RELOC_AARCH64_TLSDESC_ADD:
   4844     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
   4845     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
   4846     case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
   4847     case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
   4848     case BFD_RELOC_AARCH64_TLSDESC_LDR:
   4849       if (globals->root.sgot == NULL)
   4850 	return bfd_reloc_notsupported;
   4851       value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
   4852 	       + globals->root.sgotplt->output_section->vma
   4853 	       + globals->root.sgotplt->output_offset
   4854 	       + globals->sgotplt_jump_table_size);
   4855 
   4856       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
   4857 						   0, weak_undef_p);
   4858       *unresolved_reloc_p = FALSE;
   4859       break;
   4860 
   4861     default:
   4862       return bfd_reloc_notsupported;
   4863     }
   4864 
   4865   if (saved_addend)
   4866     *saved_addend = value;
   4867 
   4868   /* Only apply the final relocation in a sequence.  */
   4869   if (save_addend)
   4870     return bfd_reloc_continue;
   4871 
   4872   return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
   4873 				      howto, value);
   4874 }
   4875 
   4876 /* Handle TLS relaxations.  Relaxing is possible for symbols that use
   4877    R_AARCH64_TLSDESC_ADR_{PAGE, LD64_LO12_NC, ADD_LO12_NC} during a static
   4878    link.
   4879 
   4880    Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
   4881    is to then call final_link_relocate.  Return other values in the
   4882    case of error.  */
   4883 
   4884 static bfd_reloc_status_type
   4885 elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
   4886 			 bfd *input_bfd, bfd_byte *contents,
   4887 			 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h)
   4888 {
   4889   bfd_boolean is_local = h == NULL;
   4890   unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
   4891   unsigned long insn;
   4892 
   4893   BFD_ASSERT (globals && input_bfd && contents && rel);
   4894 
   4895   switch (elfNN_aarch64_bfd_reloc_from_type (r_type))
   4896     {
   4897     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
   4898     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
   4899       if (is_local)
   4900 	{
   4901 	  /* GD->LE relaxation:
   4902 	     adrp x0, :tlsgd:var     =>   movz x0, :tprel_g1:var
   4903 	     or
   4904 	     adrp x0, :tlsdesc:var   =>   movz x0, :tprel_g1:var
   4905 	   */
   4906 	  bfd_putl32 (0xd2a00000, contents + rel->r_offset);
   4907 	  return bfd_reloc_continue;
   4908 	}
   4909       else
   4910 	{
   4911 	  /* GD->IE relaxation:
   4912 	     adrp x0, :tlsgd:var     =>   adrp x0, :gottprel:var
   4913 	     or
   4914 	     adrp x0, :tlsdesc:var   =>   adrp x0, :gottprel:var
   4915 	   */
   4916 	  return bfd_reloc_continue;
   4917 	}
   4918 
   4919     case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
   4920       if (is_local)
   4921 	{
   4922 	  /* GD->LE relaxation:
   4923 	     ldr xd, [x0, #:tlsdesc_lo12:var]   =>   movk x0, :tprel_g0_nc:var
   4924 	   */
   4925 	  bfd_putl32 (0xf2800000, contents + rel->r_offset);
   4926 	  return bfd_reloc_continue;
   4927 	}
   4928       else
   4929 	{
   4930 	  /* GD->IE relaxation:
   4931 	     ldr xd, [x0, #:tlsdesc_lo12:var] => ldr x0, [x0, #:gottprel_lo12:var]
   4932 	   */
   4933 	  insn = bfd_getl32 (contents + rel->r_offset);
   4934 	  insn &= 0xffffffe0;
   4935 	  bfd_putl32 (insn, contents + rel->r_offset);
   4936 	  return bfd_reloc_continue;
   4937 	}
   4938 
   4939     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
   4940       if (is_local)
   4941 	{
   4942 	  /* GD->LE relaxation
   4943 	     add  x0, #:tlsgd_lo12:var  => movk x0, :tprel_g0_nc:var
   4944 	     bl   __tls_get_addr        => mrs  x1, tpidr_el0
   4945 	     nop                        => add  x0, x1, x0
   4946 	   */
   4947 
   4948 	  /* First kill the tls_get_addr reloc on the bl instruction.  */
   4949 	  BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
   4950 	  rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
   4951 
   4952 	  bfd_putl32 (0xf2800000, contents + rel->r_offset);
   4953 	  bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
   4954 	  bfd_putl32 (0x8b000020, contents + rel->r_offset + 8);
   4955 	  return bfd_reloc_continue;
   4956 	}
   4957       else
   4958 	{
   4959 	  /* GD->IE relaxation
   4960 	     ADD  x0, #:tlsgd_lo12:var  => ldr  x0, [x0, #:gottprel_lo12:var]
   4961 	     BL   __tls_get_addr        => mrs  x1, tpidr_el0
   4962 	       R_AARCH64_CALL26
   4963 	     NOP                        => add  x0, x1, x0
   4964 	   */
   4965 
   4966 	  BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
   4967 
   4968 	  /* Remove the relocation on the BL instruction.  */
   4969 	  rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
   4970 
   4971 	  bfd_putl32 (0xf9400000, contents + rel->r_offset);
   4972 
   4973 	  /* We choose to fixup the BL and NOP instructions using the
   4974 	     offset from the second relocation to allow flexibility in
   4975 	     scheduling instructions between the ADD and BL.  */
   4976 	  bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
   4977 	  bfd_putl32 (0x8b000020, contents + rel[1].r_offset + 4);
   4978 	  return bfd_reloc_continue;
   4979 	}
   4980 
   4981     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
   4982     case BFD_RELOC_AARCH64_TLSDESC_CALL:
   4983       /* GD->IE/LE relaxation:
   4984          add x0, x0, #:tlsdesc_lo12:var   =>   nop
   4985          blr xd                           =>   nop
   4986        */
   4987       bfd_putl32 (INSN_NOP, contents + rel->r_offset);
   4988       return bfd_reloc_ok;
   4989 
   4990     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
   4991       /* IE->LE relaxation:
   4992          adrp xd, :gottprel:var   =>   movz xd, :tprel_g1:var
   4993        */
   4994       if (is_local)
   4995 	{
   4996 	  insn = bfd_getl32 (contents + rel->r_offset);
   4997 	  bfd_putl32 (0xd2a00000 | (insn & 0x1f), contents + rel->r_offset);
   4998 	}
   4999       return bfd_reloc_continue;
   5000 
   5001     case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
   5002       /* IE->LE relaxation:
   5003          ldr  xd, [xm, #:gottprel_lo12:var]   =>   movk xd, :tprel_g0_nc:var
   5004        */
   5005       if (is_local)
   5006 	{
   5007 	  insn = bfd_getl32 (contents + rel->r_offset);
   5008 	  bfd_putl32 (0xf2800000 | (insn & 0x1f), contents + rel->r_offset);
   5009 	}
   5010       return bfd_reloc_continue;
   5011 
   5012     default:
   5013       return bfd_reloc_continue;
   5014     }
   5015 
   5016   return bfd_reloc_ok;
   5017 }
   5018 
   5019 /* Relocate an AArch64 ELF section.  */
   5020 
   5021 static bfd_boolean
   5022 elfNN_aarch64_relocate_section (bfd *output_bfd,
   5023 				struct bfd_link_info *info,
   5024 				bfd *input_bfd,
   5025 				asection *input_section,
   5026 				bfd_byte *contents,
   5027 				Elf_Internal_Rela *relocs,
   5028 				Elf_Internal_Sym *local_syms,
   5029 				asection **local_sections)
   5030 {
   5031   Elf_Internal_Shdr *symtab_hdr;
   5032   struct elf_link_hash_entry **sym_hashes;
   5033   Elf_Internal_Rela *rel;
   5034   Elf_Internal_Rela *relend;
   5035   const char *name;
   5036   struct elf_aarch64_link_hash_table *globals;
   5037   bfd_boolean save_addend = FALSE;
   5038   bfd_vma addend = 0;
   5039 
   5040   globals = elf_aarch64_hash_table (info);
   5041 
   5042   symtab_hdr = &elf_symtab_hdr (input_bfd);
   5043   sym_hashes = elf_sym_hashes (input_bfd);
   5044 
   5045   rel = relocs;
   5046   relend = relocs + input_section->reloc_count;
   5047   for (; rel < relend; rel++)
   5048     {
   5049       unsigned int r_type;
   5050       bfd_reloc_code_real_type bfd_r_type;
   5051       bfd_reloc_code_real_type relaxed_bfd_r_type;
   5052       reloc_howto_type *howto;
   5053       unsigned long r_symndx;
   5054       Elf_Internal_Sym *sym;
   5055       asection *sec;
   5056       struct elf_link_hash_entry *h;
   5057       bfd_vma relocation;
   5058       bfd_reloc_status_type r;
   5059       arelent bfd_reloc;
   5060       char sym_type;
   5061       bfd_boolean unresolved_reloc = FALSE;
   5062       char *error_message = NULL;
   5063 
   5064       r_symndx = ELFNN_R_SYM (rel->r_info);
   5065       r_type = ELFNN_R_TYPE (rel->r_info);
   5066 
   5067       bfd_reloc.howto = elfNN_aarch64_howto_from_type (r_type);
   5068       howto = bfd_reloc.howto;
   5069 
   5070       if (howto == NULL)
   5071 	{
   5072 	  (*_bfd_error_handler)
   5073 	    (_("%B: unrecognized relocation (0x%x) in section `%A'"),
   5074 	     input_bfd, input_section, r_type);
   5075 	  return FALSE;
   5076 	}
   5077       bfd_r_type = elfNN_aarch64_bfd_reloc_from_howto (howto);
   5078 
   5079       h = NULL;
   5080       sym = NULL;
   5081       sec = NULL;
   5082 
   5083       if (r_symndx < symtab_hdr->sh_info)
   5084 	{
   5085 	  sym = local_syms + r_symndx;
   5086 	  sym_type = ELFNN_ST_TYPE (sym->st_info);
   5087 	  sec = local_sections[r_symndx];
   5088 
   5089 	  /* An object file might have a reference to a local
   5090 	     undefined symbol.  This is a daft object file, but we
   5091 	     should at least do something about it.  */
   5092 	  if (r_type != R_AARCH64_NONE && r_type != R_AARCH64_NULL
   5093 	      && bfd_is_und_section (sec)
   5094 	      && ELF_ST_BIND (sym->st_info) != STB_WEAK)
   5095 	    {
   5096 	      if (!info->callbacks->undefined_symbol
   5097 		  (info, bfd_elf_string_from_elf_section
   5098 		   (input_bfd, symtab_hdr->sh_link, sym->st_name),
   5099 		   input_bfd, input_section, rel->r_offset, TRUE))
   5100 		return FALSE;
   5101 	    }
   5102 
   5103 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   5104 
   5105 	  /* Relocate against local STT_GNU_IFUNC symbol.  */
   5106 	  if (!info->relocatable
   5107 	      && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   5108 	    {
   5109 	      h = elfNN_aarch64_get_local_sym_hash (globals, input_bfd,
   5110 						    rel, FALSE);
   5111 	      if (h == NULL)
   5112 		abort ();
   5113 
   5114 	      /* Set STT_GNU_IFUNC symbol value.  */
   5115 	      h->root.u.def.value = sym->st_value;
   5116 	      h->root.u.def.section = sec;
   5117 	    }
   5118 	}
   5119       else
   5120 	{
   5121 	  bfd_boolean warned, ignored;
   5122 
   5123 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   5124 				   r_symndx, symtab_hdr, sym_hashes,
   5125 				   h, sec, relocation,
   5126 				   unresolved_reloc, warned, ignored);
   5127 
   5128 	  sym_type = h->type;
   5129 	}
   5130 
   5131       if (sec != NULL && discarded_section (sec))
   5132 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   5133 					 rel, 1, relend, howto, 0, contents);
   5134 
   5135       if (info->relocatable)
   5136 	continue;
   5137 
   5138       if (h != NULL)
   5139 	name = h->root.root.string;
   5140       else
   5141 	{
   5142 	  name = (bfd_elf_string_from_elf_section
   5143 		  (input_bfd, symtab_hdr->sh_link, sym->st_name));
   5144 	  if (name == NULL || *name == '\0')
   5145 	    name = bfd_section_name (input_bfd, sec);
   5146 	}
   5147 
   5148       if (r_symndx != 0
   5149 	  && r_type != R_AARCH64_NONE
   5150 	  && r_type != R_AARCH64_NULL
   5151 	  && (h == NULL
   5152 	      || h->root.type == bfd_link_hash_defined
   5153 	      || h->root.type == bfd_link_hash_defweak)
   5154 	  && IS_AARCH64_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS))
   5155 	{
   5156 	  (*_bfd_error_handler)
   5157 	    ((sym_type == STT_TLS
   5158 	      ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
   5159 	      : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
   5160 	     input_bfd,
   5161 	     input_section, (long) rel->r_offset, howto->name, name);
   5162 	}
   5163 
   5164       /* We relax only if we can see that there can be a valid transition
   5165          from a reloc type to another.
   5166          We call elfNN_aarch64_final_link_relocate unless we're completely
   5167          done, i.e., the relaxation produced the final output we want.  */
   5168 
   5169       relaxed_bfd_r_type = aarch64_tls_transition (input_bfd, info, r_type,
   5170 						   h, r_symndx);
   5171       if (relaxed_bfd_r_type != bfd_r_type)
   5172 	{
   5173 	  bfd_r_type = relaxed_bfd_r_type;
   5174 	  howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
   5175 	  BFD_ASSERT (howto != NULL);
   5176 	  r_type = howto->type;
   5177 	  r = elfNN_aarch64_tls_relax (globals, input_bfd, contents, rel, h);
   5178 	  unresolved_reloc = 0;
   5179 	}
   5180       else
   5181 	r = bfd_reloc_continue;
   5182 
   5183       /* There may be multiple consecutive relocations for the
   5184          same offset.  In that case we are supposed to treat the
   5185          output of each relocation as the addend for the next.  */
   5186       if (rel + 1 < relend
   5187 	  && rel->r_offset == rel[1].r_offset
   5188 	  && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NONE
   5189 	  && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NULL)
   5190 	save_addend = TRUE;
   5191       else
   5192 	save_addend = FALSE;
   5193 
   5194       if (r == bfd_reloc_continue)
   5195 	r = elfNN_aarch64_final_link_relocate (howto, input_bfd, output_bfd,
   5196 					       input_section, contents, rel,
   5197 					       relocation, info, sec,
   5198 					       h, &unresolved_reloc,
   5199 					       save_addend, &addend, sym);
   5200 
   5201       switch (elfNN_aarch64_bfd_reloc_from_type (r_type))
   5202 	{
   5203 	case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
   5204 	case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
   5205 	  if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
   5206 	    {
   5207 	      bfd_boolean need_relocs = FALSE;
   5208 	      bfd_byte *loc;
   5209 	      int indx;
   5210 	      bfd_vma off;
   5211 
   5212 	      off = symbol_got_offset (input_bfd, h, r_symndx);
   5213 	      indx = h && h->dynindx != -1 ? h->dynindx : 0;
   5214 
   5215 	      need_relocs =
   5216 		(info->shared || indx != 0) &&
   5217 		(h == NULL
   5218 		 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   5219 		 || h->root.type != bfd_link_hash_undefweak);
   5220 
   5221 	      BFD_ASSERT (globals->root.srelgot != NULL);
   5222 
   5223 	      if (need_relocs)
   5224 		{
   5225 		  Elf_Internal_Rela rela;
   5226 		  rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPMOD));
   5227 		  rela.r_addend = 0;
   5228 		  rela.r_offset = globals->root.sgot->output_section->vma +
   5229 		    globals->root.sgot->output_offset + off;
   5230 
   5231 
   5232 		  loc = globals->root.srelgot->contents;
   5233 		  loc += globals->root.srelgot->reloc_count++
   5234 		    * RELOC_SIZE (htab);
   5235 		  bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   5236 
   5237 		  if (indx == 0)
   5238 		    {
   5239 		      bfd_put_NN (output_bfd,
   5240 				  relocation - dtpoff_base (info),
   5241 				  globals->root.sgot->contents + off
   5242 				  + GOT_ENTRY_SIZE);
   5243 		    }
   5244 		  else
   5245 		    {
   5246 		      /* This TLS symbol is global. We emit a
   5247 			 relocation to fixup the tls offset at load
   5248 			 time.  */
   5249 		      rela.r_info =
   5250 			ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPREL));
   5251 		      rela.r_addend = 0;
   5252 		      rela.r_offset =
   5253 			(globals->root.sgot->output_section->vma
   5254 			 + globals->root.sgot->output_offset + off
   5255 			 + GOT_ENTRY_SIZE);
   5256 
   5257 		      loc = globals->root.srelgot->contents;
   5258 		      loc += globals->root.srelgot->reloc_count++
   5259 			* RELOC_SIZE (globals);
   5260 		      bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   5261 		      bfd_put_NN (output_bfd, (bfd_vma) 0,
   5262 				  globals->root.sgot->contents + off
   5263 				  + GOT_ENTRY_SIZE);
   5264 		    }
   5265 		}
   5266 	      else
   5267 		{
   5268 		  bfd_put_NN (output_bfd, (bfd_vma) 1,
   5269 			      globals->root.sgot->contents + off);
   5270 		  bfd_put_NN (output_bfd,
   5271 			      relocation - dtpoff_base (info),
   5272 			      globals->root.sgot->contents + off
   5273 			      + GOT_ENTRY_SIZE);
   5274 		}
   5275 
   5276 	      symbol_got_offset_mark (input_bfd, h, r_symndx);
   5277 	    }
   5278 	  break;
   5279 
   5280 	case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
   5281 	case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
   5282 	  if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
   5283 	    {
   5284 	      bfd_boolean need_relocs = FALSE;
   5285 	      bfd_byte *loc;
   5286 	      int indx;
   5287 	      bfd_vma off;
   5288 
   5289 	      off = symbol_got_offset (input_bfd, h, r_symndx);
   5290 
   5291 	      indx = h && h->dynindx != -1 ? h->dynindx : 0;
   5292 
   5293 	      need_relocs =
   5294 		(info->shared || indx != 0) &&
   5295 		(h == NULL
   5296 		 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   5297 		 || h->root.type != bfd_link_hash_undefweak);
   5298 
   5299 	      BFD_ASSERT (globals->root.srelgot != NULL);
   5300 
   5301 	      if (need_relocs)
   5302 		{
   5303 		  Elf_Internal_Rela rela;
   5304 
   5305 		  if (indx == 0)
   5306 		    rela.r_addend = relocation - dtpoff_base (info);
   5307 		  else
   5308 		    rela.r_addend = 0;
   5309 
   5310 		  rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_TPREL));
   5311 		  rela.r_offset = globals->root.sgot->output_section->vma +
   5312 		    globals->root.sgot->output_offset + off;
   5313 
   5314 		  loc = globals->root.srelgot->contents;
   5315 		  loc += globals->root.srelgot->reloc_count++
   5316 		    * RELOC_SIZE (htab);
   5317 
   5318 		  bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   5319 
   5320 		  bfd_put_NN (output_bfd, rela.r_addend,
   5321 			      globals->root.sgot->contents + off);
   5322 		}
   5323 	      else
   5324 		bfd_put_NN (output_bfd, relocation - tpoff_base (info),
   5325 			    globals->root.sgot->contents + off);
   5326 
   5327 	      symbol_got_offset_mark (input_bfd, h, r_symndx);
   5328 	    }
   5329 	  break;
   5330 
   5331 	case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
   5332 	case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
   5333 	case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
   5334 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
   5335 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
   5336 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
   5337 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
   5338 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
   5339 	  break;
   5340 
   5341 	case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
   5342 	case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
   5343 	case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
   5344 	  if (! symbol_tlsdesc_got_offset_mark_p (input_bfd, h, r_symndx))
   5345 	    {
   5346 	      bfd_boolean need_relocs = FALSE;
   5347 	      int indx = h && h->dynindx != -1 ? h->dynindx : 0;
   5348 	      bfd_vma off = symbol_tlsdesc_got_offset (input_bfd, h, r_symndx);
   5349 
   5350 	      need_relocs = (h == NULL
   5351 			     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   5352 			     || h->root.type != bfd_link_hash_undefweak);
   5353 
   5354 	      BFD_ASSERT (globals->root.srelgot != NULL);
   5355 	      BFD_ASSERT (globals->root.sgot != NULL);
   5356 
   5357 	      if (need_relocs)
   5358 		{
   5359 		  bfd_byte *loc;
   5360 		  Elf_Internal_Rela rela;
   5361 		  rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLSDESC));
   5362 
   5363 		  rela.r_addend = 0;
   5364 		  rela.r_offset = (globals->root.sgotplt->output_section->vma
   5365 				   + globals->root.sgotplt->output_offset
   5366 				   + off + globals->sgotplt_jump_table_size);
   5367 
   5368 		  if (indx == 0)
   5369 		    rela.r_addend = relocation - dtpoff_base (info);
   5370 
   5371 		  /* Allocate the next available slot in the PLT reloc
   5372 		     section to hold our R_AARCH64_TLSDESC, the next
   5373 		     available slot is determined from reloc_count,
   5374 		     which we step. But note, reloc_count was
   5375 		     artifically moved down while allocating slots for
   5376 		     real PLT relocs such that all of the PLT relocs
   5377 		     will fit above the initial reloc_count and the
   5378 		     extra stuff will fit below.  */
   5379 		  loc = globals->root.srelplt->contents;
   5380 		  loc += globals->root.srelplt->reloc_count++
   5381 		    * RELOC_SIZE (globals);
   5382 
   5383 		  bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   5384 
   5385 		  bfd_put_NN (output_bfd, (bfd_vma) 0,
   5386 			      globals->root.sgotplt->contents + off +
   5387 			      globals->sgotplt_jump_table_size);
   5388 		  bfd_put_NN (output_bfd, (bfd_vma) 0,
   5389 			      globals->root.sgotplt->contents + off +
   5390 			      globals->sgotplt_jump_table_size +
   5391 			      GOT_ENTRY_SIZE);
   5392 		}
   5393 
   5394 	      symbol_tlsdesc_got_offset_mark (input_bfd, h, r_symndx);
   5395 	    }
   5396 	  break;
   5397 	default:
   5398 	  break;
   5399 	}
   5400 
   5401       if (!save_addend)
   5402 	addend = 0;
   5403 
   5404 
   5405       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   5406          because such sections are not SEC_ALLOC and thus ld.so will
   5407          not process them.  */
   5408       if (unresolved_reloc
   5409 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   5410 	       && h->def_dynamic)
   5411 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   5412 				      +rel->r_offset) != (bfd_vma) - 1)
   5413 	{
   5414 	  (*_bfd_error_handler)
   5415 	    (_
   5416 	     ("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
   5417 	     input_bfd, input_section, (long) rel->r_offset, howto->name,
   5418 	     h->root.root.string);
   5419 	  return FALSE;
   5420 	}
   5421 
   5422       if (r != bfd_reloc_ok && r != bfd_reloc_continue)
   5423 	{
   5424 	  switch (r)
   5425 	    {
   5426 	    case bfd_reloc_overflow:
   5427 	      /* If the overflowing reloc was to an undefined symbol,
   5428 		 we have already printed one error message and there
   5429 		 is no point complaining again.  */
   5430 	      if ((!h ||
   5431 		   h->root.type != bfd_link_hash_undefined)
   5432 		  && (!((*info->callbacks->reloc_overflow)
   5433 			(info, (h ? &h->root : NULL), name, howto->name,
   5434 			 (bfd_vma) 0, input_bfd, input_section,
   5435 			 rel->r_offset))))
   5436 		return FALSE;
   5437 	      break;
   5438 
   5439 	    case bfd_reloc_undefined:
   5440 	      if (!((*info->callbacks->undefined_symbol)
   5441 		    (info, name, input_bfd, input_section,
   5442 		     rel->r_offset, TRUE)))
   5443 		return FALSE;
   5444 	      break;
   5445 
   5446 	    case bfd_reloc_outofrange:
   5447 	      error_message = _("out of range");
   5448 	      goto common_error;
   5449 
   5450 	    case bfd_reloc_notsupported:
   5451 	      error_message = _("unsupported relocation");
   5452 	      goto common_error;
   5453 
   5454 	    case bfd_reloc_dangerous:
   5455 	      /* error_message should already be set.  */
   5456 	      goto common_error;
   5457 
   5458 	    default:
   5459 	      error_message = _("unknown error");
   5460 	      /* Fall through.  */
   5461 
   5462 	    common_error:
   5463 	      BFD_ASSERT (error_message != NULL);
   5464 	      if (!((*info->callbacks->reloc_dangerous)
   5465 		    (info, error_message, input_bfd, input_section,
   5466 		     rel->r_offset)))
   5467 		return FALSE;
   5468 	      break;
   5469 	    }
   5470 	}
   5471     }
   5472 
   5473   return TRUE;
   5474 }
   5475 
   5476 /* Set the right machine number.  */
   5477 
   5478 static bfd_boolean
   5479 elfNN_aarch64_object_p (bfd *abfd)
   5480 {
   5481 #if ARCH_SIZE == 32
   5482   bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64_ilp32);
   5483 #else
   5484   bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64);
   5485 #endif
   5486   return TRUE;
   5487 }
   5488 
   5489 /* Function to keep AArch64 specific flags in the ELF header.  */
   5490 
   5491 static bfd_boolean
   5492 elfNN_aarch64_set_private_flags (bfd *abfd, flagword flags)
   5493 {
   5494   if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
   5495     {
   5496     }
   5497   else
   5498     {
   5499       elf_elfheader (abfd)->e_flags = flags;
   5500       elf_flags_init (abfd) = TRUE;
   5501     }
   5502 
   5503   return TRUE;
   5504 }
   5505 
   5506 /* Merge backend specific data from an object file to the output
   5507    object file when linking.  */
   5508 
   5509 static bfd_boolean
   5510 elfNN_aarch64_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   5511 {
   5512   flagword out_flags;
   5513   flagword in_flags;
   5514   bfd_boolean flags_compatible = TRUE;
   5515   asection *sec;
   5516 
   5517   /* Check if we have the same endianess.  */
   5518   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
   5519     return FALSE;
   5520 
   5521   if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
   5522     return TRUE;
   5523 
   5524   /* The input BFD must have had its flags initialised.  */
   5525   /* The following seems bogus to me -- The flags are initialized in
   5526      the assembler but I don't think an elf_flags_init field is
   5527      written into the object.  */
   5528   /* BFD_ASSERT (elf_flags_init (ibfd)); */
   5529 
   5530   in_flags = elf_elfheader (ibfd)->e_flags;
   5531   out_flags = elf_elfheader (obfd)->e_flags;
   5532 
   5533   if (!elf_flags_init (obfd))
   5534     {
   5535       /* If the input is the default architecture and had the default
   5536          flags then do not bother setting the flags for the output
   5537          architecture, instead allow future merges to do this.  If no
   5538          future merges ever set these flags then they will retain their
   5539          uninitialised values, which surprise surprise, correspond
   5540          to the default values.  */
   5541       if (bfd_get_arch_info (ibfd)->the_default
   5542 	  && elf_elfheader (ibfd)->e_flags == 0)
   5543 	return TRUE;
   5544 
   5545       elf_flags_init (obfd) = TRUE;
   5546       elf_elfheader (obfd)->e_flags = in_flags;
   5547 
   5548       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   5549 	  && bfd_get_arch_info (obfd)->the_default)
   5550 	return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   5551 				  bfd_get_mach (ibfd));
   5552 
   5553       return TRUE;
   5554     }
   5555 
   5556   /* Identical flags must be compatible.  */
   5557   if (in_flags == out_flags)
   5558     return TRUE;
   5559 
   5560   /* Check to see if the input BFD actually contains any sections.  If
   5561      not, its flags may not have been initialised either, but it
   5562      cannot actually cause any incompatiblity.  Do not short-circuit
   5563      dynamic objects; their section list may be emptied by
   5564      elf_link_add_object_symbols.
   5565 
   5566      Also check to see if there are no code sections in the input.
   5567      In this case there is no need to check for code specific flags.
   5568      XXX - do we need to worry about floating-point format compatability
   5569      in data sections ?  */
   5570   if (!(ibfd->flags & DYNAMIC))
   5571     {
   5572       bfd_boolean null_input_bfd = TRUE;
   5573       bfd_boolean only_data_sections = TRUE;
   5574 
   5575       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   5576 	{
   5577 	  if ((bfd_get_section_flags (ibfd, sec)
   5578 	       & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
   5579 	      == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
   5580 	    only_data_sections = FALSE;
   5581 
   5582 	  null_input_bfd = FALSE;
   5583 	  break;
   5584 	}
   5585 
   5586       if (null_input_bfd || only_data_sections)
   5587 	return TRUE;
   5588     }
   5589 
   5590   return flags_compatible;
   5591 }
   5592 
   5593 /* Display the flags field.  */
   5594 
   5595 static bfd_boolean
   5596 elfNN_aarch64_print_private_bfd_data (bfd *abfd, void *ptr)
   5597 {
   5598   FILE *file = (FILE *) ptr;
   5599   unsigned long flags;
   5600 
   5601   BFD_ASSERT (abfd != NULL && ptr != NULL);
   5602 
   5603   /* Print normal ELF private data.  */
   5604   _bfd_elf_print_private_bfd_data (abfd, ptr);
   5605 
   5606   flags = elf_elfheader (abfd)->e_flags;
   5607   /* Ignore init flag - it may not be set, despite the flags field
   5608      containing valid data.  */
   5609 
   5610   /* xgettext:c-format */
   5611   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
   5612 
   5613   if (flags)
   5614     fprintf (file, _("<Unrecognised flag bits set>"));
   5615 
   5616   fputc ('\n', file);
   5617 
   5618   return TRUE;
   5619 }
   5620 
   5621 /* Update the got entry reference counts for the section being removed.  */
   5622 
   5623 static bfd_boolean
   5624 elfNN_aarch64_gc_sweep_hook (bfd *abfd,
   5625 			     struct bfd_link_info *info,
   5626 			     asection *sec,
   5627 			     const Elf_Internal_Rela * relocs)
   5628 {
   5629   struct elf_aarch64_link_hash_table *htab;
   5630   Elf_Internal_Shdr *symtab_hdr;
   5631   struct elf_link_hash_entry **sym_hashes;
   5632   struct elf_aarch64_local_symbol *locals;
   5633   const Elf_Internal_Rela *rel, *relend;
   5634 
   5635   if (info->relocatable)
   5636     return TRUE;
   5637 
   5638   htab = elf_aarch64_hash_table (info);
   5639 
   5640   if (htab == NULL)
   5641     return FALSE;
   5642 
   5643   elf_section_data (sec)->local_dynrel = NULL;
   5644 
   5645   symtab_hdr = &elf_symtab_hdr (abfd);
   5646   sym_hashes = elf_sym_hashes (abfd);
   5647 
   5648   locals = elf_aarch64_locals (abfd);
   5649 
   5650   relend = relocs + sec->reloc_count;
   5651   for (rel = relocs; rel < relend; rel++)
   5652     {
   5653       unsigned long r_symndx;
   5654       unsigned int r_type;
   5655       struct elf_link_hash_entry *h = NULL;
   5656 
   5657       r_symndx = ELFNN_R_SYM (rel->r_info);
   5658 
   5659       if (r_symndx >= symtab_hdr->sh_info)
   5660 	{
   5661 
   5662 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   5663 	  while (h->root.type == bfd_link_hash_indirect
   5664 		 || h->root.type == bfd_link_hash_warning)
   5665 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   5666         }
   5667       else
   5668 	{
   5669 	  Elf_Internal_Sym *isym;
   5670 
   5671 	  /* A local symbol.  */
   5672 	  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5673 					abfd, r_symndx);
   5674 
   5675 	  /* Check relocation against local STT_GNU_IFUNC symbol.  */
   5676 	  if (isym != NULL
   5677 	      && ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   5678 	    {
   5679 	      h = elfNN_aarch64_get_local_sym_hash (htab, abfd, rel, FALSE);
   5680 	      if (h == NULL)
   5681 		abort ();
   5682 	    }
   5683 	}
   5684 
   5685       if (h)
   5686 	{
   5687 	  struct elf_aarch64_link_hash_entry *eh;
   5688 	  struct elf_dyn_relocs **pp;
   5689 	  struct elf_dyn_relocs *p;
   5690 
   5691 	  eh = (struct elf_aarch64_link_hash_entry *) h;
   5692 
   5693 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
   5694 	    if (p->sec == sec)
   5695 	      {
   5696 		/* Everything must go for SEC.  */
   5697 		*pp = p->next;
   5698 		break;
   5699 	      }
   5700 	}
   5701 
   5702       r_type = ELFNN_R_TYPE (rel->r_info);
   5703       switch (aarch64_tls_transition (abfd,info, r_type, h ,r_symndx))
   5704 	{
   5705 	case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
   5706 	case BFD_RELOC_AARCH64_GOT_LD_PREL19:
   5707 	case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
   5708 	case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
   5709 	case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
   5710 	case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
   5711 	case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
   5712 	case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
   5713 	case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
   5714 	case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
   5715 	case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
   5716 	case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
   5717 	case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
   5718 	case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
   5719 	case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
   5720 	case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
   5721 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
   5722 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
   5723 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
   5724 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
   5725 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
   5726 	  if (h != NULL)
   5727 	    {
   5728 	      if (h->got.refcount > 0)
   5729 		h->got.refcount -= 1;
   5730 
   5731 	      if (h->type == STT_GNU_IFUNC)
   5732 		{
   5733 		  if (h->plt.refcount > 0)
   5734 		    h->plt.refcount -= 1;
   5735 		}
   5736 	    }
   5737 	  else if (locals != NULL)
   5738 	    {
   5739 	      if (locals[r_symndx].got_refcount > 0)
   5740 		locals[r_symndx].got_refcount -= 1;
   5741 	    }
   5742 	  break;
   5743 
   5744 	case BFD_RELOC_AARCH64_CALL26:
   5745 	case BFD_RELOC_AARCH64_JUMP26:
   5746 	  /* If this is a local symbol then we resolve it
   5747 	     directly without creating a PLT entry.  */
   5748 	  if (h == NULL)
   5749 	    continue;
   5750 
   5751 	  if (h->plt.refcount > 0)
   5752 	    h->plt.refcount -= 1;
   5753 	  break;
   5754 
   5755 	case BFD_RELOC_AARCH64_MOVW_G0_NC:
   5756 	case BFD_RELOC_AARCH64_MOVW_G1_NC:
   5757 	case BFD_RELOC_AARCH64_MOVW_G2_NC:
   5758 	case BFD_RELOC_AARCH64_MOVW_G3:
   5759 	case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
   5760 	case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
   5761 	case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
   5762 	case BFD_RELOC_AARCH64_NN:
   5763 	  if (h != NULL && info->executable)
   5764 	    {
   5765 	      if (h->plt.refcount > 0)
   5766 		h->plt.refcount -= 1;
   5767 	    }
   5768 	  break;
   5769 
   5770 	default:
   5771 	  break;
   5772 	}
   5773     }
   5774 
   5775   return TRUE;
   5776 }
   5777 
   5778 /* Adjust a symbol defined by a dynamic object and referenced by a
   5779    regular object.  The current definition is in some section of the
   5780    dynamic object, but we're not including those sections.  We have to
   5781    change the definition to something the rest of the link can
   5782    understand.	*/
   5783 
   5784 static bfd_boolean
   5785 elfNN_aarch64_adjust_dynamic_symbol (struct bfd_link_info *info,
   5786 				     struct elf_link_hash_entry *h)
   5787 {
   5788   struct elf_aarch64_link_hash_table *htab;
   5789   asection *s;
   5790 
   5791   /* If this is a function, put it in the procedure linkage table.  We
   5792      will fill in the contents of the procedure linkage table later,
   5793      when we know the address of the .got section.  */
   5794   if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
   5795     {
   5796       if (h->plt.refcount <= 0
   5797 	  || (h->type != STT_GNU_IFUNC
   5798 	      && (SYMBOL_CALLS_LOCAL (info, h)
   5799 		  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   5800 		      && h->root.type == bfd_link_hash_undefweak))))
   5801 	{
   5802 	  /* This case can occur if we saw a CALL26 reloc in
   5803 	     an input file, but the symbol wasn't referred to
   5804 	     by a dynamic object or all references were
   5805 	     garbage collected. In which case we can end up
   5806 	     resolving.  */
   5807 	  h->plt.offset = (bfd_vma) - 1;
   5808 	  h->needs_plt = 0;
   5809 	}
   5810 
   5811       return TRUE;
   5812     }
   5813   else
   5814     /* It's possible that we incorrectly decided a .plt reloc was
   5815        needed for an R_X86_64_PC32 reloc to a non-function sym in
   5816        check_relocs.  We can't decide accurately between function and
   5817        non-function syms in check-relocs;  Objects loaded later in
   5818        the link may change h->type.  So fix it now.  */
   5819     h->plt.offset = (bfd_vma) - 1;
   5820 
   5821 
   5822   /* If this is a weak symbol, and there is a real definition, the
   5823      processor independent code will have arranged for us to see the
   5824      real definition first, and we can just use the same value.  */
   5825   if (h->u.weakdef != NULL)
   5826     {
   5827       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   5828 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   5829       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   5830       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   5831       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
   5832 	h->non_got_ref = h->u.weakdef->non_got_ref;
   5833       return TRUE;
   5834     }
   5835 
   5836   /* If we are creating a shared library, we must presume that the
   5837      only references to the symbol are via the global offset table.
   5838      For such cases we need not do anything here; the relocations will
   5839      be handled correctly by relocate_section.  */
   5840   if (info->shared)
   5841     return TRUE;
   5842 
   5843   /* If there are no references to this symbol that do not use the
   5844      GOT, we don't need to generate a copy reloc.  */
   5845   if (!h->non_got_ref)
   5846     return TRUE;
   5847 
   5848   /* If -z nocopyreloc was given, we won't generate them either.  */
   5849   if (info->nocopyreloc)
   5850     {
   5851       h->non_got_ref = 0;
   5852       return TRUE;
   5853     }
   5854 
   5855   /* We must allocate the symbol in our .dynbss section, which will
   5856      become part of the .bss section of the executable.  There will be
   5857      an entry for this symbol in the .dynsym section.  The dynamic
   5858      object will contain position independent code, so all references
   5859      from the dynamic object to this symbol will go through the global
   5860      offset table.  The dynamic linker will use the .dynsym entry to
   5861      determine the address it must put in the global offset table, so
   5862      both the dynamic object and the regular object will refer to the
   5863      same memory location for the variable.  */
   5864 
   5865   htab = elf_aarch64_hash_table (info);
   5866 
   5867   /* We must generate a R_AARCH64_COPY reloc to tell the dynamic linker
   5868      to copy the initial value out of the dynamic object and into the
   5869      runtime process image.  */
   5870   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   5871     {
   5872       htab->srelbss->size += RELOC_SIZE (htab);
   5873       h->needs_copy = 1;
   5874     }
   5875 
   5876   s = htab->sdynbss;
   5877 
   5878   return _bfd_elf_adjust_dynamic_copy (h, s);
   5879 
   5880 }
   5881 
   5882 static bfd_boolean
   5883 elfNN_aarch64_allocate_local_symbols (bfd *abfd, unsigned number)
   5884 {
   5885   struct elf_aarch64_local_symbol *locals;
   5886   locals = elf_aarch64_locals (abfd);
   5887   if (locals == NULL)
   5888     {
   5889       locals = (struct elf_aarch64_local_symbol *)
   5890 	bfd_zalloc (abfd, number * sizeof (struct elf_aarch64_local_symbol));
   5891       if (locals == NULL)
   5892 	return FALSE;
   5893       elf_aarch64_locals (abfd) = locals;
   5894     }
   5895   return TRUE;
   5896 }
   5897 
   5898 /* Create the .got section to hold the global offset table.  */
   5899 
   5900 static bfd_boolean
   5901 aarch64_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
   5902 {
   5903   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5904   flagword flags;
   5905   asection *s;
   5906   struct elf_link_hash_entry *h;
   5907   struct elf_link_hash_table *htab = elf_hash_table (info);
   5908 
   5909   /* This function may be called more than once.  */
   5910   s = bfd_get_linker_section (abfd, ".got");
   5911   if (s != NULL)
   5912     return TRUE;
   5913 
   5914   flags = bed->dynamic_sec_flags;
   5915 
   5916   s = bfd_make_section_anyway_with_flags (abfd,
   5917 					  (bed->rela_plts_and_copies_p
   5918 					   ? ".rela.got" : ".rel.got"),
   5919 					  (bed->dynamic_sec_flags
   5920 					   | SEC_READONLY));
   5921   if (s == NULL
   5922       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
   5923     return FALSE;
   5924   htab->srelgot = s;
   5925 
   5926   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   5927   if (s == NULL
   5928       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
   5929     return FALSE;
   5930   htab->sgot = s;
   5931   htab->sgot->size += GOT_ENTRY_SIZE;
   5932 
   5933   if (bed->want_got_sym)
   5934     {
   5935       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
   5936 	 (or .got.plt) section.  We don't do this in the linker script
   5937 	 because we don't want to define the symbol if we are not creating
   5938 	 a global offset table.  */
   5939       h = _bfd_elf_define_linkage_sym (abfd, info, s,
   5940 				       "_GLOBAL_OFFSET_TABLE_");
   5941       elf_hash_table (info)->hgot = h;
   5942       if (h == NULL)
   5943 	return FALSE;
   5944     }
   5945 
   5946   if (bed->want_got_plt)
   5947     {
   5948       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
   5949       if (s == NULL
   5950 	  || !bfd_set_section_alignment (abfd, s,
   5951 					 bed->s->log_file_align))
   5952 	return FALSE;
   5953       htab->sgotplt = s;
   5954     }
   5955 
   5956   /* The first bit of the global offset table is the header.  */
   5957   s->size += bed->got_header_size;
   5958 
   5959   return TRUE;
   5960 }
   5961 
   5962 /* Look through the relocs for a section during the first phase.  */
   5963 
   5964 static bfd_boolean
   5965 elfNN_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
   5966 			    asection *sec, const Elf_Internal_Rela *relocs)
   5967 {
   5968   Elf_Internal_Shdr *symtab_hdr;
   5969   struct elf_link_hash_entry **sym_hashes;
   5970   const Elf_Internal_Rela *rel;
   5971   const Elf_Internal_Rela *rel_end;
   5972   asection *sreloc;
   5973 
   5974   struct elf_aarch64_link_hash_table *htab;
   5975 
   5976   if (info->relocatable)
   5977     return TRUE;
   5978 
   5979   BFD_ASSERT (is_aarch64_elf (abfd));
   5980 
   5981   htab = elf_aarch64_hash_table (info);
   5982   sreloc = NULL;
   5983 
   5984   symtab_hdr = &elf_symtab_hdr (abfd);
   5985   sym_hashes = elf_sym_hashes (abfd);
   5986 
   5987   rel_end = relocs + sec->reloc_count;
   5988   for (rel = relocs; rel < rel_end; rel++)
   5989     {
   5990       struct elf_link_hash_entry *h;
   5991       unsigned long r_symndx;
   5992       unsigned int r_type;
   5993       bfd_reloc_code_real_type bfd_r_type;
   5994       Elf_Internal_Sym *isym;
   5995 
   5996       r_symndx = ELFNN_R_SYM (rel->r_info);
   5997       r_type = ELFNN_R_TYPE (rel->r_info);
   5998 
   5999       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
   6000 	{
   6001 	  (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
   6002 				 r_symndx);
   6003 	  return FALSE;
   6004 	}
   6005 
   6006       if (r_symndx < symtab_hdr->sh_info)
   6007 	{
   6008 	  /* A local symbol.  */
   6009 	  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   6010 					abfd, r_symndx);
   6011 	  if (isym == NULL)
   6012 	    return FALSE;
   6013 
   6014 	  /* Check relocation against local STT_GNU_IFUNC symbol.  */
   6015 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   6016 	    {
   6017 	      h = elfNN_aarch64_get_local_sym_hash (htab, abfd, rel,
   6018 						    TRUE);
   6019 	      if (h == NULL)
   6020 		return FALSE;
   6021 
   6022 	      /* Fake a STT_GNU_IFUNC symbol.  */
   6023 	      h->type = STT_GNU_IFUNC;
   6024 	      h->def_regular = 1;
   6025 	      h->ref_regular = 1;
   6026 	      h->forced_local = 1;
   6027 	      h->root.type = bfd_link_hash_defined;
   6028 	    }
   6029 	  else
   6030 	    h = NULL;
   6031 	}
   6032       else
   6033 	{
   6034 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6035 	  while (h->root.type == bfd_link_hash_indirect
   6036 		 || h->root.type == bfd_link_hash_warning)
   6037 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   6038 
   6039 	  /* PR15323, ref flags aren't set for references in the same
   6040 	     object.  */
   6041 	  h->root.non_ir_ref = 1;
   6042 	}
   6043 
   6044       /* Could be done earlier, if h were already available.  */
   6045       bfd_r_type = aarch64_tls_transition (abfd, info, r_type, h, r_symndx);
   6046 
   6047       if (h != NULL)
   6048 	{
   6049 	  /* Create the ifunc sections for static executables.  If we
   6050 	     never see an indirect function symbol nor we are building
   6051 	     a static executable, those sections will be empty and
   6052 	     won't appear in output.  */
   6053 	  switch (bfd_r_type)
   6054 	    {
   6055 	    default:
   6056 	      break;
   6057 
   6058 	    case BFD_RELOC_AARCH64_NN:
   6059 	    case BFD_RELOC_AARCH64_CALL26:
   6060 	    case BFD_RELOC_AARCH64_JUMP26:
   6061 	    case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
   6062 	    case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
   6063 	    case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
   6064 	    case BFD_RELOC_AARCH64_GOT_LD_PREL19:
   6065 	    case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
   6066 	    case BFD_RELOC_AARCH64_ADD_LO12:
   6067 	      if (htab->root.dynobj == NULL)
   6068 		htab->root.dynobj = abfd;
   6069 	      if (!_bfd_elf_create_ifunc_sections (htab->root.dynobj, info))
   6070 		return FALSE;
   6071 	      break;
   6072 	    }
   6073 
   6074 	  /* It is referenced by a non-shared object. */
   6075 	  h->ref_regular = 1;
   6076 	  h->root.non_ir_ref = 1;
   6077 	}
   6078 
   6079       switch (bfd_r_type)
   6080 	{
   6081 	case BFD_RELOC_AARCH64_NN:
   6082 
   6083 	  /* We don't need to handle relocs into sections not going into
   6084 	     the "real" output.  */
   6085 	  if ((sec->flags & SEC_ALLOC) == 0)
   6086 	    break;
   6087 
   6088 	  if (h != NULL)
   6089 	    {
   6090 	      if (!info->shared)
   6091 		h->non_got_ref = 1;
   6092 
   6093 	      h->plt.refcount += 1;
   6094 	      h->pointer_equality_needed = 1;
   6095 	    }
   6096 
   6097 	  /* No need to do anything if we're not creating a shared
   6098 	     object.  */
   6099 	  if (! info->shared)
   6100 	    break;
   6101 
   6102 	  {
   6103 	    struct elf_dyn_relocs *p;
   6104 	    struct elf_dyn_relocs **head;
   6105 
   6106 	    /* We must copy these reloc types into the output file.
   6107 	       Create a reloc section in dynobj and make room for
   6108 	       this reloc.  */
   6109 	    if (sreloc == NULL)
   6110 	      {
   6111 		if (htab->root.dynobj == NULL)
   6112 		  htab->root.dynobj = abfd;
   6113 
   6114 		sreloc = _bfd_elf_make_dynamic_reloc_section
   6115 		  (sec, htab->root.dynobj, LOG_FILE_ALIGN, abfd, /*rela? */ TRUE);
   6116 
   6117 		if (sreloc == NULL)
   6118 		  return FALSE;
   6119 	      }
   6120 
   6121 	    /* If this is a global symbol, we count the number of
   6122 	       relocations we need for this symbol.  */
   6123 	    if (h != NULL)
   6124 	      {
   6125 		struct elf_aarch64_link_hash_entry *eh;
   6126 		eh = (struct elf_aarch64_link_hash_entry *) h;
   6127 		head = &eh->dyn_relocs;
   6128 	      }
   6129 	    else
   6130 	      {
   6131 		/* Track dynamic relocs needed for local syms too.
   6132 		   We really need local syms available to do this
   6133 		   easily.  Oh well.  */
   6134 
   6135 		asection *s;
   6136 		void **vpp;
   6137 
   6138 		isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   6139 					      abfd, r_symndx);
   6140 		if (isym == NULL)
   6141 		  return FALSE;
   6142 
   6143 		s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   6144 		if (s == NULL)
   6145 		  s = sec;
   6146 
   6147 		/* Beware of type punned pointers vs strict aliasing
   6148 		   rules.  */
   6149 		vpp = &(elf_section_data (s)->local_dynrel);
   6150 		head = (struct elf_dyn_relocs **) vpp;
   6151 	      }
   6152 
   6153 	    p = *head;
   6154 	    if (p == NULL || p->sec != sec)
   6155 	      {
   6156 		bfd_size_type amt = sizeof *p;
   6157 		p = ((struct elf_dyn_relocs *)
   6158 		     bfd_zalloc (htab->root.dynobj, amt));
   6159 		if (p == NULL)
   6160 		  return FALSE;
   6161 		p->next = *head;
   6162 		*head = p;
   6163 		p->sec = sec;
   6164 	      }
   6165 
   6166 	    p->count += 1;
   6167 
   6168 	  }
   6169 	  break;
   6170 
   6171 	  /* RR: We probably want to keep a consistency check that
   6172 	     there are no dangling GOT_PAGE relocs.  */
   6173 	case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
   6174 	case BFD_RELOC_AARCH64_GOT_LD_PREL19:
   6175 	case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
   6176 	case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
   6177 	case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
   6178 	case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
   6179 	case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
   6180 	case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
   6181 	case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
   6182 	case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
   6183 	case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
   6184 	case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
   6185 	case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
   6186 	case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
   6187 	case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
   6188 	case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
   6189 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
   6190 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
   6191 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
   6192 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
   6193 	case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
   6194 	  {
   6195 	    unsigned got_type;
   6196 	    unsigned old_got_type;
   6197 
   6198 	    got_type = aarch64_reloc_got_type (bfd_r_type);
   6199 
   6200 	    if (h)
   6201 	      {
   6202 		h->got.refcount += 1;
   6203 		old_got_type = elf_aarch64_hash_entry (h)->got_type;
   6204 	      }
   6205 	    else
   6206 	      {
   6207 		struct elf_aarch64_local_symbol *locals;
   6208 
   6209 		if (!elfNN_aarch64_allocate_local_symbols
   6210 		    (abfd, symtab_hdr->sh_info))
   6211 		  return FALSE;
   6212 
   6213 		locals = elf_aarch64_locals (abfd);
   6214 		BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
   6215 		locals[r_symndx].got_refcount += 1;
   6216 		old_got_type = locals[r_symndx].got_type;
   6217 	      }
   6218 
   6219 	    /* If a variable is accessed with both general dynamic TLS
   6220 	       methods, two slots may be created.  */
   6221 	    if (GOT_TLS_GD_ANY_P (old_got_type) && GOT_TLS_GD_ANY_P (got_type))
   6222 	      got_type |= old_got_type;
   6223 
   6224 	    /* We will already have issued an error message if there
   6225 	       is a TLS/non-TLS mismatch, based on the symbol type.
   6226 	       So just combine any TLS types needed.  */
   6227 	    if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
   6228 		&& got_type != GOT_NORMAL)
   6229 	      got_type |= old_got_type;
   6230 
   6231 	    /* If the symbol is accessed by both IE and GD methods, we
   6232 	       are able to relax.  Turn off the GD flag, without
   6233 	       messing up with any other kind of TLS types that may be
   6234 	       involved.  */
   6235 	    if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type))
   6236 	      got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD);
   6237 
   6238 	    if (old_got_type != got_type)
   6239 	      {
   6240 		if (h != NULL)
   6241 		  elf_aarch64_hash_entry (h)->got_type = got_type;
   6242 		else
   6243 		  {
   6244 		    struct elf_aarch64_local_symbol *locals;
   6245 		    locals = elf_aarch64_locals (abfd);
   6246 		    BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
   6247 		    locals[r_symndx].got_type = got_type;
   6248 		  }
   6249 	      }
   6250 
   6251 	    if (htab->root.dynobj == NULL)
   6252 	      htab->root.dynobj = abfd;
   6253 	    if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
   6254 	      return FALSE;
   6255 	    break;
   6256 	  }
   6257 
   6258 	case BFD_RELOC_AARCH64_MOVW_G0_NC:
   6259 	case BFD_RELOC_AARCH64_MOVW_G1_NC:
   6260 	case BFD_RELOC_AARCH64_MOVW_G2_NC:
   6261 	case BFD_RELOC_AARCH64_MOVW_G3:
   6262 	  if (info->shared)
   6263 	    {
   6264 	      int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
   6265 	      (*_bfd_error_handler)
   6266 		(_("%B: relocation %s against `%s' can not be used when making "
   6267 		   "a shared object; recompile with -fPIC"),
   6268 		 abfd, elfNN_aarch64_howto_table[howto_index].name,
   6269 		 (h) ? h->root.root.string : "a local symbol");
   6270 	      bfd_set_error (bfd_error_bad_value);
   6271 	      return FALSE;
   6272 	    }
   6273 
   6274 	case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
   6275 	case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
   6276 	case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
   6277 	  if (h != NULL && info->executable)
   6278 	    {
   6279 	      /* If this reloc is in a read-only section, we might
   6280 		 need a copy reloc.  We can't check reliably at this
   6281 		 stage whether the section is read-only, as input
   6282 		 sections have not yet been mapped to output sections.
   6283 		 Tentatively set the flag for now, and correct in
   6284 		 adjust_dynamic_symbol.  */
   6285 	      h->non_got_ref = 1;
   6286 	      h->plt.refcount += 1;
   6287 	      h->pointer_equality_needed = 1;
   6288 	    }
   6289 	  /* FIXME:: RR need to handle these in shared libraries
   6290 	     and essentially bomb out as these being non-PIC
   6291 	     relocations in shared libraries.  */
   6292 	  break;
   6293 
   6294 	case BFD_RELOC_AARCH64_CALL26:
   6295 	case BFD_RELOC_AARCH64_JUMP26:
   6296 	  /* If this is a local symbol then we resolve it
   6297 	     directly without creating a PLT entry.  */
   6298 	  if (h == NULL)
   6299 	    continue;
   6300 
   6301 	  h->needs_plt = 1;
   6302 	  if (h->plt.refcount <= 0)
   6303 	    h->plt.refcount = 1;
   6304 	  else
   6305 	    h->plt.refcount += 1;
   6306 	  break;
   6307 
   6308 	default:
   6309 	  break;
   6310 	}
   6311     }
   6312 
   6313   return TRUE;
   6314 }
   6315 
   6316 /* Treat mapping symbols as special target symbols.  */
   6317 
   6318 static bfd_boolean
   6319 elfNN_aarch64_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED,
   6320 					asymbol *sym)
   6321 {
   6322   return bfd_is_aarch64_special_symbol_name (sym->name,
   6323 					     BFD_AARCH64_SPECIAL_SYM_TYPE_ANY);
   6324 }
   6325 
   6326 /* This is a copy of elf_find_function () from elf.c except that
   6327    AArch64 mapping symbols are ignored when looking for function names.  */
   6328 
   6329 static bfd_boolean
   6330 aarch64_elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
   6331 			   asymbol **symbols,
   6332 			   asection *section,
   6333 			   bfd_vma offset,
   6334 			   const char **filename_ptr,
   6335 			   const char **functionname_ptr)
   6336 {
   6337   const char *filename = NULL;
   6338   asymbol *func = NULL;
   6339   bfd_vma low_func = 0;
   6340   asymbol **p;
   6341 
   6342   for (p = symbols; *p != NULL; p++)
   6343     {
   6344       elf_symbol_type *q;
   6345 
   6346       q = (elf_symbol_type *) * p;
   6347 
   6348       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
   6349 	{
   6350 	default:
   6351 	  break;
   6352 	case STT_FILE:
   6353 	  filename = bfd_asymbol_name (&q->symbol);
   6354 	  break;
   6355 	case STT_FUNC:
   6356 	case STT_NOTYPE:
   6357 	  /* Skip mapping symbols.  */
   6358 	  if ((q->symbol.flags & BSF_LOCAL)
   6359 	      && (bfd_is_aarch64_special_symbol_name
   6360 		  (q->symbol.name, BFD_AARCH64_SPECIAL_SYM_TYPE_ANY)))
   6361 	    continue;
   6362 	  /* Fall through.  */
   6363 	  if (bfd_get_section (&q->symbol) == section
   6364 	      && q->symbol.value >= low_func && q->symbol.value <= offset)
   6365 	    {
   6366 	      func = (asymbol *) q;
   6367 	      low_func = q->symbol.value;
   6368 	    }
   6369 	  break;
   6370 	}
   6371     }
   6372 
   6373   if (func == NULL)
   6374     return FALSE;
   6375 
   6376   if (filename_ptr)
   6377     *filename_ptr = filename;
   6378   if (functionname_ptr)
   6379     *functionname_ptr = bfd_asymbol_name (func);
   6380 
   6381   return TRUE;
   6382 }
   6383 
   6384 
   6385 /* Find the nearest line to a particular section and offset, for error
   6386    reporting.   This code is a duplicate of the code in elf.c, except
   6387    that it uses aarch64_elf_find_function.  */
   6388 
   6389 static bfd_boolean
   6390 elfNN_aarch64_find_nearest_line (bfd *abfd,
   6391 				 asymbol **symbols,
   6392 				 asection *section,
   6393 				 bfd_vma offset,
   6394 				 const char **filename_ptr,
   6395 				 const char **functionname_ptr,
   6396 				 unsigned int *line_ptr,
   6397 				 unsigned int *discriminator_ptr)
   6398 {
   6399   bfd_boolean found = FALSE;
   6400 
   6401   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   6402 				     filename_ptr, functionname_ptr,
   6403 				     line_ptr, discriminator_ptr,
   6404 				     dwarf_debug_sections, 0,
   6405 				     &elf_tdata (abfd)->dwarf2_find_line_info))
   6406     {
   6407       if (!*functionname_ptr)
   6408 	aarch64_elf_find_function (abfd, symbols, section, offset,
   6409 				   *filename_ptr ? NULL : filename_ptr,
   6410 				   functionname_ptr);
   6411 
   6412       return TRUE;
   6413     }
   6414 
   6415   /* Skip _bfd_dwarf1_find_nearest_line since no known AArch64
   6416      toolchain uses DWARF1.  */
   6417 
   6418   if (!_bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   6419 					    &found, filename_ptr,
   6420 					    functionname_ptr, line_ptr,
   6421 					    &elf_tdata (abfd)->line_info))
   6422     return FALSE;
   6423 
   6424   if (found && (*functionname_ptr || *line_ptr))
   6425     return TRUE;
   6426 
   6427   if (symbols == NULL)
   6428     return FALSE;
   6429 
   6430   if (!aarch64_elf_find_function (abfd, symbols, section, offset,
   6431 				  filename_ptr, functionname_ptr))
   6432     return FALSE;
   6433 
   6434   *line_ptr = 0;
   6435   return TRUE;
   6436 }
   6437 
   6438 static bfd_boolean
   6439 elfNN_aarch64_find_inliner_info (bfd *abfd,
   6440 				 const char **filename_ptr,
   6441 				 const char **functionname_ptr,
   6442 				 unsigned int *line_ptr)
   6443 {
   6444   bfd_boolean found;
   6445   found = _bfd_dwarf2_find_inliner_info
   6446     (abfd, filename_ptr,
   6447      functionname_ptr, line_ptr, &elf_tdata (abfd)->dwarf2_find_line_info);
   6448   return found;
   6449 }
   6450 
   6451 
   6452 static void
   6453 elfNN_aarch64_post_process_headers (bfd *abfd,
   6454 				    struct bfd_link_info *link_info)
   6455 {
   6456   Elf_Internal_Ehdr *i_ehdrp;	/* ELF file header, internal form.  */
   6457 
   6458   i_ehdrp = elf_elfheader (abfd);
   6459   i_ehdrp->e_ident[EI_ABIVERSION] = AARCH64_ELF_ABI_VERSION;
   6460 
   6461   _bfd_elf_post_process_headers (abfd, link_info);
   6462 }
   6463 
   6464 static enum elf_reloc_type_class
   6465 elfNN_aarch64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   6466 				const asection *rel_sec ATTRIBUTE_UNUSED,
   6467 				const Elf_Internal_Rela *rela)
   6468 {
   6469   switch ((int) ELFNN_R_TYPE (rela->r_info))
   6470     {
   6471     case AARCH64_R (RELATIVE):
   6472       return reloc_class_relative;
   6473     case AARCH64_R (JUMP_SLOT):
   6474       return reloc_class_plt;
   6475     case AARCH64_R (COPY):
   6476       return reloc_class_copy;
   6477     default:
   6478       return reloc_class_normal;
   6479     }
   6480 }
   6481 
   6482 /* Handle an AArch64 specific section when reading an object file.  This is
   6483    called when bfd_section_from_shdr finds a section with an unknown
   6484    type.  */
   6485 
   6486 static bfd_boolean
   6487 elfNN_aarch64_section_from_shdr (bfd *abfd,
   6488 				 Elf_Internal_Shdr *hdr,
   6489 				 const char *name, int shindex)
   6490 {
   6491   /* There ought to be a place to keep ELF backend specific flags, but
   6492      at the moment there isn't one.  We just keep track of the
   6493      sections by their name, instead.  Fortunately, the ABI gives
   6494      names for all the AArch64 specific sections, so we will probably get
   6495      away with this.  */
   6496   switch (hdr->sh_type)
   6497     {
   6498     case SHT_AARCH64_ATTRIBUTES:
   6499       break;
   6500 
   6501     default:
   6502       return FALSE;
   6503     }
   6504 
   6505   if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   6506     return FALSE;
   6507 
   6508   return TRUE;
   6509 }
   6510 
   6511 /* A structure used to record a list of sections, independently
   6512    of the next and prev fields in the asection structure.  */
   6513 typedef struct section_list
   6514 {
   6515   asection *sec;
   6516   struct section_list *next;
   6517   struct section_list *prev;
   6518 }
   6519 section_list;
   6520 
   6521 /* Unfortunately we need to keep a list of sections for which
   6522    an _aarch64_elf_section_data structure has been allocated.  This
   6523    is because it is possible for functions like elfNN_aarch64_write_section
   6524    to be called on a section which has had an elf_data_structure
   6525    allocated for it (and so the used_by_bfd field is valid) but
   6526    for which the AArch64 extended version of this structure - the
   6527    _aarch64_elf_section_data structure - has not been allocated.  */
   6528 static section_list *sections_with_aarch64_elf_section_data = NULL;
   6529 
   6530 static void
   6531 record_section_with_aarch64_elf_section_data (asection *sec)
   6532 {
   6533   struct section_list *entry;
   6534 
   6535   entry = bfd_malloc (sizeof (*entry));
   6536   if (entry == NULL)
   6537     return;
   6538   entry->sec = sec;
   6539   entry->next = sections_with_aarch64_elf_section_data;
   6540   entry->prev = NULL;
   6541   if (entry->next != NULL)
   6542     entry->next->prev = entry;
   6543   sections_with_aarch64_elf_section_data = entry;
   6544 }
   6545 
   6546 static struct section_list *
   6547 find_aarch64_elf_section_entry (asection *sec)
   6548 {
   6549   struct section_list *entry;
   6550   static struct section_list *last_entry = NULL;
   6551 
   6552   /* This is a short cut for the typical case where the sections are added
   6553      to the sections_with_aarch64_elf_section_data list in forward order and
   6554      then looked up here in backwards order.  This makes a real difference
   6555      to the ld-srec/sec64k.exp linker test.  */
   6556   entry = sections_with_aarch64_elf_section_data;
   6557   if (last_entry != NULL)
   6558     {
   6559       if (last_entry->sec == sec)
   6560 	entry = last_entry;
   6561       else if (last_entry->next != NULL && last_entry->next->sec == sec)
   6562 	entry = last_entry->next;
   6563     }
   6564 
   6565   for (; entry; entry = entry->next)
   6566     if (entry->sec == sec)
   6567       break;
   6568 
   6569   if (entry)
   6570     /* Record the entry prior to this one - it is the entry we are
   6571        most likely to want to locate next time.  Also this way if we
   6572        have been called from
   6573        unrecord_section_with_aarch64_elf_section_data () we will not
   6574        be caching a pointer that is about to be freed.  */
   6575     last_entry = entry->prev;
   6576 
   6577   return entry;
   6578 }
   6579 
   6580 static void
   6581 unrecord_section_with_aarch64_elf_section_data (asection *sec)
   6582 {
   6583   struct section_list *entry;
   6584 
   6585   entry = find_aarch64_elf_section_entry (sec);
   6586 
   6587   if (entry)
   6588     {
   6589       if (entry->prev != NULL)
   6590 	entry->prev->next = entry->next;
   6591       if (entry->next != NULL)
   6592 	entry->next->prev = entry->prev;
   6593       if (entry == sections_with_aarch64_elf_section_data)
   6594 	sections_with_aarch64_elf_section_data = entry->next;
   6595       free (entry);
   6596     }
   6597 }
   6598 
   6599 
   6600 typedef struct
   6601 {
   6602   void *finfo;
   6603   struct bfd_link_info *info;
   6604   asection *sec;
   6605   int sec_shndx;
   6606   int (*func) (void *, const char *, Elf_Internal_Sym *,
   6607 	       asection *, struct elf_link_hash_entry *);
   6608 } output_arch_syminfo;
   6609 
   6610 enum map_symbol_type
   6611 {
   6612   AARCH64_MAP_INSN,
   6613   AARCH64_MAP_DATA
   6614 };
   6615 
   6616 
   6617 /* Output a single mapping symbol.  */
   6618 
   6619 static bfd_boolean
   6620 elfNN_aarch64_output_map_sym (output_arch_syminfo *osi,
   6621 			      enum map_symbol_type type, bfd_vma offset)
   6622 {
   6623   static const char *names[2] = { "$x", "$d" };
   6624   Elf_Internal_Sym sym;
   6625 
   6626   sym.st_value = (osi->sec->output_section->vma
   6627 		  + osi->sec->output_offset + offset);
   6628   sym.st_size = 0;
   6629   sym.st_other = 0;
   6630   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
   6631   sym.st_shndx = osi->sec_shndx;
   6632   return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
   6633 }
   6634 
   6635 
   6636 
   6637 /* Output mapping symbols for PLT entries associated with H.  */
   6638 
   6639 static bfd_boolean
   6640 elfNN_aarch64_output_plt_map (struct elf_link_hash_entry *h, void *inf)
   6641 {
   6642   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
   6643   bfd_vma addr;
   6644 
   6645   if (h->root.type == bfd_link_hash_indirect)
   6646     return TRUE;
   6647 
   6648   if (h->root.type == bfd_link_hash_warning)
   6649     /* When warning symbols are created, they **replace** the "real"
   6650        entry in the hash table, thus we never get to see the real
   6651        symbol in a hash traversal.  So look at it now.  */
   6652     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   6653 
   6654   if (h->plt.offset == (bfd_vma) - 1)
   6655     return TRUE;
   6656 
   6657   addr = h->plt.offset;
   6658   if (addr == 32)
   6659     {
   6660       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
   6661 	return FALSE;
   6662     }
   6663   return TRUE;
   6664 }
   6665 
   6666 
   6667 /* Output a single local symbol for a generated stub.  */
   6668 
   6669 static bfd_boolean
   6670 elfNN_aarch64_output_stub_sym (output_arch_syminfo *osi, const char *name,
   6671 			       bfd_vma offset, bfd_vma size)
   6672 {
   6673   Elf_Internal_Sym sym;
   6674 
   6675   sym.st_value = (osi->sec->output_section->vma
   6676 		  + osi->sec->output_offset + offset);
   6677   sym.st_size = size;
   6678   sym.st_other = 0;
   6679   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   6680   sym.st_shndx = osi->sec_shndx;
   6681   return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
   6682 }
   6683 
   6684 static bfd_boolean
   6685 aarch64_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   6686 {
   6687   struct elf_aarch64_stub_hash_entry *stub_entry;
   6688   asection *stub_sec;
   6689   bfd_vma addr;
   6690   char *stub_name;
   6691   output_arch_syminfo *osi;
   6692 
   6693   /* Massage our args to the form they really have.  */
   6694   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
   6695   osi = (output_arch_syminfo *) in_arg;
   6696 
   6697   stub_sec = stub_entry->stub_sec;
   6698 
   6699   /* Ensure this stub is attached to the current section being
   6700      processed.  */
   6701   if (stub_sec != osi->sec)
   6702     return TRUE;
   6703 
   6704   addr = (bfd_vma) stub_entry->stub_offset;
   6705 
   6706   stub_name = stub_entry->output_name;
   6707 
   6708   switch (stub_entry->stub_type)
   6709     {
   6710     case aarch64_stub_adrp_branch:
   6711       if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
   6712 					  sizeof (aarch64_adrp_branch_stub)))
   6713 	return FALSE;
   6714       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
   6715 	return FALSE;
   6716       break;
   6717     case aarch64_stub_long_branch:
   6718       if (!elfNN_aarch64_output_stub_sym
   6719 	  (osi, stub_name, addr, sizeof (aarch64_long_branch_stub)))
   6720 	return FALSE;
   6721       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
   6722 	return FALSE;
   6723       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_DATA, addr + 16))
   6724 	return FALSE;
   6725       break;
   6726     case aarch64_stub_erratum_835769_veneer:
   6727       if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
   6728 					  sizeof (aarch64_erratum_835769_stub)))
   6729 	return FALSE;
   6730       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
   6731 	return FALSE;
   6732       break;
   6733     case aarch64_stub_erratum_843419_veneer:
   6734       if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
   6735 					  sizeof (aarch64_erratum_843419_stub)))
   6736 	return FALSE;
   6737       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
   6738 	return FALSE;
   6739       break;
   6740 
   6741     default:
   6742       BFD_FAIL ();
   6743     }
   6744 
   6745   return TRUE;
   6746 }
   6747 
   6748 /* Output mapping symbols for linker generated sections.  */
   6749 
   6750 static bfd_boolean
   6751 elfNN_aarch64_output_arch_local_syms (bfd *output_bfd,
   6752 				      struct bfd_link_info *info,
   6753 				      void *finfo,
   6754 				      int (*func) (void *, const char *,
   6755 						   Elf_Internal_Sym *,
   6756 						   asection *,
   6757 						   struct elf_link_hash_entry
   6758 						   *))
   6759 {
   6760   output_arch_syminfo osi;
   6761   struct elf_aarch64_link_hash_table *htab;
   6762 
   6763   htab = elf_aarch64_hash_table (info);
   6764 
   6765   osi.finfo = finfo;
   6766   osi.info = info;
   6767   osi.func = func;
   6768 
   6769   /* Long calls stubs.  */
   6770   if (htab->stub_bfd && htab->stub_bfd->sections)
   6771     {
   6772       asection *stub_sec;
   6773 
   6774       for (stub_sec = htab->stub_bfd->sections;
   6775 	   stub_sec != NULL; stub_sec = stub_sec->next)
   6776 	{
   6777 	  /* Ignore non-stub sections.  */
   6778 	  if (!strstr (stub_sec->name, STUB_SUFFIX))
   6779 	    continue;
   6780 
   6781 	  osi.sec = stub_sec;
   6782 
   6783 	  osi.sec_shndx = _bfd_elf_section_from_bfd_section
   6784 	    (output_bfd, osi.sec->output_section);
   6785 
   6786 	  /* The first instruction in a stub is always a branch.  */
   6787 	  if (!elfNN_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0))
   6788 	    return FALSE;
   6789 
   6790 	  bfd_hash_traverse (&htab->stub_hash_table, aarch64_map_one_stub,
   6791 			     &osi);
   6792 	}
   6793     }
   6794 
   6795   /* Finally, output mapping symbols for the PLT.  */
   6796   if (!htab->root.splt || htab->root.splt->size == 0)
   6797     return TRUE;
   6798 
   6799   /* For now live without mapping symbols for the plt.  */
   6800   osi.sec_shndx = _bfd_elf_section_from_bfd_section
   6801     (output_bfd, htab->root.splt->output_section);
   6802   osi.sec = htab->root.splt;
   6803 
   6804   elf_link_hash_traverse (&htab->root, elfNN_aarch64_output_plt_map,
   6805 			  (void *) &osi);
   6806 
   6807   return TRUE;
   6808 
   6809 }
   6810 
   6811 /* Allocate target specific section data.  */
   6812 
   6813 static bfd_boolean
   6814 elfNN_aarch64_new_section_hook (bfd *abfd, asection *sec)
   6815 {
   6816   if (!sec->used_by_bfd)
   6817     {
   6818       _aarch64_elf_section_data *sdata;
   6819       bfd_size_type amt = sizeof (*sdata);
   6820 
   6821       sdata = bfd_zalloc (abfd, amt);
   6822       if (sdata == NULL)
   6823 	return FALSE;
   6824       sec->used_by_bfd = sdata;
   6825     }
   6826 
   6827   record_section_with_aarch64_elf_section_data (sec);
   6828 
   6829   return _bfd_elf_new_section_hook (abfd, sec);
   6830 }
   6831 
   6832 
   6833 static void
   6834 unrecord_section_via_map_over_sections (bfd *abfd ATTRIBUTE_UNUSED,
   6835 					asection *sec,
   6836 					void *ignore ATTRIBUTE_UNUSED)
   6837 {
   6838   unrecord_section_with_aarch64_elf_section_data (sec);
   6839 }
   6840 
   6841 static bfd_boolean
   6842 elfNN_aarch64_close_and_cleanup (bfd *abfd)
   6843 {
   6844   if (abfd->sections)
   6845     bfd_map_over_sections (abfd,
   6846 			   unrecord_section_via_map_over_sections, NULL);
   6847 
   6848   return _bfd_elf_close_and_cleanup (abfd);
   6849 }
   6850 
   6851 static bfd_boolean
   6852 elfNN_aarch64_bfd_free_cached_info (bfd *abfd)
   6853 {
   6854   if (abfd->sections)
   6855     bfd_map_over_sections (abfd,
   6856 			   unrecord_section_via_map_over_sections, NULL);
   6857 
   6858   return _bfd_free_cached_info (abfd);
   6859 }
   6860 
   6861 /* Create dynamic sections. This is different from the ARM backend in that
   6862    the got, plt, gotplt and their relocation sections are all created in the
   6863    standard part of the bfd elf backend.  */
   6864 
   6865 static bfd_boolean
   6866 elfNN_aarch64_create_dynamic_sections (bfd *dynobj,
   6867 				       struct bfd_link_info *info)
   6868 {
   6869   struct elf_aarch64_link_hash_table *htab;
   6870 
   6871   /* We need to create .got section.  */
   6872   if (!aarch64_elf_create_got_section (dynobj, info))
   6873     return FALSE;
   6874 
   6875   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   6876     return FALSE;
   6877 
   6878   htab = elf_aarch64_hash_table (info);
   6879   htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
   6880   if (!info->shared)
   6881     htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
   6882 
   6883   if (!htab->sdynbss || (!info->shared && !htab->srelbss))
   6884     abort ();
   6885 
   6886   return TRUE;
   6887 }
   6888 
   6889 
   6890 /* Allocate space in .plt, .got and associated reloc sections for
   6891    dynamic relocs.  */
   6892 
   6893 static bfd_boolean
   6894 elfNN_aarch64_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   6895 {
   6896   struct bfd_link_info *info;
   6897   struct elf_aarch64_link_hash_table *htab;
   6898   struct elf_aarch64_link_hash_entry *eh;
   6899   struct elf_dyn_relocs *p;
   6900 
   6901   /* An example of a bfd_link_hash_indirect symbol is versioned
   6902      symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
   6903      -> __gxx_personality_v0(bfd_link_hash_defined)
   6904 
   6905      There is no need to process bfd_link_hash_indirect symbols here
   6906      because we will also be presented with the concrete instance of
   6907      the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
   6908      called to copy all relevant data from the generic to the concrete
   6909      symbol instance.
   6910    */
   6911   if (h->root.type == bfd_link_hash_indirect)
   6912     return TRUE;
   6913 
   6914   if (h->root.type == bfd_link_hash_warning)
   6915     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   6916 
   6917   info = (struct bfd_link_info *) inf;
   6918   htab = elf_aarch64_hash_table (info);
   6919 
   6920   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
   6921      here if it is defined and referenced in a non-shared object.  */
   6922   if (h->type == STT_GNU_IFUNC
   6923       && h->def_regular)
   6924     return TRUE;
   6925   else if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
   6926     {
   6927       /* Make sure this symbol is output as a dynamic symbol.
   6928          Undefined weak syms won't yet be marked as dynamic.  */
   6929       if (h->dynindx == -1 && !h->forced_local)
   6930 	{
   6931 	  if (!bfd_elf_link_record_dynamic_symbol (info, h))
   6932 	    return FALSE;
   6933 	}
   6934 
   6935       if (info->shared || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
   6936 	{
   6937 	  asection *s = htab->root.splt;
   6938 
   6939 	  /* If this is the first .plt entry, make room for the special
   6940 	     first entry.  */
   6941 	  if (s->size == 0)
   6942 	    s->size += htab->plt_header_size;
   6943 
   6944 	  h->plt.offset = s->size;
   6945 
   6946 	  /* If this symbol is not defined in a regular file, and we are
   6947 	     not generating a shared library, then set the symbol to this
   6948 	     location in the .plt.  This is required to make function
   6949 	     pointers compare as equal between the normal executable and
   6950 	     the shared library.  */
   6951 	  if (!info->shared && !h->def_regular)
   6952 	    {
   6953 	      h->root.u.def.section = s;
   6954 	      h->root.u.def.value = h->plt.offset;
   6955 	    }
   6956 
   6957 	  /* Make room for this entry. For now we only create the
   6958 	     small model PLT entries. We later need to find a way
   6959 	     of relaxing into these from the large model PLT entries.  */
   6960 	  s->size += PLT_SMALL_ENTRY_SIZE;
   6961 
   6962 	  /* We also need to make an entry in the .got.plt section, which
   6963 	     will be placed in the .got section by the linker script.  */
   6964 	  htab->root.sgotplt->size += GOT_ENTRY_SIZE;
   6965 
   6966 	  /* We also need to make an entry in the .rela.plt section.  */
   6967 	  htab->root.srelplt->size += RELOC_SIZE (htab);
   6968 
   6969 	  /* We need to ensure that all GOT entries that serve the PLT
   6970 	     are consecutive with the special GOT slots [0] [1] and
   6971 	     [2]. Any addtional relocations, such as
   6972 	     R_AARCH64_TLSDESC, must be placed after the PLT related
   6973 	     entries.  We abuse the reloc_count such that during
   6974 	     sizing we adjust reloc_count to indicate the number of
   6975 	     PLT related reserved entries.  In subsequent phases when
   6976 	     filling in the contents of the reloc entries, PLT related
   6977 	     entries are placed by computing their PLT index (0
   6978 	     .. reloc_count). While other none PLT relocs are placed
   6979 	     at the slot indicated by reloc_count and reloc_count is
   6980 	     updated.  */
   6981 
   6982 	  htab->root.srelplt->reloc_count++;
   6983 	}
   6984       else
   6985 	{
   6986 	  h->plt.offset = (bfd_vma) - 1;
   6987 	  h->needs_plt = 0;
   6988 	}
   6989     }
   6990   else
   6991     {
   6992       h->plt.offset = (bfd_vma) - 1;
   6993       h->needs_plt = 0;
   6994     }
   6995 
   6996   eh = (struct elf_aarch64_link_hash_entry *) h;
   6997   eh->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
   6998 
   6999   if (h->got.refcount > 0)
   7000     {
   7001       bfd_boolean dyn;
   7002       unsigned got_type = elf_aarch64_hash_entry (h)->got_type;
   7003 
   7004       h->got.offset = (bfd_vma) - 1;
   7005 
   7006       dyn = htab->root.dynamic_sections_created;
   7007 
   7008       /* Make sure this symbol is output as a dynamic symbol.
   7009          Undefined weak syms won't yet be marked as dynamic.  */
   7010       if (dyn && h->dynindx == -1 && !h->forced_local)
   7011 	{
   7012 	  if (!bfd_elf_link_record_dynamic_symbol (info, h))
   7013 	    return FALSE;
   7014 	}
   7015 
   7016       if (got_type == GOT_UNKNOWN)
   7017 	{
   7018 	}
   7019       else if (got_type == GOT_NORMAL)
   7020 	{
   7021 	  h->got.offset = htab->root.sgot->size;
   7022 	  htab->root.sgot->size += GOT_ENTRY_SIZE;
   7023 	  if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   7024 	       || h->root.type != bfd_link_hash_undefweak)
   7025 	      && (info->shared
   7026 		  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
   7027 	    {
   7028 	      htab->root.srelgot->size += RELOC_SIZE (htab);
   7029 	    }
   7030 	}
   7031       else
   7032 	{
   7033 	  int indx;
   7034 	  if (got_type & GOT_TLSDESC_GD)
   7035 	    {
   7036 	      eh->tlsdesc_got_jump_table_offset =
   7037 		(htab->root.sgotplt->size
   7038 		 - aarch64_compute_jump_table_size (htab));
   7039 	      htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
   7040 	      h->got.offset = (bfd_vma) - 2;
   7041 	    }
   7042 
   7043 	  if (got_type & GOT_TLS_GD)
   7044 	    {
   7045 	      h->got.offset = htab->root.sgot->size;
   7046 	      htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
   7047 	    }
   7048 
   7049 	  if (got_type & GOT_TLS_IE)
   7050 	    {
   7051 	      h->got.offset = htab->root.sgot->size;
   7052 	      htab->root.sgot->size += GOT_ENTRY_SIZE;
   7053 	    }
   7054 
   7055 	  indx = h && h->dynindx != -1 ? h->dynindx : 0;
   7056 	  if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   7057 	       || h->root.type != bfd_link_hash_undefweak)
   7058 	      && (info->shared
   7059 		  || indx != 0
   7060 		  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
   7061 	    {
   7062 	      if (got_type & GOT_TLSDESC_GD)
   7063 		{
   7064 		  htab->root.srelplt->size += RELOC_SIZE (htab);
   7065 		  /* Note reloc_count not incremented here!  We have
   7066 		     already adjusted reloc_count for this relocation
   7067 		     type.  */
   7068 
   7069 		  /* TLSDESC PLT is now needed, but not yet determined.  */
   7070 		  htab->tlsdesc_plt = (bfd_vma) - 1;
   7071 		}
   7072 
   7073 	      if (got_type & GOT_TLS_GD)
   7074 		htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
   7075 
   7076 	      if (got_type & GOT_TLS_IE)
   7077 		htab->root.srelgot->size += RELOC_SIZE (htab);
   7078 	    }
   7079 	}
   7080     }
   7081   else
   7082     {
   7083       h->got.offset = (bfd_vma) - 1;
   7084     }
   7085 
   7086   if (eh->dyn_relocs == NULL)
   7087     return TRUE;
   7088 
   7089   /* In the shared -Bsymbolic case, discard space allocated for
   7090      dynamic pc-relative relocs against symbols which turn out to be
   7091      defined in regular objects.  For the normal shared case, discard
   7092      space for pc-relative relocs that have become local due to symbol
   7093      visibility changes.  */
   7094 
   7095   if (info->shared)
   7096     {
   7097       /* Relocs that use pc_count are those that appear on a call
   7098          insn, or certain REL relocs that can generated via assembly.
   7099          We want calls to protected symbols to resolve directly to the
   7100          function rather than going via the plt.  If people want
   7101          function pointer comparisons to work as expected then they
   7102          should avoid writing weird assembly.  */
   7103       if (SYMBOL_CALLS_LOCAL (info, h))
   7104 	{
   7105 	  struct elf_dyn_relocs **pp;
   7106 
   7107 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
   7108 	    {
   7109 	      p->count -= p->pc_count;
   7110 	      p->pc_count = 0;
   7111 	      if (p->count == 0)
   7112 		*pp = p->next;
   7113 	      else
   7114 		pp = &p->next;
   7115 	    }
   7116 	}
   7117 
   7118       /* Also discard relocs on undefined weak syms with non-default
   7119          visibility.  */
   7120       if (eh->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
   7121 	{
   7122 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   7123 	    eh->dyn_relocs = NULL;
   7124 
   7125 	  /* Make sure undefined weak symbols are output as a dynamic
   7126 	     symbol in PIEs.  */
   7127 	  else if (h->dynindx == -1
   7128 		   && !h->forced_local
   7129 		   && !bfd_elf_link_record_dynamic_symbol (info, h))
   7130 	    return FALSE;
   7131 	}
   7132 
   7133     }
   7134   else if (ELIMINATE_COPY_RELOCS)
   7135     {
   7136       /* For the non-shared case, discard space for relocs against
   7137          symbols which turn out to need copy relocs or are not
   7138          dynamic.  */
   7139 
   7140       if (!h->non_got_ref
   7141 	  && ((h->def_dynamic
   7142 	       && !h->def_regular)
   7143 	      || (htab->root.dynamic_sections_created
   7144 		  && (h->root.type == bfd_link_hash_undefweak
   7145 		      || h->root.type == bfd_link_hash_undefined))))
   7146 	{
   7147 	  /* Make sure this symbol is output as a dynamic symbol.
   7148 	     Undefined weak syms won't yet be marked as dynamic.  */
   7149 	  if (h->dynindx == -1
   7150 	      && !h->forced_local
   7151 	      && !bfd_elf_link_record_dynamic_symbol (info, h))
   7152 	    return FALSE;
   7153 
   7154 	  /* If that succeeded, we know we'll be keeping all the
   7155 	     relocs.  */
   7156 	  if (h->dynindx != -1)
   7157 	    goto keep;
   7158 	}
   7159 
   7160       eh->dyn_relocs = NULL;
   7161 
   7162     keep:;
   7163     }
   7164 
   7165   /* Finally, allocate space.  */
   7166   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   7167     {
   7168       asection *sreloc;
   7169 
   7170       sreloc = elf_section_data (p->sec)->sreloc;
   7171 
   7172       BFD_ASSERT (sreloc != NULL);
   7173 
   7174       sreloc->size += p->count * RELOC_SIZE (htab);
   7175     }
   7176 
   7177   return TRUE;
   7178 }
   7179 
   7180 /* Allocate space in .plt, .got and associated reloc sections for
   7181    ifunc dynamic relocs.  */
   7182 
   7183 static bfd_boolean
   7184 elfNN_aarch64_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
   7185 					void *inf)
   7186 {
   7187   struct bfd_link_info *info;
   7188   struct elf_aarch64_link_hash_table *htab;
   7189   struct elf_aarch64_link_hash_entry *eh;
   7190 
   7191   /* An example of a bfd_link_hash_indirect symbol is versioned
   7192      symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
   7193      -> __gxx_personality_v0(bfd_link_hash_defined)
   7194 
   7195      There is no need to process bfd_link_hash_indirect symbols here
   7196      because we will also be presented with the concrete instance of
   7197      the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
   7198      called to copy all relevant data from the generic to the concrete
   7199      symbol instance.
   7200    */
   7201   if (h->root.type == bfd_link_hash_indirect)
   7202     return TRUE;
   7203 
   7204   if (h->root.type == bfd_link_hash_warning)
   7205     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   7206 
   7207   info = (struct bfd_link_info *) inf;
   7208   htab = elf_aarch64_hash_table (info);
   7209 
   7210   eh = (struct elf_aarch64_link_hash_entry *) h;
   7211 
   7212   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
   7213      here if it is defined and referenced in a non-shared object.  */
   7214   if (h->type == STT_GNU_IFUNC
   7215       && h->def_regular)
   7216     return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
   7217 					       &eh->dyn_relocs,
   7218 					       htab->plt_entry_size,
   7219 					       htab->plt_header_size,
   7220 					       GOT_ENTRY_SIZE);
   7221   return TRUE;
   7222 }
   7223 
   7224 /* Allocate space in .plt, .got and associated reloc sections for
   7225    local dynamic relocs.  */
   7226 
   7227 static bfd_boolean
   7228 elfNN_aarch64_allocate_local_dynrelocs (void **slot, void *inf)
   7229 {
   7230   struct elf_link_hash_entry *h
   7231     = (struct elf_link_hash_entry *) *slot;
   7232 
   7233   if (h->type != STT_GNU_IFUNC
   7234       || !h->def_regular
   7235       || !h->ref_regular
   7236       || !h->forced_local
   7237       || h->root.type != bfd_link_hash_defined)
   7238     abort ();
   7239 
   7240   return elfNN_aarch64_allocate_dynrelocs (h, inf);
   7241 }
   7242 
   7243 /* Allocate space in .plt, .got and associated reloc sections for
   7244    local ifunc dynamic relocs.  */
   7245 
   7246 static bfd_boolean
   7247 elfNN_aarch64_allocate_local_ifunc_dynrelocs (void **slot, void *inf)
   7248 {
   7249   struct elf_link_hash_entry *h
   7250     = (struct elf_link_hash_entry *) *slot;
   7251 
   7252   if (h->type != STT_GNU_IFUNC
   7253       || !h->def_regular
   7254       || !h->ref_regular
   7255       || !h->forced_local
   7256       || h->root.type != bfd_link_hash_defined)
   7257     abort ();
   7258 
   7259   return elfNN_aarch64_allocate_ifunc_dynrelocs (h, inf);
   7260 }
   7261 
   7262 /* This is the most important function of all . Innocuosly named
   7263    though !  */
   7264 static bfd_boolean
   7265 elfNN_aarch64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   7266 				     struct bfd_link_info *info)
   7267 {
   7268   struct elf_aarch64_link_hash_table *htab;
   7269   bfd *dynobj;
   7270   asection *s;
   7271   bfd_boolean relocs;
   7272   bfd *ibfd;
   7273 
   7274   htab = elf_aarch64_hash_table ((info));
   7275   dynobj = htab->root.dynobj;
   7276 
   7277   BFD_ASSERT (dynobj != NULL);
   7278 
   7279   if (htab->root.dynamic_sections_created)
   7280     {
   7281       if (info->executable)
   7282 	{
   7283 	  s = bfd_get_linker_section (dynobj, ".interp");
   7284 	  if (s == NULL)
   7285 	    abort ();
   7286 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   7287 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   7288 	}
   7289     }
   7290 
   7291   /* Set up .got offsets for local syms, and space for local dynamic
   7292      relocs.  */
   7293   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7294     {
   7295       struct elf_aarch64_local_symbol *locals = NULL;
   7296       Elf_Internal_Shdr *symtab_hdr;
   7297       asection *srel;
   7298       unsigned int i;
   7299 
   7300       if (!is_aarch64_elf (ibfd))
   7301 	continue;
   7302 
   7303       for (s = ibfd->sections; s != NULL; s = s->next)
   7304 	{
   7305 	  struct elf_dyn_relocs *p;
   7306 
   7307 	  for (p = (struct elf_dyn_relocs *)
   7308 	       (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
   7309 	    {
   7310 	      if (!bfd_is_abs_section (p->sec)
   7311 		  && bfd_is_abs_section (p->sec->output_section))
   7312 		{
   7313 		  /* Input section has been discarded, either because
   7314 		     it is a copy of a linkonce section or due to
   7315 		     linker script /DISCARD/, so we'll be discarding
   7316 		     the relocs too.  */
   7317 		}
   7318 	      else if (p->count != 0)
   7319 		{
   7320 		  srel = elf_section_data (p->sec)->sreloc;
   7321 		  srel->size += p->count * RELOC_SIZE (htab);
   7322 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   7323 		    info->flags |= DF_TEXTREL;
   7324 		}
   7325 	    }
   7326 	}
   7327 
   7328       locals = elf_aarch64_locals (ibfd);
   7329       if (!locals)
   7330 	continue;
   7331 
   7332       symtab_hdr = &elf_symtab_hdr (ibfd);
   7333       srel = htab->root.srelgot;
   7334       for (i = 0; i < symtab_hdr->sh_info; i++)
   7335 	{
   7336 	  locals[i].got_offset = (bfd_vma) - 1;
   7337 	  locals[i].tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
   7338 	  if (locals[i].got_refcount > 0)
   7339 	    {
   7340 	      unsigned got_type = locals[i].got_type;
   7341 	      if (got_type & GOT_TLSDESC_GD)
   7342 		{
   7343 		  locals[i].tlsdesc_got_jump_table_offset =
   7344 		    (htab->root.sgotplt->size
   7345 		     - aarch64_compute_jump_table_size (htab));
   7346 		  htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
   7347 		  locals[i].got_offset = (bfd_vma) - 2;
   7348 		}
   7349 
   7350 	      if (got_type & GOT_TLS_GD)
   7351 		{
   7352 		  locals[i].got_offset = htab->root.sgot->size;
   7353 		  htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
   7354 		}
   7355 
   7356 	      if (got_type & GOT_TLS_IE)
   7357 		{
   7358 		  locals[i].got_offset = htab->root.sgot->size;
   7359 		  htab->root.sgot->size += GOT_ENTRY_SIZE;
   7360 		}
   7361 
   7362 	      if (got_type == GOT_UNKNOWN)
   7363 		{
   7364 		}
   7365 
   7366 	      if (got_type == GOT_NORMAL)
   7367 		{
   7368 		}
   7369 
   7370 	      if (info->shared)
   7371 		{
   7372 		  if (got_type & GOT_TLSDESC_GD)
   7373 		    {
   7374 		      htab->root.srelplt->size += RELOC_SIZE (htab);
   7375 		      /* Note RELOC_COUNT not incremented here! */
   7376 		      htab->tlsdesc_plt = (bfd_vma) - 1;
   7377 		    }
   7378 
   7379 		  if (got_type & GOT_TLS_GD)
   7380 		    htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
   7381 
   7382 		  if (got_type & GOT_TLS_IE)
   7383 		    htab->root.srelgot->size += RELOC_SIZE (htab);
   7384 		}
   7385 	    }
   7386 	  else
   7387 	    {
   7388 	      locals[i].got_refcount = (bfd_vma) - 1;
   7389 	    }
   7390 	}
   7391     }
   7392 
   7393 
   7394   /* Allocate global sym .plt and .got entries, and space for global
   7395      sym dynamic relocs.  */
   7396   elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_dynrelocs,
   7397 			  info);
   7398 
   7399   /* Allocate global ifunc sym .plt and .got entries, and space for global
   7400      ifunc sym dynamic relocs.  */
   7401   elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_ifunc_dynrelocs,
   7402 			  info);
   7403 
   7404   /* Allocate .plt and .got entries, and space for local symbols.  */
   7405   htab_traverse (htab->loc_hash_table,
   7406 		 elfNN_aarch64_allocate_local_dynrelocs,
   7407 		 info);
   7408 
   7409   /* Allocate .plt and .got entries, and space for local ifunc symbols.  */
   7410   htab_traverse (htab->loc_hash_table,
   7411 		 elfNN_aarch64_allocate_local_ifunc_dynrelocs,
   7412 		 info);
   7413 
   7414   /* For every jump slot reserved in the sgotplt, reloc_count is
   7415      incremented.  However, when we reserve space for TLS descriptors,
   7416      it's not incremented, so in order to compute the space reserved
   7417      for them, it suffices to multiply the reloc count by the jump
   7418      slot size.  */
   7419 
   7420   if (htab->root.srelplt)
   7421     htab->sgotplt_jump_table_size = aarch64_compute_jump_table_size (htab);
   7422 
   7423   if (htab->tlsdesc_plt)
   7424     {
   7425       if (htab->root.splt->size == 0)
   7426 	htab->root.splt->size += PLT_ENTRY_SIZE;
   7427 
   7428       htab->tlsdesc_plt = htab->root.splt->size;
   7429       htab->root.splt->size += PLT_TLSDESC_ENTRY_SIZE;
   7430 
   7431       /* If we're not using lazy TLS relocations, don't generate the
   7432          GOT entry required.  */
   7433       if (!(info->flags & DF_BIND_NOW))
   7434 	{
   7435 	  htab->dt_tlsdesc_got = htab->root.sgot->size;
   7436 	  htab->root.sgot->size += GOT_ENTRY_SIZE;
   7437 	}
   7438     }
   7439 
   7440   /* Init mapping symbols information to use later to distingush between
   7441      code and data while scanning for errata.  */
   7442   if (htab->fix_erratum_835769 || htab->fix_erratum_843419)
   7443     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7444       {
   7445 	if (!is_aarch64_elf (ibfd))
   7446 	  continue;
   7447 	bfd_elfNN_aarch64_init_maps (ibfd);
   7448       }
   7449 
   7450   /* We now have determined the sizes of the various dynamic sections.
   7451      Allocate memory for them.  */
   7452   relocs = FALSE;
   7453   for (s = dynobj->sections; s != NULL; s = s->next)
   7454     {
   7455       if ((s->flags & SEC_LINKER_CREATED) == 0)
   7456 	continue;
   7457 
   7458       if (s == htab->root.splt
   7459 	  || s == htab->root.sgot
   7460 	  || s == htab->root.sgotplt
   7461 	  || s == htab->root.iplt
   7462 	  || s == htab->root.igotplt || s == htab->sdynbss)
   7463 	{
   7464 	  /* Strip this section if we don't need it; see the
   7465 	     comment below.  */
   7466 	}
   7467       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
   7468 	{
   7469 	  if (s->size != 0 && s != htab->root.srelplt)
   7470 	    relocs = TRUE;
   7471 
   7472 	  /* We use the reloc_count field as a counter if we need
   7473 	     to copy relocs into the output file.  */
   7474 	  if (s != htab->root.srelplt)
   7475 	    s->reloc_count = 0;
   7476 	}
   7477       else
   7478 	{
   7479 	  /* It's not one of our sections, so don't allocate space.  */
   7480 	  continue;
   7481 	}
   7482 
   7483       if (s->size == 0)
   7484 	{
   7485 	  /* If we don't need this section, strip it from the
   7486 	     output file.  This is mostly to handle .rela.bss and
   7487 	     .rela.plt.  We must create both sections in
   7488 	     create_dynamic_sections, because they must be created
   7489 	     before the linker maps input sections to output
   7490 	     sections.  The linker does that before
   7491 	     adjust_dynamic_symbol is called, and it is that
   7492 	     function which decides whether anything needs to go
   7493 	     into these sections.  */
   7494 
   7495 	  s->flags |= SEC_EXCLUDE;
   7496 	  continue;
   7497 	}
   7498 
   7499       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   7500 	continue;
   7501 
   7502       /* Allocate memory for the section contents.  We use bfd_zalloc
   7503          here in case unused entries are not reclaimed before the
   7504          section's contents are written out.  This should not happen,
   7505          but this way if it does, we get a R_AARCH64_NONE reloc instead
   7506          of garbage.  */
   7507       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
   7508       if (s->contents == NULL)
   7509 	return FALSE;
   7510     }
   7511 
   7512   if (htab->root.dynamic_sections_created)
   7513     {
   7514       /* Add some entries to the .dynamic section.  We fill in the
   7515          values later, in elfNN_aarch64_finish_dynamic_sections, but we
   7516          must add the entries now so that we get the correct size for
   7517          the .dynamic section.  The DT_DEBUG entry is filled in by the
   7518          dynamic linker and used by the debugger.  */
   7519 #define add_dynamic_entry(TAG, VAL)			\
   7520       _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   7521 
   7522       if (info->executable)
   7523 	{
   7524 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   7525 	    return FALSE;
   7526 	}
   7527 
   7528       if (htab->root.splt->size != 0)
   7529 	{
   7530 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   7531 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   7532 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   7533 	      || !add_dynamic_entry (DT_JMPREL, 0))
   7534 	    return FALSE;
   7535 
   7536 	  if (htab->tlsdesc_plt
   7537 	      && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
   7538 		  || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
   7539 	    return FALSE;
   7540 	}
   7541 
   7542       if (relocs)
   7543 	{
   7544 	  if (!add_dynamic_entry (DT_RELA, 0)
   7545 	      || !add_dynamic_entry (DT_RELASZ, 0)
   7546 	      || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
   7547 	    return FALSE;
   7548 
   7549 	  /* If any dynamic relocs apply to a read-only section,
   7550 	     then we need a DT_TEXTREL entry.  */
   7551 	  if ((info->flags & DF_TEXTREL) != 0)
   7552 	    {
   7553 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   7554 		return FALSE;
   7555 	    }
   7556 	}
   7557     }
   7558 #undef add_dynamic_entry
   7559 
   7560   return TRUE;
   7561 }
   7562 
   7563 static inline void
   7564 elf_aarch64_update_plt_entry (bfd *output_bfd,
   7565 			      bfd_reloc_code_real_type r_type,
   7566 			      bfd_byte *plt_entry, bfd_vma value)
   7567 {
   7568   reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (r_type);
   7569 
   7570   _bfd_aarch64_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
   7571 }
   7572 
   7573 static void
   7574 elfNN_aarch64_create_small_pltn_entry (struct elf_link_hash_entry *h,
   7575 				       struct elf_aarch64_link_hash_table
   7576 				       *htab, bfd *output_bfd,
   7577 				       struct bfd_link_info *info)
   7578 {
   7579   bfd_byte *plt_entry;
   7580   bfd_vma plt_index;
   7581   bfd_vma got_offset;
   7582   bfd_vma gotplt_entry_address;
   7583   bfd_vma plt_entry_address;
   7584   Elf_Internal_Rela rela;
   7585   bfd_byte *loc;
   7586   asection *plt, *gotplt, *relplt;
   7587 
   7588   /* When building a static executable, use .iplt, .igot.plt and
   7589      .rela.iplt sections for STT_GNU_IFUNC symbols.  */
   7590   if (htab->root.splt != NULL)
   7591     {
   7592       plt = htab->root.splt;
   7593       gotplt = htab->root.sgotplt;
   7594       relplt = htab->root.srelplt;
   7595     }
   7596   else
   7597     {
   7598       plt = htab->root.iplt;
   7599       gotplt = htab->root.igotplt;
   7600       relplt = htab->root.irelplt;
   7601     }
   7602 
   7603   /* Get the index in the procedure linkage table which
   7604      corresponds to this symbol.  This is the index of this symbol
   7605      in all the symbols for which we are making plt entries.  The
   7606      first entry in the procedure linkage table is reserved.
   7607 
   7608      Get the offset into the .got table of the entry that
   7609      corresponds to this function.	Each .got entry is GOT_ENTRY_SIZE
   7610      bytes. The first three are reserved for the dynamic linker.
   7611 
   7612      For static executables, we don't reserve anything.  */
   7613 
   7614   if (plt == htab->root.splt)
   7615     {
   7616       plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
   7617       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
   7618     }
   7619   else
   7620     {
   7621       plt_index = h->plt.offset / htab->plt_entry_size;
   7622       got_offset = plt_index * GOT_ENTRY_SIZE;
   7623     }
   7624 
   7625   plt_entry = plt->contents + h->plt.offset;
   7626   plt_entry_address = plt->output_section->vma
   7627     + plt->output_offset + h->plt.offset;
   7628   gotplt_entry_address = gotplt->output_section->vma +
   7629     gotplt->output_offset + got_offset;
   7630 
   7631   /* Copy in the boiler-plate for the PLTn entry.  */
   7632   memcpy (plt_entry, elfNN_aarch64_small_plt_entry, PLT_SMALL_ENTRY_SIZE);
   7633 
   7634   /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
   7635      ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
   7636   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
   7637 				plt_entry,
   7638 				PG (gotplt_entry_address) -
   7639 				PG (plt_entry_address));
   7640 
   7641   /* Fill in the lo12 bits for the load from the pltgot.  */
   7642   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
   7643 				plt_entry + 4,
   7644 				PG_OFFSET (gotplt_entry_address));
   7645 
   7646   /* Fill in the lo12 bits for the add from the pltgot entry.  */
   7647   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
   7648 				plt_entry + 8,
   7649 				PG_OFFSET (gotplt_entry_address));
   7650 
   7651   /* All the GOTPLT Entries are essentially initialized to PLT0.  */
   7652   bfd_put_NN (output_bfd,
   7653 	      plt->output_section->vma + plt->output_offset,
   7654 	      gotplt->contents + got_offset);
   7655 
   7656   rela.r_offset = gotplt_entry_address;
   7657 
   7658   if (h->dynindx == -1
   7659       || ((info->executable
   7660 	   || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   7661 	  && h->def_regular
   7662 	  && h->type == STT_GNU_IFUNC))
   7663     {
   7664       /* If an STT_GNU_IFUNC symbol is locally defined, generate
   7665 	 R_AARCH64_IRELATIVE instead of R_AARCH64_JUMP_SLOT.  */
   7666       rela.r_info = ELFNN_R_INFO (0, AARCH64_R (IRELATIVE));
   7667       rela.r_addend = (h->root.u.def.value
   7668 		       + h->root.u.def.section->output_section->vma
   7669 		       + h->root.u.def.section->output_offset);
   7670     }
   7671   else
   7672     {
   7673       /* Fill in the entry in the .rela.plt section.  */
   7674       rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (JUMP_SLOT));
   7675       rela.r_addend = 0;
   7676     }
   7677 
   7678   /* Compute the relocation entry to used based on PLT index and do
   7679      not adjust reloc_count. The reloc_count has already been adjusted
   7680      to account for this entry.  */
   7681   loc = relplt->contents + plt_index * RELOC_SIZE (htab);
   7682   bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   7683 }
   7684 
   7685 /* Size sections even though they're not dynamic.  We use it to setup
   7686    _TLS_MODULE_BASE_, if needed.  */
   7687 
   7688 static bfd_boolean
   7689 elfNN_aarch64_always_size_sections (bfd *output_bfd,
   7690 				    struct bfd_link_info *info)
   7691 {
   7692   asection *tls_sec;
   7693 
   7694   if (info->relocatable)
   7695     return TRUE;
   7696 
   7697   tls_sec = elf_hash_table (info)->tls_sec;
   7698 
   7699   if (tls_sec)
   7700     {
   7701       struct elf_link_hash_entry *tlsbase;
   7702 
   7703       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
   7704 				      "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
   7705 
   7706       if (tlsbase)
   7707 	{
   7708 	  struct bfd_link_hash_entry *h = NULL;
   7709 	  const struct elf_backend_data *bed =
   7710 	    get_elf_backend_data (output_bfd);
   7711 
   7712 	  if (!(_bfd_generic_link_add_one_symbol
   7713 		(info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
   7714 		 tls_sec, 0, NULL, FALSE, bed->collect, &h)))
   7715 	    return FALSE;
   7716 
   7717 	  tlsbase->type = STT_TLS;
   7718 	  tlsbase = (struct elf_link_hash_entry *) h;
   7719 	  tlsbase->def_regular = 1;
   7720 	  tlsbase->other = STV_HIDDEN;
   7721 	  (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
   7722 	}
   7723     }
   7724 
   7725   return TRUE;
   7726 }
   7727 
   7728 /* Finish up dynamic symbol handling.  We set the contents of various
   7729    dynamic sections here.  */
   7730 static bfd_boolean
   7731 elfNN_aarch64_finish_dynamic_symbol (bfd *output_bfd,
   7732 				     struct bfd_link_info *info,
   7733 				     struct elf_link_hash_entry *h,
   7734 				     Elf_Internal_Sym *sym)
   7735 {
   7736   struct elf_aarch64_link_hash_table *htab;
   7737   htab = elf_aarch64_hash_table (info);
   7738 
   7739   if (h->plt.offset != (bfd_vma) - 1)
   7740     {
   7741       asection *plt, *gotplt, *relplt;
   7742 
   7743       /* This symbol has an entry in the procedure linkage table.  Set
   7744          it up.  */
   7745 
   7746       /* When building a static executable, use .iplt, .igot.plt and
   7747 	 .rela.iplt sections for STT_GNU_IFUNC symbols.  */
   7748       if (htab->root.splt != NULL)
   7749 	{
   7750 	  plt = htab->root.splt;
   7751 	  gotplt = htab->root.sgotplt;
   7752 	  relplt = htab->root.srelplt;
   7753 	}
   7754       else
   7755 	{
   7756 	  plt = htab->root.iplt;
   7757 	  gotplt = htab->root.igotplt;
   7758 	  relplt = htab->root.irelplt;
   7759 	}
   7760 
   7761       /* This symbol has an entry in the procedure linkage table.  Set
   7762 	 it up.	 */
   7763       if ((h->dynindx == -1
   7764 	   && !((h->forced_local || info->executable)
   7765 		&& h->def_regular
   7766 		&& h->type == STT_GNU_IFUNC))
   7767 	  || plt == NULL
   7768 	  || gotplt == NULL
   7769 	  || relplt == NULL)
   7770 	abort ();
   7771 
   7772       elfNN_aarch64_create_small_pltn_entry (h, htab, output_bfd, info);
   7773       if (!h->def_regular)
   7774 	{
   7775 	  /* Mark the symbol as undefined, rather than as defined in
   7776 	     the .plt section.  Leave the value alone.  This is a clue
   7777 	     for the dynamic linker, to make function pointer
   7778 	     comparisons work between an application and shared
   7779 	     library.  */
   7780 	  sym->st_shndx = SHN_UNDEF;
   7781 	}
   7782     }
   7783 
   7784   if (h->got.offset != (bfd_vma) - 1
   7785       && elf_aarch64_hash_entry (h)->got_type == GOT_NORMAL)
   7786     {
   7787       Elf_Internal_Rela rela;
   7788       bfd_byte *loc;
   7789 
   7790       /* This symbol has an entry in the global offset table.  Set it
   7791          up.  */
   7792       if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
   7793 	abort ();
   7794 
   7795       rela.r_offset = (htab->root.sgot->output_section->vma
   7796 		       + htab->root.sgot->output_offset
   7797 		       + (h->got.offset & ~(bfd_vma) 1));
   7798 
   7799       if (h->def_regular
   7800 	  && h->type == STT_GNU_IFUNC)
   7801 	{
   7802 	  if (info->shared)
   7803 	    {
   7804 	      /* Generate R_AARCH64_GLOB_DAT.  */
   7805 	      goto do_glob_dat;
   7806 	    }
   7807 	  else
   7808 	    {
   7809 	      asection *plt;
   7810 
   7811 	      if (!h->pointer_equality_needed)
   7812 		abort ();
   7813 
   7814 	      /* For non-shared object, we can't use .got.plt, which
   7815 		 contains the real function address if we need pointer
   7816 		 equality.  We load the GOT entry with the PLT entry.  */
   7817 	      plt = htab->root.splt ? htab->root.splt : htab->root.iplt;
   7818 	      bfd_put_NN (output_bfd, (plt->output_section->vma
   7819 				       + plt->output_offset
   7820 				       + h->plt.offset),
   7821 			  htab->root.sgot->contents
   7822 			  + (h->got.offset & ~(bfd_vma) 1));
   7823 	      return TRUE;
   7824 	    }
   7825 	}
   7826       else if (info->shared && SYMBOL_REFERENCES_LOCAL (info, h))
   7827 	{
   7828 	  if (!h->def_regular)
   7829 	    return FALSE;
   7830 
   7831 	  BFD_ASSERT ((h->got.offset & 1) != 0);
   7832 	  rela.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
   7833 	  rela.r_addend = (h->root.u.def.value
   7834 			   + h->root.u.def.section->output_section->vma
   7835 			   + h->root.u.def.section->output_offset);
   7836 	}
   7837       else
   7838 	{
   7839 do_glob_dat:
   7840 	  BFD_ASSERT ((h->got.offset & 1) == 0);
   7841 	  bfd_put_NN (output_bfd, (bfd_vma) 0,
   7842 		      htab->root.sgot->contents + h->got.offset);
   7843 	  rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (GLOB_DAT));
   7844 	  rela.r_addend = 0;
   7845 	}
   7846 
   7847       loc = htab->root.srelgot->contents;
   7848       loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
   7849       bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   7850     }
   7851 
   7852   if (h->needs_copy)
   7853     {
   7854       Elf_Internal_Rela rela;
   7855       bfd_byte *loc;
   7856 
   7857       /* This symbol needs a copy reloc.  Set it up.  */
   7858 
   7859       if (h->dynindx == -1
   7860 	  || (h->root.type != bfd_link_hash_defined
   7861 	      && h->root.type != bfd_link_hash_defweak)
   7862 	  || htab->srelbss == NULL)
   7863 	abort ();
   7864 
   7865       rela.r_offset = (h->root.u.def.value
   7866 		       + h->root.u.def.section->output_section->vma
   7867 		       + h->root.u.def.section->output_offset);
   7868       rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (COPY));
   7869       rela.r_addend = 0;
   7870       loc = htab->srelbss->contents;
   7871       loc += htab->srelbss->reloc_count++ * RELOC_SIZE (htab);
   7872       bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   7873     }
   7874 
   7875   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
   7876      be NULL for local symbols.  */
   7877   if (sym != NULL
   7878       && (h == elf_hash_table (info)->hdynamic
   7879 	  || h == elf_hash_table (info)->hgot))
   7880     sym->st_shndx = SHN_ABS;
   7881 
   7882   return TRUE;
   7883 }
   7884 
   7885 /* Finish up local dynamic symbol handling.  We set the contents of
   7886    various dynamic sections here.  */
   7887 
   7888 static bfd_boolean
   7889 elfNN_aarch64_finish_local_dynamic_symbol (void **slot, void *inf)
   7890 {
   7891   struct elf_link_hash_entry *h
   7892     = (struct elf_link_hash_entry *) *slot;
   7893   struct bfd_link_info *info
   7894     = (struct bfd_link_info *) inf;
   7895 
   7896   return elfNN_aarch64_finish_dynamic_symbol (info->output_bfd,
   7897 					      info, h, NULL);
   7898 }
   7899 
   7900 static void
   7901 elfNN_aarch64_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
   7902 				     struct elf_aarch64_link_hash_table
   7903 				     *htab)
   7904 {
   7905   /* Fill in PLT0. Fixme:RR Note this doesn't distinguish between
   7906      small and large plts and at the minute just generates
   7907      the small PLT.  */
   7908 
   7909   /* PLT0 of the small PLT looks like this in ELF64 -
   7910      stp x16, x30, [sp, #-16]!		// Save the reloc and lr on stack.
   7911      adrp x16, PLT_GOT + 16		// Get the page base of the GOTPLT
   7912      ldr  x17, [x16, #:lo12:PLT_GOT+16] // Load the address of the
   7913 					// symbol resolver
   7914      add  x16, x16, #:lo12:PLT_GOT+16   // Load the lo12 bits of the
   7915 					// GOTPLT entry for this.
   7916      br   x17
   7917      PLT0 will be slightly different in ELF32 due to different got entry
   7918      size.
   7919    */
   7920   bfd_vma plt_got_2nd_ent;	/* Address of GOT[2].  */
   7921   bfd_vma plt_base;
   7922 
   7923 
   7924   memcpy (htab->root.splt->contents, elfNN_aarch64_small_plt0_entry,
   7925 	  PLT_ENTRY_SIZE);
   7926   elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize =
   7927     PLT_ENTRY_SIZE;
   7928 
   7929   plt_got_2nd_ent = (htab->root.sgotplt->output_section->vma
   7930 		  + htab->root.sgotplt->output_offset
   7931 		  + GOT_ENTRY_SIZE * 2);
   7932 
   7933   plt_base = htab->root.splt->output_section->vma +
   7934     htab->root.splt->output_offset;
   7935 
   7936   /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
   7937      ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
   7938   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
   7939 				htab->root.splt->contents + 4,
   7940 				PG (plt_got_2nd_ent) - PG (plt_base + 4));
   7941 
   7942   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
   7943 				htab->root.splt->contents + 8,
   7944 				PG_OFFSET (plt_got_2nd_ent));
   7945 
   7946   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
   7947 				htab->root.splt->contents + 12,
   7948 				PG_OFFSET (plt_got_2nd_ent));
   7949 }
   7950 
   7951 static bfd_boolean
   7952 elfNN_aarch64_finish_dynamic_sections (bfd *output_bfd,
   7953 				       struct bfd_link_info *info)
   7954 {
   7955   struct elf_aarch64_link_hash_table *htab;
   7956   bfd *dynobj;
   7957   asection *sdyn;
   7958 
   7959   htab = elf_aarch64_hash_table (info);
   7960   dynobj = htab->root.dynobj;
   7961   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   7962 
   7963   if (htab->root.dynamic_sections_created)
   7964     {
   7965       ElfNN_External_Dyn *dyncon, *dynconend;
   7966 
   7967       if (sdyn == NULL || htab->root.sgot == NULL)
   7968 	abort ();
   7969 
   7970       dyncon = (ElfNN_External_Dyn *) sdyn->contents;
   7971       dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
   7972       for (; dyncon < dynconend; dyncon++)
   7973 	{
   7974 	  Elf_Internal_Dyn dyn;
   7975 	  asection *s;
   7976 
   7977 	  bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
   7978 
   7979 	  switch (dyn.d_tag)
   7980 	    {
   7981 	    default:
   7982 	      continue;
   7983 
   7984 	    case DT_PLTGOT:
   7985 	      s = htab->root.sgotplt;
   7986 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   7987 	      break;
   7988 
   7989 	    case DT_JMPREL:
   7990 	      dyn.d_un.d_ptr = htab->root.srelplt->output_section->vma;
   7991 	      break;
   7992 
   7993 	    case DT_PLTRELSZ:
   7994 	      s = htab->root.srelplt;
   7995 	      dyn.d_un.d_val = s->size;
   7996 	      break;
   7997 
   7998 	    case DT_RELASZ:
   7999 	      /* The procedure linkage table relocs (DT_JMPREL) should
   8000 		 not be included in the overall relocs (DT_RELA).
   8001 		 Therefore, we override the DT_RELASZ entry here to
   8002 		 make it not include the JMPREL relocs.  Since the
   8003 		 linker script arranges for .rela.plt to follow all
   8004 		 other relocation sections, we don't have to worry
   8005 		 about changing the DT_RELA entry.  */
   8006 	      if (htab->root.srelplt != NULL)
   8007 		{
   8008 		  s = htab->root.srelplt;
   8009 		  dyn.d_un.d_val -= s->size;
   8010 		}
   8011 	      break;
   8012 
   8013 	    case DT_TLSDESC_PLT:
   8014 	      s = htab->root.splt;
   8015 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
   8016 		+ htab->tlsdesc_plt;
   8017 	      break;
   8018 
   8019 	    case DT_TLSDESC_GOT:
   8020 	      s = htab->root.sgot;
   8021 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
   8022 		+ htab->dt_tlsdesc_got;
   8023 	      break;
   8024 	    }
   8025 
   8026 	  bfd_elfNN_swap_dyn_out (output_bfd, &dyn, dyncon);
   8027 	}
   8028 
   8029     }
   8030 
   8031   /* Fill in the special first entry in the procedure linkage table.  */
   8032   if (htab->root.splt && htab->root.splt->size > 0)
   8033     {
   8034       elfNN_aarch64_init_small_plt0_entry (output_bfd, htab);
   8035 
   8036       elf_section_data (htab->root.splt->output_section)->
   8037 	this_hdr.sh_entsize = htab->plt_entry_size;
   8038 
   8039 
   8040       if (htab->tlsdesc_plt)
   8041 	{
   8042 	  bfd_put_NN (output_bfd, (bfd_vma) 0,
   8043 		      htab->root.sgot->contents + htab->dt_tlsdesc_got);
   8044 
   8045 	  memcpy (htab->root.splt->contents + htab->tlsdesc_plt,
   8046 		  elfNN_aarch64_tlsdesc_small_plt_entry,
   8047 		  sizeof (elfNN_aarch64_tlsdesc_small_plt_entry));
   8048 
   8049 	  {
   8050 	    bfd_vma adrp1_addr =
   8051 	      htab->root.splt->output_section->vma
   8052 	      + htab->root.splt->output_offset + htab->tlsdesc_plt + 4;
   8053 
   8054 	    bfd_vma adrp2_addr = adrp1_addr + 4;
   8055 
   8056 	    bfd_vma got_addr =
   8057 	      htab->root.sgot->output_section->vma
   8058 	      + htab->root.sgot->output_offset;
   8059 
   8060 	    bfd_vma pltgot_addr =
   8061 	      htab->root.sgotplt->output_section->vma
   8062 	      + htab->root.sgotplt->output_offset;
   8063 
   8064 	    bfd_vma dt_tlsdesc_got = got_addr + htab->dt_tlsdesc_got;
   8065 
   8066 	    bfd_byte *plt_entry =
   8067 	      htab->root.splt->contents + htab->tlsdesc_plt;
   8068 
   8069 	    /* adrp x2, DT_TLSDESC_GOT */
   8070 	    elf_aarch64_update_plt_entry (output_bfd,
   8071 					  BFD_RELOC_AARCH64_ADR_HI21_PCREL,
   8072 					  plt_entry + 4,
   8073 					  (PG (dt_tlsdesc_got)
   8074 					   - PG (adrp1_addr)));
   8075 
   8076 	    /* adrp x3, 0 */
   8077 	    elf_aarch64_update_plt_entry (output_bfd,
   8078 					  BFD_RELOC_AARCH64_ADR_HI21_PCREL,
   8079 					  plt_entry + 8,
   8080 					  (PG (pltgot_addr)
   8081 					   - PG (adrp2_addr)));
   8082 
   8083 	    /* ldr x2, [x2, #0] */
   8084 	    elf_aarch64_update_plt_entry (output_bfd,
   8085 					  BFD_RELOC_AARCH64_LDSTNN_LO12,
   8086 					  plt_entry + 12,
   8087 					  PG_OFFSET (dt_tlsdesc_got));
   8088 
   8089 	    /* add x3, x3, 0 */
   8090 	    elf_aarch64_update_plt_entry (output_bfd,
   8091 					  BFD_RELOC_AARCH64_ADD_LO12,
   8092 					  plt_entry + 16,
   8093 					  PG_OFFSET (pltgot_addr));
   8094 	  }
   8095 	}
   8096     }
   8097 
   8098   if (htab->root.sgotplt)
   8099     {
   8100       if (bfd_is_abs_section (htab->root.sgotplt->output_section))
   8101 	{
   8102 	  (*_bfd_error_handler)
   8103 	    (_("discarded output section: `%A'"), htab->root.sgotplt);
   8104 	  return FALSE;
   8105 	}
   8106 
   8107       /* Fill in the first three entries in the global offset table.  */
   8108       if (htab->root.sgotplt->size > 0)
   8109 	{
   8110 	  bfd_put_NN (output_bfd, (bfd_vma) 0, htab->root.sgotplt->contents);
   8111 
   8112 	  /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
   8113 	  bfd_put_NN (output_bfd,
   8114 		      (bfd_vma) 0,
   8115 		      htab->root.sgotplt->contents + GOT_ENTRY_SIZE);
   8116 	  bfd_put_NN (output_bfd,
   8117 		      (bfd_vma) 0,
   8118 		      htab->root.sgotplt->contents + GOT_ENTRY_SIZE * 2);
   8119 	}
   8120 
   8121       if (htab->root.sgot)
   8122 	{
   8123 	  if (htab->root.sgot->size > 0)
   8124 	    {
   8125 	      bfd_vma addr =
   8126 		sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
   8127 	      bfd_put_NN (output_bfd, addr, htab->root.sgot->contents);
   8128 	    }
   8129 	}
   8130 
   8131       elf_section_data (htab->root.sgotplt->output_section)->
   8132 	this_hdr.sh_entsize = GOT_ENTRY_SIZE;
   8133     }
   8134 
   8135   if (htab->root.sgot && htab->root.sgot->size > 0)
   8136     elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
   8137       = GOT_ENTRY_SIZE;
   8138 
   8139   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
   8140   htab_traverse (htab->loc_hash_table,
   8141 		 elfNN_aarch64_finish_local_dynamic_symbol,
   8142 		 info);
   8143 
   8144   return TRUE;
   8145 }
   8146 
   8147 /* Return address for Ith PLT stub in section PLT, for relocation REL
   8148    or (bfd_vma) -1 if it should not be included.  */
   8149 
   8150 static bfd_vma
   8151 elfNN_aarch64_plt_sym_val (bfd_vma i, const asection *plt,
   8152 			   const arelent *rel ATTRIBUTE_UNUSED)
   8153 {
   8154   return plt->vma + PLT_ENTRY_SIZE + i * PLT_SMALL_ENTRY_SIZE;
   8155 }
   8156 
   8157 
   8158 /* We use this so we can override certain functions
   8159    (though currently we don't).  */
   8160 
   8161 const struct elf_size_info elfNN_aarch64_size_info =
   8162 {
   8163   sizeof (ElfNN_External_Ehdr),
   8164   sizeof (ElfNN_External_Phdr),
   8165   sizeof (ElfNN_External_Shdr),
   8166   sizeof (ElfNN_External_Rel),
   8167   sizeof (ElfNN_External_Rela),
   8168   sizeof (ElfNN_External_Sym),
   8169   sizeof (ElfNN_External_Dyn),
   8170   sizeof (Elf_External_Note),
   8171   4,				/* Hash table entry size.  */
   8172   1,				/* Internal relocs per external relocs.  */
   8173   ARCH_SIZE,			/* Arch size.  */
   8174   LOG_FILE_ALIGN,		/* Log_file_align.  */
   8175   ELFCLASSNN, EV_CURRENT,
   8176   bfd_elfNN_write_out_phdrs,
   8177   bfd_elfNN_write_shdrs_and_ehdr,
   8178   bfd_elfNN_checksum_contents,
   8179   bfd_elfNN_write_relocs,
   8180   bfd_elfNN_swap_symbol_in,
   8181   bfd_elfNN_swap_symbol_out,
   8182   bfd_elfNN_slurp_reloc_table,
   8183   bfd_elfNN_slurp_symbol_table,
   8184   bfd_elfNN_swap_dyn_in,
   8185   bfd_elfNN_swap_dyn_out,
   8186   bfd_elfNN_swap_reloc_in,
   8187   bfd_elfNN_swap_reloc_out,
   8188   bfd_elfNN_swap_reloca_in,
   8189   bfd_elfNN_swap_reloca_out
   8190 };
   8191 
   8192 #define ELF_ARCH			bfd_arch_aarch64
   8193 #define ELF_MACHINE_CODE		EM_AARCH64
   8194 #define ELF_MAXPAGESIZE			0x10000
   8195 #define ELF_MINPAGESIZE			0x1000
   8196 #define ELF_COMMONPAGESIZE		0x1000
   8197 
   8198 #define bfd_elfNN_close_and_cleanup             \
   8199   elfNN_aarch64_close_and_cleanup
   8200 
   8201 #define bfd_elfNN_bfd_free_cached_info          \
   8202   elfNN_aarch64_bfd_free_cached_info
   8203 
   8204 #define bfd_elfNN_bfd_is_target_special_symbol	\
   8205   elfNN_aarch64_is_target_special_symbol
   8206 
   8207 #define bfd_elfNN_bfd_link_hash_table_create    \
   8208   elfNN_aarch64_link_hash_table_create
   8209 
   8210 #define bfd_elfNN_bfd_merge_private_bfd_data	\
   8211   elfNN_aarch64_merge_private_bfd_data
   8212 
   8213 #define bfd_elfNN_bfd_print_private_bfd_data	\
   8214   elfNN_aarch64_print_private_bfd_data
   8215 
   8216 #define bfd_elfNN_bfd_reloc_type_lookup		\
   8217   elfNN_aarch64_reloc_type_lookup
   8218 
   8219 #define bfd_elfNN_bfd_reloc_name_lookup		\
   8220   elfNN_aarch64_reloc_name_lookup
   8221 
   8222 #define bfd_elfNN_bfd_set_private_flags		\
   8223   elfNN_aarch64_set_private_flags
   8224 
   8225 #define bfd_elfNN_find_inliner_info		\
   8226   elfNN_aarch64_find_inliner_info
   8227 
   8228 #define bfd_elfNN_find_nearest_line		\
   8229   elfNN_aarch64_find_nearest_line
   8230 
   8231 #define bfd_elfNN_mkobject			\
   8232   elfNN_aarch64_mkobject
   8233 
   8234 #define bfd_elfNN_new_section_hook		\
   8235   elfNN_aarch64_new_section_hook
   8236 
   8237 #define elf_backend_adjust_dynamic_symbol	\
   8238   elfNN_aarch64_adjust_dynamic_symbol
   8239 
   8240 #define elf_backend_always_size_sections	\
   8241   elfNN_aarch64_always_size_sections
   8242 
   8243 #define elf_backend_check_relocs		\
   8244   elfNN_aarch64_check_relocs
   8245 
   8246 #define elf_backend_copy_indirect_symbol	\
   8247   elfNN_aarch64_copy_indirect_symbol
   8248 
   8249 /* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
   8250    to them in our hash.  */
   8251 #define elf_backend_create_dynamic_sections	\
   8252   elfNN_aarch64_create_dynamic_sections
   8253 
   8254 #define elf_backend_init_index_section		\
   8255   _bfd_elf_init_2_index_sections
   8256 
   8257 #define elf_backend_finish_dynamic_sections	\
   8258   elfNN_aarch64_finish_dynamic_sections
   8259 
   8260 #define elf_backend_finish_dynamic_symbol	\
   8261   elfNN_aarch64_finish_dynamic_symbol
   8262 
   8263 #define elf_backend_gc_sweep_hook		\
   8264   elfNN_aarch64_gc_sweep_hook
   8265 
   8266 #define elf_backend_object_p			\
   8267   elfNN_aarch64_object_p
   8268 
   8269 #define elf_backend_output_arch_local_syms      \
   8270   elfNN_aarch64_output_arch_local_syms
   8271 
   8272 #define elf_backend_plt_sym_val			\
   8273   elfNN_aarch64_plt_sym_val
   8274 
   8275 #define elf_backend_post_process_headers	\
   8276   elfNN_aarch64_post_process_headers
   8277 
   8278 #define elf_backend_relocate_section		\
   8279   elfNN_aarch64_relocate_section
   8280 
   8281 #define elf_backend_reloc_type_class		\
   8282   elfNN_aarch64_reloc_type_class
   8283 
   8284 #define elf_backend_section_from_shdr		\
   8285   elfNN_aarch64_section_from_shdr
   8286 
   8287 #define elf_backend_size_dynamic_sections	\
   8288   elfNN_aarch64_size_dynamic_sections
   8289 
   8290 #define elf_backend_size_info			\
   8291   elfNN_aarch64_size_info
   8292 
   8293 #define elf_backend_write_section		\
   8294   elfNN_aarch64_write_section
   8295 
   8296 #define elf_backend_can_refcount       1
   8297 #define elf_backend_can_gc_sections    1
   8298 #define elf_backend_plt_readonly       1
   8299 #define elf_backend_want_got_plt       1
   8300 #define elf_backend_want_plt_sym       0
   8301 #define elf_backend_may_use_rel_p      0
   8302 #define elf_backend_may_use_rela_p     1
   8303 #define elf_backend_default_use_rela_p 1
   8304 #define elf_backend_rela_normal        1
   8305 #define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
   8306 #define elf_backend_default_execstack  0
   8307 
   8308 #undef  elf_backend_obj_attrs_section
   8309 #define elf_backend_obj_attrs_section		".ARM.attributes"
   8310 
   8311 #include "elfNN-target.h"
   8312